drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 2 | ** 2004 April 6 |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame^] | 12 | ** $Id: btree.c,v 1.549 2008/12/17 17:30:26 danielk1977 Exp $ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 13 | ** |
| 14 | ** This file implements a external (disk-based) database using BTrees. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 15 | ** See the header comment on "btreeInt.h" for additional information. |
| 16 | ** Including a description of file format and an overview of operation. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 17 | */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 18 | #include "btreeInt.h" |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 19 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 20 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 21 | ** The header string that appears at the beginning of every |
| 22 | ** SQLite database. |
drh | 556b2a2 | 2005-06-14 16:04:05 +0000 | [diff] [blame] | 23 | */ |
drh | 556b2a2 | 2005-06-14 16:04:05 +0000 | [diff] [blame] | 24 | static const char zMagicHeader[] = SQLITE_FILE_HEADER; |
drh | 08ed44e | 2001-04-29 23:32:55 +0000 | [diff] [blame] | 25 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 26 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 27 | ** Set this global variable to 1 to enable tracing using the TRACE |
| 28 | ** macro. |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 29 | */ |
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 | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 37 | /* |
| 38 | ** Sometimes we need a small amount of code such as a variable initialization |
| 39 | ** to setup for a later assert() statement. We do not want this code to |
| 40 | ** appear when assert() is disabled. The following macro is therefore |
| 41 | ** used to contain that setup code. The "VVA" acronym stands for |
| 42 | ** "Verification, Validation, and Accreditation". In other words, the |
| 43 | ** code within VVA_ONLY() will only run during verification processes. |
| 44 | */ |
| 45 | #ifndef NDEBUG |
| 46 | # define VVA_ONLY(X) X |
| 47 | #else |
| 48 | # define VVA_ONLY(X) |
| 49 | #endif |
| 50 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 51 | |
| 52 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 53 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 54 | /* |
danielk1977 | 502b4e0 | 2008-09-02 14:07:24 +0000 | [diff] [blame] | 55 | ** A list of BtShared objects that are eligible for participation |
| 56 | ** in shared cache. This variable has file scope during normal builds, |
| 57 | ** but the test harness needs to access it so we make it global for |
| 58 | ** test builds. |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 59 | */ |
| 60 | #ifdef SQLITE_TEST |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 61 | BtShared *SQLITE_WSD sqlite3SharedCacheList = 0; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 62 | #else |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 63 | static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 64 | #endif |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 65 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| 66 | |
| 67 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 68 | /* |
| 69 | ** Enable or disable the shared pager and schema features. |
| 70 | ** |
| 71 | ** This routine has no effect on existing database connections. |
| 72 | ** The shared cache setting effects only future calls to |
| 73 | ** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2(). |
| 74 | */ |
| 75 | int sqlite3_enable_shared_cache(int enable){ |
danielk1977 | 502b4e0 | 2008-09-02 14:07:24 +0000 | [diff] [blame] | 76 | sqlite3GlobalConfig.sharedCacheEnabled = enable; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 77 | return SQLITE_OK; |
| 78 | } |
| 79 | #endif |
| 80 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 81 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 82 | /* |
drh | 66cbd15 | 2004-09-01 16:12:25 +0000 | [diff] [blame] | 83 | ** Forward declaration |
| 84 | */ |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 85 | static int checkReadLocks(Btree*, Pgno, BtCursor*, i64); |
drh | 66cbd15 | 2004-09-01 16:12:25 +0000 | [diff] [blame] | 86 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 87 | |
| 88 | #ifdef SQLITE_OMIT_SHARED_CACHE |
| 89 | /* |
| 90 | ** The functions queryTableLock(), lockTable() and unlockAllTables() |
| 91 | ** manipulate entries in the BtShared.pLock linked list used to store |
| 92 | ** shared-cache table level locks. If the library is compiled with the |
| 93 | ** shared-cache feature disabled, then there is only ever one user |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 94 | ** of each BtShared structure and so this locking is not necessary. |
| 95 | ** So define the lock related functions as no-ops. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 96 | */ |
| 97 | #define queryTableLock(a,b,c) SQLITE_OK |
| 98 | #define lockTable(a,b,c) SQLITE_OK |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 99 | #define unlockAllTables(a) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 100 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 101 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 102 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 103 | /* |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 104 | ** Query to see if btree handle p may obtain a lock of type eLock |
| 105 | ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return |
| 106 | ** SQLITE_OK if the lock may be obtained (by calling lockTable()), or |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 107 | ** SQLITE_LOCKED if not. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 108 | */ |
| 109 | static int queryTableLock(Btree *p, Pgno iTab, u8 eLock){ |
| 110 | BtShared *pBt = p->pBt; |
| 111 | BtLock *pIter; |
| 112 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 113 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 114 | assert( eLock==READ_LOCK || eLock==WRITE_LOCK ); |
| 115 | assert( p->db!=0 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 116 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 117 | /* This is a no-op if the shared-cache is not enabled */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 118 | if( !p->sharable ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 119 | return SQLITE_OK; |
| 120 | } |
| 121 | |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 122 | /* If some other connection is holding an exclusive lock, the |
| 123 | ** requested lock may not be obtained. |
| 124 | */ |
| 125 | if( pBt->pExclusive && pBt->pExclusive!=p ){ |
| 126 | return SQLITE_LOCKED; |
| 127 | } |
| 128 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 129 | /* This (along with lockTable()) is where the ReadUncommitted flag is |
| 130 | ** dealt with. If the caller is querying for a read-lock and the flag is |
| 131 | ** set, it is unconditionally granted - even if there are write-locks |
| 132 | ** on the table. If a write-lock is requested, the ReadUncommitted flag |
| 133 | ** is not considered. |
| 134 | ** |
| 135 | ** In function lockTable(), if a read-lock is demanded and the |
| 136 | ** ReadUncommitted flag is set, no entry is added to the locks list |
| 137 | ** (BtShared.pLock). |
| 138 | ** |
| 139 | ** To summarize: If the ReadUncommitted flag is set, then read cursors do |
| 140 | ** not create or respect table locks. The locking procedure for a |
| 141 | ** write-cursor does not change. |
| 142 | */ |
| 143 | if( |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 144 | 0==(p->db->flags&SQLITE_ReadUncommitted) || |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 145 | eLock==WRITE_LOCK || |
drh | 47ded16 | 2006-01-06 01:42:58 +0000 | [diff] [blame] | 146 | iTab==MASTER_ROOT |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 147 | ){ |
| 148 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 149 | if( pIter->pBtree!=p && pIter->iTable==iTab && |
| 150 | (pIter->eLock!=eLock || eLock!=READ_LOCK) ){ |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 151 | return SQLITE_LOCKED; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 152 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | return SQLITE_OK; |
| 156 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 157 | #endif /* !SQLITE_OMIT_SHARED_CACHE */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 158 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 159 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 160 | /* |
| 161 | ** Add a lock on the table with root-page iTable to the shared-btree used |
| 162 | ** by Btree handle p. Parameter eLock must be either READ_LOCK or |
| 163 | ** WRITE_LOCK. |
| 164 | ** |
| 165 | ** SQLITE_OK is returned if the lock is added successfully. SQLITE_BUSY and |
| 166 | ** SQLITE_NOMEM may also be returned. |
| 167 | */ |
| 168 | static int lockTable(Btree *p, Pgno iTable, u8 eLock){ |
| 169 | BtShared *pBt = p->pBt; |
| 170 | BtLock *pLock = 0; |
| 171 | BtLock *pIter; |
| 172 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 173 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 174 | assert( eLock==READ_LOCK || eLock==WRITE_LOCK ); |
| 175 | assert( p->db!=0 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 176 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 177 | /* This is a no-op if the shared-cache is not enabled */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 178 | if( !p->sharable ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 179 | return SQLITE_OK; |
| 180 | } |
| 181 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 182 | assert( SQLITE_OK==queryTableLock(p, iTable, eLock) ); |
| 183 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 184 | /* If the read-uncommitted flag is set and a read-lock is requested, |
| 185 | ** return early without adding an entry to the BtShared.pLock list. See |
| 186 | ** comment in function queryTableLock() for more info on handling |
| 187 | ** the ReadUncommitted flag. |
| 188 | */ |
| 189 | if( |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 190 | (p->db->flags&SQLITE_ReadUncommitted) && |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 191 | (eLock==READ_LOCK) && |
drh | 47ded16 | 2006-01-06 01:42:58 +0000 | [diff] [blame] | 192 | iTable!=MASTER_ROOT |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 193 | ){ |
| 194 | return SQLITE_OK; |
| 195 | } |
| 196 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 197 | /* First search the list for an existing lock on this table. */ |
| 198 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 199 | if( pIter->iTable==iTable && pIter->pBtree==p ){ |
| 200 | pLock = pIter; |
| 201 | break; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /* If the above search did not find a BtLock struct associating Btree p |
| 206 | ** with table iTable, allocate one and link it into the list. |
| 207 | */ |
| 208 | if( !pLock ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 209 | pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock)); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 210 | if( !pLock ){ |
| 211 | return SQLITE_NOMEM; |
| 212 | } |
| 213 | pLock->iTable = iTable; |
| 214 | pLock->pBtree = p; |
| 215 | pLock->pNext = pBt->pLock; |
| 216 | pBt->pLock = pLock; |
| 217 | } |
| 218 | |
| 219 | /* Set the BtLock.eLock variable to the maximum of the current lock |
| 220 | ** and the requested lock. This means if a write-lock was already held |
| 221 | ** and a read-lock requested, we don't incorrectly downgrade the lock. |
| 222 | */ |
| 223 | assert( WRITE_LOCK>READ_LOCK ); |
danielk1977 | 5118b91 | 2005-12-30 16:31:53 +0000 | [diff] [blame] | 224 | if( eLock>pLock->eLock ){ |
| 225 | pLock->eLock = eLock; |
| 226 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 227 | |
| 228 | return SQLITE_OK; |
| 229 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 230 | #endif /* !SQLITE_OMIT_SHARED_CACHE */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 231 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 232 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 233 | /* |
| 234 | ** Release all the table locks (locks obtained via calls to the lockTable() |
| 235 | ** procedure) held by Btree handle p. |
| 236 | */ |
| 237 | static void unlockAllTables(Btree *p){ |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 238 | BtShared *pBt = p->pBt; |
| 239 | BtLock **ppIter = &pBt->pLock; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 240 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 241 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 242 | assert( p->sharable || 0==*ppIter ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 243 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 244 | while( *ppIter ){ |
| 245 | BtLock *pLock = *ppIter; |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 246 | assert( pBt->pExclusive==0 || pBt->pExclusive==pLock->pBtree ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 247 | if( pLock->pBtree==p ){ |
| 248 | *ppIter = pLock->pNext; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 249 | sqlite3_free(pLock); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 250 | }else{ |
| 251 | ppIter = &pLock->pNext; |
| 252 | } |
| 253 | } |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 254 | |
| 255 | if( pBt->pExclusive==p ){ |
| 256 | pBt->pExclusive = 0; |
| 257 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 258 | } |
| 259 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| 260 | |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 261 | static void releasePage(MemPage *pPage); /* Forward reference */ |
| 262 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 263 | /* |
| 264 | ** Verify that the cursor holds a mutex on the BtShared |
| 265 | */ |
| 266 | #ifndef NDEBUG |
| 267 | static int cursorHoldsMutex(BtCursor *p){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 268 | return sqlite3_mutex_held(p->pBt->mutex); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 269 | } |
| 270 | #endif |
| 271 | |
| 272 | |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 273 | #ifndef SQLITE_OMIT_INCRBLOB |
| 274 | /* |
| 275 | ** Invalidate the overflow page-list cache for cursor pCur, if any. |
| 276 | */ |
| 277 | static void invalidateOverflowCache(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 278 | assert( cursorHoldsMutex(pCur) ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 279 | sqlite3_free(pCur->aOverflow); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 280 | pCur->aOverflow = 0; |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | ** Invalidate the overflow page-list cache for all cursors opened |
| 285 | ** on the shared btree structure pBt. |
| 286 | */ |
| 287 | static void invalidateAllOverflowCache(BtShared *pBt){ |
| 288 | BtCursor *p; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 289 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 290 | for(p=pBt->pCursor; p; p=p->pNext){ |
| 291 | invalidateOverflowCache(p); |
| 292 | } |
| 293 | } |
| 294 | #else |
| 295 | #define invalidateOverflowCache(x) |
| 296 | #define invalidateAllOverflowCache(x) |
| 297 | #endif |
| 298 | |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 299 | /* |
| 300 | ** Save the current cursor position in the variables BtCursor.nKey |
| 301 | ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK. |
| 302 | */ |
| 303 | static int saveCursorPosition(BtCursor *pCur){ |
| 304 | int rc; |
| 305 | |
| 306 | assert( CURSOR_VALID==pCur->eState ); |
| 307 | assert( 0==pCur->pKey ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 308 | assert( cursorHoldsMutex(pCur) ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 309 | |
| 310 | rc = sqlite3BtreeKeySize(pCur, &pCur->nKey); |
| 311 | |
| 312 | /* If this is an intKey table, then the above call to BtreeKeySize() |
| 313 | ** stores the integer key in pCur->nKey. In this case this value is |
| 314 | ** all that is required. Otherwise, if pCur is not open on an intKey |
| 315 | ** table, then malloc space for and store the pCur->nKey bytes of key |
| 316 | ** data. |
| 317 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 318 | if( rc==SQLITE_OK && 0==pCur->apPage[0]->intKey){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 319 | void *pKey = sqlite3Malloc( (int)pCur->nKey ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 320 | if( pKey ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 321 | rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 322 | if( rc==SQLITE_OK ){ |
| 323 | pCur->pKey = pKey; |
| 324 | }else{ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 325 | sqlite3_free(pKey); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 326 | } |
| 327 | }else{ |
| 328 | rc = SQLITE_NOMEM; |
| 329 | } |
| 330 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 331 | assert( !pCur->apPage[0]->intKey || !pCur->pKey ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 332 | |
| 333 | if( rc==SQLITE_OK ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 334 | int i; |
| 335 | for(i=0; i<=pCur->iPage; i++){ |
| 336 | releasePage(pCur->apPage[i]); |
| 337 | pCur->apPage[i] = 0; |
| 338 | } |
| 339 | pCur->iPage = -1; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 340 | pCur->eState = CURSOR_REQUIRESEEK; |
| 341 | } |
| 342 | |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 343 | invalidateOverflowCache(pCur); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 344 | return rc; |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | ** Save the positions of all cursors except pExcept open on the table |
| 349 | ** with root-page iRoot. Usually, this is called just before cursor |
| 350 | ** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()). |
| 351 | */ |
| 352 | static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){ |
| 353 | BtCursor *p; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 354 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 355 | assert( pExcept==0 || pExcept->pBt==pBt ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 356 | for(p=pBt->pCursor; p; p=p->pNext){ |
| 357 | if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) && |
| 358 | p->eState==CURSOR_VALID ){ |
| 359 | int rc = saveCursorPosition(p); |
| 360 | if( SQLITE_OK!=rc ){ |
| 361 | return rc; |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | return SQLITE_OK; |
| 366 | } |
| 367 | |
| 368 | /* |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 369 | ** Clear the current cursor position. |
| 370 | */ |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 371 | void sqlite3BtreeClearCursor(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 372 | assert( cursorHoldsMutex(pCur) ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 373 | sqlite3_free(pCur->pKey); |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 374 | pCur->pKey = 0; |
| 375 | pCur->eState = CURSOR_INVALID; |
| 376 | } |
| 377 | |
| 378 | /* |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 379 | ** Restore the cursor to the position it was in (or as close to as possible) |
| 380 | ** when saveCursorPosition() was called. Note that this call deletes the |
| 381 | ** saved position info stored by saveCursorPosition(), so there can be |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 382 | ** at most one effective restoreCursorPosition() call after each |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 383 | ** saveCursorPosition(). |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 384 | */ |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 385 | int sqlite3BtreeRestoreCursorPosition(BtCursor *pCur){ |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 386 | int rc; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 387 | assert( cursorHoldsMutex(pCur) ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 388 | assert( pCur->eState>=CURSOR_REQUIRESEEK ); |
| 389 | if( pCur->eState==CURSOR_FAULT ){ |
| 390 | return pCur->skip; |
| 391 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 392 | pCur->eState = CURSOR_INVALID; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 393 | rc = sqlite3BtreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skip); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 394 | if( rc==SQLITE_OK ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 395 | sqlite3_free(pCur->pKey); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 396 | pCur->pKey = 0; |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 397 | assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 398 | } |
| 399 | return rc; |
| 400 | } |
| 401 | |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 402 | #define restoreCursorPosition(p) \ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 403 | (p->eState>=CURSOR_REQUIRESEEK ? \ |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 404 | sqlite3BtreeRestoreCursorPosition(p) : \ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 405 | SQLITE_OK) |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 406 | |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 407 | /* |
| 408 | ** Determine whether or not a cursor has moved from the position it |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 409 | ** was last placed at. Cursors can move when the row they are pointing |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 410 | ** at is deleted out from under them. |
| 411 | ** |
| 412 | ** This routine returns an error code if something goes wrong. The |
| 413 | ** integer *pHasMoved is set to one if the cursor has moved and 0 if not. |
| 414 | */ |
| 415 | int sqlite3BtreeCursorHasMoved(BtCursor *pCur, int *pHasMoved){ |
| 416 | int rc; |
| 417 | |
| 418 | rc = restoreCursorPosition(pCur); |
| 419 | if( rc ){ |
| 420 | *pHasMoved = 1; |
| 421 | return rc; |
| 422 | } |
| 423 | if( pCur->eState!=CURSOR_VALID || pCur->skip!=0 ){ |
| 424 | *pHasMoved = 1; |
| 425 | }else{ |
| 426 | *pHasMoved = 0; |
| 427 | } |
| 428 | return SQLITE_OK; |
| 429 | } |
| 430 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 431 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 432 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 433 | ** Given a page number of a regular database page, return the page |
| 434 | ** number for the pointer-map page that contains the entry for the |
| 435 | ** input page number. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 436 | */ |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 437 | static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 438 | int nPagesPerMapPage; |
| 439 | Pgno iPtrMap, ret; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 440 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 441 | nPagesPerMapPage = (pBt->usableSize/5)+1; |
| 442 | iPtrMap = (pgno-2)/nPagesPerMapPage; |
| 443 | ret = (iPtrMap*nPagesPerMapPage) + 2; |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 444 | if( ret==PENDING_BYTE_PAGE(pBt) ){ |
| 445 | ret++; |
| 446 | } |
| 447 | return ret; |
| 448 | } |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 449 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 450 | /* |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 451 | ** Write an entry into the pointer map. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 452 | ** |
| 453 | ** This routine updates the pointer map entry for page number 'key' |
| 454 | ** so that it maps to type 'eType' and parent page number 'pgno'. |
| 455 | ** An error code is returned if something goes wrong, otherwise SQLITE_OK. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 456 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 457 | static int ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 458 | DbPage *pDbPage; /* The pointer map page */ |
| 459 | u8 *pPtrmap; /* The pointer map data */ |
| 460 | Pgno iPtrmap; /* The pointer map page number */ |
| 461 | int offset; /* Offset in pointer map page */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 462 | int rc; |
| 463 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 464 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 465 | /* The master-journal page number must never be used as a pointer map page */ |
| 466 | assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) ); |
| 467 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 468 | assert( pBt->autoVacuum ); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 469 | if( key==0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 470 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 471 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 472 | iPtrmap = PTRMAP_PAGENO(pBt, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 473 | rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 474 | if( rc!=SQLITE_OK ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 475 | return rc; |
| 476 | } |
danielk1977 | 8c666b1 | 2008-07-18 09:34:57 +0000 | [diff] [blame] | 477 | offset = PTRMAP_PTROFFSET(iPtrmap, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 478 | pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 479 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 480 | if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){ |
| 481 | TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent)); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 482 | rc = sqlite3PagerWrite(pDbPage); |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 483 | if( rc==SQLITE_OK ){ |
| 484 | pPtrmap[offset] = eType; |
| 485 | put4byte(&pPtrmap[offset+1], parent); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 486 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 487 | } |
| 488 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 489 | sqlite3PagerUnref(pDbPage); |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 490 | return rc; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | /* |
| 494 | ** Read an entry from the pointer map. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 495 | ** |
| 496 | ** This routine retrieves the pointer map entry for page 'key', writing |
| 497 | ** the type and parent page number to *pEType and *pPgno respectively. |
| 498 | ** An error code is returned if something goes wrong, otherwise SQLITE_OK. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 499 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 500 | static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 501 | DbPage *pDbPage; /* The pointer map page */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 502 | int iPtrmap; /* Pointer map page index */ |
| 503 | u8 *pPtrmap; /* Pointer map page data */ |
| 504 | int offset; /* Offset of entry in pointer map */ |
| 505 | int rc; |
| 506 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 507 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 508 | |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 509 | iPtrmap = PTRMAP_PAGENO(pBt, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 510 | rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 511 | if( rc!=0 ){ |
| 512 | return rc; |
| 513 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 514 | pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 515 | |
danielk1977 | 8c666b1 | 2008-07-18 09:34:57 +0000 | [diff] [blame] | 516 | offset = PTRMAP_PTROFFSET(iPtrmap, key); |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 517 | assert( pEType!=0 ); |
| 518 | *pEType = pPtrmap[offset]; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 519 | if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 520 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 521 | sqlite3PagerUnref(pDbPage); |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 522 | if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 523 | return SQLITE_OK; |
| 524 | } |
| 525 | |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 526 | #else /* if defined SQLITE_OMIT_AUTOVACUUM */ |
| 527 | #define ptrmapPut(w,x,y,z) SQLITE_OK |
| 528 | #define ptrmapGet(w,x,y,z) SQLITE_OK |
| 529 | #define ptrmapPutOvfl(y,z) SQLITE_OK |
| 530 | #endif |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 531 | |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 532 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 533 | ** Given a btree page and a cell index (0 means the first cell on |
| 534 | ** the page, 1 means the second cell, and so forth) return a pointer |
| 535 | ** to the cell content. |
| 536 | ** |
| 537 | ** This routine works only for pages that do not contain overflow cells. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 538 | */ |
drh | 1688c86 | 2008-07-18 02:44:17 +0000 | [diff] [blame] | 539 | #define findCell(P,I) \ |
| 540 | ((P)->aData + ((P)->maskPage & get2byte(&(P)->aData[(P)->cellOffset+2*(I)]))) |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 541 | |
| 542 | /* |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 543 | ** This a more complex version of findCell() that works for |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 544 | ** pages that do contain overflow cells. See insert |
| 545 | */ |
| 546 | static u8 *findOverflowCell(MemPage *pPage, int iCell){ |
| 547 | int i; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 548 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 549 | for(i=pPage->nOverflow-1; i>=0; i--){ |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 550 | int k; |
| 551 | struct _OvflCell *pOvfl; |
| 552 | pOvfl = &pPage->aOvfl[i]; |
| 553 | k = pOvfl->idx; |
| 554 | if( k<=iCell ){ |
| 555 | if( k==iCell ){ |
| 556 | return pOvfl->pCell; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 557 | } |
| 558 | iCell--; |
| 559 | } |
| 560 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 561 | return findCell(pPage, iCell); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | /* |
| 565 | ** Parse a cell content block and fill in the CellInfo structure. There |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 566 | ** are two versions of this function. sqlite3BtreeParseCell() takes a |
| 567 | ** cell index as the second argument and sqlite3BtreeParseCellPtr() |
| 568 | ** takes a pointer to the body of the cell as its second argument. |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 569 | ** |
| 570 | ** Within this file, the parseCell() macro can be called instead of |
| 571 | ** sqlite3BtreeParseCellPtr(). Using some compilers, this will be faster. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 572 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 573 | void sqlite3BtreeParseCellPtr( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 574 | MemPage *pPage, /* Page containing the cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 575 | u8 *pCell, /* Pointer to the cell text. */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 576 | CellInfo *pInfo /* Fill in this structure */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 577 | ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 578 | u16 n; /* Number bytes in cell content header */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 579 | u32 nPayload; /* Number of bytes of cell payload */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 580 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 581 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 582 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 583 | pInfo->pCell = pCell; |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 584 | assert( pPage->leaf==0 || pPage->leaf==1 ); |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 585 | n = pPage->childPtrSize; |
| 586 | assert( n==4-4*pPage->leaf ); |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 587 | if( pPage->intKey ){ |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 588 | if( pPage->hasData ){ |
| 589 | n += getVarint32(&pCell[n], nPayload); |
| 590 | }else{ |
| 591 | nPayload = 0; |
| 592 | } |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 593 | n += getVarint(&pCell[n], (u64*)&pInfo->nKey); |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 594 | pInfo->nData = nPayload; |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 595 | }else{ |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 596 | pInfo->nData = 0; |
| 597 | n += getVarint32(&pCell[n], nPayload); |
| 598 | pInfo->nKey = nPayload; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 599 | } |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 600 | pInfo->nPayload = nPayload; |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 601 | pInfo->nHeader = n; |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 602 | if( likely(nPayload<=pPage->maxLocal) ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 603 | /* This is the (easy) common case where the entire payload fits |
| 604 | ** on the local page. No overflow is required. |
| 605 | */ |
| 606 | int nSize; /* Total size of cell content in bytes */ |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 607 | nSize = nPayload + n; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 608 | pInfo->nLocal = (u16)nPayload; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 609 | pInfo->iOverflow = 0; |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 610 | if( (nSize & ~3)==0 ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 611 | nSize = 4; /* Minimum cell size is 4 */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 612 | } |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 613 | pInfo->nSize = (u16)nSize; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 614 | }else{ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 615 | /* If the payload will not fit completely on the local page, we have |
| 616 | ** to decide how much to store locally and how much to spill onto |
| 617 | ** overflow pages. The strategy is to minimize the amount of unused |
| 618 | ** space on overflow pages while keeping the amount of local storage |
| 619 | ** in between minLocal and maxLocal. |
| 620 | ** |
| 621 | ** Warning: changing the way overflow payload is distributed in any |
| 622 | ** way will result in an incompatible file format. |
| 623 | */ |
| 624 | int minLocal; /* Minimum amount of payload held locally */ |
| 625 | int maxLocal; /* Maximum amount of payload held locally */ |
| 626 | int surplus; /* Overflow payload available for local storage */ |
| 627 | |
| 628 | minLocal = pPage->minLocal; |
| 629 | maxLocal = pPage->maxLocal; |
| 630 | surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 631 | if( surplus <= maxLocal ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 632 | pInfo->nLocal = (u16)surplus; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 633 | }else{ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 634 | pInfo->nLocal = (u16)minLocal; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 635 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 636 | pInfo->iOverflow = (u16)(pInfo->nLocal + n); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 637 | pInfo->nSize = pInfo->iOverflow + 4; |
| 638 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 639 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 640 | #define parseCell(pPage, iCell, pInfo) \ |
| 641 | sqlite3BtreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo)) |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 642 | void sqlite3BtreeParseCell( |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 643 | MemPage *pPage, /* Page containing the cell */ |
| 644 | int iCell, /* The cell index. First cell is 0 */ |
| 645 | CellInfo *pInfo /* Fill in this structure */ |
| 646 | ){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 647 | parseCell(pPage, iCell, pInfo); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 648 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 649 | |
| 650 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 651 | ** Compute the total number of bytes that a Cell needs in the cell |
| 652 | ** data area of the btree-page. The return number includes the cell |
| 653 | ** data header and the local payload, but not any overflow page or |
| 654 | ** the space used by the cell pointer. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 655 | */ |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 656 | #ifndef NDEBUG |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 657 | static u16 cellSize(MemPage *pPage, int iCell){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 658 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 659 | sqlite3BtreeParseCell(pPage, iCell, &info); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 660 | return info.nSize; |
| 661 | } |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 662 | #endif |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 663 | static u16 cellSizePtr(MemPage *pPage, u8 *pCell){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 664 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 665 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 666 | return info.nSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 667 | } |
| 668 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 669 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 670 | /* |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 671 | ** If the cell pCell, part of page pPage contains a pointer |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 672 | ** to an overflow page, insert an entry into the pointer-map |
| 673 | ** for the overflow page. |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 674 | */ |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 675 | static int ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell){ |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 676 | CellInfo info; |
| 677 | assert( pCell!=0 ); |
| 678 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
| 679 | assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); |
| 680 | if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ |
| 681 | Pgno ovfl = get4byte(&pCell[info.iOverflow]); |
| 682 | return ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 683 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 684 | return SQLITE_OK; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 685 | } |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 686 | /* |
| 687 | ** If the cell with index iCell on page pPage contains a pointer |
| 688 | ** to an overflow page, insert an entry into the pointer-map |
| 689 | ** for the overflow page. |
| 690 | */ |
| 691 | static int ptrmapPutOvfl(MemPage *pPage, int iCell){ |
| 692 | u8 *pCell; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 693 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 694 | pCell = findOverflowCell(pPage, iCell); |
| 695 | return ptrmapPutOvflPtr(pPage, pCell); |
| 696 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 697 | #endif |
| 698 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 699 | |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 700 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 701 | ** Defragment the page given. All Cells are moved to the |
drh | 3a4a2d4 | 2005-11-24 14:24:28 +0000 | [diff] [blame] | 702 | ** end of the page and all free space is collected into one |
| 703 | ** big FreeBlk that occurs in between the header and cell |
drh | 31beae9 | 2005-11-24 14:34:36 +0000 | [diff] [blame] | 704 | ** pointer array and the cell content area. |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 705 | */ |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 706 | static int defragmentPage(MemPage *pPage){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 707 | int i; /* Loop counter */ |
| 708 | int pc; /* Address of a i-th cell */ |
| 709 | int addr; /* Offset of first byte after cell pointer array */ |
| 710 | int hdr; /* Offset to the page header */ |
| 711 | int size; /* Size of a cell */ |
| 712 | int usableSize; /* Number of usable bytes on a page */ |
| 713 | int cellOffset; /* Offset to the cell pointer array */ |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 714 | int cbrk; /* Offset to the cell content area */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 715 | int nCell; /* Number of cells on the page */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 716 | unsigned char *data; /* The page data */ |
| 717 | unsigned char *temp; /* Temp area for cell content */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 718 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 719 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 720 | assert( pPage->pBt!=0 ); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 721 | assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 722 | assert( pPage->nOverflow==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 723 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 26b7994 | 2007-11-28 16:19:56 +0000 | [diff] [blame] | 724 | temp = sqlite3PagerTempSpace(pPage->pBt->pPager); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 725 | data = pPage->aData; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 726 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 727 | cellOffset = pPage->cellOffset; |
| 728 | nCell = pPage->nCell; |
| 729 | assert( nCell==get2byte(&data[hdr+3]) ); |
| 730 | usableSize = pPage->pBt->usableSize; |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 731 | cbrk = get2byte(&data[hdr+5]); |
| 732 | memcpy(&temp[cbrk], &data[cbrk], usableSize - cbrk); |
| 733 | cbrk = usableSize; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 734 | for(i=0; i<nCell; i++){ |
| 735 | u8 *pAddr; /* The i-th cell pointer */ |
| 736 | pAddr = &data[cellOffset + i*2]; |
| 737 | pc = get2byte(pAddr); |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 738 | if( pc>=usableSize ){ |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 739 | return SQLITE_CORRUPT_BKPT; |
| 740 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 741 | size = cellSizePtr(pPage, &temp[pc]); |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 742 | cbrk -= size; |
danielk1977 | 0d06541 | 2008-11-12 18:21:36 +0000 | [diff] [blame] | 743 | if( cbrk<cellOffset+2*nCell || pc+size>usableSize ){ |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 744 | return SQLITE_CORRUPT_BKPT; |
| 745 | } |
danielk1977 | 0d06541 | 2008-11-12 18:21:36 +0000 | [diff] [blame] | 746 | assert( cbrk+size<=usableSize && cbrk>=0 ); |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 747 | memcpy(&data[cbrk], &temp[pc], size); |
| 748 | put2byte(pAddr, cbrk); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 749 | } |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 750 | assert( cbrk>=cellOffset+2*nCell ); |
| 751 | put2byte(&data[hdr+5], cbrk); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 752 | data[hdr+1] = 0; |
| 753 | data[hdr+2] = 0; |
| 754 | data[hdr+7] = 0; |
| 755 | addr = cellOffset+2*nCell; |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 756 | memset(&data[addr], 0, cbrk-addr); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 757 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
danielk1977 | 360e634 | 2008-11-12 08:49:51 +0000 | [diff] [blame] | 758 | if( cbrk-addr!=pPage->nFree ){ |
| 759 | return SQLITE_CORRUPT_BKPT; |
| 760 | } |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 761 | return SQLITE_OK; |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 762 | } |
| 763 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 764 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 765 | ** Allocate nByte bytes of space on a page. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 766 | ** |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 767 | ** Return the index into pPage->aData[] of the first byte of |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 768 | ** the new allocation. The caller guarantees that there is enough |
| 769 | ** space. This routine will never fail. |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 770 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 771 | ** If the page contains nBytes of free space but does not contain |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 772 | ** nBytes of contiguous free space, then this routine automatically |
| 773 | ** calls defragementPage() to consolidate all free space before |
| 774 | ** allocating the new chunk. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 775 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 776 | static int allocateSpace(MemPage *pPage, int nByte){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 777 | int addr, pc, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 778 | int size; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 779 | int nFrag; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 780 | int top; |
| 781 | int nCell; |
| 782 | int cellOffset; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 783 | unsigned char *data; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 784 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 785 | data = pPage->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 786 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 787 | assert( pPage->pBt ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 788 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 789 | assert( nByte>=0 ); /* Minimum cell size is 4 */ |
| 790 | assert( pPage->nFree>=nByte ); |
| 791 | assert( pPage->nOverflow==0 ); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 792 | pPage->nFree -= (u16)nByte; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 793 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 794 | |
| 795 | nFrag = data[hdr+7]; |
| 796 | if( nFrag<60 ){ |
| 797 | /* Search the freelist looking for a slot big enough to satisfy the |
| 798 | ** space request. */ |
| 799 | addr = hdr+1; |
| 800 | while( (pc = get2byte(&data[addr]))>0 ){ |
| 801 | size = get2byte(&data[pc+2]); |
| 802 | if( size>=nByte ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 803 | int x = size - nByte; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 804 | if( size<nByte+4 ){ |
| 805 | memcpy(&data[addr], &data[pc], 2); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 806 | data[hdr+7] = (u8)(nFrag + x); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 807 | return pc; |
| 808 | }else{ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 809 | put2byte(&data[pc+2], x); |
| 810 | return pc + x; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 811 | } |
| 812 | } |
| 813 | addr = pc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 814 | } |
| 815 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 816 | |
| 817 | /* Allocate memory from the gap in between the cell pointer array |
| 818 | ** and the cell content area. |
| 819 | */ |
| 820 | top = get2byte(&data[hdr+5]); |
| 821 | nCell = get2byte(&data[hdr+3]); |
| 822 | cellOffset = pPage->cellOffset; |
| 823 | if( nFrag>=60 || cellOffset + 2*nCell > top - nByte ){ |
danielk1977 | 474b7cc | 2008-07-09 11:49:46 +0000 | [diff] [blame] | 824 | defragmentPage(pPage); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 825 | top = get2byte(&data[hdr+5]); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 826 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 827 | top -= nByte; |
| 828 | assert( cellOffset + 2*nCell <= top ); |
| 829 | put2byte(&data[hdr+5], top); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 830 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 831 | return top; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 835 | ** Return a section of the pPage->aData to the freelist. |
| 836 | ** The first byte of the new free block is pPage->aDisk[start] |
| 837 | ** and the size of the block is "size" bytes. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 838 | ** |
| 839 | ** Most of the effort here is involved in coalesing adjacent |
| 840 | ** free blocks into a single big free block. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 841 | */ |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 842 | static int freeSpace(MemPage *pPage, int start, int size){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 843 | int addr, pbegin, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 844 | unsigned char *data = pPage->aData; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 845 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 846 | assert( pPage->pBt!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 847 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 848 | assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) ); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 849 | assert( (start + size)<=pPage->pBt->usableSize ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 850 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 851 | assert( size>=0 ); /* Minimum cell size is 4 */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 852 | |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 853 | #ifdef SQLITE_SECURE_DELETE |
| 854 | /* Overwrite deleted information with zeros when the SECURE_DELETE |
| 855 | ** option is enabled at compile-time */ |
| 856 | memset(&data[start], 0, size); |
| 857 | #endif |
| 858 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 859 | /* Add the space back into the linked list of freeblocks */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 860 | hdr = pPage->hdrOffset; |
| 861 | addr = hdr + 1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 862 | while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 863 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 864 | if( pbegin<=addr ) { |
| 865 | return SQLITE_CORRUPT_BKPT; |
| 866 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 867 | addr = pbegin; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 868 | } |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 869 | if ( pbegin>pPage->pBt->usableSize-4 ) { |
| 870 | return SQLITE_CORRUPT_BKPT; |
| 871 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 872 | assert( pbegin>addr || pbegin==0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 873 | put2byte(&data[addr], start); |
| 874 | put2byte(&data[start], pbegin); |
| 875 | put2byte(&data[start+2], size); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 876 | pPage->nFree += (u16)size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 877 | |
| 878 | /* Coalesce adjacent free blocks */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 879 | addr = pPage->hdrOffset + 1; |
| 880 | while( (pbegin = get2byte(&data[addr]))>0 ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 881 | int pnext, psize, x; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 882 | assert( pbegin>addr ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 883 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 884 | pnext = get2byte(&data[pbegin]); |
| 885 | psize = get2byte(&data[pbegin+2]); |
| 886 | if( pbegin + psize + 3 >= pnext && pnext>0 ){ |
| 887 | int frag = pnext - (pbegin+psize); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 888 | if( (frag<0) || (frag>(int)data[pPage->hdrOffset+7]) ){ |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 889 | return SQLITE_CORRUPT_BKPT; |
| 890 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 891 | data[pPage->hdrOffset+7] -= (u8)frag; |
| 892 | x = get2byte(&data[pnext]); |
| 893 | put2byte(&data[pbegin], x); |
| 894 | x = pnext + get2byte(&data[pnext+2]) - pbegin; |
| 895 | put2byte(&data[pbegin+2], x); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 896 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 897 | addr = pbegin; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 898 | } |
| 899 | } |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 900 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 901 | /* If the cell content area begins with a freeblock, remove it. */ |
| 902 | if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){ |
| 903 | int top; |
| 904 | pbegin = get2byte(&data[hdr+1]); |
| 905 | memcpy(&data[hdr+1], &data[pbegin], 2); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 906 | top = get2byte(&data[hdr+5]) + get2byte(&data[pbegin+2]); |
| 907 | put2byte(&data[hdr+5], top); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 908 | } |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 909 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 910 | return SQLITE_OK; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 914 | ** Decode the flags byte (the first byte of the header) for a page |
| 915 | ** and initialize fields of the MemPage structure accordingly. |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 916 | ** |
| 917 | ** Only the following combinations are supported. Anything different |
| 918 | ** indicates a corrupt database files: |
| 919 | ** |
| 920 | ** PTF_ZERODATA |
| 921 | ** PTF_ZERODATA | PTF_LEAF |
| 922 | ** PTF_LEAFDATA | PTF_INTKEY |
| 923 | ** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 924 | */ |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 925 | static int decodeFlags(MemPage *pPage, int flagByte){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 926 | BtShared *pBt; /* A copy of pPage->pBt */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 927 | |
| 928 | assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 929 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 930 | pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 ); |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 931 | flagByte &= ~PTF_LEAF; |
| 932 | pPage->childPtrSize = 4-4*pPage->leaf; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 933 | pBt = pPage->pBt; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 934 | if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){ |
| 935 | pPage->intKey = 1; |
| 936 | pPage->hasData = pPage->leaf; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 937 | pPage->maxLocal = pBt->maxLeaf; |
| 938 | pPage->minLocal = pBt->minLeaf; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 939 | }else if( flagByte==PTF_ZERODATA ){ |
| 940 | pPage->intKey = 0; |
| 941 | pPage->hasData = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 942 | pPage->maxLocal = pBt->maxLocal; |
| 943 | pPage->minLocal = pBt->minLocal; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 944 | }else{ |
| 945 | return SQLITE_CORRUPT_BKPT; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 946 | } |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 947 | return SQLITE_OK; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 951 | ** Initialize the auxiliary information for a disk block. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 952 | ** |
| 953 | ** Return SQLITE_OK on success. If we see that the page does |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 954 | ** not contain a well-formed database page, then return |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 955 | ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not |
| 956 | ** guarantee that the page is well-formed. It only shows that |
| 957 | ** we failed to detect any corruption. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 958 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 959 | int sqlite3BtreeInitPage(MemPage *pPage){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 960 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 961 | assert( pPage->pBt!=0 ); |
| 962 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 963 | assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 964 | assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) ); |
| 965 | assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 966 | |
| 967 | if( !pPage->isInit ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 968 | u16 pc; /* Address of a freeblock within pPage->aData[] */ |
| 969 | u8 hdr; /* Offset to beginning of page header */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 970 | u8 *data; /* Equal to pPage->aData */ |
| 971 | BtShared *pBt; /* The main btree structure */ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 972 | u16 usableSize; /* Amount of usable space on each page */ |
| 973 | u16 cellOffset; /* Offset from start of page to first cell pointer */ |
| 974 | u16 nFree; /* Number of unused bytes on the page */ |
| 975 | u16 top; /* First byte of the cell content area */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 976 | |
| 977 | pBt = pPage->pBt; |
| 978 | |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 979 | hdr = pPage->hdrOffset; |
| 980 | data = pPage->aData; |
| 981 | if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT; |
| 982 | assert( pBt->pageSize>=512 && pBt->pageSize<=32768 ); |
| 983 | pPage->maskPage = pBt->pageSize - 1; |
| 984 | pPage->nOverflow = 0; |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 985 | usableSize = pBt->usableSize; |
| 986 | pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf; |
| 987 | top = get2byte(&data[hdr+5]); |
| 988 | pPage->nCell = get2byte(&data[hdr+3]); |
| 989 | if( pPage->nCell>MX_CELL(pBt) ){ |
| 990 | /* To many cells for a single page. The page must be corrupt */ |
| 991 | return SQLITE_CORRUPT_BKPT; |
| 992 | } |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 993 | |
| 994 | /* Compute the total free space on the page */ |
| 995 | pc = get2byte(&data[hdr+1]); |
| 996 | nFree = data[hdr+7] + top - (cellOffset + 2*pPage->nCell); |
| 997 | while( pc>0 ){ |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 998 | u16 next, size; |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 999 | if( pc>usableSize-4 ){ |
| 1000 | /* Free block is off the page */ |
| 1001 | return SQLITE_CORRUPT_BKPT; |
| 1002 | } |
| 1003 | next = get2byte(&data[pc]); |
| 1004 | size = get2byte(&data[pc+2]); |
| 1005 | if( next>0 && next<=pc+size+3 ){ |
| 1006 | /* Free blocks must be in accending order */ |
| 1007 | return SQLITE_CORRUPT_BKPT; |
| 1008 | } |
| 1009 | nFree += size; |
| 1010 | pc = next; |
| 1011 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1012 | pPage->nFree = (u16)nFree; |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1013 | if( nFree>=usableSize ){ |
| 1014 | /* Free space cannot exceed total page size */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1015 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1016 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1017 | |
drh | 1688c86 | 2008-07-18 02:44:17 +0000 | [diff] [blame] | 1018 | #if 0 |
| 1019 | /* Check that all the offsets in the cell offset array are within range. |
| 1020 | ** |
| 1021 | ** Omitting this consistency check and using the pPage->maskPage mask |
| 1022 | ** to prevent overrunning the page buffer in findCell() results in a |
| 1023 | ** 2.5% performance gain. |
| 1024 | */ |
| 1025 | { |
| 1026 | u8 *pOff; /* Iterator used to check all cell offsets are in range */ |
| 1027 | u8 *pEnd; /* Pointer to end of cell offset array */ |
| 1028 | u8 mask; /* Mask of bits that must be zero in MSB of cell offsets */ |
| 1029 | mask = ~(((u8)(pBt->pageSize>>8))-1); |
| 1030 | pEnd = &data[cellOffset + pPage->nCell*2]; |
| 1031 | for(pOff=&data[cellOffset]; pOff!=pEnd && !((*pOff)&mask); pOff+=2); |
| 1032 | if( pOff!=pEnd ){ |
| 1033 | return SQLITE_CORRUPT_BKPT; |
| 1034 | } |
danielk1977 | e16535f | 2008-06-11 18:15:29 +0000 | [diff] [blame] | 1035 | } |
drh | 1688c86 | 2008-07-18 02:44:17 +0000 | [diff] [blame] | 1036 | #endif |
danielk1977 | e16535f | 2008-06-11 18:15:29 +0000 | [diff] [blame] | 1037 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1038 | pPage->isInit = 1; |
| 1039 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1040 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1044 | ** Set up a raw page so that it looks like a database page holding |
| 1045 | ** no entries. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1046 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1047 | static void zeroPage(MemPage *pPage, int flags){ |
| 1048 | unsigned char *data = pPage->aData; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1049 | BtShared *pBt = pPage->pBt; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1050 | u8 hdr = pPage->hdrOffset; |
| 1051 | u16 first; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1052 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1053 | assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1054 | assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); |
| 1055 | assert( sqlite3PagerGetData(pPage->pDbPage) == data ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1056 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1057 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 1af4a6e | 2008-07-18 03:32:51 +0000 | [diff] [blame] | 1058 | /*memset(&data[hdr], 0, pBt->usableSize - hdr);*/ |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 1059 | data[hdr] = (char)flags; |
| 1060 | first = hdr + 8 + 4*((flags&PTF_LEAF)==0 ?1:0); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1061 | memset(&data[hdr+1], 0, 4); |
| 1062 | data[hdr+7] = 0; |
| 1063 | put2byte(&data[hdr+5], pBt->usableSize); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1064 | pPage->nFree = pBt->usableSize - first; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1065 | decodeFlags(pPage, flags); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1066 | pPage->hdrOffset = hdr; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1067 | pPage->cellOffset = first; |
| 1068 | pPage->nOverflow = 0; |
drh | 1688c86 | 2008-07-18 02:44:17 +0000 | [diff] [blame] | 1069 | assert( pBt->pageSize>=512 && pBt->pageSize<=32768 ); |
| 1070 | pPage->maskPage = pBt->pageSize - 1; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1071 | pPage->nCell = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1072 | pPage->isInit = 1; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1075 | |
| 1076 | /* |
| 1077 | ** Convert a DbPage obtained from the pager into a MemPage used by |
| 1078 | ** the btree layer. |
| 1079 | */ |
| 1080 | static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){ |
| 1081 | MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage); |
| 1082 | pPage->aData = sqlite3PagerGetData(pDbPage); |
| 1083 | pPage->pDbPage = pDbPage; |
| 1084 | pPage->pBt = pBt; |
| 1085 | pPage->pgno = pgno; |
| 1086 | pPage->hdrOffset = pPage->pgno==1 ? 100 : 0; |
| 1087 | return pPage; |
| 1088 | } |
| 1089 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1090 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1091 | ** Get a page from the pager. Initialize the MemPage.pBt and |
| 1092 | ** MemPage.aData elements if needed. |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 1093 | ** |
| 1094 | ** If the noContent flag is set, it means that we do not care about |
| 1095 | ** the content of the page at this time. So do not go to the disk |
| 1096 | ** to fetch the content. Just fill in the content with zeros for now. |
| 1097 | ** If in the future we call sqlite3PagerWrite() on this page, that |
| 1098 | ** means we have started to be concerned about content and the disk |
| 1099 | ** read should occur at that point. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1100 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1101 | int sqlite3BtreeGetPage( |
| 1102 | BtShared *pBt, /* The btree */ |
| 1103 | Pgno pgno, /* Number of the page to fetch */ |
| 1104 | MemPage **ppPage, /* Return the page in this parameter */ |
| 1105 | int noContent /* Do not load page content if true */ |
| 1106 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1107 | int rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1108 | DbPage *pDbPage; |
| 1109 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1110 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 1111 | rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1112 | if( rc ) return rc; |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1113 | *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1114 | return SQLITE_OK; |
| 1115 | } |
| 1116 | |
| 1117 | /* |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 1118 | ** Return the size of the database file in pages. If there is any kind of |
| 1119 | ** error, return ((unsigned int)-1). |
danielk1977 | 67fd7a9 | 2008-09-10 17:53:35 +0000 | [diff] [blame] | 1120 | */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 1121 | static Pgno pagerPagecount(BtShared *pBt){ |
| 1122 | int nPage = -1; |
danielk1977 | 67fd7a9 | 2008-09-10 17:53:35 +0000 | [diff] [blame] | 1123 | int rc; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 1124 | assert( pBt->pPage1 ); |
| 1125 | rc = sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 1126 | assert( rc==SQLITE_OK || nPage==-1 ); |
| 1127 | return (Pgno)nPage; |
danielk1977 | 67fd7a9 | 2008-09-10 17:53:35 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | /* |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1131 | ** Get a page from the pager and initialize it. This routine |
| 1132 | ** is just a convenience wrapper around separate calls to |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1133 | ** sqlite3BtreeGetPage() and sqlite3BtreeInitPage(). |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1134 | */ |
| 1135 | static int getAndInitPage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1136 | BtShared *pBt, /* The database file */ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1137 | Pgno pgno, /* Number of the page to get */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1138 | MemPage **ppPage /* Write the page pointer here */ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1139 | ){ |
| 1140 | int rc; |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1141 | DbPage *pDbPage; |
| 1142 | MemPage *pPage; |
| 1143 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1144 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1145 | if( pgno==0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1146 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1147 | } |
danielk1977 | 9f580ad | 2008-09-10 14:45:57 +0000 | [diff] [blame] | 1148 | |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1149 | /* It is often the case that the page we want is already in cache. |
| 1150 | ** If so, get it directly. This saves us from having to call |
| 1151 | ** pagerPagecount() to make sure pgno is within limits, which results |
| 1152 | ** in a measureable performance improvements. |
| 1153 | */ |
| 1154 | pDbPage = sqlite3PagerLookup(pBt->pPager, pgno); |
| 1155 | if( pDbPage ){ |
| 1156 | /* Page is already in cache */ |
| 1157 | *ppPage = pPage = btreePageFromDbPage(pDbPage, pgno, pBt); |
| 1158 | rc = SQLITE_OK; |
| 1159 | }else{ |
| 1160 | /* Page not in cache. Acquire it. */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 1161 | if( pgno>pagerPagecount(pBt) ){ |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1162 | return SQLITE_CORRUPT_BKPT; |
| 1163 | } |
| 1164 | rc = sqlite3BtreeGetPage(pBt, pgno, ppPage, 0); |
| 1165 | if( rc ) return rc; |
| 1166 | pPage = *ppPage; |
| 1167 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1168 | if( !pPage->isInit ){ |
| 1169 | rc = sqlite3BtreeInitPage(pPage); |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1170 | } |
| 1171 | if( rc!=SQLITE_OK ){ |
| 1172 | releasePage(pPage); |
| 1173 | *ppPage = 0; |
| 1174 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1175 | return rc; |
| 1176 | } |
| 1177 | |
| 1178 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1179 | ** Release a MemPage. This should be called once for each prior |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1180 | ** call to sqlite3BtreeGetPage. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1181 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1182 | static void releasePage(MemPage *pPage){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1183 | if( pPage ){ |
| 1184 | assert( pPage->aData ); |
| 1185 | assert( pPage->pBt ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1186 | assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); |
| 1187 | assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1188 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1189 | sqlite3PagerUnref(pPage->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1194 | ** During a rollback, when the pager reloads information into the cache |
| 1195 | ** so that the cache is restored to its original state at the start of |
| 1196 | ** the transaction, for each page restored this routine is called. |
| 1197 | ** |
| 1198 | ** This routine needs to reset the extra data section at the end of the |
| 1199 | ** page to agree with the restored data. |
| 1200 | */ |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1201 | static void pageReinit(DbPage *pData){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1202 | MemPage *pPage; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1203 | pPage = (MemPage *)sqlite3PagerGetExtra(pData); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1204 | if( pPage->isInit ){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1205 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1206 | pPage->isInit = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1207 | if( sqlite3PagerPageRefcount(pData)>0 ){ |
| 1208 | sqlite3BtreeInitPage(pPage); |
| 1209 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | /* |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1214 | ** Invoke the busy handler for a btree. |
| 1215 | */ |
danielk1977 | 1ceedd3 | 2008-11-19 10:22:33 +0000 | [diff] [blame] | 1216 | static int btreeInvokeBusyHandler(void *pArg){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1217 | BtShared *pBt = (BtShared*)pArg; |
| 1218 | assert( pBt->db ); |
| 1219 | assert( sqlite3_mutex_held(pBt->db->mutex) ); |
| 1220 | return sqlite3InvokeBusyHandler(&pBt->db->busyHandler); |
| 1221 | } |
| 1222 | |
| 1223 | /* |
drh | ad3e010 | 2004-09-03 23:32:18 +0000 | [diff] [blame] | 1224 | ** Open a database file. |
| 1225 | ** |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1226 | ** zFilename is the name of the database file. If zFilename is NULL |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1227 | ** a new database with a random name is created. This randomly named |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1228 | ** database file will be deleted when sqlite3BtreeClose() is called. |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1229 | ** If zFilename is ":memory:" then an in-memory database is created |
| 1230 | ** that is automatically destroyed when it is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1231 | */ |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1232 | int sqlite3BtreeOpen( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1233 | const char *zFilename, /* Name of the file containing the BTree database */ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1234 | sqlite3 *db, /* Associated database handle */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1235 | Btree **ppBtree, /* Pointer to new Btree object written here */ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1236 | int flags, /* Options */ |
| 1237 | int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1238 | ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1239 | sqlite3_vfs *pVfs; /* The VFS to use for this btree */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1240 | BtShared *pBt = 0; /* Shared part of btree structure */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1241 | Btree *p; /* Handle to return */ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1242 | int rc = SQLITE_OK; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1243 | u8 nReserve; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1244 | unsigned char zDbHeader[100]; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1245 | |
| 1246 | /* Set the variable isMemdb to true for an in-memory database, or |
| 1247 | ** false for a file-based database. This symbol is only required if |
| 1248 | ** either of the shared-data or autovacuum features are compiled |
| 1249 | ** into the library. |
| 1250 | */ |
| 1251 | #if !defined(SQLITE_OMIT_SHARED_CACHE) || !defined(SQLITE_OMIT_AUTOVACUUM) |
| 1252 | #ifdef SQLITE_OMIT_MEMORYDB |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 1253 | const int isMemdb = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1254 | #else |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 1255 | const int isMemdb = zFilename && !strcmp(zFilename, ":memory:"); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1256 | #endif |
| 1257 | #endif |
| 1258 | |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1259 | assert( db!=0 ); |
| 1260 | assert( sqlite3_mutex_held(db->mutex) ); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1261 | |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1262 | pVfs = db->pVfs; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1263 | p = sqlite3MallocZero(sizeof(Btree)); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1264 | if( !p ){ |
| 1265 | return SQLITE_NOMEM; |
| 1266 | } |
| 1267 | p->inTrans = TRANS_NONE; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1268 | p->db = db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1269 | |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 1270 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1271 | /* |
| 1272 | ** If this Btree is a candidate for shared cache, try to find an |
| 1273 | ** existing BtShared object that we can share with |
| 1274 | */ |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 1275 | if( isMemdb==0 |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1276 | && (db->flags & SQLITE_Vtab)==0 |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1277 | && zFilename && zFilename[0] |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1278 | ){ |
danielk1977 | 502b4e0 | 2008-09-02 14:07:24 +0000 | [diff] [blame] | 1279 | if( sqlite3GlobalConfig.sharedCacheEnabled ){ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 1280 | int nFullPathname = pVfs->mxPathname+1; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1281 | char *zFullPathname = sqlite3Malloc(nFullPathname); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1282 | sqlite3_mutex *mutexShared; |
| 1283 | p->sharable = 1; |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 1284 | db->flags |= SQLITE_SharedCache; |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1285 | if( !zFullPathname ){ |
| 1286 | sqlite3_free(p); |
| 1287 | return SQLITE_NOMEM; |
| 1288 | } |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 1289 | sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname); |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 1290 | mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1291 | sqlite3_mutex_enter(mutexShared); |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 1292 | for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1293 | assert( pBt->nRef>0 ); |
| 1294 | if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager)) |
| 1295 | && sqlite3PagerVfs(pBt->pPager)==pVfs ){ |
| 1296 | p->pBt = pBt; |
| 1297 | pBt->nRef++; |
| 1298 | break; |
| 1299 | } |
| 1300 | } |
| 1301 | sqlite3_mutex_leave(mutexShared); |
| 1302 | sqlite3_free(zFullPathname); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1303 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1304 | #ifdef SQLITE_DEBUG |
| 1305 | else{ |
| 1306 | /* In debug mode, we mark all persistent databases as sharable |
| 1307 | ** even when they are not. This exercises the locking code and |
| 1308 | ** gives more opportunity for asserts(sqlite3_mutex_held()) |
| 1309 | ** statements to find locking problems. |
| 1310 | */ |
| 1311 | p->sharable = 1; |
| 1312 | } |
| 1313 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1314 | } |
| 1315 | #endif |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1316 | if( pBt==0 ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1317 | /* |
| 1318 | ** The following asserts make sure that structures used by the btree are |
| 1319 | ** the right size. This is to guard against size changes that result |
| 1320 | ** when compiling on a different architecture. |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 1321 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1322 | assert( sizeof(i64)==8 || sizeof(i64)==4 ); |
| 1323 | assert( sizeof(u64)==8 || sizeof(u64)==4 ); |
| 1324 | assert( sizeof(u32)==4 ); |
| 1325 | assert( sizeof(u16)==2 ); |
| 1326 | assert( sizeof(Pgno)==4 ); |
| 1327 | |
| 1328 | pBt = sqlite3MallocZero( sizeof(*pBt) ); |
| 1329 | if( pBt==0 ){ |
| 1330 | rc = SQLITE_NOMEM; |
| 1331 | goto btree_open_out; |
| 1332 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1333 | rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename, |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1334 | EXTRA_SIZE, flags, vfsFlags); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1335 | if( rc==SQLITE_OK ){ |
| 1336 | rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader); |
| 1337 | } |
| 1338 | if( rc!=SQLITE_OK ){ |
| 1339 | goto btree_open_out; |
| 1340 | } |
danielk1977 | 1ceedd3 | 2008-11-19 10:22:33 +0000 | [diff] [blame] | 1341 | sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1342 | p->pBt = pBt; |
| 1343 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1344 | sqlite3PagerSetReiniter(pBt->pPager, pageReinit); |
| 1345 | pBt->pCursor = 0; |
| 1346 | pBt->pPage1 = 0; |
| 1347 | pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager); |
| 1348 | pBt->pageSize = get2byte(&zDbHeader[16]); |
| 1349 | if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE |
| 1350 | || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1351 | pBt->pageSize = 0; |
| 1352 | sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1353 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1354 | /* If the magic name ":memory:" will create an in-memory database, then |
| 1355 | ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if |
| 1356 | ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if |
| 1357 | ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a |
| 1358 | ** regular file-name. In this case the auto-vacuum applies as per normal. |
| 1359 | */ |
| 1360 | if( zFilename && !isMemdb ){ |
| 1361 | pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0); |
| 1362 | pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0); |
| 1363 | } |
| 1364 | #endif |
| 1365 | nReserve = 0; |
| 1366 | }else{ |
| 1367 | nReserve = zDbHeader[20]; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1368 | pBt->pageSizeFixed = 1; |
| 1369 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1370 | pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0); |
| 1371 | pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0); |
| 1372 | #endif |
| 1373 | } |
| 1374 | pBt->usableSize = pBt->pageSize - nReserve; |
| 1375 | assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1376 | sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1377 | |
| 1378 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
| 1379 | /* Add the new BtShared object to the linked list sharable BtShareds. |
| 1380 | */ |
| 1381 | if( p->sharable ){ |
| 1382 | sqlite3_mutex *mutexShared; |
| 1383 | pBt->nRef = 1; |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 1384 | mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 1385 | if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){ |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 1386 | pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST); |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 1387 | if( pBt->mutex==0 ){ |
| 1388 | rc = SQLITE_NOMEM; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1389 | db->mallocFailed = 0; |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 1390 | goto btree_open_out; |
| 1391 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1392 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1393 | sqlite3_mutex_enter(mutexShared); |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 1394 | pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList); |
| 1395 | GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1396 | sqlite3_mutex_leave(mutexShared); |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1397 | } |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1398 | #endif |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1399 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1400 | |
drh | cfed7bc | 2006-03-13 14:28:05 +0000 | [diff] [blame] | 1401 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1402 | /* If the new Btree uses a sharable pBtShared, then link the new |
| 1403 | ** Btree into the list of all sharable Btrees for the same connection. |
drh | abddb0c | 2007-08-20 13:14:28 +0000 | [diff] [blame] | 1404 | ** The list is kept in ascending order by pBt address. |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 1405 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1406 | if( p->sharable ){ |
| 1407 | int i; |
| 1408 | Btree *pSib; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1409 | for(i=0; i<db->nDb; i++){ |
| 1410 | if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1411 | while( pSib->pPrev ){ pSib = pSib->pPrev; } |
| 1412 | if( p->pBt<pSib->pBt ){ |
| 1413 | p->pNext = pSib; |
| 1414 | p->pPrev = 0; |
| 1415 | pSib->pPrev = p; |
| 1416 | }else{ |
drh | abddb0c | 2007-08-20 13:14:28 +0000 | [diff] [blame] | 1417 | while( pSib->pNext && pSib->pNext->pBt<p->pBt ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1418 | pSib = pSib->pNext; |
| 1419 | } |
| 1420 | p->pNext = pSib->pNext; |
| 1421 | p->pPrev = pSib; |
| 1422 | if( p->pNext ){ |
| 1423 | p->pNext->pPrev = p; |
| 1424 | } |
| 1425 | pSib->pNext = p; |
| 1426 | } |
| 1427 | break; |
| 1428 | } |
| 1429 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1430 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1431 | #endif |
| 1432 | *ppBtree = p; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1433 | |
| 1434 | btree_open_out: |
| 1435 | if( rc!=SQLITE_OK ){ |
| 1436 | if( pBt && pBt->pPager ){ |
| 1437 | sqlite3PagerClose(pBt->pPager); |
| 1438 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1439 | sqlite3_free(pBt); |
| 1440 | sqlite3_free(p); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1441 | *ppBtree = 0; |
| 1442 | } |
| 1443 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | /* |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1447 | ** Decrement the BtShared.nRef counter. When it reaches zero, |
| 1448 | ** remove the BtShared structure from the sharing list. Return |
| 1449 | ** true if the BtShared.nRef counter reaches zero and return |
| 1450 | ** false if it is still positive. |
| 1451 | */ |
| 1452 | static int removeFromSharingList(BtShared *pBt){ |
| 1453 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 1454 | sqlite3_mutex *pMaster; |
| 1455 | BtShared *pList; |
| 1456 | int removed = 0; |
| 1457 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1458 | assert( sqlite3_mutex_notheld(pBt->mutex) ); |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 1459 | pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1460 | sqlite3_mutex_enter(pMaster); |
| 1461 | pBt->nRef--; |
| 1462 | if( pBt->nRef<=0 ){ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 1463 | if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){ |
| 1464 | GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1465 | }else{ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 1466 | pList = GLOBAL(BtShared*,sqlite3SharedCacheList); |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 1467 | while( ALWAYS(pList) && pList->pNext!=pBt ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1468 | pList=pList->pNext; |
| 1469 | } |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 1470 | if( ALWAYS(pList) ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1471 | pList->pNext = pBt->pNext; |
| 1472 | } |
| 1473 | } |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 1474 | if( SQLITE_THREADSAFE ){ |
| 1475 | sqlite3_mutex_free(pBt->mutex); |
| 1476 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1477 | removed = 1; |
| 1478 | } |
| 1479 | sqlite3_mutex_leave(pMaster); |
| 1480 | return removed; |
| 1481 | #else |
| 1482 | return 1; |
| 1483 | #endif |
| 1484 | } |
| 1485 | |
| 1486 | /* |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1487 | ** Make sure pBt->pTmpSpace points to an allocation of |
| 1488 | ** MX_CELL_SIZE(pBt) bytes. |
| 1489 | */ |
| 1490 | static void allocateTempSpace(BtShared *pBt){ |
| 1491 | if( !pBt->pTmpSpace ){ |
| 1492 | pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize ); |
| 1493 | } |
| 1494 | } |
| 1495 | |
| 1496 | /* |
| 1497 | ** Free the pBt->pTmpSpace allocation |
| 1498 | */ |
| 1499 | static void freeTempSpace(BtShared *pBt){ |
| 1500 | sqlite3PageFree( pBt->pTmpSpace); |
| 1501 | pBt->pTmpSpace = 0; |
| 1502 | } |
| 1503 | |
| 1504 | /* |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1505 | ** Close an open database and invalidate all cursors. |
| 1506 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1507 | int sqlite3BtreeClose(Btree *p){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1508 | BtShared *pBt = p->pBt; |
| 1509 | BtCursor *pCur; |
| 1510 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1511 | /* Close all cursors opened via this handle. */ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1512 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1513 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1514 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1515 | pCur = pBt->pCursor; |
| 1516 | while( pCur ){ |
| 1517 | BtCursor *pTmp = pCur; |
| 1518 | pCur = pCur->pNext; |
| 1519 | if( pTmp->pBtree==p ){ |
| 1520 | sqlite3BtreeCloseCursor(pTmp); |
| 1521 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1522 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1523 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 1524 | /* Rollback any active transaction and free the handle structure. |
| 1525 | ** The call to sqlite3BtreeRollback() drops any table-locks held by |
| 1526 | ** this handle. |
| 1527 | */ |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 1528 | sqlite3BtreeRollback(p); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1529 | sqlite3BtreeLeave(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1530 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1531 | /* If there are still other outstanding references to the shared-btree |
| 1532 | ** structure, return now. The remainder of this procedure cleans |
| 1533 | ** up the shared-btree. |
| 1534 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1535 | assert( p->wantToLock==0 && p->locked==0 ); |
| 1536 | if( !p->sharable || removeFromSharingList(pBt) ){ |
| 1537 | /* The pBt is no longer on the sharing list, so we can access |
| 1538 | ** it without having to hold the mutex. |
| 1539 | ** |
| 1540 | ** Clean out and delete the BtShared object. |
| 1541 | */ |
| 1542 | assert( !pBt->pCursor ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1543 | sqlite3PagerClose(pBt->pPager); |
| 1544 | if( pBt->xFreeSchema && pBt->pSchema ){ |
| 1545 | pBt->xFreeSchema(pBt->pSchema); |
| 1546 | } |
| 1547 | sqlite3_free(pBt->pSchema); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1548 | freeTempSpace(pBt); |
drh | 65bbf29 | 2008-06-19 01:03:17 +0000 | [diff] [blame] | 1549 | sqlite3_free(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1552 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | cab5ed7 | 2007-08-22 11:41:18 +0000 | [diff] [blame] | 1553 | assert( p->wantToLock==0 ); |
| 1554 | assert( p->locked==0 ); |
| 1555 | if( p->pPrev ) p->pPrev->pNext = p->pNext; |
| 1556 | if( p->pNext ) p->pNext->pPrev = p->pPrev; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1557 | #endif |
| 1558 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1559 | sqlite3_free(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1560 | return SQLITE_OK; |
| 1561 | } |
| 1562 | |
| 1563 | /* |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1564 | ** Change the limit on the number of pages allowed in the cache. |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 1565 | ** |
| 1566 | ** The maximum number of cache pages is set to the absolute |
| 1567 | ** value of mxPage. If mxPage is negative, the pager will |
| 1568 | ** operate asynchronously - it will not stop to do fsync()s |
| 1569 | ** to insure data is written to the disk surface before |
| 1570 | ** continuing. Transactions still work if synchronous is off, |
| 1571 | ** and the database cannot be corrupted if this program |
| 1572 | ** crashes. But if the operating system crashes or there is |
| 1573 | ** an abrupt power failure when synchronous is off, the database |
| 1574 | ** could be left in an inconsistent and unrecoverable state. |
| 1575 | ** Synchronous is on by default so database corruption is not |
| 1576 | ** normally a worry. |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1577 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1578 | int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){ |
| 1579 | BtShared *pBt = p->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1580 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1581 | sqlite3BtreeEnter(p); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1582 | sqlite3PagerSetCachesize(pBt->pPager, mxPage); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1583 | sqlite3BtreeLeave(p); |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1584 | return SQLITE_OK; |
| 1585 | } |
| 1586 | |
| 1587 | /* |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1588 | ** Change the way data is synced to disk in order to increase or decrease |
| 1589 | ** how well the database resists damage due to OS crashes and power |
| 1590 | ** failures. Level 1 is the same as asynchronous (no syncs() occur and |
| 1591 | ** there is a high probability of damage) Level 2 is the default. There |
| 1592 | ** is a very low but non-zero probability of damage. Level 3 reduces the |
| 1593 | ** probability of damage to near zero but with a write performance reduction. |
| 1594 | */ |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1595 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
drh | ac530b1 | 2006-02-11 01:25:50 +0000 | [diff] [blame] | 1596 | int sqlite3BtreeSetSafetyLevel(Btree *p, int level, int fullSync){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1597 | BtShared *pBt = p->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1598 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1599 | sqlite3BtreeEnter(p); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1600 | sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1601 | sqlite3BtreeLeave(p); |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1602 | return SQLITE_OK; |
| 1603 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1604 | #endif |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1605 | |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1606 | /* |
| 1607 | ** Return TRUE if the given btree is set to safety level 1. In other |
| 1608 | ** words, return TRUE if no sync() occurs on the disk files. |
| 1609 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1610 | int sqlite3BtreeSyncDisabled(Btree *p){ |
| 1611 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1612 | int rc; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1613 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1614 | sqlite3BtreeEnter(p); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 1615 | assert( pBt && pBt->pPager ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1616 | rc = sqlite3PagerNosync(pBt->pPager); |
| 1617 | sqlite3BtreeLeave(p); |
| 1618 | return rc; |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1621 | #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1622 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1623 | ** Change the default pages size and the number of reserved bytes per page. |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 1624 | ** |
| 1625 | ** The page size must be a power of 2 between 512 and 65536. If the page |
| 1626 | ** size supplied does not meet this constraint then the page size is not |
| 1627 | ** changed. |
| 1628 | ** |
| 1629 | ** Page sizes are constrained to be a power of two so that the region |
| 1630 | ** of the database file used for locking (beginning at PENDING_BYTE, |
| 1631 | ** the first byte past the 1GB boundary, 0x40000000) needs to occur |
| 1632 | ** at the beginning of a page. |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 1633 | ** |
| 1634 | ** If parameter nReserve is less than zero, then the number of reserved |
| 1635 | ** bytes per page is left unchanged. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1636 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1637 | int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1638 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1639 | BtShared *pBt = p->pBt; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1640 | assert( nReserve>=-1 && nReserve<=255 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1641 | sqlite3BtreeEnter(p); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1642 | if( pBt->pageSizeFixed ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1643 | sqlite3BtreeLeave(p); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1644 | return SQLITE_READONLY; |
| 1645 | } |
| 1646 | if( nReserve<0 ){ |
| 1647 | nReserve = pBt->pageSize - pBt->usableSize; |
| 1648 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1649 | assert( nReserve>=0 && nReserve<=255 ); |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 1650 | if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE && |
| 1651 | ((pageSize-1)&pageSize)==0 ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1652 | assert( (pageSize & 7)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1653 | assert( !pBt->pPage1 && !pBt->pCursor ); |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 1654 | pBt->pageSize = (u16)pageSize; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1655 | freeTempSpace(pBt); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1656 | rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1657 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1658 | pBt->usableSize = pBt->pageSize - (u16)nReserve; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1659 | sqlite3BtreeLeave(p); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1660 | return rc; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | /* |
| 1664 | ** Return the currently defined page size |
| 1665 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1666 | int sqlite3BtreeGetPageSize(Btree *p){ |
| 1667 | return p->pBt->pageSize; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1668 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1669 | int sqlite3BtreeGetReserve(Btree *p){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1670 | int n; |
| 1671 | sqlite3BtreeEnter(p); |
| 1672 | n = p->pBt->pageSize - p->pBt->usableSize; |
| 1673 | sqlite3BtreeLeave(p); |
| 1674 | return n; |
drh | 2011d5f | 2004-07-22 02:40:37 +0000 | [diff] [blame] | 1675 | } |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 1676 | |
| 1677 | /* |
| 1678 | ** Set the maximum page count for a database if mxPage is positive. |
| 1679 | ** No changes are made if mxPage is 0 or negative. |
| 1680 | ** Regardless of the value of mxPage, return the maximum page count. |
| 1681 | */ |
| 1682 | int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1683 | int n; |
| 1684 | sqlite3BtreeEnter(p); |
| 1685 | n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage); |
| 1686 | sqlite3BtreeLeave(p); |
| 1687 | return n; |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 1688 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1689 | #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1690 | |
| 1691 | /* |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1692 | ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum' |
| 1693 | ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it |
| 1694 | ** is disabled. The default value for the auto-vacuum property is |
| 1695 | ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro. |
| 1696 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1697 | int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1698 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1699 | return SQLITE_READONLY; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1700 | #else |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1701 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1702 | int rc = SQLITE_OK; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1703 | u8 av = autoVacuum ?1:0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1704 | |
| 1705 | sqlite3BtreeEnter(p); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1706 | if( pBt->pageSizeFixed && av!=pBt->autoVacuum ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1707 | rc = SQLITE_READONLY; |
| 1708 | }else{ |
| 1709 | pBt->autoVacuum = av; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1710 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1711 | sqlite3BtreeLeave(p); |
| 1712 | return rc; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1713 | #endif |
| 1714 | } |
| 1715 | |
| 1716 | /* |
| 1717 | ** Return the value of the 'auto-vacuum' property. If auto-vacuum is |
| 1718 | ** enabled 1 is returned. Otherwise 0. |
| 1719 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1720 | int sqlite3BtreeGetAutoVacuum(Btree *p){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1721 | #ifdef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1722 | return BTREE_AUTOVACUUM_NONE; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1723 | #else |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1724 | int rc; |
| 1725 | sqlite3BtreeEnter(p); |
| 1726 | rc = ( |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1727 | (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE: |
| 1728 | (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL: |
| 1729 | BTREE_AUTOVACUUM_INCR |
| 1730 | ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1731 | sqlite3BtreeLeave(p); |
| 1732 | return rc; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1733 | #endif |
| 1734 | } |
| 1735 | |
| 1736 | |
| 1737 | /* |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1738 | ** Get a reference to pPage1 of the database file. This will |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1739 | ** also acquire a readlock on that file. |
| 1740 | ** |
| 1741 | ** SQLITE_OK is returned on success. If the file is not a |
| 1742 | ** well-formed database file, then SQLITE_CORRUPT is returned. |
| 1743 | ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 1744 | ** is returned if we run out of memory. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1745 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1746 | static int lockBtree(BtShared *pBt){ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1747 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1748 | MemPage *pPage1; |
danielk1977 | 93f7af9 | 2008-05-09 16:57:50 +0000 | [diff] [blame] | 1749 | int nPage; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1750 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1751 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1752 | if( pBt->pPage1 ) return SQLITE_OK; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1753 | rc = sqlite3BtreeGetPage(pBt, 1, &pPage1, 0); |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1754 | if( rc!=SQLITE_OK ) return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1755 | |
| 1756 | /* Do some checking to help insure the file we opened really is |
| 1757 | ** a valid database file. |
| 1758 | */ |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 1759 | rc = sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 1760 | if( rc!=SQLITE_OK ){ |
danielk1977 | 93f7af9 | 2008-05-09 16:57:50 +0000 | [diff] [blame] | 1761 | goto page1_init_failed; |
| 1762 | }else if( nPage>0 ){ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1763 | int pageSize; |
| 1764 | int usableSize; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1765 | u8 *page1 = pPage1->aData; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 1766 | rc = SQLITE_NOTADB; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1767 | if( memcmp(page1, zMagicHeader, 16)!=0 ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1768 | goto page1_init_failed; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1769 | } |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 1770 | if( page1[18]>1 ){ |
| 1771 | pBt->readOnly = 1; |
| 1772 | } |
| 1773 | if( page1[19]>1 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1774 | goto page1_init_failed; |
| 1775 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1776 | |
| 1777 | /* The maximum embedded fraction must be exactly 25%. And the minimum |
| 1778 | ** embedded fraction must be 12.5% for both leaf-data and non-leaf-data. |
| 1779 | ** The original design allowed these amounts to vary, but as of |
| 1780 | ** version 3.6.0, we require them to be fixed. |
| 1781 | */ |
| 1782 | if( memcmp(&page1[21], "\100\040\040",3)!=0 ){ |
| 1783 | goto page1_init_failed; |
| 1784 | } |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1785 | pageSize = get2byte(&page1[16]); |
drh | 7dc385e | 2007-09-06 23:39:36 +0000 | [diff] [blame] | 1786 | if( ((pageSize-1)&pageSize)!=0 || pageSize<512 || |
| 1787 | (SQLITE_MAX_PAGE_SIZE<32768 && pageSize>SQLITE_MAX_PAGE_SIZE) |
| 1788 | ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1789 | goto page1_init_failed; |
| 1790 | } |
| 1791 | assert( (pageSize & 7)==0 ); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1792 | usableSize = pageSize - page1[20]; |
| 1793 | if( pageSize!=pBt->pageSize ){ |
| 1794 | /* After reading the first page of the database assuming a page size |
| 1795 | ** of BtShared.pageSize, we have discovered that the page-size is |
| 1796 | ** actually pageSize. Unlock the database, leave pBt->pPage1 at |
| 1797 | ** zero and return SQLITE_OK. The caller will call this function |
| 1798 | ** again with the correct page-size. |
| 1799 | */ |
| 1800 | releasePage(pPage1); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1801 | pBt->usableSize = (u16)usableSize; |
| 1802 | pBt->pageSize = (u16)pageSize; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1803 | freeTempSpace(pBt); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1804 | sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
| 1805 | return SQLITE_OK; |
| 1806 | } |
| 1807 | if( usableSize<500 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1808 | goto page1_init_failed; |
| 1809 | } |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 1810 | pBt->pageSize = (u16)pageSize; |
| 1811 | pBt->usableSize = (u16)usableSize; |
drh | 057cd3a | 2005-02-15 16:23:02 +0000 | [diff] [blame] | 1812 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1813 | pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0); |
danielk1977 | 27b1f95 | 2007-06-25 08:16:58 +0000 | [diff] [blame] | 1814 | pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0); |
drh | 057cd3a | 2005-02-15 16:23:02 +0000 | [diff] [blame] | 1815 | #endif |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1816 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1817 | |
| 1818 | /* maxLocal is the maximum amount of payload to store locally for |
| 1819 | ** a cell. Make sure it is small enough so that at least minFanout |
| 1820 | ** cells can will fit on one page. We assume a 10-byte page header. |
| 1821 | ** Besides the payload, the cell must store: |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1822 | ** 2-byte pointer to the cell |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1823 | ** 4-byte child pointer |
| 1824 | ** 9-byte nKey value |
| 1825 | ** 4-byte nData value |
| 1826 | ** 4-byte overflow page pointer |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1827 | ** So a cell consists of a 2-byte poiner, a header which is as much as |
| 1828 | ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow |
| 1829 | ** page pointer. |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1830 | */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1831 | pBt->maxLocal = (pBt->usableSize-12)*64/255 - 23; |
| 1832 | pBt->minLocal = (pBt->usableSize-12)*32/255 - 23; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1833 | pBt->maxLeaf = pBt->usableSize - 35; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1834 | pBt->minLeaf = (pBt->usableSize-12)*32/255 - 23; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1835 | assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1836 | pBt->pPage1 = pPage1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1837 | return SQLITE_OK; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1838 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1839 | page1_init_failed: |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1840 | releasePage(pPage1); |
| 1841 | pBt->pPage1 = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1842 | return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1843 | } |
| 1844 | |
| 1845 | /* |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1846 | ** This routine works like lockBtree() except that it also invokes the |
| 1847 | ** busy callback if there is lock contention. |
| 1848 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1849 | static int lockBtreeWithRetry(Btree *pRef){ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1850 | int rc = SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1851 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1852 | assert( sqlite3BtreeHoldsMutex(pRef) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1853 | if( pRef->inTrans==TRANS_NONE ){ |
| 1854 | u8 inTransaction = pRef->pBt->inTransaction; |
| 1855 | btreeIntegrity(pRef); |
| 1856 | rc = sqlite3BtreeBeginTrans(pRef, 0); |
| 1857 | pRef->pBt->inTransaction = inTransaction; |
| 1858 | pRef->inTrans = TRANS_NONE; |
| 1859 | if( rc==SQLITE_OK ){ |
| 1860 | pRef->pBt->nTransaction--; |
| 1861 | } |
| 1862 | btreeIntegrity(pRef); |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1863 | } |
| 1864 | return rc; |
| 1865 | } |
| 1866 | |
| 1867 | |
| 1868 | /* |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1869 | ** If there are no outstanding cursors and we are not in the middle |
| 1870 | ** of a transaction but there is a read lock on the database, then |
| 1871 | ** this routine unrefs the first page of the database file which |
| 1872 | ** has the effect of releasing the read lock. |
| 1873 | ** |
| 1874 | ** If there are any outstanding cursors, this routine is a no-op. |
| 1875 | ** |
| 1876 | ** If there is a transaction in progress, this routine is a no-op. |
| 1877 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1878 | static void unlockBtreeIfUnused(BtShared *pBt){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1879 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1880 | if( pBt->inTransaction==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1881 | if( sqlite3PagerRefcount(pBt->pPager)>=1 ){ |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 1882 | assert( pBt->pPage1->aData ); |
| 1883 | #if 0 |
drh | 24c9a2e | 2007-01-05 02:00:47 +0000 | [diff] [blame] | 1884 | if( pBt->pPage1->aData==0 ){ |
| 1885 | MemPage *pPage = pBt->pPage1; |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1886 | pPage->aData = sqlite3PagerGetData(pPage->pDbPage); |
drh | 24c9a2e | 2007-01-05 02:00:47 +0000 | [diff] [blame] | 1887 | pPage->pBt = pBt; |
| 1888 | pPage->pgno = 1; |
| 1889 | } |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 1890 | #endif |
drh | 24c9a2e | 2007-01-05 02:00:47 +0000 | [diff] [blame] | 1891 | releasePage(pBt->pPage1); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 1892 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1893 | pBt->pPage1 = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1894 | pBt->inStmt = 0; |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1895 | } |
| 1896 | } |
| 1897 | |
| 1898 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1899 | ** Create a new database by initializing the first page of the |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1900 | ** file. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1901 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1902 | static int newDatabase(BtShared *pBt){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1903 | MemPage *pP1; |
| 1904 | unsigned char *data; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1905 | int rc; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 1906 | int nPage; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1907 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1908 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 1909 | rc = sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 1910 | if( rc!=SQLITE_OK || nPage>0 ){ |
| 1911 | return rc; |
| 1912 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1913 | pP1 = pBt->pPage1; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1914 | assert( pP1!=0 ); |
| 1915 | data = pP1->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1916 | rc = sqlite3PagerWrite(pP1->pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1917 | if( rc ) return rc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1918 | memcpy(data, zMagicHeader, sizeof(zMagicHeader)); |
| 1919 | assert( sizeof(zMagicHeader)==16 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1920 | put2byte(&data[16], pBt->pageSize); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1921 | data[18] = 1; |
| 1922 | data[19] = 1; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1923 | assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize); |
| 1924 | data[20] = (u8)(pBt->pageSize - pBt->usableSize); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1925 | data[21] = 64; |
| 1926 | data[22] = 32; |
| 1927 | data[23] = 32; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1928 | memset(&data[24], 0, 100-24); |
drh | e6c4381 | 2004-05-14 12:17:46 +0000 | [diff] [blame] | 1929 | zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA ); |
drh | f2a611c | 2004-09-05 00:33:43 +0000 | [diff] [blame] | 1930 | pBt->pageSizeFixed = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1931 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1932 | assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 ); |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 1933 | assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 ); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1934 | put4byte(&data[36 + 4*4], pBt->autoVacuum); |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 1935 | put4byte(&data[36 + 7*4], pBt->incrVacuum); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1936 | #endif |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1937 | return SQLITE_OK; |
| 1938 | } |
| 1939 | |
| 1940 | /* |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1941 | ** Attempt to start a new transaction. A write-transaction |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 1942 | ** is started if the second argument is nonzero, otherwise a read- |
| 1943 | ** transaction. If the second argument is 2 or more and exclusive |
| 1944 | ** transaction is started, meaning that no other process is allowed |
| 1945 | ** to access the database. A preexisting transaction may not be |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1946 | ** upgraded to exclusive by calling this routine a second time - the |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 1947 | ** exclusivity flag only works for a new transaction. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1948 | ** |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1949 | ** A write-transaction must be started before attempting any |
| 1950 | ** changes to the database. None of the following routines |
| 1951 | ** will work unless a transaction is started first: |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1952 | ** |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1953 | ** sqlite3BtreeCreateTable() |
| 1954 | ** sqlite3BtreeCreateIndex() |
| 1955 | ** sqlite3BtreeClearTable() |
| 1956 | ** sqlite3BtreeDropTable() |
| 1957 | ** sqlite3BtreeInsert() |
| 1958 | ** sqlite3BtreeDelete() |
| 1959 | ** sqlite3BtreeUpdateMeta() |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1960 | ** |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1961 | ** If an initial attempt to acquire the lock fails because of lock contention |
| 1962 | ** and the database was previously unlocked, then invoke the busy handler |
| 1963 | ** if there is one. But if there was previously a read-lock, do not |
| 1964 | ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is |
| 1965 | ** returned when there is already a read-lock in order to avoid a deadlock. |
| 1966 | ** |
| 1967 | ** Suppose there are two processes A and B. A has a read lock and B has |
| 1968 | ** a reserved lock. B tries to promote to exclusive but is blocked because |
| 1969 | ** of A's read lock. A tries to promote to reserved but is blocked by B. |
| 1970 | ** One or the other of the two processes must give way or there can be |
| 1971 | ** no progress. By returning SQLITE_BUSY and not invoking the busy callback |
| 1972 | ** when A already has a read lock, we encourage A to give up and let B |
| 1973 | ** proceed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1974 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1975 | int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ |
| 1976 | BtShared *pBt = p->pBt; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1977 | int rc = SQLITE_OK; |
| 1978 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1979 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1980 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1981 | btreeIntegrity(p); |
| 1982 | |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1983 | /* If the btree is already in a write-transaction, or it |
| 1984 | ** is already in a read-transaction and a read-transaction |
| 1985 | ** is requested, this is a no-op. |
| 1986 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1987 | if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1988 | goto trans_begun; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1989 | } |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1990 | |
| 1991 | /* Write transactions are not possible on a read-only database */ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1992 | if( pBt->readOnly && wrflag ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1993 | rc = SQLITE_READONLY; |
| 1994 | goto trans_begun; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1997 | /* If another database handle has already opened a write transaction |
| 1998 | ** on this shared-btree structure and a second write transaction is |
| 1999 | ** requested, return SQLITE_BUSY. |
| 2000 | */ |
| 2001 | if( pBt->inTransaction==TRANS_WRITE && wrflag ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2002 | rc = SQLITE_BUSY; |
| 2003 | goto trans_begun; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2004 | } |
| 2005 | |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 2006 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 2007 | if( wrflag>1 ){ |
| 2008 | BtLock *pIter; |
| 2009 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 2010 | if( pIter->pBtree!=p ){ |
| 2011 | rc = SQLITE_BUSY; |
| 2012 | goto trans_begun; |
| 2013 | } |
| 2014 | } |
| 2015 | } |
| 2016 | #endif |
| 2017 | |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2018 | do { |
drh | 8a9c17f | 2008-05-02 14:23:54 +0000 | [diff] [blame] | 2019 | if( pBt->pPage1==0 ){ |
| 2020 | do{ |
| 2021 | rc = lockBtree(pBt); |
| 2022 | }while( pBt->pPage1==0 && rc==SQLITE_OK ); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2023 | } |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 2024 | |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2025 | if( rc==SQLITE_OK && wrflag ){ |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 2026 | if( pBt->readOnly ){ |
| 2027 | rc = SQLITE_READONLY; |
| 2028 | }else{ |
| 2029 | rc = sqlite3PagerBegin(pBt->pPage1->pDbPage, wrflag>1); |
| 2030 | if( rc==SQLITE_OK ){ |
| 2031 | rc = newDatabase(pBt); |
| 2032 | } |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2033 | } |
| 2034 | } |
| 2035 | |
| 2036 | if( rc==SQLITE_OK ){ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2037 | if( wrflag ) pBt->inStmt = 0; |
| 2038 | }else{ |
| 2039 | unlockBtreeIfUnused(pBt); |
| 2040 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2041 | }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE && |
danielk1977 | 1ceedd3 | 2008-11-19 10:22:33 +0000 | [diff] [blame] | 2042 | btreeInvokeBusyHandler(pBt) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2043 | |
| 2044 | if( rc==SQLITE_OK ){ |
| 2045 | if( p->inTrans==TRANS_NONE ){ |
| 2046 | pBt->nTransaction++; |
| 2047 | } |
| 2048 | p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ); |
| 2049 | if( p->inTrans>pBt->inTransaction ){ |
| 2050 | pBt->inTransaction = p->inTrans; |
| 2051 | } |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 2052 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 2053 | if( wrflag>1 ){ |
| 2054 | assert( !pBt->pExclusive ); |
| 2055 | pBt->pExclusive = p; |
| 2056 | } |
| 2057 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2058 | } |
| 2059 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2060 | |
| 2061 | trans_begun: |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame^] | 2062 | if( rc==SQLITE_OK && wrflag ){ |
| 2063 | rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint); |
| 2064 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2065 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2066 | sqlite3BtreeLeave(p); |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 2067 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2068 | } |
| 2069 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2070 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2071 | |
| 2072 | /* |
| 2073 | ** Set the pointer-map entries for all children of page pPage. Also, if |
| 2074 | ** pPage contains cells that point to overflow pages, set the pointer |
| 2075 | ** map entries for the overflow pages as well. |
| 2076 | */ |
| 2077 | static int setChildPtrmaps(MemPage *pPage){ |
| 2078 | int i; /* Counter variable */ |
| 2079 | int nCell; /* Number of cells in page pPage */ |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 2080 | int rc; /* Return code */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2081 | BtShared *pBt = pPage->pBt; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 2082 | u8 isInitOrig = pPage->isInit; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2083 | Pgno pgno = pPage->pgno; |
| 2084 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2085 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2086 | rc = sqlite3BtreeInitPage(pPage); |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 2087 | if( rc!=SQLITE_OK ){ |
| 2088 | goto set_child_ptrmaps_out; |
| 2089 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2090 | nCell = pPage->nCell; |
| 2091 | |
| 2092 | for(i=0; i<nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2093 | u8 *pCell = findCell(pPage, i); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2094 | |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 2095 | rc = ptrmapPutOvflPtr(pPage, pCell); |
| 2096 | if( rc!=SQLITE_OK ){ |
| 2097 | goto set_child_ptrmaps_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2098 | } |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 2099 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2100 | if( !pPage->leaf ){ |
| 2101 | Pgno childPgno = get4byte(pCell); |
| 2102 | rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno); |
danielk1977 | 00a696d | 2008-09-29 16:41:31 +0000 | [diff] [blame] | 2103 | if( rc!=SQLITE_OK ) goto set_child_ptrmaps_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2104 | } |
| 2105 | } |
| 2106 | |
| 2107 | if( !pPage->leaf ){ |
| 2108 | Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 2109 | rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno); |
| 2110 | } |
| 2111 | |
| 2112 | set_child_ptrmaps_out: |
| 2113 | pPage->isInit = isInitOrig; |
| 2114 | return rc; |
| 2115 | } |
| 2116 | |
| 2117 | /* |
| 2118 | ** Somewhere on pPage, which is guarenteed to be a btree page, not an overflow |
| 2119 | ** page, is a pointer to page iFrom. Modify this pointer so that it points to |
| 2120 | ** iTo. Parameter eType describes the type of pointer to be modified, as |
| 2121 | ** follows: |
| 2122 | ** |
| 2123 | ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child |
| 2124 | ** page of pPage. |
| 2125 | ** |
| 2126 | ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow |
| 2127 | ** page pointed to by one of the cells on pPage. |
| 2128 | ** |
| 2129 | ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next |
| 2130 | ** overflow page in the list. |
| 2131 | */ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2132 | static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2133 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 2134 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2135 | if( eType==PTRMAP_OVERFLOW2 ){ |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 2136 | /* 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] | 2137 | if( get4byte(pPage->aData)!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2138 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2139 | } |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 2140 | put4byte(pPage->aData, iTo); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2141 | }else{ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 2142 | u8 isInitOrig = pPage->isInit; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2143 | int i; |
| 2144 | int nCell; |
| 2145 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2146 | sqlite3BtreeInitPage(pPage); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2147 | nCell = pPage->nCell; |
| 2148 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2149 | for(i=0; i<nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2150 | u8 *pCell = findCell(pPage, i); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2151 | if( eType==PTRMAP_OVERFLOW1 ){ |
| 2152 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2153 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2154 | if( info.iOverflow ){ |
| 2155 | if( iFrom==get4byte(&pCell[info.iOverflow]) ){ |
| 2156 | put4byte(&pCell[info.iOverflow], iTo); |
| 2157 | break; |
| 2158 | } |
| 2159 | } |
| 2160 | }else{ |
| 2161 | if( get4byte(pCell)==iFrom ){ |
| 2162 | put4byte(pCell, iTo); |
| 2163 | break; |
| 2164 | } |
| 2165 | } |
| 2166 | } |
| 2167 | |
| 2168 | if( i==nCell ){ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2169 | if( eType!=PTRMAP_BTREE || |
| 2170 | get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2171 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2172 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2173 | put4byte(&pPage->aData[pPage->hdrOffset+8], iTo); |
| 2174 | } |
| 2175 | |
| 2176 | pPage->isInit = isInitOrig; |
| 2177 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2178 | return SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2179 | } |
| 2180 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2181 | |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2182 | /* |
| 2183 | ** Move the open database page pDbPage to location iFreePage in the |
| 2184 | ** database. The pDbPage reference remains valid. |
| 2185 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2186 | static int relocatePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2187 | BtShared *pBt, /* Btree */ |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2188 | MemPage *pDbPage, /* Open page to move */ |
| 2189 | u8 eType, /* Pointer map 'type' entry for pDbPage */ |
| 2190 | Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */ |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 2191 | Pgno iFreePage, /* The location to move pDbPage to */ |
| 2192 | int isCommit |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2193 | ){ |
| 2194 | MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */ |
| 2195 | Pgno iDbPage = pDbPage->pgno; |
| 2196 | Pager *pPager = pBt->pPager; |
| 2197 | int rc; |
| 2198 | |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2199 | assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || |
| 2200 | eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2201 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 2202 | assert( pDbPage->pBt==pBt ); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2203 | |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 2204 | /* Move page iDbPage from its current location to page number iFreePage */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2205 | TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", |
| 2206 | iDbPage, iFreePage, iPtrPage, eType)); |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 2207 | rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2208 | if( rc!=SQLITE_OK ){ |
| 2209 | return rc; |
| 2210 | } |
| 2211 | pDbPage->pgno = iFreePage; |
| 2212 | |
| 2213 | /* If pDbPage was a btree-page, then it may have child pages and/or cells |
| 2214 | ** that point to overflow pages. The pointer map entries for all these |
| 2215 | ** pages need to be changed. |
| 2216 | ** |
| 2217 | ** If pDbPage is an overflow page, then the first 4 bytes may store a |
| 2218 | ** pointer to a subsequent overflow page. If this is the case, then |
| 2219 | ** the pointer map needs to be updated for the subsequent overflow page. |
| 2220 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2221 | if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2222 | rc = setChildPtrmaps(pDbPage); |
| 2223 | if( rc!=SQLITE_OK ){ |
| 2224 | return rc; |
| 2225 | } |
| 2226 | }else{ |
| 2227 | Pgno nextOvfl = get4byte(pDbPage->aData); |
| 2228 | if( nextOvfl!=0 ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2229 | rc = ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage); |
| 2230 | if( rc!=SQLITE_OK ){ |
| 2231 | return rc; |
| 2232 | } |
| 2233 | } |
| 2234 | } |
| 2235 | |
| 2236 | /* Fix the database pointer on page iPtrPage that pointed at iDbPage so |
| 2237 | ** that it points at iFreePage. Also fix the pointer map entry for |
| 2238 | ** iPtrPage. |
| 2239 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2240 | if( eType!=PTRMAP_ROOTPAGE ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2241 | rc = sqlite3BtreeGetPage(pBt, iPtrPage, &pPtrPage, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2242 | if( rc!=SQLITE_OK ){ |
| 2243 | return rc; |
| 2244 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2245 | rc = sqlite3PagerWrite(pPtrPage->pDbPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2246 | if( rc!=SQLITE_OK ){ |
| 2247 | releasePage(pPtrPage); |
| 2248 | return rc; |
| 2249 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2250 | rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2251 | releasePage(pPtrPage); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2252 | if( rc==SQLITE_OK ){ |
| 2253 | rc = ptrmapPut(pBt, iFreePage, eType, iPtrPage); |
| 2254 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2255 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2256 | return rc; |
| 2257 | } |
| 2258 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2259 | /* Forward declaration required by incrVacuumStep(). */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2260 | static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2261 | |
| 2262 | /* |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2263 | ** Perform a single step of an incremental-vacuum. If successful, |
| 2264 | ** return SQLITE_OK. If there is no work to do (and therefore no |
| 2265 | ** point in calling this function again), return SQLITE_DONE. |
| 2266 | ** |
| 2267 | ** More specificly, this function attempts to re-organize the |
| 2268 | ** database so that the last page of the file currently in use |
| 2269 | ** is no longer in use. |
| 2270 | ** |
| 2271 | ** If the nFin parameter is non-zero, the implementation assumes |
| 2272 | ** that the caller will keep calling incrVacuumStep() until |
| 2273 | ** it returns SQLITE_DONE or an error, and that nFin is the |
| 2274 | ** number of pages the database file will contain after this |
| 2275 | ** process is complete. |
| 2276 | */ |
| 2277 | static int incrVacuumStep(BtShared *pBt, Pgno nFin){ |
| 2278 | Pgno iLastPg; /* Last page in the database */ |
| 2279 | Pgno nFreeList; /* Number of pages still on the free-list */ |
| 2280 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2281 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2282 | iLastPg = pBt->nTrunc; |
| 2283 | if( iLastPg==0 ){ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 2284 | iLastPg = pagerPagecount(pBt); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2285 | } |
| 2286 | |
| 2287 | if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){ |
| 2288 | int rc; |
| 2289 | u8 eType; |
| 2290 | Pgno iPtrPage; |
| 2291 | |
| 2292 | nFreeList = get4byte(&pBt->pPage1->aData[36]); |
| 2293 | if( nFreeList==0 || nFin==iLastPg ){ |
| 2294 | return SQLITE_DONE; |
| 2295 | } |
| 2296 | |
| 2297 | rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage); |
| 2298 | if( rc!=SQLITE_OK ){ |
| 2299 | return rc; |
| 2300 | } |
| 2301 | if( eType==PTRMAP_ROOTPAGE ){ |
| 2302 | return SQLITE_CORRUPT_BKPT; |
| 2303 | } |
| 2304 | |
| 2305 | if( eType==PTRMAP_FREEPAGE ){ |
| 2306 | if( nFin==0 ){ |
| 2307 | /* Remove the page from the files free-list. This is not required |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 2308 | ** if nFin is non-zero. In that case, the free-list will be |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2309 | ** truncated to zero after this function returns, so it doesn't |
| 2310 | ** matter if it still contains some garbage entries. |
| 2311 | */ |
| 2312 | Pgno iFreePg; |
| 2313 | MemPage *pFreePg; |
| 2314 | rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1); |
| 2315 | if( rc!=SQLITE_OK ){ |
| 2316 | return rc; |
| 2317 | } |
| 2318 | assert( iFreePg==iLastPg ); |
| 2319 | releasePage(pFreePg); |
| 2320 | } |
| 2321 | } else { |
| 2322 | Pgno iFreePg; /* Index of free page to move pLastPg to */ |
| 2323 | MemPage *pLastPg; |
| 2324 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2325 | rc = sqlite3BtreeGetPage(pBt, iLastPg, &pLastPg, 0); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2326 | if( rc!=SQLITE_OK ){ |
| 2327 | return rc; |
| 2328 | } |
| 2329 | |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 2330 | /* If nFin is zero, this loop runs exactly once and page pLastPg |
| 2331 | ** is swapped with the first free page pulled off the free list. |
| 2332 | ** |
| 2333 | ** On the other hand, if nFin is greater than zero, then keep |
| 2334 | ** looping until a free-page located within the first nFin pages |
| 2335 | ** of the file is found. |
| 2336 | */ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2337 | do { |
| 2338 | MemPage *pFreePg; |
| 2339 | rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0); |
| 2340 | if( rc!=SQLITE_OK ){ |
| 2341 | releasePage(pLastPg); |
| 2342 | return rc; |
| 2343 | } |
| 2344 | releasePage(pFreePg); |
| 2345 | }while( nFin!=0 && iFreePg>nFin ); |
| 2346 | assert( iFreePg<iLastPg ); |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 2347 | |
| 2348 | rc = sqlite3PagerWrite(pLastPg->pDbPage); |
danielk1977 | 662278e | 2007-11-05 15:30:12 +0000 | [diff] [blame] | 2349 | if( rc==SQLITE_OK ){ |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 2350 | rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, nFin!=0); |
danielk1977 | 662278e | 2007-11-05 15:30:12 +0000 | [diff] [blame] | 2351 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2352 | releasePage(pLastPg); |
| 2353 | if( rc!=SQLITE_OK ){ |
| 2354 | return rc; |
danielk1977 | 662278e | 2007-11-05 15:30:12 +0000 | [diff] [blame] | 2355 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2356 | } |
| 2357 | } |
| 2358 | |
| 2359 | pBt->nTrunc = iLastPg - 1; |
| 2360 | while( pBt->nTrunc==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, pBt->nTrunc) ){ |
| 2361 | pBt->nTrunc--; |
| 2362 | } |
| 2363 | return SQLITE_OK; |
| 2364 | } |
| 2365 | |
| 2366 | /* |
| 2367 | ** A write-transaction must be opened before calling this function. |
| 2368 | ** It performs a single unit of work towards an incremental vacuum. |
| 2369 | ** |
| 2370 | ** If the incremental vacuum is finished after this function has run, |
| 2371 | ** SQLITE_DONE is returned. If it is not finished, but no error occured, |
| 2372 | ** SQLITE_OK is returned. Otherwise an SQLite error code. |
| 2373 | */ |
| 2374 | int sqlite3BtreeIncrVacuum(Btree *p){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2375 | int rc; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2376 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2377 | |
| 2378 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2379 | pBt->db = p->db; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2380 | assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE ); |
| 2381 | if( !pBt->autoVacuum ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2382 | rc = SQLITE_DONE; |
| 2383 | }else{ |
| 2384 | invalidateAllOverflowCache(pBt); |
| 2385 | rc = incrVacuumStep(pBt, 0); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2386 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2387 | sqlite3BtreeLeave(p); |
| 2388 | return rc; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2389 | } |
| 2390 | |
| 2391 | /* |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2392 | ** This routine is called prior to sqlite3PagerCommit when a transaction |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2393 | ** is commited for an auto-vacuum database. |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 2394 | ** |
| 2395 | ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages |
| 2396 | ** the database file should be truncated to during the commit process. |
| 2397 | ** i.e. the database has been reorganized so that only the first *pnTrunc |
| 2398 | ** pages are in use. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2399 | */ |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 2400 | static int autoVacuumCommit(BtShared *pBt, Pgno *pnTrunc){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2401 | int rc = SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2402 | Pager *pPager = pBt->pPager; |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 2403 | VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2404 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2405 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 2406 | invalidateAllOverflowCache(pBt); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2407 | assert(pBt->autoVacuum); |
| 2408 | if( !pBt->incrVacuum ){ |
| 2409 | Pgno nFin = 0; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2410 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2411 | if( pBt->nTrunc==0 ){ |
| 2412 | Pgno nFree; |
| 2413 | Pgno nPtrmap; |
| 2414 | const int pgsz = pBt->pageSize; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 2415 | Pgno nOrig = pagerPagecount(pBt); |
danielk1977 | e5321f0 | 2007-04-27 07:05:44 +0000 | [diff] [blame] | 2416 | |
| 2417 | if( PTRMAP_ISPAGE(pBt, nOrig) ){ |
| 2418 | return SQLITE_CORRUPT_BKPT; |
| 2419 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2420 | if( nOrig==PENDING_BYTE_PAGE(pBt) ){ |
| 2421 | nOrig--; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2422 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2423 | nFree = get4byte(&pBt->pPage1->aData[36]); |
| 2424 | nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+pgsz/5)/(pgsz/5); |
| 2425 | nFin = nOrig - nFree - nPtrmap; |
| 2426 | if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<=PENDING_BYTE_PAGE(pBt) ){ |
| 2427 | nFin--; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 2428 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2429 | while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){ |
| 2430 | nFin--; |
| 2431 | } |
| 2432 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2433 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2434 | while( rc==SQLITE_OK ){ |
| 2435 | rc = incrVacuumStep(pBt, nFin); |
| 2436 | } |
| 2437 | if( rc==SQLITE_DONE ){ |
| 2438 | assert(nFin==0 || pBt->nTrunc==0 || nFin<=pBt->nTrunc); |
| 2439 | rc = SQLITE_OK; |
danielk1977 | 0ba32df | 2008-05-07 07:13:16 +0000 | [diff] [blame] | 2440 | if( pBt->nTrunc && nFin ){ |
drh | 67f80b6 | 2007-07-23 19:26:17 +0000 | [diff] [blame] | 2441 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2442 | put4byte(&pBt->pPage1->aData[32], 0); |
| 2443 | put4byte(&pBt->pPage1->aData[36], 0); |
| 2444 | pBt->nTrunc = nFin; |
| 2445 | } |
| 2446 | } |
| 2447 | if( rc!=SQLITE_OK ){ |
| 2448 | sqlite3PagerRollback(pPager); |
| 2449 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2450 | } |
| 2451 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2452 | if( rc==SQLITE_OK ){ |
| 2453 | *pnTrunc = pBt->nTrunc; |
| 2454 | pBt->nTrunc = 0; |
| 2455 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2456 | assert( nRef==sqlite3PagerRefcount(pPager) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2457 | return rc; |
| 2458 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2459 | |
shane | 831c329 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 2460 | #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2461 | |
| 2462 | /* |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2463 | ** This routine does the first phase of a two-phase commit. This routine |
| 2464 | ** causes a rollback journal to be created (if it does not already exist) |
| 2465 | ** and populated with enough information so that if a power loss occurs |
| 2466 | ** the database can be restored to its original state by playing back |
| 2467 | ** the journal. Then the contents of the journal are flushed out to |
| 2468 | ** the disk. After the journal is safely on oxide, the changes to the |
| 2469 | ** database are written into the database file and flushed to oxide. |
| 2470 | ** At the end of this call, the rollback journal still exists on the |
| 2471 | ** disk and we are still holding all locks, so the transaction has not |
| 2472 | ** committed. See sqlite3BtreeCommit() for the second phase of the |
| 2473 | ** commit process. |
| 2474 | ** |
| 2475 | ** This call is a no-op if no write-transaction is currently active on pBt. |
| 2476 | ** |
| 2477 | ** Otherwise, sync the database file for the btree pBt. zMaster points to |
| 2478 | ** the name of a master journal file that should be written into the |
| 2479 | ** individual journal file, or is NULL, indicating no master journal file |
| 2480 | ** (single database transaction). |
| 2481 | ** |
| 2482 | ** When this is called, the master journal should already have been |
| 2483 | ** created, populated with this journal pointer and synced to disk. |
| 2484 | ** |
| 2485 | ** Once this is routine has returned, the only thing required to commit |
| 2486 | ** the write-transaction for this database file is to delete the journal. |
| 2487 | */ |
| 2488 | int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){ |
| 2489 | int rc = SQLITE_OK; |
| 2490 | if( p->inTrans==TRANS_WRITE ){ |
| 2491 | BtShared *pBt = p->pBt; |
| 2492 | Pgno nTrunc = 0; |
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; |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2495 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2496 | if( pBt->autoVacuum ){ |
| 2497 | rc = autoVacuumCommit(pBt, &nTrunc); |
| 2498 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2499 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2500 | return rc; |
| 2501 | } |
| 2502 | } |
| 2503 | #endif |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 2504 | rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, nTrunc, 0); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2505 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2506 | } |
| 2507 | return rc; |
| 2508 | } |
| 2509 | |
| 2510 | /* |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2511 | ** Commit the transaction currently in progress. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2512 | ** |
drh | 6e34599 | 2007-03-30 11:12:08 +0000 | [diff] [blame] | 2513 | ** This routine implements the second phase of a 2-phase commit. The |
| 2514 | ** sqlite3BtreeSync() routine does the first phase and should be invoked |
| 2515 | ** prior to calling this routine. The sqlite3BtreeSync() routine did |
| 2516 | ** all the work of writing information out to disk and flushing the |
| 2517 | ** contents so that they are written onto the disk platter. All this |
| 2518 | ** routine has to do is delete or truncate the rollback journal |
| 2519 | ** (which causes the transaction to commit) and drop locks. |
| 2520 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2521 | ** This will release the write lock on the database file. If there |
| 2522 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2523 | */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2524 | int sqlite3BtreeCommitPhaseTwo(Btree *p){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2525 | BtShared *pBt = p->pBt; |
| 2526 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2527 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2528 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2529 | btreeIntegrity(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2530 | |
| 2531 | /* If the handle has a write-transaction open, commit the shared-btrees |
| 2532 | ** transaction and set the shared state to TRANS_READ. |
| 2533 | */ |
| 2534 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2535 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2536 | assert( pBt->inTransaction==TRANS_WRITE ); |
| 2537 | assert( pBt->nTransaction>0 ); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2538 | rc = sqlite3PagerCommitPhaseTwo(pBt->pPager); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2539 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2540 | sqlite3BtreeLeave(p); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2541 | return rc; |
| 2542 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2543 | pBt->inTransaction = TRANS_READ; |
| 2544 | pBt->inStmt = 0; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2545 | } |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2546 | unlockAllTables(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2547 | |
| 2548 | /* If the handle has any kind of transaction open, decrement the transaction |
| 2549 | ** count of the shared btree. If the transaction count reaches 0, set |
| 2550 | ** the shared state to TRANS_NONE. The unlockBtreeIfUnused() call below |
| 2551 | ** will unlock the pager. |
| 2552 | */ |
| 2553 | if( p->inTrans!=TRANS_NONE ){ |
| 2554 | pBt->nTransaction--; |
| 2555 | if( 0==pBt->nTransaction ){ |
| 2556 | pBt->inTransaction = TRANS_NONE; |
| 2557 | } |
| 2558 | } |
| 2559 | |
| 2560 | /* Set the handles current transaction state to TRANS_NONE and unlock |
| 2561 | ** the pager if this call closed the only read or write transaction. |
| 2562 | */ |
| 2563 | p->inTrans = TRANS_NONE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2564 | unlockBtreeIfUnused(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2565 | |
| 2566 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2567 | sqlite3BtreeLeave(p); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2568 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2569 | } |
| 2570 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2571 | /* |
| 2572 | ** Do both phases of a commit. |
| 2573 | */ |
| 2574 | int sqlite3BtreeCommit(Btree *p){ |
| 2575 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2576 | sqlite3BtreeEnter(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2577 | rc = sqlite3BtreeCommitPhaseOne(p, 0); |
| 2578 | if( rc==SQLITE_OK ){ |
| 2579 | rc = sqlite3BtreeCommitPhaseTwo(p); |
| 2580 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2581 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2582 | return rc; |
| 2583 | } |
| 2584 | |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2585 | #ifndef NDEBUG |
| 2586 | /* |
| 2587 | ** Return the number of write-cursors open on this handle. This is for use |
| 2588 | ** in assert() expressions, so it is only compiled if NDEBUG is not |
| 2589 | ** defined. |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2590 | ** |
| 2591 | ** For the purposes of this routine, a write-cursor is any cursor that |
| 2592 | ** is capable of writing to the databse. That means the cursor was |
| 2593 | ** originally opened for writing and the cursor has not be disabled |
| 2594 | ** by having its state changed to CURSOR_FAULT. |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2595 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2596 | static int countWriteCursors(BtShared *pBt){ |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2597 | BtCursor *pCur; |
| 2598 | int r = 0; |
| 2599 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2600 | if( pCur->wrFlag && pCur->eState!=CURSOR_FAULT ) r++; |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2601 | } |
| 2602 | return r; |
| 2603 | } |
| 2604 | #endif |
| 2605 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2606 | /* |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2607 | ** This routine sets the state to CURSOR_FAULT and the error |
| 2608 | ** code to errCode for every cursor on BtShared that pBtree |
| 2609 | ** references. |
| 2610 | ** |
| 2611 | ** Every cursor is tripped, including cursors that belong |
| 2612 | ** to other database connections that happen to be sharing |
| 2613 | ** the cache with pBtree. |
| 2614 | ** |
| 2615 | ** This routine gets called when a rollback occurs. |
| 2616 | ** All cursors using the same cache must be tripped |
| 2617 | ** to prevent them from trying to use the btree after |
| 2618 | ** the rollback. The rollback may have deleted tables |
| 2619 | ** or moved root pages, so it is not sufficient to |
| 2620 | ** save the state of the cursor. The cursor must be |
| 2621 | ** invalidated. |
| 2622 | */ |
| 2623 | void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){ |
| 2624 | BtCursor *p; |
| 2625 | sqlite3BtreeEnter(pBtree); |
| 2626 | for(p=pBtree->pBt->pCursor; p; p=p->pNext){ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 2627 | int i; |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 2628 | sqlite3BtreeClearCursor(p); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2629 | p->eState = CURSOR_FAULT; |
| 2630 | p->skip = errCode; |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 2631 | for(i=0; i<=p->iPage; i++){ |
| 2632 | releasePage(p->apPage[i]); |
| 2633 | p->apPage[i] = 0; |
| 2634 | } |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2635 | } |
| 2636 | sqlite3BtreeLeave(pBtree); |
| 2637 | } |
| 2638 | |
| 2639 | /* |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2640 | ** Rollback the transaction in progress. All cursors will be |
| 2641 | ** invalided by this operation. Any attempt to use a cursor |
| 2642 | ** that was open at the beginning of this operation will result |
| 2643 | ** in an error. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2644 | ** |
| 2645 | ** This will release the write lock on the database file. If there |
| 2646 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2647 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2648 | int sqlite3BtreeRollback(Btree *p){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2649 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2650 | BtShared *pBt = p->pBt; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2651 | MemPage *pPage1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2652 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2653 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2654 | pBt->db = p->db; |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2655 | rc = saveAllCursors(pBt, 0, 0); |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2656 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2657 | if( rc!=SQLITE_OK ){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2658 | /* This is a horrible situation. An IO or malloc() error occured whilst |
| 2659 | ** trying to save cursor positions. If this is an automatic rollback (as |
| 2660 | ** the result of a constraint, malloc() failure or IO error) then |
| 2661 | ** the cache may be internally inconsistent (not contain valid trees) so |
| 2662 | ** we cannot simply return the error to the caller. Instead, abort |
| 2663 | ** all queries that may be using any of the cursors that failed to save. |
| 2664 | */ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2665 | sqlite3BtreeTripAllCursors(p, rc); |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2666 | } |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2667 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2668 | btreeIntegrity(p); |
| 2669 | unlockAllTables(p); |
| 2670 | |
| 2671 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2672 | int rc2; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2673 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2674 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2675 | pBt->nTrunc = 0; |
| 2676 | #endif |
| 2677 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2678 | assert( TRANS_WRITE==pBt->inTransaction ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2679 | rc2 = sqlite3PagerRollback(pBt->pPager); |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2680 | if( rc2!=SQLITE_OK ){ |
| 2681 | rc = rc2; |
| 2682 | } |
| 2683 | |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2684 | /* The rollback may have destroyed the pPage1->aData value. So |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2685 | ** call sqlite3BtreeGetPage() on page 1 again to make |
| 2686 | ** sure pPage1->aData is set correctly. */ |
| 2687 | if( sqlite3BtreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2688 | releasePage(pPage1); |
| 2689 | } |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2690 | assert( countWriteCursors(pBt)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2691 | pBt->inTransaction = TRANS_READ; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2692 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2693 | |
| 2694 | if( p->inTrans!=TRANS_NONE ){ |
| 2695 | assert( pBt->nTransaction>0 ); |
| 2696 | pBt->nTransaction--; |
| 2697 | if( 0==pBt->nTransaction ){ |
| 2698 | pBt->inTransaction = TRANS_NONE; |
| 2699 | } |
| 2700 | } |
| 2701 | |
| 2702 | p->inTrans = TRANS_NONE; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2703 | pBt->inStmt = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2704 | unlockBtreeIfUnused(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2705 | |
| 2706 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2707 | sqlite3BtreeLeave(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2708 | return rc; |
| 2709 | } |
| 2710 | |
| 2711 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2712 | ** Start a statement subtransaction. The subtransaction can |
| 2713 | ** can be rolled back independently of the main transaction. |
| 2714 | ** You must start a transaction before starting a subtransaction. |
| 2715 | ** The subtransaction is ended automatically if the main transaction |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2716 | ** commits or rolls back. |
| 2717 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2718 | ** Only one subtransaction may be active at a time. It is an error to try |
| 2719 | ** to start a new subtransaction if another subtransaction is already active. |
| 2720 | ** |
| 2721 | ** Statement subtransactions are used around individual SQL statements |
| 2722 | ** that are contained within a BEGIN...COMMIT block. If a constraint |
| 2723 | ** error occurs within the statement, the effect of that one statement |
| 2724 | ** can be rolled back without having to rollback the entire transaction. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2725 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2726 | int sqlite3BtreeBeginStmt(Btree *p){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2727 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2728 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2729 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2730 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2731 | if( (p->inTrans!=TRANS_WRITE) || pBt->inStmt ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2732 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
| 2733 | }else{ |
| 2734 | assert( pBt->inTransaction==TRANS_WRITE ); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame^] | 2735 | if( pBt->readOnly ){ |
| 2736 | rc = SQLITE_OK; |
| 2737 | }else{ |
| 2738 | /* At the pager level, a statement transaction is a savepoint with |
| 2739 | ** an index greater than all savepoints created explicitly using |
| 2740 | ** SQL statements. It is illegal to open, release or rollback any |
| 2741 | ** such savepoints while the statement transaction savepoint is active. |
| 2742 | */ |
| 2743 | int iStmtpoint = p->db->nSavepoint + 1; |
| 2744 | rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStmtpoint); |
| 2745 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2746 | pBt->inStmt = 1; |
drh | 0d65dc0 | 2002-02-03 00:56:09 +0000 | [diff] [blame] | 2747 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2748 | sqlite3BtreeLeave(p); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2749 | return rc; |
| 2750 | } |
| 2751 | |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2752 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2753 | ** Commit the statment subtransaction currently in progress. If no |
| 2754 | ** subtransaction is active, this is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2755 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2756 | int sqlite3BtreeCommitStmt(Btree *p){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2757 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2758 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2759 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2760 | pBt->db = p->db; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2761 | if( pBt->inStmt && !pBt->readOnly ){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame^] | 2762 | int iStmtpoint = p->db->nSavepoint; |
| 2763 | rc = sqlite3PagerSavepoint(pBt->pPager, SAVEPOINT_RELEASE, iStmtpoint); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2764 | }else{ |
| 2765 | rc = SQLITE_OK; |
| 2766 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2767 | pBt->inStmt = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2768 | sqlite3BtreeLeave(p); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2769 | return rc; |
| 2770 | } |
| 2771 | |
| 2772 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2773 | ** Rollback the active statement subtransaction. If no subtransaction |
| 2774 | ** is active this routine is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2775 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2776 | ** All cursors will be invalidated by this operation. Any attempt |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2777 | ** to use a cursor that was open at the beginning of this operation |
| 2778 | ** will result in an error. |
| 2779 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2780 | int sqlite3BtreeRollbackStmt(Btree *p){ |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2781 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2782 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2783 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2784 | pBt->db = p->db; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2785 | if( pBt->inStmt && !pBt->readOnly ){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame^] | 2786 | int iStmtpoint = p->db->nSavepoint; |
| 2787 | rc = sqlite3PagerSavepoint(pBt->pPager, SAVEPOINT_ROLLBACK, iStmtpoint); |
| 2788 | if( rc==SQLITE_OK ){ |
| 2789 | rc = sqlite3PagerSavepoint(pBt->pPager, SAVEPOINT_RELEASE, iStmtpoint); |
| 2790 | } |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2791 | pBt->inStmt = 0; |
| 2792 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2793 | sqlite3BtreeLeave(p); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2794 | return rc; |
| 2795 | } |
| 2796 | |
| 2797 | /* |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame^] | 2798 | ** The second argument to this function, op, is always SAVEPOINT_ROLLBACK |
| 2799 | ** or SAVEPOINT_RELEASE. This function either releases or rolls back the |
| 2800 | ** savepoint identified by parameter iSavepoint, depending on the value of |
| 2801 | ** op. |
| 2802 | */ |
| 2803 | int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){ |
| 2804 | int rc = SQLITE_OK; |
| 2805 | if( p && p->inTrans==TRANS_WRITE ){ |
| 2806 | BtShared *pBt = p->pBt; |
| 2807 | assert( pBt->inStmt==0 ); |
| 2808 | assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK ); |
| 2809 | assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) ); |
| 2810 | sqlite3BtreeEnter(p); |
| 2811 | pBt->db = p->db; |
| 2812 | rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint); |
| 2813 | sqlite3BtreeLeave(p); |
| 2814 | } |
| 2815 | return rc; |
| 2816 | } |
| 2817 | |
| 2818 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2819 | ** Create a new cursor for the BTree whose root is on the page |
| 2820 | ** iTable. The act of acquiring a cursor gets a read lock on |
| 2821 | ** the database file. |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 2822 | ** |
| 2823 | ** If wrFlag==0, then the cursor can only be used for reading. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2824 | ** If wrFlag==1, then the cursor can be used for reading or for |
| 2825 | ** writing if other conditions for writing are also met. These |
| 2826 | ** are the conditions that must be met in order for writing to |
| 2827 | ** be allowed: |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2828 | ** |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2829 | ** 1: The cursor must have been opened with wrFlag==1 |
| 2830 | ** |
drh | fe5d71d | 2007-03-19 11:54:10 +0000 | [diff] [blame] | 2831 | ** 2: Other database connections that share the same pager cache |
| 2832 | ** but which are not in the READ_UNCOMMITTED state may not have |
| 2833 | ** cursors open with wrFlag==0 on the same table. Otherwise |
| 2834 | ** the changes made by this write cursor would be visible to |
| 2835 | ** the read cursors in the other database connection. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2836 | ** |
| 2837 | ** 3: The database must be writable (not on read-only media) |
| 2838 | ** |
| 2839 | ** 4: There must be an active transaction. |
| 2840 | ** |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2841 | ** No checking is done to make sure that page iTable really is the |
| 2842 | ** root page of a b-tree. If it is not, then the cursor acquired |
| 2843 | ** will not work correctly. |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2844 | ** |
| 2845 | ** It is assumed that the sqlite3BtreeCursorSize() bytes of memory |
| 2846 | ** pointed to by pCur have been zeroed by the caller. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2847 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2848 | static int btreeCursor( |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2849 | Btree *p, /* The btree */ |
| 2850 | int iTable, /* Root page of table to open */ |
| 2851 | int wrFlag, /* 1 to write. 0 read-only */ |
| 2852 | struct KeyInfo *pKeyInfo, /* First arg to comparison function */ |
| 2853 | BtCursor *pCur /* Space for new cursor */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2854 | ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2855 | int rc; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 2856 | Pgno nPage; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2857 | BtShared *pBt = p->pBt; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2858 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2859 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 2860 | assert( wrFlag==0 || wrFlag==1 ); |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2861 | if( wrFlag ){ |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2862 | if( pBt->readOnly ){ |
| 2863 | return SQLITE_READONLY; |
| 2864 | } |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 2865 | if( checkReadLocks(p, iTable, 0, 0) ){ |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2866 | return SQLITE_LOCKED; |
| 2867 | } |
drh | a0c9a11 | 2004-03-10 13:42:37 +0000 | [diff] [blame] | 2868 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2869 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2870 | if( pBt->pPage1==0 ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2871 | rc = lockBtreeWithRetry(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2872 | if( rc!=SQLITE_OK ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2873 | return rc; |
| 2874 | } |
drh | 1831f18 | 2007-04-24 17:35:59 +0000 | [diff] [blame] | 2875 | if( pBt->readOnly && wrFlag ){ |
| 2876 | return SQLITE_READONLY; |
| 2877 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2878 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2879 | pCur->pgnoRoot = (Pgno)iTable; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 2880 | rc = sqlite3PagerPagecount(pBt->pPager, (int *)&nPage); |
| 2881 | if( rc!=SQLITE_OK ){ |
| 2882 | return rc; |
| 2883 | } |
| 2884 | if( iTable==1 && nPage==0 ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2885 | rc = SQLITE_EMPTY; |
| 2886 | goto create_cursor_exception; |
| 2887 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2888 | rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2889 | if( rc!=SQLITE_OK ){ |
| 2890 | goto create_cursor_exception; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2891 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2892 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2893 | /* Now that no other errors can occur, finish filling in the BtCursor |
| 2894 | ** variables, link the cursor into the BtShared list and set *ppCur (the |
| 2895 | ** output argument to this function). |
| 2896 | */ |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2897 | pCur->pKeyInfo = pKeyInfo; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2898 | pCur->pBtree = p; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 2899 | pCur->pBt = pBt; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 2900 | pCur->wrFlag = (u8)wrFlag; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2901 | pCur->pNext = pBt->pCursor; |
| 2902 | if( pCur->pNext ){ |
| 2903 | pCur->pNext->pPrev = pCur; |
| 2904 | } |
| 2905 | pBt->pCursor = pCur; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2906 | pCur->eState = CURSOR_INVALID; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2907 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2908 | return SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2909 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2910 | create_cursor_exception: |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2911 | releasePage(pCur->apPage[0]); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2912 | unlockBtreeIfUnused(pBt); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2913 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2914 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2915 | int sqlite3BtreeCursor( |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2916 | Btree *p, /* The btree */ |
| 2917 | int iTable, /* Root page of table to open */ |
| 2918 | int wrFlag, /* 1 to write. 0 read-only */ |
| 2919 | struct KeyInfo *pKeyInfo, /* First arg to xCompare() */ |
| 2920 | BtCursor *pCur /* Write new cursor here */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2921 | ){ |
| 2922 | int rc; |
| 2923 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2924 | p->pBt->db = p->db; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2925 | rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2926 | sqlite3BtreeLeave(p); |
| 2927 | return rc; |
| 2928 | } |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2929 | int sqlite3BtreeCursorSize(){ |
| 2930 | return sizeof(BtCursor); |
| 2931 | } |
| 2932 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2933 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2934 | |
| 2935 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2936 | ** Close a cursor. The read lock on the database file is released |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2937 | ** when the last cursor is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2938 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2939 | int sqlite3BtreeCloseCursor(BtCursor *pCur){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 2940 | Btree *pBtree = pCur->pBtree; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2941 | if( pBtree ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2942 | int i; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2943 | BtShared *pBt = pCur->pBt; |
| 2944 | sqlite3BtreeEnter(pBtree); |
| 2945 | pBt->db = pBtree->db; |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 2946 | sqlite3BtreeClearCursor(pCur); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2947 | if( pCur->pPrev ){ |
| 2948 | pCur->pPrev->pNext = pCur->pNext; |
| 2949 | }else{ |
| 2950 | pBt->pCursor = pCur->pNext; |
| 2951 | } |
| 2952 | if( pCur->pNext ){ |
| 2953 | pCur->pNext->pPrev = pCur->pPrev; |
| 2954 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2955 | for(i=0; i<=pCur->iPage; i++){ |
| 2956 | releasePage(pCur->apPage[i]); |
| 2957 | } |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2958 | unlockBtreeIfUnused(pBt); |
| 2959 | invalidateOverflowCache(pCur); |
| 2960 | /* sqlite3_free(pCur); */ |
| 2961 | sqlite3BtreeLeave(pBtree); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2962 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2963 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2964 | } |
| 2965 | |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2966 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2967 | ** Make a temporary cursor by filling in the fields of pTempCur. |
| 2968 | ** The temporary cursor is not on the cursor list for the Btree. |
| 2969 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2970 | void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2971 | int i; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2972 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2973 | memcpy(pTempCur, pCur, sizeof(BtCursor)); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2974 | pTempCur->pNext = 0; |
| 2975 | pTempCur->pPrev = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2976 | for(i=0; i<=pTempCur->iPage; i++){ |
| 2977 | sqlite3PagerRef(pTempCur->apPage[i]->pDbPage); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2978 | } |
danielk1977 | 36e2093 | 2008-11-26 07:40:30 +0000 | [diff] [blame] | 2979 | assert( pTempCur->pKey==0 ); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2980 | } |
| 2981 | |
| 2982 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2983 | ** Delete a temporary cursor such as was made by the CreateTemporaryCursor() |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2984 | ** function above. |
| 2985 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2986 | void sqlite3BtreeReleaseTempCursor(BtCursor *pCur){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2987 | int i; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2988 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2989 | for(i=0; i<=pCur->iPage; i++){ |
| 2990 | sqlite3PagerUnref(pCur->apPage[i]->pDbPage); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2991 | } |
danielk1977 | 36e2093 | 2008-11-26 07:40:30 +0000 | [diff] [blame] | 2992 | sqlite3_free(pCur->pKey); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2993 | } |
| 2994 | |
| 2995 | /* |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2996 | ** Make sure the BtCursor* given in the argument has a valid |
| 2997 | ** BtCursor.info structure. If it is not already valid, call |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2998 | ** sqlite3BtreeParseCell() to fill it in. |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2999 | ** |
| 3000 | ** BtCursor.info is a cache of the information in the current cell. |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3001 | ** Using this cache reduces the number of calls to sqlite3BtreeParseCell(). |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3002 | ** |
| 3003 | ** 2007-06-25: There is a bug in some versions of MSVC that cause the |
| 3004 | ** compiler to crash when getCellInfo() is implemented as a macro. |
| 3005 | ** But there is a measureable speed advantage to using the macro on gcc |
| 3006 | ** (when less compiler optimizations like -Os or -O0 are used and the |
| 3007 | ** compiler is not doing agressive inlining.) So we use a real function |
| 3008 | ** for MSVC and a macro for everything else. Ticket #2457. |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3009 | */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3010 | #ifndef NDEBUG |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3011 | static void assertCellInfo(BtCursor *pCur){ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3012 | CellInfo info; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3013 | int iPage = pCur->iPage; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 3014 | memset(&info, 0, sizeof(info)); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3015 | sqlite3BtreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3016 | assert( memcmp(&info, &pCur->info, sizeof(info))==0 ); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3017 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3018 | #else |
| 3019 | #define assertCellInfo(x) |
| 3020 | #endif |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3021 | #ifdef _MSC_VER |
| 3022 | /* Use a real function in MSVC to work around bugs in that compiler. */ |
| 3023 | static void getCellInfo(BtCursor *pCur){ |
| 3024 | if( pCur->info.nSize==0 ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3025 | int iPage = pCur->iPage; |
| 3026 | sqlite3BtreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3027 | pCur->validNKey = 1; |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3028 | }else{ |
| 3029 | assertCellInfo(pCur); |
| 3030 | } |
| 3031 | } |
| 3032 | #else /* if not _MSC_VER */ |
| 3033 | /* Use a macro in all other compilers so that the function is inlined */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3034 | #define getCellInfo(pCur) \ |
| 3035 | if( pCur->info.nSize==0 ){ \ |
| 3036 | int iPage = pCur->iPage; \ |
| 3037 | sqlite3BtreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); \ |
| 3038 | pCur->validNKey = 1; \ |
| 3039 | }else{ \ |
| 3040 | assertCellInfo(pCur); \ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3041 | } |
| 3042 | #endif /* _MSC_VER */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3043 | |
| 3044 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3045 | ** Set *pSize to the size of the buffer needed to hold the value of |
| 3046 | ** the key for the current entry. If the cursor is not pointing |
| 3047 | ** to a valid entry, *pSize is set to 0. |
| 3048 | ** |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3049 | ** For a table with the INTKEY flag set, this routine returns the key |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3050 | ** itself, not the number of bytes in the key. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3051 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3052 | int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3053 | int rc; |
| 3054 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3055 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 3056 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3057 | if( rc==SQLITE_OK ){ |
| 3058 | assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID ); |
| 3059 | if( pCur->eState==CURSOR_INVALID ){ |
| 3060 | *pSize = 0; |
| 3061 | }else{ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3062 | getCellInfo(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3063 | *pSize = pCur->info.nKey; |
| 3064 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3065 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3066 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3067 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3068 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3069 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3070 | ** Set *pSize to the number of bytes of data in the entry the |
| 3071 | ** cursor currently points to. Always return SQLITE_OK. |
| 3072 | ** Failure is not possible. If the cursor is not currently |
| 3073 | ** pointing to an entry (which can happen, for example, if |
| 3074 | ** the database is empty) then *pSize is set to 0. |
| 3075 | */ |
| 3076 | int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3077 | int rc; |
| 3078 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3079 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 3080 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3081 | if( rc==SQLITE_OK ){ |
| 3082 | assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID ); |
| 3083 | if( pCur->eState==CURSOR_INVALID ){ |
| 3084 | /* Not pointing at a valid entry - set *pSize to 0. */ |
| 3085 | *pSize = 0; |
| 3086 | }else{ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3087 | getCellInfo(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3088 | *pSize = pCur->info.nData; |
| 3089 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3090 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3091 | return rc; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3092 | } |
| 3093 | |
| 3094 | /* |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3095 | ** Given the page number of an overflow page in the database (parameter |
| 3096 | ** ovfl), this function finds the page number of the next page in the |
| 3097 | ** linked list of overflow pages. If possible, it uses the auto-vacuum |
| 3098 | ** pointer-map data instead of reading the content of page ovfl to do so. |
| 3099 | ** |
| 3100 | ** If an error occurs an SQLite error code is returned. Otherwise: |
| 3101 | ** |
| 3102 | ** Unless pPgnoNext is NULL, the page number of the next overflow |
| 3103 | ** page in the linked list is written to *pPgnoNext. If page ovfl |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 3104 | ** is the last page in its linked list, *pPgnoNext is set to zero. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3105 | ** |
| 3106 | ** If ppPage is not NULL, *ppPage is set to the MemPage* handle |
| 3107 | ** for page ovfl. The underlying pager page may have been requested |
| 3108 | ** with the noContent flag set, so the page data accessable via |
| 3109 | ** this handle may not be trusted. |
| 3110 | */ |
| 3111 | static int getOverflowPage( |
| 3112 | BtShared *pBt, |
| 3113 | Pgno ovfl, /* Overflow page */ |
| 3114 | MemPage **ppPage, /* OUT: MemPage handle */ |
| 3115 | Pgno *pPgnoNext /* OUT: Next overflow page number */ |
| 3116 | ){ |
| 3117 | Pgno next = 0; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 3118 | int rc = SQLITE_OK; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3119 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3120 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3121 | /* One of these must not be NULL. Otherwise, why call this function? */ |
| 3122 | assert(ppPage || pPgnoNext); |
| 3123 | |
| 3124 | /* If pPgnoNext is NULL, then this function is being called to obtain |
| 3125 | ** a MemPage* reference only. No page-data is required in this case. |
| 3126 | */ |
| 3127 | if( !pPgnoNext ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3128 | return sqlite3BtreeGetPage(pBt, ovfl, ppPage, 1); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3129 | } |
| 3130 | |
| 3131 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3132 | /* Try to find the next page in the overflow list using the |
| 3133 | ** autovacuum pointer-map pages. Guess that the next page in |
| 3134 | ** the overflow list is page number (ovfl+1). If that guess turns |
| 3135 | ** out to be wrong, fall back to loading the data of page |
| 3136 | ** number ovfl to determine the next page number. |
| 3137 | */ |
| 3138 | if( pBt->autoVacuum ){ |
| 3139 | Pgno pgno; |
| 3140 | Pgno iGuess = ovfl+1; |
| 3141 | u8 eType; |
| 3142 | |
| 3143 | while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){ |
| 3144 | iGuess++; |
| 3145 | } |
| 3146 | |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 3147 | if( iGuess<=pagerPagecount(pBt) ){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3148 | rc = ptrmapGet(pBt, iGuess, &eType, &pgno); |
| 3149 | if( rc!=SQLITE_OK ){ |
| 3150 | return rc; |
| 3151 | } |
| 3152 | if( eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){ |
| 3153 | next = iGuess; |
| 3154 | } |
| 3155 | } |
| 3156 | } |
| 3157 | #endif |
| 3158 | |
| 3159 | if( next==0 || ppPage ){ |
| 3160 | MemPage *pPage = 0; |
| 3161 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3162 | rc = sqlite3BtreeGetPage(pBt, ovfl, &pPage, next!=0); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3163 | assert(rc==SQLITE_OK || pPage==0); |
| 3164 | if( next==0 && rc==SQLITE_OK ){ |
| 3165 | next = get4byte(pPage->aData); |
| 3166 | } |
| 3167 | |
| 3168 | if( ppPage ){ |
| 3169 | *ppPage = pPage; |
| 3170 | }else{ |
| 3171 | releasePage(pPage); |
| 3172 | } |
| 3173 | } |
| 3174 | *pPgnoNext = next; |
| 3175 | |
| 3176 | return rc; |
| 3177 | } |
| 3178 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3179 | /* |
| 3180 | ** Copy data from a buffer to a page, or from a page to a buffer. |
| 3181 | ** |
| 3182 | ** pPayload is a pointer to data stored on database page pDbPage. |
| 3183 | ** If argument eOp is false, then nByte bytes of data are copied |
| 3184 | ** from pPayload to the buffer pointed at by pBuf. If eOp is true, |
| 3185 | ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes |
| 3186 | ** of data are copied from the buffer pBuf to pPayload. |
| 3187 | ** |
| 3188 | ** SQLITE_OK is returned on success, otherwise an error code. |
| 3189 | */ |
| 3190 | static int copyPayload( |
| 3191 | void *pPayload, /* Pointer to page data */ |
| 3192 | void *pBuf, /* Pointer to buffer */ |
| 3193 | int nByte, /* Number of bytes to copy */ |
| 3194 | int eOp, /* 0 -> copy from page, 1 -> copy to page */ |
| 3195 | DbPage *pDbPage /* Page containing pPayload */ |
| 3196 | ){ |
| 3197 | if( eOp ){ |
| 3198 | /* Copy data from buffer to page (a write operation) */ |
| 3199 | int rc = sqlite3PagerWrite(pDbPage); |
| 3200 | if( rc!=SQLITE_OK ){ |
| 3201 | return rc; |
| 3202 | } |
| 3203 | memcpy(pPayload, pBuf, nByte); |
| 3204 | }else{ |
| 3205 | /* Copy data from page to buffer (a read operation) */ |
| 3206 | memcpy(pBuf, pPayload, nByte); |
| 3207 | } |
| 3208 | return SQLITE_OK; |
| 3209 | } |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3210 | |
| 3211 | /* |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3212 | ** This function is used to read or overwrite payload information |
| 3213 | ** for the entry that the pCur cursor is pointing to. If the eOp |
| 3214 | ** parameter is 0, this is a read operation (data copied into |
| 3215 | ** buffer pBuf). If it is non-zero, a write (data copied from |
| 3216 | ** buffer pBuf). |
| 3217 | ** |
| 3218 | ** A total of "amt" bytes are read or written beginning at "offset". |
| 3219 | ** Data is read to or from the buffer pBuf. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3220 | ** |
| 3221 | ** This routine does not make a distinction between key and data. |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3222 | ** It just reads or writes bytes from the payload area. Data might |
| 3223 | ** appear on the main page or be scattered out on multiple overflow |
| 3224 | ** pages. |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3225 | ** |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 3226 | ** If the BtCursor.isIncrblobHandle flag is set, and the current |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3227 | ** cursor entry uses one or more overflow pages, this function |
| 3228 | ** allocates space for and lazily popluates the overflow page-list |
| 3229 | ** cache array (BtCursor.aOverflow). Subsequent calls use this |
| 3230 | ** cache to make seeking to the supplied offset more efficient. |
| 3231 | ** |
| 3232 | ** Once an overflow page-list cache has been allocated, it may be |
| 3233 | ** invalidated if some other cursor writes to the same table, or if |
| 3234 | ** the cursor is moved to a different row. Additionally, in auto-vacuum |
| 3235 | ** mode, the following events may invalidate an overflow page-list cache. |
| 3236 | ** |
| 3237 | ** * An incremental vacuum, |
| 3238 | ** * A commit in auto_vacuum="full" mode, |
| 3239 | ** * Creating a table (may require moving an overflow page). |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3240 | */ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3241 | static int accessPayload( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3242 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 3243 | u32 offset, /* Begin reading this far into payload */ |
| 3244 | u32 amt, /* Read this many bytes */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3245 | unsigned char *pBuf, /* Write the bytes into this buffer */ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3246 | int skipKey, /* offset begins at data if this is true */ |
| 3247 | int eOp /* zero to read. non-zero to write. */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3248 | ){ |
| 3249 | unsigned char *aPayload; |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3250 | int rc = SQLITE_OK; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3251 | u32 nKey; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3252 | int iIdx = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3253 | MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */ |
danielk1977 | 0d06541 | 2008-11-12 18:21:36 +0000 | [diff] [blame] | 3254 | BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3255 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3256 | assert( pPage ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3257 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3258 | assert( pCur->aiIdx[pCur->iPage]<pPage->nCell ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3259 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3260 | |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3261 | getCellInfo(pCur); |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3262 | aPayload = pCur->info.pCell + pCur->info.nHeader; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3263 | nKey = (pPage->intKey ? 0 : (int)pCur->info.nKey); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3264 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3265 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3266 | offset += nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3267 | } |
danielk1977 | 0d06541 | 2008-11-12 18:21:36 +0000 | [diff] [blame] | 3268 | if( offset+amt > nKey+pCur->info.nData |
| 3269 | || &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize] |
| 3270 | ){ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3271 | /* Trying to read or write past the end of the data is an error */ |
danielk1977 | 67fd7a9 | 2008-09-10 17:53:35 +0000 | [diff] [blame] | 3272 | return SQLITE_CORRUPT_BKPT; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3273 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3274 | |
| 3275 | /* Check if data must be read/written to/from the btree page itself. */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3276 | if( offset<pCur->info.nLocal ){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3277 | int a = amt; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3278 | if( a+offset>pCur->info.nLocal ){ |
| 3279 | a = pCur->info.nLocal - offset; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3280 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3281 | rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3282 | offset = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3283 | pBuf += a; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3284 | amt -= a; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 3285 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3286 | offset -= pCur->info.nLocal; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3287 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3288 | |
| 3289 | if( rc==SQLITE_OK && amt>0 ){ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 3290 | const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3291 | Pgno nextPage; |
| 3292 | |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3293 | nextPage = get4byte(&aPayload[pCur->info.nLocal]); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3294 | |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3295 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 3296 | /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[] |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3297 | ** has not been allocated, allocate it now. The array is sized at |
| 3298 | ** one entry for each overflow page in the overflow chain. The |
| 3299 | ** page number of the first overflow page is stored in aOverflow[0], |
| 3300 | ** etc. A value of 0 in the aOverflow[] array means "not yet known" |
| 3301 | ** (the cache is lazily populated). |
| 3302 | */ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 3303 | if( pCur->isIncrblobHandle && !pCur->aOverflow ){ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3304 | int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3305 | pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl); |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3306 | if( nOvfl && !pCur->aOverflow ){ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3307 | rc = SQLITE_NOMEM; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3308 | } |
| 3309 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3310 | |
| 3311 | /* If the overflow page-list cache has been allocated and the |
| 3312 | ** entry for the first required overflow page is valid, skip |
| 3313 | ** directly to it. |
| 3314 | */ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3315 | if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){ |
| 3316 | iIdx = (offset/ovflSize); |
| 3317 | nextPage = pCur->aOverflow[iIdx]; |
| 3318 | offset = (offset%ovflSize); |
| 3319 | } |
| 3320 | #endif |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3321 | |
| 3322 | for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){ |
| 3323 | |
| 3324 | #ifndef SQLITE_OMIT_INCRBLOB |
| 3325 | /* If required, populate the overflow page-list cache. */ |
| 3326 | if( pCur->aOverflow ){ |
| 3327 | assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage); |
| 3328 | pCur->aOverflow[iIdx] = nextPage; |
| 3329 | } |
| 3330 | #endif |
| 3331 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3332 | if( offset>=ovflSize ){ |
| 3333 | /* The only reason to read this page is to obtain the page |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3334 | ** number for the next page in the overflow chain. The page |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 3335 | ** data is not required. So first try to lookup the overflow |
| 3336 | ** page-list cache, if any, then fall back to the getOverflowPage() |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3337 | ** function. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3338 | */ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3339 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3340 | if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){ |
| 3341 | nextPage = pCur->aOverflow[iIdx+1]; |
| 3342 | } else |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3343 | #endif |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3344 | rc = getOverflowPage(pBt, nextPage, 0, &nextPage); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3345 | offset -= ovflSize; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3346 | }else{ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3347 | /* Need to read this page properly. It contains some of the |
| 3348 | ** range of data that is being read (eOp==0) or written (eOp!=0). |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3349 | */ |
| 3350 | DbPage *pDbPage; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3351 | int a = amt; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3352 | rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3353 | if( rc==SQLITE_OK ){ |
| 3354 | aPayload = sqlite3PagerGetData(pDbPage); |
| 3355 | nextPage = get4byte(aPayload); |
| 3356 | if( a + offset > ovflSize ){ |
| 3357 | a = ovflSize - offset; |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3358 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3359 | rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage); |
| 3360 | sqlite3PagerUnref(pDbPage); |
| 3361 | offset = 0; |
| 3362 | amt -= a; |
| 3363 | pBuf += a; |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3364 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3365 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3366 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3367 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3368 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3369 | if( rc==SQLITE_OK && amt>0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3370 | return SQLITE_CORRUPT_BKPT; |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 3371 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3372 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3373 | } |
| 3374 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3375 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3376 | ** Read part of the key associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3377 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3378 | ** begins at "offset". |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3379 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3380 | ** Return SQLITE_OK on success or an error code if anything goes |
| 3381 | ** wrong. An error is returned if "offset+amt" is larger than |
| 3382 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3383 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3384 | int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3385 | int rc; |
| 3386 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3387 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 3388 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3389 | if( rc==SQLITE_OK ){ |
| 3390 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3391 | assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] ); |
| 3392 | if( pCur->apPage[0]->intKey ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3393 | return SQLITE_CORRUPT_BKPT; |
| 3394 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3395 | assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3396 | rc = accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0, 0); |
drh | 6575a22 | 2005-03-10 17:06:34 +0000 | [diff] [blame] | 3397 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3398 | return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3399 | } |
| 3400 | |
| 3401 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3402 | ** Read part of the data associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3403 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3404 | ** begins at "offset". |
| 3405 | ** |
| 3406 | ** Return SQLITE_OK on success or an error code if anything goes |
| 3407 | ** wrong. An error is returned if "offset+amt" is larger than |
| 3408 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3409 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3410 | int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3411 | int rc; |
| 3412 | |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 3413 | #ifndef SQLITE_OMIT_INCRBLOB |
| 3414 | if ( pCur->eState==CURSOR_INVALID ){ |
| 3415 | return SQLITE_ABORT; |
| 3416 | } |
| 3417 | #endif |
| 3418 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3419 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 3420 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3421 | if( rc==SQLITE_OK ){ |
| 3422 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3423 | assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] ); |
| 3424 | assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3425 | rc = accessPayload(pCur, offset, amt, pBuf, 1, 0); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3426 | } |
| 3427 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3428 | } |
| 3429 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3430 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3431 | ** Return a pointer to payload information from the entry that the |
| 3432 | ** pCur cursor is pointing to. The pointer is to the beginning of |
| 3433 | ** 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] | 3434 | ** skipKey==1. The number of bytes of available key/data is written |
| 3435 | ** into *pAmt. If *pAmt==0, then the value returned will not be |
| 3436 | ** a valid pointer. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3437 | ** |
| 3438 | ** This routine is an optimization. It is common for the entire key |
| 3439 | ** and data to fit on the local page and for there to be no overflow |
| 3440 | ** pages. When that is so, this routine can be used to access the |
| 3441 | ** 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] | 3442 | ** onto overflow pages, then accessPayload() must be used to reassembly |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3443 | ** the key/data and copy it into a preallocated buffer. |
| 3444 | ** |
| 3445 | ** The pointer returned by this routine looks directly into the cached |
| 3446 | ** page of the database. The data might change or move the next time |
| 3447 | ** any btree routine is called. |
| 3448 | */ |
| 3449 | static const unsigned char *fetchPayload( |
| 3450 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3451 | int *pAmt, /* Write the number of available bytes here */ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3452 | int skipKey /* read beginning at data if this is true */ |
| 3453 | ){ |
| 3454 | unsigned char *aPayload; |
| 3455 | MemPage *pPage; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3456 | u32 nKey; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 3457 | u32 nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3458 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3459 | assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3460 | assert( pCur->eState==CURSOR_VALID ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3461 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3462 | pPage = pCur->apPage[pCur->iPage]; |
| 3463 | assert( pCur->aiIdx[pCur->iPage]<pPage->nCell ); |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3464 | getCellInfo(pCur); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3465 | aPayload = pCur->info.pCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3466 | aPayload += pCur->info.nHeader; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3467 | if( pPage->intKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3468 | nKey = 0; |
| 3469 | }else{ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3470 | nKey = (int)pCur->info.nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3471 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3472 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3473 | aPayload += nKey; |
| 3474 | nLocal = pCur->info.nLocal - nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3475 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3476 | nLocal = pCur->info.nLocal; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3477 | if( nLocal>nKey ){ |
| 3478 | nLocal = nKey; |
| 3479 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3480 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3481 | *pAmt = nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3482 | return aPayload; |
| 3483 | } |
| 3484 | |
| 3485 | |
| 3486 | /* |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3487 | ** For the entry that cursor pCur is point to, return as |
| 3488 | ** many bytes of the key or data as are available on the local |
| 3489 | ** b-tree page. Write the number of available bytes into *pAmt. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3490 | ** |
| 3491 | ** The pointer returned is ephemeral. The key/data may move |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3492 | ** or be destroyed on the next call to any Btree routine, |
| 3493 | ** including calls from other threads against the same cache. |
| 3494 | ** Hence, a mutex on the BtShared should be held prior to calling |
| 3495 | ** this routine. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3496 | ** |
| 3497 | ** These routines is used to get quick access to key and data |
| 3498 | ** in the common case where no overflow pages are used. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3499 | */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3500 | const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3501 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3502 | if( pCur->eState==CURSOR_VALID ){ |
| 3503 | return (const void*)fetchPayload(pCur, pAmt, 0); |
| 3504 | } |
| 3505 | return 0; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3506 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3507 | const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3508 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3509 | if( pCur->eState==CURSOR_VALID ){ |
| 3510 | return (const void*)fetchPayload(pCur, pAmt, 1); |
| 3511 | } |
| 3512 | return 0; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3513 | } |
| 3514 | |
| 3515 | |
| 3516 | /* |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3517 | ** 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] | 3518 | ** page number of the child page to move to. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3519 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3520 | static int moveToChild(BtCursor *pCur, u32 newPgno){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3521 | int rc; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3522 | int i = pCur->iPage; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3523 | MemPage *pNewPage; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 3524 | BtShared *pBt = pCur->pBt; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3525 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3526 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3527 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3528 | assert( pCur->iPage<BTCURSOR_MAX_DEPTH ); |
| 3529 | if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){ |
| 3530 | return SQLITE_CORRUPT_BKPT; |
| 3531 | } |
| 3532 | rc = getAndInitPage(pBt, newPgno, &pNewPage); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3533 | if( rc ) return rc; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3534 | pCur->apPage[i+1] = pNewPage; |
| 3535 | pCur->aiIdx[i+1] = 0; |
| 3536 | pCur->iPage++; |
| 3537 | |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3538 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3539 | pCur->validNKey = 0; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 3540 | if( pNewPage->nCell<1 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3541 | return SQLITE_CORRUPT_BKPT; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 3542 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3543 | return SQLITE_OK; |
| 3544 | } |
| 3545 | |
danielk1977 | bf93c56 | 2008-09-29 15:53:25 +0000 | [diff] [blame] | 3546 | #ifndef NDEBUG |
| 3547 | /* |
| 3548 | ** Page pParent is an internal (non-leaf) tree page. This function |
| 3549 | ** asserts that page number iChild is the left-child if the iIdx'th |
| 3550 | ** cell in page pParent. Or, if iIdx is equal to the total number of |
| 3551 | ** cells in pParent, that page number iChild is the right-child of |
| 3552 | ** the page. |
| 3553 | */ |
| 3554 | static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){ |
| 3555 | assert( iIdx<=pParent->nCell ); |
| 3556 | if( iIdx==pParent->nCell ){ |
| 3557 | assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild ); |
| 3558 | }else{ |
| 3559 | assert( get4byte(findCell(pParent, iIdx))==iChild ); |
| 3560 | } |
| 3561 | } |
| 3562 | #else |
| 3563 | # define assertParentIndex(x,y,z) |
| 3564 | #endif |
| 3565 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3566 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3567 | ** Move the cursor up to the parent page. |
| 3568 | ** |
| 3569 | ** pCur->idx is set to the cell index that contains the pointer |
| 3570 | ** to the page we are coming from. If we are coming from the |
| 3571 | ** right-most child page then pCur->idx is set to one more than |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3572 | ** the largest cell index. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3573 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3574 | void sqlite3BtreeMoveToParent(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3575 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3576 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3577 | assert( pCur->iPage>0 ); |
| 3578 | assert( pCur->apPage[pCur->iPage] ); |
danielk1977 | bf93c56 | 2008-09-29 15:53:25 +0000 | [diff] [blame] | 3579 | assertParentIndex( |
| 3580 | pCur->apPage[pCur->iPage-1], |
| 3581 | pCur->aiIdx[pCur->iPage-1], |
| 3582 | pCur->apPage[pCur->iPage]->pgno |
| 3583 | ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3584 | releasePage(pCur->apPage[pCur->iPage]); |
| 3585 | pCur->iPage--; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3586 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3587 | pCur->validNKey = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3588 | } |
| 3589 | |
| 3590 | /* |
| 3591 | ** Move the cursor to the root page |
| 3592 | */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3593 | static int moveToRoot(BtCursor *pCur){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3594 | MemPage *pRoot; |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3595 | int rc = SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3596 | Btree *p = pCur->pBtree; |
| 3597 | BtShared *pBt = p->pBt; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3598 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3599 | assert( cursorHoldsMutex(pCur) ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 3600 | assert( CURSOR_INVALID < CURSOR_REQUIRESEEK ); |
| 3601 | assert( CURSOR_VALID < CURSOR_REQUIRESEEK ); |
| 3602 | assert( CURSOR_FAULT > CURSOR_REQUIRESEEK ); |
| 3603 | if( pCur->eState>=CURSOR_REQUIRESEEK ){ |
| 3604 | if( pCur->eState==CURSOR_FAULT ){ |
| 3605 | return pCur->skip; |
| 3606 | } |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 3607 | sqlite3BtreeClearCursor(pCur); |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 3608 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3609 | |
| 3610 | if( pCur->iPage>=0 ){ |
| 3611 | int i; |
| 3612 | for(i=1; i<=pCur->iPage; i++){ |
| 3613 | releasePage(pCur->apPage[i]); |
danielk1977 | d9f6c53 | 2008-09-19 16:39:38 +0000 | [diff] [blame] | 3614 | } |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3615 | }else{ |
| 3616 | if( |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3617 | SQLITE_OK!=(rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0])) |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3618 | ){ |
| 3619 | pCur->eState = CURSOR_INVALID; |
| 3620 | return rc; |
| 3621 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3622 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3623 | |
| 3624 | pRoot = pCur->apPage[0]; |
| 3625 | assert( pRoot->pgno==pCur->pgnoRoot ); |
| 3626 | pCur->iPage = 0; |
| 3627 | pCur->aiIdx[0] = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3628 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3629 | pCur->atLast = 0; |
| 3630 | pCur->validNKey = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3631 | |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3632 | if( pRoot->nCell==0 && !pRoot->leaf ){ |
| 3633 | Pgno subpage; |
| 3634 | assert( pRoot->pgno==1 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3635 | subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3636 | assert( subpage>0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3637 | pCur->eState = CURSOR_VALID; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3638 | rc = moveToChild(pCur, subpage); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3639 | }else{ |
| 3640 | pCur->eState = ((pRoot->nCell>0)?CURSOR_VALID:CURSOR_INVALID); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3641 | } |
| 3642 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3643 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3644 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3645 | /* |
| 3646 | ** Move the cursor down to the left-most leaf entry beneath the |
| 3647 | ** entry to which it is currently pointing. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3648 | ** |
| 3649 | ** The left-most leaf is the one with the smallest key - the first |
| 3650 | ** in ascending order. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3651 | */ |
| 3652 | static int moveToLeftmost(BtCursor *pCur){ |
| 3653 | Pgno pgno; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3654 | int rc = SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3655 | MemPage *pPage; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3656 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3657 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3658 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3659 | while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){ |
| 3660 | assert( pCur->aiIdx[pCur->iPage]<pPage->nCell ); |
| 3661 | pgno = get4byte(findCell(pPage, pCur->aiIdx[pCur->iPage])); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3662 | rc = moveToChild(pCur, pgno); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3663 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3664 | return rc; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3665 | } |
| 3666 | |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3667 | /* |
| 3668 | ** Move the cursor down to the right-most leaf entry beneath the |
| 3669 | ** page to which it is currently pointing. Notice the difference |
| 3670 | ** between moveToLeftmost() and moveToRightmost(). moveToLeftmost() |
| 3671 | ** finds the left-most entry beneath the *entry* whereas moveToRightmost() |
| 3672 | ** finds the right-most entry beneath the *page*. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3673 | ** |
| 3674 | ** The right-most entry is the one with the largest key - the last |
| 3675 | ** key in ascending order. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3676 | */ |
| 3677 | static int moveToRightmost(BtCursor *pCur){ |
| 3678 | Pgno pgno; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3679 | int rc = SQLITE_OK; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 3680 | MemPage *pPage = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3681 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3682 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3683 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3684 | while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3685 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3686 | pCur->aiIdx[pCur->iPage] = pPage->nCell; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3687 | rc = moveToChild(pCur, pgno); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3688 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3689 | if( rc==SQLITE_OK ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3690 | pCur->aiIdx[pCur->iPage] = pPage->nCell-1; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3691 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3692 | pCur->validNKey = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3693 | } |
danielk1977 | 518002e | 2008-09-05 05:02:46 +0000 | [diff] [blame] | 3694 | return rc; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3695 | } |
| 3696 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3697 | /* Move the cursor to the first entry in the table. Return SQLITE_OK |
| 3698 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 3699 | ** or set *pRes to 1 if the table is empty. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3700 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3701 | int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3702 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3703 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3704 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3705 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3706 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3707 | if( rc==SQLITE_OK ){ |
| 3708 | if( pCur->eState==CURSOR_INVALID ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3709 | assert( pCur->apPage[pCur->iPage]->nCell==0 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3710 | *pRes = 1; |
| 3711 | rc = SQLITE_OK; |
| 3712 | }else{ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3713 | assert( pCur->apPage[pCur->iPage]->nCell>0 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3714 | *pRes = 0; |
| 3715 | rc = moveToLeftmost(pCur); |
| 3716 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3717 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3718 | return rc; |
| 3719 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3720 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3721 | /* Move the cursor to the last entry in the table. Return SQLITE_OK |
| 3722 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 3723 | ** or set *pRes to 1 if the table is empty. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3724 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3725 | int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3726 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3727 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3728 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3729 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3730 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3731 | if( rc==SQLITE_OK ){ |
| 3732 | if( CURSOR_INVALID==pCur->eState ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3733 | assert( pCur->apPage[pCur->iPage]->nCell==0 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3734 | *pRes = 1; |
| 3735 | }else{ |
| 3736 | assert( pCur->eState==CURSOR_VALID ); |
| 3737 | *pRes = 0; |
| 3738 | rc = moveToRightmost(pCur); |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3739 | getCellInfo(pCur); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3740 | pCur->atLast = rc==SQLITE_OK ?1:0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3741 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3742 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3743 | return rc; |
| 3744 | } |
| 3745 | |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 3746 | /* Move the cursor so that it points to an entry near the key |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3747 | ** specified by pIdxKey or intKey. Return a success code. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3748 | ** |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3749 | ** For INTKEY tables, the intKey parameter is used. pIdxKey |
| 3750 | ** must be NULL. For index tables, pIdxKey is used and intKey |
| 3751 | ** is ignored. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3752 | ** |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3753 | ** If an exact match is not found, then the cursor is always |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3754 | ** left pointing at a leaf page which would hold the entry if it |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3755 | ** were present. The cursor might point to an entry that comes |
| 3756 | ** before or after the key. |
| 3757 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3758 | ** The result of comparing the key with the entry to which the |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 3759 | ** cursor is written to *pRes if pRes!=NULL. The meaning of |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3760 | ** this value is as follows: |
| 3761 | ** |
| 3762 | ** *pRes<0 The cursor is left pointing at an entry that |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3763 | ** is smaller than pKey or if the table is empty |
| 3764 | ** and the cursor is therefore left point to nothing. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3765 | ** |
| 3766 | ** *pRes==0 The cursor is left pointing at an entry that |
| 3767 | ** exactly matches pKey. |
| 3768 | ** |
| 3769 | ** *pRes>0 The cursor is left pointing at an entry that |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 3770 | ** is larger than pKey. |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3771 | ** |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3772 | */ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3773 | int sqlite3BtreeMovetoUnpacked( |
| 3774 | BtCursor *pCur, /* The cursor to be moved */ |
| 3775 | UnpackedRecord *pIdxKey, /* Unpacked index key */ |
| 3776 | i64 intKey, /* The table key */ |
| 3777 | int biasRight, /* If true, bias the search to the high end */ |
| 3778 | int *pRes /* Write search results here */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3779 | ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3780 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3781 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3782 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3783 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3784 | |
| 3785 | /* If the cursor is already positioned at the point we are trying |
| 3786 | ** to move to, then just return without doing any work */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3787 | if( pCur->eState==CURSOR_VALID && pCur->validNKey |
| 3788 | && pCur->apPage[0]->intKey |
| 3789 | ){ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3790 | if( pCur->info.nKey==intKey ){ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3791 | *pRes = 0; |
| 3792 | return SQLITE_OK; |
| 3793 | } |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3794 | if( pCur->atLast && pCur->info.nKey<intKey ){ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3795 | *pRes = -1; |
| 3796 | return SQLITE_OK; |
| 3797 | } |
| 3798 | } |
| 3799 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3800 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3801 | if( rc ){ |
| 3802 | return rc; |
| 3803 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3804 | assert( pCur->apPage[pCur->iPage] ); |
| 3805 | assert( pCur->apPage[pCur->iPage]->isInit ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3806 | if( pCur->eState==CURSOR_INVALID ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3807 | *pRes = -1; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3808 | assert( pCur->apPage[pCur->iPage]->nCell==0 ); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3809 | return SQLITE_OK; |
| 3810 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3811 | assert( pCur->apPage[0]->intKey || pIdxKey ); |
drh | 1468438 | 2006-11-30 13:05:29 +0000 | [diff] [blame] | 3812 | for(;;){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3813 | int lwr, upr; |
| 3814 | Pgno chldPg; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3815 | MemPage *pPage = pCur->apPage[pCur->iPage]; |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3816 | int c = -1; /* pRes return if table is empty must be -1 */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3817 | lwr = 0; |
| 3818 | upr = pPage->nCell-1; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3819 | if( !pPage->intKey && pIdxKey==0 ){ |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3820 | rc = SQLITE_CORRUPT_BKPT; |
| 3821 | goto moveto_finish; |
drh | 4eec4c1 | 2005-01-21 00:22:37 +0000 | [diff] [blame] | 3822 | } |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3823 | if( biasRight ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3824 | pCur->aiIdx[pCur->iPage] = (u16)upr; |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3825 | }else{ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3826 | pCur->aiIdx[pCur->iPage] = (u16)((upr+lwr)/2); |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3827 | } |
drh | f1d68b3 | 2007-03-29 04:43:26 +0000 | [diff] [blame] | 3828 | if( lwr<=upr ) for(;;){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3829 | void *pCellKey; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3830 | i64 nCellKey; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3831 | int idx = pCur->aiIdx[pCur->iPage]; |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3832 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3833 | pCur->validNKey = 1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3834 | if( pPage->intKey ){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3835 | u8 *pCell; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3836 | pCell = findCell(pPage, idx) + pPage->childPtrSize; |
drh | d172f86 | 2006-01-12 15:01:15 +0000 | [diff] [blame] | 3837 | if( pPage->hasData ){ |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 3838 | u32 dummy; |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 3839 | pCell += getVarint32(pCell, dummy); |
drh | d172f86 | 2006-01-12 15:01:15 +0000 | [diff] [blame] | 3840 | } |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3841 | getVarint(pCell, (u64*)&nCellKey); |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3842 | if( nCellKey==intKey ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3843 | c = 0; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3844 | }else if( nCellKey<intKey ){ |
drh | 41eb9e9 | 2008-04-02 18:33:07 +0000 | [diff] [blame] | 3845 | c = -1; |
| 3846 | }else{ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3847 | assert( nCellKey>intKey ); |
drh | 41eb9e9 | 2008-04-02 18:33:07 +0000 | [diff] [blame] | 3848 | c = +1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3849 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3850 | }else{ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3851 | int available; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3852 | pCellKey = (void *)fetchPayload(pCur, &available, 0); |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3853 | nCellKey = pCur->info.nKey; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3854 | if( available>=nCellKey ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3855 | c = sqlite3VdbeRecordCompare((int)nCellKey, pCellKey, pIdxKey); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3856 | }else{ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3857 | pCellKey = sqlite3Malloc( (int)nCellKey ); |
danielk1977 | 6507ecb | 2008-03-25 09:56:44 +0000 | [diff] [blame] | 3858 | if( pCellKey==0 ){ |
| 3859 | rc = SQLITE_NOMEM; |
| 3860 | goto moveto_finish; |
| 3861 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3862 | rc = sqlite3BtreeKey(pCur, 0, (int)nCellKey, (void*)pCellKey); |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 3863 | c = sqlite3VdbeRecordCompare((int)nCellKey, pCellKey, pIdxKey); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 3864 | sqlite3_free(pCellKey); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3865 | if( rc ) goto moveto_finish; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3866 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3867 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3868 | if( c==0 ){ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3869 | pCur->info.nKey = nCellKey; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 3870 | if( pPage->intKey && !pPage->leaf ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3871 | lwr = idx; |
drh | fc70e6f | 2004-05-12 21:11:27 +0000 | [diff] [blame] | 3872 | upr = lwr - 1; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3873 | break; |
| 3874 | }else{ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3875 | if( pRes ) *pRes = 0; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3876 | rc = SQLITE_OK; |
| 3877 | goto moveto_finish; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3878 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3879 | } |
| 3880 | if( c<0 ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3881 | lwr = idx+1; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3882 | }else{ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3883 | upr = idx-1; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3884 | } |
drh | f1d68b3 | 2007-03-29 04:43:26 +0000 | [diff] [blame] | 3885 | if( lwr>upr ){ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3886 | pCur->info.nKey = nCellKey; |
drh | f1d68b3 | 2007-03-29 04:43:26 +0000 | [diff] [blame] | 3887 | break; |
| 3888 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3889 | pCur->aiIdx[pCur->iPage] = (u16)((lwr+upr)/2); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3890 | } |
| 3891 | assert( lwr==upr+1 ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3892 | assert( pPage->isInit ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3893 | if( pPage->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3894 | chldPg = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3895 | }else if( lwr>=pPage->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3896 | chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3897 | }else{ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3898 | chldPg = get4byte(findCell(pPage, lwr)); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3899 | } |
| 3900 | if( chldPg==0 ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3901 | assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3902 | if( pRes ) *pRes = c; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3903 | rc = SQLITE_OK; |
| 3904 | goto moveto_finish; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3905 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3906 | pCur->aiIdx[pCur->iPage] = (u16)lwr; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3907 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3908 | pCur->validNKey = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3909 | rc = moveToChild(pCur, chldPg); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3910 | if( rc ) goto moveto_finish; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3911 | } |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3912 | moveto_finish: |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3913 | return rc; |
| 3914 | } |
| 3915 | |
| 3916 | /* |
| 3917 | ** In this version of BtreeMoveto, pKey is a packed index record |
| 3918 | ** such as is generated by the OP_MakeRecord opcode. Unpack the |
| 3919 | ** record and then call BtreeMovetoUnpacked() to do the work. |
| 3920 | */ |
| 3921 | int sqlite3BtreeMoveto( |
| 3922 | BtCursor *pCur, /* Cursor open on the btree to be searched */ |
| 3923 | const void *pKey, /* Packed key if the btree is an index */ |
| 3924 | i64 nKey, /* Integer key for tables. Size of pKey for indices */ |
| 3925 | int bias, /* Bias search to the high end */ |
| 3926 | int *pRes /* Write search results here */ |
| 3927 | ){ |
| 3928 | int rc; /* Status code */ |
| 3929 | UnpackedRecord *pIdxKey; /* Unpacked index key */ |
drh | 23f79d0 | 2008-08-20 22:06:47 +0000 | [diff] [blame] | 3930 | UnpackedRecord aSpace[16]; /* Temp space for pIdxKey - to avoid a malloc */ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3931 | |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 3932 | if( pKey ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3933 | assert( nKey==(i64)(int)nKey ); |
| 3934 | pIdxKey = sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, |
drh | 23f79d0 | 2008-08-20 22:06:47 +0000 | [diff] [blame] | 3935 | aSpace, sizeof(aSpace)); |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3936 | if( pIdxKey==0 ) return SQLITE_NOMEM; |
| 3937 | }else{ |
| 3938 | pIdxKey = 0; |
| 3939 | } |
| 3940 | rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes); |
| 3941 | if( pKey ){ |
| 3942 | sqlite3VdbeDeleteUnpackedRecord(pIdxKey); |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 3943 | } |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3944 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3945 | } |
| 3946 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3947 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3948 | /* |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3949 | ** Return TRUE if the cursor is not pointing at an entry of the table. |
| 3950 | ** |
| 3951 | ** TRUE will be returned after a call to sqlite3BtreeNext() moves |
| 3952 | ** past the last entry in the table or sqlite3BtreePrev() moves past |
| 3953 | ** the first entry. TRUE is also returned if the table is empty. |
| 3954 | */ |
| 3955 | int sqlite3BtreeEof(BtCursor *pCur){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3956 | /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries |
| 3957 | ** have been deleted? This API will need to change to return an error code |
| 3958 | ** as well as the boolean result value. |
| 3959 | */ |
| 3960 | return (CURSOR_VALID!=pCur->eState); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3961 | } |
| 3962 | |
| 3963 | /* |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 3964 | ** Return the database connection handle for a cursor. |
| 3965 | */ |
| 3966 | sqlite3 *sqlite3BtreeCursorDb(const BtCursor *pCur){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3967 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
| 3968 | return pCur->pBtree->db; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 3969 | } |
| 3970 | |
| 3971 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3972 | ** Advance the cursor to the next entry in the database. If |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3973 | ** successful then set *pRes=0. If the cursor |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3974 | ** was already pointing to the last entry in the database before |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3975 | ** this routine was called, then set *pRes=1. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3976 | */ |
drh | d094db1 | 2008-04-03 21:46:57 +0000 | [diff] [blame] | 3977 | int sqlite3BtreeNext(BtCursor *pCur, int *pRes){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3978 | int rc; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3979 | int idx; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 3980 | MemPage *pPage; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3981 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3982 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 3983 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3984 | if( rc!=SQLITE_OK ){ |
| 3985 | return rc; |
| 3986 | } |
drh | 8c4d3a6 | 2007-04-06 01:03:32 +0000 | [diff] [blame] | 3987 | assert( pRes!=0 ); |
drh | 8c4d3a6 | 2007-04-06 01:03:32 +0000 | [diff] [blame] | 3988 | if( CURSOR_INVALID==pCur->eState ){ |
| 3989 | *pRes = 1; |
| 3990 | return SQLITE_OK; |
| 3991 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3992 | if( pCur->skip>0 ){ |
| 3993 | pCur->skip = 0; |
| 3994 | *pRes = 0; |
| 3995 | return SQLITE_OK; |
| 3996 | } |
| 3997 | pCur->skip = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3998 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3999 | pPage = pCur->apPage[pCur->iPage]; |
| 4000 | idx = ++pCur->aiIdx[pCur->iPage]; |
| 4001 | assert( pPage->isInit ); |
| 4002 | assert( idx<=pPage->nCell ); |
danielk1977 | 6a43f9b | 2004-11-16 04:57:24 +0000 | [diff] [blame] | 4003 | |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 4004 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 4005 | pCur->validNKey = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4006 | if( idx>=pPage->nCell ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4007 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4008 | rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4009 | if( rc ) return rc; |
| 4010 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4011 | *pRes = 0; |
| 4012 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4013 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4014 | do{ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4015 | if( pCur->iPage==0 ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4016 | *pRes = 1; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4017 | pCur->eState = CURSOR_INVALID; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4018 | return SQLITE_OK; |
| 4019 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4020 | sqlite3BtreeMoveToParent(pCur); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4021 | pPage = pCur->apPage[pCur->iPage]; |
| 4022 | }while( pCur->aiIdx[pCur->iPage]>=pPage->nCell ); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4023 | *pRes = 0; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 4024 | if( pPage->intKey ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4025 | rc = sqlite3BtreeNext(pCur, pRes); |
| 4026 | }else{ |
| 4027 | rc = SQLITE_OK; |
| 4028 | } |
| 4029 | return rc; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4030 | } |
| 4031 | *pRes = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4032 | if( pPage->leaf ){ |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4033 | return SQLITE_OK; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4034 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4035 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4036 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4037 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4038 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4039 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4040 | /* |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4041 | ** 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] | 4042 | ** successful then set *pRes=0. If the cursor |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4043 | ** was already pointing to the first entry in the database before |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4044 | ** this routine was called, then set *pRes=1. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4045 | */ |
drh | d094db1 | 2008-04-03 21:46:57 +0000 | [diff] [blame] | 4046 | int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){ |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4047 | int rc; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4048 | MemPage *pPage; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4049 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4050 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 4051 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4052 | if( rc!=SQLITE_OK ){ |
| 4053 | return rc; |
| 4054 | } |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 4055 | pCur->atLast = 0; |
drh | 8c4d3a6 | 2007-04-06 01:03:32 +0000 | [diff] [blame] | 4056 | if( CURSOR_INVALID==pCur->eState ){ |
| 4057 | *pRes = 1; |
| 4058 | return SQLITE_OK; |
| 4059 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4060 | if( pCur->skip<0 ){ |
| 4061 | pCur->skip = 0; |
| 4062 | *pRes = 0; |
| 4063 | return SQLITE_OK; |
| 4064 | } |
| 4065 | pCur->skip = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4066 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4067 | pPage = pCur->apPage[pCur->iPage]; |
| 4068 | assert( pPage->isInit ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4069 | if( !pPage->leaf ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4070 | int idx = pCur->aiIdx[pCur->iPage]; |
| 4071 | rc = moveToChild(pCur, get4byte(findCell(pPage, idx))); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4072 | if( rc ){ |
| 4073 | return rc; |
| 4074 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4075 | rc = moveToRightmost(pCur); |
| 4076 | }else{ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4077 | while( pCur->aiIdx[pCur->iPage]==0 ){ |
| 4078 | if( pCur->iPage==0 ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4079 | pCur->eState = CURSOR_INVALID; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 4080 | *pRes = 1; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4081 | return SQLITE_OK; |
| 4082 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4083 | sqlite3BtreeMoveToParent(pCur); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4084 | } |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 4085 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 4086 | pCur->validNKey = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4087 | |
| 4088 | pCur->aiIdx[pCur->iPage]--; |
| 4089 | pPage = pCur->apPage[pCur->iPage]; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 4090 | if( pPage->intKey && !pPage->leaf ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4091 | rc = sqlite3BtreePrevious(pCur, pRes); |
| 4092 | }else{ |
| 4093 | rc = SQLITE_OK; |
| 4094 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4095 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4096 | *pRes = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4097 | return rc; |
| 4098 | } |
| 4099 | |
| 4100 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4101 | ** Allocate a new page from the database file. |
| 4102 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4103 | ** The new page is marked as dirty. (In other words, sqlite3PagerWrite() |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4104 | ** has already been called on the new page.) The new page has also |
| 4105 | ** been referenced and the calling routine is responsible for calling |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4106 | ** sqlite3PagerUnref() on the new page when it is done. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4107 | ** |
| 4108 | ** SQLITE_OK is returned on success. Any other return value indicates |
| 4109 | ** an error. *ppPage and *pPgno are undefined in the event of an error. |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4110 | ** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned. |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 4111 | ** |
drh | 199e3cf | 2002-07-18 11:01:47 +0000 | [diff] [blame] | 4112 | ** If the "nearby" parameter is not 0, then a (feeble) effort is made to |
| 4113 | ** 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] | 4114 | ** attempt to keep related pages close to each other in the database file, |
| 4115 | ** which in turn can make database access faster. |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4116 | ** |
| 4117 | ** If the "exact" parameter is not 0, and the page-number nearby exists |
| 4118 | ** anywhere on the free-list, then it is guarenteed to be returned. This |
| 4119 | ** is only used by auto-vacuum databases when allocating a new table. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4120 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4121 | static int allocateBtreePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4122 | BtShared *pBt, |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4123 | MemPage **ppPage, |
| 4124 | Pgno *pPgno, |
| 4125 | Pgno nearby, |
| 4126 | u8 exact |
| 4127 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4128 | MemPage *pPage1; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4129 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4130 | int n; /* Number of pages on the freelist */ |
| 4131 | int k; /* Number of leaves on the trunk of the freelist */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4132 | MemPage *pTrunk = 0; |
| 4133 | MemPage *pPrevTrunk = 0; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4134 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4135 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4136 | pPage1 = pBt->pPage1; |
| 4137 | n = get4byte(&pPage1->aData[36]); |
| 4138 | if( n>0 ){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4139 | /* There are pages on the freelist. Reuse one of those pages. */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4140 | Pgno iTrunk; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4141 | u8 searchList = 0; /* If the free-list must be searched for 'nearby' */ |
| 4142 | |
| 4143 | /* If the 'exact' parameter was true and a query of the pointer-map |
| 4144 | ** shows that the page 'nearby' is somewhere on the free-list, then |
| 4145 | ** the entire-list will be searched for that page. |
| 4146 | */ |
| 4147 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 4148 | if( exact && nearby<=pagerPagecount(pBt) ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4149 | u8 eType; |
| 4150 | assert( nearby>0 ); |
| 4151 | assert( pBt->autoVacuum ); |
| 4152 | rc = ptrmapGet(pBt, nearby, &eType, 0); |
| 4153 | if( rc ) return rc; |
| 4154 | if( eType==PTRMAP_FREEPAGE ){ |
| 4155 | searchList = 1; |
| 4156 | } |
| 4157 | *pPgno = nearby; |
| 4158 | } |
| 4159 | #endif |
| 4160 | |
| 4161 | /* Decrement the free-list count by 1. Set iTrunk to the index of the |
| 4162 | ** first free-list trunk page. iPrevTrunk is initially 1. |
| 4163 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4164 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4165 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4166 | put4byte(&pPage1->aData[36], n-1); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4167 | |
| 4168 | /* The code within this loop is run only once if the 'searchList' variable |
| 4169 | ** is not true. Otherwise, it runs once for each trunk-page on the |
| 4170 | ** free-list until the page 'nearby' is located. |
| 4171 | */ |
| 4172 | do { |
| 4173 | pPrevTrunk = pTrunk; |
| 4174 | if( pPrevTrunk ){ |
| 4175 | iTrunk = get4byte(&pPrevTrunk->aData[0]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 4176 | }else{ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4177 | iTrunk = get4byte(&pPage1->aData[32]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 4178 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4179 | rc = sqlite3BtreeGetPage(pBt, iTrunk, &pTrunk, 0); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4180 | if( rc ){ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4181 | pTrunk = 0; |
| 4182 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4183 | } |
| 4184 | |
| 4185 | k = get4byte(&pTrunk->aData[4]); |
| 4186 | if( k==0 && !searchList ){ |
| 4187 | /* The trunk has no leaves and the list is not being searched. |
| 4188 | ** So extract the trunk page itself and use it as the newly |
| 4189 | ** allocated page */ |
| 4190 | assert( pPrevTrunk==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4191 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4192 | if( rc ){ |
| 4193 | goto end_allocate_page; |
| 4194 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4195 | *pPgno = iTrunk; |
| 4196 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 4197 | *ppPage = pTrunk; |
| 4198 | pTrunk = 0; |
| 4199 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
drh | 45b1fac | 2008-07-04 17:52:42 +0000 | [diff] [blame] | 4200 | }else if( k>pBt->usableSize/4 - 2 ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4201 | /* Value of k is out of range. Database corruption */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4202 | rc = SQLITE_CORRUPT_BKPT; |
| 4203 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4204 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4205 | }else if( searchList && nearby==iTrunk ){ |
| 4206 | /* The list is being searched and this trunk page is the page |
| 4207 | ** to allocate, regardless of whether it has leaves. |
| 4208 | */ |
| 4209 | assert( *pPgno==iTrunk ); |
| 4210 | *ppPage = pTrunk; |
| 4211 | searchList = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4212 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4213 | if( rc ){ |
| 4214 | goto end_allocate_page; |
| 4215 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4216 | if( k==0 ){ |
| 4217 | if( !pPrevTrunk ){ |
| 4218 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 4219 | }else{ |
| 4220 | memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4); |
| 4221 | } |
| 4222 | }else{ |
| 4223 | /* The trunk page is required by the caller but it contains |
| 4224 | ** pointers to free-list leaves. The first leaf becomes a trunk |
| 4225 | ** page in this case. |
| 4226 | */ |
| 4227 | MemPage *pNewTrunk; |
| 4228 | Pgno iNewTrunk = get4byte(&pTrunk->aData[8]); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4229 | rc = sqlite3BtreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4230 | if( rc!=SQLITE_OK ){ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4231 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4232 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4233 | rc = sqlite3PagerWrite(pNewTrunk->pDbPage); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4234 | if( rc!=SQLITE_OK ){ |
| 4235 | releasePage(pNewTrunk); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4236 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4237 | } |
| 4238 | memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4); |
| 4239 | put4byte(&pNewTrunk->aData[4], k-1); |
| 4240 | memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4241 | releasePage(pNewTrunk); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4242 | if( !pPrevTrunk ){ |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 4243 | assert( sqlite3PagerIswriteable(pPage1->pDbPage) ); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4244 | put4byte(&pPage1->aData[32], iNewTrunk); |
| 4245 | }else{ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4246 | rc = sqlite3PagerWrite(pPrevTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4247 | if( rc ){ |
| 4248 | goto end_allocate_page; |
| 4249 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4250 | put4byte(&pPrevTrunk->aData[0], iNewTrunk); |
| 4251 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4252 | } |
| 4253 | pTrunk = 0; |
| 4254 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
| 4255 | #endif |
| 4256 | }else{ |
| 4257 | /* Extract a leaf from the trunk */ |
| 4258 | int closest; |
| 4259 | Pgno iPage; |
| 4260 | unsigned char *aData = pTrunk->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4261 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4262 | if( rc ){ |
| 4263 | goto end_allocate_page; |
| 4264 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4265 | if( nearby>0 ){ |
| 4266 | int i, dist; |
| 4267 | closest = 0; |
| 4268 | dist = get4byte(&aData[8]) - nearby; |
| 4269 | if( dist<0 ) dist = -dist; |
| 4270 | for(i=1; i<k; i++){ |
| 4271 | int d2 = get4byte(&aData[8+i*4]) - nearby; |
| 4272 | if( d2<0 ) d2 = -d2; |
| 4273 | if( d2<dist ){ |
| 4274 | closest = i; |
| 4275 | dist = d2; |
| 4276 | } |
| 4277 | } |
| 4278 | }else{ |
| 4279 | closest = 0; |
| 4280 | } |
| 4281 | |
| 4282 | iPage = get4byte(&aData[8+closest*4]); |
| 4283 | if( !searchList || iPage==nearby ){ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 4284 | Pgno nPage; |
shane | 1f9e6aa | 2008-06-09 19:27:11 +0000 | [diff] [blame] | 4285 | *pPgno = iPage; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 4286 | nPage = pagerPagecount(pBt); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 4287 | if( *pPgno>nPage ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4288 | /* Free page off the end of the file */ |
danielk1977 | 43e377a | 2008-05-05 12:09:32 +0000 | [diff] [blame] | 4289 | rc = SQLITE_CORRUPT_BKPT; |
| 4290 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4291 | } |
| 4292 | TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d" |
| 4293 | ": %d more free pages\n", |
| 4294 | *pPgno, closest+1, k, pTrunk->pgno, n-1)); |
| 4295 | if( closest<k-1 ){ |
| 4296 | memcpy(&aData[8+closest*4], &aData[4+k*4], 4); |
| 4297 | } |
| 4298 | put4byte(&aData[4], k-1); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 4299 | assert( sqlite3PagerIswriteable(pTrunk->pDbPage) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4300 | rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 1); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4301 | if( rc==SQLITE_OK ){ |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 4302 | sqlite3PagerDontRollback((*ppPage)->pDbPage); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4303 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 4304 | if( rc!=SQLITE_OK ){ |
| 4305 | releasePage(*ppPage); |
| 4306 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4307 | } |
| 4308 | searchList = 0; |
| 4309 | } |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 4310 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4311 | releasePage(pPrevTrunk); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4312 | pPrevTrunk = 0; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4313 | }while( searchList ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4314 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4315 | /* There are no pages on the freelist, so create a new page at the |
| 4316 | ** end of the file */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 4317 | int nPage = pagerPagecount(pBt); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 4318 | *pPgno = nPage + 1; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4319 | |
| 4320 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 4321 | if( pBt->nTrunc ){ |
| 4322 | /* An incr-vacuum has already run within this transaction. So the |
| 4323 | ** page to allocate is not from the physical end of the file, but |
| 4324 | ** at pBt->nTrunc. |
| 4325 | */ |
| 4326 | *pPgno = pBt->nTrunc+1; |
| 4327 | if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ |
| 4328 | (*pPgno)++; |
| 4329 | } |
| 4330 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 4331 | if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, *pPgno) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4332 | /* If *pPgno refers to a pointer-map page, allocate two new pages |
| 4333 | ** at the end of the file instead of one. The first allocated page |
| 4334 | ** becomes a new pointer-map page, the second is used by the caller. |
| 4335 | */ |
| 4336 | TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno)); |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4337 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4338 | (*pPgno)++; |
drh | 7219043 | 2008-01-31 14:54:43 +0000 | [diff] [blame] | 4339 | if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ (*pPgno)++; } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4340 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 4341 | if( pBt->nTrunc ){ |
| 4342 | pBt->nTrunc = *pPgno; |
| 4343 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4344 | #endif |
| 4345 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4346 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4347 | rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 0); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4348 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4349 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 4350 | if( rc!=SQLITE_OK ){ |
| 4351 | releasePage(*ppPage); |
| 4352 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4353 | TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4354 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4355 | |
| 4356 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4357 | |
| 4358 | end_allocate_page: |
| 4359 | releasePage(pTrunk); |
| 4360 | releasePage(pPrevTrunk); |
danielk1977 | b247c21 | 2008-11-21 09:09:01 +0000 | [diff] [blame] | 4361 | if( rc==SQLITE_OK ){ |
| 4362 | if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){ |
| 4363 | releasePage(*ppPage); |
| 4364 | return SQLITE_CORRUPT_BKPT; |
| 4365 | } |
| 4366 | (*ppPage)->isInit = 0; |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 4367 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4368 | return rc; |
| 4369 | } |
| 4370 | |
| 4371 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4372 | ** Add a page of the database file to the freelist. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4373 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4374 | ** sqlite3PagerUnref() is NOT called for pPage. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4375 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4376 | static int freePage(MemPage *pPage){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4377 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4378 | MemPage *pPage1 = pBt->pPage1; |
| 4379 | int rc, n, k; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4380 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4381 | /* Prepare the page for freeing */ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4382 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4383 | assert( pPage->pgno>1 ); |
| 4384 | pPage->isInit = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4385 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4386 | /* Increment the free page count on pPage1 */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4387 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4388 | if( rc ) return rc; |
| 4389 | n = get4byte(&pPage1->aData[36]); |
| 4390 | put4byte(&pPage1->aData[36], n+1); |
| 4391 | |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4392 | #ifdef SQLITE_SECURE_DELETE |
| 4393 | /* If the SQLITE_SECURE_DELETE compile-time option is enabled, then |
| 4394 | ** always fully overwrite deleted information with zeros. |
| 4395 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4396 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4397 | if( rc ) return rc; |
| 4398 | memset(pPage->aData, 0, pPage->pBt->pageSize); |
| 4399 | #endif |
| 4400 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4401 | /* If the database supports auto-vacuum, write an entry in the pointer-map |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4402 | ** to indicate that the page is free. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4403 | */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 4404 | if( ISAUTOVACUUM ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4405 | rc = ptrmapPut(pBt, pPage->pgno, PTRMAP_FREEPAGE, 0); |
danielk1977 | a64a035 | 2004-11-05 01:45:13 +0000 | [diff] [blame] | 4406 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4407 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4408 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4409 | if( n==0 ){ |
| 4410 | /* This is the first free page */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4411 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4412 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4413 | memset(pPage->aData, 0, 8); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4414 | put4byte(&pPage1->aData[32], pPage->pgno); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4415 | TRACE(("FREE-PAGE: %d first\n", pPage->pgno)); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4416 | }else{ |
| 4417 | /* Other free pages already exist. Retrive the first trunk page |
| 4418 | ** of the freelist and find out how many leaves it has. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4419 | MemPage *pTrunk; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4420 | rc = sqlite3BtreeGetPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk, 0); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4421 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4422 | k = get4byte(&pTrunk->aData[4]); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 4423 | if( k>=pBt->usableSize/4 - 8 ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4424 | /* The trunk is full. Turn the page being freed into a new |
drh | 45b1fac | 2008-07-04 17:52:42 +0000 | [diff] [blame] | 4425 | ** trunk page with no leaves. |
| 4426 | ** |
| 4427 | ** Note that the trunk page is not really full until it contains |
| 4428 | ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have |
| 4429 | ** coded. But due to a coding error in versions of SQLite prior to |
| 4430 | ** 3.6.0, databases with freelist trunk pages holding more than |
| 4431 | ** usableSize/4 - 8 entries will be reported as corrupt. In order |
| 4432 | ** to maintain backwards compatibility with older versions of SQLite, |
| 4433 | ** we will contain to restrict the number of entries to usableSize/4 - 8 |
| 4434 | ** for now. At some point in the future (once everyone has upgraded |
| 4435 | ** to 3.6.0 or later) we should consider fixing the conditional above |
| 4436 | ** to read "usableSize/4-2" instead of "usableSize/4-8". |
| 4437 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4438 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | b9ee493 | 2007-09-07 14:32:06 +0000 | [diff] [blame] | 4439 | if( rc==SQLITE_OK ){ |
| 4440 | put4byte(pPage->aData, pTrunk->pgno); |
| 4441 | put4byte(&pPage->aData[4], 0); |
| 4442 | put4byte(&pPage1->aData[32], pPage->pgno); |
| 4443 | TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", |
| 4444 | pPage->pgno, pTrunk->pgno)); |
| 4445 | } |
| 4446 | }else if( k<0 ){ |
| 4447 | rc = SQLITE_CORRUPT; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4448 | }else{ |
| 4449 | /* Add the newly freed page as a leaf on the current trunk */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4450 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 4451 | if( rc==SQLITE_OK ){ |
| 4452 | put4byte(&pTrunk->aData[4], k+1); |
| 4453 | put4byte(&pTrunk->aData[8+k*4], pPage->pgno); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4454 | #ifndef SQLITE_SECURE_DELETE |
danielk1977 | a1fa00d | 2008-08-27 15:16:33 +0000 | [diff] [blame] | 4455 | rc = sqlite3PagerDontWrite(pPage->pDbPage); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4456 | #endif |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 4457 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4458 | 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] | 4459 | } |
| 4460 | releasePage(pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4461 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4462 | return rc; |
| 4463 | } |
| 4464 | |
| 4465 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4466 | ** Free any overflow pages associated with the given Cell. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4467 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4468 | static int clearCell(MemPage *pPage, unsigned char *pCell){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4469 | BtShared *pBt = pPage->pBt; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4470 | CellInfo info; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4471 | Pgno ovflPgno; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4472 | int rc; |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 4473 | int nOvfl; |
| 4474 | int ovflPageSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4475 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4476 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4477 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4478 | if( info.iOverflow==0 ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4479 | return SQLITE_OK; /* No overflow pages. Return without doing anything */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4480 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4481 | ovflPgno = get4byte(&pCell[info.iOverflow]); |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 4482 | ovflPageSize = pBt->usableSize - 4; |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 4483 | nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize; |
| 4484 | assert( ovflPgno==0 || nOvfl>0 ); |
| 4485 | while( nOvfl-- ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4486 | MemPage *pOvfl; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 4487 | if( ovflPgno==0 || ovflPgno>pagerPagecount(pBt) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 4488 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 4489 | } |
danielk1977 | 8c0a959 | 2007-04-30 16:55:00 +0000 | [diff] [blame] | 4490 | |
| 4491 | rc = getOverflowPage(pBt, ovflPgno, &pOvfl, (nOvfl==0)?0:&ovflPgno); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4492 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4493 | rc = freePage(pOvfl); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4494 | sqlite3PagerUnref(pOvfl->pDbPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 4495 | if( rc ) return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4496 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4497 | return SQLITE_OK; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4498 | } |
| 4499 | |
| 4500 | /* |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4501 | ** Create the byte sequence used to represent a cell on page pPage |
| 4502 | ** and write that byte sequence into pCell[]. Overflow pages are |
| 4503 | ** allocated and filled in as necessary. The calling procedure |
| 4504 | ** is responsible for making sure sufficient space has been allocated |
| 4505 | ** for pCell[]. |
| 4506 | ** |
| 4507 | ** Note that pCell does not necessary need to point to the pPage->aData |
| 4508 | ** area. pCell might point to some temporary storage. The cell will |
| 4509 | ** be constructed in this temporary area then copied into pPage->aData |
| 4510 | ** later. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4511 | */ |
| 4512 | static int fillInCell( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4513 | MemPage *pPage, /* The page that contains the cell */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4514 | unsigned char *pCell, /* Complete text of the cell */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 4515 | const void *pKey, i64 nKey, /* The key */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4516 | const void *pData,int nData, /* The data */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4517 | int nZero, /* Extra zero bytes to append to pData */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4518 | int *pnSize /* Write cell size here */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4519 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4520 | int nPayload; |
drh | 8c6fa9b | 2004-05-26 00:01:53 +0000 | [diff] [blame] | 4521 | const u8 *pSrc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4522 | int nSrc, n, rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4523 | int spaceLeft; |
| 4524 | MemPage *pOvfl = 0; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4525 | MemPage *pToRelease = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4526 | unsigned char *pPrior; |
| 4527 | unsigned char *pPayload; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4528 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4529 | Pgno pgnoOvfl = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4530 | int nHeader; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4531 | CellInfo info; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4532 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4533 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4534 | |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 4535 | /* pPage is not necessarily writeable since pCell might be auxiliary |
| 4536 | ** buffer space that is separate from the pPage buffer area */ |
| 4537 | assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize] |
| 4538 | || sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 4539 | |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4540 | /* Fill in the header. */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4541 | nHeader = 0; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4542 | if( !pPage->leaf ){ |
| 4543 | nHeader += 4; |
| 4544 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4545 | if( pPage->hasData ){ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4546 | nHeader += putVarint(&pCell[nHeader], nData+nZero); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4547 | }else{ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4548 | nData = nZero = 0; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4549 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4550 | nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4551 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4552 | assert( info.nHeader==nHeader ); |
| 4553 | assert( info.nKey==nKey ); |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 4554 | assert( info.nData==(u32)(nData+nZero) ); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4555 | |
| 4556 | /* Fill in the payload */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4557 | nPayload = nData + nZero; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4558 | if( pPage->intKey ){ |
| 4559 | pSrc = pData; |
| 4560 | nSrc = nData; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4561 | nData = 0; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4562 | }else{ |
| 4563 | /* TBD: Perhaps raise SQLITE_CORRUPT if nKey is larger than 31 bits? */ |
| 4564 | nPayload += (int)nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4565 | pSrc = pKey; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4566 | nSrc = (int)nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4567 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4568 | *pnSize = info.nSize; |
| 4569 | spaceLeft = info.nLocal; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4570 | pPayload = &pCell[nHeader]; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4571 | pPrior = &pCell[info.iOverflow]; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4572 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4573 | while( nPayload>0 ){ |
| 4574 | if( spaceLeft==0 ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4575 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4576 | Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */ |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4577 | if( pBt->autoVacuum ){ |
| 4578 | do{ |
| 4579 | pgnoOvfl++; |
| 4580 | } while( |
| 4581 | PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt) |
| 4582 | ); |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4583 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4584 | #endif |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4585 | rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4586 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4587 | /* If the database supports auto-vacuum, and the second or subsequent |
| 4588 | ** overflow page is being allocated, add an entry to the pointer-map |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 4589 | ** for that page now. |
| 4590 | ** |
| 4591 | ** If this is the first overflow page, then write a partial entry |
| 4592 | ** to the pointer-map. If we write nothing to this pointer-map slot, |
| 4593 | ** then the optimistic overflow chain processing in clearCell() |
| 4594 | ** may misinterpret the uninitialised values and delete the |
| 4595 | ** wrong pages from the database. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4596 | */ |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 4597 | if( pBt->autoVacuum && rc==SQLITE_OK ){ |
| 4598 | u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1); |
| 4599 | rc = ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap); |
danielk1977 | 89a4be8 | 2007-05-23 13:34:32 +0000 | [diff] [blame] | 4600 | if( rc ){ |
| 4601 | releasePage(pOvfl); |
| 4602 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4603 | } |
| 4604 | #endif |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4605 | if( rc ){ |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4606 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4607 | return rc; |
| 4608 | } |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 4609 | |
| 4610 | /* If pToRelease is not zero than pPrior points into the data area |
| 4611 | ** of pToRelease. Make sure pToRelease is still writeable. */ |
| 4612 | assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) ); |
| 4613 | |
| 4614 | /* If pPrior is part of the data area of pPage, then make sure pPage |
| 4615 | ** is still writeable */ |
| 4616 | assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize] |
| 4617 | || sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 4618 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4619 | put4byte(pPrior, pgnoOvfl); |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4620 | releasePage(pToRelease); |
| 4621 | pToRelease = pOvfl; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4622 | pPrior = pOvfl->aData; |
| 4623 | put4byte(pPrior, 0); |
| 4624 | pPayload = &pOvfl->aData[4]; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4625 | spaceLeft = pBt->usableSize - 4; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4626 | } |
| 4627 | n = nPayload; |
| 4628 | if( n>spaceLeft ) n = spaceLeft; |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 4629 | |
| 4630 | /* If pToRelease is not zero than pPayload points into the data area |
| 4631 | ** of pToRelease. Make sure pToRelease is still writeable. */ |
| 4632 | assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) ); |
| 4633 | |
| 4634 | /* If pPayload is part of the data area of pPage, then make sure pPage |
| 4635 | ** is still writeable */ |
| 4636 | assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize] |
| 4637 | || sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 4638 | |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4639 | if( nSrc>0 ){ |
| 4640 | if( n>nSrc ) n = nSrc; |
| 4641 | assert( pSrc ); |
| 4642 | memcpy(pPayload, pSrc, n); |
| 4643 | }else{ |
| 4644 | memset(pPayload, 0, n); |
| 4645 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4646 | nPayload -= n; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4647 | pPayload += n; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4648 | pSrc += n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4649 | nSrc -= n; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4650 | spaceLeft -= n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4651 | if( nSrc==0 ){ |
| 4652 | nSrc = nData; |
| 4653 | pSrc = pData; |
| 4654 | } |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4655 | } |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4656 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4657 | return SQLITE_OK; |
| 4658 | } |
| 4659 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4660 | /* |
| 4661 | ** Remove the i-th cell from pPage. This routine effects pPage only. |
| 4662 | ** The cell content is not freed or deallocated. It is assumed that |
| 4663 | ** the cell content has been copied someplace else. This routine just |
| 4664 | ** removes the reference to the cell from pPage. |
| 4665 | ** |
| 4666 | ** "sz" must be the number of bytes in the cell. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4667 | */ |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 4668 | static int dropCell(MemPage *pPage, int idx, int sz){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4669 | int i; /* Loop counter */ |
| 4670 | int pc; /* Offset to cell content of cell being deleted */ |
| 4671 | u8 *data; /* pPage->aData */ |
| 4672 | u8 *ptr; /* Used to move bytes around within data[] */ |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 4673 | int rc; /* The return code */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4674 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4675 | assert( idx>=0 && idx<pPage->nCell ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4676 | assert( sz==cellSize(pPage, idx) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4677 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4678 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4679 | data = pPage->aData; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4680 | ptr = &data[pPage->cellOffset + 2*idx]; |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 4681 | pc = get2byte(ptr); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 4682 | if( (pc<pPage->hdrOffset+6+(pPage->leaf?0:4)) |
| 4683 | || (pc+sz>pPage->pBt->usableSize) ){ |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 4684 | return SQLITE_CORRUPT_BKPT; |
| 4685 | } |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 4686 | rc = freeSpace(pPage, pc, sz); |
| 4687 | if( rc!=SQLITE_OK ){ |
| 4688 | return rc; |
| 4689 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4690 | for(i=idx+1; i<pPage->nCell; i++, ptr+=2){ |
| 4691 | ptr[0] = ptr[2]; |
| 4692 | ptr[1] = ptr[3]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4693 | } |
| 4694 | pPage->nCell--; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4695 | put2byte(&data[pPage->hdrOffset+3], pPage->nCell); |
| 4696 | pPage->nFree += 2; |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 4697 | return SQLITE_OK; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4698 | } |
| 4699 | |
| 4700 | /* |
| 4701 | ** Insert a new cell on pPage at cell index "i". pCell points to the |
| 4702 | ** content of the cell. |
| 4703 | ** |
| 4704 | ** 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] | 4705 | ** will not fit, then make a copy of the cell content into pTemp if |
| 4706 | ** pTemp is not null. Regardless of pTemp, allocate a new entry |
| 4707 | ** in pPage->aOvfl[] and make it point to the cell content (either |
| 4708 | ** in pTemp or the original pCell) and also record its index. |
| 4709 | ** Allocating a new entry in pPage->aCell[] implies that |
| 4710 | ** pPage->nOverflow is incremented. |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4711 | ** |
| 4712 | ** If nSkip is non-zero, then do not copy the first nSkip bytes of the |
| 4713 | ** cell. The caller will overwrite them after this function returns. If |
drh | 4b238df | 2005-01-08 15:43:18 +0000 | [diff] [blame] | 4714 | ** 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] | 4715 | ** (but pCell+nSkip is always valid). |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4716 | */ |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4717 | static int insertCell( |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4718 | MemPage *pPage, /* Page into which we are copying */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4719 | int i, /* New cell becomes the i-th cell of the page */ |
| 4720 | u8 *pCell, /* Content of the new cell */ |
| 4721 | int sz, /* Bytes of content in pCell */ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4722 | u8 *pTemp, /* Temp storage space for pCell, if needed */ |
| 4723 | u8 nSkip /* Do not write the first nSkip bytes of the cell */ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4724 | ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4725 | int idx; /* Where to write new cell content in data[] */ |
| 4726 | int j; /* Loop counter */ |
| 4727 | int top; /* First byte of content for any cell in data[] */ |
| 4728 | int end; /* First byte past the last cell pointer in data[] */ |
| 4729 | int ins; /* Index in data[] where new cell pointer is inserted */ |
| 4730 | int hdr; /* Offset into data[] of the page header */ |
| 4731 | int cellOffset; /* Address of first cell pointer in data[] */ |
| 4732 | u8 *data; /* The content of the whole page */ |
| 4733 | u8 *ptr; /* Used for moving information around in data[] */ |
| 4734 | |
| 4735 | assert( i>=0 && i<=pPage->nCell+pPage->nOverflow ); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4736 | assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=5460 ); |
| 4737 | assert( pPage->nOverflow<=ArraySize(pPage->aOvfl) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4738 | assert( sz==cellSizePtr(pPage, pCell) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4739 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4740 | if( pPage->nOverflow || sz+2>pPage->nFree ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4741 | if( pTemp ){ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4742 | memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4743 | pCell = pTemp; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4744 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4745 | j = pPage->nOverflow++; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 4746 | assert( j<(int)(sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0])) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4747 | pPage->aOvfl[j].pCell = pCell; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4748 | pPage->aOvfl[j].idx = (u16)i; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4749 | pPage->nFree = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4750 | }else{ |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4751 | int rc = sqlite3PagerWrite(pPage->pDbPage); |
| 4752 | if( rc!=SQLITE_OK ){ |
| 4753 | return rc; |
| 4754 | } |
| 4755 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4756 | data = pPage->aData; |
| 4757 | hdr = pPage->hdrOffset; |
| 4758 | top = get2byte(&data[hdr+5]); |
| 4759 | cellOffset = pPage->cellOffset; |
| 4760 | end = cellOffset + 2*pPage->nCell + 2; |
| 4761 | ins = cellOffset + 2*i; |
| 4762 | if( end > top - sz ){ |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 4763 | rc = defragmentPage(pPage); |
| 4764 | if( rc!=SQLITE_OK ){ |
| 4765 | return rc; |
| 4766 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4767 | top = get2byte(&data[hdr+5]); |
| 4768 | assert( end + sz <= top ); |
| 4769 | } |
| 4770 | idx = allocateSpace(pPage, sz); |
| 4771 | assert( idx>0 ); |
| 4772 | assert( end <= get2byte(&data[hdr+5]) ); |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 4773 | if (idx+sz > pPage->pBt->usableSize) { |
shane | 34ac18d | 2008-11-11 22:18:20 +0000 | [diff] [blame] | 4774 | return SQLITE_CORRUPT_BKPT; |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 4775 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4776 | pPage->nCell++; |
| 4777 | pPage->nFree -= 2; |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4778 | memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4779 | for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){ |
| 4780 | ptr[0] = ptr[-2]; |
| 4781 | ptr[1] = ptr[-1]; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4782 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4783 | put2byte(&data[ins], idx); |
| 4784 | put2byte(&data[hdr+3], pPage->nCell); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4785 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4786 | if( pPage->pBt->autoVacuum ){ |
| 4787 | /* The cell may contain a pointer to an overflow page. If so, write |
| 4788 | ** the entry for the overflow page into the pointer map. |
| 4789 | */ |
| 4790 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4791 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 4792 | assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4793 | if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ |
| 4794 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4795 | rc = ptrmapPut(pPage->pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4796 | if( rc!=SQLITE_OK ) return rc; |
| 4797 | } |
| 4798 | } |
| 4799 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4800 | } |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4801 | |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4802 | return SQLITE_OK; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4803 | } |
| 4804 | |
| 4805 | /* |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4806 | ** Add a list of cells to a page. The page should be initially empty. |
| 4807 | ** The cells are guaranteed to fit on the page. |
| 4808 | */ |
| 4809 | static void assemblePage( |
| 4810 | MemPage *pPage, /* The page to be assemblied */ |
| 4811 | int nCell, /* The number of cells to add to this page */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4812 | u8 **apCell, /* Pointers to cell bodies */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 4813 | u16 *aSize /* Sizes of the cells */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4814 | ){ |
| 4815 | int i; /* Loop counter */ |
| 4816 | int totalSize; /* Total size of all cells */ |
| 4817 | int hdr; /* Index of page header */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4818 | int cellptr; /* Address of next cell pointer */ |
| 4819 | int cellbody; /* Address of next cell body */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4820 | u8 *data; /* Data for the page */ |
| 4821 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4822 | assert( pPage->nOverflow==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4823 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4824 | assert( nCell>=0 && nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=5460 ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4825 | totalSize = 0; |
| 4826 | for(i=0; i<nCell; i++){ |
| 4827 | totalSize += aSize[i]; |
| 4828 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4829 | assert( totalSize+2*nCell<=pPage->nFree ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4830 | assert( pPage->nCell==0 ); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 4831 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4832 | cellptr = pPage->cellOffset; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4833 | data = pPage->aData; |
| 4834 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4835 | put2byte(&data[hdr+3], nCell); |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 4836 | if( nCell ){ |
| 4837 | cellbody = allocateSpace(pPage, totalSize); |
| 4838 | assert( cellbody>0 ); |
| 4839 | assert( pPage->nFree >= 2*nCell ); |
| 4840 | pPage->nFree -= 2*nCell; |
| 4841 | for(i=0; i<nCell; i++){ |
| 4842 | put2byte(&data[cellptr], cellbody); |
| 4843 | memcpy(&data[cellbody], apCell[i], aSize[i]); |
| 4844 | cellptr += 2; |
| 4845 | cellbody += aSize[i]; |
| 4846 | } |
| 4847 | assert( cellbody==pPage->pBt->usableSize ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4848 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4849 | pPage->nCell = (u16)nCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4850 | } |
| 4851 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4852 | /* |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4853 | ** The following parameters determine how many adjacent pages get involved |
| 4854 | ** in a balancing operation. NN is the number of neighbors on either side |
| 4855 | ** of the page that participate in the balancing operation. NB is the |
| 4856 | ** total number of pages that participate, including the target page and |
| 4857 | ** NN neighbors on either side. |
| 4858 | ** |
| 4859 | ** The minimum value of NN is 1 (of course). Increasing NN above 1 |
| 4860 | ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance |
| 4861 | ** in exchange for a larger degradation in INSERT and UPDATE performance. |
| 4862 | ** The value of NN appears to give the best results overall. |
| 4863 | */ |
| 4864 | #define NN 1 /* Number of neighbors on either side of pPage */ |
| 4865 | #define NB (NN*2+1) /* Total pages involved in the balance */ |
| 4866 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4867 | /* Forward reference */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4868 | static int balance(BtCursor*, int); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4869 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4870 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 4871 | /* |
| 4872 | ** This version of balance() handles the common special case where |
| 4873 | ** a new entry is being inserted on the extreme right-end of the |
| 4874 | ** tree, in other words, when the new entry will become the largest |
| 4875 | ** entry in the tree. |
| 4876 | ** |
| 4877 | ** Instead of trying balance the 3 right-most leaf pages, just add |
| 4878 | ** a new page to the right-hand side and put the one new entry in |
| 4879 | ** that page. This leaves the right side of the tree somewhat |
| 4880 | ** unbalanced. But odds are that we will be inserting new entries |
| 4881 | ** at the end soon afterwards so the nearly empty page will quickly |
| 4882 | ** fill up. On average. |
| 4883 | ** |
| 4884 | ** pPage is the leaf page which is the right-most page in the tree. |
| 4885 | ** pParent is its parent. pPage must have a single overflow entry |
| 4886 | ** which is also the right-most entry on the page. |
| 4887 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4888 | static int balance_quick(BtCursor *pCur){ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4889 | int rc; |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 4890 | MemPage *pNew = 0; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4891 | Pgno pgnoNew; |
| 4892 | u8 *pCell; |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 4893 | u16 szCell; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4894 | CellInfo info; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4895 | MemPage *pPage = pCur->apPage[pCur->iPage]; |
| 4896 | MemPage *pParent = pCur->apPage[pCur->iPage-1]; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4897 | BtShared *pBt = pPage->pBt; |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4898 | int parentIdx = pParent->nCell; /* pParent new divider cell index */ |
| 4899 | int parentSize; /* Size of new divider cell */ |
| 4900 | u8 parentCell[64]; /* Space for the new divider cell */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4901 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4902 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4903 | |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4904 | /* Allocate a new page. Insert the overflow cell from pPage |
| 4905 | ** into it. Then remove the overflow cell from pPage. |
| 4906 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4907 | rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0); |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 4908 | if( rc==SQLITE_OK ){ |
| 4909 | pCell = pPage->aOvfl[0].pCell; |
| 4910 | szCell = cellSizePtr(pPage, pCell); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 4911 | assert( sqlite3PagerIswriteable(pNew->pDbPage) ); |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 4912 | zeroPage(pNew, pPage->aData[0]); |
| 4913 | assemblePage(pNew, 1, &pCell, &szCell); |
| 4914 | pPage->nOverflow = 0; |
| 4915 | |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 4916 | /* pPage is currently the right-child of pParent. Change this |
| 4917 | ** so that the right-child is the new page allocated above and |
| 4918 | ** pPage is the next-to-right child. |
| 4919 | ** |
| 4920 | ** Ignore the return value of the call to fillInCell(). fillInCell() |
| 4921 | ** may only return other than SQLITE_OK if it is required to allocate |
| 4922 | ** one or more overflow pages. Since an internal table B-Tree cell |
| 4923 | ** may never spill over onto an overflow page (it is a maximum of |
| 4924 | ** 13 bytes in size), it is not neccessary to check the return code. |
| 4925 | ** |
| 4926 | ** Similarly, the insertCell() function cannot fail if the page |
| 4927 | ** being inserted into is already writable and the cell does not |
| 4928 | ** contain an overflow pointer. So ignore this return code too. |
| 4929 | */ |
| 4930 | assert( pPage->nCell>0 ); |
| 4931 | pCell = findCell(pPage, pPage->nCell-1); |
| 4932 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
| 4933 | fillInCell(pParent, parentCell, 0, info.nKey, 0, 0, 0, &parentSize); |
| 4934 | assert( parentSize<64 ); |
| 4935 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
| 4936 | insertCell(pParent, parentIdx, parentCell, parentSize, 0, 4); |
| 4937 | put4byte(findOverflowCell(pParent,parentIdx), pPage->pgno); |
| 4938 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew); |
| 4939 | |
| 4940 | /* If this is an auto-vacuum database, update the pointer map |
| 4941 | ** with entries for the new page, and any pointer from the |
| 4942 | ** cell on the page to an overflow page. |
| 4943 | */ |
| 4944 | if( ISAUTOVACUUM ){ |
| 4945 | rc = ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno); |
| 4946 | if( rc==SQLITE_OK ){ |
| 4947 | rc = ptrmapPutOvfl(pNew, 0); |
| 4948 | } |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4949 | } |
danielk1977 | e08a3c4 | 2008-09-18 18:17:03 +0000 | [diff] [blame] | 4950 | |
| 4951 | /* Release the reference to the new page. */ |
| 4952 | releasePage(pNew); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4953 | } |
| 4954 | |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 4955 | /* At this point the pPage->nFree variable is not set correctly with |
| 4956 | ** respect to the content of the page (because it was set to 0 by |
| 4957 | ** insertCell). So call sqlite3BtreeInitPage() to make sure it is |
| 4958 | ** correct. |
| 4959 | ** |
| 4960 | ** This has to be done even if an error will be returned. Normally, if |
| 4961 | ** an error occurs during tree balancing, the contents of MemPage are |
| 4962 | ** not important, as they will be recalculated when the page is rolled |
| 4963 | ** back. But here, in balance_quick(), it is possible that pPage has |
| 4964 | ** not yet been marked dirty or written into the journal file. Therefore |
| 4965 | ** it will not be rolled back and so it is important to make sure that |
| 4966 | ** the page data and contents of MemPage are consistent. |
| 4967 | */ |
| 4968 | pPage->isInit = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4969 | sqlite3BtreeInitPage(pPage); |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 4970 | |
danielk1977 | e08a3c4 | 2008-09-18 18:17:03 +0000 | [diff] [blame] | 4971 | /* If everything else succeeded, balance the parent page, in |
| 4972 | ** case the divider cell inserted caused it to become overfull. |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4973 | */ |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 4974 | if( rc==SQLITE_OK ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4975 | releasePage(pPage); |
| 4976 | pCur->iPage--; |
| 4977 | rc = balance(pCur, 0); |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 4978 | } |
| 4979 | return rc; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4980 | } |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4981 | #endif /* SQLITE_OMIT_QUICKBALANCE */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4982 | |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4983 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4984 | ** This routine redistributes Cells on pPage and up to NN*2 siblings |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4985 | ** 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] | 4986 | ** Usually NN siblings on either side of pPage is used in the balancing, |
| 4987 | ** though more siblings might come from one side if pPage is the first |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4988 | ** 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] | 4989 | ** (something which can only happen if pPage is the root page or a |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4990 | ** child of root) then all available siblings participate in the balancing. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4991 | ** |
drh | 0c6cc4e | 2004-06-15 02:13:26 +0000 | [diff] [blame] | 4992 | ** The number of siblings of pPage might be increased or decreased by one or |
| 4993 | ** 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] | 4994 | ** is special and is allowed to be nearly empty. If pPage is |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4995 | ** the root page, then the depth of the tree might be increased |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4996 | ** or decreased by one, as necessary, to keep the root page from being |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4997 | ** overfull or completely empty. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4998 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4999 | ** Note that when this routine is called, some of the Cells on pPage |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5000 | ** might not actually be stored in pPage->aData[]. This can happen |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5001 | ** 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] | 5002 | ** make sure all Cells for pPage once again fit in pPage->aData[]. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5003 | ** |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5004 | ** In the course of balancing the siblings of pPage, the parent of pPage |
| 5005 | ** might become overfull or underfull. If that happens, then this routine |
| 5006 | ** is called recursively on the parent. |
| 5007 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5008 | ** If this routine fails for any reason, it might leave the database |
| 5009 | ** in a corrupted state. So if this routine fails, the database should |
| 5010 | ** be rolled back. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5011 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5012 | static int balance_nonroot(BtCursor *pCur){ |
| 5013 | MemPage *pPage; /* The over or underfull page to balance */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5014 | MemPage *pParent; /* The parent of pPage */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5015 | BtShared *pBt; /* The whole database */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5016 | int nCell = 0; /* Number of cells in apCell[] */ |
| 5017 | int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5018 | int nOld; /* Number of pages in apOld[] */ |
| 5019 | int nNew; /* Number of pages in apNew[] */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5020 | int nDiv; /* Number of cells in apDiv[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5021 | int i, j, k; /* Loop counters */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5022 | int idx; /* Index of pPage in pParent->aCell[] */ |
| 5023 | int nxDiv; /* Next divider slot in pParent->aCell[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5024 | int rc; /* The return code */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5025 | int leafCorrection; /* 4 if pPage is a leaf. 0 if not */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5026 | int leafData; /* True if pPage is a leaf of a LEAFDATA tree */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5027 | int usableSpace; /* Bytes in pPage beyond the header */ |
| 5028 | int pageFlags; /* Value of pPage->aData[0] */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5029 | int subtotal; /* Subtotal of bytes in cells on one page */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5030 | int iSpace1 = 0; /* First unused byte of aSpace1[] */ |
| 5031 | int iSpace2 = 0; /* First unused byte of aSpace2[] */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5032 | int szScratch; /* Size of scratch memory requested */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5033 | MemPage *apOld[NB]; /* pPage and up to two siblings */ |
| 5034 | Pgno pgnoOld[NB]; /* Page numbers for each page in apOld[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5035 | MemPage *apCopy[NB]; /* Private copies of apOld[] pages */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5036 | MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ |
| 5037 | Pgno pgnoNew[NB+2]; /* Page numbers for each page in apNew[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5038 | u8 *apDiv[NB]; /* Divider cells in pParent */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5039 | int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */ |
| 5040 | int szNew[NB+2]; /* Combined size of cells place on i-th page */ |
danielk1977 | 50f059b | 2005-03-29 02:54:03 +0000 | [diff] [blame] | 5041 | u8 **apCell = 0; /* All cells begin balanced */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5042 | u16 *szCell; /* Local size of all cells in apCell[] */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5043 | u8 *aCopy[NB]; /* Space for holding data of apCopy[] */ |
| 5044 | u8 *aSpace1; /* Space for copies of dividers cells before balance */ |
| 5045 | u8 *aSpace2 = 0; /* Space for overflow dividers cells after balance */ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5046 | u8 *aFrom = 0; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5047 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5048 | pPage = pCur->apPage[pCur->iPage]; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5049 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 5050 | VVA_ONLY( pCur->pagesShuffled = 1 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5051 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5052 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5053 | ** Find the parent page. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5054 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5055 | assert( pCur->iPage>0 ); |
| 5056 | assert( pPage->isInit ); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 5057 | assert( sqlite3PagerIswriteable(pPage->pDbPage) || pPage->nOverflow==1 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5058 | pBt = pPage->pBt; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5059 | pParent = pCur->apPage[pCur->iPage-1]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5060 | assert( pParent ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5061 | if( SQLITE_OK!=(rc = sqlite3PagerWrite(pParent->pDbPage)) ){ |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 5062 | return rc; |
| 5063 | } |
danielk1977 | 474b7cc | 2008-07-09 11:49:46 +0000 | [diff] [blame] | 5064 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5065 | TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno)); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5066 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 5067 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 5068 | /* |
| 5069 | ** A special case: If a new entry has just been inserted into a |
| 5070 | ** 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] | 5071 | ** 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] | 5072 | ** largest key) then use the special balance_quick() routine for |
| 5073 | ** balancing. balance_quick() is much faster and results in a tighter |
| 5074 | ** packing of data in the common case. |
| 5075 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5076 | if( pPage->leaf && |
| 5077 | pPage->intKey && |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5078 | pPage->nOverflow==1 && |
| 5079 | pPage->aOvfl[0].idx==pPage->nCell && |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5080 | pParent->pgno!=1 && |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5081 | get4byte(&pParent->aData[pParent->hdrOffset+8])==pPage->pgno |
| 5082 | ){ |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 5083 | assert( pPage->intKey ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5084 | /* |
| 5085 | ** TODO: Check the siblings to the left of pPage. It may be that |
| 5086 | ** they are not full and no new page is required. |
| 5087 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5088 | return balance_quick(pCur); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5089 | } |
| 5090 | #endif |
| 5091 | |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 5092 | if( SQLITE_OK!=(rc = sqlite3PagerWrite(pPage->pDbPage)) ){ |
| 5093 | return rc; |
| 5094 | } |
| 5095 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5096 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5097 | ** Find the cell in the parent page whose left child points back |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5098 | ** to pPage. The "idx" variable is the index of that cell. If pPage |
| 5099 | ** is the rightmost child of pParent then set idx to pParent->nCell |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5100 | */ |
danielk1977 | bf93c56 | 2008-09-29 15:53:25 +0000 | [diff] [blame] | 5101 | idx = pCur->aiIdx[pCur->iPage-1]; |
| 5102 | assertParentIndex(pParent, idx, pPage->pgno); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5103 | |
| 5104 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5105 | ** Initialize variables so that it will be safe to jump |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 5106 | ** directly to balance_cleanup at any moment. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5107 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5108 | nOld = nNew = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5109 | |
| 5110 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5111 | ** Find sibling pages to pPage and the cells in pParent that divide |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5112 | ** the siblings. An attempt is made to find NN siblings on either |
| 5113 | ** side of pPage. More siblings are taken from one side, however, if |
| 5114 | ** pPage there are fewer than NN siblings on the other side. If pParent |
| 5115 | ** has NB or fewer children then all children of pParent are taken. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5116 | */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5117 | nxDiv = idx - NN; |
| 5118 | if( nxDiv + NB > pParent->nCell ){ |
| 5119 | nxDiv = pParent->nCell - NB + 1; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5120 | } |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5121 | if( nxDiv<0 ){ |
| 5122 | nxDiv = 0; |
| 5123 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5124 | nDiv = 0; |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5125 | for(i=0, k=nxDiv; i<NB; i++, k++){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5126 | if( k<pParent->nCell ){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5127 | apDiv[i] = findCell(pParent, k); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5128 | nDiv++; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5129 | assert( !pParent->leaf ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5130 | pgnoOld[i] = get4byte(apDiv[i]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5131 | }else if( k==pParent->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5132 | pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+8]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5133 | }else{ |
| 5134 | break; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5135 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5136 | rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i]); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5137 | if( rc ) goto balance_cleanup; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5138 | /* apOld[i]->idxParent = k; */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5139 | apCopy[i] = 0; |
| 5140 | assert( i==nOld ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5141 | nOld++; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5142 | nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5143 | } |
| 5144 | |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5145 | /* Make nMaxCells a multiple of 4 in order to preserve 8-byte |
drh | 8d97f1f | 2005-05-05 18:14:13 +0000 | [diff] [blame] | 5146 | ** alignment */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5147 | nMaxCells = (nMaxCells + 3)&~3; |
drh | 8d97f1f | 2005-05-05 18:14:13 +0000 | [diff] [blame] | 5148 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5149 | /* |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5150 | ** Allocate space for memory structures |
| 5151 | */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5152 | szScratch = |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5153 | nMaxCells*sizeof(u8*) /* apCell */ |
| 5154 | + nMaxCells*sizeof(u16) /* szCell */ |
| 5155 | + (ROUND8(sizeof(MemPage))+pBt->pageSize)*NB /* aCopy */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5156 | + pBt->pageSize /* aSpace1 */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5157 | + (ISAUTOVACUUM ? nMaxCells : 0); /* aFrom */ |
| 5158 | apCell = sqlite3ScratchMalloc( szScratch ); |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5159 | if( apCell==0 ){ |
| 5160 | rc = SQLITE_NOMEM; |
| 5161 | goto balance_cleanup; |
| 5162 | } |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5163 | szCell = (u16*)&apCell[nMaxCells]; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5164 | aCopy[0] = (u8*)&szCell[nMaxCells]; |
drh | 66e8008 | 2008-12-16 13:46:29 +0000 | [diff] [blame] | 5165 | assert( ((aCopy[0] - (u8*)0) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5166 | for(i=1; i<NB; i++){ |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 5167 | aCopy[i] = &aCopy[i-1][pBt->pageSize+ROUND8(sizeof(MemPage))]; |
drh | 66e8008 | 2008-12-16 13:46:29 +0000 | [diff] [blame] | 5168 | assert( ((aCopy[i] - (u8*)0) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5169 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5170 | aSpace1 = &aCopy[NB-1][pBt->pageSize+ROUND8(sizeof(MemPage))]; |
drh | 66e8008 | 2008-12-16 13:46:29 +0000 | [diff] [blame] | 5171 | assert( ((aSpace1 - (u8*)0) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5172 | if( ISAUTOVACUUM ){ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5173 | aFrom = &aSpace1[pBt->pageSize]; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5174 | } |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5175 | aSpace2 = sqlite3PageMalloc(pBt->pageSize); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5176 | if( aSpace2==0 ){ |
| 5177 | rc = SQLITE_NOMEM; |
| 5178 | goto balance_cleanup; |
| 5179 | } |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5180 | |
| 5181 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5182 | ** Make copies of the content of pPage and its siblings into aOld[]. |
| 5183 | ** The rest of this function will use data from the copies rather |
| 5184 | ** that the original pages since the original pages will be in the |
| 5185 | ** process of being overwritten. |
| 5186 | */ |
| 5187 | for(i=0; i<nOld; i++){ |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 5188 | MemPage *p = apCopy[i] = (MemPage*)aCopy[i]; |
| 5189 | memcpy(p, apOld[i], sizeof(MemPage)); |
| 5190 | p->aData = (void*)&p[1]; |
| 5191 | memcpy(p->aData, apOld[i]->aData, pBt->pageSize); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5192 | } |
| 5193 | |
| 5194 | /* |
| 5195 | ** Load pointers to all cells on sibling pages and the divider cells |
| 5196 | ** into the local apCell[] array. Make copies of the divider cells |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5197 | ** into space obtained form aSpace1[] and remove the the divider Cells |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5198 | ** from pParent. |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5199 | ** |
| 5200 | ** If the siblings are on leaf pages, then the child pointers of the |
| 5201 | ** divider cells are stripped from the cells before they are copied |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5202 | ** into aSpace1[]. In this way, all cells in apCell[] are without |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5203 | ** child pointers. If siblings are not leaves, then all cell in |
| 5204 | ** apCell[] include child pointers. Either way, all cells in apCell[] |
| 5205 | ** are alike. |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5206 | ** |
| 5207 | ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf. |
| 5208 | ** leafData: 1 if pPage holds key+data and pParent holds only keys. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5209 | */ |
| 5210 | nCell = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5211 | leafCorrection = pPage->leaf*4; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 5212 | leafData = pPage->hasData; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5213 | for(i=0; i<nOld; i++){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5214 | MemPage *pOld = apCopy[i]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5215 | int limit = pOld->nCell+pOld->nOverflow; |
| 5216 | for(j=0; j<limit; j++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5217 | assert( nCell<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5218 | apCell[nCell] = findOverflowCell(pOld, j); |
| 5219 | szCell[nCell] = cellSizePtr(pOld, apCell[nCell]); |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5220 | if( ISAUTOVACUUM ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5221 | int a; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 5222 | aFrom[nCell] = (u8)i; assert( i>=0 && i<6 ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5223 | for(a=0; a<pOld->nOverflow; a++){ |
| 5224 | if( pOld->aOvfl[a].pCell==apCell[nCell] ){ |
| 5225 | aFrom[nCell] = 0xFF; |
| 5226 | break; |
| 5227 | } |
| 5228 | } |
| 5229 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5230 | nCell++; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5231 | } |
| 5232 | if( i<nOld-1 ){ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5233 | u16 sz = cellSizePtr(pParent, apDiv[i]); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5234 | if( leafData ){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5235 | /* With the LEAFDATA flag, pParent cells hold only INTKEYs that |
| 5236 | ** are duplicates of keys on the child pages. We need to remove |
| 5237 | ** the divider cells from pParent, but the dividers cells are not |
| 5238 | ** added to apCell[] because they are duplicates of child cells. |
| 5239 | */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5240 | dropCell(pParent, nxDiv, sz); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5241 | }else{ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5242 | u8 *pTemp; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5243 | assert( nCell<nMaxCells ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5244 | szCell[nCell] = sz; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5245 | pTemp = &aSpace1[iSpace1]; |
| 5246 | iSpace1 += sz; |
| 5247 | assert( sz<=pBt->pageSize/4 ); |
| 5248 | assert( iSpace1<=pBt->pageSize ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5249 | memcpy(pTemp, apDiv[i], sz); |
| 5250 | apCell[nCell] = pTemp+leafCorrection; |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5251 | if( ISAUTOVACUUM ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5252 | aFrom[nCell] = 0xFF; |
| 5253 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5254 | dropCell(pParent, nxDiv, sz); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 5255 | assert( leafCorrection==0 || leafCorrection==4 ); |
| 5256 | szCell[nCell] -= (u16)leafCorrection; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5257 | assert( get4byte(pTemp)==pgnoOld[i] ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5258 | if( !pOld->leaf ){ |
| 5259 | assert( leafCorrection==0 ); |
| 5260 | /* The right pointer of the child page pOld becomes the left |
| 5261 | ** pointer of the divider cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5262 | memcpy(apCell[nCell], &pOld->aData[pOld->hdrOffset+8], 4); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5263 | }else{ |
| 5264 | assert( leafCorrection==4 ); |
danielk1977 | 39c9604 | 2007-05-12 10:41:47 +0000 | [diff] [blame] | 5265 | if( szCell[nCell]<4 ){ |
| 5266 | /* Do not allow any cells smaller than 4 bytes. */ |
| 5267 | szCell[nCell] = 4; |
| 5268 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5269 | } |
| 5270 | nCell++; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5271 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5272 | } |
| 5273 | } |
| 5274 | |
| 5275 | /* |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5276 | ** Figure out the number of pages needed to hold all nCell cells. |
| 5277 | ** Store this number in "k". Also compute szNew[] which is the total |
| 5278 | ** 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] | 5279 | ** in apCell[] of the cell that divides page i from page i+1. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5280 | ** cntNew[k] should equal nCell. |
| 5281 | ** |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5282 | ** Values computed by this block: |
| 5283 | ** |
| 5284 | ** k: The total number of sibling pages |
| 5285 | ** szNew[i]: Spaced used on the i-th sibling page. |
| 5286 | ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to |
| 5287 | ** the right of the i-th sibling page. |
| 5288 | ** usableSpace: Number of bytes of space available on each sibling. |
| 5289 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5290 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5291 | usableSpace = pBt->usableSize - 12 + leafCorrection; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5292 | for(subtotal=k=i=0; i<nCell; i++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5293 | assert( i<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5294 | subtotal += szCell[i] + 2; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5295 | if( subtotal > usableSpace ){ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5296 | szNew[k] = subtotal - szCell[i]; |
| 5297 | cntNew[k] = i; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5298 | if( leafData ){ i--; } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5299 | subtotal = 0; |
| 5300 | k++; |
| 5301 | } |
| 5302 | } |
| 5303 | szNew[k] = subtotal; |
| 5304 | cntNew[k] = nCell; |
| 5305 | k++; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5306 | |
| 5307 | /* |
| 5308 | ** The packing computed by the previous block is biased toward the siblings |
| 5309 | ** on the left side. The left siblings are always nearly full, while the |
| 5310 | ** right-most sibling might be nearly empty. This block of code attempts |
| 5311 | ** to adjust the packing of siblings to get a better balance. |
| 5312 | ** |
| 5313 | ** This adjustment is more than an optimization. The packing above might |
| 5314 | ** be so out of balance as to be illegal. For example, the right-most |
| 5315 | ** sibling might be completely empty. This adjustment is not optional. |
| 5316 | */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5317 | for(i=k-1; i>0; i--){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5318 | int szRight = szNew[i]; /* Size of sibling on the right */ |
| 5319 | int szLeft = szNew[i-1]; /* Size of sibling on the left */ |
| 5320 | int r; /* Index of right-most cell in left sibling */ |
| 5321 | int d; /* Index of first cell to the left of right sibling */ |
| 5322 | |
| 5323 | r = cntNew[i-1] - 1; |
| 5324 | d = r + 1 - leafData; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5325 | assert( d<nMaxCells ); |
| 5326 | assert( r<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5327 | while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){ |
| 5328 | szRight += szCell[d] + 2; |
| 5329 | szLeft -= szCell[r] + 2; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5330 | cntNew[i-1]--; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5331 | r = cntNew[i-1] - 1; |
| 5332 | d = r + 1 - leafData; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5333 | } |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5334 | szNew[i] = szRight; |
| 5335 | szNew[i-1] = szLeft; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5336 | } |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 5337 | |
| 5338 | /* Either we found one or more cells (cntnew[0])>0) or we are the |
| 5339 | ** a virtual root page. A virtual root page is when the real root |
| 5340 | ** page is page 1 and we are the only child of that page. |
| 5341 | */ |
| 5342 | assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5343 | |
| 5344 | /* |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5345 | ** Allocate k new pages. Reuse old pages where possible. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5346 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5347 | assert( pPage->pgno>1 ); |
| 5348 | pageFlags = pPage->aData[0]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5349 | for(i=0; i<k; i++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5350 | MemPage *pNew; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5351 | if( i<nOld ){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5352 | pNew = apNew[i] = apOld[i]; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5353 | pgnoNew[i] = pgnoOld[i]; |
| 5354 | apOld[i] = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5355 | rc = sqlite3PagerWrite(pNew->pDbPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 5356 | nNew++; |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 5357 | if( rc ) goto balance_cleanup; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5358 | }else{ |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 5359 | assert( i>0 ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5360 | rc = allocateBtreePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5361 | if( rc ) goto balance_cleanup; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5362 | apNew[i] = pNew; |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 5363 | nNew++; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5364 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5365 | } |
| 5366 | |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5367 | /* Free any old pages that were not reused as new pages. |
| 5368 | */ |
| 5369 | while( i<nOld ){ |
| 5370 | rc = freePage(apOld[i]); |
| 5371 | if( rc ) goto balance_cleanup; |
| 5372 | releasePage(apOld[i]); |
| 5373 | apOld[i] = 0; |
| 5374 | i++; |
| 5375 | } |
| 5376 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5377 | /* |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5378 | ** Put the new pages in accending order. This helps to |
| 5379 | ** keep entries in the disk file in order so that a scan |
| 5380 | ** of the table is a linear scan through the file. That |
| 5381 | ** in turn helps the operating system to deliver pages |
| 5382 | ** from the disk more rapidly. |
| 5383 | ** |
| 5384 | ** An O(n^2) insertion sort algorithm is used, but since |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5385 | ** n is never more than NB (a small constant), that should |
| 5386 | ** not be a problem. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5387 | ** |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5388 | ** When NB==3, this one optimization makes the database |
| 5389 | ** about 25% faster for large insertions and deletions. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5390 | */ |
| 5391 | for(i=0; i<k-1; i++){ |
| 5392 | int minV = pgnoNew[i]; |
| 5393 | int minI = i; |
| 5394 | for(j=i+1; j<k; j++){ |
drh | 7d02cb7 | 2003-06-04 16:24:39 +0000 | [diff] [blame] | 5395 | if( pgnoNew[j]<(unsigned)minV ){ |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5396 | minI = j; |
| 5397 | minV = pgnoNew[j]; |
| 5398 | } |
| 5399 | } |
| 5400 | if( minI>i ){ |
| 5401 | int t; |
| 5402 | MemPage *pT; |
| 5403 | t = pgnoNew[i]; |
| 5404 | pT = apNew[i]; |
| 5405 | pgnoNew[i] = pgnoNew[minI]; |
| 5406 | apNew[i] = apNew[minI]; |
| 5407 | pgnoNew[minI] = t; |
| 5408 | apNew[minI] = pT; |
| 5409 | } |
| 5410 | } |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5411 | 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] | 5412 | pgnoOld[0], |
| 5413 | nOld>=2 ? pgnoOld[1] : 0, |
| 5414 | nOld>=3 ? pgnoOld[2] : 0, |
drh | 10c0fa6 | 2004-05-18 12:50:17 +0000 | [diff] [blame] | 5415 | pgnoNew[0], szNew[0], |
| 5416 | nNew>=2 ? pgnoNew[1] : 0, nNew>=2 ? szNew[1] : 0, |
| 5417 | nNew>=3 ? pgnoNew[2] : 0, nNew>=3 ? szNew[2] : 0, |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5418 | nNew>=4 ? pgnoNew[3] : 0, nNew>=4 ? szNew[3] : 0, |
| 5419 | nNew>=5 ? pgnoNew[4] : 0, nNew>=5 ? szNew[4] : 0)); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5420 | |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5421 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5422 | ** Evenly distribute the data in apCell[] across the new pages. |
| 5423 | ** Insert divider cells into pParent as necessary. |
| 5424 | */ |
| 5425 | j = 0; |
| 5426 | for(i=0; i<nNew; i++){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5427 | /* Assemble the new sibling page. */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5428 | MemPage *pNew = apNew[i]; |
drh | 19642e5 | 2005-03-29 13:17:45 +0000 | [diff] [blame] | 5429 | assert( j<nMaxCells ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5430 | assert( pNew->pgno==pgnoNew[i] ); |
drh | 1013148 | 2008-07-11 03:34:09 +0000 | [diff] [blame] | 5431 | zeroPage(pNew, pageFlags); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 5432 | assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]); |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 5433 | assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5434 | assert( pNew->nOverflow==0 ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5435 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5436 | /* If this is an auto-vacuum database, update the pointer map entries |
| 5437 | ** that point to the siblings that were rearranged. These can be: left |
| 5438 | ** children of cells, the right-child of the page, or overflow pages |
| 5439 | ** pointed to by cells. |
| 5440 | */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5441 | if( ISAUTOVACUUM ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5442 | for(k=j; k<cntNew[i]; k++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5443 | assert( k<nMaxCells ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5444 | if( aFrom[k]==0xFF || apCopy[aFrom[k]]->pgno!=pNew->pgno ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5445 | rc = ptrmapPutOvfl(pNew, k-j); |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5446 | if( rc==SQLITE_OK && leafCorrection==0 ){ |
| 5447 | rc = ptrmapPut(pBt, get4byte(apCell[k]), PTRMAP_BTREE, pNew->pgno); |
| 5448 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5449 | if( rc!=SQLITE_OK ){ |
| 5450 | goto balance_cleanup; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5451 | } |
| 5452 | } |
| 5453 | } |
| 5454 | } |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5455 | |
| 5456 | j = cntNew[i]; |
| 5457 | |
| 5458 | /* If the sibling page assembled above was not the right-most sibling, |
| 5459 | ** insert a divider cell into the parent page. |
| 5460 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5461 | if( i<nNew-1 && j<nCell ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5462 | u8 *pCell; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5463 | u8 *pTemp; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5464 | int sz; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5465 | |
| 5466 | assert( j<nMaxCells ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5467 | pCell = apCell[j]; |
| 5468 | sz = szCell[j] + leafCorrection; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5469 | pTemp = &aSpace2[iSpace2]; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5470 | if( !pNew->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5471 | memcpy(&pNew->aData[8], pCell, 4); |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5472 | if( ISAUTOVACUUM |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5473 | && (aFrom[j]==0xFF || apCopy[aFrom[j]]->pgno!=pNew->pgno) |
| 5474 | ){ |
| 5475 | rc = ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno); |
| 5476 | if( rc!=SQLITE_OK ){ |
| 5477 | goto balance_cleanup; |
| 5478 | } |
| 5479 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5480 | }else if( leafData ){ |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 5481 | /* If the tree is a leaf-data tree, and the siblings are leaves, |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5482 | ** then there is no divider cell in apCell[]. Instead, the divider |
| 5483 | ** cell consists of the integer key for the right-most cell of |
| 5484 | ** the sibling-page assembled above only. |
| 5485 | */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5486 | CellInfo info; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5487 | j--; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5488 | sqlite3BtreeParseCellPtr(pNew, apCell[j], &info); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5489 | pCell = pTemp; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5490 | fillInCell(pParent, pCell, 0, info.nKey, 0, 0, 0, &sz); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5491 | pTemp = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5492 | }else{ |
| 5493 | pCell -= 4; |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 5494 | /* Obscure case for non-leaf-data trees: If the cell at pCell was |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 5495 | ** previously stored on a leaf node, and its reported size was 4 |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 5496 | ** bytes, then it may actually be smaller than this |
| 5497 | ** (see sqlite3BtreeParseCellPtr(), 4 bytes is the minimum size of |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 5498 | ** any cell). But it is important to pass the correct size to |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 5499 | ** insertCell(), so reparse the cell now. |
| 5500 | ** |
| 5501 | ** Note that this can never happen in an SQLite data file, as all |
| 5502 | ** cells are at least 4 bytes. It only happens in b-trees used |
| 5503 | ** to evaluate "IN (SELECT ...)" and similar clauses. |
| 5504 | */ |
| 5505 | if( szCell[j]==4 ){ |
| 5506 | assert(leafCorrection==4); |
| 5507 | sz = cellSizePtr(pParent, pCell); |
| 5508 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5509 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5510 | iSpace2 += sz; |
| 5511 | assert( sz<=pBt->pageSize/4 ); |
| 5512 | assert( iSpace2<=pBt->pageSize ); |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 5513 | rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4); |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 5514 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 5515 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5516 | put4byte(findOverflowCell(pParent,nxDiv), pNew->pgno); |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5517 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5518 | /* If this is an auto-vacuum database, and not a leaf-data tree, |
| 5519 | ** then update the pointer map with an entry for the overflow page |
| 5520 | ** that the cell just inserted points to (if any). |
| 5521 | */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5522 | if( ISAUTOVACUUM && !leafData ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5523 | rc = ptrmapPutOvfl(pParent, nxDiv); |
| 5524 | if( rc!=SQLITE_OK ){ |
| 5525 | goto balance_cleanup; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5526 | } |
| 5527 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5528 | j++; |
| 5529 | nxDiv++; |
| 5530 | } |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5531 | |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5532 | /* Set the pointer-map entry for the new sibling page. */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5533 | if( ISAUTOVACUUM ){ |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5534 | rc = ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno); |
| 5535 | if( rc!=SQLITE_OK ){ |
| 5536 | goto balance_cleanup; |
| 5537 | } |
| 5538 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5539 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5540 | assert( j==nCell ); |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 5541 | assert( nOld>0 ); |
| 5542 | assert( nNew>0 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5543 | if( (pageFlags & PTF_LEAF)==0 ){ |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5544 | u8 *zChild = &apCopy[nOld-1]->aData[8]; |
| 5545 | memcpy(&apNew[nNew-1]->aData[8], zChild, 4); |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5546 | if( ISAUTOVACUUM ){ |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5547 | rc = ptrmapPut(pBt, get4byte(zChild), PTRMAP_BTREE, apNew[nNew-1]->pgno); |
| 5548 | if( rc!=SQLITE_OK ){ |
| 5549 | goto balance_cleanup; |
| 5550 | } |
| 5551 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5552 | } |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 5553 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5554 | if( nxDiv==pParent->nCell+pParent->nOverflow ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5555 | /* Right-most sibling is the right-most child of pParent */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5556 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew[nNew-1]); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5557 | }else{ |
| 5558 | /* Right-most sibling is the left child of the first entry in pParent |
| 5559 | ** past the right-most divider entry */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5560 | put4byte(findOverflowCell(pParent, nxDiv), pgnoNew[nNew-1]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5561 | } |
| 5562 | |
| 5563 | /* |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5564 | ** Balance the parent page. Note that the current page (pPage) might |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5565 | ** have been added to the freelist so it might no longer be initialized. |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5566 | ** But the parent page will always be initialized. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5567 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5568 | assert( pParent->isInit ); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5569 | sqlite3ScratchFree(apCell); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5570 | apCell = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5571 | releasePage(pPage); |
| 5572 | pCur->iPage--; |
| 5573 | rc = balance(pCur, 0); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5574 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5575 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5576 | ** Cleanup before returning. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5577 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5578 | balance_cleanup: |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5579 | sqlite3PageFree(aSpace2); |
| 5580 | sqlite3ScratchFree(apCell); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5581 | for(i=0; i<nOld; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5582 | releasePage(apOld[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5583 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5584 | for(i=0; i<nNew; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5585 | releasePage(apNew[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5586 | } |
drh | 9bf9e9c | 2008-12-05 20:01:43 +0000 | [diff] [blame] | 5587 | pPage->nOverflow = 0; |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 5588 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5589 | /* releasePage(pParent); */ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5590 | TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n", |
| 5591 | pPage->pgno, nOld, nNew, nCell)); |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 5592 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5593 | return rc; |
| 5594 | } |
| 5595 | |
| 5596 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5597 | ** This routine is called for the root page of a btree when the root |
| 5598 | ** page contains no cells. This is an opportunity to make the tree |
| 5599 | ** shallower by one level. |
| 5600 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5601 | static int balance_shallower(BtCursor *pCur){ |
| 5602 | MemPage *pPage; /* Root page of B-Tree */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5603 | MemPage *pChild; /* The only child page of pPage */ |
| 5604 | Pgno pgnoChild; /* Page number for pChild */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5605 | int rc = SQLITE_OK; /* Return code from subprocedures */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5606 | BtShared *pBt; /* The main BTree structure */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5607 | int mxCellPerPage; /* Maximum number of cells per page */ |
| 5608 | u8 **apCell; /* All cells from pages being balanced */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5609 | u16 *szCell; /* Local size of all cells */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5610 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5611 | assert( pCur->iPage==0 ); |
| 5612 | pPage = pCur->apPage[0]; |
| 5613 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5614 | assert( pPage->nCell==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5615 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5616 | pBt = pPage->pBt; |
| 5617 | mxCellPerPage = MX_CELL(pBt); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5618 | apCell = sqlite3Malloc( mxCellPerPage*(sizeof(u8*)+sizeof(u16)) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5619 | if( apCell==0 ) return SQLITE_NOMEM; |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5620 | szCell = (u16*)&apCell[mxCellPerPage]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5621 | if( pPage->leaf ){ |
| 5622 | /* The table is completely empty */ |
| 5623 | TRACE(("BALANCE: empty table %d\n", pPage->pgno)); |
| 5624 | }else{ |
| 5625 | /* The root page is empty but has one child. Transfer the |
| 5626 | ** information from that one child into the root page if it |
| 5627 | ** will fit. This reduces the depth of the tree by one. |
| 5628 | ** |
| 5629 | ** If the root page is page 1, it has less space available than |
| 5630 | ** its child (due to the 100 byte header that occurs at the beginning |
| 5631 | ** of the database fle), so it might not be able to hold all of the |
| 5632 | ** information currently contained in the child. If this is the |
| 5633 | ** case, then do not do the transfer. Leave page 1 empty except |
| 5634 | ** for the right-pointer to the child page. The child page becomes |
| 5635 | ** the virtual root of the tree. |
| 5636 | */ |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 5637 | VVA_ONLY( pCur->pagesShuffled = 1 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5638 | pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 5639 | assert( pgnoChild>0 ); |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 5640 | assert( pgnoChild<=pagerPagecount(pPage->pBt) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5641 | rc = sqlite3BtreeGetPage(pPage->pBt, pgnoChild, &pChild, 0); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5642 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5643 | if( pPage->pgno==1 ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5644 | rc = sqlite3BtreeInitPage(pChild); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5645 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5646 | assert( pChild->nOverflow==0 ); |
| 5647 | if( pChild->nFree>=100 ){ |
| 5648 | /* The child information will fit on the root page, so do the |
| 5649 | ** copy */ |
| 5650 | int i; |
| 5651 | zeroPage(pPage, pChild->aData[0]); |
| 5652 | for(i=0; i<pChild->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5653 | apCell[i] = findCell(pChild,i); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5654 | szCell[i] = cellSizePtr(pChild, apCell[i]); |
| 5655 | } |
| 5656 | assemblePage(pPage, pChild->nCell, apCell, szCell); |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 5657 | /* Copy the right-pointer of the child to the parent. */ |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 5658 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 5659 | put4byte(&pPage->aData[pPage->hdrOffset+8], |
| 5660 | get4byte(&pChild->aData[pChild->hdrOffset+8])); |
drh | 9bf9e9c | 2008-12-05 20:01:43 +0000 | [diff] [blame] | 5661 | rc = freePage(pChild); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5662 | TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno)); |
| 5663 | }else{ |
| 5664 | /* The child has more information that will fit on the root. |
| 5665 | ** The tree is already balanced. Do nothing. */ |
| 5666 | TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno)); |
| 5667 | } |
| 5668 | }else{ |
| 5669 | memcpy(pPage->aData, pChild->aData, pPage->pBt->usableSize); |
| 5670 | pPage->isInit = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5671 | rc = sqlite3BtreeInitPage(pPage); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5672 | assert( rc==SQLITE_OK ); |
| 5673 | freePage(pChild); |
| 5674 | TRACE(("BALANCE: transfer child %d into root %d\n", |
| 5675 | pChild->pgno, pPage->pgno)); |
| 5676 | } |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5677 | assert( pPage->nOverflow==0 ); |
shane | 831c329 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 5678 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | 9bf9e9c | 2008-12-05 20:01:43 +0000 | [diff] [blame] | 5679 | if( ISAUTOVACUUM && rc==SQLITE_OK ){ |
danielk1977 | 00a696d | 2008-09-29 16:41:31 +0000 | [diff] [blame] | 5680 | rc = setChildPtrmaps(pPage); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5681 | } |
shane | 831c329 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 5682 | #endif |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5683 | releasePage(pChild); |
| 5684 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5685 | end_shallow_balance: |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5686 | sqlite3_free(apCell); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5687 | return rc; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5688 | } |
| 5689 | |
| 5690 | |
| 5691 | /* |
| 5692 | ** The root page is overfull |
| 5693 | ** |
| 5694 | ** When this happens, Create a new child page and copy the |
| 5695 | ** contents of the root into the child. Then make the root |
| 5696 | ** page an empty page with rightChild pointing to the new |
| 5697 | ** child. Finally, call balance_internal() on the new child |
| 5698 | ** to cause it to split. |
| 5699 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5700 | static int balance_deeper(BtCursor *pCur){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5701 | int rc; /* Return value from subprocedures */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5702 | MemPage *pPage; /* Pointer to the root page */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5703 | MemPage *pChild; /* Pointer to a new child page */ |
| 5704 | Pgno pgnoChild; /* Page number of the new child page */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5705 | BtShared *pBt; /* The BTree */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5706 | int usableSize; /* Total usable size of a page */ |
| 5707 | u8 *data; /* Content of the parent page */ |
| 5708 | u8 *cdata; /* Content of the child page */ |
| 5709 | int hdr; /* Offset to page header in parent */ |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 5710 | int cbrk; /* Offset to content of first cell in parent */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5711 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5712 | assert( pCur->iPage==0 ); |
| 5713 | assert( pCur->apPage[0]->nOverflow>0 ); |
| 5714 | |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 5715 | VVA_ONLY( pCur->pagesShuffled = 1 ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5716 | pPage = pCur->apPage[0]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5717 | pBt = pPage->pBt; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5718 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 5719 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5720 | rc = allocateBtreePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5721 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5722 | assert( sqlite3PagerIswriteable(pChild->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5723 | usableSize = pBt->usableSize; |
| 5724 | data = pPage->aData; |
| 5725 | hdr = pPage->hdrOffset; |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 5726 | cbrk = get2byte(&data[hdr+5]); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5727 | cdata = pChild->aData; |
| 5728 | memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr); |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 5729 | memcpy(&cdata[cbrk], &data[cbrk], usableSize-cbrk); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 5730 | |
| 5731 | assert( pChild->isInit==0 ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5732 | rc = sqlite3BtreeInitPage(pChild); |
| 5733 | if( rc==SQLITE_OK ){ |
| 5734 | int nCopy = pPage->nOverflow*sizeof(pPage->aOvfl[0]); |
| 5735 | memcpy(pChild->aOvfl, pPage->aOvfl, nCopy); |
| 5736 | pChild->nOverflow = pPage->nOverflow; |
| 5737 | if( pChild->nOverflow ){ |
| 5738 | pChild->nFree = 0; |
| 5739 | } |
| 5740 | assert( pChild->nCell==pPage->nCell ); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 5741 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5742 | zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF); |
| 5743 | put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild); |
| 5744 | TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno)); |
| 5745 | if( ISAUTOVACUUM ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5746 | rc = ptrmapPut(pBt, pChild->pgno, PTRMAP_BTREE, pPage->pgno); |
shane | 831c329 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 5747 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5748 | if( rc==SQLITE_OK ){ |
danielk1977 | 00a696d | 2008-09-29 16:41:31 +0000 | [diff] [blame] | 5749 | rc = setChildPtrmaps(pChild); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5750 | } |
shane | 831c329 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 5751 | #endif |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5752 | } |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5753 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5754 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5755 | if( rc==SQLITE_OK ){ |
| 5756 | pCur->iPage++; |
| 5757 | pCur->apPage[1] = pChild; |
danielk1977 | bf93c56 | 2008-09-29 15:53:25 +0000 | [diff] [blame] | 5758 | pCur->aiIdx[0] = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5759 | rc = balance_nonroot(pCur); |
| 5760 | }else{ |
| 5761 | releasePage(pChild); |
| 5762 | } |
| 5763 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5764 | return rc; |
| 5765 | } |
| 5766 | |
| 5767 | /* |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5768 | ** The page that pCur currently points to has just been modified in |
| 5769 | ** some way. This function figures out if this modification means the |
| 5770 | ** tree needs to be balanced, and if so calls the appropriate balancing |
| 5771 | ** routine. |
| 5772 | ** |
| 5773 | ** Parameter isInsert is true if a new cell was just inserted into the |
| 5774 | ** page, or false otherwise. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5775 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5776 | static int balance(BtCursor *pCur, int isInsert){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5777 | int rc = SQLITE_OK; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5778 | MemPage *pPage = pCur->apPage[pCur->iPage]; |
| 5779 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5780 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5781 | if( pCur->iPage==0 ){ |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 5782 | rc = sqlite3PagerWrite(pPage->pDbPage); |
| 5783 | if( rc==SQLITE_OK && pPage->nOverflow>0 ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5784 | rc = balance_deeper(pCur); |
drh | 9bf9e9c | 2008-12-05 20:01:43 +0000 | [diff] [blame] | 5785 | assert( pPage->nOverflow==0 || rc!=SQLITE_OK ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5786 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5787 | if( rc==SQLITE_OK && pPage->nCell==0 ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5788 | rc = balance_shallower(pCur); |
drh | 9bf9e9c | 2008-12-05 20:01:43 +0000 | [diff] [blame] | 5789 | assert( pPage->nOverflow==0 || rc!=SQLITE_OK ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5790 | } |
| 5791 | }else{ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5792 | if( pPage->nOverflow>0 || |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5793 | (!isInsert && pPage->nFree>pPage->pBt->usableSize*2/3) ){ |
| 5794 | rc = balance_nonroot(pCur); |
drh | 9bf9e9c | 2008-12-05 20:01:43 +0000 | [diff] [blame] | 5795 | assert( pPage->nOverflow==0 || rc!=SQLITE_OK ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5796 | } |
| 5797 | } |
| 5798 | return rc; |
| 5799 | } |
| 5800 | |
| 5801 | /* |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 5802 | ** This routine checks all cursors that point to table pgnoRoot. |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5803 | ** If any of those cursors were opened with wrFlag==0 in a different |
| 5804 | ** database connection (a database connection that shares the pager |
| 5805 | ** cache with the current connection) and that other connection |
| 5806 | ** is not in the ReadUncommmitted state, then this routine returns |
| 5807 | ** SQLITE_LOCKED. |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5808 | ** |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 5809 | ** As well as cursors with wrFlag==0, cursors with wrFlag==1 and |
| 5810 | ** isIncrblobHandle==1 are also considered 'read' cursors. Incremental |
| 5811 | ** blob cursors are used for both reading and writing. |
| 5812 | ** |
| 5813 | ** When pgnoRoot is the root page of an intkey table, this function is also |
| 5814 | ** responsible for invalidating incremental blob cursors when the table row |
| 5815 | ** on which they are opened is deleted or modified. Cursors are invalidated |
| 5816 | ** according to the following rules: |
| 5817 | ** |
| 5818 | ** 1) When BtreeClearTable() is called to completely delete the contents |
| 5819 | ** of a B-Tree table, pExclude is set to zero and parameter iRow is |
| 5820 | ** set to non-zero. In this case all incremental blob cursors open |
| 5821 | ** on the table rooted at pgnoRoot are invalidated. |
| 5822 | ** |
| 5823 | ** 2) When BtreeInsert(), BtreeDelete() or BtreePutData() is called to |
| 5824 | ** modify a table row via an SQL statement, pExclude is set to the |
| 5825 | ** write cursor used to do the modification and parameter iRow is set |
| 5826 | ** to the integer row id of the B-Tree entry being modified. Unless |
| 5827 | ** pExclude is itself an incremental blob cursor, then all incremental |
| 5828 | ** blob cursors open on row iRow of the B-Tree are invalidated. |
| 5829 | ** |
| 5830 | ** 3) If both pExclude and iRow are set to zero, no incremental blob |
| 5831 | ** cursors are invalidated. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5832 | */ |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 5833 | static int checkReadLocks( |
| 5834 | Btree *pBtree, |
| 5835 | Pgno pgnoRoot, |
| 5836 | BtCursor *pExclude, |
| 5837 | i64 iRow |
| 5838 | ){ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5839 | BtCursor *p; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5840 | BtShared *pBt = pBtree->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 5841 | sqlite3 *db = pBtree->db; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5842 | assert( sqlite3BtreeHoldsMutex(pBtree) ); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5843 | for(p=pBt->pCursor; p; p=p->pNext){ |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5844 | if( p==pExclude ) continue; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5845 | if( p->pgnoRoot!=pgnoRoot ) continue; |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 5846 | #ifndef SQLITE_OMIT_INCRBLOB |
| 5847 | if( p->isIncrblobHandle && ( |
| 5848 | (!pExclude && iRow) |
| 5849 | || (pExclude && !pExclude->isIncrblobHandle && p->info.nKey==iRow) |
| 5850 | )){ |
| 5851 | p->eState = CURSOR_INVALID; |
| 5852 | } |
| 5853 | #endif |
| 5854 | if( p->eState!=CURSOR_VALID ) continue; |
| 5855 | if( p->wrFlag==0 |
| 5856 | #ifndef SQLITE_OMIT_INCRBLOB |
| 5857 | || p->isIncrblobHandle |
| 5858 | #endif |
| 5859 | ){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 5860 | sqlite3 *dbOther = p->pBtree->db; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5861 | if( dbOther==0 || |
| 5862 | (dbOther!=db && (dbOther->flags & SQLITE_ReadUncommitted)==0) ){ |
| 5863 | return SQLITE_LOCKED; |
| 5864 | } |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5865 | } |
| 5866 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5867 | return SQLITE_OK; |
| 5868 | } |
| 5869 | |
| 5870 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5871 | ** Insert a new record into the BTree. The key is given by (pKey,nKey) |
| 5872 | ** 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] | 5873 | ** define what table the record should be inserted into. The cursor |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5874 | ** is left pointing at a random location. |
| 5875 | ** |
| 5876 | ** For an INTKEY table, only the nKey value of the key is used. pKey is |
| 5877 | ** ignored. For a ZERODATA table, the pData and nData are both ignored. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5878 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5879 | int sqlite3BtreeInsert( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 5880 | BtCursor *pCur, /* Insert data into the table of this cursor */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 5881 | const void *pKey, i64 nKey, /* The key of the new record */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 5882 | const void *pData, int nData, /* The data of the new record */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5883 | int nZero, /* Number of extra 0 bytes to append to data */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 5884 | int appendBias /* True if this is likely an append */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5885 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5886 | int rc; |
| 5887 | int loc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5888 | int szNew; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5889 | int idx; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5890 | MemPage *pPage; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5891 | Btree *p = pCur->pBtree; |
| 5892 | BtShared *pBt = p->pBt; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5893 | unsigned char *oldCell; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5894 | unsigned char *newCell = 0; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5895 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5896 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5897 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5898 | /* Must start a transaction before doing an insert */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5899 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5900 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5901 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5902 | assert( !pBt->readOnly ); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 5903 | if( !pCur->wrFlag ){ |
| 5904 | return SQLITE_PERM; /* Cursor not open for writing */ |
| 5905 | } |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 5906 | if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur, nKey) ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5907 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 5908 | } |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 5909 | if( pCur->eState==CURSOR_FAULT ){ |
| 5910 | return pCur->skip; |
| 5911 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5912 | |
| 5913 | /* Save the positions of any other cursors open on this table */ |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 5914 | sqlite3BtreeClearCursor(pCur); |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5915 | if( |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5916 | SQLITE_OK!=(rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur)) || |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 5917 | SQLITE_OK!=(rc = sqlite3BtreeMoveto(pCur, pKey, nKey, appendBias, &loc)) |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5918 | ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5919 | return rc; |
| 5920 | } |
| 5921 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5922 | pPage = pCur->apPage[pCur->iPage]; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 5923 | assert( pPage->intKey || nKey>=0 ); |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 5924 | assert( pPage->leaf || !pPage->intKey ); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5925 | TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n", |
| 5926 | pCur->pgnoRoot, nKey, nData, pPage->pgno, |
| 5927 | loc==0 ? "overwrite" : "new entry")); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5928 | assert( pPage->isInit ); |
danielk1977 | 52ae724 | 2008-03-25 14:24:56 +0000 | [diff] [blame] | 5929 | allocateTempSpace(pBt); |
| 5930 | newCell = pBt->pTmpSpace; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5931 | if( newCell==0 ) return SQLITE_NOMEM; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5932 | rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5933 | if( rc ) goto end_insert; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5934 | assert( szNew==cellSizePtr(pPage, newCell) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5935 | assert( szNew<=MX_CELL_SIZE(pBt) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5936 | idx = pCur->aiIdx[pCur->iPage]; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5937 | if( loc==0 && CURSOR_VALID==pCur->eState ){ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5938 | u16 szOld; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5939 | assert( idx<pPage->nCell ); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 5940 | rc = sqlite3PagerWrite(pPage->pDbPage); |
| 5941 | if( rc ){ |
| 5942 | goto end_insert; |
| 5943 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5944 | oldCell = findCell(pPage, idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5945 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5946 | memcpy(newCell, oldCell, 4); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5947 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5948 | szOld = cellSizePtr(pPage, oldCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5949 | rc = clearCell(pPage, oldCell); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5950 | if( rc ) goto end_insert; |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 5951 | rc = dropCell(pPage, idx, szOld); |
| 5952 | if( rc!=SQLITE_OK ) { |
| 5953 | goto end_insert; |
| 5954 | } |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 5955 | }else if( loc<0 && pPage->nCell>0 ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5956 | assert( pPage->leaf ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5957 | idx = ++pCur->aiIdx[pCur->iPage]; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 5958 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 5959 | pCur->validNKey = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5960 | }else{ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5961 | assert( pPage->leaf ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5962 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5963 | rc = insertCell(pPage, idx, newCell, szNew, 0, 0); |
drh | 9bf9e9c | 2008-12-05 20:01:43 +0000 | [diff] [blame] | 5964 | if( rc==SQLITE_OK ){ |
| 5965 | rc = balance(pCur, 1); |
| 5966 | } |
| 5967 | |
| 5968 | /* Must make sure nOverflow is reset to zero even if the balance() |
| 5969 | ** fails. Internal data structure corruption will result otherwise. */ |
| 5970 | assert( pPage->nOverflow==0 || rc!=SQLITE_OK ); |
| 5971 | pPage->nOverflow = 0; |
| 5972 | |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5973 | if( rc==SQLITE_OK ){ |
| 5974 | moveToRoot(pCur); |
| 5975 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5976 | end_insert: |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5977 | return rc; |
| 5978 | } |
| 5979 | |
| 5980 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5981 | ** Delete the entry that the cursor is pointing to. The cursor |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 5982 | ** is left pointing at a arbitrary location. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5983 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5984 | int sqlite3BtreeDelete(BtCursor *pCur){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5985 | MemPage *pPage = pCur->apPage[pCur->iPage]; |
| 5986 | int idx; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5987 | unsigned char *pCell; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5988 | int rc; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 5989 | Pgno pgnoChild = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5990 | Btree *p = pCur->pBtree; |
| 5991 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5992 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5993 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5994 | assert( pPage->isInit ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5995 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5996 | /* Must start a transaction before doing a delete */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5997 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5998 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5999 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 6000 | assert( !pBt->readOnly ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 6001 | if( pCur->eState==CURSOR_FAULT ){ |
| 6002 | return pCur->skip; |
| 6003 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6004 | if( pCur->aiIdx[pCur->iPage]>=pPage->nCell ){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 6005 | return SQLITE_ERROR; /* The cursor is not pointing to anything */ |
| 6006 | } |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 6007 | if( !pCur->wrFlag ){ |
| 6008 | return SQLITE_PERM; /* Did not open this cursor for writing */ |
| 6009 | } |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 6010 | if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur, pCur->info.nKey) ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 6011 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 6012 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6013 | |
| 6014 | /* Restore the current cursor position (a no-op if the cursor is not in |
| 6015 | ** CURSOR_REQUIRESEEK state) and save the positions of any other cursors |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6016 | ** open on the same table. Then call sqlite3PagerWrite() on the page |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6017 | ** that the entry will be deleted from. |
| 6018 | */ |
| 6019 | if( |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 6020 | (rc = restoreCursorPosition(pCur))!=0 || |
drh | d116739 | 2006-01-23 13:00:35 +0000 | [diff] [blame] | 6021 | (rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur))!=0 || |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6022 | (rc = sqlite3PagerWrite(pPage->pDbPage))!=0 |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6023 | ){ |
| 6024 | return rc; |
| 6025 | } |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 6026 | |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 6027 | /* Locate the cell within its page and leave pCell pointing to the |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 6028 | ** data. The clearCell() call frees any overflow pages associated with the |
| 6029 | ** cell. The cell itself is still intact. |
| 6030 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6031 | idx = pCur->aiIdx[pCur->iPage]; |
| 6032 | pCell = findCell(pPage, idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6033 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6034 | pgnoChild = get4byte(pCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6035 | } |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 6036 | rc = clearCell(pPage, pCell); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6037 | if( rc ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6038 | return rc; |
| 6039 | } |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 6040 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6041 | if( !pPage->leaf ){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6042 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6043 | ** 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] | 6044 | ** do something we will leave a hole on an internal page. |
| 6045 | ** We have to fill the hole by moving in a cell from a leaf. The |
| 6046 | ** next Cell after the one to be deleted is guaranteed to exist and |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 6047 | ** to be a leaf so we can use it. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 6048 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6049 | BtCursor leafCur; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 6050 | MemPage *pLeafPage = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6051 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6052 | unsigned char *pNext; |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 6053 | int notUsed; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6054 | unsigned char *tempCell = 0; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 6055 | assert( !pPage->intKey ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6056 | sqlite3BtreeGetTempCursor(pCur, &leafCur); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 6057 | rc = sqlite3BtreeNext(&leafCur, ¬Used); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6058 | if( rc==SQLITE_OK ){ |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6059 | assert( leafCur.aiIdx[leafCur.iPage]==0 ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6060 | pLeafPage = leafCur.apPage[leafCur.iPage]; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6061 | rc = sqlite3PagerWrite(pLeafPage->pDbPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6062 | } |
| 6063 | if( rc==SQLITE_OK ){ |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6064 | int leafCursorInvalid = 0; |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 6065 | u16 szNext; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6066 | TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n", |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6067 | pCur->pgnoRoot, pPage->pgno, pLeafPage->pgno)); |
| 6068 | dropCell(pPage, idx, cellSizePtr(pPage, pCell)); |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6069 | pNext = findCell(pLeafPage, 0); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6070 | szNext = cellSizePtr(pLeafPage, pNext); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6071 | assert( MX_CELL_SIZE(pBt)>=szNext+4 ); |
danielk1977 | 52ae724 | 2008-03-25 14:24:56 +0000 | [diff] [blame] | 6072 | allocateTempSpace(pBt); |
| 6073 | tempCell = pBt->pTmpSpace; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6074 | if( tempCell==0 ){ |
| 6075 | rc = SQLITE_NOMEM; |
| 6076 | } |
danielk1977 | 8ea1cfa | 2008-01-01 06:19:02 +0000 | [diff] [blame] | 6077 | if( rc==SQLITE_OK ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6078 | rc = insertCell(pPage, idx, pNext-4, szNext+4, tempCell, 0); |
danielk1977 | 8ea1cfa | 2008-01-01 06:19:02 +0000 | [diff] [blame] | 6079 | } |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6080 | |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 6081 | |
| 6082 | /* The "if" statement in the next code block is critical. The |
| 6083 | ** slightest error in that statement would allow SQLite to operate |
| 6084 | ** correctly most of the time but produce very rare failures. To |
| 6085 | ** guard against this, the following macros help to verify that |
| 6086 | ** the "if" statement is well tested. |
| 6087 | */ |
| 6088 | testcase( pPage->nOverflow==0 && pPage->nFree<pBt->usableSize*2/3 |
| 6089 | && pLeafPage->nFree+2+szNext > pBt->usableSize*2/3 ); |
| 6090 | testcase( pPage->nOverflow==0 && pPage->nFree==pBt->usableSize*2/3 |
| 6091 | && pLeafPage->nFree+2+szNext > pBt->usableSize*2/3 ); |
| 6092 | testcase( pPage->nOverflow==0 && pPage->nFree==pBt->usableSize*2/3+1 |
| 6093 | && pLeafPage->nFree+2+szNext > pBt->usableSize*2/3 ); |
| 6094 | testcase( pPage->nOverflow>0 && pPage->nFree<=pBt->usableSize*2/3 |
| 6095 | && pLeafPage->nFree+2+szNext > pBt->usableSize*2/3 ); |
| 6096 | testcase( (pPage->nOverflow>0 || (pPage->nFree > pBt->usableSize*2/3)) |
| 6097 | && pLeafPage->nFree+2+szNext == pBt->usableSize*2/3 ); |
| 6098 | |
| 6099 | |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6100 | if( (pPage->nOverflow>0 || (pPage->nFree > pBt->usableSize*2/3)) && |
| 6101 | (pLeafPage->nFree+2+szNext > pBt->usableSize*2/3) |
| 6102 | ){ |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 6103 | /* This branch is taken if the internal node is now either overflowing |
| 6104 | ** or underfull and the leaf node will be underfull after the just cell |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6105 | ** copied to the internal node is deleted from it. This is a special |
| 6106 | ** case because the call to balance() to correct the internal node |
| 6107 | ** may change the tree structure and invalidate the contents of |
| 6108 | ** the leafCur.apPage[] and leafCur.aiIdx[] arrays, which will be |
| 6109 | ** used by the balance() required to correct the underfull leaf |
| 6110 | ** node. |
| 6111 | ** |
| 6112 | ** The formula used in the expression above are based on facets of |
| 6113 | ** the SQLite file-format that do not change over time. |
| 6114 | */ |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 6115 | testcase( pPage->nFree==pBt->usableSize*2/3+1 ); |
| 6116 | testcase( pLeafPage->nFree+2+szNext==pBt->usableSize*2/3+1 ); |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6117 | leafCursorInvalid = 1; |
| 6118 | } |
| 6119 | |
danielk1977 | 8ea1cfa | 2008-01-01 06:19:02 +0000 | [diff] [blame] | 6120 | if( rc==SQLITE_OK ){ |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 6121 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6122 | put4byte(findOverflowCell(pPage, idx), pgnoChild); |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 6123 | VVA_ONLY( pCur->pagesShuffled = 0 ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6124 | rc = balance(pCur, 0); |
danielk1977 | 8ea1cfa | 2008-01-01 06:19:02 +0000 | [diff] [blame] | 6125 | } |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6126 | |
| 6127 | if( rc==SQLITE_OK && leafCursorInvalid ){ |
| 6128 | /* The leaf-node is now underfull and so the tree needs to be |
| 6129 | ** rebalanced. However, the balance() operation on the internal |
| 6130 | ** node above may have modified the structure of the B-Tree and |
| 6131 | ** so the current contents of leafCur.apPage[] and leafCur.aiIdx[] |
| 6132 | ** may not be trusted. |
| 6133 | ** |
| 6134 | ** It is not possible to copy the ancestry from pCur, as the same |
| 6135 | ** balance() call has invalidated the pCur->apPage[] and aiIdx[] |
| 6136 | ** arrays. |
drh | 7b68280 | 2008-09-30 14:06:28 +0000 | [diff] [blame] | 6137 | ** |
| 6138 | ** The call to saveCursorPosition() below internally saves the |
| 6139 | ** key that leafCur is currently pointing to. Currently, there |
| 6140 | ** are two copies of that key in the tree - one here on the leaf |
| 6141 | ** page and one on some internal node in the tree. The copy on |
| 6142 | ** the leaf node is always the next key in tree-order after the |
| 6143 | ** copy on the internal node. So, the call to sqlite3BtreeNext() |
| 6144 | ** calls restoreCursorPosition() to point the cursor to the copy |
| 6145 | ** stored on the internal node, then advances to the next entry, |
| 6146 | ** which happens to be the copy of the key on the internal node. |
danielk1977 | a69fda2 | 2008-09-30 16:48:10 +0000 | [diff] [blame] | 6147 | ** Net effect: leafCur is pointing back to the duplicate cell |
| 6148 | ** that needs to be removed, and the leafCur.apPage[] and |
| 6149 | ** leafCur.aiIdx[] arrays are correct. |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6150 | */ |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 6151 | VVA_ONLY( Pgno leafPgno = pLeafPage->pgno ); |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6152 | rc = saveCursorPosition(&leafCur); |
| 6153 | if( rc==SQLITE_OK ){ |
| 6154 | rc = sqlite3BtreeNext(&leafCur, ¬Used); |
| 6155 | } |
| 6156 | pLeafPage = leafCur.apPage[leafCur.iPage]; |
| 6157 | assert( pLeafPage->pgno==leafPgno ); |
| 6158 | assert( leafCur.aiIdx[leafCur.iPage]==0 ); |
| 6159 | } |
| 6160 | |
danielk1977 | 0cd1bbd | 2008-11-26 07:25:52 +0000 | [diff] [blame] | 6161 | if( SQLITE_OK==rc |
| 6162 | && SQLITE_OK==(rc = sqlite3PagerWrite(pLeafPage->pDbPage)) |
| 6163 | ){ |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6164 | dropCell(pLeafPage, 0, szNext); |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 6165 | VVA_ONLY( leafCur.pagesShuffled = 0 ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6166 | rc = balance(&leafCur, 0); |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 6167 | assert( leafCursorInvalid || !leafCur.pagesShuffled |
| 6168 | || !pCur->pagesShuffled ); |
danielk1977 | 8ea1cfa | 2008-01-01 06:19:02 +0000 | [diff] [blame] | 6169 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6170 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6171 | sqlite3BtreeReleaseTempCursor(&leafCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 6172 | }else{ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 6173 | TRACE(("DELETE: table=%d delete from leaf %d\n", |
| 6174 | pCur->pgnoRoot, pPage->pgno)); |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 6175 | rc = dropCell(pPage, idx, cellSizePtr(pPage, pCell)); |
| 6176 | if( rc==SQLITE_OK ){ |
| 6177 | rc = balance(pCur, 0); |
| 6178 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 6179 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6180 | if( rc==SQLITE_OK ){ |
| 6181 | moveToRoot(pCur); |
| 6182 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 6183 | return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 6184 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6185 | |
| 6186 | /* |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 6187 | ** Create a new BTree table. Write into *piTable the page |
| 6188 | ** number for the root page of the new table. |
| 6189 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 6190 | ** The type of type is determined by the flags parameter. Only the |
| 6191 | ** following values of flags are currently in use. Other values for |
| 6192 | ** flags might not work: |
| 6193 | ** |
| 6194 | ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys |
| 6195 | ** BTREE_ZERODATA Used for SQL indices |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6196 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6197 | static int btreeCreateTable(Btree *p, int *piTable, int flags){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6198 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6199 | MemPage *pRoot; |
| 6200 | Pgno pgnoRoot; |
| 6201 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6202 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6203 | assert( sqlite3BtreeHoldsMutex(p) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6204 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 6205 | /* Must start a transaction first */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6206 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
| 6207 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6208 | } |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 6209 | assert( !pBt->readOnly ); |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 6210 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6211 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 6212 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6213 | if( rc ){ |
| 6214 | return rc; |
| 6215 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6216 | #else |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6217 | if( pBt->autoVacuum ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6218 | Pgno pgnoMove; /* Move a page here to make room for the root-page */ |
| 6219 | MemPage *pPageMove; /* The page to move to. */ |
| 6220 | |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 6221 | /* Creating a new table may probably require moving an existing database |
| 6222 | ** to make room for the new tables root page. In case this page turns |
| 6223 | ** out to be an overflow page, delete all overflow page-map caches |
| 6224 | ** held by open cursors. |
| 6225 | */ |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 6226 | invalidateAllOverflowCache(pBt); |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 6227 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6228 | /* Read the value of meta[3] from the database to determine where the |
| 6229 | ** root page of the new table should go. meta[3] is the largest root-page |
| 6230 | ** created so far, so the new root-page is (meta[3]+1). |
| 6231 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6232 | rc = sqlite3BtreeGetMeta(p, 4, &pgnoRoot); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6233 | if( rc!=SQLITE_OK ){ |
| 6234 | return rc; |
| 6235 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6236 | pgnoRoot++; |
| 6237 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6238 | /* The new root-page may not be allocated on a pointer-map page, or the |
| 6239 | ** PENDING_BYTE page. |
| 6240 | */ |
drh | 7219043 | 2008-01-31 14:54:43 +0000 | [diff] [blame] | 6241 | while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) || |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6242 | pgnoRoot==PENDING_BYTE_PAGE(pBt) ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6243 | pgnoRoot++; |
| 6244 | } |
| 6245 | assert( pgnoRoot>=3 ); |
| 6246 | |
| 6247 | /* Allocate a page. The page that currently resides at pgnoRoot will |
| 6248 | ** be moved to the allocated page (unless the allocated page happens |
| 6249 | ** to reside at pgnoRoot). |
| 6250 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 6251 | rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6252 | if( rc!=SQLITE_OK ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6253 | return rc; |
| 6254 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6255 | |
| 6256 | if( pgnoMove!=pgnoRoot ){ |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 6257 | /* pgnoRoot is the page that will be used for the root-page of |
| 6258 | ** the new table (assuming an error did not occur). But we were |
| 6259 | ** allocated pgnoMove. If required (i.e. if it was not allocated |
| 6260 | ** by extending the file), the current page at position pgnoMove |
| 6261 | ** is already journaled. |
| 6262 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6263 | u8 eType; |
| 6264 | Pgno iPtrPage; |
| 6265 | |
| 6266 | releasePage(pPageMove); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 6267 | |
| 6268 | /* Move the page currently at pgnoRoot to pgnoMove. */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6269 | rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6270 | if( rc!=SQLITE_OK ){ |
| 6271 | return rc; |
| 6272 | } |
| 6273 | rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage); |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 6274 | if( rc!=SQLITE_OK || eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6275 | releasePage(pRoot); |
| 6276 | return rc; |
| 6277 | } |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 6278 | assert( eType!=PTRMAP_ROOTPAGE ); |
| 6279 | assert( eType!=PTRMAP_FREEPAGE ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6280 | rc = sqlite3PagerWrite(pRoot->pDbPage); |
danielk1977 | 5fd057a | 2005-03-09 13:09:43 +0000 | [diff] [blame] | 6281 | if( rc!=SQLITE_OK ){ |
| 6282 | releasePage(pRoot); |
| 6283 | return rc; |
| 6284 | } |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 6285 | rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6286 | releasePage(pRoot); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 6287 | |
| 6288 | /* Obtain the page at pgnoRoot */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6289 | if( rc!=SQLITE_OK ){ |
| 6290 | return rc; |
| 6291 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6292 | rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6293 | if( rc!=SQLITE_OK ){ |
| 6294 | return rc; |
| 6295 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6296 | rc = sqlite3PagerWrite(pRoot->pDbPage); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6297 | if( rc!=SQLITE_OK ){ |
| 6298 | releasePage(pRoot); |
| 6299 | return rc; |
| 6300 | } |
| 6301 | }else{ |
| 6302 | pRoot = pPageMove; |
| 6303 | } |
| 6304 | |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 6305 | /* Update the pointer-map and meta-data with the new root-page number. */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6306 | rc = ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0); |
| 6307 | if( rc ){ |
| 6308 | releasePage(pRoot); |
| 6309 | return rc; |
| 6310 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6311 | rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6312 | if( rc ){ |
| 6313 | releasePage(pRoot); |
| 6314 | return rc; |
| 6315 | } |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 6316 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6317 | }else{ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 6318 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6319 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6320 | } |
| 6321 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6322 | assert( sqlite3PagerIswriteable(pRoot->pDbPage) ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 6323 | zeroPage(pRoot, flags | PTF_LEAF); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6324 | sqlite3PagerUnref(pRoot->pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6325 | *piTable = (int)pgnoRoot; |
| 6326 | return SQLITE_OK; |
| 6327 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6328 | int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){ |
| 6329 | int rc; |
| 6330 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6331 | p->pBt->db = p->db; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6332 | rc = btreeCreateTable(p, piTable, flags); |
| 6333 | sqlite3BtreeLeave(p); |
| 6334 | return rc; |
| 6335 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6336 | |
| 6337 | /* |
| 6338 | ** Erase the given database page and all its children. Return |
| 6339 | ** the page to the freelist. |
| 6340 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6341 | static int clearDatabasePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6342 | BtShared *pBt, /* The BTree that contains the table */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6343 | Pgno pgno, /* Page number to clear */ |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6344 | int freePageFlag, /* Deallocate page if true */ |
| 6345 | int *pnChange |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6346 | ){ |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6347 | MemPage *pPage = 0; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6348 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6349 | unsigned char *pCell; |
| 6350 | int i; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6351 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6352 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 6353 | if( pgno>pagerPagecount(pBt) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 6354 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 6355 | } |
| 6356 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6357 | rc = getAndInitPage(pBt, pgno, &pPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6358 | if( rc ) goto cleardatabasepage_out; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6359 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 6360 | pCell = findCell(pPage, i); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6361 | if( !pPage->leaf ){ |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 6362 | rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6363 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6364 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6365 | rc = clearCell(pPage, pCell); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6366 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6367 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6368 | if( !pPage->leaf ){ |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 6369 | rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), 1, pnChange); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6370 | if( rc ) goto cleardatabasepage_out; |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6371 | }else if( pnChange ){ |
| 6372 | assert( pPage->intKey ); |
| 6373 | *pnChange += pPage->nCell; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6374 | } |
| 6375 | if( freePageFlag ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6376 | rc = freePage(pPage); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6377 | }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6378 | zeroPage(pPage, pPage->aData[0] | PTF_LEAF); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6379 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6380 | |
| 6381 | cleardatabasepage_out: |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6382 | releasePage(pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6383 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6384 | } |
| 6385 | |
| 6386 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 6387 | ** Delete all information from a single table in the database. iTable is |
| 6388 | ** the page number of the root of the table. After this routine returns, |
| 6389 | ** the root page is empty, but still exists. |
| 6390 | ** |
| 6391 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 6392 | ** read cursors on the table. Open write cursors are moved to the |
| 6393 | ** root of the table. |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6394 | ** |
| 6395 | ** If pnChange is not NULL, then table iTable must be an intkey table. The |
| 6396 | ** integer value pointed to by pnChange is incremented by the number of |
| 6397 | ** entries in the table. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6398 | */ |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6399 | int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6400 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6401 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6402 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6403 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6404 | if( p->inTrans!=TRANS_WRITE ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6405 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 6406 | }else if( (rc = checkReadLocks(p, iTable, 0, 1))!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6407 | /* nothing to do */ |
| 6408 | }else if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){ |
| 6409 | /* nothing to do */ |
| 6410 | }else{ |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 6411 | rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6412 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6413 | sqlite3BtreeLeave(p); |
| 6414 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6415 | } |
| 6416 | |
| 6417 | /* |
| 6418 | ** Erase all information in a table and add the root of the table to |
| 6419 | ** the freelist. Except, the root of the principle table (the one on |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 6420 | ** page 1) is never added to the freelist. |
| 6421 | ** |
| 6422 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 6423 | ** cursors on the table. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6424 | ** |
| 6425 | ** If AUTOVACUUM is enabled and the page at iTable is not the last |
| 6426 | ** root page in the database file, then the last root page |
| 6427 | ** in the database file is moved into the slot formerly occupied by |
| 6428 | ** iTable and that last slot formerly occupied by the last root page |
| 6429 | ** is added to the freelist instead of iTable. In this say, all |
| 6430 | ** root pages are kept at the beginning of the database file, which |
| 6431 | ** is necessary for AUTOVACUUM to work right. *piMoved is set to the |
| 6432 | ** page number that used to be the last root page in the file before |
| 6433 | ** the move. If no page gets moved, *piMoved is set to 0. |
| 6434 | ** The last root page is recorded in meta[3] and the value of |
| 6435 | ** meta[3] is updated by this procedure. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6436 | */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 6437 | static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6438 | int rc; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6439 | MemPage *pPage = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6440 | BtShared *pBt = p->pBt; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6441 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6442 | assert( sqlite3BtreeHoldsMutex(p) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6443 | if( p->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 6444 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6445 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6446 | |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 6447 | /* It is illegal to drop a table if any cursors are open on the |
| 6448 | ** database. This is because in auto-vacuum mode the backend may |
| 6449 | ** need to move another root-page to fill a gap left by the deleted |
| 6450 | ** root page. If an open cursor was using this page a problem would |
| 6451 | ** occur. |
| 6452 | */ |
| 6453 | if( pBt->pCursor ){ |
| 6454 | return SQLITE_LOCKED; |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 6455 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6456 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6457 | rc = sqlite3BtreeGetPage(pBt, (Pgno)iTable, &pPage, 0); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6458 | if( rc ) return rc; |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6459 | rc = sqlite3BtreeClearTable(p, iTable, 0); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6460 | if( rc ){ |
| 6461 | releasePage(pPage); |
| 6462 | return rc; |
| 6463 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6464 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6465 | *piMoved = 0; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6466 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6467 | if( iTable>1 ){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6468 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6469 | rc = freePage(pPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6470 | releasePage(pPage); |
| 6471 | #else |
| 6472 | if( pBt->autoVacuum ){ |
| 6473 | Pgno maxRootPgno; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6474 | rc = sqlite3BtreeGetMeta(p, 4, &maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6475 | if( rc!=SQLITE_OK ){ |
| 6476 | releasePage(pPage); |
| 6477 | return rc; |
| 6478 | } |
| 6479 | |
| 6480 | if( iTable==maxRootPgno ){ |
| 6481 | /* If the table being dropped is the table with the largest root-page |
| 6482 | ** number in the database, put the root page on the free list. |
| 6483 | */ |
| 6484 | rc = freePage(pPage); |
| 6485 | releasePage(pPage); |
| 6486 | if( rc!=SQLITE_OK ){ |
| 6487 | return rc; |
| 6488 | } |
| 6489 | }else{ |
| 6490 | /* The table being dropped does not have the largest root-page |
| 6491 | ** number in the database. So move the page that does into the |
| 6492 | ** gap left by the deleted root-page. |
| 6493 | */ |
| 6494 | MemPage *pMove; |
| 6495 | releasePage(pPage); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6496 | rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6497 | if( rc!=SQLITE_OK ){ |
| 6498 | return rc; |
| 6499 | } |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 6500 | rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6501 | releasePage(pMove); |
| 6502 | if( rc!=SQLITE_OK ){ |
| 6503 | return rc; |
| 6504 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6505 | rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6506 | if( rc!=SQLITE_OK ){ |
| 6507 | return rc; |
| 6508 | } |
| 6509 | rc = freePage(pMove); |
| 6510 | releasePage(pMove); |
| 6511 | if( rc!=SQLITE_OK ){ |
| 6512 | return rc; |
| 6513 | } |
| 6514 | *piMoved = maxRootPgno; |
| 6515 | } |
| 6516 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6517 | /* Set the new 'max-root-page' value in the database header. This |
| 6518 | ** is the old value less one, less one more if that happens to |
| 6519 | ** be a root-page number, less one again if that is the |
| 6520 | ** PENDING_BYTE_PAGE. |
| 6521 | */ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 6522 | maxRootPgno--; |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6523 | if( maxRootPgno==PENDING_BYTE_PAGE(pBt) ){ |
| 6524 | maxRootPgno--; |
| 6525 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6526 | if( maxRootPgno==PTRMAP_PAGENO(pBt, maxRootPgno) ){ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 6527 | maxRootPgno--; |
| 6528 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6529 | assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) ); |
| 6530 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6531 | rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6532 | }else{ |
| 6533 | rc = freePage(pPage); |
| 6534 | releasePage(pPage); |
| 6535 | } |
| 6536 | #endif |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6537 | }else{ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6538 | /* If sqlite3BtreeDropTable was called on page 1. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6539 | zeroPage(pPage, PTF_INTKEY|PTF_LEAF ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6540 | releasePage(pPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6541 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6542 | return rc; |
| 6543 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6544 | int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){ |
| 6545 | int rc; |
| 6546 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6547 | p->pBt->db = p->db; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6548 | rc = btreeDropTable(p, iTable, piMoved); |
| 6549 | sqlite3BtreeLeave(p); |
| 6550 | return rc; |
| 6551 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6552 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 6553 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6554 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6555 | ** Read the meta-information out of a database file. Meta[0] |
| 6556 | ** is the number of free pages currently in the database. Meta[1] |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 6557 | ** through meta[15] are available for use by higher layers. Meta[0] |
| 6558 | ** is read-only, the others are read/write. |
| 6559 | ** |
| 6560 | ** The schema layer numbers meta values differently. At the schema |
| 6561 | ** layer (and the SetCookie and ReadCookie opcodes) the number of |
| 6562 | ** 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] | 6563 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6564 | int sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){ |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 6565 | DbPage *pDbPage = 0; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6566 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6567 | unsigned char *pP1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6568 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6569 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6570 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6571 | pBt->db = p->db; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6572 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6573 | /* Reading a meta-data value requires a read-lock on page 1 (and hence |
| 6574 | ** the sqlite_master table. We grab this lock regardless of whether or |
| 6575 | ** not the SQLITE_ReadUncommitted flag is set (the table rooted at page |
| 6576 | ** 1 is treated as a special case by queryTableLock() and lockTable()). |
| 6577 | */ |
| 6578 | rc = queryTableLock(p, 1, READ_LOCK); |
| 6579 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6580 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6581 | return rc; |
| 6582 | } |
| 6583 | |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6584 | assert( idx>=0 && idx<=15 ); |
danielk1977 | d9f6c53 | 2008-09-19 16:39:38 +0000 | [diff] [blame] | 6585 | if( pBt->pPage1 ){ |
| 6586 | /* The b-tree is already holding a reference to page 1 of the database |
| 6587 | ** file. In this case the required meta-data value can be read directly |
| 6588 | ** from the page data of this reference. This is slightly faster than |
| 6589 | ** requesting a new reference from the pager layer. |
| 6590 | */ |
| 6591 | pP1 = (unsigned char *)pBt->pPage1->aData; |
| 6592 | }else{ |
| 6593 | /* The b-tree does not have a reference to page 1 of the database file. |
| 6594 | ** Obtain one from the pager layer. |
| 6595 | */ |
danielk1977 | ea89730 | 2008-09-19 15:10:58 +0000 | [diff] [blame] | 6596 | rc = sqlite3PagerGet(pBt->pPager, 1, &pDbPage); |
| 6597 | if( rc ){ |
| 6598 | sqlite3BtreeLeave(p); |
| 6599 | return rc; |
| 6600 | } |
| 6601 | pP1 = (unsigned char *)sqlite3PagerGetData(pDbPage); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6602 | } |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6603 | *pMeta = get4byte(&pP1[36 + idx*4]); |
danielk1977 | ea89730 | 2008-09-19 15:10:58 +0000 | [diff] [blame] | 6604 | |
danielk1977 | d9f6c53 | 2008-09-19 16:39:38 +0000 | [diff] [blame] | 6605 | /* If the b-tree is not holding a reference to page 1, then one was |
| 6606 | ** requested from the pager layer in the above block. Release it now. |
| 6607 | */ |
danielk1977 | ea89730 | 2008-09-19 15:10:58 +0000 | [diff] [blame] | 6608 | if( !pBt->pPage1 ){ |
| 6609 | sqlite3PagerUnref(pDbPage); |
| 6610 | } |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 6611 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6612 | /* If autovacuumed is disabled in this build but we are trying to |
| 6613 | ** access an autovacuumed database, then make the database readonly. |
| 6614 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6615 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 6616 | if( idx==4 && *pMeta>0 ) pBt->readOnly = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6617 | #endif |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 6618 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6619 | /* Grab the read-lock on page 1. */ |
| 6620 | rc = lockTable(p, 1, READ_LOCK); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6621 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6622 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6623 | } |
| 6624 | |
| 6625 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6626 | ** Write meta-information back into the database. Meta[0] is |
| 6627 | ** read-only and may not be written. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6628 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6629 | int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){ |
| 6630 | BtShared *pBt = p->pBt; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6631 | unsigned char *pP1; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6632 | int rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6633 | assert( idx>=1 && idx<=15 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6634 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6635 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6636 | if( p->inTrans!=TRANS_WRITE ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6637 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
| 6638 | }else{ |
| 6639 | assert( pBt->pPage1!=0 ); |
| 6640 | pP1 = pBt->pPage1->aData; |
| 6641 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 6642 | if( rc==SQLITE_OK ){ |
| 6643 | put4byte(&pP1[36 + idx*4], iMeta); |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 +0000 | [diff] [blame] | 6644 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6645 | if( idx==7 ){ |
| 6646 | assert( pBt->autoVacuum || iMeta==0 ); |
| 6647 | assert( iMeta==0 || iMeta==1 ); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 6648 | pBt->incrVacuum = (u8)iMeta; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6649 | } |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 +0000 | [diff] [blame] | 6650 | #endif |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6651 | } |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 6652 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6653 | sqlite3BtreeLeave(p); |
| 6654 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6655 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 6656 | |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 6657 | /* |
| 6658 | ** Return the flag byte at the beginning of the page that the cursor |
| 6659 | ** is currently pointing to. |
| 6660 | */ |
| 6661 | int sqlite3BtreeFlags(BtCursor *pCur){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6662 | /* TODO: What about CURSOR_REQUIRESEEK state? Probably need to call |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 6663 | ** restoreCursorPosition() here. |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6664 | */ |
danielk1977 | e448dc4 | 2008-01-02 11:50:51 +0000 | [diff] [blame] | 6665 | MemPage *pPage; |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 6666 | restoreCursorPosition(pCur); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6667 | pPage = pCur->apPage[pCur->iPage]; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6668 | assert( cursorHoldsMutex(pCur) ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 6669 | assert( pPage->pBt==pCur->pBt ); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 6670 | return pPage ? pPage->aData[pPage->hdrOffset] : 0; |
| 6671 | } |
| 6672 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6673 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6674 | /* |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6675 | ** Return the pager associated with a BTree. This routine is used for |
| 6676 | ** testing and debugging only. |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6677 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6678 | Pager *sqlite3BtreePager(Btree *p){ |
| 6679 | return p->pBt->pPager; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6680 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6681 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6682 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6683 | /* |
| 6684 | ** Append a message to the error message string. |
| 6685 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6686 | static void checkAppendMsg( |
| 6687 | IntegrityCk *pCheck, |
| 6688 | char *zMsg1, |
| 6689 | const char *zFormat, |
| 6690 | ... |
| 6691 | ){ |
| 6692 | va_list ap; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6693 | if( !pCheck->mxErr ) return; |
| 6694 | pCheck->mxErr--; |
| 6695 | pCheck->nErr++; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6696 | va_start(ap, zFormat); |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 6697 | if( pCheck->errMsg.nChar ){ |
| 6698 | sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6699 | } |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 6700 | if( zMsg1 ){ |
| 6701 | sqlite3StrAccumAppend(&pCheck->errMsg, zMsg1, -1); |
| 6702 | } |
| 6703 | sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap); |
| 6704 | va_end(ap); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 6705 | if( pCheck->errMsg.mallocFailed ){ |
| 6706 | pCheck->mallocFailed = 1; |
| 6707 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6708 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6709 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6710 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6711 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6712 | /* |
| 6713 | ** Add 1 to the reference count for page iPage. If this is the second |
| 6714 | ** reference to the page, add an error message to pCheck->zErrMsg. |
| 6715 | ** Return 1 if there are 2 ore more references to the page and 0 if |
| 6716 | ** if this is the first reference to the page. |
| 6717 | ** |
| 6718 | ** Also check that the page number is in bounds. |
| 6719 | */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 6720 | static int checkRef(IntegrityCk *pCheck, Pgno iPage, char *zContext){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6721 | if( iPage==0 ) return 1; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 6722 | if( iPage>pCheck->nPage ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6723 | checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6724 | return 1; |
| 6725 | } |
| 6726 | if( pCheck->anRef[iPage]==1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6727 | checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6728 | return 1; |
| 6729 | } |
| 6730 | return (pCheck->anRef[iPage]++)>1; |
| 6731 | } |
| 6732 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6733 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6734 | /* |
| 6735 | ** Check that the entry in the pointer-map for page iChild maps to |
| 6736 | ** page iParent, pointer type ptrType. If not, append an error message |
| 6737 | ** to pCheck. |
| 6738 | */ |
| 6739 | static void checkPtrmap( |
| 6740 | IntegrityCk *pCheck, /* Integrity check context */ |
| 6741 | Pgno iChild, /* Child page number */ |
| 6742 | u8 eType, /* Expected pointer map type */ |
| 6743 | Pgno iParent, /* Expected pointer map parent page number */ |
| 6744 | char *zContext /* Context description (used for error msg) */ |
| 6745 | ){ |
| 6746 | int rc; |
| 6747 | u8 ePtrmapType; |
| 6748 | Pgno iPtrmapParent; |
| 6749 | |
| 6750 | rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent); |
| 6751 | if( rc!=SQLITE_OK ){ |
drh | e43ba70 | 2008-12-05 22:40:08 +0000 | [diff] [blame] | 6752 | if( rc==SQLITE_NOMEM ) pCheck->mallocFailed = 1; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6753 | checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild); |
| 6754 | return; |
| 6755 | } |
| 6756 | |
| 6757 | if( ePtrmapType!=eType || iPtrmapParent!=iParent ){ |
| 6758 | checkAppendMsg(pCheck, zContext, |
| 6759 | "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", |
| 6760 | iChild, eType, iParent, ePtrmapType, iPtrmapParent); |
| 6761 | } |
| 6762 | } |
| 6763 | #endif |
| 6764 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6765 | /* |
| 6766 | ** Check the integrity of the freelist or of an overflow page list. |
| 6767 | ** Verify that the number of pages on the list is N. |
| 6768 | */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6769 | static void checkList( |
| 6770 | IntegrityCk *pCheck, /* Integrity checking context */ |
| 6771 | int isFreeList, /* True for a freelist. False for overflow page list */ |
| 6772 | int iPage, /* Page number for first page in the list */ |
| 6773 | int N, /* Expected number of pages in the list */ |
| 6774 | char *zContext /* Context for error messages */ |
| 6775 | ){ |
| 6776 | int i; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6777 | int expected = N; |
| 6778 | int iFirst = iPage; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6779 | while( N-- > 0 && pCheck->mxErr ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6780 | DbPage *pOvflPage; |
| 6781 | unsigned char *pOvflData; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6782 | if( iPage<1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6783 | checkAppendMsg(pCheck, zContext, |
| 6784 | "%d of %d pages missing from overflow list starting at %d", |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6785 | N+1, expected, iFirst); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6786 | break; |
| 6787 | } |
| 6788 | if( checkRef(pCheck, iPage, zContext) ) break; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6789 | if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6790 | checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6791 | break; |
| 6792 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6793 | pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6794 | if( isFreeList ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6795 | int n = get4byte(&pOvflData[4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6796 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6797 | if( pCheck->pBt->autoVacuum ){ |
| 6798 | checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext); |
| 6799 | } |
| 6800 | #endif |
drh | 45b1fac | 2008-07-04 17:52:42 +0000 | [diff] [blame] | 6801 | if( n>pCheck->pBt->usableSize/4-2 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6802 | checkAppendMsg(pCheck, zContext, |
| 6803 | "freelist leaf count too big on page %d", iPage); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 6804 | N--; |
| 6805 | }else{ |
| 6806 | for(i=0; i<n; i++){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6807 | Pgno iFreePage = get4byte(&pOvflData[8+i*4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6808 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6809 | if( pCheck->pBt->autoVacuum ){ |
| 6810 | checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext); |
| 6811 | } |
| 6812 | #endif |
| 6813 | checkRef(pCheck, iFreePage, zContext); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 6814 | } |
| 6815 | N -= n; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6816 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6817 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6818 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6819 | else{ |
| 6820 | /* If this database supports auto-vacuum and iPage is not the last |
| 6821 | ** page in this overflow list, check that the pointer-map entry for |
| 6822 | ** the following page matches iPage. |
| 6823 | */ |
| 6824 | if( pCheck->pBt->autoVacuum && N>0 ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6825 | i = get4byte(pOvflData); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6826 | checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext); |
| 6827 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6828 | } |
| 6829 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6830 | iPage = get4byte(pOvflData); |
| 6831 | sqlite3PagerUnref(pOvflPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6832 | } |
| 6833 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6834 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6835 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6836 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6837 | /* |
| 6838 | ** Do various sanity checks on a single page of a tree. Return |
| 6839 | ** the tree depth. Root pages return 0. Parents of root pages |
| 6840 | ** return 1, and so forth. |
| 6841 | ** |
| 6842 | ** These checks are done: |
| 6843 | ** |
| 6844 | ** 1. Make sure that cells and freeblocks do not overlap |
| 6845 | ** but combine to completely cover the page. |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6846 | ** NO 2. Make sure cell keys are in order. |
| 6847 | ** NO 3. Make sure no key is less than or equal to zLowerBound. |
| 6848 | ** NO 4. Make sure no key is greater than or equal to zUpperBound. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6849 | ** 5. Check the integrity of overflow pages. |
| 6850 | ** 6. Recursively call checkTreePage on all children. |
| 6851 | ** 7. Verify that the depth of all children is the same. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6852 | ** 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] | 6853 | ** the root of the tree. |
| 6854 | */ |
| 6855 | static int checkTreePage( |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 6856 | IntegrityCk *pCheck, /* Context for the sanity check */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6857 | int iPage, /* Page number of the page to check */ |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6858 | char *zParentContext /* Parent context */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6859 | ){ |
| 6860 | MemPage *pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6861 | int i, rc, depth, d2, pgno, cnt; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6862 | int hdr, cellStart; |
| 6863 | int nCell; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6864 | u8 *data; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6865 | BtShared *pBt; |
drh | 4f26bb6 | 2005-09-08 14:17:20 +0000 | [diff] [blame] | 6866 | int usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6867 | char zContext[100]; |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 6868 | char *hit = 0; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6869 | |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6870 | sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage); |
danielk1977 | ef73ee9 | 2004-11-06 12:26:07 +0000 | [diff] [blame] | 6871 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6872 | /* Check that the page exists |
| 6873 | */ |
drh | d9cb6ac | 2005-10-20 07:28:17 +0000 | [diff] [blame] | 6874 | pBt = pCheck->pBt; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 6875 | usableSize = pBt->usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6876 | if( iPage==0 ) return 0; |
| 6877 | if( checkRef(pCheck, iPage, zParentContext) ) return 0; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6878 | if( (rc = sqlite3BtreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){ |
drh | e43ba70 | 2008-12-05 22:40:08 +0000 | [diff] [blame] | 6879 | if( rc==SQLITE_NOMEM ) pCheck->mallocFailed = 1; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6880 | checkAppendMsg(pCheck, zContext, |
| 6881 | "unable to get the page. error code=%d", rc); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6882 | return 0; |
| 6883 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6884 | if( (rc = sqlite3BtreeInitPage(pPage))!=0 ){ |
drh | e43ba70 | 2008-12-05 22:40:08 +0000 | [diff] [blame] | 6885 | if( rc==SQLITE_NOMEM ) pCheck->mallocFailed = 1; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6886 | checkAppendMsg(pCheck, zContext, |
| 6887 | "sqlite3BtreeInitPage() returns error code %d", rc); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 6888 | releasePage(pPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6889 | return 0; |
| 6890 | } |
| 6891 | |
| 6892 | /* Check out all the cells. |
| 6893 | */ |
| 6894 | depth = 0; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6895 | for(i=0; i<pPage->nCell && pCheck->mxErr; i++){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 6896 | u8 *pCell; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 6897 | u32 sz; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 6898 | CellInfo info; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6899 | |
| 6900 | /* Check payload overflow pages |
| 6901 | */ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6902 | sqlite3_snprintf(sizeof(zContext), zContext, |
| 6903 | "On tree page %d cell %d: ", iPage, i); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 6904 | pCell = findCell(pPage,i); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6905 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 6906 | sz = info.nData; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 6907 | if( !pPage->intKey ) sz += (int)info.nKey; |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 6908 | assert( sz==info.nPayload ); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 6909 | if( sz>info.nLocal ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 6910 | int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6911 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
| 6912 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6913 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6914 | checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6915 | } |
| 6916 | #endif |
| 6917 | checkList(pCheck, 0, pgnoOvfl, nPage, zContext); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6918 | } |
| 6919 | |
| 6920 | /* Check sanity of left child page. |
| 6921 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6922 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6923 | pgno = get4byte(pCell); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6924 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6925 | if( pBt->autoVacuum ){ |
| 6926 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext); |
| 6927 | } |
| 6928 | #endif |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 6929 | d2 = checkTreePage(pCheck, pgno, zContext); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6930 | if( i>0 && d2!=depth ){ |
| 6931 | checkAppendMsg(pCheck, zContext, "Child page depth differs"); |
| 6932 | } |
| 6933 | depth = d2; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6934 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6935 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6936 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6937 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6938 | sqlite3_snprintf(sizeof(zContext), zContext, |
| 6939 | "On page %d at right child: ", iPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6940 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6941 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6942 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6943 | } |
| 6944 | #endif |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 6945 | checkTreePage(pCheck, pgno, zContext); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6946 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6947 | |
| 6948 | /* Check for complete coverage of the page |
| 6949 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6950 | data = pPage->aData; |
| 6951 | hdr = pPage->hdrOffset; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 6952 | hit = sqlite3PageMalloc( pBt->pageSize ); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 6953 | if( hit==0 ){ |
| 6954 | pCheck->mallocFailed = 1; |
| 6955 | }else{ |
shane | 5780ebd | 2008-11-11 17:36:30 +0000 | [diff] [blame] | 6956 | u16 contentOffset = get2byte(&data[hdr+5]); |
| 6957 | if (contentOffset > usableSize) { |
| 6958 | checkAppendMsg(pCheck, 0, |
| 6959 | "Corruption detected in header on page %d",iPage,0); |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 6960 | goto check_page_abort; |
shane | 5780ebd | 2008-11-11 17:36:30 +0000 | [diff] [blame] | 6961 | } |
| 6962 | memset(hit+contentOffset, 0, usableSize-contentOffset); |
| 6963 | memset(hit, 1, contentOffset); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6964 | nCell = get2byte(&data[hdr+3]); |
| 6965 | cellStart = hdr + 12 - 4*pPage->leaf; |
| 6966 | for(i=0; i<nCell; i++){ |
| 6967 | int pc = get2byte(&data[cellStart+i*2]); |
danielk1977 | daca543 | 2008-08-25 11:57:16 +0000 | [diff] [blame] | 6968 | u16 size = 1024; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6969 | int j; |
danielk1977 | daca543 | 2008-08-25 11:57:16 +0000 | [diff] [blame] | 6970 | if( pc<=usableSize ){ |
| 6971 | size = cellSizePtr(pPage, &data[pc]); |
| 6972 | } |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 6973 | if( (pc+size-1)>=usableSize || pc<0 ){ |
| 6974 | checkAppendMsg(pCheck, 0, |
| 6975 | "Corruption detected in cell %d on page %d",i,iPage,0); |
| 6976 | }else{ |
| 6977 | for(j=pc+size-1; j>=pc; j--) hit[j]++; |
| 6978 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6979 | } |
| 6980 | for(cnt=0, i=get2byte(&data[hdr+1]); i>0 && i<usableSize && cnt<10000; |
| 6981 | cnt++){ |
| 6982 | int size = get2byte(&data[i+2]); |
| 6983 | int j; |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 6984 | if( (i+size-1)>=usableSize || i<0 ){ |
| 6985 | checkAppendMsg(pCheck, 0, |
| 6986 | "Corruption detected in cell %d on page %d",i,iPage,0); |
| 6987 | }else{ |
| 6988 | for(j=i+size-1; j>=i; j--) hit[j]++; |
| 6989 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6990 | i = get2byte(&data[i]); |
| 6991 | } |
| 6992 | for(i=cnt=0; i<usableSize; i++){ |
| 6993 | if( hit[i]==0 ){ |
| 6994 | cnt++; |
| 6995 | }else if( hit[i]>1 ){ |
| 6996 | checkAppendMsg(pCheck, 0, |
| 6997 | "Multiple uses for byte %d of page %d", i, iPage); |
| 6998 | break; |
| 6999 | } |
| 7000 | } |
| 7001 | if( cnt!=data[hdr+7] ){ |
| 7002 | checkAppendMsg(pCheck, 0, |
| 7003 | "Fragmented space is %d byte reported as %d on page %d", |
| 7004 | cnt, data[hdr+7], iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7005 | } |
| 7006 | } |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 7007 | check_page_abort: |
| 7008 | if (hit) sqlite3PageFree(hit); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 7009 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7010 | releasePage(pPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 7011 | return depth+1; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7012 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 7013 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7014 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 7015 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7016 | /* |
| 7017 | ** This routine does a complete check of the given BTree file. aRoot[] is |
| 7018 | ** an array of pages numbers were each page number is the root page of |
| 7019 | ** a table. nRoot is the number of entries in aRoot. |
| 7020 | ** |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 7021 | ** Write the number of error seen in *pnErr. Except for some memory |
drh | e43ba70 | 2008-12-05 22:40:08 +0000 | [diff] [blame] | 7022 | ** allocation errors, an error message held in memory obtained from |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 7023 | ** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is |
drh | e43ba70 | 2008-12-05 22:40:08 +0000 | [diff] [blame] | 7024 | ** returned. If a memory allocation error occurs, NULL is returned. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7025 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 7026 | char *sqlite3BtreeIntegrityCheck( |
| 7027 | Btree *p, /* The btree to be checked */ |
| 7028 | int *aRoot, /* An array of root pages numbers for individual trees */ |
| 7029 | int nRoot, /* Number of entries in aRoot[] */ |
| 7030 | int mxErr, /* Stop reporting errors after this many */ |
| 7031 | int *pnErr /* Write number of errors seen to this variable */ |
| 7032 | ){ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 7033 | Pgno i; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7034 | int nRef; |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 7035 | IntegrityCk sCheck; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7036 | BtShared *pBt = p->pBt; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 7037 | char zErr[100]; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7038 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7039 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7040 | pBt->db = p->db; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7041 | nRef = sqlite3PagerRefcount(pBt->pPager); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7042 | if( lockBtreeWithRetry(p)!=SQLITE_OK ){ |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 7043 | *pnErr = 1; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7044 | sqlite3BtreeLeave(p); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 7045 | return sqlite3DbStrDup(0, "cannot acquire a read lock on the database"); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 7046 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7047 | sCheck.pBt = pBt; |
| 7048 | sCheck.pPager = pBt->pPager; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 7049 | sCheck.nPage = pagerPagecount(sCheck.pBt); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 7050 | sCheck.mxErr = mxErr; |
| 7051 | sCheck.nErr = 0; |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 7052 | sCheck.mallocFailed = 0; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 7053 | *pnErr = 0; |
danielk1977 | e5321f0 | 2007-04-27 07:05:44 +0000 | [diff] [blame] | 7054 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 7055 | if( pBt->nTrunc!=0 ){ |
| 7056 | sCheck.nPage = pBt->nTrunc; |
| 7057 | } |
| 7058 | #endif |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 7059 | if( sCheck.nPage==0 ){ |
| 7060 | unlockBtreeIfUnused(pBt); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7061 | sqlite3BtreeLeave(p); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 7062 | return 0; |
| 7063 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 7064 | sCheck.anRef = sqlite3Malloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) ); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 7065 | if( !sCheck.anRef ){ |
| 7066 | unlockBtreeIfUnused(pBt); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 7067 | *pnErr = 1; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7068 | sqlite3BtreeLeave(p); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 7069 | return 0; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 7070 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 7071 | for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; } |
drh | 42cac6d | 2004-11-20 20:31:11 +0000 | [diff] [blame] | 7072 | i = PENDING_BYTE_PAGE(pBt); |
drh | 1f59571 | 2004-06-15 01:40:29 +0000 | [diff] [blame] | 7073 | if( i<=sCheck.nPage ){ |
| 7074 | sCheck.anRef[i] = 1; |
| 7075 | } |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 7076 | sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), 20000); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7077 | |
| 7078 | /* Check the integrity of the freelist |
| 7079 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 7080 | checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), |
| 7081 | get4byte(&pBt->pPage1->aData[36]), "Main freelist: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7082 | |
| 7083 | /* Check all the tables. |
| 7084 | */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 7085 | for(i=0; (int)i<nRoot && sCheck.mxErr; i++){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 7086 | if( aRoot[i]==0 ) continue; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 7087 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 7088 | if( pBt->autoVacuum && aRoot[i]>1 ){ |
| 7089 | checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0); |
| 7090 | } |
| 7091 | #endif |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 7092 | checkTreePage(&sCheck, aRoot[i], "List of tree roots: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7093 | } |
| 7094 | |
| 7095 | /* Make sure every page in the file is referenced |
| 7096 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 7097 | for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7098 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7099 | if( sCheck.anRef[i]==0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7100 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7101 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7102 | #else |
| 7103 | /* If the database supports auto-vacuum, make sure no tables contain |
| 7104 | ** references to pointer-map pages. |
| 7105 | */ |
| 7106 | if( sCheck.anRef[i]==0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 7107 | (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7108 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
| 7109 | } |
| 7110 | if( sCheck.anRef[i]!=0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 7111 | (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7112 | checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i); |
| 7113 | } |
| 7114 | #endif |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7115 | } |
| 7116 | |
| 7117 | /* Make sure this analysis did not leave any unref() pages |
| 7118 | */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 7119 | unlockBtreeIfUnused(pBt); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7120 | if( nRef != sqlite3PagerRefcount(pBt->pPager) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7121 | checkAppendMsg(&sCheck, 0, |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7122 | "Outstanding page count goes from %d to %d during this analysis", |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7123 | nRef, sqlite3PagerRefcount(pBt->pPager) |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7124 | ); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7125 | } |
| 7126 | |
| 7127 | /* Clean up and report errors. |
| 7128 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7129 | sqlite3BtreeLeave(p); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 7130 | sqlite3_free(sCheck.anRef); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 7131 | if( sCheck.mallocFailed ){ |
| 7132 | sqlite3StrAccumReset(&sCheck.errMsg); |
| 7133 | *pnErr = sCheck.nErr+1; |
| 7134 | return 0; |
| 7135 | } |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 7136 | *pnErr = sCheck.nErr; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 7137 | if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg); |
| 7138 | return sqlite3StrAccumFinish(&sCheck.errMsg); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7139 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 7140 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 7141 | |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 7142 | /* |
| 7143 | ** Return the full pathname of the underlying database file. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 7144 | ** |
| 7145 | ** The pager filename is invariant as long as the pager is |
| 7146 | ** open so it is safe to access without the BtShared mutex. |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 7147 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7148 | const char *sqlite3BtreeGetFilename(Btree *p){ |
| 7149 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7150 | return sqlite3PagerFilename(p->pBt->pPager); |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 7151 | } |
| 7152 | |
| 7153 | /* |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 7154 | ** Return the pathname of the directory that contains the database file. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 7155 | ** |
| 7156 | ** The pager directory name is invariant as long as the pager is |
| 7157 | ** open so it is safe to access without the BtShared mutex. |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 7158 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7159 | const char *sqlite3BtreeGetDirname(Btree *p){ |
| 7160 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7161 | return sqlite3PagerDirname(p->pBt->pPager); |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 7162 | } |
| 7163 | |
| 7164 | /* |
| 7165 | ** Return the pathname of the journal file for this database. The return |
| 7166 | ** value of this routine is the same regardless of whether the journal file |
| 7167 | ** has been created or not. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 7168 | ** |
| 7169 | ** The pager journal filename is invariant as long as the pager is |
| 7170 | ** open so it is safe to access without the BtShared mutex. |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 7171 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7172 | const char *sqlite3BtreeGetJournalname(Btree *p){ |
| 7173 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7174 | return sqlite3PagerJournalname(p->pBt->pPager); |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 7175 | } |
| 7176 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 7177 | #ifndef SQLITE_OMIT_VACUUM |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 7178 | /* |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 7179 | ** Copy the complete content of pBtFrom into pBtTo. A transaction |
| 7180 | ** must be active for both files. |
| 7181 | ** |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7182 | ** The size of file pTo may be reduced by this operation. |
| 7183 | ** If anything goes wrong, the transaction on pTo is rolled back. |
| 7184 | ** |
| 7185 | ** If successful, CommitPhaseOne() may be called on pTo before returning. |
| 7186 | ** The caller should finish committing the transaction on pTo by calling |
| 7187 | ** sqlite3BtreeCommit(). |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 7188 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7189 | static int btreeCopyFile(Btree *pTo, Btree *pFrom){ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 7190 | int rc = SQLITE_OK; |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7191 | Pgno i; |
| 7192 | |
| 7193 | Pgno nFromPage; /* Number of pages in pFrom */ |
| 7194 | Pgno nToPage; /* Number of pages in pTo */ |
| 7195 | Pgno nNewPage; /* Number of pages in pTo after the copy */ |
| 7196 | |
| 7197 | Pgno iSkip; /* Pending byte page in pTo */ |
| 7198 | int nToPageSize; /* Page size of pTo in bytes */ |
| 7199 | int nFromPageSize; /* Page size of pFrom in bytes */ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 7200 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7201 | BtShared *pBtTo = pTo->pBt; |
| 7202 | BtShared *pBtFrom = pFrom->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7203 | pBtTo->db = pTo->db; |
| 7204 | pBtFrom->db = pFrom->db; |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7205 | |
| 7206 | nToPageSize = pBtTo->pageSize; |
| 7207 | nFromPageSize = pBtFrom->pageSize; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7208 | |
| 7209 | if( pTo->inTrans!=TRANS_WRITE || pFrom->inTrans!=TRANS_WRITE ){ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 7210 | return SQLITE_ERROR; |
| 7211 | } |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7212 | if( pBtTo->pCursor ){ |
| 7213 | return SQLITE_BUSY; |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 7214 | } |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 7215 | |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 7216 | nToPage = pagerPagecount(pBtTo); |
| 7217 | nFromPage = pagerPagecount(pBtFrom); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7218 | iSkip = PENDING_BYTE_PAGE(pBtTo); |
| 7219 | |
| 7220 | /* Variable nNewPage is the number of pages required to store the |
| 7221 | ** contents of pFrom using the current page-size of pTo. |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 7222 | */ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 7223 | nNewPage = (Pgno) |
| 7224 | (((i64)nFromPage*(i64)nFromPageSize+(i64)nToPageSize-1)/(i64)nToPageSize); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7225 | |
| 7226 | for(i=1; rc==SQLITE_OK && (i<=nToPage || i<=nNewPage); i++){ |
| 7227 | |
| 7228 | /* Journal the original page. |
| 7229 | ** |
| 7230 | ** iSkip is the page number of the locking page (PENDING_BYTE_PAGE) |
| 7231 | ** in database *pTo (before the copy). This page is never written |
| 7232 | ** into the journal file. Unless i==iSkip or the page was not |
| 7233 | ** present in pTo before the copy operation, journal page i from pTo. |
| 7234 | */ |
| 7235 | if( i!=iSkip && i<=nToPage ){ |
danielk1977 | 4abd544 | 2008-05-05 15:26:50 +0000 | [diff] [blame] | 7236 | DbPage *pDbPage = 0; |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7237 | rc = sqlite3PagerGet(pBtTo->pPager, i, &pDbPage); |
danielk1977 | 4abd544 | 2008-05-05 15:26:50 +0000 | [diff] [blame] | 7238 | if( rc==SQLITE_OK ){ |
| 7239 | rc = sqlite3PagerWrite(pDbPage); |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 7240 | if( rc==SQLITE_OK && i>nFromPage ){ |
| 7241 | /* Yeah. It seems wierd to call DontWrite() right after Write(). But |
| 7242 | ** that is because the names of those procedures do not exactly |
| 7243 | ** represent what they do. Write() really means "put this page in the |
| 7244 | ** rollback journal and mark it as dirty so that it will be written |
| 7245 | ** to the database file later." DontWrite() undoes the second part of |
| 7246 | ** that and prevents the page from being written to the database. The |
| 7247 | ** page is still on the rollback journal, though. And that is the |
| 7248 | ** whole point of this block: to put pages on the rollback journal. |
| 7249 | */ |
danielk1977 | a1fa00d | 2008-08-27 15:16:33 +0000 | [diff] [blame] | 7250 | rc = sqlite3PagerDontWrite(pDbPage); |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 7251 | } |
| 7252 | sqlite3PagerUnref(pDbPage); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7253 | } |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7254 | } |
| 7255 | |
| 7256 | /* Overwrite the data in page i of the target database */ |
| 7257 | if( rc==SQLITE_OK && i!=iSkip && i<=nNewPage ){ |
| 7258 | |
| 7259 | DbPage *pToPage = 0; |
| 7260 | sqlite3_int64 iOff; |
| 7261 | |
| 7262 | rc = sqlite3PagerGet(pBtTo->pPager, i, &pToPage); |
| 7263 | if( rc==SQLITE_OK ){ |
| 7264 | rc = sqlite3PagerWrite(pToPage); |
| 7265 | } |
| 7266 | |
| 7267 | for( |
| 7268 | iOff=(i-1)*nToPageSize; |
| 7269 | rc==SQLITE_OK && iOff<i*nToPageSize; |
| 7270 | iOff += nFromPageSize |
| 7271 | ){ |
| 7272 | DbPage *pFromPage = 0; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 7273 | Pgno iFrom = (Pgno)(iOff/nFromPageSize)+1; |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7274 | |
| 7275 | if( iFrom==PENDING_BYTE_PAGE(pBtFrom) ){ |
| 7276 | continue; |
| 7277 | } |
| 7278 | |
| 7279 | rc = sqlite3PagerGet(pBtFrom->pPager, iFrom, &pFromPage); |
| 7280 | if( rc==SQLITE_OK ){ |
| 7281 | char *zTo = sqlite3PagerGetData(pToPage); |
| 7282 | char *zFrom = sqlite3PagerGetData(pFromPage); |
| 7283 | int nCopy; |
| 7284 | |
| 7285 | if( nFromPageSize>=nToPageSize ){ |
| 7286 | zFrom += ((i-1)*nToPageSize - ((iFrom-1)*nFromPageSize)); |
| 7287 | nCopy = nToPageSize; |
| 7288 | }else{ |
| 7289 | zTo += (((iFrom-1)*nFromPageSize) - (i-1)*nToPageSize); |
| 7290 | nCopy = nFromPageSize; |
| 7291 | } |
| 7292 | |
| 7293 | memcpy(zTo, zFrom, nCopy); |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 7294 | sqlite3PagerUnref(pFromPage); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7295 | } |
| 7296 | } |
| 7297 | |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 7298 | if( pToPage ){ |
| 7299 | MemPage *p = (MemPage *)sqlite3PagerGetExtra(pToPage); |
| 7300 | p->isInit = 0; |
| 7301 | sqlite3PagerUnref(pToPage); |
| 7302 | } |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7303 | } |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 7304 | } |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7305 | |
| 7306 | /* If things have worked so far, the database file may need to be |
| 7307 | ** truncated. The complex part is that it may need to be truncated to |
| 7308 | ** a size that is not an integer multiple of nToPageSize - the current |
| 7309 | ** page size used by the pager associated with B-Tree pTo. |
| 7310 | ** |
| 7311 | ** For example, say the page-size of pTo is 2048 bytes and the original |
| 7312 | ** number of pages is 5 (10 KB file). If pFrom has a page size of 1024 |
| 7313 | ** bytes and 9 pages, then the file needs to be truncated to 9KB. |
| 7314 | */ |
| 7315 | if( rc==SQLITE_OK ){ |
| 7316 | if( nFromPageSize!=nToPageSize ){ |
| 7317 | sqlite3_file *pFile = sqlite3PagerFile(pBtTo->pPager); |
| 7318 | i64 iSize = (i64)nFromPageSize * (i64)nFromPage; |
| 7319 | i64 iNow = (i64)((nToPage>nNewPage)?nToPage:nNewPage) * (i64)nToPageSize; |
| 7320 | i64 iPending = ((i64)PENDING_BYTE_PAGE(pBtTo)-1) *(i64)nToPageSize; |
| 7321 | |
| 7322 | assert( iSize<=iNow ); |
| 7323 | |
| 7324 | /* Commit phase one syncs the journal file associated with pTo |
| 7325 | ** containing the original data. It does not sync the database file |
| 7326 | ** itself. After doing this it is safe to use OsTruncate() and other |
| 7327 | ** file APIs on the database file directly. |
| 7328 | */ |
| 7329 | pBtTo->db = pTo->db; |
| 7330 | rc = sqlite3PagerCommitPhaseOne(pBtTo->pPager, 0, 0, 1); |
| 7331 | if( iSize<iNow && rc==SQLITE_OK ){ |
| 7332 | rc = sqlite3OsTruncate(pFile, iSize); |
| 7333 | } |
| 7334 | |
| 7335 | /* The loop that copied data from database pFrom to pTo did not |
| 7336 | ** populate the locking page of database pTo. If the page-size of |
| 7337 | ** pFrom is smaller than that of pTo, this means some data will |
| 7338 | ** not have been copied. |
| 7339 | ** |
| 7340 | ** This block copies the missing data from database pFrom to pTo |
| 7341 | ** using file APIs. This is safe because at this point we know that |
| 7342 | ** all of the original data from pTo has been synced into the |
| 7343 | ** journal file. At this point it would be safe to do anything at |
| 7344 | ** all to the database file except truncate it to zero bytes. |
| 7345 | */ |
| 7346 | if( rc==SQLITE_OK && nFromPageSize<nToPageSize && iSize>iPending){ |
| 7347 | i64 iOff; |
| 7348 | for( |
| 7349 | iOff=iPending; |
| 7350 | rc==SQLITE_OK && iOff<(iPending+nToPageSize); |
| 7351 | iOff += nFromPageSize |
| 7352 | ){ |
| 7353 | DbPage *pFromPage = 0; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 7354 | Pgno iFrom = (Pgno)(iOff/nFromPageSize)+1; |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7355 | |
| 7356 | if( iFrom==PENDING_BYTE_PAGE(pBtFrom) || iFrom>nFromPage ){ |
| 7357 | continue; |
| 7358 | } |
| 7359 | |
| 7360 | rc = sqlite3PagerGet(pBtFrom->pPager, iFrom, &pFromPage); |
| 7361 | if( rc==SQLITE_OK ){ |
| 7362 | char *zFrom = sqlite3PagerGetData(pFromPage); |
danielk1977 | 06249db | 2008-08-23 16:17:55 +0000 | [diff] [blame] | 7363 | rc = sqlite3OsWrite(pFile, zFrom, nFromPageSize, iOff); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7364 | sqlite3PagerUnref(pFromPage); |
| 7365 | } |
| 7366 | } |
| 7367 | } |
| 7368 | |
| 7369 | /* Sync the database file */ |
| 7370 | if( rc==SQLITE_OK ){ |
| 7371 | rc = sqlite3PagerSync(pBtTo->pPager); |
| 7372 | } |
| 7373 | }else{ |
| 7374 | rc = sqlite3PagerTruncate(pBtTo->pPager, nNewPage); |
| 7375 | } |
| 7376 | if( rc==SQLITE_OK ){ |
| 7377 | pBtTo->pageSizeFixed = 0; |
| 7378 | } |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 7379 | } |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 7380 | |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 7381 | if( rc ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7382 | sqlite3BtreeRollback(pTo); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 7383 | } |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7384 | |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 7385 | return rc; |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 7386 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7387 | int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){ |
| 7388 | int rc; |
| 7389 | sqlite3BtreeEnter(pTo); |
| 7390 | sqlite3BtreeEnter(pFrom); |
| 7391 | rc = btreeCopyFile(pTo, pFrom); |
| 7392 | sqlite3BtreeLeave(pFrom); |
| 7393 | sqlite3BtreeLeave(pTo); |
| 7394 | return rc; |
| 7395 | } |
| 7396 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 7397 | #endif /* SQLITE_OMIT_VACUUM */ |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 7398 | |
| 7399 | /* |
| 7400 | ** Return non-zero if a transaction is active. |
| 7401 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7402 | int sqlite3BtreeIsInTrans(Btree *p){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7403 | assert( p==0 || sqlite3_mutex_held(p->db->mutex) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7404 | return (p && (p->inTrans==TRANS_WRITE)); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 7405 | } |
| 7406 | |
| 7407 | /* |
| 7408 | ** Return non-zero if a statement transaction is active. |
| 7409 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7410 | int sqlite3BtreeIsInStmt(Btree *p){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7411 | assert( sqlite3BtreeHoldsMutex(p) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7412 | return (p->pBt && p->pBt->inStmt); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 7413 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 7414 | |
| 7415 | /* |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 7416 | ** Return non-zero if a read (or write) transaction is active. |
| 7417 | */ |
| 7418 | int sqlite3BtreeIsInReadTrans(Btree *p){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7419 | assert( sqlite3_mutex_held(p->db->mutex) ); |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 7420 | return (p && (p->inTrans!=TRANS_NONE)); |
| 7421 | } |
| 7422 | |
| 7423 | /* |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7424 | ** This function returns a pointer to a blob of memory associated with |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 7425 | ** 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] | 7426 | ** purposes (for example, to store a high-level schema associated with |
| 7427 | ** the shared-btree). The btree layer manages reference counting issues. |
| 7428 | ** |
| 7429 | ** The first time this is called on a shared-btree, nBytes bytes of memory |
| 7430 | ** are allocated, zeroed, and returned to the caller. For each subsequent |
| 7431 | ** call the nBytes parameter is ignored and a pointer to the same blob |
| 7432 | ** of memory returned. |
| 7433 | ** |
danielk1977 | 171bfed | 2008-06-23 09:50:50 +0000 | [diff] [blame] | 7434 | ** If the nBytes parameter is 0 and the blob of memory has not yet been |
| 7435 | ** allocated, a null pointer is returned. If the blob has already been |
| 7436 | ** allocated, it is returned as normal. |
| 7437 | ** |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7438 | ** Just before the shared-btree is closed, the function passed as the |
| 7439 | ** xFree argument when the memory allocation was made is invoked on the |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 7440 | ** blob of allocated memory. This function should not call sqlite3_free() |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7441 | ** on the memory, the btree layer does that. |
| 7442 | */ |
| 7443 | void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){ |
| 7444 | BtShared *pBt = p->pBt; |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7445 | sqlite3BtreeEnter(p); |
danielk1977 | 171bfed | 2008-06-23 09:50:50 +0000 | [diff] [blame] | 7446 | if( !pBt->pSchema && nBytes ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 7447 | pBt->pSchema = sqlite3MallocZero(nBytes); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7448 | pBt->xFreeSchema = xFree; |
| 7449 | } |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7450 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7451 | return pBt->pSchema; |
| 7452 | } |
| 7453 | |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 7454 | /* |
| 7455 | ** Return true if another user of the same shared btree as the argument |
| 7456 | ** handle holds an exclusive lock on the sqlite_master table. |
| 7457 | */ |
| 7458 | int sqlite3BtreeSchemaLocked(Btree *p){ |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7459 | int rc; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7460 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7461 | sqlite3BtreeEnter(p); |
| 7462 | rc = (queryTableLock(p, MASTER_ROOT, READ_LOCK)!=SQLITE_OK); |
| 7463 | sqlite3BtreeLeave(p); |
| 7464 | return rc; |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 7465 | } |
| 7466 | |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 7467 | |
| 7468 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 7469 | /* |
| 7470 | ** Obtain a lock on the table whose root page is iTab. The |
| 7471 | ** lock is a write lock if isWritelock is true or a read lock |
| 7472 | ** if it is false. |
| 7473 | */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7474 | int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){ |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 7475 | int rc = SQLITE_OK; |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 7476 | if( p->sharable ){ |
| 7477 | u8 lockType = READ_LOCK + isWriteLock; |
| 7478 | assert( READ_LOCK+1==WRITE_LOCK ); |
| 7479 | assert( isWriteLock==0 || isWriteLock==1 ); |
| 7480 | sqlite3BtreeEnter(p); |
| 7481 | rc = queryTableLock(p, iTab, lockType); |
| 7482 | if( rc==SQLITE_OK ){ |
| 7483 | rc = lockTable(p, iTab, lockType); |
| 7484 | } |
| 7485 | sqlite3BtreeLeave(p); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7486 | } |
| 7487 | return rc; |
| 7488 | } |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 7489 | #endif |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 7490 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7491 | #ifndef SQLITE_OMIT_INCRBLOB |
| 7492 | /* |
| 7493 | ** Argument pCsr must be a cursor opened for writing on an |
| 7494 | ** INTKEY table currently pointing at a valid table entry. |
| 7495 | ** This function modifies the data stored as part of that entry. |
| 7496 | ** Only the data content may only be modified, it is not possible |
| 7497 | ** to change the length of the data stored. |
| 7498 | */ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7499 | int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7500 | assert( cursorHoldsMutex(pCsr) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7501 | assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) ); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7502 | assert(pCsr->isIncrblobHandle); |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 7503 | |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 7504 | restoreCursorPosition(pCsr); |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 7505 | assert( pCsr->eState!=CURSOR_REQUIRESEEK ); |
| 7506 | if( pCsr->eState!=CURSOR_VALID ){ |
| 7507 | return SQLITE_ABORT; |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7508 | } |
| 7509 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7510 | /* Check some preconditions: |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7511 | ** (a) the cursor is open for writing, |
| 7512 | ** (b) there is no read-lock on the table being modified and |
| 7513 | ** (c) the cursor points at a valid row of an intKey table. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7514 | */ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7515 | if( !pCsr->wrFlag ){ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7516 | return SQLITE_READONLY; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7517 | } |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 7518 | assert( !pCsr->pBt->readOnly |
| 7519 | && pCsr->pBt->inTransaction==TRANS_WRITE ); |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 7520 | if( checkReadLocks(pCsr->pBtree, pCsr->pgnoRoot, pCsr, 0) ){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7521 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 7522 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 7523 | if( pCsr->eState==CURSOR_INVALID || !pCsr->apPage[pCsr->iPage]->intKey ){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7524 | return SQLITE_ERROR; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7525 | } |
| 7526 | |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 7527 | return accessPayload(pCsr, offset, amt, (unsigned char *)z, 0, 1); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7528 | } |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7529 | |
| 7530 | /* |
| 7531 | ** 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] | 7532 | ** overflow list for the current row. This is used by cursors opened |
| 7533 | ** for incremental blob IO only. |
| 7534 | ** |
| 7535 | ** This function sets a flag only. The actual page location cache |
| 7536 | ** (stored in BtCursor.aOverflow[]) is allocated and used by function |
| 7537 | ** accessPayload() (the worker function for sqlite3BtreeData() and |
| 7538 | ** sqlite3BtreePutData()). |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7539 | */ |
| 7540 | void sqlite3BtreeCacheOverflow(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7541 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7542 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7543 | assert(!pCur->isIncrblobHandle); |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7544 | assert(!pCur->aOverflow); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7545 | pCur->isIncrblobHandle = 1; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7546 | } |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7547 | #endif |