drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 2 | ** 2004 April 6 |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame^] | 12 | ** $Id: btree.c,v 1.500 2008/08/22 12:57:09 drh Exp $ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 13 | ** |
| 14 | ** This file implements a external (disk-based) database using BTrees. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 15 | ** See the header comment on "btreeInt.h" for additional information. |
| 16 | ** Including a description of file format and an overview of operation. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 17 | */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 18 | #include "btreeInt.h" |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 19 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 20 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 21 | ** The header string that appears at the beginning of every |
| 22 | ** SQLite database. |
drh | 556b2a2 | 2005-06-14 16:04:05 +0000 | [diff] [blame] | 23 | */ |
drh | 556b2a2 | 2005-06-14 16:04:05 +0000 | [diff] [blame] | 24 | static const char zMagicHeader[] = SQLITE_FILE_HEADER; |
drh | 08ed44e | 2001-04-29 23:32:55 +0000 | [diff] [blame] | 25 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 26 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 27 | ** Set this global variable to 1 to enable tracing using the TRACE |
| 28 | ** macro. |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 29 | */ |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 30 | #if 0 |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 31 | int sqlite3BtreeTrace=0; /* True to enable tracing */ |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 32 | # define TRACE(X) if(sqlite3BtreeTrace){printf X;fflush(stdout);} |
| 33 | #else |
| 34 | # define TRACE(X) |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 35 | #endif |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 36 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 37 | |
| 38 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 39 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 40 | /* |
| 41 | ** A flag to indicate whether or not shared cache is enabled. Also, |
| 42 | ** a list of BtShared objects that are eligible for participation |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 43 | ** in shared cache. The variables have file scope during normal builds, |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 44 | ** but the test harness needs to access these variables so we make them |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 45 | ** global for test builds. |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 46 | */ |
| 47 | #ifdef SQLITE_TEST |
| 48 | BtShared *sqlite3SharedCacheList = 0; |
| 49 | int sqlite3SharedCacheEnabled = 0; |
| 50 | #else |
| 51 | static BtShared *sqlite3SharedCacheList = 0; |
| 52 | static int sqlite3SharedCacheEnabled = 0; |
| 53 | #endif |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 54 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| 55 | |
| 56 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 57 | /* |
| 58 | ** Enable or disable the shared pager and schema features. |
| 59 | ** |
| 60 | ** This routine has no effect on existing database connections. |
| 61 | ** The shared cache setting effects only future calls to |
| 62 | ** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2(). |
| 63 | */ |
| 64 | int sqlite3_enable_shared_cache(int enable){ |
| 65 | sqlite3SharedCacheEnabled = enable; |
| 66 | return SQLITE_OK; |
| 67 | } |
| 68 | #endif |
| 69 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 70 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 71 | /* |
drh | 66cbd15 | 2004-09-01 16:12:25 +0000 | [diff] [blame] | 72 | ** Forward declaration |
| 73 | */ |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 74 | static int checkReadLocks(Btree*, Pgno, BtCursor*, i64); |
drh | 66cbd15 | 2004-09-01 16:12:25 +0000 | [diff] [blame] | 75 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 76 | |
| 77 | #ifdef SQLITE_OMIT_SHARED_CACHE |
| 78 | /* |
| 79 | ** The functions queryTableLock(), lockTable() and unlockAllTables() |
| 80 | ** manipulate entries in the BtShared.pLock linked list used to store |
| 81 | ** shared-cache table level locks. If the library is compiled with the |
| 82 | ** shared-cache feature disabled, then there is only ever one user |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 83 | ** of each BtShared structure and so this locking is not necessary. |
| 84 | ** So define the lock related functions as no-ops. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 85 | */ |
| 86 | #define queryTableLock(a,b,c) SQLITE_OK |
| 87 | #define lockTable(a,b,c) SQLITE_OK |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 88 | #define unlockAllTables(a) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 89 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 90 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 91 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 92 | /* |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 93 | ** Query to see if btree handle p may obtain a lock of type eLock |
| 94 | ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return |
| 95 | ** SQLITE_OK if the lock may be obtained (by calling lockTable()), or |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 96 | ** SQLITE_LOCKED if not. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 97 | */ |
| 98 | static int queryTableLock(Btree *p, Pgno iTab, u8 eLock){ |
| 99 | BtShared *pBt = p->pBt; |
| 100 | BtLock *pIter; |
| 101 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 102 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 103 | assert( eLock==READ_LOCK || eLock==WRITE_LOCK ); |
| 104 | assert( p->db!=0 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 105 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 106 | /* This is a no-op if the shared-cache is not enabled */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 107 | if( !p->sharable ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 108 | return SQLITE_OK; |
| 109 | } |
| 110 | |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 111 | /* If some other connection is holding an exclusive lock, the |
| 112 | ** requested lock may not be obtained. |
| 113 | */ |
| 114 | if( pBt->pExclusive && pBt->pExclusive!=p ){ |
| 115 | return SQLITE_LOCKED; |
| 116 | } |
| 117 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 118 | /* This (along with lockTable()) is where the ReadUncommitted flag is |
| 119 | ** dealt with. If the caller is querying for a read-lock and the flag is |
| 120 | ** set, it is unconditionally granted - even if there are write-locks |
| 121 | ** on the table. If a write-lock is requested, the ReadUncommitted flag |
| 122 | ** is not considered. |
| 123 | ** |
| 124 | ** In function lockTable(), if a read-lock is demanded and the |
| 125 | ** ReadUncommitted flag is set, no entry is added to the locks list |
| 126 | ** (BtShared.pLock). |
| 127 | ** |
| 128 | ** To summarize: If the ReadUncommitted flag is set, then read cursors do |
| 129 | ** not create or respect table locks. The locking procedure for a |
| 130 | ** write-cursor does not change. |
| 131 | */ |
| 132 | if( |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 133 | 0==(p->db->flags&SQLITE_ReadUncommitted) || |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 134 | eLock==WRITE_LOCK || |
drh | 47ded16 | 2006-01-06 01:42:58 +0000 | [diff] [blame] | 135 | iTab==MASTER_ROOT |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 136 | ){ |
| 137 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 138 | if( pIter->pBtree!=p && pIter->iTable==iTab && |
| 139 | (pIter->eLock!=eLock || eLock!=READ_LOCK) ){ |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 140 | return SQLITE_LOCKED; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 141 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | return SQLITE_OK; |
| 145 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 146 | #endif /* !SQLITE_OMIT_SHARED_CACHE */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 147 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 148 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 149 | /* |
| 150 | ** Add a lock on the table with root-page iTable to the shared-btree used |
| 151 | ** by Btree handle p. Parameter eLock must be either READ_LOCK or |
| 152 | ** WRITE_LOCK. |
| 153 | ** |
| 154 | ** SQLITE_OK is returned if the lock is added successfully. SQLITE_BUSY and |
| 155 | ** SQLITE_NOMEM may also be returned. |
| 156 | */ |
| 157 | static int lockTable(Btree *p, Pgno iTable, u8 eLock){ |
| 158 | BtShared *pBt = p->pBt; |
| 159 | BtLock *pLock = 0; |
| 160 | BtLock *pIter; |
| 161 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 162 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 163 | assert( eLock==READ_LOCK || eLock==WRITE_LOCK ); |
| 164 | assert( p->db!=0 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 165 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 166 | /* This is a no-op if the shared-cache is not enabled */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 167 | if( !p->sharable ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 168 | return SQLITE_OK; |
| 169 | } |
| 170 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 171 | assert( SQLITE_OK==queryTableLock(p, iTable, eLock) ); |
| 172 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 173 | /* If the read-uncommitted flag is set and a read-lock is requested, |
| 174 | ** return early without adding an entry to the BtShared.pLock list. See |
| 175 | ** comment in function queryTableLock() for more info on handling |
| 176 | ** the ReadUncommitted flag. |
| 177 | */ |
| 178 | if( |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 179 | (p->db->flags&SQLITE_ReadUncommitted) && |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 180 | (eLock==READ_LOCK) && |
drh | 47ded16 | 2006-01-06 01:42:58 +0000 | [diff] [blame] | 181 | iTable!=MASTER_ROOT |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 182 | ){ |
| 183 | return SQLITE_OK; |
| 184 | } |
| 185 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 186 | /* First search the list for an existing lock on this table. */ |
| 187 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 188 | if( pIter->iTable==iTable && pIter->pBtree==p ){ |
| 189 | pLock = pIter; |
| 190 | break; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /* If the above search did not find a BtLock struct associating Btree p |
| 195 | ** with table iTable, allocate one and link it into the list. |
| 196 | */ |
| 197 | if( !pLock ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 198 | pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock)); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 199 | if( !pLock ){ |
| 200 | return SQLITE_NOMEM; |
| 201 | } |
| 202 | pLock->iTable = iTable; |
| 203 | pLock->pBtree = p; |
| 204 | pLock->pNext = pBt->pLock; |
| 205 | pBt->pLock = pLock; |
| 206 | } |
| 207 | |
| 208 | /* Set the BtLock.eLock variable to the maximum of the current lock |
| 209 | ** and the requested lock. This means if a write-lock was already held |
| 210 | ** and a read-lock requested, we don't incorrectly downgrade the lock. |
| 211 | */ |
| 212 | assert( WRITE_LOCK>READ_LOCK ); |
danielk1977 | 5118b91 | 2005-12-30 16:31:53 +0000 | [diff] [blame] | 213 | if( eLock>pLock->eLock ){ |
| 214 | pLock->eLock = eLock; |
| 215 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 216 | |
| 217 | return SQLITE_OK; |
| 218 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 219 | #endif /* !SQLITE_OMIT_SHARED_CACHE */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 220 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 221 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 222 | /* |
| 223 | ** Release all the table locks (locks obtained via calls to the lockTable() |
| 224 | ** procedure) held by Btree handle p. |
| 225 | */ |
| 226 | static void unlockAllTables(Btree *p){ |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 227 | BtShared *pBt = p->pBt; |
| 228 | BtLock **ppIter = &pBt->pLock; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 229 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 230 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 231 | assert( p->sharable || 0==*ppIter ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 232 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 233 | while( *ppIter ){ |
| 234 | BtLock *pLock = *ppIter; |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 235 | assert( pBt->pExclusive==0 || pBt->pExclusive==pLock->pBtree ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 236 | if( pLock->pBtree==p ){ |
| 237 | *ppIter = pLock->pNext; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 238 | sqlite3_free(pLock); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 239 | }else{ |
| 240 | ppIter = &pLock->pNext; |
| 241 | } |
| 242 | } |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 243 | |
| 244 | if( pBt->pExclusive==p ){ |
| 245 | pBt->pExclusive = 0; |
| 246 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 247 | } |
| 248 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| 249 | |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 250 | static void releasePage(MemPage *pPage); /* Forward reference */ |
| 251 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 252 | /* |
| 253 | ** Verify that the cursor holds a mutex on the BtShared |
| 254 | */ |
| 255 | #ifndef NDEBUG |
| 256 | static int cursorHoldsMutex(BtCursor *p){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 257 | return sqlite3_mutex_held(p->pBt->mutex); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 258 | } |
| 259 | #endif |
| 260 | |
| 261 | |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 262 | #ifndef SQLITE_OMIT_INCRBLOB |
| 263 | /* |
| 264 | ** Invalidate the overflow page-list cache for cursor pCur, if any. |
| 265 | */ |
| 266 | static void invalidateOverflowCache(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 267 | assert( cursorHoldsMutex(pCur) ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 268 | sqlite3_free(pCur->aOverflow); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 269 | pCur->aOverflow = 0; |
| 270 | } |
| 271 | |
| 272 | /* |
| 273 | ** Invalidate the overflow page-list cache for all cursors opened |
| 274 | ** on the shared btree structure pBt. |
| 275 | */ |
| 276 | static void invalidateAllOverflowCache(BtShared *pBt){ |
| 277 | BtCursor *p; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 278 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 279 | for(p=pBt->pCursor; p; p=p->pNext){ |
| 280 | invalidateOverflowCache(p); |
| 281 | } |
| 282 | } |
| 283 | #else |
| 284 | #define invalidateOverflowCache(x) |
| 285 | #define invalidateAllOverflowCache(x) |
| 286 | #endif |
| 287 | |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 288 | /* |
| 289 | ** Save the current cursor position in the variables BtCursor.nKey |
| 290 | ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK. |
| 291 | */ |
| 292 | static int saveCursorPosition(BtCursor *pCur){ |
| 293 | int rc; |
| 294 | |
| 295 | assert( CURSOR_VALID==pCur->eState ); |
| 296 | assert( 0==pCur->pKey ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 297 | assert( cursorHoldsMutex(pCur) ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 298 | |
| 299 | rc = sqlite3BtreeKeySize(pCur, &pCur->nKey); |
| 300 | |
| 301 | /* If this is an intKey table, then the above call to BtreeKeySize() |
| 302 | ** stores the integer key in pCur->nKey. In this case this value is |
| 303 | ** all that is required. Otherwise, if pCur is not open on an intKey |
| 304 | ** table, then malloc space for and store the pCur->nKey bytes of key |
| 305 | ** data. |
| 306 | */ |
| 307 | if( rc==SQLITE_OK && 0==pCur->pPage->intKey){ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 308 | void *pKey = sqlite3Malloc(pCur->nKey); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 309 | if( pKey ){ |
| 310 | rc = sqlite3BtreeKey(pCur, 0, pCur->nKey, pKey); |
| 311 | if( rc==SQLITE_OK ){ |
| 312 | pCur->pKey = pKey; |
| 313 | }else{ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 314 | sqlite3_free(pKey); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 315 | } |
| 316 | }else{ |
| 317 | rc = SQLITE_NOMEM; |
| 318 | } |
| 319 | } |
| 320 | assert( !pCur->pPage->intKey || !pCur->pKey ); |
| 321 | |
| 322 | if( rc==SQLITE_OK ){ |
| 323 | releasePage(pCur->pPage); |
| 324 | pCur->pPage = 0; |
| 325 | pCur->eState = CURSOR_REQUIRESEEK; |
| 326 | } |
| 327 | |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 328 | invalidateOverflowCache(pCur); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 329 | return rc; |
| 330 | } |
| 331 | |
| 332 | /* |
| 333 | ** Save the positions of all cursors except pExcept open on the table |
| 334 | ** with root-page iRoot. Usually, this is called just before cursor |
| 335 | ** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()). |
| 336 | */ |
| 337 | static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){ |
| 338 | BtCursor *p; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 339 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 340 | assert( pExcept==0 || pExcept->pBt==pBt ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 341 | for(p=pBt->pCursor; p; p=p->pNext){ |
| 342 | if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) && |
| 343 | p->eState==CURSOR_VALID ){ |
| 344 | int rc = saveCursorPosition(p); |
| 345 | if( SQLITE_OK!=rc ){ |
| 346 | return rc; |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | return SQLITE_OK; |
| 351 | } |
| 352 | |
| 353 | /* |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 354 | ** Clear the current cursor position. |
| 355 | */ |
| 356 | static void clearCursorPosition(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 357 | assert( cursorHoldsMutex(pCur) ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 358 | sqlite3_free(pCur->pKey); |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 359 | pCur->pKey = 0; |
| 360 | pCur->eState = CURSOR_INVALID; |
| 361 | } |
| 362 | |
| 363 | /* |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 364 | ** Restore the cursor to the position it was in (or as close to as possible) |
| 365 | ** when saveCursorPosition() was called. Note that this call deletes the |
| 366 | ** saved position info stored by saveCursorPosition(), so there can be |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 367 | ** at most one effective restoreCursorPosition() call after each |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 368 | ** saveCursorPosition(). |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 369 | */ |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 370 | int sqlite3BtreeRestoreCursorPosition(BtCursor *pCur){ |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 371 | int rc; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 372 | assert( cursorHoldsMutex(pCur) ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 373 | assert( pCur->eState>=CURSOR_REQUIRESEEK ); |
| 374 | if( pCur->eState==CURSOR_FAULT ){ |
| 375 | return pCur->skip; |
| 376 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 377 | pCur->eState = CURSOR_INVALID; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 378 | rc = sqlite3BtreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skip); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 379 | if( rc==SQLITE_OK ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 380 | sqlite3_free(pCur->pKey); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 381 | pCur->pKey = 0; |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 382 | assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 383 | } |
| 384 | return rc; |
| 385 | } |
| 386 | |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 387 | #define restoreCursorPosition(p) \ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 388 | (p->eState>=CURSOR_REQUIRESEEK ? \ |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 389 | sqlite3BtreeRestoreCursorPosition(p) : \ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 390 | SQLITE_OK) |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 391 | |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 392 | /* |
| 393 | ** Determine whether or not a cursor has moved from the position it |
| 394 | ** was last placed at. Cursor can move when the row they are pointing |
| 395 | ** at is deleted out from under them. |
| 396 | ** |
| 397 | ** This routine returns an error code if something goes wrong. The |
| 398 | ** integer *pHasMoved is set to one if the cursor has moved and 0 if not. |
| 399 | */ |
| 400 | int sqlite3BtreeCursorHasMoved(BtCursor *pCur, int *pHasMoved){ |
| 401 | int rc; |
| 402 | |
| 403 | rc = restoreCursorPosition(pCur); |
| 404 | if( rc ){ |
| 405 | *pHasMoved = 1; |
| 406 | return rc; |
| 407 | } |
| 408 | if( pCur->eState!=CURSOR_VALID || pCur->skip!=0 ){ |
| 409 | *pHasMoved = 1; |
| 410 | }else{ |
| 411 | *pHasMoved = 0; |
| 412 | } |
| 413 | return SQLITE_OK; |
| 414 | } |
| 415 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 416 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 417 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 418 | ** Given a page number of a regular database page, return the page |
| 419 | ** number for the pointer-map page that contains the entry for the |
| 420 | ** input page number. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 421 | */ |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 422 | static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 423 | int nPagesPerMapPage, iPtrMap, ret; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 424 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 425 | nPagesPerMapPage = (pBt->usableSize/5)+1; |
| 426 | iPtrMap = (pgno-2)/nPagesPerMapPage; |
| 427 | ret = (iPtrMap*nPagesPerMapPage) + 2; |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 428 | if( ret==PENDING_BYTE_PAGE(pBt) ){ |
| 429 | ret++; |
| 430 | } |
| 431 | return ret; |
| 432 | } |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 433 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 434 | /* |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 435 | ** Write an entry into the pointer map. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 436 | ** |
| 437 | ** This routine updates the pointer map entry for page number 'key' |
| 438 | ** so that it maps to type 'eType' and parent page number 'pgno'. |
| 439 | ** An error code is returned if something goes wrong, otherwise SQLITE_OK. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 440 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 441 | static int ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 442 | DbPage *pDbPage; /* The pointer map page */ |
| 443 | u8 *pPtrmap; /* The pointer map data */ |
| 444 | Pgno iPtrmap; /* The pointer map page number */ |
| 445 | int offset; /* Offset in pointer map page */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 446 | int rc; |
| 447 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 448 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 449 | /* The master-journal page number must never be used as a pointer map page */ |
| 450 | assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) ); |
| 451 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 452 | assert( pBt->autoVacuum ); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 453 | if( key==0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 454 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 455 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 456 | iPtrmap = PTRMAP_PAGENO(pBt, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 457 | rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 458 | if( rc!=SQLITE_OK ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 459 | return rc; |
| 460 | } |
danielk1977 | 8c666b1 | 2008-07-18 09:34:57 +0000 | [diff] [blame] | 461 | offset = PTRMAP_PTROFFSET(iPtrmap, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 462 | pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 463 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 464 | if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){ |
| 465 | TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent)); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 466 | rc = sqlite3PagerWrite(pDbPage); |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 467 | if( rc==SQLITE_OK ){ |
| 468 | pPtrmap[offset] = eType; |
| 469 | put4byte(&pPtrmap[offset+1], parent); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 470 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 471 | } |
| 472 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 473 | sqlite3PagerUnref(pDbPage); |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 474 | return rc; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | /* |
| 478 | ** Read an entry from the pointer map. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 479 | ** |
| 480 | ** This routine retrieves the pointer map entry for page 'key', writing |
| 481 | ** the type and parent page number to *pEType and *pPgno respectively. |
| 482 | ** An error code is returned if something goes wrong, otherwise SQLITE_OK. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 483 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 484 | static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 485 | DbPage *pDbPage; /* The pointer map page */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 486 | int iPtrmap; /* Pointer map page index */ |
| 487 | u8 *pPtrmap; /* Pointer map page data */ |
| 488 | int offset; /* Offset of entry in pointer map */ |
| 489 | int rc; |
| 490 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 491 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 492 | |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 493 | iPtrmap = PTRMAP_PAGENO(pBt, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 494 | rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 495 | if( rc!=0 ){ |
| 496 | return rc; |
| 497 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 498 | pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 499 | |
danielk1977 | 8c666b1 | 2008-07-18 09:34:57 +0000 | [diff] [blame] | 500 | offset = PTRMAP_PTROFFSET(iPtrmap, key); |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 501 | assert( pEType!=0 ); |
| 502 | *pEType = pPtrmap[offset]; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 503 | if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 504 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 505 | sqlite3PagerUnref(pDbPage); |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 506 | if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 507 | return SQLITE_OK; |
| 508 | } |
| 509 | |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 510 | #else /* if defined SQLITE_OMIT_AUTOVACUUM */ |
| 511 | #define ptrmapPut(w,x,y,z) SQLITE_OK |
| 512 | #define ptrmapGet(w,x,y,z) SQLITE_OK |
| 513 | #define ptrmapPutOvfl(y,z) SQLITE_OK |
| 514 | #endif |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 515 | |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 516 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 517 | ** Given a btree page and a cell index (0 means the first cell on |
| 518 | ** the page, 1 means the second cell, and so forth) return a pointer |
| 519 | ** to the cell content. |
| 520 | ** |
| 521 | ** This routine works only for pages that do not contain overflow cells. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 522 | */ |
drh | 1688c86 | 2008-07-18 02:44:17 +0000 | [diff] [blame] | 523 | #define findCell(P,I) \ |
| 524 | ((P)->aData + ((P)->maskPage & get2byte(&(P)->aData[(P)->cellOffset+2*(I)]))) |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 525 | |
| 526 | /* |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 527 | ** This a more complex version of findCell() that works for |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 528 | ** pages that do contain overflow cells. See insert |
| 529 | */ |
| 530 | static u8 *findOverflowCell(MemPage *pPage, int iCell){ |
| 531 | int i; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 532 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 533 | for(i=pPage->nOverflow-1; i>=0; i--){ |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 534 | int k; |
| 535 | struct _OvflCell *pOvfl; |
| 536 | pOvfl = &pPage->aOvfl[i]; |
| 537 | k = pOvfl->idx; |
| 538 | if( k<=iCell ){ |
| 539 | if( k==iCell ){ |
| 540 | return pOvfl->pCell; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 541 | } |
| 542 | iCell--; |
| 543 | } |
| 544 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 545 | return findCell(pPage, iCell); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | /* |
| 549 | ** Parse a cell content block and fill in the CellInfo structure. There |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 550 | ** are two versions of this function. sqlite3BtreeParseCell() takes a |
| 551 | ** cell index as the second argument and sqlite3BtreeParseCellPtr() |
| 552 | ** takes a pointer to the body of the cell as its second argument. |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 553 | ** |
| 554 | ** Within this file, the parseCell() macro can be called instead of |
| 555 | ** sqlite3BtreeParseCellPtr(). Using some compilers, this will be faster. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 556 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 557 | void sqlite3BtreeParseCellPtr( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 558 | MemPage *pPage, /* Page containing the cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 559 | u8 *pCell, /* Pointer to the cell text. */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 560 | CellInfo *pInfo /* Fill in this structure */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 561 | ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 562 | int n; /* Number bytes in cell content header */ |
| 563 | u32 nPayload; /* Number of bytes of cell payload */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 564 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 565 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 566 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 567 | pInfo->pCell = pCell; |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 568 | assert( pPage->leaf==0 || pPage->leaf==1 ); |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 569 | n = pPage->childPtrSize; |
| 570 | assert( n==4-4*pPage->leaf ); |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 571 | if( pPage->intKey ){ |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 572 | if( pPage->hasData ){ |
| 573 | n += getVarint32(&pCell[n], nPayload); |
| 574 | }else{ |
| 575 | nPayload = 0; |
| 576 | } |
| 577 | n += getVarint(&pCell[n], (u64*)&pInfo->nKey); |
| 578 | pInfo->nData = nPayload; |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 579 | }else{ |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 580 | pInfo->nData = 0; |
| 581 | n += getVarint32(&pCell[n], nPayload); |
| 582 | pInfo->nKey = nPayload; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 583 | } |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 584 | pInfo->nPayload = nPayload; |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 585 | pInfo->nHeader = n; |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 586 | if( likely(nPayload<=pPage->maxLocal) ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 587 | /* This is the (easy) common case where the entire payload fits |
| 588 | ** on the local page. No overflow is required. |
| 589 | */ |
| 590 | int nSize; /* Total size of cell content in bytes */ |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 591 | nSize = nPayload + n; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 592 | pInfo->nLocal = nPayload; |
| 593 | pInfo->iOverflow = 0; |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 594 | if( (nSize & ~3)==0 ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 595 | nSize = 4; /* Minimum cell size is 4 */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 596 | } |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 597 | pInfo->nSize = nSize; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 598 | }else{ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 599 | /* If the payload will not fit completely on the local page, we have |
| 600 | ** to decide how much to store locally and how much to spill onto |
| 601 | ** overflow pages. The strategy is to minimize the amount of unused |
| 602 | ** space on overflow pages while keeping the amount of local storage |
| 603 | ** in between minLocal and maxLocal. |
| 604 | ** |
| 605 | ** Warning: changing the way overflow payload is distributed in any |
| 606 | ** way will result in an incompatible file format. |
| 607 | */ |
| 608 | int minLocal; /* Minimum amount of payload held locally */ |
| 609 | int maxLocal; /* Maximum amount of payload held locally */ |
| 610 | int surplus; /* Overflow payload available for local storage */ |
| 611 | |
| 612 | minLocal = pPage->minLocal; |
| 613 | maxLocal = pPage->maxLocal; |
| 614 | surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 615 | if( surplus <= maxLocal ){ |
| 616 | pInfo->nLocal = surplus; |
| 617 | }else{ |
| 618 | pInfo->nLocal = minLocal; |
| 619 | } |
| 620 | pInfo->iOverflow = pInfo->nLocal + n; |
| 621 | pInfo->nSize = pInfo->iOverflow + 4; |
| 622 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 623 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 624 | #define parseCell(pPage, iCell, pInfo) \ |
| 625 | sqlite3BtreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo)) |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 626 | void sqlite3BtreeParseCell( |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 627 | MemPage *pPage, /* Page containing the cell */ |
| 628 | int iCell, /* The cell index. First cell is 0 */ |
| 629 | CellInfo *pInfo /* Fill in this structure */ |
| 630 | ){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 631 | parseCell(pPage, iCell, pInfo); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 632 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 633 | |
| 634 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 635 | ** Compute the total number of bytes that a Cell needs in the cell |
| 636 | ** data area of the btree-page. The return number includes the cell |
| 637 | ** data header and the local payload, but not any overflow page or |
| 638 | ** the space used by the cell pointer. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 639 | */ |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 640 | #ifndef NDEBUG |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 641 | static u16 cellSize(MemPage *pPage, int iCell){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 642 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 643 | sqlite3BtreeParseCell(pPage, iCell, &info); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 644 | return info.nSize; |
| 645 | } |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 646 | #endif |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 647 | static u16 cellSizePtr(MemPage *pPage, u8 *pCell){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 648 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 649 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 650 | return info.nSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 651 | } |
| 652 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 653 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 654 | /* |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 655 | ** If the cell pCell, part of page pPage contains a pointer |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 656 | ** to an overflow page, insert an entry into the pointer-map |
| 657 | ** for the overflow page. |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 658 | */ |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 659 | static int ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell){ |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 660 | CellInfo info; |
| 661 | assert( pCell!=0 ); |
| 662 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
| 663 | assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); |
| 664 | if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ |
| 665 | Pgno ovfl = get4byte(&pCell[info.iOverflow]); |
| 666 | return ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 667 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 668 | return SQLITE_OK; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 669 | } |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 670 | /* |
| 671 | ** If the cell with index iCell on page pPage contains a pointer |
| 672 | ** to an overflow page, insert an entry into the pointer-map |
| 673 | ** for the overflow page. |
| 674 | */ |
| 675 | static int ptrmapPutOvfl(MemPage *pPage, int iCell){ |
| 676 | u8 *pCell; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 677 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 678 | pCell = findOverflowCell(pPage, iCell); |
| 679 | return ptrmapPutOvflPtr(pPage, pCell); |
| 680 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 681 | #endif |
| 682 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 683 | |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 684 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 685 | ** Defragment the page given. All Cells are moved to the |
drh | 3a4a2d4 | 2005-11-24 14:24:28 +0000 | [diff] [blame] | 686 | ** end of the page and all free space is collected into one |
| 687 | ** big FreeBlk that occurs in between the header and cell |
drh | 31beae9 | 2005-11-24 14:34:36 +0000 | [diff] [blame] | 688 | ** pointer array and the cell content area. |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 689 | */ |
danielk1977 | 474b7cc | 2008-07-09 11:49:46 +0000 | [diff] [blame] | 690 | static void defragmentPage(MemPage *pPage){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 691 | int i; /* Loop counter */ |
| 692 | int pc; /* Address of a i-th cell */ |
| 693 | int addr; /* Offset of first byte after cell pointer array */ |
| 694 | int hdr; /* Offset to the page header */ |
| 695 | int size; /* Size of a cell */ |
| 696 | int usableSize; /* Number of usable bytes on a page */ |
| 697 | int cellOffset; /* Offset to the cell pointer array */ |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame^] | 698 | int cbrk; /* Offset to the cell content area */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 699 | int nCell; /* Number of cells on the page */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 700 | unsigned char *data; /* The page data */ |
| 701 | unsigned char *temp; /* Temp area for cell content */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 702 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 703 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 704 | assert( pPage->pBt!=0 ); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 705 | assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 706 | assert( pPage->nOverflow==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 707 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 26b7994 | 2007-11-28 16:19:56 +0000 | [diff] [blame] | 708 | temp = sqlite3PagerTempSpace(pPage->pBt->pPager); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 709 | data = pPage->aData; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 710 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 711 | cellOffset = pPage->cellOffset; |
| 712 | nCell = pPage->nCell; |
| 713 | assert( nCell==get2byte(&data[hdr+3]) ); |
| 714 | usableSize = pPage->pBt->usableSize; |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame^] | 715 | cbrk = get2byte(&data[hdr+5]); |
| 716 | memcpy(&temp[cbrk], &data[cbrk], usableSize - cbrk); |
| 717 | cbrk = usableSize; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 718 | for(i=0; i<nCell; i++){ |
| 719 | u8 *pAddr; /* The i-th cell pointer */ |
| 720 | pAddr = &data[cellOffset + i*2]; |
| 721 | pc = get2byte(pAddr); |
| 722 | assert( pc<pPage->pBt->usableSize ); |
| 723 | size = cellSizePtr(pPage, &temp[pc]); |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame^] | 724 | cbrk -= size; |
| 725 | memcpy(&data[cbrk], &temp[pc], size); |
| 726 | put2byte(pAddr, cbrk); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 727 | } |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame^] | 728 | assert( cbrk>=cellOffset+2*nCell ); |
| 729 | put2byte(&data[hdr+5], cbrk); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 730 | data[hdr+1] = 0; |
| 731 | data[hdr+2] = 0; |
| 732 | data[hdr+7] = 0; |
| 733 | addr = cellOffset+2*nCell; |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame^] | 734 | memset(&data[addr], 0, cbrk-addr); |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 735 | } |
| 736 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 737 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 738 | ** Allocate nByte bytes of space on a page. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 739 | ** |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 740 | ** Return the index into pPage->aData[] of the first byte of |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 741 | ** the new allocation. The caller guarantees that there is enough |
| 742 | ** space. This routine will never fail. |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 743 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 744 | ** If the page contains nBytes of free space but does not contain |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 745 | ** nBytes of contiguous free space, then this routine automatically |
| 746 | ** calls defragementPage() to consolidate all free space before |
| 747 | ** allocating the new chunk. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 748 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 749 | static int allocateSpace(MemPage *pPage, int nByte){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 750 | int addr, pc, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 751 | int size; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 752 | int nFrag; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 753 | int top; |
| 754 | int nCell; |
| 755 | int cellOffset; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 756 | unsigned char *data; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 757 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 758 | data = pPage->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 759 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 760 | assert( pPage->pBt ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 761 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 762 | assert( nByte>=0 ); /* Minimum cell size is 4 */ |
| 763 | assert( pPage->nFree>=nByte ); |
| 764 | assert( pPage->nOverflow==0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 765 | pPage->nFree -= nByte; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 766 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 767 | |
| 768 | nFrag = data[hdr+7]; |
| 769 | if( nFrag<60 ){ |
| 770 | /* Search the freelist looking for a slot big enough to satisfy the |
| 771 | ** space request. */ |
| 772 | addr = hdr+1; |
| 773 | while( (pc = get2byte(&data[addr]))>0 ){ |
| 774 | size = get2byte(&data[pc+2]); |
| 775 | if( size>=nByte ){ |
| 776 | if( size<nByte+4 ){ |
| 777 | memcpy(&data[addr], &data[pc], 2); |
| 778 | data[hdr+7] = nFrag + size - nByte; |
| 779 | return pc; |
| 780 | }else{ |
| 781 | put2byte(&data[pc+2], size-nByte); |
| 782 | return pc + size - nByte; |
| 783 | } |
| 784 | } |
| 785 | addr = pc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 786 | } |
| 787 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 788 | |
| 789 | /* Allocate memory from the gap in between the cell pointer array |
| 790 | ** and the cell content area. |
| 791 | */ |
| 792 | top = get2byte(&data[hdr+5]); |
| 793 | nCell = get2byte(&data[hdr+3]); |
| 794 | cellOffset = pPage->cellOffset; |
| 795 | if( nFrag>=60 || cellOffset + 2*nCell > top - nByte ){ |
danielk1977 | 474b7cc | 2008-07-09 11:49:46 +0000 | [diff] [blame] | 796 | defragmentPage(pPage); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 797 | top = get2byte(&data[hdr+5]); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 798 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 799 | top -= nByte; |
| 800 | assert( cellOffset + 2*nCell <= top ); |
| 801 | put2byte(&data[hdr+5], top); |
| 802 | return top; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 806 | ** Return a section of the pPage->aData to the freelist. |
| 807 | ** The first byte of the new free block is pPage->aDisk[start] |
| 808 | ** and the size of the block is "size" bytes. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 809 | ** |
| 810 | ** Most of the effort here is involved in coalesing adjacent |
| 811 | ** free blocks into a single big free block. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 812 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 813 | static void freeSpace(MemPage *pPage, int start, int size){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 814 | int addr, pbegin, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 815 | unsigned char *data = pPage->aData; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 816 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 817 | assert( pPage->pBt!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 818 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 819 | assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) ); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 820 | assert( (start + size)<=pPage->pBt->usableSize ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 821 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 822 | assert( size>=0 ); /* Minimum cell size is 4 */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 823 | |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 824 | #ifdef SQLITE_SECURE_DELETE |
| 825 | /* Overwrite deleted information with zeros when the SECURE_DELETE |
| 826 | ** option is enabled at compile-time */ |
| 827 | memset(&data[start], 0, size); |
| 828 | #endif |
| 829 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 830 | /* Add the space back into the linked list of freeblocks */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 831 | hdr = pPage->hdrOffset; |
| 832 | addr = hdr + 1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 833 | while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 834 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 835 | assert( pbegin>addr ); |
| 836 | addr = pbegin; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 837 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 838 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 839 | assert( pbegin>addr || pbegin==0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 840 | put2byte(&data[addr], start); |
| 841 | put2byte(&data[start], pbegin); |
| 842 | put2byte(&data[start+2], size); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 843 | pPage->nFree += size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 844 | |
| 845 | /* Coalesce adjacent free blocks */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 846 | addr = pPage->hdrOffset + 1; |
| 847 | while( (pbegin = get2byte(&data[addr]))>0 ){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 848 | int pnext, psize; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 849 | assert( pbegin>addr ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 850 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 851 | pnext = get2byte(&data[pbegin]); |
| 852 | psize = get2byte(&data[pbegin+2]); |
| 853 | if( pbegin + psize + 3 >= pnext && pnext>0 ){ |
| 854 | int frag = pnext - (pbegin+psize); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 855 | assert( frag<=data[pPage->hdrOffset+7] ); |
| 856 | data[pPage->hdrOffset+7] -= frag; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 857 | put2byte(&data[pbegin], get2byte(&data[pnext])); |
| 858 | put2byte(&data[pbegin+2], pnext+get2byte(&data[pnext+2])-pbegin); |
| 859 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 860 | addr = pbegin; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 861 | } |
| 862 | } |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 863 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 864 | /* If the cell content area begins with a freeblock, remove it. */ |
| 865 | if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){ |
| 866 | int top; |
| 867 | pbegin = get2byte(&data[hdr+1]); |
| 868 | memcpy(&data[hdr+1], &data[pbegin], 2); |
| 869 | top = get2byte(&data[hdr+5]); |
| 870 | put2byte(&data[hdr+5], top + get2byte(&data[pbegin+2])); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 871 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 875 | ** Decode the flags byte (the first byte of the header) for a page |
| 876 | ** and initialize fields of the MemPage structure accordingly. |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 877 | ** |
| 878 | ** Only the following combinations are supported. Anything different |
| 879 | ** indicates a corrupt database files: |
| 880 | ** |
| 881 | ** PTF_ZERODATA |
| 882 | ** PTF_ZERODATA | PTF_LEAF |
| 883 | ** PTF_LEAFDATA | PTF_INTKEY |
| 884 | ** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 885 | */ |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 886 | static int decodeFlags(MemPage *pPage, int flagByte){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 887 | BtShared *pBt; /* A copy of pPage->pBt */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 888 | |
| 889 | assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 890 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 891 | pPage->leaf = flagByte>>3; assert( PTF_LEAF == 1<<3 ); |
| 892 | flagByte &= ~PTF_LEAF; |
| 893 | pPage->childPtrSize = 4-4*pPage->leaf; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 894 | pBt = pPage->pBt; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 895 | if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){ |
| 896 | pPage->intKey = 1; |
| 897 | pPage->hasData = pPage->leaf; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 898 | pPage->maxLocal = pBt->maxLeaf; |
| 899 | pPage->minLocal = pBt->minLeaf; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 900 | }else if( flagByte==PTF_ZERODATA ){ |
| 901 | pPage->intKey = 0; |
| 902 | pPage->hasData = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 903 | pPage->maxLocal = pBt->maxLocal; |
| 904 | pPage->minLocal = pBt->minLocal; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 905 | }else{ |
| 906 | return SQLITE_CORRUPT_BKPT; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 907 | } |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 908 | return SQLITE_OK; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 912 | ** Initialize the auxiliary information for a disk block. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 913 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 914 | ** The pParent parameter must be a pointer to the MemPage which |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 915 | ** is the parent of the page being initialized. The root of a |
| 916 | ** BTree has no parent and so for that page, pParent==NULL. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 917 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 918 | ** Return SQLITE_OK on success. If we see that the page does |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 919 | ** not contain a well-formed database page, then return |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 920 | ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not |
| 921 | ** guarantee that the page is well-formed. It only shows that |
| 922 | ** we failed to detect any corruption. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 923 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 924 | int sqlite3BtreeInitPage( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 925 | MemPage *pPage, /* The page to be initialized */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 926 | MemPage *pParent /* The parent. Might be NULL */ |
| 927 | ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 928 | int pc; /* Address of a freeblock within pPage->aData[] */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 929 | int hdr; /* Offset to beginning of page header */ |
| 930 | u8 *data; /* Equal to pPage->aData */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 931 | BtShared *pBt; /* The main btree structure */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 932 | int usableSize; /* Amount of usable space on each page */ |
| 933 | int cellOffset; /* Offset from start of page to first cell pointer */ |
| 934 | int nFree; /* Number of unused bytes on the page */ |
| 935 | int top; /* First byte of the cell content area */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 936 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 937 | pBt = pPage->pBt; |
| 938 | assert( pBt!=0 ); |
| 939 | assert( pParent==0 || pParent->pBt==pBt ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 940 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 941 | assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 942 | assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) ); |
| 943 | assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) ); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 944 | if( pPage->pParent!=pParent && (pPage->pParent!=0 || pPage->isInit) ){ |
| 945 | /* The parent page should never change unless the file is corrupt */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 946 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 947 | } |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 948 | if( pPage->isInit ) return SQLITE_OK; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 949 | if( pPage->pParent==0 && pParent!=0 ){ |
| 950 | pPage->pParent = pParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 951 | sqlite3PagerRef(pParent->pDbPage); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 952 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 953 | hdr = pPage->hdrOffset; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 954 | data = pPage->aData; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 955 | if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT; |
drh | 1688c86 | 2008-07-18 02:44:17 +0000 | [diff] [blame] | 956 | assert( pBt->pageSize>=512 && pBt->pageSize<=32768 ); |
| 957 | pPage->maskPage = pBt->pageSize - 1; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 958 | pPage->nOverflow = 0; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 959 | pPage->idxShift = 0; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 960 | usableSize = pBt->usableSize; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 961 | pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf; |
| 962 | top = get2byte(&data[hdr+5]); |
| 963 | pPage->nCell = get2byte(&data[hdr+3]); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 964 | if( pPage->nCell>MX_CELL(pBt) ){ |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 965 | /* To many cells for a single page. The page must be corrupt */ |
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 | } |
| 968 | if( pPage->nCell==0 && pParent!=0 && pParent->pgno!=1 ){ |
| 969 | /* All pages must have at least one cell, except for root pages */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 970 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 971 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 972 | |
| 973 | /* Compute the total free space on the page */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 974 | pc = get2byte(&data[hdr+1]); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 975 | nFree = data[hdr+7] + top - (cellOffset + 2*pPage->nCell); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 976 | while( pc>0 ){ |
| 977 | int next, size; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 978 | if( pc>usableSize-4 ){ |
| 979 | /* Free block is off the page */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 980 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 981 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 982 | next = get2byte(&data[pc]); |
| 983 | size = get2byte(&data[pc+2]); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 984 | if( next>0 && next<=pc+size+3 ){ |
| 985 | /* Free blocks must be in accending order */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 986 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 987 | } |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 988 | nFree += size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 989 | pc = next; |
| 990 | } |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 991 | pPage->nFree = nFree; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 992 | if( nFree>=usableSize ){ |
| 993 | /* Free space cannot exceed total page size */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 994 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 995 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 996 | |
drh | 1688c86 | 2008-07-18 02:44:17 +0000 | [diff] [blame] | 997 | #if 0 |
| 998 | /* Check that all the offsets in the cell offset array are within range. |
| 999 | ** |
| 1000 | ** Omitting this consistency check and using the pPage->maskPage mask |
| 1001 | ** to prevent overrunning the page buffer in findCell() results in a |
| 1002 | ** 2.5% performance gain. |
| 1003 | */ |
| 1004 | { |
| 1005 | u8 *pOff; /* Iterator used to check all cell offsets are in range */ |
| 1006 | u8 *pEnd; /* Pointer to end of cell offset array */ |
| 1007 | u8 mask; /* Mask of bits that must be zero in MSB of cell offsets */ |
| 1008 | mask = ~(((u8)(pBt->pageSize>>8))-1); |
| 1009 | pEnd = &data[cellOffset + pPage->nCell*2]; |
| 1010 | for(pOff=&data[cellOffset]; pOff!=pEnd && !((*pOff)&mask); pOff+=2); |
| 1011 | if( pOff!=pEnd ){ |
| 1012 | return SQLITE_CORRUPT_BKPT; |
| 1013 | } |
danielk1977 | e16535f | 2008-06-11 18:15:29 +0000 | [diff] [blame] | 1014 | } |
drh | 1688c86 | 2008-07-18 02:44:17 +0000 | [diff] [blame] | 1015 | #endif |
danielk1977 | e16535f | 2008-06-11 18:15:29 +0000 | [diff] [blame] | 1016 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1017 | pPage->isInit = 1; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1018 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1022 | ** Set up a raw page so that it looks like a database page holding |
| 1023 | ** no entries. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1024 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1025 | static void zeroPage(MemPage *pPage, int flags){ |
| 1026 | unsigned char *data = pPage->aData; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1027 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1028 | int hdr = pPage->hdrOffset; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1029 | int first; |
| 1030 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1031 | assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1032 | assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); |
| 1033 | assert( sqlite3PagerGetData(pPage->pDbPage) == data ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1034 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1035 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 1af4a6e | 2008-07-18 03:32:51 +0000 | [diff] [blame] | 1036 | /*memset(&data[hdr], 0, pBt->usableSize - hdr);*/ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1037 | data[hdr] = flags; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1038 | first = hdr + 8 + 4*((flags&PTF_LEAF)==0); |
| 1039 | memset(&data[hdr+1], 0, 4); |
| 1040 | data[hdr+7] = 0; |
| 1041 | put2byte(&data[hdr+5], pBt->usableSize); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1042 | pPage->nFree = pBt->usableSize - first; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1043 | decodeFlags(pPage, flags); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1044 | pPage->hdrOffset = hdr; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1045 | pPage->cellOffset = first; |
| 1046 | pPage->nOverflow = 0; |
drh | 1688c86 | 2008-07-18 02:44:17 +0000 | [diff] [blame] | 1047 | assert( pBt->pageSize>=512 && pBt->pageSize<=32768 ); |
| 1048 | pPage->maskPage = pBt->pageSize - 1; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1049 | pPage->idxShift = 0; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1050 | pPage->nCell = 0; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1051 | pPage->isInit = 1; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1055 | ** Get a page from the pager. Initialize the MemPage.pBt and |
| 1056 | ** MemPage.aData elements if needed. |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 1057 | ** |
| 1058 | ** If the noContent flag is set, it means that we do not care about |
| 1059 | ** the content of the page at this time. So do not go to the disk |
| 1060 | ** to fetch the content. Just fill in the content with zeros for now. |
| 1061 | ** If in the future we call sqlite3PagerWrite() on this page, that |
| 1062 | ** means we have started to be concerned about content and the disk |
| 1063 | ** read should occur at that point. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1064 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1065 | int sqlite3BtreeGetPage( |
| 1066 | BtShared *pBt, /* The btree */ |
| 1067 | Pgno pgno, /* Number of the page to fetch */ |
| 1068 | MemPage **ppPage, /* Return the page in this parameter */ |
| 1069 | int noContent /* Do not load page content if true */ |
| 1070 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1071 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1072 | MemPage *pPage; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1073 | DbPage *pDbPage; |
| 1074 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1075 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 1076 | rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1077 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1078 | pPage = (MemPage *)sqlite3PagerGetExtra(pDbPage); |
| 1079 | pPage->aData = sqlite3PagerGetData(pDbPage); |
| 1080 | pPage->pDbPage = pDbPage; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1081 | pPage->pBt = pBt; |
| 1082 | pPage->pgno = pgno; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1083 | pPage->hdrOffset = pPage->pgno==1 ? 100 : 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1084 | *ppPage = pPage; |
| 1085 | return SQLITE_OK; |
| 1086 | } |
| 1087 | |
| 1088 | /* |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1089 | ** Get a page from the pager and initialize it. This routine |
| 1090 | ** is just a convenience wrapper around separate calls to |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1091 | ** sqlite3BtreeGetPage() and sqlite3BtreeInitPage(). |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1092 | */ |
| 1093 | static int getAndInitPage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1094 | BtShared *pBt, /* The database file */ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1095 | Pgno pgno, /* Number of the page to get */ |
| 1096 | MemPage **ppPage, /* Write the page pointer here */ |
| 1097 | MemPage *pParent /* Parent of the page */ |
| 1098 | ){ |
| 1099 | int rc; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1100 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1101 | if( pgno==0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1102 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1103 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1104 | rc = sqlite3BtreeGetPage(pBt, pgno, ppPage, 0); |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 1105 | if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1106 | rc = sqlite3BtreeInitPage(*ppPage, pParent); |
danielk1977 | 43e377a | 2008-05-05 12:09:32 +0000 | [diff] [blame] | 1107 | if( rc!=SQLITE_OK ){ |
| 1108 | releasePage(*ppPage); |
| 1109 | *ppPage = 0; |
| 1110 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1111 | } |
| 1112 | return rc; |
| 1113 | } |
| 1114 | |
| 1115 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1116 | ** Release a MemPage. This should be called once for each prior |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1117 | ** call to sqlite3BtreeGetPage. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1118 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1119 | static void releasePage(MemPage *pPage){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1120 | if( pPage ){ |
| 1121 | assert( pPage->aData ); |
| 1122 | assert( pPage->pBt ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1123 | assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); |
| 1124 | assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1125 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1126 | sqlite3PagerUnref(pPage->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1131 | ** This routine is called when the reference count for a page |
| 1132 | ** reaches zero. We need to unref the pParent pointer when that |
| 1133 | ** happens. |
| 1134 | */ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 1135 | static void pageDestructor(DbPage *pData){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1136 | MemPage *pPage; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1137 | pPage = (MemPage *)sqlite3PagerGetExtra(pData); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 1138 | if( pPage ){ |
| 1139 | assert( pPage->isInit==0 || sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 1140 | if( pPage->pParent ){ |
| 1141 | MemPage *pParent = pPage->pParent; |
| 1142 | assert( pParent->pBt==pPage->pBt ); |
| 1143 | pPage->pParent = 0; |
| 1144 | releasePage(pParent); |
| 1145 | } |
| 1146 | pPage->isInit = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1151 | ** During a rollback, when the pager reloads information into the cache |
| 1152 | ** so that the cache is restored to its original state at the start of |
| 1153 | ** the transaction, for each page restored this routine is called. |
| 1154 | ** |
| 1155 | ** This routine needs to reset the extra data section at the end of the |
| 1156 | ** page to agree with the restored data. |
| 1157 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1158 | static void pageReinit(DbPage *pData, int pageSize){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1159 | MemPage *pPage; |
| 1160 | assert( (pageSize & 7)==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1161 | pPage = (MemPage *)sqlite3PagerGetExtra(pData); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1162 | if( pPage->isInit ){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1163 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1164 | pPage->isInit = 0; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1165 | sqlite3BtreeInitPage(pPage, pPage->pParent); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | /* |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1170 | ** Invoke the busy handler for a btree. |
| 1171 | */ |
| 1172 | static int sqlite3BtreeInvokeBusyHandler(void *pArg, int n){ |
| 1173 | BtShared *pBt = (BtShared*)pArg; |
| 1174 | assert( pBt->db ); |
| 1175 | assert( sqlite3_mutex_held(pBt->db->mutex) ); |
| 1176 | return sqlite3InvokeBusyHandler(&pBt->db->busyHandler); |
| 1177 | } |
| 1178 | |
| 1179 | /* |
drh | ad3e010 | 2004-09-03 23:32:18 +0000 | [diff] [blame] | 1180 | ** Open a database file. |
| 1181 | ** |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1182 | ** zFilename is the name of the database file. If zFilename is NULL |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1183 | ** a new database with a random name is created. This randomly named |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1184 | ** database file will be deleted when sqlite3BtreeClose() is called. |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1185 | ** If zFilename is ":memory:" then an in-memory database is created |
| 1186 | ** that is automatically destroyed when it is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1187 | */ |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1188 | int sqlite3BtreeOpen( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1189 | const char *zFilename, /* Name of the file containing the BTree database */ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1190 | sqlite3 *db, /* Associated database handle */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1191 | Btree **ppBtree, /* Pointer to new Btree object written here */ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1192 | int flags, /* Options */ |
| 1193 | int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1194 | ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1195 | sqlite3_vfs *pVfs; /* The VFS to use for this btree */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1196 | BtShared *pBt = 0; /* Shared part of btree structure */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1197 | Btree *p; /* Handle to return */ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1198 | int rc = SQLITE_OK; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1199 | int nReserve; |
| 1200 | unsigned char zDbHeader[100]; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1201 | |
| 1202 | /* Set the variable isMemdb to true for an in-memory database, or |
| 1203 | ** false for a file-based database. This symbol is only required if |
| 1204 | ** either of the shared-data or autovacuum features are compiled |
| 1205 | ** into the library. |
| 1206 | */ |
| 1207 | #if !defined(SQLITE_OMIT_SHARED_CACHE) || !defined(SQLITE_OMIT_AUTOVACUUM) |
| 1208 | #ifdef SQLITE_OMIT_MEMORYDB |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 1209 | const int isMemdb = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1210 | #else |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 1211 | const int isMemdb = zFilename && !strcmp(zFilename, ":memory:"); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1212 | #endif |
| 1213 | #endif |
| 1214 | |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1215 | assert( db!=0 ); |
| 1216 | assert( sqlite3_mutex_held(db->mutex) ); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1217 | |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1218 | pVfs = db->pVfs; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1219 | p = sqlite3MallocZero(sizeof(Btree)); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1220 | if( !p ){ |
| 1221 | return SQLITE_NOMEM; |
| 1222 | } |
| 1223 | p->inTrans = TRANS_NONE; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1224 | p->db = db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1225 | |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 1226 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1227 | /* |
| 1228 | ** If this Btree is a candidate for shared cache, try to find an |
| 1229 | ** existing BtShared object that we can share with |
| 1230 | */ |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 1231 | if( isMemdb==0 |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1232 | && (db->flags & SQLITE_Vtab)==0 |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1233 | && zFilename && zFilename[0] |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1234 | ){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1235 | if( sqlite3SharedCacheEnabled ){ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 1236 | int nFullPathname = pVfs->mxPathname+1; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1237 | char *zFullPathname = sqlite3Malloc(nFullPathname); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1238 | sqlite3_mutex *mutexShared; |
| 1239 | p->sharable = 1; |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 1240 | db->flags |= SQLITE_SharedCache; |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1241 | if( !zFullPathname ){ |
| 1242 | sqlite3_free(p); |
| 1243 | return SQLITE_NOMEM; |
| 1244 | } |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 1245 | sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname); |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 1246 | mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1247 | sqlite3_mutex_enter(mutexShared); |
| 1248 | for(pBt=sqlite3SharedCacheList; pBt; pBt=pBt->pNext){ |
| 1249 | assert( pBt->nRef>0 ); |
| 1250 | if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager)) |
| 1251 | && sqlite3PagerVfs(pBt->pPager)==pVfs ){ |
| 1252 | p->pBt = pBt; |
| 1253 | pBt->nRef++; |
| 1254 | break; |
| 1255 | } |
| 1256 | } |
| 1257 | sqlite3_mutex_leave(mutexShared); |
| 1258 | sqlite3_free(zFullPathname); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1259 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1260 | #ifdef SQLITE_DEBUG |
| 1261 | else{ |
| 1262 | /* In debug mode, we mark all persistent databases as sharable |
| 1263 | ** even when they are not. This exercises the locking code and |
| 1264 | ** gives more opportunity for asserts(sqlite3_mutex_held()) |
| 1265 | ** statements to find locking problems. |
| 1266 | */ |
| 1267 | p->sharable = 1; |
| 1268 | } |
| 1269 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1270 | } |
| 1271 | #endif |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1272 | if( pBt==0 ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1273 | /* |
| 1274 | ** The following asserts make sure that structures used by the btree are |
| 1275 | ** the right size. This is to guard against size changes that result |
| 1276 | ** when compiling on a different architecture. |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 1277 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1278 | assert( sizeof(i64)==8 || sizeof(i64)==4 ); |
| 1279 | assert( sizeof(u64)==8 || sizeof(u64)==4 ); |
| 1280 | assert( sizeof(u32)==4 ); |
| 1281 | assert( sizeof(u16)==2 ); |
| 1282 | assert( sizeof(Pgno)==4 ); |
| 1283 | |
| 1284 | pBt = sqlite3MallocZero( sizeof(*pBt) ); |
| 1285 | if( pBt==0 ){ |
| 1286 | rc = SQLITE_NOMEM; |
| 1287 | goto btree_open_out; |
| 1288 | } |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1289 | pBt->busyHdr.xFunc = sqlite3BtreeInvokeBusyHandler; |
| 1290 | pBt->busyHdr.pArg = pBt; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 1291 | rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename, pageDestructor, |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1292 | EXTRA_SIZE, flags, vfsFlags); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1293 | if( rc==SQLITE_OK ){ |
| 1294 | rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader); |
| 1295 | } |
| 1296 | if( rc!=SQLITE_OK ){ |
| 1297 | goto btree_open_out; |
| 1298 | } |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1299 | sqlite3PagerSetBusyhandler(pBt->pPager, &pBt->busyHdr); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1300 | p->pBt = pBt; |
| 1301 | |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 1302 | /* sqlite3PagerSetDestructor(pBt->pPager, pageDestructor); */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1303 | sqlite3PagerSetReiniter(pBt->pPager, pageReinit); |
| 1304 | pBt->pCursor = 0; |
| 1305 | pBt->pPage1 = 0; |
| 1306 | pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager); |
| 1307 | pBt->pageSize = get2byte(&zDbHeader[16]); |
| 1308 | if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE |
| 1309 | || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1310 | pBt->pageSize = 0; |
| 1311 | sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1312 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1313 | /* If the magic name ":memory:" will create an in-memory database, then |
| 1314 | ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if |
| 1315 | ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if |
| 1316 | ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a |
| 1317 | ** regular file-name. In this case the auto-vacuum applies as per normal. |
| 1318 | */ |
| 1319 | if( zFilename && !isMemdb ){ |
| 1320 | pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0); |
| 1321 | pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0); |
| 1322 | } |
| 1323 | #endif |
| 1324 | nReserve = 0; |
| 1325 | }else{ |
| 1326 | nReserve = zDbHeader[20]; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1327 | pBt->pageSizeFixed = 1; |
| 1328 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1329 | pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0); |
| 1330 | pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0); |
| 1331 | #endif |
| 1332 | } |
| 1333 | pBt->usableSize = pBt->pageSize - nReserve; |
| 1334 | assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1335 | sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1336 | |
| 1337 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
| 1338 | /* Add the new BtShared object to the linked list sharable BtShareds. |
| 1339 | */ |
| 1340 | if( p->sharable ){ |
| 1341 | sqlite3_mutex *mutexShared; |
| 1342 | pBt->nRef = 1; |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 1343 | mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
| 1344 | if( SQLITE_THREADSAFE && sqlite3Config.bCoreMutex ){ |
| 1345 | pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST); |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 1346 | if( pBt->mutex==0 ){ |
| 1347 | rc = SQLITE_NOMEM; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1348 | db->mallocFailed = 0; |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 1349 | goto btree_open_out; |
| 1350 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1351 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1352 | sqlite3_mutex_enter(mutexShared); |
| 1353 | pBt->pNext = sqlite3SharedCacheList; |
| 1354 | sqlite3SharedCacheList = pBt; |
| 1355 | sqlite3_mutex_leave(mutexShared); |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1356 | } |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1357 | #endif |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1358 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1359 | |
drh | cfed7bc | 2006-03-13 14:28:05 +0000 | [diff] [blame] | 1360 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1361 | /* If the new Btree uses a sharable pBtShared, then link the new |
| 1362 | ** Btree into the list of all sharable Btrees for the same connection. |
drh | abddb0c | 2007-08-20 13:14:28 +0000 | [diff] [blame] | 1363 | ** The list is kept in ascending order by pBt address. |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 1364 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1365 | if( p->sharable ){ |
| 1366 | int i; |
| 1367 | Btree *pSib; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1368 | for(i=0; i<db->nDb; i++){ |
| 1369 | if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1370 | while( pSib->pPrev ){ pSib = pSib->pPrev; } |
| 1371 | if( p->pBt<pSib->pBt ){ |
| 1372 | p->pNext = pSib; |
| 1373 | p->pPrev = 0; |
| 1374 | pSib->pPrev = p; |
| 1375 | }else{ |
drh | abddb0c | 2007-08-20 13:14:28 +0000 | [diff] [blame] | 1376 | while( pSib->pNext && pSib->pNext->pBt<p->pBt ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1377 | pSib = pSib->pNext; |
| 1378 | } |
| 1379 | p->pNext = pSib->pNext; |
| 1380 | p->pPrev = pSib; |
| 1381 | if( p->pNext ){ |
| 1382 | p->pNext->pPrev = p; |
| 1383 | } |
| 1384 | pSib->pNext = p; |
| 1385 | } |
| 1386 | break; |
| 1387 | } |
| 1388 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1389 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1390 | #endif |
| 1391 | *ppBtree = p; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1392 | |
| 1393 | btree_open_out: |
| 1394 | if( rc!=SQLITE_OK ){ |
| 1395 | if( pBt && pBt->pPager ){ |
| 1396 | sqlite3PagerClose(pBt->pPager); |
| 1397 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1398 | sqlite3_free(pBt); |
| 1399 | sqlite3_free(p); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1400 | *ppBtree = 0; |
| 1401 | } |
| 1402 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | /* |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1406 | ** Decrement the BtShared.nRef counter. When it reaches zero, |
| 1407 | ** remove the BtShared structure from the sharing list. Return |
| 1408 | ** true if the BtShared.nRef counter reaches zero and return |
| 1409 | ** false if it is still positive. |
| 1410 | */ |
| 1411 | static int removeFromSharingList(BtShared *pBt){ |
| 1412 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 1413 | sqlite3_mutex *pMaster; |
| 1414 | BtShared *pList; |
| 1415 | int removed = 0; |
| 1416 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1417 | assert( sqlite3_mutex_notheld(pBt->mutex) ); |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 1418 | pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1419 | sqlite3_mutex_enter(pMaster); |
| 1420 | pBt->nRef--; |
| 1421 | if( pBt->nRef<=0 ){ |
| 1422 | if( sqlite3SharedCacheList==pBt ){ |
| 1423 | sqlite3SharedCacheList = pBt->pNext; |
| 1424 | }else{ |
| 1425 | pList = sqlite3SharedCacheList; |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 1426 | while( ALWAYS(pList) && pList->pNext!=pBt ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1427 | pList=pList->pNext; |
| 1428 | } |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 1429 | if( ALWAYS(pList) ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1430 | pList->pNext = pBt->pNext; |
| 1431 | } |
| 1432 | } |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 1433 | if( SQLITE_THREADSAFE ){ |
| 1434 | sqlite3_mutex_free(pBt->mutex); |
| 1435 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1436 | removed = 1; |
| 1437 | } |
| 1438 | sqlite3_mutex_leave(pMaster); |
| 1439 | return removed; |
| 1440 | #else |
| 1441 | return 1; |
| 1442 | #endif |
| 1443 | } |
| 1444 | |
| 1445 | /* |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1446 | ** Make sure pBt->pTmpSpace points to an allocation of |
| 1447 | ** MX_CELL_SIZE(pBt) bytes. |
| 1448 | */ |
| 1449 | static void allocateTempSpace(BtShared *pBt){ |
| 1450 | if( !pBt->pTmpSpace ){ |
| 1451 | pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize ); |
| 1452 | } |
| 1453 | } |
| 1454 | |
| 1455 | /* |
| 1456 | ** Free the pBt->pTmpSpace allocation |
| 1457 | */ |
| 1458 | static void freeTempSpace(BtShared *pBt){ |
| 1459 | sqlite3PageFree( pBt->pTmpSpace); |
| 1460 | pBt->pTmpSpace = 0; |
| 1461 | } |
| 1462 | |
| 1463 | /* |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1464 | ** Close an open database and invalidate all cursors. |
| 1465 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1466 | int sqlite3BtreeClose(Btree *p){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1467 | BtShared *pBt = p->pBt; |
| 1468 | BtCursor *pCur; |
| 1469 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1470 | /* Close all cursors opened via this handle. */ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1471 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1472 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1473 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1474 | pCur = pBt->pCursor; |
| 1475 | while( pCur ){ |
| 1476 | BtCursor *pTmp = pCur; |
| 1477 | pCur = pCur->pNext; |
| 1478 | if( pTmp->pBtree==p ){ |
| 1479 | sqlite3BtreeCloseCursor(pTmp); |
| 1480 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1481 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1482 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 1483 | /* Rollback any active transaction and free the handle structure. |
| 1484 | ** The call to sqlite3BtreeRollback() drops any table-locks held by |
| 1485 | ** this handle. |
| 1486 | */ |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 1487 | sqlite3BtreeRollback(p); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1488 | sqlite3BtreeLeave(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1489 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1490 | /* If there are still other outstanding references to the shared-btree |
| 1491 | ** structure, return now. The remainder of this procedure cleans |
| 1492 | ** up the shared-btree. |
| 1493 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1494 | assert( p->wantToLock==0 && p->locked==0 ); |
| 1495 | if( !p->sharable || removeFromSharingList(pBt) ){ |
| 1496 | /* The pBt is no longer on the sharing list, so we can access |
| 1497 | ** it without having to hold the mutex. |
| 1498 | ** |
| 1499 | ** Clean out and delete the BtShared object. |
| 1500 | */ |
| 1501 | assert( !pBt->pCursor ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1502 | sqlite3PagerClose(pBt->pPager); |
| 1503 | if( pBt->xFreeSchema && pBt->pSchema ){ |
| 1504 | pBt->xFreeSchema(pBt->pSchema); |
| 1505 | } |
| 1506 | sqlite3_free(pBt->pSchema); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1507 | freeTempSpace(pBt); |
drh | 65bbf29 | 2008-06-19 01:03:17 +0000 | [diff] [blame] | 1508 | sqlite3_free(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1511 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | cab5ed7 | 2007-08-22 11:41:18 +0000 | [diff] [blame] | 1512 | assert( p->wantToLock==0 ); |
| 1513 | assert( p->locked==0 ); |
| 1514 | if( p->pPrev ) p->pPrev->pNext = p->pNext; |
| 1515 | if( p->pNext ) p->pNext->pPrev = p->pPrev; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1516 | #endif |
| 1517 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1518 | sqlite3_free(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1519 | return SQLITE_OK; |
| 1520 | } |
| 1521 | |
| 1522 | /* |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1523 | ** Change the limit on the number of pages allowed in the cache. |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 1524 | ** |
| 1525 | ** The maximum number of cache pages is set to the absolute |
| 1526 | ** value of mxPage. If mxPage is negative, the pager will |
| 1527 | ** operate asynchronously - it will not stop to do fsync()s |
| 1528 | ** to insure data is written to the disk surface before |
| 1529 | ** continuing. Transactions still work if synchronous is off, |
| 1530 | ** and the database cannot be corrupted if this program |
| 1531 | ** crashes. But if the operating system crashes or there is |
| 1532 | ** an abrupt power failure when synchronous is off, the database |
| 1533 | ** could be left in an inconsistent and unrecoverable state. |
| 1534 | ** Synchronous is on by default so database corruption is not |
| 1535 | ** normally a worry. |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1536 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1537 | int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){ |
| 1538 | BtShared *pBt = p->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1539 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1540 | sqlite3BtreeEnter(p); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1541 | sqlite3PagerSetCachesize(pBt->pPager, mxPage); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1542 | sqlite3BtreeLeave(p); |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1543 | return SQLITE_OK; |
| 1544 | } |
| 1545 | |
| 1546 | /* |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1547 | ** Change the way data is synced to disk in order to increase or decrease |
| 1548 | ** how well the database resists damage due to OS crashes and power |
| 1549 | ** failures. Level 1 is the same as asynchronous (no syncs() occur and |
| 1550 | ** there is a high probability of damage) Level 2 is the default. There |
| 1551 | ** is a very low but non-zero probability of damage. Level 3 reduces the |
| 1552 | ** probability of damage to near zero but with a write performance reduction. |
| 1553 | */ |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1554 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
drh | ac530b1 | 2006-02-11 01:25:50 +0000 | [diff] [blame] | 1555 | int sqlite3BtreeSetSafetyLevel(Btree *p, int level, int fullSync){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1556 | BtShared *pBt = p->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1557 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1558 | sqlite3BtreeEnter(p); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1559 | sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1560 | sqlite3BtreeLeave(p); |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1561 | return SQLITE_OK; |
| 1562 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1563 | #endif |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1564 | |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1565 | /* |
| 1566 | ** Return TRUE if the given btree is set to safety level 1. In other |
| 1567 | ** words, return TRUE if no sync() occurs on the disk files. |
| 1568 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1569 | int sqlite3BtreeSyncDisabled(Btree *p){ |
| 1570 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1571 | int rc; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1572 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1573 | sqlite3BtreeEnter(p); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 1574 | assert( pBt && pBt->pPager ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1575 | rc = sqlite3PagerNosync(pBt->pPager); |
| 1576 | sqlite3BtreeLeave(p); |
| 1577 | return rc; |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1580 | #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1581 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1582 | ** Change the default pages size and the number of reserved bytes per page. |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 1583 | ** |
| 1584 | ** The page size must be a power of 2 between 512 and 65536. If the page |
| 1585 | ** size supplied does not meet this constraint then the page size is not |
| 1586 | ** changed. |
| 1587 | ** |
| 1588 | ** Page sizes are constrained to be a power of two so that the region |
| 1589 | ** of the database file used for locking (beginning at PENDING_BYTE, |
| 1590 | ** the first byte past the 1GB boundary, 0x40000000) needs to occur |
| 1591 | ** at the beginning of a page. |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 1592 | ** |
| 1593 | ** If parameter nReserve is less than zero, then the number of reserved |
| 1594 | ** bytes per page is left unchanged. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1595 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1596 | int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1597 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1598 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1599 | sqlite3BtreeEnter(p); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1600 | if( pBt->pageSizeFixed ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1601 | sqlite3BtreeLeave(p); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1602 | return SQLITE_READONLY; |
| 1603 | } |
| 1604 | if( nReserve<0 ){ |
| 1605 | nReserve = pBt->pageSize - pBt->usableSize; |
| 1606 | } |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 1607 | if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE && |
| 1608 | ((pageSize-1)&pageSize)==0 ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1609 | assert( (pageSize & 7)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1610 | assert( !pBt->pPage1 && !pBt->pCursor ); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1611 | pBt->pageSize = pageSize; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1612 | freeTempSpace(pBt); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1613 | rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1614 | } |
| 1615 | pBt->usableSize = pBt->pageSize - nReserve; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1616 | sqlite3BtreeLeave(p); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1617 | return rc; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
| 1620 | /* |
| 1621 | ** Return the currently defined page size |
| 1622 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1623 | int sqlite3BtreeGetPageSize(Btree *p){ |
| 1624 | return p->pBt->pageSize; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1625 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1626 | int sqlite3BtreeGetReserve(Btree *p){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1627 | int n; |
| 1628 | sqlite3BtreeEnter(p); |
| 1629 | n = p->pBt->pageSize - p->pBt->usableSize; |
| 1630 | sqlite3BtreeLeave(p); |
| 1631 | return n; |
drh | 2011d5f | 2004-07-22 02:40:37 +0000 | [diff] [blame] | 1632 | } |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 1633 | |
| 1634 | /* |
| 1635 | ** Set the maximum page count for a database if mxPage is positive. |
| 1636 | ** No changes are made if mxPage is 0 or negative. |
| 1637 | ** Regardless of the value of mxPage, return the maximum page count. |
| 1638 | */ |
| 1639 | int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1640 | int n; |
| 1641 | sqlite3BtreeEnter(p); |
| 1642 | n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage); |
| 1643 | sqlite3BtreeLeave(p); |
| 1644 | return n; |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 1645 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1646 | #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1647 | |
| 1648 | /* |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1649 | ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum' |
| 1650 | ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it |
| 1651 | ** is disabled. The default value for the auto-vacuum property is |
| 1652 | ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro. |
| 1653 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1654 | int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1655 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1656 | return SQLITE_READONLY; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1657 | #else |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1658 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1659 | int rc = SQLITE_OK; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1660 | int av = (autoVacuum?1:0); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1661 | |
| 1662 | sqlite3BtreeEnter(p); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1663 | if( pBt->pageSizeFixed && av!=pBt->autoVacuum ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1664 | rc = SQLITE_READONLY; |
| 1665 | }else{ |
| 1666 | pBt->autoVacuum = av; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1667 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1668 | sqlite3BtreeLeave(p); |
| 1669 | return rc; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1670 | #endif |
| 1671 | } |
| 1672 | |
| 1673 | /* |
| 1674 | ** Return the value of the 'auto-vacuum' property. If auto-vacuum is |
| 1675 | ** enabled 1 is returned. Otherwise 0. |
| 1676 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1677 | int sqlite3BtreeGetAutoVacuum(Btree *p){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1678 | #ifdef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1679 | return BTREE_AUTOVACUUM_NONE; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1680 | #else |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1681 | int rc; |
| 1682 | sqlite3BtreeEnter(p); |
| 1683 | rc = ( |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1684 | (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE: |
| 1685 | (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL: |
| 1686 | BTREE_AUTOVACUUM_INCR |
| 1687 | ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1688 | sqlite3BtreeLeave(p); |
| 1689 | return rc; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1690 | #endif |
| 1691 | } |
| 1692 | |
| 1693 | |
| 1694 | /* |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1695 | ** Get a reference to pPage1 of the database file. This will |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1696 | ** also acquire a readlock on that file. |
| 1697 | ** |
| 1698 | ** SQLITE_OK is returned on success. If the file is not a |
| 1699 | ** well-formed database file, then SQLITE_CORRUPT is returned. |
| 1700 | ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 1701 | ** is returned if we run out of memory. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1702 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1703 | static int lockBtree(BtShared *pBt){ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1704 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1705 | MemPage *pPage1; |
danielk1977 | 93f7af9 | 2008-05-09 16:57:50 +0000 | [diff] [blame] | 1706 | int nPage; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1707 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1708 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1709 | if( pBt->pPage1 ) return SQLITE_OK; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1710 | rc = sqlite3BtreeGetPage(pBt, 1, &pPage1, 0); |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1711 | if( rc!=SQLITE_OK ) return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1712 | |
| 1713 | /* Do some checking to help insure the file we opened really is |
| 1714 | ** a valid database file. |
| 1715 | */ |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 1716 | rc = sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 1717 | if( rc!=SQLITE_OK ){ |
danielk1977 | 93f7af9 | 2008-05-09 16:57:50 +0000 | [diff] [blame] | 1718 | goto page1_init_failed; |
| 1719 | }else if( nPage>0 ){ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1720 | int pageSize; |
| 1721 | int usableSize; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1722 | u8 *page1 = pPage1->aData; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 1723 | rc = SQLITE_NOTADB; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1724 | if( memcmp(page1, zMagicHeader, 16)!=0 ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1725 | goto page1_init_failed; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1726 | } |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 1727 | if( page1[18]>1 ){ |
| 1728 | pBt->readOnly = 1; |
| 1729 | } |
| 1730 | if( page1[19]>1 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1731 | goto page1_init_failed; |
| 1732 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1733 | |
| 1734 | /* The maximum embedded fraction must be exactly 25%. And the minimum |
| 1735 | ** embedded fraction must be 12.5% for both leaf-data and non-leaf-data. |
| 1736 | ** The original design allowed these amounts to vary, but as of |
| 1737 | ** version 3.6.0, we require them to be fixed. |
| 1738 | */ |
| 1739 | if( memcmp(&page1[21], "\100\040\040",3)!=0 ){ |
| 1740 | goto page1_init_failed; |
| 1741 | } |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1742 | pageSize = get2byte(&page1[16]); |
drh | 7dc385e | 2007-09-06 23:39:36 +0000 | [diff] [blame] | 1743 | if( ((pageSize-1)&pageSize)!=0 || pageSize<512 || |
| 1744 | (SQLITE_MAX_PAGE_SIZE<32768 && pageSize>SQLITE_MAX_PAGE_SIZE) |
| 1745 | ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1746 | goto page1_init_failed; |
| 1747 | } |
| 1748 | assert( (pageSize & 7)==0 ); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1749 | usableSize = pageSize - page1[20]; |
| 1750 | if( pageSize!=pBt->pageSize ){ |
| 1751 | /* After reading the first page of the database assuming a page size |
| 1752 | ** of BtShared.pageSize, we have discovered that the page-size is |
| 1753 | ** actually pageSize. Unlock the database, leave pBt->pPage1 at |
| 1754 | ** zero and return SQLITE_OK. The caller will call this function |
| 1755 | ** again with the correct page-size. |
| 1756 | */ |
| 1757 | releasePage(pPage1); |
| 1758 | pBt->usableSize = usableSize; |
| 1759 | pBt->pageSize = pageSize; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1760 | freeTempSpace(pBt); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1761 | sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
| 1762 | return SQLITE_OK; |
| 1763 | } |
| 1764 | if( usableSize<500 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1765 | goto page1_init_failed; |
| 1766 | } |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1767 | pBt->pageSize = pageSize; |
| 1768 | pBt->usableSize = usableSize; |
drh | 057cd3a | 2005-02-15 16:23:02 +0000 | [diff] [blame] | 1769 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1770 | pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0); |
danielk1977 | 27b1f95 | 2007-06-25 08:16:58 +0000 | [diff] [blame] | 1771 | pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0); |
drh | 057cd3a | 2005-02-15 16:23:02 +0000 | [diff] [blame] | 1772 | #endif |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1773 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1774 | |
| 1775 | /* maxLocal is the maximum amount of payload to store locally for |
| 1776 | ** a cell. Make sure it is small enough so that at least minFanout |
| 1777 | ** cells can will fit on one page. We assume a 10-byte page header. |
| 1778 | ** Besides the payload, the cell must store: |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1779 | ** 2-byte pointer to the cell |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1780 | ** 4-byte child pointer |
| 1781 | ** 9-byte nKey value |
| 1782 | ** 4-byte nData value |
| 1783 | ** 4-byte overflow page pointer |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1784 | ** So a cell consists of a 2-byte poiner, a header which is as much as |
| 1785 | ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow |
| 1786 | ** page pointer. |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1787 | */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1788 | pBt->maxLocal = (pBt->usableSize-12)*64/255 - 23; |
| 1789 | pBt->minLocal = (pBt->usableSize-12)*32/255 - 23; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1790 | pBt->maxLeaf = pBt->usableSize - 35; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1791 | pBt->minLeaf = (pBt->usableSize-12)*32/255 - 23; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1792 | assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1793 | pBt->pPage1 = pPage1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1794 | return SQLITE_OK; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1795 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1796 | page1_init_failed: |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1797 | releasePage(pPage1); |
| 1798 | pBt->pPage1 = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1799 | return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1800 | } |
| 1801 | |
| 1802 | /* |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1803 | ** This routine works like lockBtree() except that it also invokes the |
| 1804 | ** busy callback if there is lock contention. |
| 1805 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1806 | static int lockBtreeWithRetry(Btree *pRef){ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1807 | int rc = SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1808 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1809 | assert( sqlite3BtreeHoldsMutex(pRef) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1810 | if( pRef->inTrans==TRANS_NONE ){ |
| 1811 | u8 inTransaction = pRef->pBt->inTransaction; |
| 1812 | btreeIntegrity(pRef); |
| 1813 | rc = sqlite3BtreeBeginTrans(pRef, 0); |
| 1814 | pRef->pBt->inTransaction = inTransaction; |
| 1815 | pRef->inTrans = TRANS_NONE; |
| 1816 | if( rc==SQLITE_OK ){ |
| 1817 | pRef->pBt->nTransaction--; |
| 1818 | } |
| 1819 | btreeIntegrity(pRef); |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1820 | } |
| 1821 | return rc; |
| 1822 | } |
| 1823 | |
| 1824 | |
| 1825 | /* |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1826 | ** If there are no outstanding cursors and we are not in the middle |
| 1827 | ** of a transaction but there is a read lock on the database, then |
| 1828 | ** this routine unrefs the first page of the database file which |
| 1829 | ** has the effect of releasing the read lock. |
| 1830 | ** |
| 1831 | ** If there are any outstanding cursors, this routine is a no-op. |
| 1832 | ** |
| 1833 | ** If there is a transaction in progress, this routine is a no-op. |
| 1834 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1835 | static void unlockBtreeIfUnused(BtShared *pBt){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1836 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1837 | if( pBt->inTransaction==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1838 | if( sqlite3PagerRefcount(pBt->pPager)>=1 ){ |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 1839 | assert( pBt->pPage1->aData ); |
| 1840 | #if 0 |
drh | 24c9a2e | 2007-01-05 02:00:47 +0000 | [diff] [blame] | 1841 | if( pBt->pPage1->aData==0 ){ |
| 1842 | MemPage *pPage = pBt->pPage1; |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1843 | pPage->aData = sqlite3PagerGetData(pPage->pDbPage); |
drh | 24c9a2e | 2007-01-05 02:00:47 +0000 | [diff] [blame] | 1844 | pPage->pBt = pBt; |
| 1845 | pPage->pgno = 1; |
| 1846 | } |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 1847 | #endif |
drh | 24c9a2e | 2007-01-05 02:00:47 +0000 | [diff] [blame] | 1848 | releasePage(pBt->pPage1); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 1849 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1850 | pBt->pPage1 = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1851 | pBt->inStmt = 0; |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1852 | } |
| 1853 | } |
| 1854 | |
| 1855 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1856 | ** Create a new database by initializing the first page of the |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1857 | ** file. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1858 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1859 | static int newDatabase(BtShared *pBt){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1860 | MemPage *pP1; |
| 1861 | unsigned char *data; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1862 | int rc; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 1863 | int nPage; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1864 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1865 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 1866 | rc = sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 1867 | if( rc!=SQLITE_OK || nPage>0 ){ |
| 1868 | return rc; |
| 1869 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1870 | pP1 = pBt->pPage1; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1871 | assert( pP1!=0 ); |
| 1872 | data = pP1->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1873 | rc = sqlite3PagerWrite(pP1->pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1874 | if( rc ) return rc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1875 | memcpy(data, zMagicHeader, sizeof(zMagicHeader)); |
| 1876 | assert( sizeof(zMagicHeader)==16 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1877 | put2byte(&data[16], pBt->pageSize); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1878 | data[18] = 1; |
| 1879 | data[19] = 1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1880 | data[20] = pBt->pageSize - pBt->usableSize; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1881 | data[21] = 64; |
| 1882 | data[22] = 32; |
| 1883 | data[23] = 32; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1884 | memset(&data[24], 0, 100-24); |
drh | e6c4381 | 2004-05-14 12:17:46 +0000 | [diff] [blame] | 1885 | zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA ); |
drh | f2a611c | 2004-09-05 00:33:43 +0000 | [diff] [blame] | 1886 | pBt->pageSizeFixed = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1887 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1888 | assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 ); |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 1889 | assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 ); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1890 | put4byte(&data[36 + 4*4], pBt->autoVacuum); |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 1891 | put4byte(&data[36 + 7*4], pBt->incrVacuum); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1892 | #endif |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1893 | return SQLITE_OK; |
| 1894 | } |
| 1895 | |
| 1896 | /* |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1897 | ** Attempt to start a new transaction. A write-transaction |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 1898 | ** is started if the second argument is nonzero, otherwise a read- |
| 1899 | ** transaction. If the second argument is 2 or more and exclusive |
| 1900 | ** transaction is started, meaning that no other process is allowed |
| 1901 | ** to access the database. A preexisting transaction may not be |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1902 | ** upgraded to exclusive by calling this routine a second time - the |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 1903 | ** exclusivity flag only works for a new transaction. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1904 | ** |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1905 | ** A write-transaction must be started before attempting any |
| 1906 | ** changes to the database. None of the following routines |
| 1907 | ** will work unless a transaction is started first: |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1908 | ** |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1909 | ** sqlite3BtreeCreateTable() |
| 1910 | ** sqlite3BtreeCreateIndex() |
| 1911 | ** sqlite3BtreeClearTable() |
| 1912 | ** sqlite3BtreeDropTable() |
| 1913 | ** sqlite3BtreeInsert() |
| 1914 | ** sqlite3BtreeDelete() |
| 1915 | ** sqlite3BtreeUpdateMeta() |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1916 | ** |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1917 | ** If an initial attempt to acquire the lock fails because of lock contention |
| 1918 | ** and the database was previously unlocked, then invoke the busy handler |
| 1919 | ** if there is one. But if there was previously a read-lock, do not |
| 1920 | ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is |
| 1921 | ** returned when there is already a read-lock in order to avoid a deadlock. |
| 1922 | ** |
| 1923 | ** Suppose there are two processes A and B. A has a read lock and B has |
| 1924 | ** a reserved lock. B tries to promote to exclusive but is blocked because |
| 1925 | ** of A's read lock. A tries to promote to reserved but is blocked by B. |
| 1926 | ** One or the other of the two processes must give way or there can be |
| 1927 | ** no progress. By returning SQLITE_BUSY and not invoking the busy callback |
| 1928 | ** when A already has a read lock, we encourage A to give up and let B |
| 1929 | ** proceed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1930 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1931 | int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ |
| 1932 | BtShared *pBt = p->pBt; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1933 | int rc = SQLITE_OK; |
| 1934 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1935 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1936 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1937 | btreeIntegrity(p); |
| 1938 | |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1939 | /* If the btree is already in a write-transaction, or it |
| 1940 | ** is already in a read-transaction and a read-transaction |
| 1941 | ** is requested, this is a no-op. |
| 1942 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1943 | if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1944 | goto trans_begun; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1945 | } |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1946 | |
| 1947 | /* Write transactions are not possible on a read-only database */ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1948 | if( pBt->readOnly && wrflag ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1949 | rc = SQLITE_READONLY; |
| 1950 | goto trans_begun; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1951 | } |
| 1952 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1953 | /* If another database handle has already opened a write transaction |
| 1954 | ** on this shared-btree structure and a second write transaction is |
| 1955 | ** requested, return SQLITE_BUSY. |
| 1956 | */ |
| 1957 | if( pBt->inTransaction==TRANS_WRITE && wrflag ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1958 | rc = SQLITE_BUSY; |
| 1959 | goto trans_begun; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 1962 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 1963 | if( wrflag>1 ){ |
| 1964 | BtLock *pIter; |
| 1965 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 1966 | if( pIter->pBtree!=p ){ |
| 1967 | rc = SQLITE_BUSY; |
| 1968 | goto trans_begun; |
| 1969 | } |
| 1970 | } |
| 1971 | } |
| 1972 | #endif |
| 1973 | |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1974 | do { |
drh | 8a9c17f | 2008-05-02 14:23:54 +0000 | [diff] [blame] | 1975 | if( pBt->pPage1==0 ){ |
| 1976 | do{ |
| 1977 | rc = lockBtree(pBt); |
| 1978 | }while( pBt->pPage1==0 && rc==SQLITE_OK ); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1979 | } |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 1980 | |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1981 | if( rc==SQLITE_OK && wrflag ){ |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 1982 | if( pBt->readOnly ){ |
| 1983 | rc = SQLITE_READONLY; |
| 1984 | }else{ |
| 1985 | rc = sqlite3PagerBegin(pBt->pPage1->pDbPage, wrflag>1); |
| 1986 | if( rc==SQLITE_OK ){ |
| 1987 | rc = newDatabase(pBt); |
| 1988 | } |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1989 | } |
| 1990 | } |
| 1991 | |
| 1992 | if( rc==SQLITE_OK ){ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1993 | if( wrflag ) pBt->inStmt = 0; |
| 1994 | }else{ |
| 1995 | unlockBtreeIfUnused(pBt); |
| 1996 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1997 | }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE && |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1998 | sqlite3BtreeInvokeBusyHandler(pBt, 0) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1999 | |
| 2000 | if( rc==SQLITE_OK ){ |
| 2001 | if( p->inTrans==TRANS_NONE ){ |
| 2002 | pBt->nTransaction++; |
| 2003 | } |
| 2004 | p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ); |
| 2005 | if( p->inTrans>pBt->inTransaction ){ |
| 2006 | pBt->inTransaction = p->inTrans; |
| 2007 | } |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 2008 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 2009 | if( wrflag>1 ){ |
| 2010 | assert( !pBt->pExclusive ); |
| 2011 | pBt->pExclusive = p; |
| 2012 | } |
| 2013 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2014 | } |
| 2015 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2016 | |
| 2017 | trans_begun: |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2018 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2019 | sqlite3BtreeLeave(p); |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 2020 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2021 | } |
| 2022 | |
drh | 4a0611d | 2008-07-18 17:16:26 +0000 | [diff] [blame] | 2023 | /* |
| 2024 | ** Return the size of the database file in pages. Or return -1 if |
| 2025 | ** there is any kind of error. |
| 2026 | */ |
| 2027 | static int pagerPagecount(Pager *pPager){ |
| 2028 | int rc; |
| 2029 | int nPage; |
| 2030 | rc = sqlite3PagerPagecount(pPager, &nPage); |
| 2031 | return (rc==SQLITE_OK?nPage:-1); |
| 2032 | } |
| 2033 | |
| 2034 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2035 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2036 | |
| 2037 | /* |
| 2038 | ** Set the pointer-map entries for all children of page pPage. Also, if |
| 2039 | ** pPage contains cells that point to overflow pages, set the pointer |
| 2040 | ** map entries for the overflow pages as well. |
| 2041 | */ |
| 2042 | static int setChildPtrmaps(MemPage *pPage){ |
| 2043 | int i; /* Counter variable */ |
| 2044 | int nCell; /* Number of cells in page pPage */ |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 2045 | int rc; /* Return code */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2046 | BtShared *pBt = pPage->pBt; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2047 | int isInitOrig = pPage->isInit; |
| 2048 | Pgno pgno = pPage->pgno; |
| 2049 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2050 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 2051 | rc = sqlite3BtreeInitPage(pPage, pPage->pParent); |
| 2052 | if( rc!=SQLITE_OK ){ |
| 2053 | goto set_child_ptrmaps_out; |
| 2054 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2055 | nCell = pPage->nCell; |
| 2056 | |
| 2057 | for(i=0; i<nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2058 | u8 *pCell = findCell(pPage, i); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2059 | |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 2060 | rc = ptrmapPutOvflPtr(pPage, pCell); |
| 2061 | if( rc!=SQLITE_OK ){ |
| 2062 | goto set_child_ptrmaps_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2063 | } |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 2064 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2065 | if( !pPage->leaf ){ |
| 2066 | Pgno childPgno = get4byte(pCell); |
| 2067 | rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno); |
danielk1977 | 1bc7159 | 2008-07-08 17:13:59 +0000 | [diff] [blame] | 2068 | if( rc!=SQLITE_OK ) goto set_child_ptrmaps_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2069 | } |
| 2070 | } |
| 2071 | |
| 2072 | if( !pPage->leaf ){ |
| 2073 | Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 2074 | rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno); |
| 2075 | } |
| 2076 | |
| 2077 | set_child_ptrmaps_out: |
| 2078 | pPage->isInit = isInitOrig; |
| 2079 | return rc; |
| 2080 | } |
| 2081 | |
| 2082 | /* |
| 2083 | ** Somewhere on pPage, which is guarenteed to be a btree page, not an overflow |
| 2084 | ** page, is a pointer to page iFrom. Modify this pointer so that it points to |
| 2085 | ** iTo. Parameter eType describes the type of pointer to be modified, as |
| 2086 | ** follows: |
| 2087 | ** |
| 2088 | ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child |
| 2089 | ** page of pPage. |
| 2090 | ** |
| 2091 | ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow |
| 2092 | ** page pointed to by one of the cells on pPage. |
| 2093 | ** |
| 2094 | ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next |
| 2095 | ** overflow page in the list. |
| 2096 | */ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2097 | static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2098 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2099 | if( eType==PTRMAP_OVERFLOW2 ){ |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 2100 | /* 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] | 2101 | if( get4byte(pPage->aData)!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2102 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2103 | } |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 2104 | put4byte(pPage->aData, iTo); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2105 | }else{ |
| 2106 | int isInitOrig = pPage->isInit; |
| 2107 | int i; |
| 2108 | int nCell; |
| 2109 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2110 | sqlite3BtreeInitPage(pPage, 0); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2111 | nCell = pPage->nCell; |
| 2112 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2113 | for(i=0; i<nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2114 | u8 *pCell = findCell(pPage, i); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2115 | if( eType==PTRMAP_OVERFLOW1 ){ |
| 2116 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2117 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2118 | if( info.iOverflow ){ |
| 2119 | if( iFrom==get4byte(&pCell[info.iOverflow]) ){ |
| 2120 | put4byte(&pCell[info.iOverflow], iTo); |
| 2121 | break; |
| 2122 | } |
| 2123 | } |
| 2124 | }else{ |
| 2125 | if( get4byte(pCell)==iFrom ){ |
| 2126 | put4byte(pCell, iTo); |
| 2127 | break; |
| 2128 | } |
| 2129 | } |
| 2130 | } |
| 2131 | |
| 2132 | if( i==nCell ){ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2133 | if( eType!=PTRMAP_BTREE || |
| 2134 | get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2135 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2136 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2137 | put4byte(&pPage->aData[pPage->hdrOffset+8], iTo); |
| 2138 | } |
| 2139 | |
| 2140 | pPage->isInit = isInitOrig; |
| 2141 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2142 | return SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2145 | |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2146 | /* |
| 2147 | ** Move the open database page pDbPage to location iFreePage in the |
| 2148 | ** database. The pDbPage reference remains valid. |
| 2149 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2150 | static int relocatePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2151 | BtShared *pBt, /* Btree */ |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2152 | MemPage *pDbPage, /* Open page to move */ |
| 2153 | u8 eType, /* Pointer map 'type' entry for pDbPage */ |
| 2154 | Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */ |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 2155 | Pgno iFreePage, /* The location to move pDbPage to */ |
| 2156 | int isCommit |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2157 | ){ |
| 2158 | MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */ |
| 2159 | Pgno iDbPage = pDbPage->pgno; |
| 2160 | Pager *pPager = pBt->pPager; |
| 2161 | int rc; |
| 2162 | |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2163 | assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || |
| 2164 | eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2165 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 2166 | assert( pDbPage->pBt==pBt ); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2167 | |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 2168 | /* Move page iDbPage from its current location to page number iFreePage */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2169 | TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", |
| 2170 | iDbPage, iFreePage, iPtrPage, eType)); |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 2171 | rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2172 | if( rc!=SQLITE_OK ){ |
| 2173 | return rc; |
| 2174 | } |
| 2175 | pDbPage->pgno = iFreePage; |
| 2176 | |
| 2177 | /* If pDbPage was a btree-page, then it may have child pages and/or cells |
| 2178 | ** that point to overflow pages. The pointer map entries for all these |
| 2179 | ** pages need to be changed. |
| 2180 | ** |
| 2181 | ** If pDbPage is an overflow page, then the first 4 bytes may store a |
| 2182 | ** pointer to a subsequent overflow page. If this is the case, then |
| 2183 | ** the pointer map needs to be updated for the subsequent overflow page. |
| 2184 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2185 | if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2186 | rc = setChildPtrmaps(pDbPage); |
| 2187 | if( rc!=SQLITE_OK ){ |
| 2188 | return rc; |
| 2189 | } |
| 2190 | }else{ |
| 2191 | Pgno nextOvfl = get4byte(pDbPage->aData); |
| 2192 | if( nextOvfl!=0 ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2193 | rc = ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage); |
| 2194 | if( rc!=SQLITE_OK ){ |
| 2195 | return rc; |
| 2196 | } |
| 2197 | } |
| 2198 | } |
| 2199 | |
| 2200 | /* Fix the database pointer on page iPtrPage that pointed at iDbPage so |
| 2201 | ** that it points at iFreePage. Also fix the pointer map entry for |
| 2202 | ** iPtrPage. |
| 2203 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2204 | if( eType!=PTRMAP_ROOTPAGE ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2205 | rc = sqlite3BtreeGetPage(pBt, iPtrPage, &pPtrPage, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2206 | if( rc!=SQLITE_OK ){ |
| 2207 | return rc; |
| 2208 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2209 | rc = sqlite3PagerWrite(pPtrPage->pDbPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2210 | if( rc!=SQLITE_OK ){ |
| 2211 | releasePage(pPtrPage); |
| 2212 | return rc; |
| 2213 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2214 | rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2215 | releasePage(pPtrPage); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2216 | if( rc==SQLITE_OK ){ |
| 2217 | rc = ptrmapPut(pBt, iFreePage, eType, iPtrPage); |
| 2218 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2219 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2220 | return rc; |
| 2221 | } |
| 2222 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2223 | /* Forward declaration required by incrVacuumStep(). */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2224 | static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2225 | |
| 2226 | /* |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2227 | ** Perform a single step of an incremental-vacuum. If successful, |
| 2228 | ** return SQLITE_OK. If there is no work to do (and therefore no |
| 2229 | ** point in calling this function again), return SQLITE_DONE. |
| 2230 | ** |
| 2231 | ** More specificly, this function attempts to re-organize the |
| 2232 | ** database so that the last page of the file currently in use |
| 2233 | ** is no longer in use. |
| 2234 | ** |
| 2235 | ** If the nFin parameter is non-zero, the implementation assumes |
| 2236 | ** that the caller will keep calling incrVacuumStep() until |
| 2237 | ** it returns SQLITE_DONE or an error, and that nFin is the |
| 2238 | ** number of pages the database file will contain after this |
| 2239 | ** process is complete. |
| 2240 | */ |
| 2241 | static int incrVacuumStep(BtShared *pBt, Pgno nFin){ |
| 2242 | Pgno iLastPg; /* Last page in the database */ |
| 2243 | Pgno nFreeList; /* Number of pages still on the free-list */ |
| 2244 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2245 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2246 | iLastPg = pBt->nTrunc; |
| 2247 | if( iLastPg==0 ){ |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2248 | iLastPg = pagerPagecount(pBt->pPager); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2249 | } |
| 2250 | |
| 2251 | if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){ |
| 2252 | int rc; |
| 2253 | u8 eType; |
| 2254 | Pgno iPtrPage; |
| 2255 | |
| 2256 | nFreeList = get4byte(&pBt->pPage1->aData[36]); |
| 2257 | if( nFreeList==0 || nFin==iLastPg ){ |
| 2258 | return SQLITE_DONE; |
| 2259 | } |
| 2260 | |
| 2261 | rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage); |
| 2262 | if( rc!=SQLITE_OK ){ |
| 2263 | return rc; |
| 2264 | } |
| 2265 | if( eType==PTRMAP_ROOTPAGE ){ |
| 2266 | return SQLITE_CORRUPT_BKPT; |
| 2267 | } |
| 2268 | |
| 2269 | if( eType==PTRMAP_FREEPAGE ){ |
| 2270 | if( nFin==0 ){ |
| 2271 | /* Remove the page from the files free-list. This is not required |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 2272 | ** if nFin is non-zero. In that case, the free-list will be |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2273 | ** truncated to zero after this function returns, so it doesn't |
| 2274 | ** matter if it still contains some garbage entries. |
| 2275 | */ |
| 2276 | Pgno iFreePg; |
| 2277 | MemPage *pFreePg; |
| 2278 | rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1); |
| 2279 | if( rc!=SQLITE_OK ){ |
| 2280 | return rc; |
| 2281 | } |
| 2282 | assert( iFreePg==iLastPg ); |
| 2283 | releasePage(pFreePg); |
| 2284 | } |
| 2285 | } else { |
| 2286 | Pgno iFreePg; /* Index of free page to move pLastPg to */ |
| 2287 | MemPage *pLastPg; |
| 2288 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2289 | rc = sqlite3BtreeGetPage(pBt, iLastPg, &pLastPg, 0); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2290 | if( rc!=SQLITE_OK ){ |
| 2291 | return rc; |
| 2292 | } |
| 2293 | |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 2294 | /* If nFin is zero, this loop runs exactly once and page pLastPg |
| 2295 | ** is swapped with the first free page pulled off the free list. |
| 2296 | ** |
| 2297 | ** On the other hand, if nFin is greater than zero, then keep |
| 2298 | ** looping until a free-page located within the first nFin pages |
| 2299 | ** of the file is found. |
| 2300 | */ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2301 | do { |
| 2302 | MemPage *pFreePg; |
| 2303 | rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0); |
| 2304 | if( rc!=SQLITE_OK ){ |
| 2305 | releasePage(pLastPg); |
| 2306 | return rc; |
| 2307 | } |
| 2308 | releasePage(pFreePg); |
| 2309 | }while( nFin!=0 && iFreePg>nFin ); |
| 2310 | assert( iFreePg<iLastPg ); |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 2311 | |
| 2312 | rc = sqlite3PagerWrite(pLastPg->pDbPage); |
danielk1977 | 662278e | 2007-11-05 15:30:12 +0000 | [diff] [blame] | 2313 | if( rc==SQLITE_OK ){ |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 2314 | rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, nFin!=0); |
danielk1977 | 662278e | 2007-11-05 15:30:12 +0000 | [diff] [blame] | 2315 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2316 | releasePage(pLastPg); |
| 2317 | if( rc!=SQLITE_OK ){ |
| 2318 | return rc; |
danielk1977 | 662278e | 2007-11-05 15:30:12 +0000 | [diff] [blame] | 2319 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2320 | } |
| 2321 | } |
| 2322 | |
| 2323 | pBt->nTrunc = iLastPg - 1; |
| 2324 | while( pBt->nTrunc==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, pBt->nTrunc) ){ |
| 2325 | pBt->nTrunc--; |
| 2326 | } |
| 2327 | return SQLITE_OK; |
| 2328 | } |
| 2329 | |
| 2330 | /* |
| 2331 | ** A write-transaction must be opened before calling this function. |
| 2332 | ** It performs a single unit of work towards an incremental vacuum. |
| 2333 | ** |
| 2334 | ** If the incremental vacuum is finished after this function has run, |
| 2335 | ** SQLITE_DONE is returned. If it is not finished, but no error occured, |
| 2336 | ** SQLITE_OK is returned. Otherwise an SQLite error code. |
| 2337 | */ |
| 2338 | int sqlite3BtreeIncrVacuum(Btree *p){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2339 | int rc; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2340 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2341 | |
| 2342 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2343 | pBt->db = p->db; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2344 | assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE ); |
| 2345 | if( !pBt->autoVacuum ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2346 | rc = SQLITE_DONE; |
| 2347 | }else{ |
| 2348 | invalidateAllOverflowCache(pBt); |
| 2349 | rc = incrVacuumStep(pBt, 0); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2350 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2351 | sqlite3BtreeLeave(p); |
| 2352 | return rc; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2353 | } |
| 2354 | |
| 2355 | /* |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2356 | ** This routine is called prior to sqlite3PagerCommit when a transaction |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2357 | ** is commited for an auto-vacuum database. |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 2358 | ** |
| 2359 | ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages |
| 2360 | ** the database file should be truncated to during the commit process. |
| 2361 | ** i.e. the database has been reorganized so that only the first *pnTrunc |
| 2362 | ** pages are in use. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2363 | */ |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 2364 | static int autoVacuumCommit(BtShared *pBt, Pgno *pnTrunc){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2365 | int rc = SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2366 | Pager *pPager = pBt->pPager; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2367 | #ifndef NDEBUG |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2368 | int nRef = sqlite3PagerRefcount(pPager); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2369 | #endif |
| 2370 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2371 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 2372 | invalidateAllOverflowCache(pBt); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2373 | assert(pBt->autoVacuum); |
| 2374 | if( !pBt->incrVacuum ){ |
| 2375 | Pgno nFin = 0; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2376 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2377 | if( pBt->nTrunc==0 ){ |
| 2378 | Pgno nFree; |
| 2379 | Pgno nPtrmap; |
| 2380 | const int pgsz = pBt->pageSize; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2381 | int nOrig = pagerPagecount(pBt->pPager); |
danielk1977 | e5321f0 | 2007-04-27 07:05:44 +0000 | [diff] [blame] | 2382 | |
| 2383 | if( PTRMAP_ISPAGE(pBt, nOrig) ){ |
| 2384 | return SQLITE_CORRUPT_BKPT; |
| 2385 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2386 | if( nOrig==PENDING_BYTE_PAGE(pBt) ){ |
| 2387 | nOrig--; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2388 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2389 | nFree = get4byte(&pBt->pPage1->aData[36]); |
| 2390 | nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+pgsz/5)/(pgsz/5); |
| 2391 | nFin = nOrig - nFree - nPtrmap; |
| 2392 | if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<=PENDING_BYTE_PAGE(pBt) ){ |
| 2393 | nFin--; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 2394 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2395 | while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){ |
| 2396 | nFin--; |
| 2397 | } |
| 2398 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2399 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2400 | while( rc==SQLITE_OK ){ |
| 2401 | rc = incrVacuumStep(pBt, nFin); |
| 2402 | } |
| 2403 | if( rc==SQLITE_DONE ){ |
| 2404 | assert(nFin==0 || pBt->nTrunc==0 || nFin<=pBt->nTrunc); |
| 2405 | rc = SQLITE_OK; |
danielk1977 | 0ba32df | 2008-05-07 07:13:16 +0000 | [diff] [blame] | 2406 | if( pBt->nTrunc && nFin ){ |
drh | 67f80b6 | 2007-07-23 19:26:17 +0000 | [diff] [blame] | 2407 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2408 | put4byte(&pBt->pPage1->aData[32], 0); |
| 2409 | put4byte(&pBt->pPage1->aData[36], 0); |
| 2410 | pBt->nTrunc = nFin; |
| 2411 | } |
| 2412 | } |
| 2413 | if( rc!=SQLITE_OK ){ |
| 2414 | sqlite3PagerRollback(pPager); |
| 2415 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2416 | } |
| 2417 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2418 | if( rc==SQLITE_OK ){ |
| 2419 | *pnTrunc = pBt->nTrunc; |
| 2420 | pBt->nTrunc = 0; |
| 2421 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2422 | assert( nRef==sqlite3PagerRefcount(pPager) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2423 | return rc; |
| 2424 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2425 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2426 | #endif |
| 2427 | |
| 2428 | /* |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2429 | ** This routine does the first phase of a two-phase commit. This routine |
| 2430 | ** causes a rollback journal to be created (if it does not already exist) |
| 2431 | ** and populated with enough information so that if a power loss occurs |
| 2432 | ** the database can be restored to its original state by playing back |
| 2433 | ** the journal. Then the contents of the journal are flushed out to |
| 2434 | ** the disk. After the journal is safely on oxide, the changes to the |
| 2435 | ** database are written into the database file and flushed to oxide. |
| 2436 | ** At the end of this call, the rollback journal still exists on the |
| 2437 | ** disk and we are still holding all locks, so the transaction has not |
| 2438 | ** committed. See sqlite3BtreeCommit() for the second phase of the |
| 2439 | ** commit process. |
| 2440 | ** |
| 2441 | ** This call is a no-op if no write-transaction is currently active on pBt. |
| 2442 | ** |
| 2443 | ** Otherwise, sync the database file for the btree pBt. zMaster points to |
| 2444 | ** the name of a master journal file that should be written into the |
| 2445 | ** individual journal file, or is NULL, indicating no master journal file |
| 2446 | ** (single database transaction). |
| 2447 | ** |
| 2448 | ** When this is called, the master journal should already have been |
| 2449 | ** created, populated with this journal pointer and synced to disk. |
| 2450 | ** |
| 2451 | ** Once this is routine has returned, the only thing required to commit |
| 2452 | ** the write-transaction for this database file is to delete the journal. |
| 2453 | */ |
| 2454 | int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){ |
| 2455 | int rc = SQLITE_OK; |
| 2456 | if( p->inTrans==TRANS_WRITE ){ |
| 2457 | BtShared *pBt = p->pBt; |
| 2458 | Pgno nTrunc = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2459 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2460 | pBt->db = p->db; |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2461 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2462 | if( pBt->autoVacuum ){ |
| 2463 | rc = autoVacuumCommit(pBt, &nTrunc); |
| 2464 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2465 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2466 | return rc; |
| 2467 | } |
| 2468 | } |
| 2469 | #endif |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 2470 | rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, nTrunc, 0); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2471 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2472 | } |
| 2473 | return rc; |
| 2474 | } |
| 2475 | |
| 2476 | /* |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2477 | ** Commit the transaction currently in progress. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2478 | ** |
drh | 6e34599 | 2007-03-30 11:12:08 +0000 | [diff] [blame] | 2479 | ** This routine implements the second phase of a 2-phase commit. The |
| 2480 | ** sqlite3BtreeSync() routine does the first phase and should be invoked |
| 2481 | ** prior to calling this routine. The sqlite3BtreeSync() routine did |
| 2482 | ** all the work of writing information out to disk and flushing the |
| 2483 | ** contents so that they are written onto the disk platter. All this |
| 2484 | ** routine has to do is delete or truncate the rollback journal |
| 2485 | ** (which causes the transaction to commit) and drop locks. |
| 2486 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2487 | ** This will release the write lock on the database file. If there |
| 2488 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2489 | */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2490 | int sqlite3BtreeCommitPhaseTwo(Btree *p){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2491 | BtShared *pBt = p->pBt; |
| 2492 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2493 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2494 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2495 | btreeIntegrity(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2496 | |
| 2497 | /* If the handle has a write-transaction open, commit the shared-btrees |
| 2498 | ** transaction and set the shared state to TRANS_READ. |
| 2499 | */ |
| 2500 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2501 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2502 | assert( pBt->inTransaction==TRANS_WRITE ); |
| 2503 | assert( pBt->nTransaction>0 ); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2504 | rc = sqlite3PagerCommitPhaseTwo(pBt->pPager); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2505 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2506 | sqlite3BtreeLeave(p); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2507 | return rc; |
| 2508 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2509 | pBt->inTransaction = TRANS_READ; |
| 2510 | pBt->inStmt = 0; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2511 | } |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2512 | unlockAllTables(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2513 | |
| 2514 | /* If the handle has any kind of transaction open, decrement the transaction |
| 2515 | ** count of the shared btree. If the transaction count reaches 0, set |
| 2516 | ** the shared state to TRANS_NONE. The unlockBtreeIfUnused() call below |
| 2517 | ** will unlock the pager. |
| 2518 | */ |
| 2519 | if( p->inTrans!=TRANS_NONE ){ |
| 2520 | pBt->nTransaction--; |
| 2521 | if( 0==pBt->nTransaction ){ |
| 2522 | pBt->inTransaction = TRANS_NONE; |
| 2523 | } |
| 2524 | } |
| 2525 | |
| 2526 | /* Set the handles current transaction state to TRANS_NONE and unlock |
| 2527 | ** the pager if this call closed the only read or write transaction. |
| 2528 | */ |
| 2529 | p->inTrans = TRANS_NONE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2530 | unlockBtreeIfUnused(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2531 | |
| 2532 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2533 | sqlite3BtreeLeave(p); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2534 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2535 | } |
| 2536 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2537 | /* |
| 2538 | ** Do both phases of a commit. |
| 2539 | */ |
| 2540 | int sqlite3BtreeCommit(Btree *p){ |
| 2541 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2542 | sqlite3BtreeEnter(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2543 | rc = sqlite3BtreeCommitPhaseOne(p, 0); |
| 2544 | if( rc==SQLITE_OK ){ |
| 2545 | rc = sqlite3BtreeCommitPhaseTwo(p); |
| 2546 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2547 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2548 | return rc; |
| 2549 | } |
| 2550 | |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2551 | #ifndef NDEBUG |
| 2552 | /* |
| 2553 | ** Return the number of write-cursors open on this handle. This is for use |
| 2554 | ** in assert() expressions, so it is only compiled if NDEBUG is not |
| 2555 | ** defined. |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2556 | ** |
| 2557 | ** For the purposes of this routine, a write-cursor is any cursor that |
| 2558 | ** is capable of writing to the databse. That means the cursor was |
| 2559 | ** originally opened for writing and the cursor has not be disabled |
| 2560 | ** by having its state changed to CURSOR_FAULT. |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2561 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2562 | static int countWriteCursors(BtShared *pBt){ |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2563 | BtCursor *pCur; |
| 2564 | int r = 0; |
| 2565 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2566 | if( pCur->wrFlag && pCur->eState!=CURSOR_FAULT ) r++; |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2567 | } |
| 2568 | return r; |
| 2569 | } |
| 2570 | #endif |
| 2571 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2572 | /* |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2573 | ** This routine sets the state to CURSOR_FAULT and the error |
| 2574 | ** code to errCode for every cursor on BtShared that pBtree |
| 2575 | ** references. |
| 2576 | ** |
| 2577 | ** Every cursor is tripped, including cursors that belong |
| 2578 | ** to other database connections that happen to be sharing |
| 2579 | ** the cache with pBtree. |
| 2580 | ** |
| 2581 | ** This routine gets called when a rollback occurs. |
| 2582 | ** All cursors using the same cache must be tripped |
| 2583 | ** to prevent them from trying to use the btree after |
| 2584 | ** the rollback. The rollback may have deleted tables |
| 2585 | ** or moved root pages, so it is not sufficient to |
| 2586 | ** save the state of the cursor. The cursor must be |
| 2587 | ** invalidated. |
| 2588 | */ |
| 2589 | void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){ |
| 2590 | BtCursor *p; |
| 2591 | sqlite3BtreeEnter(pBtree); |
| 2592 | for(p=pBtree->pBt->pCursor; p; p=p->pNext){ |
| 2593 | clearCursorPosition(p); |
| 2594 | p->eState = CURSOR_FAULT; |
| 2595 | p->skip = errCode; |
| 2596 | } |
| 2597 | sqlite3BtreeLeave(pBtree); |
| 2598 | } |
| 2599 | |
| 2600 | /* |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2601 | ** Rollback the transaction in progress. All cursors will be |
| 2602 | ** invalided by this operation. Any attempt to use a cursor |
| 2603 | ** that was open at the beginning of this operation will result |
| 2604 | ** in an error. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2605 | ** |
| 2606 | ** This will release the write lock on the database file. If there |
| 2607 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2608 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2609 | int sqlite3BtreeRollback(Btree *p){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2610 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2611 | BtShared *pBt = p->pBt; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2612 | MemPage *pPage1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2613 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2614 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2615 | pBt->db = p->db; |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2616 | rc = saveAllCursors(pBt, 0, 0); |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2617 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2618 | if( rc!=SQLITE_OK ){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2619 | /* This is a horrible situation. An IO or malloc() error occured whilst |
| 2620 | ** trying to save cursor positions. If this is an automatic rollback (as |
| 2621 | ** the result of a constraint, malloc() failure or IO error) then |
| 2622 | ** the cache may be internally inconsistent (not contain valid trees) so |
| 2623 | ** we cannot simply return the error to the caller. Instead, abort |
| 2624 | ** all queries that may be using any of the cursors that failed to save. |
| 2625 | */ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2626 | sqlite3BtreeTripAllCursors(p, rc); |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2627 | } |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2628 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2629 | btreeIntegrity(p); |
| 2630 | unlockAllTables(p); |
| 2631 | |
| 2632 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2633 | int rc2; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2634 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2635 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2636 | pBt->nTrunc = 0; |
| 2637 | #endif |
| 2638 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2639 | assert( TRANS_WRITE==pBt->inTransaction ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2640 | rc2 = sqlite3PagerRollback(pBt->pPager); |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2641 | if( rc2!=SQLITE_OK ){ |
| 2642 | rc = rc2; |
| 2643 | } |
| 2644 | |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2645 | /* The rollback may have destroyed the pPage1->aData value. So |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2646 | ** call sqlite3BtreeGetPage() on page 1 again to make |
| 2647 | ** sure pPage1->aData is set correctly. */ |
| 2648 | if( sqlite3BtreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2649 | releasePage(pPage1); |
| 2650 | } |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2651 | assert( countWriteCursors(pBt)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2652 | pBt->inTransaction = TRANS_READ; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2653 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2654 | |
| 2655 | if( p->inTrans!=TRANS_NONE ){ |
| 2656 | assert( pBt->nTransaction>0 ); |
| 2657 | pBt->nTransaction--; |
| 2658 | if( 0==pBt->nTransaction ){ |
| 2659 | pBt->inTransaction = TRANS_NONE; |
| 2660 | } |
| 2661 | } |
| 2662 | |
| 2663 | p->inTrans = TRANS_NONE; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2664 | pBt->inStmt = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2665 | unlockBtreeIfUnused(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2666 | |
| 2667 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2668 | sqlite3BtreeLeave(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2669 | return rc; |
| 2670 | } |
| 2671 | |
| 2672 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2673 | ** Start a statement subtransaction. The subtransaction can |
| 2674 | ** can be rolled back independently of the main transaction. |
| 2675 | ** You must start a transaction before starting a subtransaction. |
| 2676 | ** The subtransaction is ended automatically if the main transaction |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2677 | ** commits or rolls back. |
| 2678 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2679 | ** Only one subtransaction may be active at a time. It is an error to try |
| 2680 | ** to start a new subtransaction if another subtransaction is already active. |
| 2681 | ** |
| 2682 | ** Statement subtransactions are used around individual SQL statements |
| 2683 | ** that are contained within a BEGIN...COMMIT block. If a constraint |
| 2684 | ** error occurs within the statement, the effect of that one statement |
| 2685 | ** can be rolled back without having to rollback the entire transaction. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2686 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2687 | int sqlite3BtreeBeginStmt(Btree *p){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2688 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2689 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2690 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2691 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2692 | if( (p->inTrans!=TRANS_WRITE) || pBt->inStmt ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2693 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
| 2694 | }else{ |
| 2695 | assert( pBt->inTransaction==TRANS_WRITE ); |
| 2696 | rc = pBt->readOnly ? SQLITE_OK : sqlite3PagerStmtBegin(pBt->pPager); |
| 2697 | pBt->inStmt = 1; |
drh | 0d65dc0 | 2002-02-03 00:56:09 +0000 | [diff] [blame] | 2698 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2699 | sqlite3BtreeLeave(p); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2700 | return rc; |
| 2701 | } |
| 2702 | |
| 2703 | |
| 2704 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2705 | ** Commit the statment subtransaction currently in progress. If no |
| 2706 | ** subtransaction is active, this is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2707 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2708 | int sqlite3BtreeCommitStmt(Btree *p){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2709 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2710 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2711 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2712 | pBt->db = p->db; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2713 | if( pBt->inStmt && !pBt->readOnly ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2714 | rc = sqlite3PagerStmtCommit(pBt->pPager); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2715 | }else{ |
| 2716 | rc = SQLITE_OK; |
| 2717 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2718 | pBt->inStmt = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2719 | sqlite3BtreeLeave(p); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2720 | return rc; |
| 2721 | } |
| 2722 | |
| 2723 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2724 | ** Rollback the active statement subtransaction. If no subtransaction |
| 2725 | ** is active this routine is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2726 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2727 | ** All cursors will be invalidated by this operation. Any attempt |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2728 | ** to use a cursor that was open at the beginning of this operation |
| 2729 | ** will result in an error. |
| 2730 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2731 | int sqlite3BtreeRollbackStmt(Btree *p){ |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2732 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2733 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2734 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2735 | pBt->db = p->db; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2736 | if( pBt->inStmt && !pBt->readOnly ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2737 | rc = sqlite3PagerStmtRollback(pBt->pPager); |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2738 | pBt->inStmt = 0; |
| 2739 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2740 | sqlite3BtreeLeave(p); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2741 | return rc; |
| 2742 | } |
| 2743 | |
| 2744 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2745 | ** Create a new cursor for the BTree whose root is on the page |
| 2746 | ** iTable. The act of acquiring a cursor gets a read lock on |
| 2747 | ** the database file. |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 2748 | ** |
| 2749 | ** If wrFlag==0, then the cursor can only be used for reading. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2750 | ** If wrFlag==1, then the cursor can be used for reading or for |
| 2751 | ** writing if other conditions for writing are also met. These |
| 2752 | ** are the conditions that must be met in order for writing to |
| 2753 | ** be allowed: |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2754 | ** |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2755 | ** 1: The cursor must have been opened with wrFlag==1 |
| 2756 | ** |
drh | fe5d71d | 2007-03-19 11:54:10 +0000 | [diff] [blame] | 2757 | ** 2: Other database connections that share the same pager cache |
| 2758 | ** but which are not in the READ_UNCOMMITTED state may not have |
| 2759 | ** cursors open with wrFlag==0 on the same table. Otherwise |
| 2760 | ** the changes made by this write cursor would be visible to |
| 2761 | ** the read cursors in the other database connection. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2762 | ** |
| 2763 | ** 3: The database must be writable (not on read-only media) |
| 2764 | ** |
| 2765 | ** 4: There must be an active transaction. |
| 2766 | ** |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2767 | ** No checking is done to make sure that page iTable really is the |
| 2768 | ** root page of a b-tree. If it is not, then the cursor acquired |
| 2769 | ** will not work correctly. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2770 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2771 | static int btreeCursor( |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2772 | Btree *p, /* The btree */ |
| 2773 | int iTable, /* Root page of table to open */ |
| 2774 | int wrFlag, /* 1 to write. 0 read-only */ |
| 2775 | struct KeyInfo *pKeyInfo, /* First arg to comparison function */ |
| 2776 | BtCursor *pCur /* Space for new cursor */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2777 | ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2778 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2779 | BtShared *pBt = p->pBt; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2780 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2781 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2782 | if( wrFlag ){ |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2783 | if( pBt->readOnly ){ |
| 2784 | return SQLITE_READONLY; |
| 2785 | } |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 2786 | if( checkReadLocks(p, iTable, 0, 0) ){ |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2787 | return SQLITE_LOCKED; |
| 2788 | } |
drh | a0c9a11 | 2004-03-10 13:42:37 +0000 | [diff] [blame] | 2789 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2790 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2791 | if( pBt->pPage1==0 ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2792 | rc = lockBtreeWithRetry(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2793 | if( rc!=SQLITE_OK ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2794 | return rc; |
| 2795 | } |
drh | 1831f18 | 2007-04-24 17:35:59 +0000 | [diff] [blame] | 2796 | if( pBt->readOnly && wrFlag ){ |
| 2797 | return SQLITE_READONLY; |
| 2798 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2799 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2800 | pCur->pgnoRoot = (Pgno)iTable; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2801 | if( iTable==1 && pagerPagecount(pBt->pPager)==0 ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2802 | rc = SQLITE_EMPTY; |
| 2803 | goto create_cursor_exception; |
| 2804 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2805 | rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->pPage, 0); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2806 | if( rc!=SQLITE_OK ){ |
| 2807 | goto create_cursor_exception; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2808 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2809 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2810 | /* Now that no other errors can occur, finish filling in the BtCursor |
| 2811 | ** variables, link the cursor into the BtShared list and set *ppCur (the |
| 2812 | ** output argument to this function). |
| 2813 | */ |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2814 | pCur->pKeyInfo = pKeyInfo; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2815 | pCur->pBtree = p; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 2816 | pCur->pBt = pBt; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2817 | pCur->wrFlag = wrFlag; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2818 | pCur->pNext = pBt->pCursor; |
| 2819 | if( pCur->pNext ){ |
| 2820 | pCur->pNext->pPrev = pCur; |
| 2821 | } |
| 2822 | pBt->pCursor = pCur; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2823 | pCur->eState = CURSOR_INVALID; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2824 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2825 | return SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2826 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2827 | create_cursor_exception: |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 2828 | releasePage(pCur->pPage); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2829 | unlockBtreeIfUnused(pBt); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2830 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2831 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2832 | int sqlite3BtreeCursor( |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2833 | Btree *p, /* The btree */ |
| 2834 | int iTable, /* Root page of table to open */ |
| 2835 | int wrFlag, /* 1 to write. 0 read-only */ |
| 2836 | struct KeyInfo *pKeyInfo, /* First arg to xCompare() */ |
| 2837 | BtCursor *pCur /* Write new cursor here */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2838 | ){ |
| 2839 | int rc; |
| 2840 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2841 | p->pBt->db = p->db; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2842 | rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2843 | sqlite3BtreeLeave(p); |
| 2844 | return rc; |
| 2845 | } |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2846 | int sqlite3BtreeCursorSize(){ |
| 2847 | return sizeof(BtCursor); |
| 2848 | } |
| 2849 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2850 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2851 | |
| 2852 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2853 | ** Close a cursor. The read lock on the database file is released |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2854 | ** when the last cursor is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2855 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2856 | int sqlite3BtreeCloseCursor(BtCursor *pCur){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 2857 | Btree *pBtree = pCur->pBtree; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2858 | if( pBtree ){ |
| 2859 | BtShared *pBt = pCur->pBt; |
| 2860 | sqlite3BtreeEnter(pBtree); |
| 2861 | pBt->db = pBtree->db; |
| 2862 | clearCursorPosition(pCur); |
| 2863 | if( pCur->pPrev ){ |
| 2864 | pCur->pPrev->pNext = pCur->pNext; |
| 2865 | }else{ |
| 2866 | pBt->pCursor = pCur->pNext; |
| 2867 | } |
| 2868 | if( pCur->pNext ){ |
| 2869 | pCur->pNext->pPrev = pCur->pPrev; |
| 2870 | } |
| 2871 | releasePage(pCur->pPage); |
| 2872 | unlockBtreeIfUnused(pBt); |
| 2873 | invalidateOverflowCache(pCur); |
| 2874 | /* sqlite3_free(pCur); */ |
| 2875 | sqlite3BtreeLeave(pBtree); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2876 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2877 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2878 | } |
| 2879 | |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2880 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2881 | ** Make a temporary cursor by filling in the fields of pTempCur. |
| 2882 | ** The temporary cursor is not on the cursor list for the Btree. |
| 2883 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2884 | void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2885 | assert( cursorHoldsMutex(pCur) ); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2886 | memcpy(pTempCur, pCur, sizeof(*pCur)); |
| 2887 | pTempCur->pNext = 0; |
| 2888 | pTempCur->pPrev = 0; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2889 | if( pTempCur->pPage ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2890 | sqlite3PagerRef(pTempCur->pPage->pDbPage); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2891 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2892 | } |
| 2893 | |
| 2894 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2895 | ** Delete a temporary cursor such as was made by the CreateTemporaryCursor() |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2896 | ** function above. |
| 2897 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2898 | void sqlite3BtreeReleaseTempCursor(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2899 | assert( cursorHoldsMutex(pCur) ); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2900 | if( pCur->pPage ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2901 | sqlite3PagerUnref(pCur->pPage->pDbPage); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2902 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2903 | } |
| 2904 | |
| 2905 | /* |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2906 | ** Make sure the BtCursor* given in the argument has a valid |
| 2907 | ** BtCursor.info structure. If it is not already valid, call |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2908 | ** sqlite3BtreeParseCell() to fill it in. |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2909 | ** |
| 2910 | ** BtCursor.info is a cache of the information in the current cell. |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2911 | ** Using this cache reduces the number of calls to sqlite3BtreeParseCell(). |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2912 | ** |
| 2913 | ** 2007-06-25: There is a bug in some versions of MSVC that cause the |
| 2914 | ** compiler to crash when getCellInfo() is implemented as a macro. |
| 2915 | ** But there is a measureable speed advantage to using the macro on gcc |
| 2916 | ** (when less compiler optimizations like -Os or -O0 are used and the |
| 2917 | ** compiler is not doing agressive inlining.) So we use a real function |
| 2918 | ** for MSVC and a macro for everything else. Ticket #2457. |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2919 | */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2920 | #ifndef NDEBUG |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2921 | static void assertCellInfo(BtCursor *pCur){ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2922 | CellInfo info; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 2923 | memset(&info, 0, sizeof(info)); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2924 | sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &info); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2925 | assert( memcmp(&info, &pCur->info, sizeof(info))==0 ); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2926 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2927 | #else |
| 2928 | #define assertCellInfo(x) |
| 2929 | #endif |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2930 | #ifdef _MSC_VER |
| 2931 | /* Use a real function in MSVC to work around bugs in that compiler. */ |
| 2932 | static void getCellInfo(BtCursor *pCur){ |
| 2933 | if( pCur->info.nSize==0 ){ |
| 2934 | sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &pCur->info); |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 2935 | pCur->validNKey = 1; |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2936 | }else{ |
| 2937 | assertCellInfo(pCur); |
| 2938 | } |
| 2939 | } |
| 2940 | #else /* if not _MSC_VER */ |
| 2941 | /* Use a macro in all other compilers so that the function is inlined */ |
| 2942 | #define getCellInfo(pCur) \ |
| 2943 | if( pCur->info.nSize==0 ){ \ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2944 | sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &pCur->info); \ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 2945 | pCur->validNKey = 1; \ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2946 | }else{ \ |
| 2947 | assertCellInfo(pCur); \ |
| 2948 | } |
| 2949 | #endif /* _MSC_VER */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2950 | |
| 2951 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2952 | ** Set *pSize to the size of the buffer needed to hold the value of |
| 2953 | ** the key for the current entry. If the cursor is not pointing |
| 2954 | ** to a valid entry, *pSize is set to 0. |
| 2955 | ** |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2956 | ** For a table with the INTKEY flag set, this routine returns the key |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2957 | ** itself, not the number of bytes in the key. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2958 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 2959 | int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2960 | int rc; |
| 2961 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2962 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 2963 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2964 | if( rc==SQLITE_OK ){ |
| 2965 | assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID ); |
| 2966 | if( pCur->eState==CURSOR_INVALID ){ |
| 2967 | *pSize = 0; |
| 2968 | }else{ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2969 | getCellInfo(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2970 | *pSize = pCur->info.nKey; |
| 2971 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2972 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2973 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2974 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2975 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2976 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2977 | ** Set *pSize to the number of bytes of data in the entry the |
| 2978 | ** cursor currently points to. Always return SQLITE_OK. |
| 2979 | ** Failure is not possible. If the cursor is not currently |
| 2980 | ** pointing to an entry (which can happen, for example, if |
| 2981 | ** the database is empty) then *pSize is set to 0. |
| 2982 | */ |
| 2983 | int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2984 | int rc; |
| 2985 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2986 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 2987 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2988 | if( rc==SQLITE_OK ){ |
| 2989 | assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID ); |
| 2990 | if( pCur->eState==CURSOR_INVALID ){ |
| 2991 | /* Not pointing at a valid entry - set *pSize to 0. */ |
| 2992 | *pSize = 0; |
| 2993 | }else{ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2994 | getCellInfo(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2995 | *pSize = pCur->info.nData; |
| 2996 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2997 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2998 | return rc; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2999 | } |
| 3000 | |
| 3001 | /* |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3002 | ** Given the page number of an overflow page in the database (parameter |
| 3003 | ** ovfl), this function finds the page number of the next page in the |
| 3004 | ** linked list of overflow pages. If possible, it uses the auto-vacuum |
| 3005 | ** pointer-map data instead of reading the content of page ovfl to do so. |
| 3006 | ** |
| 3007 | ** If an error occurs an SQLite error code is returned. Otherwise: |
| 3008 | ** |
| 3009 | ** Unless pPgnoNext is NULL, the page number of the next overflow |
| 3010 | ** page in the linked list is written to *pPgnoNext. If page ovfl |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 3011 | ** is the last page in its linked list, *pPgnoNext is set to zero. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3012 | ** |
| 3013 | ** If ppPage is not NULL, *ppPage is set to the MemPage* handle |
| 3014 | ** for page ovfl. The underlying pager page may have been requested |
| 3015 | ** with the noContent flag set, so the page data accessable via |
| 3016 | ** this handle may not be trusted. |
| 3017 | */ |
| 3018 | static int getOverflowPage( |
| 3019 | BtShared *pBt, |
| 3020 | Pgno ovfl, /* Overflow page */ |
| 3021 | MemPage **ppPage, /* OUT: MemPage handle */ |
| 3022 | Pgno *pPgnoNext /* OUT: Next overflow page number */ |
| 3023 | ){ |
| 3024 | Pgno next = 0; |
| 3025 | int rc; |
| 3026 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3027 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3028 | /* One of these must not be NULL. Otherwise, why call this function? */ |
| 3029 | assert(ppPage || pPgnoNext); |
| 3030 | |
| 3031 | /* If pPgnoNext is NULL, then this function is being called to obtain |
| 3032 | ** a MemPage* reference only. No page-data is required in this case. |
| 3033 | */ |
| 3034 | if( !pPgnoNext ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3035 | return sqlite3BtreeGetPage(pBt, ovfl, ppPage, 1); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3036 | } |
| 3037 | |
| 3038 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3039 | /* Try to find the next page in the overflow list using the |
| 3040 | ** autovacuum pointer-map pages. Guess that the next page in |
| 3041 | ** the overflow list is page number (ovfl+1). If that guess turns |
| 3042 | ** out to be wrong, fall back to loading the data of page |
| 3043 | ** number ovfl to determine the next page number. |
| 3044 | */ |
| 3045 | if( pBt->autoVacuum ){ |
| 3046 | Pgno pgno; |
| 3047 | Pgno iGuess = ovfl+1; |
| 3048 | u8 eType; |
| 3049 | |
| 3050 | while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){ |
| 3051 | iGuess++; |
| 3052 | } |
| 3053 | |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 3054 | if( iGuess<=pagerPagecount(pBt->pPager) ){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3055 | rc = ptrmapGet(pBt, iGuess, &eType, &pgno); |
| 3056 | if( rc!=SQLITE_OK ){ |
| 3057 | return rc; |
| 3058 | } |
| 3059 | if( eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){ |
| 3060 | next = iGuess; |
| 3061 | } |
| 3062 | } |
| 3063 | } |
| 3064 | #endif |
| 3065 | |
| 3066 | if( next==0 || ppPage ){ |
| 3067 | MemPage *pPage = 0; |
| 3068 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3069 | rc = sqlite3BtreeGetPage(pBt, ovfl, &pPage, next!=0); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3070 | assert(rc==SQLITE_OK || pPage==0); |
| 3071 | if( next==0 && rc==SQLITE_OK ){ |
| 3072 | next = get4byte(pPage->aData); |
| 3073 | } |
| 3074 | |
| 3075 | if( ppPage ){ |
| 3076 | *ppPage = pPage; |
| 3077 | }else{ |
| 3078 | releasePage(pPage); |
| 3079 | } |
| 3080 | } |
| 3081 | *pPgnoNext = next; |
| 3082 | |
| 3083 | return rc; |
| 3084 | } |
| 3085 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3086 | /* |
| 3087 | ** Copy data from a buffer to a page, or from a page to a buffer. |
| 3088 | ** |
| 3089 | ** pPayload is a pointer to data stored on database page pDbPage. |
| 3090 | ** If argument eOp is false, then nByte bytes of data are copied |
| 3091 | ** from pPayload to the buffer pointed at by pBuf. If eOp is true, |
| 3092 | ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes |
| 3093 | ** of data are copied from the buffer pBuf to pPayload. |
| 3094 | ** |
| 3095 | ** SQLITE_OK is returned on success, otherwise an error code. |
| 3096 | */ |
| 3097 | static int copyPayload( |
| 3098 | void *pPayload, /* Pointer to page data */ |
| 3099 | void *pBuf, /* Pointer to buffer */ |
| 3100 | int nByte, /* Number of bytes to copy */ |
| 3101 | int eOp, /* 0 -> copy from page, 1 -> copy to page */ |
| 3102 | DbPage *pDbPage /* Page containing pPayload */ |
| 3103 | ){ |
| 3104 | if( eOp ){ |
| 3105 | /* Copy data from buffer to page (a write operation) */ |
| 3106 | int rc = sqlite3PagerWrite(pDbPage); |
| 3107 | if( rc!=SQLITE_OK ){ |
| 3108 | return rc; |
| 3109 | } |
| 3110 | memcpy(pPayload, pBuf, nByte); |
| 3111 | }else{ |
| 3112 | /* Copy data from page to buffer (a read operation) */ |
| 3113 | memcpy(pBuf, pPayload, nByte); |
| 3114 | } |
| 3115 | return SQLITE_OK; |
| 3116 | } |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3117 | |
| 3118 | /* |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3119 | ** This function is used to read or overwrite payload information |
| 3120 | ** for the entry that the pCur cursor is pointing to. If the eOp |
| 3121 | ** parameter is 0, this is a read operation (data copied into |
| 3122 | ** buffer pBuf). If it is non-zero, a write (data copied from |
| 3123 | ** buffer pBuf). |
| 3124 | ** |
| 3125 | ** A total of "amt" bytes are read or written beginning at "offset". |
| 3126 | ** Data is read to or from the buffer pBuf. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3127 | ** |
| 3128 | ** This routine does not make a distinction between key and data. |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3129 | ** It just reads or writes bytes from the payload area. Data might |
| 3130 | ** appear on the main page or be scattered out on multiple overflow |
| 3131 | ** pages. |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3132 | ** |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 3133 | ** If the BtCursor.isIncrblobHandle flag is set, and the current |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3134 | ** cursor entry uses one or more overflow pages, this function |
| 3135 | ** allocates space for and lazily popluates the overflow page-list |
| 3136 | ** cache array (BtCursor.aOverflow). Subsequent calls use this |
| 3137 | ** cache to make seeking to the supplied offset more efficient. |
| 3138 | ** |
| 3139 | ** Once an overflow page-list cache has been allocated, it may be |
| 3140 | ** invalidated if some other cursor writes to the same table, or if |
| 3141 | ** the cursor is moved to a different row. Additionally, in auto-vacuum |
| 3142 | ** mode, the following events may invalidate an overflow page-list cache. |
| 3143 | ** |
| 3144 | ** * An incremental vacuum, |
| 3145 | ** * A commit in auto_vacuum="full" mode, |
| 3146 | ** * Creating a table (may require moving an overflow page). |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3147 | */ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3148 | static int accessPayload( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3149 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
| 3150 | int offset, /* Begin reading this far into payload */ |
| 3151 | int amt, /* Read this many bytes */ |
| 3152 | unsigned char *pBuf, /* Write the bytes into this buffer */ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3153 | int skipKey, /* offset begins at data if this is true */ |
| 3154 | int eOp /* zero to read. non-zero to write. */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3155 | ){ |
| 3156 | unsigned char *aPayload; |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3157 | int rc = SQLITE_OK; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3158 | u32 nKey; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3159 | int iIdx = 0; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 3160 | MemPage *pPage = pCur->pPage; /* Btree page of current cursor entry */ |
drh | 51f015e | 2007-10-16 19:45:29 +0000 | [diff] [blame] | 3161 | BtShared *pBt; /* Btree this cursor belongs to */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3162 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3163 | assert( pPage ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3164 | assert( pCur->eState==CURSOR_VALID ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3165 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3166 | assert( offset>=0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3167 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3168 | |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3169 | getCellInfo(pCur); |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3170 | aPayload = pCur->info.pCell + pCur->info.nHeader; |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3171 | nKey = (pPage->intKey ? 0 : pCur->info.nKey); |
| 3172 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3173 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3174 | offset += nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3175 | } |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3176 | if( offset+amt > nKey+pCur->info.nData ){ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3177 | /* 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] | 3178 | return SQLITE_ERROR; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3179 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3180 | |
| 3181 | /* Check if data must be read/written to/from the btree page itself. */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3182 | if( offset<pCur->info.nLocal ){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3183 | int a = amt; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3184 | if( a+offset>pCur->info.nLocal ){ |
| 3185 | a = pCur->info.nLocal - offset; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3186 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3187 | rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3188 | offset = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3189 | pBuf += a; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3190 | amt -= a; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 3191 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3192 | offset -= pCur->info.nLocal; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3193 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3194 | |
drh | 51f015e | 2007-10-16 19:45:29 +0000 | [diff] [blame] | 3195 | pBt = pCur->pBt; |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3196 | if( rc==SQLITE_OK && amt>0 ){ |
| 3197 | const int ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */ |
| 3198 | Pgno nextPage; |
| 3199 | |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3200 | nextPage = get4byte(&aPayload[pCur->info.nLocal]); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3201 | |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3202 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 3203 | /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[] |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3204 | ** has not been allocated, allocate it now. The array is sized at |
| 3205 | ** one entry for each overflow page in the overflow chain. The |
| 3206 | ** page number of the first overflow page is stored in aOverflow[0], |
| 3207 | ** etc. A value of 0 in the aOverflow[] array means "not yet known" |
| 3208 | ** (the cache is lazily populated). |
| 3209 | */ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 3210 | if( pCur->isIncrblobHandle && !pCur->aOverflow ){ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3211 | int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3212 | pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl); |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3213 | if( nOvfl && !pCur->aOverflow ){ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3214 | rc = SQLITE_NOMEM; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3215 | } |
| 3216 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3217 | |
| 3218 | /* If the overflow page-list cache has been allocated and the |
| 3219 | ** entry for the first required overflow page is valid, skip |
| 3220 | ** directly to it. |
| 3221 | */ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3222 | if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){ |
| 3223 | iIdx = (offset/ovflSize); |
| 3224 | nextPage = pCur->aOverflow[iIdx]; |
| 3225 | offset = (offset%ovflSize); |
| 3226 | } |
| 3227 | #endif |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3228 | |
| 3229 | for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){ |
| 3230 | |
| 3231 | #ifndef SQLITE_OMIT_INCRBLOB |
| 3232 | /* If required, populate the overflow page-list cache. */ |
| 3233 | if( pCur->aOverflow ){ |
| 3234 | assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage); |
| 3235 | pCur->aOverflow[iIdx] = nextPage; |
| 3236 | } |
| 3237 | #endif |
| 3238 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3239 | if( offset>=ovflSize ){ |
| 3240 | /* The only reason to read this page is to obtain the page |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3241 | ** number for the next page in the overflow chain. The page |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 3242 | ** data is not required. So first try to lookup the overflow |
| 3243 | ** page-list cache, if any, then fall back to the getOverflowPage() |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3244 | ** function. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3245 | */ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3246 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3247 | if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){ |
| 3248 | nextPage = pCur->aOverflow[iIdx+1]; |
| 3249 | } else |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3250 | #endif |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3251 | rc = getOverflowPage(pBt, nextPage, 0, &nextPage); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3252 | offset -= ovflSize; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3253 | }else{ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3254 | /* Need to read this page properly. It contains some of the |
| 3255 | ** range of data that is being read (eOp==0) or written (eOp!=0). |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3256 | */ |
| 3257 | DbPage *pDbPage; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3258 | int a = amt; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3259 | rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3260 | if( rc==SQLITE_OK ){ |
| 3261 | aPayload = sqlite3PagerGetData(pDbPage); |
| 3262 | nextPage = get4byte(aPayload); |
| 3263 | if( a + offset > ovflSize ){ |
| 3264 | a = ovflSize - offset; |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3265 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3266 | rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage); |
| 3267 | sqlite3PagerUnref(pDbPage); |
| 3268 | offset = 0; |
| 3269 | amt -= a; |
| 3270 | pBuf += a; |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3271 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3272 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3273 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3274 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3275 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3276 | if( rc==SQLITE_OK && amt>0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3277 | return SQLITE_CORRUPT_BKPT; |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 3278 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3279 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3280 | } |
| 3281 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3282 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3283 | ** Read part of the key associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3284 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3285 | ** begins at "offset". |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3286 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3287 | ** Return SQLITE_OK on success or an error code if anything goes |
| 3288 | ** wrong. An error is returned if "offset+amt" is larger than |
| 3289 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3290 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3291 | int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3292 | int rc; |
| 3293 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3294 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 3295 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3296 | if( rc==SQLITE_OK ){ |
| 3297 | assert( pCur->eState==CURSOR_VALID ); |
| 3298 | assert( pCur->pPage!=0 ); |
| 3299 | if( pCur->pPage->intKey ){ |
| 3300 | return SQLITE_CORRUPT_BKPT; |
| 3301 | } |
| 3302 | assert( pCur->pPage->intKey==0 ); |
| 3303 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3304 | rc = accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0, 0); |
drh | 6575a22 | 2005-03-10 17:06:34 +0000 | [diff] [blame] | 3305 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3306 | return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3307 | } |
| 3308 | |
| 3309 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3310 | ** Read part of the data associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3311 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3312 | ** begins at "offset". |
| 3313 | ** |
| 3314 | ** Return SQLITE_OK on success or an error code if anything goes |
| 3315 | ** wrong. An error is returned if "offset+amt" is larger than |
| 3316 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3317 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3318 | int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3319 | int rc; |
| 3320 | |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 3321 | #ifndef SQLITE_OMIT_INCRBLOB |
| 3322 | if ( pCur->eState==CURSOR_INVALID ){ |
| 3323 | return SQLITE_ABORT; |
| 3324 | } |
| 3325 | #endif |
| 3326 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3327 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 3328 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3329 | if( rc==SQLITE_OK ){ |
| 3330 | assert( pCur->eState==CURSOR_VALID ); |
| 3331 | assert( pCur->pPage!=0 ); |
| 3332 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3333 | rc = accessPayload(pCur, offset, amt, pBuf, 1, 0); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3334 | } |
| 3335 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3336 | } |
| 3337 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3338 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3339 | ** Return a pointer to payload information from the entry that the |
| 3340 | ** pCur cursor is pointing to. The pointer is to the beginning of |
| 3341 | ** 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] | 3342 | ** skipKey==1. The number of bytes of available key/data is written |
| 3343 | ** into *pAmt. If *pAmt==0, then the value returned will not be |
| 3344 | ** a valid pointer. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3345 | ** |
| 3346 | ** This routine is an optimization. It is common for the entire key |
| 3347 | ** and data to fit on the local page and for there to be no overflow |
| 3348 | ** pages. When that is so, this routine can be used to access the |
| 3349 | ** 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] | 3350 | ** onto overflow pages, then accessPayload() must be used to reassembly |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3351 | ** the key/data and copy it into a preallocated buffer. |
| 3352 | ** |
| 3353 | ** The pointer returned by this routine looks directly into the cached |
| 3354 | ** page of the database. The data might change or move the next time |
| 3355 | ** any btree routine is called. |
| 3356 | */ |
| 3357 | static const unsigned char *fetchPayload( |
| 3358 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3359 | int *pAmt, /* Write the number of available bytes here */ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3360 | int skipKey /* read beginning at data if this is true */ |
| 3361 | ){ |
| 3362 | unsigned char *aPayload; |
| 3363 | MemPage *pPage; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3364 | u32 nKey; |
| 3365 | int nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3366 | |
| 3367 | assert( pCur!=0 && pCur->pPage!=0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3368 | assert( pCur->eState==CURSOR_VALID ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3369 | assert( cursorHoldsMutex(pCur) ); |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3370 | pPage = pCur->pPage; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3371 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3372 | getCellInfo(pCur); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3373 | aPayload = pCur->info.pCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3374 | aPayload += pCur->info.nHeader; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3375 | if( pPage->intKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3376 | nKey = 0; |
| 3377 | }else{ |
| 3378 | nKey = pCur->info.nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3379 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3380 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3381 | aPayload += nKey; |
| 3382 | nLocal = pCur->info.nLocal - nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3383 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3384 | nLocal = pCur->info.nLocal; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3385 | if( nLocal>nKey ){ |
| 3386 | nLocal = nKey; |
| 3387 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3388 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3389 | *pAmt = nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3390 | return aPayload; |
| 3391 | } |
| 3392 | |
| 3393 | |
| 3394 | /* |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3395 | ** For the entry that cursor pCur is point to, return as |
| 3396 | ** many bytes of the key or data as are available on the local |
| 3397 | ** b-tree page. Write the number of available bytes into *pAmt. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3398 | ** |
| 3399 | ** The pointer returned is ephemeral. The key/data may move |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3400 | ** or be destroyed on the next call to any Btree routine, |
| 3401 | ** including calls from other threads against the same cache. |
| 3402 | ** Hence, a mutex on the BtShared should be held prior to calling |
| 3403 | ** this routine. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3404 | ** |
| 3405 | ** These routines is used to get quick access to key and data |
| 3406 | ** in the common case where no overflow pages are used. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3407 | */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3408 | const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3409 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3410 | if( pCur->eState==CURSOR_VALID ){ |
| 3411 | return (const void*)fetchPayload(pCur, pAmt, 0); |
| 3412 | } |
| 3413 | return 0; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3414 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3415 | const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){ |
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 | if( pCur->eState==CURSOR_VALID ){ |
| 3418 | return (const void*)fetchPayload(pCur, pAmt, 1); |
| 3419 | } |
| 3420 | return 0; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3421 | } |
| 3422 | |
| 3423 | |
| 3424 | /* |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3425 | ** 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] | 3426 | ** page number of the child page to move to. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3427 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3428 | static int moveToChild(BtCursor *pCur, u32 newPgno){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3429 | int rc; |
| 3430 | MemPage *pNewPage; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3431 | MemPage *pOldPage; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 3432 | BtShared *pBt = pCur->pBt; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3433 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3434 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3435 | assert( pCur->eState==CURSOR_VALID ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 3436 | rc = getAndInitPage(pBt, newPgno, &pNewPage, pCur->pPage); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3437 | if( rc ) return rc; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3438 | pNewPage->idxParent = pCur->idx; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3439 | pOldPage = pCur->pPage; |
| 3440 | pOldPage->idxShift = 0; |
| 3441 | releasePage(pOldPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3442 | pCur->pPage = pNewPage; |
| 3443 | pCur->idx = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3444 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3445 | pCur->validNKey = 0; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 3446 | if( pNewPage->nCell<1 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3447 | return SQLITE_CORRUPT_BKPT; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 3448 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3449 | return SQLITE_OK; |
| 3450 | } |
| 3451 | |
| 3452 | /* |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3453 | ** Return true if the page is the virtual root of its table. |
| 3454 | ** |
| 3455 | ** The virtual root page is the root page for most tables. But |
| 3456 | ** for the table rooted on page 1, sometime the real root page |
| 3457 | ** is empty except for the right-pointer. In such cases the |
| 3458 | ** virtual root page is the page that the right-pointer of page |
| 3459 | ** 1 is pointing to. |
| 3460 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3461 | int sqlite3BtreeIsRootPage(MemPage *pPage){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3462 | MemPage *pParent; |
| 3463 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3464 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3465 | pParent = pPage->pParent; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3466 | if( pParent==0 ) return 1; |
| 3467 | if( pParent->pgno>1 ) return 0; |
| 3468 | if( get2byte(&pParent->aData[pParent->hdrOffset+3])==0 ) return 1; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3469 | return 0; |
| 3470 | } |
| 3471 | |
| 3472 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3473 | ** Move the cursor up to the parent page. |
| 3474 | ** |
| 3475 | ** pCur->idx is set to the cell index that contains the pointer |
| 3476 | ** to the page we are coming from. If we are coming from the |
| 3477 | ** right-most child page then pCur->idx is set to one more than |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3478 | ** the largest cell index. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3479 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3480 | void sqlite3BtreeMoveToParent(BtCursor *pCur){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3481 | MemPage *pParent; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3482 | MemPage *pPage; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3483 | int idxParent; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3484 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3485 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3486 | assert( pCur->eState==CURSOR_VALID ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3487 | pPage = pCur->pPage; |
| 3488 | assert( pPage!=0 ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3489 | assert( !sqlite3BtreeIsRootPage(pPage) ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3490 | pParent = pPage->pParent; |
| 3491 | assert( pParent!=0 ); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3492 | assert( pPage->pDbPage->nRef>0 ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3493 | idxParent = pPage->idxParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3494 | sqlite3PagerRef(pParent->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3495 | releasePage(pPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3496 | pCur->pPage = pParent; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3497 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3498 | pCur->validNKey = 0; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3499 | assert( pParent->idxShift==0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3500 | pCur->idx = idxParent; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3501 | } |
| 3502 | |
| 3503 | /* |
| 3504 | ** Move the cursor to the root page |
| 3505 | */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3506 | static int moveToRoot(BtCursor *pCur){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3507 | MemPage *pRoot; |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3508 | int rc = SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3509 | Btree *p = pCur->pBtree; |
| 3510 | BtShared *pBt = p->pBt; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3511 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3512 | assert( cursorHoldsMutex(pCur) ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 3513 | assert( CURSOR_INVALID < CURSOR_REQUIRESEEK ); |
| 3514 | assert( CURSOR_VALID < CURSOR_REQUIRESEEK ); |
| 3515 | assert( CURSOR_FAULT > CURSOR_REQUIRESEEK ); |
| 3516 | if( pCur->eState>=CURSOR_REQUIRESEEK ){ |
| 3517 | if( pCur->eState==CURSOR_FAULT ){ |
| 3518 | return pCur->skip; |
| 3519 | } |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 3520 | clearCursorPosition(pCur); |
| 3521 | } |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3522 | pRoot = pCur->pPage; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 3523 | if( pRoot && pRoot->pgno==pCur->pgnoRoot ){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3524 | assert( pRoot->isInit ); |
| 3525 | }else{ |
| 3526 | if( |
| 3527 | SQLITE_OK!=(rc = getAndInitPage(pBt, pCur->pgnoRoot, &pRoot, 0)) |
| 3528 | ){ |
| 3529 | pCur->eState = CURSOR_INVALID; |
| 3530 | return rc; |
| 3531 | } |
| 3532 | releasePage(pCur->pPage); |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3533 | pCur->pPage = pRoot; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3534 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3535 | pCur->idx = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3536 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3537 | pCur->atLast = 0; |
| 3538 | pCur->validNKey = 0; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3539 | if( pRoot->nCell==0 && !pRoot->leaf ){ |
| 3540 | Pgno subpage; |
| 3541 | assert( pRoot->pgno==1 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3542 | subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3543 | assert( subpage>0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3544 | pCur->eState = CURSOR_VALID; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3545 | rc = moveToChild(pCur, subpage); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3546 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3547 | pCur->eState = ((pCur->pPage->nCell>0)?CURSOR_VALID:CURSOR_INVALID); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3548 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3549 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3550 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3551 | /* |
| 3552 | ** Move the cursor down to the left-most leaf entry beneath the |
| 3553 | ** entry to which it is currently pointing. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3554 | ** |
| 3555 | ** The left-most leaf is the one with the smallest key - the first |
| 3556 | ** in ascending order. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3557 | */ |
| 3558 | static int moveToLeftmost(BtCursor *pCur){ |
| 3559 | Pgno pgno; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3560 | int rc = SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3561 | MemPage *pPage; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3562 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3563 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3564 | assert( pCur->eState==CURSOR_VALID ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3565 | while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3566 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3567 | pgno = get4byte(findCell(pPage, pCur->idx)); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3568 | rc = moveToChild(pCur, pgno); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3569 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3570 | return rc; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3571 | } |
| 3572 | |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3573 | /* |
| 3574 | ** Move the cursor down to the right-most leaf entry beneath the |
| 3575 | ** page to which it is currently pointing. Notice the difference |
| 3576 | ** between moveToLeftmost() and moveToRightmost(). moveToLeftmost() |
| 3577 | ** finds the left-most entry beneath the *entry* whereas moveToRightmost() |
| 3578 | ** finds the right-most entry beneath the *page*. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3579 | ** |
| 3580 | ** The right-most entry is the one with the largest key - the last |
| 3581 | ** key in ascending order. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3582 | */ |
| 3583 | static int moveToRightmost(BtCursor *pCur){ |
| 3584 | Pgno pgno; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3585 | int rc = SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3586 | MemPage *pPage; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3587 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3588 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3589 | assert( pCur->eState==CURSOR_VALID ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3590 | while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3591 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3592 | pCur->idx = pPage->nCell; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3593 | rc = moveToChild(pCur, pgno); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3594 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3595 | if( rc==SQLITE_OK ){ |
| 3596 | pCur->idx = pPage->nCell - 1; |
| 3597 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3598 | pCur->validNKey = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3599 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3600 | return SQLITE_OK; |
| 3601 | } |
| 3602 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3603 | /* Move the cursor to the first entry in the table. Return SQLITE_OK |
| 3604 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 3605 | ** or set *pRes to 1 if the table is empty. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3606 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3607 | int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3608 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3609 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3610 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3611 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3612 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3613 | if( rc==SQLITE_OK ){ |
| 3614 | if( pCur->eState==CURSOR_INVALID ){ |
| 3615 | assert( pCur->pPage->nCell==0 ); |
| 3616 | *pRes = 1; |
| 3617 | rc = SQLITE_OK; |
| 3618 | }else{ |
| 3619 | assert( pCur->pPage->nCell>0 ); |
| 3620 | *pRes = 0; |
| 3621 | rc = moveToLeftmost(pCur); |
| 3622 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3623 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3624 | return rc; |
| 3625 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3626 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3627 | /* Move the cursor to the last entry in the table. Return SQLITE_OK |
| 3628 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 3629 | ** or set *pRes to 1 if the table is empty. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3630 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3631 | int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3632 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3633 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3634 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3635 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3636 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3637 | if( rc==SQLITE_OK ){ |
| 3638 | if( CURSOR_INVALID==pCur->eState ){ |
| 3639 | assert( pCur->pPage->nCell==0 ); |
| 3640 | *pRes = 1; |
| 3641 | }else{ |
| 3642 | assert( pCur->eState==CURSOR_VALID ); |
| 3643 | *pRes = 0; |
| 3644 | rc = moveToRightmost(pCur); |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3645 | getCellInfo(pCur); |
| 3646 | pCur->atLast = rc==SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3647 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3648 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3649 | return rc; |
| 3650 | } |
| 3651 | |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 3652 | /* Move the cursor so that it points to an entry near the key |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3653 | ** specified by pIdxKey or intKey. Return a success code. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3654 | ** |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3655 | ** For INTKEY tables, the intKey parameter is used. pIdxKey |
| 3656 | ** must be NULL. For index tables, pIdxKey is used and intKey |
| 3657 | ** is ignored. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3658 | ** |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3659 | ** If an exact match is not found, then the cursor is always |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3660 | ** left pointing at a leaf page which would hold the entry if it |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3661 | ** were present. The cursor might point to an entry that comes |
| 3662 | ** before or after the key. |
| 3663 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3664 | ** The result of comparing the key with the entry to which the |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 3665 | ** cursor is written to *pRes if pRes!=NULL. The meaning of |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3666 | ** this value is as follows: |
| 3667 | ** |
| 3668 | ** *pRes<0 The cursor is left pointing at an entry that |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3669 | ** is smaller than pKey or if the table is empty |
| 3670 | ** and the cursor is therefore left point to nothing. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3671 | ** |
| 3672 | ** *pRes==0 The cursor is left pointing at an entry that |
| 3673 | ** exactly matches pKey. |
| 3674 | ** |
| 3675 | ** *pRes>0 The cursor is left pointing at an entry that |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 3676 | ** is larger than pKey. |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3677 | ** |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3678 | */ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3679 | int sqlite3BtreeMovetoUnpacked( |
| 3680 | BtCursor *pCur, /* The cursor to be moved */ |
| 3681 | UnpackedRecord *pIdxKey, /* Unpacked index key */ |
| 3682 | i64 intKey, /* The table key */ |
| 3683 | int biasRight, /* If true, bias the search to the high end */ |
| 3684 | int *pRes /* Write search results here */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3685 | ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3686 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3687 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3688 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3689 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3690 | |
| 3691 | /* If the cursor is already positioned at the point we are trying |
| 3692 | ** to move to, then just return without doing any work */ |
| 3693 | if( pCur->eState==CURSOR_VALID && pCur->validNKey && pCur->pPage->intKey ){ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3694 | if( pCur->info.nKey==intKey ){ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3695 | *pRes = 0; |
| 3696 | return SQLITE_OK; |
| 3697 | } |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3698 | if( pCur->atLast && pCur->info.nKey<intKey ){ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3699 | *pRes = -1; |
| 3700 | return SQLITE_OK; |
| 3701 | } |
| 3702 | } |
| 3703 | |
| 3704 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3705 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3706 | if( rc ){ |
| 3707 | return rc; |
| 3708 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3709 | assert( pCur->pPage ); |
| 3710 | assert( pCur->pPage->isInit ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3711 | if( pCur->eState==CURSOR_INVALID ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3712 | *pRes = -1; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3713 | assert( pCur->pPage->nCell==0 ); |
| 3714 | return SQLITE_OK; |
| 3715 | } |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3716 | assert( pCur->pPage->intKey || pIdxKey ); |
drh | 1468438 | 2006-11-30 13:05:29 +0000 | [diff] [blame] | 3717 | for(;;){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3718 | int lwr, upr; |
| 3719 | Pgno chldPg; |
| 3720 | MemPage *pPage = pCur->pPage; |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3721 | int c = -1; /* pRes return if table is empty must be -1 */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3722 | lwr = 0; |
| 3723 | upr = pPage->nCell-1; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3724 | if( !pPage->intKey && pIdxKey==0 ){ |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3725 | rc = SQLITE_CORRUPT_BKPT; |
| 3726 | goto moveto_finish; |
drh | 4eec4c1 | 2005-01-21 00:22:37 +0000 | [diff] [blame] | 3727 | } |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3728 | if( biasRight ){ |
| 3729 | pCur->idx = upr; |
| 3730 | }else{ |
| 3731 | pCur->idx = (upr+lwr)/2; |
| 3732 | } |
drh | f1d68b3 | 2007-03-29 04:43:26 +0000 | [diff] [blame] | 3733 | if( lwr<=upr ) for(;;){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3734 | void *pCellKey; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3735 | i64 nCellKey; |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3736 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3737 | pCur->validNKey = 1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3738 | if( pPage->intKey ){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3739 | u8 *pCell; |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3740 | pCell = findCell(pPage, pCur->idx) + pPage->childPtrSize; |
drh | d172f86 | 2006-01-12 15:01:15 +0000 | [diff] [blame] | 3741 | if( pPage->hasData ){ |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 3742 | u32 dummy; |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 3743 | pCell += getVarint32(pCell, dummy); |
drh | d172f86 | 2006-01-12 15:01:15 +0000 | [diff] [blame] | 3744 | } |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3745 | getVarint(pCell, (u64*)&nCellKey); |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3746 | if( nCellKey==intKey ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3747 | c = 0; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3748 | }else if( nCellKey<intKey ){ |
drh | 41eb9e9 | 2008-04-02 18:33:07 +0000 | [diff] [blame] | 3749 | c = -1; |
| 3750 | }else{ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3751 | assert( nCellKey>intKey ); |
drh | 41eb9e9 | 2008-04-02 18:33:07 +0000 | [diff] [blame] | 3752 | c = +1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3753 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3754 | }else{ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3755 | int available; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3756 | pCellKey = (void *)fetchPayload(pCur, &available, 0); |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3757 | nCellKey = pCur->info.nKey; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3758 | if( available>=nCellKey ){ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3759 | c = sqlite3VdbeRecordCompare(nCellKey, pCellKey, pIdxKey); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3760 | }else{ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 3761 | pCellKey = sqlite3Malloc( nCellKey ); |
danielk1977 | 6507ecb | 2008-03-25 09:56:44 +0000 | [diff] [blame] | 3762 | if( pCellKey==0 ){ |
| 3763 | rc = SQLITE_NOMEM; |
| 3764 | goto moveto_finish; |
| 3765 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3766 | rc = sqlite3BtreeKey(pCur, 0, nCellKey, (void *)pCellKey); |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3767 | c = sqlite3VdbeRecordCompare(nCellKey, pCellKey, pIdxKey); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 3768 | sqlite3_free(pCellKey); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3769 | if( rc ) goto moveto_finish; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3770 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3771 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3772 | if( c==0 ){ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3773 | pCur->info.nKey = nCellKey; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 3774 | if( pPage->intKey && !pPage->leaf ){ |
drh | fc70e6f | 2004-05-12 21:11:27 +0000 | [diff] [blame] | 3775 | lwr = pCur->idx; |
| 3776 | upr = lwr - 1; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3777 | break; |
| 3778 | }else{ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3779 | if( pRes ) *pRes = 0; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3780 | rc = SQLITE_OK; |
| 3781 | goto moveto_finish; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3782 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3783 | } |
| 3784 | if( c<0 ){ |
| 3785 | lwr = pCur->idx+1; |
| 3786 | }else{ |
| 3787 | upr = pCur->idx-1; |
| 3788 | } |
drh | f1d68b3 | 2007-03-29 04:43:26 +0000 | [diff] [blame] | 3789 | if( lwr>upr ){ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3790 | pCur->info.nKey = nCellKey; |
drh | f1d68b3 | 2007-03-29 04:43:26 +0000 | [diff] [blame] | 3791 | break; |
| 3792 | } |
| 3793 | pCur->idx = (lwr+upr)/2; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3794 | } |
| 3795 | assert( lwr==upr+1 ); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 3796 | assert( pPage->isInit ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3797 | if( pPage->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3798 | chldPg = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3799 | }else if( lwr>=pPage->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3800 | chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3801 | }else{ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3802 | chldPg = get4byte(findCell(pPage, lwr)); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3803 | } |
| 3804 | if( chldPg==0 ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3805 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3806 | if( pRes ) *pRes = c; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3807 | rc = SQLITE_OK; |
| 3808 | goto moveto_finish; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3809 | } |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3810 | pCur->idx = lwr; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3811 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3812 | pCur->validNKey = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3813 | rc = moveToChild(pCur, chldPg); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3814 | if( rc ) goto moveto_finish; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3815 | } |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3816 | moveto_finish: |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3817 | return rc; |
| 3818 | } |
| 3819 | |
| 3820 | /* |
| 3821 | ** In this version of BtreeMoveto, pKey is a packed index record |
| 3822 | ** such as is generated by the OP_MakeRecord opcode. Unpack the |
| 3823 | ** record and then call BtreeMovetoUnpacked() to do the work. |
| 3824 | */ |
| 3825 | int sqlite3BtreeMoveto( |
| 3826 | BtCursor *pCur, /* Cursor open on the btree to be searched */ |
| 3827 | const void *pKey, /* Packed key if the btree is an index */ |
| 3828 | i64 nKey, /* Integer key for tables. Size of pKey for indices */ |
| 3829 | int bias, /* Bias search to the high end */ |
| 3830 | int *pRes /* Write search results here */ |
| 3831 | ){ |
| 3832 | int rc; /* Status code */ |
| 3833 | UnpackedRecord *pIdxKey; /* Unpacked index key */ |
drh | 23f79d0 | 2008-08-20 22:06:47 +0000 | [diff] [blame] | 3834 | UnpackedRecord aSpace[16]; /* Temp space for pIdxKey - to avoid a malloc */ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3835 | |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 3836 | if( pKey ){ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3837 | pIdxKey = sqlite3VdbeRecordUnpack(pCur->pKeyInfo, nKey, pKey, |
drh | 23f79d0 | 2008-08-20 22:06:47 +0000 | [diff] [blame] | 3838 | aSpace, sizeof(aSpace)); |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3839 | if( pIdxKey==0 ) return SQLITE_NOMEM; |
| 3840 | }else{ |
| 3841 | pIdxKey = 0; |
| 3842 | } |
| 3843 | rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes); |
| 3844 | if( pKey ){ |
| 3845 | sqlite3VdbeDeleteUnpackedRecord(pIdxKey); |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 3846 | } |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3847 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3848 | } |
| 3849 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3850 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3851 | /* |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3852 | ** Return TRUE if the cursor is not pointing at an entry of the table. |
| 3853 | ** |
| 3854 | ** TRUE will be returned after a call to sqlite3BtreeNext() moves |
| 3855 | ** past the last entry in the table or sqlite3BtreePrev() moves past |
| 3856 | ** the first entry. TRUE is also returned if the table is empty. |
| 3857 | */ |
| 3858 | int sqlite3BtreeEof(BtCursor *pCur){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3859 | /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries |
| 3860 | ** have been deleted? This API will need to change to return an error code |
| 3861 | ** as well as the boolean result value. |
| 3862 | */ |
| 3863 | return (CURSOR_VALID!=pCur->eState); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3864 | } |
| 3865 | |
| 3866 | /* |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 3867 | ** Return the database connection handle for a cursor. |
| 3868 | */ |
| 3869 | sqlite3 *sqlite3BtreeCursorDb(const BtCursor *pCur){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3870 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
| 3871 | return pCur->pBtree->db; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 3872 | } |
| 3873 | |
| 3874 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3875 | ** Advance the cursor to the next entry in the database. If |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3876 | ** successful then set *pRes=0. If the cursor |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3877 | ** was already pointing to the last entry in the database before |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3878 | ** this routine was called, then set *pRes=1. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3879 | */ |
drh | d094db1 | 2008-04-03 21:46:57 +0000 | [diff] [blame] | 3880 | int sqlite3BtreeNext(BtCursor *pCur, int *pRes){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3881 | int rc; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 3882 | MemPage *pPage; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3883 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3884 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 3885 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3886 | if( rc!=SQLITE_OK ){ |
| 3887 | return rc; |
| 3888 | } |
drh | 8c4d3a6 | 2007-04-06 01:03:32 +0000 | [diff] [blame] | 3889 | assert( pRes!=0 ); |
| 3890 | pPage = pCur->pPage; |
| 3891 | if( CURSOR_INVALID==pCur->eState ){ |
| 3892 | *pRes = 1; |
| 3893 | return SQLITE_OK; |
| 3894 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3895 | if( pCur->skip>0 ){ |
| 3896 | pCur->skip = 0; |
| 3897 | *pRes = 0; |
| 3898 | return SQLITE_OK; |
| 3899 | } |
| 3900 | pCur->skip = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3901 | |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3902 | assert( pPage->isInit ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3903 | assert( pCur->idx<pPage->nCell ); |
danielk1977 | 6a43f9b | 2004-11-16 04:57:24 +0000 | [diff] [blame] | 3904 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3905 | pCur->idx++; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3906 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3907 | pCur->validNKey = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3908 | if( pCur->idx>=pPage->nCell ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3909 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3910 | rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3911 | if( rc ) return rc; |
| 3912 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3913 | *pRes = 0; |
| 3914 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3915 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3916 | do{ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3917 | if( sqlite3BtreeIsRootPage(pPage) ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3918 | *pRes = 1; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3919 | pCur->eState = CURSOR_INVALID; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3920 | return SQLITE_OK; |
| 3921 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3922 | sqlite3BtreeMoveToParent(pCur); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3923 | pPage = pCur->pPage; |
| 3924 | }while( pCur->idx>=pPage->nCell ); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3925 | *pRes = 0; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 3926 | if( pPage->intKey ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3927 | rc = sqlite3BtreeNext(pCur, pRes); |
| 3928 | }else{ |
| 3929 | rc = SQLITE_OK; |
| 3930 | } |
| 3931 | return rc; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3932 | } |
| 3933 | *pRes = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3934 | if( pPage->leaf ){ |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3935 | return SQLITE_OK; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3936 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3937 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3938 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3939 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3940 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3941 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3942 | /* |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3943 | ** 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] | 3944 | ** successful then set *pRes=0. If the cursor |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3945 | ** was already pointing to the first entry in the database before |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3946 | ** this routine was called, then set *pRes=1. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3947 | */ |
drh | d094db1 | 2008-04-03 21:46:57 +0000 | [diff] [blame] | 3948 | int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){ |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3949 | int rc; |
| 3950 | Pgno pgno; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3951 | MemPage *pPage; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3952 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3953 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 3954 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3955 | if( rc!=SQLITE_OK ){ |
| 3956 | return rc; |
| 3957 | } |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3958 | pCur->atLast = 0; |
drh | 8c4d3a6 | 2007-04-06 01:03:32 +0000 | [diff] [blame] | 3959 | if( CURSOR_INVALID==pCur->eState ){ |
| 3960 | *pRes = 1; |
| 3961 | return SQLITE_OK; |
| 3962 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3963 | if( pCur->skip<0 ){ |
| 3964 | pCur->skip = 0; |
| 3965 | *pRes = 0; |
| 3966 | return SQLITE_OK; |
| 3967 | } |
| 3968 | pCur->skip = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3969 | |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3970 | pPage = pCur->pPage; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3971 | assert( pPage->isInit ); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3972 | assert( pCur->idx>=0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3973 | if( !pPage->leaf ){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3974 | pgno = get4byte( findCell(pPage, pCur->idx) ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3975 | rc = moveToChild(pCur, pgno); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3976 | if( rc ){ |
| 3977 | return rc; |
| 3978 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3979 | rc = moveToRightmost(pCur); |
| 3980 | }else{ |
| 3981 | while( pCur->idx==0 ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3982 | if( sqlite3BtreeIsRootPage(pPage) ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3983 | pCur->eState = CURSOR_INVALID; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3984 | *pRes = 1; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3985 | return SQLITE_OK; |
| 3986 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3987 | sqlite3BtreeMoveToParent(pCur); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3988 | pPage = pCur->pPage; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3989 | } |
| 3990 | pCur->idx--; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3991 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3992 | pCur->validNKey = 0; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 3993 | if( pPage->intKey && !pPage->leaf ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3994 | rc = sqlite3BtreePrevious(pCur, pRes); |
| 3995 | }else{ |
| 3996 | rc = SQLITE_OK; |
| 3997 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3998 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3999 | *pRes = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4000 | return rc; |
| 4001 | } |
| 4002 | |
| 4003 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4004 | ** Allocate a new page from the database file. |
| 4005 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4006 | ** The new page is marked as dirty. (In other words, sqlite3PagerWrite() |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4007 | ** has already been called on the new page.) The new page has also |
| 4008 | ** been referenced and the calling routine is responsible for calling |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4009 | ** sqlite3PagerUnref() on the new page when it is done. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4010 | ** |
| 4011 | ** SQLITE_OK is returned on success. Any other return value indicates |
| 4012 | ** an error. *ppPage and *pPgno are undefined in the event of an error. |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4013 | ** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned. |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 4014 | ** |
drh | 199e3cf | 2002-07-18 11:01:47 +0000 | [diff] [blame] | 4015 | ** If the "nearby" parameter is not 0, then a (feeble) effort is made to |
| 4016 | ** 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] | 4017 | ** attempt to keep related pages close to each other in the database file, |
| 4018 | ** which in turn can make database access faster. |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4019 | ** |
| 4020 | ** If the "exact" parameter is not 0, and the page-number nearby exists |
| 4021 | ** anywhere on the free-list, then it is guarenteed to be returned. This |
| 4022 | ** is only used by auto-vacuum databases when allocating a new table. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4023 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4024 | static int allocateBtreePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4025 | BtShared *pBt, |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4026 | MemPage **ppPage, |
| 4027 | Pgno *pPgno, |
| 4028 | Pgno nearby, |
| 4029 | u8 exact |
| 4030 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4031 | MemPage *pPage1; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4032 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4033 | int n; /* Number of pages on the freelist */ |
| 4034 | int k; /* Number of leaves on the trunk of the freelist */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4035 | MemPage *pTrunk = 0; |
| 4036 | MemPage *pPrevTrunk = 0; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4037 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4038 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4039 | pPage1 = pBt->pPage1; |
| 4040 | n = get4byte(&pPage1->aData[36]); |
| 4041 | if( n>0 ){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4042 | /* There are pages on the freelist. Reuse one of those pages. */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4043 | Pgno iTrunk; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4044 | u8 searchList = 0; /* If the free-list must be searched for 'nearby' */ |
| 4045 | |
| 4046 | /* If the 'exact' parameter was true and a query of the pointer-map |
| 4047 | ** shows that the page 'nearby' is somewhere on the free-list, then |
| 4048 | ** the entire-list will be searched for that page. |
| 4049 | */ |
| 4050 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 4051 | if( exact && nearby<=pagerPagecount(pBt->pPager) ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4052 | u8 eType; |
| 4053 | assert( nearby>0 ); |
| 4054 | assert( pBt->autoVacuum ); |
| 4055 | rc = ptrmapGet(pBt, nearby, &eType, 0); |
| 4056 | if( rc ) return rc; |
| 4057 | if( eType==PTRMAP_FREEPAGE ){ |
| 4058 | searchList = 1; |
| 4059 | } |
| 4060 | *pPgno = nearby; |
| 4061 | } |
| 4062 | #endif |
| 4063 | |
| 4064 | /* Decrement the free-list count by 1. Set iTrunk to the index of the |
| 4065 | ** first free-list trunk page. iPrevTrunk is initially 1. |
| 4066 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4067 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4068 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4069 | put4byte(&pPage1->aData[36], n-1); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4070 | |
| 4071 | /* The code within this loop is run only once if the 'searchList' variable |
| 4072 | ** is not true. Otherwise, it runs once for each trunk-page on the |
| 4073 | ** free-list until the page 'nearby' is located. |
| 4074 | */ |
| 4075 | do { |
| 4076 | pPrevTrunk = pTrunk; |
| 4077 | if( pPrevTrunk ){ |
| 4078 | iTrunk = get4byte(&pPrevTrunk->aData[0]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 4079 | }else{ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4080 | iTrunk = get4byte(&pPage1->aData[32]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 4081 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4082 | rc = sqlite3BtreeGetPage(pBt, iTrunk, &pTrunk, 0); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4083 | if( rc ){ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4084 | pTrunk = 0; |
| 4085 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4086 | } |
| 4087 | |
| 4088 | k = get4byte(&pTrunk->aData[4]); |
| 4089 | if( k==0 && !searchList ){ |
| 4090 | /* The trunk has no leaves and the list is not being searched. |
| 4091 | ** So extract the trunk page itself and use it as the newly |
| 4092 | ** allocated page */ |
| 4093 | assert( pPrevTrunk==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4094 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4095 | if( rc ){ |
| 4096 | goto end_allocate_page; |
| 4097 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4098 | *pPgno = iTrunk; |
| 4099 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 4100 | *ppPage = pTrunk; |
| 4101 | pTrunk = 0; |
| 4102 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
drh | 45b1fac | 2008-07-04 17:52:42 +0000 | [diff] [blame] | 4103 | }else if( k>pBt->usableSize/4 - 2 ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4104 | /* Value of k is out of range. Database corruption */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4105 | rc = SQLITE_CORRUPT_BKPT; |
| 4106 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4107 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4108 | }else if( searchList && nearby==iTrunk ){ |
| 4109 | /* The list is being searched and this trunk page is the page |
| 4110 | ** to allocate, regardless of whether it has leaves. |
| 4111 | */ |
| 4112 | assert( *pPgno==iTrunk ); |
| 4113 | *ppPage = pTrunk; |
| 4114 | searchList = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4115 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4116 | if( rc ){ |
| 4117 | goto end_allocate_page; |
| 4118 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4119 | if( k==0 ){ |
| 4120 | if( !pPrevTrunk ){ |
| 4121 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 4122 | }else{ |
| 4123 | memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4); |
| 4124 | } |
| 4125 | }else{ |
| 4126 | /* The trunk page is required by the caller but it contains |
| 4127 | ** pointers to free-list leaves. The first leaf becomes a trunk |
| 4128 | ** page in this case. |
| 4129 | */ |
| 4130 | MemPage *pNewTrunk; |
| 4131 | Pgno iNewTrunk = get4byte(&pTrunk->aData[8]); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4132 | rc = sqlite3BtreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4133 | if( rc!=SQLITE_OK ){ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4134 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4135 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4136 | rc = sqlite3PagerWrite(pNewTrunk->pDbPage); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4137 | if( rc!=SQLITE_OK ){ |
| 4138 | releasePage(pNewTrunk); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4139 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4140 | } |
| 4141 | memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4); |
| 4142 | put4byte(&pNewTrunk->aData[4], k-1); |
| 4143 | memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4144 | releasePage(pNewTrunk); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4145 | if( !pPrevTrunk ){ |
| 4146 | put4byte(&pPage1->aData[32], iNewTrunk); |
| 4147 | }else{ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4148 | rc = sqlite3PagerWrite(pPrevTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4149 | if( rc ){ |
| 4150 | goto end_allocate_page; |
| 4151 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4152 | put4byte(&pPrevTrunk->aData[0], iNewTrunk); |
| 4153 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4154 | } |
| 4155 | pTrunk = 0; |
| 4156 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
| 4157 | #endif |
| 4158 | }else{ |
| 4159 | /* Extract a leaf from the trunk */ |
| 4160 | int closest; |
| 4161 | Pgno iPage; |
| 4162 | unsigned char *aData = pTrunk->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4163 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4164 | if( rc ){ |
| 4165 | goto end_allocate_page; |
| 4166 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4167 | if( nearby>0 ){ |
| 4168 | int i, dist; |
| 4169 | closest = 0; |
| 4170 | dist = get4byte(&aData[8]) - nearby; |
| 4171 | if( dist<0 ) dist = -dist; |
| 4172 | for(i=1; i<k; i++){ |
| 4173 | int d2 = get4byte(&aData[8+i*4]) - nearby; |
| 4174 | if( d2<0 ) d2 = -d2; |
| 4175 | if( d2<dist ){ |
| 4176 | closest = i; |
| 4177 | dist = d2; |
| 4178 | } |
| 4179 | } |
| 4180 | }else{ |
| 4181 | closest = 0; |
| 4182 | } |
| 4183 | |
| 4184 | iPage = get4byte(&aData[8+closest*4]); |
| 4185 | if( !searchList || iPage==nearby ){ |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 4186 | int nPage; |
shane | 1f9e6aa | 2008-06-09 19:27:11 +0000 | [diff] [blame] | 4187 | *pPgno = iPage; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 4188 | nPage = pagerPagecount(pBt->pPager); |
| 4189 | if( *pPgno>nPage ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4190 | /* Free page off the end of the file */ |
danielk1977 | 43e377a | 2008-05-05 12:09:32 +0000 | [diff] [blame] | 4191 | rc = SQLITE_CORRUPT_BKPT; |
| 4192 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4193 | } |
| 4194 | TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d" |
| 4195 | ": %d more free pages\n", |
| 4196 | *pPgno, closest+1, k, pTrunk->pgno, n-1)); |
| 4197 | if( closest<k-1 ){ |
| 4198 | memcpy(&aData[8+closest*4], &aData[4+k*4], 4); |
| 4199 | } |
| 4200 | put4byte(&aData[4], k-1); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4201 | rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 1); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4202 | if( rc==SQLITE_OK ){ |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 4203 | sqlite3PagerDontRollback((*ppPage)->pDbPage); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4204 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 4205 | if( rc!=SQLITE_OK ){ |
| 4206 | releasePage(*ppPage); |
| 4207 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4208 | } |
| 4209 | searchList = 0; |
| 4210 | } |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 4211 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4212 | releasePage(pPrevTrunk); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4213 | pPrevTrunk = 0; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4214 | }while( searchList ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4215 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4216 | /* There are no pages on the freelist, so create a new page at the |
| 4217 | ** end of the file */ |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 4218 | int nPage = pagerPagecount(pBt->pPager); |
| 4219 | *pPgno = nPage + 1; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4220 | |
| 4221 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 4222 | if( pBt->nTrunc ){ |
| 4223 | /* An incr-vacuum has already run within this transaction. So the |
| 4224 | ** page to allocate is not from the physical end of the file, but |
| 4225 | ** at pBt->nTrunc. |
| 4226 | */ |
| 4227 | *pPgno = pBt->nTrunc+1; |
| 4228 | if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ |
| 4229 | (*pPgno)++; |
| 4230 | } |
| 4231 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 4232 | if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, *pPgno) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4233 | /* If *pPgno refers to a pointer-map page, allocate two new pages |
| 4234 | ** at the end of the file instead of one. The first allocated page |
| 4235 | ** becomes a new pointer-map page, the second is used by the caller. |
| 4236 | */ |
| 4237 | TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno)); |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4238 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4239 | (*pPgno)++; |
drh | 7219043 | 2008-01-31 14:54:43 +0000 | [diff] [blame] | 4240 | if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ (*pPgno)++; } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4241 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 4242 | if( pBt->nTrunc ){ |
| 4243 | pBt->nTrunc = *pPgno; |
| 4244 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4245 | #endif |
| 4246 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4247 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4248 | rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 0); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4249 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4250 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 4251 | if( rc!=SQLITE_OK ){ |
| 4252 | releasePage(*ppPage); |
| 4253 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4254 | TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4255 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4256 | |
| 4257 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4258 | |
| 4259 | end_allocate_page: |
| 4260 | releasePage(pTrunk); |
| 4261 | releasePage(pPrevTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4262 | return rc; |
| 4263 | } |
| 4264 | |
| 4265 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4266 | ** Add a page of the database file to the freelist. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4267 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4268 | ** sqlite3PagerUnref() is NOT called for pPage. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4269 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4270 | static int freePage(MemPage *pPage){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4271 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4272 | MemPage *pPage1 = pBt->pPage1; |
| 4273 | int rc, n, k; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4274 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4275 | /* Prepare the page for freeing */ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4276 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4277 | assert( pPage->pgno>1 ); |
| 4278 | pPage->isInit = 0; |
| 4279 | releasePage(pPage->pParent); |
| 4280 | pPage->pParent = 0; |
| 4281 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4282 | /* Increment the free page count on pPage1 */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4283 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4284 | if( rc ) return rc; |
| 4285 | n = get4byte(&pPage1->aData[36]); |
| 4286 | put4byte(&pPage1->aData[36], n+1); |
| 4287 | |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4288 | #ifdef SQLITE_SECURE_DELETE |
| 4289 | /* If the SQLITE_SECURE_DELETE compile-time option is enabled, then |
| 4290 | ** always fully overwrite deleted information with zeros. |
| 4291 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4292 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4293 | if( rc ) return rc; |
| 4294 | memset(pPage->aData, 0, pPage->pBt->pageSize); |
| 4295 | #endif |
| 4296 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4297 | /* If the database supports auto-vacuum, write an entry in the pointer-map |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4298 | ** to indicate that the page is free. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4299 | */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 4300 | if( ISAUTOVACUUM ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4301 | rc = ptrmapPut(pBt, pPage->pgno, PTRMAP_FREEPAGE, 0); |
danielk1977 | a64a035 | 2004-11-05 01:45:13 +0000 | [diff] [blame] | 4302 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4303 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4304 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4305 | if( n==0 ){ |
| 4306 | /* This is the first free page */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4307 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4308 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4309 | memset(pPage->aData, 0, 8); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4310 | put4byte(&pPage1->aData[32], pPage->pgno); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4311 | TRACE(("FREE-PAGE: %d first\n", pPage->pgno)); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4312 | }else{ |
| 4313 | /* Other free pages already exist. Retrive the first trunk page |
| 4314 | ** of the freelist and find out how many leaves it has. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4315 | MemPage *pTrunk; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4316 | rc = sqlite3BtreeGetPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk, 0); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4317 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4318 | k = get4byte(&pTrunk->aData[4]); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 4319 | if( k>=pBt->usableSize/4 - 8 ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4320 | /* The trunk is full. Turn the page being freed into a new |
drh | 45b1fac | 2008-07-04 17:52:42 +0000 | [diff] [blame] | 4321 | ** trunk page with no leaves. |
| 4322 | ** |
| 4323 | ** Note that the trunk page is not really full until it contains |
| 4324 | ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have |
| 4325 | ** coded. But due to a coding error in versions of SQLite prior to |
| 4326 | ** 3.6.0, databases with freelist trunk pages holding more than |
| 4327 | ** usableSize/4 - 8 entries will be reported as corrupt. In order |
| 4328 | ** to maintain backwards compatibility with older versions of SQLite, |
| 4329 | ** we will contain to restrict the number of entries to usableSize/4 - 8 |
| 4330 | ** for now. At some point in the future (once everyone has upgraded |
| 4331 | ** to 3.6.0 or later) we should consider fixing the conditional above |
| 4332 | ** to read "usableSize/4-2" instead of "usableSize/4-8". |
| 4333 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4334 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | b9ee493 | 2007-09-07 14:32:06 +0000 | [diff] [blame] | 4335 | if( rc==SQLITE_OK ){ |
| 4336 | put4byte(pPage->aData, pTrunk->pgno); |
| 4337 | put4byte(&pPage->aData[4], 0); |
| 4338 | put4byte(&pPage1->aData[32], pPage->pgno); |
| 4339 | TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", |
| 4340 | pPage->pgno, pTrunk->pgno)); |
| 4341 | } |
| 4342 | }else if( k<0 ){ |
| 4343 | rc = SQLITE_CORRUPT; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4344 | }else{ |
| 4345 | /* Add the newly freed page as a leaf on the current trunk */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4346 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 4347 | if( rc==SQLITE_OK ){ |
| 4348 | put4byte(&pTrunk->aData[4], k+1); |
| 4349 | put4byte(&pTrunk->aData[8+k*4], pPage->pgno); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4350 | #ifndef SQLITE_SECURE_DELETE |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 4351 | sqlite3PagerDontWrite(pPage->pDbPage); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4352 | #endif |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 4353 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4354 | 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] | 4355 | } |
| 4356 | releasePage(pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4357 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4358 | return rc; |
| 4359 | } |
| 4360 | |
| 4361 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4362 | ** Free any overflow pages associated with the given Cell. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4363 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4364 | static int clearCell(MemPage *pPage, unsigned char *pCell){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4365 | BtShared *pBt = pPage->pBt; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4366 | CellInfo info; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4367 | Pgno ovflPgno; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4368 | int rc; |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 4369 | int nOvfl; |
| 4370 | int ovflPageSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4371 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4372 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4373 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4374 | if( info.iOverflow==0 ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4375 | return SQLITE_OK; /* No overflow pages. Return without doing anything */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4376 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4377 | ovflPgno = get4byte(&pCell[info.iOverflow]); |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 4378 | ovflPageSize = pBt->usableSize - 4; |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 4379 | nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize; |
| 4380 | assert( ovflPgno==0 || nOvfl>0 ); |
| 4381 | while( nOvfl-- ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4382 | MemPage *pOvfl; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 4383 | if( ovflPgno==0 || ovflPgno>pagerPagecount(pBt->pPager) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 4384 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 4385 | } |
danielk1977 | 8c0a959 | 2007-04-30 16:55:00 +0000 | [diff] [blame] | 4386 | |
| 4387 | rc = getOverflowPage(pBt, ovflPgno, &pOvfl, (nOvfl==0)?0:&ovflPgno); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4388 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4389 | rc = freePage(pOvfl); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4390 | sqlite3PagerUnref(pOvfl->pDbPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 4391 | if( rc ) return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4392 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4393 | return SQLITE_OK; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4394 | } |
| 4395 | |
| 4396 | /* |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4397 | ** Create the byte sequence used to represent a cell on page pPage |
| 4398 | ** and write that byte sequence into pCell[]. Overflow pages are |
| 4399 | ** allocated and filled in as necessary. The calling procedure |
| 4400 | ** is responsible for making sure sufficient space has been allocated |
| 4401 | ** for pCell[]. |
| 4402 | ** |
| 4403 | ** Note that pCell does not necessary need to point to the pPage->aData |
| 4404 | ** area. pCell might point to some temporary storage. The cell will |
| 4405 | ** be constructed in this temporary area then copied into pPage->aData |
| 4406 | ** later. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4407 | */ |
| 4408 | static int fillInCell( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4409 | MemPage *pPage, /* The page that contains the cell */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4410 | unsigned char *pCell, /* Complete text of the cell */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 4411 | const void *pKey, i64 nKey, /* The key */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4412 | const void *pData,int nData, /* The data */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4413 | int nZero, /* Extra zero bytes to append to pData */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4414 | int *pnSize /* Write cell size here */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4415 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4416 | int nPayload; |
drh | 8c6fa9b | 2004-05-26 00:01:53 +0000 | [diff] [blame] | 4417 | const u8 *pSrc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4418 | int nSrc, n, rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4419 | int spaceLeft; |
| 4420 | MemPage *pOvfl = 0; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4421 | MemPage *pToRelease = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4422 | unsigned char *pPrior; |
| 4423 | unsigned char *pPayload; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4424 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4425 | Pgno pgnoOvfl = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4426 | int nHeader; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4427 | CellInfo info; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4428 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4429 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4430 | |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4431 | /* Fill in the header. */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4432 | nHeader = 0; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4433 | if( !pPage->leaf ){ |
| 4434 | nHeader += 4; |
| 4435 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4436 | if( pPage->hasData ){ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4437 | nHeader += putVarint(&pCell[nHeader], nData+nZero); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4438 | }else{ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4439 | nData = nZero = 0; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4440 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4441 | nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4442 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4443 | assert( info.nHeader==nHeader ); |
| 4444 | assert( info.nKey==nKey ); |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4445 | assert( info.nData==nData+nZero ); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4446 | |
| 4447 | /* Fill in the payload */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4448 | nPayload = nData + nZero; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4449 | if( pPage->intKey ){ |
| 4450 | pSrc = pData; |
| 4451 | nSrc = nData; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4452 | nData = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4453 | }else{ |
| 4454 | nPayload += nKey; |
| 4455 | pSrc = pKey; |
| 4456 | nSrc = nKey; |
| 4457 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4458 | *pnSize = info.nSize; |
| 4459 | spaceLeft = info.nLocal; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4460 | pPayload = &pCell[nHeader]; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4461 | pPrior = &pCell[info.iOverflow]; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4462 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4463 | while( nPayload>0 ){ |
| 4464 | if( spaceLeft==0 ){ |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4465 | int isExact = 0; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4466 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4467 | Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */ |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4468 | if( pBt->autoVacuum ){ |
| 4469 | do{ |
| 4470 | pgnoOvfl++; |
| 4471 | } while( |
| 4472 | PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt) |
| 4473 | ); |
danielk1977 | 89a4be8 | 2007-05-23 13:34:32 +0000 | [diff] [blame] | 4474 | if( pgnoOvfl>1 ){ |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4475 | /* isExact = 1; */ |
| 4476 | } |
| 4477 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4478 | #endif |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4479 | rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, isExact); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4480 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4481 | /* If the database supports auto-vacuum, and the second or subsequent |
| 4482 | ** overflow page is being allocated, add an entry to the pointer-map |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 4483 | ** for that page now. |
| 4484 | ** |
| 4485 | ** If this is the first overflow page, then write a partial entry |
| 4486 | ** to the pointer-map. If we write nothing to this pointer-map slot, |
| 4487 | ** then the optimistic overflow chain processing in clearCell() |
| 4488 | ** may misinterpret the uninitialised values and delete the |
| 4489 | ** wrong pages from the database. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4490 | */ |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 4491 | if( pBt->autoVacuum && rc==SQLITE_OK ){ |
| 4492 | u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1); |
| 4493 | rc = ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap); |
danielk1977 | 89a4be8 | 2007-05-23 13:34:32 +0000 | [diff] [blame] | 4494 | if( rc ){ |
| 4495 | releasePage(pOvfl); |
| 4496 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4497 | } |
| 4498 | #endif |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4499 | if( rc ){ |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4500 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4501 | return rc; |
| 4502 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4503 | put4byte(pPrior, pgnoOvfl); |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4504 | releasePage(pToRelease); |
| 4505 | pToRelease = pOvfl; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4506 | pPrior = pOvfl->aData; |
| 4507 | put4byte(pPrior, 0); |
| 4508 | pPayload = &pOvfl->aData[4]; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4509 | spaceLeft = pBt->usableSize - 4; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4510 | } |
| 4511 | n = nPayload; |
| 4512 | if( n>spaceLeft ) n = spaceLeft; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4513 | if( nSrc>0 ){ |
| 4514 | if( n>nSrc ) n = nSrc; |
| 4515 | assert( pSrc ); |
| 4516 | memcpy(pPayload, pSrc, n); |
| 4517 | }else{ |
| 4518 | memset(pPayload, 0, n); |
| 4519 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4520 | nPayload -= n; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4521 | pPayload += n; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4522 | pSrc += n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4523 | nSrc -= n; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4524 | spaceLeft -= n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4525 | if( nSrc==0 ){ |
| 4526 | nSrc = nData; |
| 4527 | pSrc = pData; |
| 4528 | } |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4529 | } |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4530 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4531 | return SQLITE_OK; |
| 4532 | } |
| 4533 | |
danielk1977 | f328bea | 2008-08-02 17:03:31 +0000 | [diff] [blame] | 4534 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4535 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4536 | ** Change the MemPage.pParent pointer on the page whose number is |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4537 | ** given in the second argument so that MemPage.pParent holds the |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4538 | ** pointer in the third argument. |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 4539 | ** |
| 4540 | ** If the final argument, updatePtrmap, is non-zero and the database |
| 4541 | ** is an auto-vacuum database, then the pointer-map entry for pgno |
| 4542 | ** is updated. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4543 | */ |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 4544 | static int reparentPage( |
| 4545 | BtShared *pBt, /* B-Tree structure */ |
| 4546 | Pgno pgno, /* Page number of child being adopted */ |
| 4547 | MemPage *pNewParent, /* New parent of pgno */ |
| 4548 | int idx, /* Index of child page pgno in pNewParent */ |
| 4549 | int updatePtrmap /* If true, update pointer-map for pgno */ |
| 4550 | ){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4551 | MemPage *pThis; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4552 | DbPage *pDbPage; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4553 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4554 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 4555 | assert( pNewParent!=0 ); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4556 | if( pgno==0 ) return SQLITE_OK; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4557 | assert( pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4558 | pDbPage = sqlite3PagerLookup(pBt->pPager, pgno); |
| 4559 | if( pDbPage ){ |
| 4560 | pThis = (MemPage *)sqlite3PagerGetExtra(pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4561 | if( pThis->isInit ){ |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 4562 | assert( pThis->aData==sqlite3PagerGetData(pDbPage) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4563 | if( pThis->pParent!=pNewParent ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4564 | if( pThis->pParent ) sqlite3PagerUnref(pThis->pParent->pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4565 | pThis->pParent = pNewParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4566 | sqlite3PagerRef(pNewParent->pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4567 | } |
| 4568 | pThis->idxParent = idx; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4569 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4570 | sqlite3PagerUnref(pDbPage); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4571 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4572 | |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 4573 | if( ISAUTOVACUUM && updatePtrmap ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4574 | return ptrmapPut(pBt, pgno, PTRMAP_BTREE, pNewParent->pgno); |
| 4575 | } |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 4576 | |
| 4577 | #ifndef NDEBUG |
| 4578 | /* If the updatePtrmap flag was clear, assert that the entry in the |
| 4579 | ** pointer-map is already correct. |
| 4580 | */ |
danielk1977 | a68468f | 2008-08-02 17:36:45 +0000 | [diff] [blame] | 4581 | if( ISAUTOVACUUM ){ |
| 4582 | pDbPage = sqlite3PagerLookup(pBt->pPager,PTRMAP_PAGENO(pBt,pgno)); |
| 4583 | if( pDbPage ){ |
| 4584 | u8 eType; |
| 4585 | Pgno ii; |
| 4586 | int rc = ptrmapGet(pBt, pgno, &eType, &ii); |
| 4587 | assert( rc==SQLITE_OK && ii==pNewParent->pgno && eType==PTRMAP_BTREE ); |
| 4588 | sqlite3PagerUnref(pDbPage); |
| 4589 | } |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 4590 | } |
| 4591 | #endif |
| 4592 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4593 | return SQLITE_OK; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4594 | } |
| 4595 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4596 | |
| 4597 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4598 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4599 | ** Change the pParent pointer of all children of pPage to point back |
| 4600 | ** to pPage. |
| 4601 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4602 | ** In other words, for every child of pPage, invoke reparentPage() |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4603 | ** to make sure that each child knows that pPage is its parent. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4604 | ** |
| 4605 | ** This routine gets called after you memcpy() one page into |
| 4606 | ** another. |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 4607 | ** |
| 4608 | ** If updatePtrmap is true, then the pointer-map entries for all child |
| 4609 | ** pages of pPage are updated. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4610 | */ |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 4611 | static int reparentChildPages(MemPage *pPage, int updatePtrmap){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4612 | int rc = SQLITE_OK; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4613 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 4614 | if( !pPage->leaf ){ |
| 4615 | int i; |
| 4616 | BtShared *pBt = pPage->pBt; |
| 4617 | Pgno iRight = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4618 | |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 4619 | for(i=0; i<pPage->nCell; i++){ |
| 4620 | u8 *pCell = findCell(pPage, i); |
| 4621 | rc = reparentPage(pBt, get4byte(pCell), pPage, i, updatePtrmap); |
| 4622 | if( rc!=SQLITE_OK ) return rc; |
| 4623 | } |
| 4624 | rc = reparentPage(pBt, iRight, pPage, i, updatePtrmap); |
| 4625 | pPage->idxShift = 0; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4626 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4627 | return rc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4628 | } |
| 4629 | |
| 4630 | /* |
| 4631 | ** Remove the i-th cell from pPage. This routine effects pPage only. |
| 4632 | ** The cell content is not freed or deallocated. It is assumed that |
| 4633 | ** the cell content has been copied someplace else. This routine just |
| 4634 | ** removes the reference to the cell from pPage. |
| 4635 | ** |
| 4636 | ** "sz" must be the number of bytes in the cell. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4637 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4638 | static void dropCell(MemPage *pPage, int idx, int sz){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4639 | int i; /* Loop counter */ |
| 4640 | int pc; /* Offset to cell content of cell being deleted */ |
| 4641 | u8 *data; /* pPage->aData */ |
| 4642 | u8 *ptr; /* Used to move bytes around within data[] */ |
| 4643 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4644 | assert( idx>=0 && idx<pPage->nCell ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4645 | assert( sz==cellSize(pPage, idx) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4646 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4647 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4648 | data = pPage->aData; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4649 | ptr = &data[pPage->cellOffset + 2*idx]; |
| 4650 | pc = get2byte(ptr); |
| 4651 | assert( pc>10 && pc+sz<=pPage->pBt->usableSize ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4652 | freeSpace(pPage, pc, sz); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4653 | for(i=idx+1; i<pPage->nCell; i++, ptr+=2){ |
| 4654 | ptr[0] = ptr[2]; |
| 4655 | ptr[1] = ptr[3]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4656 | } |
| 4657 | pPage->nCell--; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4658 | put2byte(&data[pPage->hdrOffset+3], pPage->nCell); |
| 4659 | pPage->nFree += 2; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 4660 | pPage->idxShift = 1; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4661 | } |
| 4662 | |
| 4663 | /* |
| 4664 | ** Insert a new cell on pPage at cell index "i". pCell points to the |
| 4665 | ** content of the cell. |
| 4666 | ** |
| 4667 | ** 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] | 4668 | ** will not fit, then make a copy of the cell content into pTemp if |
| 4669 | ** pTemp is not null. Regardless of pTemp, allocate a new entry |
| 4670 | ** in pPage->aOvfl[] and make it point to the cell content (either |
| 4671 | ** in pTemp or the original pCell) and also record its index. |
| 4672 | ** Allocating a new entry in pPage->aCell[] implies that |
| 4673 | ** pPage->nOverflow is incremented. |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4674 | ** |
| 4675 | ** If nSkip is non-zero, then do not copy the first nSkip bytes of the |
| 4676 | ** cell. The caller will overwrite them after this function returns. If |
drh | 4b238df | 2005-01-08 15:43:18 +0000 | [diff] [blame] | 4677 | ** 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] | 4678 | ** (but pCell+nSkip is always valid). |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4679 | */ |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4680 | static int insertCell( |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4681 | MemPage *pPage, /* Page into which we are copying */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4682 | int i, /* New cell becomes the i-th cell of the page */ |
| 4683 | u8 *pCell, /* Content of the new cell */ |
| 4684 | int sz, /* Bytes of content in pCell */ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4685 | u8 *pTemp, /* Temp storage space for pCell, if needed */ |
| 4686 | u8 nSkip /* Do not write the first nSkip bytes of the cell */ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4687 | ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4688 | int idx; /* Where to write new cell content in data[] */ |
| 4689 | int j; /* Loop counter */ |
| 4690 | int top; /* First byte of content for any cell in data[] */ |
| 4691 | int end; /* First byte past the last cell pointer in data[] */ |
| 4692 | int ins; /* Index in data[] where new cell pointer is inserted */ |
| 4693 | int hdr; /* Offset into data[] of the page header */ |
| 4694 | int cellOffset; /* Address of first cell pointer in data[] */ |
| 4695 | u8 *data; /* The content of the whole page */ |
| 4696 | u8 *ptr; /* Used for moving information around in data[] */ |
| 4697 | |
| 4698 | assert( i>=0 && i<=pPage->nCell+pPage->nOverflow ); |
| 4699 | assert( sz==cellSizePtr(pPage, pCell) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4700 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4701 | if( pPage->nOverflow || sz+2>pPage->nFree ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4702 | if( pTemp ){ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4703 | memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4704 | pCell = pTemp; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4705 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4706 | j = pPage->nOverflow++; |
| 4707 | assert( j<sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0]) ); |
| 4708 | pPage->aOvfl[j].pCell = pCell; |
| 4709 | pPage->aOvfl[j].idx = i; |
| 4710 | pPage->nFree = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4711 | }else{ |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4712 | int rc = sqlite3PagerWrite(pPage->pDbPage); |
| 4713 | if( rc!=SQLITE_OK ){ |
| 4714 | return rc; |
| 4715 | } |
| 4716 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4717 | data = pPage->aData; |
| 4718 | hdr = pPage->hdrOffset; |
| 4719 | top = get2byte(&data[hdr+5]); |
| 4720 | cellOffset = pPage->cellOffset; |
| 4721 | end = cellOffset + 2*pPage->nCell + 2; |
| 4722 | ins = cellOffset + 2*i; |
| 4723 | if( end > top - sz ){ |
danielk1977 | 474b7cc | 2008-07-09 11:49:46 +0000 | [diff] [blame] | 4724 | defragmentPage(pPage); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4725 | top = get2byte(&data[hdr+5]); |
| 4726 | assert( end + sz <= top ); |
| 4727 | } |
| 4728 | idx = allocateSpace(pPage, sz); |
| 4729 | assert( idx>0 ); |
| 4730 | assert( end <= get2byte(&data[hdr+5]) ); |
| 4731 | pPage->nCell++; |
| 4732 | pPage->nFree -= 2; |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4733 | memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4734 | for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){ |
| 4735 | ptr[0] = ptr[-2]; |
| 4736 | ptr[1] = ptr[-1]; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4737 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4738 | put2byte(&data[ins], idx); |
| 4739 | put2byte(&data[hdr+3], pPage->nCell); |
| 4740 | pPage->idxShift = 1; |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4741 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4742 | if( pPage->pBt->autoVacuum ){ |
| 4743 | /* The cell may contain a pointer to an overflow page. If so, write |
| 4744 | ** the entry for the overflow page into the pointer map. |
| 4745 | */ |
| 4746 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4747 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 4748 | assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4749 | if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ |
| 4750 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4751 | rc = ptrmapPut(pPage->pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4752 | if( rc!=SQLITE_OK ) return rc; |
| 4753 | } |
| 4754 | } |
| 4755 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4756 | } |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4757 | |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4758 | return SQLITE_OK; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4759 | } |
| 4760 | |
| 4761 | /* |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4762 | ** Add a list of cells to a page. The page should be initially empty. |
| 4763 | ** The cells are guaranteed to fit on the page. |
| 4764 | */ |
| 4765 | static void assemblePage( |
| 4766 | MemPage *pPage, /* The page to be assemblied */ |
| 4767 | int nCell, /* The number of cells to add to this page */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4768 | u8 **apCell, /* Pointers to cell bodies */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 4769 | u16 *aSize /* Sizes of the cells */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4770 | ){ |
| 4771 | int i; /* Loop counter */ |
| 4772 | int totalSize; /* Total size of all cells */ |
| 4773 | int hdr; /* Index of page header */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4774 | int cellptr; /* Address of next cell pointer */ |
| 4775 | int cellbody; /* Address of next cell body */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4776 | u8 *data; /* Data for the page */ |
| 4777 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4778 | assert( pPage->nOverflow==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4779 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4780 | totalSize = 0; |
| 4781 | for(i=0; i<nCell; i++){ |
| 4782 | totalSize += aSize[i]; |
| 4783 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4784 | assert( totalSize+2*nCell<=pPage->nFree ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4785 | assert( pPage->nCell==0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4786 | cellptr = pPage->cellOffset; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4787 | data = pPage->aData; |
| 4788 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4789 | put2byte(&data[hdr+3], nCell); |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 4790 | if( nCell ){ |
| 4791 | cellbody = allocateSpace(pPage, totalSize); |
| 4792 | assert( cellbody>0 ); |
| 4793 | assert( pPage->nFree >= 2*nCell ); |
| 4794 | pPage->nFree -= 2*nCell; |
| 4795 | for(i=0; i<nCell; i++){ |
| 4796 | put2byte(&data[cellptr], cellbody); |
| 4797 | memcpy(&data[cellbody], apCell[i], aSize[i]); |
| 4798 | cellptr += 2; |
| 4799 | cellbody += aSize[i]; |
| 4800 | } |
| 4801 | assert( cellbody==pPage->pBt->usableSize ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4802 | } |
| 4803 | pPage->nCell = nCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4804 | } |
| 4805 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4806 | /* |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4807 | ** The following parameters determine how many adjacent pages get involved |
| 4808 | ** in a balancing operation. NN is the number of neighbors on either side |
| 4809 | ** of the page that participate in the balancing operation. NB is the |
| 4810 | ** total number of pages that participate, including the target page and |
| 4811 | ** NN neighbors on either side. |
| 4812 | ** |
| 4813 | ** The minimum value of NN is 1 (of course). Increasing NN above 1 |
| 4814 | ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance |
| 4815 | ** in exchange for a larger degradation in INSERT and UPDATE performance. |
| 4816 | ** The value of NN appears to give the best results overall. |
| 4817 | */ |
| 4818 | #define NN 1 /* Number of neighbors on either side of pPage */ |
| 4819 | #define NB (NN*2+1) /* Total pages involved in the balance */ |
| 4820 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4821 | /* Forward reference */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4822 | static int balance(MemPage*, int); |
| 4823 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4824 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 4825 | /* |
| 4826 | ** This version of balance() handles the common special case where |
| 4827 | ** a new entry is being inserted on the extreme right-end of the |
| 4828 | ** tree, in other words, when the new entry will become the largest |
| 4829 | ** entry in the tree. |
| 4830 | ** |
| 4831 | ** Instead of trying balance the 3 right-most leaf pages, just add |
| 4832 | ** a new page to the right-hand side and put the one new entry in |
| 4833 | ** that page. This leaves the right side of the tree somewhat |
| 4834 | ** unbalanced. But odds are that we will be inserting new entries |
| 4835 | ** at the end soon afterwards so the nearly empty page will quickly |
| 4836 | ** fill up. On average. |
| 4837 | ** |
| 4838 | ** pPage is the leaf page which is the right-most page in the tree. |
| 4839 | ** pParent is its parent. pPage must have a single overflow entry |
| 4840 | ** which is also the right-most entry on the page. |
| 4841 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4842 | static int balance_quick(MemPage *pPage, MemPage *pParent){ |
| 4843 | int rc; |
| 4844 | MemPage *pNew; |
| 4845 | Pgno pgnoNew; |
| 4846 | u8 *pCell; |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 4847 | u16 szCell; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4848 | CellInfo info; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4849 | BtShared *pBt = pPage->pBt; |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4850 | int parentIdx = pParent->nCell; /* pParent new divider cell index */ |
| 4851 | int parentSize; /* Size of new divider cell */ |
| 4852 | u8 parentCell[64]; /* Space for the new divider cell */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4853 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4854 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4855 | |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4856 | /* Allocate a new page. Insert the overflow cell from pPage |
| 4857 | ** into it. Then remove the overflow cell from pPage. |
| 4858 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4859 | rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4860 | if( rc!=SQLITE_OK ){ |
| 4861 | return rc; |
| 4862 | } |
| 4863 | pCell = pPage->aOvfl[0].pCell; |
| 4864 | szCell = cellSizePtr(pPage, pCell); |
| 4865 | zeroPage(pNew, pPage->aData[0]); |
| 4866 | assemblePage(pNew, 1, &pCell, &szCell); |
| 4867 | pPage->nOverflow = 0; |
| 4868 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4869 | /* Set the parent of the newly allocated page to pParent. */ |
| 4870 | pNew->pParent = pParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4871 | sqlite3PagerRef(pParent->pDbPage); |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4872 | |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4873 | /* pPage is currently the right-child of pParent. Change this |
| 4874 | ** so that the right-child is the new page allocated above and |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4875 | ** pPage is the next-to-right child. |
danielk1977 | 474b7cc | 2008-07-09 11:49:46 +0000 | [diff] [blame] | 4876 | ** |
| 4877 | ** Ignore the return value of the call to fillInCell(). fillInCell() |
| 4878 | ** may only return other than SQLITE_OK if it is required to allocate |
| 4879 | ** one or more overflow pages. Since an internal table B-Tree cell |
| 4880 | ** may never spill over onto an overflow page (it is a maximum of |
| 4881 | ** 13 bytes in size), it is not neccessary to check the return code. |
| 4882 | ** |
| 4883 | ** Similarly, the insertCell() function cannot fail if the page |
| 4884 | ** being inserted into is already writable and the cell does not |
| 4885 | ** contain an overflow pointer. So ignore this return code too. |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4886 | */ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4887 | assert( pPage->nCell>0 ); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 4888 | pCell = findCell(pPage, pPage->nCell-1); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4889 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
danielk1977 | 474b7cc | 2008-07-09 11:49:46 +0000 | [diff] [blame] | 4890 | fillInCell(pParent, parentCell, 0, info.nKey, 0, 0, 0, &parentSize); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4891 | assert( parentSize<64 ); |
danielk1977 | 474b7cc | 2008-07-09 11:49:46 +0000 | [diff] [blame] | 4892 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
| 4893 | insertCell(pParent, parentIdx, parentCell, parentSize, 0, 4); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4894 | put4byte(findOverflowCell(pParent,parentIdx), pPage->pgno); |
| 4895 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew); |
| 4896 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4897 | /* If this is an auto-vacuum database, update the pointer map |
| 4898 | ** with entries for the new page, and any pointer from the |
| 4899 | ** cell on the page to an overflow page. |
| 4900 | */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 4901 | if( ISAUTOVACUUM ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4902 | rc = ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno); |
danielk1977 | deb403e | 2007-05-24 09:20:16 +0000 | [diff] [blame] | 4903 | if( rc==SQLITE_OK ){ |
| 4904 | rc = ptrmapPutOvfl(pNew, 0); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4905 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4906 | if( rc!=SQLITE_OK ){ |
danielk1977 | deb403e | 2007-05-24 09:20:16 +0000 | [diff] [blame] | 4907 | releasePage(pNew); |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4908 | return rc; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4909 | } |
| 4910 | } |
| 4911 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4912 | /* Release the reference to the new page and balance the parent page, |
| 4913 | ** in case the divider cell inserted caused it to become overfull. |
| 4914 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4915 | releasePage(pNew); |
| 4916 | return balance(pParent, 0); |
| 4917 | } |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4918 | #endif /* SQLITE_OMIT_QUICKBALANCE */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4919 | |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4920 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4921 | ** This routine redistributes Cells on pPage and up to NN*2 siblings |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4922 | ** 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] | 4923 | ** Usually NN siblings on either side of pPage is used in the balancing, |
| 4924 | ** though more siblings might come from one side if pPage is the first |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4925 | ** 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] | 4926 | ** (something which can only happen if pPage is the root page or a |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4927 | ** child of root) then all available siblings participate in the balancing. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4928 | ** |
drh | 0c6cc4e | 2004-06-15 02:13:26 +0000 | [diff] [blame] | 4929 | ** The number of siblings of pPage might be increased or decreased by one or |
| 4930 | ** 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] | 4931 | ** is special and is allowed to be nearly empty. If pPage is |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4932 | ** the root page, then the depth of the tree might be increased |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4933 | ** or decreased by one, as necessary, to keep the root page from being |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4934 | ** overfull or completely empty. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4935 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4936 | ** Note that when this routine is called, some of the Cells on pPage |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4937 | ** might not actually be stored in pPage->aData[]. This can happen |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4938 | ** 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] | 4939 | ** make sure all Cells for pPage once again fit in pPage->aData[]. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4940 | ** |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4941 | ** In the course of balancing the siblings of pPage, the parent of pPage |
| 4942 | ** might become overfull or underfull. If that happens, then this routine |
| 4943 | ** is called recursively on the parent. |
| 4944 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4945 | ** If this routine fails for any reason, it might leave the database |
| 4946 | ** in a corrupted state. So if this routine fails, the database should |
| 4947 | ** be rolled back. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4948 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4949 | static int balance_nonroot(MemPage *pPage){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4950 | MemPage *pParent; /* The parent of pPage */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4951 | BtShared *pBt; /* The whole database */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4952 | int nCell = 0; /* Number of cells in apCell[] */ |
| 4953 | int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4954 | int nOld; /* Number of pages in apOld[] */ |
| 4955 | int nNew; /* Number of pages in apNew[] */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4956 | int nDiv; /* Number of cells in apDiv[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4957 | int i, j, k; /* Loop counters */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4958 | int idx; /* Index of pPage in pParent->aCell[] */ |
| 4959 | int nxDiv; /* Next divider slot in pParent->aCell[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4960 | int rc; /* The return code */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4961 | int leafCorrection; /* 4 if pPage is a leaf. 0 if not */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4962 | int leafData; /* True if pPage is a leaf of a LEAFDATA tree */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4963 | int usableSpace; /* Bytes in pPage beyond the header */ |
| 4964 | int pageFlags; /* Value of pPage->aData[0] */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4965 | int subtotal; /* Subtotal of bytes in cells on one page */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 4966 | int iSpace1 = 0; /* First unused byte of aSpace1[] */ |
| 4967 | int iSpace2 = 0; /* First unused byte of aSpace2[] */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 4968 | int szScratch; /* Size of scratch memory requested */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4969 | MemPage *apOld[NB]; /* pPage and up to two siblings */ |
| 4970 | Pgno pgnoOld[NB]; /* Page numbers for each page in apOld[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4971 | MemPage *apCopy[NB]; /* Private copies of apOld[] pages */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4972 | MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ |
| 4973 | Pgno pgnoNew[NB+2]; /* Page numbers for each page in apNew[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4974 | u8 *apDiv[NB]; /* Divider cells in pParent */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4975 | int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */ |
| 4976 | int szNew[NB+2]; /* Combined size of cells place on i-th page */ |
danielk1977 | 50f059b | 2005-03-29 02:54:03 +0000 | [diff] [blame] | 4977 | u8 **apCell = 0; /* All cells begin balanced */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 4978 | u16 *szCell; /* Local size of all cells in apCell[] */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 4979 | u8 *aCopy[NB]; /* Space for holding data of apCopy[] */ |
| 4980 | u8 *aSpace1; /* Space for copies of dividers cells before balance */ |
| 4981 | u8 *aSpace2 = 0; /* Space for overflow dividers cells after balance */ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4982 | u8 *aFrom = 0; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4983 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4984 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4985 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4986 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4987 | ** Find the parent page. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4988 | */ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4989 | assert( pPage->isInit ); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4990 | assert( sqlite3PagerIswriteable(pPage->pDbPage) || pPage->nOverflow==1 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4991 | pBt = pPage->pBt; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4992 | pParent = pPage->pParent; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4993 | assert( pParent ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4994 | if( SQLITE_OK!=(rc = sqlite3PagerWrite(pParent->pDbPage)) ){ |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 4995 | return rc; |
| 4996 | } |
danielk1977 | 474b7cc | 2008-07-09 11:49:46 +0000 | [diff] [blame] | 4997 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4998 | TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno)); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4999 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 5000 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 5001 | /* |
| 5002 | ** A special case: If a new entry has just been inserted into a |
| 5003 | ** 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] | 5004 | ** 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] | 5005 | ** largest key) then use the special balance_quick() routine for |
| 5006 | ** balancing. balance_quick() is much faster and results in a tighter |
| 5007 | ** packing of data in the common case. |
| 5008 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5009 | if( pPage->leaf && |
| 5010 | pPage->intKey && |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5011 | pPage->nOverflow==1 && |
| 5012 | pPage->aOvfl[0].idx==pPage->nCell && |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5013 | pPage->pParent->pgno!=1 && |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5014 | get4byte(&pParent->aData[pParent->hdrOffset+8])==pPage->pgno |
| 5015 | ){ |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 5016 | assert( pPage->intKey ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5017 | /* |
| 5018 | ** TODO: Check the siblings to the left of pPage. It may be that |
| 5019 | ** they are not full and no new page is required. |
| 5020 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5021 | return balance_quick(pPage, pParent); |
| 5022 | } |
| 5023 | #endif |
| 5024 | |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 5025 | if( SQLITE_OK!=(rc = sqlite3PagerWrite(pPage->pDbPage)) ){ |
| 5026 | return rc; |
| 5027 | } |
| 5028 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5029 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5030 | ** Find the cell in the parent page whose left child points back |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5031 | ** to pPage. The "idx" variable is the index of that cell. If pPage |
| 5032 | ** is the rightmost child of pParent then set idx to pParent->nCell |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5033 | */ |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 5034 | if( pParent->idxShift ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5035 | Pgno pgno; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5036 | pgno = pPage->pgno; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5037 | assert( pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 5038 | for(idx=0; idx<pParent->nCell; idx++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5039 | if( get4byte(findCell(pParent, idx))==pgno ){ |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 5040 | break; |
| 5041 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5042 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5043 | assert( idx<pParent->nCell |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5044 | || get4byte(&pParent->aData[pParent->hdrOffset+8])==pgno ); |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 5045 | }else{ |
| 5046 | idx = pPage->idxParent; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5047 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5048 | |
| 5049 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5050 | ** Initialize variables so that it will be safe to jump |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 5051 | ** directly to balance_cleanup at any moment. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5052 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5053 | nOld = nNew = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5054 | sqlite3PagerRef(pParent->pDbPage); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5055 | |
| 5056 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5057 | ** Find sibling pages to pPage and the cells in pParent that divide |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5058 | ** the siblings. An attempt is made to find NN siblings on either |
| 5059 | ** side of pPage. More siblings are taken from one side, however, if |
| 5060 | ** pPage there are fewer than NN siblings on the other side. If pParent |
| 5061 | ** has NB or fewer children then all children of pParent are taken. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5062 | */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5063 | nxDiv = idx - NN; |
| 5064 | if( nxDiv + NB > pParent->nCell ){ |
| 5065 | nxDiv = pParent->nCell - NB + 1; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5066 | } |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5067 | if( nxDiv<0 ){ |
| 5068 | nxDiv = 0; |
| 5069 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5070 | nDiv = 0; |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5071 | for(i=0, k=nxDiv; i<NB; i++, k++){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5072 | if( k<pParent->nCell ){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5073 | apDiv[i] = findCell(pParent, k); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5074 | nDiv++; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5075 | assert( !pParent->leaf ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5076 | pgnoOld[i] = get4byte(apDiv[i]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5077 | }else if( k==pParent->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5078 | pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+8]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5079 | }else{ |
| 5080 | break; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5081 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 5082 | rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i], pParent); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5083 | if( rc ) goto balance_cleanup; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 5084 | apOld[i]->idxParent = k; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5085 | apCopy[i] = 0; |
| 5086 | assert( i==nOld ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5087 | nOld++; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5088 | nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5089 | } |
| 5090 | |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5091 | /* Make nMaxCells a multiple of 4 in order to preserve 8-byte |
drh | 8d97f1f | 2005-05-05 18:14:13 +0000 | [diff] [blame] | 5092 | ** alignment */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5093 | nMaxCells = (nMaxCells + 3)&~3; |
drh | 8d97f1f | 2005-05-05 18:14:13 +0000 | [diff] [blame] | 5094 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5095 | /* |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5096 | ** Allocate space for memory structures |
| 5097 | */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5098 | szScratch = |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5099 | nMaxCells*sizeof(u8*) /* apCell */ |
| 5100 | + nMaxCells*sizeof(u16) /* szCell */ |
| 5101 | + (ROUND8(sizeof(MemPage))+pBt->pageSize)*NB /* aCopy */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5102 | + pBt->pageSize /* aSpace1 */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5103 | + (ISAUTOVACUUM ? nMaxCells : 0); /* aFrom */ |
| 5104 | apCell = sqlite3ScratchMalloc( szScratch ); |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5105 | if( apCell==0 ){ |
| 5106 | rc = SQLITE_NOMEM; |
| 5107 | goto balance_cleanup; |
| 5108 | } |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5109 | szCell = (u16*)&apCell[nMaxCells]; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5110 | aCopy[0] = (u8*)&szCell[nMaxCells]; |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 5111 | assert( ((aCopy[0] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5112 | for(i=1; i<NB; i++){ |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 5113 | aCopy[i] = &aCopy[i-1][pBt->pageSize+ROUND8(sizeof(MemPage))]; |
| 5114 | assert( ((aCopy[i] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5115 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5116 | aSpace1 = &aCopy[NB-1][pBt->pageSize+ROUND8(sizeof(MemPage))]; |
| 5117 | assert( ((aSpace1 - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5118 | if( ISAUTOVACUUM ){ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5119 | aFrom = &aSpace1[pBt->pageSize]; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5120 | } |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5121 | aSpace2 = sqlite3PageMalloc(pBt->pageSize); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5122 | if( aSpace2==0 ){ |
| 5123 | rc = SQLITE_NOMEM; |
| 5124 | goto balance_cleanup; |
| 5125 | } |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5126 | |
| 5127 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5128 | ** Make copies of the content of pPage and its siblings into aOld[]. |
| 5129 | ** The rest of this function will use data from the copies rather |
| 5130 | ** that the original pages since the original pages will be in the |
| 5131 | ** process of being overwritten. |
| 5132 | */ |
| 5133 | for(i=0; i<nOld; i++){ |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 5134 | MemPage *p = apCopy[i] = (MemPage*)aCopy[i]; |
| 5135 | memcpy(p, apOld[i], sizeof(MemPage)); |
| 5136 | p->aData = (void*)&p[1]; |
| 5137 | memcpy(p->aData, apOld[i]->aData, pBt->pageSize); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5138 | } |
| 5139 | |
| 5140 | /* |
| 5141 | ** Load pointers to all cells on sibling pages and the divider cells |
| 5142 | ** into the local apCell[] array. Make copies of the divider cells |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5143 | ** into space obtained form aSpace1[] and remove the the divider Cells |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5144 | ** from pParent. |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5145 | ** |
| 5146 | ** If the siblings are on leaf pages, then the child pointers of the |
| 5147 | ** divider cells are stripped from the cells before they are copied |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5148 | ** into aSpace1[]. In this way, all cells in apCell[] are without |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5149 | ** child pointers. If siblings are not leaves, then all cell in |
| 5150 | ** apCell[] include child pointers. Either way, all cells in apCell[] |
| 5151 | ** are alike. |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5152 | ** |
| 5153 | ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf. |
| 5154 | ** leafData: 1 if pPage holds key+data and pParent holds only keys. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5155 | */ |
| 5156 | nCell = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5157 | leafCorrection = pPage->leaf*4; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 5158 | leafData = pPage->hasData; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5159 | for(i=0; i<nOld; i++){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5160 | MemPage *pOld = apCopy[i]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5161 | int limit = pOld->nCell+pOld->nOverflow; |
| 5162 | for(j=0; j<limit; j++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5163 | assert( nCell<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5164 | apCell[nCell] = findOverflowCell(pOld, j); |
| 5165 | szCell[nCell] = cellSizePtr(pOld, apCell[nCell]); |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5166 | if( ISAUTOVACUUM ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5167 | int a; |
| 5168 | aFrom[nCell] = i; |
| 5169 | for(a=0; a<pOld->nOverflow; a++){ |
| 5170 | if( pOld->aOvfl[a].pCell==apCell[nCell] ){ |
| 5171 | aFrom[nCell] = 0xFF; |
| 5172 | break; |
| 5173 | } |
| 5174 | } |
| 5175 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5176 | nCell++; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5177 | } |
| 5178 | if( i<nOld-1 ){ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5179 | u16 sz = cellSizePtr(pParent, apDiv[i]); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5180 | if( leafData ){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5181 | /* With the LEAFDATA flag, pParent cells hold only INTKEYs that |
| 5182 | ** are duplicates of keys on the child pages. We need to remove |
| 5183 | ** the divider cells from pParent, but the dividers cells are not |
| 5184 | ** added to apCell[] because they are duplicates of child cells. |
| 5185 | */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5186 | dropCell(pParent, nxDiv, sz); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5187 | }else{ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5188 | u8 *pTemp; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5189 | assert( nCell<nMaxCells ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5190 | szCell[nCell] = sz; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5191 | pTemp = &aSpace1[iSpace1]; |
| 5192 | iSpace1 += sz; |
| 5193 | assert( sz<=pBt->pageSize/4 ); |
| 5194 | assert( iSpace1<=pBt->pageSize ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5195 | memcpy(pTemp, apDiv[i], sz); |
| 5196 | apCell[nCell] = pTemp+leafCorrection; |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5197 | if( ISAUTOVACUUM ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5198 | aFrom[nCell] = 0xFF; |
| 5199 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5200 | dropCell(pParent, nxDiv, sz); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5201 | szCell[nCell] -= leafCorrection; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5202 | assert( get4byte(pTemp)==pgnoOld[i] ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5203 | if( !pOld->leaf ){ |
| 5204 | assert( leafCorrection==0 ); |
| 5205 | /* The right pointer of the child page pOld becomes the left |
| 5206 | ** pointer of the divider cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5207 | memcpy(apCell[nCell], &pOld->aData[pOld->hdrOffset+8], 4); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5208 | }else{ |
| 5209 | assert( leafCorrection==4 ); |
danielk1977 | 39c9604 | 2007-05-12 10:41:47 +0000 | [diff] [blame] | 5210 | if( szCell[nCell]<4 ){ |
| 5211 | /* Do not allow any cells smaller than 4 bytes. */ |
| 5212 | szCell[nCell] = 4; |
| 5213 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5214 | } |
| 5215 | nCell++; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5216 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5217 | } |
| 5218 | } |
| 5219 | |
| 5220 | /* |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5221 | ** Figure out the number of pages needed to hold all nCell cells. |
| 5222 | ** Store this number in "k". Also compute szNew[] which is the total |
| 5223 | ** 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] | 5224 | ** in apCell[] of the cell that divides page i from page i+1. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5225 | ** cntNew[k] should equal nCell. |
| 5226 | ** |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5227 | ** Values computed by this block: |
| 5228 | ** |
| 5229 | ** k: The total number of sibling pages |
| 5230 | ** szNew[i]: Spaced used on the i-th sibling page. |
| 5231 | ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to |
| 5232 | ** the right of the i-th sibling page. |
| 5233 | ** usableSpace: Number of bytes of space available on each sibling. |
| 5234 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5235 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5236 | usableSpace = pBt->usableSize - 12 + leafCorrection; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5237 | for(subtotal=k=i=0; i<nCell; i++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5238 | assert( i<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5239 | subtotal += szCell[i] + 2; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5240 | if( subtotal > usableSpace ){ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5241 | szNew[k] = subtotal - szCell[i]; |
| 5242 | cntNew[k] = i; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5243 | if( leafData ){ i--; } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5244 | subtotal = 0; |
| 5245 | k++; |
| 5246 | } |
| 5247 | } |
| 5248 | szNew[k] = subtotal; |
| 5249 | cntNew[k] = nCell; |
| 5250 | k++; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5251 | |
| 5252 | /* |
| 5253 | ** The packing computed by the previous block is biased toward the siblings |
| 5254 | ** on the left side. The left siblings are always nearly full, while the |
| 5255 | ** right-most sibling might be nearly empty. This block of code attempts |
| 5256 | ** to adjust the packing of siblings to get a better balance. |
| 5257 | ** |
| 5258 | ** This adjustment is more than an optimization. The packing above might |
| 5259 | ** be so out of balance as to be illegal. For example, the right-most |
| 5260 | ** sibling might be completely empty. This adjustment is not optional. |
| 5261 | */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5262 | for(i=k-1; i>0; i--){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5263 | int szRight = szNew[i]; /* Size of sibling on the right */ |
| 5264 | int szLeft = szNew[i-1]; /* Size of sibling on the left */ |
| 5265 | int r; /* Index of right-most cell in left sibling */ |
| 5266 | int d; /* Index of first cell to the left of right sibling */ |
| 5267 | |
| 5268 | r = cntNew[i-1] - 1; |
| 5269 | d = r + 1 - leafData; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5270 | assert( d<nMaxCells ); |
| 5271 | assert( r<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5272 | while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){ |
| 5273 | szRight += szCell[d] + 2; |
| 5274 | szLeft -= szCell[r] + 2; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5275 | cntNew[i-1]--; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5276 | r = cntNew[i-1] - 1; |
| 5277 | d = r + 1 - leafData; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5278 | } |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5279 | szNew[i] = szRight; |
| 5280 | szNew[i-1] = szLeft; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5281 | } |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 5282 | |
| 5283 | /* Either we found one or more cells (cntnew[0])>0) or we are the |
| 5284 | ** a virtual root page. A virtual root page is when the real root |
| 5285 | ** page is page 1 and we are the only child of that page. |
| 5286 | */ |
| 5287 | assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5288 | |
| 5289 | /* |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5290 | ** Allocate k new pages. Reuse old pages where possible. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5291 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5292 | assert( pPage->pgno>1 ); |
| 5293 | pageFlags = pPage->aData[0]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5294 | for(i=0; i<k; i++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5295 | MemPage *pNew; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5296 | if( i<nOld ){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5297 | pNew = apNew[i] = apOld[i]; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5298 | pgnoNew[i] = pgnoOld[i]; |
| 5299 | apOld[i] = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5300 | rc = sqlite3PagerWrite(pNew->pDbPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 5301 | nNew++; |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 5302 | if( rc ) goto balance_cleanup; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5303 | }else{ |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 5304 | assert( i>0 ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5305 | rc = allocateBtreePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5306 | if( rc ) goto balance_cleanup; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5307 | apNew[i] = pNew; |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 5308 | nNew++; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5309 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5310 | } |
| 5311 | |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5312 | /* Free any old pages that were not reused as new pages. |
| 5313 | */ |
| 5314 | while( i<nOld ){ |
| 5315 | rc = freePage(apOld[i]); |
| 5316 | if( rc ) goto balance_cleanup; |
| 5317 | releasePage(apOld[i]); |
| 5318 | apOld[i] = 0; |
| 5319 | i++; |
| 5320 | } |
| 5321 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5322 | /* |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5323 | ** Put the new pages in accending order. This helps to |
| 5324 | ** keep entries in the disk file in order so that a scan |
| 5325 | ** of the table is a linear scan through the file. That |
| 5326 | ** in turn helps the operating system to deliver pages |
| 5327 | ** from the disk more rapidly. |
| 5328 | ** |
| 5329 | ** An O(n^2) insertion sort algorithm is used, but since |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5330 | ** n is never more than NB (a small constant), that should |
| 5331 | ** not be a problem. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5332 | ** |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5333 | ** When NB==3, this one optimization makes the database |
| 5334 | ** about 25% faster for large insertions and deletions. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5335 | */ |
| 5336 | for(i=0; i<k-1; i++){ |
| 5337 | int minV = pgnoNew[i]; |
| 5338 | int minI = i; |
| 5339 | for(j=i+1; j<k; j++){ |
drh | 7d02cb7 | 2003-06-04 16:24:39 +0000 | [diff] [blame] | 5340 | if( pgnoNew[j]<(unsigned)minV ){ |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5341 | minI = j; |
| 5342 | minV = pgnoNew[j]; |
| 5343 | } |
| 5344 | } |
| 5345 | if( minI>i ){ |
| 5346 | int t; |
| 5347 | MemPage *pT; |
| 5348 | t = pgnoNew[i]; |
| 5349 | pT = apNew[i]; |
| 5350 | pgnoNew[i] = pgnoNew[minI]; |
| 5351 | apNew[i] = apNew[minI]; |
| 5352 | pgnoNew[minI] = t; |
| 5353 | apNew[minI] = pT; |
| 5354 | } |
| 5355 | } |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5356 | 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] | 5357 | pgnoOld[0], |
| 5358 | nOld>=2 ? pgnoOld[1] : 0, |
| 5359 | nOld>=3 ? pgnoOld[2] : 0, |
drh | 10c0fa6 | 2004-05-18 12:50:17 +0000 | [diff] [blame] | 5360 | pgnoNew[0], szNew[0], |
| 5361 | nNew>=2 ? pgnoNew[1] : 0, nNew>=2 ? szNew[1] : 0, |
| 5362 | nNew>=3 ? pgnoNew[2] : 0, nNew>=3 ? szNew[2] : 0, |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5363 | nNew>=4 ? pgnoNew[3] : 0, nNew>=4 ? szNew[3] : 0, |
| 5364 | nNew>=5 ? pgnoNew[4] : 0, nNew>=5 ? szNew[4] : 0)); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5365 | |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5366 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5367 | ** Evenly distribute the data in apCell[] across the new pages. |
| 5368 | ** Insert divider cells into pParent as necessary. |
| 5369 | */ |
| 5370 | j = 0; |
| 5371 | for(i=0; i<nNew; i++){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5372 | /* Assemble the new sibling page. */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5373 | MemPage *pNew = apNew[i]; |
drh | 19642e5 | 2005-03-29 13:17:45 +0000 | [diff] [blame] | 5374 | assert( j<nMaxCells ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5375 | assert( pNew->pgno==pgnoNew[i] ); |
drh | 1013148 | 2008-07-11 03:34:09 +0000 | [diff] [blame] | 5376 | zeroPage(pNew, pageFlags); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 5377 | assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]); |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 5378 | assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5379 | assert( pNew->nOverflow==0 ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5380 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5381 | /* If this is an auto-vacuum database, update the pointer map entries |
| 5382 | ** that point to the siblings that were rearranged. These can be: left |
| 5383 | ** children of cells, the right-child of the page, or overflow pages |
| 5384 | ** pointed to by cells. |
| 5385 | */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5386 | if( ISAUTOVACUUM ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5387 | for(k=j; k<cntNew[i]; k++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5388 | assert( k<nMaxCells ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5389 | if( aFrom[k]==0xFF || apCopy[aFrom[k]]->pgno!=pNew->pgno ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5390 | rc = ptrmapPutOvfl(pNew, k-j); |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5391 | if( rc==SQLITE_OK && leafCorrection==0 ){ |
| 5392 | rc = ptrmapPut(pBt, get4byte(apCell[k]), PTRMAP_BTREE, pNew->pgno); |
| 5393 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5394 | if( rc!=SQLITE_OK ){ |
| 5395 | goto balance_cleanup; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5396 | } |
| 5397 | } |
| 5398 | } |
| 5399 | } |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5400 | |
| 5401 | j = cntNew[i]; |
| 5402 | |
| 5403 | /* If the sibling page assembled above was not the right-most sibling, |
| 5404 | ** insert a divider cell into the parent page. |
| 5405 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5406 | if( i<nNew-1 && j<nCell ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5407 | u8 *pCell; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5408 | u8 *pTemp; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5409 | int sz; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5410 | |
| 5411 | assert( j<nMaxCells ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5412 | pCell = apCell[j]; |
| 5413 | sz = szCell[j] + leafCorrection; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5414 | pTemp = &aSpace2[iSpace2]; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5415 | if( !pNew->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5416 | memcpy(&pNew->aData[8], pCell, 4); |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5417 | if( ISAUTOVACUUM |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5418 | && (aFrom[j]==0xFF || apCopy[aFrom[j]]->pgno!=pNew->pgno) |
| 5419 | ){ |
| 5420 | rc = ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno); |
| 5421 | if( rc!=SQLITE_OK ){ |
| 5422 | goto balance_cleanup; |
| 5423 | } |
| 5424 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5425 | }else if( leafData ){ |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 5426 | /* If the tree is a leaf-data tree, and the siblings are leaves, |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5427 | ** then there is no divider cell in apCell[]. Instead, the divider |
| 5428 | ** cell consists of the integer key for the right-most cell of |
| 5429 | ** the sibling-page assembled above only. |
| 5430 | */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5431 | CellInfo info; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5432 | j--; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5433 | sqlite3BtreeParseCellPtr(pNew, apCell[j], &info); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5434 | pCell = pTemp; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5435 | fillInCell(pParent, pCell, 0, info.nKey, 0, 0, 0, &sz); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5436 | pTemp = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5437 | }else{ |
| 5438 | pCell -= 4; |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 5439 | /* Obscure case for non-leaf-data trees: If the cell at pCell was |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 5440 | ** previously stored on a leaf node, and its reported size was 4 |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 5441 | ** bytes, then it may actually be smaller than this |
| 5442 | ** (see sqlite3BtreeParseCellPtr(), 4 bytes is the minimum size of |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 5443 | ** any cell). But it is important to pass the correct size to |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 5444 | ** insertCell(), so reparse the cell now. |
| 5445 | ** |
| 5446 | ** Note that this can never happen in an SQLite data file, as all |
| 5447 | ** cells are at least 4 bytes. It only happens in b-trees used |
| 5448 | ** to evaluate "IN (SELECT ...)" and similar clauses. |
| 5449 | */ |
| 5450 | if( szCell[j]==4 ){ |
| 5451 | assert(leafCorrection==4); |
| 5452 | sz = cellSizePtr(pParent, pCell); |
| 5453 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5454 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5455 | iSpace2 += sz; |
| 5456 | assert( sz<=pBt->pageSize/4 ); |
| 5457 | assert( iSpace2<=pBt->pageSize ); |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 5458 | rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4); |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 5459 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5460 | put4byte(findOverflowCell(pParent,nxDiv), pNew->pgno); |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5461 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5462 | /* If this is an auto-vacuum database, and not a leaf-data tree, |
| 5463 | ** then update the pointer map with an entry for the overflow page |
| 5464 | ** that the cell just inserted points to (if any). |
| 5465 | */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5466 | if( ISAUTOVACUUM && !leafData ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5467 | rc = ptrmapPutOvfl(pParent, nxDiv); |
| 5468 | if( rc!=SQLITE_OK ){ |
| 5469 | goto balance_cleanup; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5470 | } |
| 5471 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5472 | j++; |
| 5473 | nxDiv++; |
| 5474 | } |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5475 | |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5476 | /* Set the pointer-map entry for the new sibling page. */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5477 | if( ISAUTOVACUUM ){ |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5478 | rc = ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno); |
| 5479 | if( rc!=SQLITE_OK ){ |
| 5480 | goto balance_cleanup; |
| 5481 | } |
| 5482 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5483 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5484 | assert( j==nCell ); |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 5485 | assert( nOld>0 ); |
| 5486 | assert( nNew>0 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5487 | if( (pageFlags & PTF_LEAF)==0 ){ |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5488 | u8 *zChild = &apCopy[nOld-1]->aData[8]; |
| 5489 | memcpy(&apNew[nNew-1]->aData[8], zChild, 4); |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5490 | if( ISAUTOVACUUM ){ |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5491 | rc = ptrmapPut(pBt, get4byte(zChild), PTRMAP_BTREE, apNew[nNew-1]->pgno); |
| 5492 | if( rc!=SQLITE_OK ){ |
| 5493 | goto balance_cleanup; |
| 5494 | } |
| 5495 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5496 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5497 | if( nxDiv==pParent->nCell+pParent->nOverflow ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5498 | /* Right-most sibling is the right-most child of pParent */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5499 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew[nNew-1]); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5500 | }else{ |
| 5501 | /* Right-most sibling is the left child of the first entry in pParent |
| 5502 | ** past the right-most divider entry */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5503 | put4byte(findOverflowCell(pParent, nxDiv), pgnoNew[nNew-1]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5504 | } |
| 5505 | |
| 5506 | /* |
| 5507 | ** Reparent children of all cells. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5508 | */ |
| 5509 | for(i=0; i<nNew; i++){ |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5510 | rc = reparentChildPages(apNew[i], 0); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5511 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5512 | } |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5513 | rc = reparentChildPages(pParent, 0); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5514 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5515 | |
| 5516 | /* |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5517 | ** Balance the parent page. Note that the current page (pPage) might |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5518 | ** have been added to the freelist so it might no longer be initialized. |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5519 | ** But the parent page will always be initialized. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5520 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5521 | assert( pParent->isInit ); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5522 | sqlite3ScratchFree(apCell); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5523 | apCell = 0; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5524 | rc = balance(pParent, 0); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5525 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5526 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5527 | ** Cleanup before returning. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5528 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5529 | balance_cleanup: |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5530 | sqlite3PageFree(aSpace2); |
| 5531 | sqlite3ScratchFree(apCell); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5532 | for(i=0; i<nOld; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5533 | releasePage(apOld[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5534 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5535 | for(i=0; i<nNew; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5536 | releasePage(apNew[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5537 | } |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5538 | releasePage(pParent); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5539 | TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n", |
| 5540 | pPage->pgno, nOld, nNew, nCell)); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5541 | return rc; |
| 5542 | } |
| 5543 | |
| 5544 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5545 | ** This routine is called for the root page of a btree when the root |
| 5546 | ** page contains no cells. This is an opportunity to make the tree |
| 5547 | ** shallower by one level. |
| 5548 | */ |
| 5549 | static int balance_shallower(MemPage *pPage){ |
| 5550 | MemPage *pChild; /* The only child page of pPage */ |
| 5551 | Pgno pgnoChild; /* Page number for pChild */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5552 | int rc = SQLITE_OK; /* Return code from subprocedures */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5553 | BtShared *pBt; /* The main BTree structure */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5554 | int mxCellPerPage; /* Maximum number of cells per page */ |
| 5555 | u8 **apCell; /* All cells from pages being balanced */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5556 | u16 *szCell; /* Local size of all cells */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5557 | |
| 5558 | assert( pPage->pParent==0 ); |
| 5559 | assert( pPage->nCell==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5560 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5561 | pBt = pPage->pBt; |
| 5562 | mxCellPerPage = MX_CELL(pBt); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5563 | apCell = sqlite3Malloc( mxCellPerPage*(sizeof(u8*)+sizeof(u16)) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5564 | if( apCell==0 ) return SQLITE_NOMEM; |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5565 | szCell = (u16*)&apCell[mxCellPerPage]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5566 | if( pPage->leaf ){ |
| 5567 | /* The table is completely empty */ |
| 5568 | TRACE(("BALANCE: empty table %d\n", pPage->pgno)); |
| 5569 | }else{ |
| 5570 | /* The root page is empty but has one child. Transfer the |
| 5571 | ** information from that one child into the root page if it |
| 5572 | ** will fit. This reduces the depth of the tree by one. |
| 5573 | ** |
| 5574 | ** If the root page is page 1, it has less space available than |
| 5575 | ** its child (due to the 100 byte header that occurs at the beginning |
| 5576 | ** of the database fle), so it might not be able to hold all of the |
| 5577 | ** information currently contained in the child. If this is the |
| 5578 | ** case, then do not do the transfer. Leave page 1 empty except |
| 5579 | ** for the right-pointer to the child page. The child page becomes |
| 5580 | ** the virtual root of the tree. |
| 5581 | */ |
| 5582 | pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 5583 | assert( pgnoChild>0 ); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 5584 | assert( pgnoChild<=pagerPagecount(pPage->pBt->pPager) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5585 | rc = sqlite3BtreeGetPage(pPage->pBt, pgnoChild, &pChild, 0); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5586 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5587 | if( pPage->pgno==1 ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5588 | rc = sqlite3BtreeInitPage(pChild, pPage); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5589 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5590 | assert( pChild->nOverflow==0 ); |
| 5591 | if( pChild->nFree>=100 ){ |
| 5592 | /* The child information will fit on the root page, so do the |
| 5593 | ** copy */ |
| 5594 | int i; |
| 5595 | zeroPage(pPage, pChild->aData[0]); |
| 5596 | for(i=0; i<pChild->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5597 | apCell[i] = findCell(pChild,i); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5598 | szCell[i] = cellSizePtr(pChild, apCell[i]); |
| 5599 | } |
| 5600 | assemblePage(pPage, pChild->nCell, apCell, szCell); |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 5601 | /* Copy the right-pointer of the child to the parent. */ |
| 5602 | put4byte(&pPage->aData[pPage->hdrOffset+8], |
| 5603 | get4byte(&pChild->aData[pChild->hdrOffset+8])); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5604 | freePage(pChild); |
| 5605 | TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno)); |
| 5606 | }else{ |
| 5607 | /* The child has more information that will fit on the root. |
| 5608 | ** The tree is already balanced. Do nothing. */ |
| 5609 | TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno)); |
| 5610 | } |
| 5611 | }else{ |
| 5612 | memcpy(pPage->aData, pChild->aData, pPage->pBt->usableSize); |
| 5613 | pPage->isInit = 0; |
| 5614 | pPage->pParent = 0; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5615 | rc = sqlite3BtreeInitPage(pPage, 0); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5616 | assert( rc==SQLITE_OK ); |
| 5617 | freePage(pChild); |
| 5618 | TRACE(("BALANCE: transfer child %d into root %d\n", |
| 5619 | pChild->pgno, pPage->pgno)); |
| 5620 | } |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5621 | rc = reparentChildPages(pPage, 1); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5622 | assert( pPage->nOverflow==0 ); |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5623 | if( ISAUTOVACUUM ){ |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 5624 | int i; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5625 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5626 | rc = ptrmapPutOvfl(pPage, i); |
| 5627 | if( rc!=SQLITE_OK ){ |
| 5628 | goto end_shallow_balance; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5629 | } |
| 5630 | } |
| 5631 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5632 | releasePage(pChild); |
| 5633 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5634 | end_shallow_balance: |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5635 | sqlite3_free(apCell); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5636 | return rc; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5637 | } |
| 5638 | |
| 5639 | |
| 5640 | /* |
| 5641 | ** The root page is overfull |
| 5642 | ** |
| 5643 | ** When this happens, Create a new child page and copy the |
| 5644 | ** contents of the root into the child. Then make the root |
| 5645 | ** page an empty page with rightChild pointing to the new |
| 5646 | ** child. Finally, call balance_internal() on the new child |
| 5647 | ** to cause it to split. |
| 5648 | */ |
| 5649 | static int balance_deeper(MemPage *pPage){ |
| 5650 | int rc; /* Return value from subprocedures */ |
| 5651 | MemPage *pChild; /* Pointer to a new child page */ |
| 5652 | Pgno pgnoChild; /* Page number of the new child page */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5653 | BtShared *pBt; /* The BTree */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5654 | int usableSize; /* Total usable size of a page */ |
| 5655 | u8 *data; /* Content of the parent page */ |
| 5656 | u8 *cdata; /* Content of the child page */ |
| 5657 | int hdr; /* Offset to page header in parent */ |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame^] | 5658 | int cbrk; /* Offset to content of first cell in parent */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5659 | |
| 5660 | assert( pPage->pParent==0 ); |
| 5661 | assert( pPage->nOverflow>0 ); |
| 5662 | pBt = pPage->pBt; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5663 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5664 | rc = allocateBtreePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5665 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5666 | assert( sqlite3PagerIswriteable(pChild->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5667 | usableSize = pBt->usableSize; |
| 5668 | data = pPage->aData; |
| 5669 | hdr = pPage->hdrOffset; |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame^] | 5670 | cbrk = get2byte(&data[hdr+5]); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5671 | cdata = pChild->aData; |
| 5672 | memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr); |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame^] | 5673 | memcpy(&cdata[cbrk], &data[cbrk], usableSize-cbrk); |
drh | 1013148 | 2008-07-11 03:34:09 +0000 | [diff] [blame] | 5674 | if( pChild->isInit ) return SQLITE_CORRUPT; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5675 | rc = sqlite3BtreeInitPage(pChild, pPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5676 | if( rc ) goto balancedeeper_out; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5677 | memcpy(pChild->aOvfl, pPage->aOvfl, pPage->nOverflow*sizeof(pPage->aOvfl[0])); |
| 5678 | pChild->nOverflow = pPage->nOverflow; |
| 5679 | if( pChild->nOverflow ){ |
| 5680 | pChild->nFree = 0; |
| 5681 | } |
| 5682 | assert( pChild->nCell==pPage->nCell ); |
| 5683 | zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF); |
| 5684 | put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild); |
| 5685 | TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno)); |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5686 | if( ISAUTOVACUUM ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5687 | int i; |
| 5688 | rc = ptrmapPut(pBt, pChild->pgno, PTRMAP_BTREE, pPage->pgno); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5689 | if( rc ) goto balancedeeper_out; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5690 | for(i=0; i<pChild->nCell; i++){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5691 | rc = ptrmapPutOvfl(pChild, i); |
| 5692 | if( rc!=SQLITE_OK ){ |
danielk1977 | 474b7cc | 2008-07-09 11:49:46 +0000 | [diff] [blame] | 5693 | goto balancedeeper_out; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5694 | } |
| 5695 | } |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5696 | rc = reparentChildPages(pChild, 1); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5697 | } |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5698 | if( rc==SQLITE_OK ){ |
| 5699 | rc = balance_nonroot(pChild); |
| 5700 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5701 | |
| 5702 | balancedeeper_out: |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5703 | releasePage(pChild); |
| 5704 | return rc; |
| 5705 | } |
| 5706 | |
| 5707 | /* |
| 5708 | ** Decide if the page pPage needs to be balanced. If balancing is |
| 5709 | ** required, call the appropriate balancing routine. |
| 5710 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5711 | static int balance(MemPage *pPage, int insert){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5712 | int rc = SQLITE_OK; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5713 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5714 | if( pPage->pParent==0 ){ |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 5715 | rc = sqlite3PagerWrite(pPage->pDbPage); |
| 5716 | if( rc==SQLITE_OK && pPage->nOverflow>0 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5717 | rc = balance_deeper(pPage); |
| 5718 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5719 | if( rc==SQLITE_OK && pPage->nCell==0 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5720 | rc = balance_shallower(pPage); |
| 5721 | } |
| 5722 | }else{ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5723 | if( pPage->nOverflow>0 || |
| 5724 | (!insert && pPage->nFree>pPage->pBt->usableSize*2/3) ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5725 | rc = balance_nonroot(pPage); |
| 5726 | } |
| 5727 | } |
| 5728 | return rc; |
| 5729 | } |
| 5730 | |
| 5731 | /* |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 5732 | ** This routine checks all cursors that point to table pgnoRoot. |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5733 | ** If any of those cursors were opened with wrFlag==0 in a different |
| 5734 | ** database connection (a database connection that shares the pager |
| 5735 | ** cache with the current connection) and that other connection |
| 5736 | ** is not in the ReadUncommmitted state, then this routine returns |
| 5737 | ** SQLITE_LOCKED. |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5738 | ** |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 5739 | ** As well as cursors with wrFlag==0, cursors with wrFlag==1 and |
| 5740 | ** isIncrblobHandle==1 are also considered 'read' cursors. Incremental |
| 5741 | ** blob cursors are used for both reading and writing. |
| 5742 | ** |
| 5743 | ** When pgnoRoot is the root page of an intkey table, this function is also |
| 5744 | ** responsible for invalidating incremental blob cursors when the table row |
| 5745 | ** on which they are opened is deleted or modified. Cursors are invalidated |
| 5746 | ** according to the following rules: |
| 5747 | ** |
| 5748 | ** 1) When BtreeClearTable() is called to completely delete the contents |
| 5749 | ** of a B-Tree table, pExclude is set to zero and parameter iRow is |
| 5750 | ** set to non-zero. In this case all incremental blob cursors open |
| 5751 | ** on the table rooted at pgnoRoot are invalidated. |
| 5752 | ** |
| 5753 | ** 2) When BtreeInsert(), BtreeDelete() or BtreePutData() is called to |
| 5754 | ** modify a table row via an SQL statement, pExclude is set to the |
| 5755 | ** write cursor used to do the modification and parameter iRow is set |
| 5756 | ** to the integer row id of the B-Tree entry being modified. Unless |
| 5757 | ** pExclude is itself an incremental blob cursor, then all incremental |
| 5758 | ** blob cursors open on row iRow of the B-Tree are invalidated. |
| 5759 | ** |
| 5760 | ** 3) If both pExclude and iRow are set to zero, no incremental blob |
| 5761 | ** cursors are invalidated. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5762 | */ |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 5763 | static int checkReadLocks( |
| 5764 | Btree *pBtree, |
| 5765 | Pgno pgnoRoot, |
| 5766 | BtCursor *pExclude, |
| 5767 | i64 iRow |
| 5768 | ){ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5769 | BtCursor *p; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5770 | BtShared *pBt = pBtree->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 5771 | sqlite3 *db = pBtree->db; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5772 | assert( sqlite3BtreeHoldsMutex(pBtree) ); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5773 | for(p=pBt->pCursor; p; p=p->pNext){ |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5774 | if( p==pExclude ) continue; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5775 | if( p->pgnoRoot!=pgnoRoot ) continue; |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 5776 | #ifndef SQLITE_OMIT_INCRBLOB |
| 5777 | if( p->isIncrblobHandle && ( |
| 5778 | (!pExclude && iRow) |
| 5779 | || (pExclude && !pExclude->isIncrblobHandle && p->info.nKey==iRow) |
| 5780 | )){ |
| 5781 | p->eState = CURSOR_INVALID; |
| 5782 | } |
| 5783 | #endif |
| 5784 | if( p->eState!=CURSOR_VALID ) continue; |
| 5785 | if( p->wrFlag==0 |
| 5786 | #ifndef SQLITE_OMIT_INCRBLOB |
| 5787 | || p->isIncrblobHandle |
| 5788 | #endif |
| 5789 | ){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 5790 | sqlite3 *dbOther = p->pBtree->db; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5791 | if( dbOther==0 || |
| 5792 | (dbOther!=db && (dbOther->flags & SQLITE_ReadUncommitted)==0) ){ |
| 5793 | return SQLITE_LOCKED; |
| 5794 | } |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5795 | } |
| 5796 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5797 | return SQLITE_OK; |
| 5798 | } |
| 5799 | |
| 5800 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5801 | ** Insert a new record into the BTree. The key is given by (pKey,nKey) |
| 5802 | ** 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] | 5803 | ** define what table the record should be inserted into. The cursor |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5804 | ** is left pointing at a random location. |
| 5805 | ** |
| 5806 | ** For an INTKEY table, only the nKey value of the key is used. pKey is |
| 5807 | ** ignored. For a ZERODATA table, the pData and nData are both ignored. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5808 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5809 | int sqlite3BtreeInsert( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 5810 | BtCursor *pCur, /* Insert data into the table of this cursor */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 5811 | const void *pKey, i64 nKey, /* The key of the new record */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 5812 | const void *pData, int nData, /* The data of the new record */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5813 | int nZero, /* Number of extra 0 bytes to append to data */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 5814 | int appendBias /* True if this is likely an append */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5815 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5816 | int rc; |
| 5817 | int loc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5818 | int szNew; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5819 | MemPage *pPage; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5820 | Btree *p = pCur->pBtree; |
| 5821 | BtShared *pBt = p->pBt; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5822 | unsigned char *oldCell; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5823 | unsigned char *newCell = 0; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5824 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5825 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5826 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5827 | /* Must start a transaction before doing an insert */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5828 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5829 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5830 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5831 | assert( !pBt->readOnly ); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 5832 | if( !pCur->wrFlag ){ |
| 5833 | return SQLITE_PERM; /* Cursor not open for writing */ |
| 5834 | } |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 5835 | if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur, nKey) ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5836 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 5837 | } |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 5838 | if( pCur->eState==CURSOR_FAULT ){ |
| 5839 | return pCur->skip; |
| 5840 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5841 | |
| 5842 | /* Save the positions of any other cursors open on this table */ |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 5843 | clearCursorPosition(pCur); |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5844 | if( |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5845 | SQLITE_OK!=(rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur)) || |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 5846 | SQLITE_OK!=(rc = sqlite3BtreeMoveto(pCur, pKey, nKey, appendBias, &loc)) |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5847 | ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5848 | return rc; |
| 5849 | } |
| 5850 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5851 | pPage = pCur->pPage; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 5852 | assert( pPage->intKey || nKey>=0 ); |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 5853 | assert( pPage->leaf || !pPage->intKey ); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5854 | TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n", |
| 5855 | pCur->pgnoRoot, nKey, nData, pPage->pgno, |
| 5856 | loc==0 ? "overwrite" : "new entry")); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 5857 | assert( pPage->isInit ); |
danielk1977 | 52ae724 | 2008-03-25 14:24:56 +0000 | [diff] [blame] | 5858 | allocateTempSpace(pBt); |
| 5859 | newCell = pBt->pTmpSpace; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5860 | if( newCell==0 ) return SQLITE_NOMEM; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5861 | rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5862 | if( rc ) goto end_insert; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5863 | assert( szNew==cellSizePtr(pPage, newCell) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5864 | assert( szNew<=MX_CELL_SIZE(pBt) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5865 | if( loc==0 && CURSOR_VALID==pCur->eState ){ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5866 | u16 szOld; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5867 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 5868 | rc = sqlite3PagerWrite(pPage->pDbPage); |
| 5869 | if( rc ){ |
| 5870 | goto end_insert; |
| 5871 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5872 | oldCell = findCell(pPage, pCur->idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5873 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5874 | memcpy(newCell, oldCell, 4); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5875 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5876 | szOld = cellSizePtr(pPage, oldCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5877 | rc = clearCell(pPage, oldCell); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5878 | if( rc ) goto end_insert; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5879 | dropCell(pPage, pCur->idx, szOld); |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 5880 | }else if( loc<0 && pPage->nCell>0 ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5881 | assert( pPage->leaf ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5882 | pCur->idx++; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 5883 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 5884 | pCur->validNKey = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5885 | }else{ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5886 | assert( pPage->leaf ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5887 | } |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 5888 | rc = insertCell(pPage, pCur->idx, newCell, szNew, 0, 0); |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 5889 | if( rc!=SQLITE_OK ) goto end_insert; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5890 | rc = balance(pPage, 1); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5891 | if( rc==SQLITE_OK ){ |
| 5892 | moveToRoot(pCur); |
| 5893 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5894 | end_insert: |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5895 | return rc; |
| 5896 | } |
| 5897 | |
| 5898 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5899 | ** Delete the entry that the cursor is pointing to. The cursor |
| 5900 | ** is left pointing at a random location. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5901 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5902 | int sqlite3BtreeDelete(BtCursor *pCur){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5903 | MemPage *pPage = pCur->pPage; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5904 | unsigned char *pCell; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5905 | int rc; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 5906 | Pgno pgnoChild = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5907 | Btree *p = pCur->pBtree; |
| 5908 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5909 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5910 | assert( cursorHoldsMutex(pCur) ); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 5911 | assert( pPage->isInit ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5912 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5913 | /* Must start a transaction before doing a delete */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5914 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5915 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5916 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5917 | assert( !pBt->readOnly ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 5918 | if( pCur->eState==CURSOR_FAULT ){ |
| 5919 | return pCur->skip; |
| 5920 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 5921 | if( pCur->idx >= pPage->nCell ){ |
| 5922 | return SQLITE_ERROR; /* The cursor is not pointing to anything */ |
| 5923 | } |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 5924 | if( !pCur->wrFlag ){ |
| 5925 | return SQLITE_PERM; /* Did not open this cursor for writing */ |
| 5926 | } |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 5927 | if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur, pCur->info.nKey) ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5928 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 5929 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5930 | |
| 5931 | /* Restore the current cursor position (a no-op if the cursor is not in |
| 5932 | ** CURSOR_REQUIRESEEK state) and save the positions of any other cursors |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5933 | ** open on the same table. Then call sqlite3PagerWrite() on the page |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5934 | ** that the entry will be deleted from. |
| 5935 | */ |
| 5936 | if( |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 5937 | (rc = restoreCursorPosition(pCur))!=0 || |
drh | d116739 | 2006-01-23 13:00:35 +0000 | [diff] [blame] | 5938 | (rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur))!=0 || |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5939 | (rc = sqlite3PagerWrite(pPage->pDbPage))!=0 |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5940 | ){ |
| 5941 | return rc; |
| 5942 | } |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5943 | |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 5944 | /* Locate the cell within its page and leave pCell pointing to the |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5945 | ** data. The clearCell() call frees any overflow pages associated with the |
| 5946 | ** cell. The cell itself is still intact. |
| 5947 | */ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5948 | pCell = findCell(pPage, pCur->idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5949 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5950 | pgnoChild = get4byte(pCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5951 | } |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 5952 | rc = clearCell(pPage, pCell); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5953 | if( rc ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5954 | return rc; |
| 5955 | } |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5956 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5957 | if( !pPage->leaf ){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5958 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5959 | ** 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] | 5960 | ** do something we will leave a hole on an internal page. |
| 5961 | ** We have to fill the hole by moving in a cell from a leaf. The |
| 5962 | ** next Cell after the one to be deleted is guaranteed to exist and |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5963 | ** to be a leaf so we can use it. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5964 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5965 | BtCursor leafCur; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5966 | unsigned char *pNext; |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5967 | int notUsed; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5968 | unsigned char *tempCell = 0; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 5969 | assert( !pPage->intKey ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5970 | sqlite3BtreeGetTempCursor(pCur, &leafCur); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5971 | rc = sqlite3BtreeNext(&leafCur, ¬Used); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5972 | if( rc==SQLITE_OK ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5973 | rc = sqlite3PagerWrite(leafCur.pPage->pDbPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5974 | } |
| 5975 | if( rc==SQLITE_OK ){ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5976 | u16 szNext; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5977 | TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n", |
| 5978 | pCur->pgnoRoot, pPage->pgno, leafCur.pPage->pgno)); |
| 5979 | dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell)); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5980 | pNext = findCell(leafCur.pPage, leafCur.idx); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5981 | szNext = cellSizePtr(leafCur.pPage, pNext); |
| 5982 | assert( MX_CELL_SIZE(pBt)>=szNext+4 ); |
danielk1977 | 52ae724 | 2008-03-25 14:24:56 +0000 | [diff] [blame] | 5983 | allocateTempSpace(pBt); |
| 5984 | tempCell = pBt->pTmpSpace; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5985 | if( tempCell==0 ){ |
| 5986 | rc = SQLITE_NOMEM; |
| 5987 | } |
danielk1977 | 8ea1cfa | 2008-01-01 06:19:02 +0000 | [diff] [blame] | 5988 | if( rc==SQLITE_OK ){ |
| 5989 | rc = insertCell(pPage, pCur->idx, pNext-4, szNext+4, tempCell, 0); |
| 5990 | } |
| 5991 | if( rc==SQLITE_OK ){ |
| 5992 | put4byte(findOverflowCell(pPage, pCur->idx), pgnoChild); |
| 5993 | rc = balance(pPage, 0); |
| 5994 | } |
| 5995 | if( rc==SQLITE_OK ){ |
| 5996 | dropCell(leafCur.pPage, leafCur.idx, szNext); |
| 5997 | rc = balance(leafCur.pPage, 0); |
| 5998 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5999 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6000 | sqlite3BtreeReleaseTempCursor(&leafCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 6001 | }else{ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 6002 | TRACE(("DELETE: table=%d delete from leaf %d\n", |
| 6003 | pCur->pgnoRoot, pPage->pgno)); |
| 6004 | dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell)); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 6005 | rc = balance(pPage, 0); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 6006 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6007 | if( rc==SQLITE_OK ){ |
| 6008 | moveToRoot(pCur); |
| 6009 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 6010 | return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 6011 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6012 | |
| 6013 | /* |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 6014 | ** Create a new BTree table. Write into *piTable the page |
| 6015 | ** number for the root page of the new table. |
| 6016 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 6017 | ** The type of type is determined by the flags parameter. Only the |
| 6018 | ** following values of flags are currently in use. Other values for |
| 6019 | ** flags might not work: |
| 6020 | ** |
| 6021 | ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys |
| 6022 | ** BTREE_ZERODATA Used for SQL indices |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6023 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6024 | static int btreeCreateTable(Btree *p, int *piTable, int flags){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6025 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6026 | MemPage *pRoot; |
| 6027 | Pgno pgnoRoot; |
| 6028 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6029 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6030 | assert( sqlite3BtreeHoldsMutex(p) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6031 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 6032 | /* Must start a transaction first */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6033 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
| 6034 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6035 | } |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 6036 | assert( !pBt->readOnly ); |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 6037 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6038 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 6039 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6040 | if( rc ){ |
| 6041 | return rc; |
| 6042 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6043 | #else |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6044 | if( pBt->autoVacuum ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6045 | Pgno pgnoMove; /* Move a page here to make room for the root-page */ |
| 6046 | MemPage *pPageMove; /* The page to move to. */ |
| 6047 | |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 6048 | /* Creating a new table may probably require moving an existing database |
| 6049 | ** to make room for the new tables root page. In case this page turns |
| 6050 | ** out to be an overflow page, delete all overflow page-map caches |
| 6051 | ** held by open cursors. |
| 6052 | */ |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 6053 | invalidateAllOverflowCache(pBt); |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 6054 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6055 | /* Read the value of meta[3] from the database to determine where the |
| 6056 | ** root page of the new table should go. meta[3] is the largest root-page |
| 6057 | ** created so far, so the new root-page is (meta[3]+1). |
| 6058 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6059 | rc = sqlite3BtreeGetMeta(p, 4, &pgnoRoot); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6060 | if( rc!=SQLITE_OK ){ |
| 6061 | return rc; |
| 6062 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6063 | pgnoRoot++; |
| 6064 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6065 | /* The new root-page may not be allocated on a pointer-map page, or the |
| 6066 | ** PENDING_BYTE page. |
| 6067 | */ |
drh | 7219043 | 2008-01-31 14:54:43 +0000 | [diff] [blame] | 6068 | while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) || |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6069 | pgnoRoot==PENDING_BYTE_PAGE(pBt) ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6070 | pgnoRoot++; |
| 6071 | } |
| 6072 | assert( pgnoRoot>=3 ); |
| 6073 | |
| 6074 | /* Allocate a page. The page that currently resides at pgnoRoot will |
| 6075 | ** be moved to the allocated page (unless the allocated page happens |
| 6076 | ** to reside at pgnoRoot). |
| 6077 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 6078 | rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6079 | if( rc!=SQLITE_OK ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6080 | return rc; |
| 6081 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6082 | |
| 6083 | if( pgnoMove!=pgnoRoot ){ |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 6084 | /* pgnoRoot is the page that will be used for the root-page of |
| 6085 | ** the new table (assuming an error did not occur). But we were |
| 6086 | ** allocated pgnoMove. If required (i.e. if it was not allocated |
| 6087 | ** by extending the file), the current page at position pgnoMove |
| 6088 | ** is already journaled. |
| 6089 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6090 | u8 eType; |
| 6091 | Pgno iPtrPage; |
| 6092 | |
| 6093 | releasePage(pPageMove); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 6094 | |
| 6095 | /* Move the page currently at pgnoRoot to pgnoMove. */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6096 | rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6097 | if( rc!=SQLITE_OK ){ |
| 6098 | return rc; |
| 6099 | } |
| 6100 | rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage); |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 6101 | if( rc!=SQLITE_OK || eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6102 | releasePage(pRoot); |
| 6103 | return rc; |
| 6104 | } |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 6105 | assert( eType!=PTRMAP_ROOTPAGE ); |
| 6106 | assert( eType!=PTRMAP_FREEPAGE ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6107 | rc = sqlite3PagerWrite(pRoot->pDbPage); |
danielk1977 | 5fd057a | 2005-03-09 13:09:43 +0000 | [diff] [blame] | 6108 | if( rc!=SQLITE_OK ){ |
| 6109 | releasePage(pRoot); |
| 6110 | return rc; |
| 6111 | } |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 6112 | rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6113 | releasePage(pRoot); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 6114 | |
| 6115 | /* Obtain the page at pgnoRoot */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6116 | if( rc!=SQLITE_OK ){ |
| 6117 | return rc; |
| 6118 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6119 | rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6120 | if( rc!=SQLITE_OK ){ |
| 6121 | return rc; |
| 6122 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6123 | rc = sqlite3PagerWrite(pRoot->pDbPage); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6124 | if( rc!=SQLITE_OK ){ |
| 6125 | releasePage(pRoot); |
| 6126 | return rc; |
| 6127 | } |
| 6128 | }else{ |
| 6129 | pRoot = pPageMove; |
| 6130 | } |
| 6131 | |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 6132 | /* Update the pointer-map and meta-data with the new root-page number. */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6133 | rc = ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0); |
| 6134 | if( rc ){ |
| 6135 | releasePage(pRoot); |
| 6136 | return rc; |
| 6137 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6138 | rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6139 | if( rc ){ |
| 6140 | releasePage(pRoot); |
| 6141 | return rc; |
| 6142 | } |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 6143 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6144 | }else{ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 6145 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6146 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6147 | } |
| 6148 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6149 | assert( sqlite3PagerIswriteable(pRoot->pDbPage) ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 6150 | zeroPage(pRoot, flags | PTF_LEAF); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6151 | sqlite3PagerUnref(pRoot->pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6152 | *piTable = (int)pgnoRoot; |
| 6153 | return SQLITE_OK; |
| 6154 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6155 | int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){ |
| 6156 | int rc; |
| 6157 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6158 | p->pBt->db = p->db; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6159 | rc = btreeCreateTable(p, piTable, flags); |
| 6160 | sqlite3BtreeLeave(p); |
| 6161 | return rc; |
| 6162 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6163 | |
| 6164 | /* |
| 6165 | ** Erase the given database page and all its children. Return |
| 6166 | ** the page to the freelist. |
| 6167 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6168 | static int clearDatabasePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6169 | BtShared *pBt, /* The BTree that contains the table */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6170 | Pgno pgno, /* Page number to clear */ |
| 6171 | MemPage *pParent, /* Parent page. NULL for the root */ |
| 6172 | int freePageFlag /* Deallocate page if true */ |
| 6173 | ){ |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6174 | MemPage *pPage = 0; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6175 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6176 | unsigned char *pCell; |
| 6177 | int i; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6178 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6179 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 6180 | if( pgno>pagerPagecount(pBt->pPager) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 6181 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 6182 | } |
| 6183 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 6184 | rc = getAndInitPage(pBt, pgno, &pPage, pParent); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6185 | if( rc ) goto cleardatabasepage_out; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6186 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 6187 | pCell = findCell(pPage, i); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6188 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6189 | rc = clearDatabasePage(pBt, get4byte(pCell), pPage->pParent, 1); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6190 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6191 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6192 | rc = clearCell(pPage, pCell); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6193 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6194 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6195 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6196 | rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), pPage->pParent, 1); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6197 | if( rc ) goto cleardatabasepage_out; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6198 | } |
| 6199 | if( freePageFlag ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6200 | rc = freePage(pPage); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6201 | }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6202 | zeroPage(pPage, pPage->aData[0] | PTF_LEAF); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6203 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6204 | |
| 6205 | cleardatabasepage_out: |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6206 | releasePage(pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6207 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6208 | } |
| 6209 | |
| 6210 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 6211 | ** Delete all information from a single table in the database. iTable is |
| 6212 | ** the page number of the root of the table. After this routine returns, |
| 6213 | ** the root page is empty, but still exists. |
| 6214 | ** |
| 6215 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 6216 | ** read cursors on the table. Open write cursors are moved to the |
| 6217 | ** root of the table. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6218 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6219 | int sqlite3BtreeClearTable(Btree *p, int iTable){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6220 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6221 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6222 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6223 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6224 | if( p->inTrans!=TRANS_WRITE ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6225 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 6226 | }else if( (rc = checkReadLocks(p, iTable, 0, 1))!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6227 | /* nothing to do */ |
| 6228 | }else if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){ |
| 6229 | /* nothing to do */ |
| 6230 | }else{ |
| 6231 | rc = clearDatabasePage(pBt, (Pgno)iTable, 0, 0); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6232 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6233 | sqlite3BtreeLeave(p); |
| 6234 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6235 | } |
| 6236 | |
| 6237 | /* |
| 6238 | ** Erase all information in a table and add the root of the table to |
| 6239 | ** the freelist. Except, the root of the principle table (the one on |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 6240 | ** page 1) is never added to the freelist. |
| 6241 | ** |
| 6242 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 6243 | ** cursors on the table. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6244 | ** |
| 6245 | ** If AUTOVACUUM is enabled and the page at iTable is not the last |
| 6246 | ** root page in the database file, then the last root page |
| 6247 | ** in the database file is moved into the slot formerly occupied by |
| 6248 | ** iTable and that last slot formerly occupied by the last root page |
| 6249 | ** is added to the freelist instead of iTable. In this say, all |
| 6250 | ** root pages are kept at the beginning of the database file, which |
| 6251 | ** is necessary for AUTOVACUUM to work right. *piMoved is set to the |
| 6252 | ** page number that used to be the last root page in the file before |
| 6253 | ** the move. If no page gets moved, *piMoved is set to 0. |
| 6254 | ** The last root page is recorded in meta[3] and the value of |
| 6255 | ** meta[3] is updated by this procedure. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6256 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6257 | static int btreeDropTable(Btree *p, int iTable, int *piMoved){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6258 | int rc; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6259 | MemPage *pPage = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6260 | BtShared *pBt = p->pBt; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6261 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6262 | assert( sqlite3BtreeHoldsMutex(p) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6263 | if( p->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 6264 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6265 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6266 | |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 6267 | /* It is illegal to drop a table if any cursors are open on the |
| 6268 | ** database. This is because in auto-vacuum mode the backend may |
| 6269 | ** need to move another root-page to fill a gap left by the deleted |
| 6270 | ** root page. If an open cursor was using this page a problem would |
| 6271 | ** occur. |
| 6272 | */ |
| 6273 | if( pBt->pCursor ){ |
| 6274 | return SQLITE_LOCKED; |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 6275 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6276 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6277 | rc = sqlite3BtreeGetPage(pBt, (Pgno)iTable, &pPage, 0); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6278 | if( rc ) return rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6279 | rc = sqlite3BtreeClearTable(p, iTable); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6280 | if( rc ){ |
| 6281 | releasePage(pPage); |
| 6282 | return rc; |
| 6283 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6284 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6285 | *piMoved = 0; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6286 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6287 | if( iTable>1 ){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6288 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6289 | rc = freePage(pPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6290 | releasePage(pPage); |
| 6291 | #else |
| 6292 | if( pBt->autoVacuum ){ |
| 6293 | Pgno maxRootPgno; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6294 | rc = sqlite3BtreeGetMeta(p, 4, &maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6295 | if( rc!=SQLITE_OK ){ |
| 6296 | releasePage(pPage); |
| 6297 | return rc; |
| 6298 | } |
| 6299 | |
| 6300 | if( iTable==maxRootPgno ){ |
| 6301 | /* If the table being dropped is the table with the largest root-page |
| 6302 | ** number in the database, put the root page on the free list. |
| 6303 | */ |
| 6304 | rc = freePage(pPage); |
| 6305 | releasePage(pPage); |
| 6306 | if( rc!=SQLITE_OK ){ |
| 6307 | return rc; |
| 6308 | } |
| 6309 | }else{ |
| 6310 | /* The table being dropped does not have the largest root-page |
| 6311 | ** number in the database. So move the page that does into the |
| 6312 | ** gap left by the deleted root-page. |
| 6313 | */ |
| 6314 | MemPage *pMove; |
| 6315 | releasePage(pPage); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6316 | rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6317 | if( rc!=SQLITE_OK ){ |
| 6318 | return rc; |
| 6319 | } |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 6320 | rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6321 | releasePage(pMove); |
| 6322 | if( rc!=SQLITE_OK ){ |
| 6323 | return rc; |
| 6324 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6325 | rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6326 | if( rc!=SQLITE_OK ){ |
| 6327 | return rc; |
| 6328 | } |
| 6329 | rc = freePage(pMove); |
| 6330 | releasePage(pMove); |
| 6331 | if( rc!=SQLITE_OK ){ |
| 6332 | return rc; |
| 6333 | } |
| 6334 | *piMoved = maxRootPgno; |
| 6335 | } |
| 6336 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6337 | /* Set the new 'max-root-page' value in the database header. This |
| 6338 | ** is the old value less one, less one more if that happens to |
| 6339 | ** be a root-page number, less one again if that is the |
| 6340 | ** PENDING_BYTE_PAGE. |
| 6341 | */ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 6342 | maxRootPgno--; |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6343 | if( maxRootPgno==PENDING_BYTE_PAGE(pBt) ){ |
| 6344 | maxRootPgno--; |
| 6345 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6346 | if( maxRootPgno==PTRMAP_PAGENO(pBt, maxRootPgno) ){ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 6347 | maxRootPgno--; |
| 6348 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6349 | assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) ); |
| 6350 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6351 | rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6352 | }else{ |
| 6353 | rc = freePage(pPage); |
| 6354 | releasePage(pPage); |
| 6355 | } |
| 6356 | #endif |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6357 | }else{ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6358 | /* If sqlite3BtreeDropTable was called on page 1. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6359 | zeroPage(pPage, PTF_INTKEY|PTF_LEAF ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6360 | releasePage(pPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6361 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6362 | return rc; |
| 6363 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6364 | int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){ |
| 6365 | int rc; |
| 6366 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6367 | p->pBt->db = p->db; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6368 | rc = btreeDropTable(p, iTable, piMoved); |
| 6369 | sqlite3BtreeLeave(p); |
| 6370 | return rc; |
| 6371 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6372 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 6373 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6374 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6375 | ** Read the meta-information out of a database file. Meta[0] |
| 6376 | ** is the number of free pages currently in the database. Meta[1] |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 6377 | ** through meta[15] are available for use by higher layers. Meta[0] |
| 6378 | ** is read-only, the others are read/write. |
| 6379 | ** |
| 6380 | ** The schema layer numbers meta values differently. At the schema |
| 6381 | ** layer (and the SetCookie and ReadCookie opcodes) the number of |
| 6382 | ** 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] | 6383 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6384 | int sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6385 | DbPage *pDbPage; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6386 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6387 | unsigned char *pP1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6388 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6389 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6390 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6391 | pBt->db = p->db; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6392 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6393 | /* Reading a meta-data value requires a read-lock on page 1 (and hence |
| 6394 | ** the sqlite_master table. We grab this lock regardless of whether or |
| 6395 | ** not the SQLITE_ReadUncommitted flag is set (the table rooted at page |
| 6396 | ** 1 is treated as a special case by queryTableLock() and lockTable()). |
| 6397 | */ |
| 6398 | rc = queryTableLock(p, 1, READ_LOCK); |
| 6399 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6400 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6401 | return rc; |
| 6402 | } |
| 6403 | |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6404 | assert( idx>=0 && idx<=15 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6405 | rc = sqlite3PagerGet(pBt->pPager, 1, &pDbPage); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6406 | if( rc ){ |
| 6407 | sqlite3BtreeLeave(p); |
| 6408 | return rc; |
| 6409 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6410 | pP1 = (unsigned char *)sqlite3PagerGetData(pDbPage); |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6411 | *pMeta = get4byte(&pP1[36 + idx*4]); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6412 | sqlite3PagerUnref(pDbPage); |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 6413 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6414 | /* If autovacuumed is disabled in this build but we are trying to |
| 6415 | ** access an autovacuumed database, then make the database readonly. |
| 6416 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6417 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 6418 | if( idx==4 && *pMeta>0 ) pBt->readOnly = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6419 | #endif |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 6420 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6421 | /* Grab the read-lock on page 1. */ |
| 6422 | rc = lockTable(p, 1, READ_LOCK); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6423 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6424 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6425 | } |
| 6426 | |
| 6427 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6428 | ** Write meta-information back into the database. Meta[0] is |
| 6429 | ** read-only and may not be written. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6430 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6431 | int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){ |
| 6432 | BtShared *pBt = p->pBt; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6433 | unsigned char *pP1; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6434 | int rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6435 | assert( idx>=1 && idx<=15 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6436 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6437 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6438 | if( p->inTrans!=TRANS_WRITE ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6439 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
| 6440 | }else{ |
| 6441 | assert( pBt->pPage1!=0 ); |
| 6442 | pP1 = pBt->pPage1->aData; |
| 6443 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 6444 | if( rc==SQLITE_OK ){ |
| 6445 | put4byte(&pP1[36 + idx*4], iMeta); |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 +0000 | [diff] [blame] | 6446 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6447 | if( idx==7 ){ |
| 6448 | assert( pBt->autoVacuum || iMeta==0 ); |
| 6449 | assert( iMeta==0 || iMeta==1 ); |
| 6450 | pBt->incrVacuum = iMeta; |
| 6451 | } |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 +0000 | [diff] [blame] | 6452 | #endif |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6453 | } |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 6454 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6455 | sqlite3BtreeLeave(p); |
| 6456 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6457 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 6458 | |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 6459 | /* |
| 6460 | ** Return the flag byte at the beginning of the page that the cursor |
| 6461 | ** is currently pointing to. |
| 6462 | */ |
| 6463 | int sqlite3BtreeFlags(BtCursor *pCur){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6464 | /* TODO: What about CURSOR_REQUIRESEEK state? Probably need to call |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 6465 | ** restoreCursorPosition() here. |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6466 | */ |
danielk1977 | e448dc4 | 2008-01-02 11:50:51 +0000 | [diff] [blame] | 6467 | MemPage *pPage; |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 6468 | restoreCursorPosition(pCur); |
danielk1977 | e448dc4 | 2008-01-02 11:50:51 +0000 | [diff] [blame] | 6469 | pPage = pCur->pPage; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6470 | assert( cursorHoldsMutex(pCur) ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 6471 | assert( pPage->pBt==pCur->pBt ); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 6472 | return pPage ? pPage->aData[pPage->hdrOffset] : 0; |
| 6473 | } |
| 6474 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6475 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6476 | /* |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6477 | ** Return the pager associated with a BTree. This routine is used for |
| 6478 | ** testing and debugging only. |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6479 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6480 | Pager *sqlite3BtreePager(Btree *p){ |
| 6481 | return p->pBt->pPager; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6482 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6483 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6484 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6485 | /* |
| 6486 | ** Append a message to the error message string. |
| 6487 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6488 | static void checkAppendMsg( |
| 6489 | IntegrityCk *pCheck, |
| 6490 | char *zMsg1, |
| 6491 | const char *zFormat, |
| 6492 | ... |
| 6493 | ){ |
| 6494 | va_list ap; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6495 | if( !pCheck->mxErr ) return; |
| 6496 | pCheck->mxErr--; |
| 6497 | pCheck->nErr++; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6498 | va_start(ap, zFormat); |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 6499 | if( pCheck->errMsg.nChar ){ |
| 6500 | sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6501 | } |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 6502 | if( zMsg1 ){ |
| 6503 | sqlite3StrAccumAppend(&pCheck->errMsg, zMsg1, -1); |
| 6504 | } |
| 6505 | sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap); |
| 6506 | va_end(ap); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 6507 | if( pCheck->errMsg.mallocFailed ){ |
| 6508 | pCheck->mallocFailed = 1; |
| 6509 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6510 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6511 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6512 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6513 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6514 | /* |
| 6515 | ** Add 1 to the reference count for page iPage. If this is the second |
| 6516 | ** reference to the page, add an error message to pCheck->zErrMsg. |
| 6517 | ** Return 1 if there are 2 ore more references to the page and 0 if |
| 6518 | ** if this is the first reference to the page. |
| 6519 | ** |
| 6520 | ** Also check that the page number is in bounds. |
| 6521 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 6522 | static int checkRef(IntegrityCk *pCheck, int iPage, char *zContext){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6523 | if( iPage==0 ) return 1; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 6524 | if( iPage>pCheck->nPage || iPage<0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6525 | checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6526 | return 1; |
| 6527 | } |
| 6528 | if( pCheck->anRef[iPage]==1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6529 | checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6530 | return 1; |
| 6531 | } |
| 6532 | return (pCheck->anRef[iPage]++)>1; |
| 6533 | } |
| 6534 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6535 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6536 | /* |
| 6537 | ** Check that the entry in the pointer-map for page iChild maps to |
| 6538 | ** page iParent, pointer type ptrType. If not, append an error message |
| 6539 | ** to pCheck. |
| 6540 | */ |
| 6541 | static void checkPtrmap( |
| 6542 | IntegrityCk *pCheck, /* Integrity check context */ |
| 6543 | Pgno iChild, /* Child page number */ |
| 6544 | u8 eType, /* Expected pointer map type */ |
| 6545 | Pgno iParent, /* Expected pointer map parent page number */ |
| 6546 | char *zContext /* Context description (used for error msg) */ |
| 6547 | ){ |
| 6548 | int rc; |
| 6549 | u8 ePtrmapType; |
| 6550 | Pgno iPtrmapParent; |
| 6551 | |
| 6552 | rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent); |
| 6553 | if( rc!=SQLITE_OK ){ |
| 6554 | checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild); |
| 6555 | return; |
| 6556 | } |
| 6557 | |
| 6558 | if( ePtrmapType!=eType || iPtrmapParent!=iParent ){ |
| 6559 | checkAppendMsg(pCheck, zContext, |
| 6560 | "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", |
| 6561 | iChild, eType, iParent, ePtrmapType, iPtrmapParent); |
| 6562 | } |
| 6563 | } |
| 6564 | #endif |
| 6565 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6566 | /* |
| 6567 | ** Check the integrity of the freelist or of an overflow page list. |
| 6568 | ** Verify that the number of pages on the list is N. |
| 6569 | */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6570 | static void checkList( |
| 6571 | IntegrityCk *pCheck, /* Integrity checking context */ |
| 6572 | int isFreeList, /* True for a freelist. False for overflow page list */ |
| 6573 | int iPage, /* Page number for first page in the list */ |
| 6574 | int N, /* Expected number of pages in the list */ |
| 6575 | char *zContext /* Context for error messages */ |
| 6576 | ){ |
| 6577 | int i; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6578 | int expected = N; |
| 6579 | int iFirst = iPage; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6580 | while( N-- > 0 && pCheck->mxErr ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6581 | DbPage *pOvflPage; |
| 6582 | unsigned char *pOvflData; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6583 | if( iPage<1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6584 | checkAppendMsg(pCheck, zContext, |
| 6585 | "%d of %d pages missing from overflow list starting at %d", |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6586 | N+1, expected, iFirst); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6587 | break; |
| 6588 | } |
| 6589 | if( checkRef(pCheck, iPage, zContext) ) break; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6590 | if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6591 | checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6592 | break; |
| 6593 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6594 | pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6595 | if( isFreeList ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6596 | int n = get4byte(&pOvflData[4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6597 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6598 | if( pCheck->pBt->autoVacuum ){ |
| 6599 | checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext); |
| 6600 | } |
| 6601 | #endif |
drh | 45b1fac | 2008-07-04 17:52:42 +0000 | [diff] [blame] | 6602 | if( n>pCheck->pBt->usableSize/4-2 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6603 | checkAppendMsg(pCheck, zContext, |
| 6604 | "freelist leaf count too big on page %d", iPage); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 6605 | N--; |
| 6606 | }else{ |
| 6607 | for(i=0; i<n; i++){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6608 | Pgno iFreePage = get4byte(&pOvflData[8+i*4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6609 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6610 | if( pCheck->pBt->autoVacuum ){ |
| 6611 | checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext); |
| 6612 | } |
| 6613 | #endif |
| 6614 | checkRef(pCheck, iFreePage, zContext); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 6615 | } |
| 6616 | N -= n; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6617 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6618 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6619 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6620 | else{ |
| 6621 | /* If this database supports auto-vacuum and iPage is not the last |
| 6622 | ** page in this overflow list, check that the pointer-map entry for |
| 6623 | ** the following page matches iPage. |
| 6624 | */ |
| 6625 | if( pCheck->pBt->autoVacuum && N>0 ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6626 | i = get4byte(pOvflData); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6627 | checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext); |
| 6628 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6629 | } |
| 6630 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6631 | iPage = get4byte(pOvflData); |
| 6632 | sqlite3PagerUnref(pOvflPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6633 | } |
| 6634 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6635 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6636 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6637 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6638 | /* |
| 6639 | ** Do various sanity checks on a single page of a tree. Return |
| 6640 | ** the tree depth. Root pages return 0. Parents of root pages |
| 6641 | ** return 1, and so forth. |
| 6642 | ** |
| 6643 | ** These checks are done: |
| 6644 | ** |
| 6645 | ** 1. Make sure that cells and freeblocks do not overlap |
| 6646 | ** but combine to completely cover the page. |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6647 | ** NO 2. Make sure cell keys are in order. |
| 6648 | ** NO 3. Make sure no key is less than or equal to zLowerBound. |
| 6649 | ** NO 4. Make sure no key is greater than or equal to zUpperBound. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6650 | ** 5. Check the integrity of overflow pages. |
| 6651 | ** 6. Recursively call checkTreePage on all children. |
| 6652 | ** 7. Verify that the depth of all children is the same. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6653 | ** 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] | 6654 | ** the root of the tree. |
| 6655 | */ |
| 6656 | static int checkTreePage( |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 6657 | IntegrityCk *pCheck, /* Context for the sanity check */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6658 | int iPage, /* Page number of the page to check */ |
| 6659 | MemPage *pParent, /* Parent page */ |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6660 | char *zParentContext /* Parent context */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6661 | ){ |
| 6662 | MemPage *pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6663 | int i, rc, depth, d2, pgno, cnt; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6664 | int hdr, cellStart; |
| 6665 | int nCell; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6666 | u8 *data; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6667 | BtShared *pBt; |
drh | 4f26bb6 | 2005-09-08 14:17:20 +0000 | [diff] [blame] | 6668 | int usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6669 | char zContext[100]; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6670 | char *hit; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6671 | |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6672 | sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage); |
danielk1977 | ef73ee9 | 2004-11-06 12:26:07 +0000 | [diff] [blame] | 6673 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6674 | /* Check that the page exists |
| 6675 | */ |
drh | d9cb6ac | 2005-10-20 07:28:17 +0000 | [diff] [blame] | 6676 | pBt = pCheck->pBt; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 6677 | usableSize = pBt->usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6678 | if( iPage==0 ) return 0; |
| 6679 | if( checkRef(pCheck, iPage, zParentContext) ) return 0; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6680 | if( (rc = sqlite3BtreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6681 | checkAppendMsg(pCheck, zContext, |
| 6682 | "unable to get the page. error code=%d", rc); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6683 | return 0; |
| 6684 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6685 | if( (rc = sqlite3BtreeInitPage(pPage, pParent))!=0 ){ |
| 6686 | checkAppendMsg(pCheck, zContext, |
| 6687 | "sqlite3BtreeInitPage() returns error code %d", rc); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 6688 | releasePage(pPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6689 | return 0; |
| 6690 | } |
| 6691 | |
| 6692 | /* Check out all the cells. |
| 6693 | */ |
| 6694 | depth = 0; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6695 | for(i=0; i<pPage->nCell && pCheck->mxErr; i++){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 6696 | u8 *pCell; |
| 6697 | int sz; |
| 6698 | CellInfo info; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6699 | |
| 6700 | /* Check payload overflow pages |
| 6701 | */ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6702 | sqlite3_snprintf(sizeof(zContext), zContext, |
| 6703 | "On tree page %d cell %d: ", iPage, i); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 6704 | pCell = findCell(pPage,i); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6705 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 6706 | sz = info.nData; |
| 6707 | if( !pPage->intKey ) sz += info.nKey; |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 6708 | assert( sz==info.nPayload ); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 6709 | if( sz>info.nLocal ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 6710 | int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6711 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
| 6712 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6713 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6714 | checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6715 | } |
| 6716 | #endif |
| 6717 | checkList(pCheck, 0, pgnoOvfl, nPage, zContext); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6718 | } |
| 6719 | |
| 6720 | /* Check sanity of left child page. |
| 6721 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6722 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6723 | pgno = get4byte(pCell); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6724 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6725 | if( pBt->autoVacuum ){ |
| 6726 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext); |
| 6727 | } |
| 6728 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6729 | d2 = checkTreePage(pCheck,pgno,pPage,zContext); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6730 | if( i>0 && d2!=depth ){ |
| 6731 | checkAppendMsg(pCheck, zContext, "Child page depth differs"); |
| 6732 | } |
| 6733 | depth = d2; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6734 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6735 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6736 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6737 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6738 | sqlite3_snprintf(sizeof(zContext), zContext, |
| 6739 | "On page %d at right child: ", iPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6740 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6741 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6742 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6743 | } |
| 6744 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6745 | checkTreePage(pCheck, pgno, pPage, zContext); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6746 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6747 | |
| 6748 | /* Check for complete coverage of the page |
| 6749 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6750 | data = pPage->aData; |
| 6751 | hdr = pPage->hdrOffset; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 6752 | hit = sqlite3PageMalloc( pBt->pageSize ); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 6753 | if( hit==0 ){ |
| 6754 | pCheck->mallocFailed = 1; |
| 6755 | }else{ |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 6756 | memset(hit, 0, usableSize ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6757 | memset(hit, 1, get2byte(&data[hdr+5])); |
| 6758 | nCell = get2byte(&data[hdr+3]); |
| 6759 | cellStart = hdr + 12 - 4*pPage->leaf; |
| 6760 | for(i=0; i<nCell; i++){ |
| 6761 | int pc = get2byte(&data[cellStart+i*2]); |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 6762 | u16 size = cellSizePtr(pPage, &data[pc]); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6763 | int j; |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 6764 | if( (pc+size-1)>=usableSize || pc<0 ){ |
| 6765 | checkAppendMsg(pCheck, 0, |
| 6766 | "Corruption detected in cell %d on page %d",i,iPage,0); |
| 6767 | }else{ |
| 6768 | for(j=pc+size-1; j>=pc; j--) hit[j]++; |
| 6769 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6770 | } |
| 6771 | for(cnt=0, i=get2byte(&data[hdr+1]); i>0 && i<usableSize && cnt<10000; |
| 6772 | cnt++){ |
| 6773 | int size = get2byte(&data[i+2]); |
| 6774 | int j; |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 6775 | if( (i+size-1)>=usableSize || i<0 ){ |
| 6776 | checkAppendMsg(pCheck, 0, |
| 6777 | "Corruption detected in cell %d on page %d",i,iPage,0); |
| 6778 | }else{ |
| 6779 | for(j=i+size-1; j>=i; j--) hit[j]++; |
| 6780 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6781 | i = get2byte(&data[i]); |
| 6782 | } |
| 6783 | for(i=cnt=0; i<usableSize; i++){ |
| 6784 | if( hit[i]==0 ){ |
| 6785 | cnt++; |
| 6786 | }else if( hit[i]>1 ){ |
| 6787 | checkAppendMsg(pCheck, 0, |
| 6788 | "Multiple uses for byte %d of page %d", i, iPage); |
| 6789 | break; |
| 6790 | } |
| 6791 | } |
| 6792 | if( cnt!=data[hdr+7] ){ |
| 6793 | checkAppendMsg(pCheck, 0, |
| 6794 | "Fragmented space is %d byte reported as %d on page %d", |
| 6795 | cnt, data[hdr+7], iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6796 | } |
| 6797 | } |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 6798 | sqlite3PageFree(hit); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6799 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6800 | releasePage(pPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6801 | return depth+1; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6802 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6803 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6804 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6805 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6806 | /* |
| 6807 | ** This routine does a complete check of the given BTree file. aRoot[] is |
| 6808 | ** an array of pages numbers were each page number is the root page of |
| 6809 | ** a table. nRoot is the number of entries in aRoot. |
| 6810 | ** |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 6811 | ** Write the number of error seen in *pnErr. Except for some memory |
| 6812 | ** allocation errors, nn error message is held in memory obtained from |
| 6813 | ** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is |
| 6814 | ** returned. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6815 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6816 | char *sqlite3BtreeIntegrityCheck( |
| 6817 | Btree *p, /* The btree to be checked */ |
| 6818 | int *aRoot, /* An array of root pages numbers for individual trees */ |
| 6819 | int nRoot, /* Number of entries in aRoot[] */ |
| 6820 | int mxErr, /* Stop reporting errors after this many */ |
| 6821 | int *pnErr /* Write number of errors seen to this variable */ |
| 6822 | ){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6823 | int i; |
| 6824 | int nRef; |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 6825 | IntegrityCk sCheck; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6826 | BtShared *pBt = p->pBt; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 6827 | char zErr[100]; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6828 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6829 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6830 | pBt->db = p->db; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6831 | nRef = sqlite3PagerRefcount(pBt->pPager); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6832 | if( lockBtreeWithRetry(p)!=SQLITE_OK ){ |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 6833 | *pnErr = 1; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6834 | sqlite3BtreeLeave(p); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 6835 | return sqlite3DbStrDup(0, "cannot acquire a read lock on the database"); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 6836 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6837 | sCheck.pBt = pBt; |
| 6838 | sCheck.pPager = pBt->pPager; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 6839 | sCheck.nPage = pagerPagecount(sCheck.pPager); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6840 | sCheck.mxErr = mxErr; |
| 6841 | sCheck.nErr = 0; |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 6842 | sCheck.mallocFailed = 0; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6843 | *pnErr = 0; |
danielk1977 | e5321f0 | 2007-04-27 07:05:44 +0000 | [diff] [blame] | 6844 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6845 | if( pBt->nTrunc!=0 ){ |
| 6846 | sCheck.nPage = pBt->nTrunc; |
| 6847 | } |
| 6848 | #endif |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 6849 | if( sCheck.nPage==0 ){ |
| 6850 | unlockBtreeIfUnused(pBt); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6851 | sqlite3BtreeLeave(p); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 6852 | return 0; |
| 6853 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 6854 | sCheck.anRef = sqlite3Malloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) ); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 6855 | if( !sCheck.anRef ){ |
| 6856 | unlockBtreeIfUnused(pBt); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6857 | *pnErr = 1; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6858 | sqlite3BtreeLeave(p); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 6859 | return 0; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 6860 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6861 | for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; } |
drh | 42cac6d | 2004-11-20 20:31:11 +0000 | [diff] [blame] | 6862 | i = PENDING_BYTE_PAGE(pBt); |
drh | 1f59571 | 2004-06-15 01:40:29 +0000 | [diff] [blame] | 6863 | if( i<=sCheck.nPage ){ |
| 6864 | sCheck.anRef[i] = 1; |
| 6865 | } |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 6866 | sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), 20000); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6867 | |
| 6868 | /* Check the integrity of the freelist |
| 6869 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6870 | checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), |
| 6871 | get4byte(&pBt->pPage1->aData[36]), "Main freelist: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6872 | |
| 6873 | /* Check all the tables. |
| 6874 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6875 | for(i=0; i<nRoot && sCheck.mxErr; i++){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 6876 | if( aRoot[i]==0 ) continue; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6877 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6878 | if( pBt->autoVacuum && aRoot[i]>1 ){ |
| 6879 | checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0); |
| 6880 | } |
| 6881 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6882 | checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6883 | } |
| 6884 | |
| 6885 | /* Make sure every page in the file is referenced |
| 6886 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6887 | for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6888 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6889 | if( sCheck.anRef[i]==0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6890 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6891 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6892 | #else |
| 6893 | /* If the database supports auto-vacuum, make sure no tables contain |
| 6894 | ** references to pointer-map pages. |
| 6895 | */ |
| 6896 | if( sCheck.anRef[i]==0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6897 | (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6898 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
| 6899 | } |
| 6900 | if( sCheck.anRef[i]!=0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6901 | (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6902 | checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i); |
| 6903 | } |
| 6904 | #endif |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6905 | } |
| 6906 | |
| 6907 | /* Make sure this analysis did not leave any unref() pages |
| 6908 | */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6909 | unlockBtreeIfUnused(pBt); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6910 | if( nRef != sqlite3PagerRefcount(pBt->pPager) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6911 | checkAppendMsg(&sCheck, 0, |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6912 | "Outstanding page count goes from %d to %d during this analysis", |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6913 | nRef, sqlite3PagerRefcount(pBt->pPager) |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6914 | ); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6915 | } |
| 6916 | |
| 6917 | /* Clean up and report errors. |
| 6918 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6919 | sqlite3BtreeLeave(p); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6920 | sqlite3_free(sCheck.anRef); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 6921 | if( sCheck.mallocFailed ){ |
| 6922 | sqlite3StrAccumReset(&sCheck.errMsg); |
| 6923 | *pnErr = sCheck.nErr+1; |
| 6924 | return 0; |
| 6925 | } |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6926 | *pnErr = sCheck.nErr; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 6927 | if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg); |
| 6928 | return sqlite3StrAccumFinish(&sCheck.errMsg); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6929 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6930 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 6931 | |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6932 | /* |
| 6933 | ** Return the full pathname of the underlying database file. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 6934 | ** |
| 6935 | ** The pager filename is invariant as long as the pager is |
| 6936 | ** open so it is safe to access without the BtShared mutex. |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6937 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6938 | const char *sqlite3BtreeGetFilename(Btree *p){ |
| 6939 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6940 | return sqlite3PagerFilename(p->pBt->pPager); |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6941 | } |
| 6942 | |
| 6943 | /* |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6944 | ** Return the pathname of the directory that contains the database file. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 6945 | ** |
| 6946 | ** The pager directory name is invariant as long as the pager is |
| 6947 | ** open so it is safe to access without the BtShared mutex. |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6948 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6949 | const char *sqlite3BtreeGetDirname(Btree *p){ |
| 6950 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6951 | return sqlite3PagerDirname(p->pBt->pPager); |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6952 | } |
| 6953 | |
| 6954 | /* |
| 6955 | ** Return the pathname of the journal file for this database. The return |
| 6956 | ** value of this routine is the same regardless of whether the journal file |
| 6957 | ** has been created or not. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 6958 | ** |
| 6959 | ** The pager journal filename is invariant as long as the pager is |
| 6960 | ** open so it is safe to access without the BtShared mutex. |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6961 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6962 | const char *sqlite3BtreeGetJournalname(Btree *p){ |
| 6963 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6964 | return sqlite3PagerJournalname(p->pBt->pPager); |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6965 | } |
| 6966 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6967 | #ifndef SQLITE_OMIT_VACUUM |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6968 | /* |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6969 | ** Copy the complete content of pBtFrom into pBtTo. A transaction |
| 6970 | ** must be active for both files. |
| 6971 | ** |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 6972 | ** The size of file pTo may be reduced by this operation. |
| 6973 | ** If anything goes wrong, the transaction on pTo is rolled back. |
| 6974 | ** |
| 6975 | ** If successful, CommitPhaseOne() may be called on pTo before returning. |
| 6976 | ** The caller should finish committing the transaction on pTo by calling |
| 6977 | ** sqlite3BtreeCommit(). |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6978 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6979 | static int btreeCopyFile(Btree *pTo, Btree *pFrom){ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6980 | int rc = SQLITE_OK; |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 6981 | Pgno i; |
| 6982 | |
| 6983 | Pgno nFromPage; /* Number of pages in pFrom */ |
| 6984 | Pgno nToPage; /* Number of pages in pTo */ |
| 6985 | Pgno nNewPage; /* Number of pages in pTo after the copy */ |
| 6986 | |
| 6987 | Pgno iSkip; /* Pending byte page in pTo */ |
| 6988 | int nToPageSize; /* Page size of pTo in bytes */ |
| 6989 | int nFromPageSize; /* Page size of pFrom in bytes */ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6990 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6991 | BtShared *pBtTo = pTo->pBt; |
| 6992 | BtShared *pBtFrom = pFrom->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6993 | pBtTo->db = pTo->db; |
| 6994 | pBtFrom->db = pFrom->db; |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 6995 | |
| 6996 | nToPageSize = pBtTo->pageSize; |
| 6997 | nFromPageSize = pBtFrom->pageSize; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6998 | |
| 6999 | if( pTo->inTrans!=TRANS_WRITE || pFrom->inTrans!=TRANS_WRITE ){ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 7000 | return SQLITE_ERROR; |
| 7001 | } |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7002 | if( pBtTo->pCursor ){ |
| 7003 | return SQLITE_BUSY; |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 7004 | } |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 7005 | |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 7006 | nToPage = pagerPagecount(pBtTo->pPager); |
| 7007 | nFromPage = pagerPagecount(pBtFrom->pPager); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7008 | iSkip = PENDING_BYTE_PAGE(pBtTo); |
| 7009 | |
| 7010 | /* Variable nNewPage is the number of pages required to store the |
| 7011 | ** contents of pFrom using the current page-size of pTo. |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 7012 | */ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7013 | nNewPage = ((i64)nFromPage * (i64)nFromPageSize + (i64)nToPageSize - 1) / |
| 7014 | (i64)nToPageSize; |
| 7015 | |
| 7016 | for(i=1; rc==SQLITE_OK && (i<=nToPage || i<=nNewPage); i++){ |
| 7017 | |
| 7018 | /* Journal the original page. |
| 7019 | ** |
| 7020 | ** iSkip is the page number of the locking page (PENDING_BYTE_PAGE) |
| 7021 | ** in database *pTo (before the copy). This page is never written |
| 7022 | ** into the journal file. Unless i==iSkip or the page was not |
| 7023 | ** present in pTo before the copy operation, journal page i from pTo. |
| 7024 | */ |
| 7025 | if( i!=iSkip && i<=nToPage ){ |
danielk1977 | 4abd544 | 2008-05-05 15:26:50 +0000 | [diff] [blame] | 7026 | DbPage *pDbPage = 0; |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7027 | rc = sqlite3PagerGet(pBtTo->pPager, i, &pDbPage); |
danielk1977 | 4abd544 | 2008-05-05 15:26:50 +0000 | [diff] [blame] | 7028 | if( rc==SQLITE_OK ){ |
| 7029 | rc = sqlite3PagerWrite(pDbPage); |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 7030 | if( rc==SQLITE_OK && i>nFromPage ){ |
| 7031 | /* Yeah. It seems wierd to call DontWrite() right after Write(). But |
| 7032 | ** that is because the names of those procedures do not exactly |
| 7033 | ** represent what they do. Write() really means "put this page in the |
| 7034 | ** rollback journal and mark it as dirty so that it will be written |
| 7035 | ** to the database file later." DontWrite() undoes the second part of |
| 7036 | ** that and prevents the page from being written to the database. The |
| 7037 | ** page is still on the rollback journal, though. And that is the |
| 7038 | ** whole point of this block: to put pages on the rollback journal. |
| 7039 | */ |
| 7040 | sqlite3PagerDontWrite(pDbPage); |
| 7041 | } |
| 7042 | sqlite3PagerUnref(pDbPage); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7043 | } |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7044 | } |
| 7045 | |
| 7046 | /* Overwrite the data in page i of the target database */ |
| 7047 | if( rc==SQLITE_OK && i!=iSkip && i<=nNewPage ){ |
| 7048 | |
| 7049 | DbPage *pToPage = 0; |
| 7050 | sqlite3_int64 iOff; |
| 7051 | |
| 7052 | rc = sqlite3PagerGet(pBtTo->pPager, i, &pToPage); |
| 7053 | if( rc==SQLITE_OK ){ |
| 7054 | rc = sqlite3PagerWrite(pToPage); |
| 7055 | } |
| 7056 | |
| 7057 | for( |
| 7058 | iOff=(i-1)*nToPageSize; |
| 7059 | rc==SQLITE_OK && iOff<i*nToPageSize; |
| 7060 | iOff += nFromPageSize |
| 7061 | ){ |
| 7062 | DbPage *pFromPage = 0; |
| 7063 | Pgno iFrom = (iOff/nFromPageSize)+1; |
| 7064 | |
| 7065 | if( iFrom==PENDING_BYTE_PAGE(pBtFrom) ){ |
| 7066 | continue; |
| 7067 | } |
| 7068 | |
| 7069 | rc = sqlite3PagerGet(pBtFrom->pPager, iFrom, &pFromPage); |
| 7070 | if( rc==SQLITE_OK ){ |
| 7071 | char *zTo = sqlite3PagerGetData(pToPage); |
| 7072 | char *zFrom = sqlite3PagerGetData(pFromPage); |
| 7073 | int nCopy; |
| 7074 | |
| 7075 | if( nFromPageSize>=nToPageSize ){ |
| 7076 | zFrom += ((i-1)*nToPageSize - ((iFrom-1)*nFromPageSize)); |
| 7077 | nCopy = nToPageSize; |
| 7078 | }else{ |
| 7079 | zTo += (((iFrom-1)*nFromPageSize) - (i-1)*nToPageSize); |
| 7080 | nCopy = nFromPageSize; |
| 7081 | } |
| 7082 | |
| 7083 | memcpy(zTo, zFrom, nCopy); |
| 7084 | sqlite3PagerUnref(pFromPage); |
| 7085 | } |
| 7086 | } |
| 7087 | |
| 7088 | if( pToPage ) sqlite3PagerUnref(pToPage); |
| 7089 | } |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 7090 | } |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7091 | |
| 7092 | /* If things have worked so far, the database file may need to be |
| 7093 | ** truncated. The complex part is that it may need to be truncated to |
| 7094 | ** a size that is not an integer multiple of nToPageSize - the current |
| 7095 | ** page size used by the pager associated with B-Tree pTo. |
| 7096 | ** |
| 7097 | ** For example, say the page-size of pTo is 2048 bytes and the original |
| 7098 | ** number of pages is 5 (10 KB file). If pFrom has a page size of 1024 |
| 7099 | ** bytes and 9 pages, then the file needs to be truncated to 9KB. |
| 7100 | */ |
| 7101 | if( rc==SQLITE_OK ){ |
| 7102 | if( nFromPageSize!=nToPageSize ){ |
| 7103 | sqlite3_file *pFile = sqlite3PagerFile(pBtTo->pPager); |
| 7104 | i64 iSize = (i64)nFromPageSize * (i64)nFromPage; |
| 7105 | i64 iNow = (i64)((nToPage>nNewPage)?nToPage:nNewPage) * (i64)nToPageSize; |
| 7106 | i64 iPending = ((i64)PENDING_BYTE_PAGE(pBtTo)-1) *(i64)nToPageSize; |
| 7107 | |
| 7108 | assert( iSize<=iNow ); |
| 7109 | |
| 7110 | /* Commit phase one syncs the journal file associated with pTo |
| 7111 | ** containing the original data. It does not sync the database file |
| 7112 | ** itself. After doing this it is safe to use OsTruncate() and other |
| 7113 | ** file APIs on the database file directly. |
| 7114 | */ |
| 7115 | pBtTo->db = pTo->db; |
| 7116 | rc = sqlite3PagerCommitPhaseOne(pBtTo->pPager, 0, 0, 1); |
| 7117 | if( iSize<iNow && rc==SQLITE_OK ){ |
| 7118 | rc = sqlite3OsTruncate(pFile, iSize); |
| 7119 | } |
| 7120 | |
| 7121 | /* The loop that copied data from database pFrom to pTo did not |
| 7122 | ** populate the locking page of database pTo. If the page-size of |
| 7123 | ** pFrom is smaller than that of pTo, this means some data will |
| 7124 | ** not have been copied. |
| 7125 | ** |
| 7126 | ** This block copies the missing data from database pFrom to pTo |
| 7127 | ** using file APIs. This is safe because at this point we know that |
| 7128 | ** all of the original data from pTo has been synced into the |
| 7129 | ** journal file. At this point it would be safe to do anything at |
| 7130 | ** all to the database file except truncate it to zero bytes. |
| 7131 | */ |
| 7132 | if( rc==SQLITE_OK && nFromPageSize<nToPageSize && iSize>iPending){ |
| 7133 | i64 iOff; |
| 7134 | for( |
| 7135 | iOff=iPending; |
| 7136 | rc==SQLITE_OK && iOff<(iPending+nToPageSize); |
| 7137 | iOff += nFromPageSize |
| 7138 | ){ |
| 7139 | DbPage *pFromPage = 0; |
| 7140 | Pgno iFrom = (iOff/nFromPageSize)+1; |
| 7141 | |
| 7142 | if( iFrom==PENDING_BYTE_PAGE(pBtFrom) || iFrom>nFromPage ){ |
| 7143 | continue; |
| 7144 | } |
| 7145 | |
| 7146 | rc = sqlite3PagerGet(pBtFrom->pPager, iFrom, &pFromPage); |
| 7147 | if( rc==SQLITE_OK ){ |
| 7148 | char *zFrom = sqlite3PagerGetData(pFromPage); |
| 7149 | rc = sqlite3OsWrite(pFile, zFrom, nFromPageSize, iOff); |
| 7150 | sqlite3PagerUnref(pFromPage); |
| 7151 | } |
| 7152 | } |
| 7153 | } |
| 7154 | |
| 7155 | /* Sync the database file */ |
| 7156 | if( rc==SQLITE_OK ){ |
| 7157 | rc = sqlite3PagerSync(pBtTo->pPager); |
| 7158 | } |
| 7159 | }else{ |
| 7160 | rc = sqlite3PagerTruncate(pBtTo->pPager, nNewPage); |
| 7161 | } |
| 7162 | if( rc==SQLITE_OK ){ |
| 7163 | pBtTo->pageSizeFixed = 0; |
| 7164 | } |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 7165 | } |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 7166 | |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 7167 | if( rc ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7168 | sqlite3BtreeRollback(pTo); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 7169 | } |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7170 | |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 7171 | return rc; |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 7172 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7173 | int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){ |
| 7174 | int rc; |
| 7175 | sqlite3BtreeEnter(pTo); |
| 7176 | sqlite3BtreeEnter(pFrom); |
| 7177 | rc = btreeCopyFile(pTo, pFrom); |
| 7178 | sqlite3BtreeLeave(pFrom); |
| 7179 | sqlite3BtreeLeave(pTo); |
| 7180 | return rc; |
| 7181 | } |
| 7182 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 7183 | #endif /* SQLITE_OMIT_VACUUM */ |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 7184 | |
| 7185 | /* |
| 7186 | ** Return non-zero if a transaction is active. |
| 7187 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7188 | int sqlite3BtreeIsInTrans(Btree *p){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7189 | assert( p==0 || sqlite3_mutex_held(p->db->mutex) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7190 | return (p && (p->inTrans==TRANS_WRITE)); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 7191 | } |
| 7192 | |
| 7193 | /* |
| 7194 | ** Return non-zero if a statement transaction is active. |
| 7195 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7196 | int sqlite3BtreeIsInStmt(Btree *p){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7197 | assert( sqlite3BtreeHoldsMutex(p) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7198 | return (p->pBt && p->pBt->inStmt); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 7199 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 7200 | |
| 7201 | /* |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 7202 | ** Return non-zero if a read (or write) transaction is active. |
| 7203 | */ |
| 7204 | int sqlite3BtreeIsInReadTrans(Btree *p){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7205 | assert( sqlite3_mutex_held(p->db->mutex) ); |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 7206 | return (p && (p->inTrans!=TRANS_NONE)); |
| 7207 | } |
| 7208 | |
| 7209 | /* |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7210 | ** This function returns a pointer to a blob of memory associated with |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 7211 | ** 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] | 7212 | ** purposes (for example, to store a high-level schema associated with |
| 7213 | ** the shared-btree). The btree layer manages reference counting issues. |
| 7214 | ** |
| 7215 | ** The first time this is called on a shared-btree, nBytes bytes of memory |
| 7216 | ** are allocated, zeroed, and returned to the caller. For each subsequent |
| 7217 | ** call the nBytes parameter is ignored and a pointer to the same blob |
| 7218 | ** of memory returned. |
| 7219 | ** |
danielk1977 | 171bfed | 2008-06-23 09:50:50 +0000 | [diff] [blame] | 7220 | ** If the nBytes parameter is 0 and the blob of memory has not yet been |
| 7221 | ** allocated, a null pointer is returned. If the blob has already been |
| 7222 | ** allocated, it is returned as normal. |
| 7223 | ** |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7224 | ** Just before the shared-btree is closed, the function passed as the |
| 7225 | ** xFree argument when the memory allocation was made is invoked on the |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 7226 | ** blob of allocated memory. This function should not call sqlite3_free() |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7227 | ** on the memory, the btree layer does that. |
| 7228 | */ |
| 7229 | void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){ |
| 7230 | BtShared *pBt = p->pBt; |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7231 | sqlite3BtreeEnter(p); |
danielk1977 | 171bfed | 2008-06-23 09:50:50 +0000 | [diff] [blame] | 7232 | if( !pBt->pSchema && nBytes ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 7233 | pBt->pSchema = sqlite3MallocZero(nBytes); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7234 | pBt->xFreeSchema = xFree; |
| 7235 | } |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7236 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7237 | return pBt->pSchema; |
| 7238 | } |
| 7239 | |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 7240 | /* |
| 7241 | ** Return true if another user of the same shared btree as the argument |
| 7242 | ** handle holds an exclusive lock on the sqlite_master table. |
| 7243 | */ |
| 7244 | int sqlite3BtreeSchemaLocked(Btree *p){ |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7245 | int rc; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7246 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7247 | sqlite3BtreeEnter(p); |
| 7248 | rc = (queryTableLock(p, MASTER_ROOT, READ_LOCK)!=SQLITE_OK); |
| 7249 | sqlite3BtreeLeave(p); |
| 7250 | return rc; |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 7251 | } |
| 7252 | |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 7253 | |
| 7254 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 7255 | /* |
| 7256 | ** Obtain a lock on the table whose root page is iTab. The |
| 7257 | ** lock is a write lock if isWritelock is true or a read lock |
| 7258 | ** if it is false. |
| 7259 | */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7260 | int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){ |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 7261 | int rc = SQLITE_OK; |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 7262 | if( p->sharable ){ |
| 7263 | u8 lockType = READ_LOCK + isWriteLock; |
| 7264 | assert( READ_LOCK+1==WRITE_LOCK ); |
| 7265 | assert( isWriteLock==0 || isWriteLock==1 ); |
| 7266 | sqlite3BtreeEnter(p); |
| 7267 | rc = queryTableLock(p, iTab, lockType); |
| 7268 | if( rc==SQLITE_OK ){ |
| 7269 | rc = lockTable(p, iTab, lockType); |
| 7270 | } |
| 7271 | sqlite3BtreeLeave(p); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7272 | } |
| 7273 | return rc; |
| 7274 | } |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 7275 | #endif |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 7276 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7277 | #ifndef SQLITE_OMIT_INCRBLOB |
| 7278 | /* |
| 7279 | ** Argument pCsr must be a cursor opened for writing on an |
| 7280 | ** INTKEY table currently pointing at a valid table entry. |
| 7281 | ** This function modifies the data stored as part of that entry. |
| 7282 | ** Only the data content may only be modified, it is not possible |
| 7283 | ** to change the length of the data stored. |
| 7284 | */ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7285 | int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7286 | assert( cursorHoldsMutex(pCsr) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7287 | assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) ); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7288 | assert(pCsr->isIncrblobHandle); |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 7289 | |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 7290 | restoreCursorPosition(pCsr); |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 7291 | assert( pCsr->eState!=CURSOR_REQUIRESEEK ); |
| 7292 | if( pCsr->eState!=CURSOR_VALID ){ |
| 7293 | return SQLITE_ABORT; |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7294 | } |
| 7295 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7296 | /* Check some preconditions: |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7297 | ** (a) the cursor is open for writing, |
| 7298 | ** (b) there is no read-lock on the table being modified and |
| 7299 | ** (c) the cursor points at a valid row of an intKey table. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7300 | */ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7301 | if( !pCsr->wrFlag ){ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7302 | return SQLITE_READONLY; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7303 | } |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 7304 | assert( !pCsr->pBt->readOnly |
| 7305 | && pCsr->pBt->inTransaction==TRANS_WRITE ); |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 7306 | if( checkReadLocks(pCsr->pBtree, pCsr->pgnoRoot, pCsr, 0) ){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7307 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 7308 | } |
| 7309 | if( pCsr->eState==CURSOR_INVALID || !pCsr->pPage->intKey ){ |
| 7310 | return SQLITE_ERROR; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7311 | } |
| 7312 | |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 7313 | return accessPayload(pCsr, offset, amt, (unsigned char *)z, 0, 1); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7314 | } |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7315 | |
| 7316 | /* |
| 7317 | ** 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] | 7318 | ** overflow list for the current row. This is used by cursors opened |
| 7319 | ** for incremental blob IO only. |
| 7320 | ** |
| 7321 | ** This function sets a flag only. The actual page location cache |
| 7322 | ** (stored in BtCursor.aOverflow[]) is allocated and used by function |
| 7323 | ** accessPayload() (the worker function for sqlite3BtreeData() and |
| 7324 | ** sqlite3BtreePutData()). |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7325 | */ |
| 7326 | void sqlite3BtreeCacheOverflow(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7327 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7328 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7329 | assert(!pCur->isIncrblobHandle); |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7330 | assert(!pCur->aOverflow); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7331 | pCur->isIncrblobHandle = 1; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7332 | } |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7333 | #endif |