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 | 518002e | 2008-09-05 05:02:46 +0000 | [diff] [blame^] | 12 | ** $Id: btree.c,v 1.509 2008/09/05 05:02:47 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 | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 37 | |
| 38 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 39 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 40 | /* |
danielk1977 | 502b4e0 | 2008-09-02 14:07:24 +0000 | [diff] [blame] | 41 | ** A list of BtShared objects that are eligible for participation |
| 42 | ** in shared cache. This variable has file scope during normal builds, |
| 43 | ** but the test harness needs to access it so we make it global for |
| 44 | ** test builds. |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 45 | */ |
| 46 | #ifdef SQLITE_TEST |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 47 | BtShared *SQLITE_WSD sqlite3SharedCacheList = 0; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 48 | #else |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 49 | static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 50 | #endif |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 51 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| 52 | |
| 53 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 54 | /* |
| 55 | ** Enable or disable the shared pager and schema features. |
| 56 | ** |
| 57 | ** This routine has no effect on existing database connections. |
| 58 | ** The shared cache setting effects only future calls to |
| 59 | ** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2(). |
| 60 | */ |
| 61 | int sqlite3_enable_shared_cache(int enable){ |
danielk1977 | 502b4e0 | 2008-09-02 14:07:24 +0000 | [diff] [blame] | 62 | sqlite3GlobalConfig.sharedCacheEnabled = enable; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 63 | return SQLITE_OK; |
| 64 | } |
| 65 | #endif |
| 66 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 67 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 68 | /* |
drh | 66cbd15 | 2004-09-01 16:12:25 +0000 | [diff] [blame] | 69 | ** Forward declaration |
| 70 | */ |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 71 | static int checkReadLocks(Btree*, Pgno, BtCursor*, i64); |
drh | 66cbd15 | 2004-09-01 16:12:25 +0000 | [diff] [blame] | 72 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 73 | |
| 74 | #ifdef SQLITE_OMIT_SHARED_CACHE |
| 75 | /* |
| 76 | ** The functions queryTableLock(), lockTable() and unlockAllTables() |
| 77 | ** manipulate entries in the BtShared.pLock linked list used to store |
| 78 | ** shared-cache table level locks. If the library is compiled with the |
| 79 | ** shared-cache feature disabled, then there is only ever one user |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 80 | ** of each BtShared structure and so this locking is not necessary. |
| 81 | ** So define the lock related functions as no-ops. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 82 | */ |
| 83 | #define queryTableLock(a,b,c) SQLITE_OK |
| 84 | #define lockTable(a,b,c) SQLITE_OK |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 85 | #define unlockAllTables(a) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 86 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 87 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 88 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 89 | /* |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 90 | ** Query to see if btree handle p may obtain a lock of type eLock |
| 91 | ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return |
| 92 | ** SQLITE_OK if the lock may be obtained (by calling lockTable()), or |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 93 | ** SQLITE_LOCKED if not. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 94 | */ |
| 95 | static int queryTableLock(Btree *p, Pgno iTab, u8 eLock){ |
| 96 | BtShared *pBt = p->pBt; |
| 97 | BtLock *pIter; |
| 98 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 99 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 100 | assert( eLock==READ_LOCK || eLock==WRITE_LOCK ); |
| 101 | assert( p->db!=0 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 102 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 103 | /* This is a no-op if the shared-cache is not enabled */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 104 | if( !p->sharable ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 105 | return SQLITE_OK; |
| 106 | } |
| 107 | |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 108 | /* If some other connection is holding an exclusive lock, the |
| 109 | ** requested lock may not be obtained. |
| 110 | */ |
| 111 | if( pBt->pExclusive && pBt->pExclusive!=p ){ |
| 112 | return SQLITE_LOCKED; |
| 113 | } |
| 114 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 115 | /* This (along with lockTable()) is where the ReadUncommitted flag is |
| 116 | ** dealt with. If the caller is querying for a read-lock and the flag is |
| 117 | ** set, it is unconditionally granted - even if there are write-locks |
| 118 | ** on the table. If a write-lock is requested, the ReadUncommitted flag |
| 119 | ** is not considered. |
| 120 | ** |
| 121 | ** In function lockTable(), if a read-lock is demanded and the |
| 122 | ** ReadUncommitted flag is set, no entry is added to the locks list |
| 123 | ** (BtShared.pLock). |
| 124 | ** |
| 125 | ** To summarize: If the ReadUncommitted flag is set, then read cursors do |
| 126 | ** not create or respect table locks. The locking procedure for a |
| 127 | ** write-cursor does not change. |
| 128 | */ |
| 129 | if( |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 130 | 0==(p->db->flags&SQLITE_ReadUncommitted) || |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 131 | eLock==WRITE_LOCK || |
drh | 47ded16 | 2006-01-06 01:42:58 +0000 | [diff] [blame] | 132 | iTab==MASTER_ROOT |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 133 | ){ |
| 134 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 135 | if( pIter->pBtree!=p && pIter->iTable==iTab && |
| 136 | (pIter->eLock!=eLock || eLock!=READ_LOCK) ){ |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 137 | return SQLITE_LOCKED; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 138 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | return SQLITE_OK; |
| 142 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 143 | #endif /* !SQLITE_OMIT_SHARED_CACHE */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 144 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 145 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 146 | /* |
| 147 | ** Add a lock on the table with root-page iTable to the shared-btree used |
| 148 | ** by Btree handle p. Parameter eLock must be either READ_LOCK or |
| 149 | ** WRITE_LOCK. |
| 150 | ** |
| 151 | ** SQLITE_OK is returned if the lock is added successfully. SQLITE_BUSY and |
| 152 | ** SQLITE_NOMEM may also be returned. |
| 153 | */ |
| 154 | static int lockTable(Btree *p, Pgno iTable, u8 eLock){ |
| 155 | BtShared *pBt = p->pBt; |
| 156 | BtLock *pLock = 0; |
| 157 | BtLock *pIter; |
| 158 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 159 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 160 | assert( eLock==READ_LOCK || eLock==WRITE_LOCK ); |
| 161 | assert( p->db!=0 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 162 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 163 | /* This is a no-op if the shared-cache is not enabled */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 164 | if( !p->sharable ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 165 | return SQLITE_OK; |
| 166 | } |
| 167 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 168 | assert( SQLITE_OK==queryTableLock(p, iTable, eLock) ); |
| 169 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 170 | /* If the read-uncommitted flag is set and a read-lock is requested, |
| 171 | ** return early without adding an entry to the BtShared.pLock list. See |
| 172 | ** comment in function queryTableLock() for more info on handling |
| 173 | ** the ReadUncommitted flag. |
| 174 | */ |
| 175 | if( |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 176 | (p->db->flags&SQLITE_ReadUncommitted) && |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 177 | (eLock==READ_LOCK) && |
drh | 47ded16 | 2006-01-06 01:42:58 +0000 | [diff] [blame] | 178 | iTable!=MASTER_ROOT |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 179 | ){ |
| 180 | return SQLITE_OK; |
| 181 | } |
| 182 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 183 | /* First search the list for an existing lock on this table. */ |
| 184 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 185 | if( pIter->iTable==iTable && pIter->pBtree==p ){ |
| 186 | pLock = pIter; |
| 187 | break; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | /* If the above search did not find a BtLock struct associating Btree p |
| 192 | ** with table iTable, allocate one and link it into the list. |
| 193 | */ |
| 194 | if( !pLock ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 195 | pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock)); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 196 | if( !pLock ){ |
| 197 | return SQLITE_NOMEM; |
| 198 | } |
| 199 | pLock->iTable = iTable; |
| 200 | pLock->pBtree = p; |
| 201 | pLock->pNext = pBt->pLock; |
| 202 | pBt->pLock = pLock; |
| 203 | } |
| 204 | |
| 205 | /* Set the BtLock.eLock variable to the maximum of the current lock |
| 206 | ** and the requested lock. This means if a write-lock was already held |
| 207 | ** and a read-lock requested, we don't incorrectly downgrade the lock. |
| 208 | */ |
| 209 | assert( WRITE_LOCK>READ_LOCK ); |
danielk1977 | 5118b91 | 2005-12-30 16:31:53 +0000 | [diff] [blame] | 210 | if( eLock>pLock->eLock ){ |
| 211 | pLock->eLock = eLock; |
| 212 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 213 | |
| 214 | return SQLITE_OK; |
| 215 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 216 | #endif /* !SQLITE_OMIT_SHARED_CACHE */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 217 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 218 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 219 | /* |
| 220 | ** Release all the table locks (locks obtained via calls to the lockTable() |
| 221 | ** procedure) held by Btree handle p. |
| 222 | */ |
| 223 | static void unlockAllTables(Btree *p){ |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 224 | BtShared *pBt = p->pBt; |
| 225 | BtLock **ppIter = &pBt->pLock; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 226 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 227 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 228 | assert( p->sharable || 0==*ppIter ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 229 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 230 | while( *ppIter ){ |
| 231 | BtLock *pLock = *ppIter; |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 232 | assert( pBt->pExclusive==0 || pBt->pExclusive==pLock->pBtree ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 233 | if( pLock->pBtree==p ){ |
| 234 | *ppIter = pLock->pNext; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 235 | sqlite3_free(pLock); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 236 | }else{ |
| 237 | ppIter = &pLock->pNext; |
| 238 | } |
| 239 | } |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 240 | |
| 241 | if( pBt->pExclusive==p ){ |
| 242 | pBt->pExclusive = 0; |
| 243 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 244 | } |
| 245 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| 246 | |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 247 | static void releasePage(MemPage *pPage); /* Forward reference */ |
| 248 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 249 | /* |
| 250 | ** Verify that the cursor holds a mutex on the BtShared |
| 251 | */ |
| 252 | #ifndef NDEBUG |
| 253 | static int cursorHoldsMutex(BtCursor *p){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 254 | return sqlite3_mutex_held(p->pBt->mutex); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 255 | } |
| 256 | #endif |
| 257 | |
| 258 | |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 259 | #ifndef SQLITE_OMIT_INCRBLOB |
| 260 | /* |
| 261 | ** Invalidate the overflow page-list cache for cursor pCur, if any. |
| 262 | */ |
| 263 | static void invalidateOverflowCache(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 264 | assert( cursorHoldsMutex(pCur) ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 265 | sqlite3_free(pCur->aOverflow); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 266 | pCur->aOverflow = 0; |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | ** Invalidate the overflow page-list cache for all cursors opened |
| 271 | ** on the shared btree structure pBt. |
| 272 | */ |
| 273 | static void invalidateAllOverflowCache(BtShared *pBt){ |
| 274 | BtCursor *p; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 275 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 276 | for(p=pBt->pCursor; p; p=p->pNext){ |
| 277 | invalidateOverflowCache(p); |
| 278 | } |
| 279 | } |
| 280 | #else |
| 281 | #define invalidateOverflowCache(x) |
| 282 | #define invalidateAllOverflowCache(x) |
| 283 | #endif |
| 284 | |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 285 | /* |
| 286 | ** Save the current cursor position in the variables BtCursor.nKey |
| 287 | ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK. |
| 288 | */ |
| 289 | static int saveCursorPosition(BtCursor *pCur){ |
| 290 | int rc; |
| 291 | |
| 292 | assert( CURSOR_VALID==pCur->eState ); |
| 293 | assert( 0==pCur->pKey ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 294 | assert( cursorHoldsMutex(pCur) ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 295 | |
| 296 | rc = sqlite3BtreeKeySize(pCur, &pCur->nKey); |
| 297 | |
| 298 | /* If this is an intKey table, then the above call to BtreeKeySize() |
| 299 | ** stores the integer key in pCur->nKey. In this case this value is |
| 300 | ** all that is required. Otherwise, if pCur is not open on an intKey |
| 301 | ** table, then malloc space for and store the pCur->nKey bytes of key |
| 302 | ** data. |
| 303 | */ |
| 304 | if( rc==SQLITE_OK && 0==pCur->pPage->intKey){ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 305 | void *pKey = sqlite3Malloc(pCur->nKey); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 306 | if( pKey ){ |
| 307 | rc = sqlite3BtreeKey(pCur, 0, pCur->nKey, pKey); |
| 308 | if( rc==SQLITE_OK ){ |
| 309 | pCur->pKey = pKey; |
| 310 | }else{ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 311 | sqlite3_free(pKey); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 312 | } |
| 313 | }else{ |
| 314 | rc = SQLITE_NOMEM; |
| 315 | } |
| 316 | } |
| 317 | assert( !pCur->pPage->intKey || !pCur->pKey ); |
| 318 | |
| 319 | if( rc==SQLITE_OK ){ |
| 320 | releasePage(pCur->pPage); |
| 321 | pCur->pPage = 0; |
| 322 | pCur->eState = CURSOR_REQUIRESEEK; |
| 323 | } |
| 324 | |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 325 | invalidateOverflowCache(pCur); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 326 | return rc; |
| 327 | } |
| 328 | |
| 329 | /* |
| 330 | ** Save the positions of all cursors except pExcept open on the table |
| 331 | ** with root-page iRoot. Usually, this is called just before cursor |
| 332 | ** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()). |
| 333 | */ |
| 334 | static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){ |
| 335 | BtCursor *p; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 336 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 337 | assert( pExcept==0 || pExcept->pBt==pBt ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 338 | for(p=pBt->pCursor; p; p=p->pNext){ |
| 339 | if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) && |
| 340 | p->eState==CURSOR_VALID ){ |
| 341 | int rc = saveCursorPosition(p); |
| 342 | if( SQLITE_OK!=rc ){ |
| 343 | return rc; |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | return SQLITE_OK; |
| 348 | } |
| 349 | |
| 350 | /* |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 351 | ** Clear the current cursor position. |
| 352 | */ |
| 353 | static void clearCursorPosition(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 354 | assert( cursorHoldsMutex(pCur) ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 355 | sqlite3_free(pCur->pKey); |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 356 | pCur->pKey = 0; |
| 357 | pCur->eState = CURSOR_INVALID; |
| 358 | } |
| 359 | |
| 360 | /* |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 361 | ** Restore the cursor to the position it was in (or as close to as possible) |
| 362 | ** when saveCursorPosition() was called. Note that this call deletes the |
| 363 | ** saved position info stored by saveCursorPosition(), so there can be |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 364 | ** at most one effective restoreCursorPosition() call after each |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 365 | ** saveCursorPosition(). |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 366 | */ |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 367 | int sqlite3BtreeRestoreCursorPosition(BtCursor *pCur){ |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 368 | int rc; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 369 | assert( cursorHoldsMutex(pCur) ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 370 | assert( pCur->eState>=CURSOR_REQUIRESEEK ); |
| 371 | if( pCur->eState==CURSOR_FAULT ){ |
| 372 | return pCur->skip; |
| 373 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 374 | pCur->eState = CURSOR_INVALID; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 375 | rc = sqlite3BtreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skip); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 376 | if( rc==SQLITE_OK ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 377 | sqlite3_free(pCur->pKey); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 378 | pCur->pKey = 0; |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 379 | assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 380 | } |
| 381 | return rc; |
| 382 | } |
| 383 | |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 384 | #define restoreCursorPosition(p) \ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 385 | (p->eState>=CURSOR_REQUIRESEEK ? \ |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 386 | sqlite3BtreeRestoreCursorPosition(p) : \ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 387 | SQLITE_OK) |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 388 | |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 389 | /* |
| 390 | ** Determine whether or not a cursor has moved from the position it |
| 391 | ** was last placed at. Cursor can move when the row they are pointing |
| 392 | ** at is deleted out from under them. |
| 393 | ** |
| 394 | ** This routine returns an error code if something goes wrong. The |
| 395 | ** integer *pHasMoved is set to one if the cursor has moved and 0 if not. |
| 396 | */ |
| 397 | int sqlite3BtreeCursorHasMoved(BtCursor *pCur, int *pHasMoved){ |
| 398 | int rc; |
| 399 | |
| 400 | rc = restoreCursorPosition(pCur); |
| 401 | if( rc ){ |
| 402 | *pHasMoved = 1; |
| 403 | return rc; |
| 404 | } |
| 405 | if( pCur->eState!=CURSOR_VALID || pCur->skip!=0 ){ |
| 406 | *pHasMoved = 1; |
| 407 | }else{ |
| 408 | *pHasMoved = 0; |
| 409 | } |
| 410 | return SQLITE_OK; |
| 411 | } |
| 412 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 413 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 414 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 415 | ** Given a page number of a regular database page, return the page |
| 416 | ** number for the pointer-map page that contains the entry for the |
| 417 | ** input page number. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 418 | */ |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 419 | static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 420 | int nPagesPerMapPage, iPtrMap, ret; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 421 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 422 | nPagesPerMapPage = (pBt->usableSize/5)+1; |
| 423 | iPtrMap = (pgno-2)/nPagesPerMapPage; |
| 424 | ret = (iPtrMap*nPagesPerMapPage) + 2; |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 425 | if( ret==PENDING_BYTE_PAGE(pBt) ){ |
| 426 | ret++; |
| 427 | } |
| 428 | return ret; |
| 429 | } |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 430 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 431 | /* |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 432 | ** Write an entry into the pointer map. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 433 | ** |
| 434 | ** This routine updates the pointer map entry for page number 'key' |
| 435 | ** so that it maps to type 'eType' and parent page number 'pgno'. |
| 436 | ** An error code is returned if something goes wrong, otherwise SQLITE_OK. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 437 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 438 | static int ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 439 | DbPage *pDbPage; /* The pointer map page */ |
| 440 | u8 *pPtrmap; /* The pointer map data */ |
| 441 | Pgno iPtrmap; /* The pointer map page number */ |
| 442 | int offset; /* Offset in pointer map page */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 443 | int rc; |
| 444 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 445 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 446 | /* The master-journal page number must never be used as a pointer map page */ |
| 447 | assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) ); |
| 448 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 449 | assert( pBt->autoVacuum ); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 450 | if( key==0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 451 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 452 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 453 | iPtrmap = PTRMAP_PAGENO(pBt, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 454 | rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 455 | if( rc!=SQLITE_OK ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 456 | return rc; |
| 457 | } |
danielk1977 | 8c666b1 | 2008-07-18 09:34:57 +0000 | [diff] [blame] | 458 | offset = PTRMAP_PTROFFSET(iPtrmap, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 459 | pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 460 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 461 | if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){ |
| 462 | TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent)); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 463 | rc = sqlite3PagerWrite(pDbPage); |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 464 | if( rc==SQLITE_OK ){ |
| 465 | pPtrmap[offset] = eType; |
| 466 | put4byte(&pPtrmap[offset+1], parent); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 467 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 468 | } |
| 469 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 470 | sqlite3PagerUnref(pDbPage); |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 471 | return rc; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | /* |
| 475 | ** Read an entry from the pointer map. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 476 | ** |
| 477 | ** This routine retrieves the pointer map entry for page 'key', writing |
| 478 | ** the type and parent page number to *pEType and *pPgno respectively. |
| 479 | ** An error code is returned if something goes wrong, otherwise SQLITE_OK. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 480 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 481 | static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 482 | DbPage *pDbPage; /* The pointer map page */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 483 | int iPtrmap; /* Pointer map page index */ |
| 484 | u8 *pPtrmap; /* Pointer map page data */ |
| 485 | int offset; /* Offset of entry in pointer map */ |
| 486 | int rc; |
| 487 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 488 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 489 | |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 490 | iPtrmap = PTRMAP_PAGENO(pBt, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 491 | rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 492 | if( rc!=0 ){ |
| 493 | return rc; |
| 494 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 495 | pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 496 | |
danielk1977 | 8c666b1 | 2008-07-18 09:34:57 +0000 | [diff] [blame] | 497 | offset = PTRMAP_PTROFFSET(iPtrmap, key); |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 498 | assert( pEType!=0 ); |
| 499 | *pEType = pPtrmap[offset]; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 500 | if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 501 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 502 | sqlite3PagerUnref(pDbPage); |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 503 | if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 504 | return SQLITE_OK; |
| 505 | } |
| 506 | |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 507 | #else /* if defined SQLITE_OMIT_AUTOVACUUM */ |
| 508 | #define ptrmapPut(w,x,y,z) SQLITE_OK |
| 509 | #define ptrmapGet(w,x,y,z) SQLITE_OK |
| 510 | #define ptrmapPutOvfl(y,z) SQLITE_OK |
| 511 | #endif |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 512 | |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 513 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 514 | ** Given a btree page and a cell index (0 means the first cell on |
| 515 | ** the page, 1 means the second cell, and so forth) return a pointer |
| 516 | ** to the cell content. |
| 517 | ** |
| 518 | ** This routine works only for pages that do not contain overflow cells. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 519 | */ |
drh | 1688c86 | 2008-07-18 02:44:17 +0000 | [diff] [blame] | 520 | #define findCell(P,I) \ |
| 521 | ((P)->aData + ((P)->maskPage & get2byte(&(P)->aData[(P)->cellOffset+2*(I)]))) |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 522 | |
| 523 | /* |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 524 | ** This a more complex version of findCell() that works for |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 525 | ** pages that do contain overflow cells. See insert |
| 526 | */ |
| 527 | static u8 *findOverflowCell(MemPage *pPage, int iCell){ |
| 528 | int i; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 529 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 530 | for(i=pPage->nOverflow-1; i>=0; i--){ |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 531 | int k; |
| 532 | struct _OvflCell *pOvfl; |
| 533 | pOvfl = &pPage->aOvfl[i]; |
| 534 | k = pOvfl->idx; |
| 535 | if( k<=iCell ){ |
| 536 | if( k==iCell ){ |
| 537 | return pOvfl->pCell; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 538 | } |
| 539 | iCell--; |
| 540 | } |
| 541 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 542 | return findCell(pPage, iCell); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | /* |
| 546 | ** Parse a cell content block and fill in the CellInfo structure. There |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 547 | ** are two versions of this function. sqlite3BtreeParseCell() takes a |
| 548 | ** cell index as the second argument and sqlite3BtreeParseCellPtr() |
| 549 | ** takes a pointer to the body of the cell as its second argument. |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 550 | ** |
| 551 | ** Within this file, the parseCell() macro can be called instead of |
| 552 | ** sqlite3BtreeParseCellPtr(). Using some compilers, this will be faster. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 553 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 554 | void sqlite3BtreeParseCellPtr( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 555 | MemPage *pPage, /* Page containing the cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 556 | u8 *pCell, /* Pointer to the cell text. */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 557 | CellInfo *pInfo /* Fill in this structure */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 558 | ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 559 | int n; /* Number bytes in cell content header */ |
| 560 | u32 nPayload; /* Number of bytes of cell payload */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 561 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 562 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 563 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 564 | pInfo->pCell = pCell; |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 565 | assert( pPage->leaf==0 || pPage->leaf==1 ); |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 566 | n = pPage->childPtrSize; |
| 567 | assert( n==4-4*pPage->leaf ); |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 568 | if( pPage->intKey ){ |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 569 | if( pPage->hasData ){ |
| 570 | n += getVarint32(&pCell[n], nPayload); |
| 571 | }else{ |
| 572 | nPayload = 0; |
| 573 | } |
| 574 | n += getVarint(&pCell[n], (u64*)&pInfo->nKey); |
| 575 | pInfo->nData = nPayload; |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 576 | }else{ |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 577 | pInfo->nData = 0; |
| 578 | n += getVarint32(&pCell[n], nPayload); |
| 579 | pInfo->nKey = nPayload; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 580 | } |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 581 | pInfo->nPayload = nPayload; |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 582 | pInfo->nHeader = n; |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 583 | if( likely(nPayload<=pPage->maxLocal) ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 584 | /* This is the (easy) common case where the entire payload fits |
| 585 | ** on the local page. No overflow is required. |
| 586 | */ |
| 587 | int nSize; /* Total size of cell content in bytes */ |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 588 | nSize = nPayload + n; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 589 | pInfo->nLocal = nPayload; |
| 590 | pInfo->iOverflow = 0; |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 591 | if( (nSize & ~3)==0 ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 592 | nSize = 4; /* Minimum cell size is 4 */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 593 | } |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 594 | pInfo->nSize = nSize; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 595 | }else{ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 596 | /* If the payload will not fit completely on the local page, we have |
| 597 | ** to decide how much to store locally and how much to spill onto |
| 598 | ** overflow pages. The strategy is to minimize the amount of unused |
| 599 | ** space on overflow pages while keeping the amount of local storage |
| 600 | ** in between minLocal and maxLocal. |
| 601 | ** |
| 602 | ** Warning: changing the way overflow payload is distributed in any |
| 603 | ** way will result in an incompatible file format. |
| 604 | */ |
| 605 | int minLocal; /* Minimum amount of payload held locally */ |
| 606 | int maxLocal; /* Maximum amount of payload held locally */ |
| 607 | int surplus; /* Overflow payload available for local storage */ |
| 608 | |
| 609 | minLocal = pPage->minLocal; |
| 610 | maxLocal = pPage->maxLocal; |
| 611 | surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 612 | if( surplus <= maxLocal ){ |
| 613 | pInfo->nLocal = surplus; |
| 614 | }else{ |
| 615 | pInfo->nLocal = minLocal; |
| 616 | } |
| 617 | pInfo->iOverflow = pInfo->nLocal + n; |
| 618 | pInfo->nSize = pInfo->iOverflow + 4; |
| 619 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 620 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 621 | #define parseCell(pPage, iCell, pInfo) \ |
| 622 | sqlite3BtreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo)) |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 623 | void sqlite3BtreeParseCell( |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 624 | MemPage *pPage, /* Page containing the cell */ |
| 625 | int iCell, /* The cell index. First cell is 0 */ |
| 626 | CellInfo *pInfo /* Fill in this structure */ |
| 627 | ){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 628 | parseCell(pPage, iCell, pInfo); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 629 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 630 | |
| 631 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 632 | ** Compute the total number of bytes that a Cell needs in the cell |
| 633 | ** data area of the btree-page. The return number includes the cell |
| 634 | ** data header and the local payload, but not any overflow page or |
| 635 | ** the space used by the cell pointer. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 636 | */ |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 637 | #ifndef NDEBUG |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 638 | static u16 cellSize(MemPage *pPage, int iCell){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 639 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 640 | sqlite3BtreeParseCell(pPage, iCell, &info); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 641 | return info.nSize; |
| 642 | } |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 643 | #endif |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 644 | static u16 cellSizePtr(MemPage *pPage, u8 *pCell){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 645 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 646 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 647 | return info.nSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 648 | } |
| 649 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 650 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 651 | /* |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 652 | ** If the cell pCell, part of page pPage contains a pointer |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 653 | ** to an overflow page, insert an entry into the pointer-map |
| 654 | ** for the overflow page. |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 655 | */ |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 656 | static int ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell){ |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 657 | CellInfo info; |
| 658 | assert( pCell!=0 ); |
| 659 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
| 660 | assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); |
| 661 | if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ |
| 662 | Pgno ovfl = get4byte(&pCell[info.iOverflow]); |
| 663 | return ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 664 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 665 | return SQLITE_OK; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 666 | } |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 667 | /* |
| 668 | ** If the cell with index iCell on page pPage contains a pointer |
| 669 | ** to an overflow page, insert an entry into the pointer-map |
| 670 | ** for the overflow page. |
| 671 | */ |
| 672 | static int ptrmapPutOvfl(MemPage *pPage, int iCell){ |
| 673 | u8 *pCell; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 674 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 675 | pCell = findOverflowCell(pPage, iCell); |
| 676 | return ptrmapPutOvflPtr(pPage, pCell); |
| 677 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 678 | #endif |
| 679 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 680 | |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 681 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 682 | ** Defragment the page given. All Cells are moved to the |
drh | 3a4a2d4 | 2005-11-24 14:24:28 +0000 | [diff] [blame] | 683 | ** end of the page and all free space is collected into one |
| 684 | ** big FreeBlk that occurs in between the header and cell |
drh | 31beae9 | 2005-11-24 14:34:36 +0000 | [diff] [blame] | 685 | ** pointer array and the cell content area. |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 686 | */ |
danielk1977 | 474b7cc | 2008-07-09 11:49:46 +0000 | [diff] [blame] | 687 | static void defragmentPage(MemPage *pPage){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 688 | int i; /* Loop counter */ |
| 689 | int pc; /* Address of a i-th cell */ |
| 690 | int addr; /* Offset of first byte after cell pointer array */ |
| 691 | int hdr; /* Offset to the page header */ |
| 692 | int size; /* Size of a cell */ |
| 693 | int usableSize; /* Number of usable bytes on a page */ |
| 694 | int cellOffset; /* Offset to the cell pointer array */ |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 695 | int cbrk; /* Offset to the cell content area */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 696 | int nCell; /* Number of cells on the page */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 697 | unsigned char *data; /* The page data */ |
| 698 | unsigned char *temp; /* Temp area for cell content */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 699 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 700 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 701 | assert( pPage->pBt!=0 ); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 702 | assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 703 | assert( pPage->nOverflow==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 704 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 26b7994 | 2007-11-28 16:19:56 +0000 | [diff] [blame] | 705 | temp = sqlite3PagerTempSpace(pPage->pBt->pPager); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 706 | data = pPage->aData; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 707 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 708 | cellOffset = pPage->cellOffset; |
| 709 | nCell = pPage->nCell; |
| 710 | assert( nCell==get2byte(&data[hdr+3]) ); |
| 711 | usableSize = pPage->pBt->usableSize; |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 712 | cbrk = get2byte(&data[hdr+5]); |
| 713 | memcpy(&temp[cbrk], &data[cbrk], usableSize - cbrk); |
| 714 | cbrk = usableSize; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 715 | for(i=0; i<nCell; i++){ |
| 716 | u8 *pAddr; /* The i-th cell pointer */ |
| 717 | pAddr = &data[cellOffset + i*2]; |
| 718 | pc = get2byte(pAddr); |
| 719 | assert( pc<pPage->pBt->usableSize ); |
| 720 | size = cellSizePtr(pPage, &temp[pc]); |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 721 | cbrk -= size; |
| 722 | memcpy(&data[cbrk], &temp[pc], size); |
| 723 | put2byte(pAddr, cbrk); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 724 | } |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 725 | assert( cbrk>=cellOffset+2*nCell ); |
| 726 | put2byte(&data[hdr+5], cbrk); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 727 | data[hdr+1] = 0; |
| 728 | data[hdr+2] = 0; |
| 729 | data[hdr+7] = 0; |
| 730 | addr = cellOffset+2*nCell; |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 731 | memset(&data[addr], 0, cbrk-addr); |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 732 | } |
| 733 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 734 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 735 | ** Allocate nByte bytes of space on a page. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 736 | ** |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 737 | ** Return the index into pPage->aData[] of the first byte of |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 738 | ** the new allocation. The caller guarantees that there is enough |
| 739 | ** space. This routine will never fail. |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 740 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 741 | ** If the page contains nBytes of free space but does not contain |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 742 | ** nBytes of contiguous free space, then this routine automatically |
| 743 | ** calls defragementPage() to consolidate all free space before |
| 744 | ** allocating the new chunk. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 745 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 746 | static int allocateSpace(MemPage *pPage, int nByte){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 747 | int addr, pc, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 748 | int size; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 749 | int nFrag; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 750 | int top; |
| 751 | int nCell; |
| 752 | int cellOffset; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 753 | unsigned char *data; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 754 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 755 | data = pPage->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 756 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 757 | assert( pPage->pBt ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 758 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 759 | assert( nByte>=0 ); /* Minimum cell size is 4 */ |
| 760 | assert( pPage->nFree>=nByte ); |
| 761 | assert( pPage->nOverflow==0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 762 | pPage->nFree -= nByte; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 763 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 764 | |
| 765 | nFrag = data[hdr+7]; |
| 766 | if( nFrag<60 ){ |
| 767 | /* Search the freelist looking for a slot big enough to satisfy the |
| 768 | ** space request. */ |
| 769 | addr = hdr+1; |
| 770 | while( (pc = get2byte(&data[addr]))>0 ){ |
| 771 | size = get2byte(&data[pc+2]); |
| 772 | if( size>=nByte ){ |
| 773 | if( size<nByte+4 ){ |
| 774 | memcpy(&data[addr], &data[pc], 2); |
| 775 | data[hdr+7] = nFrag + size - nByte; |
| 776 | return pc; |
| 777 | }else{ |
| 778 | put2byte(&data[pc+2], size-nByte); |
| 779 | return pc + size - nByte; |
| 780 | } |
| 781 | } |
| 782 | addr = pc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 783 | } |
| 784 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 785 | |
| 786 | /* Allocate memory from the gap in between the cell pointer array |
| 787 | ** and the cell content area. |
| 788 | */ |
| 789 | top = get2byte(&data[hdr+5]); |
| 790 | nCell = get2byte(&data[hdr+3]); |
| 791 | cellOffset = pPage->cellOffset; |
| 792 | if( nFrag>=60 || cellOffset + 2*nCell > top - nByte ){ |
danielk1977 | 474b7cc | 2008-07-09 11:49:46 +0000 | [diff] [blame] | 793 | defragmentPage(pPage); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 794 | top = get2byte(&data[hdr+5]); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 795 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 796 | top -= nByte; |
| 797 | assert( cellOffset + 2*nCell <= top ); |
| 798 | put2byte(&data[hdr+5], top); |
| 799 | return top; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 803 | ** Return a section of the pPage->aData to the freelist. |
| 804 | ** The first byte of the new free block is pPage->aDisk[start] |
| 805 | ** and the size of the block is "size" bytes. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 806 | ** |
| 807 | ** Most of the effort here is involved in coalesing adjacent |
| 808 | ** free blocks into a single big free block. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 809 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 810 | static void freeSpace(MemPage *pPage, int start, int size){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 811 | int addr, pbegin, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 812 | unsigned char *data = pPage->aData; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 813 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 814 | assert( pPage->pBt!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 815 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 816 | assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) ); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 817 | assert( (start + size)<=pPage->pBt->usableSize ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 818 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 819 | assert( size>=0 ); /* Minimum cell size is 4 */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 820 | |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 821 | #ifdef SQLITE_SECURE_DELETE |
| 822 | /* Overwrite deleted information with zeros when the SECURE_DELETE |
| 823 | ** option is enabled at compile-time */ |
| 824 | memset(&data[start], 0, size); |
| 825 | #endif |
| 826 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 827 | /* Add the space back into the linked list of freeblocks */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 828 | hdr = pPage->hdrOffset; |
| 829 | addr = hdr + 1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 830 | while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 831 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 832 | assert( pbegin>addr ); |
| 833 | addr = pbegin; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 834 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 835 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 836 | assert( pbegin>addr || pbegin==0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 837 | put2byte(&data[addr], start); |
| 838 | put2byte(&data[start], pbegin); |
| 839 | put2byte(&data[start+2], size); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 840 | pPage->nFree += size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 841 | |
| 842 | /* Coalesce adjacent free blocks */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 843 | addr = pPage->hdrOffset + 1; |
| 844 | while( (pbegin = get2byte(&data[addr]))>0 ){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 845 | int pnext, psize; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 846 | assert( pbegin>addr ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 847 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 848 | pnext = get2byte(&data[pbegin]); |
| 849 | psize = get2byte(&data[pbegin+2]); |
| 850 | if( pbegin + psize + 3 >= pnext && pnext>0 ){ |
| 851 | int frag = pnext - (pbegin+psize); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 852 | assert( frag<=data[pPage->hdrOffset+7] ); |
| 853 | data[pPage->hdrOffset+7] -= frag; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 854 | put2byte(&data[pbegin], get2byte(&data[pnext])); |
| 855 | put2byte(&data[pbegin+2], pnext+get2byte(&data[pnext+2])-pbegin); |
| 856 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 857 | addr = pbegin; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 858 | } |
| 859 | } |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 860 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 861 | /* If the cell content area begins with a freeblock, remove it. */ |
| 862 | if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){ |
| 863 | int top; |
| 864 | pbegin = get2byte(&data[hdr+1]); |
| 865 | memcpy(&data[hdr+1], &data[pbegin], 2); |
| 866 | top = get2byte(&data[hdr+5]); |
| 867 | put2byte(&data[hdr+5], top + get2byte(&data[pbegin+2])); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 868 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 872 | ** Decode the flags byte (the first byte of the header) for a page |
| 873 | ** and initialize fields of the MemPage structure accordingly. |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 874 | ** |
| 875 | ** Only the following combinations are supported. Anything different |
| 876 | ** indicates a corrupt database files: |
| 877 | ** |
| 878 | ** PTF_ZERODATA |
| 879 | ** PTF_ZERODATA | PTF_LEAF |
| 880 | ** PTF_LEAFDATA | PTF_INTKEY |
| 881 | ** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 882 | */ |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 883 | static int decodeFlags(MemPage *pPage, int flagByte){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 884 | BtShared *pBt; /* A copy of pPage->pBt */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 885 | |
| 886 | assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 887 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 888 | pPage->leaf = flagByte>>3; assert( PTF_LEAF == 1<<3 ); |
| 889 | flagByte &= ~PTF_LEAF; |
| 890 | pPage->childPtrSize = 4-4*pPage->leaf; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 891 | pBt = pPage->pBt; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 892 | if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){ |
| 893 | pPage->intKey = 1; |
| 894 | pPage->hasData = pPage->leaf; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 895 | pPage->maxLocal = pBt->maxLeaf; |
| 896 | pPage->minLocal = pBt->minLeaf; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 897 | }else if( flagByte==PTF_ZERODATA ){ |
| 898 | pPage->intKey = 0; |
| 899 | pPage->hasData = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 900 | pPage->maxLocal = pBt->maxLocal; |
| 901 | pPage->minLocal = pBt->minLocal; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 902 | }else{ |
| 903 | return SQLITE_CORRUPT_BKPT; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 904 | } |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 905 | return SQLITE_OK; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 909 | ** Initialize the auxiliary information for a disk block. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 910 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 911 | ** The pParent parameter must be a pointer to the MemPage which |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 912 | ** is the parent of the page being initialized. The root of a |
| 913 | ** BTree has no parent and so for that page, pParent==NULL. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 914 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 915 | ** Return SQLITE_OK on success. If we see that the page does |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 916 | ** not contain a well-formed database page, then return |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 917 | ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not |
| 918 | ** guarantee that the page is well-formed. It only shows that |
| 919 | ** we failed to detect any corruption. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 920 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 921 | int sqlite3BtreeInitPage( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 922 | MemPage *pPage, /* The page to be initialized */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 923 | MemPage *pParent /* The parent. Might be NULL */ |
| 924 | ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 925 | int pc; /* Address of a freeblock within pPage->aData[] */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 926 | int hdr; /* Offset to beginning of page header */ |
| 927 | u8 *data; /* Equal to pPage->aData */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 928 | BtShared *pBt; /* The main btree structure */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 929 | int usableSize; /* Amount of usable space on each page */ |
| 930 | int cellOffset; /* Offset from start of page to first cell pointer */ |
| 931 | int nFree; /* Number of unused bytes on the page */ |
| 932 | int top; /* First byte of the cell content area */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 933 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 934 | pBt = pPage->pBt; |
| 935 | assert( pBt!=0 ); |
| 936 | assert( pParent==0 || pParent->pBt==pBt ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 937 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 938 | assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 939 | assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) ); |
| 940 | assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) ); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 941 | if( pPage->pParent!=pParent && (pPage->pParent!=0 || pPage->isInit) ){ |
| 942 | /* The parent page should never change unless the file is corrupt */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 943 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 944 | } |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 945 | if( pPage->isInit ) return SQLITE_OK; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 946 | if( pPage->pParent==0 && pParent!=0 ){ |
| 947 | pPage->pParent = pParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 948 | sqlite3PagerRef(pParent->pDbPage); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 949 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 950 | hdr = pPage->hdrOffset; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 951 | data = pPage->aData; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 952 | if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT; |
drh | 1688c86 | 2008-07-18 02:44:17 +0000 | [diff] [blame] | 953 | assert( pBt->pageSize>=512 && pBt->pageSize<=32768 ); |
| 954 | pPage->maskPage = pBt->pageSize - 1; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 955 | pPage->nOverflow = 0; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 956 | pPage->idxShift = 0; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 957 | usableSize = pBt->usableSize; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 958 | pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf; |
| 959 | top = get2byte(&data[hdr+5]); |
| 960 | pPage->nCell = get2byte(&data[hdr+3]); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 961 | if( pPage->nCell>MX_CELL(pBt) ){ |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 962 | /* To many cells for a single page. The page must be corrupt */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 963 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 964 | } |
| 965 | if( pPage->nCell==0 && pParent!=0 && pParent->pgno!=1 ){ |
| 966 | /* All pages must have at least one cell, except for root pages */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 967 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 968 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 969 | |
| 970 | /* Compute the total free space on the page */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 971 | pc = get2byte(&data[hdr+1]); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 972 | nFree = data[hdr+7] + top - (cellOffset + 2*pPage->nCell); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 973 | while( pc>0 ){ |
| 974 | int next, size; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 975 | if( pc>usableSize-4 ){ |
| 976 | /* Free block is off the page */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 977 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 978 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 979 | next = get2byte(&data[pc]); |
| 980 | size = get2byte(&data[pc+2]); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 981 | if( next>0 && next<=pc+size+3 ){ |
| 982 | /* Free blocks must be in accending order */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 983 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 984 | } |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 985 | nFree += size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 986 | pc = next; |
| 987 | } |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 988 | pPage->nFree = nFree; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 989 | if( nFree>=usableSize ){ |
| 990 | /* Free space cannot exceed total page size */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 991 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 992 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 993 | |
drh | 1688c86 | 2008-07-18 02:44:17 +0000 | [diff] [blame] | 994 | #if 0 |
| 995 | /* Check that all the offsets in the cell offset array are within range. |
| 996 | ** |
| 997 | ** Omitting this consistency check and using the pPage->maskPage mask |
| 998 | ** to prevent overrunning the page buffer in findCell() results in a |
| 999 | ** 2.5% performance gain. |
| 1000 | */ |
| 1001 | { |
| 1002 | u8 *pOff; /* Iterator used to check all cell offsets are in range */ |
| 1003 | u8 *pEnd; /* Pointer to end of cell offset array */ |
| 1004 | u8 mask; /* Mask of bits that must be zero in MSB of cell offsets */ |
| 1005 | mask = ~(((u8)(pBt->pageSize>>8))-1); |
| 1006 | pEnd = &data[cellOffset + pPage->nCell*2]; |
| 1007 | for(pOff=&data[cellOffset]; pOff!=pEnd && !((*pOff)&mask); pOff+=2); |
| 1008 | if( pOff!=pEnd ){ |
| 1009 | return SQLITE_CORRUPT_BKPT; |
| 1010 | } |
danielk1977 | e16535f | 2008-06-11 18:15:29 +0000 | [diff] [blame] | 1011 | } |
drh | 1688c86 | 2008-07-18 02:44:17 +0000 | [diff] [blame] | 1012 | #endif |
danielk1977 | e16535f | 2008-06-11 18:15:29 +0000 | [diff] [blame] | 1013 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1014 | pPage->isInit = 1; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1015 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1019 | ** Set up a raw page so that it looks like a database page holding |
| 1020 | ** no entries. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1021 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1022 | static void zeroPage(MemPage *pPage, int flags){ |
| 1023 | unsigned char *data = pPage->aData; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1024 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1025 | int hdr = pPage->hdrOffset; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1026 | int first; |
| 1027 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1028 | assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1029 | assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); |
| 1030 | assert( sqlite3PagerGetData(pPage->pDbPage) == data ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1031 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1032 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 1af4a6e | 2008-07-18 03:32:51 +0000 | [diff] [blame] | 1033 | /*memset(&data[hdr], 0, pBt->usableSize - hdr);*/ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1034 | data[hdr] = flags; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1035 | first = hdr + 8 + 4*((flags&PTF_LEAF)==0); |
| 1036 | memset(&data[hdr+1], 0, 4); |
| 1037 | data[hdr+7] = 0; |
| 1038 | put2byte(&data[hdr+5], pBt->usableSize); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1039 | pPage->nFree = pBt->usableSize - first; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1040 | decodeFlags(pPage, flags); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1041 | pPage->hdrOffset = hdr; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1042 | pPage->cellOffset = first; |
| 1043 | pPage->nOverflow = 0; |
drh | 1688c86 | 2008-07-18 02:44:17 +0000 | [diff] [blame] | 1044 | assert( pBt->pageSize>=512 && pBt->pageSize<=32768 ); |
| 1045 | pPage->maskPage = pBt->pageSize - 1; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1046 | pPage->idxShift = 0; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1047 | pPage->nCell = 0; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1048 | pPage->isInit = 1; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1052 | ** Get a page from the pager. Initialize the MemPage.pBt and |
| 1053 | ** MemPage.aData elements if needed. |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 1054 | ** |
| 1055 | ** If the noContent flag is set, it means that we do not care about |
| 1056 | ** the content of the page at this time. So do not go to the disk |
| 1057 | ** to fetch the content. Just fill in the content with zeros for now. |
| 1058 | ** If in the future we call sqlite3PagerWrite() on this page, that |
| 1059 | ** means we have started to be concerned about content and the disk |
| 1060 | ** read should occur at that point. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1061 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1062 | int sqlite3BtreeGetPage( |
| 1063 | BtShared *pBt, /* The btree */ |
| 1064 | Pgno pgno, /* Number of the page to fetch */ |
| 1065 | MemPage **ppPage, /* Return the page in this parameter */ |
| 1066 | int noContent /* Do not load page content if true */ |
| 1067 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1068 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1069 | MemPage *pPage; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1070 | DbPage *pDbPage; |
| 1071 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1072 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 1073 | rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1074 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1075 | pPage = (MemPage *)sqlite3PagerGetExtra(pDbPage); |
| 1076 | pPage->aData = sqlite3PagerGetData(pDbPage); |
| 1077 | pPage->pDbPage = pDbPage; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1078 | pPage->pBt = pBt; |
| 1079 | pPage->pgno = pgno; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1080 | pPage->hdrOffset = pPage->pgno==1 ? 100 : 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1081 | *ppPage = pPage; |
| 1082 | return SQLITE_OK; |
| 1083 | } |
| 1084 | |
| 1085 | /* |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1086 | ** Get a page from the pager and initialize it. This routine |
| 1087 | ** is just a convenience wrapper around separate calls to |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1088 | ** sqlite3BtreeGetPage() and sqlite3BtreeInitPage(). |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1089 | */ |
| 1090 | static int getAndInitPage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1091 | BtShared *pBt, /* The database file */ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1092 | Pgno pgno, /* Number of the page to get */ |
| 1093 | MemPage **ppPage, /* Write the page pointer here */ |
| 1094 | MemPage *pParent /* Parent of the page */ |
| 1095 | ){ |
| 1096 | int rc; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1097 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1098 | if( pgno==0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1099 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1100 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1101 | rc = sqlite3BtreeGetPage(pBt, pgno, ppPage, 0); |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 1102 | if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1103 | rc = sqlite3BtreeInitPage(*ppPage, pParent); |
danielk1977 | 43e377a | 2008-05-05 12:09:32 +0000 | [diff] [blame] | 1104 | if( rc!=SQLITE_OK ){ |
| 1105 | releasePage(*ppPage); |
| 1106 | *ppPage = 0; |
| 1107 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1108 | } |
| 1109 | return rc; |
| 1110 | } |
| 1111 | |
| 1112 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1113 | ** Release a MemPage. This should be called once for each prior |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1114 | ** call to sqlite3BtreeGetPage. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1115 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1116 | static void releasePage(MemPage *pPage){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1117 | if( pPage ){ |
| 1118 | assert( pPage->aData ); |
| 1119 | assert( pPage->pBt ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1120 | assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); |
| 1121 | assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1122 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1123 | sqlite3PagerUnref(pPage->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1128 | ** This routine is called when the reference count for a page |
| 1129 | ** reaches zero. We need to unref the pParent pointer when that |
| 1130 | ** happens. |
| 1131 | */ |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 1132 | static void pageDestructor(DbPage *pData){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1133 | MemPage *pPage; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1134 | pPage = (MemPage *)sqlite3PagerGetExtra(pData); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 1135 | if( pPage ){ |
| 1136 | assert( pPage->isInit==0 || sqlite3_mutex_held(pPage->pBt->mutex) ); |
| 1137 | if( pPage->pParent ){ |
| 1138 | MemPage *pParent = pPage->pParent; |
| 1139 | assert( pParent->pBt==pPage->pBt ); |
| 1140 | pPage->pParent = 0; |
| 1141 | releasePage(pParent); |
| 1142 | } |
| 1143 | pPage->isInit = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1148 | ** During a rollback, when the pager reloads information into the cache |
| 1149 | ** so that the cache is restored to its original state at the start of |
| 1150 | ** the transaction, for each page restored this routine is called. |
| 1151 | ** |
| 1152 | ** This routine needs to reset the extra data section at the end of the |
| 1153 | ** page to agree with the restored data. |
| 1154 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1155 | static void pageReinit(DbPage *pData, int pageSize){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1156 | MemPage *pPage; |
| 1157 | assert( (pageSize & 7)==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1158 | pPage = (MemPage *)sqlite3PagerGetExtra(pData); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1159 | if( pPage->isInit ){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1160 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1161 | pPage->isInit = 0; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1162 | sqlite3BtreeInitPage(pPage, pPage->pParent); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | /* |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1167 | ** Invoke the busy handler for a btree. |
| 1168 | */ |
| 1169 | static int sqlite3BtreeInvokeBusyHandler(void *pArg, int n){ |
| 1170 | BtShared *pBt = (BtShared*)pArg; |
| 1171 | assert( pBt->db ); |
| 1172 | assert( sqlite3_mutex_held(pBt->db->mutex) ); |
| 1173 | return sqlite3InvokeBusyHandler(&pBt->db->busyHandler); |
| 1174 | } |
| 1175 | |
| 1176 | /* |
drh | ad3e010 | 2004-09-03 23:32:18 +0000 | [diff] [blame] | 1177 | ** Open a database file. |
| 1178 | ** |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1179 | ** zFilename is the name of the database file. If zFilename is NULL |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1180 | ** a new database with a random name is created. This randomly named |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1181 | ** database file will be deleted when sqlite3BtreeClose() is called. |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1182 | ** If zFilename is ":memory:" then an in-memory database is created |
| 1183 | ** that is automatically destroyed when it is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1184 | */ |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1185 | int sqlite3BtreeOpen( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1186 | const char *zFilename, /* Name of the file containing the BTree database */ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1187 | sqlite3 *db, /* Associated database handle */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1188 | Btree **ppBtree, /* Pointer to new Btree object written here */ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1189 | int flags, /* Options */ |
| 1190 | int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1191 | ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1192 | sqlite3_vfs *pVfs; /* The VFS to use for this btree */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1193 | BtShared *pBt = 0; /* Shared part of btree structure */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1194 | Btree *p; /* Handle to return */ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1195 | int rc = SQLITE_OK; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1196 | int nReserve; |
| 1197 | unsigned char zDbHeader[100]; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1198 | |
| 1199 | /* Set the variable isMemdb to true for an in-memory database, or |
| 1200 | ** false for a file-based database. This symbol is only required if |
| 1201 | ** either of the shared-data or autovacuum features are compiled |
| 1202 | ** into the library. |
| 1203 | */ |
| 1204 | #if !defined(SQLITE_OMIT_SHARED_CACHE) || !defined(SQLITE_OMIT_AUTOVACUUM) |
| 1205 | #ifdef SQLITE_OMIT_MEMORYDB |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 1206 | const int isMemdb = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1207 | #else |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 1208 | const int isMemdb = zFilename && !strcmp(zFilename, ":memory:"); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1209 | #endif |
| 1210 | #endif |
| 1211 | |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1212 | assert( db!=0 ); |
| 1213 | assert( sqlite3_mutex_held(db->mutex) ); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1214 | |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1215 | pVfs = db->pVfs; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1216 | p = sqlite3MallocZero(sizeof(Btree)); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1217 | if( !p ){ |
| 1218 | return SQLITE_NOMEM; |
| 1219 | } |
| 1220 | p->inTrans = TRANS_NONE; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1221 | p->db = db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1222 | |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 1223 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1224 | /* |
| 1225 | ** If this Btree is a candidate for shared cache, try to find an |
| 1226 | ** existing BtShared object that we can share with |
| 1227 | */ |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 1228 | if( isMemdb==0 |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1229 | && (db->flags & SQLITE_Vtab)==0 |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1230 | && zFilename && zFilename[0] |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1231 | ){ |
danielk1977 | 502b4e0 | 2008-09-02 14:07:24 +0000 | [diff] [blame] | 1232 | if( sqlite3GlobalConfig.sharedCacheEnabled ){ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 1233 | int nFullPathname = pVfs->mxPathname+1; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1234 | char *zFullPathname = sqlite3Malloc(nFullPathname); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1235 | sqlite3_mutex *mutexShared; |
| 1236 | p->sharable = 1; |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 1237 | db->flags |= SQLITE_SharedCache; |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1238 | if( !zFullPathname ){ |
| 1239 | sqlite3_free(p); |
| 1240 | return SQLITE_NOMEM; |
| 1241 | } |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 1242 | sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname); |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 1243 | mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1244 | sqlite3_mutex_enter(mutexShared); |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 1245 | for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1246 | assert( pBt->nRef>0 ); |
| 1247 | if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager)) |
| 1248 | && sqlite3PagerVfs(pBt->pPager)==pVfs ){ |
| 1249 | p->pBt = pBt; |
| 1250 | pBt->nRef++; |
| 1251 | break; |
| 1252 | } |
| 1253 | } |
| 1254 | sqlite3_mutex_leave(mutexShared); |
| 1255 | sqlite3_free(zFullPathname); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1256 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1257 | #ifdef SQLITE_DEBUG |
| 1258 | else{ |
| 1259 | /* In debug mode, we mark all persistent databases as sharable |
| 1260 | ** even when they are not. This exercises the locking code and |
| 1261 | ** gives more opportunity for asserts(sqlite3_mutex_held()) |
| 1262 | ** statements to find locking problems. |
| 1263 | */ |
| 1264 | p->sharable = 1; |
| 1265 | } |
| 1266 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1267 | } |
| 1268 | #endif |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1269 | if( pBt==0 ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1270 | /* |
| 1271 | ** The following asserts make sure that structures used by the btree are |
| 1272 | ** the right size. This is to guard against size changes that result |
| 1273 | ** when compiling on a different architecture. |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 1274 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1275 | assert( sizeof(i64)==8 || sizeof(i64)==4 ); |
| 1276 | assert( sizeof(u64)==8 || sizeof(u64)==4 ); |
| 1277 | assert( sizeof(u32)==4 ); |
| 1278 | assert( sizeof(u16)==2 ); |
| 1279 | assert( sizeof(Pgno)==4 ); |
| 1280 | |
| 1281 | pBt = sqlite3MallocZero( sizeof(*pBt) ); |
| 1282 | if( pBt==0 ){ |
| 1283 | rc = SQLITE_NOMEM; |
| 1284 | goto btree_open_out; |
| 1285 | } |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1286 | pBt->busyHdr.xFunc = sqlite3BtreeInvokeBusyHandler; |
| 1287 | pBt->busyHdr.pArg = pBt; |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 1288 | rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename, pageDestructor, |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1289 | EXTRA_SIZE, flags, vfsFlags); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1290 | if( rc==SQLITE_OK ){ |
| 1291 | rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader); |
| 1292 | } |
| 1293 | if( rc!=SQLITE_OK ){ |
| 1294 | goto btree_open_out; |
| 1295 | } |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1296 | sqlite3PagerSetBusyhandler(pBt->pPager, &pBt->busyHdr); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1297 | p->pBt = pBt; |
| 1298 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1299 | sqlite3PagerSetReiniter(pBt->pPager, pageReinit); |
| 1300 | pBt->pCursor = 0; |
| 1301 | pBt->pPage1 = 0; |
| 1302 | pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager); |
| 1303 | pBt->pageSize = get2byte(&zDbHeader[16]); |
| 1304 | if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE |
| 1305 | || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1306 | pBt->pageSize = 0; |
| 1307 | sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1308 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1309 | /* If the magic name ":memory:" will create an in-memory database, then |
| 1310 | ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if |
| 1311 | ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if |
| 1312 | ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a |
| 1313 | ** regular file-name. In this case the auto-vacuum applies as per normal. |
| 1314 | */ |
| 1315 | if( zFilename && !isMemdb ){ |
| 1316 | pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0); |
| 1317 | pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0); |
| 1318 | } |
| 1319 | #endif |
| 1320 | nReserve = 0; |
| 1321 | }else{ |
| 1322 | nReserve = zDbHeader[20]; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1323 | pBt->pageSizeFixed = 1; |
| 1324 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1325 | pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0); |
| 1326 | pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0); |
| 1327 | #endif |
| 1328 | } |
| 1329 | pBt->usableSize = pBt->pageSize - nReserve; |
| 1330 | assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1331 | sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1332 | |
| 1333 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
| 1334 | /* Add the new BtShared object to the linked list sharable BtShareds. |
| 1335 | */ |
| 1336 | if( p->sharable ){ |
| 1337 | sqlite3_mutex *mutexShared; |
| 1338 | pBt->nRef = 1; |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 1339 | mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 1340 | if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){ |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 1341 | pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST); |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 1342 | if( pBt->mutex==0 ){ |
| 1343 | rc = SQLITE_NOMEM; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1344 | db->mallocFailed = 0; |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 1345 | goto btree_open_out; |
| 1346 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1347 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1348 | sqlite3_mutex_enter(mutexShared); |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 1349 | pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList); |
| 1350 | GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1351 | sqlite3_mutex_leave(mutexShared); |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1352 | } |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1353 | #endif |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1354 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1355 | |
drh | cfed7bc | 2006-03-13 14:28:05 +0000 | [diff] [blame] | 1356 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1357 | /* If the new Btree uses a sharable pBtShared, then link the new |
| 1358 | ** Btree into the list of all sharable Btrees for the same connection. |
drh | abddb0c | 2007-08-20 13:14:28 +0000 | [diff] [blame] | 1359 | ** The list is kept in ascending order by pBt address. |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 1360 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1361 | if( p->sharable ){ |
| 1362 | int i; |
| 1363 | Btree *pSib; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1364 | for(i=0; i<db->nDb; i++){ |
| 1365 | if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1366 | while( pSib->pPrev ){ pSib = pSib->pPrev; } |
| 1367 | if( p->pBt<pSib->pBt ){ |
| 1368 | p->pNext = pSib; |
| 1369 | p->pPrev = 0; |
| 1370 | pSib->pPrev = p; |
| 1371 | }else{ |
drh | abddb0c | 2007-08-20 13:14:28 +0000 | [diff] [blame] | 1372 | while( pSib->pNext && pSib->pNext->pBt<p->pBt ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1373 | pSib = pSib->pNext; |
| 1374 | } |
| 1375 | p->pNext = pSib->pNext; |
| 1376 | p->pPrev = pSib; |
| 1377 | if( p->pNext ){ |
| 1378 | p->pNext->pPrev = p; |
| 1379 | } |
| 1380 | pSib->pNext = p; |
| 1381 | } |
| 1382 | break; |
| 1383 | } |
| 1384 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1385 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1386 | #endif |
| 1387 | *ppBtree = p; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1388 | |
| 1389 | btree_open_out: |
| 1390 | if( rc!=SQLITE_OK ){ |
| 1391 | if( pBt && pBt->pPager ){ |
| 1392 | sqlite3PagerClose(pBt->pPager); |
| 1393 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1394 | sqlite3_free(pBt); |
| 1395 | sqlite3_free(p); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1396 | *ppBtree = 0; |
| 1397 | } |
| 1398 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | /* |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1402 | ** Decrement the BtShared.nRef counter. When it reaches zero, |
| 1403 | ** remove the BtShared structure from the sharing list. Return |
| 1404 | ** true if the BtShared.nRef counter reaches zero and return |
| 1405 | ** false if it is still positive. |
| 1406 | */ |
| 1407 | static int removeFromSharingList(BtShared *pBt){ |
| 1408 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 1409 | sqlite3_mutex *pMaster; |
| 1410 | BtShared *pList; |
| 1411 | int removed = 0; |
| 1412 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1413 | assert( sqlite3_mutex_notheld(pBt->mutex) ); |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 1414 | pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1415 | sqlite3_mutex_enter(pMaster); |
| 1416 | pBt->nRef--; |
| 1417 | if( pBt->nRef<=0 ){ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 1418 | if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){ |
| 1419 | GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1420 | }else{ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 1421 | pList = GLOBAL(BtShared*,sqlite3SharedCacheList); |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 1422 | while( ALWAYS(pList) && pList->pNext!=pBt ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1423 | pList=pList->pNext; |
| 1424 | } |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 1425 | if( ALWAYS(pList) ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1426 | pList->pNext = pBt->pNext; |
| 1427 | } |
| 1428 | } |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 1429 | if( SQLITE_THREADSAFE ){ |
| 1430 | sqlite3_mutex_free(pBt->mutex); |
| 1431 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1432 | removed = 1; |
| 1433 | } |
| 1434 | sqlite3_mutex_leave(pMaster); |
| 1435 | return removed; |
| 1436 | #else |
| 1437 | return 1; |
| 1438 | #endif |
| 1439 | } |
| 1440 | |
| 1441 | /* |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1442 | ** Make sure pBt->pTmpSpace points to an allocation of |
| 1443 | ** MX_CELL_SIZE(pBt) bytes. |
| 1444 | */ |
| 1445 | static void allocateTempSpace(BtShared *pBt){ |
| 1446 | if( !pBt->pTmpSpace ){ |
| 1447 | pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize ); |
| 1448 | } |
| 1449 | } |
| 1450 | |
| 1451 | /* |
| 1452 | ** Free the pBt->pTmpSpace allocation |
| 1453 | */ |
| 1454 | static void freeTempSpace(BtShared *pBt){ |
| 1455 | sqlite3PageFree( pBt->pTmpSpace); |
| 1456 | pBt->pTmpSpace = 0; |
| 1457 | } |
| 1458 | |
| 1459 | /* |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1460 | ** Close an open database and invalidate all cursors. |
| 1461 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1462 | int sqlite3BtreeClose(Btree *p){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1463 | BtShared *pBt = p->pBt; |
| 1464 | BtCursor *pCur; |
| 1465 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1466 | /* Close all cursors opened via this handle. */ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1467 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1468 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1469 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1470 | pCur = pBt->pCursor; |
| 1471 | while( pCur ){ |
| 1472 | BtCursor *pTmp = pCur; |
| 1473 | pCur = pCur->pNext; |
| 1474 | if( pTmp->pBtree==p ){ |
| 1475 | sqlite3BtreeCloseCursor(pTmp); |
| 1476 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1477 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1478 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 1479 | /* Rollback any active transaction and free the handle structure. |
| 1480 | ** The call to sqlite3BtreeRollback() drops any table-locks held by |
| 1481 | ** this handle. |
| 1482 | */ |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 1483 | sqlite3BtreeRollback(p); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1484 | sqlite3BtreeLeave(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1485 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1486 | /* If there are still other outstanding references to the shared-btree |
| 1487 | ** structure, return now. The remainder of this procedure cleans |
| 1488 | ** up the shared-btree. |
| 1489 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1490 | assert( p->wantToLock==0 && p->locked==0 ); |
| 1491 | if( !p->sharable || removeFromSharingList(pBt) ){ |
| 1492 | /* The pBt is no longer on the sharing list, so we can access |
| 1493 | ** it without having to hold the mutex. |
| 1494 | ** |
| 1495 | ** Clean out and delete the BtShared object. |
| 1496 | */ |
| 1497 | assert( !pBt->pCursor ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1498 | sqlite3PagerClose(pBt->pPager); |
| 1499 | if( pBt->xFreeSchema && pBt->pSchema ){ |
| 1500 | pBt->xFreeSchema(pBt->pSchema); |
| 1501 | } |
| 1502 | sqlite3_free(pBt->pSchema); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1503 | freeTempSpace(pBt); |
drh | 65bbf29 | 2008-06-19 01:03:17 +0000 | [diff] [blame] | 1504 | sqlite3_free(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1507 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | cab5ed7 | 2007-08-22 11:41:18 +0000 | [diff] [blame] | 1508 | assert( p->wantToLock==0 ); |
| 1509 | assert( p->locked==0 ); |
| 1510 | if( p->pPrev ) p->pPrev->pNext = p->pNext; |
| 1511 | if( p->pNext ) p->pNext->pPrev = p->pPrev; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1512 | #endif |
| 1513 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1514 | sqlite3_free(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1515 | return SQLITE_OK; |
| 1516 | } |
| 1517 | |
| 1518 | /* |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1519 | ** Change the limit on the number of pages allowed in the cache. |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 1520 | ** |
| 1521 | ** The maximum number of cache pages is set to the absolute |
| 1522 | ** value of mxPage. If mxPage is negative, the pager will |
| 1523 | ** operate asynchronously - it will not stop to do fsync()s |
| 1524 | ** to insure data is written to the disk surface before |
| 1525 | ** continuing. Transactions still work if synchronous is off, |
| 1526 | ** and the database cannot be corrupted if this program |
| 1527 | ** crashes. But if the operating system crashes or there is |
| 1528 | ** an abrupt power failure when synchronous is off, the database |
| 1529 | ** could be left in an inconsistent and unrecoverable state. |
| 1530 | ** Synchronous is on by default so database corruption is not |
| 1531 | ** normally a worry. |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1532 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1533 | int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){ |
| 1534 | BtShared *pBt = p->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1535 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1536 | sqlite3BtreeEnter(p); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1537 | sqlite3PagerSetCachesize(pBt->pPager, mxPage); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1538 | sqlite3BtreeLeave(p); |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1539 | return SQLITE_OK; |
| 1540 | } |
| 1541 | |
| 1542 | /* |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1543 | ** Change the way data is synced to disk in order to increase or decrease |
| 1544 | ** how well the database resists damage due to OS crashes and power |
| 1545 | ** failures. Level 1 is the same as asynchronous (no syncs() occur and |
| 1546 | ** there is a high probability of damage) Level 2 is the default. There |
| 1547 | ** is a very low but non-zero probability of damage. Level 3 reduces the |
| 1548 | ** probability of damage to near zero but with a write performance reduction. |
| 1549 | */ |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1550 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
drh | ac530b1 | 2006-02-11 01:25:50 +0000 | [diff] [blame] | 1551 | int sqlite3BtreeSetSafetyLevel(Btree *p, int level, int fullSync){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1552 | BtShared *pBt = p->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1553 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1554 | sqlite3BtreeEnter(p); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1555 | sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1556 | sqlite3BtreeLeave(p); |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1557 | return SQLITE_OK; |
| 1558 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1559 | #endif |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1560 | |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1561 | /* |
| 1562 | ** Return TRUE if the given btree is set to safety level 1. In other |
| 1563 | ** words, return TRUE if no sync() occurs on the disk files. |
| 1564 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1565 | int sqlite3BtreeSyncDisabled(Btree *p){ |
| 1566 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1567 | int rc; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1568 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1569 | sqlite3BtreeEnter(p); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 1570 | assert( pBt && pBt->pPager ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1571 | rc = sqlite3PagerNosync(pBt->pPager); |
| 1572 | sqlite3BtreeLeave(p); |
| 1573 | return rc; |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1576 | #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1577 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1578 | ** Change the default pages size and the number of reserved bytes per page. |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 1579 | ** |
| 1580 | ** The page size must be a power of 2 between 512 and 65536. If the page |
| 1581 | ** size supplied does not meet this constraint then the page size is not |
| 1582 | ** changed. |
| 1583 | ** |
| 1584 | ** Page sizes are constrained to be a power of two so that the region |
| 1585 | ** of the database file used for locking (beginning at PENDING_BYTE, |
| 1586 | ** the first byte past the 1GB boundary, 0x40000000) needs to occur |
| 1587 | ** at the beginning of a page. |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 1588 | ** |
| 1589 | ** If parameter nReserve is less than zero, then the number of reserved |
| 1590 | ** bytes per page is left unchanged. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1591 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1592 | int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1593 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1594 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1595 | sqlite3BtreeEnter(p); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1596 | if( pBt->pageSizeFixed ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1597 | sqlite3BtreeLeave(p); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1598 | return SQLITE_READONLY; |
| 1599 | } |
| 1600 | if( nReserve<0 ){ |
| 1601 | nReserve = pBt->pageSize - pBt->usableSize; |
| 1602 | } |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 1603 | if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE && |
| 1604 | ((pageSize-1)&pageSize)==0 ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1605 | assert( (pageSize & 7)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1606 | assert( !pBt->pPage1 && !pBt->pCursor ); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1607 | pBt->pageSize = pageSize; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1608 | freeTempSpace(pBt); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1609 | rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1610 | } |
| 1611 | pBt->usableSize = pBt->pageSize - nReserve; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1612 | sqlite3BtreeLeave(p); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1613 | return rc; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | /* |
| 1617 | ** Return the currently defined page size |
| 1618 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1619 | int sqlite3BtreeGetPageSize(Btree *p){ |
| 1620 | return p->pBt->pageSize; |
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 sqlite3BtreeGetReserve(Btree *p){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1623 | int n; |
| 1624 | sqlite3BtreeEnter(p); |
| 1625 | n = p->pBt->pageSize - p->pBt->usableSize; |
| 1626 | sqlite3BtreeLeave(p); |
| 1627 | return n; |
drh | 2011d5f | 2004-07-22 02:40:37 +0000 | [diff] [blame] | 1628 | } |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 1629 | |
| 1630 | /* |
| 1631 | ** Set the maximum page count for a database if mxPage is positive. |
| 1632 | ** No changes are made if mxPage is 0 or negative. |
| 1633 | ** Regardless of the value of mxPage, return the maximum page count. |
| 1634 | */ |
| 1635 | int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1636 | int n; |
| 1637 | sqlite3BtreeEnter(p); |
| 1638 | n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage); |
| 1639 | sqlite3BtreeLeave(p); |
| 1640 | return n; |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 1641 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1642 | #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1643 | |
| 1644 | /* |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1645 | ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum' |
| 1646 | ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it |
| 1647 | ** is disabled. The default value for the auto-vacuum property is |
| 1648 | ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro. |
| 1649 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1650 | int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1651 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1652 | return SQLITE_READONLY; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1653 | #else |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1654 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1655 | int rc = SQLITE_OK; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1656 | int av = (autoVacuum?1:0); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1657 | |
| 1658 | sqlite3BtreeEnter(p); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1659 | if( pBt->pageSizeFixed && av!=pBt->autoVacuum ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1660 | rc = SQLITE_READONLY; |
| 1661 | }else{ |
| 1662 | pBt->autoVacuum = av; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1663 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1664 | sqlite3BtreeLeave(p); |
| 1665 | return rc; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1666 | #endif |
| 1667 | } |
| 1668 | |
| 1669 | /* |
| 1670 | ** Return the value of the 'auto-vacuum' property. If auto-vacuum is |
| 1671 | ** enabled 1 is returned. Otherwise 0. |
| 1672 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1673 | int sqlite3BtreeGetAutoVacuum(Btree *p){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1674 | #ifdef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1675 | return BTREE_AUTOVACUUM_NONE; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1676 | #else |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1677 | int rc; |
| 1678 | sqlite3BtreeEnter(p); |
| 1679 | rc = ( |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1680 | (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE: |
| 1681 | (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL: |
| 1682 | BTREE_AUTOVACUUM_INCR |
| 1683 | ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1684 | sqlite3BtreeLeave(p); |
| 1685 | return rc; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1686 | #endif |
| 1687 | } |
| 1688 | |
| 1689 | |
| 1690 | /* |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1691 | ** Get a reference to pPage1 of the database file. This will |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1692 | ** also acquire a readlock on that file. |
| 1693 | ** |
| 1694 | ** SQLITE_OK is returned on success. If the file is not a |
| 1695 | ** well-formed database file, then SQLITE_CORRUPT is returned. |
| 1696 | ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 1697 | ** is returned if we run out of memory. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1698 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1699 | static int lockBtree(BtShared *pBt){ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1700 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1701 | MemPage *pPage1; |
danielk1977 | 93f7af9 | 2008-05-09 16:57:50 +0000 | [diff] [blame] | 1702 | int nPage; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1703 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1704 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1705 | if( pBt->pPage1 ) return SQLITE_OK; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1706 | rc = sqlite3BtreeGetPage(pBt, 1, &pPage1, 0); |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1707 | if( rc!=SQLITE_OK ) return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1708 | |
| 1709 | /* Do some checking to help insure the file we opened really is |
| 1710 | ** a valid database file. |
| 1711 | */ |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 1712 | rc = sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 1713 | if( rc!=SQLITE_OK ){ |
danielk1977 | 93f7af9 | 2008-05-09 16:57:50 +0000 | [diff] [blame] | 1714 | goto page1_init_failed; |
| 1715 | }else if( nPage>0 ){ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1716 | int pageSize; |
| 1717 | int usableSize; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1718 | u8 *page1 = pPage1->aData; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 1719 | rc = SQLITE_NOTADB; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1720 | if( memcmp(page1, zMagicHeader, 16)!=0 ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1721 | goto page1_init_failed; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1722 | } |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 1723 | if( page1[18]>1 ){ |
| 1724 | pBt->readOnly = 1; |
| 1725 | } |
| 1726 | if( page1[19]>1 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1727 | goto page1_init_failed; |
| 1728 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1729 | |
| 1730 | /* The maximum embedded fraction must be exactly 25%. And the minimum |
| 1731 | ** embedded fraction must be 12.5% for both leaf-data and non-leaf-data. |
| 1732 | ** The original design allowed these amounts to vary, but as of |
| 1733 | ** version 3.6.0, we require them to be fixed. |
| 1734 | */ |
| 1735 | if( memcmp(&page1[21], "\100\040\040",3)!=0 ){ |
| 1736 | goto page1_init_failed; |
| 1737 | } |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1738 | pageSize = get2byte(&page1[16]); |
drh | 7dc385e | 2007-09-06 23:39:36 +0000 | [diff] [blame] | 1739 | if( ((pageSize-1)&pageSize)!=0 || pageSize<512 || |
| 1740 | (SQLITE_MAX_PAGE_SIZE<32768 && pageSize>SQLITE_MAX_PAGE_SIZE) |
| 1741 | ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1742 | goto page1_init_failed; |
| 1743 | } |
| 1744 | assert( (pageSize & 7)==0 ); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1745 | usableSize = pageSize - page1[20]; |
| 1746 | if( pageSize!=pBt->pageSize ){ |
| 1747 | /* After reading the first page of the database assuming a page size |
| 1748 | ** of BtShared.pageSize, we have discovered that the page-size is |
| 1749 | ** actually pageSize. Unlock the database, leave pBt->pPage1 at |
| 1750 | ** zero and return SQLITE_OK. The caller will call this function |
| 1751 | ** again with the correct page-size. |
| 1752 | */ |
| 1753 | releasePage(pPage1); |
| 1754 | pBt->usableSize = usableSize; |
| 1755 | pBt->pageSize = pageSize; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1756 | freeTempSpace(pBt); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1757 | sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
| 1758 | return SQLITE_OK; |
| 1759 | } |
| 1760 | if( usableSize<500 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1761 | goto page1_init_failed; |
| 1762 | } |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1763 | pBt->pageSize = pageSize; |
| 1764 | pBt->usableSize = usableSize; |
drh | 057cd3a | 2005-02-15 16:23:02 +0000 | [diff] [blame] | 1765 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1766 | pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0); |
danielk1977 | 27b1f95 | 2007-06-25 08:16:58 +0000 | [diff] [blame] | 1767 | pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0); |
drh | 057cd3a | 2005-02-15 16:23:02 +0000 | [diff] [blame] | 1768 | #endif |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1769 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1770 | |
| 1771 | /* maxLocal is the maximum amount of payload to store locally for |
| 1772 | ** a cell. Make sure it is small enough so that at least minFanout |
| 1773 | ** cells can will fit on one page. We assume a 10-byte page header. |
| 1774 | ** Besides the payload, the cell must store: |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1775 | ** 2-byte pointer to the cell |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1776 | ** 4-byte child pointer |
| 1777 | ** 9-byte nKey value |
| 1778 | ** 4-byte nData value |
| 1779 | ** 4-byte overflow page pointer |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1780 | ** So a cell consists of a 2-byte poiner, a header which is as much as |
| 1781 | ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow |
| 1782 | ** page pointer. |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1783 | */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1784 | pBt->maxLocal = (pBt->usableSize-12)*64/255 - 23; |
| 1785 | pBt->minLocal = (pBt->usableSize-12)*32/255 - 23; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1786 | pBt->maxLeaf = pBt->usableSize - 35; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1787 | pBt->minLeaf = (pBt->usableSize-12)*32/255 - 23; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1788 | assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1789 | pBt->pPage1 = pPage1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1790 | return SQLITE_OK; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1791 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1792 | page1_init_failed: |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1793 | releasePage(pPage1); |
| 1794 | pBt->pPage1 = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1795 | return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1796 | } |
| 1797 | |
| 1798 | /* |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1799 | ** This routine works like lockBtree() except that it also invokes the |
| 1800 | ** busy callback if there is lock contention. |
| 1801 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1802 | static int lockBtreeWithRetry(Btree *pRef){ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1803 | int rc = SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1804 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1805 | assert( sqlite3BtreeHoldsMutex(pRef) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1806 | if( pRef->inTrans==TRANS_NONE ){ |
| 1807 | u8 inTransaction = pRef->pBt->inTransaction; |
| 1808 | btreeIntegrity(pRef); |
| 1809 | rc = sqlite3BtreeBeginTrans(pRef, 0); |
| 1810 | pRef->pBt->inTransaction = inTransaction; |
| 1811 | pRef->inTrans = TRANS_NONE; |
| 1812 | if( rc==SQLITE_OK ){ |
| 1813 | pRef->pBt->nTransaction--; |
| 1814 | } |
| 1815 | btreeIntegrity(pRef); |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1816 | } |
| 1817 | return rc; |
| 1818 | } |
| 1819 | |
| 1820 | |
| 1821 | /* |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1822 | ** If there are no outstanding cursors and we are not in the middle |
| 1823 | ** of a transaction but there is a read lock on the database, then |
| 1824 | ** this routine unrefs the first page of the database file which |
| 1825 | ** has the effect of releasing the read lock. |
| 1826 | ** |
| 1827 | ** If there are any outstanding cursors, this routine is a no-op. |
| 1828 | ** |
| 1829 | ** If there is a transaction in progress, this routine is a no-op. |
| 1830 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1831 | static void unlockBtreeIfUnused(BtShared *pBt){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1832 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1833 | if( pBt->inTransaction==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1834 | if( sqlite3PagerRefcount(pBt->pPager)>=1 ){ |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 1835 | assert( pBt->pPage1->aData ); |
| 1836 | #if 0 |
drh | 24c9a2e | 2007-01-05 02:00:47 +0000 | [diff] [blame] | 1837 | if( pBt->pPage1->aData==0 ){ |
| 1838 | MemPage *pPage = pBt->pPage1; |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1839 | pPage->aData = sqlite3PagerGetData(pPage->pDbPage); |
drh | 24c9a2e | 2007-01-05 02:00:47 +0000 | [diff] [blame] | 1840 | pPage->pBt = pBt; |
| 1841 | pPage->pgno = 1; |
| 1842 | } |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 1843 | #endif |
drh | 24c9a2e | 2007-01-05 02:00:47 +0000 | [diff] [blame] | 1844 | releasePage(pBt->pPage1); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 1845 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1846 | pBt->pPage1 = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1847 | pBt->inStmt = 0; |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1848 | } |
| 1849 | } |
| 1850 | |
| 1851 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1852 | ** Create a new database by initializing the first page of the |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1853 | ** file. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1854 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1855 | static int newDatabase(BtShared *pBt){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1856 | MemPage *pP1; |
| 1857 | unsigned char *data; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1858 | int rc; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 1859 | int nPage; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1860 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1861 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 1862 | rc = sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 1863 | if( rc!=SQLITE_OK || nPage>0 ){ |
| 1864 | return rc; |
| 1865 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1866 | pP1 = pBt->pPage1; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1867 | assert( pP1!=0 ); |
| 1868 | data = pP1->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1869 | rc = sqlite3PagerWrite(pP1->pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1870 | if( rc ) return rc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1871 | memcpy(data, zMagicHeader, sizeof(zMagicHeader)); |
| 1872 | assert( sizeof(zMagicHeader)==16 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1873 | put2byte(&data[16], pBt->pageSize); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1874 | data[18] = 1; |
| 1875 | data[19] = 1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1876 | data[20] = pBt->pageSize - pBt->usableSize; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1877 | data[21] = 64; |
| 1878 | data[22] = 32; |
| 1879 | data[23] = 32; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1880 | memset(&data[24], 0, 100-24); |
drh | e6c4381 | 2004-05-14 12:17:46 +0000 | [diff] [blame] | 1881 | zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA ); |
drh | f2a611c | 2004-09-05 00:33:43 +0000 | [diff] [blame] | 1882 | pBt->pageSizeFixed = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1883 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1884 | assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 ); |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 1885 | assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 ); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1886 | put4byte(&data[36 + 4*4], pBt->autoVacuum); |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 1887 | put4byte(&data[36 + 7*4], pBt->incrVacuum); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1888 | #endif |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1889 | return SQLITE_OK; |
| 1890 | } |
| 1891 | |
| 1892 | /* |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1893 | ** Attempt to start a new transaction. A write-transaction |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 1894 | ** is started if the second argument is nonzero, otherwise a read- |
| 1895 | ** transaction. If the second argument is 2 or more and exclusive |
| 1896 | ** transaction is started, meaning that no other process is allowed |
| 1897 | ** to access the database. A preexisting transaction may not be |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1898 | ** upgraded to exclusive by calling this routine a second time - the |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 1899 | ** exclusivity flag only works for a new transaction. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1900 | ** |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1901 | ** A write-transaction must be started before attempting any |
| 1902 | ** changes to the database. None of the following routines |
| 1903 | ** will work unless a transaction is started first: |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1904 | ** |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1905 | ** sqlite3BtreeCreateTable() |
| 1906 | ** sqlite3BtreeCreateIndex() |
| 1907 | ** sqlite3BtreeClearTable() |
| 1908 | ** sqlite3BtreeDropTable() |
| 1909 | ** sqlite3BtreeInsert() |
| 1910 | ** sqlite3BtreeDelete() |
| 1911 | ** sqlite3BtreeUpdateMeta() |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1912 | ** |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1913 | ** If an initial attempt to acquire the lock fails because of lock contention |
| 1914 | ** and the database was previously unlocked, then invoke the busy handler |
| 1915 | ** if there is one. But if there was previously a read-lock, do not |
| 1916 | ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is |
| 1917 | ** returned when there is already a read-lock in order to avoid a deadlock. |
| 1918 | ** |
| 1919 | ** Suppose there are two processes A and B. A has a read lock and B has |
| 1920 | ** a reserved lock. B tries to promote to exclusive but is blocked because |
| 1921 | ** of A's read lock. A tries to promote to reserved but is blocked by B. |
| 1922 | ** One or the other of the two processes must give way or there can be |
| 1923 | ** no progress. By returning SQLITE_BUSY and not invoking the busy callback |
| 1924 | ** when A already has a read lock, we encourage A to give up and let B |
| 1925 | ** proceed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1926 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1927 | int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ |
| 1928 | BtShared *pBt = p->pBt; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1929 | int rc = SQLITE_OK; |
| 1930 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1931 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1932 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1933 | btreeIntegrity(p); |
| 1934 | |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1935 | /* If the btree is already in a write-transaction, or it |
| 1936 | ** is already in a read-transaction and a read-transaction |
| 1937 | ** is requested, this is a no-op. |
| 1938 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1939 | if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1940 | goto trans_begun; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1941 | } |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1942 | |
| 1943 | /* Write transactions are not possible on a read-only database */ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1944 | if( pBt->readOnly && wrflag ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1945 | rc = SQLITE_READONLY; |
| 1946 | goto trans_begun; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1947 | } |
| 1948 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1949 | /* If another database handle has already opened a write transaction |
| 1950 | ** on this shared-btree structure and a second write transaction is |
| 1951 | ** requested, return SQLITE_BUSY. |
| 1952 | */ |
| 1953 | if( pBt->inTransaction==TRANS_WRITE && wrflag ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1954 | rc = SQLITE_BUSY; |
| 1955 | goto trans_begun; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1956 | } |
| 1957 | |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 1958 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 1959 | if( wrflag>1 ){ |
| 1960 | BtLock *pIter; |
| 1961 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 1962 | if( pIter->pBtree!=p ){ |
| 1963 | rc = SQLITE_BUSY; |
| 1964 | goto trans_begun; |
| 1965 | } |
| 1966 | } |
| 1967 | } |
| 1968 | #endif |
| 1969 | |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1970 | do { |
drh | 8a9c17f | 2008-05-02 14:23:54 +0000 | [diff] [blame] | 1971 | if( pBt->pPage1==0 ){ |
| 1972 | do{ |
| 1973 | rc = lockBtree(pBt); |
| 1974 | }while( pBt->pPage1==0 && rc==SQLITE_OK ); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1975 | } |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 1976 | |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1977 | if( rc==SQLITE_OK && wrflag ){ |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 1978 | if( pBt->readOnly ){ |
| 1979 | rc = SQLITE_READONLY; |
| 1980 | }else{ |
| 1981 | rc = sqlite3PagerBegin(pBt->pPage1->pDbPage, wrflag>1); |
| 1982 | if( rc==SQLITE_OK ){ |
| 1983 | rc = newDatabase(pBt); |
| 1984 | } |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1985 | } |
| 1986 | } |
| 1987 | |
| 1988 | if( rc==SQLITE_OK ){ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1989 | if( wrflag ) pBt->inStmt = 0; |
| 1990 | }else{ |
| 1991 | unlockBtreeIfUnused(pBt); |
| 1992 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1993 | }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE && |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1994 | sqlite3BtreeInvokeBusyHandler(pBt, 0) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1995 | |
| 1996 | if( rc==SQLITE_OK ){ |
| 1997 | if( p->inTrans==TRANS_NONE ){ |
| 1998 | pBt->nTransaction++; |
| 1999 | } |
| 2000 | p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ); |
| 2001 | if( p->inTrans>pBt->inTransaction ){ |
| 2002 | pBt->inTransaction = p->inTrans; |
| 2003 | } |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 2004 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 2005 | if( wrflag>1 ){ |
| 2006 | assert( !pBt->pExclusive ); |
| 2007 | pBt->pExclusive = p; |
| 2008 | } |
| 2009 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2010 | } |
| 2011 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2012 | |
| 2013 | trans_begun: |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2014 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2015 | sqlite3BtreeLeave(p); |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 2016 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2017 | } |
| 2018 | |
drh | 4a0611d | 2008-07-18 17:16:26 +0000 | [diff] [blame] | 2019 | /* |
| 2020 | ** Return the size of the database file in pages. Or return -1 if |
| 2021 | ** there is any kind of error. |
| 2022 | */ |
| 2023 | static int pagerPagecount(Pager *pPager){ |
| 2024 | int rc; |
| 2025 | int nPage; |
| 2026 | rc = sqlite3PagerPagecount(pPager, &nPage); |
| 2027 | return (rc==SQLITE_OK?nPage:-1); |
| 2028 | } |
| 2029 | |
| 2030 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2031 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2032 | |
| 2033 | /* |
| 2034 | ** Set the pointer-map entries for all children of page pPage. Also, if |
| 2035 | ** pPage contains cells that point to overflow pages, set the pointer |
| 2036 | ** map entries for the overflow pages as well. |
| 2037 | */ |
| 2038 | static int setChildPtrmaps(MemPage *pPage){ |
| 2039 | int i; /* Counter variable */ |
| 2040 | int nCell; /* Number of cells in page pPage */ |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 2041 | int rc; /* Return code */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2042 | BtShared *pBt = pPage->pBt; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2043 | int isInitOrig = pPage->isInit; |
| 2044 | Pgno pgno = pPage->pgno; |
| 2045 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2046 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 2047 | rc = sqlite3BtreeInitPage(pPage, pPage->pParent); |
| 2048 | if( rc!=SQLITE_OK ){ |
| 2049 | goto set_child_ptrmaps_out; |
| 2050 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2051 | nCell = pPage->nCell; |
| 2052 | |
| 2053 | for(i=0; i<nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2054 | u8 *pCell = findCell(pPage, i); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2055 | |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 2056 | rc = ptrmapPutOvflPtr(pPage, pCell); |
| 2057 | if( rc!=SQLITE_OK ){ |
| 2058 | goto set_child_ptrmaps_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2059 | } |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 2060 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2061 | if( !pPage->leaf ){ |
| 2062 | Pgno childPgno = get4byte(pCell); |
| 2063 | rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno); |
danielk1977 | 1bc7159 | 2008-07-08 17:13:59 +0000 | [diff] [blame] | 2064 | if( rc!=SQLITE_OK ) goto set_child_ptrmaps_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2065 | } |
| 2066 | } |
| 2067 | |
| 2068 | if( !pPage->leaf ){ |
| 2069 | Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 2070 | rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno); |
| 2071 | } |
| 2072 | |
| 2073 | set_child_ptrmaps_out: |
| 2074 | pPage->isInit = isInitOrig; |
| 2075 | return rc; |
| 2076 | } |
| 2077 | |
| 2078 | /* |
| 2079 | ** Somewhere on pPage, which is guarenteed to be a btree page, not an overflow |
| 2080 | ** page, is a pointer to page iFrom. Modify this pointer so that it points to |
| 2081 | ** iTo. Parameter eType describes the type of pointer to be modified, as |
| 2082 | ** follows: |
| 2083 | ** |
| 2084 | ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child |
| 2085 | ** page of pPage. |
| 2086 | ** |
| 2087 | ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow |
| 2088 | ** page pointed to by one of the cells on pPage. |
| 2089 | ** |
| 2090 | ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next |
| 2091 | ** overflow page in the list. |
| 2092 | */ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2093 | static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2094 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2095 | if( eType==PTRMAP_OVERFLOW2 ){ |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 2096 | /* 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] | 2097 | if( get4byte(pPage->aData)!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2098 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2099 | } |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 2100 | put4byte(pPage->aData, iTo); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2101 | }else{ |
| 2102 | int isInitOrig = pPage->isInit; |
| 2103 | int i; |
| 2104 | int nCell; |
| 2105 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2106 | sqlite3BtreeInitPage(pPage, 0); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2107 | nCell = pPage->nCell; |
| 2108 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2109 | for(i=0; i<nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2110 | u8 *pCell = findCell(pPage, i); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2111 | if( eType==PTRMAP_OVERFLOW1 ){ |
| 2112 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2113 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2114 | if( info.iOverflow ){ |
| 2115 | if( iFrom==get4byte(&pCell[info.iOverflow]) ){ |
| 2116 | put4byte(&pCell[info.iOverflow], iTo); |
| 2117 | break; |
| 2118 | } |
| 2119 | } |
| 2120 | }else{ |
| 2121 | if( get4byte(pCell)==iFrom ){ |
| 2122 | put4byte(pCell, iTo); |
| 2123 | break; |
| 2124 | } |
| 2125 | } |
| 2126 | } |
| 2127 | |
| 2128 | if( i==nCell ){ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2129 | if( eType!=PTRMAP_BTREE || |
| 2130 | get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2131 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2132 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2133 | put4byte(&pPage->aData[pPage->hdrOffset+8], iTo); |
| 2134 | } |
| 2135 | |
| 2136 | pPage->isInit = isInitOrig; |
| 2137 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2138 | return SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2139 | } |
| 2140 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2141 | |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2142 | /* |
| 2143 | ** Move the open database page pDbPage to location iFreePage in the |
| 2144 | ** database. The pDbPage reference remains valid. |
| 2145 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2146 | static int relocatePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2147 | BtShared *pBt, /* Btree */ |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2148 | MemPage *pDbPage, /* Open page to move */ |
| 2149 | u8 eType, /* Pointer map 'type' entry for pDbPage */ |
| 2150 | Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */ |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 2151 | Pgno iFreePage, /* The location to move pDbPage to */ |
| 2152 | int isCommit |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2153 | ){ |
| 2154 | MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */ |
| 2155 | Pgno iDbPage = pDbPage->pgno; |
| 2156 | Pager *pPager = pBt->pPager; |
| 2157 | int rc; |
| 2158 | |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2159 | assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || |
| 2160 | eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2161 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 2162 | assert( pDbPage->pBt==pBt ); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2163 | |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 2164 | /* Move page iDbPage from its current location to page number iFreePage */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2165 | TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", |
| 2166 | iDbPage, iFreePage, iPtrPage, eType)); |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 2167 | rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2168 | if( rc!=SQLITE_OK ){ |
| 2169 | return rc; |
| 2170 | } |
| 2171 | pDbPage->pgno = iFreePage; |
| 2172 | |
| 2173 | /* If pDbPage was a btree-page, then it may have child pages and/or cells |
| 2174 | ** that point to overflow pages. The pointer map entries for all these |
| 2175 | ** pages need to be changed. |
| 2176 | ** |
| 2177 | ** If pDbPage is an overflow page, then the first 4 bytes may store a |
| 2178 | ** pointer to a subsequent overflow page. If this is the case, then |
| 2179 | ** the pointer map needs to be updated for the subsequent overflow page. |
| 2180 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2181 | if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2182 | rc = setChildPtrmaps(pDbPage); |
| 2183 | if( rc!=SQLITE_OK ){ |
| 2184 | return rc; |
| 2185 | } |
| 2186 | }else{ |
| 2187 | Pgno nextOvfl = get4byte(pDbPage->aData); |
| 2188 | if( nextOvfl!=0 ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2189 | rc = ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage); |
| 2190 | if( rc!=SQLITE_OK ){ |
| 2191 | return rc; |
| 2192 | } |
| 2193 | } |
| 2194 | } |
| 2195 | |
| 2196 | /* Fix the database pointer on page iPtrPage that pointed at iDbPage so |
| 2197 | ** that it points at iFreePage. Also fix the pointer map entry for |
| 2198 | ** iPtrPage. |
| 2199 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2200 | if( eType!=PTRMAP_ROOTPAGE ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2201 | rc = sqlite3BtreeGetPage(pBt, iPtrPage, &pPtrPage, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2202 | if( rc!=SQLITE_OK ){ |
| 2203 | return rc; |
| 2204 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2205 | rc = sqlite3PagerWrite(pPtrPage->pDbPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2206 | if( rc!=SQLITE_OK ){ |
| 2207 | releasePage(pPtrPage); |
| 2208 | return rc; |
| 2209 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2210 | rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2211 | releasePage(pPtrPage); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2212 | if( rc==SQLITE_OK ){ |
| 2213 | rc = ptrmapPut(pBt, iFreePage, eType, iPtrPage); |
| 2214 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2215 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2216 | return rc; |
| 2217 | } |
| 2218 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2219 | /* Forward declaration required by incrVacuumStep(). */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2220 | static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2221 | |
| 2222 | /* |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2223 | ** Perform a single step of an incremental-vacuum. If successful, |
| 2224 | ** return SQLITE_OK. If there is no work to do (and therefore no |
| 2225 | ** point in calling this function again), return SQLITE_DONE. |
| 2226 | ** |
| 2227 | ** More specificly, this function attempts to re-organize the |
| 2228 | ** database so that the last page of the file currently in use |
| 2229 | ** is no longer in use. |
| 2230 | ** |
| 2231 | ** If the nFin parameter is non-zero, the implementation assumes |
| 2232 | ** that the caller will keep calling incrVacuumStep() until |
| 2233 | ** it returns SQLITE_DONE or an error, and that nFin is the |
| 2234 | ** number of pages the database file will contain after this |
| 2235 | ** process is complete. |
| 2236 | */ |
| 2237 | static int incrVacuumStep(BtShared *pBt, Pgno nFin){ |
| 2238 | Pgno iLastPg; /* Last page in the database */ |
| 2239 | Pgno nFreeList; /* Number of pages still on the free-list */ |
| 2240 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2241 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2242 | iLastPg = pBt->nTrunc; |
| 2243 | if( iLastPg==0 ){ |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2244 | iLastPg = pagerPagecount(pBt->pPager); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2245 | } |
| 2246 | |
| 2247 | if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){ |
| 2248 | int rc; |
| 2249 | u8 eType; |
| 2250 | Pgno iPtrPage; |
| 2251 | |
| 2252 | nFreeList = get4byte(&pBt->pPage1->aData[36]); |
| 2253 | if( nFreeList==0 || nFin==iLastPg ){ |
| 2254 | return SQLITE_DONE; |
| 2255 | } |
| 2256 | |
| 2257 | rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage); |
| 2258 | if( rc!=SQLITE_OK ){ |
| 2259 | return rc; |
| 2260 | } |
| 2261 | if( eType==PTRMAP_ROOTPAGE ){ |
| 2262 | return SQLITE_CORRUPT_BKPT; |
| 2263 | } |
| 2264 | |
| 2265 | if( eType==PTRMAP_FREEPAGE ){ |
| 2266 | if( nFin==0 ){ |
| 2267 | /* Remove the page from the files free-list. This is not required |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 2268 | ** if nFin is non-zero. In that case, the free-list will be |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2269 | ** truncated to zero after this function returns, so it doesn't |
| 2270 | ** matter if it still contains some garbage entries. |
| 2271 | */ |
| 2272 | Pgno iFreePg; |
| 2273 | MemPage *pFreePg; |
| 2274 | rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1); |
| 2275 | if( rc!=SQLITE_OK ){ |
| 2276 | return rc; |
| 2277 | } |
| 2278 | assert( iFreePg==iLastPg ); |
| 2279 | releasePage(pFreePg); |
| 2280 | } |
| 2281 | } else { |
| 2282 | Pgno iFreePg; /* Index of free page to move pLastPg to */ |
| 2283 | MemPage *pLastPg; |
| 2284 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2285 | rc = sqlite3BtreeGetPage(pBt, iLastPg, &pLastPg, 0); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2286 | if( rc!=SQLITE_OK ){ |
| 2287 | return rc; |
| 2288 | } |
| 2289 | |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 2290 | /* If nFin is zero, this loop runs exactly once and page pLastPg |
| 2291 | ** is swapped with the first free page pulled off the free list. |
| 2292 | ** |
| 2293 | ** On the other hand, if nFin is greater than zero, then keep |
| 2294 | ** looping until a free-page located within the first nFin pages |
| 2295 | ** of the file is found. |
| 2296 | */ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2297 | do { |
| 2298 | MemPage *pFreePg; |
| 2299 | rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0); |
| 2300 | if( rc!=SQLITE_OK ){ |
| 2301 | releasePage(pLastPg); |
| 2302 | return rc; |
| 2303 | } |
| 2304 | releasePage(pFreePg); |
| 2305 | }while( nFin!=0 && iFreePg>nFin ); |
| 2306 | assert( iFreePg<iLastPg ); |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 2307 | |
| 2308 | rc = sqlite3PagerWrite(pLastPg->pDbPage); |
danielk1977 | 662278e | 2007-11-05 15:30:12 +0000 | [diff] [blame] | 2309 | if( rc==SQLITE_OK ){ |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 2310 | rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, nFin!=0); |
danielk1977 | 662278e | 2007-11-05 15:30:12 +0000 | [diff] [blame] | 2311 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2312 | releasePage(pLastPg); |
| 2313 | if( rc!=SQLITE_OK ){ |
| 2314 | return rc; |
danielk1977 | 662278e | 2007-11-05 15:30:12 +0000 | [diff] [blame] | 2315 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2316 | } |
| 2317 | } |
| 2318 | |
| 2319 | pBt->nTrunc = iLastPg - 1; |
| 2320 | while( pBt->nTrunc==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, pBt->nTrunc) ){ |
| 2321 | pBt->nTrunc--; |
| 2322 | } |
| 2323 | return SQLITE_OK; |
| 2324 | } |
| 2325 | |
| 2326 | /* |
| 2327 | ** A write-transaction must be opened before calling this function. |
| 2328 | ** It performs a single unit of work towards an incremental vacuum. |
| 2329 | ** |
| 2330 | ** If the incremental vacuum is finished after this function has run, |
| 2331 | ** SQLITE_DONE is returned. If it is not finished, but no error occured, |
| 2332 | ** SQLITE_OK is returned. Otherwise an SQLite error code. |
| 2333 | */ |
| 2334 | int sqlite3BtreeIncrVacuum(Btree *p){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2335 | int rc; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2336 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2337 | |
| 2338 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2339 | pBt->db = p->db; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2340 | assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE ); |
| 2341 | if( !pBt->autoVacuum ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2342 | rc = SQLITE_DONE; |
| 2343 | }else{ |
| 2344 | invalidateAllOverflowCache(pBt); |
| 2345 | rc = incrVacuumStep(pBt, 0); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2346 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2347 | sqlite3BtreeLeave(p); |
| 2348 | return rc; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2349 | } |
| 2350 | |
| 2351 | /* |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2352 | ** This routine is called prior to sqlite3PagerCommit when a transaction |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2353 | ** is commited for an auto-vacuum database. |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 2354 | ** |
| 2355 | ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages |
| 2356 | ** the database file should be truncated to during the commit process. |
| 2357 | ** i.e. the database has been reorganized so that only the first *pnTrunc |
| 2358 | ** pages are in use. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2359 | */ |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 2360 | static int autoVacuumCommit(BtShared *pBt, Pgno *pnTrunc){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2361 | int rc = SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2362 | Pager *pPager = pBt->pPager; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2363 | #ifndef NDEBUG |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2364 | int nRef = sqlite3PagerRefcount(pPager); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2365 | #endif |
| 2366 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2367 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 2368 | invalidateAllOverflowCache(pBt); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2369 | assert(pBt->autoVacuum); |
| 2370 | if( !pBt->incrVacuum ){ |
| 2371 | Pgno nFin = 0; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2372 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2373 | if( pBt->nTrunc==0 ){ |
| 2374 | Pgno nFree; |
| 2375 | Pgno nPtrmap; |
| 2376 | const int pgsz = pBt->pageSize; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2377 | int nOrig = pagerPagecount(pBt->pPager); |
danielk1977 | e5321f0 | 2007-04-27 07:05:44 +0000 | [diff] [blame] | 2378 | |
| 2379 | if( PTRMAP_ISPAGE(pBt, nOrig) ){ |
| 2380 | return SQLITE_CORRUPT_BKPT; |
| 2381 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2382 | if( nOrig==PENDING_BYTE_PAGE(pBt) ){ |
| 2383 | nOrig--; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2384 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2385 | nFree = get4byte(&pBt->pPage1->aData[36]); |
| 2386 | nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+pgsz/5)/(pgsz/5); |
| 2387 | nFin = nOrig - nFree - nPtrmap; |
| 2388 | if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<=PENDING_BYTE_PAGE(pBt) ){ |
| 2389 | nFin--; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 2390 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2391 | while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){ |
| 2392 | nFin--; |
| 2393 | } |
| 2394 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2395 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2396 | while( rc==SQLITE_OK ){ |
| 2397 | rc = incrVacuumStep(pBt, nFin); |
| 2398 | } |
| 2399 | if( rc==SQLITE_DONE ){ |
| 2400 | assert(nFin==0 || pBt->nTrunc==0 || nFin<=pBt->nTrunc); |
| 2401 | rc = SQLITE_OK; |
danielk1977 | 0ba32df | 2008-05-07 07:13:16 +0000 | [diff] [blame] | 2402 | if( pBt->nTrunc && nFin ){ |
drh | 67f80b6 | 2007-07-23 19:26:17 +0000 | [diff] [blame] | 2403 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2404 | put4byte(&pBt->pPage1->aData[32], 0); |
| 2405 | put4byte(&pBt->pPage1->aData[36], 0); |
| 2406 | pBt->nTrunc = nFin; |
| 2407 | } |
| 2408 | } |
| 2409 | if( rc!=SQLITE_OK ){ |
| 2410 | sqlite3PagerRollback(pPager); |
| 2411 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2412 | } |
| 2413 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2414 | if( rc==SQLITE_OK ){ |
| 2415 | *pnTrunc = pBt->nTrunc; |
| 2416 | pBt->nTrunc = 0; |
| 2417 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2418 | assert( nRef==sqlite3PagerRefcount(pPager) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2419 | return rc; |
| 2420 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2421 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2422 | #endif |
| 2423 | |
| 2424 | /* |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2425 | ** This routine does the first phase of a two-phase commit. This routine |
| 2426 | ** causes a rollback journal to be created (if it does not already exist) |
| 2427 | ** and populated with enough information so that if a power loss occurs |
| 2428 | ** the database can be restored to its original state by playing back |
| 2429 | ** the journal. Then the contents of the journal are flushed out to |
| 2430 | ** the disk. After the journal is safely on oxide, the changes to the |
| 2431 | ** database are written into the database file and flushed to oxide. |
| 2432 | ** At the end of this call, the rollback journal still exists on the |
| 2433 | ** disk and we are still holding all locks, so the transaction has not |
| 2434 | ** committed. See sqlite3BtreeCommit() for the second phase of the |
| 2435 | ** commit process. |
| 2436 | ** |
| 2437 | ** This call is a no-op if no write-transaction is currently active on pBt. |
| 2438 | ** |
| 2439 | ** Otherwise, sync the database file for the btree pBt. zMaster points to |
| 2440 | ** the name of a master journal file that should be written into the |
| 2441 | ** individual journal file, or is NULL, indicating no master journal file |
| 2442 | ** (single database transaction). |
| 2443 | ** |
| 2444 | ** When this is called, the master journal should already have been |
| 2445 | ** created, populated with this journal pointer and synced to disk. |
| 2446 | ** |
| 2447 | ** Once this is routine has returned, the only thing required to commit |
| 2448 | ** the write-transaction for this database file is to delete the journal. |
| 2449 | */ |
| 2450 | int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){ |
| 2451 | int rc = SQLITE_OK; |
| 2452 | if( p->inTrans==TRANS_WRITE ){ |
| 2453 | BtShared *pBt = p->pBt; |
| 2454 | Pgno nTrunc = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2455 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2456 | pBt->db = p->db; |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2457 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2458 | if( pBt->autoVacuum ){ |
| 2459 | rc = autoVacuumCommit(pBt, &nTrunc); |
| 2460 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2461 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2462 | return rc; |
| 2463 | } |
| 2464 | } |
| 2465 | #endif |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 2466 | rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, nTrunc, 0); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2467 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2468 | } |
| 2469 | return rc; |
| 2470 | } |
| 2471 | |
| 2472 | /* |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2473 | ** Commit the transaction currently in progress. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2474 | ** |
drh | 6e34599 | 2007-03-30 11:12:08 +0000 | [diff] [blame] | 2475 | ** This routine implements the second phase of a 2-phase commit. The |
| 2476 | ** sqlite3BtreeSync() routine does the first phase and should be invoked |
| 2477 | ** prior to calling this routine. The sqlite3BtreeSync() routine did |
| 2478 | ** all the work of writing information out to disk and flushing the |
| 2479 | ** contents so that they are written onto the disk platter. All this |
| 2480 | ** routine has to do is delete or truncate the rollback journal |
| 2481 | ** (which causes the transaction to commit) and drop locks. |
| 2482 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2483 | ** This will release the write lock on the database file. If there |
| 2484 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2485 | */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2486 | int sqlite3BtreeCommitPhaseTwo(Btree *p){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2487 | BtShared *pBt = p->pBt; |
| 2488 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2489 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2490 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2491 | btreeIntegrity(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2492 | |
| 2493 | /* If the handle has a write-transaction open, commit the shared-btrees |
| 2494 | ** transaction and set the shared state to TRANS_READ. |
| 2495 | */ |
| 2496 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2497 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2498 | assert( pBt->inTransaction==TRANS_WRITE ); |
| 2499 | assert( pBt->nTransaction>0 ); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2500 | rc = sqlite3PagerCommitPhaseTwo(pBt->pPager); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2501 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2502 | sqlite3BtreeLeave(p); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2503 | return rc; |
| 2504 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2505 | pBt->inTransaction = TRANS_READ; |
| 2506 | pBt->inStmt = 0; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2507 | } |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2508 | unlockAllTables(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2509 | |
| 2510 | /* If the handle has any kind of transaction open, decrement the transaction |
| 2511 | ** count of the shared btree. If the transaction count reaches 0, set |
| 2512 | ** the shared state to TRANS_NONE. The unlockBtreeIfUnused() call below |
| 2513 | ** will unlock the pager. |
| 2514 | */ |
| 2515 | if( p->inTrans!=TRANS_NONE ){ |
| 2516 | pBt->nTransaction--; |
| 2517 | if( 0==pBt->nTransaction ){ |
| 2518 | pBt->inTransaction = TRANS_NONE; |
| 2519 | } |
| 2520 | } |
| 2521 | |
| 2522 | /* Set the handles current transaction state to TRANS_NONE and unlock |
| 2523 | ** the pager if this call closed the only read or write transaction. |
| 2524 | */ |
| 2525 | p->inTrans = TRANS_NONE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2526 | unlockBtreeIfUnused(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2527 | |
| 2528 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2529 | sqlite3BtreeLeave(p); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2530 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2531 | } |
| 2532 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2533 | /* |
| 2534 | ** Do both phases of a commit. |
| 2535 | */ |
| 2536 | int sqlite3BtreeCommit(Btree *p){ |
| 2537 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2538 | sqlite3BtreeEnter(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2539 | rc = sqlite3BtreeCommitPhaseOne(p, 0); |
| 2540 | if( rc==SQLITE_OK ){ |
| 2541 | rc = sqlite3BtreeCommitPhaseTwo(p); |
| 2542 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2543 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2544 | return rc; |
| 2545 | } |
| 2546 | |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2547 | #ifndef NDEBUG |
| 2548 | /* |
| 2549 | ** Return the number of write-cursors open on this handle. This is for use |
| 2550 | ** in assert() expressions, so it is only compiled if NDEBUG is not |
| 2551 | ** defined. |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2552 | ** |
| 2553 | ** For the purposes of this routine, a write-cursor is any cursor that |
| 2554 | ** is capable of writing to the databse. That means the cursor was |
| 2555 | ** originally opened for writing and the cursor has not be disabled |
| 2556 | ** by having its state changed to CURSOR_FAULT. |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2557 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2558 | static int countWriteCursors(BtShared *pBt){ |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2559 | BtCursor *pCur; |
| 2560 | int r = 0; |
| 2561 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2562 | if( pCur->wrFlag && pCur->eState!=CURSOR_FAULT ) r++; |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2563 | } |
| 2564 | return r; |
| 2565 | } |
| 2566 | #endif |
| 2567 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2568 | /* |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2569 | ** This routine sets the state to CURSOR_FAULT and the error |
| 2570 | ** code to errCode for every cursor on BtShared that pBtree |
| 2571 | ** references. |
| 2572 | ** |
| 2573 | ** Every cursor is tripped, including cursors that belong |
| 2574 | ** to other database connections that happen to be sharing |
| 2575 | ** the cache with pBtree. |
| 2576 | ** |
| 2577 | ** This routine gets called when a rollback occurs. |
| 2578 | ** All cursors using the same cache must be tripped |
| 2579 | ** to prevent them from trying to use the btree after |
| 2580 | ** the rollback. The rollback may have deleted tables |
| 2581 | ** or moved root pages, so it is not sufficient to |
| 2582 | ** save the state of the cursor. The cursor must be |
| 2583 | ** invalidated. |
| 2584 | */ |
| 2585 | void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){ |
| 2586 | BtCursor *p; |
| 2587 | sqlite3BtreeEnter(pBtree); |
| 2588 | for(p=pBtree->pBt->pCursor; p; p=p->pNext){ |
| 2589 | clearCursorPosition(p); |
| 2590 | p->eState = CURSOR_FAULT; |
| 2591 | p->skip = errCode; |
| 2592 | } |
| 2593 | sqlite3BtreeLeave(pBtree); |
| 2594 | } |
| 2595 | |
| 2596 | /* |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2597 | ** Rollback the transaction in progress. All cursors will be |
| 2598 | ** invalided by this operation. Any attempt to use a cursor |
| 2599 | ** that was open at the beginning of this operation will result |
| 2600 | ** in an error. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2601 | ** |
| 2602 | ** This will release the write lock on the database file. If there |
| 2603 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2604 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2605 | int sqlite3BtreeRollback(Btree *p){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2606 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2607 | BtShared *pBt = p->pBt; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2608 | MemPage *pPage1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2609 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2610 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2611 | pBt->db = p->db; |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2612 | rc = saveAllCursors(pBt, 0, 0); |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2613 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2614 | if( rc!=SQLITE_OK ){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2615 | /* This is a horrible situation. An IO or malloc() error occured whilst |
| 2616 | ** trying to save cursor positions. If this is an automatic rollback (as |
| 2617 | ** the result of a constraint, malloc() failure or IO error) then |
| 2618 | ** the cache may be internally inconsistent (not contain valid trees) so |
| 2619 | ** we cannot simply return the error to the caller. Instead, abort |
| 2620 | ** all queries that may be using any of the cursors that failed to save. |
| 2621 | */ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2622 | sqlite3BtreeTripAllCursors(p, rc); |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2623 | } |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2624 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2625 | btreeIntegrity(p); |
| 2626 | unlockAllTables(p); |
| 2627 | |
| 2628 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2629 | int rc2; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2630 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2631 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2632 | pBt->nTrunc = 0; |
| 2633 | #endif |
| 2634 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2635 | assert( TRANS_WRITE==pBt->inTransaction ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2636 | rc2 = sqlite3PagerRollback(pBt->pPager); |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2637 | if( rc2!=SQLITE_OK ){ |
| 2638 | rc = rc2; |
| 2639 | } |
| 2640 | |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2641 | /* The rollback may have destroyed the pPage1->aData value. So |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2642 | ** call sqlite3BtreeGetPage() on page 1 again to make |
| 2643 | ** sure pPage1->aData is set correctly. */ |
| 2644 | if( sqlite3BtreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2645 | releasePage(pPage1); |
| 2646 | } |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2647 | assert( countWriteCursors(pBt)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2648 | pBt->inTransaction = TRANS_READ; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2649 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2650 | |
| 2651 | if( p->inTrans!=TRANS_NONE ){ |
| 2652 | assert( pBt->nTransaction>0 ); |
| 2653 | pBt->nTransaction--; |
| 2654 | if( 0==pBt->nTransaction ){ |
| 2655 | pBt->inTransaction = TRANS_NONE; |
| 2656 | } |
| 2657 | } |
| 2658 | |
| 2659 | p->inTrans = TRANS_NONE; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2660 | pBt->inStmt = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2661 | unlockBtreeIfUnused(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2662 | |
| 2663 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2664 | sqlite3BtreeLeave(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2665 | return rc; |
| 2666 | } |
| 2667 | |
| 2668 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2669 | ** Start a statement subtransaction. The subtransaction can |
| 2670 | ** can be rolled back independently of the main transaction. |
| 2671 | ** You must start a transaction before starting a subtransaction. |
| 2672 | ** The subtransaction is ended automatically if the main transaction |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2673 | ** commits or rolls back. |
| 2674 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2675 | ** Only one subtransaction may be active at a time. It is an error to try |
| 2676 | ** to start a new subtransaction if another subtransaction is already active. |
| 2677 | ** |
| 2678 | ** Statement subtransactions are used around individual SQL statements |
| 2679 | ** that are contained within a BEGIN...COMMIT block. If a constraint |
| 2680 | ** error occurs within the statement, the effect of that one statement |
| 2681 | ** can be rolled back without having to rollback the entire transaction. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2682 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2683 | int sqlite3BtreeBeginStmt(Btree *p){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2684 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2685 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2686 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2687 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2688 | if( (p->inTrans!=TRANS_WRITE) || pBt->inStmt ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2689 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
| 2690 | }else{ |
| 2691 | assert( pBt->inTransaction==TRANS_WRITE ); |
| 2692 | rc = pBt->readOnly ? SQLITE_OK : sqlite3PagerStmtBegin(pBt->pPager); |
| 2693 | pBt->inStmt = 1; |
drh | 0d65dc0 | 2002-02-03 00:56:09 +0000 | [diff] [blame] | 2694 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2695 | sqlite3BtreeLeave(p); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2696 | return rc; |
| 2697 | } |
| 2698 | |
| 2699 | |
| 2700 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2701 | ** Commit the statment subtransaction currently in progress. If no |
| 2702 | ** subtransaction is active, this is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2703 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2704 | int sqlite3BtreeCommitStmt(Btree *p){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2705 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2706 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2707 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2708 | pBt->db = p->db; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2709 | if( pBt->inStmt && !pBt->readOnly ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2710 | rc = sqlite3PagerStmtCommit(pBt->pPager); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2711 | }else{ |
| 2712 | rc = SQLITE_OK; |
| 2713 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2714 | pBt->inStmt = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2715 | sqlite3BtreeLeave(p); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2716 | return rc; |
| 2717 | } |
| 2718 | |
| 2719 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2720 | ** Rollback the active statement subtransaction. If no subtransaction |
| 2721 | ** is active this routine is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2722 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2723 | ** All cursors will be invalidated by this operation. Any attempt |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2724 | ** to use a cursor that was open at the beginning of this operation |
| 2725 | ** will result in an error. |
| 2726 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2727 | int sqlite3BtreeRollbackStmt(Btree *p){ |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2728 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2729 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2730 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2731 | pBt->db = p->db; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2732 | if( pBt->inStmt && !pBt->readOnly ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2733 | rc = sqlite3PagerStmtRollback(pBt->pPager); |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2734 | pBt->inStmt = 0; |
| 2735 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2736 | sqlite3BtreeLeave(p); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2737 | return rc; |
| 2738 | } |
| 2739 | |
| 2740 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2741 | ** Create a new cursor for the BTree whose root is on the page |
| 2742 | ** iTable. The act of acquiring a cursor gets a read lock on |
| 2743 | ** the database file. |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 2744 | ** |
| 2745 | ** If wrFlag==0, then the cursor can only be used for reading. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2746 | ** If wrFlag==1, then the cursor can be used for reading or for |
| 2747 | ** writing if other conditions for writing are also met. These |
| 2748 | ** are the conditions that must be met in order for writing to |
| 2749 | ** be allowed: |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2750 | ** |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2751 | ** 1: The cursor must have been opened with wrFlag==1 |
| 2752 | ** |
drh | fe5d71d | 2007-03-19 11:54:10 +0000 | [diff] [blame] | 2753 | ** 2: Other database connections that share the same pager cache |
| 2754 | ** but which are not in the READ_UNCOMMITTED state may not have |
| 2755 | ** cursors open with wrFlag==0 on the same table. Otherwise |
| 2756 | ** the changes made by this write cursor would be visible to |
| 2757 | ** the read cursors in the other database connection. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2758 | ** |
| 2759 | ** 3: The database must be writable (not on read-only media) |
| 2760 | ** |
| 2761 | ** 4: There must be an active transaction. |
| 2762 | ** |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2763 | ** No checking is done to make sure that page iTable really is the |
| 2764 | ** root page of a b-tree. If it is not, then the cursor acquired |
| 2765 | ** will not work correctly. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2766 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2767 | static int btreeCursor( |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2768 | Btree *p, /* The btree */ |
| 2769 | int iTable, /* Root page of table to open */ |
| 2770 | int wrFlag, /* 1 to write. 0 read-only */ |
| 2771 | struct KeyInfo *pKeyInfo, /* First arg to comparison function */ |
| 2772 | BtCursor *pCur /* Space for new cursor */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2773 | ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2774 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2775 | BtShared *pBt = p->pBt; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2776 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2777 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2778 | if( wrFlag ){ |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2779 | if( pBt->readOnly ){ |
| 2780 | return SQLITE_READONLY; |
| 2781 | } |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 2782 | if( checkReadLocks(p, iTable, 0, 0) ){ |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2783 | return SQLITE_LOCKED; |
| 2784 | } |
drh | a0c9a11 | 2004-03-10 13:42:37 +0000 | [diff] [blame] | 2785 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2786 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2787 | if( pBt->pPage1==0 ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2788 | rc = lockBtreeWithRetry(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2789 | if( rc!=SQLITE_OK ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2790 | return rc; |
| 2791 | } |
drh | 1831f18 | 2007-04-24 17:35:59 +0000 | [diff] [blame] | 2792 | if( pBt->readOnly && wrFlag ){ |
| 2793 | return SQLITE_READONLY; |
| 2794 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2795 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2796 | pCur->pgnoRoot = (Pgno)iTable; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2797 | if( iTable==1 && pagerPagecount(pBt->pPager)==0 ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2798 | rc = SQLITE_EMPTY; |
| 2799 | goto create_cursor_exception; |
| 2800 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2801 | rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->pPage, 0); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2802 | if( rc!=SQLITE_OK ){ |
| 2803 | goto create_cursor_exception; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2804 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2805 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2806 | /* Now that no other errors can occur, finish filling in the BtCursor |
| 2807 | ** variables, link the cursor into the BtShared list and set *ppCur (the |
| 2808 | ** output argument to this function). |
| 2809 | */ |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2810 | pCur->pKeyInfo = pKeyInfo; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2811 | pCur->pBtree = p; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 2812 | pCur->pBt = pBt; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2813 | pCur->wrFlag = wrFlag; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2814 | pCur->pNext = pBt->pCursor; |
| 2815 | if( pCur->pNext ){ |
| 2816 | pCur->pNext->pPrev = pCur; |
| 2817 | } |
| 2818 | pBt->pCursor = pCur; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2819 | pCur->eState = CURSOR_INVALID; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2820 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2821 | return SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2822 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2823 | create_cursor_exception: |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 2824 | releasePage(pCur->pPage); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2825 | unlockBtreeIfUnused(pBt); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2826 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2827 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2828 | int sqlite3BtreeCursor( |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2829 | Btree *p, /* The btree */ |
| 2830 | int iTable, /* Root page of table to open */ |
| 2831 | int wrFlag, /* 1 to write. 0 read-only */ |
| 2832 | struct KeyInfo *pKeyInfo, /* First arg to xCompare() */ |
| 2833 | BtCursor *pCur /* Write new cursor here */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2834 | ){ |
| 2835 | int rc; |
| 2836 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2837 | p->pBt->db = p->db; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2838 | rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2839 | sqlite3BtreeLeave(p); |
| 2840 | return rc; |
| 2841 | } |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2842 | int sqlite3BtreeCursorSize(){ |
| 2843 | return sizeof(BtCursor); |
| 2844 | } |
| 2845 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2846 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2847 | |
| 2848 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2849 | ** Close a cursor. The read lock on the database file is released |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2850 | ** when the last cursor is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2851 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2852 | int sqlite3BtreeCloseCursor(BtCursor *pCur){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 2853 | Btree *pBtree = pCur->pBtree; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2854 | if( pBtree ){ |
| 2855 | BtShared *pBt = pCur->pBt; |
| 2856 | sqlite3BtreeEnter(pBtree); |
| 2857 | pBt->db = pBtree->db; |
| 2858 | clearCursorPosition(pCur); |
| 2859 | if( pCur->pPrev ){ |
| 2860 | pCur->pPrev->pNext = pCur->pNext; |
| 2861 | }else{ |
| 2862 | pBt->pCursor = pCur->pNext; |
| 2863 | } |
| 2864 | if( pCur->pNext ){ |
| 2865 | pCur->pNext->pPrev = pCur->pPrev; |
| 2866 | } |
| 2867 | releasePage(pCur->pPage); |
| 2868 | unlockBtreeIfUnused(pBt); |
| 2869 | invalidateOverflowCache(pCur); |
| 2870 | /* sqlite3_free(pCur); */ |
| 2871 | sqlite3BtreeLeave(pBtree); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2872 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2873 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2874 | } |
| 2875 | |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2876 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2877 | ** Make a temporary cursor by filling in the fields of pTempCur. |
| 2878 | ** The temporary cursor is not on the cursor list for the Btree. |
| 2879 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2880 | void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2881 | assert( cursorHoldsMutex(pCur) ); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2882 | memcpy(pTempCur, pCur, sizeof(*pCur)); |
| 2883 | pTempCur->pNext = 0; |
| 2884 | pTempCur->pPrev = 0; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2885 | if( pTempCur->pPage ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2886 | sqlite3PagerRef(pTempCur->pPage->pDbPage); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2887 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2888 | } |
| 2889 | |
| 2890 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2891 | ** Delete a temporary cursor such as was made by the CreateTemporaryCursor() |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2892 | ** function above. |
| 2893 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2894 | void sqlite3BtreeReleaseTempCursor(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2895 | assert( cursorHoldsMutex(pCur) ); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2896 | if( pCur->pPage ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2897 | sqlite3PagerUnref(pCur->pPage->pDbPage); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2898 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
| 2901 | /* |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2902 | ** Make sure the BtCursor* given in the argument has a valid |
| 2903 | ** BtCursor.info structure. If it is not already valid, call |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2904 | ** sqlite3BtreeParseCell() to fill it in. |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2905 | ** |
| 2906 | ** BtCursor.info is a cache of the information in the current cell. |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2907 | ** Using this cache reduces the number of calls to sqlite3BtreeParseCell(). |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2908 | ** |
| 2909 | ** 2007-06-25: There is a bug in some versions of MSVC that cause the |
| 2910 | ** compiler to crash when getCellInfo() is implemented as a macro. |
| 2911 | ** But there is a measureable speed advantage to using the macro on gcc |
| 2912 | ** (when less compiler optimizations like -Os or -O0 are used and the |
| 2913 | ** compiler is not doing agressive inlining.) So we use a real function |
| 2914 | ** for MSVC and a macro for everything else. Ticket #2457. |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2915 | */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2916 | #ifndef NDEBUG |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2917 | static void assertCellInfo(BtCursor *pCur){ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2918 | CellInfo info; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 2919 | memset(&info, 0, sizeof(info)); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2920 | sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &info); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2921 | assert( memcmp(&info, &pCur->info, sizeof(info))==0 ); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2922 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2923 | #else |
| 2924 | #define assertCellInfo(x) |
| 2925 | #endif |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2926 | #ifdef _MSC_VER |
| 2927 | /* Use a real function in MSVC to work around bugs in that compiler. */ |
| 2928 | static void getCellInfo(BtCursor *pCur){ |
| 2929 | if( pCur->info.nSize==0 ){ |
| 2930 | sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &pCur->info); |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 2931 | pCur->validNKey = 1; |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2932 | }else{ |
| 2933 | assertCellInfo(pCur); |
| 2934 | } |
| 2935 | } |
| 2936 | #else /* if not _MSC_VER */ |
| 2937 | /* Use a macro in all other compilers so that the function is inlined */ |
| 2938 | #define getCellInfo(pCur) \ |
| 2939 | if( pCur->info.nSize==0 ){ \ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2940 | sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &pCur->info); \ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 2941 | pCur->validNKey = 1; \ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2942 | }else{ \ |
| 2943 | assertCellInfo(pCur); \ |
| 2944 | } |
| 2945 | #endif /* _MSC_VER */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2946 | |
| 2947 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2948 | ** Set *pSize to the size of the buffer needed to hold the value of |
| 2949 | ** the key for the current entry. If the cursor is not pointing |
| 2950 | ** to a valid entry, *pSize is set to 0. |
| 2951 | ** |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2952 | ** For a table with the INTKEY flag set, this routine returns the key |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2953 | ** itself, not the number of bytes in the key. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2954 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 2955 | int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2956 | int rc; |
| 2957 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2958 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 2959 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2960 | if( rc==SQLITE_OK ){ |
| 2961 | assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID ); |
| 2962 | if( pCur->eState==CURSOR_INVALID ){ |
| 2963 | *pSize = 0; |
| 2964 | }else{ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2965 | getCellInfo(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2966 | *pSize = pCur->info.nKey; |
| 2967 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2968 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2969 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2970 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2971 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2972 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2973 | ** Set *pSize to the number of bytes of data in the entry the |
| 2974 | ** cursor currently points to. Always return SQLITE_OK. |
| 2975 | ** Failure is not possible. If the cursor is not currently |
| 2976 | ** pointing to an entry (which can happen, for example, if |
| 2977 | ** the database is empty) then *pSize is set to 0. |
| 2978 | */ |
| 2979 | int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2980 | int rc; |
| 2981 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2982 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 2983 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2984 | if( rc==SQLITE_OK ){ |
| 2985 | assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID ); |
| 2986 | if( pCur->eState==CURSOR_INVALID ){ |
| 2987 | /* Not pointing at a valid entry - set *pSize to 0. */ |
| 2988 | *pSize = 0; |
| 2989 | }else{ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2990 | getCellInfo(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2991 | *pSize = pCur->info.nData; |
| 2992 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2993 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2994 | return rc; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2995 | } |
| 2996 | |
| 2997 | /* |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2998 | ** Given the page number of an overflow page in the database (parameter |
| 2999 | ** ovfl), this function finds the page number of the next page in the |
| 3000 | ** linked list of overflow pages. If possible, it uses the auto-vacuum |
| 3001 | ** pointer-map data instead of reading the content of page ovfl to do so. |
| 3002 | ** |
| 3003 | ** If an error occurs an SQLite error code is returned. Otherwise: |
| 3004 | ** |
| 3005 | ** Unless pPgnoNext is NULL, the page number of the next overflow |
| 3006 | ** page in the linked list is written to *pPgnoNext. If page ovfl |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 3007 | ** is the last page in its linked list, *pPgnoNext is set to zero. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3008 | ** |
| 3009 | ** If ppPage is not NULL, *ppPage is set to the MemPage* handle |
| 3010 | ** for page ovfl. The underlying pager page may have been requested |
| 3011 | ** with the noContent flag set, so the page data accessable via |
| 3012 | ** this handle may not be trusted. |
| 3013 | */ |
| 3014 | static int getOverflowPage( |
| 3015 | BtShared *pBt, |
| 3016 | Pgno ovfl, /* Overflow page */ |
| 3017 | MemPage **ppPage, /* OUT: MemPage handle */ |
| 3018 | Pgno *pPgnoNext /* OUT: Next overflow page number */ |
| 3019 | ){ |
| 3020 | Pgno next = 0; |
| 3021 | int rc; |
| 3022 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3023 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3024 | /* One of these must not be NULL. Otherwise, why call this function? */ |
| 3025 | assert(ppPage || pPgnoNext); |
| 3026 | |
| 3027 | /* If pPgnoNext is NULL, then this function is being called to obtain |
| 3028 | ** a MemPage* reference only. No page-data is required in this case. |
| 3029 | */ |
| 3030 | if( !pPgnoNext ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3031 | return sqlite3BtreeGetPage(pBt, ovfl, ppPage, 1); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3032 | } |
| 3033 | |
| 3034 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3035 | /* Try to find the next page in the overflow list using the |
| 3036 | ** autovacuum pointer-map pages. Guess that the next page in |
| 3037 | ** the overflow list is page number (ovfl+1). If that guess turns |
| 3038 | ** out to be wrong, fall back to loading the data of page |
| 3039 | ** number ovfl to determine the next page number. |
| 3040 | */ |
| 3041 | if( pBt->autoVacuum ){ |
| 3042 | Pgno pgno; |
| 3043 | Pgno iGuess = ovfl+1; |
| 3044 | u8 eType; |
| 3045 | |
| 3046 | while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){ |
| 3047 | iGuess++; |
| 3048 | } |
| 3049 | |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 3050 | if( iGuess<=pagerPagecount(pBt->pPager) ){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3051 | rc = ptrmapGet(pBt, iGuess, &eType, &pgno); |
| 3052 | if( rc!=SQLITE_OK ){ |
| 3053 | return rc; |
| 3054 | } |
| 3055 | if( eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){ |
| 3056 | next = iGuess; |
| 3057 | } |
| 3058 | } |
| 3059 | } |
| 3060 | #endif |
| 3061 | |
| 3062 | if( next==0 || ppPage ){ |
| 3063 | MemPage *pPage = 0; |
| 3064 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3065 | rc = sqlite3BtreeGetPage(pBt, ovfl, &pPage, next!=0); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3066 | assert(rc==SQLITE_OK || pPage==0); |
| 3067 | if( next==0 && rc==SQLITE_OK ){ |
| 3068 | next = get4byte(pPage->aData); |
| 3069 | } |
| 3070 | |
| 3071 | if( ppPage ){ |
| 3072 | *ppPage = pPage; |
| 3073 | }else{ |
| 3074 | releasePage(pPage); |
| 3075 | } |
| 3076 | } |
| 3077 | *pPgnoNext = next; |
| 3078 | |
| 3079 | return rc; |
| 3080 | } |
| 3081 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3082 | /* |
| 3083 | ** Copy data from a buffer to a page, or from a page to a buffer. |
| 3084 | ** |
| 3085 | ** pPayload is a pointer to data stored on database page pDbPage. |
| 3086 | ** If argument eOp is false, then nByte bytes of data are copied |
| 3087 | ** from pPayload to the buffer pointed at by pBuf. If eOp is true, |
| 3088 | ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes |
| 3089 | ** of data are copied from the buffer pBuf to pPayload. |
| 3090 | ** |
| 3091 | ** SQLITE_OK is returned on success, otherwise an error code. |
| 3092 | */ |
| 3093 | static int copyPayload( |
| 3094 | void *pPayload, /* Pointer to page data */ |
| 3095 | void *pBuf, /* Pointer to buffer */ |
| 3096 | int nByte, /* Number of bytes to copy */ |
| 3097 | int eOp, /* 0 -> copy from page, 1 -> copy to page */ |
| 3098 | DbPage *pDbPage /* Page containing pPayload */ |
| 3099 | ){ |
| 3100 | if( eOp ){ |
| 3101 | /* Copy data from buffer to page (a write operation) */ |
| 3102 | int rc = sqlite3PagerWrite(pDbPage); |
| 3103 | if( rc!=SQLITE_OK ){ |
| 3104 | return rc; |
| 3105 | } |
| 3106 | memcpy(pPayload, pBuf, nByte); |
| 3107 | }else{ |
| 3108 | /* Copy data from page to buffer (a read operation) */ |
| 3109 | memcpy(pBuf, pPayload, nByte); |
| 3110 | } |
| 3111 | return SQLITE_OK; |
| 3112 | } |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3113 | |
| 3114 | /* |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3115 | ** This function is used to read or overwrite payload information |
| 3116 | ** for the entry that the pCur cursor is pointing to. If the eOp |
| 3117 | ** parameter is 0, this is a read operation (data copied into |
| 3118 | ** buffer pBuf). If it is non-zero, a write (data copied from |
| 3119 | ** buffer pBuf). |
| 3120 | ** |
| 3121 | ** A total of "amt" bytes are read or written beginning at "offset". |
| 3122 | ** Data is read to or from the buffer pBuf. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3123 | ** |
| 3124 | ** This routine does not make a distinction between key and data. |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3125 | ** It just reads or writes bytes from the payload area. Data might |
| 3126 | ** appear on the main page or be scattered out on multiple overflow |
| 3127 | ** pages. |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3128 | ** |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 3129 | ** If the BtCursor.isIncrblobHandle flag is set, and the current |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3130 | ** cursor entry uses one or more overflow pages, this function |
| 3131 | ** allocates space for and lazily popluates the overflow page-list |
| 3132 | ** cache array (BtCursor.aOverflow). Subsequent calls use this |
| 3133 | ** cache to make seeking to the supplied offset more efficient. |
| 3134 | ** |
| 3135 | ** Once an overflow page-list cache has been allocated, it may be |
| 3136 | ** invalidated if some other cursor writes to the same table, or if |
| 3137 | ** the cursor is moved to a different row. Additionally, in auto-vacuum |
| 3138 | ** mode, the following events may invalidate an overflow page-list cache. |
| 3139 | ** |
| 3140 | ** * An incremental vacuum, |
| 3141 | ** * A commit in auto_vacuum="full" mode, |
| 3142 | ** * Creating a table (may require moving an overflow page). |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3143 | */ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3144 | static int accessPayload( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3145 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
| 3146 | int offset, /* Begin reading this far into payload */ |
| 3147 | int amt, /* Read this many bytes */ |
| 3148 | unsigned char *pBuf, /* Write the bytes into this buffer */ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3149 | int skipKey, /* offset begins at data if this is true */ |
| 3150 | int eOp /* zero to read. non-zero to write. */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3151 | ){ |
| 3152 | unsigned char *aPayload; |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3153 | int rc = SQLITE_OK; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3154 | u32 nKey; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3155 | int iIdx = 0; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 3156 | MemPage *pPage = pCur->pPage; /* Btree page of current cursor entry */ |
drh | 51f015e | 2007-10-16 19:45:29 +0000 | [diff] [blame] | 3157 | BtShared *pBt; /* Btree this cursor belongs to */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3158 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3159 | assert( pPage ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3160 | assert( pCur->eState==CURSOR_VALID ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3161 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3162 | assert( offset>=0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3163 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3164 | |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3165 | getCellInfo(pCur); |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3166 | aPayload = pCur->info.pCell + pCur->info.nHeader; |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3167 | nKey = (pPage->intKey ? 0 : pCur->info.nKey); |
| 3168 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3169 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3170 | offset += nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3171 | } |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3172 | if( offset+amt > nKey+pCur->info.nData ){ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3173 | /* Trying to read or write past the end of the data is an error */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3174 | return SQLITE_ERROR; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3175 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3176 | |
| 3177 | /* Check if data must be read/written to/from the btree page itself. */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3178 | if( offset<pCur->info.nLocal ){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3179 | int a = amt; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3180 | if( a+offset>pCur->info.nLocal ){ |
| 3181 | a = pCur->info.nLocal - offset; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3182 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3183 | rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3184 | offset = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3185 | pBuf += a; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3186 | amt -= a; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 3187 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3188 | offset -= pCur->info.nLocal; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3189 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3190 | |
drh | 51f015e | 2007-10-16 19:45:29 +0000 | [diff] [blame] | 3191 | pBt = pCur->pBt; |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3192 | if( rc==SQLITE_OK && amt>0 ){ |
| 3193 | const int ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */ |
| 3194 | Pgno nextPage; |
| 3195 | |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3196 | nextPage = get4byte(&aPayload[pCur->info.nLocal]); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3197 | |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3198 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 3199 | /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[] |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3200 | ** has not been allocated, allocate it now. The array is sized at |
| 3201 | ** one entry for each overflow page in the overflow chain. The |
| 3202 | ** page number of the first overflow page is stored in aOverflow[0], |
| 3203 | ** etc. A value of 0 in the aOverflow[] array means "not yet known" |
| 3204 | ** (the cache is lazily populated). |
| 3205 | */ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 3206 | if( pCur->isIncrblobHandle && !pCur->aOverflow ){ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3207 | int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3208 | pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl); |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3209 | if( nOvfl && !pCur->aOverflow ){ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3210 | rc = SQLITE_NOMEM; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3211 | } |
| 3212 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3213 | |
| 3214 | /* If the overflow page-list cache has been allocated and the |
| 3215 | ** entry for the first required overflow page is valid, skip |
| 3216 | ** directly to it. |
| 3217 | */ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3218 | if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){ |
| 3219 | iIdx = (offset/ovflSize); |
| 3220 | nextPage = pCur->aOverflow[iIdx]; |
| 3221 | offset = (offset%ovflSize); |
| 3222 | } |
| 3223 | #endif |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3224 | |
| 3225 | for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){ |
| 3226 | |
| 3227 | #ifndef SQLITE_OMIT_INCRBLOB |
| 3228 | /* If required, populate the overflow page-list cache. */ |
| 3229 | if( pCur->aOverflow ){ |
| 3230 | assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage); |
| 3231 | pCur->aOverflow[iIdx] = nextPage; |
| 3232 | } |
| 3233 | #endif |
| 3234 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3235 | if( offset>=ovflSize ){ |
| 3236 | /* The only reason to read this page is to obtain the page |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3237 | ** number for the next page in the overflow chain. The page |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 3238 | ** data is not required. So first try to lookup the overflow |
| 3239 | ** page-list cache, if any, then fall back to the getOverflowPage() |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3240 | ** function. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3241 | */ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3242 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3243 | if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){ |
| 3244 | nextPage = pCur->aOverflow[iIdx+1]; |
| 3245 | } else |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3246 | #endif |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3247 | rc = getOverflowPage(pBt, nextPage, 0, &nextPage); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3248 | offset -= ovflSize; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3249 | }else{ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3250 | /* Need to read this page properly. It contains some of the |
| 3251 | ** range of data that is being read (eOp==0) or written (eOp!=0). |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3252 | */ |
| 3253 | DbPage *pDbPage; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3254 | int a = amt; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3255 | rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3256 | if( rc==SQLITE_OK ){ |
| 3257 | aPayload = sqlite3PagerGetData(pDbPage); |
| 3258 | nextPage = get4byte(aPayload); |
| 3259 | if( a + offset > ovflSize ){ |
| 3260 | a = ovflSize - offset; |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3261 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3262 | rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage); |
| 3263 | sqlite3PagerUnref(pDbPage); |
| 3264 | offset = 0; |
| 3265 | amt -= a; |
| 3266 | pBuf += a; |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3267 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3268 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3269 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3270 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3271 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3272 | if( rc==SQLITE_OK && amt>0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3273 | return SQLITE_CORRUPT_BKPT; |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 3274 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3275 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3276 | } |
| 3277 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3278 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3279 | ** Read part of the key associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3280 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3281 | ** begins at "offset". |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3282 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3283 | ** Return SQLITE_OK on success or an error code if anything goes |
| 3284 | ** wrong. An error is returned if "offset+amt" is larger than |
| 3285 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3286 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3287 | int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3288 | int rc; |
| 3289 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3290 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 3291 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3292 | if( rc==SQLITE_OK ){ |
| 3293 | assert( pCur->eState==CURSOR_VALID ); |
| 3294 | assert( pCur->pPage!=0 ); |
| 3295 | if( pCur->pPage->intKey ){ |
| 3296 | return SQLITE_CORRUPT_BKPT; |
| 3297 | } |
| 3298 | assert( pCur->pPage->intKey==0 ); |
| 3299 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3300 | rc = accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0, 0); |
drh | 6575a22 | 2005-03-10 17:06:34 +0000 | [diff] [blame] | 3301 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3302 | return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3303 | } |
| 3304 | |
| 3305 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3306 | ** Read part of the data 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". |
| 3309 | ** |
| 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 | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3314 | int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3315 | int rc; |
| 3316 | |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 3317 | #ifndef SQLITE_OMIT_INCRBLOB |
| 3318 | if ( pCur->eState==CURSOR_INVALID ){ |
| 3319 | return SQLITE_ABORT; |
| 3320 | } |
| 3321 | #endif |
| 3322 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3323 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 3324 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3325 | if( rc==SQLITE_OK ){ |
| 3326 | assert( pCur->eState==CURSOR_VALID ); |
| 3327 | assert( pCur->pPage!=0 ); |
| 3328 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3329 | rc = accessPayload(pCur, offset, amt, pBuf, 1, 0); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3330 | } |
| 3331 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3332 | } |
| 3333 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3334 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3335 | ** Return a pointer to payload information from the entry that the |
| 3336 | ** pCur cursor is pointing to. The pointer is to the beginning of |
| 3337 | ** 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] | 3338 | ** skipKey==1. The number of bytes of available key/data is written |
| 3339 | ** into *pAmt. If *pAmt==0, then the value returned will not be |
| 3340 | ** a valid pointer. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3341 | ** |
| 3342 | ** This routine is an optimization. It is common for the entire key |
| 3343 | ** and data to fit on the local page and for there to be no overflow |
| 3344 | ** pages. When that is so, this routine can be used to access the |
| 3345 | ** 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] | 3346 | ** onto overflow pages, then accessPayload() must be used to reassembly |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3347 | ** the key/data and copy it into a preallocated buffer. |
| 3348 | ** |
| 3349 | ** The pointer returned by this routine looks directly into the cached |
| 3350 | ** page of the database. The data might change or move the next time |
| 3351 | ** any btree routine is called. |
| 3352 | */ |
| 3353 | static const unsigned char *fetchPayload( |
| 3354 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3355 | int *pAmt, /* Write the number of available bytes here */ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3356 | int skipKey /* read beginning at data if this is true */ |
| 3357 | ){ |
| 3358 | unsigned char *aPayload; |
| 3359 | MemPage *pPage; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3360 | u32 nKey; |
| 3361 | int nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3362 | |
| 3363 | assert( pCur!=0 && pCur->pPage!=0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3364 | assert( pCur->eState==CURSOR_VALID ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3365 | assert( cursorHoldsMutex(pCur) ); |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3366 | pPage = pCur->pPage; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3367 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3368 | getCellInfo(pCur); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3369 | aPayload = pCur->info.pCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3370 | aPayload += pCur->info.nHeader; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3371 | if( pPage->intKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3372 | nKey = 0; |
| 3373 | }else{ |
| 3374 | nKey = pCur->info.nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3375 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3376 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3377 | aPayload += nKey; |
| 3378 | nLocal = pCur->info.nLocal - nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3379 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3380 | nLocal = pCur->info.nLocal; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3381 | if( nLocal>nKey ){ |
| 3382 | nLocal = nKey; |
| 3383 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3384 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3385 | *pAmt = nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3386 | return aPayload; |
| 3387 | } |
| 3388 | |
| 3389 | |
| 3390 | /* |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3391 | ** For the entry that cursor pCur is point to, return as |
| 3392 | ** many bytes of the key or data as are available on the local |
| 3393 | ** b-tree page. Write the number of available bytes into *pAmt. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3394 | ** |
| 3395 | ** The pointer returned is ephemeral. The key/data may move |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3396 | ** or be destroyed on the next call to any Btree routine, |
| 3397 | ** including calls from other threads against the same cache. |
| 3398 | ** Hence, a mutex on the BtShared should be held prior to calling |
| 3399 | ** this routine. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3400 | ** |
| 3401 | ** These routines is used to get quick access to key and data |
| 3402 | ** in the common case where no overflow pages are used. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3403 | */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3404 | const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3405 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3406 | if( pCur->eState==CURSOR_VALID ){ |
| 3407 | return (const void*)fetchPayload(pCur, pAmt, 0); |
| 3408 | } |
| 3409 | return 0; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3410 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3411 | const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3412 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3413 | if( pCur->eState==CURSOR_VALID ){ |
| 3414 | return (const void*)fetchPayload(pCur, pAmt, 1); |
| 3415 | } |
| 3416 | return 0; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3417 | } |
| 3418 | |
| 3419 | |
| 3420 | /* |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3421 | ** 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] | 3422 | ** page number of the child page to move to. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3423 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3424 | static int moveToChild(BtCursor *pCur, u32 newPgno){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3425 | int rc; |
| 3426 | MemPage *pNewPage; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3427 | MemPage *pOldPage; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 3428 | BtShared *pBt = pCur->pBt; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3429 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3430 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3431 | assert( pCur->eState==CURSOR_VALID ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 3432 | rc = getAndInitPage(pBt, newPgno, &pNewPage, pCur->pPage); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3433 | if( rc ) return rc; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3434 | pNewPage->idxParent = pCur->idx; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3435 | pOldPage = pCur->pPage; |
| 3436 | pOldPage->idxShift = 0; |
| 3437 | releasePage(pOldPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3438 | pCur->pPage = pNewPage; |
| 3439 | pCur->idx = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3440 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3441 | pCur->validNKey = 0; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 3442 | if( pNewPage->nCell<1 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3443 | return SQLITE_CORRUPT_BKPT; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 3444 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3445 | return SQLITE_OK; |
| 3446 | } |
| 3447 | |
| 3448 | /* |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3449 | ** Return true if the page is the virtual root of its table. |
| 3450 | ** |
| 3451 | ** The virtual root page is the root page for most tables. But |
| 3452 | ** for the table rooted on page 1, sometime the real root page |
| 3453 | ** is empty except for the right-pointer. In such cases the |
| 3454 | ** virtual root page is the page that the right-pointer of page |
| 3455 | ** 1 is pointing to. |
| 3456 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3457 | int sqlite3BtreeIsRootPage(MemPage *pPage){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3458 | MemPage *pParent; |
| 3459 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3460 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3461 | pParent = pPage->pParent; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3462 | if( pParent==0 ) return 1; |
| 3463 | if( pParent->pgno>1 ) return 0; |
| 3464 | if( get2byte(&pParent->aData[pParent->hdrOffset+3])==0 ) return 1; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3465 | return 0; |
| 3466 | } |
| 3467 | |
| 3468 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3469 | ** Move the cursor up to the parent page. |
| 3470 | ** |
| 3471 | ** pCur->idx is set to the cell index that contains the pointer |
| 3472 | ** to the page we are coming from. If we are coming from the |
| 3473 | ** right-most child page then pCur->idx is set to one more than |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3474 | ** the largest cell index. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3475 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3476 | void sqlite3BtreeMoveToParent(BtCursor *pCur){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3477 | MemPage *pParent; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3478 | MemPage *pPage; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3479 | int idxParent; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3480 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3481 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3482 | assert( pCur->eState==CURSOR_VALID ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3483 | pPage = pCur->pPage; |
| 3484 | assert( pPage!=0 ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3485 | assert( !sqlite3BtreeIsRootPage(pPage) ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3486 | pParent = pPage->pParent; |
| 3487 | assert( pParent!=0 ); |
danielk1977 | 8c0a791 | 2008-08-20 14:49:23 +0000 | [diff] [blame] | 3488 | assert( pPage->pDbPage->nRef>0 ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3489 | idxParent = pPage->idxParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3490 | sqlite3PagerRef(pParent->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3491 | releasePage(pPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3492 | pCur->pPage = pParent; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3493 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3494 | pCur->validNKey = 0; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3495 | assert( pParent->idxShift==0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3496 | pCur->idx = idxParent; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3497 | } |
| 3498 | |
| 3499 | /* |
| 3500 | ** Move the cursor to the root page |
| 3501 | */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3502 | static int moveToRoot(BtCursor *pCur){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3503 | MemPage *pRoot; |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3504 | int rc = SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3505 | Btree *p = pCur->pBtree; |
| 3506 | BtShared *pBt = p->pBt; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3507 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3508 | assert( cursorHoldsMutex(pCur) ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 3509 | assert( CURSOR_INVALID < CURSOR_REQUIRESEEK ); |
| 3510 | assert( CURSOR_VALID < CURSOR_REQUIRESEEK ); |
| 3511 | assert( CURSOR_FAULT > CURSOR_REQUIRESEEK ); |
| 3512 | if( pCur->eState>=CURSOR_REQUIRESEEK ){ |
| 3513 | if( pCur->eState==CURSOR_FAULT ){ |
| 3514 | return pCur->skip; |
| 3515 | } |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 3516 | clearCursorPosition(pCur); |
| 3517 | } |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3518 | pRoot = pCur->pPage; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 3519 | if( pRoot && pRoot->pgno==pCur->pgnoRoot ){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3520 | assert( pRoot->isInit ); |
| 3521 | }else{ |
| 3522 | if( |
| 3523 | SQLITE_OK!=(rc = getAndInitPage(pBt, pCur->pgnoRoot, &pRoot, 0)) |
| 3524 | ){ |
| 3525 | pCur->eState = CURSOR_INVALID; |
| 3526 | return rc; |
| 3527 | } |
| 3528 | releasePage(pCur->pPage); |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3529 | pCur->pPage = pRoot; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3530 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3531 | pCur->idx = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3532 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3533 | pCur->atLast = 0; |
| 3534 | pCur->validNKey = 0; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3535 | if( pRoot->nCell==0 && !pRoot->leaf ){ |
| 3536 | Pgno subpage; |
| 3537 | assert( pRoot->pgno==1 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3538 | subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3539 | assert( subpage>0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3540 | pCur->eState = CURSOR_VALID; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3541 | rc = moveToChild(pCur, subpage); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3542 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3543 | pCur->eState = ((pCur->pPage->nCell>0)?CURSOR_VALID:CURSOR_INVALID); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3544 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3545 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3546 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3547 | /* |
| 3548 | ** Move the cursor down to the left-most leaf entry beneath the |
| 3549 | ** entry to which it is currently pointing. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3550 | ** |
| 3551 | ** The left-most leaf is the one with the smallest key - the first |
| 3552 | ** in ascending order. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3553 | */ |
| 3554 | static int moveToLeftmost(BtCursor *pCur){ |
| 3555 | Pgno pgno; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3556 | int rc = SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3557 | MemPage *pPage; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3558 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3559 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3560 | assert( pCur->eState==CURSOR_VALID ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3561 | while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3562 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3563 | pgno = get4byte(findCell(pPage, pCur->idx)); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3564 | rc = moveToChild(pCur, pgno); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3565 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3566 | return rc; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3567 | } |
| 3568 | |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3569 | /* |
| 3570 | ** Move the cursor down to the right-most leaf entry beneath the |
| 3571 | ** page to which it is currently pointing. Notice the difference |
| 3572 | ** between moveToLeftmost() and moveToRightmost(). moveToLeftmost() |
| 3573 | ** finds the left-most entry beneath the *entry* whereas moveToRightmost() |
| 3574 | ** finds the right-most entry beneath the *page*. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3575 | ** |
| 3576 | ** The right-most entry is the one with the largest key - the last |
| 3577 | ** key in ascending order. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3578 | */ |
| 3579 | static int moveToRightmost(BtCursor *pCur){ |
| 3580 | Pgno pgno; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3581 | int rc = SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3582 | MemPage *pPage; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3583 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3584 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3585 | assert( pCur->eState==CURSOR_VALID ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3586 | while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3587 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3588 | pCur->idx = pPage->nCell; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3589 | rc = moveToChild(pCur, pgno); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3590 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3591 | if( rc==SQLITE_OK ){ |
| 3592 | pCur->idx = pPage->nCell - 1; |
| 3593 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3594 | pCur->validNKey = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3595 | } |
danielk1977 | 518002e | 2008-09-05 05:02:46 +0000 | [diff] [blame^] | 3596 | return rc; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3597 | } |
| 3598 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3599 | /* Move the cursor to the first entry in the table. Return SQLITE_OK |
| 3600 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 3601 | ** or set *pRes to 1 if the table is empty. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3602 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3603 | int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3604 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3605 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3606 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3607 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3608 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3609 | if( rc==SQLITE_OK ){ |
| 3610 | if( pCur->eState==CURSOR_INVALID ){ |
| 3611 | assert( pCur->pPage->nCell==0 ); |
| 3612 | *pRes = 1; |
| 3613 | rc = SQLITE_OK; |
| 3614 | }else{ |
| 3615 | assert( pCur->pPage->nCell>0 ); |
| 3616 | *pRes = 0; |
| 3617 | rc = moveToLeftmost(pCur); |
| 3618 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3619 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3620 | return rc; |
| 3621 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3622 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3623 | /* Move the cursor to the last entry in the table. Return SQLITE_OK |
| 3624 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 3625 | ** or set *pRes to 1 if the table is empty. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3626 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3627 | int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3628 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3629 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3630 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3631 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3632 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3633 | if( rc==SQLITE_OK ){ |
| 3634 | if( CURSOR_INVALID==pCur->eState ){ |
| 3635 | assert( pCur->pPage->nCell==0 ); |
| 3636 | *pRes = 1; |
| 3637 | }else{ |
| 3638 | assert( pCur->eState==CURSOR_VALID ); |
| 3639 | *pRes = 0; |
| 3640 | rc = moveToRightmost(pCur); |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3641 | getCellInfo(pCur); |
| 3642 | pCur->atLast = rc==SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3643 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3644 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3645 | return rc; |
| 3646 | } |
| 3647 | |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 3648 | /* Move the cursor so that it points to an entry near the key |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3649 | ** specified by pIdxKey or intKey. Return a success code. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3650 | ** |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3651 | ** For INTKEY tables, the intKey parameter is used. pIdxKey |
| 3652 | ** must be NULL. For index tables, pIdxKey is used and intKey |
| 3653 | ** is ignored. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3654 | ** |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3655 | ** If an exact match is not found, then the cursor is always |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3656 | ** left pointing at a leaf page which would hold the entry if it |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3657 | ** were present. The cursor might point to an entry that comes |
| 3658 | ** before or after the key. |
| 3659 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3660 | ** The result of comparing the key with the entry to which the |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 3661 | ** cursor is written to *pRes if pRes!=NULL. The meaning of |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3662 | ** this value is as follows: |
| 3663 | ** |
| 3664 | ** *pRes<0 The cursor is left pointing at an entry that |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3665 | ** is smaller than pKey or if the table is empty |
| 3666 | ** and the cursor is therefore left point to nothing. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3667 | ** |
| 3668 | ** *pRes==0 The cursor is left pointing at an entry that |
| 3669 | ** exactly matches pKey. |
| 3670 | ** |
| 3671 | ** *pRes>0 The cursor is left pointing at an entry that |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 3672 | ** is larger than pKey. |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3673 | ** |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3674 | */ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3675 | int sqlite3BtreeMovetoUnpacked( |
| 3676 | BtCursor *pCur, /* The cursor to be moved */ |
| 3677 | UnpackedRecord *pIdxKey, /* Unpacked index key */ |
| 3678 | i64 intKey, /* The table key */ |
| 3679 | int biasRight, /* If true, bias the search to the high end */ |
| 3680 | int *pRes /* Write search results here */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3681 | ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3682 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3683 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3684 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3685 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3686 | |
| 3687 | /* If the cursor is already positioned at the point we are trying |
| 3688 | ** to move to, then just return without doing any work */ |
| 3689 | if( pCur->eState==CURSOR_VALID && pCur->validNKey && pCur->pPage->intKey ){ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3690 | if( pCur->info.nKey==intKey ){ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3691 | *pRes = 0; |
| 3692 | return SQLITE_OK; |
| 3693 | } |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3694 | if( pCur->atLast && pCur->info.nKey<intKey ){ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3695 | *pRes = -1; |
| 3696 | return SQLITE_OK; |
| 3697 | } |
| 3698 | } |
| 3699 | |
| 3700 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3701 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3702 | if( rc ){ |
| 3703 | return rc; |
| 3704 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3705 | assert( pCur->pPage ); |
| 3706 | assert( pCur->pPage->isInit ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3707 | if( pCur->eState==CURSOR_INVALID ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3708 | *pRes = -1; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3709 | assert( pCur->pPage->nCell==0 ); |
| 3710 | return SQLITE_OK; |
| 3711 | } |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3712 | assert( pCur->pPage->intKey || pIdxKey ); |
drh | 1468438 | 2006-11-30 13:05:29 +0000 | [diff] [blame] | 3713 | for(;;){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3714 | int lwr, upr; |
| 3715 | Pgno chldPg; |
| 3716 | MemPage *pPage = pCur->pPage; |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3717 | int c = -1; /* pRes return if table is empty must be -1 */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3718 | lwr = 0; |
| 3719 | upr = pPage->nCell-1; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3720 | if( !pPage->intKey && pIdxKey==0 ){ |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3721 | rc = SQLITE_CORRUPT_BKPT; |
| 3722 | goto moveto_finish; |
drh | 4eec4c1 | 2005-01-21 00:22:37 +0000 | [diff] [blame] | 3723 | } |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3724 | if( biasRight ){ |
| 3725 | pCur->idx = upr; |
| 3726 | }else{ |
| 3727 | pCur->idx = (upr+lwr)/2; |
| 3728 | } |
drh | f1d68b3 | 2007-03-29 04:43:26 +0000 | [diff] [blame] | 3729 | if( lwr<=upr ) for(;;){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3730 | void *pCellKey; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3731 | i64 nCellKey; |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3732 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3733 | pCur->validNKey = 1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3734 | if( pPage->intKey ){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3735 | u8 *pCell; |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3736 | pCell = findCell(pPage, pCur->idx) + pPage->childPtrSize; |
drh | d172f86 | 2006-01-12 15:01:15 +0000 | [diff] [blame] | 3737 | if( pPage->hasData ){ |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 3738 | u32 dummy; |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 3739 | pCell += getVarint32(pCell, dummy); |
drh | d172f86 | 2006-01-12 15:01:15 +0000 | [diff] [blame] | 3740 | } |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3741 | getVarint(pCell, (u64*)&nCellKey); |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3742 | if( nCellKey==intKey ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3743 | c = 0; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3744 | }else if( nCellKey<intKey ){ |
drh | 41eb9e9 | 2008-04-02 18:33:07 +0000 | [diff] [blame] | 3745 | c = -1; |
| 3746 | }else{ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3747 | assert( nCellKey>intKey ); |
drh | 41eb9e9 | 2008-04-02 18:33:07 +0000 | [diff] [blame] | 3748 | c = +1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3749 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3750 | }else{ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3751 | int available; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3752 | pCellKey = (void *)fetchPayload(pCur, &available, 0); |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3753 | nCellKey = pCur->info.nKey; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3754 | if( available>=nCellKey ){ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3755 | c = sqlite3VdbeRecordCompare(nCellKey, pCellKey, pIdxKey); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3756 | }else{ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 3757 | pCellKey = sqlite3Malloc( nCellKey ); |
danielk1977 | 6507ecb | 2008-03-25 09:56:44 +0000 | [diff] [blame] | 3758 | if( pCellKey==0 ){ |
| 3759 | rc = SQLITE_NOMEM; |
| 3760 | goto moveto_finish; |
| 3761 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3762 | rc = sqlite3BtreeKey(pCur, 0, nCellKey, (void *)pCellKey); |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3763 | c = sqlite3VdbeRecordCompare(nCellKey, pCellKey, pIdxKey); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 3764 | sqlite3_free(pCellKey); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3765 | if( rc ) goto moveto_finish; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3766 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3767 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3768 | if( c==0 ){ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3769 | pCur->info.nKey = nCellKey; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 3770 | if( pPage->intKey && !pPage->leaf ){ |
drh | fc70e6f | 2004-05-12 21:11:27 +0000 | [diff] [blame] | 3771 | lwr = pCur->idx; |
| 3772 | upr = lwr - 1; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3773 | break; |
| 3774 | }else{ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3775 | if( pRes ) *pRes = 0; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3776 | rc = SQLITE_OK; |
| 3777 | goto moveto_finish; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3778 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3779 | } |
| 3780 | if( c<0 ){ |
| 3781 | lwr = pCur->idx+1; |
| 3782 | }else{ |
| 3783 | upr = pCur->idx-1; |
| 3784 | } |
drh | f1d68b3 | 2007-03-29 04:43:26 +0000 | [diff] [blame] | 3785 | if( lwr>upr ){ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3786 | pCur->info.nKey = nCellKey; |
drh | f1d68b3 | 2007-03-29 04:43:26 +0000 | [diff] [blame] | 3787 | break; |
| 3788 | } |
| 3789 | pCur->idx = (lwr+upr)/2; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3790 | } |
| 3791 | assert( lwr==upr+1 ); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 3792 | assert( pPage->isInit ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3793 | if( pPage->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3794 | chldPg = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3795 | }else if( lwr>=pPage->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3796 | chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3797 | }else{ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3798 | chldPg = get4byte(findCell(pPage, lwr)); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3799 | } |
| 3800 | if( chldPg==0 ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3801 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3802 | if( pRes ) *pRes = c; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3803 | rc = SQLITE_OK; |
| 3804 | goto moveto_finish; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3805 | } |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3806 | pCur->idx = lwr; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3807 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3808 | pCur->validNKey = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3809 | rc = moveToChild(pCur, chldPg); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3810 | if( rc ) goto moveto_finish; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3811 | } |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3812 | moveto_finish: |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3813 | return rc; |
| 3814 | } |
| 3815 | |
| 3816 | /* |
| 3817 | ** In this version of BtreeMoveto, pKey is a packed index record |
| 3818 | ** such as is generated by the OP_MakeRecord opcode. Unpack the |
| 3819 | ** record and then call BtreeMovetoUnpacked() to do the work. |
| 3820 | */ |
| 3821 | int sqlite3BtreeMoveto( |
| 3822 | BtCursor *pCur, /* Cursor open on the btree to be searched */ |
| 3823 | const void *pKey, /* Packed key if the btree is an index */ |
| 3824 | i64 nKey, /* Integer key for tables. Size of pKey for indices */ |
| 3825 | int bias, /* Bias search to the high end */ |
| 3826 | int *pRes /* Write search results here */ |
| 3827 | ){ |
| 3828 | int rc; /* Status code */ |
| 3829 | UnpackedRecord *pIdxKey; /* Unpacked index key */ |
drh | 23f79d0 | 2008-08-20 22:06:47 +0000 | [diff] [blame] | 3830 | UnpackedRecord aSpace[16]; /* Temp space for pIdxKey - to avoid a malloc */ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3831 | |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 3832 | if( pKey ){ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3833 | pIdxKey = sqlite3VdbeRecordUnpack(pCur->pKeyInfo, nKey, pKey, |
drh | 23f79d0 | 2008-08-20 22:06:47 +0000 | [diff] [blame] | 3834 | aSpace, sizeof(aSpace)); |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3835 | if( pIdxKey==0 ) return SQLITE_NOMEM; |
| 3836 | }else{ |
| 3837 | pIdxKey = 0; |
| 3838 | } |
| 3839 | rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes); |
| 3840 | if( pKey ){ |
| 3841 | sqlite3VdbeDeleteUnpackedRecord(pIdxKey); |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 3842 | } |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3843 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3844 | } |
| 3845 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3846 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3847 | /* |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3848 | ** Return TRUE if the cursor is not pointing at an entry of the table. |
| 3849 | ** |
| 3850 | ** TRUE will be returned after a call to sqlite3BtreeNext() moves |
| 3851 | ** past the last entry in the table or sqlite3BtreePrev() moves past |
| 3852 | ** the first entry. TRUE is also returned if the table is empty. |
| 3853 | */ |
| 3854 | int sqlite3BtreeEof(BtCursor *pCur){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3855 | /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries |
| 3856 | ** have been deleted? This API will need to change to return an error code |
| 3857 | ** as well as the boolean result value. |
| 3858 | */ |
| 3859 | return (CURSOR_VALID!=pCur->eState); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3860 | } |
| 3861 | |
| 3862 | /* |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 3863 | ** Return the database connection handle for a cursor. |
| 3864 | */ |
| 3865 | sqlite3 *sqlite3BtreeCursorDb(const BtCursor *pCur){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3866 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
| 3867 | return pCur->pBtree->db; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 3868 | } |
| 3869 | |
| 3870 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3871 | ** Advance the cursor to the next entry in the database. If |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3872 | ** successful then set *pRes=0. If the cursor |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3873 | ** was already pointing to the last entry in the database before |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3874 | ** this routine was called, then set *pRes=1. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3875 | */ |
drh | d094db1 | 2008-04-03 21:46:57 +0000 | [diff] [blame] | 3876 | int sqlite3BtreeNext(BtCursor *pCur, int *pRes){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3877 | int rc; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 3878 | MemPage *pPage; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3879 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3880 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 3881 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3882 | if( rc!=SQLITE_OK ){ |
| 3883 | return rc; |
| 3884 | } |
drh | 8c4d3a6 | 2007-04-06 01:03:32 +0000 | [diff] [blame] | 3885 | assert( pRes!=0 ); |
| 3886 | pPage = pCur->pPage; |
| 3887 | if( CURSOR_INVALID==pCur->eState ){ |
| 3888 | *pRes = 1; |
| 3889 | return SQLITE_OK; |
| 3890 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3891 | if( pCur->skip>0 ){ |
| 3892 | pCur->skip = 0; |
| 3893 | *pRes = 0; |
| 3894 | return SQLITE_OK; |
| 3895 | } |
| 3896 | pCur->skip = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3897 | |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3898 | assert( pPage->isInit ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3899 | assert( pCur->idx<pPage->nCell ); |
danielk1977 | 6a43f9b | 2004-11-16 04:57:24 +0000 | [diff] [blame] | 3900 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3901 | pCur->idx++; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3902 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3903 | pCur->validNKey = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3904 | if( pCur->idx>=pPage->nCell ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3905 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3906 | rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3907 | if( rc ) return rc; |
| 3908 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3909 | *pRes = 0; |
| 3910 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3911 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3912 | do{ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3913 | if( sqlite3BtreeIsRootPage(pPage) ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3914 | *pRes = 1; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3915 | pCur->eState = CURSOR_INVALID; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3916 | return SQLITE_OK; |
| 3917 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3918 | sqlite3BtreeMoveToParent(pCur); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3919 | pPage = pCur->pPage; |
| 3920 | }while( pCur->idx>=pPage->nCell ); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3921 | *pRes = 0; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 3922 | if( pPage->intKey ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3923 | rc = sqlite3BtreeNext(pCur, pRes); |
| 3924 | }else{ |
| 3925 | rc = SQLITE_OK; |
| 3926 | } |
| 3927 | return rc; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3928 | } |
| 3929 | *pRes = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3930 | if( pPage->leaf ){ |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3931 | return SQLITE_OK; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3932 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3933 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3934 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3935 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3936 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3937 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3938 | /* |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3939 | ** 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] | 3940 | ** successful then set *pRes=0. If the cursor |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3941 | ** was already pointing to the first entry in the database before |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3942 | ** this routine was called, then set *pRes=1. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3943 | */ |
drh | d094db1 | 2008-04-03 21:46:57 +0000 | [diff] [blame] | 3944 | int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){ |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3945 | int rc; |
| 3946 | Pgno pgno; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3947 | MemPage *pPage; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3948 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3949 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 3950 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3951 | if( rc!=SQLITE_OK ){ |
| 3952 | return rc; |
| 3953 | } |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3954 | pCur->atLast = 0; |
drh | 8c4d3a6 | 2007-04-06 01:03:32 +0000 | [diff] [blame] | 3955 | if( CURSOR_INVALID==pCur->eState ){ |
| 3956 | *pRes = 1; |
| 3957 | return SQLITE_OK; |
| 3958 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3959 | if( pCur->skip<0 ){ |
| 3960 | pCur->skip = 0; |
| 3961 | *pRes = 0; |
| 3962 | return SQLITE_OK; |
| 3963 | } |
| 3964 | pCur->skip = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3965 | |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3966 | pPage = pCur->pPage; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3967 | assert( pPage->isInit ); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3968 | assert( pCur->idx>=0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3969 | if( !pPage->leaf ){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3970 | pgno = get4byte( findCell(pPage, pCur->idx) ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3971 | rc = moveToChild(pCur, pgno); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3972 | if( rc ){ |
| 3973 | return rc; |
| 3974 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3975 | rc = moveToRightmost(pCur); |
| 3976 | }else{ |
| 3977 | while( pCur->idx==0 ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3978 | if( sqlite3BtreeIsRootPage(pPage) ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3979 | pCur->eState = CURSOR_INVALID; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3980 | *pRes = 1; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3981 | return SQLITE_OK; |
| 3982 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3983 | sqlite3BtreeMoveToParent(pCur); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3984 | pPage = pCur->pPage; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3985 | } |
| 3986 | pCur->idx--; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3987 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3988 | pCur->validNKey = 0; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 3989 | if( pPage->intKey && !pPage->leaf ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3990 | rc = sqlite3BtreePrevious(pCur, pRes); |
| 3991 | }else{ |
| 3992 | rc = SQLITE_OK; |
| 3993 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3994 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3995 | *pRes = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3996 | return rc; |
| 3997 | } |
| 3998 | |
| 3999 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4000 | ** Allocate a new page from the database file. |
| 4001 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4002 | ** The new page is marked as dirty. (In other words, sqlite3PagerWrite() |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4003 | ** has already been called on the new page.) The new page has also |
| 4004 | ** been referenced and the calling routine is responsible for calling |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4005 | ** sqlite3PagerUnref() on the new page when it is done. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4006 | ** |
| 4007 | ** SQLITE_OK is returned on success. Any other return value indicates |
| 4008 | ** an error. *ppPage and *pPgno are undefined in the event of an error. |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4009 | ** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned. |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 4010 | ** |
drh | 199e3cf | 2002-07-18 11:01:47 +0000 | [diff] [blame] | 4011 | ** If the "nearby" parameter is not 0, then a (feeble) effort is made to |
| 4012 | ** 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] | 4013 | ** attempt to keep related pages close to each other in the database file, |
| 4014 | ** which in turn can make database access faster. |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4015 | ** |
| 4016 | ** If the "exact" parameter is not 0, and the page-number nearby exists |
| 4017 | ** anywhere on the free-list, then it is guarenteed to be returned. This |
| 4018 | ** is only used by auto-vacuum databases when allocating a new table. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4019 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4020 | static int allocateBtreePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4021 | BtShared *pBt, |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4022 | MemPage **ppPage, |
| 4023 | Pgno *pPgno, |
| 4024 | Pgno nearby, |
| 4025 | u8 exact |
| 4026 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4027 | MemPage *pPage1; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4028 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4029 | int n; /* Number of pages on the freelist */ |
| 4030 | int k; /* Number of leaves on the trunk of the freelist */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4031 | MemPage *pTrunk = 0; |
| 4032 | MemPage *pPrevTrunk = 0; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4033 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4034 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4035 | pPage1 = pBt->pPage1; |
| 4036 | n = get4byte(&pPage1->aData[36]); |
| 4037 | if( n>0 ){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4038 | /* There are pages on the freelist. Reuse one of those pages. */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4039 | Pgno iTrunk; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4040 | u8 searchList = 0; /* If the free-list must be searched for 'nearby' */ |
| 4041 | |
| 4042 | /* If the 'exact' parameter was true and a query of the pointer-map |
| 4043 | ** shows that the page 'nearby' is somewhere on the free-list, then |
| 4044 | ** the entire-list will be searched for that page. |
| 4045 | */ |
| 4046 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 4047 | if( exact && nearby<=pagerPagecount(pBt->pPager) ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4048 | u8 eType; |
| 4049 | assert( nearby>0 ); |
| 4050 | assert( pBt->autoVacuum ); |
| 4051 | rc = ptrmapGet(pBt, nearby, &eType, 0); |
| 4052 | if( rc ) return rc; |
| 4053 | if( eType==PTRMAP_FREEPAGE ){ |
| 4054 | searchList = 1; |
| 4055 | } |
| 4056 | *pPgno = nearby; |
| 4057 | } |
| 4058 | #endif |
| 4059 | |
| 4060 | /* Decrement the free-list count by 1. Set iTrunk to the index of the |
| 4061 | ** first free-list trunk page. iPrevTrunk is initially 1. |
| 4062 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4063 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4064 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4065 | put4byte(&pPage1->aData[36], n-1); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4066 | |
| 4067 | /* The code within this loop is run only once if the 'searchList' variable |
| 4068 | ** is not true. Otherwise, it runs once for each trunk-page on the |
| 4069 | ** free-list until the page 'nearby' is located. |
| 4070 | */ |
| 4071 | do { |
| 4072 | pPrevTrunk = pTrunk; |
| 4073 | if( pPrevTrunk ){ |
| 4074 | iTrunk = get4byte(&pPrevTrunk->aData[0]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 4075 | }else{ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4076 | iTrunk = get4byte(&pPage1->aData[32]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 4077 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4078 | rc = sqlite3BtreeGetPage(pBt, iTrunk, &pTrunk, 0); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4079 | if( rc ){ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4080 | pTrunk = 0; |
| 4081 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4082 | } |
| 4083 | |
| 4084 | k = get4byte(&pTrunk->aData[4]); |
| 4085 | if( k==0 && !searchList ){ |
| 4086 | /* The trunk has no leaves and the list is not being searched. |
| 4087 | ** So extract the trunk page itself and use it as the newly |
| 4088 | ** allocated page */ |
| 4089 | assert( pPrevTrunk==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4090 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4091 | if( rc ){ |
| 4092 | goto end_allocate_page; |
| 4093 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4094 | *pPgno = iTrunk; |
| 4095 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 4096 | *ppPage = pTrunk; |
| 4097 | pTrunk = 0; |
| 4098 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
drh | 45b1fac | 2008-07-04 17:52:42 +0000 | [diff] [blame] | 4099 | }else if( k>pBt->usableSize/4 - 2 ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4100 | /* Value of k is out of range. Database corruption */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4101 | rc = SQLITE_CORRUPT_BKPT; |
| 4102 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4103 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4104 | }else if( searchList && nearby==iTrunk ){ |
| 4105 | /* The list is being searched and this trunk page is the page |
| 4106 | ** to allocate, regardless of whether it has leaves. |
| 4107 | */ |
| 4108 | assert( *pPgno==iTrunk ); |
| 4109 | *ppPage = pTrunk; |
| 4110 | searchList = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4111 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4112 | if( rc ){ |
| 4113 | goto end_allocate_page; |
| 4114 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4115 | if( k==0 ){ |
| 4116 | if( !pPrevTrunk ){ |
| 4117 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 4118 | }else{ |
| 4119 | memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4); |
| 4120 | } |
| 4121 | }else{ |
| 4122 | /* The trunk page is required by the caller but it contains |
| 4123 | ** pointers to free-list leaves. The first leaf becomes a trunk |
| 4124 | ** page in this case. |
| 4125 | */ |
| 4126 | MemPage *pNewTrunk; |
| 4127 | Pgno iNewTrunk = get4byte(&pTrunk->aData[8]); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4128 | rc = sqlite3BtreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4129 | if( rc!=SQLITE_OK ){ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4130 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4131 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4132 | rc = sqlite3PagerWrite(pNewTrunk->pDbPage); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4133 | if( rc!=SQLITE_OK ){ |
| 4134 | releasePage(pNewTrunk); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4135 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4136 | } |
| 4137 | memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4); |
| 4138 | put4byte(&pNewTrunk->aData[4], k-1); |
| 4139 | memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4140 | releasePage(pNewTrunk); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4141 | if( !pPrevTrunk ){ |
| 4142 | put4byte(&pPage1->aData[32], iNewTrunk); |
| 4143 | }else{ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4144 | rc = sqlite3PagerWrite(pPrevTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4145 | if( rc ){ |
| 4146 | goto end_allocate_page; |
| 4147 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4148 | put4byte(&pPrevTrunk->aData[0], iNewTrunk); |
| 4149 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4150 | } |
| 4151 | pTrunk = 0; |
| 4152 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
| 4153 | #endif |
| 4154 | }else{ |
| 4155 | /* Extract a leaf from the trunk */ |
| 4156 | int closest; |
| 4157 | Pgno iPage; |
| 4158 | unsigned char *aData = pTrunk->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4159 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4160 | if( rc ){ |
| 4161 | goto end_allocate_page; |
| 4162 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4163 | if( nearby>0 ){ |
| 4164 | int i, dist; |
| 4165 | closest = 0; |
| 4166 | dist = get4byte(&aData[8]) - nearby; |
| 4167 | if( dist<0 ) dist = -dist; |
| 4168 | for(i=1; i<k; i++){ |
| 4169 | int d2 = get4byte(&aData[8+i*4]) - nearby; |
| 4170 | if( d2<0 ) d2 = -d2; |
| 4171 | if( d2<dist ){ |
| 4172 | closest = i; |
| 4173 | dist = d2; |
| 4174 | } |
| 4175 | } |
| 4176 | }else{ |
| 4177 | closest = 0; |
| 4178 | } |
| 4179 | |
| 4180 | iPage = get4byte(&aData[8+closest*4]); |
| 4181 | if( !searchList || iPage==nearby ){ |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 4182 | int nPage; |
shane | 1f9e6aa | 2008-06-09 19:27:11 +0000 | [diff] [blame] | 4183 | *pPgno = iPage; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 4184 | nPage = pagerPagecount(pBt->pPager); |
| 4185 | if( *pPgno>nPage ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4186 | /* Free page off the end of the file */ |
danielk1977 | 43e377a | 2008-05-05 12:09:32 +0000 | [diff] [blame] | 4187 | rc = SQLITE_CORRUPT_BKPT; |
| 4188 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4189 | } |
| 4190 | TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d" |
| 4191 | ": %d more free pages\n", |
| 4192 | *pPgno, closest+1, k, pTrunk->pgno, n-1)); |
| 4193 | if( closest<k-1 ){ |
| 4194 | memcpy(&aData[8+closest*4], &aData[4+k*4], 4); |
| 4195 | } |
| 4196 | put4byte(&aData[4], k-1); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4197 | rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 1); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4198 | if( rc==SQLITE_OK ){ |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 4199 | sqlite3PagerDontRollback((*ppPage)->pDbPage); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4200 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 4201 | if( rc!=SQLITE_OK ){ |
| 4202 | releasePage(*ppPage); |
| 4203 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4204 | } |
| 4205 | searchList = 0; |
| 4206 | } |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 4207 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4208 | releasePage(pPrevTrunk); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4209 | pPrevTrunk = 0; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4210 | }while( searchList ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4211 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4212 | /* There are no pages on the freelist, so create a new page at the |
| 4213 | ** end of the file */ |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 4214 | int nPage = pagerPagecount(pBt->pPager); |
| 4215 | *pPgno = nPage + 1; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4216 | |
| 4217 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 4218 | if( pBt->nTrunc ){ |
| 4219 | /* An incr-vacuum has already run within this transaction. So the |
| 4220 | ** page to allocate is not from the physical end of the file, but |
| 4221 | ** at pBt->nTrunc. |
| 4222 | */ |
| 4223 | *pPgno = pBt->nTrunc+1; |
| 4224 | if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ |
| 4225 | (*pPgno)++; |
| 4226 | } |
| 4227 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 4228 | if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, *pPgno) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4229 | /* If *pPgno refers to a pointer-map page, allocate two new pages |
| 4230 | ** at the end of the file instead of one. The first allocated page |
| 4231 | ** becomes a new pointer-map page, the second is used by the caller. |
| 4232 | */ |
| 4233 | TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno)); |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4234 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4235 | (*pPgno)++; |
drh | 7219043 | 2008-01-31 14:54:43 +0000 | [diff] [blame] | 4236 | if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ (*pPgno)++; } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4237 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 4238 | if( pBt->nTrunc ){ |
| 4239 | pBt->nTrunc = *pPgno; |
| 4240 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4241 | #endif |
| 4242 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4243 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4244 | rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 0); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4245 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4246 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 4247 | if( rc!=SQLITE_OK ){ |
| 4248 | releasePage(*ppPage); |
| 4249 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4250 | TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4251 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4252 | |
| 4253 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4254 | |
| 4255 | end_allocate_page: |
| 4256 | releasePage(pTrunk); |
| 4257 | releasePage(pPrevTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4258 | return rc; |
| 4259 | } |
| 4260 | |
| 4261 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4262 | ** Add a page of the database file to the freelist. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4263 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4264 | ** sqlite3PagerUnref() is NOT called for pPage. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4265 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4266 | static int freePage(MemPage *pPage){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4267 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4268 | MemPage *pPage1 = pBt->pPage1; |
| 4269 | int rc, n, k; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4270 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4271 | /* Prepare the page for freeing */ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4272 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4273 | assert( pPage->pgno>1 ); |
| 4274 | pPage->isInit = 0; |
| 4275 | releasePage(pPage->pParent); |
| 4276 | pPage->pParent = 0; |
| 4277 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4278 | /* Increment the free page count on pPage1 */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4279 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4280 | if( rc ) return rc; |
| 4281 | n = get4byte(&pPage1->aData[36]); |
| 4282 | put4byte(&pPage1->aData[36], n+1); |
| 4283 | |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4284 | #ifdef SQLITE_SECURE_DELETE |
| 4285 | /* If the SQLITE_SECURE_DELETE compile-time option is enabled, then |
| 4286 | ** always fully overwrite deleted information with zeros. |
| 4287 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4288 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4289 | if( rc ) return rc; |
| 4290 | memset(pPage->aData, 0, pPage->pBt->pageSize); |
| 4291 | #endif |
| 4292 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4293 | /* If the database supports auto-vacuum, write an entry in the pointer-map |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4294 | ** to indicate that the page is free. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4295 | */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 4296 | if( ISAUTOVACUUM ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4297 | rc = ptrmapPut(pBt, pPage->pgno, PTRMAP_FREEPAGE, 0); |
danielk1977 | a64a035 | 2004-11-05 01:45:13 +0000 | [diff] [blame] | 4298 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4299 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4300 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4301 | if( n==0 ){ |
| 4302 | /* This is the first free page */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4303 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4304 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4305 | memset(pPage->aData, 0, 8); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4306 | put4byte(&pPage1->aData[32], pPage->pgno); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4307 | TRACE(("FREE-PAGE: %d first\n", pPage->pgno)); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4308 | }else{ |
| 4309 | /* Other free pages already exist. Retrive the first trunk page |
| 4310 | ** of the freelist and find out how many leaves it has. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4311 | MemPage *pTrunk; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4312 | rc = sqlite3BtreeGetPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk, 0); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4313 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4314 | k = get4byte(&pTrunk->aData[4]); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 4315 | if( k>=pBt->usableSize/4 - 8 ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4316 | /* The trunk is full. Turn the page being freed into a new |
drh | 45b1fac | 2008-07-04 17:52:42 +0000 | [diff] [blame] | 4317 | ** trunk page with no leaves. |
| 4318 | ** |
| 4319 | ** Note that the trunk page is not really full until it contains |
| 4320 | ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have |
| 4321 | ** coded. But due to a coding error in versions of SQLite prior to |
| 4322 | ** 3.6.0, databases with freelist trunk pages holding more than |
| 4323 | ** usableSize/4 - 8 entries will be reported as corrupt. In order |
| 4324 | ** to maintain backwards compatibility with older versions of SQLite, |
| 4325 | ** we will contain to restrict the number of entries to usableSize/4 - 8 |
| 4326 | ** for now. At some point in the future (once everyone has upgraded |
| 4327 | ** to 3.6.0 or later) we should consider fixing the conditional above |
| 4328 | ** to read "usableSize/4-2" instead of "usableSize/4-8". |
| 4329 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4330 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | b9ee493 | 2007-09-07 14:32:06 +0000 | [diff] [blame] | 4331 | if( rc==SQLITE_OK ){ |
| 4332 | put4byte(pPage->aData, pTrunk->pgno); |
| 4333 | put4byte(&pPage->aData[4], 0); |
| 4334 | put4byte(&pPage1->aData[32], pPage->pgno); |
| 4335 | TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", |
| 4336 | pPage->pgno, pTrunk->pgno)); |
| 4337 | } |
| 4338 | }else if( k<0 ){ |
| 4339 | rc = SQLITE_CORRUPT; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4340 | }else{ |
| 4341 | /* Add the newly freed page as a leaf on the current trunk */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4342 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 4343 | if( rc==SQLITE_OK ){ |
| 4344 | put4byte(&pTrunk->aData[4], k+1); |
| 4345 | put4byte(&pTrunk->aData[8+k*4], pPage->pgno); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4346 | #ifndef SQLITE_SECURE_DELETE |
danielk1977 | a1fa00d | 2008-08-27 15:16:33 +0000 | [diff] [blame] | 4347 | rc = sqlite3PagerDontWrite(pPage->pDbPage); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4348 | #endif |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 4349 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4350 | 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] | 4351 | } |
| 4352 | releasePage(pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4353 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4354 | return rc; |
| 4355 | } |
| 4356 | |
| 4357 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4358 | ** Free any overflow pages associated with the given Cell. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4359 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4360 | static int clearCell(MemPage *pPage, unsigned char *pCell){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4361 | BtShared *pBt = pPage->pBt; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4362 | CellInfo info; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4363 | Pgno ovflPgno; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4364 | int rc; |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 4365 | int nOvfl; |
| 4366 | int ovflPageSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4367 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4368 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4369 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4370 | if( info.iOverflow==0 ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4371 | return SQLITE_OK; /* No overflow pages. Return without doing anything */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4372 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4373 | ovflPgno = get4byte(&pCell[info.iOverflow]); |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 4374 | ovflPageSize = pBt->usableSize - 4; |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 4375 | nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize; |
| 4376 | assert( ovflPgno==0 || nOvfl>0 ); |
| 4377 | while( nOvfl-- ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4378 | MemPage *pOvfl; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 4379 | if( ovflPgno==0 || ovflPgno>pagerPagecount(pBt->pPager) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 4380 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 4381 | } |
danielk1977 | 8c0a959 | 2007-04-30 16:55:00 +0000 | [diff] [blame] | 4382 | |
| 4383 | rc = getOverflowPage(pBt, ovflPgno, &pOvfl, (nOvfl==0)?0:&ovflPgno); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4384 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4385 | rc = freePage(pOvfl); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4386 | sqlite3PagerUnref(pOvfl->pDbPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 4387 | if( rc ) return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4388 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4389 | return SQLITE_OK; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4390 | } |
| 4391 | |
| 4392 | /* |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4393 | ** Create the byte sequence used to represent a cell on page pPage |
| 4394 | ** and write that byte sequence into pCell[]. Overflow pages are |
| 4395 | ** allocated and filled in as necessary. The calling procedure |
| 4396 | ** is responsible for making sure sufficient space has been allocated |
| 4397 | ** for pCell[]. |
| 4398 | ** |
| 4399 | ** Note that pCell does not necessary need to point to the pPage->aData |
| 4400 | ** area. pCell might point to some temporary storage. The cell will |
| 4401 | ** be constructed in this temporary area then copied into pPage->aData |
| 4402 | ** later. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4403 | */ |
| 4404 | static int fillInCell( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4405 | MemPage *pPage, /* The page that contains the cell */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4406 | unsigned char *pCell, /* Complete text of the cell */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 4407 | const void *pKey, i64 nKey, /* The key */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4408 | const void *pData,int nData, /* The data */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4409 | int nZero, /* Extra zero bytes to append to pData */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4410 | int *pnSize /* Write cell size here */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4411 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4412 | int nPayload; |
drh | 8c6fa9b | 2004-05-26 00:01:53 +0000 | [diff] [blame] | 4413 | const u8 *pSrc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4414 | int nSrc, n, rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4415 | int spaceLeft; |
| 4416 | MemPage *pOvfl = 0; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4417 | MemPage *pToRelease = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4418 | unsigned char *pPrior; |
| 4419 | unsigned char *pPayload; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4420 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4421 | Pgno pgnoOvfl = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4422 | int nHeader; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4423 | CellInfo info; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4424 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4425 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4426 | |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4427 | /* Fill in the header. */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4428 | nHeader = 0; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4429 | if( !pPage->leaf ){ |
| 4430 | nHeader += 4; |
| 4431 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4432 | if( pPage->hasData ){ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4433 | nHeader += putVarint(&pCell[nHeader], nData+nZero); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4434 | }else{ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4435 | nData = nZero = 0; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4436 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4437 | nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4438 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4439 | assert( info.nHeader==nHeader ); |
| 4440 | assert( info.nKey==nKey ); |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4441 | assert( info.nData==nData+nZero ); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4442 | |
| 4443 | /* Fill in the payload */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4444 | nPayload = nData + nZero; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4445 | if( pPage->intKey ){ |
| 4446 | pSrc = pData; |
| 4447 | nSrc = nData; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4448 | nData = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4449 | }else{ |
| 4450 | nPayload += nKey; |
| 4451 | pSrc = pKey; |
| 4452 | nSrc = nKey; |
| 4453 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4454 | *pnSize = info.nSize; |
| 4455 | spaceLeft = info.nLocal; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4456 | pPayload = &pCell[nHeader]; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4457 | pPrior = &pCell[info.iOverflow]; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4458 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4459 | while( nPayload>0 ){ |
| 4460 | if( spaceLeft==0 ){ |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4461 | int isExact = 0; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4462 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4463 | Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */ |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4464 | if( pBt->autoVacuum ){ |
| 4465 | do{ |
| 4466 | pgnoOvfl++; |
| 4467 | } while( |
| 4468 | PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt) |
| 4469 | ); |
danielk1977 | 89a4be8 | 2007-05-23 13:34:32 +0000 | [diff] [blame] | 4470 | if( pgnoOvfl>1 ){ |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4471 | /* isExact = 1; */ |
| 4472 | } |
| 4473 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4474 | #endif |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4475 | rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, isExact); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4476 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4477 | /* If the database supports auto-vacuum, and the second or subsequent |
| 4478 | ** overflow page is being allocated, add an entry to the pointer-map |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 4479 | ** for that page now. |
| 4480 | ** |
| 4481 | ** If this is the first overflow page, then write a partial entry |
| 4482 | ** to the pointer-map. If we write nothing to this pointer-map slot, |
| 4483 | ** then the optimistic overflow chain processing in clearCell() |
| 4484 | ** may misinterpret the uninitialised values and delete the |
| 4485 | ** wrong pages from the database. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4486 | */ |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 4487 | if( pBt->autoVacuum && rc==SQLITE_OK ){ |
| 4488 | u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1); |
| 4489 | rc = ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap); |
danielk1977 | 89a4be8 | 2007-05-23 13:34:32 +0000 | [diff] [blame] | 4490 | if( rc ){ |
| 4491 | releasePage(pOvfl); |
| 4492 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4493 | } |
| 4494 | #endif |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4495 | if( rc ){ |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4496 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4497 | return rc; |
| 4498 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4499 | put4byte(pPrior, pgnoOvfl); |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4500 | releasePage(pToRelease); |
| 4501 | pToRelease = pOvfl; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4502 | pPrior = pOvfl->aData; |
| 4503 | put4byte(pPrior, 0); |
| 4504 | pPayload = &pOvfl->aData[4]; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4505 | spaceLeft = pBt->usableSize - 4; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4506 | } |
| 4507 | n = nPayload; |
| 4508 | if( n>spaceLeft ) n = spaceLeft; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4509 | if( nSrc>0 ){ |
| 4510 | if( n>nSrc ) n = nSrc; |
| 4511 | assert( pSrc ); |
| 4512 | memcpy(pPayload, pSrc, n); |
| 4513 | }else{ |
| 4514 | memset(pPayload, 0, n); |
| 4515 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4516 | nPayload -= n; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4517 | pPayload += n; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4518 | pSrc += n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4519 | nSrc -= n; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4520 | spaceLeft -= n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4521 | if( nSrc==0 ){ |
| 4522 | nSrc = nData; |
| 4523 | pSrc = pData; |
| 4524 | } |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4525 | } |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4526 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4527 | return SQLITE_OK; |
| 4528 | } |
| 4529 | |
danielk1977 | f328bea | 2008-08-02 17:03:31 +0000 | [diff] [blame] | 4530 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4531 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4532 | ** Change the MemPage.pParent pointer on the page whose number is |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4533 | ** given in the second argument so that MemPage.pParent holds the |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4534 | ** pointer in the third argument. |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 4535 | ** |
| 4536 | ** If the final argument, updatePtrmap, is non-zero and the database |
| 4537 | ** is an auto-vacuum database, then the pointer-map entry for pgno |
| 4538 | ** is updated. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4539 | */ |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 4540 | static int reparentPage( |
| 4541 | BtShared *pBt, /* B-Tree structure */ |
| 4542 | Pgno pgno, /* Page number of child being adopted */ |
| 4543 | MemPage *pNewParent, /* New parent of pgno */ |
| 4544 | int idx, /* Index of child page pgno in pNewParent */ |
| 4545 | int updatePtrmap /* If true, update pointer-map for pgno */ |
| 4546 | ){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4547 | MemPage *pThis; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4548 | DbPage *pDbPage; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4549 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4550 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 4551 | assert( pNewParent!=0 ); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4552 | if( pgno==0 ) return SQLITE_OK; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4553 | assert( pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4554 | pDbPage = sqlite3PagerLookup(pBt->pPager, pgno); |
| 4555 | if( pDbPage ){ |
| 4556 | pThis = (MemPage *)sqlite3PagerGetExtra(pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4557 | if( pThis->isInit ){ |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 4558 | assert( pThis->aData==sqlite3PagerGetData(pDbPage) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4559 | if( pThis->pParent!=pNewParent ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4560 | if( pThis->pParent ) sqlite3PagerUnref(pThis->pParent->pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4561 | pThis->pParent = pNewParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4562 | sqlite3PagerRef(pNewParent->pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4563 | } |
| 4564 | pThis->idxParent = idx; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4565 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4566 | sqlite3PagerUnref(pDbPage); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4567 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4568 | |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 4569 | if( ISAUTOVACUUM && updatePtrmap ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4570 | return ptrmapPut(pBt, pgno, PTRMAP_BTREE, pNewParent->pgno); |
| 4571 | } |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 4572 | |
| 4573 | #ifndef NDEBUG |
| 4574 | /* If the updatePtrmap flag was clear, assert that the entry in the |
| 4575 | ** pointer-map is already correct. |
| 4576 | */ |
danielk1977 | a68468f | 2008-08-02 17:36:45 +0000 | [diff] [blame] | 4577 | if( ISAUTOVACUUM ){ |
| 4578 | pDbPage = sqlite3PagerLookup(pBt->pPager,PTRMAP_PAGENO(pBt,pgno)); |
| 4579 | if( pDbPage ){ |
| 4580 | u8 eType; |
| 4581 | Pgno ii; |
| 4582 | int rc = ptrmapGet(pBt, pgno, &eType, &ii); |
| 4583 | assert( rc==SQLITE_OK && ii==pNewParent->pgno && eType==PTRMAP_BTREE ); |
| 4584 | sqlite3PagerUnref(pDbPage); |
| 4585 | } |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 4586 | } |
| 4587 | #endif |
| 4588 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4589 | return SQLITE_OK; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4590 | } |
| 4591 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4592 | |
| 4593 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4594 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4595 | ** Change the pParent pointer of all children of pPage to point back |
| 4596 | ** to pPage. |
| 4597 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4598 | ** In other words, for every child of pPage, invoke reparentPage() |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4599 | ** to make sure that each child knows that pPage is its parent. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4600 | ** |
| 4601 | ** This routine gets called after you memcpy() one page into |
| 4602 | ** another. |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 4603 | ** |
| 4604 | ** If updatePtrmap is true, then the pointer-map entries for all child |
| 4605 | ** pages of pPage are updated. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4606 | */ |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 4607 | static int reparentChildPages(MemPage *pPage, int updatePtrmap){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4608 | int rc = SQLITE_OK; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4609 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 4610 | if( !pPage->leaf ){ |
| 4611 | int i; |
| 4612 | BtShared *pBt = pPage->pBt; |
| 4613 | Pgno iRight = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4614 | |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 4615 | for(i=0; i<pPage->nCell; i++){ |
| 4616 | u8 *pCell = findCell(pPage, i); |
| 4617 | rc = reparentPage(pBt, get4byte(pCell), pPage, i, updatePtrmap); |
| 4618 | if( rc!=SQLITE_OK ) return rc; |
| 4619 | } |
| 4620 | rc = reparentPage(pBt, iRight, pPage, i, updatePtrmap); |
| 4621 | pPage->idxShift = 0; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4622 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4623 | return rc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4624 | } |
| 4625 | |
| 4626 | /* |
| 4627 | ** Remove the i-th cell from pPage. This routine effects pPage only. |
| 4628 | ** The cell content is not freed or deallocated. It is assumed that |
| 4629 | ** the cell content has been copied someplace else. This routine just |
| 4630 | ** removes the reference to the cell from pPage. |
| 4631 | ** |
| 4632 | ** "sz" must be the number of bytes in the cell. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4633 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4634 | static void dropCell(MemPage *pPage, int idx, int sz){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4635 | int i; /* Loop counter */ |
| 4636 | int pc; /* Offset to cell content of cell being deleted */ |
| 4637 | u8 *data; /* pPage->aData */ |
| 4638 | u8 *ptr; /* Used to move bytes around within data[] */ |
| 4639 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4640 | assert( idx>=0 && idx<pPage->nCell ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4641 | assert( sz==cellSize(pPage, idx) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4642 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4643 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4644 | data = pPage->aData; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4645 | ptr = &data[pPage->cellOffset + 2*idx]; |
| 4646 | pc = get2byte(ptr); |
| 4647 | assert( pc>10 && pc+sz<=pPage->pBt->usableSize ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4648 | freeSpace(pPage, pc, sz); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4649 | for(i=idx+1; i<pPage->nCell; i++, ptr+=2){ |
| 4650 | ptr[0] = ptr[2]; |
| 4651 | ptr[1] = ptr[3]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4652 | } |
| 4653 | pPage->nCell--; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4654 | put2byte(&data[pPage->hdrOffset+3], pPage->nCell); |
| 4655 | pPage->nFree += 2; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 4656 | pPage->idxShift = 1; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4657 | } |
| 4658 | |
| 4659 | /* |
| 4660 | ** Insert a new cell on pPage at cell index "i". pCell points to the |
| 4661 | ** content of the cell. |
| 4662 | ** |
| 4663 | ** 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] | 4664 | ** will not fit, then make a copy of the cell content into pTemp if |
| 4665 | ** pTemp is not null. Regardless of pTemp, allocate a new entry |
| 4666 | ** in pPage->aOvfl[] and make it point to the cell content (either |
| 4667 | ** in pTemp or the original pCell) and also record its index. |
| 4668 | ** Allocating a new entry in pPage->aCell[] implies that |
| 4669 | ** pPage->nOverflow is incremented. |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4670 | ** |
| 4671 | ** If nSkip is non-zero, then do not copy the first nSkip bytes of the |
| 4672 | ** cell. The caller will overwrite them after this function returns. If |
drh | 4b238df | 2005-01-08 15:43:18 +0000 | [diff] [blame] | 4673 | ** 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] | 4674 | ** (but pCell+nSkip is always valid). |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4675 | */ |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4676 | static int insertCell( |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4677 | MemPage *pPage, /* Page into which we are copying */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4678 | int i, /* New cell becomes the i-th cell of the page */ |
| 4679 | u8 *pCell, /* Content of the new cell */ |
| 4680 | int sz, /* Bytes of content in pCell */ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4681 | u8 *pTemp, /* Temp storage space for pCell, if needed */ |
| 4682 | u8 nSkip /* Do not write the first nSkip bytes of the cell */ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4683 | ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4684 | int idx; /* Where to write new cell content in data[] */ |
| 4685 | int j; /* Loop counter */ |
| 4686 | int top; /* First byte of content for any cell in data[] */ |
| 4687 | int end; /* First byte past the last cell pointer in data[] */ |
| 4688 | int ins; /* Index in data[] where new cell pointer is inserted */ |
| 4689 | int hdr; /* Offset into data[] of the page header */ |
| 4690 | int cellOffset; /* Address of first cell pointer in data[] */ |
| 4691 | u8 *data; /* The content of the whole page */ |
| 4692 | u8 *ptr; /* Used for moving information around in data[] */ |
| 4693 | |
| 4694 | assert( i>=0 && i<=pPage->nCell+pPage->nOverflow ); |
| 4695 | assert( sz==cellSizePtr(pPage, pCell) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4696 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4697 | if( pPage->nOverflow || sz+2>pPage->nFree ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4698 | if( pTemp ){ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4699 | memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4700 | pCell = pTemp; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4701 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4702 | j = pPage->nOverflow++; |
| 4703 | assert( j<sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0]) ); |
| 4704 | pPage->aOvfl[j].pCell = pCell; |
| 4705 | pPage->aOvfl[j].idx = i; |
| 4706 | pPage->nFree = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4707 | }else{ |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4708 | int rc = sqlite3PagerWrite(pPage->pDbPage); |
| 4709 | if( rc!=SQLITE_OK ){ |
| 4710 | return rc; |
| 4711 | } |
| 4712 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4713 | data = pPage->aData; |
| 4714 | hdr = pPage->hdrOffset; |
| 4715 | top = get2byte(&data[hdr+5]); |
| 4716 | cellOffset = pPage->cellOffset; |
| 4717 | end = cellOffset + 2*pPage->nCell + 2; |
| 4718 | ins = cellOffset + 2*i; |
| 4719 | if( end > top - sz ){ |
danielk1977 | 474b7cc | 2008-07-09 11:49:46 +0000 | [diff] [blame] | 4720 | defragmentPage(pPage); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4721 | top = get2byte(&data[hdr+5]); |
| 4722 | assert( end + sz <= top ); |
| 4723 | } |
| 4724 | idx = allocateSpace(pPage, sz); |
| 4725 | assert( idx>0 ); |
| 4726 | assert( end <= get2byte(&data[hdr+5]) ); |
| 4727 | pPage->nCell++; |
| 4728 | pPage->nFree -= 2; |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4729 | memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4730 | for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){ |
| 4731 | ptr[0] = ptr[-2]; |
| 4732 | ptr[1] = ptr[-1]; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4733 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4734 | put2byte(&data[ins], idx); |
| 4735 | put2byte(&data[hdr+3], pPage->nCell); |
| 4736 | pPage->idxShift = 1; |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4737 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4738 | if( pPage->pBt->autoVacuum ){ |
| 4739 | /* The cell may contain a pointer to an overflow page. If so, write |
| 4740 | ** the entry for the overflow page into the pointer map. |
| 4741 | */ |
| 4742 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4743 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 4744 | assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4745 | if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ |
| 4746 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4747 | rc = ptrmapPut(pPage->pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4748 | if( rc!=SQLITE_OK ) return rc; |
| 4749 | } |
| 4750 | } |
| 4751 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4752 | } |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4753 | |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4754 | return SQLITE_OK; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4755 | } |
| 4756 | |
| 4757 | /* |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4758 | ** Add a list of cells to a page. The page should be initially empty. |
| 4759 | ** The cells are guaranteed to fit on the page. |
| 4760 | */ |
| 4761 | static void assemblePage( |
| 4762 | MemPage *pPage, /* The page to be assemblied */ |
| 4763 | int nCell, /* The number of cells to add to this page */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4764 | u8 **apCell, /* Pointers to cell bodies */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 4765 | u16 *aSize /* Sizes of the cells */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4766 | ){ |
| 4767 | int i; /* Loop counter */ |
| 4768 | int totalSize; /* Total size of all cells */ |
| 4769 | int hdr; /* Index of page header */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4770 | int cellptr; /* Address of next cell pointer */ |
| 4771 | int cellbody; /* Address of next cell body */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4772 | u8 *data; /* Data for the page */ |
| 4773 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4774 | assert( pPage->nOverflow==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4775 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4776 | totalSize = 0; |
| 4777 | for(i=0; i<nCell; i++){ |
| 4778 | totalSize += aSize[i]; |
| 4779 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4780 | assert( totalSize+2*nCell<=pPage->nFree ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4781 | assert( pPage->nCell==0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4782 | cellptr = pPage->cellOffset; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4783 | data = pPage->aData; |
| 4784 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4785 | put2byte(&data[hdr+3], nCell); |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 4786 | if( nCell ){ |
| 4787 | cellbody = allocateSpace(pPage, totalSize); |
| 4788 | assert( cellbody>0 ); |
| 4789 | assert( pPage->nFree >= 2*nCell ); |
| 4790 | pPage->nFree -= 2*nCell; |
| 4791 | for(i=0; i<nCell; i++){ |
| 4792 | put2byte(&data[cellptr], cellbody); |
| 4793 | memcpy(&data[cellbody], apCell[i], aSize[i]); |
| 4794 | cellptr += 2; |
| 4795 | cellbody += aSize[i]; |
| 4796 | } |
| 4797 | assert( cellbody==pPage->pBt->usableSize ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4798 | } |
| 4799 | pPage->nCell = nCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4800 | } |
| 4801 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4802 | /* |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4803 | ** The following parameters determine how many adjacent pages get involved |
| 4804 | ** in a balancing operation. NN is the number of neighbors on either side |
| 4805 | ** of the page that participate in the balancing operation. NB is the |
| 4806 | ** total number of pages that participate, including the target page and |
| 4807 | ** NN neighbors on either side. |
| 4808 | ** |
| 4809 | ** The minimum value of NN is 1 (of course). Increasing NN above 1 |
| 4810 | ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance |
| 4811 | ** in exchange for a larger degradation in INSERT and UPDATE performance. |
| 4812 | ** The value of NN appears to give the best results overall. |
| 4813 | */ |
| 4814 | #define NN 1 /* Number of neighbors on either side of pPage */ |
| 4815 | #define NB (NN*2+1) /* Total pages involved in the balance */ |
| 4816 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4817 | /* Forward reference */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4818 | static int balance(MemPage*, int); |
| 4819 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4820 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 4821 | /* |
| 4822 | ** This version of balance() handles the common special case where |
| 4823 | ** a new entry is being inserted on the extreme right-end of the |
| 4824 | ** tree, in other words, when the new entry will become the largest |
| 4825 | ** entry in the tree. |
| 4826 | ** |
| 4827 | ** Instead of trying balance the 3 right-most leaf pages, just add |
| 4828 | ** a new page to the right-hand side and put the one new entry in |
| 4829 | ** that page. This leaves the right side of the tree somewhat |
| 4830 | ** unbalanced. But odds are that we will be inserting new entries |
| 4831 | ** at the end soon afterwards so the nearly empty page will quickly |
| 4832 | ** fill up. On average. |
| 4833 | ** |
| 4834 | ** pPage is the leaf page which is the right-most page in the tree. |
| 4835 | ** pParent is its parent. pPage must have a single overflow entry |
| 4836 | ** which is also the right-most entry on the page. |
| 4837 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4838 | static int balance_quick(MemPage *pPage, MemPage *pParent){ |
| 4839 | int rc; |
| 4840 | MemPage *pNew; |
| 4841 | Pgno pgnoNew; |
| 4842 | u8 *pCell; |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 4843 | u16 szCell; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4844 | CellInfo info; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4845 | BtShared *pBt = pPage->pBt; |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4846 | int parentIdx = pParent->nCell; /* pParent new divider cell index */ |
| 4847 | int parentSize; /* Size of new divider cell */ |
| 4848 | u8 parentCell[64]; /* Space for the new divider cell */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4849 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4850 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4851 | |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4852 | /* Allocate a new page. Insert the overflow cell from pPage |
| 4853 | ** into it. Then remove the overflow cell from pPage. |
| 4854 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4855 | rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4856 | if( rc!=SQLITE_OK ){ |
| 4857 | return rc; |
| 4858 | } |
| 4859 | pCell = pPage->aOvfl[0].pCell; |
| 4860 | szCell = cellSizePtr(pPage, pCell); |
| 4861 | zeroPage(pNew, pPage->aData[0]); |
| 4862 | assemblePage(pNew, 1, &pCell, &szCell); |
| 4863 | pPage->nOverflow = 0; |
| 4864 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4865 | /* Set the parent of the newly allocated page to pParent. */ |
| 4866 | pNew->pParent = pParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4867 | sqlite3PagerRef(pParent->pDbPage); |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4868 | |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4869 | /* pPage is currently the right-child of pParent. Change this |
| 4870 | ** so that the right-child is the new page allocated above and |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4871 | ** pPage is the next-to-right child. |
danielk1977 | 474b7cc | 2008-07-09 11:49:46 +0000 | [diff] [blame] | 4872 | ** |
| 4873 | ** Ignore the return value of the call to fillInCell(). fillInCell() |
| 4874 | ** may only return other than SQLITE_OK if it is required to allocate |
| 4875 | ** one or more overflow pages. Since an internal table B-Tree cell |
| 4876 | ** may never spill over onto an overflow page (it is a maximum of |
| 4877 | ** 13 bytes in size), it is not neccessary to check the return code. |
| 4878 | ** |
| 4879 | ** Similarly, the insertCell() function cannot fail if the page |
| 4880 | ** being inserted into is already writable and the cell does not |
| 4881 | ** contain an overflow pointer. So ignore this return code too. |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4882 | */ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4883 | assert( pPage->nCell>0 ); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 4884 | pCell = findCell(pPage, pPage->nCell-1); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4885 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
danielk1977 | 474b7cc | 2008-07-09 11:49:46 +0000 | [diff] [blame] | 4886 | fillInCell(pParent, parentCell, 0, info.nKey, 0, 0, 0, &parentSize); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4887 | assert( parentSize<64 ); |
danielk1977 | 474b7cc | 2008-07-09 11:49:46 +0000 | [diff] [blame] | 4888 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
| 4889 | insertCell(pParent, parentIdx, parentCell, parentSize, 0, 4); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4890 | put4byte(findOverflowCell(pParent,parentIdx), pPage->pgno); |
| 4891 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew); |
| 4892 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4893 | /* If this is an auto-vacuum database, update the pointer map |
| 4894 | ** with entries for the new page, and any pointer from the |
| 4895 | ** cell on the page to an overflow page. |
| 4896 | */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 4897 | if( ISAUTOVACUUM ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4898 | rc = ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno); |
danielk1977 | deb403e | 2007-05-24 09:20:16 +0000 | [diff] [blame] | 4899 | if( rc==SQLITE_OK ){ |
| 4900 | rc = ptrmapPutOvfl(pNew, 0); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4901 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4902 | if( rc!=SQLITE_OK ){ |
danielk1977 | deb403e | 2007-05-24 09:20:16 +0000 | [diff] [blame] | 4903 | releasePage(pNew); |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4904 | return rc; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4905 | } |
| 4906 | } |
| 4907 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4908 | /* Release the reference to the new page and balance the parent page, |
| 4909 | ** in case the divider cell inserted caused it to become overfull. |
| 4910 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4911 | releasePage(pNew); |
| 4912 | return balance(pParent, 0); |
| 4913 | } |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4914 | #endif /* SQLITE_OMIT_QUICKBALANCE */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4915 | |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4916 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4917 | ** This routine redistributes Cells on pPage and up to NN*2 siblings |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4918 | ** 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] | 4919 | ** Usually NN siblings on either side of pPage is used in the balancing, |
| 4920 | ** though more siblings might come from one side if pPage is the first |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4921 | ** 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] | 4922 | ** (something which can only happen if pPage is the root page or a |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4923 | ** child of root) then all available siblings participate in the balancing. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4924 | ** |
drh | 0c6cc4e | 2004-06-15 02:13:26 +0000 | [diff] [blame] | 4925 | ** The number of siblings of pPage might be increased or decreased by one or |
| 4926 | ** 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] | 4927 | ** is special and is allowed to be nearly empty. If pPage is |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4928 | ** the root page, then the depth of the tree might be increased |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4929 | ** or decreased by one, as necessary, to keep the root page from being |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4930 | ** overfull or completely empty. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4931 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4932 | ** Note that when this routine is called, some of the Cells on pPage |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4933 | ** might not actually be stored in pPage->aData[]. This can happen |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4934 | ** 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] | 4935 | ** make sure all Cells for pPage once again fit in pPage->aData[]. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4936 | ** |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4937 | ** In the course of balancing the siblings of pPage, the parent of pPage |
| 4938 | ** might become overfull or underfull. If that happens, then this routine |
| 4939 | ** is called recursively on the parent. |
| 4940 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4941 | ** If this routine fails for any reason, it might leave the database |
| 4942 | ** in a corrupted state. So if this routine fails, the database should |
| 4943 | ** be rolled back. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4944 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4945 | static int balance_nonroot(MemPage *pPage){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4946 | MemPage *pParent; /* The parent of pPage */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4947 | BtShared *pBt; /* The whole database */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4948 | int nCell = 0; /* Number of cells in apCell[] */ |
| 4949 | int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4950 | int nOld; /* Number of pages in apOld[] */ |
| 4951 | int nNew; /* Number of pages in apNew[] */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4952 | int nDiv; /* Number of cells in apDiv[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4953 | int i, j, k; /* Loop counters */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4954 | int idx; /* Index of pPage in pParent->aCell[] */ |
| 4955 | int nxDiv; /* Next divider slot in pParent->aCell[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4956 | int rc; /* The return code */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4957 | int leafCorrection; /* 4 if pPage is a leaf. 0 if not */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4958 | int leafData; /* True if pPage is a leaf of a LEAFDATA tree */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4959 | int usableSpace; /* Bytes in pPage beyond the header */ |
| 4960 | int pageFlags; /* Value of pPage->aData[0] */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4961 | int subtotal; /* Subtotal of bytes in cells on one page */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 4962 | int iSpace1 = 0; /* First unused byte of aSpace1[] */ |
| 4963 | int iSpace2 = 0; /* First unused byte of aSpace2[] */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 4964 | int szScratch; /* Size of scratch memory requested */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4965 | MemPage *apOld[NB]; /* pPage and up to two siblings */ |
| 4966 | Pgno pgnoOld[NB]; /* Page numbers for each page in apOld[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4967 | MemPage *apCopy[NB]; /* Private copies of apOld[] pages */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4968 | MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ |
| 4969 | Pgno pgnoNew[NB+2]; /* Page numbers for each page in apNew[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4970 | u8 *apDiv[NB]; /* Divider cells in pParent */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4971 | int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */ |
| 4972 | int szNew[NB+2]; /* Combined size of cells place on i-th page */ |
danielk1977 | 50f059b | 2005-03-29 02:54:03 +0000 | [diff] [blame] | 4973 | u8 **apCell = 0; /* All cells begin balanced */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 4974 | u16 *szCell; /* Local size of all cells in apCell[] */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 4975 | u8 *aCopy[NB]; /* Space for holding data of apCopy[] */ |
| 4976 | u8 *aSpace1; /* Space for copies of dividers cells before balance */ |
| 4977 | u8 *aSpace2 = 0; /* Space for overflow dividers cells after balance */ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4978 | u8 *aFrom = 0; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4979 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4980 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4981 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4982 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4983 | ** Find the parent page. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4984 | */ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4985 | assert( pPage->isInit ); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4986 | assert( sqlite3PagerIswriteable(pPage->pDbPage) || pPage->nOverflow==1 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4987 | pBt = pPage->pBt; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4988 | pParent = pPage->pParent; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4989 | assert( pParent ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4990 | if( SQLITE_OK!=(rc = sqlite3PagerWrite(pParent->pDbPage)) ){ |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 4991 | return rc; |
| 4992 | } |
danielk1977 | 474b7cc | 2008-07-09 11:49:46 +0000 | [diff] [blame] | 4993 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4994 | TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno)); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4995 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4996 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 4997 | /* |
| 4998 | ** A special case: If a new entry has just been inserted into a |
| 4999 | ** 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] | 5000 | ** 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] | 5001 | ** largest key) then use the special balance_quick() routine for |
| 5002 | ** balancing. balance_quick() is much faster and results in a tighter |
| 5003 | ** packing of data in the common case. |
| 5004 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5005 | if( pPage->leaf && |
| 5006 | pPage->intKey && |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5007 | pPage->nOverflow==1 && |
| 5008 | pPage->aOvfl[0].idx==pPage->nCell && |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5009 | pPage->pParent->pgno!=1 && |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5010 | get4byte(&pParent->aData[pParent->hdrOffset+8])==pPage->pgno |
| 5011 | ){ |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 5012 | assert( pPage->intKey ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5013 | /* |
| 5014 | ** TODO: Check the siblings to the left of pPage. It may be that |
| 5015 | ** they are not full and no new page is required. |
| 5016 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5017 | return balance_quick(pPage, pParent); |
| 5018 | } |
| 5019 | #endif |
| 5020 | |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 5021 | if( SQLITE_OK!=(rc = sqlite3PagerWrite(pPage->pDbPage)) ){ |
| 5022 | return rc; |
| 5023 | } |
| 5024 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5025 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5026 | ** Find the cell in the parent page whose left child points back |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5027 | ** to pPage. The "idx" variable is the index of that cell. If pPage |
| 5028 | ** is the rightmost child of pParent then set idx to pParent->nCell |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5029 | */ |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 5030 | if( pParent->idxShift ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5031 | Pgno pgno; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5032 | pgno = pPage->pgno; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5033 | assert( pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 5034 | for(idx=0; idx<pParent->nCell; idx++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5035 | if( get4byte(findCell(pParent, idx))==pgno ){ |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 5036 | break; |
| 5037 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5038 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5039 | assert( idx<pParent->nCell |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5040 | || get4byte(&pParent->aData[pParent->hdrOffset+8])==pgno ); |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 5041 | }else{ |
| 5042 | idx = pPage->idxParent; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5043 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5044 | |
| 5045 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5046 | ** Initialize variables so that it will be safe to jump |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 5047 | ** directly to balance_cleanup at any moment. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5048 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5049 | nOld = nNew = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5050 | sqlite3PagerRef(pParent->pDbPage); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5051 | |
| 5052 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5053 | ** Find sibling pages to pPage and the cells in pParent that divide |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5054 | ** the siblings. An attempt is made to find NN siblings on either |
| 5055 | ** side of pPage. More siblings are taken from one side, however, if |
| 5056 | ** pPage there are fewer than NN siblings on the other side. If pParent |
| 5057 | ** has NB or fewer children then all children of pParent are taken. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5058 | */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5059 | nxDiv = idx - NN; |
| 5060 | if( nxDiv + NB > pParent->nCell ){ |
| 5061 | nxDiv = pParent->nCell - NB + 1; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5062 | } |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5063 | if( nxDiv<0 ){ |
| 5064 | nxDiv = 0; |
| 5065 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5066 | nDiv = 0; |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5067 | for(i=0, k=nxDiv; i<NB; i++, k++){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5068 | if( k<pParent->nCell ){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5069 | apDiv[i] = findCell(pParent, k); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5070 | nDiv++; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5071 | assert( !pParent->leaf ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5072 | pgnoOld[i] = get4byte(apDiv[i]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5073 | }else if( k==pParent->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5074 | pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+8]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5075 | }else{ |
| 5076 | break; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5077 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 5078 | rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i], pParent); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5079 | if( rc ) goto balance_cleanup; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 5080 | apOld[i]->idxParent = k; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5081 | apCopy[i] = 0; |
| 5082 | assert( i==nOld ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5083 | nOld++; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5084 | nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5085 | } |
| 5086 | |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5087 | /* Make nMaxCells a multiple of 4 in order to preserve 8-byte |
drh | 8d97f1f | 2005-05-05 18:14:13 +0000 | [diff] [blame] | 5088 | ** alignment */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5089 | nMaxCells = (nMaxCells + 3)&~3; |
drh | 8d97f1f | 2005-05-05 18:14:13 +0000 | [diff] [blame] | 5090 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5091 | /* |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5092 | ** Allocate space for memory structures |
| 5093 | */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5094 | szScratch = |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5095 | nMaxCells*sizeof(u8*) /* apCell */ |
| 5096 | + nMaxCells*sizeof(u16) /* szCell */ |
| 5097 | + (ROUND8(sizeof(MemPage))+pBt->pageSize)*NB /* aCopy */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5098 | + pBt->pageSize /* aSpace1 */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5099 | + (ISAUTOVACUUM ? nMaxCells : 0); /* aFrom */ |
| 5100 | apCell = sqlite3ScratchMalloc( szScratch ); |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5101 | if( apCell==0 ){ |
| 5102 | rc = SQLITE_NOMEM; |
| 5103 | goto balance_cleanup; |
| 5104 | } |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5105 | szCell = (u16*)&apCell[nMaxCells]; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5106 | aCopy[0] = (u8*)&szCell[nMaxCells]; |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 5107 | assert( ((aCopy[0] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5108 | for(i=1; i<NB; i++){ |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 5109 | aCopy[i] = &aCopy[i-1][pBt->pageSize+ROUND8(sizeof(MemPage))]; |
| 5110 | assert( ((aCopy[i] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5111 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5112 | aSpace1 = &aCopy[NB-1][pBt->pageSize+ROUND8(sizeof(MemPage))]; |
| 5113 | assert( ((aSpace1 - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5114 | if( ISAUTOVACUUM ){ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5115 | aFrom = &aSpace1[pBt->pageSize]; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5116 | } |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5117 | aSpace2 = sqlite3PageMalloc(pBt->pageSize); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5118 | if( aSpace2==0 ){ |
| 5119 | rc = SQLITE_NOMEM; |
| 5120 | goto balance_cleanup; |
| 5121 | } |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5122 | |
| 5123 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5124 | ** Make copies of the content of pPage and its siblings into aOld[]. |
| 5125 | ** The rest of this function will use data from the copies rather |
| 5126 | ** that the original pages since the original pages will be in the |
| 5127 | ** process of being overwritten. |
| 5128 | */ |
| 5129 | for(i=0; i<nOld; i++){ |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 5130 | MemPage *p = apCopy[i] = (MemPage*)aCopy[i]; |
| 5131 | memcpy(p, apOld[i], sizeof(MemPage)); |
| 5132 | p->aData = (void*)&p[1]; |
| 5133 | memcpy(p->aData, apOld[i]->aData, pBt->pageSize); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5134 | } |
| 5135 | |
| 5136 | /* |
| 5137 | ** Load pointers to all cells on sibling pages and the divider cells |
| 5138 | ** into the local apCell[] array. Make copies of the divider cells |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5139 | ** into space obtained form aSpace1[] and remove the the divider Cells |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5140 | ** from pParent. |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5141 | ** |
| 5142 | ** If the siblings are on leaf pages, then the child pointers of the |
| 5143 | ** divider cells are stripped from the cells before they are copied |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5144 | ** into aSpace1[]. In this way, all cells in apCell[] are without |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5145 | ** child pointers. If siblings are not leaves, then all cell in |
| 5146 | ** apCell[] include child pointers. Either way, all cells in apCell[] |
| 5147 | ** are alike. |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5148 | ** |
| 5149 | ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf. |
| 5150 | ** leafData: 1 if pPage holds key+data and pParent holds only keys. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5151 | */ |
| 5152 | nCell = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5153 | leafCorrection = pPage->leaf*4; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 5154 | leafData = pPage->hasData; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5155 | for(i=0; i<nOld; i++){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5156 | MemPage *pOld = apCopy[i]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5157 | int limit = pOld->nCell+pOld->nOverflow; |
| 5158 | for(j=0; j<limit; j++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5159 | assert( nCell<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5160 | apCell[nCell] = findOverflowCell(pOld, j); |
| 5161 | szCell[nCell] = cellSizePtr(pOld, apCell[nCell]); |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5162 | if( ISAUTOVACUUM ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5163 | int a; |
| 5164 | aFrom[nCell] = i; |
| 5165 | for(a=0; a<pOld->nOverflow; a++){ |
| 5166 | if( pOld->aOvfl[a].pCell==apCell[nCell] ){ |
| 5167 | aFrom[nCell] = 0xFF; |
| 5168 | break; |
| 5169 | } |
| 5170 | } |
| 5171 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5172 | nCell++; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5173 | } |
| 5174 | if( i<nOld-1 ){ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5175 | u16 sz = cellSizePtr(pParent, apDiv[i]); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5176 | if( leafData ){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5177 | /* With the LEAFDATA flag, pParent cells hold only INTKEYs that |
| 5178 | ** are duplicates of keys on the child pages. We need to remove |
| 5179 | ** the divider cells from pParent, but the dividers cells are not |
| 5180 | ** added to apCell[] because they are duplicates of child cells. |
| 5181 | */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5182 | dropCell(pParent, nxDiv, sz); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5183 | }else{ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5184 | u8 *pTemp; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5185 | assert( nCell<nMaxCells ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5186 | szCell[nCell] = sz; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5187 | pTemp = &aSpace1[iSpace1]; |
| 5188 | iSpace1 += sz; |
| 5189 | assert( sz<=pBt->pageSize/4 ); |
| 5190 | assert( iSpace1<=pBt->pageSize ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5191 | memcpy(pTemp, apDiv[i], sz); |
| 5192 | apCell[nCell] = pTemp+leafCorrection; |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5193 | if( ISAUTOVACUUM ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5194 | aFrom[nCell] = 0xFF; |
| 5195 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5196 | dropCell(pParent, nxDiv, sz); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5197 | szCell[nCell] -= leafCorrection; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5198 | assert( get4byte(pTemp)==pgnoOld[i] ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5199 | if( !pOld->leaf ){ |
| 5200 | assert( leafCorrection==0 ); |
| 5201 | /* The right pointer of the child page pOld becomes the left |
| 5202 | ** pointer of the divider cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5203 | memcpy(apCell[nCell], &pOld->aData[pOld->hdrOffset+8], 4); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5204 | }else{ |
| 5205 | assert( leafCorrection==4 ); |
danielk1977 | 39c9604 | 2007-05-12 10:41:47 +0000 | [diff] [blame] | 5206 | if( szCell[nCell]<4 ){ |
| 5207 | /* Do not allow any cells smaller than 4 bytes. */ |
| 5208 | szCell[nCell] = 4; |
| 5209 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5210 | } |
| 5211 | nCell++; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5212 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5213 | } |
| 5214 | } |
| 5215 | |
| 5216 | /* |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5217 | ** Figure out the number of pages needed to hold all nCell cells. |
| 5218 | ** Store this number in "k". Also compute szNew[] which is the total |
| 5219 | ** 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] | 5220 | ** in apCell[] of the cell that divides page i from page i+1. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5221 | ** cntNew[k] should equal nCell. |
| 5222 | ** |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5223 | ** Values computed by this block: |
| 5224 | ** |
| 5225 | ** k: The total number of sibling pages |
| 5226 | ** szNew[i]: Spaced used on the i-th sibling page. |
| 5227 | ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to |
| 5228 | ** the right of the i-th sibling page. |
| 5229 | ** usableSpace: Number of bytes of space available on each sibling. |
| 5230 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5231 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5232 | usableSpace = pBt->usableSize - 12 + leafCorrection; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5233 | for(subtotal=k=i=0; i<nCell; i++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5234 | assert( i<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5235 | subtotal += szCell[i] + 2; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5236 | if( subtotal > usableSpace ){ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5237 | szNew[k] = subtotal - szCell[i]; |
| 5238 | cntNew[k] = i; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5239 | if( leafData ){ i--; } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5240 | subtotal = 0; |
| 5241 | k++; |
| 5242 | } |
| 5243 | } |
| 5244 | szNew[k] = subtotal; |
| 5245 | cntNew[k] = nCell; |
| 5246 | k++; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5247 | |
| 5248 | /* |
| 5249 | ** The packing computed by the previous block is biased toward the siblings |
| 5250 | ** on the left side. The left siblings are always nearly full, while the |
| 5251 | ** right-most sibling might be nearly empty. This block of code attempts |
| 5252 | ** to adjust the packing of siblings to get a better balance. |
| 5253 | ** |
| 5254 | ** This adjustment is more than an optimization. The packing above might |
| 5255 | ** be so out of balance as to be illegal. For example, the right-most |
| 5256 | ** sibling might be completely empty. This adjustment is not optional. |
| 5257 | */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5258 | for(i=k-1; i>0; i--){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5259 | int szRight = szNew[i]; /* Size of sibling on the right */ |
| 5260 | int szLeft = szNew[i-1]; /* Size of sibling on the left */ |
| 5261 | int r; /* Index of right-most cell in left sibling */ |
| 5262 | int d; /* Index of first cell to the left of right sibling */ |
| 5263 | |
| 5264 | r = cntNew[i-1] - 1; |
| 5265 | d = r + 1 - leafData; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5266 | assert( d<nMaxCells ); |
| 5267 | assert( r<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5268 | while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){ |
| 5269 | szRight += szCell[d] + 2; |
| 5270 | szLeft -= szCell[r] + 2; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5271 | cntNew[i-1]--; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5272 | r = cntNew[i-1] - 1; |
| 5273 | d = r + 1 - leafData; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5274 | } |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5275 | szNew[i] = szRight; |
| 5276 | szNew[i-1] = szLeft; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5277 | } |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 5278 | |
| 5279 | /* Either we found one or more cells (cntnew[0])>0) or we are the |
| 5280 | ** a virtual root page. A virtual root page is when the real root |
| 5281 | ** page is page 1 and we are the only child of that page. |
| 5282 | */ |
| 5283 | assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5284 | |
| 5285 | /* |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5286 | ** Allocate k new pages. Reuse old pages where possible. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5287 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5288 | assert( pPage->pgno>1 ); |
| 5289 | pageFlags = pPage->aData[0]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5290 | for(i=0; i<k; i++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5291 | MemPage *pNew; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5292 | if( i<nOld ){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5293 | pNew = apNew[i] = apOld[i]; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5294 | pgnoNew[i] = pgnoOld[i]; |
| 5295 | apOld[i] = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5296 | rc = sqlite3PagerWrite(pNew->pDbPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 5297 | nNew++; |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 5298 | if( rc ) goto balance_cleanup; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5299 | }else{ |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 5300 | assert( i>0 ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5301 | rc = allocateBtreePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5302 | if( rc ) goto balance_cleanup; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5303 | apNew[i] = pNew; |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 5304 | nNew++; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5305 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5306 | } |
| 5307 | |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5308 | /* Free any old pages that were not reused as new pages. |
| 5309 | */ |
| 5310 | while( i<nOld ){ |
| 5311 | rc = freePage(apOld[i]); |
| 5312 | if( rc ) goto balance_cleanup; |
| 5313 | releasePage(apOld[i]); |
| 5314 | apOld[i] = 0; |
| 5315 | i++; |
| 5316 | } |
| 5317 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5318 | /* |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5319 | ** Put the new pages in accending order. This helps to |
| 5320 | ** keep entries in the disk file in order so that a scan |
| 5321 | ** of the table is a linear scan through the file. That |
| 5322 | ** in turn helps the operating system to deliver pages |
| 5323 | ** from the disk more rapidly. |
| 5324 | ** |
| 5325 | ** An O(n^2) insertion sort algorithm is used, but since |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5326 | ** n is never more than NB (a small constant), that should |
| 5327 | ** not be a problem. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5328 | ** |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5329 | ** When NB==3, this one optimization makes the database |
| 5330 | ** about 25% faster for large insertions and deletions. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5331 | */ |
| 5332 | for(i=0; i<k-1; i++){ |
| 5333 | int minV = pgnoNew[i]; |
| 5334 | int minI = i; |
| 5335 | for(j=i+1; j<k; j++){ |
drh | 7d02cb7 | 2003-06-04 16:24:39 +0000 | [diff] [blame] | 5336 | if( pgnoNew[j]<(unsigned)minV ){ |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5337 | minI = j; |
| 5338 | minV = pgnoNew[j]; |
| 5339 | } |
| 5340 | } |
| 5341 | if( minI>i ){ |
| 5342 | int t; |
| 5343 | MemPage *pT; |
| 5344 | t = pgnoNew[i]; |
| 5345 | pT = apNew[i]; |
| 5346 | pgnoNew[i] = pgnoNew[minI]; |
| 5347 | apNew[i] = apNew[minI]; |
| 5348 | pgnoNew[minI] = t; |
| 5349 | apNew[minI] = pT; |
| 5350 | } |
| 5351 | } |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5352 | 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] | 5353 | pgnoOld[0], |
| 5354 | nOld>=2 ? pgnoOld[1] : 0, |
| 5355 | nOld>=3 ? pgnoOld[2] : 0, |
drh | 10c0fa6 | 2004-05-18 12:50:17 +0000 | [diff] [blame] | 5356 | pgnoNew[0], szNew[0], |
| 5357 | nNew>=2 ? pgnoNew[1] : 0, nNew>=2 ? szNew[1] : 0, |
| 5358 | nNew>=3 ? pgnoNew[2] : 0, nNew>=3 ? szNew[2] : 0, |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5359 | nNew>=4 ? pgnoNew[3] : 0, nNew>=4 ? szNew[3] : 0, |
| 5360 | nNew>=5 ? pgnoNew[4] : 0, nNew>=5 ? szNew[4] : 0)); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5361 | |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5362 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5363 | ** Evenly distribute the data in apCell[] across the new pages. |
| 5364 | ** Insert divider cells into pParent as necessary. |
| 5365 | */ |
| 5366 | j = 0; |
| 5367 | for(i=0; i<nNew; i++){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5368 | /* Assemble the new sibling page. */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5369 | MemPage *pNew = apNew[i]; |
drh | 19642e5 | 2005-03-29 13:17:45 +0000 | [diff] [blame] | 5370 | assert( j<nMaxCells ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5371 | assert( pNew->pgno==pgnoNew[i] ); |
drh | 1013148 | 2008-07-11 03:34:09 +0000 | [diff] [blame] | 5372 | zeroPage(pNew, pageFlags); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 5373 | assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]); |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 5374 | assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5375 | assert( pNew->nOverflow==0 ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5376 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5377 | /* If this is an auto-vacuum database, update the pointer map entries |
| 5378 | ** that point to the siblings that were rearranged. These can be: left |
| 5379 | ** children of cells, the right-child of the page, or overflow pages |
| 5380 | ** pointed to by cells. |
| 5381 | */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5382 | if( ISAUTOVACUUM ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5383 | for(k=j; k<cntNew[i]; k++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5384 | assert( k<nMaxCells ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5385 | if( aFrom[k]==0xFF || apCopy[aFrom[k]]->pgno!=pNew->pgno ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5386 | rc = ptrmapPutOvfl(pNew, k-j); |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5387 | if( rc==SQLITE_OK && leafCorrection==0 ){ |
| 5388 | rc = ptrmapPut(pBt, get4byte(apCell[k]), PTRMAP_BTREE, pNew->pgno); |
| 5389 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5390 | if( rc!=SQLITE_OK ){ |
| 5391 | goto balance_cleanup; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5392 | } |
| 5393 | } |
| 5394 | } |
| 5395 | } |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5396 | |
| 5397 | j = cntNew[i]; |
| 5398 | |
| 5399 | /* If the sibling page assembled above was not the right-most sibling, |
| 5400 | ** insert a divider cell into the parent page. |
| 5401 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5402 | if( i<nNew-1 && j<nCell ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5403 | u8 *pCell; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5404 | u8 *pTemp; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5405 | int sz; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5406 | |
| 5407 | assert( j<nMaxCells ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5408 | pCell = apCell[j]; |
| 5409 | sz = szCell[j] + leafCorrection; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5410 | pTemp = &aSpace2[iSpace2]; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5411 | if( !pNew->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5412 | memcpy(&pNew->aData[8], pCell, 4); |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5413 | if( ISAUTOVACUUM |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5414 | && (aFrom[j]==0xFF || apCopy[aFrom[j]]->pgno!=pNew->pgno) |
| 5415 | ){ |
| 5416 | rc = ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno); |
| 5417 | if( rc!=SQLITE_OK ){ |
| 5418 | goto balance_cleanup; |
| 5419 | } |
| 5420 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5421 | }else if( leafData ){ |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 5422 | /* If the tree is a leaf-data tree, and the siblings are leaves, |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5423 | ** then there is no divider cell in apCell[]. Instead, the divider |
| 5424 | ** cell consists of the integer key for the right-most cell of |
| 5425 | ** the sibling-page assembled above only. |
| 5426 | */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5427 | CellInfo info; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5428 | j--; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5429 | sqlite3BtreeParseCellPtr(pNew, apCell[j], &info); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5430 | pCell = pTemp; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5431 | fillInCell(pParent, pCell, 0, info.nKey, 0, 0, 0, &sz); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5432 | pTemp = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5433 | }else{ |
| 5434 | pCell -= 4; |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 5435 | /* Obscure case for non-leaf-data trees: If the cell at pCell was |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 5436 | ** previously stored on a leaf node, and its reported size was 4 |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 5437 | ** bytes, then it may actually be smaller than this |
| 5438 | ** (see sqlite3BtreeParseCellPtr(), 4 bytes is the minimum size of |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 5439 | ** any cell). But it is important to pass the correct size to |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 5440 | ** insertCell(), so reparse the cell now. |
| 5441 | ** |
| 5442 | ** Note that this can never happen in an SQLite data file, as all |
| 5443 | ** cells are at least 4 bytes. It only happens in b-trees used |
| 5444 | ** to evaluate "IN (SELECT ...)" and similar clauses. |
| 5445 | */ |
| 5446 | if( szCell[j]==4 ){ |
| 5447 | assert(leafCorrection==4); |
| 5448 | sz = cellSizePtr(pParent, pCell); |
| 5449 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5450 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5451 | iSpace2 += sz; |
| 5452 | assert( sz<=pBt->pageSize/4 ); |
| 5453 | assert( iSpace2<=pBt->pageSize ); |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 5454 | rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4); |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 5455 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5456 | put4byte(findOverflowCell(pParent,nxDiv), pNew->pgno); |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5457 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5458 | /* If this is an auto-vacuum database, and not a leaf-data tree, |
| 5459 | ** then update the pointer map with an entry for the overflow page |
| 5460 | ** that the cell just inserted points to (if any). |
| 5461 | */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5462 | if( ISAUTOVACUUM && !leafData ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5463 | rc = ptrmapPutOvfl(pParent, nxDiv); |
| 5464 | if( rc!=SQLITE_OK ){ |
| 5465 | goto balance_cleanup; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5466 | } |
| 5467 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5468 | j++; |
| 5469 | nxDiv++; |
| 5470 | } |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5471 | |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5472 | /* Set the pointer-map entry for the new sibling page. */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5473 | if( ISAUTOVACUUM ){ |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5474 | rc = ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno); |
| 5475 | if( rc!=SQLITE_OK ){ |
| 5476 | goto balance_cleanup; |
| 5477 | } |
| 5478 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5479 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5480 | assert( j==nCell ); |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 5481 | assert( nOld>0 ); |
| 5482 | assert( nNew>0 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5483 | if( (pageFlags & PTF_LEAF)==0 ){ |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5484 | u8 *zChild = &apCopy[nOld-1]->aData[8]; |
| 5485 | memcpy(&apNew[nNew-1]->aData[8], zChild, 4); |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5486 | if( ISAUTOVACUUM ){ |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5487 | rc = ptrmapPut(pBt, get4byte(zChild), PTRMAP_BTREE, apNew[nNew-1]->pgno); |
| 5488 | if( rc!=SQLITE_OK ){ |
| 5489 | goto balance_cleanup; |
| 5490 | } |
| 5491 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5492 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5493 | if( nxDiv==pParent->nCell+pParent->nOverflow ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5494 | /* Right-most sibling is the right-most child of pParent */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5495 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew[nNew-1]); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5496 | }else{ |
| 5497 | /* Right-most sibling is the left child of the first entry in pParent |
| 5498 | ** past the right-most divider entry */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5499 | put4byte(findOverflowCell(pParent, nxDiv), pgnoNew[nNew-1]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5500 | } |
| 5501 | |
| 5502 | /* |
| 5503 | ** Reparent children of all cells. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5504 | */ |
| 5505 | for(i=0; i<nNew; i++){ |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5506 | rc = reparentChildPages(apNew[i], 0); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5507 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5508 | } |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5509 | rc = reparentChildPages(pParent, 0); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5510 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5511 | |
| 5512 | /* |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5513 | ** Balance the parent page. Note that the current page (pPage) might |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5514 | ** have been added to the freelist so it might no longer be initialized. |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5515 | ** But the parent page will always be initialized. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5516 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5517 | assert( pParent->isInit ); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5518 | sqlite3ScratchFree(apCell); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5519 | apCell = 0; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5520 | rc = balance(pParent, 0); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5521 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5522 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5523 | ** Cleanup before returning. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5524 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5525 | balance_cleanup: |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5526 | sqlite3PageFree(aSpace2); |
| 5527 | sqlite3ScratchFree(apCell); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5528 | for(i=0; i<nOld; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5529 | releasePage(apOld[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5530 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5531 | for(i=0; i<nNew; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5532 | releasePage(apNew[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5533 | } |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5534 | releasePage(pParent); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5535 | TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n", |
| 5536 | pPage->pgno, nOld, nNew, nCell)); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5537 | return rc; |
| 5538 | } |
| 5539 | |
| 5540 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5541 | ** This routine is called for the root page of a btree when the root |
| 5542 | ** page contains no cells. This is an opportunity to make the tree |
| 5543 | ** shallower by one level. |
| 5544 | */ |
| 5545 | static int balance_shallower(MemPage *pPage){ |
| 5546 | MemPage *pChild; /* The only child page of pPage */ |
| 5547 | Pgno pgnoChild; /* Page number for pChild */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5548 | int rc = SQLITE_OK; /* Return code from subprocedures */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5549 | BtShared *pBt; /* The main BTree structure */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5550 | int mxCellPerPage; /* Maximum number of cells per page */ |
| 5551 | u8 **apCell; /* All cells from pages being balanced */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5552 | u16 *szCell; /* Local size of all cells */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5553 | |
| 5554 | assert( pPage->pParent==0 ); |
| 5555 | assert( pPage->nCell==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5556 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5557 | pBt = pPage->pBt; |
| 5558 | mxCellPerPage = MX_CELL(pBt); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5559 | apCell = sqlite3Malloc( mxCellPerPage*(sizeof(u8*)+sizeof(u16)) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5560 | if( apCell==0 ) return SQLITE_NOMEM; |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5561 | szCell = (u16*)&apCell[mxCellPerPage]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5562 | if( pPage->leaf ){ |
| 5563 | /* The table is completely empty */ |
| 5564 | TRACE(("BALANCE: empty table %d\n", pPage->pgno)); |
| 5565 | }else{ |
| 5566 | /* The root page is empty but has one child. Transfer the |
| 5567 | ** information from that one child into the root page if it |
| 5568 | ** will fit. This reduces the depth of the tree by one. |
| 5569 | ** |
| 5570 | ** If the root page is page 1, it has less space available than |
| 5571 | ** its child (due to the 100 byte header that occurs at the beginning |
| 5572 | ** of the database fle), so it might not be able to hold all of the |
| 5573 | ** information currently contained in the child. If this is the |
| 5574 | ** case, then do not do the transfer. Leave page 1 empty except |
| 5575 | ** for the right-pointer to the child page. The child page becomes |
| 5576 | ** the virtual root of the tree. |
| 5577 | */ |
| 5578 | pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 5579 | assert( pgnoChild>0 ); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 5580 | assert( pgnoChild<=pagerPagecount(pPage->pBt->pPager) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5581 | rc = sqlite3BtreeGetPage(pPage->pBt, pgnoChild, &pChild, 0); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5582 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5583 | if( pPage->pgno==1 ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5584 | rc = sqlite3BtreeInitPage(pChild, pPage); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5585 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5586 | assert( pChild->nOverflow==0 ); |
| 5587 | if( pChild->nFree>=100 ){ |
| 5588 | /* The child information will fit on the root page, so do the |
| 5589 | ** copy */ |
| 5590 | int i; |
| 5591 | zeroPage(pPage, pChild->aData[0]); |
| 5592 | for(i=0; i<pChild->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5593 | apCell[i] = findCell(pChild,i); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5594 | szCell[i] = cellSizePtr(pChild, apCell[i]); |
| 5595 | } |
| 5596 | assemblePage(pPage, pChild->nCell, apCell, szCell); |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 5597 | /* Copy the right-pointer of the child to the parent. */ |
| 5598 | put4byte(&pPage->aData[pPage->hdrOffset+8], |
| 5599 | get4byte(&pChild->aData[pChild->hdrOffset+8])); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5600 | freePage(pChild); |
| 5601 | TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno)); |
| 5602 | }else{ |
| 5603 | /* The child has more information that will fit on the root. |
| 5604 | ** The tree is already balanced. Do nothing. */ |
| 5605 | TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno)); |
| 5606 | } |
| 5607 | }else{ |
| 5608 | memcpy(pPage->aData, pChild->aData, pPage->pBt->usableSize); |
| 5609 | pPage->isInit = 0; |
| 5610 | pPage->pParent = 0; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5611 | rc = sqlite3BtreeInitPage(pPage, 0); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5612 | assert( rc==SQLITE_OK ); |
| 5613 | freePage(pChild); |
| 5614 | TRACE(("BALANCE: transfer child %d into root %d\n", |
| 5615 | pChild->pgno, pPage->pgno)); |
| 5616 | } |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5617 | rc = reparentChildPages(pPage, 1); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5618 | assert( pPage->nOverflow==0 ); |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5619 | if( ISAUTOVACUUM ){ |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 5620 | int i; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5621 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5622 | rc = ptrmapPutOvfl(pPage, i); |
| 5623 | if( rc!=SQLITE_OK ){ |
| 5624 | goto end_shallow_balance; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5625 | } |
| 5626 | } |
| 5627 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5628 | releasePage(pChild); |
| 5629 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5630 | end_shallow_balance: |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5631 | sqlite3_free(apCell); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5632 | return rc; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5633 | } |
| 5634 | |
| 5635 | |
| 5636 | /* |
| 5637 | ** The root page is overfull |
| 5638 | ** |
| 5639 | ** When this happens, Create a new child page and copy the |
| 5640 | ** contents of the root into the child. Then make the root |
| 5641 | ** page an empty page with rightChild pointing to the new |
| 5642 | ** child. Finally, call balance_internal() on the new child |
| 5643 | ** to cause it to split. |
| 5644 | */ |
| 5645 | static int balance_deeper(MemPage *pPage){ |
| 5646 | int rc; /* Return value from subprocedures */ |
| 5647 | MemPage *pChild; /* Pointer to a new child page */ |
| 5648 | Pgno pgnoChild; /* Page number of the new child page */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5649 | BtShared *pBt; /* The BTree */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5650 | int usableSize; /* Total usable size of a page */ |
| 5651 | u8 *data; /* Content of the parent page */ |
| 5652 | u8 *cdata; /* Content of the child page */ |
| 5653 | int hdr; /* Offset to page header in parent */ |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 5654 | int cbrk; /* Offset to content of first cell in parent */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5655 | |
| 5656 | assert( pPage->pParent==0 ); |
| 5657 | assert( pPage->nOverflow>0 ); |
| 5658 | pBt = pPage->pBt; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5659 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5660 | rc = allocateBtreePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5661 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5662 | assert( sqlite3PagerIswriteable(pChild->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5663 | usableSize = pBt->usableSize; |
| 5664 | data = pPage->aData; |
| 5665 | hdr = pPage->hdrOffset; |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 5666 | cbrk = get2byte(&data[hdr+5]); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5667 | cdata = pChild->aData; |
| 5668 | memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr); |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 5669 | memcpy(&cdata[cbrk], &data[cbrk], usableSize-cbrk); |
drh | 1013148 | 2008-07-11 03:34:09 +0000 | [diff] [blame] | 5670 | if( pChild->isInit ) return SQLITE_CORRUPT; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5671 | rc = sqlite3BtreeInitPage(pChild, pPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5672 | if( rc ) goto balancedeeper_out; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5673 | memcpy(pChild->aOvfl, pPage->aOvfl, pPage->nOverflow*sizeof(pPage->aOvfl[0])); |
| 5674 | pChild->nOverflow = pPage->nOverflow; |
| 5675 | if( pChild->nOverflow ){ |
| 5676 | pChild->nFree = 0; |
| 5677 | } |
| 5678 | assert( pChild->nCell==pPage->nCell ); |
| 5679 | zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF); |
| 5680 | put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild); |
| 5681 | TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno)); |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5682 | if( ISAUTOVACUUM ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5683 | int i; |
| 5684 | rc = ptrmapPut(pBt, pChild->pgno, PTRMAP_BTREE, pPage->pgno); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5685 | if( rc ) goto balancedeeper_out; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5686 | for(i=0; i<pChild->nCell; i++){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5687 | rc = ptrmapPutOvfl(pChild, i); |
| 5688 | if( rc!=SQLITE_OK ){ |
danielk1977 | 474b7cc | 2008-07-09 11:49:46 +0000 | [diff] [blame] | 5689 | goto balancedeeper_out; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5690 | } |
| 5691 | } |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5692 | rc = reparentChildPages(pChild, 1); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5693 | } |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5694 | if( rc==SQLITE_OK ){ |
| 5695 | rc = balance_nonroot(pChild); |
| 5696 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5697 | |
| 5698 | balancedeeper_out: |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5699 | releasePage(pChild); |
| 5700 | return rc; |
| 5701 | } |
| 5702 | |
| 5703 | /* |
| 5704 | ** Decide if the page pPage needs to be balanced. If balancing is |
| 5705 | ** required, call the appropriate balancing routine. |
| 5706 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5707 | static int balance(MemPage *pPage, int insert){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5708 | int rc = SQLITE_OK; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5709 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5710 | if( pPage->pParent==0 ){ |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 5711 | rc = sqlite3PagerWrite(pPage->pDbPage); |
| 5712 | if( rc==SQLITE_OK && pPage->nOverflow>0 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5713 | rc = balance_deeper(pPage); |
| 5714 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5715 | if( rc==SQLITE_OK && pPage->nCell==0 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5716 | rc = balance_shallower(pPage); |
| 5717 | } |
| 5718 | }else{ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5719 | if( pPage->nOverflow>0 || |
| 5720 | (!insert && pPage->nFree>pPage->pBt->usableSize*2/3) ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5721 | rc = balance_nonroot(pPage); |
| 5722 | } |
| 5723 | } |
| 5724 | return rc; |
| 5725 | } |
| 5726 | |
| 5727 | /* |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 5728 | ** This routine checks all cursors that point to table pgnoRoot. |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5729 | ** If any of those cursors were opened with wrFlag==0 in a different |
| 5730 | ** database connection (a database connection that shares the pager |
| 5731 | ** cache with the current connection) and that other connection |
| 5732 | ** is not in the ReadUncommmitted state, then this routine returns |
| 5733 | ** SQLITE_LOCKED. |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5734 | ** |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 5735 | ** As well as cursors with wrFlag==0, cursors with wrFlag==1 and |
| 5736 | ** isIncrblobHandle==1 are also considered 'read' cursors. Incremental |
| 5737 | ** blob cursors are used for both reading and writing. |
| 5738 | ** |
| 5739 | ** When pgnoRoot is the root page of an intkey table, this function is also |
| 5740 | ** responsible for invalidating incremental blob cursors when the table row |
| 5741 | ** on which they are opened is deleted or modified. Cursors are invalidated |
| 5742 | ** according to the following rules: |
| 5743 | ** |
| 5744 | ** 1) When BtreeClearTable() is called to completely delete the contents |
| 5745 | ** of a B-Tree table, pExclude is set to zero and parameter iRow is |
| 5746 | ** set to non-zero. In this case all incremental blob cursors open |
| 5747 | ** on the table rooted at pgnoRoot are invalidated. |
| 5748 | ** |
| 5749 | ** 2) When BtreeInsert(), BtreeDelete() or BtreePutData() is called to |
| 5750 | ** modify a table row via an SQL statement, pExclude is set to the |
| 5751 | ** write cursor used to do the modification and parameter iRow is set |
| 5752 | ** to the integer row id of the B-Tree entry being modified. Unless |
| 5753 | ** pExclude is itself an incremental blob cursor, then all incremental |
| 5754 | ** blob cursors open on row iRow of the B-Tree are invalidated. |
| 5755 | ** |
| 5756 | ** 3) If both pExclude and iRow are set to zero, no incremental blob |
| 5757 | ** cursors are invalidated. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5758 | */ |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 5759 | static int checkReadLocks( |
| 5760 | Btree *pBtree, |
| 5761 | Pgno pgnoRoot, |
| 5762 | BtCursor *pExclude, |
| 5763 | i64 iRow |
| 5764 | ){ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5765 | BtCursor *p; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5766 | BtShared *pBt = pBtree->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 5767 | sqlite3 *db = pBtree->db; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5768 | assert( sqlite3BtreeHoldsMutex(pBtree) ); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5769 | for(p=pBt->pCursor; p; p=p->pNext){ |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5770 | if( p==pExclude ) continue; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5771 | if( p->pgnoRoot!=pgnoRoot ) continue; |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 5772 | #ifndef SQLITE_OMIT_INCRBLOB |
| 5773 | if( p->isIncrblobHandle && ( |
| 5774 | (!pExclude && iRow) |
| 5775 | || (pExclude && !pExclude->isIncrblobHandle && p->info.nKey==iRow) |
| 5776 | )){ |
| 5777 | p->eState = CURSOR_INVALID; |
| 5778 | } |
| 5779 | #endif |
| 5780 | if( p->eState!=CURSOR_VALID ) continue; |
| 5781 | if( p->wrFlag==0 |
| 5782 | #ifndef SQLITE_OMIT_INCRBLOB |
| 5783 | || p->isIncrblobHandle |
| 5784 | #endif |
| 5785 | ){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 5786 | sqlite3 *dbOther = p->pBtree->db; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5787 | if( dbOther==0 || |
| 5788 | (dbOther!=db && (dbOther->flags & SQLITE_ReadUncommitted)==0) ){ |
| 5789 | return SQLITE_LOCKED; |
| 5790 | } |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5791 | } |
| 5792 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5793 | return SQLITE_OK; |
| 5794 | } |
| 5795 | |
| 5796 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5797 | ** Insert a new record into the BTree. The key is given by (pKey,nKey) |
| 5798 | ** 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] | 5799 | ** define what table the record should be inserted into. The cursor |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5800 | ** is left pointing at a random location. |
| 5801 | ** |
| 5802 | ** For an INTKEY table, only the nKey value of the key is used. pKey is |
| 5803 | ** ignored. For a ZERODATA table, the pData and nData are both ignored. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5804 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5805 | int sqlite3BtreeInsert( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 5806 | BtCursor *pCur, /* Insert data into the table of this cursor */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 5807 | const void *pKey, i64 nKey, /* The key of the new record */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 5808 | const void *pData, int nData, /* The data of the new record */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5809 | int nZero, /* Number of extra 0 bytes to append to data */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 5810 | int appendBias /* True if this is likely an append */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5811 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5812 | int rc; |
| 5813 | int loc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5814 | int szNew; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5815 | MemPage *pPage; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5816 | Btree *p = pCur->pBtree; |
| 5817 | BtShared *pBt = p->pBt; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5818 | unsigned char *oldCell; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5819 | unsigned char *newCell = 0; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5820 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5821 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5822 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5823 | /* Must start a transaction before doing an insert */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5824 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5825 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5826 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5827 | assert( !pBt->readOnly ); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 5828 | if( !pCur->wrFlag ){ |
| 5829 | return SQLITE_PERM; /* Cursor not open for writing */ |
| 5830 | } |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 5831 | if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur, nKey) ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5832 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 5833 | } |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 5834 | if( pCur->eState==CURSOR_FAULT ){ |
| 5835 | return pCur->skip; |
| 5836 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5837 | |
| 5838 | /* Save the positions of any other cursors open on this table */ |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 5839 | clearCursorPosition(pCur); |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5840 | if( |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5841 | SQLITE_OK!=(rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur)) || |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 5842 | SQLITE_OK!=(rc = sqlite3BtreeMoveto(pCur, pKey, nKey, appendBias, &loc)) |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5843 | ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5844 | return rc; |
| 5845 | } |
| 5846 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5847 | pPage = pCur->pPage; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 5848 | assert( pPage->intKey || nKey>=0 ); |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 5849 | assert( pPage->leaf || !pPage->intKey ); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5850 | TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n", |
| 5851 | pCur->pgnoRoot, nKey, nData, pPage->pgno, |
| 5852 | loc==0 ? "overwrite" : "new entry")); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 5853 | assert( pPage->isInit ); |
danielk1977 | 52ae724 | 2008-03-25 14:24:56 +0000 | [diff] [blame] | 5854 | allocateTempSpace(pBt); |
| 5855 | newCell = pBt->pTmpSpace; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5856 | if( newCell==0 ) return SQLITE_NOMEM; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5857 | rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5858 | if( rc ) goto end_insert; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5859 | assert( szNew==cellSizePtr(pPage, newCell) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5860 | assert( szNew<=MX_CELL_SIZE(pBt) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5861 | if( loc==0 && CURSOR_VALID==pCur->eState ){ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5862 | u16 szOld; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5863 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 5864 | rc = sqlite3PagerWrite(pPage->pDbPage); |
| 5865 | if( rc ){ |
| 5866 | goto end_insert; |
| 5867 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5868 | oldCell = findCell(pPage, pCur->idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5869 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5870 | memcpy(newCell, oldCell, 4); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5871 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5872 | szOld = cellSizePtr(pPage, oldCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5873 | rc = clearCell(pPage, oldCell); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5874 | if( rc ) goto end_insert; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5875 | dropCell(pPage, pCur->idx, szOld); |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 5876 | }else if( loc<0 && pPage->nCell>0 ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5877 | assert( pPage->leaf ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5878 | pCur->idx++; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 5879 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 5880 | pCur->validNKey = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5881 | }else{ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5882 | assert( pPage->leaf ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5883 | } |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 5884 | rc = insertCell(pPage, pCur->idx, newCell, szNew, 0, 0); |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 5885 | if( rc!=SQLITE_OK ) goto end_insert; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5886 | rc = balance(pPage, 1); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5887 | if( rc==SQLITE_OK ){ |
| 5888 | moveToRoot(pCur); |
| 5889 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5890 | end_insert: |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5891 | return rc; |
| 5892 | } |
| 5893 | |
| 5894 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5895 | ** Delete the entry that the cursor is pointing to. The cursor |
| 5896 | ** is left pointing at a random location. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5897 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5898 | int sqlite3BtreeDelete(BtCursor *pCur){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5899 | MemPage *pPage = pCur->pPage; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5900 | unsigned char *pCell; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5901 | int rc; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 5902 | Pgno pgnoChild = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5903 | Btree *p = pCur->pBtree; |
| 5904 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5905 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5906 | assert( cursorHoldsMutex(pCur) ); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 5907 | assert( pPage->isInit ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5908 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5909 | /* Must start a transaction before doing a delete */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5910 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5911 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5912 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5913 | assert( !pBt->readOnly ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 5914 | if( pCur->eState==CURSOR_FAULT ){ |
| 5915 | return pCur->skip; |
| 5916 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 5917 | if( pCur->idx >= pPage->nCell ){ |
| 5918 | return SQLITE_ERROR; /* The cursor is not pointing to anything */ |
| 5919 | } |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 5920 | if( !pCur->wrFlag ){ |
| 5921 | return SQLITE_PERM; /* Did not open this cursor for writing */ |
| 5922 | } |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 5923 | if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur, pCur->info.nKey) ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5924 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 5925 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5926 | |
| 5927 | /* Restore the current cursor position (a no-op if the cursor is not in |
| 5928 | ** CURSOR_REQUIRESEEK state) and save the positions of any other cursors |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5929 | ** open on the same table. Then call sqlite3PagerWrite() on the page |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5930 | ** that the entry will be deleted from. |
| 5931 | */ |
| 5932 | if( |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 5933 | (rc = restoreCursorPosition(pCur))!=0 || |
drh | d116739 | 2006-01-23 13:00:35 +0000 | [diff] [blame] | 5934 | (rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur))!=0 || |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5935 | (rc = sqlite3PagerWrite(pPage->pDbPage))!=0 |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5936 | ){ |
| 5937 | return rc; |
| 5938 | } |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5939 | |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 5940 | /* Locate the cell within its page and leave pCell pointing to the |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5941 | ** data. The clearCell() call frees any overflow pages associated with the |
| 5942 | ** cell. The cell itself is still intact. |
| 5943 | */ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5944 | pCell = findCell(pPage, pCur->idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5945 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5946 | pgnoChild = get4byte(pCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5947 | } |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 5948 | rc = clearCell(pPage, pCell); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5949 | if( rc ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5950 | return rc; |
| 5951 | } |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5952 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5953 | if( !pPage->leaf ){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5954 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5955 | ** 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] | 5956 | ** do something we will leave a hole on an internal page. |
| 5957 | ** We have to fill the hole by moving in a cell from a leaf. The |
| 5958 | ** next Cell after the one to be deleted is guaranteed to exist and |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5959 | ** to be a leaf so we can use it. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5960 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5961 | BtCursor leafCur; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5962 | unsigned char *pNext; |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5963 | int notUsed; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5964 | unsigned char *tempCell = 0; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 5965 | assert( !pPage->intKey ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5966 | sqlite3BtreeGetTempCursor(pCur, &leafCur); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5967 | rc = sqlite3BtreeNext(&leafCur, ¬Used); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5968 | if( rc==SQLITE_OK ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5969 | rc = sqlite3PagerWrite(leafCur.pPage->pDbPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5970 | } |
| 5971 | if( rc==SQLITE_OK ){ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5972 | u16 szNext; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5973 | TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n", |
| 5974 | pCur->pgnoRoot, pPage->pgno, leafCur.pPage->pgno)); |
| 5975 | dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell)); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5976 | pNext = findCell(leafCur.pPage, leafCur.idx); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5977 | szNext = cellSizePtr(leafCur.pPage, pNext); |
| 5978 | assert( MX_CELL_SIZE(pBt)>=szNext+4 ); |
danielk1977 | 52ae724 | 2008-03-25 14:24:56 +0000 | [diff] [blame] | 5979 | allocateTempSpace(pBt); |
| 5980 | tempCell = pBt->pTmpSpace; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5981 | if( tempCell==0 ){ |
| 5982 | rc = SQLITE_NOMEM; |
| 5983 | } |
danielk1977 | 8ea1cfa | 2008-01-01 06:19:02 +0000 | [diff] [blame] | 5984 | if( rc==SQLITE_OK ){ |
| 5985 | rc = insertCell(pPage, pCur->idx, pNext-4, szNext+4, tempCell, 0); |
| 5986 | } |
| 5987 | if( rc==SQLITE_OK ){ |
| 5988 | put4byte(findOverflowCell(pPage, pCur->idx), pgnoChild); |
| 5989 | rc = balance(pPage, 0); |
| 5990 | } |
| 5991 | if( rc==SQLITE_OK ){ |
| 5992 | dropCell(leafCur.pPage, leafCur.idx, szNext); |
| 5993 | rc = balance(leafCur.pPage, 0); |
| 5994 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5995 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5996 | sqlite3BtreeReleaseTempCursor(&leafCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5997 | }else{ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5998 | TRACE(("DELETE: table=%d delete from leaf %d\n", |
| 5999 | pCur->pgnoRoot, pPage->pgno)); |
| 6000 | dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell)); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 6001 | rc = balance(pPage, 0); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 6002 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6003 | if( rc==SQLITE_OK ){ |
| 6004 | moveToRoot(pCur); |
| 6005 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 6006 | return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 6007 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6008 | |
| 6009 | /* |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 6010 | ** Create a new BTree table. Write into *piTable the page |
| 6011 | ** number for the root page of the new table. |
| 6012 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 6013 | ** The type of type is determined by the flags parameter. Only the |
| 6014 | ** following values of flags are currently in use. Other values for |
| 6015 | ** flags might not work: |
| 6016 | ** |
| 6017 | ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys |
| 6018 | ** BTREE_ZERODATA Used for SQL indices |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6019 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6020 | static int btreeCreateTable(Btree *p, int *piTable, int flags){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6021 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6022 | MemPage *pRoot; |
| 6023 | Pgno pgnoRoot; |
| 6024 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6025 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6026 | assert( sqlite3BtreeHoldsMutex(p) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6027 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 6028 | /* Must start a transaction first */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6029 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
| 6030 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6031 | } |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 6032 | assert( !pBt->readOnly ); |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 6033 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6034 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 6035 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6036 | if( rc ){ |
| 6037 | return rc; |
| 6038 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6039 | #else |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6040 | if( pBt->autoVacuum ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6041 | Pgno pgnoMove; /* Move a page here to make room for the root-page */ |
| 6042 | MemPage *pPageMove; /* The page to move to. */ |
| 6043 | |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 6044 | /* Creating a new table may probably require moving an existing database |
| 6045 | ** to make room for the new tables root page. In case this page turns |
| 6046 | ** out to be an overflow page, delete all overflow page-map caches |
| 6047 | ** held by open cursors. |
| 6048 | */ |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 6049 | invalidateAllOverflowCache(pBt); |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 6050 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6051 | /* Read the value of meta[3] from the database to determine where the |
| 6052 | ** root page of the new table should go. meta[3] is the largest root-page |
| 6053 | ** created so far, so the new root-page is (meta[3]+1). |
| 6054 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6055 | rc = sqlite3BtreeGetMeta(p, 4, &pgnoRoot); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6056 | if( rc!=SQLITE_OK ){ |
| 6057 | return rc; |
| 6058 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6059 | pgnoRoot++; |
| 6060 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6061 | /* The new root-page may not be allocated on a pointer-map page, or the |
| 6062 | ** PENDING_BYTE page. |
| 6063 | */ |
drh | 7219043 | 2008-01-31 14:54:43 +0000 | [diff] [blame] | 6064 | while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) || |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6065 | pgnoRoot==PENDING_BYTE_PAGE(pBt) ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6066 | pgnoRoot++; |
| 6067 | } |
| 6068 | assert( pgnoRoot>=3 ); |
| 6069 | |
| 6070 | /* Allocate a page. The page that currently resides at pgnoRoot will |
| 6071 | ** be moved to the allocated page (unless the allocated page happens |
| 6072 | ** to reside at pgnoRoot). |
| 6073 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 6074 | rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6075 | if( rc!=SQLITE_OK ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6076 | return rc; |
| 6077 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6078 | |
| 6079 | if( pgnoMove!=pgnoRoot ){ |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 6080 | /* pgnoRoot is the page that will be used for the root-page of |
| 6081 | ** the new table (assuming an error did not occur). But we were |
| 6082 | ** allocated pgnoMove. If required (i.e. if it was not allocated |
| 6083 | ** by extending the file), the current page at position pgnoMove |
| 6084 | ** is already journaled. |
| 6085 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6086 | u8 eType; |
| 6087 | Pgno iPtrPage; |
| 6088 | |
| 6089 | releasePage(pPageMove); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 6090 | |
| 6091 | /* Move the page currently at pgnoRoot to pgnoMove. */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6092 | rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6093 | if( rc!=SQLITE_OK ){ |
| 6094 | return rc; |
| 6095 | } |
| 6096 | rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage); |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 6097 | if( rc!=SQLITE_OK || eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6098 | releasePage(pRoot); |
| 6099 | return rc; |
| 6100 | } |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 6101 | assert( eType!=PTRMAP_ROOTPAGE ); |
| 6102 | assert( eType!=PTRMAP_FREEPAGE ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6103 | rc = sqlite3PagerWrite(pRoot->pDbPage); |
danielk1977 | 5fd057a | 2005-03-09 13:09:43 +0000 | [diff] [blame] | 6104 | if( rc!=SQLITE_OK ){ |
| 6105 | releasePage(pRoot); |
| 6106 | return rc; |
| 6107 | } |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 6108 | rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6109 | releasePage(pRoot); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 6110 | |
| 6111 | /* Obtain the page at pgnoRoot */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6112 | if( rc!=SQLITE_OK ){ |
| 6113 | return rc; |
| 6114 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6115 | rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6116 | if( rc!=SQLITE_OK ){ |
| 6117 | return rc; |
| 6118 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6119 | rc = sqlite3PagerWrite(pRoot->pDbPage); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6120 | if( rc!=SQLITE_OK ){ |
| 6121 | releasePage(pRoot); |
| 6122 | return rc; |
| 6123 | } |
| 6124 | }else{ |
| 6125 | pRoot = pPageMove; |
| 6126 | } |
| 6127 | |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 6128 | /* Update the pointer-map and meta-data with the new root-page number. */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6129 | rc = ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0); |
| 6130 | if( rc ){ |
| 6131 | releasePage(pRoot); |
| 6132 | return rc; |
| 6133 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6134 | rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6135 | if( rc ){ |
| 6136 | releasePage(pRoot); |
| 6137 | return rc; |
| 6138 | } |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 6139 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6140 | }else{ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 6141 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6142 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6143 | } |
| 6144 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6145 | assert( sqlite3PagerIswriteable(pRoot->pDbPage) ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 6146 | zeroPage(pRoot, flags | PTF_LEAF); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6147 | sqlite3PagerUnref(pRoot->pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6148 | *piTable = (int)pgnoRoot; |
| 6149 | return SQLITE_OK; |
| 6150 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6151 | int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){ |
| 6152 | int rc; |
| 6153 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6154 | p->pBt->db = p->db; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6155 | rc = btreeCreateTable(p, piTable, flags); |
| 6156 | sqlite3BtreeLeave(p); |
| 6157 | return rc; |
| 6158 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6159 | |
| 6160 | /* |
| 6161 | ** Erase the given database page and all its children. Return |
| 6162 | ** the page to the freelist. |
| 6163 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6164 | static int clearDatabasePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6165 | BtShared *pBt, /* The BTree that contains the table */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6166 | Pgno pgno, /* Page number to clear */ |
| 6167 | MemPage *pParent, /* Parent page. NULL for the root */ |
| 6168 | int freePageFlag /* Deallocate page if true */ |
| 6169 | ){ |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6170 | MemPage *pPage = 0; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6171 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6172 | unsigned char *pCell; |
| 6173 | int i; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6174 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6175 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 6176 | if( pgno>pagerPagecount(pBt->pPager) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 6177 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 6178 | } |
| 6179 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 6180 | rc = getAndInitPage(pBt, pgno, &pPage, pParent); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6181 | if( rc ) goto cleardatabasepage_out; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6182 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 6183 | pCell = findCell(pPage, i); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6184 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6185 | rc = clearDatabasePage(pBt, get4byte(pCell), pPage->pParent, 1); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6186 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6187 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6188 | rc = clearCell(pPage, pCell); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6189 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6190 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6191 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6192 | rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), pPage->pParent, 1); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6193 | if( rc ) goto cleardatabasepage_out; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6194 | } |
| 6195 | if( freePageFlag ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6196 | rc = freePage(pPage); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6197 | }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6198 | zeroPage(pPage, pPage->aData[0] | PTF_LEAF); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6199 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6200 | |
| 6201 | cleardatabasepage_out: |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6202 | releasePage(pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6203 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6204 | } |
| 6205 | |
| 6206 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 6207 | ** Delete all information from a single table in the database. iTable is |
| 6208 | ** the page number of the root of the table. After this routine returns, |
| 6209 | ** the root page is empty, but still exists. |
| 6210 | ** |
| 6211 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 6212 | ** read cursors on the table. Open write cursors are moved to the |
| 6213 | ** root of the table. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6214 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6215 | int sqlite3BtreeClearTable(Btree *p, int iTable){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6216 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6217 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6218 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6219 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6220 | if( p->inTrans!=TRANS_WRITE ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6221 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 6222 | }else if( (rc = checkReadLocks(p, iTable, 0, 1))!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6223 | /* nothing to do */ |
| 6224 | }else if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){ |
| 6225 | /* nothing to do */ |
| 6226 | }else{ |
| 6227 | rc = clearDatabasePage(pBt, (Pgno)iTable, 0, 0); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6228 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6229 | sqlite3BtreeLeave(p); |
| 6230 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6231 | } |
| 6232 | |
| 6233 | /* |
| 6234 | ** Erase all information in a table and add the root of the table to |
| 6235 | ** the freelist. Except, the root of the principle table (the one on |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 6236 | ** page 1) is never added to the freelist. |
| 6237 | ** |
| 6238 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 6239 | ** cursors on the table. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6240 | ** |
| 6241 | ** If AUTOVACUUM is enabled and the page at iTable is not the last |
| 6242 | ** root page in the database file, then the last root page |
| 6243 | ** in the database file is moved into the slot formerly occupied by |
| 6244 | ** iTable and that last slot formerly occupied by the last root page |
| 6245 | ** is added to the freelist instead of iTable. In this say, all |
| 6246 | ** root pages are kept at the beginning of the database file, which |
| 6247 | ** is necessary for AUTOVACUUM to work right. *piMoved is set to the |
| 6248 | ** page number that used to be the last root page in the file before |
| 6249 | ** the move. If no page gets moved, *piMoved is set to 0. |
| 6250 | ** The last root page is recorded in meta[3] and the value of |
| 6251 | ** meta[3] is updated by this procedure. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6252 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6253 | static int btreeDropTable(Btree *p, int iTable, int *piMoved){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6254 | int rc; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6255 | MemPage *pPage = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6256 | BtShared *pBt = p->pBt; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6257 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6258 | assert( sqlite3BtreeHoldsMutex(p) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6259 | if( p->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 6260 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6261 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6262 | |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 6263 | /* It is illegal to drop a table if any cursors are open on the |
| 6264 | ** database. This is because in auto-vacuum mode the backend may |
| 6265 | ** need to move another root-page to fill a gap left by the deleted |
| 6266 | ** root page. If an open cursor was using this page a problem would |
| 6267 | ** occur. |
| 6268 | */ |
| 6269 | if( pBt->pCursor ){ |
| 6270 | return SQLITE_LOCKED; |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 6271 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6272 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6273 | rc = sqlite3BtreeGetPage(pBt, (Pgno)iTable, &pPage, 0); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6274 | if( rc ) return rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6275 | rc = sqlite3BtreeClearTable(p, iTable); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6276 | if( rc ){ |
| 6277 | releasePage(pPage); |
| 6278 | return rc; |
| 6279 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6280 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6281 | *piMoved = 0; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6282 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6283 | if( iTable>1 ){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6284 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6285 | rc = freePage(pPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6286 | releasePage(pPage); |
| 6287 | #else |
| 6288 | if( pBt->autoVacuum ){ |
| 6289 | Pgno maxRootPgno; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6290 | rc = sqlite3BtreeGetMeta(p, 4, &maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6291 | if( rc!=SQLITE_OK ){ |
| 6292 | releasePage(pPage); |
| 6293 | return rc; |
| 6294 | } |
| 6295 | |
| 6296 | if( iTable==maxRootPgno ){ |
| 6297 | /* If the table being dropped is the table with the largest root-page |
| 6298 | ** number in the database, put the root page on the free list. |
| 6299 | */ |
| 6300 | rc = freePage(pPage); |
| 6301 | releasePage(pPage); |
| 6302 | if( rc!=SQLITE_OK ){ |
| 6303 | return rc; |
| 6304 | } |
| 6305 | }else{ |
| 6306 | /* The table being dropped does not have the largest root-page |
| 6307 | ** number in the database. So move the page that does into the |
| 6308 | ** gap left by the deleted root-page. |
| 6309 | */ |
| 6310 | MemPage *pMove; |
| 6311 | releasePage(pPage); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6312 | rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6313 | if( rc!=SQLITE_OK ){ |
| 6314 | return rc; |
| 6315 | } |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 6316 | rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6317 | releasePage(pMove); |
| 6318 | if( rc!=SQLITE_OK ){ |
| 6319 | return rc; |
| 6320 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6321 | rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6322 | if( rc!=SQLITE_OK ){ |
| 6323 | return rc; |
| 6324 | } |
| 6325 | rc = freePage(pMove); |
| 6326 | releasePage(pMove); |
| 6327 | if( rc!=SQLITE_OK ){ |
| 6328 | return rc; |
| 6329 | } |
| 6330 | *piMoved = maxRootPgno; |
| 6331 | } |
| 6332 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6333 | /* Set the new 'max-root-page' value in the database header. This |
| 6334 | ** is the old value less one, less one more if that happens to |
| 6335 | ** be a root-page number, less one again if that is the |
| 6336 | ** PENDING_BYTE_PAGE. |
| 6337 | */ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 6338 | maxRootPgno--; |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6339 | if( maxRootPgno==PENDING_BYTE_PAGE(pBt) ){ |
| 6340 | maxRootPgno--; |
| 6341 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6342 | if( maxRootPgno==PTRMAP_PAGENO(pBt, maxRootPgno) ){ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 6343 | maxRootPgno--; |
| 6344 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6345 | assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) ); |
| 6346 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6347 | rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6348 | }else{ |
| 6349 | rc = freePage(pPage); |
| 6350 | releasePage(pPage); |
| 6351 | } |
| 6352 | #endif |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6353 | }else{ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6354 | /* If sqlite3BtreeDropTable was called on page 1. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6355 | zeroPage(pPage, PTF_INTKEY|PTF_LEAF ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6356 | releasePage(pPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6357 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6358 | return rc; |
| 6359 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6360 | int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){ |
| 6361 | int rc; |
| 6362 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6363 | p->pBt->db = p->db; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6364 | rc = btreeDropTable(p, iTable, piMoved); |
| 6365 | sqlite3BtreeLeave(p); |
| 6366 | return rc; |
| 6367 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6368 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 6369 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6370 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6371 | ** Read the meta-information out of a database file. Meta[0] |
| 6372 | ** is the number of free pages currently in the database. Meta[1] |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 6373 | ** through meta[15] are available for use by higher layers. Meta[0] |
| 6374 | ** is read-only, the others are read/write. |
| 6375 | ** |
| 6376 | ** The schema layer numbers meta values differently. At the schema |
| 6377 | ** layer (and the SetCookie and ReadCookie opcodes) the number of |
| 6378 | ** 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] | 6379 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6380 | int sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6381 | DbPage *pDbPage; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6382 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6383 | unsigned char *pP1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6384 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6385 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6386 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6387 | pBt->db = p->db; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6388 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6389 | /* Reading a meta-data value requires a read-lock on page 1 (and hence |
| 6390 | ** the sqlite_master table. We grab this lock regardless of whether or |
| 6391 | ** not the SQLITE_ReadUncommitted flag is set (the table rooted at page |
| 6392 | ** 1 is treated as a special case by queryTableLock() and lockTable()). |
| 6393 | */ |
| 6394 | rc = queryTableLock(p, 1, READ_LOCK); |
| 6395 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6396 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6397 | return rc; |
| 6398 | } |
| 6399 | |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6400 | assert( idx>=0 && idx<=15 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6401 | rc = sqlite3PagerGet(pBt->pPager, 1, &pDbPage); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6402 | if( rc ){ |
| 6403 | sqlite3BtreeLeave(p); |
| 6404 | return rc; |
| 6405 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6406 | pP1 = (unsigned char *)sqlite3PagerGetData(pDbPage); |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6407 | *pMeta = get4byte(&pP1[36 + idx*4]); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6408 | sqlite3PagerUnref(pDbPage); |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 6409 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6410 | /* If autovacuumed is disabled in this build but we are trying to |
| 6411 | ** access an autovacuumed database, then make the database readonly. |
| 6412 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6413 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 6414 | if( idx==4 && *pMeta>0 ) pBt->readOnly = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6415 | #endif |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 6416 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6417 | /* Grab the read-lock on page 1. */ |
| 6418 | rc = lockTable(p, 1, READ_LOCK); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6419 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6420 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6421 | } |
| 6422 | |
| 6423 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6424 | ** Write meta-information back into the database. Meta[0] is |
| 6425 | ** read-only and may not be written. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6426 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6427 | int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){ |
| 6428 | BtShared *pBt = p->pBt; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6429 | unsigned char *pP1; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6430 | int rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6431 | assert( idx>=1 && idx<=15 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6432 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6433 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6434 | if( p->inTrans!=TRANS_WRITE ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6435 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
| 6436 | }else{ |
| 6437 | assert( pBt->pPage1!=0 ); |
| 6438 | pP1 = pBt->pPage1->aData; |
| 6439 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 6440 | if( rc==SQLITE_OK ){ |
| 6441 | put4byte(&pP1[36 + idx*4], iMeta); |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 +0000 | [diff] [blame] | 6442 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6443 | if( idx==7 ){ |
| 6444 | assert( pBt->autoVacuum || iMeta==0 ); |
| 6445 | assert( iMeta==0 || iMeta==1 ); |
| 6446 | pBt->incrVacuum = iMeta; |
| 6447 | } |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 +0000 | [diff] [blame] | 6448 | #endif |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6449 | } |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 6450 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6451 | sqlite3BtreeLeave(p); |
| 6452 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6453 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 6454 | |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 6455 | /* |
| 6456 | ** Return the flag byte at the beginning of the page that the cursor |
| 6457 | ** is currently pointing to. |
| 6458 | */ |
| 6459 | int sqlite3BtreeFlags(BtCursor *pCur){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6460 | /* TODO: What about CURSOR_REQUIRESEEK state? Probably need to call |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 6461 | ** restoreCursorPosition() here. |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6462 | */ |
danielk1977 | e448dc4 | 2008-01-02 11:50:51 +0000 | [diff] [blame] | 6463 | MemPage *pPage; |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 6464 | restoreCursorPosition(pCur); |
danielk1977 | e448dc4 | 2008-01-02 11:50:51 +0000 | [diff] [blame] | 6465 | pPage = pCur->pPage; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6466 | assert( cursorHoldsMutex(pCur) ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 6467 | assert( pPage->pBt==pCur->pBt ); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 6468 | return pPage ? pPage->aData[pPage->hdrOffset] : 0; |
| 6469 | } |
| 6470 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6471 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6472 | /* |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6473 | ** Return the pager associated with a BTree. This routine is used for |
| 6474 | ** testing and debugging only. |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6475 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6476 | Pager *sqlite3BtreePager(Btree *p){ |
| 6477 | return p->pBt->pPager; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6478 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6479 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6480 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6481 | /* |
| 6482 | ** Append a message to the error message string. |
| 6483 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6484 | static void checkAppendMsg( |
| 6485 | IntegrityCk *pCheck, |
| 6486 | char *zMsg1, |
| 6487 | const char *zFormat, |
| 6488 | ... |
| 6489 | ){ |
| 6490 | va_list ap; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6491 | if( !pCheck->mxErr ) return; |
| 6492 | pCheck->mxErr--; |
| 6493 | pCheck->nErr++; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6494 | va_start(ap, zFormat); |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 6495 | if( pCheck->errMsg.nChar ){ |
| 6496 | sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6497 | } |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 6498 | if( zMsg1 ){ |
| 6499 | sqlite3StrAccumAppend(&pCheck->errMsg, zMsg1, -1); |
| 6500 | } |
| 6501 | sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap); |
| 6502 | va_end(ap); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 6503 | if( pCheck->errMsg.mallocFailed ){ |
| 6504 | pCheck->mallocFailed = 1; |
| 6505 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6506 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6507 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6508 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6509 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6510 | /* |
| 6511 | ** Add 1 to the reference count for page iPage. If this is the second |
| 6512 | ** reference to the page, add an error message to pCheck->zErrMsg. |
| 6513 | ** Return 1 if there are 2 ore more references to the page and 0 if |
| 6514 | ** if this is the first reference to the page. |
| 6515 | ** |
| 6516 | ** Also check that the page number is in bounds. |
| 6517 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 6518 | static int checkRef(IntegrityCk *pCheck, int iPage, char *zContext){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6519 | if( iPage==0 ) return 1; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 6520 | if( iPage>pCheck->nPage || iPage<0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6521 | checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6522 | return 1; |
| 6523 | } |
| 6524 | if( pCheck->anRef[iPage]==1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6525 | checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6526 | return 1; |
| 6527 | } |
| 6528 | return (pCheck->anRef[iPage]++)>1; |
| 6529 | } |
| 6530 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6531 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6532 | /* |
| 6533 | ** Check that the entry in the pointer-map for page iChild maps to |
| 6534 | ** page iParent, pointer type ptrType. If not, append an error message |
| 6535 | ** to pCheck. |
| 6536 | */ |
| 6537 | static void checkPtrmap( |
| 6538 | IntegrityCk *pCheck, /* Integrity check context */ |
| 6539 | Pgno iChild, /* Child page number */ |
| 6540 | u8 eType, /* Expected pointer map type */ |
| 6541 | Pgno iParent, /* Expected pointer map parent page number */ |
| 6542 | char *zContext /* Context description (used for error msg) */ |
| 6543 | ){ |
| 6544 | int rc; |
| 6545 | u8 ePtrmapType; |
| 6546 | Pgno iPtrmapParent; |
| 6547 | |
| 6548 | rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent); |
| 6549 | if( rc!=SQLITE_OK ){ |
| 6550 | checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild); |
| 6551 | return; |
| 6552 | } |
| 6553 | |
| 6554 | if( ePtrmapType!=eType || iPtrmapParent!=iParent ){ |
| 6555 | checkAppendMsg(pCheck, zContext, |
| 6556 | "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", |
| 6557 | iChild, eType, iParent, ePtrmapType, iPtrmapParent); |
| 6558 | } |
| 6559 | } |
| 6560 | #endif |
| 6561 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6562 | /* |
| 6563 | ** Check the integrity of the freelist or of an overflow page list. |
| 6564 | ** Verify that the number of pages on the list is N. |
| 6565 | */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6566 | static void checkList( |
| 6567 | IntegrityCk *pCheck, /* Integrity checking context */ |
| 6568 | int isFreeList, /* True for a freelist. False for overflow page list */ |
| 6569 | int iPage, /* Page number for first page in the list */ |
| 6570 | int N, /* Expected number of pages in the list */ |
| 6571 | char *zContext /* Context for error messages */ |
| 6572 | ){ |
| 6573 | int i; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6574 | int expected = N; |
| 6575 | int iFirst = iPage; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6576 | while( N-- > 0 && pCheck->mxErr ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6577 | DbPage *pOvflPage; |
| 6578 | unsigned char *pOvflData; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6579 | if( iPage<1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6580 | checkAppendMsg(pCheck, zContext, |
| 6581 | "%d of %d pages missing from overflow list starting at %d", |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6582 | N+1, expected, iFirst); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6583 | break; |
| 6584 | } |
| 6585 | if( checkRef(pCheck, iPage, zContext) ) break; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6586 | if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6587 | checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6588 | break; |
| 6589 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6590 | pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6591 | if( isFreeList ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6592 | int n = get4byte(&pOvflData[4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6593 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6594 | if( pCheck->pBt->autoVacuum ){ |
| 6595 | checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext); |
| 6596 | } |
| 6597 | #endif |
drh | 45b1fac | 2008-07-04 17:52:42 +0000 | [diff] [blame] | 6598 | if( n>pCheck->pBt->usableSize/4-2 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6599 | checkAppendMsg(pCheck, zContext, |
| 6600 | "freelist leaf count too big on page %d", iPage); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 6601 | N--; |
| 6602 | }else{ |
| 6603 | for(i=0; i<n; i++){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6604 | Pgno iFreePage = get4byte(&pOvflData[8+i*4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6605 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6606 | if( pCheck->pBt->autoVacuum ){ |
| 6607 | checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext); |
| 6608 | } |
| 6609 | #endif |
| 6610 | checkRef(pCheck, iFreePage, zContext); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 6611 | } |
| 6612 | N -= n; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6613 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6614 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6615 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6616 | else{ |
| 6617 | /* If this database supports auto-vacuum and iPage is not the last |
| 6618 | ** page in this overflow list, check that the pointer-map entry for |
| 6619 | ** the following page matches iPage. |
| 6620 | */ |
| 6621 | if( pCheck->pBt->autoVacuum && N>0 ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6622 | i = get4byte(pOvflData); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6623 | checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext); |
| 6624 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6625 | } |
| 6626 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6627 | iPage = get4byte(pOvflData); |
| 6628 | sqlite3PagerUnref(pOvflPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6629 | } |
| 6630 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6631 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6632 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6633 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6634 | /* |
| 6635 | ** Do various sanity checks on a single page of a tree. Return |
| 6636 | ** the tree depth. Root pages return 0. Parents of root pages |
| 6637 | ** return 1, and so forth. |
| 6638 | ** |
| 6639 | ** These checks are done: |
| 6640 | ** |
| 6641 | ** 1. Make sure that cells and freeblocks do not overlap |
| 6642 | ** but combine to completely cover the page. |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6643 | ** NO 2. Make sure cell keys are in order. |
| 6644 | ** NO 3. Make sure no key is less than or equal to zLowerBound. |
| 6645 | ** NO 4. Make sure no key is greater than or equal to zUpperBound. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6646 | ** 5. Check the integrity of overflow pages. |
| 6647 | ** 6. Recursively call checkTreePage on all children. |
| 6648 | ** 7. Verify that the depth of all children is the same. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6649 | ** 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] | 6650 | ** the root of the tree. |
| 6651 | */ |
| 6652 | static int checkTreePage( |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 6653 | IntegrityCk *pCheck, /* Context for the sanity check */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6654 | int iPage, /* Page number of the page to check */ |
| 6655 | MemPage *pParent, /* Parent page */ |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6656 | char *zParentContext /* Parent context */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6657 | ){ |
| 6658 | MemPage *pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6659 | int i, rc, depth, d2, pgno, cnt; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6660 | int hdr, cellStart; |
| 6661 | int nCell; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6662 | u8 *data; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6663 | BtShared *pBt; |
drh | 4f26bb6 | 2005-09-08 14:17:20 +0000 | [diff] [blame] | 6664 | int usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6665 | char zContext[100]; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6666 | char *hit; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6667 | |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6668 | sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage); |
danielk1977 | ef73ee9 | 2004-11-06 12:26:07 +0000 | [diff] [blame] | 6669 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6670 | /* Check that the page exists |
| 6671 | */ |
drh | d9cb6ac | 2005-10-20 07:28:17 +0000 | [diff] [blame] | 6672 | pBt = pCheck->pBt; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 6673 | usableSize = pBt->usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6674 | if( iPage==0 ) return 0; |
| 6675 | if( checkRef(pCheck, iPage, zParentContext) ) return 0; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6676 | if( (rc = sqlite3BtreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6677 | checkAppendMsg(pCheck, zContext, |
| 6678 | "unable to get the page. error code=%d", rc); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6679 | return 0; |
| 6680 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6681 | if( (rc = sqlite3BtreeInitPage(pPage, pParent))!=0 ){ |
| 6682 | checkAppendMsg(pCheck, zContext, |
| 6683 | "sqlite3BtreeInitPage() returns error code %d", rc); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 6684 | releasePage(pPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6685 | return 0; |
| 6686 | } |
| 6687 | |
| 6688 | /* Check out all the cells. |
| 6689 | */ |
| 6690 | depth = 0; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6691 | for(i=0; i<pPage->nCell && pCheck->mxErr; i++){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 6692 | u8 *pCell; |
| 6693 | int sz; |
| 6694 | CellInfo info; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6695 | |
| 6696 | /* Check payload overflow pages |
| 6697 | */ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6698 | sqlite3_snprintf(sizeof(zContext), zContext, |
| 6699 | "On tree page %d cell %d: ", iPage, i); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 6700 | pCell = findCell(pPage,i); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6701 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 6702 | sz = info.nData; |
| 6703 | if( !pPage->intKey ) sz += info.nKey; |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 6704 | assert( sz==info.nPayload ); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 6705 | if( sz>info.nLocal ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 6706 | int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6707 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
| 6708 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6709 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6710 | checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6711 | } |
| 6712 | #endif |
| 6713 | checkList(pCheck, 0, pgnoOvfl, nPage, zContext); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6714 | } |
| 6715 | |
| 6716 | /* Check sanity of left child page. |
| 6717 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6718 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6719 | pgno = get4byte(pCell); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6720 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6721 | if( pBt->autoVacuum ){ |
| 6722 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext); |
| 6723 | } |
| 6724 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6725 | d2 = checkTreePage(pCheck,pgno,pPage,zContext); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6726 | if( i>0 && d2!=depth ){ |
| 6727 | checkAppendMsg(pCheck, zContext, "Child page depth differs"); |
| 6728 | } |
| 6729 | depth = d2; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6730 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6731 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6732 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6733 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6734 | sqlite3_snprintf(sizeof(zContext), zContext, |
| 6735 | "On page %d at right child: ", iPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6736 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6737 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6738 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6739 | } |
| 6740 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6741 | checkTreePage(pCheck, pgno, pPage, zContext); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6742 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6743 | |
| 6744 | /* Check for complete coverage of the page |
| 6745 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6746 | data = pPage->aData; |
| 6747 | hdr = pPage->hdrOffset; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 6748 | hit = sqlite3PageMalloc( pBt->pageSize ); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 6749 | if( hit==0 ){ |
| 6750 | pCheck->mallocFailed = 1; |
| 6751 | }else{ |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 6752 | memset(hit, 0, usableSize ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6753 | memset(hit, 1, get2byte(&data[hdr+5])); |
| 6754 | nCell = get2byte(&data[hdr+3]); |
| 6755 | cellStart = hdr + 12 - 4*pPage->leaf; |
| 6756 | for(i=0; i<nCell; i++){ |
| 6757 | int pc = get2byte(&data[cellStart+i*2]); |
danielk1977 | daca543 | 2008-08-25 11:57:16 +0000 | [diff] [blame] | 6758 | u16 size = 1024; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6759 | int j; |
danielk1977 | daca543 | 2008-08-25 11:57:16 +0000 | [diff] [blame] | 6760 | if( pc<=usableSize ){ |
| 6761 | size = cellSizePtr(pPage, &data[pc]); |
| 6762 | } |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 6763 | if( (pc+size-1)>=usableSize || pc<0 ){ |
| 6764 | checkAppendMsg(pCheck, 0, |
| 6765 | "Corruption detected in cell %d on page %d",i,iPage,0); |
| 6766 | }else{ |
| 6767 | for(j=pc+size-1; j>=pc; j--) hit[j]++; |
| 6768 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6769 | } |
| 6770 | for(cnt=0, i=get2byte(&data[hdr+1]); i>0 && i<usableSize && cnt<10000; |
| 6771 | cnt++){ |
| 6772 | int size = get2byte(&data[i+2]); |
| 6773 | int j; |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 6774 | if( (i+size-1)>=usableSize || i<0 ){ |
| 6775 | checkAppendMsg(pCheck, 0, |
| 6776 | "Corruption detected in cell %d on page %d",i,iPage,0); |
| 6777 | }else{ |
| 6778 | for(j=i+size-1; j>=i; j--) hit[j]++; |
| 6779 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6780 | i = get2byte(&data[i]); |
| 6781 | } |
| 6782 | for(i=cnt=0; i<usableSize; i++){ |
| 6783 | if( hit[i]==0 ){ |
| 6784 | cnt++; |
| 6785 | }else if( hit[i]>1 ){ |
| 6786 | checkAppendMsg(pCheck, 0, |
| 6787 | "Multiple uses for byte %d of page %d", i, iPage); |
| 6788 | break; |
| 6789 | } |
| 6790 | } |
| 6791 | if( cnt!=data[hdr+7] ){ |
| 6792 | checkAppendMsg(pCheck, 0, |
| 6793 | "Fragmented space is %d byte reported as %d on page %d", |
| 6794 | cnt, data[hdr+7], iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6795 | } |
| 6796 | } |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 6797 | sqlite3PageFree(hit); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6798 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6799 | releasePage(pPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6800 | return depth+1; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6801 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6802 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6803 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6804 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6805 | /* |
| 6806 | ** This routine does a complete check of the given BTree file. aRoot[] is |
| 6807 | ** an array of pages numbers were each page number is the root page of |
| 6808 | ** a table. nRoot is the number of entries in aRoot. |
| 6809 | ** |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 6810 | ** Write the number of error seen in *pnErr. Except for some memory |
| 6811 | ** allocation errors, nn error message is held in memory obtained from |
| 6812 | ** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is |
| 6813 | ** returned. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6814 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6815 | char *sqlite3BtreeIntegrityCheck( |
| 6816 | Btree *p, /* The btree to be checked */ |
| 6817 | int *aRoot, /* An array of root pages numbers for individual trees */ |
| 6818 | int nRoot, /* Number of entries in aRoot[] */ |
| 6819 | int mxErr, /* Stop reporting errors after this many */ |
| 6820 | int *pnErr /* Write number of errors seen to this variable */ |
| 6821 | ){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6822 | int i; |
| 6823 | int nRef; |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 6824 | IntegrityCk sCheck; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6825 | BtShared *pBt = p->pBt; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 6826 | char zErr[100]; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6827 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6828 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6829 | pBt->db = p->db; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6830 | nRef = sqlite3PagerRefcount(pBt->pPager); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6831 | if( lockBtreeWithRetry(p)!=SQLITE_OK ){ |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 6832 | *pnErr = 1; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6833 | sqlite3BtreeLeave(p); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 6834 | return sqlite3DbStrDup(0, "cannot acquire a read lock on the database"); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 6835 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6836 | sCheck.pBt = pBt; |
| 6837 | sCheck.pPager = pBt->pPager; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 6838 | sCheck.nPage = pagerPagecount(sCheck.pPager); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6839 | sCheck.mxErr = mxErr; |
| 6840 | sCheck.nErr = 0; |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 6841 | sCheck.mallocFailed = 0; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6842 | *pnErr = 0; |
danielk1977 | e5321f0 | 2007-04-27 07:05:44 +0000 | [diff] [blame] | 6843 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6844 | if( pBt->nTrunc!=0 ){ |
| 6845 | sCheck.nPage = pBt->nTrunc; |
| 6846 | } |
| 6847 | #endif |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 6848 | if( sCheck.nPage==0 ){ |
| 6849 | unlockBtreeIfUnused(pBt); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6850 | sqlite3BtreeLeave(p); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 6851 | return 0; |
| 6852 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 6853 | sCheck.anRef = sqlite3Malloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) ); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 6854 | if( !sCheck.anRef ){ |
| 6855 | unlockBtreeIfUnused(pBt); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6856 | *pnErr = 1; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6857 | sqlite3BtreeLeave(p); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 6858 | return 0; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 6859 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6860 | for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; } |
drh | 42cac6d | 2004-11-20 20:31:11 +0000 | [diff] [blame] | 6861 | i = PENDING_BYTE_PAGE(pBt); |
drh | 1f59571 | 2004-06-15 01:40:29 +0000 | [diff] [blame] | 6862 | if( i<=sCheck.nPage ){ |
| 6863 | sCheck.anRef[i] = 1; |
| 6864 | } |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 6865 | sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), 20000); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6866 | |
| 6867 | /* Check the integrity of the freelist |
| 6868 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6869 | checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), |
| 6870 | get4byte(&pBt->pPage1->aData[36]), "Main freelist: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6871 | |
| 6872 | /* Check all the tables. |
| 6873 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6874 | for(i=0; i<nRoot && sCheck.mxErr; i++){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 6875 | if( aRoot[i]==0 ) continue; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6876 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6877 | if( pBt->autoVacuum && aRoot[i]>1 ){ |
| 6878 | checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0); |
| 6879 | } |
| 6880 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6881 | checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6882 | } |
| 6883 | |
| 6884 | /* Make sure every page in the file is referenced |
| 6885 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6886 | for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6887 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6888 | if( sCheck.anRef[i]==0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6889 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6890 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6891 | #else |
| 6892 | /* If the database supports auto-vacuum, make sure no tables contain |
| 6893 | ** references to pointer-map pages. |
| 6894 | */ |
| 6895 | if( sCheck.anRef[i]==0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6896 | (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6897 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
| 6898 | } |
| 6899 | if( sCheck.anRef[i]!=0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6900 | (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6901 | checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i); |
| 6902 | } |
| 6903 | #endif |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6904 | } |
| 6905 | |
| 6906 | /* Make sure this analysis did not leave any unref() pages |
| 6907 | */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6908 | unlockBtreeIfUnused(pBt); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6909 | if( nRef != sqlite3PagerRefcount(pBt->pPager) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6910 | checkAppendMsg(&sCheck, 0, |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6911 | "Outstanding page count goes from %d to %d during this analysis", |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6912 | nRef, sqlite3PagerRefcount(pBt->pPager) |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6913 | ); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6914 | } |
| 6915 | |
| 6916 | /* Clean up and report errors. |
| 6917 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6918 | sqlite3BtreeLeave(p); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6919 | sqlite3_free(sCheck.anRef); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 6920 | if( sCheck.mallocFailed ){ |
| 6921 | sqlite3StrAccumReset(&sCheck.errMsg); |
| 6922 | *pnErr = sCheck.nErr+1; |
| 6923 | return 0; |
| 6924 | } |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6925 | *pnErr = sCheck.nErr; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 6926 | if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg); |
| 6927 | return sqlite3StrAccumFinish(&sCheck.errMsg); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6928 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6929 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 6930 | |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6931 | /* |
| 6932 | ** Return the full pathname of the underlying database file. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 6933 | ** |
| 6934 | ** The pager filename is invariant as long as the pager is |
| 6935 | ** open so it is safe to access without the BtShared mutex. |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6936 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6937 | const char *sqlite3BtreeGetFilename(Btree *p){ |
| 6938 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6939 | return sqlite3PagerFilename(p->pBt->pPager); |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6940 | } |
| 6941 | |
| 6942 | /* |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6943 | ** Return the pathname of the directory that contains the database file. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 6944 | ** |
| 6945 | ** The pager directory name is invariant as long as the pager is |
| 6946 | ** open so it is safe to access without the BtShared mutex. |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6947 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6948 | const char *sqlite3BtreeGetDirname(Btree *p){ |
| 6949 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6950 | return sqlite3PagerDirname(p->pBt->pPager); |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6951 | } |
| 6952 | |
| 6953 | /* |
| 6954 | ** Return the pathname of the journal file for this database. The return |
| 6955 | ** value of this routine is the same regardless of whether the journal file |
| 6956 | ** has been created or not. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 6957 | ** |
| 6958 | ** The pager journal filename is invariant as long as the pager is |
| 6959 | ** open so it is safe to access without the BtShared mutex. |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6960 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6961 | const char *sqlite3BtreeGetJournalname(Btree *p){ |
| 6962 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6963 | return sqlite3PagerJournalname(p->pBt->pPager); |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6964 | } |
| 6965 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6966 | #ifndef SQLITE_OMIT_VACUUM |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6967 | /* |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6968 | ** Copy the complete content of pBtFrom into pBtTo. A transaction |
| 6969 | ** must be active for both files. |
| 6970 | ** |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 6971 | ** The size of file pTo may be reduced by this operation. |
| 6972 | ** If anything goes wrong, the transaction on pTo is rolled back. |
| 6973 | ** |
| 6974 | ** If successful, CommitPhaseOne() may be called on pTo before returning. |
| 6975 | ** The caller should finish committing the transaction on pTo by calling |
| 6976 | ** sqlite3BtreeCommit(). |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6977 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6978 | static int btreeCopyFile(Btree *pTo, Btree *pFrom){ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6979 | int rc = SQLITE_OK; |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 6980 | Pgno i; |
| 6981 | |
| 6982 | Pgno nFromPage; /* Number of pages in pFrom */ |
| 6983 | Pgno nToPage; /* Number of pages in pTo */ |
| 6984 | Pgno nNewPage; /* Number of pages in pTo after the copy */ |
| 6985 | |
| 6986 | Pgno iSkip; /* Pending byte page in pTo */ |
| 6987 | int nToPageSize; /* Page size of pTo in bytes */ |
| 6988 | int nFromPageSize; /* Page size of pFrom in bytes */ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6989 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6990 | BtShared *pBtTo = pTo->pBt; |
| 6991 | BtShared *pBtFrom = pFrom->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6992 | pBtTo->db = pTo->db; |
| 6993 | pBtFrom->db = pFrom->db; |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 6994 | |
| 6995 | nToPageSize = pBtTo->pageSize; |
| 6996 | nFromPageSize = pBtFrom->pageSize; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6997 | |
| 6998 | if( pTo->inTrans!=TRANS_WRITE || pFrom->inTrans!=TRANS_WRITE ){ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 6999 | return SQLITE_ERROR; |
| 7000 | } |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7001 | if( pBtTo->pCursor ){ |
| 7002 | return SQLITE_BUSY; |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 7003 | } |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 7004 | |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 7005 | nToPage = pagerPagecount(pBtTo->pPager); |
| 7006 | nFromPage = pagerPagecount(pBtFrom->pPager); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7007 | iSkip = PENDING_BYTE_PAGE(pBtTo); |
| 7008 | |
| 7009 | /* Variable nNewPage is the number of pages required to store the |
| 7010 | ** contents of pFrom using the current page-size of pTo. |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 7011 | */ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7012 | nNewPage = ((i64)nFromPage * (i64)nFromPageSize + (i64)nToPageSize - 1) / |
| 7013 | (i64)nToPageSize; |
| 7014 | |
| 7015 | for(i=1; rc==SQLITE_OK && (i<=nToPage || i<=nNewPage); i++){ |
| 7016 | |
| 7017 | /* Journal the original page. |
| 7018 | ** |
| 7019 | ** iSkip is the page number of the locking page (PENDING_BYTE_PAGE) |
| 7020 | ** in database *pTo (before the copy). This page is never written |
| 7021 | ** into the journal file. Unless i==iSkip or the page was not |
| 7022 | ** present in pTo before the copy operation, journal page i from pTo. |
| 7023 | */ |
| 7024 | if( i!=iSkip && i<=nToPage ){ |
danielk1977 | 4abd544 | 2008-05-05 15:26:50 +0000 | [diff] [blame] | 7025 | DbPage *pDbPage = 0; |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7026 | rc = sqlite3PagerGet(pBtTo->pPager, i, &pDbPage); |
danielk1977 | 4abd544 | 2008-05-05 15:26:50 +0000 | [diff] [blame] | 7027 | if( rc==SQLITE_OK ){ |
| 7028 | rc = sqlite3PagerWrite(pDbPage); |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 7029 | if( rc==SQLITE_OK && i>nFromPage ){ |
| 7030 | /* Yeah. It seems wierd to call DontWrite() right after Write(). But |
| 7031 | ** that is because the names of those procedures do not exactly |
| 7032 | ** represent what they do. Write() really means "put this page in the |
| 7033 | ** rollback journal and mark it as dirty so that it will be written |
| 7034 | ** to the database file later." DontWrite() undoes the second part of |
| 7035 | ** that and prevents the page from being written to the database. The |
| 7036 | ** page is still on the rollback journal, though. And that is the |
| 7037 | ** whole point of this block: to put pages on the rollback journal. |
| 7038 | */ |
danielk1977 | a1fa00d | 2008-08-27 15:16:33 +0000 | [diff] [blame] | 7039 | rc = sqlite3PagerDontWrite(pDbPage); |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 7040 | } |
| 7041 | sqlite3PagerUnref(pDbPage); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7042 | } |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7043 | } |
| 7044 | |
| 7045 | /* Overwrite the data in page i of the target database */ |
| 7046 | if( rc==SQLITE_OK && i!=iSkip && i<=nNewPage ){ |
| 7047 | |
| 7048 | DbPage *pToPage = 0; |
| 7049 | sqlite3_int64 iOff; |
| 7050 | |
| 7051 | rc = sqlite3PagerGet(pBtTo->pPager, i, &pToPage); |
| 7052 | if( rc==SQLITE_OK ){ |
| 7053 | rc = sqlite3PagerWrite(pToPage); |
| 7054 | } |
| 7055 | |
| 7056 | for( |
| 7057 | iOff=(i-1)*nToPageSize; |
| 7058 | rc==SQLITE_OK && iOff<i*nToPageSize; |
| 7059 | iOff += nFromPageSize |
| 7060 | ){ |
| 7061 | DbPage *pFromPage = 0; |
| 7062 | Pgno iFrom = (iOff/nFromPageSize)+1; |
| 7063 | |
| 7064 | if( iFrom==PENDING_BYTE_PAGE(pBtFrom) ){ |
| 7065 | continue; |
| 7066 | } |
| 7067 | |
| 7068 | rc = sqlite3PagerGet(pBtFrom->pPager, iFrom, &pFromPage); |
| 7069 | if( rc==SQLITE_OK ){ |
| 7070 | char *zTo = sqlite3PagerGetData(pToPage); |
| 7071 | char *zFrom = sqlite3PagerGetData(pFromPage); |
| 7072 | int nCopy; |
| 7073 | |
| 7074 | if( nFromPageSize>=nToPageSize ){ |
| 7075 | zFrom += ((i-1)*nToPageSize - ((iFrom-1)*nFromPageSize)); |
| 7076 | nCopy = nToPageSize; |
| 7077 | }else{ |
| 7078 | zTo += (((iFrom-1)*nFromPageSize) - (i-1)*nToPageSize); |
| 7079 | nCopy = nFromPageSize; |
| 7080 | } |
| 7081 | |
| 7082 | memcpy(zTo, zFrom, nCopy); |
| 7083 | sqlite3PagerUnref(pFromPage); |
| 7084 | } |
| 7085 | } |
| 7086 | |
| 7087 | if( pToPage ) sqlite3PagerUnref(pToPage); |
| 7088 | } |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 7089 | } |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7090 | |
| 7091 | /* If things have worked so far, the database file may need to be |
| 7092 | ** truncated. The complex part is that it may need to be truncated to |
| 7093 | ** a size that is not an integer multiple of nToPageSize - the current |
| 7094 | ** page size used by the pager associated with B-Tree pTo. |
| 7095 | ** |
| 7096 | ** For example, say the page-size of pTo is 2048 bytes and the original |
| 7097 | ** number of pages is 5 (10 KB file). If pFrom has a page size of 1024 |
| 7098 | ** bytes and 9 pages, then the file needs to be truncated to 9KB. |
| 7099 | */ |
| 7100 | if( rc==SQLITE_OK ){ |
| 7101 | if( nFromPageSize!=nToPageSize ){ |
| 7102 | sqlite3_file *pFile = sqlite3PagerFile(pBtTo->pPager); |
| 7103 | i64 iSize = (i64)nFromPageSize * (i64)nFromPage; |
| 7104 | i64 iNow = (i64)((nToPage>nNewPage)?nToPage:nNewPage) * (i64)nToPageSize; |
| 7105 | i64 iPending = ((i64)PENDING_BYTE_PAGE(pBtTo)-1) *(i64)nToPageSize; |
| 7106 | |
| 7107 | assert( iSize<=iNow ); |
| 7108 | |
| 7109 | /* Commit phase one syncs the journal file associated with pTo |
| 7110 | ** containing the original data. It does not sync the database file |
| 7111 | ** itself. After doing this it is safe to use OsTruncate() and other |
| 7112 | ** file APIs on the database file directly. |
| 7113 | */ |
| 7114 | pBtTo->db = pTo->db; |
| 7115 | rc = sqlite3PagerCommitPhaseOne(pBtTo->pPager, 0, 0, 1); |
| 7116 | if( iSize<iNow && rc==SQLITE_OK ){ |
| 7117 | rc = sqlite3OsTruncate(pFile, iSize); |
| 7118 | } |
| 7119 | |
| 7120 | /* The loop that copied data from database pFrom to pTo did not |
| 7121 | ** populate the locking page of database pTo. If the page-size of |
| 7122 | ** pFrom is smaller than that of pTo, this means some data will |
| 7123 | ** not have been copied. |
| 7124 | ** |
| 7125 | ** This block copies the missing data from database pFrom to pTo |
| 7126 | ** using file APIs. This is safe because at this point we know that |
| 7127 | ** all of the original data from pTo has been synced into the |
| 7128 | ** journal file. At this point it would be safe to do anything at |
| 7129 | ** all to the database file except truncate it to zero bytes. |
| 7130 | */ |
| 7131 | if( rc==SQLITE_OK && nFromPageSize<nToPageSize && iSize>iPending){ |
| 7132 | i64 iOff; |
| 7133 | for( |
| 7134 | iOff=iPending; |
| 7135 | rc==SQLITE_OK && iOff<(iPending+nToPageSize); |
| 7136 | iOff += nFromPageSize |
| 7137 | ){ |
| 7138 | DbPage *pFromPage = 0; |
| 7139 | Pgno iFrom = (iOff/nFromPageSize)+1; |
| 7140 | |
| 7141 | if( iFrom==PENDING_BYTE_PAGE(pBtFrom) || iFrom>nFromPage ){ |
| 7142 | continue; |
| 7143 | } |
| 7144 | |
| 7145 | rc = sqlite3PagerGet(pBtFrom->pPager, iFrom, &pFromPage); |
| 7146 | if( rc==SQLITE_OK ){ |
| 7147 | char *zFrom = sqlite3PagerGetData(pFromPage); |
danielk1977 | 06249db | 2008-08-23 16:17:55 +0000 | [diff] [blame] | 7148 | rc = sqlite3OsWrite(pFile, zFrom, nFromPageSize, iOff); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7149 | sqlite3PagerUnref(pFromPage); |
| 7150 | } |
| 7151 | } |
| 7152 | } |
| 7153 | |
| 7154 | /* Sync the database file */ |
| 7155 | if( rc==SQLITE_OK ){ |
| 7156 | rc = sqlite3PagerSync(pBtTo->pPager); |
| 7157 | } |
| 7158 | }else{ |
| 7159 | rc = sqlite3PagerTruncate(pBtTo->pPager, nNewPage); |
| 7160 | } |
| 7161 | if( rc==SQLITE_OK ){ |
| 7162 | pBtTo->pageSizeFixed = 0; |
| 7163 | } |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 7164 | } |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 7165 | |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 7166 | if( rc ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7167 | sqlite3BtreeRollback(pTo); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 7168 | } |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7169 | |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 7170 | return rc; |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 7171 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7172 | int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){ |
| 7173 | int rc; |
| 7174 | sqlite3BtreeEnter(pTo); |
| 7175 | sqlite3BtreeEnter(pFrom); |
| 7176 | rc = btreeCopyFile(pTo, pFrom); |
| 7177 | sqlite3BtreeLeave(pFrom); |
| 7178 | sqlite3BtreeLeave(pTo); |
| 7179 | return rc; |
| 7180 | } |
| 7181 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 7182 | #endif /* SQLITE_OMIT_VACUUM */ |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 7183 | |
| 7184 | /* |
| 7185 | ** Return non-zero if a transaction is active. |
| 7186 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7187 | int sqlite3BtreeIsInTrans(Btree *p){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7188 | assert( p==0 || sqlite3_mutex_held(p->db->mutex) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7189 | return (p && (p->inTrans==TRANS_WRITE)); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 7190 | } |
| 7191 | |
| 7192 | /* |
| 7193 | ** Return non-zero if a statement transaction is active. |
| 7194 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7195 | int sqlite3BtreeIsInStmt(Btree *p){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7196 | assert( sqlite3BtreeHoldsMutex(p) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7197 | return (p->pBt && p->pBt->inStmt); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 7198 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 7199 | |
| 7200 | /* |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 7201 | ** Return non-zero if a read (or write) transaction is active. |
| 7202 | */ |
| 7203 | int sqlite3BtreeIsInReadTrans(Btree *p){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7204 | assert( sqlite3_mutex_held(p->db->mutex) ); |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 7205 | return (p && (p->inTrans!=TRANS_NONE)); |
| 7206 | } |
| 7207 | |
| 7208 | /* |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7209 | ** This function returns a pointer to a blob of memory associated with |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 7210 | ** 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] | 7211 | ** purposes (for example, to store a high-level schema associated with |
| 7212 | ** the shared-btree). The btree layer manages reference counting issues. |
| 7213 | ** |
| 7214 | ** The first time this is called on a shared-btree, nBytes bytes of memory |
| 7215 | ** are allocated, zeroed, and returned to the caller. For each subsequent |
| 7216 | ** call the nBytes parameter is ignored and a pointer to the same blob |
| 7217 | ** of memory returned. |
| 7218 | ** |
danielk1977 | 171bfed | 2008-06-23 09:50:50 +0000 | [diff] [blame] | 7219 | ** If the nBytes parameter is 0 and the blob of memory has not yet been |
| 7220 | ** allocated, a null pointer is returned. If the blob has already been |
| 7221 | ** allocated, it is returned as normal. |
| 7222 | ** |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7223 | ** Just before the shared-btree is closed, the function passed as the |
| 7224 | ** xFree argument when the memory allocation was made is invoked on the |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 7225 | ** blob of allocated memory. This function should not call sqlite3_free() |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7226 | ** on the memory, the btree layer does that. |
| 7227 | */ |
| 7228 | void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){ |
| 7229 | BtShared *pBt = p->pBt; |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7230 | sqlite3BtreeEnter(p); |
danielk1977 | 171bfed | 2008-06-23 09:50:50 +0000 | [diff] [blame] | 7231 | if( !pBt->pSchema && nBytes ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 7232 | pBt->pSchema = sqlite3MallocZero(nBytes); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7233 | pBt->xFreeSchema = xFree; |
| 7234 | } |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7235 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7236 | return pBt->pSchema; |
| 7237 | } |
| 7238 | |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 7239 | /* |
| 7240 | ** Return true if another user of the same shared btree as the argument |
| 7241 | ** handle holds an exclusive lock on the sqlite_master table. |
| 7242 | */ |
| 7243 | int sqlite3BtreeSchemaLocked(Btree *p){ |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7244 | int rc; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7245 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7246 | sqlite3BtreeEnter(p); |
| 7247 | rc = (queryTableLock(p, MASTER_ROOT, READ_LOCK)!=SQLITE_OK); |
| 7248 | sqlite3BtreeLeave(p); |
| 7249 | return rc; |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 7250 | } |
| 7251 | |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 7252 | |
| 7253 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 7254 | /* |
| 7255 | ** Obtain a lock on the table whose root page is iTab. The |
| 7256 | ** lock is a write lock if isWritelock is true or a read lock |
| 7257 | ** if it is false. |
| 7258 | */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7259 | int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){ |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 7260 | int rc = SQLITE_OK; |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 7261 | if( p->sharable ){ |
| 7262 | u8 lockType = READ_LOCK + isWriteLock; |
| 7263 | assert( READ_LOCK+1==WRITE_LOCK ); |
| 7264 | assert( isWriteLock==0 || isWriteLock==1 ); |
| 7265 | sqlite3BtreeEnter(p); |
| 7266 | rc = queryTableLock(p, iTab, lockType); |
| 7267 | if( rc==SQLITE_OK ){ |
| 7268 | rc = lockTable(p, iTab, lockType); |
| 7269 | } |
| 7270 | sqlite3BtreeLeave(p); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7271 | } |
| 7272 | return rc; |
| 7273 | } |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 7274 | #endif |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 7275 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7276 | #ifndef SQLITE_OMIT_INCRBLOB |
| 7277 | /* |
| 7278 | ** Argument pCsr must be a cursor opened for writing on an |
| 7279 | ** INTKEY table currently pointing at a valid table entry. |
| 7280 | ** This function modifies the data stored as part of that entry. |
| 7281 | ** Only the data content may only be modified, it is not possible |
| 7282 | ** to change the length of the data stored. |
| 7283 | */ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7284 | int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7285 | assert( cursorHoldsMutex(pCsr) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7286 | assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) ); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7287 | assert(pCsr->isIncrblobHandle); |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 7288 | |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 7289 | restoreCursorPosition(pCsr); |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 7290 | assert( pCsr->eState!=CURSOR_REQUIRESEEK ); |
| 7291 | if( pCsr->eState!=CURSOR_VALID ){ |
| 7292 | return SQLITE_ABORT; |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7293 | } |
| 7294 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7295 | /* Check some preconditions: |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7296 | ** (a) the cursor is open for writing, |
| 7297 | ** (b) there is no read-lock on the table being modified and |
| 7298 | ** (c) the cursor points at a valid row of an intKey table. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7299 | */ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7300 | if( !pCsr->wrFlag ){ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7301 | return SQLITE_READONLY; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7302 | } |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 7303 | assert( !pCsr->pBt->readOnly |
| 7304 | && pCsr->pBt->inTransaction==TRANS_WRITE ); |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 7305 | if( checkReadLocks(pCsr->pBtree, pCsr->pgnoRoot, pCsr, 0) ){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7306 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 7307 | } |
| 7308 | if( pCsr->eState==CURSOR_INVALID || !pCsr->pPage->intKey ){ |
| 7309 | return SQLITE_ERROR; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7310 | } |
| 7311 | |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 7312 | return accessPayload(pCsr, offset, amt, (unsigned char *)z, 0, 1); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7313 | } |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7314 | |
| 7315 | /* |
| 7316 | ** 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] | 7317 | ** overflow list for the current row. This is used by cursors opened |
| 7318 | ** for incremental blob IO only. |
| 7319 | ** |
| 7320 | ** This function sets a flag only. The actual page location cache |
| 7321 | ** (stored in BtCursor.aOverflow[]) is allocated and used by function |
| 7322 | ** accessPayload() (the worker function for sqlite3BtreeData() and |
| 7323 | ** sqlite3BtreePutData()). |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7324 | */ |
| 7325 | void sqlite3BtreeCacheOverflow(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7326 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7327 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7328 | assert(!pCur->isIncrblobHandle); |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7329 | assert(!pCur->aOverflow); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7330 | pCur->isIncrblobHandle = 1; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7331 | } |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7332 | #endif |