drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 2 | ** 2004 April 6 |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
drh | 11b57d6 | 2009-02-24 19:21:41 +0000 | [diff] [blame^] | 12 | ** $Id: btree.c,v 1.570 2009/02/24 19:21:41 drh Exp $ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 13 | ** |
| 14 | ** This file implements a external (disk-based) database using BTrees. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 15 | ** See the header comment on "btreeInt.h" for additional information. |
| 16 | ** Including a description of file format and an overview of operation. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 17 | */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 18 | #include "btreeInt.h" |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 19 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 20 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 21 | ** The header string that appears at the beginning of every |
| 22 | ** SQLite database. |
drh | 556b2a2 | 2005-06-14 16:04:05 +0000 | [diff] [blame] | 23 | */ |
drh | 556b2a2 | 2005-06-14 16:04:05 +0000 | [diff] [blame] | 24 | static const char zMagicHeader[] = SQLITE_FILE_HEADER; |
drh | 08ed44e | 2001-04-29 23:32:55 +0000 | [diff] [blame] | 25 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 26 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 27 | ** Set this global variable to 1 to enable tracing using the TRACE |
| 28 | ** macro. |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 29 | */ |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 30 | #if 0 |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 31 | int sqlite3BtreeTrace=0; /* True to enable tracing */ |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 32 | # define TRACE(X) if(sqlite3BtreeTrace){printf X;fflush(stdout);} |
| 33 | #else |
| 34 | # define TRACE(X) |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 35 | #endif |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 36 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 37 | |
| 38 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 39 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 40 | /* |
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 | */ |
drh | 11b57d6 | 2009-02-24 19:21:41 +0000 | [diff] [blame^] | 71 | static int checkForReadConflicts(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 | /* |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 76 | ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(), |
| 77 | ** and clearAllSharedCacheTableLocks() |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 78 | ** manipulate entries in the BtShared.pLock linked list used to store |
| 79 | ** shared-cache table level locks. If the library is compiled with the |
| 80 | ** shared-cache feature disabled, then there is only ever one user |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 81 | ** of each BtShared structure and so this locking is not necessary. |
| 82 | ** So define the lock related functions as no-ops. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 83 | */ |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 84 | #define querySharedCacheTableLock(a,b,c) SQLITE_OK |
| 85 | #define setSharedCacheTableLock(a,b,c) SQLITE_OK |
| 86 | #define clearAllSharedCacheTableLocks(a) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 87 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 88 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 89 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 90 | /* |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 91 | ** Query to see if btree handle p may obtain a lock of type eLock |
| 92 | ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 93 | ** SQLITE_OK if the lock may be obtained (by calling |
| 94 | ** setSharedCacheTableLock()), or SQLITE_LOCKED if not. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 95 | */ |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 96 | static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 97 | BtShared *pBt = p->pBt; |
| 98 | BtLock *pIter; |
| 99 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 100 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 101 | assert( eLock==READ_LOCK || eLock==WRITE_LOCK ); |
| 102 | assert( p->db!=0 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 103 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 104 | /* This is a no-op if the shared-cache is not enabled */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 105 | if( !p->sharable ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 106 | return SQLITE_OK; |
| 107 | } |
| 108 | |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 109 | /* If some other connection is holding an exclusive lock, the |
| 110 | ** requested lock may not be obtained. |
| 111 | */ |
| 112 | if( pBt->pExclusive && pBt->pExclusive!=p ){ |
| 113 | return SQLITE_LOCKED; |
| 114 | } |
| 115 | |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 116 | /* This (along with setSharedCacheTableLock()) is where |
| 117 | ** the ReadUncommitted flag is dealt with. |
| 118 | ** If the caller is querying for a read-lock on any table |
drh | c74d0b1d | 2009-02-24 16:18:05 +0000 | [diff] [blame] | 119 | ** other than the sqlite_master table (table 1) and if the ReadUncommitted |
| 120 | ** flag is set, then the lock granted even if there are write-locks |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 121 | ** on the table. If a write-lock is requested, the ReadUncommitted flag |
| 122 | ** is not considered. |
| 123 | ** |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 124 | ** In function setSharedCacheTableLock(), if a read-lock is demanded and the |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 125 | ** ReadUncommitted flag is set, no entry is added to the locks list |
| 126 | ** (BtShared.pLock). |
| 127 | ** |
drh | c74d0b1d | 2009-02-24 16:18:05 +0000 | [diff] [blame] | 128 | ** To summarize: If the ReadUncommitted flag is set, then read cursors |
| 129 | ** on non-schema tables do not create or respect table locks. The locking |
| 130 | ** procedure for a write-cursor does not change. |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 131 | */ |
| 132 | if( |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 133 | 0==(p->db->flags&SQLITE_ReadUncommitted) || |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 134 | eLock==WRITE_LOCK || |
drh | 47ded16 | 2006-01-06 01:42:58 +0000 | [diff] [blame] | 135 | iTab==MASTER_ROOT |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 136 | ){ |
| 137 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 138 | if( pIter->pBtree!=p && pIter->iTable==iTab && |
| 139 | (pIter->eLock!=eLock || eLock!=READ_LOCK) ){ |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 140 | return SQLITE_LOCKED; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 141 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | return SQLITE_OK; |
| 145 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 146 | #endif /* !SQLITE_OMIT_SHARED_CACHE */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 147 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 148 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 149 | /* |
| 150 | ** Add a lock on the table with root-page iTable to the shared-btree used |
| 151 | ** by Btree handle p. Parameter eLock must be either READ_LOCK or |
| 152 | ** WRITE_LOCK. |
| 153 | ** |
| 154 | ** SQLITE_OK is returned if the lock is added successfully. SQLITE_BUSY and |
| 155 | ** SQLITE_NOMEM may also be returned. |
| 156 | */ |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 157 | static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 158 | BtShared *pBt = p->pBt; |
| 159 | BtLock *pLock = 0; |
| 160 | BtLock *pIter; |
| 161 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 162 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 163 | assert( eLock==READ_LOCK || eLock==WRITE_LOCK ); |
| 164 | assert( p->db!=0 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 165 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 166 | /* This is a no-op if the shared-cache is not enabled */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 167 | if( !p->sharable ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 168 | return SQLITE_OK; |
| 169 | } |
| 170 | |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 171 | assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 172 | |
drh | c74d0b1d | 2009-02-24 16:18:05 +0000 | [diff] [blame] | 173 | /* If the read-uncommitted flag is set and a read-lock is requested on |
| 174 | ** a non-schema table, then the lock is always granted. Return early |
| 175 | ** without adding an entry to the BtShared.pLock list. See |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 176 | ** comment in function querySharedCacheTableLock() for more info |
| 177 | ** on handling the ReadUncommitted flag. |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 178 | */ |
| 179 | if( |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 180 | (p->db->flags&SQLITE_ReadUncommitted) && |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 181 | (eLock==READ_LOCK) && |
drh | 47ded16 | 2006-01-06 01:42:58 +0000 | [diff] [blame] | 182 | iTable!=MASTER_ROOT |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 183 | ){ |
| 184 | return SQLITE_OK; |
| 185 | } |
| 186 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 187 | /* First search the list for an existing lock on this table. */ |
| 188 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 189 | if( pIter->iTable==iTable && pIter->pBtree==p ){ |
| 190 | pLock = pIter; |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /* If the above search did not find a BtLock struct associating Btree p |
| 196 | ** with table iTable, allocate one and link it into the list. |
| 197 | */ |
| 198 | if( !pLock ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 199 | pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock)); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 200 | if( !pLock ){ |
| 201 | return SQLITE_NOMEM; |
| 202 | } |
| 203 | pLock->iTable = iTable; |
| 204 | pLock->pBtree = p; |
| 205 | pLock->pNext = pBt->pLock; |
| 206 | pBt->pLock = pLock; |
| 207 | } |
| 208 | |
| 209 | /* Set the BtLock.eLock variable to the maximum of the current lock |
| 210 | ** and the requested lock. This means if a write-lock was already held |
| 211 | ** and a read-lock requested, we don't incorrectly downgrade the lock. |
| 212 | */ |
| 213 | assert( WRITE_LOCK>READ_LOCK ); |
danielk1977 | 5118b91 | 2005-12-30 16:31:53 +0000 | [diff] [blame] | 214 | if( eLock>pLock->eLock ){ |
| 215 | pLock->eLock = eLock; |
| 216 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 217 | |
| 218 | return SQLITE_OK; |
| 219 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 220 | #endif /* !SQLITE_OMIT_SHARED_CACHE */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 221 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 222 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 223 | /* |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 224 | ** Release all the table locks (locks obtained via calls to |
| 225 | ** the setSharedCacheTableLock() procedure) held by Btree handle p. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 226 | */ |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 227 | static void clearAllSharedCacheTableLocks(Btree *p){ |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 228 | BtShared *pBt = p->pBt; |
| 229 | BtLock **ppIter = &pBt->pLock; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 230 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 231 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 232 | assert( p->sharable || 0==*ppIter ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 233 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 234 | while( *ppIter ){ |
| 235 | BtLock *pLock = *ppIter; |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 236 | assert( pBt->pExclusive==0 || pBt->pExclusive==pLock->pBtree ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 237 | if( pLock->pBtree==p ){ |
| 238 | *ppIter = pLock->pNext; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 239 | sqlite3_free(pLock); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 240 | }else{ |
| 241 | ppIter = &pLock->pNext; |
| 242 | } |
| 243 | } |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 244 | |
| 245 | if( pBt->pExclusive==p ){ |
| 246 | pBt->pExclusive = 0; |
| 247 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 248 | } |
| 249 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| 250 | |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 251 | static void releasePage(MemPage *pPage); /* Forward reference */ |
| 252 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 253 | /* |
| 254 | ** Verify that the cursor holds a mutex on the BtShared |
| 255 | */ |
| 256 | #ifndef NDEBUG |
| 257 | static int cursorHoldsMutex(BtCursor *p){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 258 | return sqlite3_mutex_held(p->pBt->mutex); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 259 | } |
| 260 | #endif |
| 261 | |
| 262 | |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 263 | #ifndef SQLITE_OMIT_INCRBLOB |
| 264 | /* |
| 265 | ** Invalidate the overflow page-list cache for cursor pCur, if any. |
| 266 | */ |
| 267 | static void invalidateOverflowCache(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 268 | assert( cursorHoldsMutex(pCur) ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 269 | sqlite3_free(pCur->aOverflow); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 270 | pCur->aOverflow = 0; |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | ** Invalidate the overflow page-list cache for all cursors opened |
| 275 | ** on the shared btree structure pBt. |
| 276 | */ |
| 277 | static void invalidateAllOverflowCache(BtShared *pBt){ |
| 278 | BtCursor *p; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 279 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 280 | for(p=pBt->pCursor; p; p=p->pNext){ |
| 281 | invalidateOverflowCache(p); |
| 282 | } |
| 283 | } |
| 284 | #else |
| 285 | #define invalidateOverflowCache(x) |
| 286 | #define invalidateAllOverflowCache(x) |
| 287 | #endif |
| 288 | |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 289 | /* |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 290 | ** Set bit pgno of the BtShared.pHasContent bitvec. This is called |
| 291 | ** when a page that previously contained data becomes a free-list leaf |
| 292 | ** page. |
| 293 | ** |
| 294 | ** The BtShared.pHasContent bitvec exists to work around an obscure |
| 295 | ** bug caused by the interaction of two useful IO optimizations surrounding |
| 296 | ** free-list leaf pages: |
| 297 | ** |
| 298 | ** 1) When all data is deleted from a page and the page becomes |
| 299 | ** a free-list leaf page, the page is not written to the database |
| 300 | ** (as free-list leaf pages contain no meaningful data). Sometimes |
| 301 | ** such a page is not even journalled (as it will not be modified, |
| 302 | ** why bother journalling it?). |
| 303 | ** |
| 304 | ** 2) When a free-list leaf page is reused, its content is not read |
| 305 | ** from the database or written to the journal file (why should it |
| 306 | ** be, if it is not at all meaningful?). |
| 307 | ** |
| 308 | ** By themselves, these optimizations work fine and provide a handy |
| 309 | ** performance boost to bulk delete or insert operations. However, if |
| 310 | ** a page is moved to the free-list and then reused within the same |
| 311 | ** transaction, a problem comes up. If the page is not journalled when |
| 312 | ** it is moved to the free-list and it is also not journalled when it |
| 313 | ** is extracted from the free-list and reused, then the original data |
| 314 | ** may be lost. In the event of a rollback, it may not be possible |
| 315 | ** to restore the database to its original configuration. |
| 316 | ** |
| 317 | ** The solution is the BtShared.pHasContent bitvec. Whenever a page is |
| 318 | ** moved to become a free-list leaf page, the corresponding bit is |
| 319 | ** set in the bitvec. Whenever a leaf page is extracted from the free-list, |
| 320 | ** optimization 2 above is ommitted if the corresponding bit is already |
| 321 | ** set in BtShared.pHasContent. The contents of the bitvec are cleared |
| 322 | ** at the end of every transaction. |
| 323 | */ |
| 324 | static int btreeSetHasContent(BtShared *pBt, Pgno pgno){ |
| 325 | int rc = SQLITE_OK; |
| 326 | if( !pBt->pHasContent ){ |
| 327 | int nPage; |
| 328 | rc = sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 329 | if( rc==SQLITE_OK ){ |
| 330 | pBt->pHasContent = sqlite3BitvecCreate((u32)nPage); |
| 331 | if( !pBt->pHasContent ){ |
| 332 | rc = SQLITE_NOMEM; |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){ |
| 337 | rc = sqlite3BitvecSet(pBt->pHasContent, pgno); |
| 338 | } |
| 339 | return rc; |
| 340 | } |
| 341 | |
| 342 | /* |
| 343 | ** Query the BtShared.pHasContent vector. |
| 344 | ** |
| 345 | ** This function is called when a free-list leaf page is removed from the |
| 346 | ** free-list for reuse. It returns false if it is safe to retrieve the |
| 347 | ** page from the pager layer with the 'no-content' flag set. True otherwise. |
| 348 | */ |
| 349 | static int btreeGetHasContent(BtShared *pBt, Pgno pgno){ |
| 350 | Bitvec *p = pBt->pHasContent; |
| 351 | return (p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTest(p, pgno))); |
| 352 | } |
| 353 | |
| 354 | /* |
| 355 | ** Clear (destroy) the BtShared.pHasContent bitvec. This should be |
| 356 | ** invoked at the conclusion of each write-transaction. |
| 357 | */ |
| 358 | static void btreeClearHasContent(BtShared *pBt){ |
| 359 | sqlite3BitvecDestroy(pBt->pHasContent); |
| 360 | pBt->pHasContent = 0; |
| 361 | } |
| 362 | |
| 363 | /* |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 364 | ** Save the current cursor position in the variables BtCursor.nKey |
| 365 | ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK. |
| 366 | */ |
| 367 | static int saveCursorPosition(BtCursor *pCur){ |
| 368 | int rc; |
| 369 | |
| 370 | assert( CURSOR_VALID==pCur->eState ); |
| 371 | assert( 0==pCur->pKey ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 372 | assert( cursorHoldsMutex(pCur) ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 373 | |
| 374 | rc = sqlite3BtreeKeySize(pCur, &pCur->nKey); |
| 375 | |
| 376 | /* If this is an intKey table, then the above call to BtreeKeySize() |
| 377 | ** stores the integer key in pCur->nKey. In this case this value is |
| 378 | ** all that is required. Otherwise, if pCur is not open on an intKey |
| 379 | ** table, then malloc space for and store the pCur->nKey bytes of key |
| 380 | ** data. |
| 381 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 382 | if( rc==SQLITE_OK && 0==pCur->apPage[0]->intKey){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 383 | void *pKey = sqlite3Malloc( (int)pCur->nKey ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 384 | if( pKey ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 385 | rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 386 | if( rc==SQLITE_OK ){ |
| 387 | pCur->pKey = pKey; |
| 388 | }else{ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 389 | sqlite3_free(pKey); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 390 | } |
| 391 | }else{ |
| 392 | rc = SQLITE_NOMEM; |
| 393 | } |
| 394 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 395 | assert( !pCur->apPage[0]->intKey || !pCur->pKey ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 396 | |
| 397 | if( rc==SQLITE_OK ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 398 | int i; |
| 399 | for(i=0; i<=pCur->iPage; i++){ |
| 400 | releasePage(pCur->apPage[i]); |
| 401 | pCur->apPage[i] = 0; |
| 402 | } |
| 403 | pCur->iPage = -1; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 404 | pCur->eState = CURSOR_REQUIRESEEK; |
| 405 | } |
| 406 | |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 407 | invalidateOverflowCache(pCur); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 408 | return rc; |
| 409 | } |
| 410 | |
| 411 | /* |
| 412 | ** Save the positions of all cursors except pExcept open on the table |
| 413 | ** with root-page iRoot. Usually, this is called just before cursor |
| 414 | ** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()). |
| 415 | */ |
| 416 | static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){ |
| 417 | BtCursor *p; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 418 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 419 | assert( pExcept==0 || pExcept->pBt==pBt ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 420 | for(p=pBt->pCursor; p; p=p->pNext){ |
| 421 | if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) && |
| 422 | p->eState==CURSOR_VALID ){ |
| 423 | int rc = saveCursorPosition(p); |
| 424 | if( SQLITE_OK!=rc ){ |
| 425 | return rc; |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | return SQLITE_OK; |
| 430 | } |
| 431 | |
| 432 | /* |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 433 | ** Clear the current cursor position. |
| 434 | */ |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 435 | void sqlite3BtreeClearCursor(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 436 | assert( cursorHoldsMutex(pCur) ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 437 | sqlite3_free(pCur->pKey); |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 438 | pCur->pKey = 0; |
| 439 | pCur->eState = CURSOR_INVALID; |
| 440 | } |
| 441 | |
| 442 | /* |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 443 | ** Restore the cursor to the position it was in (or as close to as possible) |
| 444 | ** when saveCursorPosition() was called. Note that this call deletes the |
| 445 | ** saved position info stored by saveCursorPosition(), so there can be |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 446 | ** at most one effective restoreCursorPosition() call after each |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 447 | ** saveCursorPosition(). |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 448 | */ |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 449 | int sqlite3BtreeRestoreCursorPosition(BtCursor *pCur){ |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 450 | int rc; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 451 | assert( cursorHoldsMutex(pCur) ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 452 | assert( pCur->eState>=CURSOR_REQUIRESEEK ); |
| 453 | if( pCur->eState==CURSOR_FAULT ){ |
| 454 | return pCur->skip; |
| 455 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 456 | pCur->eState = CURSOR_INVALID; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 457 | rc = sqlite3BtreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skip); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 458 | if( rc==SQLITE_OK ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 459 | sqlite3_free(pCur->pKey); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 460 | pCur->pKey = 0; |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 461 | assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 462 | } |
| 463 | return rc; |
| 464 | } |
| 465 | |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 466 | #define restoreCursorPosition(p) \ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 467 | (p->eState>=CURSOR_REQUIRESEEK ? \ |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 468 | sqlite3BtreeRestoreCursorPosition(p) : \ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 469 | SQLITE_OK) |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 470 | |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 471 | /* |
| 472 | ** Determine whether or not a cursor has moved from the position it |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 473 | ** was last placed at. Cursors can move when the row they are pointing |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 474 | ** at is deleted out from under them. |
| 475 | ** |
| 476 | ** This routine returns an error code if something goes wrong. The |
| 477 | ** integer *pHasMoved is set to one if the cursor has moved and 0 if not. |
| 478 | */ |
| 479 | int sqlite3BtreeCursorHasMoved(BtCursor *pCur, int *pHasMoved){ |
| 480 | int rc; |
| 481 | |
| 482 | rc = restoreCursorPosition(pCur); |
| 483 | if( rc ){ |
| 484 | *pHasMoved = 1; |
| 485 | return rc; |
| 486 | } |
| 487 | if( pCur->eState!=CURSOR_VALID || pCur->skip!=0 ){ |
| 488 | *pHasMoved = 1; |
| 489 | }else{ |
| 490 | *pHasMoved = 0; |
| 491 | } |
| 492 | return SQLITE_OK; |
| 493 | } |
| 494 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 495 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 496 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 497 | ** Given a page number of a regular database page, return the page |
| 498 | ** number for the pointer-map page that contains the entry for the |
| 499 | ** input page number. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 500 | */ |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 501 | static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 502 | int nPagesPerMapPage; |
| 503 | Pgno iPtrMap, ret; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 504 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 505 | nPagesPerMapPage = (pBt->usableSize/5)+1; |
| 506 | iPtrMap = (pgno-2)/nPagesPerMapPage; |
| 507 | ret = (iPtrMap*nPagesPerMapPage) + 2; |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 508 | if( ret==PENDING_BYTE_PAGE(pBt) ){ |
| 509 | ret++; |
| 510 | } |
| 511 | return ret; |
| 512 | } |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 513 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 514 | /* |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 515 | ** Write an entry into the pointer map. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 516 | ** |
| 517 | ** This routine updates the pointer map entry for page number 'key' |
| 518 | ** so that it maps to type 'eType' and parent page number 'pgno'. |
| 519 | ** An error code is returned if something goes wrong, otherwise SQLITE_OK. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 520 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 521 | static int ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 522 | DbPage *pDbPage; /* The pointer map page */ |
| 523 | u8 *pPtrmap; /* The pointer map data */ |
| 524 | Pgno iPtrmap; /* The pointer map page number */ |
| 525 | int offset; /* Offset in pointer map page */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 526 | int rc; |
| 527 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 528 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 529 | /* The master-journal page number must never be used as a pointer map page */ |
| 530 | assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) ); |
| 531 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 532 | assert( pBt->autoVacuum ); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 533 | if( key==0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 534 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 535 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 536 | iPtrmap = PTRMAP_PAGENO(pBt, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 537 | rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 538 | if( rc!=SQLITE_OK ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 539 | return rc; |
| 540 | } |
danielk1977 | 8c666b1 | 2008-07-18 09:34:57 +0000 | [diff] [blame] | 541 | offset = PTRMAP_PTROFFSET(iPtrmap, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 542 | pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 543 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 544 | if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){ |
| 545 | TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent)); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 546 | rc = sqlite3PagerWrite(pDbPage); |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 547 | if( rc==SQLITE_OK ){ |
| 548 | pPtrmap[offset] = eType; |
| 549 | put4byte(&pPtrmap[offset+1], parent); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 550 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 551 | } |
| 552 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 553 | sqlite3PagerUnref(pDbPage); |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 554 | return rc; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | /* |
| 558 | ** Read an entry from the pointer map. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 559 | ** |
| 560 | ** This routine retrieves the pointer map entry for page 'key', writing |
| 561 | ** the type and parent page number to *pEType and *pPgno respectively. |
| 562 | ** An error code is returned if something goes wrong, otherwise SQLITE_OK. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 563 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 564 | static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 565 | DbPage *pDbPage; /* The pointer map page */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 566 | int iPtrmap; /* Pointer map page index */ |
| 567 | u8 *pPtrmap; /* Pointer map page data */ |
| 568 | int offset; /* Offset of entry in pointer map */ |
| 569 | int rc; |
| 570 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 571 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 572 | |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 573 | iPtrmap = PTRMAP_PAGENO(pBt, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 574 | rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 575 | if( rc!=0 ){ |
| 576 | return rc; |
| 577 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 578 | pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 579 | |
danielk1977 | 8c666b1 | 2008-07-18 09:34:57 +0000 | [diff] [blame] | 580 | offset = PTRMAP_PTROFFSET(iPtrmap, key); |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 581 | assert( pEType!=0 ); |
| 582 | *pEType = pPtrmap[offset]; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 583 | if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 584 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 585 | sqlite3PagerUnref(pDbPage); |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 586 | if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 587 | return SQLITE_OK; |
| 588 | } |
| 589 | |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 590 | #else /* if defined SQLITE_OMIT_AUTOVACUUM */ |
| 591 | #define ptrmapPut(w,x,y,z) SQLITE_OK |
| 592 | #define ptrmapGet(w,x,y,z) SQLITE_OK |
| 593 | #define ptrmapPutOvfl(y,z) SQLITE_OK |
| 594 | #endif |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 595 | |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 596 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 597 | ** Given a btree page and a cell index (0 means the first cell on |
| 598 | ** the page, 1 means the second cell, and so forth) return a pointer |
| 599 | ** to the cell content. |
| 600 | ** |
| 601 | ** This routine works only for pages that do not contain overflow cells. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 602 | */ |
drh | 1688c86 | 2008-07-18 02:44:17 +0000 | [diff] [blame] | 603 | #define findCell(P,I) \ |
| 604 | ((P)->aData + ((P)->maskPage & get2byte(&(P)->aData[(P)->cellOffset+2*(I)]))) |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 605 | |
| 606 | /* |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 607 | ** This a more complex version of findCell() that works for |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 608 | ** pages that do contain overflow cells. See insert |
| 609 | */ |
| 610 | static u8 *findOverflowCell(MemPage *pPage, int iCell){ |
| 611 | int i; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 612 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 613 | for(i=pPage->nOverflow-1; i>=0; i--){ |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 614 | int k; |
| 615 | struct _OvflCell *pOvfl; |
| 616 | pOvfl = &pPage->aOvfl[i]; |
| 617 | k = pOvfl->idx; |
| 618 | if( k<=iCell ){ |
| 619 | if( k==iCell ){ |
| 620 | return pOvfl->pCell; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 621 | } |
| 622 | iCell--; |
| 623 | } |
| 624 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 625 | return findCell(pPage, iCell); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | /* |
| 629 | ** Parse a cell content block and fill in the CellInfo structure. There |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 630 | ** are two versions of this function. sqlite3BtreeParseCell() takes a |
| 631 | ** cell index as the second argument and sqlite3BtreeParseCellPtr() |
| 632 | ** takes a pointer to the body of the cell as its second argument. |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 633 | ** |
| 634 | ** Within this file, the parseCell() macro can be called instead of |
| 635 | ** sqlite3BtreeParseCellPtr(). Using some compilers, this will be faster. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 636 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 637 | void sqlite3BtreeParseCellPtr( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 638 | MemPage *pPage, /* Page containing the cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 639 | u8 *pCell, /* Pointer to the cell text. */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 640 | CellInfo *pInfo /* Fill in this structure */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 641 | ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 642 | u16 n; /* Number bytes in cell content header */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 643 | u32 nPayload; /* Number of bytes of cell payload */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 644 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 645 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 646 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 647 | pInfo->pCell = pCell; |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 648 | assert( pPage->leaf==0 || pPage->leaf==1 ); |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 649 | n = pPage->childPtrSize; |
| 650 | assert( n==4-4*pPage->leaf ); |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 651 | if( pPage->intKey ){ |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 652 | if( pPage->hasData ){ |
| 653 | n += getVarint32(&pCell[n], nPayload); |
| 654 | }else{ |
| 655 | nPayload = 0; |
| 656 | } |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 657 | n += getVarint(&pCell[n], (u64*)&pInfo->nKey); |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 658 | pInfo->nData = nPayload; |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 659 | }else{ |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 660 | pInfo->nData = 0; |
| 661 | n += getVarint32(&pCell[n], nPayload); |
| 662 | pInfo->nKey = nPayload; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 663 | } |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 664 | pInfo->nPayload = nPayload; |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 665 | pInfo->nHeader = n; |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 666 | if( likely(nPayload<=pPage->maxLocal) ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 667 | /* This is the (easy) common case where the entire payload fits |
| 668 | ** on the local page. No overflow is required. |
| 669 | */ |
| 670 | int nSize; /* Total size of cell content in bytes */ |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 671 | nSize = nPayload + n; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 672 | pInfo->nLocal = (u16)nPayload; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 673 | pInfo->iOverflow = 0; |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 674 | if( (nSize & ~3)==0 ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 675 | nSize = 4; /* Minimum cell size is 4 */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 676 | } |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 677 | pInfo->nSize = (u16)nSize; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 678 | }else{ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 679 | /* If the payload will not fit completely on the local page, we have |
| 680 | ** to decide how much to store locally and how much to spill onto |
| 681 | ** overflow pages. The strategy is to minimize the amount of unused |
| 682 | ** space on overflow pages while keeping the amount of local storage |
| 683 | ** in between minLocal and maxLocal. |
| 684 | ** |
| 685 | ** Warning: changing the way overflow payload is distributed in any |
| 686 | ** way will result in an incompatible file format. |
| 687 | */ |
| 688 | int minLocal; /* Minimum amount of payload held locally */ |
| 689 | int maxLocal; /* Maximum amount of payload held locally */ |
| 690 | int surplus; /* Overflow payload available for local storage */ |
| 691 | |
| 692 | minLocal = pPage->minLocal; |
| 693 | maxLocal = pPage->maxLocal; |
| 694 | surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 695 | if( surplus <= maxLocal ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 696 | pInfo->nLocal = (u16)surplus; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 697 | }else{ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 698 | pInfo->nLocal = (u16)minLocal; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 699 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 700 | pInfo->iOverflow = (u16)(pInfo->nLocal + n); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 701 | pInfo->nSize = pInfo->iOverflow + 4; |
| 702 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 703 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 704 | #define parseCell(pPage, iCell, pInfo) \ |
| 705 | sqlite3BtreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo)) |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 706 | void sqlite3BtreeParseCell( |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 707 | MemPage *pPage, /* Page containing the cell */ |
| 708 | int iCell, /* The cell index. First cell is 0 */ |
| 709 | CellInfo *pInfo /* Fill in this structure */ |
| 710 | ){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 711 | parseCell(pPage, iCell, pInfo); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 712 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 713 | |
| 714 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 715 | ** Compute the total number of bytes that a Cell needs in the cell |
| 716 | ** data area of the btree-page. The return number includes the cell |
| 717 | ** data header and the local payload, but not any overflow page or |
| 718 | ** the space used by the cell pointer. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 719 | */ |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 720 | #ifndef NDEBUG |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 721 | static u16 cellSize(MemPage *pPage, int iCell){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 722 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 723 | sqlite3BtreeParseCell(pPage, iCell, &info); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 724 | return info.nSize; |
| 725 | } |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 726 | #endif |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 727 | static u16 cellSizePtr(MemPage *pPage, u8 *pCell){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 728 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 729 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 730 | return info.nSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 731 | } |
| 732 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 733 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 734 | /* |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 735 | ** If the cell pCell, part of page pPage contains a pointer |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 736 | ** to an overflow page, insert an entry into the pointer-map |
| 737 | ** for the overflow page. |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 738 | */ |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 739 | static int ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell){ |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 740 | CellInfo info; |
| 741 | assert( pCell!=0 ); |
| 742 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
| 743 | assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); |
| 744 | if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ |
| 745 | Pgno ovfl = get4byte(&pCell[info.iOverflow]); |
| 746 | return ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 747 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 748 | return SQLITE_OK; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 749 | } |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 750 | /* |
| 751 | ** If the cell with index iCell on page pPage contains a pointer |
| 752 | ** to an overflow page, insert an entry into the pointer-map |
| 753 | ** for the overflow page. |
| 754 | */ |
| 755 | static int ptrmapPutOvfl(MemPage *pPage, int iCell){ |
| 756 | u8 *pCell; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 757 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 758 | pCell = findOverflowCell(pPage, iCell); |
| 759 | return ptrmapPutOvflPtr(pPage, pCell); |
| 760 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 761 | #endif |
| 762 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 763 | |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 764 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 765 | ** Defragment the page given. All Cells are moved to the |
drh | 3a4a2d4 | 2005-11-24 14:24:28 +0000 | [diff] [blame] | 766 | ** end of the page and all free space is collected into one |
| 767 | ** big FreeBlk that occurs in between the header and cell |
drh | 31beae9 | 2005-11-24 14:34:36 +0000 | [diff] [blame] | 768 | ** pointer array and the cell content area. |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 769 | */ |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 770 | static int defragmentPage(MemPage *pPage){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 771 | int i; /* Loop counter */ |
| 772 | int pc; /* Address of a i-th cell */ |
| 773 | int addr; /* Offset of first byte after cell pointer array */ |
| 774 | int hdr; /* Offset to the page header */ |
| 775 | int size; /* Size of a cell */ |
| 776 | int usableSize; /* Number of usable bytes on a page */ |
| 777 | int cellOffset; /* Offset to the cell pointer array */ |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 778 | int cbrk; /* Offset to the cell content area */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 779 | int nCell; /* Number of cells on the page */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 780 | unsigned char *data; /* The page data */ |
| 781 | unsigned char *temp; /* Temp area for cell content */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 782 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 783 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 784 | assert( pPage->pBt!=0 ); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 785 | assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 786 | assert( pPage->nOverflow==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 787 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 26b7994 | 2007-11-28 16:19:56 +0000 | [diff] [blame] | 788 | temp = sqlite3PagerTempSpace(pPage->pBt->pPager); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 789 | data = pPage->aData; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 790 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 791 | cellOffset = pPage->cellOffset; |
| 792 | nCell = pPage->nCell; |
| 793 | assert( nCell==get2byte(&data[hdr+3]) ); |
| 794 | usableSize = pPage->pBt->usableSize; |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 795 | cbrk = get2byte(&data[hdr+5]); |
| 796 | memcpy(&temp[cbrk], &data[cbrk], usableSize - cbrk); |
| 797 | cbrk = usableSize; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 798 | for(i=0; i<nCell; i++){ |
| 799 | u8 *pAddr; /* The i-th cell pointer */ |
| 800 | pAddr = &data[cellOffset + i*2]; |
| 801 | pc = get2byte(pAddr); |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 802 | if( pc>=usableSize ){ |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 803 | return SQLITE_CORRUPT_BKPT; |
| 804 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 805 | size = cellSizePtr(pPage, &temp[pc]); |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 806 | cbrk -= size; |
danielk1977 | 0d06541 | 2008-11-12 18:21:36 +0000 | [diff] [blame] | 807 | if( cbrk<cellOffset+2*nCell || pc+size>usableSize ){ |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 808 | return SQLITE_CORRUPT_BKPT; |
| 809 | } |
danielk1977 | 0d06541 | 2008-11-12 18:21:36 +0000 | [diff] [blame] | 810 | assert( cbrk+size<=usableSize && cbrk>=0 ); |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 811 | memcpy(&data[cbrk], &temp[pc], size); |
| 812 | put2byte(pAddr, cbrk); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 813 | } |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 814 | assert( cbrk>=cellOffset+2*nCell ); |
| 815 | put2byte(&data[hdr+5], cbrk); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 816 | data[hdr+1] = 0; |
| 817 | data[hdr+2] = 0; |
| 818 | data[hdr+7] = 0; |
| 819 | addr = cellOffset+2*nCell; |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 820 | memset(&data[addr], 0, cbrk-addr); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 821 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
danielk1977 | 360e634 | 2008-11-12 08:49:51 +0000 | [diff] [blame] | 822 | if( cbrk-addr!=pPage->nFree ){ |
| 823 | return SQLITE_CORRUPT_BKPT; |
| 824 | } |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 825 | return SQLITE_OK; |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 826 | } |
| 827 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 828 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 829 | ** Allocate nByte bytes of space on a page. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 830 | ** |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 831 | ** Return the index into pPage->aData[] of the first byte of |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 832 | ** the new allocation. The caller guarantees that there is enough |
| 833 | ** space. This routine will never fail. |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 834 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 835 | ** If the page contains nBytes of free space but does not contain |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 836 | ** nBytes of contiguous free space, then this routine automatically |
| 837 | ** calls defragementPage() to consolidate all free space before |
| 838 | ** allocating the new chunk. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 839 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 840 | static int allocateSpace(MemPage *pPage, int nByte){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 841 | int addr, pc, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 842 | int size; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 843 | int nFrag; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 844 | int top; |
| 845 | int nCell; |
| 846 | int cellOffset; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 847 | unsigned char *data; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 848 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 849 | data = pPage->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 850 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 851 | assert( pPage->pBt ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 852 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 853 | assert( nByte>=0 ); /* Minimum cell size is 4 */ |
| 854 | assert( pPage->nFree>=nByte ); |
| 855 | assert( pPage->nOverflow==0 ); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 856 | pPage->nFree -= (u16)nByte; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 857 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 858 | |
| 859 | nFrag = data[hdr+7]; |
| 860 | if( nFrag<60 ){ |
| 861 | /* Search the freelist looking for a slot big enough to satisfy the |
| 862 | ** space request. */ |
| 863 | addr = hdr+1; |
| 864 | while( (pc = get2byte(&data[addr]))>0 ){ |
| 865 | size = get2byte(&data[pc+2]); |
| 866 | if( size>=nByte ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 867 | int x = size - nByte; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 868 | if( size<nByte+4 ){ |
| 869 | memcpy(&data[addr], &data[pc], 2); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 870 | data[hdr+7] = (u8)(nFrag + x); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 871 | return pc; |
| 872 | }else{ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 873 | put2byte(&data[pc+2], x); |
| 874 | return pc + x; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 875 | } |
| 876 | } |
| 877 | addr = pc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 878 | } |
| 879 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 880 | |
| 881 | /* Allocate memory from the gap in between the cell pointer array |
| 882 | ** and the cell content area. |
| 883 | */ |
| 884 | top = get2byte(&data[hdr+5]); |
| 885 | nCell = get2byte(&data[hdr+3]); |
| 886 | cellOffset = pPage->cellOffset; |
| 887 | if( nFrag>=60 || cellOffset + 2*nCell > top - nByte ){ |
danielk1977 | 474b7cc | 2008-07-09 11:49:46 +0000 | [diff] [blame] | 888 | defragmentPage(pPage); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 889 | top = get2byte(&data[hdr+5]); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 890 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 891 | top -= nByte; |
| 892 | assert( cellOffset + 2*nCell <= top ); |
| 893 | put2byte(&data[hdr+5], top); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 894 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 895 | return top; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 899 | ** Return a section of the pPage->aData to the freelist. |
| 900 | ** The first byte of the new free block is pPage->aDisk[start] |
| 901 | ** and the size of the block is "size" bytes. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 902 | ** |
| 903 | ** Most of the effort here is involved in coalesing adjacent |
| 904 | ** free blocks into a single big free block. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 905 | */ |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 906 | static int freeSpace(MemPage *pPage, int start, int size){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 907 | int addr, pbegin, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 908 | unsigned char *data = pPage->aData; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 909 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 910 | assert( pPage->pBt!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 911 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 912 | assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) ); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 913 | assert( (start + size)<=pPage->pBt->usableSize ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 914 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 915 | assert( size>=0 ); /* Minimum cell size is 4 */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 916 | |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 917 | #ifdef SQLITE_SECURE_DELETE |
| 918 | /* Overwrite deleted information with zeros when the SECURE_DELETE |
| 919 | ** option is enabled at compile-time */ |
| 920 | memset(&data[start], 0, size); |
| 921 | #endif |
| 922 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 923 | /* Add the space back into the linked list of freeblocks */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 924 | hdr = pPage->hdrOffset; |
| 925 | addr = hdr + 1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 926 | while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 927 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 928 | if( pbegin<=addr ) { |
| 929 | return SQLITE_CORRUPT_BKPT; |
| 930 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 931 | addr = pbegin; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 932 | } |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 933 | if ( pbegin>pPage->pBt->usableSize-4 ) { |
| 934 | return SQLITE_CORRUPT_BKPT; |
| 935 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 936 | assert( pbegin>addr || pbegin==0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 937 | put2byte(&data[addr], start); |
| 938 | put2byte(&data[start], pbegin); |
| 939 | put2byte(&data[start+2], size); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 940 | pPage->nFree += (u16)size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 941 | |
| 942 | /* Coalesce adjacent free blocks */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 943 | addr = pPage->hdrOffset + 1; |
| 944 | while( (pbegin = get2byte(&data[addr]))>0 ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 945 | int pnext, psize, x; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 946 | assert( pbegin>addr ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 947 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 948 | pnext = get2byte(&data[pbegin]); |
| 949 | psize = get2byte(&data[pbegin+2]); |
| 950 | if( pbegin + psize + 3 >= pnext && pnext>0 ){ |
| 951 | int frag = pnext - (pbegin+psize); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 952 | if( (frag<0) || (frag>(int)data[pPage->hdrOffset+7]) ){ |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 953 | return SQLITE_CORRUPT_BKPT; |
| 954 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 955 | data[pPage->hdrOffset+7] -= (u8)frag; |
| 956 | x = get2byte(&data[pnext]); |
| 957 | put2byte(&data[pbegin], x); |
| 958 | x = pnext + get2byte(&data[pnext+2]) - pbegin; |
| 959 | put2byte(&data[pbegin+2], x); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 960 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 961 | addr = pbegin; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 962 | } |
| 963 | } |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 964 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 965 | /* If the cell content area begins with a freeblock, remove it. */ |
| 966 | if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){ |
| 967 | int top; |
| 968 | pbegin = get2byte(&data[hdr+1]); |
| 969 | memcpy(&data[hdr+1], &data[pbegin], 2); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 970 | top = get2byte(&data[hdr+5]) + get2byte(&data[pbegin+2]); |
| 971 | put2byte(&data[hdr+5], top); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 972 | } |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 973 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 974 | return SQLITE_OK; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 978 | ** Decode the flags byte (the first byte of the header) for a page |
| 979 | ** and initialize fields of the MemPage structure accordingly. |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 980 | ** |
| 981 | ** Only the following combinations are supported. Anything different |
| 982 | ** indicates a corrupt database files: |
| 983 | ** |
| 984 | ** PTF_ZERODATA |
| 985 | ** PTF_ZERODATA | PTF_LEAF |
| 986 | ** PTF_LEAFDATA | PTF_INTKEY |
| 987 | ** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 988 | */ |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 989 | static int decodeFlags(MemPage *pPage, int flagByte){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 990 | BtShared *pBt; /* A copy of pPage->pBt */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 991 | |
| 992 | assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 993 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 994 | pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 ); |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 995 | flagByte &= ~PTF_LEAF; |
| 996 | pPage->childPtrSize = 4-4*pPage->leaf; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 997 | pBt = pPage->pBt; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 998 | if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){ |
| 999 | pPage->intKey = 1; |
| 1000 | pPage->hasData = pPage->leaf; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1001 | pPage->maxLocal = pBt->maxLeaf; |
| 1002 | pPage->minLocal = pBt->minLeaf; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1003 | }else if( flagByte==PTF_ZERODATA ){ |
| 1004 | pPage->intKey = 0; |
| 1005 | pPage->hasData = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1006 | pPage->maxLocal = pBt->maxLocal; |
| 1007 | pPage->minLocal = pBt->minLocal; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1008 | }else{ |
| 1009 | return SQLITE_CORRUPT_BKPT; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1010 | } |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1011 | return SQLITE_OK; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1015 | ** Initialize the auxiliary information for a disk block. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1016 | ** |
| 1017 | ** Return SQLITE_OK on success. If we see that the page does |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1018 | ** not contain a well-formed database page, then return |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1019 | ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not |
| 1020 | ** guarantee that the page is well-formed. It only shows that |
| 1021 | ** we failed to detect any corruption. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1022 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1023 | int sqlite3BtreeInitPage(MemPage *pPage){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1024 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1025 | assert( pPage->pBt!=0 ); |
| 1026 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1027 | assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1028 | assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) ); |
| 1029 | assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1030 | |
| 1031 | if( !pPage->isInit ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1032 | u16 pc; /* Address of a freeblock within pPage->aData[] */ |
| 1033 | u8 hdr; /* Offset to beginning of page header */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1034 | u8 *data; /* Equal to pPage->aData */ |
| 1035 | BtShared *pBt; /* The main btree structure */ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1036 | u16 usableSize; /* Amount of usable space on each page */ |
| 1037 | u16 cellOffset; /* Offset from start of page to first cell pointer */ |
| 1038 | u16 nFree; /* Number of unused bytes on the page */ |
| 1039 | u16 top; /* First byte of the cell content area */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1040 | |
| 1041 | pBt = pPage->pBt; |
| 1042 | |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1043 | hdr = pPage->hdrOffset; |
| 1044 | data = pPage->aData; |
| 1045 | if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT; |
| 1046 | assert( pBt->pageSize>=512 && pBt->pageSize<=32768 ); |
| 1047 | pPage->maskPage = pBt->pageSize - 1; |
| 1048 | pPage->nOverflow = 0; |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1049 | usableSize = pBt->usableSize; |
| 1050 | pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf; |
| 1051 | top = get2byte(&data[hdr+5]); |
| 1052 | pPage->nCell = get2byte(&data[hdr+3]); |
| 1053 | if( pPage->nCell>MX_CELL(pBt) ){ |
| 1054 | /* To many cells for a single page. The page must be corrupt */ |
| 1055 | return SQLITE_CORRUPT_BKPT; |
| 1056 | } |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1057 | |
| 1058 | /* Compute the total free space on the page */ |
| 1059 | pc = get2byte(&data[hdr+1]); |
| 1060 | nFree = data[hdr+7] + top - (cellOffset + 2*pPage->nCell); |
| 1061 | while( pc>0 ){ |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 1062 | u16 next, size; |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1063 | if( pc>usableSize-4 ){ |
| 1064 | /* Free block is off the page */ |
| 1065 | return SQLITE_CORRUPT_BKPT; |
| 1066 | } |
| 1067 | next = get2byte(&data[pc]); |
| 1068 | size = get2byte(&data[pc+2]); |
| 1069 | if( next>0 && next<=pc+size+3 ){ |
| 1070 | /* Free blocks must be in accending order */ |
| 1071 | return SQLITE_CORRUPT_BKPT; |
| 1072 | } |
| 1073 | nFree += size; |
| 1074 | pc = next; |
| 1075 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1076 | pPage->nFree = (u16)nFree; |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1077 | if( nFree>=usableSize ){ |
| 1078 | /* Free space cannot exceed total page size */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1079 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1080 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1081 | |
drh | 1688c86 | 2008-07-18 02:44:17 +0000 | [diff] [blame] | 1082 | #if 0 |
| 1083 | /* Check that all the offsets in the cell offset array are within range. |
| 1084 | ** |
| 1085 | ** Omitting this consistency check and using the pPage->maskPage mask |
| 1086 | ** to prevent overrunning the page buffer in findCell() results in a |
| 1087 | ** 2.5% performance gain. |
| 1088 | */ |
| 1089 | { |
| 1090 | u8 *pOff; /* Iterator used to check all cell offsets are in range */ |
| 1091 | u8 *pEnd; /* Pointer to end of cell offset array */ |
| 1092 | u8 mask; /* Mask of bits that must be zero in MSB of cell offsets */ |
| 1093 | mask = ~(((u8)(pBt->pageSize>>8))-1); |
| 1094 | pEnd = &data[cellOffset + pPage->nCell*2]; |
| 1095 | for(pOff=&data[cellOffset]; pOff!=pEnd && !((*pOff)&mask); pOff+=2); |
| 1096 | if( pOff!=pEnd ){ |
| 1097 | return SQLITE_CORRUPT_BKPT; |
| 1098 | } |
danielk1977 | e16535f | 2008-06-11 18:15:29 +0000 | [diff] [blame] | 1099 | } |
drh | 1688c86 | 2008-07-18 02:44:17 +0000 | [diff] [blame] | 1100 | #endif |
danielk1977 | e16535f | 2008-06-11 18:15:29 +0000 | [diff] [blame] | 1101 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1102 | pPage->isInit = 1; |
| 1103 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1104 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1108 | ** Set up a raw page so that it looks like a database page holding |
| 1109 | ** no entries. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1110 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1111 | static void zeroPage(MemPage *pPage, int flags){ |
| 1112 | unsigned char *data = pPage->aData; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1113 | BtShared *pBt = pPage->pBt; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1114 | u8 hdr = pPage->hdrOffset; |
| 1115 | u16 first; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1116 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1117 | assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1118 | assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); |
| 1119 | assert( sqlite3PagerGetData(pPage->pDbPage) == data ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1120 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1121 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 1af4a6e | 2008-07-18 03:32:51 +0000 | [diff] [blame] | 1122 | /*memset(&data[hdr], 0, pBt->usableSize - hdr);*/ |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 1123 | data[hdr] = (char)flags; |
| 1124 | first = hdr + 8 + 4*((flags&PTF_LEAF)==0 ?1:0); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1125 | memset(&data[hdr+1], 0, 4); |
| 1126 | data[hdr+7] = 0; |
| 1127 | put2byte(&data[hdr+5], pBt->usableSize); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1128 | pPage->nFree = pBt->usableSize - first; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1129 | decodeFlags(pPage, flags); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1130 | pPage->hdrOffset = hdr; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1131 | pPage->cellOffset = first; |
| 1132 | pPage->nOverflow = 0; |
drh | 1688c86 | 2008-07-18 02:44:17 +0000 | [diff] [blame] | 1133 | assert( pBt->pageSize>=512 && pBt->pageSize<=32768 ); |
| 1134 | pPage->maskPage = pBt->pageSize - 1; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1135 | pPage->nCell = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1136 | pPage->isInit = 1; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1139 | |
| 1140 | /* |
| 1141 | ** Convert a DbPage obtained from the pager into a MemPage used by |
| 1142 | ** the btree layer. |
| 1143 | */ |
| 1144 | static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){ |
| 1145 | MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage); |
| 1146 | pPage->aData = sqlite3PagerGetData(pDbPage); |
| 1147 | pPage->pDbPage = pDbPage; |
| 1148 | pPage->pBt = pBt; |
| 1149 | pPage->pgno = pgno; |
| 1150 | pPage->hdrOffset = pPage->pgno==1 ? 100 : 0; |
| 1151 | return pPage; |
| 1152 | } |
| 1153 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1154 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1155 | ** Get a page from the pager. Initialize the MemPage.pBt and |
| 1156 | ** MemPage.aData elements if needed. |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 1157 | ** |
| 1158 | ** If the noContent flag is set, it means that we do not care about |
| 1159 | ** the content of the page at this time. So do not go to the disk |
| 1160 | ** to fetch the content. Just fill in the content with zeros for now. |
| 1161 | ** If in the future we call sqlite3PagerWrite() on this page, that |
| 1162 | ** means we have started to be concerned about content and the disk |
| 1163 | ** read should occur at that point. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1164 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1165 | int sqlite3BtreeGetPage( |
| 1166 | BtShared *pBt, /* The btree */ |
| 1167 | Pgno pgno, /* Number of the page to fetch */ |
| 1168 | MemPage **ppPage, /* Return the page in this parameter */ |
| 1169 | int noContent /* Do not load page content if true */ |
| 1170 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1171 | int rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1172 | DbPage *pDbPage; |
| 1173 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1174 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 1175 | rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1176 | if( rc ) return rc; |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1177 | *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1178 | return SQLITE_OK; |
| 1179 | } |
| 1180 | |
| 1181 | /* |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 1182 | ** Retrieve a page from the pager cache. If the requested page is not |
| 1183 | ** already in the pager cache return NULL. Initialize the MemPage.pBt and |
| 1184 | ** MemPage.aData elements if needed. |
| 1185 | */ |
| 1186 | static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){ |
| 1187 | DbPage *pDbPage; |
| 1188 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 1189 | pDbPage = sqlite3PagerLookup(pBt->pPager, pgno); |
| 1190 | if( pDbPage ){ |
| 1191 | return btreePageFromDbPage(pDbPage, pgno, pBt); |
| 1192 | } |
| 1193 | return 0; |
| 1194 | } |
| 1195 | |
| 1196 | /* |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 1197 | ** Return the size of the database file in pages. If there is any kind of |
| 1198 | ** error, return ((unsigned int)-1). |
danielk1977 | 67fd7a9 | 2008-09-10 17:53:35 +0000 | [diff] [blame] | 1199 | */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 1200 | static Pgno pagerPagecount(BtShared *pBt){ |
| 1201 | int nPage = -1; |
danielk1977 | 67fd7a9 | 2008-09-10 17:53:35 +0000 | [diff] [blame] | 1202 | int rc; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 1203 | assert( pBt->pPage1 ); |
| 1204 | rc = sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 1205 | assert( rc==SQLITE_OK || nPage==-1 ); |
| 1206 | return (Pgno)nPage; |
danielk1977 | 67fd7a9 | 2008-09-10 17:53:35 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | /* |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1210 | ** Get a page from the pager and initialize it. This routine |
| 1211 | ** is just a convenience wrapper around separate calls to |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1212 | ** sqlite3BtreeGetPage() and sqlite3BtreeInitPage(). |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1213 | */ |
| 1214 | static int getAndInitPage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1215 | BtShared *pBt, /* The database file */ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1216 | Pgno pgno, /* Number of the page to get */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1217 | MemPage **ppPage /* Write the page pointer here */ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1218 | ){ |
| 1219 | int rc; |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1220 | MemPage *pPage; |
| 1221 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1222 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1223 | if( pgno==0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1224 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1225 | } |
danielk1977 | 9f580ad | 2008-09-10 14:45:57 +0000 | [diff] [blame] | 1226 | |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1227 | /* It is often the case that the page we want is already in cache. |
| 1228 | ** If so, get it directly. This saves us from having to call |
| 1229 | ** pagerPagecount() to make sure pgno is within limits, which results |
| 1230 | ** in a measureable performance improvements. |
| 1231 | */ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 1232 | *ppPage = pPage = btreePageLookup(pBt, pgno); |
| 1233 | if( pPage ){ |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1234 | /* Page is already in cache */ |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1235 | rc = SQLITE_OK; |
| 1236 | }else{ |
| 1237 | /* Page not in cache. Acquire it. */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 1238 | if( pgno>pagerPagecount(pBt) ){ |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1239 | return SQLITE_CORRUPT_BKPT; |
| 1240 | } |
| 1241 | rc = sqlite3BtreeGetPage(pBt, pgno, ppPage, 0); |
| 1242 | if( rc ) return rc; |
| 1243 | pPage = *ppPage; |
| 1244 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1245 | if( !pPage->isInit ){ |
| 1246 | rc = sqlite3BtreeInitPage(pPage); |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1247 | } |
| 1248 | if( rc!=SQLITE_OK ){ |
| 1249 | releasePage(pPage); |
| 1250 | *ppPage = 0; |
| 1251 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1252 | return rc; |
| 1253 | } |
| 1254 | |
| 1255 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1256 | ** Release a MemPage. This should be called once for each prior |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1257 | ** call to sqlite3BtreeGetPage. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1258 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1259 | static void releasePage(MemPage *pPage){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1260 | if( pPage ){ |
drh | 30df009 | 2008-12-23 15:58:06 +0000 | [diff] [blame] | 1261 | assert( pPage->nOverflow==0 || sqlite3PagerPageRefcount(pPage->pDbPage)>1 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1262 | assert( pPage->aData ); |
| 1263 | assert( pPage->pBt ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1264 | assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); |
| 1265 | assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1266 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1267 | sqlite3PagerUnref(pPage->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1272 | ** During a rollback, when the pager reloads information into the cache |
| 1273 | ** so that the cache is restored to its original state at the start of |
| 1274 | ** the transaction, for each page restored this routine is called. |
| 1275 | ** |
| 1276 | ** This routine needs to reset the extra data section at the end of the |
| 1277 | ** page to agree with the restored data. |
| 1278 | */ |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1279 | static void pageReinit(DbPage *pData){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1280 | MemPage *pPage; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1281 | pPage = (MemPage *)sqlite3PagerGetExtra(pData); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1282 | if( pPage->isInit ){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1283 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1284 | pPage->isInit = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1285 | if( sqlite3PagerPageRefcount(pData)>0 ){ |
| 1286 | sqlite3BtreeInitPage(pPage); |
| 1287 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1288 | } |
| 1289 | } |
| 1290 | |
| 1291 | /* |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1292 | ** Invoke the busy handler for a btree. |
| 1293 | */ |
danielk1977 | 1ceedd3 | 2008-11-19 10:22:33 +0000 | [diff] [blame] | 1294 | static int btreeInvokeBusyHandler(void *pArg){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1295 | BtShared *pBt = (BtShared*)pArg; |
| 1296 | assert( pBt->db ); |
| 1297 | assert( sqlite3_mutex_held(pBt->db->mutex) ); |
| 1298 | return sqlite3InvokeBusyHandler(&pBt->db->busyHandler); |
| 1299 | } |
| 1300 | |
| 1301 | /* |
drh | ad3e010 | 2004-09-03 23:32:18 +0000 | [diff] [blame] | 1302 | ** Open a database file. |
| 1303 | ** |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1304 | ** zFilename is the name of the database file. If zFilename is NULL |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1305 | ** a new database with a random name is created. This randomly named |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1306 | ** database file will be deleted when sqlite3BtreeClose() is called. |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1307 | ** If zFilename is ":memory:" then an in-memory database is created |
| 1308 | ** that is automatically destroyed when it is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1309 | */ |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1310 | int sqlite3BtreeOpen( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1311 | const char *zFilename, /* Name of the file containing the BTree database */ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1312 | sqlite3 *db, /* Associated database handle */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1313 | Btree **ppBtree, /* Pointer to new Btree object written here */ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1314 | int flags, /* Options */ |
| 1315 | int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1316 | ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1317 | sqlite3_vfs *pVfs; /* The VFS to use for this btree */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1318 | BtShared *pBt = 0; /* Shared part of btree structure */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1319 | Btree *p; /* Handle to return */ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1320 | int rc = SQLITE_OK; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1321 | u8 nReserve; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1322 | unsigned char zDbHeader[100]; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1323 | |
| 1324 | /* Set the variable isMemdb to true for an in-memory database, or |
| 1325 | ** false for a file-based database. This symbol is only required if |
| 1326 | ** either of the shared-data or autovacuum features are compiled |
| 1327 | ** into the library. |
| 1328 | */ |
| 1329 | #if !defined(SQLITE_OMIT_SHARED_CACHE) || !defined(SQLITE_OMIT_AUTOVACUUM) |
| 1330 | #ifdef SQLITE_OMIT_MEMORYDB |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 1331 | const int isMemdb = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1332 | #else |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 1333 | const int isMemdb = zFilename && !strcmp(zFilename, ":memory:"); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1334 | #endif |
| 1335 | #endif |
| 1336 | |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1337 | assert( db!=0 ); |
| 1338 | assert( sqlite3_mutex_held(db->mutex) ); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1339 | |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1340 | pVfs = db->pVfs; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1341 | p = sqlite3MallocZero(sizeof(Btree)); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1342 | if( !p ){ |
| 1343 | return SQLITE_NOMEM; |
| 1344 | } |
| 1345 | p->inTrans = TRANS_NONE; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1346 | p->db = db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1347 | |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 1348 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1349 | /* |
| 1350 | ** If this Btree is a candidate for shared cache, try to find an |
| 1351 | ** existing BtShared object that we can share with |
| 1352 | */ |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 1353 | if( isMemdb==0 |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1354 | && (db->flags & SQLITE_Vtab)==0 |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1355 | && zFilename && zFilename[0] |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1356 | ){ |
danielk1977 | 502b4e0 | 2008-09-02 14:07:24 +0000 | [diff] [blame] | 1357 | if( sqlite3GlobalConfig.sharedCacheEnabled ){ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 1358 | int nFullPathname = pVfs->mxPathname+1; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1359 | char *zFullPathname = sqlite3Malloc(nFullPathname); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1360 | sqlite3_mutex *mutexShared; |
| 1361 | p->sharable = 1; |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 1362 | db->flags |= SQLITE_SharedCache; |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1363 | if( !zFullPathname ){ |
| 1364 | sqlite3_free(p); |
| 1365 | return SQLITE_NOMEM; |
| 1366 | } |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 1367 | sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname); |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 1368 | mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1369 | sqlite3_mutex_enter(mutexShared); |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 1370 | for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1371 | assert( pBt->nRef>0 ); |
| 1372 | if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager)) |
| 1373 | && sqlite3PagerVfs(pBt->pPager)==pVfs ){ |
| 1374 | p->pBt = pBt; |
| 1375 | pBt->nRef++; |
| 1376 | break; |
| 1377 | } |
| 1378 | } |
| 1379 | sqlite3_mutex_leave(mutexShared); |
| 1380 | sqlite3_free(zFullPathname); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1381 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1382 | #ifdef SQLITE_DEBUG |
| 1383 | else{ |
| 1384 | /* In debug mode, we mark all persistent databases as sharable |
| 1385 | ** even when they are not. This exercises the locking code and |
| 1386 | ** gives more opportunity for asserts(sqlite3_mutex_held()) |
| 1387 | ** statements to find locking problems. |
| 1388 | */ |
| 1389 | p->sharable = 1; |
| 1390 | } |
| 1391 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1392 | } |
| 1393 | #endif |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1394 | if( pBt==0 ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1395 | /* |
| 1396 | ** The following asserts make sure that structures used by the btree are |
| 1397 | ** the right size. This is to guard against size changes that result |
| 1398 | ** when compiling on a different architecture. |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 1399 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1400 | assert( sizeof(i64)==8 || sizeof(i64)==4 ); |
| 1401 | assert( sizeof(u64)==8 || sizeof(u64)==4 ); |
| 1402 | assert( sizeof(u32)==4 ); |
| 1403 | assert( sizeof(u16)==2 ); |
| 1404 | assert( sizeof(Pgno)==4 ); |
| 1405 | |
| 1406 | pBt = sqlite3MallocZero( sizeof(*pBt) ); |
| 1407 | if( pBt==0 ){ |
| 1408 | rc = SQLITE_NOMEM; |
| 1409 | goto btree_open_out; |
| 1410 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1411 | rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename, |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1412 | EXTRA_SIZE, flags, vfsFlags); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1413 | if( rc==SQLITE_OK ){ |
| 1414 | rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader); |
| 1415 | } |
| 1416 | if( rc!=SQLITE_OK ){ |
| 1417 | goto btree_open_out; |
| 1418 | } |
danielk1977 | 1ceedd3 | 2008-11-19 10:22:33 +0000 | [diff] [blame] | 1419 | sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1420 | p->pBt = pBt; |
| 1421 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1422 | sqlite3PagerSetReiniter(pBt->pPager, pageReinit); |
| 1423 | pBt->pCursor = 0; |
| 1424 | pBt->pPage1 = 0; |
| 1425 | pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager); |
| 1426 | pBt->pageSize = get2byte(&zDbHeader[16]); |
| 1427 | if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE |
| 1428 | || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1429 | pBt->pageSize = 0; |
| 1430 | sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1431 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1432 | /* If the magic name ":memory:" will create an in-memory database, then |
| 1433 | ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if |
| 1434 | ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if |
| 1435 | ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a |
| 1436 | ** regular file-name. In this case the auto-vacuum applies as per normal. |
| 1437 | */ |
| 1438 | if( zFilename && !isMemdb ){ |
| 1439 | pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0); |
| 1440 | pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0); |
| 1441 | } |
| 1442 | #endif |
| 1443 | nReserve = 0; |
| 1444 | }else{ |
| 1445 | nReserve = zDbHeader[20]; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1446 | pBt->pageSizeFixed = 1; |
| 1447 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1448 | pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0); |
| 1449 | pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0); |
| 1450 | #endif |
| 1451 | } |
| 1452 | pBt->usableSize = pBt->pageSize - nReserve; |
| 1453 | assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1454 | sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1455 | |
| 1456 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
| 1457 | /* Add the new BtShared object to the linked list sharable BtShareds. |
| 1458 | */ |
| 1459 | if( p->sharable ){ |
| 1460 | sqlite3_mutex *mutexShared; |
| 1461 | pBt->nRef = 1; |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 1462 | mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 1463 | if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){ |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 1464 | pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST); |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 1465 | if( pBt->mutex==0 ){ |
| 1466 | rc = SQLITE_NOMEM; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1467 | db->mallocFailed = 0; |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 1468 | goto btree_open_out; |
| 1469 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1470 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1471 | sqlite3_mutex_enter(mutexShared); |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 1472 | pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList); |
| 1473 | GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1474 | sqlite3_mutex_leave(mutexShared); |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1475 | } |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1476 | #endif |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1477 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1478 | |
drh | cfed7bc | 2006-03-13 14:28:05 +0000 | [diff] [blame] | 1479 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1480 | /* If the new Btree uses a sharable pBtShared, then link the new |
| 1481 | ** Btree into the list of all sharable Btrees for the same connection. |
drh | abddb0c | 2007-08-20 13:14:28 +0000 | [diff] [blame] | 1482 | ** The list is kept in ascending order by pBt address. |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 1483 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1484 | if( p->sharable ){ |
| 1485 | int i; |
| 1486 | Btree *pSib; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1487 | for(i=0; i<db->nDb; i++){ |
| 1488 | if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1489 | while( pSib->pPrev ){ pSib = pSib->pPrev; } |
| 1490 | if( p->pBt<pSib->pBt ){ |
| 1491 | p->pNext = pSib; |
| 1492 | p->pPrev = 0; |
| 1493 | pSib->pPrev = p; |
| 1494 | }else{ |
drh | abddb0c | 2007-08-20 13:14:28 +0000 | [diff] [blame] | 1495 | while( pSib->pNext && pSib->pNext->pBt<p->pBt ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1496 | pSib = pSib->pNext; |
| 1497 | } |
| 1498 | p->pNext = pSib->pNext; |
| 1499 | p->pPrev = pSib; |
| 1500 | if( p->pNext ){ |
| 1501 | p->pNext->pPrev = p; |
| 1502 | } |
| 1503 | pSib->pNext = p; |
| 1504 | } |
| 1505 | break; |
| 1506 | } |
| 1507 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1508 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1509 | #endif |
| 1510 | *ppBtree = p; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1511 | |
| 1512 | btree_open_out: |
| 1513 | if( rc!=SQLITE_OK ){ |
| 1514 | if( pBt && pBt->pPager ){ |
| 1515 | sqlite3PagerClose(pBt->pPager); |
| 1516 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1517 | sqlite3_free(pBt); |
| 1518 | sqlite3_free(p); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1519 | *ppBtree = 0; |
| 1520 | } |
| 1521 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | /* |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1525 | ** Decrement the BtShared.nRef counter. When it reaches zero, |
| 1526 | ** remove the BtShared structure from the sharing list. Return |
| 1527 | ** true if the BtShared.nRef counter reaches zero and return |
| 1528 | ** false if it is still positive. |
| 1529 | */ |
| 1530 | static int removeFromSharingList(BtShared *pBt){ |
| 1531 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 1532 | sqlite3_mutex *pMaster; |
| 1533 | BtShared *pList; |
| 1534 | int removed = 0; |
| 1535 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1536 | assert( sqlite3_mutex_notheld(pBt->mutex) ); |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 1537 | pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1538 | sqlite3_mutex_enter(pMaster); |
| 1539 | pBt->nRef--; |
| 1540 | if( pBt->nRef<=0 ){ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 1541 | if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){ |
| 1542 | GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1543 | }else{ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 1544 | pList = GLOBAL(BtShared*,sqlite3SharedCacheList); |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 1545 | while( ALWAYS(pList) && pList->pNext!=pBt ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1546 | pList=pList->pNext; |
| 1547 | } |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 1548 | if( ALWAYS(pList) ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1549 | pList->pNext = pBt->pNext; |
| 1550 | } |
| 1551 | } |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 1552 | if( SQLITE_THREADSAFE ){ |
| 1553 | sqlite3_mutex_free(pBt->mutex); |
| 1554 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1555 | removed = 1; |
| 1556 | } |
| 1557 | sqlite3_mutex_leave(pMaster); |
| 1558 | return removed; |
| 1559 | #else |
| 1560 | return 1; |
| 1561 | #endif |
| 1562 | } |
| 1563 | |
| 1564 | /* |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1565 | ** Make sure pBt->pTmpSpace points to an allocation of |
| 1566 | ** MX_CELL_SIZE(pBt) bytes. |
| 1567 | */ |
| 1568 | static void allocateTempSpace(BtShared *pBt){ |
| 1569 | if( !pBt->pTmpSpace ){ |
| 1570 | pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize ); |
| 1571 | } |
| 1572 | } |
| 1573 | |
| 1574 | /* |
| 1575 | ** Free the pBt->pTmpSpace allocation |
| 1576 | */ |
| 1577 | static void freeTempSpace(BtShared *pBt){ |
| 1578 | sqlite3PageFree( pBt->pTmpSpace); |
| 1579 | pBt->pTmpSpace = 0; |
| 1580 | } |
| 1581 | |
| 1582 | /* |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1583 | ** Close an open database and invalidate all cursors. |
| 1584 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1585 | int sqlite3BtreeClose(Btree *p){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1586 | BtShared *pBt = p->pBt; |
| 1587 | BtCursor *pCur; |
| 1588 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1589 | /* Close all cursors opened via this handle. */ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1590 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1591 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1592 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1593 | pCur = pBt->pCursor; |
| 1594 | while( pCur ){ |
| 1595 | BtCursor *pTmp = pCur; |
| 1596 | pCur = pCur->pNext; |
| 1597 | if( pTmp->pBtree==p ){ |
| 1598 | sqlite3BtreeCloseCursor(pTmp); |
| 1599 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1600 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1601 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 1602 | /* Rollback any active transaction and free the handle structure. |
| 1603 | ** The call to sqlite3BtreeRollback() drops any table-locks held by |
| 1604 | ** this handle. |
| 1605 | */ |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 1606 | sqlite3BtreeRollback(p); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1607 | sqlite3BtreeLeave(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1608 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1609 | /* If there are still other outstanding references to the shared-btree |
| 1610 | ** structure, return now. The remainder of this procedure cleans |
| 1611 | ** up the shared-btree. |
| 1612 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1613 | assert( p->wantToLock==0 && p->locked==0 ); |
| 1614 | if( !p->sharable || removeFromSharingList(pBt) ){ |
| 1615 | /* The pBt is no longer on the sharing list, so we can access |
| 1616 | ** it without having to hold the mutex. |
| 1617 | ** |
| 1618 | ** Clean out and delete the BtShared object. |
| 1619 | */ |
| 1620 | assert( !pBt->pCursor ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1621 | sqlite3PagerClose(pBt->pPager); |
| 1622 | if( pBt->xFreeSchema && pBt->pSchema ){ |
| 1623 | pBt->xFreeSchema(pBt->pSchema); |
| 1624 | } |
| 1625 | sqlite3_free(pBt->pSchema); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1626 | freeTempSpace(pBt); |
drh | 65bbf29 | 2008-06-19 01:03:17 +0000 | [diff] [blame] | 1627 | sqlite3_free(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1630 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | cab5ed7 | 2007-08-22 11:41:18 +0000 | [diff] [blame] | 1631 | assert( p->wantToLock==0 ); |
| 1632 | assert( p->locked==0 ); |
| 1633 | if( p->pPrev ) p->pPrev->pNext = p->pNext; |
| 1634 | if( p->pNext ) p->pNext->pPrev = p->pPrev; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1635 | #endif |
| 1636 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1637 | sqlite3_free(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1638 | return SQLITE_OK; |
| 1639 | } |
| 1640 | |
| 1641 | /* |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1642 | ** Change the limit on the number of pages allowed in the cache. |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 1643 | ** |
| 1644 | ** The maximum number of cache pages is set to the absolute |
| 1645 | ** value of mxPage. If mxPage is negative, the pager will |
| 1646 | ** operate asynchronously - it will not stop to do fsync()s |
| 1647 | ** to insure data is written to the disk surface before |
| 1648 | ** continuing. Transactions still work if synchronous is off, |
| 1649 | ** and the database cannot be corrupted if this program |
| 1650 | ** crashes. But if the operating system crashes or there is |
| 1651 | ** an abrupt power failure when synchronous is off, the database |
| 1652 | ** could be left in an inconsistent and unrecoverable state. |
| 1653 | ** Synchronous is on by default so database corruption is not |
| 1654 | ** normally a worry. |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1655 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1656 | int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){ |
| 1657 | BtShared *pBt = p->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1658 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1659 | sqlite3BtreeEnter(p); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1660 | sqlite3PagerSetCachesize(pBt->pPager, mxPage); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1661 | sqlite3BtreeLeave(p); |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1662 | return SQLITE_OK; |
| 1663 | } |
| 1664 | |
| 1665 | /* |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1666 | ** Change the way data is synced to disk in order to increase or decrease |
| 1667 | ** how well the database resists damage due to OS crashes and power |
| 1668 | ** failures. Level 1 is the same as asynchronous (no syncs() occur and |
| 1669 | ** there is a high probability of damage) Level 2 is the default. There |
| 1670 | ** is a very low but non-zero probability of damage. Level 3 reduces the |
| 1671 | ** probability of damage to near zero but with a write performance reduction. |
| 1672 | */ |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1673 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
drh | ac530b1 | 2006-02-11 01:25:50 +0000 | [diff] [blame] | 1674 | int sqlite3BtreeSetSafetyLevel(Btree *p, int level, int fullSync){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1675 | BtShared *pBt = p->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1676 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1677 | sqlite3BtreeEnter(p); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1678 | sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1679 | sqlite3BtreeLeave(p); |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1680 | return SQLITE_OK; |
| 1681 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1682 | #endif |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1683 | |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1684 | /* |
| 1685 | ** Return TRUE if the given btree is set to safety level 1. In other |
| 1686 | ** words, return TRUE if no sync() occurs on the disk files. |
| 1687 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1688 | int sqlite3BtreeSyncDisabled(Btree *p){ |
| 1689 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1690 | int rc; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1691 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1692 | sqlite3BtreeEnter(p); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 1693 | assert( pBt && pBt->pPager ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1694 | rc = sqlite3PagerNosync(pBt->pPager); |
| 1695 | sqlite3BtreeLeave(p); |
| 1696 | return rc; |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1699 | #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1700 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1701 | ** Change the default pages size and the number of reserved bytes per page. |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 1702 | ** |
| 1703 | ** The page size must be a power of 2 between 512 and 65536. If the page |
| 1704 | ** size supplied does not meet this constraint then the page size is not |
| 1705 | ** changed. |
| 1706 | ** |
| 1707 | ** Page sizes are constrained to be a power of two so that the region |
| 1708 | ** of the database file used for locking (beginning at PENDING_BYTE, |
| 1709 | ** the first byte past the 1GB boundary, 0x40000000) needs to occur |
| 1710 | ** at the beginning of a page. |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 1711 | ** |
| 1712 | ** If parameter nReserve is less than zero, then the number of reserved |
| 1713 | ** bytes per page is left unchanged. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1714 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1715 | int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1716 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1717 | BtShared *pBt = p->pBt; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1718 | assert( nReserve>=-1 && nReserve<=255 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1719 | sqlite3BtreeEnter(p); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1720 | if( pBt->pageSizeFixed ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1721 | sqlite3BtreeLeave(p); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1722 | return SQLITE_READONLY; |
| 1723 | } |
| 1724 | if( nReserve<0 ){ |
| 1725 | nReserve = pBt->pageSize - pBt->usableSize; |
| 1726 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1727 | assert( nReserve>=0 && nReserve<=255 ); |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 1728 | if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE && |
| 1729 | ((pageSize-1)&pageSize)==0 ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1730 | assert( (pageSize & 7)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1731 | assert( !pBt->pPage1 && !pBt->pCursor ); |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 1732 | pBt->pageSize = (u16)pageSize; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1733 | freeTempSpace(pBt); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1734 | rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1735 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1736 | pBt->usableSize = pBt->pageSize - (u16)nReserve; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1737 | sqlite3BtreeLeave(p); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1738 | return rc; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1739 | } |
| 1740 | |
| 1741 | /* |
| 1742 | ** Return the currently defined page size |
| 1743 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1744 | int sqlite3BtreeGetPageSize(Btree *p){ |
| 1745 | return p->pBt->pageSize; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1746 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1747 | int sqlite3BtreeGetReserve(Btree *p){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1748 | int n; |
| 1749 | sqlite3BtreeEnter(p); |
| 1750 | n = p->pBt->pageSize - p->pBt->usableSize; |
| 1751 | sqlite3BtreeLeave(p); |
| 1752 | return n; |
drh | 2011d5f | 2004-07-22 02:40:37 +0000 | [diff] [blame] | 1753 | } |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 1754 | |
| 1755 | /* |
| 1756 | ** Set the maximum page count for a database if mxPage is positive. |
| 1757 | ** No changes are made if mxPage is 0 or negative. |
| 1758 | ** Regardless of the value of mxPage, return the maximum page count. |
| 1759 | */ |
| 1760 | int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1761 | int n; |
| 1762 | sqlite3BtreeEnter(p); |
| 1763 | n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage); |
| 1764 | sqlite3BtreeLeave(p); |
| 1765 | return n; |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 1766 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1767 | #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1768 | |
| 1769 | /* |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1770 | ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum' |
| 1771 | ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it |
| 1772 | ** is disabled. The default value for the auto-vacuum property is |
| 1773 | ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro. |
| 1774 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1775 | int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1776 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1777 | return SQLITE_READONLY; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1778 | #else |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1779 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1780 | int rc = SQLITE_OK; |
drh | 076d466 | 2009-02-18 20:31:18 +0000 | [diff] [blame] | 1781 | u8 av = (u8)autoVacuum; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1782 | |
| 1783 | sqlite3BtreeEnter(p); |
drh | 076d466 | 2009-02-18 20:31:18 +0000 | [diff] [blame] | 1784 | if( pBt->pageSizeFixed && (av ?1:0)!=pBt->autoVacuum ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1785 | rc = SQLITE_READONLY; |
| 1786 | }else{ |
drh | 076d466 | 2009-02-18 20:31:18 +0000 | [diff] [blame] | 1787 | pBt->autoVacuum = av ?1:0; |
| 1788 | pBt->incrVacuum = av==2 ?1:0; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1789 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1790 | sqlite3BtreeLeave(p); |
| 1791 | return rc; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1792 | #endif |
| 1793 | } |
| 1794 | |
| 1795 | /* |
| 1796 | ** Return the value of the 'auto-vacuum' property. If auto-vacuum is |
| 1797 | ** enabled 1 is returned. Otherwise 0. |
| 1798 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1799 | int sqlite3BtreeGetAutoVacuum(Btree *p){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1800 | #ifdef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1801 | return BTREE_AUTOVACUUM_NONE; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1802 | #else |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1803 | int rc; |
| 1804 | sqlite3BtreeEnter(p); |
| 1805 | rc = ( |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1806 | (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE: |
| 1807 | (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL: |
| 1808 | BTREE_AUTOVACUUM_INCR |
| 1809 | ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1810 | sqlite3BtreeLeave(p); |
| 1811 | return rc; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1812 | #endif |
| 1813 | } |
| 1814 | |
| 1815 | |
| 1816 | /* |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1817 | ** Get a reference to pPage1 of the database file. This will |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1818 | ** also acquire a readlock on that file. |
| 1819 | ** |
| 1820 | ** SQLITE_OK is returned on success. If the file is not a |
| 1821 | ** well-formed database file, then SQLITE_CORRUPT is returned. |
| 1822 | ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 1823 | ** is returned if we run out of memory. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1824 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1825 | static int lockBtree(BtShared *pBt){ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1826 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1827 | MemPage *pPage1; |
danielk1977 | 93f7af9 | 2008-05-09 16:57:50 +0000 | [diff] [blame] | 1828 | int nPage; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1829 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1830 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1831 | if( pBt->pPage1 ) return SQLITE_OK; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1832 | rc = sqlite3BtreeGetPage(pBt, 1, &pPage1, 0); |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1833 | if( rc!=SQLITE_OK ) return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1834 | |
| 1835 | /* Do some checking to help insure the file we opened really is |
| 1836 | ** a valid database file. |
| 1837 | */ |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 1838 | rc = sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 1839 | if( rc!=SQLITE_OK ){ |
danielk1977 | 93f7af9 | 2008-05-09 16:57:50 +0000 | [diff] [blame] | 1840 | goto page1_init_failed; |
| 1841 | }else if( nPage>0 ){ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1842 | int pageSize; |
| 1843 | int usableSize; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1844 | u8 *page1 = pPage1->aData; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 1845 | rc = SQLITE_NOTADB; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1846 | if( memcmp(page1, zMagicHeader, 16)!=0 ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1847 | goto page1_init_failed; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1848 | } |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 1849 | if( page1[18]>1 ){ |
| 1850 | pBt->readOnly = 1; |
| 1851 | } |
| 1852 | if( page1[19]>1 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1853 | goto page1_init_failed; |
| 1854 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1855 | |
| 1856 | /* The maximum embedded fraction must be exactly 25%. And the minimum |
| 1857 | ** embedded fraction must be 12.5% for both leaf-data and non-leaf-data. |
| 1858 | ** The original design allowed these amounts to vary, but as of |
| 1859 | ** version 3.6.0, we require them to be fixed. |
| 1860 | */ |
| 1861 | if( memcmp(&page1[21], "\100\040\040",3)!=0 ){ |
| 1862 | goto page1_init_failed; |
| 1863 | } |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1864 | pageSize = get2byte(&page1[16]); |
drh | 7dc385e | 2007-09-06 23:39:36 +0000 | [diff] [blame] | 1865 | if( ((pageSize-1)&pageSize)!=0 || pageSize<512 || |
| 1866 | (SQLITE_MAX_PAGE_SIZE<32768 && pageSize>SQLITE_MAX_PAGE_SIZE) |
| 1867 | ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1868 | goto page1_init_failed; |
| 1869 | } |
| 1870 | assert( (pageSize & 7)==0 ); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1871 | usableSize = pageSize - page1[20]; |
| 1872 | if( pageSize!=pBt->pageSize ){ |
| 1873 | /* After reading the first page of the database assuming a page size |
| 1874 | ** of BtShared.pageSize, we have discovered that the page-size is |
| 1875 | ** actually pageSize. Unlock the database, leave pBt->pPage1 at |
| 1876 | ** zero and return SQLITE_OK. The caller will call this function |
| 1877 | ** again with the correct page-size. |
| 1878 | */ |
| 1879 | releasePage(pPage1); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1880 | pBt->usableSize = (u16)usableSize; |
| 1881 | pBt->pageSize = (u16)pageSize; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1882 | freeTempSpace(pBt); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1883 | sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
| 1884 | return SQLITE_OK; |
| 1885 | } |
| 1886 | if( usableSize<500 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1887 | goto page1_init_failed; |
| 1888 | } |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 1889 | pBt->pageSize = (u16)pageSize; |
| 1890 | pBt->usableSize = (u16)usableSize; |
drh | 057cd3a | 2005-02-15 16:23:02 +0000 | [diff] [blame] | 1891 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1892 | pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0); |
danielk1977 | 27b1f95 | 2007-06-25 08:16:58 +0000 | [diff] [blame] | 1893 | pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0); |
drh | 057cd3a | 2005-02-15 16:23:02 +0000 | [diff] [blame] | 1894 | #endif |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1895 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1896 | |
| 1897 | /* maxLocal is the maximum amount of payload to store locally for |
| 1898 | ** a cell. Make sure it is small enough so that at least minFanout |
| 1899 | ** cells can will fit on one page. We assume a 10-byte page header. |
| 1900 | ** Besides the payload, the cell must store: |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1901 | ** 2-byte pointer to the cell |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1902 | ** 4-byte child pointer |
| 1903 | ** 9-byte nKey value |
| 1904 | ** 4-byte nData value |
| 1905 | ** 4-byte overflow page pointer |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1906 | ** So a cell consists of a 2-byte poiner, a header which is as much as |
| 1907 | ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow |
| 1908 | ** page pointer. |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1909 | */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1910 | pBt->maxLocal = (pBt->usableSize-12)*64/255 - 23; |
| 1911 | pBt->minLocal = (pBt->usableSize-12)*32/255 - 23; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1912 | pBt->maxLeaf = pBt->usableSize - 35; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1913 | pBt->minLeaf = (pBt->usableSize-12)*32/255 - 23; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1914 | assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1915 | pBt->pPage1 = pPage1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1916 | return SQLITE_OK; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1917 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1918 | page1_init_failed: |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1919 | releasePage(pPage1); |
| 1920 | pBt->pPage1 = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1921 | return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1922 | } |
| 1923 | |
| 1924 | /* |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1925 | ** This routine works like lockBtree() except that it also invokes the |
| 1926 | ** busy callback if there is lock contention. |
| 1927 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1928 | static int lockBtreeWithRetry(Btree *pRef){ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1929 | int rc = SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1930 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1931 | assert( sqlite3BtreeHoldsMutex(pRef) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1932 | if( pRef->inTrans==TRANS_NONE ){ |
| 1933 | u8 inTransaction = pRef->pBt->inTransaction; |
| 1934 | btreeIntegrity(pRef); |
| 1935 | rc = sqlite3BtreeBeginTrans(pRef, 0); |
| 1936 | pRef->pBt->inTransaction = inTransaction; |
| 1937 | pRef->inTrans = TRANS_NONE; |
| 1938 | if( rc==SQLITE_OK ){ |
| 1939 | pRef->pBt->nTransaction--; |
| 1940 | } |
| 1941 | btreeIntegrity(pRef); |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1942 | } |
| 1943 | return rc; |
| 1944 | } |
| 1945 | |
| 1946 | |
| 1947 | /* |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1948 | ** If there are no outstanding cursors and we are not in the middle |
| 1949 | ** of a transaction but there is a read lock on the database, then |
| 1950 | ** this routine unrefs the first page of the database file which |
| 1951 | ** has the effect of releasing the read lock. |
| 1952 | ** |
| 1953 | ** If there are any outstanding cursors, this routine is a no-op. |
| 1954 | ** |
| 1955 | ** If there is a transaction in progress, this routine is a no-op. |
| 1956 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1957 | static void unlockBtreeIfUnused(BtShared *pBt){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1958 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1959 | if( pBt->inTransaction==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1960 | if( sqlite3PagerRefcount(pBt->pPager)>=1 ){ |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 1961 | assert( pBt->pPage1->aData ); |
| 1962 | #if 0 |
drh | 24c9a2e | 2007-01-05 02:00:47 +0000 | [diff] [blame] | 1963 | if( pBt->pPage1->aData==0 ){ |
| 1964 | MemPage *pPage = pBt->pPage1; |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1965 | pPage->aData = sqlite3PagerGetData(pPage->pDbPage); |
drh | 24c9a2e | 2007-01-05 02:00:47 +0000 | [diff] [blame] | 1966 | pPage->pBt = pBt; |
| 1967 | pPage->pgno = 1; |
| 1968 | } |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 1969 | #endif |
drh | 24c9a2e | 2007-01-05 02:00:47 +0000 | [diff] [blame] | 1970 | releasePage(pBt->pPage1); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 1971 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1972 | pBt->pPage1 = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1973 | pBt->inStmt = 0; |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1974 | } |
| 1975 | } |
| 1976 | |
| 1977 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1978 | ** Create a new database by initializing the first page of the |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1979 | ** file. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1980 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1981 | static int newDatabase(BtShared *pBt){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1982 | MemPage *pP1; |
| 1983 | unsigned char *data; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1984 | int rc; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 1985 | int nPage; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1986 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1987 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 1988 | rc = sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 1989 | if( rc!=SQLITE_OK || nPage>0 ){ |
| 1990 | return rc; |
| 1991 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1992 | pP1 = pBt->pPage1; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1993 | assert( pP1!=0 ); |
| 1994 | data = pP1->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1995 | rc = sqlite3PagerWrite(pP1->pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1996 | if( rc ) return rc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1997 | memcpy(data, zMagicHeader, sizeof(zMagicHeader)); |
| 1998 | assert( sizeof(zMagicHeader)==16 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1999 | put2byte(&data[16], pBt->pageSize); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 2000 | data[18] = 1; |
| 2001 | data[19] = 1; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 2002 | assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize); |
| 2003 | data[20] = (u8)(pBt->pageSize - pBt->usableSize); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 2004 | data[21] = 64; |
| 2005 | data[22] = 32; |
| 2006 | data[23] = 32; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2007 | memset(&data[24], 0, 100-24); |
drh | e6c4381 | 2004-05-14 12:17:46 +0000 | [diff] [blame] | 2008 | zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA ); |
drh | f2a611c | 2004-09-05 00:33:43 +0000 | [diff] [blame] | 2009 | pBt->pageSizeFixed = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2010 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2011 | assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 ); |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 2012 | assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 ); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2013 | put4byte(&data[36 + 4*4], pBt->autoVacuum); |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 2014 | put4byte(&data[36 + 7*4], pBt->incrVacuum); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2015 | #endif |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2016 | return SQLITE_OK; |
| 2017 | } |
| 2018 | |
| 2019 | /* |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2020 | ** Attempt to start a new transaction. A write-transaction |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 2021 | ** is started if the second argument is nonzero, otherwise a read- |
| 2022 | ** transaction. If the second argument is 2 or more and exclusive |
| 2023 | ** transaction is started, meaning that no other process is allowed |
| 2024 | ** to access the database. A preexisting transaction may not be |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2025 | ** upgraded to exclusive by calling this routine a second time - the |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 2026 | ** exclusivity flag only works for a new transaction. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2027 | ** |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2028 | ** A write-transaction must be started before attempting any |
| 2029 | ** changes to the database. None of the following routines |
| 2030 | ** will work unless a transaction is started first: |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2031 | ** |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 2032 | ** sqlite3BtreeCreateTable() |
| 2033 | ** sqlite3BtreeCreateIndex() |
| 2034 | ** sqlite3BtreeClearTable() |
| 2035 | ** sqlite3BtreeDropTable() |
| 2036 | ** sqlite3BtreeInsert() |
| 2037 | ** sqlite3BtreeDelete() |
| 2038 | ** sqlite3BtreeUpdateMeta() |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 2039 | ** |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2040 | ** If an initial attempt to acquire the lock fails because of lock contention |
| 2041 | ** and the database was previously unlocked, then invoke the busy handler |
| 2042 | ** if there is one. But if there was previously a read-lock, do not |
| 2043 | ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is |
| 2044 | ** returned when there is already a read-lock in order to avoid a deadlock. |
| 2045 | ** |
| 2046 | ** Suppose there are two processes A and B. A has a read lock and B has |
| 2047 | ** a reserved lock. B tries to promote to exclusive but is blocked because |
| 2048 | ** of A's read lock. A tries to promote to reserved but is blocked by B. |
| 2049 | ** One or the other of the two processes must give way or there can be |
| 2050 | ** no progress. By returning SQLITE_BUSY and not invoking the busy callback |
| 2051 | ** when A already has a read lock, we encourage A to give up and let B |
| 2052 | ** proceed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2053 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2054 | int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ |
| 2055 | BtShared *pBt = p->pBt; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2056 | int rc = SQLITE_OK; |
| 2057 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2058 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2059 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2060 | btreeIntegrity(p); |
| 2061 | |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2062 | /* If the btree is already in a write-transaction, or it |
| 2063 | ** is already in a read-transaction and a read-transaction |
| 2064 | ** is requested, this is a no-op. |
| 2065 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2066 | if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2067 | goto trans_begun; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2068 | } |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2069 | |
| 2070 | /* Write transactions are not possible on a read-only database */ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2071 | if( pBt->readOnly && wrflag ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2072 | rc = SQLITE_READONLY; |
| 2073 | goto trans_begun; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2074 | } |
| 2075 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2076 | /* If another database handle has already opened a write transaction |
| 2077 | ** on this shared-btree structure and a second write transaction is |
| 2078 | ** requested, return SQLITE_BUSY. |
| 2079 | */ |
| 2080 | if( pBt->inTransaction==TRANS_WRITE && wrflag ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2081 | rc = SQLITE_BUSY; |
| 2082 | goto trans_begun; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2083 | } |
| 2084 | |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 2085 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 2086 | if( wrflag>1 ){ |
| 2087 | BtLock *pIter; |
| 2088 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 2089 | if( pIter->pBtree!=p ){ |
| 2090 | rc = SQLITE_BUSY; |
| 2091 | goto trans_begun; |
| 2092 | } |
| 2093 | } |
| 2094 | } |
| 2095 | #endif |
| 2096 | |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2097 | do { |
drh | 8a9c17f | 2008-05-02 14:23:54 +0000 | [diff] [blame] | 2098 | if( pBt->pPage1==0 ){ |
| 2099 | do{ |
| 2100 | rc = lockBtree(pBt); |
| 2101 | }while( pBt->pPage1==0 && rc==SQLITE_OK ); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2102 | } |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 2103 | |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2104 | if( rc==SQLITE_OK && wrflag ){ |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 2105 | if( pBt->readOnly ){ |
| 2106 | rc = SQLITE_READONLY; |
| 2107 | }else{ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 2108 | rc = sqlite3PagerBegin(pBt->pPager, wrflag>1); |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 2109 | if( rc==SQLITE_OK ){ |
| 2110 | rc = newDatabase(pBt); |
| 2111 | } |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2112 | } |
| 2113 | } |
| 2114 | |
| 2115 | if( rc==SQLITE_OK ){ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2116 | if( wrflag ) pBt->inStmt = 0; |
| 2117 | }else{ |
| 2118 | unlockBtreeIfUnused(pBt); |
| 2119 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2120 | }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE && |
danielk1977 | 1ceedd3 | 2008-11-19 10:22:33 +0000 | [diff] [blame] | 2121 | btreeInvokeBusyHandler(pBt) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2122 | |
| 2123 | if( rc==SQLITE_OK ){ |
| 2124 | if( p->inTrans==TRANS_NONE ){ |
| 2125 | pBt->nTransaction++; |
| 2126 | } |
| 2127 | p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ); |
| 2128 | if( p->inTrans>pBt->inTransaction ){ |
| 2129 | pBt->inTransaction = p->inTrans; |
| 2130 | } |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 2131 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 2132 | if( wrflag>1 ){ |
| 2133 | assert( !pBt->pExclusive ); |
| 2134 | pBt->pExclusive = p; |
| 2135 | } |
| 2136 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2137 | } |
| 2138 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2139 | |
| 2140 | trans_begun: |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2141 | if( rc==SQLITE_OK && wrflag ){ |
danielk1977 | 12dd549 | 2008-12-18 15:45:07 +0000 | [diff] [blame] | 2142 | /* This call makes sure that the pager has the correct number of |
| 2143 | ** open savepoints. If the second parameter is greater than 0 and |
| 2144 | ** the sub-journal is not already open, then it will be opened here. |
| 2145 | */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2146 | rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint); |
| 2147 | } |
danielk1977 | 12dd549 | 2008-12-18 15:45:07 +0000 | [diff] [blame] | 2148 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2149 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2150 | sqlite3BtreeLeave(p); |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 2151 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2152 | } |
| 2153 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2154 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2155 | |
| 2156 | /* |
| 2157 | ** Set the pointer-map entries for all children of page pPage. Also, if |
| 2158 | ** pPage contains cells that point to overflow pages, set the pointer |
| 2159 | ** map entries for the overflow pages as well. |
| 2160 | */ |
| 2161 | static int setChildPtrmaps(MemPage *pPage){ |
| 2162 | int i; /* Counter variable */ |
| 2163 | int nCell; /* Number of cells in page pPage */ |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 2164 | int rc; /* Return code */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2165 | BtShared *pBt = pPage->pBt; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 2166 | u8 isInitOrig = pPage->isInit; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2167 | Pgno pgno = pPage->pgno; |
| 2168 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2169 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2170 | rc = sqlite3BtreeInitPage(pPage); |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 2171 | if( rc!=SQLITE_OK ){ |
| 2172 | goto set_child_ptrmaps_out; |
| 2173 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2174 | nCell = pPage->nCell; |
| 2175 | |
| 2176 | for(i=0; i<nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2177 | u8 *pCell = findCell(pPage, i); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2178 | |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 2179 | rc = ptrmapPutOvflPtr(pPage, pCell); |
| 2180 | if( rc!=SQLITE_OK ){ |
| 2181 | goto set_child_ptrmaps_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2182 | } |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 2183 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2184 | if( !pPage->leaf ){ |
| 2185 | Pgno childPgno = get4byte(pCell); |
| 2186 | rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno); |
danielk1977 | 00a696d | 2008-09-29 16:41:31 +0000 | [diff] [blame] | 2187 | if( rc!=SQLITE_OK ) goto set_child_ptrmaps_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2188 | } |
| 2189 | } |
| 2190 | |
| 2191 | if( !pPage->leaf ){ |
| 2192 | Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 2193 | rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno); |
| 2194 | } |
| 2195 | |
| 2196 | set_child_ptrmaps_out: |
| 2197 | pPage->isInit = isInitOrig; |
| 2198 | return rc; |
| 2199 | } |
| 2200 | |
| 2201 | /* |
| 2202 | ** Somewhere on pPage, which is guarenteed to be a btree page, not an overflow |
| 2203 | ** page, is a pointer to page iFrom. Modify this pointer so that it points to |
| 2204 | ** iTo. Parameter eType describes the type of pointer to be modified, as |
| 2205 | ** follows: |
| 2206 | ** |
| 2207 | ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child |
| 2208 | ** page of pPage. |
| 2209 | ** |
| 2210 | ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow |
| 2211 | ** page pointed to by one of the cells on pPage. |
| 2212 | ** |
| 2213 | ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next |
| 2214 | ** overflow page in the list. |
| 2215 | */ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2216 | static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2217 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 2218 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2219 | if( eType==PTRMAP_OVERFLOW2 ){ |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 2220 | /* 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] | 2221 | if( get4byte(pPage->aData)!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2222 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2223 | } |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 2224 | put4byte(pPage->aData, iTo); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2225 | }else{ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 2226 | u8 isInitOrig = pPage->isInit; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2227 | int i; |
| 2228 | int nCell; |
| 2229 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2230 | sqlite3BtreeInitPage(pPage); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2231 | nCell = pPage->nCell; |
| 2232 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2233 | for(i=0; i<nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2234 | u8 *pCell = findCell(pPage, i); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2235 | if( eType==PTRMAP_OVERFLOW1 ){ |
| 2236 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2237 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2238 | if( info.iOverflow ){ |
| 2239 | if( iFrom==get4byte(&pCell[info.iOverflow]) ){ |
| 2240 | put4byte(&pCell[info.iOverflow], iTo); |
| 2241 | break; |
| 2242 | } |
| 2243 | } |
| 2244 | }else{ |
| 2245 | if( get4byte(pCell)==iFrom ){ |
| 2246 | put4byte(pCell, iTo); |
| 2247 | break; |
| 2248 | } |
| 2249 | } |
| 2250 | } |
| 2251 | |
| 2252 | if( i==nCell ){ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2253 | if( eType!=PTRMAP_BTREE || |
| 2254 | get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2255 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2256 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2257 | put4byte(&pPage->aData[pPage->hdrOffset+8], iTo); |
| 2258 | } |
| 2259 | |
| 2260 | pPage->isInit = isInitOrig; |
| 2261 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2262 | return SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2263 | } |
| 2264 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2265 | |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2266 | /* |
| 2267 | ** Move the open database page pDbPage to location iFreePage in the |
| 2268 | ** database. The pDbPage reference remains valid. |
| 2269 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2270 | static int relocatePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2271 | BtShared *pBt, /* Btree */ |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2272 | MemPage *pDbPage, /* Open page to move */ |
| 2273 | u8 eType, /* Pointer map 'type' entry for pDbPage */ |
| 2274 | Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */ |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 2275 | Pgno iFreePage, /* The location to move pDbPage to */ |
| 2276 | int isCommit |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2277 | ){ |
| 2278 | MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */ |
| 2279 | Pgno iDbPage = pDbPage->pgno; |
| 2280 | Pager *pPager = pBt->pPager; |
| 2281 | int rc; |
| 2282 | |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2283 | assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || |
| 2284 | eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2285 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 2286 | assert( pDbPage->pBt==pBt ); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2287 | |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 2288 | /* Move page iDbPage from its current location to page number iFreePage */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2289 | TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", |
| 2290 | iDbPage, iFreePage, iPtrPage, eType)); |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 2291 | rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2292 | if( rc!=SQLITE_OK ){ |
| 2293 | return rc; |
| 2294 | } |
| 2295 | pDbPage->pgno = iFreePage; |
| 2296 | |
| 2297 | /* If pDbPage was a btree-page, then it may have child pages and/or cells |
| 2298 | ** that point to overflow pages. The pointer map entries for all these |
| 2299 | ** pages need to be changed. |
| 2300 | ** |
| 2301 | ** If pDbPage is an overflow page, then the first 4 bytes may store a |
| 2302 | ** pointer to a subsequent overflow page. If this is the case, then |
| 2303 | ** the pointer map needs to be updated for the subsequent overflow page. |
| 2304 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2305 | if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2306 | rc = setChildPtrmaps(pDbPage); |
| 2307 | if( rc!=SQLITE_OK ){ |
| 2308 | return rc; |
| 2309 | } |
| 2310 | }else{ |
| 2311 | Pgno nextOvfl = get4byte(pDbPage->aData); |
| 2312 | if( nextOvfl!=0 ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2313 | rc = ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage); |
| 2314 | if( rc!=SQLITE_OK ){ |
| 2315 | return rc; |
| 2316 | } |
| 2317 | } |
| 2318 | } |
| 2319 | |
| 2320 | /* Fix the database pointer on page iPtrPage that pointed at iDbPage so |
| 2321 | ** that it points at iFreePage. Also fix the pointer map entry for |
| 2322 | ** iPtrPage. |
| 2323 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2324 | if( eType!=PTRMAP_ROOTPAGE ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2325 | rc = sqlite3BtreeGetPage(pBt, iPtrPage, &pPtrPage, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2326 | if( rc!=SQLITE_OK ){ |
| 2327 | return rc; |
| 2328 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2329 | rc = sqlite3PagerWrite(pPtrPage->pDbPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2330 | if( rc!=SQLITE_OK ){ |
| 2331 | releasePage(pPtrPage); |
| 2332 | return rc; |
| 2333 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2334 | rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2335 | releasePage(pPtrPage); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2336 | if( rc==SQLITE_OK ){ |
| 2337 | rc = ptrmapPut(pBt, iFreePage, eType, iPtrPage); |
| 2338 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2339 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2340 | return rc; |
| 2341 | } |
| 2342 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2343 | /* Forward declaration required by incrVacuumStep(). */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2344 | static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2345 | |
| 2346 | /* |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2347 | ** Perform a single step of an incremental-vacuum. If successful, |
| 2348 | ** return SQLITE_OK. If there is no work to do (and therefore no |
| 2349 | ** point in calling this function again), return SQLITE_DONE. |
| 2350 | ** |
| 2351 | ** More specificly, this function attempts to re-organize the |
| 2352 | ** database so that the last page of the file currently in use |
| 2353 | ** is no longer in use. |
| 2354 | ** |
| 2355 | ** If the nFin parameter is non-zero, the implementation assumes |
| 2356 | ** that the caller will keep calling incrVacuumStep() until |
| 2357 | ** it returns SQLITE_DONE or an error, and that nFin is the |
| 2358 | ** number of pages the database file will contain after this |
| 2359 | ** process is complete. |
| 2360 | */ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2361 | static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2362 | Pgno nFreeList; /* Number of pages still on the free-list */ |
| 2363 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2364 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2365 | |
| 2366 | if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){ |
| 2367 | int rc; |
| 2368 | u8 eType; |
| 2369 | Pgno iPtrPage; |
| 2370 | |
| 2371 | nFreeList = get4byte(&pBt->pPage1->aData[36]); |
| 2372 | if( nFreeList==0 || nFin==iLastPg ){ |
| 2373 | return SQLITE_DONE; |
| 2374 | } |
| 2375 | |
| 2376 | rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage); |
| 2377 | if( rc!=SQLITE_OK ){ |
| 2378 | return rc; |
| 2379 | } |
| 2380 | if( eType==PTRMAP_ROOTPAGE ){ |
| 2381 | return SQLITE_CORRUPT_BKPT; |
| 2382 | } |
| 2383 | |
| 2384 | if( eType==PTRMAP_FREEPAGE ){ |
| 2385 | if( nFin==0 ){ |
| 2386 | /* Remove the page from the files free-list. This is not required |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 2387 | ** if nFin is non-zero. In that case, the free-list will be |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2388 | ** truncated to zero after this function returns, so it doesn't |
| 2389 | ** matter if it still contains some garbage entries. |
| 2390 | */ |
| 2391 | Pgno iFreePg; |
| 2392 | MemPage *pFreePg; |
| 2393 | rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1); |
| 2394 | if( rc!=SQLITE_OK ){ |
| 2395 | return rc; |
| 2396 | } |
| 2397 | assert( iFreePg==iLastPg ); |
| 2398 | releasePage(pFreePg); |
| 2399 | } |
| 2400 | } else { |
| 2401 | Pgno iFreePg; /* Index of free page to move pLastPg to */ |
| 2402 | MemPage *pLastPg; |
| 2403 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2404 | rc = sqlite3BtreeGetPage(pBt, iLastPg, &pLastPg, 0); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2405 | if( rc!=SQLITE_OK ){ |
| 2406 | return rc; |
| 2407 | } |
| 2408 | |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 2409 | /* If nFin is zero, this loop runs exactly once and page pLastPg |
| 2410 | ** is swapped with the first free page pulled off the free list. |
| 2411 | ** |
| 2412 | ** On the other hand, if nFin is greater than zero, then keep |
| 2413 | ** looping until a free-page located within the first nFin pages |
| 2414 | ** of the file is found. |
| 2415 | */ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2416 | do { |
| 2417 | MemPage *pFreePg; |
| 2418 | rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0); |
| 2419 | if( rc!=SQLITE_OK ){ |
| 2420 | releasePage(pLastPg); |
| 2421 | return rc; |
| 2422 | } |
| 2423 | releasePage(pFreePg); |
| 2424 | }while( nFin!=0 && iFreePg>nFin ); |
| 2425 | assert( iFreePg<iLastPg ); |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 2426 | |
| 2427 | rc = sqlite3PagerWrite(pLastPg->pDbPage); |
danielk1977 | 662278e | 2007-11-05 15:30:12 +0000 | [diff] [blame] | 2428 | if( rc==SQLITE_OK ){ |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 2429 | rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, nFin!=0); |
danielk1977 | 662278e | 2007-11-05 15:30:12 +0000 | [diff] [blame] | 2430 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2431 | releasePage(pLastPg); |
| 2432 | if( rc!=SQLITE_OK ){ |
| 2433 | return rc; |
danielk1977 | 662278e | 2007-11-05 15:30:12 +0000 | [diff] [blame] | 2434 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2435 | } |
| 2436 | } |
| 2437 | |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2438 | if( nFin==0 ){ |
| 2439 | iLastPg--; |
| 2440 | while( iLastPg==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, iLastPg) ){ |
| 2441 | iLastPg--; |
| 2442 | } |
| 2443 | sqlite3PagerTruncateImage(pBt->pPager, iLastPg); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2444 | } |
| 2445 | return SQLITE_OK; |
| 2446 | } |
| 2447 | |
| 2448 | /* |
| 2449 | ** A write-transaction must be opened before calling this function. |
| 2450 | ** It performs a single unit of work towards an incremental vacuum. |
| 2451 | ** |
| 2452 | ** If the incremental vacuum is finished after this function has run, |
| 2453 | ** SQLITE_DONE is returned. If it is not finished, but no error occured, |
| 2454 | ** SQLITE_OK is returned. Otherwise an SQLite error code. |
| 2455 | */ |
| 2456 | int sqlite3BtreeIncrVacuum(Btree *p){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2457 | int rc; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2458 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2459 | |
| 2460 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2461 | pBt->db = p->db; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2462 | assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE ); |
| 2463 | if( !pBt->autoVacuum ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2464 | rc = SQLITE_DONE; |
| 2465 | }else{ |
| 2466 | invalidateAllOverflowCache(pBt); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 2467 | rc = incrVacuumStep(pBt, 0, pagerPagecount(pBt)); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2468 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2469 | sqlite3BtreeLeave(p); |
| 2470 | return rc; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2471 | } |
| 2472 | |
| 2473 | /* |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2474 | ** This routine is called prior to sqlite3PagerCommit when a transaction |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2475 | ** is commited for an auto-vacuum database. |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 2476 | ** |
| 2477 | ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages |
| 2478 | ** the database file should be truncated to during the commit process. |
| 2479 | ** i.e. the database has been reorganized so that only the first *pnTrunc |
| 2480 | ** pages are in use. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2481 | */ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2482 | static int autoVacuumCommit(BtShared *pBt){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2483 | int rc = SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2484 | Pager *pPager = pBt->pPager; |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 2485 | VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2486 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2487 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 2488 | invalidateAllOverflowCache(pBt); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2489 | assert(pBt->autoVacuum); |
| 2490 | if( !pBt->incrVacuum ){ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2491 | Pgno nFin; |
| 2492 | Pgno nFree; |
| 2493 | Pgno nPtrmap; |
| 2494 | Pgno iFree; |
| 2495 | const int pgsz = pBt->pageSize; |
| 2496 | Pgno nOrig = pagerPagecount(pBt); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2497 | |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2498 | if( PTRMAP_ISPAGE(pBt, nOrig) ){ |
| 2499 | return SQLITE_CORRUPT_BKPT; |
| 2500 | } |
| 2501 | if( nOrig==PENDING_BYTE_PAGE(pBt) ){ |
| 2502 | nOrig--; |
| 2503 | } |
| 2504 | nFree = get4byte(&pBt->pPage1->aData[36]); |
| 2505 | nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+pgsz/5)/(pgsz/5); |
| 2506 | nFin = nOrig - nFree - nPtrmap; |
| 2507 | if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<=PENDING_BYTE_PAGE(pBt) ){ |
| 2508 | nFin--; |
| 2509 | } |
| 2510 | while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){ |
| 2511 | nFin--; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2512 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2513 | |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2514 | for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){ |
| 2515 | rc = incrVacuumStep(pBt, nFin, iFree); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2516 | } |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2517 | if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2518 | rc = SQLITE_OK; |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2519 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 2520 | put4byte(&pBt->pPage1->aData[32], 0); |
| 2521 | put4byte(&pBt->pPage1->aData[36], 0); |
| 2522 | sqlite3PagerTruncateImage(pBt->pPager, nFin); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2523 | } |
| 2524 | if( rc!=SQLITE_OK ){ |
| 2525 | sqlite3PagerRollback(pPager); |
| 2526 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2527 | } |
| 2528 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2529 | assert( nRef==sqlite3PagerRefcount(pPager) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2530 | return rc; |
| 2531 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2532 | |
shane | 831c329 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 2533 | #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2534 | |
| 2535 | /* |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2536 | ** This routine does the first phase of a two-phase commit. This routine |
| 2537 | ** causes a rollback journal to be created (if it does not already exist) |
| 2538 | ** and populated with enough information so that if a power loss occurs |
| 2539 | ** the database can be restored to its original state by playing back |
| 2540 | ** the journal. Then the contents of the journal are flushed out to |
| 2541 | ** the disk. After the journal is safely on oxide, the changes to the |
| 2542 | ** database are written into the database file and flushed to oxide. |
| 2543 | ** At the end of this call, the rollback journal still exists on the |
| 2544 | ** disk and we are still holding all locks, so the transaction has not |
| 2545 | ** committed. See sqlite3BtreeCommit() for the second phase of the |
| 2546 | ** commit process. |
| 2547 | ** |
| 2548 | ** This call is a no-op if no write-transaction is currently active on pBt. |
| 2549 | ** |
| 2550 | ** Otherwise, sync the database file for the btree pBt. zMaster points to |
| 2551 | ** the name of a master journal file that should be written into the |
| 2552 | ** individual journal file, or is NULL, indicating no master journal file |
| 2553 | ** (single database transaction). |
| 2554 | ** |
| 2555 | ** When this is called, the master journal should already have been |
| 2556 | ** created, populated with this journal pointer and synced to disk. |
| 2557 | ** |
| 2558 | ** Once this is routine has returned, the only thing required to commit |
| 2559 | ** the write-transaction for this database file is to delete the journal. |
| 2560 | */ |
| 2561 | int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){ |
| 2562 | int rc = SQLITE_OK; |
| 2563 | if( p->inTrans==TRANS_WRITE ){ |
| 2564 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2565 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2566 | pBt->db = p->db; |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2567 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2568 | if( pBt->autoVacuum ){ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2569 | rc = autoVacuumCommit(pBt); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2570 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2571 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2572 | return rc; |
| 2573 | } |
| 2574 | } |
| 2575 | #endif |
drh | 49b9d33 | 2009-01-02 18:10:42 +0000 | [diff] [blame] | 2576 | rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2577 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2578 | } |
| 2579 | return rc; |
| 2580 | } |
| 2581 | |
| 2582 | /* |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2583 | ** Commit the transaction currently in progress. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2584 | ** |
drh | 6e34599 | 2007-03-30 11:12:08 +0000 | [diff] [blame] | 2585 | ** This routine implements the second phase of a 2-phase commit. The |
| 2586 | ** sqlite3BtreeSync() routine does the first phase and should be invoked |
| 2587 | ** prior to calling this routine. The sqlite3BtreeSync() routine did |
| 2588 | ** all the work of writing information out to disk and flushing the |
| 2589 | ** contents so that they are written onto the disk platter. All this |
| 2590 | ** routine has to do is delete or truncate the rollback journal |
| 2591 | ** (which causes the transaction to commit) and drop locks. |
| 2592 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2593 | ** This will release the write lock on the database file. If there |
| 2594 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2595 | */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2596 | int sqlite3BtreeCommitPhaseTwo(Btree *p){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2597 | BtShared *pBt = p->pBt; |
| 2598 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2599 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2600 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2601 | btreeIntegrity(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2602 | |
| 2603 | /* If the handle has a write-transaction open, commit the shared-btrees |
| 2604 | ** transaction and set the shared state to TRANS_READ. |
| 2605 | */ |
| 2606 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2607 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2608 | assert( pBt->inTransaction==TRANS_WRITE ); |
| 2609 | assert( pBt->nTransaction>0 ); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2610 | rc = sqlite3PagerCommitPhaseTwo(pBt->pPager); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2611 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2612 | sqlite3BtreeLeave(p); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2613 | return rc; |
| 2614 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2615 | pBt->inTransaction = TRANS_READ; |
| 2616 | pBt->inStmt = 0; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2617 | } |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 2618 | clearAllSharedCacheTableLocks(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2619 | |
| 2620 | /* If the handle has any kind of transaction open, decrement the transaction |
| 2621 | ** count of the shared btree. If the transaction count reaches 0, set |
| 2622 | ** the shared state to TRANS_NONE. The unlockBtreeIfUnused() call below |
| 2623 | ** will unlock the pager. |
| 2624 | */ |
| 2625 | if( p->inTrans!=TRANS_NONE ){ |
| 2626 | pBt->nTransaction--; |
| 2627 | if( 0==pBt->nTransaction ){ |
| 2628 | pBt->inTransaction = TRANS_NONE; |
| 2629 | } |
| 2630 | } |
| 2631 | |
| 2632 | /* Set the handles current transaction state to TRANS_NONE and unlock |
| 2633 | ** the pager if this call closed the only read or write transaction. |
| 2634 | */ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 2635 | btreeClearHasContent(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2636 | p->inTrans = TRANS_NONE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2637 | unlockBtreeIfUnused(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2638 | |
| 2639 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2640 | sqlite3BtreeLeave(p); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2641 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2642 | } |
| 2643 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2644 | /* |
| 2645 | ** Do both phases of a commit. |
| 2646 | */ |
| 2647 | int sqlite3BtreeCommit(Btree *p){ |
| 2648 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2649 | sqlite3BtreeEnter(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2650 | rc = sqlite3BtreeCommitPhaseOne(p, 0); |
| 2651 | if( rc==SQLITE_OK ){ |
| 2652 | rc = sqlite3BtreeCommitPhaseTwo(p); |
| 2653 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2654 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2655 | return rc; |
| 2656 | } |
| 2657 | |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2658 | #ifndef NDEBUG |
| 2659 | /* |
| 2660 | ** Return the number of write-cursors open on this handle. This is for use |
| 2661 | ** in assert() expressions, so it is only compiled if NDEBUG is not |
| 2662 | ** defined. |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2663 | ** |
| 2664 | ** For the purposes of this routine, a write-cursor is any cursor that |
| 2665 | ** is capable of writing to the databse. That means the cursor was |
| 2666 | ** originally opened for writing and the cursor has not be disabled |
| 2667 | ** by having its state changed to CURSOR_FAULT. |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2668 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2669 | static int countWriteCursors(BtShared *pBt){ |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2670 | BtCursor *pCur; |
| 2671 | int r = 0; |
| 2672 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2673 | if( pCur->wrFlag && pCur->eState!=CURSOR_FAULT ) r++; |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2674 | } |
| 2675 | return r; |
| 2676 | } |
| 2677 | #endif |
| 2678 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2679 | /* |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2680 | ** This routine sets the state to CURSOR_FAULT and the error |
| 2681 | ** code to errCode for every cursor on BtShared that pBtree |
| 2682 | ** references. |
| 2683 | ** |
| 2684 | ** Every cursor is tripped, including cursors that belong |
| 2685 | ** to other database connections that happen to be sharing |
| 2686 | ** the cache with pBtree. |
| 2687 | ** |
| 2688 | ** This routine gets called when a rollback occurs. |
| 2689 | ** All cursors using the same cache must be tripped |
| 2690 | ** to prevent them from trying to use the btree after |
| 2691 | ** the rollback. The rollback may have deleted tables |
| 2692 | ** or moved root pages, so it is not sufficient to |
| 2693 | ** save the state of the cursor. The cursor must be |
| 2694 | ** invalidated. |
| 2695 | */ |
| 2696 | void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){ |
| 2697 | BtCursor *p; |
| 2698 | sqlite3BtreeEnter(pBtree); |
| 2699 | for(p=pBtree->pBt->pCursor; p; p=p->pNext){ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 2700 | int i; |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 2701 | sqlite3BtreeClearCursor(p); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2702 | p->eState = CURSOR_FAULT; |
| 2703 | p->skip = errCode; |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 2704 | for(i=0; i<=p->iPage; i++){ |
| 2705 | releasePage(p->apPage[i]); |
| 2706 | p->apPage[i] = 0; |
| 2707 | } |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2708 | } |
| 2709 | sqlite3BtreeLeave(pBtree); |
| 2710 | } |
| 2711 | |
| 2712 | /* |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2713 | ** Rollback the transaction in progress. All cursors will be |
| 2714 | ** invalided by this operation. Any attempt to use a cursor |
| 2715 | ** that was open at the beginning of this operation will result |
| 2716 | ** in an error. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2717 | ** |
| 2718 | ** This will release the write lock on the database file. If there |
| 2719 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2720 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2721 | int sqlite3BtreeRollback(Btree *p){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2722 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2723 | BtShared *pBt = p->pBt; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2724 | MemPage *pPage1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2725 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2726 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2727 | pBt->db = p->db; |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2728 | rc = saveAllCursors(pBt, 0, 0); |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2729 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2730 | if( rc!=SQLITE_OK ){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2731 | /* This is a horrible situation. An IO or malloc() error occured whilst |
| 2732 | ** trying to save cursor positions. If this is an automatic rollback (as |
| 2733 | ** the result of a constraint, malloc() failure or IO error) then |
| 2734 | ** the cache may be internally inconsistent (not contain valid trees) so |
| 2735 | ** we cannot simply return the error to the caller. Instead, abort |
| 2736 | ** all queries that may be using any of the cursors that failed to save. |
| 2737 | */ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2738 | sqlite3BtreeTripAllCursors(p, rc); |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2739 | } |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2740 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2741 | btreeIntegrity(p); |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 2742 | clearAllSharedCacheTableLocks(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2743 | |
| 2744 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2745 | int rc2; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2746 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2747 | assert( TRANS_WRITE==pBt->inTransaction ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2748 | rc2 = sqlite3PagerRollback(pBt->pPager); |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2749 | if( rc2!=SQLITE_OK ){ |
| 2750 | rc = rc2; |
| 2751 | } |
| 2752 | |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2753 | /* The rollback may have destroyed the pPage1->aData value. So |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2754 | ** call sqlite3BtreeGetPage() on page 1 again to make |
| 2755 | ** sure pPage1->aData is set correctly. */ |
| 2756 | if( sqlite3BtreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2757 | releasePage(pPage1); |
| 2758 | } |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2759 | assert( countWriteCursors(pBt)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2760 | pBt->inTransaction = TRANS_READ; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2761 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2762 | |
| 2763 | if( p->inTrans!=TRANS_NONE ){ |
| 2764 | assert( pBt->nTransaction>0 ); |
| 2765 | pBt->nTransaction--; |
| 2766 | if( 0==pBt->nTransaction ){ |
| 2767 | pBt->inTransaction = TRANS_NONE; |
| 2768 | } |
| 2769 | } |
| 2770 | |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 2771 | btreeClearHasContent(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2772 | p->inTrans = TRANS_NONE; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2773 | pBt->inStmt = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2774 | unlockBtreeIfUnused(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2775 | |
| 2776 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2777 | sqlite3BtreeLeave(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2778 | return rc; |
| 2779 | } |
| 2780 | |
| 2781 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2782 | ** Start a statement subtransaction. The subtransaction can |
| 2783 | ** can be rolled back independently of the main transaction. |
| 2784 | ** You must start a transaction before starting a subtransaction. |
| 2785 | ** The subtransaction is ended automatically if the main transaction |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2786 | ** commits or rolls back. |
| 2787 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2788 | ** Only one subtransaction may be active at a time. It is an error to try |
| 2789 | ** to start a new subtransaction if another subtransaction is already active. |
| 2790 | ** |
| 2791 | ** Statement subtransactions are used around individual SQL statements |
| 2792 | ** that are contained within a BEGIN...COMMIT block. If a constraint |
| 2793 | ** error occurs within the statement, the effect of that one statement |
| 2794 | ** can be rolled back without having to rollback the entire transaction. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2795 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2796 | int sqlite3BtreeBeginStmt(Btree *p){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2797 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2798 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2799 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2800 | pBt->db = p->db; |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 2801 | assert( p->inTrans==TRANS_WRITE ); |
| 2802 | assert( !pBt->inStmt ); |
| 2803 | assert( pBt->readOnly==0 ); |
| 2804 | if( NEVER(p->inTrans!=TRANS_WRITE || pBt->inStmt || pBt->readOnly) ){ |
| 2805 | rc = SQLITE_INTERNAL; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2806 | }else{ |
| 2807 | assert( pBt->inTransaction==TRANS_WRITE ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 2808 | /* At the pager level, a statement transaction is a savepoint with |
| 2809 | ** an index greater than all savepoints created explicitly using |
| 2810 | ** SQL statements. It is illegal to open, release or rollback any |
| 2811 | ** such savepoints while the statement transaction savepoint is active. |
| 2812 | */ |
| 2813 | rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint+1); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2814 | pBt->inStmt = 1; |
drh | 0d65dc0 | 2002-02-03 00:56:09 +0000 | [diff] [blame] | 2815 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2816 | sqlite3BtreeLeave(p); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2817 | return rc; |
| 2818 | } |
| 2819 | |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2820 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2821 | ** Commit the statment subtransaction currently in progress. If no |
| 2822 | ** subtransaction is active, this is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2823 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2824 | int sqlite3BtreeCommitStmt(Btree *p){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2825 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2826 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2827 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2828 | pBt->db = p->db; |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 2829 | assert( pBt->readOnly==0 ); |
| 2830 | if( pBt->inStmt ){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2831 | int iStmtpoint = p->db->nSavepoint; |
| 2832 | rc = sqlite3PagerSavepoint(pBt->pPager, SAVEPOINT_RELEASE, iStmtpoint); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2833 | }else{ |
| 2834 | rc = SQLITE_OK; |
| 2835 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2836 | pBt->inStmt = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2837 | sqlite3BtreeLeave(p); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2838 | return rc; |
| 2839 | } |
| 2840 | |
| 2841 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2842 | ** Rollback the active statement subtransaction. If no subtransaction |
| 2843 | ** is active this routine is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2844 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2845 | ** All cursors will be invalidated by this operation. Any attempt |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2846 | ** to use a cursor that was open at the beginning of this operation |
| 2847 | ** will result in an error. |
| 2848 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2849 | int sqlite3BtreeRollbackStmt(Btree *p){ |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2850 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2851 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2852 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2853 | pBt->db = p->db; |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 2854 | assert( pBt->readOnly==0 ); |
| 2855 | if( pBt->inStmt ){ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2856 | int iStmtpoint = p->db->nSavepoint; |
| 2857 | rc = sqlite3PagerSavepoint(pBt->pPager, SAVEPOINT_ROLLBACK, iStmtpoint); |
| 2858 | if( rc==SQLITE_OK ){ |
| 2859 | rc = sqlite3PagerSavepoint(pBt->pPager, SAVEPOINT_RELEASE, iStmtpoint); |
| 2860 | } |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2861 | pBt->inStmt = 0; |
| 2862 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2863 | sqlite3BtreeLeave(p); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2864 | return rc; |
| 2865 | } |
| 2866 | |
| 2867 | /* |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2868 | ** The second argument to this function, op, is always SAVEPOINT_ROLLBACK |
| 2869 | ** or SAVEPOINT_RELEASE. This function either releases or rolls back the |
danielk1977 | 12dd549 | 2008-12-18 15:45:07 +0000 | [diff] [blame] | 2870 | ** savepoint identified by parameter iSavepoint, depending on the value |
| 2871 | ** of op. |
| 2872 | ** |
| 2873 | ** Normally, iSavepoint is greater than or equal to zero. However, if op is |
| 2874 | ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the |
| 2875 | ** contents of the entire transaction are rolled back. This is different |
| 2876 | ** from a normal transaction rollback, as no locks are released and the |
| 2877 | ** transaction remains open. |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2878 | */ |
| 2879 | int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){ |
| 2880 | int rc = SQLITE_OK; |
| 2881 | if( p && p->inTrans==TRANS_WRITE ){ |
| 2882 | BtShared *pBt = p->pBt; |
| 2883 | assert( pBt->inStmt==0 ); |
| 2884 | assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK ); |
| 2885 | assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) ); |
| 2886 | sqlite3BtreeEnter(p); |
| 2887 | pBt->db = p->db; |
| 2888 | rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint); |
drh | 9f0bbf9 | 2009-01-02 21:08:09 +0000 | [diff] [blame] | 2889 | if( rc==SQLITE_OK ){ |
| 2890 | rc = newDatabase(pBt); |
| 2891 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2892 | sqlite3BtreeLeave(p); |
| 2893 | } |
| 2894 | return rc; |
| 2895 | } |
| 2896 | |
| 2897 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2898 | ** Create a new cursor for the BTree whose root is on the page |
| 2899 | ** iTable. The act of acquiring a cursor gets a read lock on |
| 2900 | ** the database file. |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 2901 | ** |
| 2902 | ** If wrFlag==0, then the cursor can only be used for reading. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2903 | ** If wrFlag==1, then the cursor can be used for reading or for |
| 2904 | ** writing if other conditions for writing are also met. These |
| 2905 | ** are the conditions that must be met in order for writing to |
| 2906 | ** be allowed: |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2907 | ** |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2908 | ** 1: The cursor must have been opened with wrFlag==1 |
| 2909 | ** |
drh | fe5d71d | 2007-03-19 11:54:10 +0000 | [diff] [blame] | 2910 | ** 2: Other database connections that share the same pager cache |
| 2911 | ** but which are not in the READ_UNCOMMITTED state may not have |
| 2912 | ** cursors open with wrFlag==0 on the same table. Otherwise |
| 2913 | ** the changes made by this write cursor would be visible to |
| 2914 | ** the read cursors in the other database connection. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2915 | ** |
| 2916 | ** 3: The database must be writable (not on read-only media) |
| 2917 | ** |
| 2918 | ** 4: There must be an active transaction. |
| 2919 | ** |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2920 | ** No checking is done to make sure that page iTable really is the |
| 2921 | ** root page of a b-tree. If it is not, then the cursor acquired |
| 2922 | ** will not work correctly. |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2923 | ** |
| 2924 | ** It is assumed that the sqlite3BtreeCursorSize() bytes of memory |
| 2925 | ** pointed to by pCur have been zeroed by the caller. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2926 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2927 | static int btreeCursor( |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2928 | Btree *p, /* The btree */ |
| 2929 | int iTable, /* Root page of table to open */ |
| 2930 | int wrFlag, /* 1 to write. 0 read-only */ |
| 2931 | struct KeyInfo *pKeyInfo, /* First arg to comparison function */ |
| 2932 | BtCursor *pCur /* Space for new cursor */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2933 | ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2934 | int rc; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 2935 | Pgno nPage; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2936 | BtShared *pBt = p->pBt; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2937 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2938 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 2939 | assert( wrFlag==0 || wrFlag==1 ); |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2940 | if( wrFlag ){ |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 2941 | assert( !pBt->readOnly ); |
| 2942 | if( NEVER(pBt->readOnly) ){ |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2943 | return SQLITE_READONLY; |
| 2944 | } |
drh | 11b57d6 | 2009-02-24 19:21:41 +0000 | [diff] [blame^] | 2945 | if( checkForReadConflicts(p, iTable, 0, 0) ){ |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2946 | return SQLITE_LOCKED; |
| 2947 | } |
drh | a0c9a11 | 2004-03-10 13:42:37 +0000 | [diff] [blame] | 2948 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2949 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2950 | if( pBt->pPage1==0 ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2951 | rc = lockBtreeWithRetry(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2952 | if( rc!=SQLITE_OK ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2953 | return rc; |
| 2954 | } |
| 2955 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2956 | pCur->pgnoRoot = (Pgno)iTable; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 2957 | rc = sqlite3PagerPagecount(pBt->pPager, (int *)&nPage); |
| 2958 | if( rc!=SQLITE_OK ){ |
| 2959 | return rc; |
| 2960 | } |
| 2961 | if( iTable==1 && nPage==0 ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2962 | rc = SQLITE_EMPTY; |
| 2963 | goto create_cursor_exception; |
| 2964 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2965 | rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2966 | if( rc!=SQLITE_OK ){ |
| 2967 | goto create_cursor_exception; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2968 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2969 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2970 | /* Now that no other errors can occur, finish filling in the BtCursor |
| 2971 | ** variables, link the cursor into the BtShared list and set *ppCur (the |
| 2972 | ** output argument to this function). |
| 2973 | */ |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2974 | pCur->pKeyInfo = pKeyInfo; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2975 | pCur->pBtree = p; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 2976 | pCur->pBt = pBt; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 2977 | pCur->wrFlag = (u8)wrFlag; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2978 | pCur->pNext = pBt->pCursor; |
| 2979 | if( pCur->pNext ){ |
| 2980 | pCur->pNext->pPrev = pCur; |
| 2981 | } |
| 2982 | pBt->pCursor = pCur; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2983 | pCur->eState = CURSOR_INVALID; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2984 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2985 | return SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2986 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2987 | create_cursor_exception: |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2988 | releasePage(pCur->apPage[0]); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2989 | unlockBtreeIfUnused(pBt); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2990 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2991 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2992 | int sqlite3BtreeCursor( |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2993 | Btree *p, /* The btree */ |
| 2994 | int iTable, /* Root page of table to open */ |
| 2995 | int wrFlag, /* 1 to write. 0 read-only */ |
| 2996 | struct KeyInfo *pKeyInfo, /* First arg to xCompare() */ |
| 2997 | BtCursor *pCur /* Write new cursor here */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2998 | ){ |
| 2999 | int rc; |
| 3000 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3001 | p->pBt->db = p->db; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3002 | rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3003 | sqlite3BtreeLeave(p); |
| 3004 | return rc; |
| 3005 | } |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3006 | int sqlite3BtreeCursorSize(){ |
| 3007 | return sizeof(BtCursor); |
| 3008 | } |
| 3009 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3010 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3011 | |
| 3012 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3013 | ** Close a cursor. The read lock on the database file is released |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3014 | ** when the last cursor is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3015 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3016 | int sqlite3BtreeCloseCursor(BtCursor *pCur){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 3017 | Btree *pBtree = pCur->pBtree; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3018 | if( pBtree ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3019 | int i; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3020 | BtShared *pBt = pCur->pBt; |
| 3021 | sqlite3BtreeEnter(pBtree); |
| 3022 | pBt->db = pBtree->db; |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 3023 | sqlite3BtreeClearCursor(pCur); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3024 | if( pCur->pPrev ){ |
| 3025 | pCur->pPrev->pNext = pCur->pNext; |
| 3026 | }else{ |
| 3027 | pBt->pCursor = pCur->pNext; |
| 3028 | } |
| 3029 | if( pCur->pNext ){ |
| 3030 | pCur->pNext->pPrev = pCur->pPrev; |
| 3031 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3032 | for(i=0; i<=pCur->iPage; i++){ |
| 3033 | releasePage(pCur->apPage[i]); |
| 3034 | } |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3035 | unlockBtreeIfUnused(pBt); |
| 3036 | invalidateOverflowCache(pCur); |
| 3037 | /* sqlite3_free(pCur); */ |
| 3038 | sqlite3BtreeLeave(pBtree); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3039 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3040 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3041 | } |
| 3042 | |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3043 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3044 | ** Make a temporary cursor by filling in the fields of pTempCur. |
| 3045 | ** The temporary cursor is not on the cursor list for the Btree. |
| 3046 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3047 | void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3048 | int i; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3049 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3050 | memcpy(pTempCur, pCur, sizeof(BtCursor)); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3051 | pTempCur->pNext = 0; |
| 3052 | pTempCur->pPrev = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3053 | for(i=0; i<=pTempCur->iPage; i++){ |
| 3054 | sqlite3PagerRef(pTempCur->apPage[i]->pDbPage); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3055 | } |
danielk1977 | 36e2093 | 2008-11-26 07:40:30 +0000 | [diff] [blame] | 3056 | assert( pTempCur->pKey==0 ); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3057 | } |
| 3058 | |
| 3059 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3060 | ** Delete a temporary cursor such as was made by the CreateTemporaryCursor() |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3061 | ** function above. |
| 3062 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3063 | void sqlite3BtreeReleaseTempCursor(BtCursor *pCur){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3064 | int i; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3065 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3066 | for(i=0; i<=pCur->iPage; i++){ |
| 3067 | sqlite3PagerUnref(pCur->apPage[i]->pDbPage); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3068 | } |
danielk1977 | 36e2093 | 2008-11-26 07:40:30 +0000 | [diff] [blame] | 3069 | sqlite3_free(pCur->pKey); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3070 | } |
| 3071 | |
| 3072 | /* |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3073 | ** Make sure the BtCursor* given in the argument has a valid |
| 3074 | ** BtCursor.info structure. If it is not already valid, call |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3075 | ** sqlite3BtreeParseCell() to fill it in. |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 3076 | ** |
| 3077 | ** BtCursor.info is a cache of the information in the current cell. |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3078 | ** Using this cache reduces the number of calls to sqlite3BtreeParseCell(). |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3079 | ** |
| 3080 | ** 2007-06-25: There is a bug in some versions of MSVC that cause the |
| 3081 | ** compiler to crash when getCellInfo() is implemented as a macro. |
| 3082 | ** But there is a measureable speed advantage to using the macro on gcc |
| 3083 | ** (when less compiler optimizations like -Os or -O0 are used and the |
| 3084 | ** compiler is not doing agressive inlining.) So we use a real function |
| 3085 | ** for MSVC and a macro for everything else. Ticket #2457. |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3086 | */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3087 | #ifndef NDEBUG |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3088 | static void assertCellInfo(BtCursor *pCur){ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3089 | CellInfo info; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3090 | int iPage = pCur->iPage; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 3091 | memset(&info, 0, sizeof(info)); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3092 | sqlite3BtreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3093 | assert( memcmp(&info, &pCur->info, sizeof(info))==0 ); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3094 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3095 | #else |
| 3096 | #define assertCellInfo(x) |
| 3097 | #endif |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3098 | #ifdef _MSC_VER |
| 3099 | /* Use a real function in MSVC to work around bugs in that compiler. */ |
| 3100 | static void getCellInfo(BtCursor *pCur){ |
| 3101 | if( pCur->info.nSize==0 ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3102 | int iPage = pCur->iPage; |
| 3103 | sqlite3BtreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3104 | pCur->validNKey = 1; |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3105 | }else{ |
| 3106 | assertCellInfo(pCur); |
| 3107 | } |
| 3108 | } |
| 3109 | #else /* if not _MSC_VER */ |
| 3110 | /* Use a macro in all other compilers so that the function is inlined */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3111 | #define getCellInfo(pCur) \ |
| 3112 | if( pCur->info.nSize==0 ){ \ |
| 3113 | int iPage = pCur->iPage; \ |
| 3114 | sqlite3BtreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); \ |
| 3115 | pCur->validNKey = 1; \ |
| 3116 | }else{ \ |
| 3117 | assertCellInfo(pCur); \ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3118 | } |
| 3119 | #endif /* _MSC_VER */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3120 | |
| 3121 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3122 | ** Set *pSize to the size of the buffer needed to hold the value of |
| 3123 | ** the key for the current entry. If the cursor is not pointing |
| 3124 | ** to a valid entry, *pSize is set to 0. |
| 3125 | ** |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3126 | ** For a table with the INTKEY flag set, this routine returns the key |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3127 | ** itself, not the number of bytes in the key. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3128 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3129 | int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3130 | int rc; |
| 3131 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3132 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 3133 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3134 | if( rc==SQLITE_OK ){ |
| 3135 | assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID ); |
| 3136 | if( pCur->eState==CURSOR_INVALID ){ |
| 3137 | *pSize = 0; |
| 3138 | }else{ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3139 | getCellInfo(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3140 | *pSize = pCur->info.nKey; |
| 3141 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3142 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3143 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3144 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3145 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3146 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3147 | ** Set *pSize to the number of bytes of data in the entry the |
| 3148 | ** cursor currently points to. Always return SQLITE_OK. |
| 3149 | ** Failure is not possible. If the cursor is not currently |
| 3150 | ** pointing to an entry (which can happen, for example, if |
| 3151 | ** the database is empty) then *pSize is set to 0. |
| 3152 | */ |
| 3153 | int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3154 | int rc; |
| 3155 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3156 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 3157 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3158 | if( rc==SQLITE_OK ){ |
| 3159 | assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID ); |
| 3160 | if( pCur->eState==CURSOR_INVALID ){ |
| 3161 | /* Not pointing at a valid entry - set *pSize to 0. */ |
| 3162 | *pSize = 0; |
| 3163 | }else{ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3164 | getCellInfo(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3165 | *pSize = pCur->info.nData; |
| 3166 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3167 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3168 | return rc; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3169 | } |
| 3170 | |
| 3171 | /* |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3172 | ** Given the page number of an overflow page in the database (parameter |
| 3173 | ** ovfl), this function finds the page number of the next page in the |
| 3174 | ** linked list of overflow pages. If possible, it uses the auto-vacuum |
| 3175 | ** pointer-map data instead of reading the content of page ovfl to do so. |
| 3176 | ** |
| 3177 | ** If an error occurs an SQLite error code is returned. Otherwise: |
| 3178 | ** |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3179 | ** The page number of the next overflow page in the linked list is |
| 3180 | ** written to *pPgnoNext. If page ovfl is the last page in its linked |
| 3181 | ** list, *pPgnoNext is set to zero. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3182 | ** |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3183 | ** If ppPage is not NULL, and a reference to the MemPage object corresponding |
| 3184 | ** to page number pOvfl was obtained, then *ppPage is set to point to that |
| 3185 | ** reference. It is the responsibility of the caller to call releasePage() |
| 3186 | ** on *ppPage to free the reference. In no reference was obtained (because |
| 3187 | ** the pointer-map was used to obtain the value for *pPgnoNext), then |
| 3188 | ** *ppPage is set to zero. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3189 | */ |
| 3190 | static int getOverflowPage( |
| 3191 | BtShared *pBt, |
| 3192 | Pgno ovfl, /* Overflow page */ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3193 | MemPage **ppPage, /* OUT: MemPage handle (may be NULL) */ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3194 | Pgno *pPgnoNext /* OUT: Next overflow page number */ |
| 3195 | ){ |
| 3196 | Pgno next = 0; |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3197 | MemPage *pPage = 0; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 3198 | int rc = SQLITE_OK; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3199 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3200 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3201 | assert(pPgnoNext); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3202 | |
| 3203 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3204 | /* Try to find the next page in the overflow list using the |
| 3205 | ** autovacuum pointer-map pages. Guess that the next page in |
| 3206 | ** the overflow list is page number (ovfl+1). If that guess turns |
| 3207 | ** out to be wrong, fall back to loading the data of page |
| 3208 | ** number ovfl to determine the next page number. |
| 3209 | */ |
| 3210 | if( pBt->autoVacuum ){ |
| 3211 | Pgno pgno; |
| 3212 | Pgno iGuess = ovfl+1; |
| 3213 | u8 eType; |
| 3214 | |
| 3215 | while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){ |
| 3216 | iGuess++; |
| 3217 | } |
| 3218 | |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 3219 | if( iGuess<=pagerPagecount(pBt) ){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3220 | rc = ptrmapGet(pBt, iGuess, &eType, &pgno); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3221 | if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3222 | next = iGuess; |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3223 | rc = SQLITE_DONE; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3224 | } |
| 3225 | } |
| 3226 | } |
| 3227 | #endif |
| 3228 | |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3229 | if( rc==SQLITE_OK ){ |
| 3230 | rc = sqlite3BtreeGetPage(pBt, ovfl, &pPage, 0); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3231 | assert(rc==SQLITE_OK || pPage==0); |
| 3232 | if( next==0 && rc==SQLITE_OK ){ |
| 3233 | next = get4byte(pPage->aData); |
| 3234 | } |
danielk1977 | 443c059 | 2009-01-16 15:21:05 +0000 | [diff] [blame] | 3235 | } |
danielk1977 | 45d6882 | 2009-01-16 16:23:38 +0000 | [diff] [blame] | 3236 | |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3237 | *pPgnoNext = next; |
| 3238 | if( ppPage ){ |
| 3239 | *ppPage = pPage; |
| 3240 | }else{ |
| 3241 | releasePage(pPage); |
| 3242 | } |
| 3243 | return (rc==SQLITE_DONE ? SQLITE_OK : rc); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3244 | } |
| 3245 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3246 | /* |
| 3247 | ** Copy data from a buffer to a page, or from a page to a buffer. |
| 3248 | ** |
| 3249 | ** pPayload is a pointer to data stored on database page pDbPage. |
| 3250 | ** If argument eOp is false, then nByte bytes of data are copied |
| 3251 | ** from pPayload to the buffer pointed at by pBuf. If eOp is true, |
| 3252 | ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes |
| 3253 | ** of data are copied from the buffer pBuf to pPayload. |
| 3254 | ** |
| 3255 | ** SQLITE_OK is returned on success, otherwise an error code. |
| 3256 | */ |
| 3257 | static int copyPayload( |
| 3258 | void *pPayload, /* Pointer to page data */ |
| 3259 | void *pBuf, /* Pointer to buffer */ |
| 3260 | int nByte, /* Number of bytes to copy */ |
| 3261 | int eOp, /* 0 -> copy from page, 1 -> copy to page */ |
| 3262 | DbPage *pDbPage /* Page containing pPayload */ |
| 3263 | ){ |
| 3264 | if( eOp ){ |
| 3265 | /* Copy data from buffer to page (a write operation) */ |
| 3266 | int rc = sqlite3PagerWrite(pDbPage); |
| 3267 | if( rc!=SQLITE_OK ){ |
| 3268 | return rc; |
| 3269 | } |
| 3270 | memcpy(pPayload, pBuf, nByte); |
| 3271 | }else{ |
| 3272 | /* Copy data from page to buffer (a read operation) */ |
| 3273 | memcpy(pBuf, pPayload, nByte); |
| 3274 | } |
| 3275 | return SQLITE_OK; |
| 3276 | } |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3277 | |
| 3278 | /* |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3279 | ** This function is used to read or overwrite payload information |
| 3280 | ** for the entry that the pCur cursor is pointing to. If the eOp |
| 3281 | ** parameter is 0, this is a read operation (data copied into |
| 3282 | ** buffer pBuf). If it is non-zero, a write (data copied from |
| 3283 | ** buffer pBuf). |
| 3284 | ** |
| 3285 | ** A total of "amt" bytes are read or written beginning at "offset". |
| 3286 | ** Data is read to or from the buffer pBuf. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3287 | ** |
| 3288 | ** This routine does not make a distinction between key and data. |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3289 | ** It just reads or writes bytes from the payload area. Data might |
| 3290 | ** appear on the main page or be scattered out on multiple overflow |
| 3291 | ** pages. |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3292 | ** |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 3293 | ** If the BtCursor.isIncrblobHandle flag is set, and the current |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3294 | ** cursor entry uses one or more overflow pages, this function |
| 3295 | ** allocates space for and lazily popluates the overflow page-list |
| 3296 | ** cache array (BtCursor.aOverflow). Subsequent calls use this |
| 3297 | ** cache to make seeking to the supplied offset more efficient. |
| 3298 | ** |
| 3299 | ** Once an overflow page-list cache has been allocated, it may be |
| 3300 | ** invalidated if some other cursor writes to the same table, or if |
| 3301 | ** the cursor is moved to a different row. Additionally, in auto-vacuum |
| 3302 | ** mode, the following events may invalidate an overflow page-list cache. |
| 3303 | ** |
| 3304 | ** * An incremental vacuum, |
| 3305 | ** * A commit in auto_vacuum="full" mode, |
| 3306 | ** * Creating a table (may require moving an overflow page). |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3307 | */ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3308 | static int accessPayload( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3309 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 3310 | u32 offset, /* Begin reading this far into payload */ |
| 3311 | u32 amt, /* Read this many bytes */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3312 | unsigned char *pBuf, /* Write the bytes into this buffer */ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3313 | int skipKey, /* offset begins at data if this is true */ |
| 3314 | int eOp /* zero to read. non-zero to write. */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3315 | ){ |
| 3316 | unsigned char *aPayload; |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3317 | int rc = SQLITE_OK; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3318 | u32 nKey; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3319 | int iIdx = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3320 | MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */ |
danielk1977 | 0d06541 | 2008-11-12 18:21:36 +0000 | [diff] [blame] | 3321 | BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3322 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3323 | assert( pPage ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3324 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3325 | assert( pCur->aiIdx[pCur->iPage]<pPage->nCell ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3326 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3327 | |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3328 | getCellInfo(pCur); |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3329 | aPayload = pCur->info.pCell + pCur->info.nHeader; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3330 | nKey = (pPage->intKey ? 0 : (int)pCur->info.nKey); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3331 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3332 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3333 | offset += nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3334 | } |
danielk1977 | 0d06541 | 2008-11-12 18:21:36 +0000 | [diff] [blame] | 3335 | if( offset+amt > nKey+pCur->info.nData |
| 3336 | || &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize] |
| 3337 | ){ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3338 | /* Trying to read or write past the end of the data is an error */ |
danielk1977 | 67fd7a9 | 2008-09-10 17:53:35 +0000 | [diff] [blame] | 3339 | return SQLITE_CORRUPT_BKPT; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3340 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3341 | |
| 3342 | /* Check if data must be read/written to/from the btree page itself. */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3343 | if( offset<pCur->info.nLocal ){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3344 | int a = amt; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3345 | if( a+offset>pCur->info.nLocal ){ |
| 3346 | a = pCur->info.nLocal - offset; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3347 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3348 | rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3349 | offset = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3350 | pBuf += a; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3351 | amt -= a; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 3352 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3353 | offset -= pCur->info.nLocal; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3354 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3355 | |
| 3356 | if( rc==SQLITE_OK && amt>0 ){ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 3357 | const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3358 | Pgno nextPage; |
| 3359 | |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3360 | nextPage = get4byte(&aPayload[pCur->info.nLocal]); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3361 | |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3362 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 3363 | /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[] |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3364 | ** has not been allocated, allocate it now. The array is sized at |
| 3365 | ** one entry for each overflow page in the overflow chain. The |
| 3366 | ** page number of the first overflow page is stored in aOverflow[0], |
| 3367 | ** etc. A value of 0 in the aOverflow[] array means "not yet known" |
| 3368 | ** (the cache is lazily populated). |
| 3369 | */ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 3370 | if( pCur->isIncrblobHandle && !pCur->aOverflow ){ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3371 | int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3372 | pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl); |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3373 | if( nOvfl && !pCur->aOverflow ){ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3374 | rc = SQLITE_NOMEM; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3375 | } |
| 3376 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3377 | |
| 3378 | /* If the overflow page-list cache has been allocated and the |
| 3379 | ** entry for the first required overflow page is valid, skip |
| 3380 | ** directly to it. |
| 3381 | */ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3382 | if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){ |
| 3383 | iIdx = (offset/ovflSize); |
| 3384 | nextPage = pCur->aOverflow[iIdx]; |
| 3385 | offset = (offset%ovflSize); |
| 3386 | } |
| 3387 | #endif |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3388 | |
| 3389 | for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){ |
| 3390 | |
| 3391 | #ifndef SQLITE_OMIT_INCRBLOB |
| 3392 | /* If required, populate the overflow page-list cache. */ |
| 3393 | if( pCur->aOverflow ){ |
| 3394 | assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage); |
| 3395 | pCur->aOverflow[iIdx] = nextPage; |
| 3396 | } |
| 3397 | #endif |
| 3398 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3399 | if( offset>=ovflSize ){ |
| 3400 | /* The only reason to read this page is to obtain the page |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3401 | ** number for the next page in the overflow chain. The page |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 3402 | ** data is not required. So first try to lookup the overflow |
| 3403 | ** page-list cache, if any, then fall back to the getOverflowPage() |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3404 | ** function. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3405 | */ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3406 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3407 | if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){ |
| 3408 | nextPage = pCur->aOverflow[iIdx+1]; |
| 3409 | } else |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3410 | #endif |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3411 | rc = getOverflowPage(pBt, nextPage, 0, &nextPage); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3412 | offset -= ovflSize; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3413 | }else{ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3414 | /* Need to read this page properly. It contains some of the |
| 3415 | ** range of data that is being read (eOp==0) or written (eOp!=0). |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3416 | */ |
| 3417 | DbPage *pDbPage; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3418 | int a = amt; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3419 | rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3420 | if( rc==SQLITE_OK ){ |
| 3421 | aPayload = sqlite3PagerGetData(pDbPage); |
| 3422 | nextPage = get4byte(aPayload); |
| 3423 | if( a + offset > ovflSize ){ |
| 3424 | a = ovflSize - offset; |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3425 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3426 | rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage); |
| 3427 | sqlite3PagerUnref(pDbPage); |
| 3428 | offset = 0; |
| 3429 | amt -= a; |
| 3430 | pBuf += a; |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3431 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3432 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3433 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3434 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3435 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3436 | if( rc==SQLITE_OK && amt>0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3437 | return SQLITE_CORRUPT_BKPT; |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 3438 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3439 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3440 | } |
| 3441 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3442 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3443 | ** Read part of the key associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3444 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3445 | ** begins at "offset". |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3446 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3447 | ** Return SQLITE_OK on success or an error code if anything goes |
| 3448 | ** wrong. An error is returned if "offset+amt" is larger than |
| 3449 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3450 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3451 | int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3452 | int rc; |
| 3453 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3454 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 3455 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3456 | if( rc==SQLITE_OK ){ |
| 3457 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3458 | assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] ); |
| 3459 | if( pCur->apPage[0]->intKey ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3460 | return SQLITE_CORRUPT_BKPT; |
| 3461 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3462 | assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3463 | rc = accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0, 0); |
drh | 6575a22 | 2005-03-10 17:06:34 +0000 | [diff] [blame] | 3464 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3465 | return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3466 | } |
| 3467 | |
| 3468 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3469 | ** Read part of the data associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3470 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3471 | ** begins at "offset". |
| 3472 | ** |
| 3473 | ** Return SQLITE_OK on success or an error code if anything goes |
| 3474 | ** wrong. An error is returned if "offset+amt" is larger than |
| 3475 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3476 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3477 | int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3478 | int rc; |
| 3479 | |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 3480 | #ifndef SQLITE_OMIT_INCRBLOB |
| 3481 | if ( pCur->eState==CURSOR_INVALID ){ |
| 3482 | return SQLITE_ABORT; |
| 3483 | } |
| 3484 | #endif |
| 3485 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3486 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 3487 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3488 | if( rc==SQLITE_OK ){ |
| 3489 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3490 | assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] ); |
| 3491 | assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3492 | rc = accessPayload(pCur, offset, amt, pBuf, 1, 0); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3493 | } |
| 3494 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3495 | } |
| 3496 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3497 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3498 | ** Return a pointer to payload information from the entry that the |
| 3499 | ** pCur cursor is pointing to. The pointer is to the beginning of |
| 3500 | ** 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] | 3501 | ** skipKey==1. The number of bytes of available key/data is written |
| 3502 | ** into *pAmt. If *pAmt==0, then the value returned will not be |
| 3503 | ** a valid pointer. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3504 | ** |
| 3505 | ** This routine is an optimization. It is common for the entire key |
| 3506 | ** and data to fit on the local page and for there to be no overflow |
| 3507 | ** pages. When that is so, this routine can be used to access the |
| 3508 | ** 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] | 3509 | ** onto overflow pages, then accessPayload() must be used to reassembly |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3510 | ** the key/data and copy it into a preallocated buffer. |
| 3511 | ** |
| 3512 | ** The pointer returned by this routine looks directly into the cached |
| 3513 | ** page of the database. The data might change or move the next time |
| 3514 | ** any btree routine is called. |
| 3515 | */ |
| 3516 | static const unsigned char *fetchPayload( |
| 3517 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3518 | int *pAmt, /* Write the number of available bytes here */ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3519 | int skipKey /* read beginning at data if this is true */ |
| 3520 | ){ |
| 3521 | unsigned char *aPayload; |
| 3522 | MemPage *pPage; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3523 | u32 nKey; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 3524 | u32 nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3525 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3526 | assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3527 | assert( pCur->eState==CURSOR_VALID ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3528 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3529 | pPage = pCur->apPage[pCur->iPage]; |
| 3530 | assert( pCur->aiIdx[pCur->iPage]<pPage->nCell ); |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3531 | getCellInfo(pCur); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3532 | aPayload = pCur->info.pCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3533 | aPayload += pCur->info.nHeader; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3534 | if( pPage->intKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3535 | nKey = 0; |
| 3536 | }else{ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3537 | nKey = (int)pCur->info.nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3538 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3539 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3540 | aPayload += nKey; |
| 3541 | nLocal = pCur->info.nLocal - nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3542 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3543 | nLocal = pCur->info.nLocal; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3544 | if( nLocal>nKey ){ |
| 3545 | nLocal = nKey; |
| 3546 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3547 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3548 | *pAmt = nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3549 | return aPayload; |
| 3550 | } |
| 3551 | |
| 3552 | |
| 3553 | /* |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3554 | ** For the entry that cursor pCur is point to, return as |
| 3555 | ** many bytes of the key or data as are available on the local |
| 3556 | ** b-tree page. Write the number of available bytes into *pAmt. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3557 | ** |
| 3558 | ** The pointer returned is ephemeral. The key/data may move |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3559 | ** or be destroyed on the next call to any Btree routine, |
| 3560 | ** including calls from other threads against the same cache. |
| 3561 | ** Hence, a mutex on the BtShared should be held prior to calling |
| 3562 | ** this routine. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3563 | ** |
| 3564 | ** These routines is used to get quick access to key and data |
| 3565 | ** in the common case where no overflow pages are used. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3566 | */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3567 | const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3568 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3569 | if( pCur->eState==CURSOR_VALID ){ |
| 3570 | return (const void*)fetchPayload(pCur, pAmt, 0); |
| 3571 | } |
| 3572 | return 0; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3573 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3574 | const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3575 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3576 | if( pCur->eState==CURSOR_VALID ){ |
| 3577 | return (const void*)fetchPayload(pCur, pAmt, 1); |
| 3578 | } |
| 3579 | return 0; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3580 | } |
| 3581 | |
| 3582 | |
| 3583 | /* |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3584 | ** 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] | 3585 | ** page number of the child page to move to. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3586 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3587 | static int moveToChild(BtCursor *pCur, u32 newPgno){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3588 | int rc; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3589 | int i = pCur->iPage; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3590 | MemPage *pNewPage; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 3591 | BtShared *pBt = pCur->pBt; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3592 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3593 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3594 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3595 | assert( pCur->iPage<BTCURSOR_MAX_DEPTH ); |
| 3596 | if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){ |
| 3597 | return SQLITE_CORRUPT_BKPT; |
| 3598 | } |
| 3599 | rc = getAndInitPage(pBt, newPgno, &pNewPage); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3600 | if( rc ) return rc; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3601 | pCur->apPage[i+1] = pNewPage; |
| 3602 | pCur->aiIdx[i+1] = 0; |
| 3603 | pCur->iPage++; |
| 3604 | |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3605 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3606 | pCur->validNKey = 0; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 3607 | if( pNewPage->nCell<1 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3608 | return SQLITE_CORRUPT_BKPT; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 3609 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3610 | return SQLITE_OK; |
| 3611 | } |
| 3612 | |
danielk1977 | bf93c56 | 2008-09-29 15:53:25 +0000 | [diff] [blame] | 3613 | #ifndef NDEBUG |
| 3614 | /* |
| 3615 | ** Page pParent is an internal (non-leaf) tree page. This function |
| 3616 | ** asserts that page number iChild is the left-child if the iIdx'th |
| 3617 | ** cell in page pParent. Or, if iIdx is equal to the total number of |
| 3618 | ** cells in pParent, that page number iChild is the right-child of |
| 3619 | ** the page. |
| 3620 | */ |
| 3621 | static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){ |
| 3622 | assert( iIdx<=pParent->nCell ); |
| 3623 | if( iIdx==pParent->nCell ){ |
| 3624 | assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild ); |
| 3625 | }else{ |
| 3626 | assert( get4byte(findCell(pParent, iIdx))==iChild ); |
| 3627 | } |
| 3628 | } |
| 3629 | #else |
| 3630 | # define assertParentIndex(x,y,z) |
| 3631 | #endif |
| 3632 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3633 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3634 | ** Move the cursor up to the parent page. |
| 3635 | ** |
| 3636 | ** pCur->idx is set to the cell index that contains the pointer |
| 3637 | ** to the page we are coming from. If we are coming from the |
| 3638 | ** right-most child page then pCur->idx is set to one more than |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3639 | ** the largest cell index. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3640 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3641 | void sqlite3BtreeMoveToParent(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3642 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3643 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3644 | assert( pCur->iPage>0 ); |
| 3645 | assert( pCur->apPage[pCur->iPage] ); |
danielk1977 | bf93c56 | 2008-09-29 15:53:25 +0000 | [diff] [blame] | 3646 | assertParentIndex( |
| 3647 | pCur->apPage[pCur->iPage-1], |
| 3648 | pCur->aiIdx[pCur->iPage-1], |
| 3649 | pCur->apPage[pCur->iPage]->pgno |
| 3650 | ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3651 | releasePage(pCur->apPage[pCur->iPage]); |
| 3652 | pCur->iPage--; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3653 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3654 | pCur->validNKey = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3655 | } |
| 3656 | |
| 3657 | /* |
| 3658 | ** Move the cursor to the root page |
| 3659 | */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3660 | static int moveToRoot(BtCursor *pCur){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3661 | MemPage *pRoot; |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3662 | int rc = SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3663 | Btree *p = pCur->pBtree; |
| 3664 | BtShared *pBt = p->pBt; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3665 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3666 | assert( cursorHoldsMutex(pCur) ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 3667 | assert( CURSOR_INVALID < CURSOR_REQUIRESEEK ); |
| 3668 | assert( CURSOR_VALID < CURSOR_REQUIRESEEK ); |
| 3669 | assert( CURSOR_FAULT > CURSOR_REQUIRESEEK ); |
| 3670 | if( pCur->eState>=CURSOR_REQUIRESEEK ){ |
| 3671 | if( pCur->eState==CURSOR_FAULT ){ |
| 3672 | return pCur->skip; |
| 3673 | } |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 3674 | sqlite3BtreeClearCursor(pCur); |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 3675 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3676 | |
| 3677 | if( pCur->iPage>=0 ){ |
| 3678 | int i; |
| 3679 | for(i=1; i<=pCur->iPage; i++){ |
| 3680 | releasePage(pCur->apPage[i]); |
danielk1977 | d9f6c53 | 2008-09-19 16:39:38 +0000 | [diff] [blame] | 3681 | } |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3682 | }else{ |
| 3683 | if( |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3684 | SQLITE_OK!=(rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0])) |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3685 | ){ |
| 3686 | pCur->eState = CURSOR_INVALID; |
| 3687 | return rc; |
| 3688 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3689 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3690 | |
| 3691 | pRoot = pCur->apPage[0]; |
| 3692 | assert( pRoot->pgno==pCur->pgnoRoot ); |
| 3693 | pCur->iPage = 0; |
| 3694 | pCur->aiIdx[0] = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3695 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3696 | pCur->atLast = 0; |
| 3697 | pCur->validNKey = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3698 | |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3699 | if( pRoot->nCell==0 && !pRoot->leaf ){ |
| 3700 | Pgno subpage; |
| 3701 | assert( pRoot->pgno==1 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3702 | subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3703 | assert( subpage>0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3704 | pCur->eState = CURSOR_VALID; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3705 | rc = moveToChild(pCur, subpage); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3706 | }else{ |
| 3707 | pCur->eState = ((pRoot->nCell>0)?CURSOR_VALID:CURSOR_INVALID); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3708 | } |
| 3709 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3710 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3711 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3712 | /* |
| 3713 | ** Move the cursor down to the left-most leaf entry beneath the |
| 3714 | ** entry to which it is currently pointing. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3715 | ** |
| 3716 | ** The left-most leaf is the one with the smallest key - the first |
| 3717 | ** in ascending order. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3718 | */ |
| 3719 | static int moveToLeftmost(BtCursor *pCur){ |
| 3720 | Pgno pgno; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3721 | int rc = SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3722 | MemPage *pPage; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3723 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3724 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3725 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3726 | while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){ |
| 3727 | assert( pCur->aiIdx[pCur->iPage]<pPage->nCell ); |
| 3728 | pgno = get4byte(findCell(pPage, pCur->aiIdx[pCur->iPage])); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3729 | rc = moveToChild(pCur, pgno); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3730 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3731 | return rc; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3732 | } |
| 3733 | |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3734 | /* |
| 3735 | ** Move the cursor down to the right-most leaf entry beneath the |
| 3736 | ** page to which it is currently pointing. Notice the difference |
| 3737 | ** between moveToLeftmost() and moveToRightmost(). moveToLeftmost() |
| 3738 | ** finds the left-most entry beneath the *entry* whereas moveToRightmost() |
| 3739 | ** finds the right-most entry beneath the *page*. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3740 | ** |
| 3741 | ** The right-most entry is the one with the largest key - the last |
| 3742 | ** key in ascending order. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3743 | */ |
| 3744 | static int moveToRightmost(BtCursor *pCur){ |
| 3745 | Pgno pgno; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3746 | int rc = SQLITE_OK; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 3747 | MemPage *pPage = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3748 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3749 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3750 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3751 | while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3752 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3753 | pCur->aiIdx[pCur->iPage] = pPage->nCell; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3754 | rc = moveToChild(pCur, pgno); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3755 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3756 | if( rc==SQLITE_OK ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3757 | pCur->aiIdx[pCur->iPage] = pPage->nCell-1; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3758 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3759 | pCur->validNKey = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3760 | } |
danielk1977 | 518002e | 2008-09-05 05:02:46 +0000 | [diff] [blame] | 3761 | return rc; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3762 | } |
| 3763 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3764 | /* Move the cursor to the first entry in the table. Return SQLITE_OK |
| 3765 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 3766 | ** or set *pRes to 1 if the table is empty. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3767 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3768 | int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3769 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3770 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3771 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3772 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3773 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3774 | if( rc==SQLITE_OK ){ |
| 3775 | if( pCur->eState==CURSOR_INVALID ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3776 | assert( pCur->apPage[pCur->iPage]->nCell==0 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3777 | *pRes = 1; |
| 3778 | rc = SQLITE_OK; |
| 3779 | }else{ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3780 | assert( pCur->apPage[pCur->iPage]->nCell>0 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3781 | *pRes = 0; |
| 3782 | rc = moveToLeftmost(pCur); |
| 3783 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3784 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3785 | return rc; |
| 3786 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3787 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3788 | /* Move the cursor to the last entry in the table. Return SQLITE_OK |
| 3789 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 3790 | ** or set *pRes to 1 if the table is empty. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3791 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3792 | int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3793 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3794 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3795 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3796 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3797 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3798 | if( rc==SQLITE_OK ){ |
| 3799 | if( CURSOR_INVALID==pCur->eState ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3800 | assert( pCur->apPage[pCur->iPage]->nCell==0 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3801 | *pRes = 1; |
| 3802 | }else{ |
| 3803 | assert( pCur->eState==CURSOR_VALID ); |
| 3804 | *pRes = 0; |
| 3805 | rc = moveToRightmost(pCur); |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3806 | getCellInfo(pCur); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3807 | pCur->atLast = rc==SQLITE_OK ?1:0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3808 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3809 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3810 | return rc; |
| 3811 | } |
| 3812 | |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 3813 | /* Move the cursor so that it points to an entry near the key |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3814 | ** specified by pIdxKey or intKey. Return a success code. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3815 | ** |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3816 | ** For INTKEY tables, the intKey parameter is used. pIdxKey |
| 3817 | ** must be NULL. For index tables, pIdxKey is used and intKey |
| 3818 | ** is ignored. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3819 | ** |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3820 | ** If an exact match is not found, then the cursor is always |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3821 | ** left pointing at a leaf page which would hold the entry if it |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3822 | ** were present. The cursor might point to an entry that comes |
| 3823 | ** before or after the key. |
| 3824 | ** |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 3825 | ** An integer is written into *pRes which is the result of |
| 3826 | ** comparing the key with the entry to which the cursor is |
| 3827 | ** pointing. The meaning of the integer written into |
| 3828 | ** *pRes is as follows: |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3829 | ** |
| 3830 | ** *pRes<0 The cursor is left pointing at an entry that |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 3831 | ** is smaller than intKey/pIdxKey or if the table is empty |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3832 | ** and the cursor is therefore left point to nothing. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3833 | ** |
| 3834 | ** *pRes==0 The cursor is left pointing at an entry that |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 3835 | ** exactly matches intKey/pIdxKey. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3836 | ** |
| 3837 | ** *pRes>0 The cursor is left pointing at an entry that |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 3838 | ** is larger than intKey/pIdxKey. |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3839 | ** |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3840 | */ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3841 | int sqlite3BtreeMovetoUnpacked( |
| 3842 | BtCursor *pCur, /* The cursor to be moved */ |
| 3843 | UnpackedRecord *pIdxKey, /* Unpacked index key */ |
| 3844 | i64 intKey, /* The table key */ |
| 3845 | int biasRight, /* If true, bias the search to the high end */ |
| 3846 | int *pRes /* Write search results here */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3847 | ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3848 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3849 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3850 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3851 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3852 | |
| 3853 | /* If the cursor is already positioned at the point we are trying |
| 3854 | ** to move to, then just return without doing any work */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3855 | if( pCur->eState==CURSOR_VALID && pCur->validNKey |
| 3856 | && pCur->apPage[0]->intKey |
| 3857 | ){ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3858 | if( pCur->info.nKey==intKey ){ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3859 | *pRes = 0; |
| 3860 | return SQLITE_OK; |
| 3861 | } |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3862 | if( pCur->atLast && pCur->info.nKey<intKey ){ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3863 | *pRes = -1; |
| 3864 | return SQLITE_OK; |
| 3865 | } |
| 3866 | } |
| 3867 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3868 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3869 | if( rc ){ |
| 3870 | return rc; |
| 3871 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3872 | assert( pCur->apPage[pCur->iPage] ); |
| 3873 | assert( pCur->apPage[pCur->iPage]->isInit ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3874 | if( pCur->eState==CURSOR_INVALID ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3875 | *pRes = -1; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3876 | assert( pCur->apPage[pCur->iPage]->nCell==0 ); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3877 | return SQLITE_OK; |
| 3878 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3879 | assert( pCur->apPage[0]->intKey || pIdxKey ); |
drh | 1468438 | 2006-11-30 13:05:29 +0000 | [diff] [blame] | 3880 | for(;;){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3881 | int lwr, upr; |
| 3882 | Pgno chldPg; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3883 | MemPage *pPage = pCur->apPage[pCur->iPage]; |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3884 | int c = -1; /* pRes return if table is empty must be -1 */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3885 | lwr = 0; |
| 3886 | upr = pPage->nCell-1; |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 3887 | if( (!pPage->intKey && pIdxKey==0) || upr<0 ){ |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3888 | rc = SQLITE_CORRUPT_BKPT; |
| 3889 | goto moveto_finish; |
drh | 4eec4c1 | 2005-01-21 00:22:37 +0000 | [diff] [blame] | 3890 | } |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3891 | if( biasRight ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3892 | pCur->aiIdx[pCur->iPage] = (u16)upr; |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3893 | }else{ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3894 | pCur->aiIdx[pCur->iPage] = (u16)((upr+lwr)/2); |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3895 | } |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 3896 | for(;;){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3897 | void *pCellKey; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3898 | i64 nCellKey; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3899 | int idx = pCur->aiIdx[pCur->iPage]; |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3900 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3901 | pCur->validNKey = 1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3902 | if( pPage->intKey ){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3903 | u8 *pCell; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3904 | pCell = findCell(pPage, idx) + pPage->childPtrSize; |
drh | d172f86 | 2006-01-12 15:01:15 +0000 | [diff] [blame] | 3905 | if( pPage->hasData ){ |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 3906 | u32 dummy; |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 3907 | pCell += getVarint32(pCell, dummy); |
drh | d172f86 | 2006-01-12 15:01:15 +0000 | [diff] [blame] | 3908 | } |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3909 | getVarint(pCell, (u64*)&nCellKey); |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3910 | if( nCellKey==intKey ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3911 | c = 0; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3912 | }else if( nCellKey<intKey ){ |
drh | 41eb9e9 | 2008-04-02 18:33:07 +0000 | [diff] [blame] | 3913 | c = -1; |
| 3914 | }else{ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3915 | assert( nCellKey>intKey ); |
drh | 41eb9e9 | 2008-04-02 18:33:07 +0000 | [diff] [blame] | 3916 | c = +1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3917 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3918 | }else{ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3919 | int available; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3920 | pCellKey = (void *)fetchPayload(pCur, &available, 0); |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3921 | nCellKey = pCur->info.nKey; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3922 | if( available>=nCellKey ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3923 | c = sqlite3VdbeRecordCompare((int)nCellKey, pCellKey, pIdxKey); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3924 | }else{ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3925 | pCellKey = sqlite3Malloc( (int)nCellKey ); |
danielk1977 | 6507ecb | 2008-03-25 09:56:44 +0000 | [diff] [blame] | 3926 | if( pCellKey==0 ){ |
| 3927 | rc = SQLITE_NOMEM; |
| 3928 | goto moveto_finish; |
| 3929 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3930 | rc = sqlite3BtreeKey(pCur, 0, (int)nCellKey, (void*)pCellKey); |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 3931 | c = sqlite3VdbeRecordCompare((int)nCellKey, pCellKey, pIdxKey); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 3932 | sqlite3_free(pCellKey); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3933 | if( rc ) goto moveto_finish; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3934 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3935 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3936 | if( c==0 ){ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3937 | pCur->info.nKey = nCellKey; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 3938 | if( pPage->intKey && !pPage->leaf ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3939 | lwr = idx; |
drh | fc70e6f | 2004-05-12 21:11:27 +0000 | [diff] [blame] | 3940 | upr = lwr - 1; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3941 | break; |
| 3942 | }else{ |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 3943 | *pRes = 0; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3944 | rc = SQLITE_OK; |
| 3945 | goto moveto_finish; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3946 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3947 | } |
| 3948 | if( c<0 ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3949 | lwr = idx+1; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3950 | }else{ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3951 | upr = idx-1; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3952 | } |
drh | f1d68b3 | 2007-03-29 04:43:26 +0000 | [diff] [blame] | 3953 | if( lwr>upr ){ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3954 | pCur->info.nKey = nCellKey; |
drh | f1d68b3 | 2007-03-29 04:43:26 +0000 | [diff] [blame] | 3955 | break; |
| 3956 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3957 | pCur->aiIdx[pCur->iPage] = (u16)((lwr+upr)/2); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3958 | } |
| 3959 | assert( lwr==upr+1 ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3960 | assert( pPage->isInit ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3961 | if( pPage->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3962 | chldPg = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3963 | }else if( lwr>=pPage->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3964 | chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3965 | }else{ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3966 | chldPg = get4byte(findCell(pPage, lwr)); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3967 | } |
| 3968 | if( chldPg==0 ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3969 | assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3970 | if( pRes ) *pRes = c; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3971 | rc = SQLITE_OK; |
| 3972 | goto moveto_finish; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3973 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3974 | pCur->aiIdx[pCur->iPage] = (u16)lwr; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3975 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3976 | pCur->validNKey = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3977 | rc = moveToChild(pCur, chldPg); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3978 | if( rc ) goto moveto_finish; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3979 | } |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3980 | moveto_finish: |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3981 | return rc; |
| 3982 | } |
| 3983 | |
| 3984 | /* |
| 3985 | ** In this version of BtreeMoveto, pKey is a packed index record |
| 3986 | ** such as is generated by the OP_MakeRecord opcode. Unpack the |
| 3987 | ** record and then call BtreeMovetoUnpacked() to do the work. |
| 3988 | */ |
| 3989 | int sqlite3BtreeMoveto( |
| 3990 | BtCursor *pCur, /* Cursor open on the btree to be searched */ |
| 3991 | const void *pKey, /* Packed key if the btree is an index */ |
| 3992 | i64 nKey, /* Integer key for tables. Size of pKey for indices */ |
| 3993 | int bias, /* Bias search to the high end */ |
| 3994 | int *pRes /* Write search results here */ |
| 3995 | ){ |
| 3996 | int rc; /* Status code */ |
| 3997 | UnpackedRecord *pIdxKey; /* Unpacked index key */ |
drh | 23f79d0 | 2008-08-20 22:06:47 +0000 | [diff] [blame] | 3998 | UnpackedRecord aSpace[16]; /* Temp space for pIdxKey - to avoid a malloc */ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3999 | |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 4000 | if( pKey ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4001 | assert( nKey==(i64)(int)nKey ); |
| 4002 | pIdxKey = sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, |
drh | 23f79d0 | 2008-08-20 22:06:47 +0000 | [diff] [blame] | 4003 | aSpace, sizeof(aSpace)); |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4004 | if( pIdxKey==0 ) return SQLITE_NOMEM; |
| 4005 | }else{ |
| 4006 | pIdxKey = 0; |
| 4007 | } |
| 4008 | rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes); |
| 4009 | if( pKey ){ |
| 4010 | sqlite3VdbeDeleteUnpackedRecord(pIdxKey); |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 4011 | } |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 4012 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4013 | } |
| 4014 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4015 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4016 | /* |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 4017 | ** Return TRUE if the cursor is not pointing at an entry of the table. |
| 4018 | ** |
| 4019 | ** TRUE will be returned after a call to sqlite3BtreeNext() moves |
| 4020 | ** past the last entry in the table or sqlite3BtreePrev() moves past |
| 4021 | ** the first entry. TRUE is also returned if the table is empty. |
| 4022 | */ |
| 4023 | int sqlite3BtreeEof(BtCursor *pCur){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4024 | /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries |
| 4025 | ** have been deleted? This API will need to change to return an error code |
| 4026 | ** as well as the boolean result value. |
| 4027 | */ |
| 4028 | return (CURSOR_VALID!=pCur->eState); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 4029 | } |
| 4030 | |
| 4031 | /* |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 4032 | ** Return the database connection handle for a cursor. |
| 4033 | */ |
| 4034 | sqlite3 *sqlite3BtreeCursorDb(const BtCursor *pCur){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 4035 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
| 4036 | return pCur->pBtree->db; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 4037 | } |
| 4038 | |
| 4039 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4040 | ** Advance the cursor to the next entry in the database. If |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4041 | ** successful then set *pRes=0. If the cursor |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4042 | ** was already pointing to the last entry in the database before |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4043 | ** this routine was called, then set *pRes=1. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4044 | */ |
drh | d094db1 | 2008-04-03 21:46:57 +0000 | [diff] [blame] | 4045 | int sqlite3BtreeNext(BtCursor *pCur, int *pRes){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4046 | int rc; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4047 | int idx; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 4048 | MemPage *pPage; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4049 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4050 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 4051 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4052 | if( rc!=SQLITE_OK ){ |
| 4053 | return rc; |
| 4054 | } |
drh | 8c4d3a6 | 2007-04-06 01:03:32 +0000 | [diff] [blame] | 4055 | assert( pRes!=0 ); |
drh | 8c4d3a6 | 2007-04-06 01:03:32 +0000 | [diff] [blame] | 4056 | if( CURSOR_INVALID==pCur->eState ){ |
| 4057 | *pRes = 1; |
| 4058 | return SQLITE_OK; |
| 4059 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4060 | if( pCur->skip>0 ){ |
| 4061 | pCur->skip = 0; |
| 4062 | *pRes = 0; |
| 4063 | return SQLITE_OK; |
| 4064 | } |
| 4065 | pCur->skip = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4066 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4067 | pPage = pCur->apPage[pCur->iPage]; |
| 4068 | idx = ++pCur->aiIdx[pCur->iPage]; |
| 4069 | assert( pPage->isInit ); |
| 4070 | assert( idx<=pPage->nCell ); |
danielk1977 | 6a43f9b | 2004-11-16 04:57:24 +0000 | [diff] [blame] | 4071 | |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 4072 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 4073 | pCur->validNKey = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4074 | if( idx>=pPage->nCell ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4075 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4076 | rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4077 | if( rc ) return rc; |
| 4078 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4079 | *pRes = 0; |
| 4080 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4081 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4082 | do{ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4083 | if( pCur->iPage==0 ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4084 | *pRes = 1; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4085 | pCur->eState = CURSOR_INVALID; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4086 | return SQLITE_OK; |
| 4087 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4088 | sqlite3BtreeMoveToParent(pCur); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4089 | pPage = pCur->apPage[pCur->iPage]; |
| 4090 | }while( pCur->aiIdx[pCur->iPage]>=pPage->nCell ); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4091 | *pRes = 0; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 4092 | if( pPage->intKey ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4093 | rc = sqlite3BtreeNext(pCur, pRes); |
| 4094 | }else{ |
| 4095 | rc = SQLITE_OK; |
| 4096 | } |
| 4097 | return rc; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4098 | } |
| 4099 | *pRes = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4100 | if( pPage->leaf ){ |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4101 | return SQLITE_OK; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4102 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4103 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4104 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4105 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4106 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4107 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4108 | /* |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4109 | ** 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] | 4110 | ** successful then set *pRes=0. If the cursor |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4111 | ** was already pointing to the first entry in the database before |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4112 | ** this routine was called, then set *pRes=1. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4113 | */ |
drh | d094db1 | 2008-04-03 21:46:57 +0000 | [diff] [blame] | 4114 | int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){ |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4115 | int rc; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4116 | MemPage *pPage; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4117 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4118 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 4119 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4120 | if( rc!=SQLITE_OK ){ |
| 4121 | return rc; |
| 4122 | } |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 4123 | pCur->atLast = 0; |
drh | 8c4d3a6 | 2007-04-06 01:03:32 +0000 | [diff] [blame] | 4124 | if( CURSOR_INVALID==pCur->eState ){ |
| 4125 | *pRes = 1; |
| 4126 | return SQLITE_OK; |
| 4127 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4128 | if( pCur->skip<0 ){ |
| 4129 | pCur->skip = 0; |
| 4130 | *pRes = 0; |
| 4131 | return SQLITE_OK; |
| 4132 | } |
| 4133 | pCur->skip = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4134 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4135 | pPage = pCur->apPage[pCur->iPage]; |
| 4136 | assert( pPage->isInit ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4137 | if( !pPage->leaf ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4138 | int idx = pCur->aiIdx[pCur->iPage]; |
| 4139 | rc = moveToChild(pCur, get4byte(findCell(pPage, idx))); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4140 | if( rc ){ |
| 4141 | return rc; |
| 4142 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4143 | rc = moveToRightmost(pCur); |
| 4144 | }else{ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4145 | while( pCur->aiIdx[pCur->iPage]==0 ){ |
| 4146 | if( pCur->iPage==0 ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4147 | pCur->eState = CURSOR_INVALID; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 4148 | *pRes = 1; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4149 | return SQLITE_OK; |
| 4150 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4151 | sqlite3BtreeMoveToParent(pCur); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4152 | } |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 4153 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 4154 | pCur->validNKey = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4155 | |
| 4156 | pCur->aiIdx[pCur->iPage]--; |
| 4157 | pPage = pCur->apPage[pCur->iPage]; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 4158 | if( pPage->intKey && !pPage->leaf ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4159 | rc = sqlite3BtreePrevious(pCur, pRes); |
| 4160 | }else{ |
| 4161 | rc = SQLITE_OK; |
| 4162 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4163 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4164 | *pRes = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4165 | return rc; |
| 4166 | } |
| 4167 | |
| 4168 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4169 | ** Allocate a new page from the database file. |
| 4170 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4171 | ** The new page is marked as dirty. (In other words, sqlite3PagerWrite() |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4172 | ** has already been called on the new page.) The new page has also |
| 4173 | ** been referenced and the calling routine is responsible for calling |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4174 | ** sqlite3PagerUnref() on the new page when it is done. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4175 | ** |
| 4176 | ** SQLITE_OK is returned on success. Any other return value indicates |
| 4177 | ** an error. *ppPage and *pPgno are undefined in the event of an error. |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4178 | ** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned. |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 4179 | ** |
drh | 199e3cf | 2002-07-18 11:01:47 +0000 | [diff] [blame] | 4180 | ** If the "nearby" parameter is not 0, then a (feeble) effort is made to |
| 4181 | ** 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] | 4182 | ** attempt to keep related pages close to each other in the database file, |
| 4183 | ** which in turn can make database access faster. |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4184 | ** |
| 4185 | ** If the "exact" parameter is not 0, and the page-number nearby exists |
| 4186 | ** anywhere on the free-list, then it is guarenteed to be returned. This |
| 4187 | ** is only used by auto-vacuum databases when allocating a new table. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4188 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4189 | static int allocateBtreePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4190 | BtShared *pBt, |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4191 | MemPage **ppPage, |
| 4192 | Pgno *pPgno, |
| 4193 | Pgno nearby, |
| 4194 | u8 exact |
| 4195 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4196 | MemPage *pPage1; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4197 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4198 | int n; /* Number of pages on the freelist */ |
| 4199 | int k; /* Number of leaves on the trunk of the freelist */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4200 | MemPage *pTrunk = 0; |
| 4201 | MemPage *pPrevTrunk = 0; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4202 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4203 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4204 | pPage1 = pBt->pPage1; |
| 4205 | n = get4byte(&pPage1->aData[36]); |
| 4206 | if( n>0 ){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4207 | /* There are pages on the freelist. Reuse one of those pages. */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4208 | Pgno iTrunk; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4209 | u8 searchList = 0; /* If the free-list must be searched for 'nearby' */ |
| 4210 | |
| 4211 | /* If the 'exact' parameter was true and a query of the pointer-map |
| 4212 | ** shows that the page 'nearby' is somewhere on the free-list, then |
| 4213 | ** the entire-list will be searched for that page. |
| 4214 | */ |
| 4215 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 4216 | if( exact && nearby<=pagerPagecount(pBt) ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4217 | u8 eType; |
| 4218 | assert( nearby>0 ); |
| 4219 | assert( pBt->autoVacuum ); |
| 4220 | rc = ptrmapGet(pBt, nearby, &eType, 0); |
| 4221 | if( rc ) return rc; |
| 4222 | if( eType==PTRMAP_FREEPAGE ){ |
| 4223 | searchList = 1; |
| 4224 | } |
| 4225 | *pPgno = nearby; |
| 4226 | } |
| 4227 | #endif |
| 4228 | |
| 4229 | /* Decrement the free-list count by 1. Set iTrunk to the index of the |
| 4230 | ** first free-list trunk page. iPrevTrunk is initially 1. |
| 4231 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4232 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4233 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4234 | put4byte(&pPage1->aData[36], n-1); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4235 | |
| 4236 | /* The code within this loop is run only once if the 'searchList' variable |
| 4237 | ** is not true. Otherwise, it runs once for each trunk-page on the |
| 4238 | ** free-list until the page 'nearby' is located. |
| 4239 | */ |
| 4240 | do { |
| 4241 | pPrevTrunk = pTrunk; |
| 4242 | if( pPrevTrunk ){ |
| 4243 | iTrunk = get4byte(&pPrevTrunk->aData[0]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 4244 | }else{ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4245 | iTrunk = get4byte(&pPage1->aData[32]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 4246 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4247 | rc = sqlite3BtreeGetPage(pBt, iTrunk, &pTrunk, 0); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4248 | if( rc ){ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4249 | pTrunk = 0; |
| 4250 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4251 | } |
| 4252 | |
| 4253 | k = get4byte(&pTrunk->aData[4]); |
| 4254 | if( k==0 && !searchList ){ |
| 4255 | /* The trunk has no leaves and the list is not being searched. |
| 4256 | ** So extract the trunk page itself and use it as the newly |
| 4257 | ** allocated page */ |
| 4258 | assert( pPrevTrunk==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4259 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4260 | if( rc ){ |
| 4261 | goto end_allocate_page; |
| 4262 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4263 | *pPgno = iTrunk; |
| 4264 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 4265 | *ppPage = pTrunk; |
| 4266 | pTrunk = 0; |
| 4267 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
drh | 45b1fac | 2008-07-04 17:52:42 +0000 | [diff] [blame] | 4268 | }else if( k>pBt->usableSize/4 - 2 ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4269 | /* Value of k is out of range. Database corruption */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4270 | rc = SQLITE_CORRUPT_BKPT; |
| 4271 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4272 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4273 | }else if( searchList && nearby==iTrunk ){ |
| 4274 | /* The list is being searched and this trunk page is the page |
| 4275 | ** to allocate, regardless of whether it has leaves. |
| 4276 | */ |
| 4277 | assert( *pPgno==iTrunk ); |
| 4278 | *ppPage = pTrunk; |
| 4279 | searchList = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4280 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4281 | if( rc ){ |
| 4282 | goto end_allocate_page; |
| 4283 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4284 | if( k==0 ){ |
| 4285 | if( !pPrevTrunk ){ |
| 4286 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 4287 | }else{ |
| 4288 | memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4); |
| 4289 | } |
| 4290 | }else{ |
| 4291 | /* The trunk page is required by the caller but it contains |
| 4292 | ** pointers to free-list leaves. The first leaf becomes a trunk |
| 4293 | ** page in this case. |
| 4294 | */ |
| 4295 | MemPage *pNewTrunk; |
| 4296 | Pgno iNewTrunk = get4byte(&pTrunk->aData[8]); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4297 | rc = sqlite3BtreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4298 | if( rc!=SQLITE_OK ){ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4299 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4300 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4301 | rc = sqlite3PagerWrite(pNewTrunk->pDbPage); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4302 | if( rc!=SQLITE_OK ){ |
| 4303 | releasePage(pNewTrunk); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4304 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4305 | } |
| 4306 | memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4); |
| 4307 | put4byte(&pNewTrunk->aData[4], k-1); |
| 4308 | memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4309 | releasePage(pNewTrunk); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4310 | if( !pPrevTrunk ){ |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 4311 | assert( sqlite3PagerIswriteable(pPage1->pDbPage) ); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4312 | put4byte(&pPage1->aData[32], iNewTrunk); |
| 4313 | }else{ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4314 | rc = sqlite3PagerWrite(pPrevTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4315 | if( rc ){ |
| 4316 | goto end_allocate_page; |
| 4317 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4318 | put4byte(&pPrevTrunk->aData[0], iNewTrunk); |
| 4319 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4320 | } |
| 4321 | pTrunk = 0; |
| 4322 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
| 4323 | #endif |
| 4324 | }else{ |
| 4325 | /* Extract a leaf from the trunk */ |
| 4326 | int closest; |
| 4327 | Pgno iPage; |
| 4328 | unsigned char *aData = pTrunk->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4329 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4330 | if( rc ){ |
| 4331 | goto end_allocate_page; |
| 4332 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4333 | if( nearby>0 ){ |
| 4334 | int i, dist; |
| 4335 | closest = 0; |
| 4336 | dist = get4byte(&aData[8]) - nearby; |
| 4337 | if( dist<0 ) dist = -dist; |
| 4338 | for(i=1; i<k; i++){ |
| 4339 | int d2 = get4byte(&aData[8+i*4]) - nearby; |
| 4340 | if( d2<0 ) d2 = -d2; |
| 4341 | if( d2<dist ){ |
| 4342 | closest = i; |
| 4343 | dist = d2; |
| 4344 | } |
| 4345 | } |
| 4346 | }else{ |
| 4347 | closest = 0; |
| 4348 | } |
| 4349 | |
| 4350 | iPage = get4byte(&aData[8+closest*4]); |
| 4351 | if( !searchList || iPage==nearby ){ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4352 | int noContent; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 4353 | Pgno nPage; |
shane | 1f9e6aa | 2008-06-09 19:27:11 +0000 | [diff] [blame] | 4354 | *pPgno = iPage; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 4355 | nPage = pagerPagecount(pBt); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 4356 | if( *pPgno>nPage ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4357 | /* Free page off the end of the file */ |
danielk1977 | 43e377a | 2008-05-05 12:09:32 +0000 | [diff] [blame] | 4358 | rc = SQLITE_CORRUPT_BKPT; |
| 4359 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4360 | } |
| 4361 | TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d" |
| 4362 | ": %d more free pages\n", |
| 4363 | *pPgno, closest+1, k, pTrunk->pgno, n-1)); |
| 4364 | if( closest<k-1 ){ |
| 4365 | memcpy(&aData[8+closest*4], &aData[4+k*4], 4); |
| 4366 | } |
| 4367 | put4byte(&aData[4], k-1); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 4368 | assert( sqlite3PagerIswriteable(pTrunk->pDbPage) ); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4369 | noContent = !btreeGetHasContent(pBt, *pPgno); |
| 4370 | rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, noContent); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4371 | if( rc==SQLITE_OK ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4372 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 4373 | if( rc!=SQLITE_OK ){ |
| 4374 | releasePage(*ppPage); |
| 4375 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4376 | } |
| 4377 | searchList = 0; |
| 4378 | } |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 4379 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4380 | releasePage(pPrevTrunk); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4381 | pPrevTrunk = 0; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4382 | }while( searchList ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4383 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4384 | /* There are no pages on the freelist, so create a new page at the |
| 4385 | ** end of the file */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 4386 | int nPage = pagerPagecount(pBt); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 4387 | *pPgno = nPage + 1; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4388 | |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4389 | if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ |
| 4390 | (*pPgno)++; |
| 4391 | } |
| 4392 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4393 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 4394 | if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, *pPgno) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4395 | /* If *pPgno refers to a pointer-map page, allocate two new pages |
| 4396 | ** at the end of the file instead of one. The first allocated page |
| 4397 | ** becomes a new pointer-map page, the second is used by the caller. |
| 4398 | */ |
| 4399 | TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno)); |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4400 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4401 | (*pPgno)++; |
drh | 7219043 | 2008-01-31 14:54:43 +0000 | [diff] [blame] | 4402 | if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ (*pPgno)++; } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4403 | } |
| 4404 | #endif |
| 4405 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4406 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4407 | rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 0); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4408 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4409 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 4410 | if( rc!=SQLITE_OK ){ |
| 4411 | releasePage(*ppPage); |
| 4412 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4413 | TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4414 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4415 | |
| 4416 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4417 | |
| 4418 | end_allocate_page: |
| 4419 | releasePage(pTrunk); |
| 4420 | releasePage(pPrevTrunk); |
danielk1977 | b247c21 | 2008-11-21 09:09:01 +0000 | [diff] [blame] | 4421 | if( rc==SQLITE_OK ){ |
| 4422 | if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){ |
| 4423 | releasePage(*ppPage); |
| 4424 | return SQLITE_CORRUPT_BKPT; |
| 4425 | } |
| 4426 | (*ppPage)->isInit = 0; |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 4427 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4428 | return rc; |
| 4429 | } |
| 4430 | |
| 4431 | /* |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4432 | ** This function is used to add page iPage to the database file free-list. |
| 4433 | ** It is assumed that the page is not already a part of the free-list. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4434 | ** |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4435 | ** The value passed as the second argument to this function is optional. |
| 4436 | ** If the caller happens to have a pointer to the MemPage object |
| 4437 | ** corresponding to page iPage handy, it may pass it as the second value. |
| 4438 | ** Otherwise, it may pass NULL. |
| 4439 | ** |
| 4440 | ** If a pointer to a MemPage object is passed as the second argument, |
| 4441 | ** its reference count is not altered by this function. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4442 | */ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4443 | static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){ |
| 4444 | MemPage *pTrunk = 0; /* Free-list trunk page */ |
| 4445 | Pgno iTrunk = 0; /* Page number of free-list trunk page */ |
| 4446 | MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */ |
| 4447 | MemPage *pPage; /* Page being freed. May be NULL. */ |
| 4448 | int rc; /* Return Code */ |
| 4449 | int nFree; /* Initial number of pages on free-list */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4450 | |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4451 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 4452 | assert( iPage>1 ); |
| 4453 | assert( !pMemPage || pMemPage->pgno==iPage ); |
| 4454 | |
| 4455 | if( pMemPage ){ |
| 4456 | pPage = pMemPage; |
| 4457 | sqlite3PagerRef(pPage->pDbPage); |
| 4458 | }else{ |
| 4459 | pPage = btreePageLookup(pBt, iPage); |
| 4460 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4461 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4462 | /* Increment the free page count on pPage1 */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4463 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4464 | if( rc ) goto freepage_out; |
| 4465 | nFree = get4byte(&pPage1->aData[36]); |
| 4466 | put4byte(&pPage1->aData[36], nFree+1); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4467 | |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4468 | #ifdef SQLITE_SECURE_DELETE |
| 4469 | /* If the SQLITE_SECURE_DELETE compile-time option is enabled, then |
| 4470 | ** always fully overwrite deleted information with zeros. |
| 4471 | */ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4472 | if( (!pPage && (rc = sqlite3BtreeGetPage(pBt, iPage, &pPage, 0))) |
| 4473 | || (rc = sqlite3PagerWrite(pPage->pDbPage)) |
| 4474 | ){ |
| 4475 | goto freepage_out; |
| 4476 | } |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4477 | memset(pPage->aData, 0, pPage->pBt->pageSize); |
| 4478 | #endif |
| 4479 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4480 | /* If the database supports auto-vacuum, write an entry in the pointer-map |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4481 | ** to indicate that the page is free. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4482 | */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 4483 | if( ISAUTOVACUUM ){ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4484 | rc = ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0); |
| 4485 | if( rc ) goto freepage_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4486 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4487 | |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4488 | /* Now manipulate the actual database free-list structure. There are two |
| 4489 | ** possibilities. If the free-list is currently empty, or if the first |
| 4490 | ** trunk page in the free-list is full, then this page will become a |
| 4491 | ** new free-list trunk page. Otherwise, it will become a leaf of the |
| 4492 | ** first trunk page in the current free-list. This block tests if it |
| 4493 | ** is possible to add the page as a new free-list leaf. |
| 4494 | */ |
| 4495 | if( nFree!=0 ){ |
| 4496 | int nLeaf; /* Initial number of leaf cells on trunk page */ |
| 4497 | |
| 4498 | iTrunk = get4byte(&pPage1->aData[32]); |
| 4499 | rc = sqlite3BtreeGetPage(pBt, iTrunk, &pTrunk, 0); |
| 4500 | if( rc!=SQLITE_OK ){ |
| 4501 | goto freepage_out; |
| 4502 | } |
| 4503 | |
| 4504 | nLeaf = get4byte(&pTrunk->aData[4]); |
| 4505 | if( nLeaf<0 ){ |
| 4506 | rc = SQLITE_CORRUPT_BKPT; |
| 4507 | goto freepage_out; |
| 4508 | } |
| 4509 | if( nLeaf<pBt->usableSize/4 - 8 ){ |
| 4510 | /* In this case there is room on the trunk page to insert the page |
| 4511 | ** being freed as a new leaf. |
drh | 45b1fac | 2008-07-04 17:52:42 +0000 | [diff] [blame] | 4512 | ** |
| 4513 | ** Note that the trunk page is not really full until it contains |
| 4514 | ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have |
| 4515 | ** coded. But due to a coding error in versions of SQLite prior to |
| 4516 | ** 3.6.0, databases with freelist trunk pages holding more than |
| 4517 | ** usableSize/4 - 8 entries will be reported as corrupt. In order |
| 4518 | ** to maintain backwards compatibility with older versions of SQLite, |
| 4519 | ** we will contain to restrict the number of entries to usableSize/4 - 8 |
| 4520 | ** for now. At some point in the future (once everyone has upgraded |
| 4521 | ** to 3.6.0 or later) we should consider fixing the conditional above |
| 4522 | ** to read "usableSize/4-2" instead of "usableSize/4-8". |
| 4523 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4524 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 4525 | if( rc==SQLITE_OK ){ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4526 | put4byte(&pTrunk->aData[4], nLeaf+1); |
| 4527 | put4byte(&pTrunk->aData[8+nLeaf*4], iPage); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4528 | #ifndef SQLITE_SECURE_DELETE |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4529 | if( pPage ){ |
| 4530 | sqlite3PagerDontWrite(pPage->pDbPage); |
| 4531 | } |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4532 | #endif |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4533 | rc = btreeSetHasContent(pBt, iPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 4534 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4535 | TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno)); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4536 | goto freepage_out; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4537 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4538 | } |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4539 | |
| 4540 | /* If control flows to this point, then it was not possible to add the |
| 4541 | ** the page being freed as a leaf page of the first trunk in the free-list. |
| 4542 | ** Possibly because the free-list is empty, or possibly because the |
| 4543 | ** first trunk in the free-list is full. Either way, the page being freed |
| 4544 | ** will become the new first trunk page in the free-list. |
| 4545 | */ |
shane | 63207ab | 2009-02-04 01:49:30 +0000 | [diff] [blame] | 4546 | if( ((!pPage) && (0 != (rc = sqlite3BtreeGetPage(pBt, iPage, &pPage, 0)))) |
| 4547 | || (0 != (rc = sqlite3PagerWrite(pPage->pDbPage))) |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4548 | ){ |
| 4549 | goto freepage_out; |
| 4550 | } |
| 4551 | put4byte(pPage->aData, iTrunk); |
| 4552 | put4byte(&pPage->aData[4], 0); |
| 4553 | put4byte(&pPage1->aData[32], iPage); |
| 4554 | TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk)); |
| 4555 | |
| 4556 | freepage_out: |
| 4557 | if( pPage ){ |
| 4558 | pPage->isInit = 0; |
| 4559 | } |
| 4560 | releasePage(pPage); |
| 4561 | releasePage(pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4562 | return rc; |
| 4563 | } |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4564 | static int freePage(MemPage *pPage){ |
| 4565 | return freePage2(pPage->pBt, pPage, pPage->pgno); |
| 4566 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4567 | |
| 4568 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4569 | ** Free any overflow pages associated with the given Cell. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4570 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4571 | static int clearCell(MemPage *pPage, unsigned char *pCell){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4572 | BtShared *pBt = pPage->pBt; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4573 | CellInfo info; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4574 | Pgno ovflPgno; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4575 | int rc; |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 4576 | int nOvfl; |
shane | 63207ab | 2009-02-04 01:49:30 +0000 | [diff] [blame] | 4577 | u16 ovflPageSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4578 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4579 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4580 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4581 | if( info.iOverflow==0 ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4582 | return SQLITE_OK; /* No overflow pages. Return without doing anything */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4583 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4584 | ovflPgno = get4byte(&pCell[info.iOverflow]); |
shane | 63207ab | 2009-02-04 01:49:30 +0000 | [diff] [blame] | 4585 | assert( pBt->usableSize > 4 ); |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 4586 | ovflPageSize = pBt->usableSize - 4; |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 4587 | nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize; |
| 4588 | assert( ovflPgno==0 || nOvfl>0 ); |
| 4589 | while( nOvfl-- ){ |
shane | 63207ab | 2009-02-04 01:49:30 +0000 | [diff] [blame] | 4590 | Pgno iNext = 0; |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4591 | MemPage *pOvfl = 0; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 4592 | if( ovflPgno==0 || ovflPgno>pagerPagecount(pBt) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 4593 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 4594 | } |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4595 | if( nOvfl ){ |
| 4596 | rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext); |
| 4597 | if( rc ) return rc; |
| 4598 | } |
| 4599 | rc = freePage2(pBt, pOvfl, ovflPgno); |
| 4600 | if( pOvfl ){ |
| 4601 | sqlite3PagerUnref(pOvfl->pDbPage); |
| 4602 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4603 | if( rc ) return rc; |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4604 | ovflPgno = iNext; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4605 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4606 | return SQLITE_OK; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4607 | } |
| 4608 | |
| 4609 | /* |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4610 | ** Create the byte sequence used to represent a cell on page pPage |
| 4611 | ** and write that byte sequence into pCell[]. Overflow pages are |
| 4612 | ** allocated and filled in as necessary. The calling procedure |
| 4613 | ** is responsible for making sure sufficient space has been allocated |
| 4614 | ** for pCell[]. |
| 4615 | ** |
| 4616 | ** Note that pCell does not necessary need to point to the pPage->aData |
| 4617 | ** area. pCell might point to some temporary storage. The cell will |
| 4618 | ** be constructed in this temporary area then copied into pPage->aData |
| 4619 | ** later. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4620 | */ |
| 4621 | static int fillInCell( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4622 | MemPage *pPage, /* The page that contains the cell */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4623 | unsigned char *pCell, /* Complete text of the cell */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 4624 | const void *pKey, i64 nKey, /* The key */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4625 | const void *pData,int nData, /* The data */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4626 | int nZero, /* Extra zero bytes to append to pData */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4627 | int *pnSize /* Write cell size here */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4628 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4629 | int nPayload; |
drh | 8c6fa9b | 2004-05-26 00:01:53 +0000 | [diff] [blame] | 4630 | const u8 *pSrc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4631 | int nSrc, n, rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4632 | int spaceLeft; |
| 4633 | MemPage *pOvfl = 0; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4634 | MemPage *pToRelease = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4635 | unsigned char *pPrior; |
| 4636 | unsigned char *pPayload; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4637 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4638 | Pgno pgnoOvfl = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4639 | int nHeader; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4640 | CellInfo info; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4641 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4642 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4643 | |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 4644 | /* pPage is not necessarily writeable since pCell might be auxiliary |
| 4645 | ** buffer space that is separate from the pPage buffer area */ |
| 4646 | assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize] |
| 4647 | || sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 4648 | |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4649 | /* Fill in the header. */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4650 | nHeader = 0; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4651 | if( !pPage->leaf ){ |
| 4652 | nHeader += 4; |
| 4653 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4654 | if( pPage->hasData ){ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4655 | nHeader += putVarint(&pCell[nHeader], nData+nZero); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4656 | }else{ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4657 | nData = nZero = 0; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4658 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4659 | nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4660 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4661 | assert( info.nHeader==nHeader ); |
| 4662 | assert( info.nKey==nKey ); |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 4663 | assert( info.nData==(u32)(nData+nZero) ); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4664 | |
| 4665 | /* Fill in the payload */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4666 | nPayload = nData + nZero; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4667 | if( pPage->intKey ){ |
| 4668 | pSrc = pData; |
| 4669 | nSrc = nData; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4670 | nData = 0; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4671 | }else{ |
drh | 20abac2 | 2009-01-28 20:21:17 +0000 | [diff] [blame] | 4672 | if( nKey>0x7fffffff || pKey==0 ){ |
| 4673 | return SQLITE_CORRUPT; |
| 4674 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4675 | nPayload += (int)nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4676 | pSrc = pKey; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4677 | nSrc = (int)nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4678 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4679 | *pnSize = info.nSize; |
| 4680 | spaceLeft = info.nLocal; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4681 | pPayload = &pCell[nHeader]; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4682 | pPrior = &pCell[info.iOverflow]; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4683 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4684 | while( nPayload>0 ){ |
| 4685 | if( spaceLeft==0 ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4686 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4687 | Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */ |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4688 | if( pBt->autoVacuum ){ |
| 4689 | do{ |
| 4690 | pgnoOvfl++; |
| 4691 | } while( |
| 4692 | PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt) |
| 4693 | ); |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4694 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4695 | #endif |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4696 | rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4697 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4698 | /* If the database supports auto-vacuum, and the second or subsequent |
| 4699 | ** overflow page is being allocated, add an entry to the pointer-map |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 4700 | ** for that page now. |
| 4701 | ** |
| 4702 | ** If this is the first overflow page, then write a partial entry |
| 4703 | ** to the pointer-map. If we write nothing to this pointer-map slot, |
| 4704 | ** then the optimistic overflow chain processing in clearCell() |
| 4705 | ** may misinterpret the uninitialised values and delete the |
| 4706 | ** wrong pages from the database. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4707 | */ |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 4708 | if( pBt->autoVacuum && rc==SQLITE_OK ){ |
| 4709 | u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1); |
| 4710 | rc = ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap); |
danielk1977 | 89a4be8 | 2007-05-23 13:34:32 +0000 | [diff] [blame] | 4711 | if( rc ){ |
| 4712 | releasePage(pOvfl); |
| 4713 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4714 | } |
| 4715 | #endif |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4716 | if( rc ){ |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4717 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4718 | return rc; |
| 4719 | } |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 4720 | |
| 4721 | /* If pToRelease is not zero than pPrior points into the data area |
| 4722 | ** of pToRelease. Make sure pToRelease is still writeable. */ |
| 4723 | assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) ); |
| 4724 | |
| 4725 | /* If pPrior is part of the data area of pPage, then make sure pPage |
| 4726 | ** is still writeable */ |
| 4727 | assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize] |
| 4728 | || sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 4729 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4730 | put4byte(pPrior, pgnoOvfl); |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4731 | releasePage(pToRelease); |
| 4732 | pToRelease = pOvfl; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4733 | pPrior = pOvfl->aData; |
| 4734 | put4byte(pPrior, 0); |
| 4735 | pPayload = &pOvfl->aData[4]; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4736 | spaceLeft = pBt->usableSize - 4; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4737 | } |
| 4738 | n = nPayload; |
| 4739 | if( n>spaceLeft ) n = spaceLeft; |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 4740 | |
| 4741 | /* If pToRelease is not zero than pPayload points into the data area |
| 4742 | ** of pToRelease. Make sure pToRelease is still writeable. */ |
| 4743 | assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) ); |
| 4744 | |
| 4745 | /* If pPayload is part of the data area of pPage, then make sure pPage |
| 4746 | ** is still writeable */ |
| 4747 | assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize] |
| 4748 | || sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 4749 | |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4750 | if( nSrc>0 ){ |
| 4751 | if( n>nSrc ) n = nSrc; |
| 4752 | assert( pSrc ); |
| 4753 | memcpy(pPayload, pSrc, n); |
| 4754 | }else{ |
| 4755 | memset(pPayload, 0, n); |
| 4756 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4757 | nPayload -= n; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4758 | pPayload += n; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4759 | pSrc += n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4760 | nSrc -= n; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4761 | spaceLeft -= n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4762 | if( nSrc==0 ){ |
| 4763 | nSrc = nData; |
| 4764 | pSrc = pData; |
| 4765 | } |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4766 | } |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4767 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4768 | return SQLITE_OK; |
| 4769 | } |
| 4770 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4771 | /* |
| 4772 | ** Remove the i-th cell from pPage. This routine effects pPage only. |
| 4773 | ** The cell content is not freed or deallocated. It is assumed that |
| 4774 | ** the cell content has been copied someplace else. This routine just |
| 4775 | ** removes the reference to the cell from pPage. |
| 4776 | ** |
| 4777 | ** "sz" must be the number of bytes in the cell. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4778 | */ |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 4779 | static int dropCell(MemPage *pPage, int idx, int sz){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4780 | int i; /* Loop counter */ |
| 4781 | int pc; /* Offset to cell content of cell being deleted */ |
| 4782 | u8 *data; /* pPage->aData */ |
| 4783 | u8 *ptr; /* Used to move bytes around within data[] */ |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 4784 | int rc; /* The return code */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4785 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4786 | assert( idx>=0 && idx<pPage->nCell ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4787 | assert( sz==cellSize(pPage, idx) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4788 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4789 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4790 | data = pPage->aData; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4791 | ptr = &data[pPage->cellOffset + 2*idx]; |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 4792 | pc = get2byte(ptr); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 4793 | if( (pc<pPage->hdrOffset+6+(pPage->leaf?0:4)) |
| 4794 | || (pc+sz>pPage->pBt->usableSize) ){ |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 4795 | return SQLITE_CORRUPT_BKPT; |
| 4796 | } |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 4797 | rc = freeSpace(pPage, pc, sz); |
| 4798 | if( rc!=SQLITE_OK ){ |
| 4799 | return rc; |
| 4800 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4801 | for(i=idx+1; i<pPage->nCell; i++, ptr+=2){ |
| 4802 | ptr[0] = ptr[2]; |
| 4803 | ptr[1] = ptr[3]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4804 | } |
| 4805 | pPage->nCell--; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4806 | put2byte(&data[pPage->hdrOffset+3], pPage->nCell); |
| 4807 | pPage->nFree += 2; |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 4808 | return SQLITE_OK; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4809 | } |
| 4810 | |
| 4811 | /* |
| 4812 | ** Insert a new cell on pPage at cell index "i". pCell points to the |
| 4813 | ** content of the cell. |
| 4814 | ** |
| 4815 | ** 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] | 4816 | ** will not fit, then make a copy of the cell content into pTemp if |
| 4817 | ** pTemp is not null. Regardless of pTemp, allocate a new entry |
| 4818 | ** in pPage->aOvfl[] and make it point to the cell content (either |
| 4819 | ** in pTemp or the original pCell) and also record its index. |
| 4820 | ** Allocating a new entry in pPage->aCell[] implies that |
| 4821 | ** pPage->nOverflow is incremented. |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4822 | ** |
| 4823 | ** If nSkip is non-zero, then do not copy the first nSkip bytes of the |
| 4824 | ** cell. The caller will overwrite them after this function returns. If |
drh | 4b238df | 2005-01-08 15:43:18 +0000 | [diff] [blame] | 4825 | ** 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] | 4826 | ** (but pCell+nSkip is always valid). |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4827 | */ |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4828 | static int insertCell( |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4829 | MemPage *pPage, /* Page into which we are copying */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4830 | int i, /* New cell becomes the i-th cell of the page */ |
| 4831 | u8 *pCell, /* Content of the new cell */ |
| 4832 | int sz, /* Bytes of content in pCell */ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4833 | u8 *pTemp, /* Temp storage space for pCell, if needed */ |
| 4834 | u8 nSkip /* Do not write the first nSkip bytes of the cell */ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4835 | ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4836 | int idx; /* Where to write new cell content in data[] */ |
| 4837 | int j; /* Loop counter */ |
| 4838 | int top; /* First byte of content for any cell in data[] */ |
| 4839 | int end; /* First byte past the last cell pointer in data[] */ |
| 4840 | int ins; /* Index in data[] where new cell pointer is inserted */ |
| 4841 | int hdr; /* Offset into data[] of the page header */ |
| 4842 | int cellOffset; /* Address of first cell pointer in data[] */ |
| 4843 | u8 *data; /* The content of the whole page */ |
| 4844 | u8 *ptr; /* Used for moving information around in data[] */ |
| 4845 | |
| 4846 | assert( i>=0 && i<=pPage->nCell+pPage->nOverflow ); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4847 | assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=5460 ); |
| 4848 | assert( pPage->nOverflow<=ArraySize(pPage->aOvfl) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4849 | assert( sz==cellSizePtr(pPage, pCell) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4850 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4851 | if( pPage->nOverflow || sz+2>pPage->nFree ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4852 | if( pTemp ){ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4853 | memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4854 | pCell = pTemp; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4855 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4856 | j = pPage->nOverflow++; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 4857 | assert( j<(int)(sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0])) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4858 | pPage->aOvfl[j].pCell = pCell; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4859 | pPage->aOvfl[j].idx = (u16)i; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4860 | pPage->nFree = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4861 | }else{ |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4862 | int rc = sqlite3PagerWrite(pPage->pDbPage); |
| 4863 | if( rc!=SQLITE_OK ){ |
| 4864 | return rc; |
| 4865 | } |
| 4866 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4867 | data = pPage->aData; |
| 4868 | hdr = pPage->hdrOffset; |
| 4869 | top = get2byte(&data[hdr+5]); |
| 4870 | cellOffset = pPage->cellOffset; |
| 4871 | end = cellOffset + 2*pPage->nCell + 2; |
| 4872 | ins = cellOffset + 2*i; |
| 4873 | if( end > top - sz ){ |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 4874 | rc = defragmentPage(pPage); |
| 4875 | if( rc!=SQLITE_OK ){ |
| 4876 | return rc; |
| 4877 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4878 | top = get2byte(&data[hdr+5]); |
| 4879 | assert( end + sz <= top ); |
| 4880 | } |
| 4881 | idx = allocateSpace(pPage, sz); |
| 4882 | assert( idx>0 ); |
| 4883 | assert( end <= get2byte(&data[hdr+5]) ); |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 4884 | if (idx+sz > pPage->pBt->usableSize) { |
shane | 34ac18d | 2008-11-11 22:18:20 +0000 | [diff] [blame] | 4885 | return SQLITE_CORRUPT_BKPT; |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 4886 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4887 | pPage->nCell++; |
| 4888 | pPage->nFree -= 2; |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4889 | memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4890 | for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){ |
| 4891 | ptr[0] = ptr[-2]; |
| 4892 | ptr[1] = ptr[-1]; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4893 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4894 | put2byte(&data[ins], idx); |
| 4895 | put2byte(&data[hdr+3], pPage->nCell); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4896 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4897 | if( pPage->pBt->autoVacuum ){ |
| 4898 | /* The cell may contain a pointer to an overflow page. If so, write |
| 4899 | ** the entry for the overflow page into the pointer map. |
| 4900 | */ |
| 4901 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4902 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 4903 | assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4904 | if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ |
| 4905 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4906 | rc = ptrmapPut(pPage->pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4907 | if( rc!=SQLITE_OK ) return rc; |
| 4908 | } |
| 4909 | } |
| 4910 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4911 | } |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4912 | |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4913 | return SQLITE_OK; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4914 | } |
| 4915 | |
| 4916 | /* |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4917 | ** Add a list of cells to a page. The page should be initially empty. |
| 4918 | ** The cells are guaranteed to fit on the page. |
| 4919 | */ |
| 4920 | static void assemblePage( |
| 4921 | MemPage *pPage, /* The page to be assemblied */ |
| 4922 | int nCell, /* The number of cells to add to this page */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4923 | u8 **apCell, /* Pointers to cell bodies */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 4924 | u16 *aSize /* Sizes of the cells */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4925 | ){ |
| 4926 | int i; /* Loop counter */ |
| 4927 | int totalSize; /* Total size of all cells */ |
| 4928 | int hdr; /* Index of page header */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4929 | int cellptr; /* Address of next cell pointer */ |
| 4930 | int cellbody; /* Address of next cell body */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4931 | u8 *data; /* Data for the page */ |
| 4932 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4933 | assert( pPage->nOverflow==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4934 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4935 | assert( nCell>=0 && nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=5460 ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4936 | totalSize = 0; |
| 4937 | for(i=0; i<nCell; i++){ |
| 4938 | totalSize += aSize[i]; |
| 4939 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4940 | assert( totalSize+2*nCell<=pPage->nFree ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4941 | assert( pPage->nCell==0 ); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 4942 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4943 | cellptr = pPage->cellOffset; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4944 | data = pPage->aData; |
| 4945 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4946 | put2byte(&data[hdr+3], nCell); |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 4947 | if( nCell ){ |
| 4948 | cellbody = allocateSpace(pPage, totalSize); |
| 4949 | assert( cellbody>0 ); |
| 4950 | assert( pPage->nFree >= 2*nCell ); |
| 4951 | pPage->nFree -= 2*nCell; |
| 4952 | for(i=0; i<nCell; i++){ |
| 4953 | put2byte(&data[cellptr], cellbody); |
| 4954 | memcpy(&data[cellbody], apCell[i], aSize[i]); |
| 4955 | cellptr += 2; |
| 4956 | cellbody += aSize[i]; |
| 4957 | } |
| 4958 | assert( cellbody==pPage->pBt->usableSize ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4959 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4960 | pPage->nCell = (u16)nCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4961 | } |
| 4962 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4963 | /* |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4964 | ** The following parameters determine how many adjacent pages get involved |
| 4965 | ** in a balancing operation. NN is the number of neighbors on either side |
| 4966 | ** of the page that participate in the balancing operation. NB is the |
| 4967 | ** total number of pages that participate, including the target page and |
| 4968 | ** NN neighbors on either side. |
| 4969 | ** |
| 4970 | ** The minimum value of NN is 1 (of course). Increasing NN above 1 |
| 4971 | ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance |
| 4972 | ** in exchange for a larger degradation in INSERT and UPDATE performance. |
| 4973 | ** The value of NN appears to give the best results overall. |
| 4974 | */ |
| 4975 | #define NN 1 /* Number of neighbors on either side of pPage */ |
| 4976 | #define NB (NN*2+1) /* Total pages involved in the balance */ |
| 4977 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4978 | /* Forward reference */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4979 | static int balance(BtCursor*, int); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4980 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4981 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 4982 | /* |
| 4983 | ** This version of balance() handles the common special case where |
| 4984 | ** a new entry is being inserted on the extreme right-end of the |
| 4985 | ** tree, in other words, when the new entry will become the largest |
| 4986 | ** entry in the tree. |
| 4987 | ** |
| 4988 | ** Instead of trying balance the 3 right-most leaf pages, just add |
| 4989 | ** a new page to the right-hand side and put the one new entry in |
| 4990 | ** that page. This leaves the right side of the tree somewhat |
| 4991 | ** unbalanced. But odds are that we will be inserting new entries |
| 4992 | ** at the end soon afterwards so the nearly empty page will quickly |
| 4993 | ** fill up. On average. |
| 4994 | ** |
| 4995 | ** pPage is the leaf page which is the right-most page in the tree. |
| 4996 | ** pParent is its parent. pPage must have a single overflow entry |
| 4997 | ** which is also the right-most entry on the page. |
| 4998 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4999 | static int balance_quick(BtCursor *pCur){ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5000 | int rc; |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 5001 | MemPage *pNew = 0; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5002 | Pgno pgnoNew; |
| 5003 | u8 *pCell; |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5004 | u16 szCell; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5005 | CellInfo info; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5006 | MemPage *pPage = pCur->apPage[pCur->iPage]; |
| 5007 | MemPage *pParent = pCur->apPage[pCur->iPage-1]; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5008 | BtShared *pBt = pPage->pBt; |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5009 | int parentIdx = pParent->nCell; /* pParent new divider cell index */ |
| 5010 | int parentSize; /* Size of new divider cell */ |
| 5011 | u8 parentCell[64]; /* Space for the new divider cell */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5012 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5013 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5014 | |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5015 | /* Allocate a new page. Insert the overflow cell from pPage |
| 5016 | ** into it. Then remove the overflow cell from pPage. |
| 5017 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5018 | rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0); |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 5019 | if( rc==SQLITE_OK ){ |
| 5020 | pCell = pPage->aOvfl[0].pCell; |
| 5021 | szCell = cellSizePtr(pPage, pCell); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 5022 | assert( sqlite3PagerIswriteable(pNew->pDbPage) ); |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 5023 | zeroPage(pNew, pPage->aData[0]); |
| 5024 | assemblePage(pNew, 1, &pCell, &szCell); |
| 5025 | pPage->nOverflow = 0; |
| 5026 | |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 5027 | /* pPage is currently the right-child of pParent. Change this |
| 5028 | ** so that the right-child is the new page allocated above and |
| 5029 | ** pPage is the next-to-right child. |
| 5030 | ** |
| 5031 | ** Ignore the return value of the call to fillInCell(). fillInCell() |
| 5032 | ** may only return other than SQLITE_OK if it is required to allocate |
| 5033 | ** one or more overflow pages. Since an internal table B-Tree cell |
| 5034 | ** may never spill over onto an overflow page (it is a maximum of |
| 5035 | ** 13 bytes in size), it is not neccessary to check the return code. |
| 5036 | ** |
| 5037 | ** Similarly, the insertCell() function cannot fail if the page |
| 5038 | ** being inserted into is already writable and the cell does not |
| 5039 | ** contain an overflow pointer. So ignore this return code too. |
| 5040 | */ |
| 5041 | assert( pPage->nCell>0 ); |
| 5042 | pCell = findCell(pPage, pPage->nCell-1); |
| 5043 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
| 5044 | fillInCell(pParent, parentCell, 0, info.nKey, 0, 0, 0, &parentSize); |
| 5045 | assert( parentSize<64 ); |
| 5046 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
| 5047 | insertCell(pParent, parentIdx, parentCell, parentSize, 0, 4); |
| 5048 | put4byte(findOverflowCell(pParent,parentIdx), pPage->pgno); |
| 5049 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew); |
| 5050 | |
| 5051 | /* If this is an auto-vacuum database, update the pointer map |
| 5052 | ** with entries for the new page, and any pointer from the |
| 5053 | ** cell on the page to an overflow page. |
| 5054 | */ |
| 5055 | if( ISAUTOVACUUM ){ |
| 5056 | rc = ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno); |
| 5057 | if( rc==SQLITE_OK ){ |
| 5058 | rc = ptrmapPutOvfl(pNew, 0); |
| 5059 | } |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5060 | } |
danielk1977 | e08a3c4 | 2008-09-18 18:17:03 +0000 | [diff] [blame] | 5061 | |
| 5062 | /* Release the reference to the new page. */ |
| 5063 | releasePage(pNew); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5064 | } |
| 5065 | |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 5066 | /* At this point the pPage->nFree variable is not set correctly with |
| 5067 | ** respect to the content of the page (because it was set to 0 by |
| 5068 | ** insertCell). So call sqlite3BtreeInitPage() to make sure it is |
| 5069 | ** correct. |
| 5070 | ** |
| 5071 | ** This has to be done even if an error will be returned. Normally, if |
| 5072 | ** an error occurs during tree balancing, the contents of MemPage are |
| 5073 | ** not important, as they will be recalculated when the page is rolled |
| 5074 | ** back. But here, in balance_quick(), it is possible that pPage has |
| 5075 | ** not yet been marked dirty or written into the journal file. Therefore |
| 5076 | ** it will not be rolled back and so it is important to make sure that |
| 5077 | ** the page data and contents of MemPage are consistent. |
| 5078 | */ |
| 5079 | pPage->isInit = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5080 | sqlite3BtreeInitPage(pPage); |
danielk1977 | a4124bd | 2008-12-23 10:37:47 +0000 | [diff] [blame] | 5081 | assert( pPage->nOverflow==0 ); |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 5082 | |
danielk1977 | e08a3c4 | 2008-09-18 18:17:03 +0000 | [diff] [blame] | 5083 | /* If everything else succeeded, balance the parent page, in |
| 5084 | ** case the divider cell inserted caused it to become overfull. |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5085 | */ |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 5086 | if( rc==SQLITE_OK ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5087 | releasePage(pPage); |
| 5088 | pCur->iPage--; |
| 5089 | rc = balance(pCur, 0); |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 5090 | } |
| 5091 | return rc; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5092 | } |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 5093 | #endif /* SQLITE_OMIT_QUICKBALANCE */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5094 | |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5095 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 5096 | ** This routine redistributes Cells on pPage and up to NN*2 siblings |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5097 | ** 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] | 5098 | ** Usually NN siblings on either side of pPage is used in the balancing, |
| 5099 | ** though more siblings might come from one side if pPage is the first |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 5100 | ** 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] | 5101 | ** (something which can only happen if pPage is the root page or a |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5102 | ** child of root) then all available siblings participate in the balancing. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5103 | ** |
drh | 0c6cc4e | 2004-06-15 02:13:26 +0000 | [diff] [blame] | 5104 | ** The number of siblings of pPage might be increased or decreased by one or |
| 5105 | ** 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] | 5106 | ** is special and is allowed to be nearly empty. If pPage is |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5107 | ** the root page, then the depth of the tree might be increased |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5108 | ** or decreased by one, as necessary, to keep the root page from being |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 5109 | ** overfull or completely empty. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5110 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5111 | ** Note that when this routine is called, some of the Cells on pPage |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5112 | ** might not actually be stored in pPage->aData[]. This can happen |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5113 | ** 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] | 5114 | ** make sure all Cells for pPage once again fit in pPage->aData[]. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5115 | ** |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5116 | ** In the course of balancing the siblings of pPage, the parent of pPage |
| 5117 | ** might become overfull or underfull. If that happens, then this routine |
| 5118 | ** is called recursively on the parent. |
| 5119 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5120 | ** If this routine fails for any reason, it might leave the database |
| 5121 | ** in a corrupted state. So if this routine fails, the database should |
| 5122 | ** be rolled back. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5123 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5124 | static int balance_nonroot(BtCursor *pCur){ |
| 5125 | MemPage *pPage; /* The over or underfull page to balance */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5126 | MemPage *pParent; /* The parent of pPage */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5127 | BtShared *pBt; /* The whole database */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5128 | int nCell = 0; /* Number of cells in apCell[] */ |
| 5129 | int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */ |
danielk1977 | a4124bd | 2008-12-23 10:37:47 +0000 | [diff] [blame] | 5130 | int nOld = 0; /* Number of pages in apOld[] */ |
| 5131 | int nNew = 0; /* Number of pages in apNew[] */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5132 | int nDiv; /* Number of cells in apDiv[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5133 | int i, j, k; /* Loop counters */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5134 | int idx; /* Index of pPage in pParent->aCell[] */ |
| 5135 | int nxDiv; /* Next divider slot in pParent->aCell[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5136 | int rc; /* The return code */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5137 | int leafCorrection; /* 4 if pPage is a leaf. 0 if not */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5138 | int leafData; /* True if pPage is a leaf of a LEAFDATA tree */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5139 | int usableSpace; /* Bytes in pPage beyond the header */ |
| 5140 | int pageFlags; /* Value of pPage->aData[0] */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5141 | int subtotal; /* Subtotal of bytes in cells on one page */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5142 | int iSpace1 = 0; /* First unused byte of aSpace1[] */ |
| 5143 | int iSpace2 = 0; /* First unused byte of aSpace2[] */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5144 | int szScratch; /* Size of scratch memory requested */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5145 | MemPage *apOld[NB]; /* pPage and up to two siblings */ |
| 5146 | Pgno pgnoOld[NB]; /* Page numbers for each page in apOld[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5147 | MemPage *apCopy[NB]; /* Private copies of apOld[] pages */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5148 | MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ |
| 5149 | Pgno pgnoNew[NB+2]; /* Page numbers for each page in apNew[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5150 | u8 *apDiv[NB]; /* Divider cells in pParent */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5151 | int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */ |
| 5152 | int szNew[NB+2]; /* Combined size of cells place on i-th page */ |
danielk1977 | 50f059b | 2005-03-29 02:54:03 +0000 | [diff] [blame] | 5153 | u8 **apCell = 0; /* All cells begin balanced */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5154 | u16 *szCell; /* Local size of all cells in apCell[] */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5155 | u8 *aCopy[NB]; /* Space for holding data of apCopy[] */ |
| 5156 | u8 *aSpace1; /* Space for copies of dividers cells before balance */ |
| 5157 | u8 *aSpace2 = 0; /* Space for overflow dividers cells after balance */ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5158 | u8 *aFrom = 0; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5159 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5160 | pPage = pCur->apPage[pCur->iPage]; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5161 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 5162 | VVA_ONLY( pCur->pagesShuffled = 1 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5163 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5164 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5165 | ** Find the parent page. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5166 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5167 | assert( pCur->iPage>0 ); |
| 5168 | assert( pPage->isInit ); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 5169 | assert( sqlite3PagerIswriteable(pPage->pDbPage) || pPage->nOverflow==1 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5170 | pBt = pPage->pBt; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5171 | pParent = pCur->apPage[pCur->iPage-1]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5172 | assert( pParent ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5173 | if( SQLITE_OK!=(rc = sqlite3PagerWrite(pParent->pDbPage)) ){ |
danielk1977 | a4124bd | 2008-12-23 10:37:47 +0000 | [diff] [blame] | 5174 | goto balance_cleanup; |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 5175 | } |
danielk1977 | 474b7cc | 2008-07-09 11:49:46 +0000 | [diff] [blame] | 5176 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5177 | TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno)); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5178 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 5179 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 5180 | /* |
| 5181 | ** A special case: If a new entry has just been inserted into a |
| 5182 | ** 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] | 5183 | ** 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] | 5184 | ** largest key) then use the special balance_quick() routine for |
| 5185 | ** balancing. balance_quick() is much faster and results in a tighter |
| 5186 | ** packing of data in the common case. |
| 5187 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5188 | if( pPage->leaf && |
| 5189 | pPage->intKey && |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5190 | pPage->nOverflow==1 && |
| 5191 | pPage->aOvfl[0].idx==pPage->nCell && |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5192 | pParent->pgno!=1 && |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5193 | get4byte(&pParent->aData[pParent->hdrOffset+8])==pPage->pgno |
| 5194 | ){ |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 5195 | assert( pPage->intKey ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5196 | /* |
| 5197 | ** TODO: Check the siblings to the left of pPage. It may be that |
| 5198 | ** they are not full and no new page is required. |
| 5199 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5200 | return balance_quick(pCur); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5201 | } |
| 5202 | #endif |
| 5203 | |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 5204 | if( SQLITE_OK!=(rc = sqlite3PagerWrite(pPage->pDbPage)) ){ |
danielk1977 | a4124bd | 2008-12-23 10:37:47 +0000 | [diff] [blame] | 5205 | goto balance_cleanup; |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 5206 | } |
| 5207 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5208 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5209 | ** Find the cell in the parent page whose left child points back |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5210 | ** to pPage. The "idx" variable is the index of that cell. If pPage |
| 5211 | ** is the rightmost child of pParent then set idx to pParent->nCell |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5212 | */ |
danielk1977 | bf93c56 | 2008-09-29 15:53:25 +0000 | [diff] [blame] | 5213 | idx = pCur->aiIdx[pCur->iPage-1]; |
| 5214 | assertParentIndex(pParent, idx, pPage->pgno); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5215 | |
| 5216 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5217 | ** Find sibling pages to pPage and the cells in pParent that divide |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5218 | ** the siblings. An attempt is made to find NN siblings on either |
| 5219 | ** side of pPage. More siblings are taken from one side, however, if |
| 5220 | ** pPage there are fewer than NN siblings on the other side. If pParent |
| 5221 | ** has NB or fewer children then all children of pParent are taken. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5222 | */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5223 | nxDiv = idx - NN; |
| 5224 | if( nxDiv + NB > pParent->nCell ){ |
| 5225 | nxDiv = pParent->nCell - NB + 1; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5226 | } |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5227 | if( nxDiv<0 ){ |
| 5228 | nxDiv = 0; |
| 5229 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5230 | nDiv = 0; |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5231 | for(i=0, k=nxDiv; i<NB; i++, k++){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5232 | if( k<pParent->nCell ){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5233 | apDiv[i] = findCell(pParent, k); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5234 | nDiv++; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5235 | assert( !pParent->leaf ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5236 | pgnoOld[i] = get4byte(apDiv[i]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5237 | }else if( k==pParent->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5238 | pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+8]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5239 | }else{ |
| 5240 | break; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5241 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5242 | rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i]); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5243 | if( rc ) goto balance_cleanup; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5244 | /* apOld[i]->idxParent = k; */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5245 | apCopy[i] = 0; |
| 5246 | assert( i==nOld ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5247 | nOld++; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5248 | nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5249 | } |
| 5250 | |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5251 | /* Make nMaxCells a multiple of 4 in order to preserve 8-byte |
drh | 8d97f1f | 2005-05-05 18:14:13 +0000 | [diff] [blame] | 5252 | ** alignment */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5253 | nMaxCells = (nMaxCells + 3)&~3; |
drh | 8d97f1f | 2005-05-05 18:14:13 +0000 | [diff] [blame] | 5254 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5255 | /* |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5256 | ** Allocate space for memory structures |
| 5257 | */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5258 | szScratch = |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5259 | nMaxCells*sizeof(u8*) /* apCell */ |
| 5260 | + nMaxCells*sizeof(u16) /* szCell */ |
| 5261 | + (ROUND8(sizeof(MemPage))+pBt->pageSize)*NB /* aCopy */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5262 | + pBt->pageSize /* aSpace1 */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5263 | + (ISAUTOVACUUM ? nMaxCells : 0); /* aFrom */ |
| 5264 | apCell = sqlite3ScratchMalloc( szScratch ); |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5265 | if( apCell==0 ){ |
| 5266 | rc = SQLITE_NOMEM; |
| 5267 | goto balance_cleanup; |
| 5268 | } |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5269 | szCell = (u16*)&apCell[nMaxCells]; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5270 | aCopy[0] = (u8*)&szCell[nMaxCells]; |
drh | 66e8008 | 2008-12-16 13:46:29 +0000 | [diff] [blame] | 5271 | assert( ((aCopy[0] - (u8*)0) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5272 | for(i=1; i<NB; i++){ |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 5273 | aCopy[i] = &aCopy[i-1][pBt->pageSize+ROUND8(sizeof(MemPage))]; |
drh | 66e8008 | 2008-12-16 13:46:29 +0000 | [diff] [blame] | 5274 | assert( ((aCopy[i] - (u8*)0) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5275 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5276 | aSpace1 = &aCopy[NB-1][pBt->pageSize+ROUND8(sizeof(MemPage))]; |
drh | 66e8008 | 2008-12-16 13:46:29 +0000 | [diff] [blame] | 5277 | assert( ((aSpace1 - (u8*)0) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5278 | if( ISAUTOVACUUM ){ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5279 | aFrom = &aSpace1[pBt->pageSize]; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5280 | } |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5281 | aSpace2 = sqlite3PageMalloc(pBt->pageSize); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5282 | if( aSpace2==0 ){ |
| 5283 | rc = SQLITE_NOMEM; |
| 5284 | goto balance_cleanup; |
| 5285 | } |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5286 | |
| 5287 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5288 | ** Make copies of the content of pPage and its siblings into aOld[]. |
| 5289 | ** The rest of this function will use data from the copies rather |
| 5290 | ** that the original pages since the original pages will be in the |
| 5291 | ** process of being overwritten. |
| 5292 | */ |
| 5293 | for(i=0; i<nOld; i++){ |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 5294 | MemPage *p = apCopy[i] = (MemPage*)aCopy[i]; |
| 5295 | memcpy(p, apOld[i], sizeof(MemPage)); |
| 5296 | p->aData = (void*)&p[1]; |
| 5297 | memcpy(p->aData, apOld[i]->aData, pBt->pageSize); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5298 | } |
| 5299 | |
| 5300 | /* |
| 5301 | ** Load pointers to all cells on sibling pages and the divider cells |
| 5302 | ** into the local apCell[] array. Make copies of the divider cells |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5303 | ** into space obtained form aSpace1[] and remove the the divider Cells |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5304 | ** from pParent. |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5305 | ** |
| 5306 | ** If the siblings are on leaf pages, then the child pointers of the |
| 5307 | ** divider cells are stripped from the cells before they are copied |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5308 | ** into aSpace1[]. In this way, all cells in apCell[] are without |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5309 | ** child pointers. If siblings are not leaves, then all cell in |
| 5310 | ** apCell[] include child pointers. Either way, all cells in apCell[] |
| 5311 | ** are alike. |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5312 | ** |
| 5313 | ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf. |
| 5314 | ** leafData: 1 if pPage holds key+data and pParent holds only keys. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5315 | */ |
| 5316 | nCell = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5317 | leafCorrection = pPage->leaf*4; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 5318 | leafData = pPage->hasData; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5319 | for(i=0; i<nOld; i++){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5320 | MemPage *pOld = apCopy[i]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5321 | int limit = pOld->nCell+pOld->nOverflow; |
| 5322 | for(j=0; j<limit; j++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5323 | assert( nCell<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5324 | apCell[nCell] = findOverflowCell(pOld, j); |
| 5325 | szCell[nCell] = cellSizePtr(pOld, apCell[nCell]); |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5326 | if( ISAUTOVACUUM ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5327 | int a; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 5328 | aFrom[nCell] = (u8)i; assert( i>=0 && i<6 ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5329 | for(a=0; a<pOld->nOverflow; a++){ |
| 5330 | if( pOld->aOvfl[a].pCell==apCell[nCell] ){ |
| 5331 | aFrom[nCell] = 0xFF; |
| 5332 | break; |
| 5333 | } |
| 5334 | } |
| 5335 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5336 | nCell++; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5337 | } |
| 5338 | if( i<nOld-1 ){ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5339 | u16 sz = cellSizePtr(pParent, apDiv[i]); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5340 | if( leafData ){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5341 | /* With the LEAFDATA flag, pParent cells hold only INTKEYs that |
| 5342 | ** are duplicates of keys on the child pages. We need to remove |
| 5343 | ** the divider cells from pParent, but the dividers cells are not |
| 5344 | ** added to apCell[] because they are duplicates of child cells. |
| 5345 | */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5346 | dropCell(pParent, nxDiv, sz); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5347 | }else{ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5348 | u8 *pTemp; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5349 | assert( nCell<nMaxCells ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5350 | szCell[nCell] = sz; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5351 | pTemp = &aSpace1[iSpace1]; |
| 5352 | iSpace1 += sz; |
| 5353 | assert( sz<=pBt->pageSize/4 ); |
| 5354 | assert( iSpace1<=pBt->pageSize ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5355 | memcpy(pTemp, apDiv[i], sz); |
| 5356 | apCell[nCell] = pTemp+leafCorrection; |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5357 | if( ISAUTOVACUUM ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5358 | aFrom[nCell] = 0xFF; |
| 5359 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5360 | dropCell(pParent, nxDiv, sz); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 5361 | assert( leafCorrection==0 || leafCorrection==4 ); |
| 5362 | szCell[nCell] -= (u16)leafCorrection; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5363 | assert( get4byte(pTemp)==pgnoOld[i] ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5364 | if( !pOld->leaf ){ |
| 5365 | assert( leafCorrection==0 ); |
| 5366 | /* The right pointer of the child page pOld becomes the left |
| 5367 | ** pointer of the divider cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5368 | memcpy(apCell[nCell], &pOld->aData[pOld->hdrOffset+8], 4); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5369 | }else{ |
| 5370 | assert( leafCorrection==4 ); |
danielk1977 | 39c9604 | 2007-05-12 10:41:47 +0000 | [diff] [blame] | 5371 | if( szCell[nCell]<4 ){ |
| 5372 | /* Do not allow any cells smaller than 4 bytes. */ |
| 5373 | szCell[nCell] = 4; |
| 5374 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5375 | } |
| 5376 | nCell++; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5377 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5378 | } |
| 5379 | } |
| 5380 | |
| 5381 | /* |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5382 | ** Figure out the number of pages needed to hold all nCell cells. |
| 5383 | ** Store this number in "k". Also compute szNew[] which is the total |
| 5384 | ** 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] | 5385 | ** in apCell[] of the cell that divides page i from page i+1. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5386 | ** cntNew[k] should equal nCell. |
| 5387 | ** |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5388 | ** Values computed by this block: |
| 5389 | ** |
| 5390 | ** k: The total number of sibling pages |
| 5391 | ** szNew[i]: Spaced used on the i-th sibling page. |
| 5392 | ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to |
| 5393 | ** the right of the i-th sibling page. |
| 5394 | ** usableSpace: Number of bytes of space available on each sibling. |
| 5395 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5396 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5397 | usableSpace = pBt->usableSize - 12 + leafCorrection; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5398 | for(subtotal=k=i=0; i<nCell; i++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5399 | assert( i<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5400 | subtotal += szCell[i] + 2; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5401 | if( subtotal > usableSpace ){ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5402 | szNew[k] = subtotal - szCell[i]; |
| 5403 | cntNew[k] = i; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5404 | if( leafData ){ i--; } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5405 | subtotal = 0; |
| 5406 | k++; |
| 5407 | } |
| 5408 | } |
| 5409 | szNew[k] = subtotal; |
| 5410 | cntNew[k] = nCell; |
| 5411 | k++; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5412 | |
| 5413 | /* |
| 5414 | ** The packing computed by the previous block is biased toward the siblings |
| 5415 | ** on the left side. The left siblings are always nearly full, while the |
| 5416 | ** right-most sibling might be nearly empty. This block of code attempts |
| 5417 | ** to adjust the packing of siblings to get a better balance. |
| 5418 | ** |
| 5419 | ** This adjustment is more than an optimization. The packing above might |
| 5420 | ** be so out of balance as to be illegal. For example, the right-most |
| 5421 | ** sibling might be completely empty. This adjustment is not optional. |
| 5422 | */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5423 | for(i=k-1; i>0; i--){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5424 | int szRight = szNew[i]; /* Size of sibling on the right */ |
| 5425 | int szLeft = szNew[i-1]; /* Size of sibling on the left */ |
| 5426 | int r; /* Index of right-most cell in left sibling */ |
| 5427 | int d; /* Index of first cell to the left of right sibling */ |
| 5428 | |
| 5429 | r = cntNew[i-1] - 1; |
| 5430 | d = r + 1 - leafData; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5431 | assert( d<nMaxCells ); |
| 5432 | assert( r<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5433 | while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){ |
| 5434 | szRight += szCell[d] + 2; |
| 5435 | szLeft -= szCell[r] + 2; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5436 | cntNew[i-1]--; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5437 | r = cntNew[i-1] - 1; |
| 5438 | d = r + 1 - leafData; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5439 | } |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5440 | szNew[i] = szRight; |
| 5441 | szNew[i-1] = szLeft; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5442 | } |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 5443 | |
| 5444 | /* Either we found one or more cells (cntnew[0])>0) or we are the |
| 5445 | ** a virtual root page. A virtual root page is when the real root |
| 5446 | ** page is page 1 and we are the only child of that page. |
| 5447 | */ |
| 5448 | assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5449 | |
| 5450 | /* |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5451 | ** Allocate k new pages. Reuse old pages where possible. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5452 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5453 | assert( pPage->pgno>1 ); |
| 5454 | pageFlags = pPage->aData[0]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5455 | for(i=0; i<k; i++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5456 | MemPage *pNew; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5457 | if( i<nOld ){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5458 | pNew = apNew[i] = apOld[i]; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5459 | pgnoNew[i] = pgnoOld[i]; |
| 5460 | apOld[i] = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5461 | rc = sqlite3PagerWrite(pNew->pDbPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 5462 | nNew++; |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 5463 | if( rc ) goto balance_cleanup; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5464 | }else{ |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 5465 | assert( i>0 ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5466 | rc = allocateBtreePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5467 | if( rc ) goto balance_cleanup; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5468 | apNew[i] = pNew; |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 5469 | nNew++; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5470 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5471 | } |
| 5472 | |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5473 | /* Free any old pages that were not reused as new pages. |
| 5474 | */ |
| 5475 | while( i<nOld ){ |
| 5476 | rc = freePage(apOld[i]); |
| 5477 | if( rc ) goto balance_cleanup; |
| 5478 | releasePage(apOld[i]); |
| 5479 | apOld[i] = 0; |
| 5480 | i++; |
| 5481 | } |
| 5482 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5483 | /* |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5484 | ** Put the new pages in accending order. This helps to |
| 5485 | ** keep entries in the disk file in order so that a scan |
| 5486 | ** of the table is a linear scan through the file. That |
| 5487 | ** in turn helps the operating system to deliver pages |
| 5488 | ** from the disk more rapidly. |
| 5489 | ** |
| 5490 | ** An O(n^2) insertion sort algorithm is used, but since |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5491 | ** n is never more than NB (a small constant), that should |
| 5492 | ** not be a problem. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5493 | ** |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5494 | ** When NB==3, this one optimization makes the database |
| 5495 | ** about 25% faster for large insertions and deletions. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5496 | */ |
| 5497 | for(i=0; i<k-1; i++){ |
| 5498 | int minV = pgnoNew[i]; |
| 5499 | int minI = i; |
| 5500 | for(j=i+1; j<k; j++){ |
drh | 7d02cb7 | 2003-06-04 16:24:39 +0000 | [diff] [blame] | 5501 | if( pgnoNew[j]<(unsigned)minV ){ |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5502 | minI = j; |
| 5503 | minV = pgnoNew[j]; |
| 5504 | } |
| 5505 | } |
| 5506 | if( minI>i ){ |
| 5507 | int t; |
| 5508 | MemPage *pT; |
| 5509 | t = pgnoNew[i]; |
| 5510 | pT = apNew[i]; |
| 5511 | pgnoNew[i] = pgnoNew[minI]; |
| 5512 | apNew[i] = apNew[minI]; |
| 5513 | pgnoNew[minI] = t; |
| 5514 | apNew[minI] = pT; |
| 5515 | } |
| 5516 | } |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5517 | 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] | 5518 | pgnoOld[0], |
| 5519 | nOld>=2 ? pgnoOld[1] : 0, |
| 5520 | nOld>=3 ? pgnoOld[2] : 0, |
drh | 10c0fa6 | 2004-05-18 12:50:17 +0000 | [diff] [blame] | 5521 | pgnoNew[0], szNew[0], |
| 5522 | nNew>=2 ? pgnoNew[1] : 0, nNew>=2 ? szNew[1] : 0, |
| 5523 | nNew>=3 ? pgnoNew[2] : 0, nNew>=3 ? szNew[2] : 0, |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5524 | nNew>=4 ? pgnoNew[3] : 0, nNew>=4 ? szNew[3] : 0, |
| 5525 | nNew>=5 ? pgnoNew[4] : 0, nNew>=5 ? szNew[4] : 0)); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5526 | |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5527 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5528 | ** Evenly distribute the data in apCell[] across the new pages. |
| 5529 | ** Insert divider cells into pParent as necessary. |
| 5530 | */ |
| 5531 | j = 0; |
| 5532 | for(i=0; i<nNew; i++){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5533 | /* Assemble the new sibling page. */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5534 | MemPage *pNew = apNew[i]; |
drh | 19642e5 | 2005-03-29 13:17:45 +0000 | [diff] [blame] | 5535 | assert( j<nMaxCells ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5536 | assert( pNew->pgno==pgnoNew[i] ); |
drh | 1013148 | 2008-07-11 03:34:09 +0000 | [diff] [blame] | 5537 | zeroPage(pNew, pageFlags); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 5538 | assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]); |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 5539 | assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5540 | assert( pNew->nOverflow==0 ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5541 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5542 | /* If this is an auto-vacuum database, update the pointer map entries |
| 5543 | ** that point to the siblings that were rearranged. These can be: left |
| 5544 | ** children of cells, the right-child of the page, or overflow pages |
| 5545 | ** pointed to by cells. |
| 5546 | */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5547 | if( ISAUTOVACUUM ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5548 | for(k=j; k<cntNew[i]; k++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5549 | assert( k<nMaxCells ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5550 | if( aFrom[k]==0xFF || apCopy[aFrom[k]]->pgno!=pNew->pgno ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5551 | rc = ptrmapPutOvfl(pNew, k-j); |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5552 | if( rc==SQLITE_OK && leafCorrection==0 ){ |
| 5553 | rc = ptrmapPut(pBt, get4byte(apCell[k]), PTRMAP_BTREE, pNew->pgno); |
| 5554 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5555 | if( rc!=SQLITE_OK ){ |
| 5556 | goto balance_cleanup; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5557 | } |
| 5558 | } |
| 5559 | } |
| 5560 | } |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5561 | |
| 5562 | j = cntNew[i]; |
| 5563 | |
| 5564 | /* If the sibling page assembled above was not the right-most sibling, |
| 5565 | ** insert a divider cell into the parent page. |
| 5566 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5567 | if( i<nNew-1 && j<nCell ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5568 | u8 *pCell; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5569 | u8 *pTemp; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5570 | int sz; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5571 | |
| 5572 | assert( j<nMaxCells ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5573 | pCell = apCell[j]; |
| 5574 | sz = szCell[j] + leafCorrection; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5575 | pTemp = &aSpace2[iSpace2]; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5576 | if( !pNew->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5577 | memcpy(&pNew->aData[8], pCell, 4); |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5578 | if( ISAUTOVACUUM |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5579 | && (aFrom[j]==0xFF || apCopy[aFrom[j]]->pgno!=pNew->pgno) |
| 5580 | ){ |
| 5581 | rc = ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno); |
| 5582 | if( rc!=SQLITE_OK ){ |
| 5583 | goto balance_cleanup; |
| 5584 | } |
| 5585 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5586 | }else if( leafData ){ |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 5587 | /* If the tree is a leaf-data tree, and the siblings are leaves, |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5588 | ** then there is no divider cell in apCell[]. Instead, the divider |
| 5589 | ** cell consists of the integer key for the right-most cell of |
| 5590 | ** the sibling-page assembled above only. |
| 5591 | */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5592 | CellInfo info; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5593 | j--; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5594 | sqlite3BtreeParseCellPtr(pNew, apCell[j], &info); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5595 | pCell = pTemp; |
drh | 20abac2 | 2009-01-28 20:21:17 +0000 | [diff] [blame] | 5596 | rc = fillInCell(pParent, pCell, 0, info.nKey, 0, 0, 0, &sz); |
| 5597 | if( rc!=SQLITE_OK ){ |
| 5598 | goto balance_cleanup; |
| 5599 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5600 | pTemp = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5601 | }else{ |
| 5602 | pCell -= 4; |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 5603 | /* Obscure case for non-leaf-data trees: If the cell at pCell was |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 5604 | ** previously stored on a leaf node, and its reported size was 4 |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 5605 | ** bytes, then it may actually be smaller than this |
| 5606 | ** (see sqlite3BtreeParseCellPtr(), 4 bytes is the minimum size of |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 5607 | ** any cell). But it is important to pass the correct size to |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 5608 | ** insertCell(), so reparse the cell now. |
| 5609 | ** |
| 5610 | ** Note that this can never happen in an SQLite data file, as all |
| 5611 | ** cells are at least 4 bytes. It only happens in b-trees used |
| 5612 | ** to evaluate "IN (SELECT ...)" and similar clauses. |
| 5613 | */ |
| 5614 | if( szCell[j]==4 ){ |
| 5615 | assert(leafCorrection==4); |
| 5616 | sz = cellSizePtr(pParent, pCell); |
| 5617 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5618 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5619 | iSpace2 += sz; |
| 5620 | assert( sz<=pBt->pageSize/4 ); |
| 5621 | assert( iSpace2<=pBt->pageSize ); |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 5622 | rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4); |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 5623 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 5624 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5625 | put4byte(findOverflowCell(pParent,nxDiv), pNew->pgno); |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5626 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5627 | /* If this is an auto-vacuum database, and not a leaf-data tree, |
| 5628 | ** then update the pointer map with an entry for the overflow page |
| 5629 | ** that the cell just inserted points to (if any). |
| 5630 | */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5631 | if( ISAUTOVACUUM && !leafData ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5632 | rc = ptrmapPutOvfl(pParent, nxDiv); |
| 5633 | if( rc!=SQLITE_OK ){ |
| 5634 | goto balance_cleanup; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5635 | } |
| 5636 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5637 | j++; |
| 5638 | nxDiv++; |
| 5639 | } |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5640 | |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5641 | /* Set the pointer-map entry for the new sibling page. */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5642 | if( ISAUTOVACUUM ){ |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5643 | rc = ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno); |
| 5644 | if( rc!=SQLITE_OK ){ |
| 5645 | goto balance_cleanup; |
| 5646 | } |
| 5647 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5648 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5649 | assert( j==nCell ); |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 5650 | assert( nOld>0 ); |
| 5651 | assert( nNew>0 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5652 | if( (pageFlags & PTF_LEAF)==0 ){ |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5653 | u8 *zChild = &apCopy[nOld-1]->aData[8]; |
| 5654 | memcpy(&apNew[nNew-1]->aData[8], zChild, 4); |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5655 | if( ISAUTOVACUUM ){ |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5656 | rc = ptrmapPut(pBt, get4byte(zChild), PTRMAP_BTREE, apNew[nNew-1]->pgno); |
| 5657 | if( rc!=SQLITE_OK ){ |
| 5658 | goto balance_cleanup; |
| 5659 | } |
| 5660 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5661 | } |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 5662 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5663 | if( nxDiv==pParent->nCell+pParent->nOverflow ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5664 | /* Right-most sibling is the right-most child of pParent */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5665 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew[nNew-1]); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5666 | }else{ |
| 5667 | /* Right-most sibling is the left child of the first entry in pParent |
| 5668 | ** past the right-most divider entry */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5669 | put4byte(findOverflowCell(pParent, nxDiv), pgnoNew[nNew-1]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5670 | } |
| 5671 | |
| 5672 | /* |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5673 | ** Balance the parent page. Note that the current page (pPage) might |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5674 | ** have been added to the freelist so it might no longer be initialized. |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5675 | ** But the parent page will always be initialized. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5676 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5677 | assert( pParent->isInit ); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5678 | sqlite3ScratchFree(apCell); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5679 | apCell = 0; |
danielk1977 | a4124bd | 2008-12-23 10:37:47 +0000 | [diff] [blame] | 5680 | TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n", |
| 5681 | pPage->pgno, nOld, nNew, nCell)); |
| 5682 | pPage->nOverflow = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5683 | releasePage(pPage); |
| 5684 | pCur->iPage--; |
| 5685 | rc = balance(pCur, 0); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5686 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5687 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5688 | ** Cleanup before returning. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5689 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5690 | balance_cleanup: |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5691 | sqlite3PageFree(aSpace2); |
| 5692 | sqlite3ScratchFree(apCell); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5693 | for(i=0; i<nOld; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5694 | releasePage(apOld[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5695 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5696 | for(i=0; i<nNew; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5697 | releasePage(apNew[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5698 | } |
danielk1977 | a4124bd | 2008-12-23 10:37:47 +0000 | [diff] [blame] | 5699 | pCur->apPage[pCur->iPage]->nOverflow = 0; |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 5700 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5701 | return rc; |
| 5702 | } |
| 5703 | |
| 5704 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5705 | ** This routine is called for the root page of a btree when the root |
| 5706 | ** page contains no cells. This is an opportunity to make the tree |
| 5707 | ** shallower by one level. |
| 5708 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5709 | static int balance_shallower(BtCursor *pCur){ |
| 5710 | MemPage *pPage; /* Root page of B-Tree */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5711 | MemPage *pChild; /* The only child page of pPage */ |
| 5712 | Pgno pgnoChild; /* Page number for pChild */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5713 | int rc = SQLITE_OK; /* Return code from subprocedures */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5714 | BtShared *pBt; /* The main BTree structure */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5715 | int mxCellPerPage; /* Maximum number of cells per page */ |
| 5716 | u8 **apCell; /* All cells from pages being balanced */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5717 | u16 *szCell; /* Local size of all cells */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5718 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5719 | assert( pCur->iPage==0 ); |
| 5720 | pPage = pCur->apPage[0]; |
| 5721 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5722 | assert( pPage->nCell==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5723 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5724 | pBt = pPage->pBt; |
| 5725 | mxCellPerPage = MX_CELL(pBt); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5726 | apCell = sqlite3Malloc( mxCellPerPage*(sizeof(u8*)+sizeof(u16)) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5727 | if( apCell==0 ) return SQLITE_NOMEM; |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5728 | szCell = (u16*)&apCell[mxCellPerPage]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5729 | if( pPage->leaf ){ |
| 5730 | /* The table is completely empty */ |
| 5731 | TRACE(("BALANCE: empty table %d\n", pPage->pgno)); |
| 5732 | }else{ |
| 5733 | /* The root page is empty but has one child. Transfer the |
| 5734 | ** information from that one child into the root page if it |
| 5735 | ** will fit. This reduces the depth of the tree by one. |
| 5736 | ** |
| 5737 | ** If the root page is page 1, it has less space available than |
| 5738 | ** its child (due to the 100 byte header that occurs at the beginning |
| 5739 | ** of the database fle), so it might not be able to hold all of the |
| 5740 | ** information currently contained in the child. If this is the |
| 5741 | ** case, then do not do the transfer. Leave page 1 empty except |
| 5742 | ** for the right-pointer to the child page. The child page becomes |
| 5743 | ** the virtual root of the tree. |
| 5744 | */ |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 5745 | VVA_ONLY( pCur->pagesShuffled = 1 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5746 | pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 5747 | assert( pgnoChild>0 ); |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 5748 | assert( pgnoChild<=pagerPagecount(pPage->pBt) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5749 | rc = sqlite3BtreeGetPage(pPage->pBt, pgnoChild, &pChild, 0); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5750 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5751 | if( pPage->pgno==1 ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5752 | rc = sqlite3BtreeInitPage(pChild); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5753 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5754 | assert( pChild->nOverflow==0 ); |
| 5755 | if( pChild->nFree>=100 ){ |
| 5756 | /* The child information will fit on the root page, so do the |
| 5757 | ** copy */ |
| 5758 | int i; |
| 5759 | zeroPage(pPage, pChild->aData[0]); |
| 5760 | for(i=0; i<pChild->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5761 | apCell[i] = findCell(pChild,i); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5762 | szCell[i] = cellSizePtr(pChild, apCell[i]); |
| 5763 | } |
| 5764 | assemblePage(pPage, pChild->nCell, apCell, szCell); |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 5765 | /* Copy the right-pointer of the child to the parent. */ |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 5766 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 5767 | put4byte(&pPage->aData[pPage->hdrOffset+8], |
| 5768 | get4byte(&pChild->aData[pChild->hdrOffset+8])); |
drh | 9bf9e9c | 2008-12-05 20:01:43 +0000 | [diff] [blame] | 5769 | rc = freePage(pChild); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5770 | TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno)); |
| 5771 | }else{ |
| 5772 | /* The child has more information that will fit on the root. |
| 5773 | ** The tree is already balanced. Do nothing. */ |
| 5774 | TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno)); |
| 5775 | } |
| 5776 | }else{ |
| 5777 | memcpy(pPage->aData, pChild->aData, pPage->pBt->usableSize); |
| 5778 | pPage->isInit = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5779 | rc = sqlite3BtreeInitPage(pPage); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5780 | assert( rc==SQLITE_OK ); |
| 5781 | freePage(pChild); |
| 5782 | TRACE(("BALANCE: transfer child %d into root %d\n", |
| 5783 | pChild->pgno, pPage->pgno)); |
| 5784 | } |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5785 | assert( pPage->nOverflow==0 ); |
shane | 831c329 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 5786 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | 9bf9e9c | 2008-12-05 20:01:43 +0000 | [diff] [blame] | 5787 | if( ISAUTOVACUUM && rc==SQLITE_OK ){ |
danielk1977 | 00a696d | 2008-09-29 16:41:31 +0000 | [diff] [blame] | 5788 | rc = setChildPtrmaps(pPage); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5789 | } |
shane | 831c329 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 5790 | #endif |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5791 | releasePage(pChild); |
| 5792 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5793 | end_shallow_balance: |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5794 | sqlite3_free(apCell); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5795 | return rc; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5796 | } |
| 5797 | |
| 5798 | |
| 5799 | /* |
| 5800 | ** The root page is overfull |
| 5801 | ** |
| 5802 | ** When this happens, Create a new child page and copy the |
| 5803 | ** contents of the root into the child. Then make the root |
| 5804 | ** page an empty page with rightChild pointing to the new |
| 5805 | ** child. Finally, call balance_internal() on the new child |
| 5806 | ** to cause it to split. |
| 5807 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5808 | static int balance_deeper(BtCursor *pCur){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5809 | int rc; /* Return value from subprocedures */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5810 | MemPage *pPage; /* Pointer to the root page */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5811 | MemPage *pChild; /* Pointer to a new child page */ |
| 5812 | Pgno pgnoChild; /* Page number of the new child page */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5813 | BtShared *pBt; /* The BTree */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5814 | int usableSize; /* Total usable size of a page */ |
| 5815 | u8 *data; /* Content of the parent page */ |
| 5816 | u8 *cdata; /* Content of the child page */ |
| 5817 | int hdr; /* Offset to page header in parent */ |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 5818 | int cbrk; /* Offset to content of first cell in parent */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5819 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5820 | assert( pCur->iPage==0 ); |
| 5821 | assert( pCur->apPage[0]->nOverflow>0 ); |
| 5822 | |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 5823 | VVA_ONLY( pCur->pagesShuffled = 1 ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5824 | pPage = pCur->apPage[0]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5825 | pBt = pPage->pBt; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5826 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 5827 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5828 | rc = allocateBtreePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5829 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5830 | assert( sqlite3PagerIswriteable(pChild->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5831 | usableSize = pBt->usableSize; |
| 5832 | data = pPage->aData; |
| 5833 | hdr = pPage->hdrOffset; |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 5834 | cbrk = get2byte(&data[hdr+5]); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5835 | cdata = pChild->aData; |
| 5836 | memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr); |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 5837 | memcpy(&cdata[cbrk], &data[cbrk], usableSize-cbrk); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 5838 | |
| 5839 | assert( pChild->isInit==0 ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5840 | rc = sqlite3BtreeInitPage(pChild); |
| 5841 | if( rc==SQLITE_OK ){ |
| 5842 | int nCopy = pPage->nOverflow*sizeof(pPage->aOvfl[0]); |
| 5843 | memcpy(pChild->aOvfl, pPage->aOvfl, nCopy); |
| 5844 | pChild->nOverflow = pPage->nOverflow; |
| 5845 | if( pChild->nOverflow ){ |
| 5846 | pChild->nFree = 0; |
| 5847 | } |
| 5848 | assert( pChild->nCell==pPage->nCell ); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 5849 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5850 | zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF); |
| 5851 | put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild); |
| 5852 | TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno)); |
| 5853 | if( ISAUTOVACUUM ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5854 | rc = ptrmapPut(pBt, pChild->pgno, PTRMAP_BTREE, pPage->pgno); |
shane | 831c329 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 5855 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5856 | if( rc==SQLITE_OK ){ |
danielk1977 | 00a696d | 2008-09-29 16:41:31 +0000 | [diff] [blame] | 5857 | rc = setChildPtrmaps(pChild); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5858 | } |
drh | 30df009 | 2008-12-23 15:58:06 +0000 | [diff] [blame] | 5859 | if( rc ){ |
| 5860 | pChild->nOverflow = 0; |
| 5861 | } |
shane | 831c329 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 5862 | #endif |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5863 | } |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5864 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5865 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5866 | if( rc==SQLITE_OK ){ |
| 5867 | pCur->iPage++; |
| 5868 | pCur->apPage[1] = pChild; |
danielk1977 | bf93c56 | 2008-09-29 15:53:25 +0000 | [diff] [blame] | 5869 | pCur->aiIdx[0] = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5870 | rc = balance_nonroot(pCur); |
| 5871 | }else{ |
| 5872 | releasePage(pChild); |
| 5873 | } |
| 5874 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5875 | return rc; |
| 5876 | } |
| 5877 | |
| 5878 | /* |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5879 | ** The page that pCur currently points to has just been modified in |
| 5880 | ** some way. This function figures out if this modification means the |
| 5881 | ** tree needs to be balanced, and if so calls the appropriate balancing |
| 5882 | ** routine. |
| 5883 | ** |
| 5884 | ** Parameter isInsert is true if a new cell was just inserted into the |
| 5885 | ** page, or false otherwise. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5886 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5887 | static int balance(BtCursor *pCur, int isInsert){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5888 | int rc = SQLITE_OK; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5889 | MemPage *pPage = pCur->apPage[pCur->iPage]; |
| 5890 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5891 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5892 | if( pCur->iPage==0 ){ |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 5893 | rc = sqlite3PagerWrite(pPage->pDbPage); |
| 5894 | if( rc==SQLITE_OK && pPage->nOverflow>0 ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5895 | rc = balance_deeper(pCur); |
danielk1977 | a4124bd | 2008-12-23 10:37:47 +0000 | [diff] [blame] | 5896 | assert( pCur->apPage[0]==pPage ); |
drh | 9bf9e9c | 2008-12-05 20:01:43 +0000 | [diff] [blame] | 5897 | assert( pPage->nOverflow==0 || rc!=SQLITE_OK ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5898 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5899 | if( rc==SQLITE_OK && pPage->nCell==0 ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5900 | rc = balance_shallower(pCur); |
danielk1977 | a4124bd | 2008-12-23 10:37:47 +0000 | [diff] [blame] | 5901 | assert( pCur->apPage[0]==pPage ); |
drh | 9bf9e9c | 2008-12-05 20:01:43 +0000 | [diff] [blame] | 5902 | assert( pPage->nOverflow==0 || rc!=SQLITE_OK ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5903 | } |
| 5904 | }else{ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5905 | if( pPage->nOverflow>0 || |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5906 | (!isInsert && pPage->nFree>pPage->pBt->usableSize*2/3) ){ |
| 5907 | rc = balance_nonroot(pCur); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5908 | } |
| 5909 | } |
| 5910 | return rc; |
| 5911 | } |
| 5912 | |
| 5913 | /* |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 5914 | ** This routine checks all cursors that point to table pgnoRoot. |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5915 | ** If any of those cursors were opened with wrFlag==0 in a different |
| 5916 | ** database connection (a database connection that shares the pager |
| 5917 | ** cache with the current connection) and that other connection |
| 5918 | ** is not in the ReadUncommmitted state, then this routine returns |
| 5919 | ** SQLITE_LOCKED. |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5920 | ** |
drh | 11b57d6 | 2009-02-24 19:21:41 +0000 | [diff] [blame^] | 5921 | ** As well as cursors with wrFlag==0, cursors with |
| 5922 | ** isIncrblobHandle==1 are also considered 'read' cursors because |
| 5923 | ** incremental blob cursors are used for both reading and writing. |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 5924 | ** |
| 5925 | ** When pgnoRoot is the root page of an intkey table, this function is also |
| 5926 | ** responsible for invalidating incremental blob cursors when the table row |
| 5927 | ** on which they are opened is deleted or modified. Cursors are invalidated |
| 5928 | ** according to the following rules: |
| 5929 | ** |
| 5930 | ** 1) When BtreeClearTable() is called to completely delete the contents |
| 5931 | ** of a B-Tree table, pExclude is set to zero and parameter iRow is |
| 5932 | ** set to non-zero. In this case all incremental blob cursors open |
| 5933 | ** on the table rooted at pgnoRoot are invalidated. |
| 5934 | ** |
| 5935 | ** 2) When BtreeInsert(), BtreeDelete() or BtreePutData() is called to |
| 5936 | ** modify a table row via an SQL statement, pExclude is set to the |
| 5937 | ** write cursor used to do the modification and parameter iRow is set |
| 5938 | ** to the integer row id of the B-Tree entry being modified. Unless |
| 5939 | ** pExclude is itself an incremental blob cursor, then all incremental |
| 5940 | ** blob cursors open on row iRow of the B-Tree are invalidated. |
| 5941 | ** |
| 5942 | ** 3) If both pExclude and iRow are set to zero, no incremental blob |
| 5943 | ** cursors are invalidated. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5944 | */ |
drh | 11b57d6 | 2009-02-24 19:21:41 +0000 | [diff] [blame^] | 5945 | static int checkForReadConflicts( |
| 5946 | Btree *pBtree, /* The database file to check */ |
| 5947 | Pgno pgnoRoot, /* Look for read cursors on this btree */ |
| 5948 | BtCursor *pExclude, /* Ignore this cursor */ |
| 5949 | i64 iRow /* The rowid that might be changing */ |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 5950 | ){ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5951 | BtCursor *p; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5952 | BtShared *pBt = pBtree->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 5953 | sqlite3 *db = pBtree->db; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5954 | assert( sqlite3BtreeHoldsMutex(pBtree) ); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5955 | for(p=pBt->pCursor; p; p=p->pNext){ |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5956 | if( p==pExclude ) continue; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5957 | if( p->pgnoRoot!=pgnoRoot ) continue; |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 5958 | #ifndef SQLITE_OMIT_INCRBLOB |
| 5959 | if( p->isIncrblobHandle && ( |
| 5960 | (!pExclude && iRow) |
| 5961 | || (pExclude && !pExclude->isIncrblobHandle && p->info.nKey==iRow) |
| 5962 | )){ |
| 5963 | p->eState = CURSOR_INVALID; |
| 5964 | } |
| 5965 | #endif |
| 5966 | if( p->eState!=CURSOR_VALID ) continue; |
| 5967 | if( p->wrFlag==0 |
| 5968 | #ifndef SQLITE_OMIT_INCRBLOB |
| 5969 | || p->isIncrblobHandle |
| 5970 | #endif |
| 5971 | ){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 5972 | sqlite3 *dbOther = p->pBtree->db; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5973 | if( dbOther==0 || |
| 5974 | (dbOther!=db && (dbOther->flags & SQLITE_ReadUncommitted)==0) ){ |
| 5975 | return SQLITE_LOCKED; |
| 5976 | } |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5977 | } |
| 5978 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5979 | return SQLITE_OK; |
| 5980 | } |
| 5981 | |
| 5982 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5983 | ** Insert a new record into the BTree. The key is given by (pKey,nKey) |
| 5984 | ** 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] | 5985 | ** define what table the record should be inserted into. The cursor |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5986 | ** is left pointing at a random location. |
| 5987 | ** |
| 5988 | ** For an INTKEY table, only the nKey value of the key is used. pKey is |
| 5989 | ** ignored. For a ZERODATA table, the pData and nData are both ignored. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5990 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5991 | int sqlite3BtreeInsert( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 5992 | BtCursor *pCur, /* Insert data into the table of this cursor */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 5993 | const void *pKey, i64 nKey, /* The key of the new record */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 5994 | const void *pData, int nData, /* The data of the new record */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5995 | int nZero, /* Number of extra 0 bytes to append to data */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 5996 | int appendBias /* True if this is likely an append */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5997 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5998 | int rc; |
| 5999 | int loc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6000 | int szNew; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6001 | int idx; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 6002 | MemPage *pPage; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6003 | Btree *p = pCur->pBtree; |
| 6004 | BtShared *pBt = p->pBt; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6005 | unsigned char *oldCell; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6006 | unsigned char *newCell = 0; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 6007 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6008 | assert( cursorHoldsMutex(pCur) ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 6009 | assert( pBt->inTransaction==TRANS_WRITE ); |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 6010 | assert( !pBt->readOnly ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 6011 | assert( pCur->wrFlag ); |
drh | 11b57d6 | 2009-02-24 19:21:41 +0000 | [diff] [blame^] | 6012 | if( checkForReadConflicts(pCur->pBtree, pCur->pgnoRoot, pCur, nKey) ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 6013 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 6014 | } |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 6015 | if( pCur->eState==CURSOR_FAULT ){ |
| 6016 | return pCur->skip; |
| 6017 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6018 | |
| 6019 | /* Save the positions of any other cursors open on this table */ |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 6020 | sqlite3BtreeClearCursor(pCur); |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 6021 | if( |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 6022 | SQLITE_OK!=(rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur)) || |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 6023 | SQLITE_OK!=(rc = sqlite3BtreeMoveto(pCur, pKey, nKey, appendBias, &loc)) |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 6024 | ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6025 | return rc; |
| 6026 | } |
| 6027 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6028 | pPage = pCur->apPage[pCur->iPage]; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 6029 | assert( pPage->intKey || nKey>=0 ); |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 6030 | assert( pPage->leaf || !pPage->intKey ); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6031 | TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n", |
| 6032 | pCur->pgnoRoot, nKey, nData, pPage->pgno, |
| 6033 | loc==0 ? "overwrite" : "new entry")); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6034 | assert( pPage->isInit ); |
danielk1977 | 52ae724 | 2008-03-25 14:24:56 +0000 | [diff] [blame] | 6035 | allocateTempSpace(pBt); |
| 6036 | newCell = pBt->pTmpSpace; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6037 | if( newCell==0 ) return SQLITE_NOMEM; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 6038 | rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6039 | if( rc ) goto end_insert; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6040 | assert( szNew==cellSizePtr(pPage, newCell) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6041 | assert( szNew<=MX_CELL_SIZE(pBt) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6042 | idx = pCur->aiIdx[pCur->iPage]; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6043 | if( loc==0 && CURSOR_VALID==pCur->eState ){ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 6044 | u16 szOld; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6045 | assert( idx<pPage->nCell ); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 6046 | rc = sqlite3PagerWrite(pPage->pDbPage); |
| 6047 | if( rc ){ |
| 6048 | goto end_insert; |
| 6049 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6050 | oldCell = findCell(pPage, idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6051 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6052 | memcpy(newCell, oldCell, 4); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6053 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6054 | szOld = cellSizePtr(pPage, oldCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6055 | rc = clearCell(pPage, oldCell); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6056 | if( rc ) goto end_insert; |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 6057 | rc = dropCell(pPage, idx, szOld); |
| 6058 | if( rc!=SQLITE_OK ) { |
| 6059 | goto end_insert; |
| 6060 | } |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 6061 | }else if( loc<0 && pPage->nCell>0 ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6062 | assert( pPage->leaf ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6063 | idx = ++pCur->aiIdx[pCur->iPage]; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 6064 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 6065 | pCur->validNKey = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6066 | }else{ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6067 | assert( pPage->leaf ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 6068 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6069 | rc = insertCell(pPage, idx, newCell, szNew, 0, 0); |
drh | 9bf9e9c | 2008-12-05 20:01:43 +0000 | [diff] [blame] | 6070 | if( rc==SQLITE_OK ){ |
| 6071 | rc = balance(pCur, 1); |
| 6072 | } |
| 6073 | |
| 6074 | /* Must make sure nOverflow is reset to zero even if the balance() |
| 6075 | ** fails. Internal data structure corruption will result otherwise. */ |
danielk1977 | a4124bd | 2008-12-23 10:37:47 +0000 | [diff] [blame] | 6076 | pCur->apPage[pCur->iPage]->nOverflow = 0; |
drh | 9bf9e9c | 2008-12-05 20:01:43 +0000 | [diff] [blame] | 6077 | |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 6078 | if( rc==SQLITE_OK ){ |
| 6079 | moveToRoot(pCur); |
| 6080 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6081 | end_insert: |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 6082 | return rc; |
| 6083 | } |
| 6084 | |
| 6085 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6086 | ** Delete the entry that the cursor is pointing to. The cursor |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 6087 | ** is left pointing at a arbitrary location. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 6088 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 6089 | int sqlite3BtreeDelete(BtCursor *pCur){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6090 | MemPage *pPage = pCur->apPage[pCur->iPage]; |
| 6091 | int idx; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6092 | unsigned char *pCell; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 6093 | int rc; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 6094 | Pgno pgnoChild = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6095 | Btree *p = pCur->pBtree; |
| 6096 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6097 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6098 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6099 | assert( pPage->isInit ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 6100 | assert( pBt->inTransaction==TRANS_WRITE ); |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 6101 | assert( !pBt->readOnly ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 6102 | if( pCur->eState==CURSOR_FAULT ){ |
| 6103 | return pCur->skip; |
| 6104 | } |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 6105 | if( NEVER(pCur->aiIdx[pCur->iPage]>=pPage->nCell) ){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 6106 | return SQLITE_ERROR; /* The cursor is not pointing to anything */ |
| 6107 | } |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 6108 | assert( pCur->wrFlag ); |
drh | 11b57d6 | 2009-02-24 19:21:41 +0000 | [diff] [blame^] | 6109 | if( checkForReadConflicts(pCur->pBtree, pCur->pgnoRoot, |
| 6110 | pCur, pCur->info.nKey) |
| 6111 | ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 6112 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 6113 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6114 | |
| 6115 | /* Restore the current cursor position (a no-op if the cursor is not in |
| 6116 | ** CURSOR_REQUIRESEEK state) and save the positions of any other cursors |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6117 | ** open on the same table. Then call sqlite3PagerWrite() on the page |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6118 | ** that the entry will be deleted from. |
| 6119 | */ |
| 6120 | if( |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 6121 | (rc = restoreCursorPosition(pCur))!=0 || |
drh | d116739 | 2006-01-23 13:00:35 +0000 | [diff] [blame] | 6122 | (rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur))!=0 || |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6123 | (rc = sqlite3PagerWrite(pPage->pDbPage))!=0 |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6124 | ){ |
| 6125 | return rc; |
| 6126 | } |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 6127 | |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 6128 | /* Locate the cell within its page and leave pCell pointing to the |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 6129 | ** data. The clearCell() call frees any overflow pages associated with the |
| 6130 | ** cell. The cell itself is still intact. |
| 6131 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6132 | idx = pCur->aiIdx[pCur->iPage]; |
| 6133 | pCell = findCell(pPage, idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6134 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6135 | pgnoChild = get4byte(pCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6136 | } |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 6137 | rc = clearCell(pPage, pCell); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6138 | if( rc ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6139 | return rc; |
| 6140 | } |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 6141 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6142 | if( !pPage->leaf ){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6143 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6144 | ** 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] | 6145 | ** do something we will leave a hole on an internal page. |
| 6146 | ** We have to fill the hole by moving in a cell from a leaf. The |
| 6147 | ** next Cell after the one to be deleted is guaranteed to exist and |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 6148 | ** to be a leaf so we can use it. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 6149 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6150 | BtCursor leafCur; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 6151 | MemPage *pLeafPage = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6152 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6153 | unsigned char *pNext; |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 6154 | int notUsed; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6155 | unsigned char *tempCell = 0; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 6156 | assert( !pPage->intKey ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6157 | sqlite3BtreeGetTempCursor(pCur, &leafCur); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 6158 | rc = sqlite3BtreeNext(&leafCur, ¬Used); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6159 | if( rc==SQLITE_OK ){ |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6160 | assert( leafCur.aiIdx[leafCur.iPage]==0 ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6161 | pLeafPage = leafCur.apPage[leafCur.iPage]; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6162 | rc = sqlite3PagerWrite(pLeafPage->pDbPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6163 | } |
| 6164 | if( rc==SQLITE_OK ){ |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6165 | int leafCursorInvalid = 0; |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 6166 | u16 szNext; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6167 | TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n", |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6168 | pCur->pgnoRoot, pPage->pgno, pLeafPage->pgno)); |
| 6169 | dropCell(pPage, idx, cellSizePtr(pPage, pCell)); |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6170 | pNext = findCell(pLeafPage, 0); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6171 | szNext = cellSizePtr(pLeafPage, pNext); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6172 | assert( MX_CELL_SIZE(pBt)>=szNext+4 ); |
danielk1977 | 52ae724 | 2008-03-25 14:24:56 +0000 | [diff] [blame] | 6173 | allocateTempSpace(pBt); |
| 6174 | tempCell = pBt->pTmpSpace; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6175 | if( tempCell==0 ){ |
| 6176 | rc = SQLITE_NOMEM; |
| 6177 | } |
danielk1977 | 8ea1cfa | 2008-01-01 06:19:02 +0000 | [diff] [blame] | 6178 | if( rc==SQLITE_OK ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6179 | rc = insertCell(pPage, idx, pNext-4, szNext+4, tempCell, 0); |
danielk1977 | 8ea1cfa | 2008-01-01 06:19:02 +0000 | [diff] [blame] | 6180 | } |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6181 | |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 6182 | |
| 6183 | /* The "if" statement in the next code block is critical. The |
| 6184 | ** slightest error in that statement would allow SQLite to operate |
| 6185 | ** correctly most of the time but produce very rare failures. To |
| 6186 | ** guard against this, the following macros help to verify that |
| 6187 | ** the "if" statement is well tested. |
| 6188 | */ |
| 6189 | testcase( pPage->nOverflow==0 && pPage->nFree<pBt->usableSize*2/3 |
| 6190 | && pLeafPage->nFree+2+szNext > pBt->usableSize*2/3 ); |
| 6191 | testcase( pPage->nOverflow==0 && pPage->nFree==pBt->usableSize*2/3 |
| 6192 | && pLeafPage->nFree+2+szNext > pBt->usableSize*2/3 ); |
| 6193 | testcase( pPage->nOverflow==0 && pPage->nFree==pBt->usableSize*2/3+1 |
| 6194 | && pLeafPage->nFree+2+szNext > pBt->usableSize*2/3 ); |
| 6195 | testcase( pPage->nOverflow>0 && pPage->nFree<=pBt->usableSize*2/3 |
| 6196 | && pLeafPage->nFree+2+szNext > pBt->usableSize*2/3 ); |
| 6197 | testcase( (pPage->nOverflow>0 || (pPage->nFree > pBt->usableSize*2/3)) |
| 6198 | && pLeafPage->nFree+2+szNext == pBt->usableSize*2/3 ); |
| 6199 | |
| 6200 | |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6201 | if( (pPage->nOverflow>0 || (pPage->nFree > pBt->usableSize*2/3)) && |
| 6202 | (pLeafPage->nFree+2+szNext > pBt->usableSize*2/3) |
| 6203 | ){ |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 6204 | /* This branch is taken if the internal node is now either overflowing |
| 6205 | ** or underfull and the leaf node will be underfull after the just cell |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6206 | ** copied to the internal node is deleted from it. This is a special |
| 6207 | ** case because the call to balance() to correct the internal node |
| 6208 | ** may change the tree structure and invalidate the contents of |
| 6209 | ** the leafCur.apPage[] and leafCur.aiIdx[] arrays, which will be |
| 6210 | ** used by the balance() required to correct the underfull leaf |
| 6211 | ** node. |
| 6212 | ** |
| 6213 | ** The formula used in the expression above are based on facets of |
| 6214 | ** the SQLite file-format that do not change over time. |
| 6215 | */ |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 6216 | testcase( pPage->nFree==pBt->usableSize*2/3+1 ); |
| 6217 | testcase( pLeafPage->nFree+2+szNext==pBt->usableSize*2/3+1 ); |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6218 | leafCursorInvalid = 1; |
| 6219 | } |
| 6220 | |
danielk1977 | 8ea1cfa | 2008-01-01 06:19:02 +0000 | [diff] [blame] | 6221 | if( rc==SQLITE_OK ){ |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 6222 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6223 | put4byte(findOverflowCell(pPage, idx), pgnoChild); |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 6224 | VVA_ONLY( pCur->pagesShuffled = 0 ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6225 | rc = balance(pCur, 0); |
danielk1977 | 8ea1cfa | 2008-01-01 06:19:02 +0000 | [diff] [blame] | 6226 | } |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6227 | |
| 6228 | if( rc==SQLITE_OK && leafCursorInvalid ){ |
| 6229 | /* The leaf-node is now underfull and so the tree needs to be |
| 6230 | ** rebalanced. However, the balance() operation on the internal |
| 6231 | ** node above may have modified the structure of the B-Tree and |
| 6232 | ** so the current contents of leafCur.apPage[] and leafCur.aiIdx[] |
| 6233 | ** may not be trusted. |
| 6234 | ** |
| 6235 | ** It is not possible to copy the ancestry from pCur, as the same |
| 6236 | ** balance() call has invalidated the pCur->apPage[] and aiIdx[] |
| 6237 | ** arrays. |
drh | 7b68280 | 2008-09-30 14:06:28 +0000 | [diff] [blame] | 6238 | ** |
| 6239 | ** The call to saveCursorPosition() below internally saves the |
| 6240 | ** key that leafCur is currently pointing to. Currently, there |
| 6241 | ** are two copies of that key in the tree - one here on the leaf |
| 6242 | ** page and one on some internal node in the tree. The copy on |
| 6243 | ** the leaf node is always the next key in tree-order after the |
| 6244 | ** copy on the internal node. So, the call to sqlite3BtreeNext() |
| 6245 | ** calls restoreCursorPosition() to point the cursor to the copy |
| 6246 | ** stored on the internal node, then advances to the next entry, |
| 6247 | ** which happens to be the copy of the key on the internal node. |
danielk1977 | a69fda2 | 2008-09-30 16:48:10 +0000 | [diff] [blame] | 6248 | ** Net effect: leafCur is pointing back to the duplicate cell |
| 6249 | ** that needs to be removed, and the leafCur.apPage[] and |
| 6250 | ** leafCur.aiIdx[] arrays are correct. |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6251 | */ |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 6252 | VVA_ONLY( Pgno leafPgno = pLeafPage->pgno ); |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6253 | rc = saveCursorPosition(&leafCur); |
| 6254 | if( rc==SQLITE_OK ){ |
| 6255 | rc = sqlite3BtreeNext(&leafCur, ¬Used); |
| 6256 | } |
| 6257 | pLeafPage = leafCur.apPage[leafCur.iPage]; |
| 6258 | assert( pLeafPage->pgno==leafPgno ); |
| 6259 | assert( leafCur.aiIdx[leafCur.iPage]==0 ); |
| 6260 | } |
| 6261 | |
danielk1977 | 0cd1bbd | 2008-11-26 07:25:52 +0000 | [diff] [blame] | 6262 | if( SQLITE_OK==rc |
| 6263 | && SQLITE_OK==(rc = sqlite3PagerWrite(pLeafPage->pDbPage)) |
| 6264 | ){ |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6265 | dropCell(pLeafPage, 0, szNext); |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 6266 | VVA_ONLY( leafCur.pagesShuffled = 0 ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6267 | rc = balance(&leafCur, 0); |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 6268 | assert( leafCursorInvalid || !leafCur.pagesShuffled |
| 6269 | || !pCur->pagesShuffled ); |
danielk1977 | 8ea1cfa | 2008-01-01 06:19:02 +0000 | [diff] [blame] | 6270 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6271 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6272 | sqlite3BtreeReleaseTempCursor(&leafCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 6273 | }else{ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 6274 | TRACE(("DELETE: table=%d delete from leaf %d\n", |
| 6275 | pCur->pgnoRoot, pPage->pgno)); |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 6276 | rc = dropCell(pPage, idx, cellSizePtr(pPage, pCell)); |
| 6277 | if( rc==SQLITE_OK ){ |
| 6278 | rc = balance(pCur, 0); |
| 6279 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 6280 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6281 | if( rc==SQLITE_OK ){ |
| 6282 | moveToRoot(pCur); |
| 6283 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 6284 | return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 6285 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6286 | |
| 6287 | /* |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 6288 | ** Create a new BTree table. Write into *piTable the page |
| 6289 | ** number for the root page of the new table. |
| 6290 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 6291 | ** The type of type is determined by the flags parameter. Only the |
| 6292 | ** following values of flags are currently in use. Other values for |
| 6293 | ** flags might not work: |
| 6294 | ** |
| 6295 | ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys |
| 6296 | ** BTREE_ZERODATA Used for SQL indices |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6297 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6298 | static int btreeCreateTable(Btree *p, int *piTable, int flags){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6299 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6300 | MemPage *pRoot; |
| 6301 | Pgno pgnoRoot; |
| 6302 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6303 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6304 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 6305 | assert( pBt->inTransaction==TRANS_WRITE ); |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 6306 | assert( !pBt->readOnly ); |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 6307 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6308 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 6309 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6310 | if( rc ){ |
| 6311 | return rc; |
| 6312 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6313 | #else |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6314 | if( pBt->autoVacuum ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6315 | Pgno pgnoMove; /* Move a page here to make room for the root-page */ |
| 6316 | MemPage *pPageMove; /* The page to move to. */ |
| 6317 | |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 6318 | /* Creating a new table may probably require moving an existing database |
| 6319 | ** to make room for the new tables root page. In case this page turns |
| 6320 | ** out to be an overflow page, delete all overflow page-map caches |
| 6321 | ** held by open cursors. |
| 6322 | */ |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 6323 | invalidateAllOverflowCache(pBt); |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 6324 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6325 | /* Read the value of meta[3] from the database to determine where the |
| 6326 | ** root page of the new table should go. meta[3] is the largest root-page |
| 6327 | ** created so far, so the new root-page is (meta[3]+1). |
| 6328 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6329 | rc = sqlite3BtreeGetMeta(p, 4, &pgnoRoot); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6330 | if( rc!=SQLITE_OK ){ |
| 6331 | return rc; |
| 6332 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6333 | pgnoRoot++; |
| 6334 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6335 | /* The new root-page may not be allocated on a pointer-map page, or the |
| 6336 | ** PENDING_BYTE page. |
| 6337 | */ |
drh | 7219043 | 2008-01-31 14:54:43 +0000 | [diff] [blame] | 6338 | while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) || |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6339 | pgnoRoot==PENDING_BYTE_PAGE(pBt) ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6340 | pgnoRoot++; |
| 6341 | } |
| 6342 | assert( pgnoRoot>=3 ); |
| 6343 | |
| 6344 | /* Allocate a page. The page that currently resides at pgnoRoot will |
| 6345 | ** be moved to the allocated page (unless the allocated page happens |
| 6346 | ** to reside at pgnoRoot). |
| 6347 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 6348 | rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6349 | if( rc!=SQLITE_OK ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6350 | return rc; |
| 6351 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6352 | |
| 6353 | if( pgnoMove!=pgnoRoot ){ |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 6354 | /* pgnoRoot is the page that will be used for the root-page of |
| 6355 | ** the new table (assuming an error did not occur). But we were |
| 6356 | ** allocated pgnoMove. If required (i.e. if it was not allocated |
| 6357 | ** by extending the file), the current page at position pgnoMove |
| 6358 | ** is already journaled. |
| 6359 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6360 | u8 eType; |
| 6361 | Pgno iPtrPage; |
| 6362 | |
| 6363 | releasePage(pPageMove); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 6364 | |
| 6365 | /* Move the page currently at pgnoRoot to pgnoMove. */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6366 | rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6367 | if( rc!=SQLITE_OK ){ |
| 6368 | return rc; |
| 6369 | } |
| 6370 | rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage); |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 6371 | if( rc!=SQLITE_OK || eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6372 | releasePage(pRoot); |
| 6373 | return rc; |
| 6374 | } |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 6375 | assert( eType!=PTRMAP_ROOTPAGE ); |
| 6376 | assert( eType!=PTRMAP_FREEPAGE ); |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 6377 | rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6378 | releasePage(pRoot); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 6379 | |
| 6380 | /* Obtain the page at pgnoRoot */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6381 | if( rc!=SQLITE_OK ){ |
| 6382 | return rc; |
| 6383 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6384 | rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6385 | if( rc!=SQLITE_OK ){ |
| 6386 | return rc; |
| 6387 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6388 | rc = sqlite3PagerWrite(pRoot->pDbPage); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6389 | if( rc!=SQLITE_OK ){ |
| 6390 | releasePage(pRoot); |
| 6391 | return rc; |
| 6392 | } |
| 6393 | }else{ |
| 6394 | pRoot = pPageMove; |
| 6395 | } |
| 6396 | |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 6397 | /* Update the pointer-map and meta-data with the new root-page number. */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6398 | rc = ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0); |
| 6399 | if( rc ){ |
| 6400 | releasePage(pRoot); |
| 6401 | return rc; |
| 6402 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6403 | rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6404 | if( rc ){ |
| 6405 | releasePage(pRoot); |
| 6406 | return rc; |
| 6407 | } |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 6408 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6409 | }else{ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 6410 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6411 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6412 | } |
| 6413 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6414 | assert( sqlite3PagerIswriteable(pRoot->pDbPage) ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 6415 | zeroPage(pRoot, flags | PTF_LEAF); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6416 | sqlite3PagerUnref(pRoot->pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6417 | *piTable = (int)pgnoRoot; |
| 6418 | return SQLITE_OK; |
| 6419 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6420 | int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){ |
| 6421 | int rc; |
| 6422 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6423 | p->pBt->db = p->db; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6424 | rc = btreeCreateTable(p, piTable, flags); |
| 6425 | sqlite3BtreeLeave(p); |
| 6426 | return rc; |
| 6427 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6428 | |
| 6429 | /* |
| 6430 | ** Erase the given database page and all its children. Return |
| 6431 | ** the page to the freelist. |
| 6432 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6433 | static int clearDatabasePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6434 | BtShared *pBt, /* The BTree that contains the table */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6435 | Pgno pgno, /* Page number to clear */ |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6436 | int freePageFlag, /* Deallocate page if true */ |
| 6437 | int *pnChange |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6438 | ){ |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6439 | MemPage *pPage = 0; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6440 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6441 | unsigned char *pCell; |
| 6442 | int i; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6443 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6444 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 6445 | if( pgno>pagerPagecount(pBt) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 6446 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 6447 | } |
| 6448 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6449 | rc = getAndInitPage(pBt, pgno, &pPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6450 | if( rc ) goto cleardatabasepage_out; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6451 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 6452 | pCell = findCell(pPage, i); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6453 | if( !pPage->leaf ){ |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 6454 | rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6455 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6456 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6457 | rc = clearCell(pPage, pCell); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6458 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6459 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6460 | if( !pPage->leaf ){ |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 6461 | rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), 1, pnChange); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6462 | if( rc ) goto cleardatabasepage_out; |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6463 | }else if( pnChange ){ |
| 6464 | assert( pPage->intKey ); |
| 6465 | *pnChange += pPage->nCell; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6466 | } |
| 6467 | if( freePageFlag ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6468 | rc = freePage(pPage); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6469 | }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6470 | zeroPage(pPage, pPage->aData[0] | PTF_LEAF); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6471 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6472 | |
| 6473 | cleardatabasepage_out: |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6474 | releasePage(pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6475 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6476 | } |
| 6477 | |
| 6478 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 6479 | ** Delete all information from a single table in the database. iTable is |
| 6480 | ** the page number of the root of the table. After this routine returns, |
| 6481 | ** the root page is empty, but still exists. |
| 6482 | ** |
| 6483 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 6484 | ** read cursors on the table. Open write cursors are moved to the |
| 6485 | ** root of the table. |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6486 | ** |
| 6487 | ** If pnChange is not NULL, then table iTable must be an intkey table. The |
| 6488 | ** integer value pointed to by pnChange is incremented by the number of |
| 6489 | ** entries in the table. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6490 | */ |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6491 | int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6492 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6493 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6494 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6495 | pBt->db = p->db; |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 6496 | assert( p->inTrans==TRANS_WRITE ); |
drh | 11b57d6 | 2009-02-24 19:21:41 +0000 | [diff] [blame^] | 6497 | if( (rc = checkForReadConflicts(p, iTable, 0, 1))!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6498 | /* nothing to do */ |
| 6499 | }else if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){ |
| 6500 | /* nothing to do */ |
| 6501 | }else{ |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 6502 | rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6503 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6504 | sqlite3BtreeLeave(p); |
| 6505 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6506 | } |
| 6507 | |
| 6508 | /* |
| 6509 | ** Erase all information in a table and add the root of the table to |
| 6510 | ** the freelist. Except, the root of the principle table (the one on |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 6511 | ** page 1) is never added to the freelist. |
| 6512 | ** |
| 6513 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 6514 | ** cursors on the table. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6515 | ** |
| 6516 | ** If AUTOVACUUM is enabled and the page at iTable is not the last |
| 6517 | ** root page in the database file, then the last root page |
| 6518 | ** in the database file is moved into the slot formerly occupied by |
| 6519 | ** iTable and that last slot formerly occupied by the last root page |
| 6520 | ** is added to the freelist instead of iTable. In this say, all |
| 6521 | ** root pages are kept at the beginning of the database file, which |
| 6522 | ** is necessary for AUTOVACUUM to work right. *piMoved is set to the |
| 6523 | ** page number that used to be the last root page in the file before |
| 6524 | ** the move. If no page gets moved, *piMoved is set to 0. |
| 6525 | ** The last root page is recorded in meta[3] and the value of |
| 6526 | ** meta[3] is updated by this procedure. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6527 | */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 6528 | static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6529 | int rc; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6530 | MemPage *pPage = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6531 | BtShared *pBt = p->pBt; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6532 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6533 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 6534 | assert( p->inTrans==TRANS_WRITE ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6535 | |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 6536 | /* It is illegal to drop a table if any cursors are open on the |
| 6537 | ** database. This is because in auto-vacuum mode the backend may |
| 6538 | ** need to move another root-page to fill a gap left by the deleted |
| 6539 | ** root page. If an open cursor was using this page a problem would |
| 6540 | ** occur. |
| 6541 | */ |
| 6542 | if( pBt->pCursor ){ |
| 6543 | return SQLITE_LOCKED; |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 6544 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6545 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6546 | rc = sqlite3BtreeGetPage(pBt, (Pgno)iTable, &pPage, 0); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6547 | if( rc ) return rc; |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6548 | rc = sqlite3BtreeClearTable(p, iTable, 0); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6549 | if( rc ){ |
| 6550 | releasePage(pPage); |
| 6551 | return rc; |
| 6552 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6553 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6554 | *piMoved = 0; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6555 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6556 | if( iTable>1 ){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6557 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6558 | rc = freePage(pPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6559 | releasePage(pPage); |
| 6560 | #else |
| 6561 | if( pBt->autoVacuum ){ |
| 6562 | Pgno maxRootPgno; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6563 | rc = sqlite3BtreeGetMeta(p, 4, &maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6564 | if( rc!=SQLITE_OK ){ |
| 6565 | releasePage(pPage); |
| 6566 | return rc; |
| 6567 | } |
| 6568 | |
| 6569 | if( iTable==maxRootPgno ){ |
| 6570 | /* If the table being dropped is the table with the largest root-page |
| 6571 | ** number in the database, put the root page on the free list. |
| 6572 | */ |
| 6573 | rc = freePage(pPage); |
| 6574 | releasePage(pPage); |
| 6575 | if( rc!=SQLITE_OK ){ |
| 6576 | return rc; |
| 6577 | } |
| 6578 | }else{ |
| 6579 | /* The table being dropped does not have the largest root-page |
| 6580 | ** number in the database. So move the page that does into the |
| 6581 | ** gap left by the deleted root-page. |
| 6582 | */ |
| 6583 | MemPage *pMove; |
| 6584 | releasePage(pPage); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6585 | rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6586 | if( rc!=SQLITE_OK ){ |
| 6587 | return rc; |
| 6588 | } |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 6589 | rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6590 | releasePage(pMove); |
| 6591 | if( rc!=SQLITE_OK ){ |
| 6592 | return rc; |
| 6593 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6594 | rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6595 | if( rc!=SQLITE_OK ){ |
| 6596 | return rc; |
| 6597 | } |
| 6598 | rc = freePage(pMove); |
| 6599 | releasePage(pMove); |
| 6600 | if( rc!=SQLITE_OK ){ |
| 6601 | return rc; |
| 6602 | } |
| 6603 | *piMoved = maxRootPgno; |
| 6604 | } |
| 6605 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6606 | /* Set the new 'max-root-page' value in the database header. This |
| 6607 | ** is the old value less one, less one more if that happens to |
| 6608 | ** be a root-page number, less one again if that is the |
| 6609 | ** PENDING_BYTE_PAGE. |
| 6610 | */ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 6611 | maxRootPgno--; |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6612 | if( maxRootPgno==PENDING_BYTE_PAGE(pBt) ){ |
| 6613 | maxRootPgno--; |
| 6614 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6615 | if( maxRootPgno==PTRMAP_PAGENO(pBt, maxRootPgno) ){ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 6616 | maxRootPgno--; |
| 6617 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6618 | assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) ); |
| 6619 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6620 | rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6621 | }else{ |
| 6622 | rc = freePage(pPage); |
| 6623 | releasePage(pPage); |
| 6624 | } |
| 6625 | #endif |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6626 | }else{ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6627 | /* If sqlite3BtreeDropTable was called on page 1. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6628 | zeroPage(pPage, PTF_INTKEY|PTF_LEAF ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6629 | releasePage(pPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6630 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6631 | return rc; |
| 6632 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6633 | int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){ |
| 6634 | int rc; |
| 6635 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6636 | p->pBt->db = p->db; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6637 | rc = btreeDropTable(p, iTable, piMoved); |
| 6638 | sqlite3BtreeLeave(p); |
| 6639 | return rc; |
| 6640 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6641 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 6642 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6643 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6644 | ** Read the meta-information out of a database file. Meta[0] |
| 6645 | ** is the number of free pages currently in the database. Meta[1] |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 6646 | ** through meta[15] are available for use by higher layers. Meta[0] |
| 6647 | ** is read-only, the others are read/write. |
| 6648 | ** |
| 6649 | ** The schema layer numbers meta values differently. At the schema |
| 6650 | ** layer (and the SetCookie and ReadCookie opcodes) the number of |
| 6651 | ** 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] | 6652 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6653 | int sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){ |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 6654 | DbPage *pDbPage = 0; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6655 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6656 | unsigned char *pP1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6657 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6658 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6659 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6660 | pBt->db = p->db; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6661 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6662 | /* Reading a meta-data value requires a read-lock on page 1 (and hence |
| 6663 | ** the sqlite_master table. We grab this lock regardless of whether or |
| 6664 | ** not the SQLITE_ReadUncommitted flag is set (the table rooted at page |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 6665 | ** 1 is treated as a special case by querySharedCacheTableLock() |
| 6666 | ** and setSharedCacheTableLock()). |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6667 | */ |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 6668 | rc = querySharedCacheTableLock(p, 1, READ_LOCK); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6669 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6670 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6671 | return rc; |
| 6672 | } |
| 6673 | |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6674 | assert( idx>=0 && idx<=15 ); |
danielk1977 | d9f6c53 | 2008-09-19 16:39:38 +0000 | [diff] [blame] | 6675 | if( pBt->pPage1 ){ |
| 6676 | /* The b-tree is already holding a reference to page 1 of the database |
| 6677 | ** file. In this case the required meta-data value can be read directly |
| 6678 | ** from the page data of this reference. This is slightly faster than |
| 6679 | ** requesting a new reference from the pager layer. |
| 6680 | */ |
| 6681 | pP1 = (unsigned char *)pBt->pPage1->aData; |
| 6682 | }else{ |
| 6683 | /* The b-tree does not have a reference to page 1 of the database file. |
| 6684 | ** Obtain one from the pager layer. |
| 6685 | */ |
danielk1977 | ea89730 | 2008-09-19 15:10:58 +0000 | [diff] [blame] | 6686 | rc = sqlite3PagerGet(pBt->pPager, 1, &pDbPage); |
| 6687 | if( rc ){ |
| 6688 | sqlite3BtreeLeave(p); |
| 6689 | return rc; |
| 6690 | } |
| 6691 | pP1 = (unsigned char *)sqlite3PagerGetData(pDbPage); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6692 | } |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6693 | *pMeta = get4byte(&pP1[36 + idx*4]); |
danielk1977 | ea89730 | 2008-09-19 15:10:58 +0000 | [diff] [blame] | 6694 | |
danielk1977 | d9f6c53 | 2008-09-19 16:39:38 +0000 | [diff] [blame] | 6695 | /* If the b-tree is not holding a reference to page 1, then one was |
| 6696 | ** requested from the pager layer in the above block. Release it now. |
| 6697 | */ |
danielk1977 | ea89730 | 2008-09-19 15:10:58 +0000 | [diff] [blame] | 6698 | if( !pBt->pPage1 ){ |
| 6699 | sqlite3PagerUnref(pDbPage); |
| 6700 | } |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 6701 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6702 | /* If autovacuumed is disabled in this build but we are trying to |
| 6703 | ** access an autovacuumed database, then make the database readonly. |
| 6704 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6705 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 6706 | if( idx==4 && *pMeta>0 ) pBt->readOnly = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6707 | #endif |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 6708 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6709 | /* Grab the read-lock on page 1. */ |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 6710 | rc = setSharedCacheTableLock(p, 1, READ_LOCK); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6711 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6712 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6713 | } |
| 6714 | |
| 6715 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6716 | ** Write meta-information back into the database. Meta[0] is |
| 6717 | ** read-only and may not be written. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6718 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6719 | int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){ |
| 6720 | BtShared *pBt = p->pBt; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6721 | unsigned char *pP1; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6722 | int rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6723 | assert( idx>=1 && idx<=15 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6724 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6725 | pBt->db = p->db; |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 6726 | assert( p->inTrans==TRANS_WRITE ); |
| 6727 | assert( pBt->pPage1!=0 ); |
| 6728 | pP1 = pBt->pPage1->aData; |
| 6729 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 6730 | if( rc==SQLITE_OK ){ |
| 6731 | put4byte(&pP1[36 + idx*4], iMeta); |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 +0000 | [diff] [blame] | 6732 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 6733 | if( idx==7 ){ |
| 6734 | assert( pBt->autoVacuum || iMeta==0 ); |
| 6735 | assert( iMeta==0 || iMeta==1 ); |
| 6736 | pBt->incrVacuum = (u8)iMeta; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6737 | } |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 6738 | #endif |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 6739 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6740 | sqlite3BtreeLeave(p); |
| 6741 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6742 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 6743 | |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 6744 | /* |
| 6745 | ** Return the flag byte at the beginning of the page that the cursor |
| 6746 | ** is currently pointing to. |
| 6747 | */ |
| 6748 | int sqlite3BtreeFlags(BtCursor *pCur){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6749 | /* TODO: What about CURSOR_REQUIRESEEK state? Probably need to call |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 6750 | ** restoreCursorPosition() here. |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6751 | */ |
danielk1977 | e448dc4 | 2008-01-02 11:50:51 +0000 | [diff] [blame] | 6752 | MemPage *pPage; |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 6753 | restoreCursorPosition(pCur); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6754 | pPage = pCur->apPage[pCur->iPage]; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6755 | assert( cursorHoldsMutex(pCur) ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 6756 | assert( pPage!=0 ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 6757 | assert( pPage->pBt==pCur->pBt ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 6758 | return pPage->aData[pPage->hdrOffset]; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 6759 | } |
| 6760 | |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 6761 | #ifndef SQLITE_OMIT_BTREECOUNT |
| 6762 | /* |
| 6763 | ** The first argument, pCur, is a cursor opened on some b-tree. Count the |
| 6764 | ** number of entries in the b-tree and write the result to *pnEntry. |
| 6765 | ** |
| 6766 | ** SQLITE_OK is returned if the operation is successfully executed. |
| 6767 | ** Otherwise, if an error is encountered (i.e. an IO error or database |
| 6768 | ** corruption) an SQLite error code is returned. |
| 6769 | */ |
| 6770 | int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){ |
| 6771 | i64 nEntry = 0; /* Value to return in *pnEntry */ |
| 6772 | int rc; /* Return code */ |
| 6773 | rc = moveToRoot(pCur); |
| 6774 | |
| 6775 | /* Unless an error occurs, the following loop runs one iteration for each |
| 6776 | ** page in the B-Tree structure (not including overflow pages). |
| 6777 | */ |
| 6778 | while( rc==SQLITE_OK ){ |
| 6779 | int iIdx; /* Index of child node in parent */ |
| 6780 | MemPage *pPage; /* Current page of the b-tree */ |
| 6781 | |
| 6782 | /* If this is a leaf page or the tree is not an int-key tree, then |
| 6783 | ** this page contains countable entries. Increment the entry counter |
| 6784 | ** accordingly. |
| 6785 | */ |
| 6786 | pPage = pCur->apPage[pCur->iPage]; |
| 6787 | if( pPage->leaf || !pPage->intKey ){ |
| 6788 | nEntry += pPage->nCell; |
| 6789 | } |
| 6790 | |
| 6791 | /* pPage is a leaf node. This loop navigates the cursor so that it |
| 6792 | ** points to the first interior cell that it points to the parent of |
| 6793 | ** the next page in the tree that has not yet been visited. The |
| 6794 | ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell |
| 6795 | ** of the page, or to the number of cells in the page if the next page |
| 6796 | ** to visit is the right-child of its parent. |
| 6797 | ** |
| 6798 | ** If all pages in the tree have been visited, return SQLITE_OK to the |
| 6799 | ** caller. |
| 6800 | */ |
| 6801 | if( pPage->leaf ){ |
| 6802 | do { |
| 6803 | if( pCur->iPage==0 ){ |
| 6804 | /* All pages of the b-tree have been visited. Return successfully. */ |
| 6805 | *pnEntry = nEntry; |
| 6806 | return SQLITE_OK; |
| 6807 | } |
| 6808 | sqlite3BtreeMoveToParent(pCur); |
| 6809 | }while ( pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell ); |
| 6810 | |
| 6811 | pCur->aiIdx[pCur->iPage]++; |
| 6812 | pPage = pCur->apPage[pCur->iPage]; |
| 6813 | } |
| 6814 | |
| 6815 | /* Descend to the child node of the cell that the cursor currently |
| 6816 | ** points at. This is the right-child if (iIdx==pPage->nCell). |
| 6817 | */ |
| 6818 | iIdx = pCur->aiIdx[pCur->iPage]; |
| 6819 | if( iIdx==pPage->nCell ){ |
| 6820 | rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); |
| 6821 | }else{ |
| 6822 | rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx))); |
| 6823 | } |
| 6824 | } |
| 6825 | |
| 6826 | /* An error has occured. Return an error code. */ |
| 6827 | return rc; |
| 6828 | } |
| 6829 | #endif |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6830 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6831 | /* |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6832 | ** Return the pager associated with a BTree. This routine is used for |
| 6833 | ** testing and debugging only. |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6834 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6835 | Pager *sqlite3BtreePager(Btree *p){ |
| 6836 | return p->pBt->pPager; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6837 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6838 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6839 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6840 | /* |
| 6841 | ** Append a message to the error message string. |
| 6842 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6843 | static void checkAppendMsg( |
| 6844 | IntegrityCk *pCheck, |
| 6845 | char *zMsg1, |
| 6846 | const char *zFormat, |
| 6847 | ... |
| 6848 | ){ |
| 6849 | va_list ap; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6850 | if( !pCheck->mxErr ) return; |
| 6851 | pCheck->mxErr--; |
| 6852 | pCheck->nErr++; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6853 | va_start(ap, zFormat); |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 6854 | if( pCheck->errMsg.nChar ){ |
| 6855 | sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6856 | } |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 6857 | if( zMsg1 ){ |
| 6858 | sqlite3StrAccumAppend(&pCheck->errMsg, zMsg1, -1); |
| 6859 | } |
| 6860 | sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap); |
| 6861 | va_end(ap); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 6862 | if( pCheck->errMsg.mallocFailed ){ |
| 6863 | pCheck->mallocFailed = 1; |
| 6864 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6865 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6866 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6867 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6868 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6869 | /* |
| 6870 | ** Add 1 to the reference count for page iPage. If this is the second |
| 6871 | ** reference to the page, add an error message to pCheck->zErrMsg. |
| 6872 | ** Return 1 if there are 2 ore more references to the page and 0 if |
| 6873 | ** if this is the first reference to the page. |
| 6874 | ** |
| 6875 | ** Also check that the page number is in bounds. |
| 6876 | */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 6877 | static int checkRef(IntegrityCk *pCheck, Pgno iPage, char *zContext){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6878 | if( iPage==0 ) return 1; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 6879 | if( iPage>pCheck->nPage ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6880 | checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6881 | return 1; |
| 6882 | } |
| 6883 | if( pCheck->anRef[iPage]==1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6884 | checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6885 | return 1; |
| 6886 | } |
| 6887 | return (pCheck->anRef[iPage]++)>1; |
| 6888 | } |
| 6889 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6890 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6891 | /* |
| 6892 | ** Check that the entry in the pointer-map for page iChild maps to |
| 6893 | ** page iParent, pointer type ptrType. If not, append an error message |
| 6894 | ** to pCheck. |
| 6895 | */ |
| 6896 | static void checkPtrmap( |
| 6897 | IntegrityCk *pCheck, /* Integrity check context */ |
| 6898 | Pgno iChild, /* Child page number */ |
| 6899 | u8 eType, /* Expected pointer map type */ |
| 6900 | Pgno iParent, /* Expected pointer map parent page number */ |
| 6901 | char *zContext /* Context description (used for error msg) */ |
| 6902 | ){ |
| 6903 | int rc; |
| 6904 | u8 ePtrmapType; |
| 6905 | Pgno iPtrmapParent; |
| 6906 | |
| 6907 | rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent); |
| 6908 | if( rc!=SQLITE_OK ){ |
drh | e43ba70 | 2008-12-05 22:40:08 +0000 | [diff] [blame] | 6909 | if( rc==SQLITE_NOMEM ) pCheck->mallocFailed = 1; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6910 | checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild); |
| 6911 | return; |
| 6912 | } |
| 6913 | |
| 6914 | if( ePtrmapType!=eType || iPtrmapParent!=iParent ){ |
| 6915 | checkAppendMsg(pCheck, zContext, |
| 6916 | "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", |
| 6917 | iChild, eType, iParent, ePtrmapType, iPtrmapParent); |
| 6918 | } |
| 6919 | } |
| 6920 | #endif |
| 6921 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6922 | /* |
| 6923 | ** Check the integrity of the freelist or of an overflow page list. |
| 6924 | ** Verify that the number of pages on the list is N. |
| 6925 | */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6926 | static void checkList( |
| 6927 | IntegrityCk *pCheck, /* Integrity checking context */ |
| 6928 | int isFreeList, /* True for a freelist. False for overflow page list */ |
| 6929 | int iPage, /* Page number for first page in the list */ |
| 6930 | int N, /* Expected number of pages in the list */ |
| 6931 | char *zContext /* Context for error messages */ |
| 6932 | ){ |
| 6933 | int i; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6934 | int expected = N; |
| 6935 | int iFirst = iPage; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6936 | while( N-- > 0 && pCheck->mxErr ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6937 | DbPage *pOvflPage; |
| 6938 | unsigned char *pOvflData; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6939 | if( iPage<1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6940 | checkAppendMsg(pCheck, zContext, |
| 6941 | "%d of %d pages missing from overflow list starting at %d", |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6942 | N+1, expected, iFirst); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6943 | break; |
| 6944 | } |
| 6945 | if( checkRef(pCheck, iPage, zContext) ) break; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6946 | if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6947 | checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6948 | break; |
| 6949 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6950 | pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6951 | if( isFreeList ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6952 | int n = get4byte(&pOvflData[4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6953 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6954 | if( pCheck->pBt->autoVacuum ){ |
| 6955 | checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext); |
| 6956 | } |
| 6957 | #endif |
drh | 45b1fac | 2008-07-04 17:52:42 +0000 | [diff] [blame] | 6958 | if( n>pCheck->pBt->usableSize/4-2 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6959 | checkAppendMsg(pCheck, zContext, |
| 6960 | "freelist leaf count too big on page %d", iPage); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 6961 | N--; |
| 6962 | }else{ |
| 6963 | for(i=0; i<n; i++){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6964 | Pgno iFreePage = get4byte(&pOvflData[8+i*4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6965 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6966 | if( pCheck->pBt->autoVacuum ){ |
| 6967 | checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext); |
| 6968 | } |
| 6969 | #endif |
| 6970 | checkRef(pCheck, iFreePage, zContext); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 6971 | } |
| 6972 | N -= n; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6973 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6974 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6975 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6976 | else{ |
| 6977 | /* If this database supports auto-vacuum and iPage is not the last |
| 6978 | ** page in this overflow list, check that the pointer-map entry for |
| 6979 | ** the following page matches iPage. |
| 6980 | */ |
| 6981 | if( pCheck->pBt->autoVacuum && N>0 ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6982 | i = get4byte(pOvflData); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6983 | checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext); |
| 6984 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6985 | } |
| 6986 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6987 | iPage = get4byte(pOvflData); |
| 6988 | sqlite3PagerUnref(pOvflPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6989 | } |
| 6990 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6991 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6992 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6993 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6994 | /* |
| 6995 | ** Do various sanity checks on a single page of a tree. Return |
| 6996 | ** the tree depth. Root pages return 0. Parents of root pages |
| 6997 | ** return 1, and so forth. |
| 6998 | ** |
| 6999 | ** These checks are done: |
| 7000 | ** |
| 7001 | ** 1. Make sure that cells and freeblocks do not overlap |
| 7002 | ** but combine to completely cover the page. |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 7003 | ** NO 2. Make sure cell keys are in order. |
| 7004 | ** NO 3. Make sure no key is less than or equal to zLowerBound. |
| 7005 | ** NO 4. Make sure no key is greater than or equal to zUpperBound. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7006 | ** 5. Check the integrity of overflow pages. |
| 7007 | ** 6. Recursively call checkTreePage on all children. |
| 7008 | ** 7. Verify that the depth of all children is the same. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 7009 | ** 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] | 7010 | ** the root of the tree. |
| 7011 | */ |
| 7012 | static int checkTreePage( |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 7013 | IntegrityCk *pCheck, /* Context for the sanity check */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7014 | int iPage, /* Page number of the page to check */ |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 7015 | char *zParentContext /* Parent context */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7016 | ){ |
| 7017 | MemPage *pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 7018 | int i, rc, depth, d2, pgno, cnt; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 7019 | int hdr, cellStart; |
| 7020 | int nCell; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 7021 | u8 *data; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7022 | BtShared *pBt; |
drh | 4f26bb6 | 2005-09-08 14:17:20 +0000 | [diff] [blame] | 7023 | int usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7024 | char zContext[100]; |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 7025 | char *hit = 0; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7026 | |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 7027 | sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage); |
danielk1977 | ef73ee9 | 2004-11-06 12:26:07 +0000 | [diff] [blame] | 7028 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7029 | /* Check that the page exists |
| 7030 | */ |
drh | d9cb6ac | 2005-10-20 07:28:17 +0000 | [diff] [blame] | 7031 | pBt = pCheck->pBt; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 7032 | usableSize = pBt->usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7033 | if( iPage==0 ) return 0; |
| 7034 | if( checkRef(pCheck, iPage, zParentContext) ) return 0; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 7035 | if( (rc = sqlite3BtreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){ |
drh | e43ba70 | 2008-12-05 22:40:08 +0000 | [diff] [blame] | 7036 | if( rc==SQLITE_NOMEM ) pCheck->mallocFailed = 1; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7037 | checkAppendMsg(pCheck, zContext, |
| 7038 | "unable to get the page. error code=%d", rc); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7039 | return 0; |
| 7040 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 7041 | if( (rc = sqlite3BtreeInitPage(pPage))!=0 ){ |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 7042 | assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 7043 | checkAppendMsg(pCheck, zContext, |
| 7044 | "sqlite3BtreeInitPage() returns error code %d", rc); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 7045 | releasePage(pPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7046 | return 0; |
| 7047 | } |
| 7048 | |
| 7049 | /* Check out all the cells. |
| 7050 | */ |
| 7051 | depth = 0; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 7052 | for(i=0; i<pPage->nCell && pCheck->mxErr; i++){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 7053 | u8 *pCell; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 7054 | u32 sz; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 7055 | CellInfo info; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7056 | |
| 7057 | /* Check payload overflow pages |
| 7058 | */ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 7059 | sqlite3_snprintf(sizeof(zContext), zContext, |
| 7060 | "On tree page %d cell %d: ", iPage, i); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 7061 | pCell = findCell(pPage,i); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 7062 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 7063 | sz = info.nData; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 7064 | if( !pPage->intKey ) sz += (int)info.nKey; |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 7065 | assert( sz==info.nPayload ); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 7066 | if( sz>info.nLocal ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 7067 | int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7068 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
| 7069 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 7070 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 7071 | checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7072 | } |
| 7073 | #endif |
| 7074 | checkList(pCheck, 0, pgnoOvfl, nPage, zContext); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7075 | } |
| 7076 | |
| 7077 | /* Check sanity of left child page. |
| 7078 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 7079 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 7080 | pgno = get4byte(pCell); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7081 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 7082 | if( pBt->autoVacuum ){ |
| 7083 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext); |
| 7084 | } |
| 7085 | #endif |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 7086 | d2 = checkTreePage(pCheck, pgno, zContext); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 7087 | if( i>0 && d2!=depth ){ |
| 7088 | checkAppendMsg(pCheck, zContext, "Child page depth differs"); |
| 7089 | } |
| 7090 | depth = d2; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7091 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7092 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 7093 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 7094 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 7095 | sqlite3_snprintf(sizeof(zContext), zContext, |
| 7096 | "On page %d at right child: ", iPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7097 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 7098 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 7099 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7100 | } |
| 7101 | #endif |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 7102 | checkTreePage(pCheck, pgno, zContext); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 7103 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7104 | |
| 7105 | /* Check for complete coverage of the page |
| 7106 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 7107 | data = pPage->aData; |
| 7108 | hdr = pPage->hdrOffset; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 7109 | hit = sqlite3PageMalloc( pBt->pageSize ); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 7110 | if( hit==0 ){ |
| 7111 | pCheck->mallocFailed = 1; |
| 7112 | }else{ |
shane | 5780ebd | 2008-11-11 17:36:30 +0000 | [diff] [blame] | 7113 | u16 contentOffset = get2byte(&data[hdr+5]); |
| 7114 | if (contentOffset > usableSize) { |
| 7115 | checkAppendMsg(pCheck, 0, |
| 7116 | "Corruption detected in header on page %d",iPage,0); |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 7117 | goto check_page_abort; |
shane | 5780ebd | 2008-11-11 17:36:30 +0000 | [diff] [blame] | 7118 | } |
| 7119 | memset(hit+contentOffset, 0, usableSize-contentOffset); |
| 7120 | memset(hit, 1, contentOffset); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7121 | nCell = get2byte(&data[hdr+3]); |
| 7122 | cellStart = hdr + 12 - 4*pPage->leaf; |
| 7123 | for(i=0; i<nCell; i++){ |
| 7124 | int pc = get2byte(&data[cellStart+i*2]); |
danielk1977 | daca543 | 2008-08-25 11:57:16 +0000 | [diff] [blame] | 7125 | u16 size = 1024; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7126 | int j; |
danielk1977 | daca543 | 2008-08-25 11:57:16 +0000 | [diff] [blame] | 7127 | if( pc<=usableSize ){ |
| 7128 | size = cellSizePtr(pPage, &data[pc]); |
| 7129 | } |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 7130 | if( (pc+size-1)>=usableSize || pc<0 ){ |
| 7131 | checkAppendMsg(pCheck, 0, |
| 7132 | "Corruption detected in cell %d on page %d",i,iPage,0); |
| 7133 | }else{ |
| 7134 | for(j=pc+size-1; j>=pc; j--) hit[j]++; |
| 7135 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7136 | } |
| 7137 | for(cnt=0, i=get2byte(&data[hdr+1]); i>0 && i<usableSize && cnt<10000; |
| 7138 | cnt++){ |
| 7139 | int size = get2byte(&data[i+2]); |
| 7140 | int j; |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 7141 | if( (i+size-1)>=usableSize || i<0 ){ |
| 7142 | checkAppendMsg(pCheck, 0, |
| 7143 | "Corruption detected in cell %d on page %d",i,iPage,0); |
| 7144 | }else{ |
| 7145 | for(j=i+size-1; j>=i; j--) hit[j]++; |
| 7146 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7147 | i = get2byte(&data[i]); |
| 7148 | } |
| 7149 | for(i=cnt=0; i<usableSize; i++){ |
| 7150 | if( hit[i]==0 ){ |
| 7151 | cnt++; |
| 7152 | }else if( hit[i]>1 ){ |
| 7153 | checkAppendMsg(pCheck, 0, |
| 7154 | "Multiple uses for byte %d of page %d", i, iPage); |
| 7155 | break; |
| 7156 | } |
| 7157 | } |
| 7158 | if( cnt!=data[hdr+7] ){ |
| 7159 | checkAppendMsg(pCheck, 0, |
| 7160 | "Fragmented space is %d byte reported as %d on page %d", |
| 7161 | cnt, data[hdr+7], iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7162 | } |
| 7163 | } |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 7164 | check_page_abort: |
| 7165 | if (hit) sqlite3PageFree(hit); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 7166 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7167 | releasePage(pPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 7168 | return depth+1; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7169 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 7170 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7171 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 7172 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7173 | /* |
| 7174 | ** This routine does a complete check of the given BTree file. aRoot[] is |
| 7175 | ** an array of pages numbers were each page number is the root page of |
| 7176 | ** a table. nRoot is the number of entries in aRoot. |
| 7177 | ** |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 7178 | ** Write the number of error seen in *pnErr. Except for some memory |
drh | e43ba70 | 2008-12-05 22:40:08 +0000 | [diff] [blame] | 7179 | ** allocation errors, an error message held in memory obtained from |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 7180 | ** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is |
drh | e43ba70 | 2008-12-05 22:40:08 +0000 | [diff] [blame] | 7181 | ** returned. If a memory allocation error occurs, NULL is returned. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7182 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 7183 | char *sqlite3BtreeIntegrityCheck( |
| 7184 | Btree *p, /* The btree to be checked */ |
| 7185 | int *aRoot, /* An array of root pages numbers for individual trees */ |
| 7186 | int nRoot, /* Number of entries in aRoot[] */ |
| 7187 | int mxErr, /* Stop reporting errors after this many */ |
| 7188 | int *pnErr /* Write number of errors seen to this variable */ |
| 7189 | ){ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 7190 | Pgno i; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7191 | int nRef; |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 7192 | IntegrityCk sCheck; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7193 | BtShared *pBt = p->pBt; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 7194 | char zErr[100]; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7195 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7196 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7197 | pBt->db = p->db; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7198 | nRef = sqlite3PagerRefcount(pBt->pPager); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7199 | if( lockBtreeWithRetry(p)!=SQLITE_OK ){ |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 7200 | *pnErr = 1; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7201 | sqlite3BtreeLeave(p); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 7202 | return sqlite3DbStrDup(0, "cannot acquire a read lock on the database"); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 7203 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7204 | sCheck.pBt = pBt; |
| 7205 | sCheck.pPager = pBt->pPager; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 7206 | sCheck.nPage = pagerPagecount(sCheck.pBt); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 7207 | sCheck.mxErr = mxErr; |
| 7208 | sCheck.nErr = 0; |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 7209 | sCheck.mallocFailed = 0; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 7210 | *pnErr = 0; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 7211 | if( sCheck.nPage==0 ){ |
| 7212 | unlockBtreeIfUnused(pBt); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7213 | sqlite3BtreeLeave(p); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 7214 | return 0; |
| 7215 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 7216 | sCheck.anRef = sqlite3Malloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) ); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 7217 | if( !sCheck.anRef ){ |
| 7218 | unlockBtreeIfUnused(pBt); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 7219 | *pnErr = 1; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7220 | sqlite3BtreeLeave(p); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 7221 | return 0; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 7222 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 7223 | for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; } |
drh | 42cac6d | 2004-11-20 20:31:11 +0000 | [diff] [blame] | 7224 | i = PENDING_BYTE_PAGE(pBt); |
drh | 1f59571 | 2004-06-15 01:40:29 +0000 | [diff] [blame] | 7225 | if( i<=sCheck.nPage ){ |
| 7226 | sCheck.anRef[i] = 1; |
| 7227 | } |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 7228 | sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), 20000); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7229 | |
| 7230 | /* Check the integrity of the freelist |
| 7231 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 7232 | checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), |
| 7233 | get4byte(&pBt->pPage1->aData[36]), "Main freelist: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7234 | |
| 7235 | /* Check all the tables. |
| 7236 | */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 7237 | for(i=0; (int)i<nRoot && sCheck.mxErr; i++){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 7238 | if( aRoot[i]==0 ) continue; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 7239 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 7240 | if( pBt->autoVacuum && aRoot[i]>1 ){ |
| 7241 | checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0); |
| 7242 | } |
| 7243 | #endif |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 7244 | checkTreePage(&sCheck, aRoot[i], "List of tree roots: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7245 | } |
| 7246 | |
| 7247 | /* Make sure every page in the file is referenced |
| 7248 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 7249 | for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7250 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7251 | if( sCheck.anRef[i]==0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7252 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7253 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7254 | #else |
| 7255 | /* If the database supports auto-vacuum, make sure no tables contain |
| 7256 | ** references to pointer-map pages. |
| 7257 | */ |
| 7258 | if( sCheck.anRef[i]==0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 7259 | (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7260 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
| 7261 | } |
| 7262 | if( sCheck.anRef[i]!=0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 7263 | (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7264 | checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i); |
| 7265 | } |
| 7266 | #endif |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7267 | } |
| 7268 | |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 7269 | /* Make sure this analysis did not leave any unref() pages. |
| 7270 | ** This is an internal consistency check; an integrity check |
| 7271 | ** of the integrity check. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7272 | */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 7273 | unlockBtreeIfUnused(pBt); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 7274 | if( NEVER(nRef != sqlite3PagerRefcount(pBt->pPager)) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7275 | checkAppendMsg(&sCheck, 0, |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7276 | "Outstanding page count goes from %d to %d during this analysis", |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7277 | nRef, sqlite3PagerRefcount(pBt->pPager) |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7278 | ); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7279 | } |
| 7280 | |
| 7281 | /* Clean up and report errors. |
| 7282 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7283 | sqlite3BtreeLeave(p); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 7284 | sqlite3_free(sCheck.anRef); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 7285 | if( sCheck.mallocFailed ){ |
| 7286 | sqlite3StrAccumReset(&sCheck.errMsg); |
| 7287 | *pnErr = sCheck.nErr+1; |
| 7288 | return 0; |
| 7289 | } |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 7290 | *pnErr = sCheck.nErr; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 7291 | if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg); |
| 7292 | return sqlite3StrAccumFinish(&sCheck.errMsg); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7293 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 7294 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 7295 | |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 7296 | /* |
| 7297 | ** Return the full pathname of the underlying database file. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 7298 | ** |
| 7299 | ** The pager filename is invariant as long as the pager is |
| 7300 | ** open so it is safe to access without the BtShared mutex. |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 7301 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7302 | const char *sqlite3BtreeGetFilename(Btree *p){ |
| 7303 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7304 | return sqlite3PagerFilename(p->pBt->pPager); |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 7305 | } |
| 7306 | |
| 7307 | /* |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 7308 | ** Return the pathname of the journal file for this database. The return |
| 7309 | ** value of this routine is the same regardless of whether the journal file |
| 7310 | ** has been created or not. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 7311 | ** |
| 7312 | ** The pager journal filename is invariant as long as the pager is |
| 7313 | ** open so it is safe to access without the BtShared mutex. |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 7314 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7315 | const char *sqlite3BtreeGetJournalname(Btree *p){ |
| 7316 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7317 | return sqlite3PagerJournalname(p->pBt->pPager); |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 7318 | } |
| 7319 | |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 7320 | /* |
| 7321 | ** Return non-zero if a transaction is active. |
| 7322 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7323 | int sqlite3BtreeIsInTrans(Btree *p){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7324 | assert( p==0 || sqlite3_mutex_held(p->db->mutex) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7325 | return (p && (p->inTrans==TRANS_WRITE)); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 7326 | } |
| 7327 | |
| 7328 | /* |
| 7329 | ** Return non-zero if a statement transaction is active. |
| 7330 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7331 | int sqlite3BtreeIsInStmt(Btree *p){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7332 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 7333 | return ALWAYS(p->pBt) && p->pBt->inStmt; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 7334 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 7335 | |
| 7336 | /* |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 7337 | ** Return non-zero if a read (or write) transaction is active. |
| 7338 | */ |
| 7339 | int sqlite3BtreeIsInReadTrans(Btree *p){ |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 7340 | assert( p ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7341 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 7342 | return p->inTrans!=TRANS_NONE; |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 7343 | } |
| 7344 | |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 7345 | int sqlite3BtreeIsInBackup(Btree *p){ |
| 7346 | assert( p ); |
| 7347 | assert( sqlite3_mutex_held(p->db->mutex) ); |
| 7348 | return p->nBackup!=0; |
| 7349 | } |
| 7350 | |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 7351 | /* |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7352 | ** This function returns a pointer to a blob of memory associated with |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 7353 | ** 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] | 7354 | ** purposes (for example, to store a high-level schema associated with |
| 7355 | ** the shared-btree). The btree layer manages reference counting issues. |
| 7356 | ** |
| 7357 | ** The first time this is called on a shared-btree, nBytes bytes of memory |
| 7358 | ** are allocated, zeroed, and returned to the caller. For each subsequent |
| 7359 | ** call the nBytes parameter is ignored and a pointer to the same blob |
| 7360 | ** of memory returned. |
| 7361 | ** |
danielk1977 | 171bfed | 2008-06-23 09:50:50 +0000 | [diff] [blame] | 7362 | ** If the nBytes parameter is 0 and the blob of memory has not yet been |
| 7363 | ** allocated, a null pointer is returned. If the blob has already been |
| 7364 | ** allocated, it is returned as normal. |
| 7365 | ** |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7366 | ** Just before the shared-btree is closed, the function passed as the |
| 7367 | ** xFree argument when the memory allocation was made is invoked on the |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 7368 | ** blob of allocated memory. This function should not call sqlite3_free() |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7369 | ** on the memory, the btree layer does that. |
| 7370 | */ |
| 7371 | void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){ |
| 7372 | BtShared *pBt = p->pBt; |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7373 | sqlite3BtreeEnter(p); |
danielk1977 | 171bfed | 2008-06-23 09:50:50 +0000 | [diff] [blame] | 7374 | if( !pBt->pSchema && nBytes ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 7375 | pBt->pSchema = sqlite3MallocZero(nBytes); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7376 | pBt->xFreeSchema = xFree; |
| 7377 | } |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7378 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7379 | return pBt->pSchema; |
| 7380 | } |
| 7381 | |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 7382 | /* |
| 7383 | ** Return true if another user of the same shared btree as the argument |
| 7384 | ** handle holds an exclusive lock on the sqlite_master table. |
| 7385 | */ |
| 7386 | int sqlite3BtreeSchemaLocked(Btree *p){ |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7387 | int rc; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7388 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7389 | sqlite3BtreeEnter(p); |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 7390 | rc = (querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK)!=SQLITE_OK); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7391 | sqlite3BtreeLeave(p); |
| 7392 | return rc; |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 7393 | } |
| 7394 | |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 7395 | |
| 7396 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 7397 | /* |
| 7398 | ** Obtain a lock on the table whose root page is iTab. The |
| 7399 | ** lock is a write lock if isWritelock is true or a read lock |
| 7400 | ** if it is false. |
| 7401 | */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7402 | int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){ |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 7403 | int rc = SQLITE_OK; |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 7404 | if( p->sharable ){ |
| 7405 | u8 lockType = READ_LOCK + isWriteLock; |
| 7406 | assert( READ_LOCK+1==WRITE_LOCK ); |
| 7407 | assert( isWriteLock==0 || isWriteLock==1 ); |
| 7408 | sqlite3BtreeEnter(p); |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 7409 | rc = querySharedCacheTableLock(p, iTab, lockType); |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 7410 | if( rc==SQLITE_OK ){ |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 7411 | rc = setSharedCacheTableLock(p, iTab, lockType); |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 7412 | } |
| 7413 | sqlite3BtreeLeave(p); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7414 | } |
| 7415 | return rc; |
| 7416 | } |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 7417 | #endif |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 7418 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7419 | #ifndef SQLITE_OMIT_INCRBLOB |
| 7420 | /* |
| 7421 | ** Argument pCsr must be a cursor opened for writing on an |
| 7422 | ** INTKEY table currently pointing at a valid table entry. |
| 7423 | ** This function modifies the data stored as part of that entry. |
| 7424 | ** Only the data content may only be modified, it is not possible |
| 7425 | ** to change the length of the data stored. |
| 7426 | */ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7427 | int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7428 | assert( cursorHoldsMutex(pCsr) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7429 | assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) ); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7430 | assert(pCsr->isIncrblobHandle); |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 7431 | |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 7432 | restoreCursorPosition(pCsr); |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 7433 | assert( pCsr->eState!=CURSOR_REQUIRESEEK ); |
| 7434 | if( pCsr->eState!=CURSOR_VALID ){ |
| 7435 | return SQLITE_ABORT; |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7436 | } |
| 7437 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7438 | /* Check some preconditions: |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7439 | ** (a) the cursor is open for writing, |
| 7440 | ** (b) there is no read-lock on the table being modified and |
| 7441 | ** (c) the cursor points at a valid row of an intKey table. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7442 | */ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7443 | if( !pCsr->wrFlag ){ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7444 | return SQLITE_READONLY; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7445 | } |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 7446 | assert( !pCsr->pBt->readOnly |
| 7447 | && pCsr->pBt->inTransaction==TRANS_WRITE ); |
drh | 11b57d6 | 2009-02-24 19:21:41 +0000 | [diff] [blame^] | 7448 | if( checkForReadConflicts(pCsr->pBtree, pCsr->pgnoRoot, pCsr, 0) ){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7449 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 7450 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 7451 | if( pCsr->eState==CURSOR_INVALID || !pCsr->apPage[pCsr->iPage]->intKey ){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7452 | return SQLITE_ERROR; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7453 | } |
| 7454 | |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 7455 | return accessPayload(pCsr, offset, amt, (unsigned char *)z, 0, 1); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7456 | } |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7457 | |
| 7458 | /* |
| 7459 | ** 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] | 7460 | ** overflow list for the current row. This is used by cursors opened |
| 7461 | ** for incremental blob IO only. |
| 7462 | ** |
| 7463 | ** This function sets a flag only. The actual page location cache |
| 7464 | ** (stored in BtCursor.aOverflow[]) is allocated and used by function |
| 7465 | ** accessPayload() (the worker function for sqlite3BtreeData() and |
| 7466 | ** sqlite3BtreePutData()). |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7467 | */ |
| 7468 | void sqlite3BtreeCacheOverflow(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7469 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7470 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7471 | assert(!pCur->isIncrblobHandle); |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7472 | assert(!pCur->aOverflow); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7473 | pCur->isIncrblobHandle = 1; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7474 | } |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7475 | #endif |