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 | 360e634 | 2008-11-12 08:49:51 +0000 | [diff] [blame^] | 12 | ** $Id: btree.c,v 1.533 2008/11/12 08:49:52 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 | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 319 | void *pKey = sqlite3Malloc(pCur->nKey); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 320 | if( pKey ){ |
| 321 | rc = sqlite3BtreeKey(pCur, 0, pCur->nKey, pKey); |
| 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){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 438 | int nPagesPerMapPage, iPtrMap, ret; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 439 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 440 | nPagesPerMapPage = (pBt->usableSize/5)+1; |
| 441 | iPtrMap = (pgno-2)/nPagesPerMapPage; |
| 442 | ret = (iPtrMap*nPagesPerMapPage) + 2; |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 443 | if( ret==PENDING_BYTE_PAGE(pBt) ){ |
| 444 | ret++; |
| 445 | } |
| 446 | return ret; |
| 447 | } |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 448 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 449 | /* |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 450 | ** Write an entry into the pointer map. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 451 | ** |
| 452 | ** This routine updates the pointer map entry for page number 'key' |
| 453 | ** so that it maps to type 'eType' and parent page number 'pgno'. |
| 454 | ** An error code is returned if something goes wrong, otherwise SQLITE_OK. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 455 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 456 | static int ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 457 | DbPage *pDbPage; /* The pointer map page */ |
| 458 | u8 *pPtrmap; /* The pointer map data */ |
| 459 | Pgno iPtrmap; /* The pointer map page number */ |
| 460 | int offset; /* Offset in pointer map page */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 461 | int rc; |
| 462 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 463 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 464 | /* The master-journal page number must never be used as a pointer map page */ |
| 465 | assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) ); |
| 466 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 467 | assert( pBt->autoVacuum ); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 468 | if( key==0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 469 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 470 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 471 | iPtrmap = PTRMAP_PAGENO(pBt, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 472 | rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 473 | if( rc!=SQLITE_OK ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 474 | return rc; |
| 475 | } |
danielk1977 | 8c666b1 | 2008-07-18 09:34:57 +0000 | [diff] [blame] | 476 | offset = PTRMAP_PTROFFSET(iPtrmap, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 477 | pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 478 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 479 | if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){ |
| 480 | TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent)); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 481 | rc = sqlite3PagerWrite(pDbPage); |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 482 | if( rc==SQLITE_OK ){ |
| 483 | pPtrmap[offset] = eType; |
| 484 | put4byte(&pPtrmap[offset+1], parent); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 485 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 486 | } |
| 487 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 488 | sqlite3PagerUnref(pDbPage); |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 489 | return rc; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | /* |
| 493 | ** Read an entry from the pointer map. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 494 | ** |
| 495 | ** This routine retrieves the pointer map entry for page 'key', writing |
| 496 | ** the type and parent page number to *pEType and *pPgno respectively. |
| 497 | ** An error code is returned if something goes wrong, otherwise SQLITE_OK. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 498 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 499 | static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 500 | DbPage *pDbPage; /* The pointer map page */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 501 | int iPtrmap; /* Pointer map page index */ |
| 502 | u8 *pPtrmap; /* Pointer map page data */ |
| 503 | int offset; /* Offset of entry in pointer map */ |
| 504 | int rc; |
| 505 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 506 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 507 | |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 508 | iPtrmap = PTRMAP_PAGENO(pBt, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 509 | rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 510 | if( rc!=0 ){ |
| 511 | return rc; |
| 512 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 513 | pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 514 | |
danielk1977 | 8c666b1 | 2008-07-18 09:34:57 +0000 | [diff] [blame] | 515 | offset = PTRMAP_PTROFFSET(iPtrmap, key); |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 516 | assert( pEType!=0 ); |
| 517 | *pEType = pPtrmap[offset]; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 518 | if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 519 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 520 | sqlite3PagerUnref(pDbPage); |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 521 | if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 522 | return SQLITE_OK; |
| 523 | } |
| 524 | |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 525 | #else /* if defined SQLITE_OMIT_AUTOVACUUM */ |
| 526 | #define ptrmapPut(w,x,y,z) SQLITE_OK |
| 527 | #define ptrmapGet(w,x,y,z) SQLITE_OK |
| 528 | #define ptrmapPutOvfl(y,z) SQLITE_OK |
| 529 | #endif |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 530 | |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 531 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 532 | ** Given a btree page and a cell index (0 means the first cell on |
| 533 | ** the page, 1 means the second cell, and so forth) return a pointer |
| 534 | ** to the cell content. |
| 535 | ** |
| 536 | ** This routine works only for pages that do not contain overflow cells. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 537 | */ |
drh | 1688c86 | 2008-07-18 02:44:17 +0000 | [diff] [blame] | 538 | #define findCell(P,I) \ |
| 539 | ((P)->aData + ((P)->maskPage & get2byte(&(P)->aData[(P)->cellOffset+2*(I)]))) |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 540 | |
| 541 | /* |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 542 | ** This a more complex version of findCell() that works for |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 543 | ** pages that do contain overflow cells. See insert |
| 544 | */ |
| 545 | static u8 *findOverflowCell(MemPage *pPage, int iCell){ |
| 546 | int i; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 547 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 548 | for(i=pPage->nOverflow-1; i>=0; i--){ |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 549 | int k; |
| 550 | struct _OvflCell *pOvfl; |
| 551 | pOvfl = &pPage->aOvfl[i]; |
| 552 | k = pOvfl->idx; |
| 553 | if( k<=iCell ){ |
| 554 | if( k==iCell ){ |
| 555 | return pOvfl->pCell; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 556 | } |
| 557 | iCell--; |
| 558 | } |
| 559 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 560 | return findCell(pPage, iCell); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | /* |
| 564 | ** Parse a cell content block and fill in the CellInfo structure. There |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 565 | ** are two versions of this function. sqlite3BtreeParseCell() takes a |
| 566 | ** cell index as the second argument and sqlite3BtreeParseCellPtr() |
| 567 | ** takes a pointer to the body of the cell as its second argument. |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 568 | ** |
| 569 | ** Within this file, the parseCell() macro can be called instead of |
| 570 | ** sqlite3BtreeParseCellPtr(). Using some compilers, this will be faster. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 571 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 572 | void sqlite3BtreeParseCellPtr( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 573 | MemPage *pPage, /* Page containing the cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 574 | u8 *pCell, /* Pointer to the cell text. */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 575 | CellInfo *pInfo /* Fill in this structure */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 576 | ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 577 | int n; /* Number bytes in cell content header */ |
| 578 | u32 nPayload; /* Number of bytes of cell payload */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 579 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 580 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 581 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 582 | pInfo->pCell = pCell; |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 583 | assert( pPage->leaf==0 || pPage->leaf==1 ); |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 584 | n = pPage->childPtrSize; |
| 585 | assert( n==4-4*pPage->leaf ); |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 586 | if( pPage->intKey ){ |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 587 | if( pPage->hasData ){ |
| 588 | n += getVarint32(&pCell[n], nPayload); |
| 589 | }else{ |
| 590 | nPayload = 0; |
| 591 | } |
| 592 | n += getVarint(&pCell[n], (u64*)&pInfo->nKey); |
| 593 | pInfo->nData = nPayload; |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 594 | }else{ |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 595 | pInfo->nData = 0; |
| 596 | n += getVarint32(&pCell[n], nPayload); |
| 597 | pInfo->nKey = nPayload; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 598 | } |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 599 | pInfo->nPayload = nPayload; |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 600 | pInfo->nHeader = n; |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 601 | if( likely(nPayload<=pPage->maxLocal) ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 602 | /* This is the (easy) common case where the entire payload fits |
| 603 | ** on the local page. No overflow is required. |
| 604 | */ |
| 605 | int nSize; /* Total size of cell content in bytes */ |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 606 | nSize = nPayload + n; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 607 | pInfo->nLocal = nPayload; |
| 608 | pInfo->iOverflow = 0; |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 609 | if( (nSize & ~3)==0 ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 610 | nSize = 4; /* Minimum cell size is 4 */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 611 | } |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 612 | pInfo->nSize = nSize; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 613 | }else{ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 614 | /* If the payload will not fit completely on the local page, we have |
| 615 | ** to decide how much to store locally and how much to spill onto |
| 616 | ** overflow pages. The strategy is to minimize the amount of unused |
| 617 | ** space on overflow pages while keeping the amount of local storage |
| 618 | ** in between minLocal and maxLocal. |
| 619 | ** |
| 620 | ** Warning: changing the way overflow payload is distributed in any |
| 621 | ** way will result in an incompatible file format. |
| 622 | */ |
| 623 | int minLocal; /* Minimum amount of payload held locally */ |
| 624 | int maxLocal; /* Maximum amount of payload held locally */ |
| 625 | int surplus; /* Overflow payload available for local storage */ |
| 626 | |
| 627 | minLocal = pPage->minLocal; |
| 628 | maxLocal = pPage->maxLocal; |
| 629 | surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 630 | if( surplus <= maxLocal ){ |
| 631 | pInfo->nLocal = surplus; |
| 632 | }else{ |
| 633 | pInfo->nLocal = minLocal; |
| 634 | } |
| 635 | pInfo->iOverflow = pInfo->nLocal + n; |
| 636 | pInfo->nSize = pInfo->iOverflow + 4; |
| 637 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 638 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 639 | #define parseCell(pPage, iCell, pInfo) \ |
| 640 | sqlite3BtreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo)) |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 641 | void sqlite3BtreeParseCell( |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 642 | MemPage *pPage, /* Page containing the cell */ |
| 643 | int iCell, /* The cell index. First cell is 0 */ |
| 644 | CellInfo *pInfo /* Fill in this structure */ |
| 645 | ){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 646 | parseCell(pPage, iCell, pInfo); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 647 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 648 | |
| 649 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 650 | ** Compute the total number of bytes that a Cell needs in the cell |
| 651 | ** data area of the btree-page. The return number includes the cell |
| 652 | ** data header and the local payload, but not any overflow page or |
| 653 | ** the space used by the cell pointer. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 654 | */ |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 655 | #ifndef NDEBUG |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 656 | static u16 cellSize(MemPage *pPage, int iCell){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 657 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 658 | sqlite3BtreeParseCell(pPage, iCell, &info); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 659 | return info.nSize; |
| 660 | } |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 661 | #endif |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 662 | static u16 cellSizePtr(MemPage *pPage, u8 *pCell){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 663 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 664 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 665 | return info.nSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 666 | } |
| 667 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 668 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 669 | /* |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 670 | ** If the cell pCell, part of page pPage contains a pointer |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 671 | ** to an overflow page, insert an entry into the pointer-map |
| 672 | ** for the overflow page. |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 673 | */ |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 674 | static int ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell){ |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 675 | CellInfo info; |
| 676 | assert( pCell!=0 ); |
| 677 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
| 678 | assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); |
| 679 | if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ |
| 680 | Pgno ovfl = get4byte(&pCell[info.iOverflow]); |
| 681 | return ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 682 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 683 | return SQLITE_OK; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 684 | } |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 685 | /* |
| 686 | ** If the cell with index iCell on page pPage contains a pointer |
| 687 | ** to an overflow page, insert an entry into the pointer-map |
| 688 | ** for the overflow page. |
| 689 | */ |
| 690 | static int ptrmapPutOvfl(MemPage *pPage, int iCell){ |
| 691 | u8 *pCell; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 692 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 693 | pCell = findOverflowCell(pPage, iCell); |
| 694 | return ptrmapPutOvflPtr(pPage, pCell); |
| 695 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 696 | #endif |
| 697 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 698 | |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 699 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 700 | ** Defragment the page given. All Cells are moved to the |
drh | 3a4a2d4 | 2005-11-24 14:24:28 +0000 | [diff] [blame] | 701 | ** end of the page and all free space is collected into one |
| 702 | ** big FreeBlk that occurs in between the header and cell |
drh | 31beae9 | 2005-11-24 14:34:36 +0000 | [diff] [blame] | 703 | ** pointer array and the cell content area. |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 704 | */ |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 705 | static int defragmentPage(MemPage *pPage){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 706 | int i; /* Loop counter */ |
| 707 | int pc; /* Address of a i-th cell */ |
| 708 | int addr; /* Offset of first byte after cell pointer array */ |
| 709 | int hdr; /* Offset to the page header */ |
| 710 | int size; /* Size of a cell */ |
| 711 | int usableSize; /* Number of usable bytes on a page */ |
| 712 | int cellOffset; /* Offset to the cell pointer array */ |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 713 | int cbrk; /* Offset to the cell content area */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 714 | int nCell; /* Number of cells on the page */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 715 | unsigned char *data; /* The page data */ |
| 716 | unsigned char *temp; /* Temp area for cell content */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 717 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 718 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 719 | assert( pPage->pBt!=0 ); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 720 | assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 721 | assert( pPage->nOverflow==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 722 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 26b7994 | 2007-11-28 16:19:56 +0000 | [diff] [blame] | 723 | temp = sqlite3PagerTempSpace(pPage->pBt->pPager); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 724 | data = pPage->aData; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 725 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 726 | cellOffset = pPage->cellOffset; |
| 727 | nCell = pPage->nCell; |
| 728 | assert( nCell==get2byte(&data[hdr+3]) ); |
| 729 | usableSize = pPage->pBt->usableSize; |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 730 | cbrk = get2byte(&data[hdr+5]); |
| 731 | memcpy(&temp[cbrk], &data[cbrk], usableSize - cbrk); |
| 732 | cbrk = usableSize; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 733 | for(i=0; i<nCell; i++){ |
| 734 | u8 *pAddr; /* The i-th cell pointer */ |
| 735 | pAddr = &data[cellOffset + i*2]; |
| 736 | pc = get2byte(pAddr); |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 737 | if (pc >= pPage->pBt->usableSize) { |
| 738 | return SQLITE_CORRUPT_BKPT; |
| 739 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 740 | size = cellSizePtr(pPage, &temp[pc]); |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 741 | cbrk -= size; |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 742 | if ((cbrk < cellOffset+2*nCell) || (cbrk+size>pPage->pBt->usableSize)) { |
| 743 | return SQLITE_CORRUPT_BKPT; |
| 744 | } |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 745 | memcpy(&data[cbrk], &temp[pc], size); |
| 746 | put2byte(pAddr, cbrk); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 747 | } |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 748 | assert( cbrk>=cellOffset+2*nCell ); |
| 749 | put2byte(&data[hdr+5], cbrk); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 750 | data[hdr+1] = 0; |
| 751 | data[hdr+2] = 0; |
| 752 | data[hdr+7] = 0; |
| 753 | addr = cellOffset+2*nCell; |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 754 | memset(&data[addr], 0, cbrk-addr); |
danielk1977 | 360e634 | 2008-11-12 08:49:51 +0000 | [diff] [blame^] | 755 | if( cbrk-addr!=pPage->nFree ){ |
| 756 | return SQLITE_CORRUPT_BKPT; |
| 757 | } |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 758 | return SQLITE_OK; |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 759 | } |
| 760 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 761 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 762 | ** Allocate nByte bytes of space on a page. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 763 | ** |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 764 | ** Return the index into pPage->aData[] of the first byte of |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 765 | ** the new allocation. The caller guarantees that there is enough |
| 766 | ** space. This routine will never fail. |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 767 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 768 | ** If the page contains nBytes of free space but does not contain |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 769 | ** nBytes of contiguous free space, then this routine automatically |
| 770 | ** calls defragementPage() to consolidate all free space before |
| 771 | ** allocating the new chunk. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 772 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 773 | static int allocateSpace(MemPage *pPage, int nByte){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 774 | int addr, pc, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 775 | int size; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 776 | int nFrag; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 777 | int top; |
| 778 | int nCell; |
| 779 | int cellOffset; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 780 | unsigned char *data; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 781 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 782 | data = pPage->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 783 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 784 | assert( pPage->pBt ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 785 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 786 | assert( nByte>=0 ); /* Minimum cell size is 4 */ |
| 787 | assert( pPage->nFree>=nByte ); |
| 788 | assert( pPage->nOverflow==0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 789 | pPage->nFree -= nByte; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 790 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 791 | |
| 792 | nFrag = data[hdr+7]; |
| 793 | if( nFrag<60 ){ |
| 794 | /* Search the freelist looking for a slot big enough to satisfy the |
| 795 | ** space request. */ |
| 796 | addr = hdr+1; |
| 797 | while( (pc = get2byte(&data[addr]))>0 ){ |
| 798 | size = get2byte(&data[pc+2]); |
| 799 | if( size>=nByte ){ |
| 800 | if( size<nByte+4 ){ |
| 801 | memcpy(&data[addr], &data[pc], 2); |
| 802 | data[hdr+7] = nFrag + size - nByte; |
| 803 | return pc; |
| 804 | }else{ |
| 805 | put2byte(&data[pc+2], size-nByte); |
| 806 | return pc + size - nByte; |
| 807 | } |
| 808 | } |
| 809 | addr = pc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 810 | } |
| 811 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 812 | |
| 813 | /* Allocate memory from the gap in between the cell pointer array |
| 814 | ** and the cell content area. |
| 815 | */ |
| 816 | top = get2byte(&data[hdr+5]); |
| 817 | nCell = get2byte(&data[hdr+3]); |
| 818 | cellOffset = pPage->cellOffset; |
| 819 | if( nFrag>=60 || cellOffset + 2*nCell > top - nByte ){ |
danielk1977 | 474b7cc | 2008-07-09 11:49:46 +0000 | [diff] [blame] | 820 | defragmentPage(pPage); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 821 | top = get2byte(&data[hdr+5]); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 822 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 823 | top -= nByte; |
| 824 | assert( cellOffset + 2*nCell <= top ); |
| 825 | put2byte(&data[hdr+5], top); |
| 826 | return top; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 830 | ** Return a section of the pPage->aData to the freelist. |
| 831 | ** The first byte of the new free block is pPage->aDisk[start] |
| 832 | ** and the size of the block is "size" bytes. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 833 | ** |
| 834 | ** Most of the effort here is involved in coalesing adjacent |
| 835 | ** free blocks into a single big free block. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 836 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 837 | static void freeSpace(MemPage *pPage, int start, int size){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 838 | int addr, pbegin, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 839 | unsigned char *data = pPage->aData; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 840 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 841 | assert( pPage->pBt!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 842 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 843 | assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) ); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 844 | assert( (start + size)<=pPage->pBt->usableSize ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 845 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 846 | assert( size>=0 ); /* Minimum cell size is 4 */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 847 | |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 848 | #ifdef SQLITE_SECURE_DELETE |
| 849 | /* Overwrite deleted information with zeros when the SECURE_DELETE |
| 850 | ** option is enabled at compile-time */ |
| 851 | memset(&data[start], 0, size); |
| 852 | #endif |
| 853 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 854 | /* Add the space back into the linked list of freeblocks */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 855 | hdr = pPage->hdrOffset; |
| 856 | addr = hdr + 1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 857 | while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 858 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 859 | assert( pbegin>addr ); |
| 860 | addr = pbegin; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 861 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 862 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 863 | assert( pbegin>addr || pbegin==0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 864 | put2byte(&data[addr], start); |
| 865 | put2byte(&data[start], pbegin); |
| 866 | put2byte(&data[start+2], size); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 867 | pPage->nFree += size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 868 | |
| 869 | /* Coalesce adjacent free blocks */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 870 | addr = pPage->hdrOffset + 1; |
| 871 | while( (pbegin = get2byte(&data[addr]))>0 ){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 872 | int pnext, psize; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 873 | assert( pbegin>addr ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 874 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 875 | pnext = get2byte(&data[pbegin]); |
| 876 | psize = get2byte(&data[pbegin+2]); |
| 877 | if( pbegin + psize + 3 >= pnext && pnext>0 ){ |
| 878 | int frag = pnext - (pbegin+psize); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 879 | assert( frag<=data[pPage->hdrOffset+7] ); |
| 880 | data[pPage->hdrOffset+7] -= frag; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 881 | put2byte(&data[pbegin], get2byte(&data[pnext])); |
| 882 | put2byte(&data[pbegin+2], pnext+get2byte(&data[pnext+2])-pbegin); |
| 883 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 884 | addr = pbegin; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 885 | } |
| 886 | } |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 887 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 888 | /* If the cell content area begins with a freeblock, remove it. */ |
| 889 | if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){ |
| 890 | int top; |
| 891 | pbegin = get2byte(&data[hdr+1]); |
| 892 | memcpy(&data[hdr+1], &data[pbegin], 2); |
| 893 | top = get2byte(&data[hdr+5]); |
| 894 | put2byte(&data[hdr+5], top + get2byte(&data[pbegin+2])); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 895 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 899 | ** Decode the flags byte (the first byte of the header) for a page |
| 900 | ** and initialize fields of the MemPage structure accordingly. |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 901 | ** |
| 902 | ** Only the following combinations are supported. Anything different |
| 903 | ** indicates a corrupt database files: |
| 904 | ** |
| 905 | ** PTF_ZERODATA |
| 906 | ** PTF_ZERODATA | PTF_LEAF |
| 907 | ** PTF_LEAFDATA | PTF_INTKEY |
| 908 | ** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 909 | */ |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 910 | static int decodeFlags(MemPage *pPage, int flagByte){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 911 | BtShared *pBt; /* A copy of pPage->pBt */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 912 | |
| 913 | assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 914 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 915 | pPage->leaf = flagByte>>3; assert( PTF_LEAF == 1<<3 ); |
| 916 | flagByte &= ~PTF_LEAF; |
| 917 | pPage->childPtrSize = 4-4*pPage->leaf; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 918 | pBt = pPage->pBt; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 919 | if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){ |
| 920 | pPage->intKey = 1; |
| 921 | pPage->hasData = pPage->leaf; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 922 | pPage->maxLocal = pBt->maxLeaf; |
| 923 | pPage->minLocal = pBt->minLeaf; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 924 | }else if( flagByte==PTF_ZERODATA ){ |
| 925 | pPage->intKey = 0; |
| 926 | pPage->hasData = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 927 | pPage->maxLocal = pBt->maxLocal; |
| 928 | pPage->minLocal = pBt->minLocal; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 929 | }else{ |
| 930 | return SQLITE_CORRUPT_BKPT; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 931 | } |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 932 | return SQLITE_OK; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 936 | ** Initialize the auxiliary information for a disk block. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 937 | ** |
| 938 | ** Return SQLITE_OK on success. If we see that the page does |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 939 | ** not contain a well-formed database page, then return |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 940 | ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not |
| 941 | ** guarantee that the page is well-formed. It only shows that |
| 942 | ** we failed to detect any corruption. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 943 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 944 | int sqlite3BtreeInitPage(MemPage *pPage){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 945 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 946 | assert( pPage->pBt!=0 ); |
| 947 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 948 | assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 949 | assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) ); |
| 950 | assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 951 | |
| 952 | if( !pPage->isInit ){ |
| 953 | int pc; /* Address of a freeblock within pPage->aData[] */ |
| 954 | int hdr; /* Offset to beginning of page header */ |
| 955 | u8 *data; /* Equal to pPage->aData */ |
| 956 | BtShared *pBt; /* The main btree structure */ |
| 957 | int usableSize; /* Amount of usable space on each page */ |
| 958 | int cellOffset; /* Offset from start of page to first cell pointer */ |
| 959 | int nFree; /* Number of unused bytes on the page */ |
| 960 | int top; /* First byte of the cell content area */ |
| 961 | |
| 962 | pBt = pPage->pBt; |
| 963 | |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 964 | hdr = pPage->hdrOffset; |
| 965 | data = pPage->aData; |
| 966 | if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT; |
| 967 | assert( pBt->pageSize>=512 && pBt->pageSize<=32768 ); |
| 968 | pPage->maskPage = pBt->pageSize - 1; |
| 969 | pPage->nOverflow = 0; |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 970 | usableSize = pBt->usableSize; |
| 971 | pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf; |
| 972 | top = get2byte(&data[hdr+5]); |
| 973 | pPage->nCell = get2byte(&data[hdr+3]); |
| 974 | if( pPage->nCell>MX_CELL(pBt) ){ |
| 975 | /* To many cells for a single page. The page must be corrupt */ |
| 976 | return SQLITE_CORRUPT_BKPT; |
| 977 | } |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 978 | |
| 979 | /* Compute the total free space on the page */ |
| 980 | pc = get2byte(&data[hdr+1]); |
| 981 | nFree = data[hdr+7] + top - (cellOffset + 2*pPage->nCell); |
| 982 | while( pc>0 ){ |
| 983 | int next, size; |
| 984 | if( pc>usableSize-4 ){ |
| 985 | /* Free block is off the page */ |
| 986 | return SQLITE_CORRUPT_BKPT; |
| 987 | } |
| 988 | next = get2byte(&data[pc]); |
| 989 | size = get2byte(&data[pc+2]); |
| 990 | if( next>0 && next<=pc+size+3 ){ |
| 991 | /* Free blocks must be in accending order */ |
| 992 | return SQLITE_CORRUPT_BKPT; |
| 993 | } |
| 994 | nFree += size; |
| 995 | pc = next; |
| 996 | } |
| 997 | pPage->nFree = nFree; |
| 998 | if( nFree>=usableSize ){ |
| 999 | /* Free space cannot exceed total page size */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1000 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1001 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1002 | |
drh | 1688c86 | 2008-07-18 02:44:17 +0000 | [diff] [blame] | 1003 | #if 0 |
| 1004 | /* Check that all the offsets in the cell offset array are within range. |
| 1005 | ** |
| 1006 | ** Omitting this consistency check and using the pPage->maskPage mask |
| 1007 | ** to prevent overrunning the page buffer in findCell() results in a |
| 1008 | ** 2.5% performance gain. |
| 1009 | */ |
| 1010 | { |
| 1011 | u8 *pOff; /* Iterator used to check all cell offsets are in range */ |
| 1012 | u8 *pEnd; /* Pointer to end of cell offset array */ |
| 1013 | u8 mask; /* Mask of bits that must be zero in MSB of cell offsets */ |
| 1014 | mask = ~(((u8)(pBt->pageSize>>8))-1); |
| 1015 | pEnd = &data[cellOffset + pPage->nCell*2]; |
| 1016 | for(pOff=&data[cellOffset]; pOff!=pEnd && !((*pOff)&mask); pOff+=2); |
| 1017 | if( pOff!=pEnd ){ |
| 1018 | return SQLITE_CORRUPT_BKPT; |
| 1019 | } |
danielk1977 | e16535f | 2008-06-11 18:15:29 +0000 | [diff] [blame] | 1020 | } |
drh | 1688c86 | 2008-07-18 02:44:17 +0000 | [diff] [blame] | 1021 | #endif |
danielk1977 | e16535f | 2008-06-11 18:15:29 +0000 | [diff] [blame] | 1022 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1023 | pPage->isInit = 1; |
| 1024 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1025 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1029 | ** Set up a raw page so that it looks like a database page holding |
| 1030 | ** no entries. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1031 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1032 | static void zeroPage(MemPage *pPage, int flags){ |
| 1033 | unsigned char *data = pPage->aData; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1034 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1035 | int hdr = pPage->hdrOffset; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1036 | int first; |
| 1037 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1038 | assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1039 | assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); |
| 1040 | assert( sqlite3PagerGetData(pPage->pDbPage) == data ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1041 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1042 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 1af4a6e | 2008-07-18 03:32:51 +0000 | [diff] [blame] | 1043 | /*memset(&data[hdr], 0, pBt->usableSize - hdr);*/ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1044 | data[hdr] = flags; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1045 | first = hdr + 8 + 4*((flags&PTF_LEAF)==0); |
| 1046 | memset(&data[hdr+1], 0, 4); |
| 1047 | data[hdr+7] = 0; |
| 1048 | put2byte(&data[hdr+5], pBt->usableSize); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1049 | pPage->nFree = pBt->usableSize - first; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1050 | decodeFlags(pPage, flags); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1051 | pPage->hdrOffset = hdr; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1052 | pPage->cellOffset = first; |
| 1053 | pPage->nOverflow = 0; |
drh | 1688c86 | 2008-07-18 02:44:17 +0000 | [diff] [blame] | 1054 | assert( pBt->pageSize>=512 && pBt->pageSize<=32768 ); |
| 1055 | pPage->maskPage = pBt->pageSize - 1; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1056 | pPage->nCell = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1057 | pPage->isInit = 1; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1060 | |
| 1061 | /* |
| 1062 | ** Convert a DbPage obtained from the pager into a MemPage used by |
| 1063 | ** the btree layer. |
| 1064 | */ |
| 1065 | static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){ |
| 1066 | MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage); |
| 1067 | pPage->aData = sqlite3PagerGetData(pDbPage); |
| 1068 | pPage->pDbPage = pDbPage; |
| 1069 | pPage->pBt = pBt; |
| 1070 | pPage->pgno = pgno; |
| 1071 | pPage->hdrOffset = pPage->pgno==1 ? 100 : 0; |
| 1072 | return pPage; |
| 1073 | } |
| 1074 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1075 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1076 | ** Get a page from the pager. Initialize the MemPage.pBt and |
| 1077 | ** MemPage.aData elements if needed. |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 1078 | ** |
| 1079 | ** If the noContent flag is set, it means that we do not care about |
| 1080 | ** the content of the page at this time. So do not go to the disk |
| 1081 | ** to fetch the content. Just fill in the content with zeros for now. |
| 1082 | ** If in the future we call sqlite3PagerWrite() on this page, that |
| 1083 | ** means we have started to be concerned about content and the disk |
| 1084 | ** read should occur at that point. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1085 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1086 | int sqlite3BtreeGetPage( |
| 1087 | BtShared *pBt, /* The btree */ |
| 1088 | Pgno pgno, /* Number of the page to fetch */ |
| 1089 | MemPage **ppPage, /* Return the page in this parameter */ |
| 1090 | int noContent /* Do not load page content if true */ |
| 1091 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1092 | int rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1093 | DbPage *pDbPage; |
| 1094 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1095 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 1096 | rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1097 | if( rc ) return rc; |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1098 | *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1099 | return SQLITE_OK; |
| 1100 | } |
| 1101 | |
| 1102 | /* |
danielk1977 | 67fd7a9 | 2008-09-10 17:53:35 +0000 | [diff] [blame] | 1103 | ** Return the size of the database file in pages. Or return -1 if |
| 1104 | ** there is any kind of error. |
| 1105 | */ |
| 1106 | static int pagerPagecount(Pager *pPager){ |
| 1107 | int rc; |
| 1108 | int nPage; |
| 1109 | rc = sqlite3PagerPagecount(pPager, &nPage); |
| 1110 | return (rc==SQLITE_OK?nPage:-1); |
| 1111 | } |
| 1112 | |
| 1113 | /* |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1114 | ** Get a page from the pager and initialize it. This routine |
| 1115 | ** is just a convenience wrapper around separate calls to |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1116 | ** sqlite3BtreeGetPage() and sqlite3BtreeInitPage(). |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1117 | */ |
| 1118 | static int getAndInitPage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1119 | BtShared *pBt, /* The database file */ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1120 | Pgno pgno, /* Number of the page to get */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1121 | MemPage **ppPage /* Write the page pointer here */ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1122 | ){ |
| 1123 | int rc; |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1124 | DbPage *pDbPage; |
| 1125 | MemPage *pPage; |
| 1126 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1127 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1128 | if( pgno==0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1129 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1130 | } |
danielk1977 | 9f580ad | 2008-09-10 14:45:57 +0000 | [diff] [blame] | 1131 | |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1132 | /* It is often the case that the page we want is already in cache. |
| 1133 | ** If so, get it directly. This saves us from having to call |
| 1134 | ** pagerPagecount() to make sure pgno is within limits, which results |
| 1135 | ** in a measureable performance improvements. |
| 1136 | */ |
| 1137 | pDbPage = sqlite3PagerLookup(pBt->pPager, pgno); |
| 1138 | if( pDbPage ){ |
| 1139 | /* Page is already in cache */ |
| 1140 | *ppPage = pPage = btreePageFromDbPage(pDbPage, pgno, pBt); |
| 1141 | rc = SQLITE_OK; |
| 1142 | }else{ |
| 1143 | /* Page not in cache. Acquire it. */ |
| 1144 | if( pgno>pagerPagecount(pBt->pPager) ){ |
| 1145 | return SQLITE_CORRUPT_BKPT; |
| 1146 | } |
| 1147 | rc = sqlite3BtreeGetPage(pBt, pgno, ppPage, 0); |
| 1148 | if( rc ) return rc; |
| 1149 | pPage = *ppPage; |
| 1150 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1151 | if( !pPage->isInit ){ |
| 1152 | rc = sqlite3BtreeInitPage(pPage); |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1153 | } |
| 1154 | if( rc!=SQLITE_OK ){ |
| 1155 | releasePage(pPage); |
| 1156 | *ppPage = 0; |
| 1157 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1158 | return rc; |
| 1159 | } |
| 1160 | |
| 1161 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1162 | ** Release a MemPage. This should be called once for each prior |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1163 | ** call to sqlite3BtreeGetPage. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1164 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1165 | static void releasePage(MemPage *pPage){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1166 | if( pPage ){ |
| 1167 | assert( pPage->aData ); |
| 1168 | assert( pPage->pBt ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1169 | assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); |
| 1170 | assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1171 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1172 | sqlite3PagerUnref(pPage->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1177 | ** During a rollback, when the pager reloads information into the cache |
| 1178 | ** so that the cache is restored to its original state at the start of |
| 1179 | ** the transaction, for each page restored this routine is called. |
| 1180 | ** |
| 1181 | ** This routine needs to reset the extra data section at the end of the |
| 1182 | ** page to agree with the restored data. |
| 1183 | */ |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1184 | static void pageReinit(DbPage *pData){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1185 | MemPage *pPage; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1186 | pPage = (MemPage *)sqlite3PagerGetExtra(pData); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1187 | if( pPage->isInit ){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1188 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1189 | pPage->isInit = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1190 | if( sqlite3PagerPageRefcount(pData)>0 ){ |
| 1191 | sqlite3BtreeInitPage(pPage); |
| 1192 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | /* |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1197 | ** Invoke the busy handler for a btree. |
| 1198 | */ |
| 1199 | static int sqlite3BtreeInvokeBusyHandler(void *pArg, int n){ |
| 1200 | BtShared *pBt = (BtShared*)pArg; |
| 1201 | assert( pBt->db ); |
| 1202 | assert( sqlite3_mutex_held(pBt->db->mutex) ); |
| 1203 | return sqlite3InvokeBusyHandler(&pBt->db->busyHandler); |
| 1204 | } |
| 1205 | |
| 1206 | /* |
drh | ad3e010 | 2004-09-03 23:32:18 +0000 | [diff] [blame] | 1207 | ** Open a database file. |
| 1208 | ** |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1209 | ** zFilename is the name of the database file. If zFilename is NULL |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1210 | ** a new database with a random name is created. This randomly named |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1211 | ** database file will be deleted when sqlite3BtreeClose() is called. |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1212 | ** If zFilename is ":memory:" then an in-memory database is created |
| 1213 | ** that is automatically destroyed when it is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1214 | */ |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1215 | int sqlite3BtreeOpen( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1216 | const char *zFilename, /* Name of the file containing the BTree database */ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1217 | sqlite3 *db, /* Associated database handle */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1218 | Btree **ppBtree, /* Pointer to new Btree object written here */ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1219 | int flags, /* Options */ |
| 1220 | int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1221 | ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1222 | sqlite3_vfs *pVfs; /* The VFS to use for this btree */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1223 | BtShared *pBt = 0; /* Shared part of btree structure */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1224 | Btree *p; /* Handle to return */ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1225 | int rc = SQLITE_OK; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1226 | int nReserve; |
| 1227 | unsigned char zDbHeader[100]; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1228 | |
| 1229 | /* Set the variable isMemdb to true for an in-memory database, or |
| 1230 | ** false for a file-based database. This symbol is only required if |
| 1231 | ** either of the shared-data or autovacuum features are compiled |
| 1232 | ** into the library. |
| 1233 | */ |
| 1234 | #if !defined(SQLITE_OMIT_SHARED_CACHE) || !defined(SQLITE_OMIT_AUTOVACUUM) |
| 1235 | #ifdef SQLITE_OMIT_MEMORYDB |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 1236 | const int isMemdb = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1237 | #else |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 1238 | const int isMemdb = zFilename && !strcmp(zFilename, ":memory:"); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1239 | #endif |
| 1240 | #endif |
| 1241 | |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1242 | assert( db!=0 ); |
| 1243 | assert( sqlite3_mutex_held(db->mutex) ); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1244 | |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1245 | pVfs = db->pVfs; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1246 | p = sqlite3MallocZero(sizeof(Btree)); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1247 | if( !p ){ |
| 1248 | return SQLITE_NOMEM; |
| 1249 | } |
| 1250 | p->inTrans = TRANS_NONE; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1251 | p->db = db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1252 | |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 1253 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1254 | /* |
| 1255 | ** If this Btree is a candidate for shared cache, try to find an |
| 1256 | ** existing BtShared object that we can share with |
| 1257 | */ |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 1258 | if( isMemdb==0 |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1259 | && (db->flags & SQLITE_Vtab)==0 |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1260 | && zFilename && zFilename[0] |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1261 | ){ |
danielk1977 | 502b4e0 | 2008-09-02 14:07:24 +0000 | [diff] [blame] | 1262 | if( sqlite3GlobalConfig.sharedCacheEnabled ){ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 1263 | int nFullPathname = pVfs->mxPathname+1; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1264 | char *zFullPathname = sqlite3Malloc(nFullPathname); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1265 | sqlite3_mutex *mutexShared; |
| 1266 | p->sharable = 1; |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 1267 | db->flags |= SQLITE_SharedCache; |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1268 | if( !zFullPathname ){ |
| 1269 | sqlite3_free(p); |
| 1270 | return SQLITE_NOMEM; |
| 1271 | } |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 1272 | sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname); |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 1273 | mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1274 | sqlite3_mutex_enter(mutexShared); |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 1275 | for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1276 | assert( pBt->nRef>0 ); |
| 1277 | if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager)) |
| 1278 | && sqlite3PagerVfs(pBt->pPager)==pVfs ){ |
| 1279 | p->pBt = pBt; |
| 1280 | pBt->nRef++; |
| 1281 | break; |
| 1282 | } |
| 1283 | } |
| 1284 | sqlite3_mutex_leave(mutexShared); |
| 1285 | sqlite3_free(zFullPathname); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1286 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1287 | #ifdef SQLITE_DEBUG |
| 1288 | else{ |
| 1289 | /* In debug mode, we mark all persistent databases as sharable |
| 1290 | ** even when they are not. This exercises the locking code and |
| 1291 | ** gives more opportunity for asserts(sqlite3_mutex_held()) |
| 1292 | ** statements to find locking problems. |
| 1293 | */ |
| 1294 | p->sharable = 1; |
| 1295 | } |
| 1296 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1297 | } |
| 1298 | #endif |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1299 | if( pBt==0 ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1300 | /* |
| 1301 | ** The following asserts make sure that structures used by the btree are |
| 1302 | ** the right size. This is to guard against size changes that result |
| 1303 | ** when compiling on a different architecture. |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 1304 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1305 | assert( sizeof(i64)==8 || sizeof(i64)==4 ); |
| 1306 | assert( sizeof(u64)==8 || sizeof(u64)==4 ); |
| 1307 | assert( sizeof(u32)==4 ); |
| 1308 | assert( sizeof(u16)==2 ); |
| 1309 | assert( sizeof(Pgno)==4 ); |
| 1310 | |
| 1311 | pBt = sqlite3MallocZero( sizeof(*pBt) ); |
| 1312 | if( pBt==0 ){ |
| 1313 | rc = SQLITE_NOMEM; |
| 1314 | goto btree_open_out; |
| 1315 | } |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1316 | pBt->busyHdr.xFunc = sqlite3BtreeInvokeBusyHandler; |
| 1317 | pBt->busyHdr.pArg = pBt; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1318 | rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename, |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1319 | EXTRA_SIZE, flags, vfsFlags); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1320 | if( rc==SQLITE_OK ){ |
| 1321 | rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader); |
| 1322 | } |
| 1323 | if( rc!=SQLITE_OK ){ |
| 1324 | goto btree_open_out; |
| 1325 | } |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1326 | sqlite3PagerSetBusyhandler(pBt->pPager, &pBt->busyHdr); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1327 | p->pBt = pBt; |
| 1328 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1329 | sqlite3PagerSetReiniter(pBt->pPager, pageReinit); |
| 1330 | pBt->pCursor = 0; |
| 1331 | pBt->pPage1 = 0; |
| 1332 | pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager); |
| 1333 | pBt->pageSize = get2byte(&zDbHeader[16]); |
| 1334 | if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE |
| 1335 | || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1336 | pBt->pageSize = 0; |
| 1337 | sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1338 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1339 | /* If the magic name ":memory:" will create an in-memory database, then |
| 1340 | ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if |
| 1341 | ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if |
| 1342 | ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a |
| 1343 | ** regular file-name. In this case the auto-vacuum applies as per normal. |
| 1344 | */ |
| 1345 | if( zFilename && !isMemdb ){ |
| 1346 | pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0); |
| 1347 | pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0); |
| 1348 | } |
| 1349 | #endif |
| 1350 | nReserve = 0; |
| 1351 | }else{ |
| 1352 | nReserve = zDbHeader[20]; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1353 | pBt->pageSizeFixed = 1; |
| 1354 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1355 | pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0); |
| 1356 | pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0); |
| 1357 | #endif |
| 1358 | } |
| 1359 | pBt->usableSize = pBt->pageSize - nReserve; |
| 1360 | assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1361 | sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1362 | |
| 1363 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
| 1364 | /* Add the new BtShared object to the linked list sharable BtShareds. |
| 1365 | */ |
| 1366 | if( p->sharable ){ |
| 1367 | sqlite3_mutex *mutexShared; |
| 1368 | pBt->nRef = 1; |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 1369 | mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 1370 | if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){ |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 1371 | pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST); |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 1372 | if( pBt->mutex==0 ){ |
| 1373 | rc = SQLITE_NOMEM; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1374 | db->mallocFailed = 0; |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 1375 | goto btree_open_out; |
| 1376 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1377 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1378 | sqlite3_mutex_enter(mutexShared); |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 1379 | pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList); |
| 1380 | GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1381 | sqlite3_mutex_leave(mutexShared); |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1382 | } |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1383 | #endif |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1384 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1385 | |
drh | cfed7bc | 2006-03-13 14:28:05 +0000 | [diff] [blame] | 1386 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1387 | /* If the new Btree uses a sharable pBtShared, then link the new |
| 1388 | ** Btree into the list of all sharable Btrees for the same connection. |
drh | abddb0c | 2007-08-20 13:14:28 +0000 | [diff] [blame] | 1389 | ** The list is kept in ascending order by pBt address. |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 1390 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1391 | if( p->sharable ){ |
| 1392 | int i; |
| 1393 | Btree *pSib; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1394 | for(i=0; i<db->nDb; i++){ |
| 1395 | if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1396 | while( pSib->pPrev ){ pSib = pSib->pPrev; } |
| 1397 | if( p->pBt<pSib->pBt ){ |
| 1398 | p->pNext = pSib; |
| 1399 | p->pPrev = 0; |
| 1400 | pSib->pPrev = p; |
| 1401 | }else{ |
drh | abddb0c | 2007-08-20 13:14:28 +0000 | [diff] [blame] | 1402 | while( pSib->pNext && pSib->pNext->pBt<p->pBt ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1403 | pSib = pSib->pNext; |
| 1404 | } |
| 1405 | p->pNext = pSib->pNext; |
| 1406 | p->pPrev = pSib; |
| 1407 | if( p->pNext ){ |
| 1408 | p->pNext->pPrev = p; |
| 1409 | } |
| 1410 | pSib->pNext = p; |
| 1411 | } |
| 1412 | break; |
| 1413 | } |
| 1414 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1415 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1416 | #endif |
| 1417 | *ppBtree = p; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1418 | |
| 1419 | btree_open_out: |
| 1420 | if( rc!=SQLITE_OK ){ |
| 1421 | if( pBt && pBt->pPager ){ |
| 1422 | sqlite3PagerClose(pBt->pPager); |
| 1423 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1424 | sqlite3_free(pBt); |
| 1425 | sqlite3_free(p); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1426 | *ppBtree = 0; |
| 1427 | } |
| 1428 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | /* |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1432 | ** Decrement the BtShared.nRef counter. When it reaches zero, |
| 1433 | ** remove the BtShared structure from the sharing list. Return |
| 1434 | ** true if the BtShared.nRef counter reaches zero and return |
| 1435 | ** false if it is still positive. |
| 1436 | */ |
| 1437 | static int removeFromSharingList(BtShared *pBt){ |
| 1438 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 1439 | sqlite3_mutex *pMaster; |
| 1440 | BtShared *pList; |
| 1441 | int removed = 0; |
| 1442 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1443 | assert( sqlite3_mutex_notheld(pBt->mutex) ); |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 1444 | pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1445 | sqlite3_mutex_enter(pMaster); |
| 1446 | pBt->nRef--; |
| 1447 | if( pBt->nRef<=0 ){ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 1448 | if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){ |
| 1449 | GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1450 | }else{ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 1451 | pList = GLOBAL(BtShared*,sqlite3SharedCacheList); |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 1452 | while( ALWAYS(pList) && pList->pNext!=pBt ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1453 | pList=pList->pNext; |
| 1454 | } |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 1455 | if( ALWAYS(pList) ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1456 | pList->pNext = pBt->pNext; |
| 1457 | } |
| 1458 | } |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 1459 | if( SQLITE_THREADSAFE ){ |
| 1460 | sqlite3_mutex_free(pBt->mutex); |
| 1461 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1462 | removed = 1; |
| 1463 | } |
| 1464 | sqlite3_mutex_leave(pMaster); |
| 1465 | return removed; |
| 1466 | #else |
| 1467 | return 1; |
| 1468 | #endif |
| 1469 | } |
| 1470 | |
| 1471 | /* |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1472 | ** Make sure pBt->pTmpSpace points to an allocation of |
| 1473 | ** MX_CELL_SIZE(pBt) bytes. |
| 1474 | */ |
| 1475 | static void allocateTempSpace(BtShared *pBt){ |
| 1476 | if( !pBt->pTmpSpace ){ |
| 1477 | pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize ); |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | /* |
| 1482 | ** Free the pBt->pTmpSpace allocation |
| 1483 | */ |
| 1484 | static void freeTempSpace(BtShared *pBt){ |
| 1485 | sqlite3PageFree( pBt->pTmpSpace); |
| 1486 | pBt->pTmpSpace = 0; |
| 1487 | } |
| 1488 | |
| 1489 | /* |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1490 | ** Close an open database and invalidate all cursors. |
| 1491 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1492 | int sqlite3BtreeClose(Btree *p){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1493 | BtShared *pBt = p->pBt; |
| 1494 | BtCursor *pCur; |
| 1495 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1496 | /* Close all cursors opened via this handle. */ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1497 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1498 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1499 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1500 | pCur = pBt->pCursor; |
| 1501 | while( pCur ){ |
| 1502 | BtCursor *pTmp = pCur; |
| 1503 | pCur = pCur->pNext; |
| 1504 | if( pTmp->pBtree==p ){ |
| 1505 | sqlite3BtreeCloseCursor(pTmp); |
| 1506 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1507 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1508 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 1509 | /* Rollback any active transaction and free the handle structure. |
| 1510 | ** The call to sqlite3BtreeRollback() drops any table-locks held by |
| 1511 | ** this handle. |
| 1512 | */ |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 1513 | sqlite3BtreeRollback(p); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1514 | sqlite3BtreeLeave(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1515 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1516 | /* If there are still other outstanding references to the shared-btree |
| 1517 | ** structure, return now. The remainder of this procedure cleans |
| 1518 | ** up the shared-btree. |
| 1519 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1520 | assert( p->wantToLock==0 && p->locked==0 ); |
| 1521 | if( !p->sharable || removeFromSharingList(pBt) ){ |
| 1522 | /* The pBt is no longer on the sharing list, so we can access |
| 1523 | ** it without having to hold the mutex. |
| 1524 | ** |
| 1525 | ** Clean out and delete the BtShared object. |
| 1526 | */ |
| 1527 | assert( !pBt->pCursor ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1528 | sqlite3PagerClose(pBt->pPager); |
| 1529 | if( pBt->xFreeSchema && pBt->pSchema ){ |
| 1530 | pBt->xFreeSchema(pBt->pSchema); |
| 1531 | } |
| 1532 | sqlite3_free(pBt->pSchema); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1533 | freeTempSpace(pBt); |
drh | 65bbf29 | 2008-06-19 01:03:17 +0000 | [diff] [blame] | 1534 | sqlite3_free(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1537 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | cab5ed7 | 2007-08-22 11:41:18 +0000 | [diff] [blame] | 1538 | assert( p->wantToLock==0 ); |
| 1539 | assert( p->locked==0 ); |
| 1540 | if( p->pPrev ) p->pPrev->pNext = p->pNext; |
| 1541 | if( p->pNext ) p->pNext->pPrev = p->pPrev; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1542 | #endif |
| 1543 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1544 | sqlite3_free(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1545 | return SQLITE_OK; |
| 1546 | } |
| 1547 | |
| 1548 | /* |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1549 | ** Change the limit on the number of pages allowed in the cache. |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 1550 | ** |
| 1551 | ** The maximum number of cache pages is set to the absolute |
| 1552 | ** value of mxPage. If mxPage is negative, the pager will |
| 1553 | ** operate asynchronously - it will not stop to do fsync()s |
| 1554 | ** to insure data is written to the disk surface before |
| 1555 | ** continuing. Transactions still work if synchronous is off, |
| 1556 | ** and the database cannot be corrupted if this program |
| 1557 | ** crashes. But if the operating system crashes or there is |
| 1558 | ** an abrupt power failure when synchronous is off, the database |
| 1559 | ** could be left in an inconsistent and unrecoverable state. |
| 1560 | ** Synchronous is on by default so database corruption is not |
| 1561 | ** normally a worry. |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1562 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1563 | int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){ |
| 1564 | BtShared *pBt = p->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1565 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1566 | sqlite3BtreeEnter(p); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1567 | sqlite3PagerSetCachesize(pBt->pPager, mxPage); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1568 | sqlite3BtreeLeave(p); |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1569 | return SQLITE_OK; |
| 1570 | } |
| 1571 | |
| 1572 | /* |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1573 | ** Change the way data is synced to disk in order to increase or decrease |
| 1574 | ** how well the database resists damage due to OS crashes and power |
| 1575 | ** failures. Level 1 is the same as asynchronous (no syncs() occur and |
| 1576 | ** there is a high probability of damage) Level 2 is the default. There |
| 1577 | ** is a very low but non-zero probability of damage. Level 3 reduces the |
| 1578 | ** probability of damage to near zero but with a write performance reduction. |
| 1579 | */ |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1580 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
drh | ac530b1 | 2006-02-11 01:25:50 +0000 | [diff] [blame] | 1581 | int sqlite3BtreeSetSafetyLevel(Btree *p, int level, int fullSync){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1582 | BtShared *pBt = p->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1583 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1584 | sqlite3BtreeEnter(p); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1585 | sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1586 | sqlite3BtreeLeave(p); |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1587 | return SQLITE_OK; |
| 1588 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1589 | #endif |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1590 | |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1591 | /* |
| 1592 | ** Return TRUE if the given btree is set to safety level 1. In other |
| 1593 | ** words, return TRUE if no sync() occurs on the disk files. |
| 1594 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1595 | int sqlite3BtreeSyncDisabled(Btree *p){ |
| 1596 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1597 | int rc; |
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); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 1600 | assert( pBt && pBt->pPager ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1601 | rc = sqlite3PagerNosync(pBt->pPager); |
| 1602 | sqlite3BtreeLeave(p); |
| 1603 | return rc; |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1606 | #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1607 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1608 | ** Change the default pages size and the number of reserved bytes per page. |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 1609 | ** |
| 1610 | ** The page size must be a power of 2 between 512 and 65536. If the page |
| 1611 | ** size supplied does not meet this constraint then the page size is not |
| 1612 | ** changed. |
| 1613 | ** |
| 1614 | ** Page sizes are constrained to be a power of two so that the region |
| 1615 | ** of the database file used for locking (beginning at PENDING_BYTE, |
| 1616 | ** the first byte past the 1GB boundary, 0x40000000) needs to occur |
| 1617 | ** at the beginning of a page. |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 1618 | ** |
| 1619 | ** If parameter nReserve is less than zero, then the number of reserved |
| 1620 | ** bytes per page is left unchanged. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1621 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1622 | int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1623 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1624 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1625 | sqlite3BtreeEnter(p); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1626 | if( pBt->pageSizeFixed ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1627 | sqlite3BtreeLeave(p); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1628 | return SQLITE_READONLY; |
| 1629 | } |
| 1630 | if( nReserve<0 ){ |
| 1631 | nReserve = pBt->pageSize - pBt->usableSize; |
| 1632 | } |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 1633 | if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE && |
| 1634 | ((pageSize-1)&pageSize)==0 ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1635 | assert( (pageSize & 7)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1636 | assert( !pBt->pPage1 && !pBt->pCursor ); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1637 | pBt->pageSize = pageSize; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1638 | freeTempSpace(pBt); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1639 | rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1640 | } |
| 1641 | pBt->usableSize = pBt->pageSize - nReserve; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1642 | sqlite3BtreeLeave(p); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1643 | return rc; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | /* |
| 1647 | ** Return the currently defined page size |
| 1648 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1649 | int sqlite3BtreeGetPageSize(Btree *p){ |
| 1650 | return p->pBt->pageSize; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1651 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1652 | int sqlite3BtreeGetReserve(Btree *p){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1653 | int n; |
| 1654 | sqlite3BtreeEnter(p); |
| 1655 | n = p->pBt->pageSize - p->pBt->usableSize; |
| 1656 | sqlite3BtreeLeave(p); |
| 1657 | return n; |
drh | 2011d5f | 2004-07-22 02:40:37 +0000 | [diff] [blame] | 1658 | } |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 1659 | |
| 1660 | /* |
| 1661 | ** Set the maximum page count for a database if mxPage is positive. |
| 1662 | ** No changes are made if mxPage is 0 or negative. |
| 1663 | ** Regardless of the value of mxPage, return the maximum page count. |
| 1664 | */ |
| 1665 | int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1666 | int n; |
| 1667 | sqlite3BtreeEnter(p); |
| 1668 | n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage); |
| 1669 | sqlite3BtreeLeave(p); |
| 1670 | return n; |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 1671 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1672 | #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1673 | |
| 1674 | /* |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1675 | ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum' |
| 1676 | ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it |
| 1677 | ** is disabled. The default value for the auto-vacuum property is |
| 1678 | ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro. |
| 1679 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1680 | int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1681 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1682 | return SQLITE_READONLY; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1683 | #else |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1684 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1685 | int rc = SQLITE_OK; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1686 | int av = (autoVacuum?1:0); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1687 | |
| 1688 | sqlite3BtreeEnter(p); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1689 | if( pBt->pageSizeFixed && av!=pBt->autoVacuum ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1690 | rc = SQLITE_READONLY; |
| 1691 | }else{ |
| 1692 | pBt->autoVacuum = av; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1693 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1694 | sqlite3BtreeLeave(p); |
| 1695 | return rc; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1696 | #endif |
| 1697 | } |
| 1698 | |
| 1699 | /* |
| 1700 | ** Return the value of the 'auto-vacuum' property. If auto-vacuum is |
| 1701 | ** enabled 1 is returned. Otherwise 0. |
| 1702 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1703 | int sqlite3BtreeGetAutoVacuum(Btree *p){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1704 | #ifdef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1705 | return BTREE_AUTOVACUUM_NONE; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1706 | #else |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1707 | int rc; |
| 1708 | sqlite3BtreeEnter(p); |
| 1709 | rc = ( |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1710 | (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE: |
| 1711 | (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL: |
| 1712 | BTREE_AUTOVACUUM_INCR |
| 1713 | ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1714 | sqlite3BtreeLeave(p); |
| 1715 | return rc; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1716 | #endif |
| 1717 | } |
| 1718 | |
| 1719 | |
| 1720 | /* |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1721 | ** Get a reference to pPage1 of the database file. This will |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1722 | ** also acquire a readlock on that file. |
| 1723 | ** |
| 1724 | ** SQLITE_OK is returned on success. If the file is not a |
| 1725 | ** well-formed database file, then SQLITE_CORRUPT is returned. |
| 1726 | ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 1727 | ** is returned if we run out of memory. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1728 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1729 | static int lockBtree(BtShared *pBt){ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1730 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1731 | MemPage *pPage1; |
danielk1977 | 93f7af9 | 2008-05-09 16:57:50 +0000 | [diff] [blame] | 1732 | int nPage; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1733 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1734 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1735 | if( pBt->pPage1 ) return SQLITE_OK; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1736 | rc = sqlite3BtreeGetPage(pBt, 1, &pPage1, 0); |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1737 | if( rc!=SQLITE_OK ) return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1738 | |
| 1739 | /* Do some checking to help insure the file we opened really is |
| 1740 | ** a valid database file. |
| 1741 | */ |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 1742 | rc = sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 1743 | if( rc!=SQLITE_OK ){ |
danielk1977 | 93f7af9 | 2008-05-09 16:57:50 +0000 | [diff] [blame] | 1744 | goto page1_init_failed; |
| 1745 | }else if( nPage>0 ){ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1746 | int pageSize; |
| 1747 | int usableSize; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1748 | u8 *page1 = pPage1->aData; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 1749 | rc = SQLITE_NOTADB; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1750 | if( memcmp(page1, zMagicHeader, 16)!=0 ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1751 | goto page1_init_failed; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1752 | } |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 1753 | if( page1[18]>1 ){ |
| 1754 | pBt->readOnly = 1; |
| 1755 | } |
| 1756 | if( page1[19]>1 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1757 | goto page1_init_failed; |
| 1758 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1759 | |
| 1760 | /* The maximum embedded fraction must be exactly 25%. And the minimum |
| 1761 | ** embedded fraction must be 12.5% for both leaf-data and non-leaf-data. |
| 1762 | ** The original design allowed these amounts to vary, but as of |
| 1763 | ** version 3.6.0, we require them to be fixed. |
| 1764 | */ |
| 1765 | if( memcmp(&page1[21], "\100\040\040",3)!=0 ){ |
| 1766 | goto page1_init_failed; |
| 1767 | } |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1768 | pageSize = get2byte(&page1[16]); |
drh | 7dc385e | 2007-09-06 23:39:36 +0000 | [diff] [blame] | 1769 | if( ((pageSize-1)&pageSize)!=0 || pageSize<512 || |
| 1770 | (SQLITE_MAX_PAGE_SIZE<32768 && pageSize>SQLITE_MAX_PAGE_SIZE) |
| 1771 | ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1772 | goto page1_init_failed; |
| 1773 | } |
| 1774 | assert( (pageSize & 7)==0 ); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1775 | usableSize = pageSize - page1[20]; |
| 1776 | if( pageSize!=pBt->pageSize ){ |
| 1777 | /* After reading the first page of the database assuming a page size |
| 1778 | ** of BtShared.pageSize, we have discovered that the page-size is |
| 1779 | ** actually pageSize. Unlock the database, leave pBt->pPage1 at |
| 1780 | ** zero and return SQLITE_OK. The caller will call this function |
| 1781 | ** again with the correct page-size. |
| 1782 | */ |
| 1783 | releasePage(pPage1); |
| 1784 | pBt->usableSize = usableSize; |
| 1785 | pBt->pageSize = pageSize; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1786 | freeTempSpace(pBt); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1787 | sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
| 1788 | return SQLITE_OK; |
| 1789 | } |
| 1790 | if( usableSize<500 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1791 | goto page1_init_failed; |
| 1792 | } |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1793 | pBt->pageSize = pageSize; |
| 1794 | pBt->usableSize = usableSize; |
drh | 057cd3a | 2005-02-15 16:23:02 +0000 | [diff] [blame] | 1795 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1796 | pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0); |
danielk1977 | 27b1f95 | 2007-06-25 08:16:58 +0000 | [diff] [blame] | 1797 | pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0); |
drh | 057cd3a | 2005-02-15 16:23:02 +0000 | [diff] [blame] | 1798 | #endif |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1799 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1800 | |
| 1801 | /* maxLocal is the maximum amount of payload to store locally for |
| 1802 | ** a cell. Make sure it is small enough so that at least minFanout |
| 1803 | ** cells can will fit on one page. We assume a 10-byte page header. |
| 1804 | ** Besides the payload, the cell must store: |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1805 | ** 2-byte pointer to the cell |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1806 | ** 4-byte child pointer |
| 1807 | ** 9-byte nKey value |
| 1808 | ** 4-byte nData value |
| 1809 | ** 4-byte overflow page pointer |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1810 | ** So a cell consists of a 2-byte poiner, a header which is as much as |
| 1811 | ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow |
| 1812 | ** page pointer. |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1813 | */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1814 | pBt->maxLocal = (pBt->usableSize-12)*64/255 - 23; |
| 1815 | pBt->minLocal = (pBt->usableSize-12)*32/255 - 23; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1816 | pBt->maxLeaf = pBt->usableSize - 35; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1817 | pBt->minLeaf = (pBt->usableSize-12)*32/255 - 23; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1818 | assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1819 | pBt->pPage1 = pPage1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1820 | return SQLITE_OK; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1821 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1822 | page1_init_failed: |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1823 | releasePage(pPage1); |
| 1824 | pBt->pPage1 = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1825 | return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1826 | } |
| 1827 | |
| 1828 | /* |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1829 | ** This routine works like lockBtree() except that it also invokes the |
| 1830 | ** busy callback if there is lock contention. |
| 1831 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1832 | static int lockBtreeWithRetry(Btree *pRef){ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1833 | int rc = SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1834 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1835 | assert( sqlite3BtreeHoldsMutex(pRef) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1836 | if( pRef->inTrans==TRANS_NONE ){ |
| 1837 | u8 inTransaction = pRef->pBt->inTransaction; |
| 1838 | btreeIntegrity(pRef); |
| 1839 | rc = sqlite3BtreeBeginTrans(pRef, 0); |
| 1840 | pRef->pBt->inTransaction = inTransaction; |
| 1841 | pRef->inTrans = TRANS_NONE; |
| 1842 | if( rc==SQLITE_OK ){ |
| 1843 | pRef->pBt->nTransaction--; |
| 1844 | } |
| 1845 | btreeIntegrity(pRef); |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1846 | } |
| 1847 | return rc; |
| 1848 | } |
| 1849 | |
| 1850 | |
| 1851 | /* |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1852 | ** If there are no outstanding cursors and we are not in the middle |
| 1853 | ** of a transaction but there is a read lock on the database, then |
| 1854 | ** this routine unrefs the first page of the database file which |
| 1855 | ** has the effect of releasing the read lock. |
| 1856 | ** |
| 1857 | ** If there are any outstanding cursors, this routine is a no-op. |
| 1858 | ** |
| 1859 | ** If there is a transaction in progress, this routine is a no-op. |
| 1860 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1861 | static void unlockBtreeIfUnused(BtShared *pBt){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1862 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1863 | if( pBt->inTransaction==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1864 | if( sqlite3PagerRefcount(pBt->pPager)>=1 ){ |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 1865 | assert( pBt->pPage1->aData ); |
| 1866 | #if 0 |
drh | 24c9a2e | 2007-01-05 02:00:47 +0000 | [diff] [blame] | 1867 | if( pBt->pPage1->aData==0 ){ |
| 1868 | MemPage *pPage = pBt->pPage1; |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1869 | pPage->aData = sqlite3PagerGetData(pPage->pDbPage); |
drh | 24c9a2e | 2007-01-05 02:00:47 +0000 | [diff] [blame] | 1870 | pPage->pBt = pBt; |
| 1871 | pPage->pgno = 1; |
| 1872 | } |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 1873 | #endif |
drh | 24c9a2e | 2007-01-05 02:00:47 +0000 | [diff] [blame] | 1874 | releasePage(pBt->pPage1); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 1875 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1876 | pBt->pPage1 = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1877 | pBt->inStmt = 0; |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1878 | } |
| 1879 | } |
| 1880 | |
| 1881 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1882 | ** Create a new database by initializing the first page of the |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1883 | ** file. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1884 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1885 | static int newDatabase(BtShared *pBt){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1886 | MemPage *pP1; |
| 1887 | unsigned char *data; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1888 | int rc; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 1889 | int nPage; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1890 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1891 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 1892 | rc = sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 1893 | if( rc!=SQLITE_OK || nPage>0 ){ |
| 1894 | return rc; |
| 1895 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1896 | pP1 = pBt->pPage1; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1897 | assert( pP1!=0 ); |
| 1898 | data = pP1->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1899 | rc = sqlite3PagerWrite(pP1->pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1900 | if( rc ) return rc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1901 | memcpy(data, zMagicHeader, sizeof(zMagicHeader)); |
| 1902 | assert( sizeof(zMagicHeader)==16 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1903 | put2byte(&data[16], pBt->pageSize); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1904 | data[18] = 1; |
| 1905 | data[19] = 1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1906 | data[20] = pBt->pageSize - pBt->usableSize; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1907 | data[21] = 64; |
| 1908 | data[22] = 32; |
| 1909 | data[23] = 32; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1910 | memset(&data[24], 0, 100-24); |
drh | e6c4381 | 2004-05-14 12:17:46 +0000 | [diff] [blame] | 1911 | zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA ); |
drh | f2a611c | 2004-09-05 00:33:43 +0000 | [diff] [blame] | 1912 | pBt->pageSizeFixed = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1913 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1914 | assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 ); |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 1915 | assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 ); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1916 | put4byte(&data[36 + 4*4], pBt->autoVacuum); |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 1917 | put4byte(&data[36 + 7*4], pBt->incrVacuum); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1918 | #endif |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1919 | return SQLITE_OK; |
| 1920 | } |
| 1921 | |
| 1922 | /* |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1923 | ** Attempt to start a new transaction. A write-transaction |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 1924 | ** is started if the second argument is nonzero, otherwise a read- |
| 1925 | ** transaction. If the second argument is 2 or more and exclusive |
| 1926 | ** transaction is started, meaning that no other process is allowed |
| 1927 | ** to access the database. A preexisting transaction may not be |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1928 | ** upgraded to exclusive by calling this routine a second time - the |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 1929 | ** exclusivity flag only works for a new transaction. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1930 | ** |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1931 | ** A write-transaction must be started before attempting any |
| 1932 | ** changes to the database. None of the following routines |
| 1933 | ** will work unless a transaction is started first: |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1934 | ** |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1935 | ** sqlite3BtreeCreateTable() |
| 1936 | ** sqlite3BtreeCreateIndex() |
| 1937 | ** sqlite3BtreeClearTable() |
| 1938 | ** sqlite3BtreeDropTable() |
| 1939 | ** sqlite3BtreeInsert() |
| 1940 | ** sqlite3BtreeDelete() |
| 1941 | ** sqlite3BtreeUpdateMeta() |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1942 | ** |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1943 | ** If an initial attempt to acquire the lock fails because of lock contention |
| 1944 | ** and the database was previously unlocked, then invoke the busy handler |
| 1945 | ** if there is one. But if there was previously a read-lock, do not |
| 1946 | ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is |
| 1947 | ** returned when there is already a read-lock in order to avoid a deadlock. |
| 1948 | ** |
| 1949 | ** Suppose there are two processes A and B. A has a read lock and B has |
| 1950 | ** a reserved lock. B tries to promote to exclusive but is blocked because |
| 1951 | ** of A's read lock. A tries to promote to reserved but is blocked by B. |
| 1952 | ** One or the other of the two processes must give way or there can be |
| 1953 | ** no progress. By returning SQLITE_BUSY and not invoking the busy callback |
| 1954 | ** when A already has a read lock, we encourage A to give up and let B |
| 1955 | ** proceed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1956 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1957 | int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ |
| 1958 | BtShared *pBt = p->pBt; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1959 | int rc = SQLITE_OK; |
| 1960 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1961 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1962 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1963 | btreeIntegrity(p); |
| 1964 | |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1965 | /* If the btree is already in a write-transaction, or it |
| 1966 | ** is already in a read-transaction and a read-transaction |
| 1967 | ** is requested, this is a no-op. |
| 1968 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1969 | if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1970 | goto trans_begun; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1971 | } |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1972 | |
| 1973 | /* Write transactions are not possible on a read-only database */ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1974 | if( pBt->readOnly && wrflag ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1975 | rc = SQLITE_READONLY; |
| 1976 | goto trans_begun; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1977 | } |
| 1978 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1979 | /* If another database handle has already opened a write transaction |
| 1980 | ** on this shared-btree structure and a second write transaction is |
| 1981 | ** requested, return SQLITE_BUSY. |
| 1982 | */ |
| 1983 | if( pBt->inTransaction==TRANS_WRITE && wrflag ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1984 | rc = SQLITE_BUSY; |
| 1985 | goto trans_begun; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1986 | } |
| 1987 | |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 1988 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 1989 | if( wrflag>1 ){ |
| 1990 | BtLock *pIter; |
| 1991 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 1992 | if( pIter->pBtree!=p ){ |
| 1993 | rc = SQLITE_BUSY; |
| 1994 | goto trans_begun; |
| 1995 | } |
| 1996 | } |
| 1997 | } |
| 1998 | #endif |
| 1999 | |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2000 | do { |
drh | 8a9c17f | 2008-05-02 14:23:54 +0000 | [diff] [blame] | 2001 | if( pBt->pPage1==0 ){ |
| 2002 | do{ |
| 2003 | rc = lockBtree(pBt); |
| 2004 | }while( pBt->pPage1==0 && rc==SQLITE_OK ); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2005 | } |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 2006 | |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2007 | if( rc==SQLITE_OK && wrflag ){ |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 2008 | if( pBt->readOnly ){ |
| 2009 | rc = SQLITE_READONLY; |
| 2010 | }else{ |
| 2011 | rc = sqlite3PagerBegin(pBt->pPage1->pDbPage, wrflag>1); |
| 2012 | if( rc==SQLITE_OK ){ |
| 2013 | rc = newDatabase(pBt); |
| 2014 | } |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2015 | } |
| 2016 | } |
| 2017 | |
| 2018 | if( rc==SQLITE_OK ){ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2019 | if( wrflag ) pBt->inStmt = 0; |
| 2020 | }else{ |
| 2021 | unlockBtreeIfUnused(pBt); |
| 2022 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2023 | }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE && |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2024 | sqlite3BtreeInvokeBusyHandler(pBt, 0) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2025 | |
| 2026 | if( rc==SQLITE_OK ){ |
| 2027 | if( p->inTrans==TRANS_NONE ){ |
| 2028 | pBt->nTransaction++; |
| 2029 | } |
| 2030 | p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ); |
| 2031 | if( p->inTrans>pBt->inTransaction ){ |
| 2032 | pBt->inTransaction = p->inTrans; |
| 2033 | } |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 2034 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 2035 | if( wrflag>1 ){ |
| 2036 | assert( !pBt->pExclusive ); |
| 2037 | pBt->pExclusive = p; |
| 2038 | } |
| 2039 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2040 | } |
| 2041 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2042 | |
| 2043 | trans_begun: |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2044 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2045 | sqlite3BtreeLeave(p); |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 2046 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2047 | } |
| 2048 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2049 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2050 | |
| 2051 | /* |
| 2052 | ** Set the pointer-map entries for all children of page pPage. Also, if |
| 2053 | ** pPage contains cells that point to overflow pages, set the pointer |
| 2054 | ** map entries for the overflow pages as well. |
| 2055 | */ |
| 2056 | static int setChildPtrmaps(MemPage *pPage){ |
| 2057 | int i; /* Counter variable */ |
| 2058 | int nCell; /* Number of cells in page pPage */ |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 2059 | int rc; /* Return code */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2060 | BtShared *pBt = pPage->pBt; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2061 | int isInitOrig = pPage->isInit; |
| 2062 | Pgno pgno = pPage->pgno; |
| 2063 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2064 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2065 | rc = sqlite3BtreeInitPage(pPage); |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 2066 | if( rc!=SQLITE_OK ){ |
| 2067 | goto set_child_ptrmaps_out; |
| 2068 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2069 | nCell = pPage->nCell; |
| 2070 | |
| 2071 | for(i=0; i<nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2072 | u8 *pCell = findCell(pPage, i); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2073 | |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 2074 | rc = ptrmapPutOvflPtr(pPage, pCell); |
| 2075 | if( rc!=SQLITE_OK ){ |
| 2076 | goto set_child_ptrmaps_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2077 | } |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 2078 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2079 | if( !pPage->leaf ){ |
| 2080 | Pgno childPgno = get4byte(pCell); |
| 2081 | rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno); |
danielk1977 | 00a696d | 2008-09-29 16:41:31 +0000 | [diff] [blame] | 2082 | if( rc!=SQLITE_OK ) goto set_child_ptrmaps_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2083 | } |
| 2084 | } |
| 2085 | |
| 2086 | if( !pPage->leaf ){ |
| 2087 | Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 2088 | rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno); |
| 2089 | } |
| 2090 | |
| 2091 | set_child_ptrmaps_out: |
| 2092 | pPage->isInit = isInitOrig; |
| 2093 | return rc; |
| 2094 | } |
| 2095 | |
| 2096 | /* |
| 2097 | ** Somewhere on pPage, which is guarenteed to be a btree page, not an overflow |
| 2098 | ** page, is a pointer to page iFrom. Modify this pointer so that it points to |
| 2099 | ** iTo. Parameter eType describes the type of pointer to be modified, as |
| 2100 | ** follows: |
| 2101 | ** |
| 2102 | ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child |
| 2103 | ** page of pPage. |
| 2104 | ** |
| 2105 | ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow |
| 2106 | ** page pointed to by one of the cells on pPage. |
| 2107 | ** |
| 2108 | ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next |
| 2109 | ** overflow page in the list. |
| 2110 | */ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2111 | static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2112 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2113 | if( eType==PTRMAP_OVERFLOW2 ){ |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 2114 | /* 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] | 2115 | if( get4byte(pPage->aData)!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2116 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2117 | } |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 2118 | put4byte(pPage->aData, iTo); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2119 | }else{ |
| 2120 | int isInitOrig = pPage->isInit; |
| 2121 | int i; |
| 2122 | int nCell; |
| 2123 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2124 | sqlite3BtreeInitPage(pPage); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2125 | nCell = pPage->nCell; |
| 2126 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2127 | for(i=0; i<nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2128 | u8 *pCell = findCell(pPage, i); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2129 | if( eType==PTRMAP_OVERFLOW1 ){ |
| 2130 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2131 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2132 | if( info.iOverflow ){ |
| 2133 | if( iFrom==get4byte(&pCell[info.iOverflow]) ){ |
| 2134 | put4byte(&pCell[info.iOverflow], iTo); |
| 2135 | break; |
| 2136 | } |
| 2137 | } |
| 2138 | }else{ |
| 2139 | if( get4byte(pCell)==iFrom ){ |
| 2140 | put4byte(pCell, iTo); |
| 2141 | break; |
| 2142 | } |
| 2143 | } |
| 2144 | } |
| 2145 | |
| 2146 | if( i==nCell ){ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2147 | if( eType!=PTRMAP_BTREE || |
| 2148 | get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2149 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2150 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2151 | put4byte(&pPage->aData[pPage->hdrOffset+8], iTo); |
| 2152 | } |
| 2153 | |
| 2154 | pPage->isInit = isInitOrig; |
| 2155 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2156 | return SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2157 | } |
| 2158 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2159 | |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2160 | /* |
| 2161 | ** Move the open database page pDbPage to location iFreePage in the |
| 2162 | ** database. The pDbPage reference remains valid. |
| 2163 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2164 | static int relocatePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2165 | BtShared *pBt, /* Btree */ |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2166 | MemPage *pDbPage, /* Open page to move */ |
| 2167 | u8 eType, /* Pointer map 'type' entry for pDbPage */ |
| 2168 | Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */ |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 2169 | Pgno iFreePage, /* The location to move pDbPage to */ |
| 2170 | int isCommit |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2171 | ){ |
| 2172 | MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */ |
| 2173 | Pgno iDbPage = pDbPage->pgno; |
| 2174 | Pager *pPager = pBt->pPager; |
| 2175 | int rc; |
| 2176 | |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2177 | assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || |
| 2178 | eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2179 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 2180 | assert( pDbPage->pBt==pBt ); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2181 | |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 2182 | /* Move page iDbPage from its current location to page number iFreePage */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2183 | TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", |
| 2184 | iDbPage, iFreePage, iPtrPage, eType)); |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 2185 | rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2186 | if( rc!=SQLITE_OK ){ |
| 2187 | return rc; |
| 2188 | } |
| 2189 | pDbPage->pgno = iFreePage; |
| 2190 | |
| 2191 | /* If pDbPage was a btree-page, then it may have child pages and/or cells |
| 2192 | ** that point to overflow pages. The pointer map entries for all these |
| 2193 | ** pages need to be changed. |
| 2194 | ** |
| 2195 | ** If pDbPage is an overflow page, then the first 4 bytes may store a |
| 2196 | ** pointer to a subsequent overflow page. If this is the case, then |
| 2197 | ** the pointer map needs to be updated for the subsequent overflow page. |
| 2198 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2199 | if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2200 | rc = setChildPtrmaps(pDbPage); |
| 2201 | if( rc!=SQLITE_OK ){ |
| 2202 | return rc; |
| 2203 | } |
| 2204 | }else{ |
| 2205 | Pgno nextOvfl = get4byte(pDbPage->aData); |
| 2206 | if( nextOvfl!=0 ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2207 | rc = ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage); |
| 2208 | if( rc!=SQLITE_OK ){ |
| 2209 | return rc; |
| 2210 | } |
| 2211 | } |
| 2212 | } |
| 2213 | |
| 2214 | /* Fix the database pointer on page iPtrPage that pointed at iDbPage so |
| 2215 | ** that it points at iFreePage. Also fix the pointer map entry for |
| 2216 | ** iPtrPage. |
| 2217 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2218 | if( eType!=PTRMAP_ROOTPAGE ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2219 | rc = sqlite3BtreeGetPage(pBt, iPtrPage, &pPtrPage, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2220 | if( rc!=SQLITE_OK ){ |
| 2221 | return rc; |
| 2222 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2223 | rc = sqlite3PagerWrite(pPtrPage->pDbPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2224 | if( rc!=SQLITE_OK ){ |
| 2225 | releasePage(pPtrPage); |
| 2226 | return rc; |
| 2227 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2228 | rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2229 | releasePage(pPtrPage); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2230 | if( rc==SQLITE_OK ){ |
| 2231 | rc = ptrmapPut(pBt, iFreePage, eType, iPtrPage); |
| 2232 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2233 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2234 | return rc; |
| 2235 | } |
| 2236 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2237 | /* Forward declaration required by incrVacuumStep(). */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2238 | static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2239 | |
| 2240 | /* |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2241 | ** Perform a single step of an incremental-vacuum. If successful, |
| 2242 | ** return SQLITE_OK. If there is no work to do (and therefore no |
| 2243 | ** point in calling this function again), return SQLITE_DONE. |
| 2244 | ** |
| 2245 | ** More specificly, this function attempts to re-organize the |
| 2246 | ** database so that the last page of the file currently in use |
| 2247 | ** is no longer in use. |
| 2248 | ** |
| 2249 | ** If the nFin parameter is non-zero, the implementation assumes |
| 2250 | ** that the caller will keep calling incrVacuumStep() until |
| 2251 | ** it returns SQLITE_DONE or an error, and that nFin is the |
| 2252 | ** number of pages the database file will contain after this |
| 2253 | ** process is complete. |
| 2254 | */ |
| 2255 | static int incrVacuumStep(BtShared *pBt, Pgno nFin){ |
| 2256 | Pgno iLastPg; /* Last page in the database */ |
| 2257 | Pgno nFreeList; /* Number of pages still on the free-list */ |
| 2258 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2259 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2260 | iLastPg = pBt->nTrunc; |
| 2261 | if( iLastPg==0 ){ |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2262 | iLastPg = pagerPagecount(pBt->pPager); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2263 | } |
| 2264 | |
| 2265 | if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){ |
| 2266 | int rc; |
| 2267 | u8 eType; |
| 2268 | Pgno iPtrPage; |
| 2269 | |
| 2270 | nFreeList = get4byte(&pBt->pPage1->aData[36]); |
| 2271 | if( nFreeList==0 || nFin==iLastPg ){ |
| 2272 | return SQLITE_DONE; |
| 2273 | } |
| 2274 | |
| 2275 | rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage); |
| 2276 | if( rc!=SQLITE_OK ){ |
| 2277 | return rc; |
| 2278 | } |
| 2279 | if( eType==PTRMAP_ROOTPAGE ){ |
| 2280 | return SQLITE_CORRUPT_BKPT; |
| 2281 | } |
| 2282 | |
| 2283 | if( eType==PTRMAP_FREEPAGE ){ |
| 2284 | if( nFin==0 ){ |
| 2285 | /* Remove the page from the files free-list. This is not required |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 2286 | ** if nFin is non-zero. In that case, the free-list will be |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2287 | ** truncated to zero after this function returns, so it doesn't |
| 2288 | ** matter if it still contains some garbage entries. |
| 2289 | */ |
| 2290 | Pgno iFreePg; |
| 2291 | MemPage *pFreePg; |
| 2292 | rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1); |
| 2293 | if( rc!=SQLITE_OK ){ |
| 2294 | return rc; |
| 2295 | } |
| 2296 | assert( iFreePg==iLastPg ); |
| 2297 | releasePage(pFreePg); |
| 2298 | } |
| 2299 | } else { |
| 2300 | Pgno iFreePg; /* Index of free page to move pLastPg to */ |
| 2301 | MemPage *pLastPg; |
| 2302 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2303 | rc = sqlite3BtreeGetPage(pBt, iLastPg, &pLastPg, 0); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2304 | if( rc!=SQLITE_OK ){ |
| 2305 | return rc; |
| 2306 | } |
| 2307 | |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 2308 | /* If nFin is zero, this loop runs exactly once and page pLastPg |
| 2309 | ** is swapped with the first free page pulled off the free list. |
| 2310 | ** |
| 2311 | ** On the other hand, if nFin is greater than zero, then keep |
| 2312 | ** looping until a free-page located within the first nFin pages |
| 2313 | ** of the file is found. |
| 2314 | */ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2315 | do { |
| 2316 | MemPage *pFreePg; |
| 2317 | rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0); |
| 2318 | if( rc!=SQLITE_OK ){ |
| 2319 | releasePage(pLastPg); |
| 2320 | return rc; |
| 2321 | } |
| 2322 | releasePage(pFreePg); |
| 2323 | }while( nFin!=0 && iFreePg>nFin ); |
| 2324 | assert( iFreePg<iLastPg ); |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 2325 | |
| 2326 | rc = sqlite3PagerWrite(pLastPg->pDbPage); |
danielk1977 | 662278e | 2007-11-05 15:30:12 +0000 | [diff] [blame] | 2327 | if( rc==SQLITE_OK ){ |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 2328 | rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, nFin!=0); |
danielk1977 | 662278e | 2007-11-05 15:30:12 +0000 | [diff] [blame] | 2329 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2330 | releasePage(pLastPg); |
| 2331 | if( rc!=SQLITE_OK ){ |
| 2332 | return rc; |
danielk1977 | 662278e | 2007-11-05 15:30:12 +0000 | [diff] [blame] | 2333 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2334 | } |
| 2335 | } |
| 2336 | |
| 2337 | pBt->nTrunc = iLastPg - 1; |
| 2338 | while( pBt->nTrunc==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, pBt->nTrunc) ){ |
| 2339 | pBt->nTrunc--; |
| 2340 | } |
| 2341 | return SQLITE_OK; |
| 2342 | } |
| 2343 | |
| 2344 | /* |
| 2345 | ** A write-transaction must be opened before calling this function. |
| 2346 | ** It performs a single unit of work towards an incremental vacuum. |
| 2347 | ** |
| 2348 | ** If the incremental vacuum is finished after this function has run, |
| 2349 | ** SQLITE_DONE is returned. If it is not finished, but no error occured, |
| 2350 | ** SQLITE_OK is returned. Otherwise an SQLite error code. |
| 2351 | */ |
| 2352 | int sqlite3BtreeIncrVacuum(Btree *p){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2353 | int rc; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2354 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2355 | |
| 2356 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2357 | pBt->db = p->db; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2358 | assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE ); |
| 2359 | if( !pBt->autoVacuum ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2360 | rc = SQLITE_DONE; |
| 2361 | }else{ |
| 2362 | invalidateAllOverflowCache(pBt); |
| 2363 | rc = incrVacuumStep(pBt, 0); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2364 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2365 | sqlite3BtreeLeave(p); |
| 2366 | return rc; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2367 | } |
| 2368 | |
| 2369 | /* |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2370 | ** This routine is called prior to sqlite3PagerCommit when a transaction |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2371 | ** is commited for an auto-vacuum database. |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 2372 | ** |
| 2373 | ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages |
| 2374 | ** the database file should be truncated to during the commit process. |
| 2375 | ** i.e. the database has been reorganized so that only the first *pnTrunc |
| 2376 | ** pages are in use. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2377 | */ |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 2378 | static int autoVacuumCommit(BtShared *pBt, Pgno *pnTrunc){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2379 | int rc = SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2380 | Pager *pPager = pBt->pPager; |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 2381 | VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2382 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2383 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 2384 | invalidateAllOverflowCache(pBt); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2385 | assert(pBt->autoVacuum); |
| 2386 | if( !pBt->incrVacuum ){ |
| 2387 | Pgno nFin = 0; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2388 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2389 | if( pBt->nTrunc==0 ){ |
| 2390 | Pgno nFree; |
| 2391 | Pgno nPtrmap; |
| 2392 | const int pgsz = pBt->pageSize; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2393 | int nOrig = pagerPagecount(pBt->pPager); |
danielk1977 | e5321f0 | 2007-04-27 07:05:44 +0000 | [diff] [blame] | 2394 | |
| 2395 | if( PTRMAP_ISPAGE(pBt, nOrig) ){ |
| 2396 | return SQLITE_CORRUPT_BKPT; |
| 2397 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2398 | if( nOrig==PENDING_BYTE_PAGE(pBt) ){ |
| 2399 | nOrig--; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2400 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2401 | nFree = get4byte(&pBt->pPage1->aData[36]); |
| 2402 | nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+pgsz/5)/(pgsz/5); |
| 2403 | nFin = nOrig - nFree - nPtrmap; |
| 2404 | if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<=PENDING_BYTE_PAGE(pBt) ){ |
| 2405 | nFin--; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 2406 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2407 | while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){ |
| 2408 | nFin--; |
| 2409 | } |
| 2410 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2411 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2412 | while( rc==SQLITE_OK ){ |
| 2413 | rc = incrVacuumStep(pBt, nFin); |
| 2414 | } |
| 2415 | if( rc==SQLITE_DONE ){ |
| 2416 | assert(nFin==0 || pBt->nTrunc==0 || nFin<=pBt->nTrunc); |
| 2417 | rc = SQLITE_OK; |
danielk1977 | 0ba32df | 2008-05-07 07:13:16 +0000 | [diff] [blame] | 2418 | if( pBt->nTrunc && nFin ){ |
drh | 67f80b6 | 2007-07-23 19:26:17 +0000 | [diff] [blame] | 2419 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2420 | put4byte(&pBt->pPage1->aData[32], 0); |
| 2421 | put4byte(&pBt->pPage1->aData[36], 0); |
| 2422 | pBt->nTrunc = nFin; |
| 2423 | } |
| 2424 | } |
| 2425 | if( rc!=SQLITE_OK ){ |
| 2426 | sqlite3PagerRollback(pPager); |
| 2427 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2428 | } |
| 2429 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2430 | if( rc==SQLITE_OK ){ |
| 2431 | *pnTrunc = pBt->nTrunc; |
| 2432 | pBt->nTrunc = 0; |
| 2433 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2434 | assert( nRef==sqlite3PagerRefcount(pPager) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2435 | return rc; |
| 2436 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2437 | |
shane | 831c329 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 2438 | #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2439 | |
| 2440 | /* |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2441 | ** This routine does the first phase of a two-phase commit. This routine |
| 2442 | ** causes a rollback journal to be created (if it does not already exist) |
| 2443 | ** and populated with enough information so that if a power loss occurs |
| 2444 | ** the database can be restored to its original state by playing back |
| 2445 | ** the journal. Then the contents of the journal are flushed out to |
| 2446 | ** the disk. After the journal is safely on oxide, the changes to the |
| 2447 | ** database are written into the database file and flushed to oxide. |
| 2448 | ** At the end of this call, the rollback journal still exists on the |
| 2449 | ** disk and we are still holding all locks, so the transaction has not |
| 2450 | ** committed. See sqlite3BtreeCommit() for the second phase of the |
| 2451 | ** commit process. |
| 2452 | ** |
| 2453 | ** This call is a no-op if no write-transaction is currently active on pBt. |
| 2454 | ** |
| 2455 | ** Otherwise, sync the database file for the btree pBt. zMaster points to |
| 2456 | ** the name of a master journal file that should be written into the |
| 2457 | ** individual journal file, or is NULL, indicating no master journal file |
| 2458 | ** (single database transaction). |
| 2459 | ** |
| 2460 | ** When this is called, the master journal should already have been |
| 2461 | ** created, populated with this journal pointer and synced to disk. |
| 2462 | ** |
| 2463 | ** Once this is routine has returned, the only thing required to commit |
| 2464 | ** the write-transaction for this database file is to delete the journal. |
| 2465 | */ |
| 2466 | int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){ |
| 2467 | int rc = SQLITE_OK; |
| 2468 | if( p->inTrans==TRANS_WRITE ){ |
| 2469 | BtShared *pBt = p->pBt; |
| 2470 | Pgno nTrunc = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2471 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2472 | pBt->db = p->db; |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2473 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2474 | if( pBt->autoVacuum ){ |
| 2475 | rc = autoVacuumCommit(pBt, &nTrunc); |
| 2476 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2477 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2478 | return rc; |
| 2479 | } |
| 2480 | } |
| 2481 | #endif |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 2482 | rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, nTrunc, 0); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2483 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2484 | } |
| 2485 | return rc; |
| 2486 | } |
| 2487 | |
| 2488 | /* |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2489 | ** Commit the transaction currently in progress. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2490 | ** |
drh | 6e34599 | 2007-03-30 11:12:08 +0000 | [diff] [blame] | 2491 | ** This routine implements the second phase of a 2-phase commit. The |
| 2492 | ** sqlite3BtreeSync() routine does the first phase and should be invoked |
| 2493 | ** prior to calling this routine. The sqlite3BtreeSync() routine did |
| 2494 | ** all the work of writing information out to disk and flushing the |
| 2495 | ** contents so that they are written onto the disk platter. All this |
| 2496 | ** routine has to do is delete or truncate the rollback journal |
| 2497 | ** (which causes the transaction to commit) and drop locks. |
| 2498 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2499 | ** This will release the write lock on the database file. If there |
| 2500 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2501 | */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2502 | int sqlite3BtreeCommitPhaseTwo(Btree *p){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2503 | BtShared *pBt = p->pBt; |
| 2504 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2505 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2506 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2507 | btreeIntegrity(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2508 | |
| 2509 | /* If the handle has a write-transaction open, commit the shared-btrees |
| 2510 | ** transaction and set the shared state to TRANS_READ. |
| 2511 | */ |
| 2512 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2513 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2514 | assert( pBt->inTransaction==TRANS_WRITE ); |
| 2515 | assert( pBt->nTransaction>0 ); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2516 | rc = sqlite3PagerCommitPhaseTwo(pBt->pPager); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2517 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2518 | sqlite3BtreeLeave(p); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2519 | return rc; |
| 2520 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2521 | pBt->inTransaction = TRANS_READ; |
| 2522 | pBt->inStmt = 0; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2523 | } |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2524 | unlockAllTables(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2525 | |
| 2526 | /* If the handle has any kind of transaction open, decrement the transaction |
| 2527 | ** count of the shared btree. If the transaction count reaches 0, set |
| 2528 | ** the shared state to TRANS_NONE. The unlockBtreeIfUnused() call below |
| 2529 | ** will unlock the pager. |
| 2530 | */ |
| 2531 | if( p->inTrans!=TRANS_NONE ){ |
| 2532 | pBt->nTransaction--; |
| 2533 | if( 0==pBt->nTransaction ){ |
| 2534 | pBt->inTransaction = TRANS_NONE; |
| 2535 | } |
| 2536 | } |
| 2537 | |
| 2538 | /* Set the handles current transaction state to TRANS_NONE and unlock |
| 2539 | ** the pager if this call closed the only read or write transaction. |
| 2540 | */ |
| 2541 | p->inTrans = TRANS_NONE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2542 | unlockBtreeIfUnused(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2543 | |
| 2544 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2545 | sqlite3BtreeLeave(p); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2546 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2547 | } |
| 2548 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2549 | /* |
| 2550 | ** Do both phases of a commit. |
| 2551 | */ |
| 2552 | int sqlite3BtreeCommit(Btree *p){ |
| 2553 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2554 | sqlite3BtreeEnter(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2555 | rc = sqlite3BtreeCommitPhaseOne(p, 0); |
| 2556 | if( rc==SQLITE_OK ){ |
| 2557 | rc = sqlite3BtreeCommitPhaseTwo(p); |
| 2558 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2559 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2560 | return rc; |
| 2561 | } |
| 2562 | |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2563 | #ifndef NDEBUG |
| 2564 | /* |
| 2565 | ** Return the number of write-cursors open on this handle. This is for use |
| 2566 | ** in assert() expressions, so it is only compiled if NDEBUG is not |
| 2567 | ** defined. |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2568 | ** |
| 2569 | ** For the purposes of this routine, a write-cursor is any cursor that |
| 2570 | ** is capable of writing to the databse. That means the cursor was |
| 2571 | ** originally opened for writing and the cursor has not be disabled |
| 2572 | ** by having its state changed to CURSOR_FAULT. |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2573 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2574 | static int countWriteCursors(BtShared *pBt){ |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2575 | BtCursor *pCur; |
| 2576 | int r = 0; |
| 2577 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2578 | if( pCur->wrFlag && pCur->eState!=CURSOR_FAULT ) r++; |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2579 | } |
| 2580 | return r; |
| 2581 | } |
| 2582 | #endif |
| 2583 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2584 | /* |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2585 | ** This routine sets the state to CURSOR_FAULT and the error |
| 2586 | ** code to errCode for every cursor on BtShared that pBtree |
| 2587 | ** references. |
| 2588 | ** |
| 2589 | ** Every cursor is tripped, including cursors that belong |
| 2590 | ** to other database connections that happen to be sharing |
| 2591 | ** the cache with pBtree. |
| 2592 | ** |
| 2593 | ** This routine gets called when a rollback occurs. |
| 2594 | ** All cursors using the same cache must be tripped |
| 2595 | ** to prevent them from trying to use the btree after |
| 2596 | ** the rollback. The rollback may have deleted tables |
| 2597 | ** or moved root pages, so it is not sufficient to |
| 2598 | ** save the state of the cursor. The cursor must be |
| 2599 | ** invalidated. |
| 2600 | */ |
| 2601 | void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){ |
| 2602 | BtCursor *p; |
| 2603 | sqlite3BtreeEnter(pBtree); |
| 2604 | for(p=pBtree->pBt->pCursor; p; p=p->pNext){ |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 2605 | sqlite3BtreeClearCursor(p); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2606 | p->eState = CURSOR_FAULT; |
| 2607 | p->skip = errCode; |
| 2608 | } |
| 2609 | sqlite3BtreeLeave(pBtree); |
| 2610 | } |
| 2611 | |
| 2612 | /* |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2613 | ** Rollback the transaction in progress. All cursors will be |
| 2614 | ** invalided by this operation. Any attempt to use a cursor |
| 2615 | ** that was open at the beginning of this operation will result |
| 2616 | ** in an error. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2617 | ** |
| 2618 | ** This will release the write lock on the database file. If there |
| 2619 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2620 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2621 | int sqlite3BtreeRollback(Btree *p){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2622 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2623 | BtShared *pBt = p->pBt; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2624 | MemPage *pPage1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2625 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2626 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2627 | pBt->db = p->db; |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2628 | rc = saveAllCursors(pBt, 0, 0); |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2629 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2630 | if( rc!=SQLITE_OK ){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2631 | /* This is a horrible situation. An IO or malloc() error occured whilst |
| 2632 | ** trying to save cursor positions. If this is an automatic rollback (as |
| 2633 | ** the result of a constraint, malloc() failure or IO error) then |
| 2634 | ** the cache may be internally inconsistent (not contain valid trees) so |
| 2635 | ** we cannot simply return the error to the caller. Instead, abort |
| 2636 | ** all queries that may be using any of the cursors that failed to save. |
| 2637 | */ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2638 | sqlite3BtreeTripAllCursors(p, rc); |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2639 | } |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2640 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2641 | btreeIntegrity(p); |
| 2642 | unlockAllTables(p); |
| 2643 | |
| 2644 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2645 | int rc2; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2646 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2647 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2648 | pBt->nTrunc = 0; |
| 2649 | #endif |
| 2650 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2651 | assert( TRANS_WRITE==pBt->inTransaction ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2652 | rc2 = sqlite3PagerRollback(pBt->pPager); |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2653 | if( rc2!=SQLITE_OK ){ |
| 2654 | rc = rc2; |
| 2655 | } |
| 2656 | |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2657 | /* The rollback may have destroyed the pPage1->aData value. So |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2658 | ** call sqlite3BtreeGetPage() on page 1 again to make |
| 2659 | ** sure pPage1->aData is set correctly. */ |
| 2660 | if( sqlite3BtreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2661 | releasePage(pPage1); |
| 2662 | } |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2663 | assert( countWriteCursors(pBt)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2664 | pBt->inTransaction = TRANS_READ; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2665 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2666 | |
| 2667 | if( p->inTrans!=TRANS_NONE ){ |
| 2668 | assert( pBt->nTransaction>0 ); |
| 2669 | pBt->nTransaction--; |
| 2670 | if( 0==pBt->nTransaction ){ |
| 2671 | pBt->inTransaction = TRANS_NONE; |
| 2672 | } |
| 2673 | } |
| 2674 | |
| 2675 | p->inTrans = TRANS_NONE; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2676 | pBt->inStmt = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2677 | unlockBtreeIfUnused(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2678 | |
| 2679 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2680 | sqlite3BtreeLeave(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2681 | return rc; |
| 2682 | } |
| 2683 | |
| 2684 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2685 | ** Start a statement subtransaction. The subtransaction can |
| 2686 | ** can be rolled back independently of the main transaction. |
| 2687 | ** You must start a transaction before starting a subtransaction. |
| 2688 | ** The subtransaction is ended automatically if the main transaction |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2689 | ** commits or rolls back. |
| 2690 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2691 | ** Only one subtransaction may be active at a time. It is an error to try |
| 2692 | ** to start a new subtransaction if another subtransaction is already active. |
| 2693 | ** |
| 2694 | ** Statement subtransactions are used around individual SQL statements |
| 2695 | ** that are contained within a BEGIN...COMMIT block. If a constraint |
| 2696 | ** error occurs within the statement, the effect of that one statement |
| 2697 | ** can be rolled back without having to rollback the entire transaction. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2698 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2699 | int sqlite3BtreeBeginStmt(Btree *p){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2700 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2701 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2702 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2703 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2704 | if( (p->inTrans!=TRANS_WRITE) || pBt->inStmt ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2705 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
| 2706 | }else{ |
| 2707 | assert( pBt->inTransaction==TRANS_WRITE ); |
| 2708 | rc = pBt->readOnly ? SQLITE_OK : sqlite3PagerStmtBegin(pBt->pPager); |
| 2709 | pBt->inStmt = 1; |
drh | 0d65dc0 | 2002-02-03 00:56:09 +0000 | [diff] [blame] | 2710 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2711 | sqlite3BtreeLeave(p); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2712 | return rc; |
| 2713 | } |
| 2714 | |
| 2715 | |
| 2716 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2717 | ** Commit the statment subtransaction currently in progress. If no |
| 2718 | ** subtransaction is active, this is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2719 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2720 | int sqlite3BtreeCommitStmt(Btree *p){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2721 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2722 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2723 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2724 | pBt->db = p->db; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2725 | if( pBt->inStmt && !pBt->readOnly ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2726 | rc = sqlite3PagerStmtCommit(pBt->pPager); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2727 | }else{ |
| 2728 | rc = SQLITE_OK; |
| 2729 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2730 | pBt->inStmt = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2731 | sqlite3BtreeLeave(p); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2732 | return rc; |
| 2733 | } |
| 2734 | |
| 2735 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2736 | ** Rollback the active statement subtransaction. If no subtransaction |
| 2737 | ** is active this routine is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2738 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2739 | ** All cursors will be invalidated by this operation. Any attempt |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2740 | ** to use a cursor that was open at the beginning of this operation |
| 2741 | ** will result in an error. |
| 2742 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2743 | int sqlite3BtreeRollbackStmt(Btree *p){ |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2744 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2745 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2746 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2747 | pBt->db = p->db; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2748 | if( pBt->inStmt && !pBt->readOnly ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2749 | rc = sqlite3PagerStmtRollback(pBt->pPager); |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2750 | pBt->inStmt = 0; |
| 2751 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2752 | sqlite3BtreeLeave(p); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2753 | return rc; |
| 2754 | } |
| 2755 | |
| 2756 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2757 | ** Create a new cursor for the BTree whose root is on the page |
| 2758 | ** iTable. The act of acquiring a cursor gets a read lock on |
| 2759 | ** the database file. |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 2760 | ** |
| 2761 | ** If wrFlag==0, then the cursor can only be used for reading. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2762 | ** If wrFlag==1, then the cursor can be used for reading or for |
| 2763 | ** writing if other conditions for writing are also met. These |
| 2764 | ** are the conditions that must be met in order for writing to |
| 2765 | ** be allowed: |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2766 | ** |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2767 | ** 1: The cursor must have been opened with wrFlag==1 |
| 2768 | ** |
drh | fe5d71d | 2007-03-19 11:54:10 +0000 | [diff] [blame] | 2769 | ** 2: Other database connections that share the same pager cache |
| 2770 | ** but which are not in the READ_UNCOMMITTED state may not have |
| 2771 | ** cursors open with wrFlag==0 on the same table. Otherwise |
| 2772 | ** the changes made by this write cursor would be visible to |
| 2773 | ** the read cursors in the other database connection. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2774 | ** |
| 2775 | ** 3: The database must be writable (not on read-only media) |
| 2776 | ** |
| 2777 | ** 4: There must be an active transaction. |
| 2778 | ** |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2779 | ** No checking is done to make sure that page iTable really is the |
| 2780 | ** root page of a b-tree. If it is not, then the cursor acquired |
| 2781 | ** will not work correctly. |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2782 | ** |
| 2783 | ** It is assumed that the sqlite3BtreeCursorSize() bytes of memory |
| 2784 | ** pointed to by pCur have been zeroed by the caller. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2785 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2786 | static int btreeCursor( |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2787 | Btree *p, /* The btree */ |
| 2788 | int iTable, /* Root page of table to open */ |
| 2789 | int wrFlag, /* 1 to write. 0 read-only */ |
| 2790 | struct KeyInfo *pKeyInfo, /* First arg to comparison function */ |
| 2791 | BtCursor *pCur /* Space for new cursor */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2792 | ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2793 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2794 | BtShared *pBt = p->pBt; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2795 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2796 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2797 | if( wrFlag ){ |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2798 | if( pBt->readOnly ){ |
| 2799 | return SQLITE_READONLY; |
| 2800 | } |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 2801 | if( checkReadLocks(p, iTable, 0, 0) ){ |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2802 | return SQLITE_LOCKED; |
| 2803 | } |
drh | a0c9a11 | 2004-03-10 13:42:37 +0000 | [diff] [blame] | 2804 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2805 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2806 | if( pBt->pPage1==0 ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2807 | rc = lockBtreeWithRetry(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2808 | if( rc!=SQLITE_OK ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2809 | return rc; |
| 2810 | } |
drh | 1831f18 | 2007-04-24 17:35:59 +0000 | [diff] [blame] | 2811 | if( pBt->readOnly && wrFlag ){ |
| 2812 | return SQLITE_READONLY; |
| 2813 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2814 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2815 | pCur->pgnoRoot = (Pgno)iTable; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2816 | if( iTable==1 && pagerPagecount(pBt->pPager)==0 ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2817 | rc = SQLITE_EMPTY; |
| 2818 | goto create_cursor_exception; |
| 2819 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2820 | rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2821 | if( rc!=SQLITE_OK ){ |
| 2822 | goto create_cursor_exception; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2823 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2824 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2825 | /* Now that no other errors can occur, finish filling in the BtCursor |
| 2826 | ** variables, link the cursor into the BtShared list and set *ppCur (the |
| 2827 | ** output argument to this function). |
| 2828 | */ |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2829 | pCur->pKeyInfo = pKeyInfo; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2830 | pCur->pBtree = p; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 2831 | pCur->pBt = pBt; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2832 | pCur->wrFlag = wrFlag; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2833 | pCur->pNext = pBt->pCursor; |
| 2834 | if( pCur->pNext ){ |
| 2835 | pCur->pNext->pPrev = pCur; |
| 2836 | } |
| 2837 | pBt->pCursor = pCur; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2838 | pCur->eState = CURSOR_INVALID; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2839 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2840 | return SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2841 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2842 | create_cursor_exception: |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2843 | releasePage(pCur->apPage[0]); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2844 | unlockBtreeIfUnused(pBt); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2845 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2846 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2847 | int sqlite3BtreeCursor( |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2848 | Btree *p, /* The btree */ |
| 2849 | int iTable, /* Root page of table to open */ |
| 2850 | int wrFlag, /* 1 to write. 0 read-only */ |
| 2851 | struct KeyInfo *pKeyInfo, /* First arg to xCompare() */ |
| 2852 | BtCursor *pCur /* Write new cursor here */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2853 | ){ |
| 2854 | int rc; |
| 2855 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2856 | p->pBt->db = p->db; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2857 | rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2858 | sqlite3BtreeLeave(p); |
| 2859 | return rc; |
| 2860 | } |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2861 | int sqlite3BtreeCursorSize(){ |
| 2862 | return sizeof(BtCursor); |
| 2863 | } |
| 2864 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2865 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2866 | |
| 2867 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2868 | ** Close a cursor. The read lock on the database file is released |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2869 | ** when the last cursor is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2870 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2871 | int sqlite3BtreeCloseCursor(BtCursor *pCur){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 2872 | Btree *pBtree = pCur->pBtree; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2873 | if( pBtree ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2874 | int i; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2875 | BtShared *pBt = pCur->pBt; |
| 2876 | sqlite3BtreeEnter(pBtree); |
| 2877 | pBt->db = pBtree->db; |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 2878 | sqlite3BtreeClearCursor(pCur); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2879 | if( pCur->pPrev ){ |
| 2880 | pCur->pPrev->pNext = pCur->pNext; |
| 2881 | }else{ |
| 2882 | pBt->pCursor = pCur->pNext; |
| 2883 | } |
| 2884 | if( pCur->pNext ){ |
| 2885 | pCur->pNext->pPrev = pCur->pPrev; |
| 2886 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2887 | for(i=0; i<=pCur->iPage; i++){ |
| 2888 | releasePage(pCur->apPage[i]); |
| 2889 | } |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2890 | unlockBtreeIfUnused(pBt); |
| 2891 | invalidateOverflowCache(pCur); |
| 2892 | /* sqlite3_free(pCur); */ |
| 2893 | sqlite3BtreeLeave(pBtree); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2894 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2895 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2896 | } |
| 2897 | |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2898 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2899 | ** Make a temporary cursor by filling in the fields of pTempCur. |
| 2900 | ** The temporary cursor is not on the cursor list for the Btree. |
| 2901 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2902 | void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2903 | int i; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2904 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2905 | memcpy(pTempCur, pCur, sizeof(BtCursor)); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2906 | pTempCur->pNext = 0; |
| 2907 | pTempCur->pPrev = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2908 | for(i=0; i<=pTempCur->iPage; i++){ |
| 2909 | sqlite3PagerRef(pTempCur->apPage[i]->pDbPage); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2910 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2911 | } |
| 2912 | |
| 2913 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2914 | ** Delete a temporary cursor such as was made by the CreateTemporaryCursor() |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2915 | ** function above. |
| 2916 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2917 | void sqlite3BtreeReleaseTempCursor(BtCursor *pCur){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2918 | int i; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2919 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2920 | for(i=0; i<=pCur->iPage; i++){ |
| 2921 | sqlite3PagerUnref(pCur->apPage[i]->pDbPage); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2922 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2923 | } |
| 2924 | |
| 2925 | /* |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2926 | ** Make sure the BtCursor* given in the argument has a valid |
| 2927 | ** BtCursor.info structure. If it is not already valid, call |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2928 | ** sqlite3BtreeParseCell() to fill it in. |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2929 | ** |
| 2930 | ** BtCursor.info is a cache of the information in the current cell. |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2931 | ** Using this cache reduces the number of calls to sqlite3BtreeParseCell(). |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2932 | ** |
| 2933 | ** 2007-06-25: There is a bug in some versions of MSVC that cause the |
| 2934 | ** compiler to crash when getCellInfo() is implemented as a macro. |
| 2935 | ** But there is a measureable speed advantage to using the macro on gcc |
| 2936 | ** (when less compiler optimizations like -Os or -O0 are used and the |
| 2937 | ** compiler is not doing agressive inlining.) So we use a real function |
| 2938 | ** for MSVC and a macro for everything else. Ticket #2457. |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2939 | */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2940 | #ifndef NDEBUG |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2941 | static void assertCellInfo(BtCursor *pCur){ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2942 | CellInfo info; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2943 | int iPage = pCur->iPage; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 2944 | memset(&info, 0, sizeof(info)); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2945 | sqlite3BtreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2946 | assert( memcmp(&info, &pCur->info, sizeof(info))==0 ); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2947 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2948 | #else |
| 2949 | #define assertCellInfo(x) |
| 2950 | #endif |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2951 | #ifdef _MSC_VER |
| 2952 | /* Use a real function in MSVC to work around bugs in that compiler. */ |
| 2953 | static void getCellInfo(BtCursor *pCur){ |
| 2954 | if( pCur->info.nSize==0 ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2955 | int iPage = pCur->iPage; |
| 2956 | sqlite3BtreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 2957 | pCur->validNKey = 1; |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2958 | }else{ |
| 2959 | assertCellInfo(pCur); |
| 2960 | } |
| 2961 | } |
| 2962 | #else /* if not _MSC_VER */ |
| 2963 | /* Use a macro in all other compilers so that the function is inlined */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2964 | #define getCellInfo(pCur) \ |
| 2965 | if( pCur->info.nSize==0 ){ \ |
| 2966 | int iPage = pCur->iPage; \ |
| 2967 | sqlite3BtreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); \ |
| 2968 | pCur->validNKey = 1; \ |
| 2969 | }else{ \ |
| 2970 | assertCellInfo(pCur); \ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2971 | } |
| 2972 | #endif /* _MSC_VER */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2973 | |
| 2974 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2975 | ** Set *pSize to the size of the buffer needed to hold the value of |
| 2976 | ** the key for the current entry. If the cursor is not pointing |
| 2977 | ** to a valid entry, *pSize is set to 0. |
| 2978 | ** |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2979 | ** For a table with the INTKEY flag set, this routine returns the key |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2980 | ** itself, not the number of bytes in the key. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2981 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 2982 | int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2983 | int rc; |
| 2984 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2985 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 2986 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2987 | if( rc==SQLITE_OK ){ |
| 2988 | assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID ); |
| 2989 | if( pCur->eState==CURSOR_INVALID ){ |
| 2990 | *pSize = 0; |
| 2991 | }else{ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2992 | getCellInfo(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2993 | *pSize = pCur->info.nKey; |
| 2994 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2995 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2996 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2997 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2998 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2999 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3000 | ** Set *pSize to the number of bytes of data in the entry the |
| 3001 | ** cursor currently points to. Always return SQLITE_OK. |
| 3002 | ** Failure is not possible. If the cursor is not currently |
| 3003 | ** pointing to an entry (which can happen, for example, if |
| 3004 | ** the database is empty) then *pSize is set to 0. |
| 3005 | */ |
| 3006 | int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3007 | int rc; |
| 3008 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3009 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 3010 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3011 | if( rc==SQLITE_OK ){ |
| 3012 | assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID ); |
| 3013 | if( pCur->eState==CURSOR_INVALID ){ |
| 3014 | /* Not pointing at a valid entry - set *pSize to 0. */ |
| 3015 | *pSize = 0; |
| 3016 | }else{ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3017 | getCellInfo(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3018 | *pSize = pCur->info.nData; |
| 3019 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3020 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3021 | return rc; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3022 | } |
| 3023 | |
| 3024 | /* |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3025 | ** Given the page number of an overflow page in the database (parameter |
| 3026 | ** ovfl), this function finds the page number of the next page in the |
| 3027 | ** linked list of overflow pages. If possible, it uses the auto-vacuum |
| 3028 | ** pointer-map data instead of reading the content of page ovfl to do so. |
| 3029 | ** |
| 3030 | ** If an error occurs an SQLite error code is returned. Otherwise: |
| 3031 | ** |
| 3032 | ** Unless pPgnoNext is NULL, the page number of the next overflow |
| 3033 | ** page in the linked list is written to *pPgnoNext. If page ovfl |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 3034 | ** is the last page in its linked list, *pPgnoNext is set to zero. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3035 | ** |
| 3036 | ** If ppPage is not NULL, *ppPage is set to the MemPage* handle |
| 3037 | ** for page ovfl. The underlying pager page may have been requested |
| 3038 | ** with the noContent flag set, so the page data accessable via |
| 3039 | ** this handle may not be trusted. |
| 3040 | */ |
| 3041 | static int getOverflowPage( |
| 3042 | BtShared *pBt, |
| 3043 | Pgno ovfl, /* Overflow page */ |
| 3044 | MemPage **ppPage, /* OUT: MemPage handle */ |
| 3045 | Pgno *pPgnoNext /* OUT: Next overflow page number */ |
| 3046 | ){ |
| 3047 | Pgno next = 0; |
| 3048 | int rc; |
| 3049 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3050 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3051 | /* One of these must not be NULL. Otherwise, why call this function? */ |
| 3052 | assert(ppPage || pPgnoNext); |
| 3053 | |
| 3054 | /* If pPgnoNext is NULL, then this function is being called to obtain |
| 3055 | ** a MemPage* reference only. No page-data is required in this case. |
| 3056 | */ |
| 3057 | if( !pPgnoNext ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3058 | return sqlite3BtreeGetPage(pBt, ovfl, ppPage, 1); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3059 | } |
| 3060 | |
| 3061 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3062 | /* Try to find the next page in the overflow list using the |
| 3063 | ** autovacuum pointer-map pages. Guess that the next page in |
| 3064 | ** the overflow list is page number (ovfl+1). If that guess turns |
| 3065 | ** out to be wrong, fall back to loading the data of page |
| 3066 | ** number ovfl to determine the next page number. |
| 3067 | */ |
| 3068 | if( pBt->autoVacuum ){ |
| 3069 | Pgno pgno; |
| 3070 | Pgno iGuess = ovfl+1; |
| 3071 | u8 eType; |
| 3072 | |
| 3073 | while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){ |
| 3074 | iGuess++; |
| 3075 | } |
| 3076 | |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 3077 | if( iGuess<=pagerPagecount(pBt->pPager) ){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3078 | rc = ptrmapGet(pBt, iGuess, &eType, &pgno); |
| 3079 | if( rc!=SQLITE_OK ){ |
| 3080 | return rc; |
| 3081 | } |
| 3082 | if( eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){ |
| 3083 | next = iGuess; |
| 3084 | } |
| 3085 | } |
| 3086 | } |
| 3087 | #endif |
| 3088 | |
| 3089 | if( next==0 || ppPage ){ |
| 3090 | MemPage *pPage = 0; |
| 3091 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3092 | rc = sqlite3BtreeGetPage(pBt, ovfl, &pPage, next!=0); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3093 | assert(rc==SQLITE_OK || pPage==0); |
| 3094 | if( next==0 && rc==SQLITE_OK ){ |
| 3095 | next = get4byte(pPage->aData); |
| 3096 | } |
| 3097 | |
| 3098 | if( ppPage ){ |
| 3099 | *ppPage = pPage; |
| 3100 | }else{ |
| 3101 | releasePage(pPage); |
| 3102 | } |
| 3103 | } |
| 3104 | *pPgnoNext = next; |
| 3105 | |
| 3106 | return rc; |
| 3107 | } |
| 3108 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3109 | /* |
| 3110 | ** Copy data from a buffer to a page, or from a page to a buffer. |
| 3111 | ** |
| 3112 | ** pPayload is a pointer to data stored on database page pDbPage. |
| 3113 | ** If argument eOp is false, then nByte bytes of data are copied |
| 3114 | ** from pPayload to the buffer pointed at by pBuf. If eOp is true, |
| 3115 | ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes |
| 3116 | ** of data are copied from the buffer pBuf to pPayload. |
| 3117 | ** |
| 3118 | ** SQLITE_OK is returned on success, otherwise an error code. |
| 3119 | */ |
| 3120 | static int copyPayload( |
| 3121 | void *pPayload, /* Pointer to page data */ |
| 3122 | void *pBuf, /* Pointer to buffer */ |
| 3123 | int nByte, /* Number of bytes to copy */ |
| 3124 | int eOp, /* 0 -> copy from page, 1 -> copy to page */ |
| 3125 | DbPage *pDbPage /* Page containing pPayload */ |
| 3126 | ){ |
| 3127 | if( eOp ){ |
| 3128 | /* Copy data from buffer to page (a write operation) */ |
| 3129 | int rc = sqlite3PagerWrite(pDbPage); |
| 3130 | if( rc!=SQLITE_OK ){ |
| 3131 | return rc; |
| 3132 | } |
| 3133 | memcpy(pPayload, pBuf, nByte); |
| 3134 | }else{ |
| 3135 | /* Copy data from page to buffer (a read operation) */ |
| 3136 | memcpy(pBuf, pPayload, nByte); |
| 3137 | } |
| 3138 | return SQLITE_OK; |
| 3139 | } |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3140 | |
| 3141 | /* |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3142 | ** This function is used to read or overwrite payload information |
| 3143 | ** for the entry that the pCur cursor is pointing to. If the eOp |
| 3144 | ** parameter is 0, this is a read operation (data copied into |
| 3145 | ** buffer pBuf). If it is non-zero, a write (data copied from |
| 3146 | ** buffer pBuf). |
| 3147 | ** |
| 3148 | ** A total of "amt" bytes are read or written beginning at "offset". |
| 3149 | ** Data is read to or from the buffer pBuf. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3150 | ** |
| 3151 | ** This routine does not make a distinction between key and data. |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3152 | ** It just reads or writes bytes from the payload area. Data might |
| 3153 | ** appear on the main page or be scattered out on multiple overflow |
| 3154 | ** pages. |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3155 | ** |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 3156 | ** If the BtCursor.isIncrblobHandle flag is set, and the current |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3157 | ** cursor entry uses one or more overflow pages, this function |
| 3158 | ** allocates space for and lazily popluates the overflow page-list |
| 3159 | ** cache array (BtCursor.aOverflow). Subsequent calls use this |
| 3160 | ** cache to make seeking to the supplied offset more efficient. |
| 3161 | ** |
| 3162 | ** Once an overflow page-list cache has been allocated, it may be |
| 3163 | ** invalidated if some other cursor writes to the same table, or if |
| 3164 | ** the cursor is moved to a different row. Additionally, in auto-vacuum |
| 3165 | ** mode, the following events may invalidate an overflow page-list cache. |
| 3166 | ** |
| 3167 | ** * An incremental vacuum, |
| 3168 | ** * A commit in auto_vacuum="full" mode, |
| 3169 | ** * Creating a table (may require moving an overflow page). |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3170 | */ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3171 | static int accessPayload( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3172 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
| 3173 | int offset, /* Begin reading this far into payload */ |
| 3174 | int amt, /* Read this many bytes */ |
| 3175 | unsigned char *pBuf, /* Write the bytes into this buffer */ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3176 | int skipKey, /* offset begins at data if this is true */ |
| 3177 | int eOp /* zero to read. non-zero to write. */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3178 | ){ |
| 3179 | unsigned char *aPayload; |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3180 | int rc = SQLITE_OK; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3181 | u32 nKey; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3182 | int iIdx = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3183 | MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */ |
| 3184 | BtShared *pBt; /* Btree this cursor belongs to */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3185 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3186 | assert( pPage ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3187 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3188 | assert( pCur->aiIdx[pCur->iPage]<pPage->nCell ); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3189 | assert( offset>=0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3190 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3191 | |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3192 | getCellInfo(pCur); |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3193 | aPayload = pCur->info.pCell + pCur->info.nHeader; |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3194 | nKey = (pPage->intKey ? 0 : pCur->info.nKey); |
| 3195 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3196 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3197 | offset += nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3198 | } |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3199 | if( offset+amt > nKey+pCur->info.nData ){ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3200 | /* 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] | 3201 | return SQLITE_CORRUPT_BKPT; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3202 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3203 | |
| 3204 | /* Check if data must be read/written to/from the btree page itself. */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3205 | if( offset<pCur->info.nLocal ){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3206 | int a = amt; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3207 | if( a+offset>pCur->info.nLocal ){ |
| 3208 | a = pCur->info.nLocal - offset; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3209 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3210 | rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3211 | offset = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3212 | pBuf += a; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3213 | amt -= a; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 3214 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3215 | offset -= pCur->info.nLocal; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3216 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3217 | |
drh | 51f015e | 2007-10-16 19:45:29 +0000 | [diff] [blame] | 3218 | pBt = pCur->pBt; |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3219 | if( rc==SQLITE_OK && amt>0 ){ |
| 3220 | const int ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */ |
| 3221 | Pgno nextPage; |
| 3222 | |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3223 | nextPage = get4byte(&aPayload[pCur->info.nLocal]); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3224 | |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3225 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 3226 | /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[] |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3227 | ** has not been allocated, allocate it now. The array is sized at |
| 3228 | ** one entry for each overflow page in the overflow chain. The |
| 3229 | ** page number of the first overflow page is stored in aOverflow[0], |
| 3230 | ** etc. A value of 0 in the aOverflow[] array means "not yet known" |
| 3231 | ** (the cache is lazily populated). |
| 3232 | */ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 3233 | if( pCur->isIncrblobHandle && !pCur->aOverflow ){ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3234 | int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3235 | pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl); |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3236 | if( nOvfl && !pCur->aOverflow ){ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3237 | rc = SQLITE_NOMEM; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3238 | } |
| 3239 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3240 | |
| 3241 | /* If the overflow page-list cache has been allocated and the |
| 3242 | ** entry for the first required overflow page is valid, skip |
| 3243 | ** directly to it. |
| 3244 | */ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3245 | if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){ |
| 3246 | iIdx = (offset/ovflSize); |
| 3247 | nextPage = pCur->aOverflow[iIdx]; |
| 3248 | offset = (offset%ovflSize); |
| 3249 | } |
| 3250 | #endif |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3251 | |
| 3252 | for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){ |
| 3253 | |
| 3254 | #ifndef SQLITE_OMIT_INCRBLOB |
| 3255 | /* If required, populate the overflow page-list cache. */ |
| 3256 | if( pCur->aOverflow ){ |
| 3257 | assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage); |
| 3258 | pCur->aOverflow[iIdx] = nextPage; |
| 3259 | } |
| 3260 | #endif |
| 3261 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3262 | if( offset>=ovflSize ){ |
| 3263 | /* The only reason to read this page is to obtain the page |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3264 | ** number for the next page in the overflow chain. The page |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 3265 | ** data is not required. So first try to lookup the overflow |
| 3266 | ** page-list cache, if any, then fall back to the getOverflowPage() |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3267 | ** function. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3268 | */ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3269 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3270 | if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){ |
| 3271 | nextPage = pCur->aOverflow[iIdx+1]; |
| 3272 | } else |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3273 | #endif |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3274 | rc = getOverflowPage(pBt, nextPage, 0, &nextPage); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3275 | offset -= ovflSize; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3276 | }else{ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3277 | /* Need to read this page properly. It contains some of the |
| 3278 | ** range of data that is being read (eOp==0) or written (eOp!=0). |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3279 | */ |
| 3280 | DbPage *pDbPage; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3281 | int a = amt; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3282 | rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3283 | if( rc==SQLITE_OK ){ |
| 3284 | aPayload = sqlite3PagerGetData(pDbPage); |
| 3285 | nextPage = get4byte(aPayload); |
| 3286 | if( a + offset > ovflSize ){ |
| 3287 | a = ovflSize - offset; |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3288 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3289 | rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage); |
| 3290 | sqlite3PagerUnref(pDbPage); |
| 3291 | offset = 0; |
| 3292 | amt -= a; |
| 3293 | pBuf += a; |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3294 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3295 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3296 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3297 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3298 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3299 | if( rc==SQLITE_OK && amt>0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3300 | return SQLITE_CORRUPT_BKPT; |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 3301 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3302 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3303 | } |
| 3304 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3305 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3306 | ** Read part of the key associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3307 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3308 | ** begins at "offset". |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3309 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3310 | ** Return SQLITE_OK on success or an error code if anything goes |
| 3311 | ** wrong. An error is returned if "offset+amt" is larger than |
| 3312 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3313 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3314 | int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3315 | int rc; |
| 3316 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3317 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 3318 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3319 | if( rc==SQLITE_OK ){ |
| 3320 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3321 | assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] ); |
| 3322 | if( pCur->apPage[0]->intKey ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3323 | return SQLITE_CORRUPT_BKPT; |
| 3324 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3325 | assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3326 | rc = accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0, 0); |
drh | 6575a22 | 2005-03-10 17:06:34 +0000 | [diff] [blame] | 3327 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3328 | return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3329 | } |
| 3330 | |
| 3331 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3332 | ** Read part of the data associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3333 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3334 | ** begins at "offset". |
| 3335 | ** |
| 3336 | ** Return SQLITE_OK on success or an error code if anything goes |
| 3337 | ** wrong. An error is returned if "offset+amt" is larger than |
| 3338 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3339 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3340 | int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3341 | int rc; |
| 3342 | |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 3343 | #ifndef SQLITE_OMIT_INCRBLOB |
| 3344 | if ( pCur->eState==CURSOR_INVALID ){ |
| 3345 | return SQLITE_ABORT; |
| 3346 | } |
| 3347 | #endif |
| 3348 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3349 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 3350 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3351 | if( rc==SQLITE_OK ){ |
| 3352 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3353 | assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] ); |
| 3354 | assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3355 | rc = accessPayload(pCur, offset, amt, pBuf, 1, 0); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3356 | } |
| 3357 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3358 | } |
| 3359 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3360 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3361 | ** Return a pointer to payload information from the entry that the |
| 3362 | ** pCur cursor is pointing to. The pointer is to the beginning of |
| 3363 | ** 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] | 3364 | ** skipKey==1. The number of bytes of available key/data is written |
| 3365 | ** into *pAmt. If *pAmt==0, then the value returned will not be |
| 3366 | ** a valid pointer. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3367 | ** |
| 3368 | ** This routine is an optimization. It is common for the entire key |
| 3369 | ** and data to fit on the local page and for there to be no overflow |
| 3370 | ** pages. When that is so, this routine can be used to access the |
| 3371 | ** 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] | 3372 | ** onto overflow pages, then accessPayload() must be used to reassembly |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3373 | ** the key/data and copy it into a preallocated buffer. |
| 3374 | ** |
| 3375 | ** The pointer returned by this routine looks directly into the cached |
| 3376 | ** page of the database. The data might change or move the next time |
| 3377 | ** any btree routine is called. |
| 3378 | */ |
| 3379 | static const unsigned char *fetchPayload( |
| 3380 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3381 | int *pAmt, /* Write the number of available bytes here */ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3382 | int skipKey /* read beginning at data if this is true */ |
| 3383 | ){ |
| 3384 | unsigned char *aPayload; |
| 3385 | MemPage *pPage; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3386 | u32 nKey; |
| 3387 | int nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3388 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3389 | assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3390 | assert( pCur->eState==CURSOR_VALID ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3391 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3392 | pPage = pCur->apPage[pCur->iPage]; |
| 3393 | assert( pCur->aiIdx[pCur->iPage]<pPage->nCell ); |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3394 | getCellInfo(pCur); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3395 | aPayload = pCur->info.pCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3396 | aPayload += pCur->info.nHeader; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3397 | if( pPage->intKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3398 | nKey = 0; |
| 3399 | }else{ |
| 3400 | nKey = pCur->info.nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3401 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3402 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3403 | aPayload += nKey; |
| 3404 | nLocal = pCur->info.nLocal - nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3405 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3406 | nLocal = pCur->info.nLocal; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3407 | if( nLocal>nKey ){ |
| 3408 | nLocal = nKey; |
| 3409 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3410 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3411 | *pAmt = nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3412 | return aPayload; |
| 3413 | } |
| 3414 | |
| 3415 | |
| 3416 | /* |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3417 | ** For the entry that cursor pCur is point to, return as |
| 3418 | ** many bytes of the key or data as are available on the local |
| 3419 | ** b-tree page. Write the number of available bytes into *pAmt. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3420 | ** |
| 3421 | ** The pointer returned is ephemeral. The key/data may move |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3422 | ** or be destroyed on the next call to any Btree routine, |
| 3423 | ** including calls from other threads against the same cache. |
| 3424 | ** Hence, a mutex on the BtShared should be held prior to calling |
| 3425 | ** this routine. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3426 | ** |
| 3427 | ** These routines is used to get quick access to key and data |
| 3428 | ** in the common case where no overflow pages are used. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3429 | */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3430 | const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3431 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3432 | if( pCur->eState==CURSOR_VALID ){ |
| 3433 | return (const void*)fetchPayload(pCur, pAmt, 0); |
| 3434 | } |
| 3435 | return 0; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3436 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3437 | const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3438 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3439 | if( pCur->eState==CURSOR_VALID ){ |
| 3440 | return (const void*)fetchPayload(pCur, pAmt, 1); |
| 3441 | } |
| 3442 | return 0; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3443 | } |
| 3444 | |
| 3445 | |
| 3446 | /* |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3447 | ** 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] | 3448 | ** page number of the child page to move to. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3449 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3450 | static int moveToChild(BtCursor *pCur, u32 newPgno){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3451 | int rc; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3452 | int i = pCur->iPage; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3453 | MemPage *pNewPage; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 3454 | BtShared *pBt = pCur->pBt; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3455 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3456 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3457 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3458 | assert( pCur->iPage<BTCURSOR_MAX_DEPTH ); |
| 3459 | if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){ |
| 3460 | return SQLITE_CORRUPT_BKPT; |
| 3461 | } |
| 3462 | rc = getAndInitPage(pBt, newPgno, &pNewPage); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3463 | if( rc ) return rc; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3464 | pCur->apPage[i+1] = pNewPage; |
| 3465 | pCur->aiIdx[i+1] = 0; |
| 3466 | pCur->iPage++; |
| 3467 | |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3468 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3469 | pCur->validNKey = 0; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 3470 | if( pNewPage->nCell<1 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3471 | return SQLITE_CORRUPT_BKPT; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 3472 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3473 | return SQLITE_OK; |
| 3474 | } |
| 3475 | |
danielk1977 | bf93c56 | 2008-09-29 15:53:25 +0000 | [diff] [blame] | 3476 | #ifndef NDEBUG |
| 3477 | /* |
| 3478 | ** Page pParent is an internal (non-leaf) tree page. This function |
| 3479 | ** asserts that page number iChild is the left-child if the iIdx'th |
| 3480 | ** cell in page pParent. Or, if iIdx is equal to the total number of |
| 3481 | ** cells in pParent, that page number iChild is the right-child of |
| 3482 | ** the page. |
| 3483 | */ |
| 3484 | static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){ |
| 3485 | assert( iIdx<=pParent->nCell ); |
| 3486 | if( iIdx==pParent->nCell ){ |
| 3487 | assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild ); |
| 3488 | }else{ |
| 3489 | assert( get4byte(findCell(pParent, iIdx))==iChild ); |
| 3490 | } |
| 3491 | } |
| 3492 | #else |
| 3493 | # define assertParentIndex(x,y,z) |
| 3494 | #endif |
| 3495 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3496 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3497 | ** Move the cursor up to the parent page. |
| 3498 | ** |
| 3499 | ** pCur->idx is set to the cell index that contains the pointer |
| 3500 | ** to the page we are coming from. If we are coming from the |
| 3501 | ** right-most child page then pCur->idx is set to one more than |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3502 | ** the largest cell index. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3503 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3504 | void sqlite3BtreeMoveToParent(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3505 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3506 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3507 | assert( pCur->iPage>0 ); |
| 3508 | assert( pCur->apPage[pCur->iPage] ); |
danielk1977 | bf93c56 | 2008-09-29 15:53:25 +0000 | [diff] [blame] | 3509 | assertParentIndex( |
| 3510 | pCur->apPage[pCur->iPage-1], |
| 3511 | pCur->aiIdx[pCur->iPage-1], |
| 3512 | pCur->apPage[pCur->iPage]->pgno |
| 3513 | ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3514 | releasePage(pCur->apPage[pCur->iPage]); |
| 3515 | pCur->iPage--; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3516 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3517 | pCur->validNKey = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3518 | } |
| 3519 | |
| 3520 | /* |
| 3521 | ** Move the cursor to the root page |
| 3522 | */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3523 | static int moveToRoot(BtCursor *pCur){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3524 | MemPage *pRoot; |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3525 | int rc = SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3526 | Btree *p = pCur->pBtree; |
| 3527 | BtShared *pBt = p->pBt; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3528 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3529 | assert( cursorHoldsMutex(pCur) ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 3530 | assert( CURSOR_INVALID < CURSOR_REQUIRESEEK ); |
| 3531 | assert( CURSOR_VALID < CURSOR_REQUIRESEEK ); |
| 3532 | assert( CURSOR_FAULT > CURSOR_REQUIRESEEK ); |
| 3533 | if( pCur->eState>=CURSOR_REQUIRESEEK ){ |
| 3534 | if( pCur->eState==CURSOR_FAULT ){ |
| 3535 | return pCur->skip; |
| 3536 | } |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 3537 | sqlite3BtreeClearCursor(pCur); |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 3538 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3539 | |
| 3540 | if( pCur->iPage>=0 ){ |
| 3541 | int i; |
| 3542 | for(i=1; i<=pCur->iPage; i++){ |
| 3543 | releasePage(pCur->apPage[i]); |
danielk1977 | d9f6c53 | 2008-09-19 16:39:38 +0000 | [diff] [blame] | 3544 | } |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3545 | }else{ |
| 3546 | if( |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3547 | SQLITE_OK!=(rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0])) |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3548 | ){ |
| 3549 | pCur->eState = CURSOR_INVALID; |
| 3550 | return rc; |
| 3551 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3552 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3553 | |
| 3554 | pRoot = pCur->apPage[0]; |
| 3555 | assert( pRoot->pgno==pCur->pgnoRoot ); |
| 3556 | pCur->iPage = 0; |
| 3557 | pCur->aiIdx[0] = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3558 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3559 | pCur->atLast = 0; |
| 3560 | pCur->validNKey = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3561 | |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3562 | if( pRoot->nCell==0 && !pRoot->leaf ){ |
| 3563 | Pgno subpage; |
| 3564 | assert( pRoot->pgno==1 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3565 | subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3566 | assert( subpage>0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3567 | pCur->eState = CURSOR_VALID; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3568 | rc = moveToChild(pCur, subpage); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3569 | }else{ |
| 3570 | pCur->eState = ((pRoot->nCell>0)?CURSOR_VALID:CURSOR_INVALID); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3571 | } |
| 3572 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3573 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3574 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3575 | /* |
| 3576 | ** Move the cursor down to the left-most leaf entry beneath the |
| 3577 | ** entry to which it is currently pointing. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3578 | ** |
| 3579 | ** The left-most leaf is the one with the smallest key - the first |
| 3580 | ** in ascending order. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3581 | */ |
| 3582 | static int moveToLeftmost(BtCursor *pCur){ |
| 3583 | Pgno pgno; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3584 | int rc = SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3585 | MemPage *pPage; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3586 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3587 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3588 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3589 | while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){ |
| 3590 | assert( pCur->aiIdx[pCur->iPage]<pPage->nCell ); |
| 3591 | pgno = get4byte(findCell(pPage, pCur->aiIdx[pCur->iPage])); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3592 | rc = moveToChild(pCur, pgno); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3593 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3594 | return rc; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3595 | } |
| 3596 | |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3597 | /* |
| 3598 | ** Move the cursor down to the right-most leaf entry beneath the |
| 3599 | ** page to which it is currently pointing. Notice the difference |
| 3600 | ** between moveToLeftmost() and moveToRightmost(). moveToLeftmost() |
| 3601 | ** finds the left-most entry beneath the *entry* whereas moveToRightmost() |
| 3602 | ** finds the right-most entry beneath the *page*. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3603 | ** |
| 3604 | ** The right-most entry is the one with the largest key - the last |
| 3605 | ** key in ascending order. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3606 | */ |
| 3607 | static int moveToRightmost(BtCursor *pCur){ |
| 3608 | Pgno pgno; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3609 | int rc = SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3610 | MemPage *pPage; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3611 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3612 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3613 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3614 | while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3615 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3616 | pCur->aiIdx[pCur->iPage] = pPage->nCell; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3617 | rc = moveToChild(pCur, pgno); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3618 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3619 | if( rc==SQLITE_OK ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3620 | pCur->aiIdx[pCur->iPage] = pPage->nCell-1; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3621 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3622 | pCur->validNKey = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3623 | } |
danielk1977 | 518002e | 2008-09-05 05:02:46 +0000 | [diff] [blame] | 3624 | return rc; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3625 | } |
| 3626 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3627 | /* Move the cursor to the first entry in the table. Return SQLITE_OK |
| 3628 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 3629 | ** or set *pRes to 1 if the table is empty. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3630 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3631 | int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3632 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3633 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3634 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3635 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3636 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3637 | if( rc==SQLITE_OK ){ |
| 3638 | if( pCur->eState==CURSOR_INVALID ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3639 | assert( pCur->apPage[pCur->iPage]->nCell==0 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3640 | *pRes = 1; |
| 3641 | rc = SQLITE_OK; |
| 3642 | }else{ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3643 | assert( pCur->apPage[pCur->iPage]->nCell>0 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3644 | *pRes = 0; |
| 3645 | rc = moveToLeftmost(pCur); |
| 3646 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3647 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3648 | return rc; |
| 3649 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3650 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3651 | /* Move the cursor to the last entry in the table. Return SQLITE_OK |
| 3652 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 3653 | ** or set *pRes to 1 if the table is empty. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3654 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3655 | int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3656 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3657 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3658 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3659 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3660 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3661 | if( rc==SQLITE_OK ){ |
| 3662 | if( CURSOR_INVALID==pCur->eState ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3663 | assert( pCur->apPage[pCur->iPage]->nCell==0 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3664 | *pRes = 1; |
| 3665 | }else{ |
| 3666 | assert( pCur->eState==CURSOR_VALID ); |
| 3667 | *pRes = 0; |
| 3668 | rc = moveToRightmost(pCur); |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3669 | getCellInfo(pCur); |
| 3670 | pCur->atLast = rc==SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3671 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3672 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3673 | return rc; |
| 3674 | } |
| 3675 | |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 3676 | /* Move the cursor so that it points to an entry near the key |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3677 | ** specified by pIdxKey or intKey. Return a success code. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3678 | ** |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3679 | ** For INTKEY tables, the intKey parameter is used. pIdxKey |
| 3680 | ** must be NULL. For index tables, pIdxKey is used and intKey |
| 3681 | ** is ignored. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3682 | ** |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3683 | ** If an exact match is not found, then the cursor is always |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3684 | ** left pointing at a leaf page which would hold the entry if it |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3685 | ** were present. The cursor might point to an entry that comes |
| 3686 | ** before or after the key. |
| 3687 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3688 | ** The result of comparing the key with the entry to which the |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 3689 | ** cursor is written to *pRes if pRes!=NULL. The meaning of |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3690 | ** this value is as follows: |
| 3691 | ** |
| 3692 | ** *pRes<0 The cursor is left pointing at an entry that |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3693 | ** is smaller than pKey or if the table is empty |
| 3694 | ** and the cursor is therefore left point to nothing. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3695 | ** |
| 3696 | ** *pRes==0 The cursor is left pointing at an entry that |
| 3697 | ** exactly matches pKey. |
| 3698 | ** |
| 3699 | ** *pRes>0 The cursor is left pointing at an entry that |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 3700 | ** is larger than pKey. |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3701 | ** |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3702 | */ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3703 | int sqlite3BtreeMovetoUnpacked( |
| 3704 | BtCursor *pCur, /* The cursor to be moved */ |
| 3705 | UnpackedRecord *pIdxKey, /* Unpacked index key */ |
| 3706 | i64 intKey, /* The table key */ |
| 3707 | int biasRight, /* If true, bias the search to the high end */ |
| 3708 | int *pRes /* Write search results here */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3709 | ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3710 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3711 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3712 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3713 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3714 | |
| 3715 | /* If the cursor is already positioned at the point we are trying |
| 3716 | ** to move to, then just return without doing any work */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3717 | if( pCur->eState==CURSOR_VALID && pCur->validNKey |
| 3718 | && pCur->apPage[0]->intKey |
| 3719 | ){ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3720 | if( pCur->info.nKey==intKey ){ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3721 | *pRes = 0; |
| 3722 | return SQLITE_OK; |
| 3723 | } |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3724 | if( pCur->atLast && pCur->info.nKey<intKey ){ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3725 | *pRes = -1; |
| 3726 | return SQLITE_OK; |
| 3727 | } |
| 3728 | } |
| 3729 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3730 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3731 | if( rc ){ |
| 3732 | return rc; |
| 3733 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3734 | assert( pCur->apPage[pCur->iPage] ); |
| 3735 | assert( pCur->apPage[pCur->iPage]->isInit ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3736 | if( pCur->eState==CURSOR_INVALID ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3737 | *pRes = -1; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3738 | assert( pCur->apPage[pCur->iPage]->nCell==0 ); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3739 | return SQLITE_OK; |
| 3740 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3741 | assert( pCur->apPage[0]->intKey || pIdxKey ); |
drh | 1468438 | 2006-11-30 13:05:29 +0000 | [diff] [blame] | 3742 | for(;;){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3743 | int lwr, upr; |
| 3744 | Pgno chldPg; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3745 | MemPage *pPage = pCur->apPage[pCur->iPage]; |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3746 | int c = -1; /* pRes return if table is empty must be -1 */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3747 | lwr = 0; |
| 3748 | upr = pPage->nCell-1; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3749 | if( !pPage->intKey && pIdxKey==0 ){ |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3750 | rc = SQLITE_CORRUPT_BKPT; |
| 3751 | goto moveto_finish; |
drh | 4eec4c1 | 2005-01-21 00:22:37 +0000 | [diff] [blame] | 3752 | } |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3753 | if( biasRight ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3754 | pCur->aiIdx[pCur->iPage] = upr; |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3755 | }else{ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3756 | pCur->aiIdx[pCur->iPage] = (upr+lwr)/2; |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3757 | } |
drh | f1d68b3 | 2007-03-29 04:43:26 +0000 | [diff] [blame] | 3758 | if( lwr<=upr ) for(;;){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3759 | void *pCellKey; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3760 | i64 nCellKey; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3761 | int idx = pCur->aiIdx[pCur->iPage]; |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3762 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3763 | pCur->validNKey = 1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3764 | if( pPage->intKey ){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3765 | u8 *pCell; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3766 | pCell = findCell(pPage, idx) + pPage->childPtrSize; |
drh | d172f86 | 2006-01-12 15:01:15 +0000 | [diff] [blame] | 3767 | if( pPage->hasData ){ |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 3768 | u32 dummy; |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 3769 | pCell += getVarint32(pCell, dummy); |
drh | d172f86 | 2006-01-12 15:01:15 +0000 | [diff] [blame] | 3770 | } |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3771 | getVarint(pCell, (u64*)&nCellKey); |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3772 | if( nCellKey==intKey ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3773 | c = 0; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3774 | }else if( nCellKey<intKey ){ |
drh | 41eb9e9 | 2008-04-02 18:33:07 +0000 | [diff] [blame] | 3775 | c = -1; |
| 3776 | }else{ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3777 | assert( nCellKey>intKey ); |
drh | 41eb9e9 | 2008-04-02 18:33:07 +0000 | [diff] [blame] | 3778 | c = +1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3779 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3780 | }else{ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3781 | int available; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3782 | pCellKey = (void *)fetchPayload(pCur, &available, 0); |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3783 | nCellKey = pCur->info.nKey; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3784 | if( available>=nCellKey ){ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3785 | c = sqlite3VdbeRecordCompare(nCellKey, pCellKey, pIdxKey); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3786 | }else{ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 3787 | pCellKey = sqlite3Malloc( nCellKey ); |
danielk1977 | 6507ecb | 2008-03-25 09:56:44 +0000 | [diff] [blame] | 3788 | if( pCellKey==0 ){ |
| 3789 | rc = SQLITE_NOMEM; |
| 3790 | goto moveto_finish; |
| 3791 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3792 | rc = sqlite3BtreeKey(pCur, 0, nCellKey, (void *)pCellKey); |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3793 | c = sqlite3VdbeRecordCompare(nCellKey, pCellKey, pIdxKey); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 3794 | sqlite3_free(pCellKey); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3795 | if( rc ) goto moveto_finish; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3796 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3797 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3798 | if( c==0 ){ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3799 | pCur->info.nKey = nCellKey; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 3800 | if( pPage->intKey && !pPage->leaf ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3801 | lwr = idx; |
drh | fc70e6f | 2004-05-12 21:11:27 +0000 | [diff] [blame] | 3802 | upr = lwr - 1; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3803 | break; |
| 3804 | }else{ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3805 | if( pRes ) *pRes = 0; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3806 | rc = SQLITE_OK; |
| 3807 | goto moveto_finish; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3808 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3809 | } |
| 3810 | if( c<0 ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3811 | lwr = idx+1; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3812 | }else{ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3813 | upr = idx-1; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3814 | } |
drh | f1d68b3 | 2007-03-29 04:43:26 +0000 | [diff] [blame] | 3815 | if( lwr>upr ){ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3816 | pCur->info.nKey = nCellKey; |
drh | f1d68b3 | 2007-03-29 04:43:26 +0000 | [diff] [blame] | 3817 | break; |
| 3818 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3819 | pCur->aiIdx[pCur->iPage] = (lwr+upr)/2; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3820 | } |
| 3821 | assert( lwr==upr+1 ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3822 | assert( pPage->isInit ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3823 | if( pPage->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3824 | chldPg = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3825 | }else if( lwr>=pPage->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3826 | chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3827 | }else{ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3828 | chldPg = get4byte(findCell(pPage, lwr)); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3829 | } |
| 3830 | if( chldPg==0 ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3831 | assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3832 | if( pRes ) *pRes = c; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3833 | rc = SQLITE_OK; |
| 3834 | goto moveto_finish; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3835 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3836 | pCur->aiIdx[pCur->iPage] = lwr; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3837 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3838 | pCur->validNKey = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3839 | rc = moveToChild(pCur, chldPg); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3840 | if( rc ) goto moveto_finish; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3841 | } |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3842 | moveto_finish: |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3843 | return rc; |
| 3844 | } |
| 3845 | |
| 3846 | /* |
| 3847 | ** In this version of BtreeMoveto, pKey is a packed index record |
| 3848 | ** such as is generated by the OP_MakeRecord opcode. Unpack the |
| 3849 | ** record and then call BtreeMovetoUnpacked() to do the work. |
| 3850 | */ |
| 3851 | int sqlite3BtreeMoveto( |
| 3852 | BtCursor *pCur, /* Cursor open on the btree to be searched */ |
| 3853 | const void *pKey, /* Packed key if the btree is an index */ |
| 3854 | i64 nKey, /* Integer key for tables. Size of pKey for indices */ |
| 3855 | int bias, /* Bias search to the high end */ |
| 3856 | int *pRes /* Write search results here */ |
| 3857 | ){ |
| 3858 | int rc; /* Status code */ |
| 3859 | UnpackedRecord *pIdxKey; /* Unpacked index key */ |
drh | 23f79d0 | 2008-08-20 22:06:47 +0000 | [diff] [blame] | 3860 | UnpackedRecord aSpace[16]; /* Temp space for pIdxKey - to avoid a malloc */ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3861 | |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 3862 | if( pKey ){ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3863 | pIdxKey = sqlite3VdbeRecordUnpack(pCur->pKeyInfo, nKey, pKey, |
drh | 23f79d0 | 2008-08-20 22:06:47 +0000 | [diff] [blame] | 3864 | aSpace, sizeof(aSpace)); |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3865 | if( pIdxKey==0 ) return SQLITE_NOMEM; |
| 3866 | }else{ |
| 3867 | pIdxKey = 0; |
| 3868 | } |
| 3869 | rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes); |
| 3870 | if( pKey ){ |
| 3871 | sqlite3VdbeDeleteUnpackedRecord(pIdxKey); |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 3872 | } |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3873 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3874 | } |
| 3875 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3876 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3877 | /* |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3878 | ** Return TRUE if the cursor is not pointing at an entry of the table. |
| 3879 | ** |
| 3880 | ** TRUE will be returned after a call to sqlite3BtreeNext() moves |
| 3881 | ** past the last entry in the table or sqlite3BtreePrev() moves past |
| 3882 | ** the first entry. TRUE is also returned if the table is empty. |
| 3883 | */ |
| 3884 | int sqlite3BtreeEof(BtCursor *pCur){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3885 | /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries |
| 3886 | ** have been deleted? This API will need to change to return an error code |
| 3887 | ** as well as the boolean result value. |
| 3888 | */ |
| 3889 | return (CURSOR_VALID!=pCur->eState); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3890 | } |
| 3891 | |
| 3892 | /* |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 3893 | ** Return the database connection handle for a cursor. |
| 3894 | */ |
| 3895 | sqlite3 *sqlite3BtreeCursorDb(const BtCursor *pCur){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3896 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
| 3897 | return pCur->pBtree->db; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 3898 | } |
| 3899 | |
| 3900 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3901 | ** Advance the cursor to the next entry in the database. If |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3902 | ** successful then set *pRes=0. If the cursor |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3903 | ** was already pointing to the last entry in the database before |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3904 | ** this routine was called, then set *pRes=1. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3905 | */ |
drh | d094db1 | 2008-04-03 21:46:57 +0000 | [diff] [blame] | 3906 | int sqlite3BtreeNext(BtCursor *pCur, int *pRes){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3907 | int rc; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3908 | int idx; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 3909 | MemPage *pPage; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3910 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3911 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 3912 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3913 | if( rc!=SQLITE_OK ){ |
| 3914 | return rc; |
| 3915 | } |
drh | 8c4d3a6 | 2007-04-06 01:03:32 +0000 | [diff] [blame] | 3916 | assert( pRes!=0 ); |
drh | 8c4d3a6 | 2007-04-06 01:03:32 +0000 | [diff] [blame] | 3917 | if( CURSOR_INVALID==pCur->eState ){ |
| 3918 | *pRes = 1; |
| 3919 | return SQLITE_OK; |
| 3920 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3921 | if( pCur->skip>0 ){ |
| 3922 | pCur->skip = 0; |
| 3923 | *pRes = 0; |
| 3924 | return SQLITE_OK; |
| 3925 | } |
| 3926 | pCur->skip = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3927 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3928 | pPage = pCur->apPage[pCur->iPage]; |
| 3929 | idx = ++pCur->aiIdx[pCur->iPage]; |
| 3930 | assert( pPage->isInit ); |
| 3931 | assert( idx<=pPage->nCell ); |
danielk1977 | 6a43f9b | 2004-11-16 04:57:24 +0000 | [diff] [blame] | 3932 | |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3933 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3934 | pCur->validNKey = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3935 | if( idx>=pPage->nCell ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3936 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3937 | rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3938 | if( rc ) return rc; |
| 3939 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3940 | *pRes = 0; |
| 3941 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3942 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3943 | do{ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3944 | if( pCur->iPage==0 ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3945 | *pRes = 1; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3946 | pCur->eState = CURSOR_INVALID; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3947 | return SQLITE_OK; |
| 3948 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3949 | sqlite3BtreeMoveToParent(pCur); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3950 | pPage = pCur->apPage[pCur->iPage]; |
| 3951 | }while( pCur->aiIdx[pCur->iPage]>=pPage->nCell ); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3952 | *pRes = 0; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 3953 | if( pPage->intKey ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3954 | rc = sqlite3BtreeNext(pCur, pRes); |
| 3955 | }else{ |
| 3956 | rc = SQLITE_OK; |
| 3957 | } |
| 3958 | return rc; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3959 | } |
| 3960 | *pRes = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3961 | if( pPage->leaf ){ |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3962 | return SQLITE_OK; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3963 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3964 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3965 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3966 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3967 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3968 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3969 | /* |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3970 | ** 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] | 3971 | ** successful then set *pRes=0. If the cursor |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3972 | ** was already pointing to the first entry in the database before |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3973 | ** this routine was called, then set *pRes=1. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3974 | */ |
drh | d094db1 | 2008-04-03 21:46:57 +0000 | [diff] [blame] | 3975 | int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){ |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3976 | int rc; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3977 | MemPage *pPage; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3978 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3979 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 3980 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3981 | if( rc!=SQLITE_OK ){ |
| 3982 | return rc; |
| 3983 | } |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3984 | pCur->atLast = 0; |
drh | 8c4d3a6 | 2007-04-06 01:03:32 +0000 | [diff] [blame] | 3985 | if( CURSOR_INVALID==pCur->eState ){ |
| 3986 | *pRes = 1; |
| 3987 | return SQLITE_OK; |
| 3988 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3989 | if( pCur->skip<0 ){ |
| 3990 | pCur->skip = 0; |
| 3991 | *pRes = 0; |
| 3992 | return SQLITE_OK; |
| 3993 | } |
| 3994 | pCur->skip = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3995 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3996 | pPage = pCur->apPage[pCur->iPage]; |
| 3997 | assert( pPage->isInit ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3998 | if( !pPage->leaf ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3999 | int idx = pCur->aiIdx[pCur->iPage]; |
| 4000 | rc = moveToChild(pCur, get4byte(findCell(pPage, idx))); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4001 | if( rc ){ |
| 4002 | return rc; |
| 4003 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4004 | rc = moveToRightmost(pCur); |
| 4005 | }else{ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4006 | while( pCur->aiIdx[pCur->iPage]==0 ){ |
| 4007 | if( pCur->iPage==0 ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4008 | pCur->eState = CURSOR_INVALID; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 4009 | *pRes = 1; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4010 | return SQLITE_OK; |
| 4011 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4012 | sqlite3BtreeMoveToParent(pCur); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4013 | } |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 4014 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 4015 | pCur->validNKey = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4016 | |
| 4017 | pCur->aiIdx[pCur->iPage]--; |
| 4018 | pPage = pCur->apPage[pCur->iPage]; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 4019 | if( pPage->intKey && !pPage->leaf ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4020 | rc = sqlite3BtreePrevious(pCur, pRes); |
| 4021 | }else{ |
| 4022 | rc = SQLITE_OK; |
| 4023 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4024 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4025 | *pRes = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4026 | return rc; |
| 4027 | } |
| 4028 | |
| 4029 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4030 | ** Allocate a new page from the database file. |
| 4031 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4032 | ** The new page is marked as dirty. (In other words, sqlite3PagerWrite() |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4033 | ** has already been called on the new page.) The new page has also |
| 4034 | ** been referenced and the calling routine is responsible for calling |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4035 | ** sqlite3PagerUnref() on the new page when it is done. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4036 | ** |
| 4037 | ** SQLITE_OK is returned on success. Any other return value indicates |
| 4038 | ** an error. *ppPage and *pPgno are undefined in the event of an error. |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4039 | ** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned. |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 4040 | ** |
drh | 199e3cf | 2002-07-18 11:01:47 +0000 | [diff] [blame] | 4041 | ** If the "nearby" parameter is not 0, then a (feeble) effort is made to |
| 4042 | ** 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] | 4043 | ** attempt to keep related pages close to each other in the database file, |
| 4044 | ** which in turn can make database access faster. |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4045 | ** |
| 4046 | ** If the "exact" parameter is not 0, and the page-number nearby exists |
| 4047 | ** anywhere on the free-list, then it is guarenteed to be returned. This |
| 4048 | ** is only used by auto-vacuum databases when allocating a new table. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4049 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4050 | static int allocateBtreePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4051 | BtShared *pBt, |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4052 | MemPage **ppPage, |
| 4053 | Pgno *pPgno, |
| 4054 | Pgno nearby, |
| 4055 | u8 exact |
| 4056 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4057 | MemPage *pPage1; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4058 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4059 | int n; /* Number of pages on the freelist */ |
| 4060 | int k; /* Number of leaves on the trunk of the freelist */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4061 | MemPage *pTrunk = 0; |
| 4062 | MemPage *pPrevTrunk = 0; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4063 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4064 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4065 | pPage1 = pBt->pPage1; |
| 4066 | n = get4byte(&pPage1->aData[36]); |
| 4067 | if( n>0 ){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4068 | /* There are pages on the freelist. Reuse one of those pages. */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4069 | Pgno iTrunk; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4070 | u8 searchList = 0; /* If the free-list must be searched for 'nearby' */ |
| 4071 | |
| 4072 | /* If the 'exact' parameter was true and a query of the pointer-map |
| 4073 | ** shows that the page 'nearby' is somewhere on the free-list, then |
| 4074 | ** the entire-list will be searched for that page. |
| 4075 | */ |
| 4076 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 4077 | if( exact && nearby<=pagerPagecount(pBt->pPager) ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4078 | u8 eType; |
| 4079 | assert( nearby>0 ); |
| 4080 | assert( pBt->autoVacuum ); |
| 4081 | rc = ptrmapGet(pBt, nearby, &eType, 0); |
| 4082 | if( rc ) return rc; |
| 4083 | if( eType==PTRMAP_FREEPAGE ){ |
| 4084 | searchList = 1; |
| 4085 | } |
| 4086 | *pPgno = nearby; |
| 4087 | } |
| 4088 | #endif |
| 4089 | |
| 4090 | /* Decrement the free-list count by 1. Set iTrunk to the index of the |
| 4091 | ** first free-list trunk page. iPrevTrunk is initially 1. |
| 4092 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4093 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4094 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4095 | put4byte(&pPage1->aData[36], n-1); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4096 | |
| 4097 | /* The code within this loop is run only once if the 'searchList' variable |
| 4098 | ** is not true. Otherwise, it runs once for each trunk-page on the |
| 4099 | ** free-list until the page 'nearby' is located. |
| 4100 | */ |
| 4101 | do { |
| 4102 | pPrevTrunk = pTrunk; |
| 4103 | if( pPrevTrunk ){ |
| 4104 | iTrunk = get4byte(&pPrevTrunk->aData[0]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 4105 | }else{ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4106 | iTrunk = get4byte(&pPage1->aData[32]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 4107 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4108 | rc = sqlite3BtreeGetPage(pBt, iTrunk, &pTrunk, 0); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4109 | if( rc ){ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4110 | pTrunk = 0; |
| 4111 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4112 | } |
| 4113 | |
| 4114 | k = get4byte(&pTrunk->aData[4]); |
| 4115 | if( k==0 && !searchList ){ |
| 4116 | /* The trunk has no leaves and the list is not being searched. |
| 4117 | ** So extract the trunk page itself and use it as the newly |
| 4118 | ** allocated page */ |
| 4119 | assert( pPrevTrunk==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4120 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4121 | if( rc ){ |
| 4122 | goto end_allocate_page; |
| 4123 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4124 | *pPgno = iTrunk; |
| 4125 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 4126 | *ppPage = pTrunk; |
| 4127 | pTrunk = 0; |
| 4128 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
drh | 45b1fac | 2008-07-04 17:52:42 +0000 | [diff] [blame] | 4129 | }else if( k>pBt->usableSize/4 - 2 ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4130 | /* Value of k is out of range. Database corruption */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4131 | rc = SQLITE_CORRUPT_BKPT; |
| 4132 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4133 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4134 | }else if( searchList && nearby==iTrunk ){ |
| 4135 | /* The list is being searched and this trunk page is the page |
| 4136 | ** to allocate, regardless of whether it has leaves. |
| 4137 | */ |
| 4138 | assert( *pPgno==iTrunk ); |
| 4139 | *ppPage = pTrunk; |
| 4140 | searchList = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4141 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4142 | if( rc ){ |
| 4143 | goto end_allocate_page; |
| 4144 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4145 | if( k==0 ){ |
| 4146 | if( !pPrevTrunk ){ |
| 4147 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 4148 | }else{ |
| 4149 | memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4); |
| 4150 | } |
| 4151 | }else{ |
| 4152 | /* The trunk page is required by the caller but it contains |
| 4153 | ** pointers to free-list leaves. The first leaf becomes a trunk |
| 4154 | ** page in this case. |
| 4155 | */ |
| 4156 | MemPage *pNewTrunk; |
| 4157 | Pgno iNewTrunk = get4byte(&pTrunk->aData[8]); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4158 | rc = sqlite3BtreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4159 | if( rc!=SQLITE_OK ){ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4160 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4161 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4162 | rc = sqlite3PagerWrite(pNewTrunk->pDbPage); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4163 | if( rc!=SQLITE_OK ){ |
| 4164 | releasePage(pNewTrunk); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4165 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4166 | } |
| 4167 | memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4); |
| 4168 | put4byte(&pNewTrunk->aData[4], k-1); |
| 4169 | memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4170 | releasePage(pNewTrunk); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4171 | if( !pPrevTrunk ){ |
| 4172 | put4byte(&pPage1->aData[32], iNewTrunk); |
| 4173 | }else{ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4174 | rc = sqlite3PagerWrite(pPrevTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4175 | if( rc ){ |
| 4176 | goto end_allocate_page; |
| 4177 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4178 | put4byte(&pPrevTrunk->aData[0], iNewTrunk); |
| 4179 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4180 | } |
| 4181 | pTrunk = 0; |
| 4182 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
| 4183 | #endif |
| 4184 | }else{ |
| 4185 | /* Extract a leaf from the trunk */ |
| 4186 | int closest; |
| 4187 | Pgno iPage; |
| 4188 | unsigned char *aData = pTrunk->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4189 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4190 | if( rc ){ |
| 4191 | goto end_allocate_page; |
| 4192 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4193 | if( nearby>0 ){ |
| 4194 | int i, dist; |
| 4195 | closest = 0; |
| 4196 | dist = get4byte(&aData[8]) - nearby; |
| 4197 | if( dist<0 ) dist = -dist; |
| 4198 | for(i=1; i<k; i++){ |
| 4199 | int d2 = get4byte(&aData[8+i*4]) - nearby; |
| 4200 | if( d2<0 ) d2 = -d2; |
| 4201 | if( d2<dist ){ |
| 4202 | closest = i; |
| 4203 | dist = d2; |
| 4204 | } |
| 4205 | } |
| 4206 | }else{ |
| 4207 | closest = 0; |
| 4208 | } |
| 4209 | |
| 4210 | iPage = get4byte(&aData[8+closest*4]); |
| 4211 | if( !searchList || iPage==nearby ){ |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 4212 | int nPage; |
shane | 1f9e6aa | 2008-06-09 19:27:11 +0000 | [diff] [blame] | 4213 | *pPgno = iPage; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 4214 | nPage = pagerPagecount(pBt->pPager); |
| 4215 | if( *pPgno>nPage ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4216 | /* Free page off the end of the file */ |
danielk1977 | 43e377a | 2008-05-05 12:09:32 +0000 | [diff] [blame] | 4217 | rc = SQLITE_CORRUPT_BKPT; |
| 4218 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4219 | } |
| 4220 | TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d" |
| 4221 | ": %d more free pages\n", |
| 4222 | *pPgno, closest+1, k, pTrunk->pgno, n-1)); |
| 4223 | if( closest<k-1 ){ |
| 4224 | memcpy(&aData[8+closest*4], &aData[4+k*4], 4); |
| 4225 | } |
| 4226 | put4byte(&aData[4], k-1); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4227 | rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 1); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4228 | if( rc==SQLITE_OK ){ |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 4229 | sqlite3PagerDontRollback((*ppPage)->pDbPage); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4230 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 4231 | if( rc!=SQLITE_OK ){ |
| 4232 | releasePage(*ppPage); |
| 4233 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4234 | } |
| 4235 | searchList = 0; |
| 4236 | } |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 4237 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4238 | releasePage(pPrevTrunk); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4239 | pPrevTrunk = 0; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4240 | }while( searchList ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4241 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4242 | /* There are no pages on the freelist, so create a new page at the |
| 4243 | ** end of the file */ |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 4244 | int nPage = pagerPagecount(pBt->pPager); |
| 4245 | *pPgno = nPage + 1; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4246 | |
| 4247 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 4248 | if( pBt->nTrunc ){ |
| 4249 | /* An incr-vacuum has already run within this transaction. So the |
| 4250 | ** page to allocate is not from the physical end of the file, but |
| 4251 | ** at pBt->nTrunc. |
| 4252 | */ |
| 4253 | *pPgno = pBt->nTrunc+1; |
| 4254 | if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ |
| 4255 | (*pPgno)++; |
| 4256 | } |
| 4257 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 4258 | if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, *pPgno) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4259 | /* If *pPgno refers to a pointer-map page, allocate two new pages |
| 4260 | ** at the end of the file instead of one. The first allocated page |
| 4261 | ** becomes a new pointer-map page, the second is used by the caller. |
| 4262 | */ |
| 4263 | TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno)); |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4264 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4265 | (*pPgno)++; |
drh | 7219043 | 2008-01-31 14:54:43 +0000 | [diff] [blame] | 4266 | if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ (*pPgno)++; } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4267 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 4268 | if( pBt->nTrunc ){ |
| 4269 | pBt->nTrunc = *pPgno; |
| 4270 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4271 | #endif |
| 4272 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4273 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4274 | rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 0); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4275 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4276 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 4277 | if( rc!=SQLITE_OK ){ |
| 4278 | releasePage(*ppPage); |
| 4279 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4280 | TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4281 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4282 | |
| 4283 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4284 | |
| 4285 | end_allocate_page: |
| 4286 | releasePage(pTrunk); |
| 4287 | releasePage(pPrevTrunk); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4288 | if( rc==SQLITE_OK && sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){ |
| 4289 | releasePage(*ppPage); |
| 4290 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 4291 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4292 | return rc; |
| 4293 | } |
| 4294 | |
| 4295 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4296 | ** Add a page of the database file to the freelist. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4297 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4298 | ** sqlite3PagerUnref() is NOT called for pPage. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4299 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4300 | static int freePage(MemPage *pPage){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4301 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4302 | MemPage *pPage1 = pBt->pPage1; |
| 4303 | int rc, n, k; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4304 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4305 | /* Prepare the page for freeing */ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4306 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4307 | assert( pPage->pgno>1 ); |
| 4308 | pPage->isInit = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4309 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4310 | /* Increment the free page count on pPage1 */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4311 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4312 | if( rc ) return rc; |
| 4313 | n = get4byte(&pPage1->aData[36]); |
| 4314 | put4byte(&pPage1->aData[36], n+1); |
| 4315 | |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4316 | #ifdef SQLITE_SECURE_DELETE |
| 4317 | /* If the SQLITE_SECURE_DELETE compile-time option is enabled, then |
| 4318 | ** always fully overwrite deleted information with zeros. |
| 4319 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4320 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4321 | if( rc ) return rc; |
| 4322 | memset(pPage->aData, 0, pPage->pBt->pageSize); |
| 4323 | #endif |
| 4324 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4325 | /* If the database supports auto-vacuum, write an entry in the pointer-map |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4326 | ** to indicate that the page is free. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4327 | */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 4328 | if( ISAUTOVACUUM ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4329 | rc = ptrmapPut(pBt, pPage->pgno, PTRMAP_FREEPAGE, 0); |
danielk1977 | a64a035 | 2004-11-05 01:45:13 +0000 | [diff] [blame] | 4330 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4331 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4332 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4333 | if( n==0 ){ |
| 4334 | /* This is the first free page */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4335 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4336 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4337 | memset(pPage->aData, 0, 8); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4338 | put4byte(&pPage1->aData[32], pPage->pgno); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4339 | TRACE(("FREE-PAGE: %d first\n", pPage->pgno)); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4340 | }else{ |
| 4341 | /* Other free pages already exist. Retrive the first trunk page |
| 4342 | ** of the freelist and find out how many leaves it has. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4343 | MemPage *pTrunk; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4344 | rc = sqlite3BtreeGetPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk, 0); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4345 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4346 | k = get4byte(&pTrunk->aData[4]); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 4347 | if( k>=pBt->usableSize/4 - 8 ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4348 | /* The trunk is full. Turn the page being freed into a new |
drh | 45b1fac | 2008-07-04 17:52:42 +0000 | [diff] [blame] | 4349 | ** trunk page with no leaves. |
| 4350 | ** |
| 4351 | ** Note that the trunk page is not really full until it contains |
| 4352 | ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have |
| 4353 | ** coded. But due to a coding error in versions of SQLite prior to |
| 4354 | ** 3.6.0, databases with freelist trunk pages holding more than |
| 4355 | ** usableSize/4 - 8 entries will be reported as corrupt. In order |
| 4356 | ** to maintain backwards compatibility with older versions of SQLite, |
| 4357 | ** we will contain to restrict the number of entries to usableSize/4 - 8 |
| 4358 | ** for now. At some point in the future (once everyone has upgraded |
| 4359 | ** to 3.6.0 or later) we should consider fixing the conditional above |
| 4360 | ** to read "usableSize/4-2" instead of "usableSize/4-8". |
| 4361 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4362 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | b9ee493 | 2007-09-07 14:32:06 +0000 | [diff] [blame] | 4363 | if( rc==SQLITE_OK ){ |
| 4364 | put4byte(pPage->aData, pTrunk->pgno); |
| 4365 | put4byte(&pPage->aData[4], 0); |
| 4366 | put4byte(&pPage1->aData[32], pPage->pgno); |
| 4367 | TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", |
| 4368 | pPage->pgno, pTrunk->pgno)); |
| 4369 | } |
| 4370 | }else if( k<0 ){ |
| 4371 | rc = SQLITE_CORRUPT; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4372 | }else{ |
| 4373 | /* Add the newly freed page as a leaf on the current trunk */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4374 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 4375 | if( rc==SQLITE_OK ){ |
| 4376 | put4byte(&pTrunk->aData[4], k+1); |
| 4377 | put4byte(&pTrunk->aData[8+k*4], pPage->pgno); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4378 | #ifndef SQLITE_SECURE_DELETE |
danielk1977 | a1fa00d | 2008-08-27 15:16:33 +0000 | [diff] [blame] | 4379 | rc = sqlite3PagerDontWrite(pPage->pDbPage); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4380 | #endif |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 4381 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4382 | 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] | 4383 | } |
| 4384 | releasePage(pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4385 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4386 | return rc; |
| 4387 | } |
| 4388 | |
| 4389 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4390 | ** Free any overflow pages associated with the given Cell. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4391 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4392 | static int clearCell(MemPage *pPage, unsigned char *pCell){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4393 | BtShared *pBt = pPage->pBt; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4394 | CellInfo info; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4395 | Pgno ovflPgno; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4396 | int rc; |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 4397 | int nOvfl; |
| 4398 | int ovflPageSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4399 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4400 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4401 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4402 | if( info.iOverflow==0 ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4403 | return SQLITE_OK; /* No overflow pages. Return without doing anything */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4404 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4405 | ovflPgno = get4byte(&pCell[info.iOverflow]); |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 4406 | ovflPageSize = pBt->usableSize - 4; |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 4407 | nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize; |
| 4408 | assert( ovflPgno==0 || nOvfl>0 ); |
| 4409 | while( nOvfl-- ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4410 | MemPage *pOvfl; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 4411 | if( ovflPgno==0 || ovflPgno>pagerPagecount(pBt->pPager) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 4412 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 4413 | } |
danielk1977 | 8c0a959 | 2007-04-30 16:55:00 +0000 | [diff] [blame] | 4414 | |
| 4415 | rc = getOverflowPage(pBt, ovflPgno, &pOvfl, (nOvfl==0)?0:&ovflPgno); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4416 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4417 | rc = freePage(pOvfl); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4418 | sqlite3PagerUnref(pOvfl->pDbPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 4419 | if( rc ) return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4420 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4421 | return SQLITE_OK; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4422 | } |
| 4423 | |
| 4424 | /* |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4425 | ** Create the byte sequence used to represent a cell on page pPage |
| 4426 | ** and write that byte sequence into pCell[]. Overflow pages are |
| 4427 | ** allocated and filled in as necessary. The calling procedure |
| 4428 | ** is responsible for making sure sufficient space has been allocated |
| 4429 | ** for pCell[]. |
| 4430 | ** |
| 4431 | ** Note that pCell does not necessary need to point to the pPage->aData |
| 4432 | ** area. pCell might point to some temporary storage. The cell will |
| 4433 | ** be constructed in this temporary area then copied into pPage->aData |
| 4434 | ** later. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4435 | */ |
| 4436 | static int fillInCell( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4437 | MemPage *pPage, /* The page that contains the cell */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4438 | unsigned char *pCell, /* Complete text of the cell */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 4439 | const void *pKey, i64 nKey, /* The key */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4440 | const void *pData,int nData, /* The data */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4441 | int nZero, /* Extra zero bytes to append to pData */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4442 | int *pnSize /* Write cell size here */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4443 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4444 | int nPayload; |
drh | 8c6fa9b | 2004-05-26 00:01:53 +0000 | [diff] [blame] | 4445 | const u8 *pSrc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4446 | int nSrc, n, rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4447 | int spaceLeft; |
| 4448 | MemPage *pOvfl = 0; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4449 | MemPage *pToRelease = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4450 | unsigned char *pPrior; |
| 4451 | unsigned char *pPayload; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4452 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4453 | Pgno pgnoOvfl = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4454 | int nHeader; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4455 | CellInfo info; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4456 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4457 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4458 | |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4459 | /* Fill in the header. */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4460 | nHeader = 0; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4461 | if( !pPage->leaf ){ |
| 4462 | nHeader += 4; |
| 4463 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4464 | if( pPage->hasData ){ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4465 | nHeader += putVarint(&pCell[nHeader], nData+nZero); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4466 | }else{ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4467 | nData = nZero = 0; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4468 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4469 | nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4470 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4471 | assert( info.nHeader==nHeader ); |
| 4472 | assert( info.nKey==nKey ); |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4473 | assert( info.nData==nData+nZero ); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4474 | |
| 4475 | /* Fill in the payload */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4476 | nPayload = nData + nZero; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4477 | if( pPage->intKey ){ |
| 4478 | pSrc = pData; |
| 4479 | nSrc = nData; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4480 | nData = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4481 | }else{ |
| 4482 | nPayload += nKey; |
| 4483 | pSrc = pKey; |
| 4484 | nSrc = nKey; |
| 4485 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4486 | *pnSize = info.nSize; |
| 4487 | spaceLeft = info.nLocal; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4488 | pPayload = &pCell[nHeader]; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4489 | pPrior = &pCell[info.iOverflow]; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4490 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4491 | while( nPayload>0 ){ |
| 4492 | if( spaceLeft==0 ){ |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4493 | int isExact = 0; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4494 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4495 | Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */ |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4496 | if( pBt->autoVacuum ){ |
| 4497 | do{ |
| 4498 | pgnoOvfl++; |
| 4499 | } while( |
| 4500 | PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt) |
| 4501 | ); |
danielk1977 | 89a4be8 | 2007-05-23 13:34:32 +0000 | [diff] [blame] | 4502 | if( pgnoOvfl>1 ){ |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4503 | /* isExact = 1; */ |
| 4504 | } |
| 4505 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4506 | #endif |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4507 | rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, isExact); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4508 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4509 | /* If the database supports auto-vacuum, and the second or subsequent |
| 4510 | ** overflow page is being allocated, add an entry to the pointer-map |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 4511 | ** for that page now. |
| 4512 | ** |
| 4513 | ** If this is the first overflow page, then write a partial entry |
| 4514 | ** to the pointer-map. If we write nothing to this pointer-map slot, |
| 4515 | ** then the optimistic overflow chain processing in clearCell() |
| 4516 | ** may misinterpret the uninitialised values and delete the |
| 4517 | ** wrong pages from the database. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4518 | */ |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 4519 | if( pBt->autoVacuum && rc==SQLITE_OK ){ |
| 4520 | u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1); |
| 4521 | rc = ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap); |
danielk1977 | 89a4be8 | 2007-05-23 13:34:32 +0000 | [diff] [blame] | 4522 | if( rc ){ |
| 4523 | releasePage(pOvfl); |
| 4524 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4525 | } |
| 4526 | #endif |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4527 | if( rc ){ |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4528 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4529 | return rc; |
| 4530 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4531 | put4byte(pPrior, pgnoOvfl); |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4532 | releasePage(pToRelease); |
| 4533 | pToRelease = pOvfl; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4534 | pPrior = pOvfl->aData; |
| 4535 | put4byte(pPrior, 0); |
| 4536 | pPayload = &pOvfl->aData[4]; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4537 | spaceLeft = pBt->usableSize - 4; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4538 | } |
| 4539 | n = nPayload; |
| 4540 | if( n>spaceLeft ) n = spaceLeft; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4541 | if( nSrc>0 ){ |
| 4542 | if( n>nSrc ) n = nSrc; |
| 4543 | assert( pSrc ); |
| 4544 | memcpy(pPayload, pSrc, n); |
| 4545 | }else{ |
| 4546 | memset(pPayload, 0, n); |
| 4547 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4548 | nPayload -= n; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4549 | pPayload += n; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4550 | pSrc += n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4551 | nSrc -= n; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4552 | spaceLeft -= n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4553 | if( nSrc==0 ){ |
| 4554 | nSrc = nData; |
| 4555 | pSrc = pData; |
| 4556 | } |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4557 | } |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4558 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4559 | return SQLITE_OK; |
| 4560 | } |
| 4561 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4562 | /* |
| 4563 | ** Remove the i-th cell from pPage. This routine effects pPage only. |
| 4564 | ** The cell content is not freed or deallocated. It is assumed that |
| 4565 | ** the cell content has been copied someplace else. This routine just |
| 4566 | ** removes the reference to the cell from pPage. |
| 4567 | ** |
| 4568 | ** "sz" must be the number of bytes in the cell. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4569 | */ |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 4570 | static int dropCell(MemPage *pPage, int idx, int sz){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4571 | int i; /* Loop counter */ |
| 4572 | int pc; /* Offset to cell content of cell being deleted */ |
| 4573 | u8 *data; /* pPage->aData */ |
| 4574 | u8 *ptr; /* Used to move bytes around within data[] */ |
| 4575 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4576 | assert( idx>=0 && idx<pPage->nCell ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4577 | assert( sz==cellSize(pPage, idx) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4578 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4579 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4580 | data = pPage->aData; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4581 | ptr = &data[pPage->cellOffset + 2*idx]; |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 4582 | pc = get2byte(ptr); |
| 4583 | if ( pc<=10 || pc+sz>pPage->pBt->usableSize ) { |
| 4584 | return SQLITE_CORRUPT_BKPT; |
| 4585 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4586 | freeSpace(pPage, pc, sz); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4587 | for(i=idx+1; i<pPage->nCell; i++, ptr+=2){ |
| 4588 | ptr[0] = ptr[2]; |
| 4589 | ptr[1] = ptr[3]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4590 | } |
| 4591 | pPage->nCell--; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4592 | put2byte(&data[pPage->hdrOffset+3], pPage->nCell); |
| 4593 | pPage->nFree += 2; |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 4594 | return SQLITE_OK; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4595 | } |
| 4596 | |
| 4597 | /* |
| 4598 | ** Insert a new cell on pPage at cell index "i". pCell points to the |
| 4599 | ** content of the cell. |
| 4600 | ** |
| 4601 | ** 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] | 4602 | ** will not fit, then make a copy of the cell content into pTemp if |
| 4603 | ** pTemp is not null. Regardless of pTemp, allocate a new entry |
| 4604 | ** in pPage->aOvfl[] and make it point to the cell content (either |
| 4605 | ** in pTemp or the original pCell) and also record its index. |
| 4606 | ** Allocating a new entry in pPage->aCell[] implies that |
| 4607 | ** pPage->nOverflow is incremented. |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4608 | ** |
| 4609 | ** If nSkip is non-zero, then do not copy the first nSkip bytes of the |
| 4610 | ** cell. The caller will overwrite them after this function returns. If |
drh | 4b238df | 2005-01-08 15:43:18 +0000 | [diff] [blame] | 4611 | ** 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] | 4612 | ** (but pCell+nSkip is always valid). |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4613 | */ |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4614 | static int insertCell( |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4615 | MemPage *pPage, /* Page into which we are copying */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4616 | int i, /* New cell becomes the i-th cell of the page */ |
| 4617 | u8 *pCell, /* Content of the new cell */ |
| 4618 | int sz, /* Bytes of content in pCell */ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4619 | u8 *pTemp, /* Temp storage space for pCell, if needed */ |
| 4620 | u8 nSkip /* Do not write the first nSkip bytes of the cell */ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4621 | ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4622 | int idx; /* Where to write new cell content in data[] */ |
| 4623 | int j; /* Loop counter */ |
| 4624 | int top; /* First byte of content for any cell in data[] */ |
| 4625 | int end; /* First byte past the last cell pointer in data[] */ |
| 4626 | int ins; /* Index in data[] where new cell pointer is inserted */ |
| 4627 | int hdr; /* Offset into data[] of the page header */ |
| 4628 | int cellOffset; /* Address of first cell pointer in data[] */ |
| 4629 | u8 *data; /* The content of the whole page */ |
| 4630 | u8 *ptr; /* Used for moving information around in data[] */ |
| 4631 | |
| 4632 | assert( i>=0 && i<=pPage->nCell+pPage->nOverflow ); |
| 4633 | assert( sz==cellSizePtr(pPage, pCell) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4634 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4635 | if( pPage->nOverflow || sz+2>pPage->nFree ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4636 | if( pTemp ){ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4637 | memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4638 | pCell = pTemp; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4639 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4640 | j = pPage->nOverflow++; |
| 4641 | assert( j<sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0]) ); |
| 4642 | pPage->aOvfl[j].pCell = pCell; |
| 4643 | pPage->aOvfl[j].idx = i; |
| 4644 | pPage->nFree = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4645 | }else{ |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4646 | int rc = sqlite3PagerWrite(pPage->pDbPage); |
| 4647 | if( rc!=SQLITE_OK ){ |
| 4648 | return rc; |
| 4649 | } |
| 4650 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4651 | data = pPage->aData; |
| 4652 | hdr = pPage->hdrOffset; |
| 4653 | top = get2byte(&data[hdr+5]); |
| 4654 | cellOffset = pPage->cellOffset; |
| 4655 | end = cellOffset + 2*pPage->nCell + 2; |
| 4656 | ins = cellOffset + 2*i; |
| 4657 | if( end > top - sz ){ |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 4658 | rc = defragmentPage(pPage); |
| 4659 | if( rc!=SQLITE_OK ){ |
| 4660 | return rc; |
| 4661 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4662 | top = get2byte(&data[hdr+5]); |
| 4663 | assert( end + sz <= top ); |
| 4664 | } |
| 4665 | idx = allocateSpace(pPage, sz); |
| 4666 | assert( idx>0 ); |
| 4667 | assert( end <= get2byte(&data[hdr+5]) ); |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 4668 | if (idx+sz > pPage->pBt->usableSize) { |
shane | 34ac18d | 2008-11-11 22:18:20 +0000 | [diff] [blame] | 4669 | return SQLITE_CORRUPT_BKPT; |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 4670 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4671 | pPage->nCell++; |
| 4672 | pPage->nFree -= 2; |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4673 | memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4674 | for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){ |
| 4675 | ptr[0] = ptr[-2]; |
| 4676 | ptr[1] = ptr[-1]; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4677 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4678 | put2byte(&data[ins], idx); |
| 4679 | put2byte(&data[hdr+3], pPage->nCell); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4680 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4681 | if( pPage->pBt->autoVacuum ){ |
| 4682 | /* The cell may contain a pointer to an overflow page. If so, write |
| 4683 | ** the entry for the overflow page into the pointer map. |
| 4684 | */ |
| 4685 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4686 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 4687 | assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4688 | if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ |
| 4689 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4690 | rc = ptrmapPut(pPage->pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4691 | if( rc!=SQLITE_OK ) return rc; |
| 4692 | } |
| 4693 | } |
| 4694 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4695 | } |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4696 | |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4697 | return SQLITE_OK; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4698 | } |
| 4699 | |
| 4700 | /* |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4701 | ** Add a list of cells to a page. The page should be initially empty. |
| 4702 | ** The cells are guaranteed to fit on the page. |
| 4703 | */ |
| 4704 | static void assemblePage( |
| 4705 | MemPage *pPage, /* The page to be assemblied */ |
| 4706 | int nCell, /* The number of cells to add to this page */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4707 | u8 **apCell, /* Pointers to cell bodies */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 4708 | u16 *aSize /* Sizes of the cells */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4709 | ){ |
| 4710 | int i; /* Loop counter */ |
| 4711 | int totalSize; /* Total size of all cells */ |
| 4712 | int hdr; /* Index of page header */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4713 | int cellptr; /* Address of next cell pointer */ |
| 4714 | int cellbody; /* Address of next cell body */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4715 | u8 *data; /* Data for the page */ |
| 4716 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4717 | assert( pPage->nOverflow==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4718 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4719 | totalSize = 0; |
| 4720 | for(i=0; i<nCell; i++){ |
| 4721 | totalSize += aSize[i]; |
| 4722 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4723 | assert( totalSize+2*nCell<=pPage->nFree ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4724 | assert( pPage->nCell==0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4725 | cellptr = pPage->cellOffset; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4726 | data = pPage->aData; |
| 4727 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4728 | put2byte(&data[hdr+3], nCell); |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 4729 | if( nCell ){ |
| 4730 | cellbody = allocateSpace(pPage, totalSize); |
| 4731 | assert( cellbody>0 ); |
| 4732 | assert( pPage->nFree >= 2*nCell ); |
| 4733 | pPage->nFree -= 2*nCell; |
| 4734 | for(i=0; i<nCell; i++){ |
| 4735 | put2byte(&data[cellptr], cellbody); |
| 4736 | memcpy(&data[cellbody], apCell[i], aSize[i]); |
| 4737 | cellptr += 2; |
| 4738 | cellbody += aSize[i]; |
| 4739 | } |
| 4740 | assert( cellbody==pPage->pBt->usableSize ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4741 | } |
| 4742 | pPage->nCell = nCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4743 | } |
| 4744 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4745 | /* |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4746 | ** The following parameters determine how many adjacent pages get involved |
| 4747 | ** in a balancing operation. NN is the number of neighbors on either side |
| 4748 | ** of the page that participate in the balancing operation. NB is the |
| 4749 | ** total number of pages that participate, including the target page and |
| 4750 | ** NN neighbors on either side. |
| 4751 | ** |
| 4752 | ** The minimum value of NN is 1 (of course). Increasing NN above 1 |
| 4753 | ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance |
| 4754 | ** in exchange for a larger degradation in INSERT and UPDATE performance. |
| 4755 | ** The value of NN appears to give the best results overall. |
| 4756 | */ |
| 4757 | #define NN 1 /* Number of neighbors on either side of pPage */ |
| 4758 | #define NB (NN*2+1) /* Total pages involved in the balance */ |
| 4759 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4760 | /* Forward reference */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4761 | static int balance(BtCursor*, int); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4762 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4763 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 4764 | /* |
| 4765 | ** This version of balance() handles the common special case where |
| 4766 | ** a new entry is being inserted on the extreme right-end of the |
| 4767 | ** tree, in other words, when the new entry will become the largest |
| 4768 | ** entry in the tree. |
| 4769 | ** |
| 4770 | ** Instead of trying balance the 3 right-most leaf pages, just add |
| 4771 | ** a new page to the right-hand side and put the one new entry in |
| 4772 | ** that page. This leaves the right side of the tree somewhat |
| 4773 | ** unbalanced. But odds are that we will be inserting new entries |
| 4774 | ** at the end soon afterwards so the nearly empty page will quickly |
| 4775 | ** fill up. On average. |
| 4776 | ** |
| 4777 | ** pPage is the leaf page which is the right-most page in the tree. |
| 4778 | ** pParent is its parent. pPage must have a single overflow entry |
| 4779 | ** which is also the right-most entry on the page. |
| 4780 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4781 | static int balance_quick(BtCursor *pCur){ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4782 | int rc; |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 4783 | MemPage *pNew = 0; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4784 | Pgno pgnoNew; |
| 4785 | u8 *pCell; |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 4786 | u16 szCell; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4787 | CellInfo info; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4788 | MemPage *pPage = pCur->apPage[pCur->iPage]; |
| 4789 | MemPage *pParent = pCur->apPage[pCur->iPage-1]; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4790 | BtShared *pBt = pPage->pBt; |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4791 | int parentIdx = pParent->nCell; /* pParent new divider cell index */ |
| 4792 | int parentSize; /* Size of new divider cell */ |
| 4793 | u8 parentCell[64]; /* Space for the new divider cell */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4794 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4795 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4796 | |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4797 | /* Allocate a new page. Insert the overflow cell from pPage |
| 4798 | ** into it. Then remove the overflow cell from pPage. |
| 4799 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4800 | rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0); |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 4801 | if( rc==SQLITE_OK ){ |
| 4802 | pCell = pPage->aOvfl[0].pCell; |
| 4803 | szCell = cellSizePtr(pPage, pCell); |
| 4804 | zeroPage(pNew, pPage->aData[0]); |
| 4805 | assemblePage(pNew, 1, &pCell, &szCell); |
| 4806 | pPage->nOverflow = 0; |
| 4807 | |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 4808 | /* pPage is currently the right-child of pParent. Change this |
| 4809 | ** so that the right-child is the new page allocated above and |
| 4810 | ** pPage is the next-to-right child. |
| 4811 | ** |
| 4812 | ** Ignore the return value of the call to fillInCell(). fillInCell() |
| 4813 | ** may only return other than SQLITE_OK if it is required to allocate |
| 4814 | ** one or more overflow pages. Since an internal table B-Tree cell |
| 4815 | ** may never spill over onto an overflow page (it is a maximum of |
| 4816 | ** 13 bytes in size), it is not neccessary to check the return code. |
| 4817 | ** |
| 4818 | ** Similarly, the insertCell() function cannot fail if the page |
| 4819 | ** being inserted into is already writable and the cell does not |
| 4820 | ** contain an overflow pointer. So ignore this return code too. |
| 4821 | */ |
| 4822 | assert( pPage->nCell>0 ); |
| 4823 | pCell = findCell(pPage, pPage->nCell-1); |
| 4824 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
| 4825 | fillInCell(pParent, parentCell, 0, info.nKey, 0, 0, 0, &parentSize); |
| 4826 | assert( parentSize<64 ); |
| 4827 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
| 4828 | insertCell(pParent, parentIdx, parentCell, parentSize, 0, 4); |
| 4829 | put4byte(findOverflowCell(pParent,parentIdx), pPage->pgno); |
| 4830 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew); |
| 4831 | |
| 4832 | /* If this is an auto-vacuum database, update the pointer map |
| 4833 | ** with entries for the new page, and any pointer from the |
| 4834 | ** cell on the page to an overflow page. |
| 4835 | */ |
| 4836 | if( ISAUTOVACUUM ){ |
| 4837 | rc = ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno); |
| 4838 | if( rc==SQLITE_OK ){ |
| 4839 | rc = ptrmapPutOvfl(pNew, 0); |
| 4840 | } |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4841 | } |
danielk1977 | e08a3c4 | 2008-09-18 18:17:03 +0000 | [diff] [blame] | 4842 | |
| 4843 | /* Release the reference to the new page. */ |
| 4844 | releasePage(pNew); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4845 | } |
| 4846 | |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 4847 | /* At this point the pPage->nFree variable is not set correctly with |
| 4848 | ** respect to the content of the page (because it was set to 0 by |
| 4849 | ** insertCell). So call sqlite3BtreeInitPage() to make sure it is |
| 4850 | ** correct. |
| 4851 | ** |
| 4852 | ** This has to be done even if an error will be returned. Normally, if |
| 4853 | ** an error occurs during tree balancing, the contents of MemPage are |
| 4854 | ** not important, as they will be recalculated when the page is rolled |
| 4855 | ** back. But here, in balance_quick(), it is possible that pPage has |
| 4856 | ** not yet been marked dirty or written into the journal file. Therefore |
| 4857 | ** it will not be rolled back and so it is important to make sure that |
| 4858 | ** the page data and contents of MemPage are consistent. |
| 4859 | */ |
| 4860 | pPage->isInit = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4861 | sqlite3BtreeInitPage(pPage); |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 4862 | |
danielk1977 | e08a3c4 | 2008-09-18 18:17:03 +0000 | [diff] [blame] | 4863 | /* If everything else succeeded, balance the parent page, in |
| 4864 | ** case the divider cell inserted caused it to become overfull. |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4865 | */ |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 4866 | if( rc==SQLITE_OK ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4867 | releasePage(pPage); |
| 4868 | pCur->iPage--; |
| 4869 | rc = balance(pCur, 0); |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 4870 | } |
| 4871 | return rc; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4872 | } |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4873 | #endif /* SQLITE_OMIT_QUICKBALANCE */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4874 | |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4875 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4876 | ** This routine redistributes Cells on pPage and up to NN*2 siblings |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4877 | ** 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] | 4878 | ** Usually NN siblings on either side of pPage is used in the balancing, |
| 4879 | ** though more siblings might come from one side if pPage is the first |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4880 | ** 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] | 4881 | ** (something which can only happen if pPage is the root page or a |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4882 | ** child of root) then all available siblings participate in the balancing. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4883 | ** |
drh | 0c6cc4e | 2004-06-15 02:13:26 +0000 | [diff] [blame] | 4884 | ** The number of siblings of pPage might be increased or decreased by one or |
| 4885 | ** 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] | 4886 | ** is special and is allowed to be nearly empty. If pPage is |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4887 | ** the root page, then the depth of the tree might be increased |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4888 | ** or decreased by one, as necessary, to keep the root page from being |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4889 | ** overfull or completely empty. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4890 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4891 | ** Note that when this routine is called, some of the Cells on pPage |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4892 | ** might not actually be stored in pPage->aData[]. This can happen |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4893 | ** 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] | 4894 | ** make sure all Cells for pPage once again fit in pPage->aData[]. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4895 | ** |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4896 | ** In the course of balancing the siblings of pPage, the parent of pPage |
| 4897 | ** might become overfull or underfull. If that happens, then this routine |
| 4898 | ** is called recursively on the parent. |
| 4899 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4900 | ** If this routine fails for any reason, it might leave the database |
| 4901 | ** in a corrupted state. So if this routine fails, the database should |
| 4902 | ** be rolled back. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4903 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4904 | static int balance_nonroot(BtCursor *pCur){ |
| 4905 | MemPage *pPage; /* The over or underfull page to balance */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4906 | MemPage *pParent; /* The parent of pPage */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4907 | BtShared *pBt; /* The whole database */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4908 | int nCell = 0; /* Number of cells in apCell[] */ |
| 4909 | int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4910 | int nOld; /* Number of pages in apOld[] */ |
| 4911 | int nNew; /* Number of pages in apNew[] */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4912 | int nDiv; /* Number of cells in apDiv[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4913 | int i, j, k; /* Loop counters */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4914 | int idx; /* Index of pPage in pParent->aCell[] */ |
| 4915 | int nxDiv; /* Next divider slot in pParent->aCell[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4916 | int rc; /* The return code */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4917 | int leafCorrection; /* 4 if pPage is a leaf. 0 if not */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4918 | int leafData; /* True if pPage is a leaf of a LEAFDATA tree */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4919 | int usableSpace; /* Bytes in pPage beyond the header */ |
| 4920 | int pageFlags; /* Value of pPage->aData[0] */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4921 | int subtotal; /* Subtotal of bytes in cells on one page */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 4922 | int iSpace1 = 0; /* First unused byte of aSpace1[] */ |
| 4923 | int iSpace2 = 0; /* First unused byte of aSpace2[] */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 4924 | int szScratch; /* Size of scratch memory requested */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4925 | MemPage *apOld[NB]; /* pPage and up to two siblings */ |
| 4926 | Pgno pgnoOld[NB]; /* Page numbers for each page in apOld[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4927 | MemPage *apCopy[NB]; /* Private copies of apOld[] pages */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4928 | MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ |
| 4929 | Pgno pgnoNew[NB+2]; /* Page numbers for each page in apNew[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4930 | u8 *apDiv[NB]; /* Divider cells in pParent */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4931 | int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */ |
| 4932 | int szNew[NB+2]; /* Combined size of cells place on i-th page */ |
danielk1977 | 50f059b | 2005-03-29 02:54:03 +0000 | [diff] [blame] | 4933 | u8 **apCell = 0; /* All cells begin balanced */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 4934 | u16 *szCell; /* Local size of all cells in apCell[] */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 4935 | u8 *aCopy[NB]; /* Space for holding data of apCopy[] */ |
| 4936 | u8 *aSpace1; /* Space for copies of dividers cells before balance */ |
| 4937 | u8 *aSpace2 = 0; /* Space for overflow dividers cells after balance */ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4938 | u8 *aFrom = 0; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4939 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4940 | pPage = pCur->apPage[pCur->iPage]; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4941 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 4942 | VVA_ONLY( pCur->pagesShuffled = 1 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4943 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4944 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4945 | ** Find the parent page. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4946 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4947 | assert( pCur->iPage>0 ); |
| 4948 | assert( pPage->isInit ); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4949 | assert( sqlite3PagerIswriteable(pPage->pDbPage) || pPage->nOverflow==1 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4950 | pBt = pPage->pBt; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4951 | pParent = pCur->apPage[pCur->iPage-1]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4952 | assert( pParent ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4953 | if( SQLITE_OK!=(rc = sqlite3PagerWrite(pParent->pDbPage)) ){ |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 4954 | return rc; |
| 4955 | } |
danielk1977 | 474b7cc | 2008-07-09 11:49:46 +0000 | [diff] [blame] | 4956 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4957 | TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno)); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4958 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4959 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 4960 | /* |
| 4961 | ** A special case: If a new entry has just been inserted into a |
| 4962 | ** 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] | 4963 | ** 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] | 4964 | ** largest key) then use the special balance_quick() routine for |
| 4965 | ** balancing. balance_quick() is much faster and results in a tighter |
| 4966 | ** packing of data in the common case. |
| 4967 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4968 | if( pPage->leaf && |
| 4969 | pPage->intKey && |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4970 | pPage->nOverflow==1 && |
| 4971 | pPage->aOvfl[0].idx==pPage->nCell && |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4972 | pParent->pgno!=1 && |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4973 | get4byte(&pParent->aData[pParent->hdrOffset+8])==pPage->pgno |
| 4974 | ){ |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 4975 | assert( pPage->intKey ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4976 | /* |
| 4977 | ** TODO: Check the siblings to the left of pPage. It may be that |
| 4978 | ** they are not full and no new page is required. |
| 4979 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4980 | return balance_quick(pCur); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4981 | } |
| 4982 | #endif |
| 4983 | |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4984 | if( SQLITE_OK!=(rc = sqlite3PagerWrite(pPage->pDbPage)) ){ |
| 4985 | return rc; |
| 4986 | } |
| 4987 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4988 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4989 | ** Find the cell in the parent page whose left child points back |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4990 | ** to pPage. The "idx" variable is the index of that cell. If pPage |
| 4991 | ** is the rightmost child of pParent then set idx to pParent->nCell |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4992 | */ |
danielk1977 | bf93c56 | 2008-09-29 15:53:25 +0000 | [diff] [blame] | 4993 | idx = pCur->aiIdx[pCur->iPage-1]; |
| 4994 | assertParentIndex(pParent, idx, pPage->pgno); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4995 | |
| 4996 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4997 | ** Initialize variables so that it will be safe to jump |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4998 | ** directly to balance_cleanup at any moment. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4999 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5000 | nOld = nNew = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5001 | |
| 5002 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5003 | ** Find sibling pages to pPage and the cells in pParent that divide |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5004 | ** the siblings. An attempt is made to find NN siblings on either |
| 5005 | ** side of pPage. More siblings are taken from one side, however, if |
| 5006 | ** pPage there are fewer than NN siblings on the other side. If pParent |
| 5007 | ** has NB or fewer children then all children of pParent are taken. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5008 | */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5009 | nxDiv = idx - NN; |
| 5010 | if( nxDiv + NB > pParent->nCell ){ |
| 5011 | nxDiv = pParent->nCell - NB + 1; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5012 | } |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5013 | if( nxDiv<0 ){ |
| 5014 | nxDiv = 0; |
| 5015 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5016 | nDiv = 0; |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5017 | for(i=0, k=nxDiv; i<NB; i++, k++){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5018 | if( k<pParent->nCell ){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5019 | apDiv[i] = findCell(pParent, k); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5020 | nDiv++; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5021 | assert( !pParent->leaf ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5022 | pgnoOld[i] = get4byte(apDiv[i]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5023 | }else if( k==pParent->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5024 | pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+8]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5025 | }else{ |
| 5026 | break; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5027 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5028 | rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i]); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5029 | if( rc ) goto balance_cleanup; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5030 | /* apOld[i]->idxParent = k; */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5031 | apCopy[i] = 0; |
| 5032 | assert( i==nOld ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5033 | nOld++; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5034 | nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5035 | } |
| 5036 | |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5037 | /* Make nMaxCells a multiple of 4 in order to preserve 8-byte |
drh | 8d97f1f | 2005-05-05 18:14:13 +0000 | [diff] [blame] | 5038 | ** alignment */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5039 | nMaxCells = (nMaxCells + 3)&~3; |
drh | 8d97f1f | 2005-05-05 18:14:13 +0000 | [diff] [blame] | 5040 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5041 | /* |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5042 | ** Allocate space for memory structures |
| 5043 | */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5044 | szScratch = |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5045 | nMaxCells*sizeof(u8*) /* apCell */ |
| 5046 | + nMaxCells*sizeof(u16) /* szCell */ |
| 5047 | + (ROUND8(sizeof(MemPage))+pBt->pageSize)*NB /* aCopy */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5048 | + pBt->pageSize /* aSpace1 */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5049 | + (ISAUTOVACUUM ? nMaxCells : 0); /* aFrom */ |
| 5050 | apCell = sqlite3ScratchMalloc( szScratch ); |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5051 | if( apCell==0 ){ |
| 5052 | rc = SQLITE_NOMEM; |
| 5053 | goto balance_cleanup; |
| 5054 | } |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5055 | szCell = (u16*)&apCell[nMaxCells]; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5056 | aCopy[0] = (u8*)&szCell[nMaxCells]; |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 5057 | assert( ((aCopy[0] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5058 | for(i=1; i<NB; i++){ |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 5059 | aCopy[i] = &aCopy[i-1][pBt->pageSize+ROUND8(sizeof(MemPage))]; |
| 5060 | assert( ((aCopy[i] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5061 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5062 | aSpace1 = &aCopy[NB-1][pBt->pageSize+ROUND8(sizeof(MemPage))]; |
| 5063 | assert( ((aSpace1 - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5064 | if( ISAUTOVACUUM ){ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5065 | aFrom = &aSpace1[pBt->pageSize]; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5066 | } |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5067 | aSpace2 = sqlite3PageMalloc(pBt->pageSize); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5068 | if( aSpace2==0 ){ |
| 5069 | rc = SQLITE_NOMEM; |
| 5070 | goto balance_cleanup; |
| 5071 | } |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5072 | |
| 5073 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5074 | ** Make copies of the content of pPage and its siblings into aOld[]. |
| 5075 | ** The rest of this function will use data from the copies rather |
| 5076 | ** that the original pages since the original pages will be in the |
| 5077 | ** process of being overwritten. |
| 5078 | */ |
| 5079 | for(i=0; i<nOld; i++){ |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 5080 | MemPage *p = apCopy[i] = (MemPage*)aCopy[i]; |
| 5081 | memcpy(p, apOld[i], sizeof(MemPage)); |
| 5082 | p->aData = (void*)&p[1]; |
| 5083 | memcpy(p->aData, apOld[i]->aData, pBt->pageSize); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5084 | } |
| 5085 | |
| 5086 | /* |
| 5087 | ** Load pointers to all cells on sibling pages and the divider cells |
| 5088 | ** into the local apCell[] array. Make copies of the divider cells |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5089 | ** into space obtained form aSpace1[] and remove the the divider Cells |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5090 | ** from pParent. |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5091 | ** |
| 5092 | ** If the siblings are on leaf pages, then the child pointers of the |
| 5093 | ** divider cells are stripped from the cells before they are copied |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5094 | ** into aSpace1[]. In this way, all cells in apCell[] are without |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5095 | ** child pointers. If siblings are not leaves, then all cell in |
| 5096 | ** apCell[] include child pointers. Either way, all cells in apCell[] |
| 5097 | ** are alike. |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5098 | ** |
| 5099 | ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf. |
| 5100 | ** leafData: 1 if pPage holds key+data and pParent holds only keys. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5101 | */ |
| 5102 | nCell = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5103 | leafCorrection = pPage->leaf*4; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 5104 | leafData = pPage->hasData; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5105 | for(i=0; i<nOld; i++){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5106 | MemPage *pOld = apCopy[i]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5107 | int limit = pOld->nCell+pOld->nOverflow; |
| 5108 | for(j=0; j<limit; j++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5109 | assert( nCell<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5110 | apCell[nCell] = findOverflowCell(pOld, j); |
| 5111 | szCell[nCell] = cellSizePtr(pOld, apCell[nCell]); |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5112 | if( ISAUTOVACUUM ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5113 | int a; |
| 5114 | aFrom[nCell] = i; |
| 5115 | for(a=0; a<pOld->nOverflow; a++){ |
| 5116 | if( pOld->aOvfl[a].pCell==apCell[nCell] ){ |
| 5117 | aFrom[nCell] = 0xFF; |
| 5118 | break; |
| 5119 | } |
| 5120 | } |
| 5121 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5122 | nCell++; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5123 | } |
| 5124 | if( i<nOld-1 ){ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5125 | u16 sz = cellSizePtr(pParent, apDiv[i]); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5126 | if( leafData ){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5127 | /* With the LEAFDATA flag, pParent cells hold only INTKEYs that |
| 5128 | ** are duplicates of keys on the child pages. We need to remove |
| 5129 | ** the divider cells from pParent, but the dividers cells are not |
| 5130 | ** added to apCell[] because they are duplicates of child cells. |
| 5131 | */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5132 | dropCell(pParent, nxDiv, sz); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5133 | }else{ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5134 | u8 *pTemp; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5135 | assert( nCell<nMaxCells ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5136 | szCell[nCell] = sz; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5137 | pTemp = &aSpace1[iSpace1]; |
| 5138 | iSpace1 += sz; |
| 5139 | assert( sz<=pBt->pageSize/4 ); |
| 5140 | assert( iSpace1<=pBt->pageSize ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5141 | memcpy(pTemp, apDiv[i], sz); |
| 5142 | apCell[nCell] = pTemp+leafCorrection; |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5143 | if( ISAUTOVACUUM ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5144 | aFrom[nCell] = 0xFF; |
| 5145 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5146 | dropCell(pParent, nxDiv, sz); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5147 | szCell[nCell] -= leafCorrection; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5148 | assert( get4byte(pTemp)==pgnoOld[i] ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5149 | if( !pOld->leaf ){ |
| 5150 | assert( leafCorrection==0 ); |
| 5151 | /* The right pointer of the child page pOld becomes the left |
| 5152 | ** pointer of the divider cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5153 | memcpy(apCell[nCell], &pOld->aData[pOld->hdrOffset+8], 4); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5154 | }else{ |
| 5155 | assert( leafCorrection==4 ); |
danielk1977 | 39c9604 | 2007-05-12 10:41:47 +0000 | [diff] [blame] | 5156 | if( szCell[nCell]<4 ){ |
| 5157 | /* Do not allow any cells smaller than 4 bytes. */ |
| 5158 | szCell[nCell] = 4; |
| 5159 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5160 | } |
| 5161 | nCell++; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5162 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5163 | } |
| 5164 | } |
| 5165 | |
| 5166 | /* |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5167 | ** Figure out the number of pages needed to hold all nCell cells. |
| 5168 | ** Store this number in "k". Also compute szNew[] which is the total |
| 5169 | ** 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] | 5170 | ** in apCell[] of the cell that divides page i from page i+1. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5171 | ** cntNew[k] should equal nCell. |
| 5172 | ** |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5173 | ** Values computed by this block: |
| 5174 | ** |
| 5175 | ** k: The total number of sibling pages |
| 5176 | ** szNew[i]: Spaced used on the i-th sibling page. |
| 5177 | ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to |
| 5178 | ** the right of the i-th sibling page. |
| 5179 | ** usableSpace: Number of bytes of space available on each sibling. |
| 5180 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5181 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5182 | usableSpace = pBt->usableSize - 12 + leafCorrection; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5183 | for(subtotal=k=i=0; i<nCell; i++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5184 | assert( i<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5185 | subtotal += szCell[i] + 2; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5186 | if( subtotal > usableSpace ){ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5187 | szNew[k] = subtotal - szCell[i]; |
| 5188 | cntNew[k] = i; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5189 | if( leafData ){ i--; } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5190 | subtotal = 0; |
| 5191 | k++; |
| 5192 | } |
| 5193 | } |
| 5194 | szNew[k] = subtotal; |
| 5195 | cntNew[k] = nCell; |
| 5196 | k++; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5197 | |
| 5198 | /* |
| 5199 | ** The packing computed by the previous block is biased toward the siblings |
| 5200 | ** on the left side. The left siblings are always nearly full, while the |
| 5201 | ** right-most sibling might be nearly empty. This block of code attempts |
| 5202 | ** to adjust the packing of siblings to get a better balance. |
| 5203 | ** |
| 5204 | ** This adjustment is more than an optimization. The packing above might |
| 5205 | ** be so out of balance as to be illegal. For example, the right-most |
| 5206 | ** sibling might be completely empty. This adjustment is not optional. |
| 5207 | */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5208 | for(i=k-1; i>0; i--){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5209 | int szRight = szNew[i]; /* Size of sibling on the right */ |
| 5210 | int szLeft = szNew[i-1]; /* Size of sibling on the left */ |
| 5211 | int r; /* Index of right-most cell in left sibling */ |
| 5212 | int d; /* Index of first cell to the left of right sibling */ |
| 5213 | |
| 5214 | r = cntNew[i-1] - 1; |
| 5215 | d = r + 1 - leafData; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5216 | assert( d<nMaxCells ); |
| 5217 | assert( r<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5218 | while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){ |
| 5219 | szRight += szCell[d] + 2; |
| 5220 | szLeft -= szCell[r] + 2; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5221 | cntNew[i-1]--; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5222 | r = cntNew[i-1] - 1; |
| 5223 | d = r + 1 - leafData; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5224 | } |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5225 | szNew[i] = szRight; |
| 5226 | szNew[i-1] = szLeft; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5227 | } |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 5228 | |
| 5229 | /* Either we found one or more cells (cntnew[0])>0) or we are the |
| 5230 | ** a virtual root page. A virtual root page is when the real root |
| 5231 | ** page is page 1 and we are the only child of that page. |
| 5232 | */ |
| 5233 | assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5234 | |
| 5235 | /* |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5236 | ** Allocate k new pages. Reuse old pages where possible. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5237 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5238 | assert( pPage->pgno>1 ); |
| 5239 | pageFlags = pPage->aData[0]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5240 | for(i=0; i<k; i++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5241 | MemPage *pNew; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5242 | if( i<nOld ){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5243 | pNew = apNew[i] = apOld[i]; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5244 | pgnoNew[i] = pgnoOld[i]; |
| 5245 | apOld[i] = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5246 | rc = sqlite3PagerWrite(pNew->pDbPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 5247 | nNew++; |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 5248 | if( rc ) goto balance_cleanup; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5249 | }else{ |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 5250 | assert( i>0 ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5251 | rc = allocateBtreePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5252 | if( rc ) goto balance_cleanup; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5253 | apNew[i] = pNew; |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 5254 | nNew++; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5255 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5256 | } |
| 5257 | |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5258 | /* Free any old pages that were not reused as new pages. |
| 5259 | */ |
| 5260 | while( i<nOld ){ |
| 5261 | rc = freePage(apOld[i]); |
| 5262 | if( rc ) goto balance_cleanup; |
| 5263 | releasePage(apOld[i]); |
| 5264 | apOld[i] = 0; |
| 5265 | i++; |
| 5266 | } |
| 5267 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5268 | /* |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5269 | ** Put the new pages in accending order. This helps to |
| 5270 | ** keep entries in the disk file in order so that a scan |
| 5271 | ** of the table is a linear scan through the file. That |
| 5272 | ** in turn helps the operating system to deliver pages |
| 5273 | ** from the disk more rapidly. |
| 5274 | ** |
| 5275 | ** An O(n^2) insertion sort algorithm is used, but since |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5276 | ** n is never more than NB (a small constant), that should |
| 5277 | ** not be a problem. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5278 | ** |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5279 | ** When NB==3, this one optimization makes the database |
| 5280 | ** about 25% faster for large insertions and deletions. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5281 | */ |
| 5282 | for(i=0; i<k-1; i++){ |
| 5283 | int minV = pgnoNew[i]; |
| 5284 | int minI = i; |
| 5285 | for(j=i+1; j<k; j++){ |
drh | 7d02cb7 | 2003-06-04 16:24:39 +0000 | [diff] [blame] | 5286 | if( pgnoNew[j]<(unsigned)minV ){ |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5287 | minI = j; |
| 5288 | minV = pgnoNew[j]; |
| 5289 | } |
| 5290 | } |
| 5291 | if( minI>i ){ |
| 5292 | int t; |
| 5293 | MemPage *pT; |
| 5294 | t = pgnoNew[i]; |
| 5295 | pT = apNew[i]; |
| 5296 | pgnoNew[i] = pgnoNew[minI]; |
| 5297 | apNew[i] = apNew[minI]; |
| 5298 | pgnoNew[minI] = t; |
| 5299 | apNew[minI] = pT; |
| 5300 | } |
| 5301 | } |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5302 | 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] | 5303 | pgnoOld[0], |
| 5304 | nOld>=2 ? pgnoOld[1] : 0, |
| 5305 | nOld>=3 ? pgnoOld[2] : 0, |
drh | 10c0fa6 | 2004-05-18 12:50:17 +0000 | [diff] [blame] | 5306 | pgnoNew[0], szNew[0], |
| 5307 | nNew>=2 ? pgnoNew[1] : 0, nNew>=2 ? szNew[1] : 0, |
| 5308 | nNew>=3 ? pgnoNew[2] : 0, nNew>=3 ? szNew[2] : 0, |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5309 | nNew>=4 ? pgnoNew[3] : 0, nNew>=4 ? szNew[3] : 0, |
| 5310 | nNew>=5 ? pgnoNew[4] : 0, nNew>=5 ? szNew[4] : 0)); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5311 | |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5312 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5313 | ** Evenly distribute the data in apCell[] across the new pages. |
| 5314 | ** Insert divider cells into pParent as necessary. |
| 5315 | */ |
| 5316 | j = 0; |
| 5317 | for(i=0; i<nNew; i++){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5318 | /* Assemble the new sibling page. */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5319 | MemPage *pNew = apNew[i]; |
drh | 19642e5 | 2005-03-29 13:17:45 +0000 | [diff] [blame] | 5320 | assert( j<nMaxCells ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5321 | assert( pNew->pgno==pgnoNew[i] ); |
drh | 1013148 | 2008-07-11 03:34:09 +0000 | [diff] [blame] | 5322 | zeroPage(pNew, pageFlags); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 5323 | assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]); |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 5324 | assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5325 | assert( pNew->nOverflow==0 ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5326 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5327 | /* If this is an auto-vacuum database, update the pointer map entries |
| 5328 | ** that point to the siblings that were rearranged. These can be: left |
| 5329 | ** children of cells, the right-child of the page, or overflow pages |
| 5330 | ** pointed to by cells. |
| 5331 | */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5332 | if( ISAUTOVACUUM ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5333 | for(k=j; k<cntNew[i]; k++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5334 | assert( k<nMaxCells ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5335 | if( aFrom[k]==0xFF || apCopy[aFrom[k]]->pgno!=pNew->pgno ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5336 | rc = ptrmapPutOvfl(pNew, k-j); |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5337 | if( rc==SQLITE_OK && leafCorrection==0 ){ |
| 5338 | rc = ptrmapPut(pBt, get4byte(apCell[k]), PTRMAP_BTREE, pNew->pgno); |
| 5339 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5340 | if( rc!=SQLITE_OK ){ |
| 5341 | goto balance_cleanup; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5342 | } |
| 5343 | } |
| 5344 | } |
| 5345 | } |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5346 | |
| 5347 | j = cntNew[i]; |
| 5348 | |
| 5349 | /* If the sibling page assembled above was not the right-most sibling, |
| 5350 | ** insert a divider cell into the parent page. |
| 5351 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5352 | if( i<nNew-1 && j<nCell ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5353 | u8 *pCell; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5354 | u8 *pTemp; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5355 | int sz; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5356 | |
| 5357 | assert( j<nMaxCells ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5358 | pCell = apCell[j]; |
| 5359 | sz = szCell[j] + leafCorrection; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5360 | pTemp = &aSpace2[iSpace2]; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5361 | if( !pNew->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5362 | memcpy(&pNew->aData[8], pCell, 4); |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5363 | if( ISAUTOVACUUM |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5364 | && (aFrom[j]==0xFF || apCopy[aFrom[j]]->pgno!=pNew->pgno) |
| 5365 | ){ |
| 5366 | rc = ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno); |
| 5367 | if( rc!=SQLITE_OK ){ |
| 5368 | goto balance_cleanup; |
| 5369 | } |
| 5370 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5371 | }else if( leafData ){ |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 5372 | /* If the tree is a leaf-data tree, and the siblings are leaves, |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5373 | ** then there is no divider cell in apCell[]. Instead, the divider |
| 5374 | ** cell consists of the integer key for the right-most cell of |
| 5375 | ** the sibling-page assembled above only. |
| 5376 | */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5377 | CellInfo info; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5378 | j--; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5379 | sqlite3BtreeParseCellPtr(pNew, apCell[j], &info); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5380 | pCell = pTemp; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5381 | fillInCell(pParent, pCell, 0, info.nKey, 0, 0, 0, &sz); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5382 | pTemp = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5383 | }else{ |
| 5384 | pCell -= 4; |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 5385 | /* Obscure case for non-leaf-data trees: If the cell at pCell was |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 5386 | ** previously stored on a leaf node, and its reported size was 4 |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 5387 | ** bytes, then it may actually be smaller than this |
| 5388 | ** (see sqlite3BtreeParseCellPtr(), 4 bytes is the minimum size of |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 5389 | ** any cell). But it is important to pass the correct size to |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 5390 | ** insertCell(), so reparse the cell now. |
| 5391 | ** |
| 5392 | ** Note that this can never happen in an SQLite data file, as all |
| 5393 | ** cells are at least 4 bytes. It only happens in b-trees used |
| 5394 | ** to evaluate "IN (SELECT ...)" and similar clauses. |
| 5395 | */ |
| 5396 | if( szCell[j]==4 ){ |
| 5397 | assert(leafCorrection==4); |
| 5398 | sz = cellSizePtr(pParent, pCell); |
| 5399 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5400 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5401 | iSpace2 += sz; |
| 5402 | assert( sz<=pBt->pageSize/4 ); |
| 5403 | assert( iSpace2<=pBt->pageSize ); |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 5404 | rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4); |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 5405 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5406 | put4byte(findOverflowCell(pParent,nxDiv), pNew->pgno); |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5407 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5408 | /* If this is an auto-vacuum database, and not a leaf-data tree, |
| 5409 | ** then update the pointer map with an entry for the overflow page |
| 5410 | ** that the cell just inserted points to (if any). |
| 5411 | */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5412 | if( ISAUTOVACUUM && !leafData ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5413 | rc = ptrmapPutOvfl(pParent, nxDiv); |
| 5414 | if( rc!=SQLITE_OK ){ |
| 5415 | goto balance_cleanup; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5416 | } |
| 5417 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5418 | j++; |
| 5419 | nxDiv++; |
| 5420 | } |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5421 | |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5422 | /* Set the pointer-map entry for the new sibling page. */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5423 | if( ISAUTOVACUUM ){ |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5424 | rc = ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno); |
| 5425 | if( rc!=SQLITE_OK ){ |
| 5426 | goto balance_cleanup; |
| 5427 | } |
| 5428 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5429 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5430 | assert( j==nCell ); |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 5431 | assert( nOld>0 ); |
| 5432 | assert( nNew>0 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5433 | if( (pageFlags & PTF_LEAF)==0 ){ |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5434 | u8 *zChild = &apCopy[nOld-1]->aData[8]; |
| 5435 | memcpy(&apNew[nNew-1]->aData[8], zChild, 4); |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5436 | if( ISAUTOVACUUM ){ |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5437 | rc = ptrmapPut(pBt, get4byte(zChild), PTRMAP_BTREE, apNew[nNew-1]->pgno); |
| 5438 | if( rc!=SQLITE_OK ){ |
| 5439 | goto balance_cleanup; |
| 5440 | } |
| 5441 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5442 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5443 | if( nxDiv==pParent->nCell+pParent->nOverflow ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5444 | /* Right-most sibling is the right-most child of pParent */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5445 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew[nNew-1]); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5446 | }else{ |
| 5447 | /* Right-most sibling is the left child of the first entry in pParent |
| 5448 | ** past the right-most divider entry */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5449 | put4byte(findOverflowCell(pParent, nxDiv), pgnoNew[nNew-1]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5450 | } |
| 5451 | |
| 5452 | /* |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5453 | ** Balance the parent page. Note that the current page (pPage) might |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5454 | ** have been added to the freelist so it might no longer be initialized. |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5455 | ** But the parent page will always be initialized. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5456 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5457 | assert( pParent->isInit ); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5458 | sqlite3ScratchFree(apCell); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5459 | apCell = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5460 | releasePage(pPage); |
| 5461 | pCur->iPage--; |
| 5462 | rc = balance(pCur, 0); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5463 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5464 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5465 | ** Cleanup before returning. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5466 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5467 | balance_cleanup: |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5468 | sqlite3PageFree(aSpace2); |
| 5469 | sqlite3ScratchFree(apCell); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5470 | for(i=0; i<nOld; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5471 | releasePage(apOld[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5472 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5473 | for(i=0; i<nNew; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5474 | releasePage(apNew[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5475 | } |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 5476 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5477 | /* releasePage(pParent); */ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5478 | TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n", |
| 5479 | pPage->pgno, nOld, nNew, nCell)); |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 5480 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5481 | return rc; |
| 5482 | } |
| 5483 | |
| 5484 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5485 | ** This routine is called for the root page of a btree when the root |
| 5486 | ** page contains no cells. This is an opportunity to make the tree |
| 5487 | ** shallower by one level. |
| 5488 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5489 | static int balance_shallower(BtCursor *pCur){ |
| 5490 | MemPage *pPage; /* Root page of B-Tree */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5491 | MemPage *pChild; /* The only child page of pPage */ |
| 5492 | Pgno pgnoChild; /* Page number for pChild */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5493 | int rc = SQLITE_OK; /* Return code from subprocedures */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5494 | BtShared *pBt; /* The main BTree structure */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5495 | int mxCellPerPage; /* Maximum number of cells per page */ |
| 5496 | u8 **apCell; /* All cells from pages being balanced */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5497 | u16 *szCell; /* Local size of all cells */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5498 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5499 | assert( pCur->iPage==0 ); |
| 5500 | pPage = pCur->apPage[0]; |
| 5501 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5502 | assert( pPage->nCell==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5503 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5504 | pBt = pPage->pBt; |
| 5505 | mxCellPerPage = MX_CELL(pBt); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5506 | apCell = sqlite3Malloc( mxCellPerPage*(sizeof(u8*)+sizeof(u16)) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5507 | if( apCell==0 ) return SQLITE_NOMEM; |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5508 | szCell = (u16*)&apCell[mxCellPerPage]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5509 | if( pPage->leaf ){ |
| 5510 | /* The table is completely empty */ |
| 5511 | TRACE(("BALANCE: empty table %d\n", pPage->pgno)); |
| 5512 | }else{ |
| 5513 | /* The root page is empty but has one child. Transfer the |
| 5514 | ** information from that one child into the root page if it |
| 5515 | ** will fit. This reduces the depth of the tree by one. |
| 5516 | ** |
| 5517 | ** If the root page is page 1, it has less space available than |
| 5518 | ** its child (due to the 100 byte header that occurs at the beginning |
| 5519 | ** of the database fle), so it might not be able to hold all of the |
| 5520 | ** information currently contained in the child. If this is the |
| 5521 | ** case, then do not do the transfer. Leave page 1 empty except |
| 5522 | ** for the right-pointer to the child page. The child page becomes |
| 5523 | ** the virtual root of the tree. |
| 5524 | */ |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 5525 | VVA_ONLY( pCur->pagesShuffled = 1 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5526 | pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 5527 | assert( pgnoChild>0 ); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 5528 | assert( pgnoChild<=pagerPagecount(pPage->pBt->pPager) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5529 | rc = sqlite3BtreeGetPage(pPage->pBt, pgnoChild, &pChild, 0); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5530 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5531 | if( pPage->pgno==1 ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5532 | rc = sqlite3BtreeInitPage(pChild); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5533 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5534 | assert( pChild->nOverflow==0 ); |
| 5535 | if( pChild->nFree>=100 ){ |
| 5536 | /* The child information will fit on the root page, so do the |
| 5537 | ** copy */ |
| 5538 | int i; |
| 5539 | zeroPage(pPage, pChild->aData[0]); |
| 5540 | for(i=0; i<pChild->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5541 | apCell[i] = findCell(pChild,i); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5542 | szCell[i] = cellSizePtr(pChild, apCell[i]); |
| 5543 | } |
| 5544 | assemblePage(pPage, pChild->nCell, apCell, szCell); |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 5545 | /* Copy the right-pointer of the child to the parent. */ |
| 5546 | put4byte(&pPage->aData[pPage->hdrOffset+8], |
| 5547 | get4byte(&pChild->aData[pChild->hdrOffset+8])); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5548 | freePage(pChild); |
| 5549 | TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno)); |
| 5550 | }else{ |
| 5551 | /* The child has more information that will fit on the root. |
| 5552 | ** The tree is already balanced. Do nothing. */ |
| 5553 | TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno)); |
| 5554 | } |
| 5555 | }else{ |
| 5556 | memcpy(pPage->aData, pChild->aData, pPage->pBt->usableSize); |
| 5557 | pPage->isInit = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5558 | rc = sqlite3BtreeInitPage(pPage); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5559 | assert( rc==SQLITE_OK ); |
| 5560 | freePage(pChild); |
| 5561 | TRACE(("BALANCE: transfer child %d into root %d\n", |
| 5562 | pChild->pgno, pPage->pgno)); |
| 5563 | } |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5564 | assert( pPage->nOverflow==0 ); |
shane | 831c329 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 5565 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5566 | if( ISAUTOVACUUM ){ |
danielk1977 | 00a696d | 2008-09-29 16:41:31 +0000 | [diff] [blame] | 5567 | rc = setChildPtrmaps(pPage); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5568 | } |
shane | 831c329 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 5569 | #endif |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5570 | releasePage(pChild); |
| 5571 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5572 | end_shallow_balance: |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5573 | sqlite3_free(apCell); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5574 | return rc; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5575 | } |
| 5576 | |
| 5577 | |
| 5578 | /* |
| 5579 | ** The root page is overfull |
| 5580 | ** |
| 5581 | ** When this happens, Create a new child page and copy the |
| 5582 | ** contents of the root into the child. Then make the root |
| 5583 | ** page an empty page with rightChild pointing to the new |
| 5584 | ** child. Finally, call balance_internal() on the new child |
| 5585 | ** to cause it to split. |
| 5586 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5587 | static int balance_deeper(BtCursor *pCur){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5588 | int rc; /* Return value from subprocedures */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5589 | MemPage *pPage; /* Pointer to the root page */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5590 | MemPage *pChild; /* Pointer to a new child page */ |
| 5591 | Pgno pgnoChild; /* Page number of the new child page */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5592 | BtShared *pBt; /* The BTree */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5593 | int usableSize; /* Total usable size of a page */ |
| 5594 | u8 *data; /* Content of the parent page */ |
| 5595 | u8 *cdata; /* Content of the child page */ |
| 5596 | int hdr; /* Offset to page header in parent */ |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 5597 | int cbrk; /* Offset to content of first cell in parent */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5598 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5599 | assert( pCur->iPage==0 ); |
| 5600 | assert( pCur->apPage[0]->nOverflow>0 ); |
| 5601 | |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 5602 | VVA_ONLY( pCur->pagesShuffled = 1 ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5603 | pPage = pCur->apPage[0]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5604 | pBt = pPage->pBt; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5605 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5606 | rc = allocateBtreePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5607 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5608 | assert( sqlite3PagerIswriteable(pChild->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5609 | usableSize = pBt->usableSize; |
| 5610 | data = pPage->aData; |
| 5611 | hdr = pPage->hdrOffset; |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 5612 | cbrk = get2byte(&data[hdr+5]); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5613 | cdata = pChild->aData; |
| 5614 | memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr); |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 5615 | memcpy(&cdata[cbrk], &data[cbrk], usableSize-cbrk); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5616 | |
| 5617 | rc = sqlite3BtreeInitPage(pChild); |
| 5618 | if( rc==SQLITE_OK ){ |
| 5619 | int nCopy = pPage->nOverflow*sizeof(pPage->aOvfl[0]); |
| 5620 | memcpy(pChild->aOvfl, pPage->aOvfl, nCopy); |
| 5621 | pChild->nOverflow = pPage->nOverflow; |
| 5622 | if( pChild->nOverflow ){ |
| 5623 | pChild->nFree = 0; |
| 5624 | } |
| 5625 | assert( pChild->nCell==pPage->nCell ); |
| 5626 | zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF); |
| 5627 | put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild); |
| 5628 | TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno)); |
| 5629 | if( ISAUTOVACUUM ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5630 | rc = ptrmapPut(pBt, pChild->pgno, PTRMAP_BTREE, pPage->pgno); |
shane | 831c329 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 5631 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5632 | if( rc==SQLITE_OK ){ |
danielk1977 | 00a696d | 2008-09-29 16:41:31 +0000 | [diff] [blame] | 5633 | rc = setChildPtrmaps(pChild); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5634 | } |
shane | 831c329 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 5635 | #endif |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5636 | } |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5637 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5638 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5639 | if( rc==SQLITE_OK ){ |
| 5640 | pCur->iPage++; |
| 5641 | pCur->apPage[1] = pChild; |
danielk1977 | bf93c56 | 2008-09-29 15:53:25 +0000 | [diff] [blame] | 5642 | pCur->aiIdx[0] = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5643 | rc = balance_nonroot(pCur); |
| 5644 | }else{ |
| 5645 | releasePage(pChild); |
| 5646 | } |
| 5647 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5648 | return rc; |
| 5649 | } |
| 5650 | |
| 5651 | /* |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5652 | ** The page that pCur currently points to has just been modified in |
| 5653 | ** some way. This function figures out if this modification means the |
| 5654 | ** tree needs to be balanced, and if so calls the appropriate balancing |
| 5655 | ** routine. |
| 5656 | ** |
| 5657 | ** Parameter isInsert is true if a new cell was just inserted into the |
| 5658 | ** page, or false otherwise. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5659 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5660 | static int balance(BtCursor *pCur, int isInsert){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5661 | int rc = SQLITE_OK; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5662 | MemPage *pPage = pCur->apPage[pCur->iPage]; |
| 5663 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5664 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5665 | if( pCur->iPage==0 ){ |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 5666 | rc = sqlite3PagerWrite(pPage->pDbPage); |
| 5667 | if( rc==SQLITE_OK && pPage->nOverflow>0 ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5668 | rc = balance_deeper(pCur); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5669 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5670 | if( rc==SQLITE_OK && pPage->nCell==0 ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5671 | rc = balance_shallower(pCur); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5672 | } |
| 5673 | }else{ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5674 | if( pPage->nOverflow>0 || |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5675 | (!isInsert && pPage->nFree>pPage->pBt->usableSize*2/3) ){ |
| 5676 | rc = balance_nonroot(pCur); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5677 | } |
| 5678 | } |
| 5679 | return rc; |
| 5680 | } |
| 5681 | |
| 5682 | /* |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 5683 | ** This routine checks all cursors that point to table pgnoRoot. |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5684 | ** If any of those cursors were opened with wrFlag==0 in a different |
| 5685 | ** database connection (a database connection that shares the pager |
| 5686 | ** cache with the current connection) and that other connection |
| 5687 | ** is not in the ReadUncommmitted state, then this routine returns |
| 5688 | ** SQLITE_LOCKED. |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5689 | ** |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 5690 | ** As well as cursors with wrFlag==0, cursors with wrFlag==1 and |
| 5691 | ** isIncrblobHandle==1 are also considered 'read' cursors. Incremental |
| 5692 | ** blob cursors are used for both reading and writing. |
| 5693 | ** |
| 5694 | ** When pgnoRoot is the root page of an intkey table, this function is also |
| 5695 | ** responsible for invalidating incremental blob cursors when the table row |
| 5696 | ** on which they are opened is deleted or modified. Cursors are invalidated |
| 5697 | ** according to the following rules: |
| 5698 | ** |
| 5699 | ** 1) When BtreeClearTable() is called to completely delete the contents |
| 5700 | ** of a B-Tree table, pExclude is set to zero and parameter iRow is |
| 5701 | ** set to non-zero. In this case all incremental blob cursors open |
| 5702 | ** on the table rooted at pgnoRoot are invalidated. |
| 5703 | ** |
| 5704 | ** 2) When BtreeInsert(), BtreeDelete() or BtreePutData() is called to |
| 5705 | ** modify a table row via an SQL statement, pExclude is set to the |
| 5706 | ** write cursor used to do the modification and parameter iRow is set |
| 5707 | ** to the integer row id of the B-Tree entry being modified. Unless |
| 5708 | ** pExclude is itself an incremental blob cursor, then all incremental |
| 5709 | ** blob cursors open on row iRow of the B-Tree are invalidated. |
| 5710 | ** |
| 5711 | ** 3) If both pExclude and iRow are set to zero, no incremental blob |
| 5712 | ** cursors are invalidated. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5713 | */ |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 5714 | static int checkReadLocks( |
| 5715 | Btree *pBtree, |
| 5716 | Pgno pgnoRoot, |
| 5717 | BtCursor *pExclude, |
| 5718 | i64 iRow |
| 5719 | ){ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5720 | BtCursor *p; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5721 | BtShared *pBt = pBtree->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 5722 | sqlite3 *db = pBtree->db; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5723 | assert( sqlite3BtreeHoldsMutex(pBtree) ); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5724 | for(p=pBt->pCursor; p; p=p->pNext){ |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5725 | if( p==pExclude ) continue; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5726 | if( p->pgnoRoot!=pgnoRoot ) continue; |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 5727 | #ifndef SQLITE_OMIT_INCRBLOB |
| 5728 | if( p->isIncrblobHandle && ( |
| 5729 | (!pExclude && iRow) |
| 5730 | || (pExclude && !pExclude->isIncrblobHandle && p->info.nKey==iRow) |
| 5731 | )){ |
| 5732 | p->eState = CURSOR_INVALID; |
| 5733 | } |
| 5734 | #endif |
| 5735 | if( p->eState!=CURSOR_VALID ) continue; |
| 5736 | if( p->wrFlag==0 |
| 5737 | #ifndef SQLITE_OMIT_INCRBLOB |
| 5738 | || p->isIncrblobHandle |
| 5739 | #endif |
| 5740 | ){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 5741 | sqlite3 *dbOther = p->pBtree->db; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5742 | if( dbOther==0 || |
| 5743 | (dbOther!=db && (dbOther->flags & SQLITE_ReadUncommitted)==0) ){ |
| 5744 | return SQLITE_LOCKED; |
| 5745 | } |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5746 | } |
| 5747 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5748 | return SQLITE_OK; |
| 5749 | } |
| 5750 | |
| 5751 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5752 | ** Insert a new record into the BTree. The key is given by (pKey,nKey) |
| 5753 | ** 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] | 5754 | ** define what table the record should be inserted into. The cursor |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5755 | ** is left pointing at a random location. |
| 5756 | ** |
| 5757 | ** For an INTKEY table, only the nKey value of the key is used. pKey is |
| 5758 | ** ignored. For a ZERODATA table, the pData and nData are both ignored. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5759 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5760 | int sqlite3BtreeInsert( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 5761 | BtCursor *pCur, /* Insert data into the table of this cursor */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 5762 | const void *pKey, i64 nKey, /* The key of the new record */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 5763 | const void *pData, int nData, /* The data of the new record */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5764 | int nZero, /* Number of extra 0 bytes to append to data */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 5765 | int appendBias /* True if this is likely an append */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5766 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5767 | int rc; |
| 5768 | int loc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5769 | int szNew; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5770 | int idx; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5771 | MemPage *pPage; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5772 | Btree *p = pCur->pBtree; |
| 5773 | BtShared *pBt = p->pBt; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5774 | unsigned char *oldCell; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5775 | unsigned char *newCell = 0; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5776 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5777 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5778 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5779 | /* Must start a transaction before doing an insert */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5780 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5781 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5782 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5783 | assert( !pBt->readOnly ); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 5784 | if( !pCur->wrFlag ){ |
| 5785 | return SQLITE_PERM; /* Cursor not open for writing */ |
| 5786 | } |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 5787 | if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur, nKey) ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5788 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 5789 | } |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 5790 | if( pCur->eState==CURSOR_FAULT ){ |
| 5791 | return pCur->skip; |
| 5792 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5793 | |
| 5794 | /* Save the positions of any other cursors open on this table */ |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 5795 | sqlite3BtreeClearCursor(pCur); |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5796 | if( |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5797 | SQLITE_OK!=(rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur)) || |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 5798 | SQLITE_OK!=(rc = sqlite3BtreeMoveto(pCur, pKey, nKey, appendBias, &loc)) |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5799 | ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5800 | return rc; |
| 5801 | } |
| 5802 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5803 | pPage = pCur->apPage[pCur->iPage]; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 5804 | assert( pPage->intKey || nKey>=0 ); |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 5805 | assert( pPage->leaf || !pPage->intKey ); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5806 | TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n", |
| 5807 | pCur->pgnoRoot, nKey, nData, pPage->pgno, |
| 5808 | loc==0 ? "overwrite" : "new entry")); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5809 | assert( pPage->isInit ); |
danielk1977 | 52ae724 | 2008-03-25 14:24:56 +0000 | [diff] [blame] | 5810 | allocateTempSpace(pBt); |
| 5811 | newCell = pBt->pTmpSpace; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5812 | if( newCell==0 ) return SQLITE_NOMEM; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5813 | rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5814 | if( rc ) goto end_insert; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5815 | assert( szNew==cellSizePtr(pPage, newCell) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5816 | assert( szNew<=MX_CELL_SIZE(pBt) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5817 | idx = pCur->aiIdx[pCur->iPage]; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5818 | if( loc==0 && CURSOR_VALID==pCur->eState ){ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5819 | u16 szOld; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5820 | assert( idx<pPage->nCell ); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 5821 | rc = sqlite3PagerWrite(pPage->pDbPage); |
| 5822 | if( rc ){ |
| 5823 | goto end_insert; |
| 5824 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5825 | oldCell = findCell(pPage, idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5826 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5827 | memcpy(newCell, oldCell, 4); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5828 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5829 | szOld = cellSizePtr(pPage, oldCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5830 | rc = clearCell(pPage, oldCell); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5831 | if( rc ) goto end_insert; |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 5832 | rc = dropCell(pPage, idx, szOld); |
| 5833 | if( rc!=SQLITE_OK ) { |
| 5834 | goto end_insert; |
| 5835 | } |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 5836 | }else if( loc<0 && pPage->nCell>0 ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5837 | assert( pPage->leaf ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5838 | idx = ++pCur->aiIdx[pCur->iPage]; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 5839 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 5840 | pCur->validNKey = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5841 | }else{ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5842 | assert( pPage->leaf ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5843 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5844 | rc = insertCell(pPage, idx, newCell, szNew, 0, 0); |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 5845 | if( rc!=SQLITE_OK ) goto end_insert; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5846 | rc = balance(pCur, 1); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5847 | if( rc==SQLITE_OK ){ |
| 5848 | moveToRoot(pCur); |
| 5849 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5850 | end_insert: |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5851 | return rc; |
| 5852 | } |
| 5853 | |
| 5854 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5855 | ** Delete the entry that the cursor is pointing to. The cursor |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 5856 | ** is left pointing at a arbitrary location. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5857 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5858 | int sqlite3BtreeDelete(BtCursor *pCur){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5859 | MemPage *pPage = pCur->apPage[pCur->iPage]; |
| 5860 | int idx; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5861 | unsigned char *pCell; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5862 | int rc; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 5863 | Pgno pgnoChild = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5864 | Btree *p = pCur->pBtree; |
| 5865 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5866 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5867 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5868 | assert( pPage->isInit ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5869 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5870 | /* Must start a transaction before doing a delete */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5871 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5872 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5873 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5874 | assert( !pBt->readOnly ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 5875 | if( pCur->eState==CURSOR_FAULT ){ |
| 5876 | return pCur->skip; |
| 5877 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5878 | if( pCur->aiIdx[pCur->iPage]>=pPage->nCell ){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 5879 | return SQLITE_ERROR; /* The cursor is not pointing to anything */ |
| 5880 | } |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 5881 | if( !pCur->wrFlag ){ |
| 5882 | return SQLITE_PERM; /* Did not open this cursor for writing */ |
| 5883 | } |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 5884 | if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur, pCur->info.nKey) ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5885 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 5886 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5887 | |
| 5888 | /* Restore the current cursor position (a no-op if the cursor is not in |
| 5889 | ** CURSOR_REQUIRESEEK state) and save the positions of any other cursors |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5890 | ** open on the same table. Then call sqlite3PagerWrite() on the page |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5891 | ** that the entry will be deleted from. |
| 5892 | */ |
| 5893 | if( |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 5894 | (rc = restoreCursorPosition(pCur))!=0 || |
drh | d116739 | 2006-01-23 13:00:35 +0000 | [diff] [blame] | 5895 | (rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur))!=0 || |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5896 | (rc = sqlite3PagerWrite(pPage->pDbPage))!=0 |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5897 | ){ |
| 5898 | return rc; |
| 5899 | } |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5900 | |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 5901 | /* Locate the cell within its page and leave pCell pointing to the |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5902 | ** data. The clearCell() call frees any overflow pages associated with the |
| 5903 | ** cell. The cell itself is still intact. |
| 5904 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5905 | idx = pCur->aiIdx[pCur->iPage]; |
| 5906 | pCell = findCell(pPage, idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5907 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5908 | pgnoChild = get4byte(pCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5909 | } |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 5910 | rc = clearCell(pPage, pCell); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5911 | if( rc ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5912 | return rc; |
| 5913 | } |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5914 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5915 | if( !pPage->leaf ){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5916 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5917 | ** 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] | 5918 | ** do something we will leave a hole on an internal page. |
| 5919 | ** We have to fill the hole by moving in a cell from a leaf. The |
| 5920 | ** next Cell after the one to be deleted is guaranteed to exist and |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5921 | ** to be a leaf so we can use it. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5922 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5923 | BtCursor leafCur; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5924 | MemPage *pLeafPage; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5925 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5926 | unsigned char *pNext; |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5927 | int notUsed; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5928 | unsigned char *tempCell = 0; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 5929 | assert( !pPage->intKey ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5930 | sqlite3BtreeGetTempCursor(pCur, &leafCur); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5931 | rc = sqlite3BtreeNext(&leafCur, ¬Used); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5932 | if( rc==SQLITE_OK ){ |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 5933 | assert( leafCur.aiIdx[leafCur.iPage]==0 ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5934 | pLeafPage = leafCur.apPage[leafCur.iPage]; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5935 | rc = sqlite3PagerWrite(pLeafPage->pDbPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5936 | } |
| 5937 | if( rc==SQLITE_OK ){ |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 5938 | int leafCursorInvalid = 0; |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5939 | u16 szNext; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5940 | TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n", |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5941 | pCur->pgnoRoot, pPage->pgno, pLeafPage->pgno)); |
| 5942 | dropCell(pPage, idx, cellSizePtr(pPage, pCell)); |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 5943 | pNext = findCell(pLeafPage, 0); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5944 | szNext = cellSizePtr(pLeafPage, pNext); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5945 | assert( MX_CELL_SIZE(pBt)>=szNext+4 ); |
danielk1977 | 52ae724 | 2008-03-25 14:24:56 +0000 | [diff] [blame] | 5946 | allocateTempSpace(pBt); |
| 5947 | tempCell = pBt->pTmpSpace; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5948 | if( tempCell==0 ){ |
| 5949 | rc = SQLITE_NOMEM; |
| 5950 | } |
danielk1977 | 8ea1cfa | 2008-01-01 06:19:02 +0000 | [diff] [blame] | 5951 | if( rc==SQLITE_OK ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5952 | rc = insertCell(pPage, idx, pNext-4, szNext+4, tempCell, 0); |
danielk1977 | 8ea1cfa | 2008-01-01 06:19:02 +0000 | [diff] [blame] | 5953 | } |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 5954 | |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 5955 | |
| 5956 | /* The "if" statement in the next code block is critical. The |
| 5957 | ** slightest error in that statement would allow SQLite to operate |
| 5958 | ** correctly most of the time but produce very rare failures. To |
| 5959 | ** guard against this, the following macros help to verify that |
| 5960 | ** the "if" statement is well tested. |
| 5961 | */ |
| 5962 | testcase( pPage->nOverflow==0 && pPage->nFree<pBt->usableSize*2/3 |
| 5963 | && pLeafPage->nFree+2+szNext > pBt->usableSize*2/3 ); |
| 5964 | testcase( pPage->nOverflow==0 && pPage->nFree==pBt->usableSize*2/3 |
| 5965 | && pLeafPage->nFree+2+szNext > pBt->usableSize*2/3 ); |
| 5966 | testcase( pPage->nOverflow==0 && pPage->nFree==pBt->usableSize*2/3+1 |
| 5967 | && pLeafPage->nFree+2+szNext > pBt->usableSize*2/3 ); |
| 5968 | testcase( pPage->nOverflow>0 && pPage->nFree<=pBt->usableSize*2/3 |
| 5969 | && pLeafPage->nFree+2+szNext > pBt->usableSize*2/3 ); |
| 5970 | testcase( (pPage->nOverflow>0 || (pPage->nFree > pBt->usableSize*2/3)) |
| 5971 | && pLeafPage->nFree+2+szNext == pBt->usableSize*2/3 ); |
| 5972 | |
| 5973 | |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 5974 | if( (pPage->nOverflow>0 || (pPage->nFree > pBt->usableSize*2/3)) && |
| 5975 | (pLeafPage->nFree+2+szNext > pBt->usableSize*2/3) |
| 5976 | ){ |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 5977 | /* This branch is taken if the internal node is now either overflowing |
| 5978 | ** or underfull and the leaf node will be underfull after the just cell |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 5979 | ** copied to the internal node is deleted from it. This is a special |
| 5980 | ** case because the call to balance() to correct the internal node |
| 5981 | ** may change the tree structure and invalidate the contents of |
| 5982 | ** the leafCur.apPage[] and leafCur.aiIdx[] arrays, which will be |
| 5983 | ** used by the balance() required to correct the underfull leaf |
| 5984 | ** node. |
| 5985 | ** |
| 5986 | ** The formula used in the expression above are based on facets of |
| 5987 | ** the SQLite file-format that do not change over time. |
| 5988 | */ |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 5989 | testcase( pPage->nFree==pBt->usableSize*2/3+1 ); |
| 5990 | testcase( pLeafPage->nFree+2+szNext==pBt->usableSize*2/3+1 ); |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 5991 | leafCursorInvalid = 1; |
| 5992 | } |
| 5993 | |
danielk1977 | 8ea1cfa | 2008-01-01 06:19:02 +0000 | [diff] [blame] | 5994 | if( rc==SQLITE_OK ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5995 | put4byte(findOverflowCell(pPage, idx), pgnoChild); |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 5996 | VVA_ONLY( pCur->pagesShuffled = 0 ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5997 | rc = balance(pCur, 0); |
danielk1977 | 8ea1cfa | 2008-01-01 06:19:02 +0000 | [diff] [blame] | 5998 | } |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 5999 | |
| 6000 | if( rc==SQLITE_OK && leafCursorInvalid ){ |
| 6001 | /* The leaf-node is now underfull and so the tree needs to be |
| 6002 | ** rebalanced. However, the balance() operation on the internal |
| 6003 | ** node above may have modified the structure of the B-Tree and |
| 6004 | ** so the current contents of leafCur.apPage[] and leafCur.aiIdx[] |
| 6005 | ** may not be trusted. |
| 6006 | ** |
| 6007 | ** It is not possible to copy the ancestry from pCur, as the same |
| 6008 | ** balance() call has invalidated the pCur->apPage[] and aiIdx[] |
| 6009 | ** arrays. |
drh | 7b68280 | 2008-09-30 14:06:28 +0000 | [diff] [blame] | 6010 | ** |
| 6011 | ** The call to saveCursorPosition() below internally saves the |
| 6012 | ** key that leafCur is currently pointing to. Currently, there |
| 6013 | ** are two copies of that key in the tree - one here on the leaf |
| 6014 | ** page and one on some internal node in the tree. The copy on |
| 6015 | ** the leaf node is always the next key in tree-order after the |
| 6016 | ** copy on the internal node. So, the call to sqlite3BtreeNext() |
| 6017 | ** calls restoreCursorPosition() to point the cursor to the copy |
| 6018 | ** stored on the internal node, then advances to the next entry, |
| 6019 | ** which happens to be the copy of the key on the internal node. |
danielk1977 | a69fda2 | 2008-09-30 16:48:10 +0000 | [diff] [blame] | 6020 | ** Net effect: leafCur is pointing back to the duplicate cell |
| 6021 | ** that needs to be removed, and the leafCur.apPage[] and |
| 6022 | ** leafCur.aiIdx[] arrays are correct. |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6023 | */ |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 6024 | VVA_ONLY( Pgno leafPgno = pLeafPage->pgno ); |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6025 | rc = saveCursorPosition(&leafCur); |
| 6026 | if( rc==SQLITE_OK ){ |
| 6027 | rc = sqlite3BtreeNext(&leafCur, ¬Used); |
| 6028 | } |
| 6029 | pLeafPage = leafCur.apPage[leafCur.iPage]; |
| 6030 | assert( pLeafPage->pgno==leafPgno ); |
| 6031 | assert( leafCur.aiIdx[leafCur.iPage]==0 ); |
| 6032 | } |
| 6033 | |
danielk1977 | 8ea1cfa | 2008-01-01 06:19:02 +0000 | [diff] [blame] | 6034 | if( rc==SQLITE_OK ){ |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6035 | dropCell(pLeafPage, 0, szNext); |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 6036 | VVA_ONLY( leafCur.pagesShuffled = 0 ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6037 | rc = balance(&leafCur, 0); |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 6038 | assert( leafCursorInvalid || !leafCur.pagesShuffled |
| 6039 | || !pCur->pagesShuffled ); |
danielk1977 | 8ea1cfa | 2008-01-01 06:19:02 +0000 | [diff] [blame] | 6040 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6041 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6042 | sqlite3BtreeReleaseTempCursor(&leafCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 6043 | }else{ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 6044 | TRACE(("DELETE: table=%d delete from leaf %d\n", |
| 6045 | pCur->pgnoRoot, pPage->pgno)); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6046 | dropCell(pPage, idx, cellSizePtr(pPage, pCell)); |
| 6047 | rc = balance(pCur, 0); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 6048 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6049 | if( rc==SQLITE_OK ){ |
| 6050 | moveToRoot(pCur); |
| 6051 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 6052 | return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 6053 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6054 | |
| 6055 | /* |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 6056 | ** Create a new BTree table. Write into *piTable the page |
| 6057 | ** number for the root page of the new table. |
| 6058 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 6059 | ** The type of type is determined by the flags parameter. Only the |
| 6060 | ** following values of flags are currently in use. Other values for |
| 6061 | ** flags might not work: |
| 6062 | ** |
| 6063 | ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys |
| 6064 | ** BTREE_ZERODATA Used for SQL indices |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6065 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6066 | static int btreeCreateTable(Btree *p, int *piTable, int flags){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6067 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6068 | MemPage *pRoot; |
| 6069 | Pgno pgnoRoot; |
| 6070 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6071 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6072 | assert( sqlite3BtreeHoldsMutex(p) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6073 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 6074 | /* Must start a transaction first */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6075 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
| 6076 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6077 | } |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 6078 | assert( !pBt->readOnly ); |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 6079 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6080 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 6081 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6082 | if( rc ){ |
| 6083 | return rc; |
| 6084 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6085 | #else |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6086 | if( pBt->autoVacuum ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6087 | Pgno pgnoMove; /* Move a page here to make room for the root-page */ |
| 6088 | MemPage *pPageMove; /* The page to move to. */ |
| 6089 | |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 6090 | /* Creating a new table may probably require moving an existing database |
| 6091 | ** to make room for the new tables root page. In case this page turns |
| 6092 | ** out to be an overflow page, delete all overflow page-map caches |
| 6093 | ** held by open cursors. |
| 6094 | */ |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 6095 | invalidateAllOverflowCache(pBt); |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 6096 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6097 | /* Read the value of meta[3] from the database to determine where the |
| 6098 | ** root page of the new table should go. meta[3] is the largest root-page |
| 6099 | ** created so far, so the new root-page is (meta[3]+1). |
| 6100 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6101 | rc = sqlite3BtreeGetMeta(p, 4, &pgnoRoot); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6102 | if( rc!=SQLITE_OK ){ |
| 6103 | return rc; |
| 6104 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6105 | pgnoRoot++; |
| 6106 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6107 | /* The new root-page may not be allocated on a pointer-map page, or the |
| 6108 | ** PENDING_BYTE page. |
| 6109 | */ |
drh | 7219043 | 2008-01-31 14:54:43 +0000 | [diff] [blame] | 6110 | while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) || |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6111 | pgnoRoot==PENDING_BYTE_PAGE(pBt) ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6112 | pgnoRoot++; |
| 6113 | } |
| 6114 | assert( pgnoRoot>=3 ); |
| 6115 | |
| 6116 | /* Allocate a page. The page that currently resides at pgnoRoot will |
| 6117 | ** be moved to the allocated page (unless the allocated page happens |
| 6118 | ** to reside at pgnoRoot). |
| 6119 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 6120 | rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6121 | if( rc!=SQLITE_OK ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6122 | return rc; |
| 6123 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6124 | |
| 6125 | if( pgnoMove!=pgnoRoot ){ |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 6126 | /* pgnoRoot is the page that will be used for the root-page of |
| 6127 | ** the new table (assuming an error did not occur). But we were |
| 6128 | ** allocated pgnoMove. If required (i.e. if it was not allocated |
| 6129 | ** by extending the file), the current page at position pgnoMove |
| 6130 | ** is already journaled. |
| 6131 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6132 | u8 eType; |
| 6133 | Pgno iPtrPage; |
| 6134 | |
| 6135 | releasePage(pPageMove); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 6136 | |
| 6137 | /* Move the page currently at pgnoRoot to pgnoMove. */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6138 | rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6139 | if( rc!=SQLITE_OK ){ |
| 6140 | return rc; |
| 6141 | } |
| 6142 | rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage); |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 6143 | if( rc!=SQLITE_OK || eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6144 | releasePage(pRoot); |
| 6145 | return rc; |
| 6146 | } |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 6147 | assert( eType!=PTRMAP_ROOTPAGE ); |
| 6148 | assert( eType!=PTRMAP_FREEPAGE ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6149 | rc = sqlite3PagerWrite(pRoot->pDbPage); |
danielk1977 | 5fd057a | 2005-03-09 13:09:43 +0000 | [diff] [blame] | 6150 | if( rc!=SQLITE_OK ){ |
| 6151 | releasePage(pRoot); |
| 6152 | return rc; |
| 6153 | } |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 6154 | rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6155 | releasePage(pRoot); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 6156 | |
| 6157 | /* Obtain the page at pgnoRoot */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6158 | if( rc!=SQLITE_OK ){ |
| 6159 | return rc; |
| 6160 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6161 | rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6162 | if( rc!=SQLITE_OK ){ |
| 6163 | return rc; |
| 6164 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6165 | rc = sqlite3PagerWrite(pRoot->pDbPage); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6166 | if( rc!=SQLITE_OK ){ |
| 6167 | releasePage(pRoot); |
| 6168 | return rc; |
| 6169 | } |
| 6170 | }else{ |
| 6171 | pRoot = pPageMove; |
| 6172 | } |
| 6173 | |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 6174 | /* Update the pointer-map and meta-data with the new root-page number. */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6175 | rc = ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0); |
| 6176 | if( rc ){ |
| 6177 | releasePage(pRoot); |
| 6178 | return rc; |
| 6179 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6180 | rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6181 | if( rc ){ |
| 6182 | releasePage(pRoot); |
| 6183 | return rc; |
| 6184 | } |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 6185 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6186 | }else{ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 6187 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6188 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6189 | } |
| 6190 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6191 | assert( sqlite3PagerIswriteable(pRoot->pDbPage) ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 6192 | zeroPage(pRoot, flags | PTF_LEAF); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6193 | sqlite3PagerUnref(pRoot->pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6194 | *piTable = (int)pgnoRoot; |
| 6195 | return SQLITE_OK; |
| 6196 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6197 | int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){ |
| 6198 | int rc; |
| 6199 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6200 | p->pBt->db = p->db; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6201 | rc = btreeCreateTable(p, piTable, flags); |
| 6202 | sqlite3BtreeLeave(p); |
| 6203 | return rc; |
| 6204 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6205 | |
| 6206 | /* |
| 6207 | ** Erase the given database page and all its children. Return |
| 6208 | ** the page to the freelist. |
| 6209 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6210 | static int clearDatabasePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6211 | BtShared *pBt, /* The BTree that contains the table */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6212 | Pgno pgno, /* Page number to clear */ |
| 6213 | MemPage *pParent, /* Parent page. NULL for the root */ |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6214 | int freePageFlag, /* Deallocate page if true */ |
| 6215 | int *pnChange |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6216 | ){ |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6217 | MemPage *pPage = 0; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6218 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6219 | unsigned char *pCell; |
| 6220 | int i; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6221 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6222 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 6223 | if( pgno>pagerPagecount(pBt->pPager) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 6224 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 6225 | } |
| 6226 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6227 | rc = getAndInitPage(pBt, pgno, &pPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6228 | if( rc ) goto cleardatabasepage_out; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6229 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 6230 | pCell = findCell(pPage, i); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6231 | if( !pPage->leaf ){ |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6232 | rc = clearDatabasePage(pBt, get4byte(pCell), pPage, 1, pnChange); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6233 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6234 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6235 | rc = clearCell(pPage, pCell); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6236 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6237 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6238 | if( !pPage->leaf ){ |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6239 | rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), pPage, 1, pnChange); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6240 | if( rc ) goto cleardatabasepage_out; |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6241 | }else if( pnChange ){ |
| 6242 | assert( pPage->intKey ); |
| 6243 | *pnChange += pPage->nCell; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6244 | } |
| 6245 | if( freePageFlag ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6246 | rc = freePage(pPage); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6247 | }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6248 | zeroPage(pPage, pPage->aData[0] | PTF_LEAF); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6249 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6250 | |
| 6251 | cleardatabasepage_out: |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6252 | releasePage(pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6253 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6254 | } |
| 6255 | |
| 6256 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 6257 | ** Delete all information from a single table in the database. iTable is |
| 6258 | ** the page number of the root of the table. After this routine returns, |
| 6259 | ** the root page is empty, but still exists. |
| 6260 | ** |
| 6261 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 6262 | ** read cursors on the table. Open write cursors are moved to the |
| 6263 | ** root of the table. |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6264 | ** |
| 6265 | ** If pnChange is not NULL, then table iTable must be an intkey table. The |
| 6266 | ** integer value pointed to by pnChange is incremented by the number of |
| 6267 | ** entries in the table. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6268 | */ |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6269 | int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6270 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6271 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6272 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6273 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6274 | if( p->inTrans!=TRANS_WRITE ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6275 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 6276 | }else if( (rc = checkReadLocks(p, iTable, 0, 1))!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6277 | /* nothing to do */ |
| 6278 | }else if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){ |
| 6279 | /* nothing to do */ |
| 6280 | }else{ |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6281 | rc = clearDatabasePage(pBt, (Pgno)iTable, 0, 0, pnChange); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6282 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6283 | sqlite3BtreeLeave(p); |
| 6284 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6285 | } |
| 6286 | |
| 6287 | /* |
| 6288 | ** Erase all information in a table and add the root of the table to |
| 6289 | ** the freelist. Except, the root of the principle table (the one on |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 6290 | ** page 1) is never added to the freelist. |
| 6291 | ** |
| 6292 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 6293 | ** cursors on the table. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6294 | ** |
| 6295 | ** If AUTOVACUUM is enabled and the page at iTable is not the last |
| 6296 | ** root page in the database file, then the last root page |
| 6297 | ** in the database file is moved into the slot formerly occupied by |
| 6298 | ** iTable and that last slot formerly occupied by the last root page |
| 6299 | ** is added to the freelist instead of iTable. In this say, all |
| 6300 | ** root pages are kept at the beginning of the database file, which |
| 6301 | ** is necessary for AUTOVACUUM to work right. *piMoved is set to the |
| 6302 | ** page number that used to be the last root page in the file before |
| 6303 | ** the move. If no page gets moved, *piMoved is set to 0. |
| 6304 | ** The last root page is recorded in meta[3] and the value of |
| 6305 | ** meta[3] is updated by this procedure. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6306 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6307 | static int btreeDropTable(Btree *p, int iTable, int *piMoved){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6308 | int rc; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6309 | MemPage *pPage = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6310 | BtShared *pBt = p->pBt; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6311 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6312 | assert( sqlite3BtreeHoldsMutex(p) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6313 | if( p->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 6314 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6315 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6316 | |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 6317 | /* It is illegal to drop a table if any cursors are open on the |
| 6318 | ** database. This is because in auto-vacuum mode the backend may |
| 6319 | ** need to move another root-page to fill a gap left by the deleted |
| 6320 | ** root page. If an open cursor was using this page a problem would |
| 6321 | ** occur. |
| 6322 | */ |
| 6323 | if( pBt->pCursor ){ |
| 6324 | return SQLITE_LOCKED; |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 6325 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6326 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6327 | rc = sqlite3BtreeGetPage(pBt, (Pgno)iTable, &pPage, 0); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6328 | if( rc ) return rc; |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6329 | rc = sqlite3BtreeClearTable(p, iTable, 0); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6330 | if( rc ){ |
| 6331 | releasePage(pPage); |
| 6332 | return rc; |
| 6333 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6334 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6335 | *piMoved = 0; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6336 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6337 | if( iTable>1 ){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6338 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6339 | rc = freePage(pPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6340 | releasePage(pPage); |
| 6341 | #else |
| 6342 | if( pBt->autoVacuum ){ |
| 6343 | Pgno maxRootPgno; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6344 | rc = sqlite3BtreeGetMeta(p, 4, &maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6345 | if( rc!=SQLITE_OK ){ |
| 6346 | releasePage(pPage); |
| 6347 | return rc; |
| 6348 | } |
| 6349 | |
| 6350 | if( iTable==maxRootPgno ){ |
| 6351 | /* If the table being dropped is the table with the largest root-page |
| 6352 | ** number in the database, put the root page on the free list. |
| 6353 | */ |
| 6354 | rc = freePage(pPage); |
| 6355 | releasePage(pPage); |
| 6356 | if( rc!=SQLITE_OK ){ |
| 6357 | return rc; |
| 6358 | } |
| 6359 | }else{ |
| 6360 | /* The table being dropped does not have the largest root-page |
| 6361 | ** number in the database. So move the page that does into the |
| 6362 | ** gap left by the deleted root-page. |
| 6363 | */ |
| 6364 | MemPage *pMove; |
| 6365 | releasePage(pPage); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6366 | rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6367 | if( rc!=SQLITE_OK ){ |
| 6368 | return rc; |
| 6369 | } |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 6370 | rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6371 | releasePage(pMove); |
| 6372 | if( rc!=SQLITE_OK ){ |
| 6373 | return rc; |
| 6374 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6375 | rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6376 | if( rc!=SQLITE_OK ){ |
| 6377 | return rc; |
| 6378 | } |
| 6379 | rc = freePage(pMove); |
| 6380 | releasePage(pMove); |
| 6381 | if( rc!=SQLITE_OK ){ |
| 6382 | return rc; |
| 6383 | } |
| 6384 | *piMoved = maxRootPgno; |
| 6385 | } |
| 6386 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6387 | /* Set the new 'max-root-page' value in the database header. This |
| 6388 | ** is the old value less one, less one more if that happens to |
| 6389 | ** be a root-page number, less one again if that is the |
| 6390 | ** PENDING_BYTE_PAGE. |
| 6391 | */ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 6392 | maxRootPgno--; |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6393 | if( maxRootPgno==PENDING_BYTE_PAGE(pBt) ){ |
| 6394 | maxRootPgno--; |
| 6395 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6396 | if( maxRootPgno==PTRMAP_PAGENO(pBt, maxRootPgno) ){ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 6397 | maxRootPgno--; |
| 6398 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6399 | assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) ); |
| 6400 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6401 | rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6402 | }else{ |
| 6403 | rc = freePage(pPage); |
| 6404 | releasePage(pPage); |
| 6405 | } |
| 6406 | #endif |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6407 | }else{ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6408 | /* If sqlite3BtreeDropTable was called on page 1. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6409 | zeroPage(pPage, PTF_INTKEY|PTF_LEAF ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6410 | releasePage(pPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6411 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6412 | return rc; |
| 6413 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6414 | int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){ |
| 6415 | int rc; |
| 6416 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6417 | p->pBt->db = p->db; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6418 | rc = btreeDropTable(p, iTable, piMoved); |
| 6419 | sqlite3BtreeLeave(p); |
| 6420 | return rc; |
| 6421 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6422 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 6423 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6424 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6425 | ** Read the meta-information out of a database file. Meta[0] |
| 6426 | ** is the number of free pages currently in the database. Meta[1] |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 6427 | ** through meta[15] are available for use by higher layers. Meta[0] |
| 6428 | ** is read-only, the others are read/write. |
| 6429 | ** |
| 6430 | ** The schema layer numbers meta values differently. At the schema |
| 6431 | ** layer (and the SetCookie and ReadCookie opcodes) the number of |
| 6432 | ** 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] | 6433 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6434 | int sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6435 | DbPage *pDbPage; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6436 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6437 | unsigned char *pP1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6438 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6439 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6440 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6441 | pBt->db = p->db; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6442 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6443 | /* Reading a meta-data value requires a read-lock on page 1 (and hence |
| 6444 | ** the sqlite_master table. We grab this lock regardless of whether or |
| 6445 | ** not the SQLITE_ReadUncommitted flag is set (the table rooted at page |
| 6446 | ** 1 is treated as a special case by queryTableLock() and lockTable()). |
| 6447 | */ |
| 6448 | rc = queryTableLock(p, 1, READ_LOCK); |
| 6449 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6450 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6451 | return rc; |
| 6452 | } |
| 6453 | |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6454 | assert( idx>=0 && idx<=15 ); |
danielk1977 | d9f6c53 | 2008-09-19 16:39:38 +0000 | [diff] [blame] | 6455 | if( pBt->pPage1 ){ |
| 6456 | /* The b-tree is already holding a reference to page 1 of the database |
| 6457 | ** file. In this case the required meta-data value can be read directly |
| 6458 | ** from the page data of this reference. This is slightly faster than |
| 6459 | ** requesting a new reference from the pager layer. |
| 6460 | */ |
| 6461 | pP1 = (unsigned char *)pBt->pPage1->aData; |
| 6462 | }else{ |
| 6463 | /* The b-tree does not have a reference to page 1 of the database file. |
| 6464 | ** Obtain one from the pager layer. |
| 6465 | */ |
danielk1977 | ea89730 | 2008-09-19 15:10:58 +0000 | [diff] [blame] | 6466 | rc = sqlite3PagerGet(pBt->pPager, 1, &pDbPage); |
| 6467 | if( rc ){ |
| 6468 | sqlite3BtreeLeave(p); |
| 6469 | return rc; |
| 6470 | } |
| 6471 | pP1 = (unsigned char *)sqlite3PagerGetData(pDbPage); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6472 | } |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6473 | *pMeta = get4byte(&pP1[36 + idx*4]); |
danielk1977 | ea89730 | 2008-09-19 15:10:58 +0000 | [diff] [blame] | 6474 | |
danielk1977 | d9f6c53 | 2008-09-19 16:39:38 +0000 | [diff] [blame] | 6475 | /* If the b-tree is not holding a reference to page 1, then one was |
| 6476 | ** requested from the pager layer in the above block. Release it now. |
| 6477 | */ |
danielk1977 | ea89730 | 2008-09-19 15:10:58 +0000 | [diff] [blame] | 6478 | if( !pBt->pPage1 ){ |
| 6479 | sqlite3PagerUnref(pDbPage); |
| 6480 | } |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 6481 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6482 | /* If autovacuumed is disabled in this build but we are trying to |
| 6483 | ** access an autovacuumed database, then make the database readonly. |
| 6484 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6485 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 6486 | if( idx==4 && *pMeta>0 ) pBt->readOnly = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6487 | #endif |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 6488 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6489 | /* Grab the read-lock on page 1. */ |
| 6490 | rc = lockTable(p, 1, READ_LOCK); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6491 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6492 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6493 | } |
| 6494 | |
| 6495 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6496 | ** Write meta-information back into the database. Meta[0] is |
| 6497 | ** read-only and may not be written. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6498 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6499 | int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){ |
| 6500 | BtShared *pBt = p->pBt; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6501 | unsigned char *pP1; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6502 | int rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6503 | assert( idx>=1 && idx<=15 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6504 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6505 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6506 | if( p->inTrans!=TRANS_WRITE ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6507 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
| 6508 | }else{ |
| 6509 | assert( pBt->pPage1!=0 ); |
| 6510 | pP1 = pBt->pPage1->aData; |
| 6511 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 6512 | if( rc==SQLITE_OK ){ |
| 6513 | put4byte(&pP1[36 + idx*4], iMeta); |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 +0000 | [diff] [blame] | 6514 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6515 | if( idx==7 ){ |
| 6516 | assert( pBt->autoVacuum || iMeta==0 ); |
| 6517 | assert( iMeta==0 || iMeta==1 ); |
| 6518 | pBt->incrVacuum = iMeta; |
| 6519 | } |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 +0000 | [diff] [blame] | 6520 | #endif |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6521 | } |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 6522 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6523 | sqlite3BtreeLeave(p); |
| 6524 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6525 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 6526 | |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 6527 | /* |
| 6528 | ** Return the flag byte at the beginning of the page that the cursor |
| 6529 | ** is currently pointing to. |
| 6530 | */ |
| 6531 | int sqlite3BtreeFlags(BtCursor *pCur){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6532 | /* TODO: What about CURSOR_REQUIRESEEK state? Probably need to call |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 6533 | ** restoreCursorPosition() here. |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6534 | */ |
danielk1977 | e448dc4 | 2008-01-02 11:50:51 +0000 | [diff] [blame] | 6535 | MemPage *pPage; |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 6536 | restoreCursorPosition(pCur); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6537 | pPage = pCur->apPage[pCur->iPage]; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6538 | assert( cursorHoldsMutex(pCur) ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 6539 | assert( pPage->pBt==pCur->pBt ); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 6540 | return pPage ? pPage->aData[pPage->hdrOffset] : 0; |
| 6541 | } |
| 6542 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6543 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6544 | /* |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6545 | ** Return the pager associated with a BTree. This routine is used for |
| 6546 | ** testing and debugging only. |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6547 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6548 | Pager *sqlite3BtreePager(Btree *p){ |
| 6549 | return p->pBt->pPager; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6550 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6551 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6552 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6553 | /* |
| 6554 | ** Append a message to the error message string. |
| 6555 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6556 | static void checkAppendMsg( |
| 6557 | IntegrityCk *pCheck, |
| 6558 | char *zMsg1, |
| 6559 | const char *zFormat, |
| 6560 | ... |
| 6561 | ){ |
| 6562 | va_list ap; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6563 | if( !pCheck->mxErr ) return; |
| 6564 | pCheck->mxErr--; |
| 6565 | pCheck->nErr++; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6566 | va_start(ap, zFormat); |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 6567 | if( pCheck->errMsg.nChar ){ |
| 6568 | sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6569 | } |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 6570 | if( zMsg1 ){ |
| 6571 | sqlite3StrAccumAppend(&pCheck->errMsg, zMsg1, -1); |
| 6572 | } |
| 6573 | sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap); |
| 6574 | va_end(ap); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 6575 | if( pCheck->errMsg.mallocFailed ){ |
| 6576 | pCheck->mallocFailed = 1; |
| 6577 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6578 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6579 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6580 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6581 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6582 | /* |
| 6583 | ** Add 1 to the reference count for page iPage. If this is the second |
| 6584 | ** reference to the page, add an error message to pCheck->zErrMsg. |
| 6585 | ** Return 1 if there are 2 ore more references to the page and 0 if |
| 6586 | ** if this is the first reference to the page. |
| 6587 | ** |
| 6588 | ** Also check that the page number is in bounds. |
| 6589 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 6590 | static int checkRef(IntegrityCk *pCheck, int iPage, char *zContext){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6591 | if( iPage==0 ) return 1; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 6592 | if( iPage>pCheck->nPage || iPage<0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6593 | checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6594 | return 1; |
| 6595 | } |
| 6596 | if( pCheck->anRef[iPage]==1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6597 | checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6598 | return 1; |
| 6599 | } |
| 6600 | return (pCheck->anRef[iPage]++)>1; |
| 6601 | } |
| 6602 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6603 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6604 | /* |
| 6605 | ** Check that the entry in the pointer-map for page iChild maps to |
| 6606 | ** page iParent, pointer type ptrType. If not, append an error message |
| 6607 | ** to pCheck. |
| 6608 | */ |
| 6609 | static void checkPtrmap( |
| 6610 | IntegrityCk *pCheck, /* Integrity check context */ |
| 6611 | Pgno iChild, /* Child page number */ |
| 6612 | u8 eType, /* Expected pointer map type */ |
| 6613 | Pgno iParent, /* Expected pointer map parent page number */ |
| 6614 | char *zContext /* Context description (used for error msg) */ |
| 6615 | ){ |
| 6616 | int rc; |
| 6617 | u8 ePtrmapType; |
| 6618 | Pgno iPtrmapParent; |
| 6619 | |
| 6620 | rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent); |
| 6621 | if( rc!=SQLITE_OK ){ |
| 6622 | checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild); |
| 6623 | return; |
| 6624 | } |
| 6625 | |
| 6626 | if( ePtrmapType!=eType || iPtrmapParent!=iParent ){ |
| 6627 | checkAppendMsg(pCheck, zContext, |
| 6628 | "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", |
| 6629 | iChild, eType, iParent, ePtrmapType, iPtrmapParent); |
| 6630 | } |
| 6631 | } |
| 6632 | #endif |
| 6633 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6634 | /* |
| 6635 | ** Check the integrity of the freelist or of an overflow page list. |
| 6636 | ** Verify that the number of pages on the list is N. |
| 6637 | */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6638 | static void checkList( |
| 6639 | IntegrityCk *pCheck, /* Integrity checking context */ |
| 6640 | int isFreeList, /* True for a freelist. False for overflow page list */ |
| 6641 | int iPage, /* Page number for first page in the list */ |
| 6642 | int N, /* Expected number of pages in the list */ |
| 6643 | char *zContext /* Context for error messages */ |
| 6644 | ){ |
| 6645 | int i; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6646 | int expected = N; |
| 6647 | int iFirst = iPage; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6648 | while( N-- > 0 && pCheck->mxErr ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6649 | DbPage *pOvflPage; |
| 6650 | unsigned char *pOvflData; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6651 | if( iPage<1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6652 | checkAppendMsg(pCheck, zContext, |
| 6653 | "%d of %d pages missing from overflow list starting at %d", |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6654 | N+1, expected, iFirst); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6655 | break; |
| 6656 | } |
| 6657 | if( checkRef(pCheck, iPage, zContext) ) break; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6658 | if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6659 | checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6660 | break; |
| 6661 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6662 | pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6663 | if( isFreeList ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6664 | int n = get4byte(&pOvflData[4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6665 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6666 | if( pCheck->pBt->autoVacuum ){ |
| 6667 | checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext); |
| 6668 | } |
| 6669 | #endif |
drh | 45b1fac | 2008-07-04 17:52:42 +0000 | [diff] [blame] | 6670 | if( n>pCheck->pBt->usableSize/4-2 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6671 | checkAppendMsg(pCheck, zContext, |
| 6672 | "freelist leaf count too big on page %d", iPage); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 6673 | N--; |
| 6674 | }else{ |
| 6675 | for(i=0; i<n; i++){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6676 | Pgno iFreePage = get4byte(&pOvflData[8+i*4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6677 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6678 | if( pCheck->pBt->autoVacuum ){ |
| 6679 | checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext); |
| 6680 | } |
| 6681 | #endif |
| 6682 | checkRef(pCheck, iFreePage, zContext); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 6683 | } |
| 6684 | N -= n; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6685 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6686 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6687 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6688 | else{ |
| 6689 | /* If this database supports auto-vacuum and iPage is not the last |
| 6690 | ** page in this overflow list, check that the pointer-map entry for |
| 6691 | ** the following page matches iPage. |
| 6692 | */ |
| 6693 | if( pCheck->pBt->autoVacuum && N>0 ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6694 | i = get4byte(pOvflData); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6695 | checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext); |
| 6696 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6697 | } |
| 6698 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6699 | iPage = get4byte(pOvflData); |
| 6700 | sqlite3PagerUnref(pOvflPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6701 | } |
| 6702 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6703 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6704 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6705 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6706 | /* |
| 6707 | ** Do various sanity checks on a single page of a tree. Return |
| 6708 | ** the tree depth. Root pages return 0. Parents of root pages |
| 6709 | ** return 1, and so forth. |
| 6710 | ** |
| 6711 | ** These checks are done: |
| 6712 | ** |
| 6713 | ** 1. Make sure that cells and freeblocks do not overlap |
| 6714 | ** but combine to completely cover the page. |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6715 | ** NO 2. Make sure cell keys are in order. |
| 6716 | ** NO 3. Make sure no key is less than or equal to zLowerBound. |
| 6717 | ** NO 4. Make sure no key is greater than or equal to zUpperBound. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6718 | ** 5. Check the integrity of overflow pages. |
| 6719 | ** 6. Recursively call checkTreePage on all children. |
| 6720 | ** 7. Verify that the depth of all children is the same. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6721 | ** 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] | 6722 | ** the root of the tree. |
| 6723 | */ |
| 6724 | static int checkTreePage( |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 6725 | IntegrityCk *pCheck, /* Context for the sanity check */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6726 | int iPage, /* Page number of the page to check */ |
| 6727 | MemPage *pParent, /* Parent page */ |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6728 | char *zParentContext /* Parent context */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6729 | ){ |
| 6730 | MemPage *pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6731 | int i, rc, depth, d2, pgno, cnt; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6732 | int hdr, cellStart; |
| 6733 | int nCell; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6734 | u8 *data; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6735 | BtShared *pBt; |
drh | 4f26bb6 | 2005-09-08 14:17:20 +0000 | [diff] [blame] | 6736 | int usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6737 | char zContext[100]; |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 6738 | char *hit = 0; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6739 | |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6740 | sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage); |
danielk1977 | ef73ee9 | 2004-11-06 12:26:07 +0000 | [diff] [blame] | 6741 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6742 | /* Check that the page exists |
| 6743 | */ |
drh | d9cb6ac | 2005-10-20 07:28:17 +0000 | [diff] [blame] | 6744 | pBt = pCheck->pBt; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 6745 | usableSize = pBt->usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6746 | if( iPage==0 ) return 0; |
| 6747 | if( checkRef(pCheck, iPage, zParentContext) ) return 0; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6748 | if( (rc = sqlite3BtreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6749 | checkAppendMsg(pCheck, zContext, |
| 6750 | "unable to get the page. error code=%d", rc); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6751 | return 0; |
| 6752 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6753 | if( (rc = sqlite3BtreeInitPage(pPage))!=0 ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6754 | checkAppendMsg(pCheck, zContext, |
| 6755 | "sqlite3BtreeInitPage() returns error code %d", rc); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 6756 | releasePage(pPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6757 | return 0; |
| 6758 | } |
| 6759 | |
| 6760 | /* Check out all the cells. |
| 6761 | */ |
| 6762 | depth = 0; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6763 | for(i=0; i<pPage->nCell && pCheck->mxErr; i++){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 6764 | u8 *pCell; |
| 6765 | int sz; |
| 6766 | CellInfo info; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6767 | |
| 6768 | /* Check payload overflow pages |
| 6769 | */ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6770 | sqlite3_snprintf(sizeof(zContext), zContext, |
| 6771 | "On tree page %d cell %d: ", iPage, i); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 6772 | pCell = findCell(pPage,i); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6773 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 6774 | sz = info.nData; |
| 6775 | if( !pPage->intKey ) sz += info.nKey; |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 6776 | assert( sz==info.nPayload ); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 6777 | if( sz>info.nLocal ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 6778 | int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6779 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
| 6780 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6781 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6782 | checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6783 | } |
| 6784 | #endif |
| 6785 | checkList(pCheck, 0, pgnoOvfl, nPage, zContext); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6786 | } |
| 6787 | |
| 6788 | /* Check sanity of left child page. |
| 6789 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6790 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6791 | pgno = get4byte(pCell); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6792 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6793 | if( pBt->autoVacuum ){ |
| 6794 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext); |
| 6795 | } |
| 6796 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6797 | d2 = checkTreePage(pCheck,pgno,pPage,zContext); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6798 | if( i>0 && d2!=depth ){ |
| 6799 | checkAppendMsg(pCheck, zContext, "Child page depth differs"); |
| 6800 | } |
| 6801 | depth = d2; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6802 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6803 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6804 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6805 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6806 | sqlite3_snprintf(sizeof(zContext), zContext, |
| 6807 | "On page %d at right child: ", iPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6808 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6809 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6810 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6811 | } |
| 6812 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6813 | checkTreePage(pCheck, pgno, pPage, zContext); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6814 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6815 | |
| 6816 | /* Check for complete coverage of the page |
| 6817 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6818 | data = pPage->aData; |
| 6819 | hdr = pPage->hdrOffset; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 6820 | hit = sqlite3PageMalloc( pBt->pageSize ); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 6821 | if( hit==0 ){ |
| 6822 | pCheck->mallocFailed = 1; |
| 6823 | }else{ |
shane | 5780ebd | 2008-11-11 17:36:30 +0000 | [diff] [blame] | 6824 | u16 contentOffset = get2byte(&data[hdr+5]); |
| 6825 | if (contentOffset > usableSize) { |
| 6826 | checkAppendMsg(pCheck, 0, |
| 6827 | "Corruption detected in header on page %d",iPage,0); |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 6828 | goto check_page_abort; |
shane | 5780ebd | 2008-11-11 17:36:30 +0000 | [diff] [blame] | 6829 | } |
| 6830 | memset(hit+contentOffset, 0, usableSize-contentOffset); |
| 6831 | memset(hit, 1, contentOffset); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6832 | nCell = get2byte(&data[hdr+3]); |
| 6833 | cellStart = hdr + 12 - 4*pPage->leaf; |
| 6834 | for(i=0; i<nCell; i++){ |
| 6835 | int pc = get2byte(&data[cellStart+i*2]); |
danielk1977 | daca543 | 2008-08-25 11:57:16 +0000 | [diff] [blame] | 6836 | u16 size = 1024; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6837 | int j; |
danielk1977 | daca543 | 2008-08-25 11:57:16 +0000 | [diff] [blame] | 6838 | if( pc<=usableSize ){ |
| 6839 | size = cellSizePtr(pPage, &data[pc]); |
| 6840 | } |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 6841 | if( (pc+size-1)>=usableSize || pc<0 ){ |
| 6842 | checkAppendMsg(pCheck, 0, |
| 6843 | "Corruption detected in cell %d on page %d",i,iPage,0); |
| 6844 | }else{ |
| 6845 | for(j=pc+size-1; j>=pc; j--) hit[j]++; |
| 6846 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6847 | } |
| 6848 | for(cnt=0, i=get2byte(&data[hdr+1]); i>0 && i<usableSize && cnt<10000; |
| 6849 | cnt++){ |
| 6850 | int size = get2byte(&data[i+2]); |
| 6851 | int j; |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 6852 | if( (i+size-1)>=usableSize || i<0 ){ |
| 6853 | checkAppendMsg(pCheck, 0, |
| 6854 | "Corruption detected in cell %d on page %d",i,iPage,0); |
| 6855 | }else{ |
| 6856 | for(j=i+size-1; j>=i; j--) hit[j]++; |
| 6857 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6858 | i = get2byte(&data[i]); |
| 6859 | } |
| 6860 | for(i=cnt=0; i<usableSize; i++){ |
| 6861 | if( hit[i]==0 ){ |
| 6862 | cnt++; |
| 6863 | }else if( hit[i]>1 ){ |
| 6864 | checkAppendMsg(pCheck, 0, |
| 6865 | "Multiple uses for byte %d of page %d", i, iPage); |
| 6866 | break; |
| 6867 | } |
| 6868 | } |
| 6869 | if( cnt!=data[hdr+7] ){ |
| 6870 | checkAppendMsg(pCheck, 0, |
| 6871 | "Fragmented space is %d byte reported as %d on page %d", |
| 6872 | cnt, data[hdr+7], iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6873 | } |
| 6874 | } |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 6875 | check_page_abort: |
| 6876 | if (hit) sqlite3PageFree(hit); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6877 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6878 | releasePage(pPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6879 | return depth+1; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6880 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6881 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6882 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6883 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6884 | /* |
| 6885 | ** This routine does a complete check of the given BTree file. aRoot[] is |
| 6886 | ** an array of pages numbers were each page number is the root page of |
| 6887 | ** a table. nRoot is the number of entries in aRoot. |
| 6888 | ** |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 6889 | ** Write the number of error seen in *pnErr. Except for some memory |
| 6890 | ** allocation errors, nn error message is held in memory obtained from |
| 6891 | ** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is |
| 6892 | ** returned. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6893 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6894 | char *sqlite3BtreeIntegrityCheck( |
| 6895 | Btree *p, /* The btree to be checked */ |
| 6896 | int *aRoot, /* An array of root pages numbers for individual trees */ |
| 6897 | int nRoot, /* Number of entries in aRoot[] */ |
| 6898 | int mxErr, /* Stop reporting errors after this many */ |
| 6899 | int *pnErr /* Write number of errors seen to this variable */ |
| 6900 | ){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6901 | int i; |
| 6902 | int nRef; |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 6903 | IntegrityCk sCheck; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6904 | BtShared *pBt = p->pBt; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 6905 | char zErr[100]; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6906 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6907 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6908 | pBt->db = p->db; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6909 | nRef = sqlite3PagerRefcount(pBt->pPager); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6910 | if( lockBtreeWithRetry(p)!=SQLITE_OK ){ |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 6911 | *pnErr = 1; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6912 | sqlite3BtreeLeave(p); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 6913 | return sqlite3DbStrDup(0, "cannot acquire a read lock on the database"); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 6914 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6915 | sCheck.pBt = pBt; |
| 6916 | sCheck.pPager = pBt->pPager; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 6917 | sCheck.nPage = pagerPagecount(sCheck.pPager); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6918 | sCheck.mxErr = mxErr; |
| 6919 | sCheck.nErr = 0; |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 6920 | sCheck.mallocFailed = 0; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6921 | *pnErr = 0; |
danielk1977 | e5321f0 | 2007-04-27 07:05:44 +0000 | [diff] [blame] | 6922 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6923 | if( pBt->nTrunc!=0 ){ |
| 6924 | sCheck.nPage = pBt->nTrunc; |
| 6925 | } |
| 6926 | #endif |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 6927 | if( sCheck.nPage==0 ){ |
| 6928 | unlockBtreeIfUnused(pBt); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6929 | sqlite3BtreeLeave(p); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 6930 | return 0; |
| 6931 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 6932 | sCheck.anRef = sqlite3Malloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) ); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 6933 | if( !sCheck.anRef ){ |
| 6934 | unlockBtreeIfUnused(pBt); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6935 | *pnErr = 1; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6936 | sqlite3BtreeLeave(p); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 6937 | return 0; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 6938 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6939 | for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; } |
drh | 42cac6d | 2004-11-20 20:31:11 +0000 | [diff] [blame] | 6940 | i = PENDING_BYTE_PAGE(pBt); |
drh | 1f59571 | 2004-06-15 01:40:29 +0000 | [diff] [blame] | 6941 | if( i<=sCheck.nPage ){ |
| 6942 | sCheck.anRef[i] = 1; |
| 6943 | } |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 6944 | sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), 20000); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6945 | |
| 6946 | /* Check the integrity of the freelist |
| 6947 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6948 | checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), |
| 6949 | get4byte(&pBt->pPage1->aData[36]), "Main freelist: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6950 | |
| 6951 | /* Check all the tables. |
| 6952 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6953 | for(i=0; i<nRoot && sCheck.mxErr; i++){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 6954 | if( aRoot[i]==0 ) continue; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6955 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6956 | if( pBt->autoVacuum && aRoot[i]>1 ){ |
| 6957 | checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0); |
| 6958 | } |
| 6959 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6960 | checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6961 | } |
| 6962 | |
| 6963 | /* Make sure every page in the file is referenced |
| 6964 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6965 | for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6966 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6967 | if( sCheck.anRef[i]==0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6968 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6969 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6970 | #else |
| 6971 | /* If the database supports auto-vacuum, make sure no tables contain |
| 6972 | ** references to pointer-map pages. |
| 6973 | */ |
| 6974 | if( sCheck.anRef[i]==0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6975 | (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6976 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
| 6977 | } |
| 6978 | if( sCheck.anRef[i]!=0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6979 | (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6980 | checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i); |
| 6981 | } |
| 6982 | #endif |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6983 | } |
| 6984 | |
| 6985 | /* Make sure this analysis did not leave any unref() pages |
| 6986 | */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6987 | unlockBtreeIfUnused(pBt); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6988 | if( nRef != sqlite3PagerRefcount(pBt->pPager) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6989 | checkAppendMsg(&sCheck, 0, |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6990 | "Outstanding page count goes from %d to %d during this analysis", |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6991 | nRef, sqlite3PagerRefcount(pBt->pPager) |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6992 | ); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6993 | } |
| 6994 | |
| 6995 | /* Clean up and report errors. |
| 6996 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6997 | sqlite3BtreeLeave(p); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6998 | sqlite3_free(sCheck.anRef); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 6999 | if( sCheck.mallocFailed ){ |
| 7000 | sqlite3StrAccumReset(&sCheck.errMsg); |
| 7001 | *pnErr = sCheck.nErr+1; |
| 7002 | return 0; |
| 7003 | } |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 7004 | *pnErr = sCheck.nErr; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 7005 | if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg); |
| 7006 | return sqlite3StrAccumFinish(&sCheck.errMsg); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7007 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 7008 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 7009 | |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 7010 | /* |
| 7011 | ** Return the full pathname of the underlying database file. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 7012 | ** |
| 7013 | ** The pager filename is invariant as long as the pager is |
| 7014 | ** open so it is safe to access without the BtShared mutex. |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 7015 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7016 | const char *sqlite3BtreeGetFilename(Btree *p){ |
| 7017 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7018 | return sqlite3PagerFilename(p->pBt->pPager); |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 7019 | } |
| 7020 | |
| 7021 | /* |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 7022 | ** Return the pathname of the directory that contains the database file. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 7023 | ** |
| 7024 | ** The pager directory name is invariant as long as the pager is |
| 7025 | ** open so it is safe to access without the BtShared mutex. |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 7026 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7027 | const char *sqlite3BtreeGetDirname(Btree *p){ |
| 7028 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7029 | return sqlite3PagerDirname(p->pBt->pPager); |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 7030 | } |
| 7031 | |
| 7032 | /* |
| 7033 | ** Return the pathname of the journal file for this database. The return |
| 7034 | ** value of this routine is the same regardless of whether the journal file |
| 7035 | ** has been created or not. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 7036 | ** |
| 7037 | ** The pager journal filename is invariant as long as the pager is |
| 7038 | ** open so it is safe to access without the BtShared mutex. |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 7039 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7040 | const char *sqlite3BtreeGetJournalname(Btree *p){ |
| 7041 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7042 | return sqlite3PagerJournalname(p->pBt->pPager); |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 7043 | } |
| 7044 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 7045 | #ifndef SQLITE_OMIT_VACUUM |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 7046 | /* |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 7047 | ** Copy the complete content of pBtFrom into pBtTo. A transaction |
| 7048 | ** must be active for both files. |
| 7049 | ** |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7050 | ** The size of file pTo may be reduced by this operation. |
| 7051 | ** If anything goes wrong, the transaction on pTo is rolled back. |
| 7052 | ** |
| 7053 | ** If successful, CommitPhaseOne() may be called on pTo before returning. |
| 7054 | ** The caller should finish committing the transaction on pTo by calling |
| 7055 | ** sqlite3BtreeCommit(). |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 7056 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7057 | static int btreeCopyFile(Btree *pTo, Btree *pFrom){ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 7058 | int rc = SQLITE_OK; |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7059 | Pgno i; |
| 7060 | |
| 7061 | Pgno nFromPage; /* Number of pages in pFrom */ |
| 7062 | Pgno nToPage; /* Number of pages in pTo */ |
| 7063 | Pgno nNewPage; /* Number of pages in pTo after the copy */ |
| 7064 | |
| 7065 | Pgno iSkip; /* Pending byte page in pTo */ |
| 7066 | int nToPageSize; /* Page size of pTo in bytes */ |
| 7067 | int nFromPageSize; /* Page size of pFrom in bytes */ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 7068 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7069 | BtShared *pBtTo = pTo->pBt; |
| 7070 | BtShared *pBtFrom = pFrom->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7071 | pBtTo->db = pTo->db; |
| 7072 | pBtFrom->db = pFrom->db; |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7073 | |
| 7074 | nToPageSize = pBtTo->pageSize; |
| 7075 | nFromPageSize = pBtFrom->pageSize; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7076 | |
| 7077 | if( pTo->inTrans!=TRANS_WRITE || pFrom->inTrans!=TRANS_WRITE ){ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 7078 | return SQLITE_ERROR; |
| 7079 | } |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7080 | if( pBtTo->pCursor ){ |
| 7081 | return SQLITE_BUSY; |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 7082 | } |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 7083 | |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 7084 | nToPage = pagerPagecount(pBtTo->pPager); |
| 7085 | nFromPage = pagerPagecount(pBtFrom->pPager); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7086 | iSkip = PENDING_BYTE_PAGE(pBtTo); |
| 7087 | |
| 7088 | /* Variable nNewPage is the number of pages required to store the |
| 7089 | ** contents of pFrom using the current page-size of pTo. |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 7090 | */ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7091 | nNewPage = ((i64)nFromPage * (i64)nFromPageSize + (i64)nToPageSize - 1) / |
| 7092 | (i64)nToPageSize; |
| 7093 | |
| 7094 | for(i=1; rc==SQLITE_OK && (i<=nToPage || i<=nNewPage); i++){ |
| 7095 | |
| 7096 | /* Journal the original page. |
| 7097 | ** |
| 7098 | ** iSkip is the page number of the locking page (PENDING_BYTE_PAGE) |
| 7099 | ** in database *pTo (before the copy). This page is never written |
| 7100 | ** into the journal file. Unless i==iSkip or the page was not |
| 7101 | ** present in pTo before the copy operation, journal page i from pTo. |
| 7102 | */ |
| 7103 | if( i!=iSkip && i<=nToPage ){ |
danielk1977 | 4abd544 | 2008-05-05 15:26:50 +0000 | [diff] [blame] | 7104 | DbPage *pDbPage = 0; |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7105 | rc = sqlite3PagerGet(pBtTo->pPager, i, &pDbPage); |
danielk1977 | 4abd544 | 2008-05-05 15:26:50 +0000 | [diff] [blame] | 7106 | if( rc==SQLITE_OK ){ |
| 7107 | rc = sqlite3PagerWrite(pDbPage); |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 7108 | if( rc==SQLITE_OK && i>nFromPage ){ |
| 7109 | /* Yeah. It seems wierd to call DontWrite() right after Write(). But |
| 7110 | ** that is because the names of those procedures do not exactly |
| 7111 | ** represent what they do. Write() really means "put this page in the |
| 7112 | ** rollback journal and mark it as dirty so that it will be written |
| 7113 | ** to the database file later." DontWrite() undoes the second part of |
| 7114 | ** that and prevents the page from being written to the database. The |
| 7115 | ** page is still on the rollback journal, though. And that is the |
| 7116 | ** whole point of this block: to put pages on the rollback journal. |
| 7117 | */ |
danielk1977 | a1fa00d | 2008-08-27 15:16:33 +0000 | [diff] [blame] | 7118 | rc = sqlite3PagerDontWrite(pDbPage); |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 7119 | } |
| 7120 | sqlite3PagerUnref(pDbPage); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7121 | } |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7122 | } |
| 7123 | |
| 7124 | /* Overwrite the data in page i of the target database */ |
| 7125 | if( rc==SQLITE_OK && i!=iSkip && i<=nNewPage ){ |
| 7126 | |
| 7127 | DbPage *pToPage = 0; |
| 7128 | sqlite3_int64 iOff; |
| 7129 | |
| 7130 | rc = sqlite3PagerGet(pBtTo->pPager, i, &pToPage); |
| 7131 | if( rc==SQLITE_OK ){ |
| 7132 | rc = sqlite3PagerWrite(pToPage); |
| 7133 | } |
| 7134 | |
| 7135 | for( |
| 7136 | iOff=(i-1)*nToPageSize; |
| 7137 | rc==SQLITE_OK && iOff<i*nToPageSize; |
| 7138 | iOff += nFromPageSize |
| 7139 | ){ |
| 7140 | DbPage *pFromPage = 0; |
| 7141 | Pgno iFrom = (iOff/nFromPageSize)+1; |
| 7142 | |
| 7143 | if( iFrom==PENDING_BYTE_PAGE(pBtFrom) ){ |
| 7144 | continue; |
| 7145 | } |
| 7146 | |
| 7147 | rc = sqlite3PagerGet(pBtFrom->pPager, iFrom, &pFromPage); |
| 7148 | if( rc==SQLITE_OK ){ |
| 7149 | char *zTo = sqlite3PagerGetData(pToPage); |
| 7150 | char *zFrom = sqlite3PagerGetData(pFromPage); |
| 7151 | int nCopy; |
| 7152 | |
| 7153 | if( nFromPageSize>=nToPageSize ){ |
| 7154 | zFrom += ((i-1)*nToPageSize - ((iFrom-1)*nFromPageSize)); |
| 7155 | nCopy = nToPageSize; |
| 7156 | }else{ |
| 7157 | zTo += (((iFrom-1)*nFromPageSize) - (i-1)*nToPageSize); |
| 7158 | nCopy = nFromPageSize; |
| 7159 | } |
| 7160 | |
| 7161 | memcpy(zTo, zFrom, nCopy); |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 7162 | sqlite3PagerUnref(pFromPage); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7163 | } |
| 7164 | } |
| 7165 | |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 7166 | if( pToPage ){ |
| 7167 | MemPage *p = (MemPage *)sqlite3PagerGetExtra(pToPage); |
| 7168 | p->isInit = 0; |
| 7169 | sqlite3PagerUnref(pToPage); |
| 7170 | } |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7171 | } |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 7172 | } |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7173 | |
| 7174 | /* If things have worked so far, the database file may need to be |
| 7175 | ** truncated. The complex part is that it may need to be truncated to |
| 7176 | ** a size that is not an integer multiple of nToPageSize - the current |
| 7177 | ** page size used by the pager associated with B-Tree pTo. |
| 7178 | ** |
| 7179 | ** For example, say the page-size of pTo is 2048 bytes and the original |
| 7180 | ** number of pages is 5 (10 KB file). If pFrom has a page size of 1024 |
| 7181 | ** bytes and 9 pages, then the file needs to be truncated to 9KB. |
| 7182 | */ |
| 7183 | if( rc==SQLITE_OK ){ |
| 7184 | if( nFromPageSize!=nToPageSize ){ |
| 7185 | sqlite3_file *pFile = sqlite3PagerFile(pBtTo->pPager); |
| 7186 | i64 iSize = (i64)nFromPageSize * (i64)nFromPage; |
| 7187 | i64 iNow = (i64)((nToPage>nNewPage)?nToPage:nNewPage) * (i64)nToPageSize; |
| 7188 | i64 iPending = ((i64)PENDING_BYTE_PAGE(pBtTo)-1) *(i64)nToPageSize; |
| 7189 | |
| 7190 | assert( iSize<=iNow ); |
| 7191 | |
| 7192 | /* Commit phase one syncs the journal file associated with pTo |
| 7193 | ** containing the original data. It does not sync the database file |
| 7194 | ** itself. After doing this it is safe to use OsTruncate() and other |
| 7195 | ** file APIs on the database file directly. |
| 7196 | */ |
| 7197 | pBtTo->db = pTo->db; |
| 7198 | rc = sqlite3PagerCommitPhaseOne(pBtTo->pPager, 0, 0, 1); |
| 7199 | if( iSize<iNow && rc==SQLITE_OK ){ |
| 7200 | rc = sqlite3OsTruncate(pFile, iSize); |
| 7201 | } |
| 7202 | |
| 7203 | /* The loop that copied data from database pFrom to pTo did not |
| 7204 | ** populate the locking page of database pTo. If the page-size of |
| 7205 | ** pFrom is smaller than that of pTo, this means some data will |
| 7206 | ** not have been copied. |
| 7207 | ** |
| 7208 | ** This block copies the missing data from database pFrom to pTo |
| 7209 | ** using file APIs. This is safe because at this point we know that |
| 7210 | ** all of the original data from pTo has been synced into the |
| 7211 | ** journal file. At this point it would be safe to do anything at |
| 7212 | ** all to the database file except truncate it to zero bytes. |
| 7213 | */ |
| 7214 | if( rc==SQLITE_OK && nFromPageSize<nToPageSize && iSize>iPending){ |
| 7215 | i64 iOff; |
| 7216 | for( |
| 7217 | iOff=iPending; |
| 7218 | rc==SQLITE_OK && iOff<(iPending+nToPageSize); |
| 7219 | iOff += nFromPageSize |
| 7220 | ){ |
| 7221 | DbPage *pFromPage = 0; |
| 7222 | Pgno iFrom = (iOff/nFromPageSize)+1; |
| 7223 | |
| 7224 | if( iFrom==PENDING_BYTE_PAGE(pBtFrom) || iFrom>nFromPage ){ |
| 7225 | continue; |
| 7226 | } |
| 7227 | |
| 7228 | rc = sqlite3PagerGet(pBtFrom->pPager, iFrom, &pFromPage); |
| 7229 | if( rc==SQLITE_OK ){ |
| 7230 | char *zFrom = sqlite3PagerGetData(pFromPage); |
danielk1977 | 06249db | 2008-08-23 16:17:55 +0000 | [diff] [blame] | 7231 | rc = sqlite3OsWrite(pFile, zFrom, nFromPageSize, iOff); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7232 | sqlite3PagerUnref(pFromPage); |
| 7233 | } |
| 7234 | } |
| 7235 | } |
| 7236 | |
| 7237 | /* Sync the database file */ |
| 7238 | if( rc==SQLITE_OK ){ |
| 7239 | rc = sqlite3PagerSync(pBtTo->pPager); |
| 7240 | } |
| 7241 | }else{ |
| 7242 | rc = sqlite3PagerTruncate(pBtTo->pPager, nNewPage); |
| 7243 | } |
| 7244 | if( rc==SQLITE_OK ){ |
| 7245 | pBtTo->pageSizeFixed = 0; |
| 7246 | } |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 7247 | } |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 7248 | |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 7249 | if( rc ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7250 | sqlite3BtreeRollback(pTo); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 7251 | } |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7252 | |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 7253 | return rc; |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 7254 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7255 | int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){ |
| 7256 | int rc; |
| 7257 | sqlite3BtreeEnter(pTo); |
| 7258 | sqlite3BtreeEnter(pFrom); |
| 7259 | rc = btreeCopyFile(pTo, pFrom); |
| 7260 | sqlite3BtreeLeave(pFrom); |
| 7261 | sqlite3BtreeLeave(pTo); |
| 7262 | return rc; |
| 7263 | } |
| 7264 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 7265 | #endif /* SQLITE_OMIT_VACUUM */ |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 7266 | |
| 7267 | /* |
| 7268 | ** Return non-zero if a transaction is active. |
| 7269 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7270 | int sqlite3BtreeIsInTrans(Btree *p){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7271 | assert( p==0 || sqlite3_mutex_held(p->db->mutex) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7272 | return (p && (p->inTrans==TRANS_WRITE)); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 7273 | } |
| 7274 | |
| 7275 | /* |
| 7276 | ** Return non-zero if a statement transaction is active. |
| 7277 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7278 | int sqlite3BtreeIsInStmt(Btree *p){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7279 | assert( sqlite3BtreeHoldsMutex(p) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7280 | return (p->pBt && p->pBt->inStmt); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 7281 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 7282 | |
| 7283 | /* |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 7284 | ** Return non-zero if a read (or write) transaction is active. |
| 7285 | */ |
| 7286 | int sqlite3BtreeIsInReadTrans(Btree *p){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7287 | assert( sqlite3_mutex_held(p->db->mutex) ); |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 7288 | return (p && (p->inTrans!=TRANS_NONE)); |
| 7289 | } |
| 7290 | |
| 7291 | /* |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7292 | ** This function returns a pointer to a blob of memory associated with |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 7293 | ** 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] | 7294 | ** purposes (for example, to store a high-level schema associated with |
| 7295 | ** the shared-btree). The btree layer manages reference counting issues. |
| 7296 | ** |
| 7297 | ** The first time this is called on a shared-btree, nBytes bytes of memory |
| 7298 | ** are allocated, zeroed, and returned to the caller. For each subsequent |
| 7299 | ** call the nBytes parameter is ignored and a pointer to the same blob |
| 7300 | ** of memory returned. |
| 7301 | ** |
danielk1977 | 171bfed | 2008-06-23 09:50:50 +0000 | [diff] [blame] | 7302 | ** If the nBytes parameter is 0 and the blob of memory has not yet been |
| 7303 | ** allocated, a null pointer is returned. If the blob has already been |
| 7304 | ** allocated, it is returned as normal. |
| 7305 | ** |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7306 | ** Just before the shared-btree is closed, the function passed as the |
| 7307 | ** xFree argument when the memory allocation was made is invoked on the |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 7308 | ** blob of allocated memory. This function should not call sqlite3_free() |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7309 | ** on the memory, the btree layer does that. |
| 7310 | */ |
| 7311 | void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){ |
| 7312 | BtShared *pBt = p->pBt; |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7313 | sqlite3BtreeEnter(p); |
danielk1977 | 171bfed | 2008-06-23 09:50:50 +0000 | [diff] [blame] | 7314 | if( !pBt->pSchema && nBytes ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 7315 | pBt->pSchema = sqlite3MallocZero(nBytes); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7316 | pBt->xFreeSchema = xFree; |
| 7317 | } |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7318 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7319 | return pBt->pSchema; |
| 7320 | } |
| 7321 | |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 7322 | /* |
| 7323 | ** Return true if another user of the same shared btree as the argument |
| 7324 | ** handle holds an exclusive lock on the sqlite_master table. |
| 7325 | */ |
| 7326 | int sqlite3BtreeSchemaLocked(Btree *p){ |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7327 | int rc; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7328 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7329 | sqlite3BtreeEnter(p); |
| 7330 | rc = (queryTableLock(p, MASTER_ROOT, READ_LOCK)!=SQLITE_OK); |
| 7331 | sqlite3BtreeLeave(p); |
| 7332 | return rc; |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 7333 | } |
| 7334 | |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 7335 | |
| 7336 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 7337 | /* |
| 7338 | ** Obtain a lock on the table whose root page is iTab. The |
| 7339 | ** lock is a write lock if isWritelock is true or a read lock |
| 7340 | ** if it is false. |
| 7341 | */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7342 | int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){ |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 7343 | int rc = SQLITE_OK; |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 7344 | if( p->sharable ){ |
| 7345 | u8 lockType = READ_LOCK + isWriteLock; |
| 7346 | assert( READ_LOCK+1==WRITE_LOCK ); |
| 7347 | assert( isWriteLock==0 || isWriteLock==1 ); |
| 7348 | sqlite3BtreeEnter(p); |
| 7349 | rc = queryTableLock(p, iTab, lockType); |
| 7350 | if( rc==SQLITE_OK ){ |
| 7351 | rc = lockTable(p, iTab, lockType); |
| 7352 | } |
| 7353 | sqlite3BtreeLeave(p); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7354 | } |
| 7355 | return rc; |
| 7356 | } |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 7357 | #endif |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 7358 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7359 | #ifndef SQLITE_OMIT_INCRBLOB |
| 7360 | /* |
| 7361 | ** Argument pCsr must be a cursor opened for writing on an |
| 7362 | ** INTKEY table currently pointing at a valid table entry. |
| 7363 | ** This function modifies the data stored as part of that entry. |
| 7364 | ** Only the data content may only be modified, it is not possible |
| 7365 | ** to change the length of the data stored. |
| 7366 | */ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7367 | int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7368 | assert( cursorHoldsMutex(pCsr) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7369 | assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) ); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7370 | assert(pCsr->isIncrblobHandle); |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 7371 | |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 7372 | restoreCursorPosition(pCsr); |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 7373 | assert( pCsr->eState!=CURSOR_REQUIRESEEK ); |
| 7374 | if( pCsr->eState!=CURSOR_VALID ){ |
| 7375 | return SQLITE_ABORT; |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7376 | } |
| 7377 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7378 | /* Check some preconditions: |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7379 | ** (a) the cursor is open for writing, |
| 7380 | ** (b) there is no read-lock on the table being modified and |
| 7381 | ** (c) the cursor points at a valid row of an intKey table. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7382 | */ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7383 | if( !pCsr->wrFlag ){ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7384 | return SQLITE_READONLY; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7385 | } |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 7386 | assert( !pCsr->pBt->readOnly |
| 7387 | && pCsr->pBt->inTransaction==TRANS_WRITE ); |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 7388 | if( checkReadLocks(pCsr->pBtree, pCsr->pgnoRoot, pCsr, 0) ){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7389 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 7390 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 7391 | if( pCsr->eState==CURSOR_INVALID || !pCsr->apPage[pCsr->iPage]->intKey ){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7392 | return SQLITE_ERROR; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7393 | } |
| 7394 | |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 7395 | return accessPayload(pCsr, offset, amt, (unsigned char *)z, 0, 1); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7396 | } |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7397 | |
| 7398 | /* |
| 7399 | ** 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] | 7400 | ** overflow list for the current row. This is used by cursors opened |
| 7401 | ** for incremental blob IO only. |
| 7402 | ** |
| 7403 | ** This function sets a flag only. The actual page location cache |
| 7404 | ** (stored in BtCursor.aOverflow[]) is allocated and used by function |
| 7405 | ** accessPayload() (the worker function for sqlite3BtreeData() and |
| 7406 | ** sqlite3BtreePutData()). |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7407 | */ |
| 7408 | void sqlite3BtreeCacheOverflow(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7409 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7410 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7411 | assert(!pCur->isIncrblobHandle); |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7412 | assert(!pCur->aOverflow); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7413 | pCur->isIncrblobHandle = 1; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7414 | } |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7415 | #endif |