drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 2 | ** 2004 April 6 |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame^] | 12 | ** $Id: btree.c,v 1.575 2009/03/18 10:33:01 danielk1977 Exp $ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 13 | ** |
| 14 | ** This file implements a external (disk-based) database using BTrees. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 15 | ** See the header comment on "btreeInt.h" for additional information. |
| 16 | ** Including a description of file format and an overview of operation. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 17 | */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 18 | #include "btreeInt.h" |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 19 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 20 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 21 | ** The header string that appears at the beginning of every |
| 22 | ** SQLite database. |
drh | 556b2a2 | 2005-06-14 16:04:05 +0000 | [diff] [blame] | 23 | */ |
drh | 556b2a2 | 2005-06-14 16:04:05 +0000 | [diff] [blame] | 24 | static const char zMagicHeader[] = SQLITE_FILE_HEADER; |
drh | 08ed44e | 2001-04-29 23:32:55 +0000 | [diff] [blame] | 25 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 26 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 27 | ** Set this global variable to 1 to enable tracing using the TRACE |
| 28 | ** macro. |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 29 | */ |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 30 | #if 0 |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 31 | int sqlite3BtreeTrace=0; /* True to enable tracing */ |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 32 | # define TRACE(X) if(sqlite3BtreeTrace){printf X;fflush(stdout);} |
| 33 | #else |
| 34 | # define TRACE(X) |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 35 | #endif |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 36 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 37 | |
| 38 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 39 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 40 | /* |
danielk1977 | 502b4e0 | 2008-09-02 14:07:24 +0000 | [diff] [blame] | 41 | ** A list of BtShared objects that are eligible for participation |
| 42 | ** in shared cache. This variable has file scope during normal builds, |
| 43 | ** but the test harness needs to access it so we make it global for |
| 44 | ** test builds. |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 45 | */ |
| 46 | #ifdef SQLITE_TEST |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 47 | BtShared *SQLITE_WSD sqlite3SharedCacheList = 0; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 48 | #else |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 49 | static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 50 | #endif |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 51 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| 52 | |
| 53 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 54 | /* |
| 55 | ** Enable or disable the shared pager and schema features. |
| 56 | ** |
| 57 | ** This routine has no effect on existing database connections. |
| 58 | ** The shared cache setting effects only future calls to |
| 59 | ** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2(). |
| 60 | */ |
| 61 | int sqlite3_enable_shared_cache(int enable){ |
danielk1977 | 502b4e0 | 2008-09-02 14:07:24 +0000 | [diff] [blame] | 62 | sqlite3GlobalConfig.sharedCacheEnabled = enable; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 63 | return SQLITE_OK; |
| 64 | } |
| 65 | #endif |
| 66 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 67 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 68 | /* |
drh | 66cbd15 | 2004-09-01 16:12:25 +0000 | [diff] [blame] | 69 | ** Forward declaration |
| 70 | */ |
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 | */ |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 112 | if( pBt->pWriter!=p && pBt->isExclusive ){ |
| 113 | sqlite3ConnectionBlocked(p->db, pBt->pWriter->db); |
| 114 | return SQLITE_LOCKED_SHAREDCACHE; |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 115 | } |
| 116 | |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 117 | /* This (along with setSharedCacheTableLock()) is where |
| 118 | ** the ReadUncommitted flag is dealt with. |
| 119 | ** If the caller is querying for a read-lock on any table |
drh | c74d0b1d | 2009-02-24 16:18:05 +0000 | [diff] [blame] | 120 | ** other than the sqlite_master table (table 1) and if the ReadUncommitted |
| 121 | ** flag is set, then the lock granted even if there are write-locks |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 122 | ** on the table. If a write-lock is requested, the ReadUncommitted flag |
| 123 | ** is not considered. |
| 124 | ** |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 125 | ** In function setSharedCacheTableLock(), if a read-lock is demanded and the |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 126 | ** ReadUncommitted flag is set, no entry is added to the locks list |
| 127 | ** (BtShared.pLock). |
| 128 | ** |
drh | c74d0b1d | 2009-02-24 16:18:05 +0000 | [diff] [blame] | 129 | ** To summarize: If the ReadUncommitted flag is set, then read cursors |
| 130 | ** on non-schema tables do not create or respect table locks. The locking |
| 131 | ** procedure for a write-cursor does not change. |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 132 | */ |
| 133 | if( |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 134 | 0==(p->db->flags&SQLITE_ReadUncommitted) || |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 135 | eLock==WRITE_LOCK || |
drh | 47ded16 | 2006-01-06 01:42:58 +0000 | [diff] [blame] | 136 | iTab==MASTER_ROOT |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 137 | ){ |
| 138 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 139 | if( pIter->pBtree!=p && pIter->iTable==iTab && |
| 140 | (pIter->eLock!=eLock || eLock!=READ_LOCK) ){ |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 141 | sqlite3ConnectionBlocked(p->db, pIter->pBtree->db); |
| 142 | if( eLock==WRITE_LOCK ){ |
| 143 | assert( p==pBt->pWriter ); |
| 144 | pBt->isPending = 1; |
| 145 | } |
| 146 | return SQLITE_LOCKED_SHAREDCACHE; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 147 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | return SQLITE_OK; |
| 151 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 152 | #endif /* !SQLITE_OMIT_SHARED_CACHE */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 153 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 154 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 155 | /* |
| 156 | ** Add a lock on the table with root-page iTable to the shared-btree used |
| 157 | ** by Btree handle p. Parameter eLock must be either READ_LOCK or |
| 158 | ** WRITE_LOCK. |
| 159 | ** |
| 160 | ** SQLITE_OK is returned if the lock is added successfully. SQLITE_BUSY and |
| 161 | ** SQLITE_NOMEM may also be returned. |
| 162 | */ |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 163 | static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 164 | BtShared *pBt = p->pBt; |
| 165 | BtLock *pLock = 0; |
| 166 | BtLock *pIter; |
| 167 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 168 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 169 | assert( eLock==READ_LOCK || eLock==WRITE_LOCK ); |
| 170 | assert( p->db!=0 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 171 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 172 | /* This is a no-op if the shared-cache is not enabled */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 173 | if( !p->sharable ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 174 | return SQLITE_OK; |
| 175 | } |
| 176 | |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 177 | assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 178 | |
drh | c74d0b1d | 2009-02-24 16:18:05 +0000 | [diff] [blame] | 179 | /* If the read-uncommitted flag is set and a read-lock is requested on |
| 180 | ** a non-schema table, then the lock is always granted. Return early |
| 181 | ** without adding an entry to the BtShared.pLock list. See |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 182 | ** comment in function querySharedCacheTableLock() for more info |
| 183 | ** on handling the ReadUncommitted flag. |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 184 | */ |
| 185 | if( |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 186 | (p->db->flags&SQLITE_ReadUncommitted) && |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 187 | (eLock==READ_LOCK) && |
drh | 47ded16 | 2006-01-06 01:42:58 +0000 | [diff] [blame] | 188 | iTable!=MASTER_ROOT |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 189 | ){ |
| 190 | return SQLITE_OK; |
| 191 | } |
| 192 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 193 | /* First search the list for an existing lock on this table. */ |
| 194 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 195 | if( pIter->iTable==iTable && pIter->pBtree==p ){ |
| 196 | pLock = pIter; |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /* If the above search did not find a BtLock struct associating Btree p |
| 202 | ** with table iTable, allocate one and link it into the list. |
| 203 | */ |
| 204 | if( !pLock ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 205 | pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock)); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 206 | if( !pLock ){ |
| 207 | return SQLITE_NOMEM; |
| 208 | } |
| 209 | pLock->iTable = iTable; |
| 210 | pLock->pBtree = p; |
| 211 | pLock->pNext = pBt->pLock; |
| 212 | pBt->pLock = pLock; |
| 213 | } |
| 214 | |
| 215 | /* Set the BtLock.eLock variable to the maximum of the current lock |
| 216 | ** and the requested lock. This means if a write-lock was already held |
| 217 | ** and a read-lock requested, we don't incorrectly downgrade the lock. |
| 218 | */ |
| 219 | assert( WRITE_LOCK>READ_LOCK ); |
danielk1977 | 5118b91 | 2005-12-30 16:31:53 +0000 | [diff] [blame] | 220 | if( eLock>pLock->eLock ){ |
| 221 | pLock->eLock = eLock; |
| 222 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 223 | |
| 224 | return SQLITE_OK; |
| 225 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 226 | #endif /* !SQLITE_OMIT_SHARED_CACHE */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 227 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 228 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 229 | /* |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 230 | ** Release all the table locks (locks obtained via calls to |
| 231 | ** the setSharedCacheTableLock() procedure) held by Btree handle p. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 232 | */ |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 233 | static void clearAllSharedCacheTableLocks(Btree *p){ |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 234 | BtShared *pBt = p->pBt; |
| 235 | BtLock **ppIter = &pBt->pLock; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 236 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 237 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 238 | assert( p->sharable || 0==*ppIter ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 239 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 240 | while( *ppIter ){ |
| 241 | BtLock *pLock = *ppIter; |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 242 | assert( pBt->isExclusive==0 || pBt->pWriter==pLock->pBtree ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 243 | if( pLock->pBtree==p ){ |
| 244 | *ppIter = pLock->pNext; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 245 | sqlite3_free(pLock); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 246 | }else{ |
| 247 | ppIter = &pLock->pNext; |
| 248 | } |
| 249 | } |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 250 | |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 251 | assert( pBt->isPending==0 || pBt->pWriter ); |
| 252 | if( pBt->pWriter==p ){ |
| 253 | pBt->pWriter = 0; |
| 254 | pBt->isExclusive = 0; |
| 255 | pBt->isPending = 0; |
| 256 | }else if( pBt->nTransaction==2 ){ |
| 257 | /* This function is called when connection p is concluding its |
| 258 | ** transaction. If there currently exists a writer, and p is not |
| 259 | ** that writer, then the number of locks held by connections other |
| 260 | ** than the writer must be about to drop to zero. In this case |
| 261 | ** set the isPending flag to 0. |
| 262 | ** |
| 263 | ** If there is not currently a writer, then BtShared.isPending must |
| 264 | ** be zero already. So this next line is harmless in that case. |
| 265 | */ |
| 266 | pBt->isPending = 0; |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 267 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 268 | } |
| 269 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| 270 | |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 271 | static void releasePage(MemPage *pPage); /* Forward reference */ |
| 272 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 273 | /* |
| 274 | ** Verify that the cursor holds a mutex on the BtShared |
| 275 | */ |
| 276 | #ifndef NDEBUG |
| 277 | static int cursorHoldsMutex(BtCursor *p){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 278 | return sqlite3_mutex_held(p->pBt->mutex); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 279 | } |
| 280 | #endif |
| 281 | |
| 282 | |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 283 | #ifndef SQLITE_OMIT_INCRBLOB |
| 284 | /* |
| 285 | ** Invalidate the overflow page-list cache for cursor pCur, if any. |
| 286 | */ |
| 287 | static void invalidateOverflowCache(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 288 | assert( cursorHoldsMutex(pCur) ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 289 | sqlite3_free(pCur->aOverflow); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 290 | pCur->aOverflow = 0; |
| 291 | } |
| 292 | |
| 293 | /* |
| 294 | ** Invalidate the overflow page-list cache for all cursors opened |
| 295 | ** on the shared btree structure pBt. |
| 296 | */ |
| 297 | static void invalidateAllOverflowCache(BtShared *pBt){ |
| 298 | BtCursor *p; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 299 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 300 | for(p=pBt->pCursor; p; p=p->pNext){ |
| 301 | invalidateOverflowCache(p); |
| 302 | } |
| 303 | } |
| 304 | #else |
| 305 | #define invalidateOverflowCache(x) |
| 306 | #define invalidateAllOverflowCache(x) |
| 307 | #endif |
| 308 | |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 309 | /* |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 310 | ** Set bit pgno of the BtShared.pHasContent bitvec. This is called |
| 311 | ** when a page that previously contained data becomes a free-list leaf |
| 312 | ** page. |
| 313 | ** |
| 314 | ** The BtShared.pHasContent bitvec exists to work around an obscure |
| 315 | ** bug caused by the interaction of two useful IO optimizations surrounding |
| 316 | ** free-list leaf pages: |
| 317 | ** |
| 318 | ** 1) When all data is deleted from a page and the page becomes |
| 319 | ** a free-list leaf page, the page is not written to the database |
| 320 | ** (as free-list leaf pages contain no meaningful data). Sometimes |
| 321 | ** such a page is not even journalled (as it will not be modified, |
| 322 | ** why bother journalling it?). |
| 323 | ** |
| 324 | ** 2) When a free-list leaf page is reused, its content is not read |
| 325 | ** from the database or written to the journal file (why should it |
| 326 | ** be, if it is not at all meaningful?). |
| 327 | ** |
| 328 | ** By themselves, these optimizations work fine and provide a handy |
| 329 | ** performance boost to bulk delete or insert operations. However, if |
| 330 | ** a page is moved to the free-list and then reused within the same |
| 331 | ** transaction, a problem comes up. If the page is not journalled when |
| 332 | ** it is moved to the free-list and it is also not journalled when it |
| 333 | ** is extracted from the free-list and reused, then the original data |
| 334 | ** may be lost. In the event of a rollback, it may not be possible |
| 335 | ** to restore the database to its original configuration. |
| 336 | ** |
| 337 | ** The solution is the BtShared.pHasContent bitvec. Whenever a page is |
| 338 | ** moved to become a free-list leaf page, the corresponding bit is |
| 339 | ** set in the bitvec. Whenever a leaf page is extracted from the free-list, |
| 340 | ** optimization 2 above is ommitted if the corresponding bit is already |
| 341 | ** set in BtShared.pHasContent. The contents of the bitvec are cleared |
| 342 | ** at the end of every transaction. |
| 343 | */ |
| 344 | static int btreeSetHasContent(BtShared *pBt, Pgno pgno){ |
| 345 | int rc = SQLITE_OK; |
| 346 | if( !pBt->pHasContent ){ |
| 347 | int nPage; |
| 348 | rc = sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 349 | if( rc==SQLITE_OK ){ |
| 350 | pBt->pHasContent = sqlite3BitvecCreate((u32)nPage); |
| 351 | if( !pBt->pHasContent ){ |
| 352 | rc = SQLITE_NOMEM; |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){ |
| 357 | rc = sqlite3BitvecSet(pBt->pHasContent, pgno); |
| 358 | } |
| 359 | return rc; |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | ** Query the BtShared.pHasContent vector. |
| 364 | ** |
| 365 | ** This function is called when a free-list leaf page is removed from the |
| 366 | ** free-list for reuse. It returns false if it is safe to retrieve the |
| 367 | ** page from the pager layer with the 'no-content' flag set. True otherwise. |
| 368 | */ |
| 369 | static int btreeGetHasContent(BtShared *pBt, Pgno pgno){ |
| 370 | Bitvec *p = pBt->pHasContent; |
| 371 | return (p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTest(p, pgno))); |
| 372 | } |
| 373 | |
| 374 | /* |
| 375 | ** Clear (destroy) the BtShared.pHasContent bitvec. This should be |
| 376 | ** invoked at the conclusion of each write-transaction. |
| 377 | */ |
| 378 | static void btreeClearHasContent(BtShared *pBt){ |
| 379 | sqlite3BitvecDestroy(pBt->pHasContent); |
| 380 | pBt->pHasContent = 0; |
| 381 | } |
| 382 | |
| 383 | /* |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 384 | ** Save the current cursor position in the variables BtCursor.nKey |
| 385 | ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK. |
| 386 | */ |
| 387 | static int saveCursorPosition(BtCursor *pCur){ |
| 388 | int rc; |
| 389 | |
| 390 | assert( CURSOR_VALID==pCur->eState ); |
| 391 | assert( 0==pCur->pKey ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 392 | assert( cursorHoldsMutex(pCur) ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 393 | |
| 394 | rc = sqlite3BtreeKeySize(pCur, &pCur->nKey); |
| 395 | |
| 396 | /* If this is an intKey table, then the above call to BtreeKeySize() |
| 397 | ** stores the integer key in pCur->nKey. In this case this value is |
| 398 | ** all that is required. Otherwise, if pCur is not open on an intKey |
| 399 | ** table, then malloc space for and store the pCur->nKey bytes of key |
| 400 | ** data. |
| 401 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 402 | if( rc==SQLITE_OK && 0==pCur->apPage[0]->intKey){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 403 | void *pKey = sqlite3Malloc( (int)pCur->nKey ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 404 | if( pKey ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 405 | rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 406 | if( rc==SQLITE_OK ){ |
| 407 | pCur->pKey = pKey; |
| 408 | }else{ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 409 | sqlite3_free(pKey); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 410 | } |
| 411 | }else{ |
| 412 | rc = SQLITE_NOMEM; |
| 413 | } |
| 414 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 415 | assert( !pCur->apPage[0]->intKey || !pCur->pKey ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 416 | |
| 417 | if( rc==SQLITE_OK ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 418 | int i; |
| 419 | for(i=0; i<=pCur->iPage; i++){ |
| 420 | releasePage(pCur->apPage[i]); |
| 421 | pCur->apPage[i] = 0; |
| 422 | } |
| 423 | pCur->iPage = -1; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 424 | pCur->eState = CURSOR_REQUIRESEEK; |
| 425 | } |
| 426 | |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 427 | invalidateOverflowCache(pCur); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 428 | return rc; |
| 429 | } |
| 430 | |
| 431 | /* |
| 432 | ** Save the positions of all cursors except pExcept open on the table |
| 433 | ** with root-page iRoot. Usually, this is called just before cursor |
| 434 | ** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()). |
| 435 | */ |
| 436 | static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){ |
| 437 | BtCursor *p; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 438 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 439 | assert( pExcept==0 || pExcept->pBt==pBt ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 440 | for(p=pBt->pCursor; p; p=p->pNext){ |
| 441 | if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) && |
| 442 | p->eState==CURSOR_VALID ){ |
| 443 | int rc = saveCursorPosition(p); |
| 444 | if( SQLITE_OK!=rc ){ |
| 445 | return rc; |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | return SQLITE_OK; |
| 450 | } |
| 451 | |
| 452 | /* |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 453 | ** Clear the current cursor position. |
| 454 | */ |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 455 | void sqlite3BtreeClearCursor(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 456 | assert( cursorHoldsMutex(pCur) ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 457 | sqlite3_free(pCur->pKey); |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 458 | pCur->pKey = 0; |
| 459 | pCur->eState = CURSOR_INVALID; |
| 460 | } |
| 461 | |
| 462 | /* |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 463 | ** Restore the cursor to the position it was in (or as close to as possible) |
| 464 | ** when saveCursorPosition() was called. Note that this call deletes the |
| 465 | ** saved position info stored by saveCursorPosition(), so there can be |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 466 | ** at most one effective restoreCursorPosition() call after each |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 467 | ** saveCursorPosition(). |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 468 | */ |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 469 | int sqlite3BtreeRestoreCursorPosition(BtCursor *pCur){ |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 470 | int rc; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 471 | assert( cursorHoldsMutex(pCur) ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 472 | assert( pCur->eState>=CURSOR_REQUIRESEEK ); |
| 473 | if( pCur->eState==CURSOR_FAULT ){ |
| 474 | return pCur->skip; |
| 475 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 476 | pCur->eState = CURSOR_INVALID; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 477 | rc = sqlite3BtreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skip); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 478 | if( rc==SQLITE_OK ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 479 | sqlite3_free(pCur->pKey); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 480 | pCur->pKey = 0; |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 481 | assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 482 | } |
| 483 | return rc; |
| 484 | } |
| 485 | |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 486 | #define restoreCursorPosition(p) \ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 487 | (p->eState>=CURSOR_REQUIRESEEK ? \ |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 488 | sqlite3BtreeRestoreCursorPosition(p) : \ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 489 | SQLITE_OK) |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 490 | |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 491 | /* |
| 492 | ** Determine whether or not a cursor has moved from the position it |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 493 | ** was last placed at. Cursors can move when the row they are pointing |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 494 | ** at is deleted out from under them. |
| 495 | ** |
| 496 | ** This routine returns an error code if something goes wrong. The |
| 497 | ** integer *pHasMoved is set to one if the cursor has moved and 0 if not. |
| 498 | */ |
| 499 | int sqlite3BtreeCursorHasMoved(BtCursor *pCur, int *pHasMoved){ |
| 500 | int rc; |
| 501 | |
| 502 | rc = restoreCursorPosition(pCur); |
| 503 | if( rc ){ |
| 504 | *pHasMoved = 1; |
| 505 | return rc; |
| 506 | } |
| 507 | if( pCur->eState!=CURSOR_VALID || pCur->skip!=0 ){ |
| 508 | *pHasMoved = 1; |
| 509 | }else{ |
| 510 | *pHasMoved = 0; |
| 511 | } |
| 512 | return SQLITE_OK; |
| 513 | } |
| 514 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 515 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 516 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 517 | ** Given a page number of a regular database page, return the page |
| 518 | ** number for the pointer-map page that contains the entry for the |
| 519 | ** input page number. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 520 | */ |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 521 | static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 522 | int nPagesPerMapPage; |
| 523 | Pgno iPtrMap, ret; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 524 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 525 | nPagesPerMapPage = (pBt->usableSize/5)+1; |
| 526 | iPtrMap = (pgno-2)/nPagesPerMapPage; |
| 527 | ret = (iPtrMap*nPagesPerMapPage) + 2; |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 528 | if( ret==PENDING_BYTE_PAGE(pBt) ){ |
| 529 | ret++; |
| 530 | } |
| 531 | return ret; |
| 532 | } |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 533 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 534 | /* |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 535 | ** Write an entry into the pointer map. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 536 | ** |
| 537 | ** This routine updates the pointer map entry for page number 'key' |
| 538 | ** so that it maps to type 'eType' and parent page number 'pgno'. |
| 539 | ** An error code is returned if something goes wrong, otherwise SQLITE_OK. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 540 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 541 | static int ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 542 | DbPage *pDbPage; /* The pointer map page */ |
| 543 | u8 *pPtrmap; /* The pointer map data */ |
| 544 | Pgno iPtrmap; /* The pointer map page number */ |
| 545 | int offset; /* Offset in pointer map page */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 546 | int rc; |
| 547 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 548 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 549 | /* The master-journal page number must never be used as a pointer map page */ |
| 550 | assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) ); |
| 551 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 552 | assert( pBt->autoVacuum ); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 553 | if( key==0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 554 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 555 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 556 | iPtrmap = PTRMAP_PAGENO(pBt, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 557 | rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 558 | if( rc!=SQLITE_OK ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 559 | return rc; |
| 560 | } |
danielk1977 | 8c666b1 | 2008-07-18 09:34:57 +0000 | [diff] [blame] | 561 | offset = PTRMAP_PTROFFSET(iPtrmap, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 562 | pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 563 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 564 | if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){ |
| 565 | TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent)); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 566 | rc = sqlite3PagerWrite(pDbPage); |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 567 | if( rc==SQLITE_OK ){ |
| 568 | pPtrmap[offset] = eType; |
| 569 | put4byte(&pPtrmap[offset+1], parent); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 570 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 571 | } |
| 572 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 573 | sqlite3PagerUnref(pDbPage); |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 574 | return rc; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | /* |
| 578 | ** Read an entry from the pointer map. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 579 | ** |
| 580 | ** This routine retrieves the pointer map entry for page 'key', writing |
| 581 | ** the type and parent page number to *pEType and *pPgno respectively. |
| 582 | ** An error code is returned if something goes wrong, otherwise SQLITE_OK. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 583 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 584 | static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 585 | DbPage *pDbPage; /* The pointer map page */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 586 | int iPtrmap; /* Pointer map page index */ |
| 587 | u8 *pPtrmap; /* Pointer map page data */ |
| 588 | int offset; /* Offset of entry in pointer map */ |
| 589 | int rc; |
| 590 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 591 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 592 | |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 593 | iPtrmap = PTRMAP_PAGENO(pBt, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 594 | rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 595 | if( rc!=0 ){ |
| 596 | return rc; |
| 597 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 598 | pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 599 | |
danielk1977 | 8c666b1 | 2008-07-18 09:34:57 +0000 | [diff] [blame] | 600 | offset = PTRMAP_PTROFFSET(iPtrmap, key); |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 601 | assert( pEType!=0 ); |
| 602 | *pEType = pPtrmap[offset]; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 603 | if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 604 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 605 | sqlite3PagerUnref(pDbPage); |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 606 | if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 607 | return SQLITE_OK; |
| 608 | } |
| 609 | |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 610 | #else /* if defined SQLITE_OMIT_AUTOVACUUM */ |
| 611 | #define ptrmapPut(w,x,y,z) SQLITE_OK |
| 612 | #define ptrmapGet(w,x,y,z) SQLITE_OK |
| 613 | #define ptrmapPutOvfl(y,z) SQLITE_OK |
| 614 | #endif |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 615 | |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 616 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 617 | ** Given a btree page and a cell index (0 means the first cell on |
| 618 | ** the page, 1 means the second cell, and so forth) return a pointer |
| 619 | ** to the cell content. |
| 620 | ** |
| 621 | ** This routine works only for pages that do not contain overflow cells. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 622 | */ |
drh | 1688c86 | 2008-07-18 02:44:17 +0000 | [diff] [blame] | 623 | #define findCell(P,I) \ |
| 624 | ((P)->aData + ((P)->maskPage & get2byte(&(P)->aData[(P)->cellOffset+2*(I)]))) |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 625 | |
| 626 | /* |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 627 | ** This a more complex version of findCell() that works for |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 628 | ** pages that do contain overflow cells. See insert |
| 629 | */ |
| 630 | static u8 *findOverflowCell(MemPage *pPage, int iCell){ |
| 631 | int i; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 632 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 633 | for(i=pPage->nOverflow-1; i>=0; i--){ |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 634 | int k; |
| 635 | struct _OvflCell *pOvfl; |
| 636 | pOvfl = &pPage->aOvfl[i]; |
| 637 | k = pOvfl->idx; |
| 638 | if( k<=iCell ){ |
| 639 | if( k==iCell ){ |
| 640 | return pOvfl->pCell; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 641 | } |
| 642 | iCell--; |
| 643 | } |
| 644 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 645 | return findCell(pPage, iCell); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | /* |
| 649 | ** Parse a cell content block and fill in the CellInfo structure. There |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 650 | ** are two versions of this function. sqlite3BtreeParseCell() takes a |
| 651 | ** cell index as the second argument and sqlite3BtreeParseCellPtr() |
| 652 | ** takes a pointer to the body of the cell as its second argument. |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 653 | ** |
| 654 | ** Within this file, the parseCell() macro can be called instead of |
| 655 | ** sqlite3BtreeParseCellPtr(). Using some compilers, this will be faster. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 656 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 657 | void sqlite3BtreeParseCellPtr( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 658 | MemPage *pPage, /* Page containing the cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 659 | u8 *pCell, /* Pointer to the cell text. */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 660 | CellInfo *pInfo /* Fill in this structure */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 661 | ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 662 | u16 n; /* Number bytes in cell content header */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 663 | u32 nPayload; /* Number of bytes of cell payload */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 664 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 665 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 666 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 667 | pInfo->pCell = pCell; |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 668 | assert( pPage->leaf==0 || pPage->leaf==1 ); |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 669 | n = pPage->childPtrSize; |
| 670 | assert( n==4-4*pPage->leaf ); |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 671 | if( pPage->intKey ){ |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 672 | if( pPage->hasData ){ |
| 673 | n += getVarint32(&pCell[n], nPayload); |
| 674 | }else{ |
| 675 | nPayload = 0; |
| 676 | } |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 677 | n += getVarint(&pCell[n], (u64*)&pInfo->nKey); |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 678 | pInfo->nData = nPayload; |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 679 | }else{ |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 680 | pInfo->nData = 0; |
| 681 | n += getVarint32(&pCell[n], nPayload); |
| 682 | pInfo->nKey = nPayload; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 683 | } |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 684 | pInfo->nPayload = nPayload; |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 685 | pInfo->nHeader = n; |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 686 | if( likely(nPayload<=pPage->maxLocal) ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 687 | /* This is the (easy) common case where the entire payload fits |
| 688 | ** on the local page. No overflow is required. |
| 689 | */ |
| 690 | int nSize; /* Total size of cell content in bytes */ |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 691 | nSize = nPayload + n; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 692 | pInfo->nLocal = (u16)nPayload; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 693 | pInfo->iOverflow = 0; |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 694 | if( (nSize & ~3)==0 ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 695 | nSize = 4; /* Minimum cell size is 4 */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 696 | } |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 697 | pInfo->nSize = (u16)nSize; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 698 | }else{ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 699 | /* If the payload will not fit completely on the local page, we have |
| 700 | ** to decide how much to store locally and how much to spill onto |
| 701 | ** overflow pages. The strategy is to minimize the amount of unused |
| 702 | ** space on overflow pages while keeping the amount of local storage |
| 703 | ** in between minLocal and maxLocal. |
| 704 | ** |
| 705 | ** Warning: changing the way overflow payload is distributed in any |
| 706 | ** way will result in an incompatible file format. |
| 707 | */ |
| 708 | int minLocal; /* Minimum amount of payload held locally */ |
| 709 | int maxLocal; /* Maximum amount of payload held locally */ |
| 710 | int surplus; /* Overflow payload available for local storage */ |
| 711 | |
| 712 | minLocal = pPage->minLocal; |
| 713 | maxLocal = pPage->maxLocal; |
| 714 | surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 715 | if( surplus <= maxLocal ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 716 | pInfo->nLocal = (u16)surplus; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 717 | }else{ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 718 | pInfo->nLocal = (u16)minLocal; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 719 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 720 | pInfo->iOverflow = (u16)(pInfo->nLocal + n); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 721 | pInfo->nSize = pInfo->iOverflow + 4; |
| 722 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 723 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 724 | #define parseCell(pPage, iCell, pInfo) \ |
| 725 | sqlite3BtreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo)) |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 726 | void sqlite3BtreeParseCell( |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 727 | MemPage *pPage, /* Page containing the cell */ |
| 728 | int iCell, /* The cell index. First cell is 0 */ |
| 729 | CellInfo *pInfo /* Fill in this structure */ |
| 730 | ){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 731 | parseCell(pPage, iCell, pInfo); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 732 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 733 | |
| 734 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 735 | ** Compute the total number of bytes that a Cell needs in the cell |
| 736 | ** data area of the btree-page. The return number includes the cell |
| 737 | ** data header and the local payload, but not any overflow page or |
| 738 | ** the space used by the cell pointer. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 739 | */ |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 740 | #ifndef NDEBUG |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 741 | static u16 cellSize(MemPage *pPage, int iCell){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 742 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 743 | sqlite3BtreeParseCell(pPage, iCell, &info); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 744 | return info.nSize; |
| 745 | } |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 746 | #endif |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 747 | static u16 cellSizePtr(MemPage *pPage, u8 *pCell){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 748 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 749 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 750 | return info.nSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 751 | } |
| 752 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 753 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 754 | /* |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 755 | ** If the cell pCell, part of page pPage contains a pointer |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 756 | ** to an overflow page, insert an entry into the pointer-map |
| 757 | ** for the overflow page. |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 758 | */ |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 759 | static int ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell){ |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 760 | CellInfo info; |
| 761 | assert( pCell!=0 ); |
| 762 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
| 763 | assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); |
| 764 | if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ |
| 765 | Pgno ovfl = get4byte(&pCell[info.iOverflow]); |
| 766 | return ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 767 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 768 | return SQLITE_OK; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 769 | } |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 770 | /* |
| 771 | ** If the cell with index iCell on page pPage contains a pointer |
| 772 | ** to an overflow page, insert an entry into the pointer-map |
| 773 | ** for the overflow page. |
| 774 | */ |
| 775 | static int ptrmapPutOvfl(MemPage *pPage, int iCell){ |
| 776 | u8 *pCell; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 777 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 778 | pCell = findOverflowCell(pPage, iCell); |
| 779 | return ptrmapPutOvflPtr(pPage, pCell); |
| 780 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 781 | #endif |
| 782 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 783 | |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 784 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 785 | ** Defragment the page given. All Cells are moved to the |
drh | 3a4a2d4 | 2005-11-24 14:24:28 +0000 | [diff] [blame] | 786 | ** end of the page and all free space is collected into one |
| 787 | ** big FreeBlk that occurs in between the header and cell |
drh | 31beae9 | 2005-11-24 14:34:36 +0000 | [diff] [blame] | 788 | ** pointer array and the cell content area. |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 789 | */ |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 790 | static int defragmentPage(MemPage *pPage){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 791 | int i; /* Loop counter */ |
| 792 | int pc; /* Address of a i-th cell */ |
| 793 | int addr; /* Offset of first byte after cell pointer array */ |
| 794 | int hdr; /* Offset to the page header */ |
| 795 | int size; /* Size of a cell */ |
| 796 | int usableSize; /* Number of usable bytes on a page */ |
| 797 | int cellOffset; /* Offset to the cell pointer array */ |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 798 | int cbrk; /* Offset to the cell content area */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 799 | int nCell; /* Number of cells on the page */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 800 | unsigned char *data; /* The page data */ |
| 801 | unsigned char *temp; /* Temp area for cell content */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 802 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 803 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 804 | assert( pPage->pBt!=0 ); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 805 | assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 806 | assert( pPage->nOverflow==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 807 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 26b7994 | 2007-11-28 16:19:56 +0000 | [diff] [blame] | 808 | temp = sqlite3PagerTempSpace(pPage->pBt->pPager); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 809 | data = pPage->aData; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 810 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 811 | cellOffset = pPage->cellOffset; |
| 812 | nCell = pPage->nCell; |
| 813 | assert( nCell==get2byte(&data[hdr+3]) ); |
| 814 | usableSize = pPage->pBt->usableSize; |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 815 | cbrk = get2byte(&data[hdr+5]); |
| 816 | memcpy(&temp[cbrk], &data[cbrk], usableSize - cbrk); |
| 817 | cbrk = usableSize; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 818 | for(i=0; i<nCell; i++){ |
| 819 | u8 *pAddr; /* The i-th cell pointer */ |
| 820 | pAddr = &data[cellOffset + i*2]; |
| 821 | pc = get2byte(pAddr); |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 822 | if( pc>=usableSize ){ |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 823 | return SQLITE_CORRUPT_BKPT; |
| 824 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 825 | size = cellSizePtr(pPage, &temp[pc]); |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 826 | cbrk -= size; |
danielk1977 | 0d06541 | 2008-11-12 18:21:36 +0000 | [diff] [blame] | 827 | if( cbrk<cellOffset+2*nCell || pc+size>usableSize ){ |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 828 | return SQLITE_CORRUPT_BKPT; |
| 829 | } |
danielk1977 | 0d06541 | 2008-11-12 18:21:36 +0000 | [diff] [blame] | 830 | assert( cbrk+size<=usableSize && cbrk>=0 ); |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 831 | memcpy(&data[cbrk], &temp[pc], size); |
| 832 | put2byte(pAddr, cbrk); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 833 | } |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 834 | assert( cbrk>=cellOffset+2*nCell ); |
| 835 | put2byte(&data[hdr+5], cbrk); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 836 | data[hdr+1] = 0; |
| 837 | data[hdr+2] = 0; |
| 838 | data[hdr+7] = 0; |
| 839 | addr = cellOffset+2*nCell; |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 840 | memset(&data[addr], 0, cbrk-addr); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 841 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
danielk1977 | 360e634 | 2008-11-12 08:49:51 +0000 | [diff] [blame] | 842 | if( cbrk-addr!=pPage->nFree ){ |
| 843 | return SQLITE_CORRUPT_BKPT; |
| 844 | } |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 845 | return SQLITE_OK; |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 846 | } |
| 847 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 848 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 849 | ** Allocate nByte bytes of space on a page. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 850 | ** |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 851 | ** Return the index into pPage->aData[] of the first byte of |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 852 | ** the new allocation. The caller guarantees that there is enough |
| 853 | ** space. This routine will never fail. |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 854 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 855 | ** If the page contains nBytes of free space but does not contain |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 856 | ** nBytes of contiguous free space, then this routine automatically |
| 857 | ** calls defragementPage() to consolidate all free space before |
| 858 | ** allocating the new chunk. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 859 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 860 | static int allocateSpace(MemPage *pPage, int nByte){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 861 | int addr, pc, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 862 | int size; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 863 | int nFrag; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 864 | int top; |
| 865 | int nCell; |
| 866 | int cellOffset; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 867 | unsigned char *data; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 868 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 869 | data = pPage->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 870 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 871 | assert( pPage->pBt ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 872 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 873 | assert( nByte>=0 ); /* Minimum cell size is 4 */ |
| 874 | assert( pPage->nFree>=nByte ); |
| 875 | assert( pPage->nOverflow==0 ); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 876 | pPage->nFree -= (u16)nByte; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 877 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 878 | |
| 879 | nFrag = data[hdr+7]; |
| 880 | if( nFrag<60 ){ |
| 881 | /* Search the freelist looking for a slot big enough to satisfy the |
| 882 | ** space request. */ |
| 883 | addr = hdr+1; |
| 884 | while( (pc = get2byte(&data[addr]))>0 ){ |
| 885 | size = get2byte(&data[pc+2]); |
| 886 | if( size>=nByte ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 887 | int x = size - nByte; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 888 | if( size<nByte+4 ){ |
| 889 | memcpy(&data[addr], &data[pc], 2); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 890 | data[hdr+7] = (u8)(nFrag + x); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 891 | return pc; |
| 892 | }else{ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 893 | put2byte(&data[pc+2], x); |
| 894 | return pc + x; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | addr = pc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 898 | } |
| 899 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 900 | |
| 901 | /* Allocate memory from the gap in between the cell pointer array |
| 902 | ** and the cell content area. |
| 903 | */ |
| 904 | top = get2byte(&data[hdr+5]); |
| 905 | nCell = get2byte(&data[hdr+3]); |
| 906 | cellOffset = pPage->cellOffset; |
| 907 | if( nFrag>=60 || cellOffset + 2*nCell > top - nByte ){ |
danielk1977 | 474b7cc | 2008-07-09 11:49:46 +0000 | [diff] [blame] | 908 | defragmentPage(pPage); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 909 | top = get2byte(&data[hdr+5]); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 910 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 911 | top -= nByte; |
| 912 | assert( cellOffset + 2*nCell <= top ); |
| 913 | put2byte(&data[hdr+5], top); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 914 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 915 | return top; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 919 | ** Return a section of the pPage->aData to the freelist. |
| 920 | ** The first byte of the new free block is pPage->aDisk[start] |
| 921 | ** and the size of the block is "size" bytes. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 922 | ** |
| 923 | ** Most of the effort here is involved in coalesing adjacent |
| 924 | ** free blocks into a single big free block. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 925 | */ |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 926 | static int freeSpace(MemPage *pPage, int start, int size){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 927 | int addr, pbegin, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 928 | unsigned char *data = pPage->aData; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 929 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 930 | assert( pPage->pBt!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 931 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 932 | assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) ); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 933 | assert( (start + size)<=pPage->pBt->usableSize ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 934 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 935 | assert( size>=0 ); /* Minimum cell size is 4 */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 936 | |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 937 | #ifdef SQLITE_SECURE_DELETE |
| 938 | /* Overwrite deleted information with zeros when the SECURE_DELETE |
| 939 | ** option is enabled at compile-time */ |
| 940 | memset(&data[start], 0, size); |
| 941 | #endif |
| 942 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 943 | /* Add the space back into the linked list of freeblocks */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 944 | hdr = pPage->hdrOffset; |
| 945 | addr = hdr + 1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 946 | while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 947 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 948 | if( pbegin<=addr ) { |
| 949 | return SQLITE_CORRUPT_BKPT; |
| 950 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 951 | addr = pbegin; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 952 | } |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 953 | if ( pbegin>pPage->pBt->usableSize-4 ) { |
| 954 | return SQLITE_CORRUPT_BKPT; |
| 955 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 956 | assert( pbegin>addr || pbegin==0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 957 | put2byte(&data[addr], start); |
| 958 | put2byte(&data[start], pbegin); |
| 959 | put2byte(&data[start+2], size); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 960 | pPage->nFree += (u16)size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 961 | |
| 962 | /* Coalesce adjacent free blocks */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 963 | addr = pPage->hdrOffset + 1; |
| 964 | while( (pbegin = get2byte(&data[addr]))>0 ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 965 | int pnext, psize, x; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 966 | assert( pbegin>addr ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 967 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 968 | pnext = get2byte(&data[pbegin]); |
| 969 | psize = get2byte(&data[pbegin+2]); |
| 970 | if( pbegin + psize + 3 >= pnext && pnext>0 ){ |
| 971 | int frag = pnext - (pbegin+psize); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 972 | if( (frag<0) || (frag>(int)data[pPage->hdrOffset+7]) ){ |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 973 | return SQLITE_CORRUPT_BKPT; |
| 974 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 975 | data[pPage->hdrOffset+7] -= (u8)frag; |
| 976 | x = get2byte(&data[pnext]); |
| 977 | put2byte(&data[pbegin], x); |
| 978 | x = pnext + get2byte(&data[pnext+2]) - pbegin; |
| 979 | put2byte(&data[pbegin+2], x); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 980 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 981 | addr = pbegin; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 982 | } |
| 983 | } |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 984 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 985 | /* If the cell content area begins with a freeblock, remove it. */ |
| 986 | if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){ |
| 987 | int top; |
| 988 | pbegin = get2byte(&data[hdr+1]); |
| 989 | memcpy(&data[hdr+1], &data[pbegin], 2); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 990 | top = get2byte(&data[hdr+5]) + get2byte(&data[pbegin+2]); |
| 991 | put2byte(&data[hdr+5], top); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 992 | } |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 993 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 994 | return SQLITE_OK; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 998 | ** Decode the flags byte (the first byte of the header) for a page |
| 999 | ** and initialize fields of the MemPage structure accordingly. |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1000 | ** |
| 1001 | ** Only the following combinations are supported. Anything different |
| 1002 | ** indicates a corrupt database files: |
| 1003 | ** |
| 1004 | ** PTF_ZERODATA |
| 1005 | ** PTF_ZERODATA | PTF_LEAF |
| 1006 | ** PTF_LEAFDATA | PTF_INTKEY |
| 1007 | ** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1008 | */ |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1009 | static int decodeFlags(MemPage *pPage, int flagByte){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1010 | BtShared *pBt; /* A copy of pPage->pBt */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1011 | |
| 1012 | assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1013 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1014 | pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 ); |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1015 | flagByte &= ~PTF_LEAF; |
| 1016 | pPage->childPtrSize = 4-4*pPage->leaf; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1017 | pBt = pPage->pBt; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1018 | if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){ |
| 1019 | pPage->intKey = 1; |
| 1020 | pPage->hasData = pPage->leaf; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1021 | pPage->maxLocal = pBt->maxLeaf; |
| 1022 | pPage->minLocal = pBt->minLeaf; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1023 | }else if( flagByte==PTF_ZERODATA ){ |
| 1024 | pPage->intKey = 0; |
| 1025 | pPage->hasData = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1026 | pPage->maxLocal = pBt->maxLocal; |
| 1027 | pPage->minLocal = pBt->minLocal; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1028 | }else{ |
| 1029 | return SQLITE_CORRUPT_BKPT; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1030 | } |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1031 | return SQLITE_OK; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1035 | ** Initialize the auxiliary information for a disk block. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1036 | ** |
| 1037 | ** Return SQLITE_OK on success. If we see that the page does |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1038 | ** not contain a well-formed database page, then return |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1039 | ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not |
| 1040 | ** guarantee that the page is well-formed. It only shows that |
| 1041 | ** we failed to detect any corruption. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1042 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1043 | int sqlite3BtreeInitPage(MemPage *pPage){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1044 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1045 | assert( pPage->pBt!=0 ); |
| 1046 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1047 | assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1048 | assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) ); |
| 1049 | assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1050 | |
| 1051 | if( !pPage->isInit ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1052 | u16 pc; /* Address of a freeblock within pPage->aData[] */ |
| 1053 | u8 hdr; /* Offset to beginning of page header */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1054 | u8 *data; /* Equal to pPage->aData */ |
| 1055 | BtShared *pBt; /* The main btree structure */ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1056 | u16 usableSize; /* Amount of usable space on each page */ |
| 1057 | u16 cellOffset; /* Offset from start of page to first cell pointer */ |
| 1058 | u16 nFree; /* Number of unused bytes on the page */ |
| 1059 | u16 top; /* First byte of the cell content area */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1060 | |
| 1061 | pBt = pPage->pBt; |
| 1062 | |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1063 | hdr = pPage->hdrOffset; |
| 1064 | data = pPage->aData; |
| 1065 | if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT; |
| 1066 | assert( pBt->pageSize>=512 && pBt->pageSize<=32768 ); |
| 1067 | pPage->maskPage = pBt->pageSize - 1; |
| 1068 | pPage->nOverflow = 0; |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1069 | usableSize = pBt->usableSize; |
| 1070 | pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf; |
| 1071 | top = get2byte(&data[hdr+5]); |
| 1072 | pPage->nCell = get2byte(&data[hdr+3]); |
| 1073 | if( pPage->nCell>MX_CELL(pBt) ){ |
| 1074 | /* To many cells for a single page. The page must be corrupt */ |
| 1075 | return SQLITE_CORRUPT_BKPT; |
| 1076 | } |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1077 | |
| 1078 | /* Compute the total free space on the page */ |
| 1079 | pc = get2byte(&data[hdr+1]); |
| 1080 | nFree = data[hdr+7] + top - (cellOffset + 2*pPage->nCell); |
| 1081 | while( pc>0 ){ |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 1082 | u16 next, size; |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1083 | if( pc>usableSize-4 ){ |
| 1084 | /* Free block is off the page */ |
| 1085 | return SQLITE_CORRUPT_BKPT; |
| 1086 | } |
| 1087 | next = get2byte(&data[pc]); |
| 1088 | size = get2byte(&data[pc+2]); |
| 1089 | if( next>0 && next<=pc+size+3 ){ |
| 1090 | /* Free blocks must be in accending order */ |
| 1091 | return SQLITE_CORRUPT_BKPT; |
| 1092 | } |
| 1093 | nFree += size; |
| 1094 | pc = next; |
| 1095 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1096 | pPage->nFree = (u16)nFree; |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1097 | if( nFree>=usableSize ){ |
| 1098 | /* Free space cannot exceed total page size */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1099 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1100 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1101 | |
drh | 1688c86 | 2008-07-18 02:44:17 +0000 | [diff] [blame] | 1102 | #if 0 |
| 1103 | /* Check that all the offsets in the cell offset array are within range. |
| 1104 | ** |
| 1105 | ** Omitting this consistency check and using the pPage->maskPage mask |
| 1106 | ** to prevent overrunning the page buffer in findCell() results in a |
| 1107 | ** 2.5% performance gain. |
| 1108 | */ |
| 1109 | { |
| 1110 | u8 *pOff; /* Iterator used to check all cell offsets are in range */ |
| 1111 | u8 *pEnd; /* Pointer to end of cell offset array */ |
| 1112 | u8 mask; /* Mask of bits that must be zero in MSB of cell offsets */ |
| 1113 | mask = ~(((u8)(pBt->pageSize>>8))-1); |
| 1114 | pEnd = &data[cellOffset + pPage->nCell*2]; |
| 1115 | for(pOff=&data[cellOffset]; pOff!=pEnd && !((*pOff)&mask); pOff+=2); |
| 1116 | if( pOff!=pEnd ){ |
| 1117 | return SQLITE_CORRUPT_BKPT; |
| 1118 | } |
danielk1977 | e16535f | 2008-06-11 18:15:29 +0000 | [diff] [blame] | 1119 | } |
drh | 1688c86 | 2008-07-18 02:44:17 +0000 | [diff] [blame] | 1120 | #endif |
danielk1977 | e16535f | 2008-06-11 18:15:29 +0000 | [diff] [blame] | 1121 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1122 | pPage->isInit = 1; |
| 1123 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1124 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1128 | ** Set up a raw page so that it looks like a database page holding |
| 1129 | ** no entries. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1130 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1131 | static void zeroPage(MemPage *pPage, int flags){ |
| 1132 | unsigned char *data = pPage->aData; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1133 | BtShared *pBt = pPage->pBt; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1134 | u8 hdr = pPage->hdrOffset; |
| 1135 | u16 first; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1136 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1137 | assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1138 | assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); |
| 1139 | assert( sqlite3PagerGetData(pPage->pDbPage) == data ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1140 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1141 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 1af4a6e | 2008-07-18 03:32:51 +0000 | [diff] [blame] | 1142 | /*memset(&data[hdr], 0, pBt->usableSize - hdr);*/ |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 1143 | data[hdr] = (char)flags; |
| 1144 | first = hdr + 8 + 4*((flags&PTF_LEAF)==0 ?1:0); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1145 | memset(&data[hdr+1], 0, 4); |
| 1146 | data[hdr+7] = 0; |
| 1147 | put2byte(&data[hdr+5], pBt->usableSize); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1148 | pPage->nFree = pBt->usableSize - first; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1149 | decodeFlags(pPage, flags); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1150 | pPage->hdrOffset = hdr; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1151 | pPage->cellOffset = first; |
| 1152 | pPage->nOverflow = 0; |
drh | 1688c86 | 2008-07-18 02:44:17 +0000 | [diff] [blame] | 1153 | assert( pBt->pageSize>=512 && pBt->pageSize<=32768 ); |
| 1154 | pPage->maskPage = pBt->pageSize - 1; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1155 | pPage->nCell = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1156 | pPage->isInit = 1; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1159 | |
| 1160 | /* |
| 1161 | ** Convert a DbPage obtained from the pager into a MemPage used by |
| 1162 | ** the btree layer. |
| 1163 | */ |
| 1164 | static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){ |
| 1165 | MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage); |
| 1166 | pPage->aData = sqlite3PagerGetData(pDbPage); |
| 1167 | pPage->pDbPage = pDbPage; |
| 1168 | pPage->pBt = pBt; |
| 1169 | pPage->pgno = pgno; |
| 1170 | pPage->hdrOffset = pPage->pgno==1 ? 100 : 0; |
| 1171 | return pPage; |
| 1172 | } |
| 1173 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1174 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1175 | ** Get a page from the pager. Initialize the MemPage.pBt and |
| 1176 | ** MemPage.aData elements if needed. |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 1177 | ** |
| 1178 | ** If the noContent flag is set, it means that we do not care about |
| 1179 | ** the content of the page at this time. So do not go to the disk |
| 1180 | ** to fetch the content. Just fill in the content with zeros for now. |
| 1181 | ** If in the future we call sqlite3PagerWrite() on this page, that |
| 1182 | ** means we have started to be concerned about content and the disk |
| 1183 | ** read should occur at that point. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1184 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1185 | int sqlite3BtreeGetPage( |
| 1186 | BtShared *pBt, /* The btree */ |
| 1187 | Pgno pgno, /* Number of the page to fetch */ |
| 1188 | MemPage **ppPage, /* Return the page in this parameter */ |
| 1189 | int noContent /* Do not load page content if true */ |
| 1190 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1191 | int rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1192 | DbPage *pDbPage; |
| 1193 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1194 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 1195 | rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1196 | if( rc ) return rc; |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1197 | *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1198 | return SQLITE_OK; |
| 1199 | } |
| 1200 | |
| 1201 | /* |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 1202 | ** Retrieve a page from the pager cache. If the requested page is not |
| 1203 | ** already in the pager cache return NULL. Initialize the MemPage.pBt and |
| 1204 | ** MemPage.aData elements if needed. |
| 1205 | */ |
| 1206 | static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){ |
| 1207 | DbPage *pDbPage; |
| 1208 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 1209 | pDbPage = sqlite3PagerLookup(pBt->pPager, pgno); |
| 1210 | if( pDbPage ){ |
| 1211 | return btreePageFromDbPage(pDbPage, pgno, pBt); |
| 1212 | } |
| 1213 | return 0; |
| 1214 | } |
| 1215 | |
| 1216 | /* |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 1217 | ** Return the size of the database file in pages. If there is any kind of |
| 1218 | ** error, return ((unsigned int)-1). |
danielk1977 | 67fd7a9 | 2008-09-10 17:53:35 +0000 | [diff] [blame] | 1219 | */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 1220 | static Pgno pagerPagecount(BtShared *pBt){ |
| 1221 | int nPage = -1; |
danielk1977 | 67fd7a9 | 2008-09-10 17:53:35 +0000 | [diff] [blame] | 1222 | int rc; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 1223 | assert( pBt->pPage1 ); |
| 1224 | rc = sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 1225 | assert( rc==SQLITE_OK || nPage==-1 ); |
| 1226 | return (Pgno)nPage; |
danielk1977 | 67fd7a9 | 2008-09-10 17:53:35 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | /* |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1230 | ** Get a page from the pager and initialize it. This routine |
| 1231 | ** is just a convenience wrapper around separate calls to |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1232 | ** sqlite3BtreeGetPage() and sqlite3BtreeInitPage(). |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1233 | */ |
| 1234 | static int getAndInitPage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1235 | BtShared *pBt, /* The database file */ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1236 | Pgno pgno, /* Number of the page to get */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1237 | MemPage **ppPage /* Write the page pointer here */ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1238 | ){ |
| 1239 | int rc; |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1240 | MemPage *pPage; |
| 1241 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1242 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1243 | if( pgno==0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1244 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1245 | } |
danielk1977 | 9f580ad | 2008-09-10 14:45:57 +0000 | [diff] [blame] | 1246 | |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1247 | /* It is often the case that the page we want is already in cache. |
| 1248 | ** If so, get it directly. This saves us from having to call |
| 1249 | ** pagerPagecount() to make sure pgno is within limits, which results |
| 1250 | ** in a measureable performance improvements. |
| 1251 | */ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 1252 | *ppPage = pPage = btreePageLookup(pBt, pgno); |
| 1253 | if( pPage ){ |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1254 | /* Page is already in cache */ |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1255 | rc = SQLITE_OK; |
| 1256 | }else{ |
| 1257 | /* Page not in cache. Acquire it. */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 1258 | if( pgno>pagerPagecount(pBt) ){ |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1259 | return SQLITE_CORRUPT_BKPT; |
| 1260 | } |
| 1261 | rc = sqlite3BtreeGetPage(pBt, pgno, ppPage, 0); |
| 1262 | if( rc ) return rc; |
| 1263 | pPage = *ppPage; |
| 1264 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1265 | if( !pPage->isInit ){ |
| 1266 | rc = sqlite3BtreeInitPage(pPage); |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1267 | } |
| 1268 | if( rc!=SQLITE_OK ){ |
| 1269 | releasePage(pPage); |
| 1270 | *ppPage = 0; |
| 1271 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1272 | return rc; |
| 1273 | } |
| 1274 | |
| 1275 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1276 | ** Release a MemPage. This should be called once for each prior |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1277 | ** call to sqlite3BtreeGetPage. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1278 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1279 | static void releasePage(MemPage *pPage){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1280 | if( pPage ){ |
drh | 30df009 | 2008-12-23 15:58:06 +0000 | [diff] [blame] | 1281 | assert( pPage->nOverflow==0 || sqlite3PagerPageRefcount(pPage->pDbPage)>1 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1282 | assert( pPage->aData ); |
| 1283 | assert( pPage->pBt ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1284 | assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); |
| 1285 | assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1286 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1287 | sqlite3PagerUnref(pPage->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1288 | } |
| 1289 | } |
| 1290 | |
| 1291 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1292 | ** During a rollback, when the pager reloads information into the cache |
| 1293 | ** so that the cache is restored to its original state at the start of |
| 1294 | ** the transaction, for each page restored this routine is called. |
| 1295 | ** |
| 1296 | ** This routine needs to reset the extra data section at the end of the |
| 1297 | ** page to agree with the restored data. |
| 1298 | */ |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1299 | static void pageReinit(DbPage *pData){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1300 | MemPage *pPage; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1301 | pPage = (MemPage *)sqlite3PagerGetExtra(pData); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1302 | if( pPage->isInit ){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1303 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1304 | pPage->isInit = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1305 | if( sqlite3PagerPageRefcount(pData)>0 ){ |
| 1306 | sqlite3BtreeInitPage(pPage); |
| 1307 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | /* |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1312 | ** Invoke the busy handler for a btree. |
| 1313 | */ |
danielk1977 | 1ceedd3 | 2008-11-19 10:22:33 +0000 | [diff] [blame] | 1314 | static int btreeInvokeBusyHandler(void *pArg){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1315 | BtShared *pBt = (BtShared*)pArg; |
| 1316 | assert( pBt->db ); |
| 1317 | assert( sqlite3_mutex_held(pBt->db->mutex) ); |
| 1318 | return sqlite3InvokeBusyHandler(&pBt->db->busyHandler); |
| 1319 | } |
| 1320 | |
| 1321 | /* |
drh | ad3e010 | 2004-09-03 23:32:18 +0000 | [diff] [blame] | 1322 | ** Open a database file. |
| 1323 | ** |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1324 | ** zFilename is the name of the database file. If zFilename is NULL |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1325 | ** a new database with a random name is created. This randomly named |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1326 | ** database file will be deleted when sqlite3BtreeClose() is called. |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1327 | ** If zFilename is ":memory:" then an in-memory database is created |
| 1328 | ** that is automatically destroyed when it is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1329 | */ |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1330 | int sqlite3BtreeOpen( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1331 | const char *zFilename, /* Name of the file containing the BTree database */ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1332 | sqlite3 *db, /* Associated database handle */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1333 | Btree **ppBtree, /* Pointer to new Btree object written here */ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1334 | int flags, /* Options */ |
| 1335 | int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1336 | ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1337 | sqlite3_vfs *pVfs; /* The VFS to use for this btree */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1338 | BtShared *pBt = 0; /* Shared part of btree structure */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1339 | Btree *p; /* Handle to return */ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1340 | int rc = SQLITE_OK; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1341 | u8 nReserve; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1342 | unsigned char zDbHeader[100]; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1343 | |
| 1344 | /* Set the variable isMemdb to true for an in-memory database, or |
| 1345 | ** false for a file-based database. This symbol is only required if |
| 1346 | ** either of the shared-data or autovacuum features are compiled |
| 1347 | ** into the library. |
| 1348 | */ |
| 1349 | #if !defined(SQLITE_OMIT_SHARED_CACHE) || !defined(SQLITE_OMIT_AUTOVACUUM) |
| 1350 | #ifdef SQLITE_OMIT_MEMORYDB |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 1351 | const int isMemdb = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1352 | #else |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 1353 | const int isMemdb = zFilename && !strcmp(zFilename, ":memory:"); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1354 | #endif |
| 1355 | #endif |
| 1356 | |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1357 | assert( db!=0 ); |
| 1358 | assert( sqlite3_mutex_held(db->mutex) ); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1359 | |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1360 | pVfs = db->pVfs; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1361 | p = sqlite3MallocZero(sizeof(Btree)); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1362 | if( !p ){ |
| 1363 | return SQLITE_NOMEM; |
| 1364 | } |
| 1365 | p->inTrans = TRANS_NONE; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1366 | p->db = db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1367 | |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 1368 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1369 | /* |
| 1370 | ** If this Btree is a candidate for shared cache, try to find an |
| 1371 | ** existing BtShared object that we can share with |
| 1372 | */ |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 1373 | if( isMemdb==0 |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1374 | && (db->flags & SQLITE_Vtab)==0 |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1375 | && zFilename && zFilename[0] |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1376 | ){ |
danielk1977 | 502b4e0 | 2008-09-02 14:07:24 +0000 | [diff] [blame] | 1377 | if( sqlite3GlobalConfig.sharedCacheEnabled ){ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 1378 | int nFullPathname = pVfs->mxPathname+1; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1379 | char *zFullPathname = sqlite3Malloc(nFullPathname); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1380 | sqlite3_mutex *mutexShared; |
| 1381 | p->sharable = 1; |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 1382 | db->flags |= SQLITE_SharedCache; |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1383 | if( !zFullPathname ){ |
| 1384 | sqlite3_free(p); |
| 1385 | return SQLITE_NOMEM; |
| 1386 | } |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 1387 | sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname); |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 1388 | mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1389 | sqlite3_mutex_enter(mutexShared); |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 1390 | for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1391 | assert( pBt->nRef>0 ); |
| 1392 | if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager)) |
| 1393 | && sqlite3PagerVfs(pBt->pPager)==pVfs ){ |
| 1394 | p->pBt = pBt; |
| 1395 | pBt->nRef++; |
| 1396 | break; |
| 1397 | } |
| 1398 | } |
| 1399 | sqlite3_mutex_leave(mutexShared); |
| 1400 | sqlite3_free(zFullPathname); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1401 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1402 | #ifdef SQLITE_DEBUG |
| 1403 | else{ |
| 1404 | /* In debug mode, we mark all persistent databases as sharable |
| 1405 | ** even when they are not. This exercises the locking code and |
| 1406 | ** gives more opportunity for asserts(sqlite3_mutex_held()) |
| 1407 | ** statements to find locking problems. |
| 1408 | */ |
| 1409 | p->sharable = 1; |
| 1410 | } |
| 1411 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1412 | } |
| 1413 | #endif |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1414 | if( pBt==0 ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1415 | /* |
| 1416 | ** The following asserts make sure that structures used by the btree are |
| 1417 | ** the right size. This is to guard against size changes that result |
| 1418 | ** when compiling on a different architecture. |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 1419 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1420 | assert( sizeof(i64)==8 || sizeof(i64)==4 ); |
| 1421 | assert( sizeof(u64)==8 || sizeof(u64)==4 ); |
| 1422 | assert( sizeof(u32)==4 ); |
| 1423 | assert( sizeof(u16)==2 ); |
| 1424 | assert( sizeof(Pgno)==4 ); |
| 1425 | |
| 1426 | pBt = sqlite3MallocZero( sizeof(*pBt) ); |
| 1427 | if( pBt==0 ){ |
| 1428 | rc = SQLITE_NOMEM; |
| 1429 | goto btree_open_out; |
| 1430 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1431 | rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename, |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1432 | EXTRA_SIZE, flags, vfsFlags); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1433 | if( rc==SQLITE_OK ){ |
| 1434 | rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader); |
| 1435 | } |
| 1436 | if( rc!=SQLITE_OK ){ |
| 1437 | goto btree_open_out; |
| 1438 | } |
danielk1977 | 1ceedd3 | 2008-11-19 10:22:33 +0000 | [diff] [blame] | 1439 | sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1440 | p->pBt = pBt; |
| 1441 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1442 | sqlite3PagerSetReiniter(pBt->pPager, pageReinit); |
| 1443 | pBt->pCursor = 0; |
| 1444 | pBt->pPage1 = 0; |
| 1445 | pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager); |
| 1446 | pBt->pageSize = get2byte(&zDbHeader[16]); |
| 1447 | if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE |
| 1448 | || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1449 | pBt->pageSize = 0; |
| 1450 | sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1451 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1452 | /* If the magic name ":memory:" will create an in-memory database, then |
| 1453 | ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if |
| 1454 | ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if |
| 1455 | ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a |
| 1456 | ** regular file-name. In this case the auto-vacuum applies as per normal. |
| 1457 | */ |
| 1458 | if( zFilename && !isMemdb ){ |
| 1459 | pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0); |
| 1460 | pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0); |
| 1461 | } |
| 1462 | #endif |
| 1463 | nReserve = 0; |
| 1464 | }else{ |
| 1465 | nReserve = zDbHeader[20]; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1466 | pBt->pageSizeFixed = 1; |
| 1467 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1468 | pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0); |
| 1469 | pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0); |
| 1470 | #endif |
| 1471 | } |
| 1472 | pBt->usableSize = pBt->pageSize - nReserve; |
| 1473 | assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1474 | sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1475 | |
| 1476 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
| 1477 | /* Add the new BtShared object to the linked list sharable BtShareds. |
| 1478 | */ |
| 1479 | if( p->sharable ){ |
| 1480 | sqlite3_mutex *mutexShared; |
| 1481 | pBt->nRef = 1; |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 1482 | mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 1483 | if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){ |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 1484 | pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST); |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 1485 | if( pBt->mutex==0 ){ |
| 1486 | rc = SQLITE_NOMEM; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1487 | db->mallocFailed = 0; |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 1488 | goto btree_open_out; |
| 1489 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1490 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1491 | sqlite3_mutex_enter(mutexShared); |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 1492 | pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList); |
| 1493 | GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1494 | sqlite3_mutex_leave(mutexShared); |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1495 | } |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1496 | #endif |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1497 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1498 | |
drh | cfed7bc | 2006-03-13 14:28:05 +0000 | [diff] [blame] | 1499 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1500 | /* If the new Btree uses a sharable pBtShared, then link the new |
| 1501 | ** Btree into the list of all sharable Btrees for the same connection. |
drh | abddb0c | 2007-08-20 13:14:28 +0000 | [diff] [blame] | 1502 | ** The list is kept in ascending order by pBt address. |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 1503 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1504 | if( p->sharable ){ |
| 1505 | int i; |
| 1506 | Btree *pSib; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1507 | for(i=0; i<db->nDb; i++){ |
| 1508 | if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1509 | while( pSib->pPrev ){ pSib = pSib->pPrev; } |
| 1510 | if( p->pBt<pSib->pBt ){ |
| 1511 | p->pNext = pSib; |
| 1512 | p->pPrev = 0; |
| 1513 | pSib->pPrev = p; |
| 1514 | }else{ |
drh | abddb0c | 2007-08-20 13:14:28 +0000 | [diff] [blame] | 1515 | while( pSib->pNext && pSib->pNext->pBt<p->pBt ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1516 | pSib = pSib->pNext; |
| 1517 | } |
| 1518 | p->pNext = pSib->pNext; |
| 1519 | p->pPrev = pSib; |
| 1520 | if( p->pNext ){ |
| 1521 | p->pNext->pPrev = p; |
| 1522 | } |
| 1523 | pSib->pNext = p; |
| 1524 | } |
| 1525 | break; |
| 1526 | } |
| 1527 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1528 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1529 | #endif |
| 1530 | *ppBtree = p; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1531 | |
| 1532 | btree_open_out: |
| 1533 | if( rc!=SQLITE_OK ){ |
| 1534 | if( pBt && pBt->pPager ){ |
| 1535 | sqlite3PagerClose(pBt->pPager); |
| 1536 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1537 | sqlite3_free(pBt); |
| 1538 | sqlite3_free(p); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1539 | *ppBtree = 0; |
| 1540 | } |
| 1541 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | /* |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1545 | ** Decrement the BtShared.nRef counter. When it reaches zero, |
| 1546 | ** remove the BtShared structure from the sharing list. Return |
| 1547 | ** true if the BtShared.nRef counter reaches zero and return |
| 1548 | ** false if it is still positive. |
| 1549 | */ |
| 1550 | static int removeFromSharingList(BtShared *pBt){ |
| 1551 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 1552 | sqlite3_mutex *pMaster; |
| 1553 | BtShared *pList; |
| 1554 | int removed = 0; |
| 1555 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1556 | assert( sqlite3_mutex_notheld(pBt->mutex) ); |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 1557 | pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1558 | sqlite3_mutex_enter(pMaster); |
| 1559 | pBt->nRef--; |
| 1560 | if( pBt->nRef<=0 ){ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 1561 | if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){ |
| 1562 | GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1563 | }else{ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 1564 | pList = GLOBAL(BtShared*,sqlite3SharedCacheList); |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 1565 | while( ALWAYS(pList) && pList->pNext!=pBt ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1566 | pList=pList->pNext; |
| 1567 | } |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 1568 | if( ALWAYS(pList) ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1569 | pList->pNext = pBt->pNext; |
| 1570 | } |
| 1571 | } |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 1572 | if( SQLITE_THREADSAFE ){ |
| 1573 | sqlite3_mutex_free(pBt->mutex); |
| 1574 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1575 | removed = 1; |
| 1576 | } |
| 1577 | sqlite3_mutex_leave(pMaster); |
| 1578 | return removed; |
| 1579 | #else |
| 1580 | return 1; |
| 1581 | #endif |
| 1582 | } |
| 1583 | |
| 1584 | /* |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1585 | ** Make sure pBt->pTmpSpace points to an allocation of |
| 1586 | ** MX_CELL_SIZE(pBt) bytes. |
| 1587 | */ |
| 1588 | static void allocateTempSpace(BtShared *pBt){ |
| 1589 | if( !pBt->pTmpSpace ){ |
| 1590 | pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize ); |
| 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | /* |
| 1595 | ** Free the pBt->pTmpSpace allocation |
| 1596 | */ |
| 1597 | static void freeTempSpace(BtShared *pBt){ |
| 1598 | sqlite3PageFree( pBt->pTmpSpace); |
| 1599 | pBt->pTmpSpace = 0; |
| 1600 | } |
| 1601 | |
| 1602 | /* |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1603 | ** Close an open database and invalidate all cursors. |
| 1604 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1605 | int sqlite3BtreeClose(Btree *p){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1606 | BtShared *pBt = p->pBt; |
| 1607 | BtCursor *pCur; |
| 1608 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1609 | /* Close all cursors opened via this handle. */ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1610 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1611 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1612 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1613 | pCur = pBt->pCursor; |
| 1614 | while( pCur ){ |
| 1615 | BtCursor *pTmp = pCur; |
| 1616 | pCur = pCur->pNext; |
| 1617 | if( pTmp->pBtree==p ){ |
| 1618 | sqlite3BtreeCloseCursor(pTmp); |
| 1619 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1620 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1621 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 1622 | /* Rollback any active transaction and free the handle structure. |
| 1623 | ** The call to sqlite3BtreeRollback() drops any table-locks held by |
| 1624 | ** this handle. |
| 1625 | */ |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 1626 | sqlite3BtreeRollback(p); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1627 | sqlite3BtreeLeave(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1628 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1629 | /* If there are still other outstanding references to the shared-btree |
| 1630 | ** structure, return now. The remainder of this procedure cleans |
| 1631 | ** up the shared-btree. |
| 1632 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1633 | assert( p->wantToLock==0 && p->locked==0 ); |
| 1634 | if( !p->sharable || removeFromSharingList(pBt) ){ |
| 1635 | /* The pBt is no longer on the sharing list, so we can access |
| 1636 | ** it without having to hold the mutex. |
| 1637 | ** |
| 1638 | ** Clean out and delete the BtShared object. |
| 1639 | */ |
| 1640 | assert( !pBt->pCursor ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1641 | sqlite3PagerClose(pBt->pPager); |
| 1642 | if( pBt->xFreeSchema && pBt->pSchema ){ |
| 1643 | pBt->xFreeSchema(pBt->pSchema); |
| 1644 | } |
| 1645 | sqlite3_free(pBt->pSchema); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1646 | freeTempSpace(pBt); |
drh | 65bbf29 | 2008-06-19 01:03:17 +0000 | [diff] [blame] | 1647 | sqlite3_free(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1648 | } |
| 1649 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1650 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | cab5ed7 | 2007-08-22 11:41:18 +0000 | [diff] [blame] | 1651 | assert( p->wantToLock==0 ); |
| 1652 | assert( p->locked==0 ); |
| 1653 | if( p->pPrev ) p->pPrev->pNext = p->pNext; |
| 1654 | if( p->pNext ) p->pNext->pPrev = p->pPrev; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1655 | #endif |
| 1656 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1657 | sqlite3_free(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1658 | return SQLITE_OK; |
| 1659 | } |
| 1660 | |
| 1661 | /* |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1662 | ** Change the limit on the number of pages allowed in the cache. |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 1663 | ** |
| 1664 | ** The maximum number of cache pages is set to the absolute |
| 1665 | ** value of mxPage. If mxPage is negative, the pager will |
| 1666 | ** operate asynchronously - it will not stop to do fsync()s |
| 1667 | ** to insure data is written to the disk surface before |
| 1668 | ** continuing. Transactions still work if synchronous is off, |
| 1669 | ** and the database cannot be corrupted if this program |
| 1670 | ** crashes. But if the operating system crashes or there is |
| 1671 | ** an abrupt power failure when synchronous is off, the database |
| 1672 | ** could be left in an inconsistent and unrecoverable state. |
| 1673 | ** Synchronous is on by default so database corruption is not |
| 1674 | ** normally a worry. |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1675 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1676 | int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){ |
| 1677 | BtShared *pBt = p->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1678 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1679 | sqlite3BtreeEnter(p); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1680 | sqlite3PagerSetCachesize(pBt->pPager, mxPage); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1681 | sqlite3BtreeLeave(p); |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1682 | return SQLITE_OK; |
| 1683 | } |
| 1684 | |
| 1685 | /* |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1686 | ** Change the way data is synced to disk in order to increase or decrease |
| 1687 | ** how well the database resists damage due to OS crashes and power |
| 1688 | ** failures. Level 1 is the same as asynchronous (no syncs() occur and |
| 1689 | ** there is a high probability of damage) Level 2 is the default. There |
| 1690 | ** is a very low but non-zero probability of damage. Level 3 reduces the |
| 1691 | ** probability of damage to near zero but with a write performance reduction. |
| 1692 | */ |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1693 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
drh | ac530b1 | 2006-02-11 01:25:50 +0000 | [diff] [blame] | 1694 | int sqlite3BtreeSetSafetyLevel(Btree *p, int level, int fullSync){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1695 | BtShared *pBt = p->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1696 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1697 | sqlite3BtreeEnter(p); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1698 | sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1699 | sqlite3BtreeLeave(p); |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1700 | return SQLITE_OK; |
| 1701 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1702 | #endif |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1703 | |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1704 | /* |
| 1705 | ** Return TRUE if the given btree is set to safety level 1. In other |
| 1706 | ** words, return TRUE if no sync() occurs on the disk files. |
| 1707 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1708 | int sqlite3BtreeSyncDisabled(Btree *p){ |
| 1709 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1710 | int rc; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1711 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1712 | sqlite3BtreeEnter(p); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 1713 | assert( pBt && pBt->pPager ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1714 | rc = sqlite3PagerNosync(pBt->pPager); |
| 1715 | sqlite3BtreeLeave(p); |
| 1716 | return rc; |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1719 | #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1720 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1721 | ** Change the default pages size and the number of reserved bytes per page. |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 1722 | ** |
| 1723 | ** The page size must be a power of 2 between 512 and 65536. If the page |
| 1724 | ** size supplied does not meet this constraint then the page size is not |
| 1725 | ** changed. |
| 1726 | ** |
| 1727 | ** Page sizes are constrained to be a power of two so that the region |
| 1728 | ** of the database file used for locking (beginning at PENDING_BYTE, |
| 1729 | ** the first byte past the 1GB boundary, 0x40000000) needs to occur |
| 1730 | ** at the beginning of a page. |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 1731 | ** |
| 1732 | ** If parameter nReserve is less than zero, then the number of reserved |
| 1733 | ** bytes per page is left unchanged. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1734 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1735 | int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1736 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1737 | BtShared *pBt = p->pBt; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1738 | assert( nReserve>=-1 && nReserve<=255 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1739 | sqlite3BtreeEnter(p); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1740 | if( pBt->pageSizeFixed ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1741 | sqlite3BtreeLeave(p); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1742 | return SQLITE_READONLY; |
| 1743 | } |
| 1744 | if( nReserve<0 ){ |
| 1745 | nReserve = pBt->pageSize - pBt->usableSize; |
| 1746 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1747 | assert( nReserve>=0 && nReserve<=255 ); |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 1748 | if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE && |
| 1749 | ((pageSize-1)&pageSize)==0 ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1750 | assert( (pageSize & 7)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1751 | assert( !pBt->pPage1 && !pBt->pCursor ); |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 1752 | pBt->pageSize = (u16)pageSize; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1753 | freeTempSpace(pBt); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1754 | rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1755 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1756 | pBt->usableSize = pBt->pageSize - (u16)nReserve; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1757 | sqlite3BtreeLeave(p); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1758 | return rc; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | /* |
| 1762 | ** Return the currently defined page size |
| 1763 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1764 | int sqlite3BtreeGetPageSize(Btree *p){ |
| 1765 | return p->pBt->pageSize; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1766 | } |
drh | 7f75122 | 2009-03-17 22:33:00 +0000 | [diff] [blame] | 1767 | |
| 1768 | /* |
| 1769 | ** Return the number of bytes of space at the end of every page that |
| 1770 | ** are intentually left unused. This is the "reserved" space that is |
| 1771 | ** sometimes used by extensions. |
| 1772 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1773 | int sqlite3BtreeGetReserve(Btree *p){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1774 | int n; |
| 1775 | sqlite3BtreeEnter(p); |
| 1776 | n = p->pBt->pageSize - p->pBt->usableSize; |
| 1777 | sqlite3BtreeLeave(p); |
| 1778 | return n; |
drh | 2011d5f | 2004-07-22 02:40:37 +0000 | [diff] [blame] | 1779 | } |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 1780 | |
| 1781 | /* |
| 1782 | ** Set the maximum page count for a database if mxPage is positive. |
| 1783 | ** No changes are made if mxPage is 0 or negative. |
| 1784 | ** Regardless of the value of mxPage, return the maximum page count. |
| 1785 | */ |
| 1786 | int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1787 | int n; |
| 1788 | sqlite3BtreeEnter(p); |
| 1789 | n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage); |
| 1790 | sqlite3BtreeLeave(p); |
| 1791 | return n; |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 1792 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1793 | #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1794 | |
| 1795 | /* |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1796 | ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum' |
| 1797 | ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it |
| 1798 | ** is disabled. The default value for the auto-vacuum property is |
| 1799 | ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro. |
| 1800 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1801 | int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1802 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1803 | return SQLITE_READONLY; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1804 | #else |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1805 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1806 | int rc = SQLITE_OK; |
drh | 076d466 | 2009-02-18 20:31:18 +0000 | [diff] [blame] | 1807 | u8 av = (u8)autoVacuum; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1808 | |
| 1809 | sqlite3BtreeEnter(p); |
drh | 076d466 | 2009-02-18 20:31:18 +0000 | [diff] [blame] | 1810 | if( pBt->pageSizeFixed && (av ?1:0)!=pBt->autoVacuum ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1811 | rc = SQLITE_READONLY; |
| 1812 | }else{ |
drh | 076d466 | 2009-02-18 20:31:18 +0000 | [diff] [blame] | 1813 | pBt->autoVacuum = av ?1:0; |
| 1814 | pBt->incrVacuum = av==2 ?1:0; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1815 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1816 | sqlite3BtreeLeave(p); |
| 1817 | return rc; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1818 | #endif |
| 1819 | } |
| 1820 | |
| 1821 | /* |
| 1822 | ** Return the value of the 'auto-vacuum' property. If auto-vacuum is |
| 1823 | ** enabled 1 is returned. Otherwise 0. |
| 1824 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1825 | int sqlite3BtreeGetAutoVacuum(Btree *p){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1826 | #ifdef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1827 | return BTREE_AUTOVACUUM_NONE; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1828 | #else |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1829 | int rc; |
| 1830 | sqlite3BtreeEnter(p); |
| 1831 | rc = ( |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1832 | (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE: |
| 1833 | (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL: |
| 1834 | BTREE_AUTOVACUUM_INCR |
| 1835 | ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1836 | sqlite3BtreeLeave(p); |
| 1837 | return rc; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1838 | #endif |
| 1839 | } |
| 1840 | |
| 1841 | |
| 1842 | /* |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1843 | ** Get a reference to pPage1 of the database file. This will |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1844 | ** also acquire a readlock on that file. |
| 1845 | ** |
| 1846 | ** SQLITE_OK is returned on success. If the file is not a |
| 1847 | ** well-formed database file, then SQLITE_CORRUPT is returned. |
| 1848 | ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 1849 | ** is returned if we run out of memory. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1850 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1851 | static int lockBtree(BtShared *pBt){ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1852 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1853 | MemPage *pPage1; |
danielk1977 | 93f7af9 | 2008-05-09 16:57:50 +0000 | [diff] [blame] | 1854 | int nPage; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1855 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1856 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1857 | if( pBt->pPage1 ) return SQLITE_OK; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1858 | rc = sqlite3BtreeGetPage(pBt, 1, &pPage1, 0); |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1859 | if( rc!=SQLITE_OK ) return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1860 | |
| 1861 | /* Do some checking to help insure the file we opened really is |
| 1862 | ** a valid database file. |
| 1863 | */ |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 1864 | rc = sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 1865 | if( rc!=SQLITE_OK ){ |
danielk1977 | 93f7af9 | 2008-05-09 16:57:50 +0000 | [diff] [blame] | 1866 | goto page1_init_failed; |
| 1867 | }else if( nPage>0 ){ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1868 | int pageSize; |
| 1869 | int usableSize; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1870 | u8 *page1 = pPage1->aData; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 1871 | rc = SQLITE_NOTADB; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1872 | if( memcmp(page1, zMagicHeader, 16)!=0 ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1873 | goto page1_init_failed; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1874 | } |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 1875 | if( page1[18]>1 ){ |
| 1876 | pBt->readOnly = 1; |
| 1877 | } |
| 1878 | if( page1[19]>1 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1879 | goto page1_init_failed; |
| 1880 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1881 | |
| 1882 | /* The maximum embedded fraction must be exactly 25%. And the minimum |
| 1883 | ** embedded fraction must be 12.5% for both leaf-data and non-leaf-data. |
| 1884 | ** The original design allowed these amounts to vary, but as of |
| 1885 | ** version 3.6.0, we require them to be fixed. |
| 1886 | */ |
| 1887 | if( memcmp(&page1[21], "\100\040\040",3)!=0 ){ |
| 1888 | goto page1_init_failed; |
| 1889 | } |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1890 | pageSize = get2byte(&page1[16]); |
drh | 7dc385e | 2007-09-06 23:39:36 +0000 | [diff] [blame] | 1891 | if( ((pageSize-1)&pageSize)!=0 || pageSize<512 || |
| 1892 | (SQLITE_MAX_PAGE_SIZE<32768 && pageSize>SQLITE_MAX_PAGE_SIZE) |
| 1893 | ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1894 | goto page1_init_failed; |
| 1895 | } |
| 1896 | assert( (pageSize & 7)==0 ); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1897 | usableSize = pageSize - page1[20]; |
| 1898 | if( pageSize!=pBt->pageSize ){ |
| 1899 | /* After reading the first page of the database assuming a page size |
| 1900 | ** of BtShared.pageSize, we have discovered that the page-size is |
| 1901 | ** actually pageSize. Unlock the database, leave pBt->pPage1 at |
| 1902 | ** zero and return SQLITE_OK. The caller will call this function |
| 1903 | ** again with the correct page-size. |
| 1904 | */ |
| 1905 | releasePage(pPage1); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1906 | pBt->usableSize = (u16)usableSize; |
| 1907 | pBt->pageSize = (u16)pageSize; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1908 | freeTempSpace(pBt); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1909 | sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
| 1910 | return SQLITE_OK; |
| 1911 | } |
| 1912 | if( usableSize<500 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1913 | goto page1_init_failed; |
| 1914 | } |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 1915 | pBt->pageSize = (u16)pageSize; |
| 1916 | pBt->usableSize = (u16)usableSize; |
drh | 057cd3a | 2005-02-15 16:23:02 +0000 | [diff] [blame] | 1917 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1918 | pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0); |
danielk1977 | 27b1f95 | 2007-06-25 08:16:58 +0000 | [diff] [blame] | 1919 | pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0); |
drh | 057cd3a | 2005-02-15 16:23:02 +0000 | [diff] [blame] | 1920 | #endif |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1921 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1922 | |
| 1923 | /* maxLocal is the maximum amount of payload to store locally for |
| 1924 | ** a cell. Make sure it is small enough so that at least minFanout |
| 1925 | ** cells can will fit on one page. We assume a 10-byte page header. |
| 1926 | ** Besides the payload, the cell must store: |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1927 | ** 2-byte pointer to the cell |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1928 | ** 4-byte child pointer |
| 1929 | ** 9-byte nKey value |
| 1930 | ** 4-byte nData value |
| 1931 | ** 4-byte overflow page pointer |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1932 | ** So a cell consists of a 2-byte poiner, a header which is as much as |
| 1933 | ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow |
| 1934 | ** page pointer. |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1935 | */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1936 | pBt->maxLocal = (pBt->usableSize-12)*64/255 - 23; |
| 1937 | pBt->minLocal = (pBt->usableSize-12)*32/255 - 23; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1938 | pBt->maxLeaf = pBt->usableSize - 35; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1939 | pBt->minLeaf = (pBt->usableSize-12)*32/255 - 23; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1940 | assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1941 | pBt->pPage1 = pPage1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1942 | return SQLITE_OK; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1943 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1944 | page1_init_failed: |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1945 | releasePage(pPage1); |
| 1946 | pBt->pPage1 = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1947 | return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1948 | } |
| 1949 | |
| 1950 | /* |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1951 | ** This routine works like lockBtree() except that it also invokes the |
| 1952 | ** busy callback if there is lock contention. |
| 1953 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1954 | static int lockBtreeWithRetry(Btree *pRef){ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1955 | int rc = SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1956 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1957 | assert( sqlite3BtreeHoldsMutex(pRef) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1958 | if( pRef->inTrans==TRANS_NONE ){ |
| 1959 | u8 inTransaction = pRef->pBt->inTransaction; |
| 1960 | btreeIntegrity(pRef); |
| 1961 | rc = sqlite3BtreeBeginTrans(pRef, 0); |
| 1962 | pRef->pBt->inTransaction = inTransaction; |
| 1963 | pRef->inTrans = TRANS_NONE; |
| 1964 | if( rc==SQLITE_OK ){ |
| 1965 | pRef->pBt->nTransaction--; |
| 1966 | } |
| 1967 | btreeIntegrity(pRef); |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1968 | } |
| 1969 | return rc; |
| 1970 | } |
| 1971 | |
| 1972 | |
| 1973 | /* |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1974 | ** If there are no outstanding cursors and we are not in the middle |
| 1975 | ** of a transaction but there is a read lock on the database, then |
| 1976 | ** this routine unrefs the first page of the database file which |
| 1977 | ** has the effect of releasing the read lock. |
| 1978 | ** |
| 1979 | ** If there are any outstanding cursors, this routine is a no-op. |
| 1980 | ** |
| 1981 | ** If there is a transaction in progress, this routine is a no-op. |
| 1982 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1983 | static void unlockBtreeIfUnused(BtShared *pBt){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1984 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1985 | if( pBt->inTransaction==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1986 | if( sqlite3PagerRefcount(pBt->pPager)>=1 ){ |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 1987 | assert( pBt->pPage1->aData ); |
| 1988 | #if 0 |
drh | 24c9a2e | 2007-01-05 02:00:47 +0000 | [diff] [blame] | 1989 | if( pBt->pPage1->aData==0 ){ |
| 1990 | MemPage *pPage = pBt->pPage1; |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1991 | pPage->aData = sqlite3PagerGetData(pPage->pDbPage); |
drh | 24c9a2e | 2007-01-05 02:00:47 +0000 | [diff] [blame] | 1992 | pPage->pBt = pBt; |
| 1993 | pPage->pgno = 1; |
| 1994 | } |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 1995 | #endif |
drh | 24c9a2e | 2007-01-05 02:00:47 +0000 | [diff] [blame] | 1996 | releasePage(pBt->pPage1); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 1997 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1998 | pBt->pPage1 = 0; |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1999 | } |
| 2000 | } |
| 2001 | |
| 2002 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 2003 | ** Create a new database by initializing the first page of the |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2004 | ** file. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2005 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2006 | static int newDatabase(BtShared *pBt){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 2007 | MemPage *pP1; |
| 2008 | unsigned char *data; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2009 | int rc; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2010 | int nPage; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2011 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2012 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2013 | rc = sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 2014 | if( rc!=SQLITE_OK || nPage>0 ){ |
| 2015 | return rc; |
| 2016 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2017 | pP1 = pBt->pPage1; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 2018 | assert( pP1!=0 ); |
| 2019 | data = pP1->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2020 | rc = sqlite3PagerWrite(pP1->pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2021 | if( rc ) return rc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 2022 | memcpy(data, zMagicHeader, sizeof(zMagicHeader)); |
| 2023 | assert( sizeof(zMagicHeader)==16 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2024 | put2byte(&data[16], pBt->pageSize); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 2025 | data[18] = 1; |
| 2026 | data[19] = 1; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 2027 | assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize); |
| 2028 | data[20] = (u8)(pBt->pageSize - pBt->usableSize); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 2029 | data[21] = 64; |
| 2030 | data[22] = 32; |
| 2031 | data[23] = 32; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2032 | memset(&data[24], 0, 100-24); |
drh | e6c4381 | 2004-05-14 12:17:46 +0000 | [diff] [blame] | 2033 | zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA ); |
drh | f2a611c | 2004-09-05 00:33:43 +0000 | [diff] [blame] | 2034 | pBt->pageSizeFixed = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2035 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2036 | assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 ); |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 2037 | assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 ); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2038 | put4byte(&data[36 + 4*4], pBt->autoVacuum); |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 2039 | put4byte(&data[36 + 7*4], pBt->incrVacuum); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2040 | #endif |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2041 | return SQLITE_OK; |
| 2042 | } |
| 2043 | |
| 2044 | /* |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2045 | ** Attempt to start a new transaction. A write-transaction |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 2046 | ** is started if the second argument is nonzero, otherwise a read- |
| 2047 | ** transaction. If the second argument is 2 or more and exclusive |
| 2048 | ** transaction is started, meaning that no other process is allowed |
| 2049 | ** to access the database. A preexisting transaction may not be |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2050 | ** upgraded to exclusive by calling this routine a second time - the |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 2051 | ** exclusivity flag only works for a new transaction. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2052 | ** |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2053 | ** A write-transaction must be started before attempting any |
| 2054 | ** changes to the database. None of the following routines |
| 2055 | ** will work unless a transaction is started first: |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2056 | ** |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 2057 | ** sqlite3BtreeCreateTable() |
| 2058 | ** sqlite3BtreeCreateIndex() |
| 2059 | ** sqlite3BtreeClearTable() |
| 2060 | ** sqlite3BtreeDropTable() |
| 2061 | ** sqlite3BtreeInsert() |
| 2062 | ** sqlite3BtreeDelete() |
| 2063 | ** sqlite3BtreeUpdateMeta() |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 2064 | ** |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2065 | ** If an initial attempt to acquire the lock fails because of lock contention |
| 2066 | ** and the database was previously unlocked, then invoke the busy handler |
| 2067 | ** if there is one. But if there was previously a read-lock, do not |
| 2068 | ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is |
| 2069 | ** returned when there is already a read-lock in order to avoid a deadlock. |
| 2070 | ** |
| 2071 | ** Suppose there are two processes A and B. A has a read lock and B has |
| 2072 | ** a reserved lock. B tries to promote to exclusive but is blocked because |
| 2073 | ** of A's read lock. A tries to promote to reserved but is blocked by B. |
| 2074 | ** One or the other of the two processes must give way or there can be |
| 2075 | ** no progress. By returning SQLITE_BUSY and not invoking the busy callback |
| 2076 | ** when A already has a read lock, we encourage A to give up and let B |
| 2077 | ** proceed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2078 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2079 | int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2080 | sqlite3 *pBlock = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2081 | BtShared *pBt = p->pBt; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2082 | int rc = SQLITE_OK; |
| 2083 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2084 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2085 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2086 | btreeIntegrity(p); |
| 2087 | |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2088 | /* If the btree is already in a write-transaction, or it |
| 2089 | ** is already in a read-transaction and a read-transaction |
| 2090 | ** is requested, this is a no-op. |
| 2091 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2092 | if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2093 | goto trans_begun; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2094 | } |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2095 | |
| 2096 | /* Write transactions are not possible on a read-only database */ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2097 | if( pBt->readOnly && wrflag ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2098 | rc = SQLITE_READONLY; |
| 2099 | goto trans_begun; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2100 | } |
| 2101 | |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2102 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2103 | /* If another database handle has already opened a write transaction |
| 2104 | ** on this shared-btree structure and a second write transaction is |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2105 | ** requested, return SQLITE_LOCKED. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2106 | */ |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2107 | if( (wrflag && pBt->inTransaction==TRANS_WRITE) || pBt->isPending ){ |
| 2108 | pBlock = pBt->pWriter->db; |
| 2109 | }else if( wrflag>1 ){ |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 2110 | BtLock *pIter; |
| 2111 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 2112 | if( pIter->pBtree!=p ){ |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2113 | pBlock = pIter->pBtree->db; |
| 2114 | break; |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 2115 | } |
| 2116 | } |
| 2117 | } |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2118 | if( pBlock ){ |
| 2119 | sqlite3ConnectionBlocked(p->db, pBlock); |
| 2120 | rc = SQLITE_LOCKED_SHAREDCACHE; |
| 2121 | goto trans_begun; |
| 2122 | } |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 2123 | #endif |
| 2124 | |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2125 | do { |
drh | 8a9c17f | 2008-05-02 14:23:54 +0000 | [diff] [blame] | 2126 | if( pBt->pPage1==0 ){ |
| 2127 | do{ |
| 2128 | rc = lockBtree(pBt); |
| 2129 | }while( pBt->pPage1==0 && rc==SQLITE_OK ); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2130 | } |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 2131 | |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2132 | if( rc==SQLITE_OK && wrflag ){ |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 2133 | if( pBt->readOnly ){ |
| 2134 | rc = SQLITE_READONLY; |
| 2135 | }else{ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 2136 | rc = sqlite3PagerBegin(pBt->pPager, wrflag>1); |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 2137 | if( rc==SQLITE_OK ){ |
| 2138 | rc = newDatabase(pBt); |
| 2139 | } |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2140 | } |
| 2141 | } |
| 2142 | |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame^] | 2143 | if( rc!=SQLITE_OK ){ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2144 | unlockBtreeIfUnused(pBt); |
| 2145 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2146 | }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE && |
danielk1977 | 1ceedd3 | 2008-11-19 10:22:33 +0000 | [diff] [blame] | 2147 | btreeInvokeBusyHandler(pBt) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2148 | |
| 2149 | if( rc==SQLITE_OK ){ |
| 2150 | if( p->inTrans==TRANS_NONE ){ |
| 2151 | pBt->nTransaction++; |
| 2152 | } |
| 2153 | p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ); |
| 2154 | if( p->inTrans>pBt->inTransaction ){ |
| 2155 | pBt->inTransaction = p->inTrans; |
| 2156 | } |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 2157 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2158 | if( wrflag ){ |
| 2159 | assert( !pBt->pWriter ); |
| 2160 | pBt->pWriter = p; |
| 2161 | pBt->isExclusive = (wrflag>1); |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 2162 | } |
| 2163 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2164 | } |
| 2165 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2166 | |
| 2167 | trans_begun: |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2168 | if( rc==SQLITE_OK && wrflag ){ |
danielk1977 | 12dd549 | 2008-12-18 15:45:07 +0000 | [diff] [blame] | 2169 | /* This call makes sure that the pager has the correct number of |
| 2170 | ** open savepoints. If the second parameter is greater than 0 and |
| 2171 | ** the sub-journal is not already open, then it will be opened here. |
| 2172 | */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2173 | rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint); |
| 2174 | } |
danielk1977 | 12dd549 | 2008-12-18 15:45:07 +0000 | [diff] [blame] | 2175 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2176 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2177 | sqlite3BtreeLeave(p); |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 2178 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2179 | } |
| 2180 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2181 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2182 | |
| 2183 | /* |
| 2184 | ** Set the pointer-map entries for all children of page pPage. Also, if |
| 2185 | ** pPage contains cells that point to overflow pages, set the pointer |
| 2186 | ** map entries for the overflow pages as well. |
| 2187 | */ |
| 2188 | static int setChildPtrmaps(MemPage *pPage){ |
| 2189 | int i; /* Counter variable */ |
| 2190 | int nCell; /* Number of cells in page pPage */ |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 2191 | int rc; /* Return code */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2192 | BtShared *pBt = pPage->pBt; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 2193 | u8 isInitOrig = pPage->isInit; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2194 | Pgno pgno = pPage->pgno; |
| 2195 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2196 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2197 | rc = sqlite3BtreeInitPage(pPage); |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 2198 | if( rc!=SQLITE_OK ){ |
| 2199 | goto set_child_ptrmaps_out; |
| 2200 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2201 | nCell = pPage->nCell; |
| 2202 | |
| 2203 | for(i=0; i<nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2204 | u8 *pCell = findCell(pPage, i); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2205 | |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 2206 | rc = ptrmapPutOvflPtr(pPage, pCell); |
| 2207 | if( rc!=SQLITE_OK ){ |
| 2208 | goto set_child_ptrmaps_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2209 | } |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 2210 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2211 | if( !pPage->leaf ){ |
| 2212 | Pgno childPgno = get4byte(pCell); |
| 2213 | rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno); |
danielk1977 | 00a696d | 2008-09-29 16:41:31 +0000 | [diff] [blame] | 2214 | if( rc!=SQLITE_OK ) goto set_child_ptrmaps_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2215 | } |
| 2216 | } |
| 2217 | |
| 2218 | if( !pPage->leaf ){ |
| 2219 | Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 2220 | rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno); |
| 2221 | } |
| 2222 | |
| 2223 | set_child_ptrmaps_out: |
| 2224 | pPage->isInit = isInitOrig; |
| 2225 | return rc; |
| 2226 | } |
| 2227 | |
| 2228 | /* |
| 2229 | ** Somewhere on pPage, which is guarenteed to be a btree page, not an overflow |
| 2230 | ** page, is a pointer to page iFrom. Modify this pointer so that it points to |
| 2231 | ** iTo. Parameter eType describes the type of pointer to be modified, as |
| 2232 | ** follows: |
| 2233 | ** |
| 2234 | ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child |
| 2235 | ** page of pPage. |
| 2236 | ** |
| 2237 | ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow |
| 2238 | ** page pointed to by one of the cells on pPage. |
| 2239 | ** |
| 2240 | ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next |
| 2241 | ** overflow page in the list. |
| 2242 | */ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2243 | static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2244 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 2245 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2246 | if( eType==PTRMAP_OVERFLOW2 ){ |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 2247 | /* 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] | 2248 | if( get4byte(pPage->aData)!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2249 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2250 | } |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 2251 | put4byte(pPage->aData, iTo); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2252 | }else{ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 2253 | u8 isInitOrig = pPage->isInit; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2254 | int i; |
| 2255 | int nCell; |
| 2256 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2257 | sqlite3BtreeInitPage(pPage); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2258 | nCell = pPage->nCell; |
| 2259 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2260 | for(i=0; i<nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2261 | u8 *pCell = findCell(pPage, i); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2262 | if( eType==PTRMAP_OVERFLOW1 ){ |
| 2263 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2264 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2265 | if( info.iOverflow ){ |
| 2266 | if( iFrom==get4byte(&pCell[info.iOverflow]) ){ |
| 2267 | put4byte(&pCell[info.iOverflow], iTo); |
| 2268 | break; |
| 2269 | } |
| 2270 | } |
| 2271 | }else{ |
| 2272 | if( get4byte(pCell)==iFrom ){ |
| 2273 | put4byte(pCell, iTo); |
| 2274 | break; |
| 2275 | } |
| 2276 | } |
| 2277 | } |
| 2278 | |
| 2279 | if( i==nCell ){ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2280 | if( eType!=PTRMAP_BTREE || |
| 2281 | get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2282 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2283 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2284 | put4byte(&pPage->aData[pPage->hdrOffset+8], iTo); |
| 2285 | } |
| 2286 | |
| 2287 | pPage->isInit = isInitOrig; |
| 2288 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2289 | return SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2290 | } |
| 2291 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2292 | |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2293 | /* |
| 2294 | ** Move the open database page pDbPage to location iFreePage in the |
| 2295 | ** database. The pDbPage reference remains valid. |
| 2296 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2297 | static int relocatePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2298 | BtShared *pBt, /* Btree */ |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2299 | MemPage *pDbPage, /* Open page to move */ |
| 2300 | u8 eType, /* Pointer map 'type' entry for pDbPage */ |
| 2301 | Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */ |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 2302 | Pgno iFreePage, /* The location to move pDbPage to */ |
| 2303 | int isCommit |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2304 | ){ |
| 2305 | MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */ |
| 2306 | Pgno iDbPage = pDbPage->pgno; |
| 2307 | Pager *pPager = pBt->pPager; |
| 2308 | int rc; |
| 2309 | |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2310 | assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || |
| 2311 | eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2312 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 2313 | assert( pDbPage->pBt==pBt ); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2314 | |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 2315 | /* Move page iDbPage from its current location to page number iFreePage */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2316 | TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", |
| 2317 | iDbPage, iFreePage, iPtrPage, eType)); |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 2318 | rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2319 | if( rc!=SQLITE_OK ){ |
| 2320 | return rc; |
| 2321 | } |
| 2322 | pDbPage->pgno = iFreePage; |
| 2323 | |
| 2324 | /* If pDbPage was a btree-page, then it may have child pages and/or cells |
| 2325 | ** that point to overflow pages. The pointer map entries for all these |
| 2326 | ** pages need to be changed. |
| 2327 | ** |
| 2328 | ** If pDbPage is an overflow page, then the first 4 bytes may store a |
| 2329 | ** pointer to a subsequent overflow page. If this is the case, then |
| 2330 | ** the pointer map needs to be updated for the subsequent overflow page. |
| 2331 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2332 | if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2333 | rc = setChildPtrmaps(pDbPage); |
| 2334 | if( rc!=SQLITE_OK ){ |
| 2335 | return rc; |
| 2336 | } |
| 2337 | }else{ |
| 2338 | Pgno nextOvfl = get4byte(pDbPage->aData); |
| 2339 | if( nextOvfl!=0 ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2340 | rc = ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage); |
| 2341 | if( rc!=SQLITE_OK ){ |
| 2342 | return rc; |
| 2343 | } |
| 2344 | } |
| 2345 | } |
| 2346 | |
| 2347 | /* Fix the database pointer on page iPtrPage that pointed at iDbPage so |
| 2348 | ** that it points at iFreePage. Also fix the pointer map entry for |
| 2349 | ** iPtrPage. |
| 2350 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2351 | if( eType!=PTRMAP_ROOTPAGE ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2352 | rc = sqlite3BtreeGetPage(pBt, iPtrPage, &pPtrPage, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2353 | if( rc!=SQLITE_OK ){ |
| 2354 | return rc; |
| 2355 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2356 | rc = sqlite3PagerWrite(pPtrPage->pDbPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2357 | if( rc!=SQLITE_OK ){ |
| 2358 | releasePage(pPtrPage); |
| 2359 | return rc; |
| 2360 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2361 | rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2362 | releasePage(pPtrPage); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2363 | if( rc==SQLITE_OK ){ |
| 2364 | rc = ptrmapPut(pBt, iFreePage, eType, iPtrPage); |
| 2365 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2366 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2367 | return rc; |
| 2368 | } |
| 2369 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2370 | /* Forward declaration required by incrVacuumStep(). */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2371 | static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2372 | |
| 2373 | /* |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2374 | ** Perform a single step of an incremental-vacuum. If successful, |
| 2375 | ** return SQLITE_OK. If there is no work to do (and therefore no |
| 2376 | ** point in calling this function again), return SQLITE_DONE. |
| 2377 | ** |
| 2378 | ** More specificly, this function attempts to re-organize the |
| 2379 | ** database so that the last page of the file currently in use |
| 2380 | ** is no longer in use. |
| 2381 | ** |
| 2382 | ** If the nFin parameter is non-zero, the implementation assumes |
| 2383 | ** that the caller will keep calling incrVacuumStep() until |
| 2384 | ** it returns SQLITE_DONE or an error, and that nFin is the |
| 2385 | ** number of pages the database file will contain after this |
| 2386 | ** process is complete. |
| 2387 | */ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2388 | static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2389 | Pgno nFreeList; /* Number of pages still on the free-list */ |
| 2390 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2391 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2392 | |
| 2393 | if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){ |
| 2394 | int rc; |
| 2395 | u8 eType; |
| 2396 | Pgno iPtrPage; |
| 2397 | |
| 2398 | nFreeList = get4byte(&pBt->pPage1->aData[36]); |
| 2399 | if( nFreeList==0 || nFin==iLastPg ){ |
| 2400 | return SQLITE_DONE; |
| 2401 | } |
| 2402 | |
| 2403 | rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage); |
| 2404 | if( rc!=SQLITE_OK ){ |
| 2405 | return rc; |
| 2406 | } |
| 2407 | if( eType==PTRMAP_ROOTPAGE ){ |
| 2408 | return SQLITE_CORRUPT_BKPT; |
| 2409 | } |
| 2410 | |
| 2411 | if( eType==PTRMAP_FREEPAGE ){ |
| 2412 | if( nFin==0 ){ |
| 2413 | /* Remove the page from the files free-list. This is not required |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 2414 | ** if nFin is non-zero. In that case, the free-list will be |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2415 | ** truncated to zero after this function returns, so it doesn't |
| 2416 | ** matter if it still contains some garbage entries. |
| 2417 | */ |
| 2418 | Pgno iFreePg; |
| 2419 | MemPage *pFreePg; |
| 2420 | rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1); |
| 2421 | if( rc!=SQLITE_OK ){ |
| 2422 | return rc; |
| 2423 | } |
| 2424 | assert( iFreePg==iLastPg ); |
| 2425 | releasePage(pFreePg); |
| 2426 | } |
| 2427 | } else { |
| 2428 | Pgno iFreePg; /* Index of free page to move pLastPg to */ |
| 2429 | MemPage *pLastPg; |
| 2430 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2431 | rc = sqlite3BtreeGetPage(pBt, iLastPg, &pLastPg, 0); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2432 | if( rc!=SQLITE_OK ){ |
| 2433 | return rc; |
| 2434 | } |
| 2435 | |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 2436 | /* If nFin is zero, this loop runs exactly once and page pLastPg |
| 2437 | ** is swapped with the first free page pulled off the free list. |
| 2438 | ** |
| 2439 | ** On the other hand, if nFin is greater than zero, then keep |
| 2440 | ** looping until a free-page located within the first nFin pages |
| 2441 | ** of the file is found. |
| 2442 | */ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2443 | do { |
| 2444 | MemPage *pFreePg; |
| 2445 | rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0); |
| 2446 | if( rc!=SQLITE_OK ){ |
| 2447 | releasePage(pLastPg); |
| 2448 | return rc; |
| 2449 | } |
| 2450 | releasePage(pFreePg); |
| 2451 | }while( nFin!=0 && iFreePg>nFin ); |
| 2452 | assert( iFreePg<iLastPg ); |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 2453 | |
| 2454 | rc = sqlite3PagerWrite(pLastPg->pDbPage); |
danielk1977 | 662278e | 2007-11-05 15:30:12 +0000 | [diff] [blame] | 2455 | if( rc==SQLITE_OK ){ |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 2456 | rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, nFin!=0); |
danielk1977 | 662278e | 2007-11-05 15:30:12 +0000 | [diff] [blame] | 2457 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2458 | releasePage(pLastPg); |
| 2459 | if( rc!=SQLITE_OK ){ |
| 2460 | return rc; |
danielk1977 | 662278e | 2007-11-05 15:30:12 +0000 | [diff] [blame] | 2461 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2462 | } |
| 2463 | } |
| 2464 | |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2465 | if( nFin==0 ){ |
| 2466 | iLastPg--; |
| 2467 | while( iLastPg==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, iLastPg) ){ |
| 2468 | iLastPg--; |
| 2469 | } |
| 2470 | sqlite3PagerTruncateImage(pBt->pPager, iLastPg); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2471 | } |
| 2472 | return SQLITE_OK; |
| 2473 | } |
| 2474 | |
| 2475 | /* |
| 2476 | ** A write-transaction must be opened before calling this function. |
| 2477 | ** It performs a single unit of work towards an incremental vacuum. |
| 2478 | ** |
| 2479 | ** If the incremental vacuum is finished after this function has run, |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 2480 | ** SQLITE_DONE is returned. If it is not finished, but no error occurred, |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2481 | ** SQLITE_OK is returned. Otherwise an SQLite error code. |
| 2482 | */ |
| 2483 | int sqlite3BtreeIncrVacuum(Btree *p){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2484 | int rc; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2485 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2486 | |
| 2487 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2488 | pBt->db = p->db; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2489 | assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE ); |
| 2490 | if( !pBt->autoVacuum ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2491 | rc = SQLITE_DONE; |
| 2492 | }else{ |
| 2493 | invalidateAllOverflowCache(pBt); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 2494 | rc = incrVacuumStep(pBt, 0, pagerPagecount(pBt)); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2495 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2496 | sqlite3BtreeLeave(p); |
| 2497 | return rc; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2498 | } |
| 2499 | |
| 2500 | /* |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2501 | ** This routine is called prior to sqlite3PagerCommit when a transaction |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2502 | ** is commited for an auto-vacuum database. |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 2503 | ** |
| 2504 | ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages |
| 2505 | ** the database file should be truncated to during the commit process. |
| 2506 | ** i.e. the database has been reorganized so that only the first *pnTrunc |
| 2507 | ** pages are in use. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2508 | */ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2509 | static int autoVacuumCommit(BtShared *pBt){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2510 | int rc = SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2511 | Pager *pPager = pBt->pPager; |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 2512 | VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2513 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2514 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 2515 | invalidateAllOverflowCache(pBt); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2516 | assert(pBt->autoVacuum); |
| 2517 | if( !pBt->incrVacuum ){ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2518 | Pgno nFin; |
| 2519 | Pgno nFree; |
| 2520 | Pgno nPtrmap; |
| 2521 | Pgno iFree; |
| 2522 | const int pgsz = pBt->pageSize; |
| 2523 | Pgno nOrig = pagerPagecount(pBt); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2524 | |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2525 | if( PTRMAP_ISPAGE(pBt, nOrig) ){ |
| 2526 | return SQLITE_CORRUPT_BKPT; |
| 2527 | } |
| 2528 | if( nOrig==PENDING_BYTE_PAGE(pBt) ){ |
| 2529 | nOrig--; |
| 2530 | } |
| 2531 | nFree = get4byte(&pBt->pPage1->aData[36]); |
| 2532 | nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+pgsz/5)/(pgsz/5); |
| 2533 | nFin = nOrig - nFree - nPtrmap; |
| 2534 | if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<=PENDING_BYTE_PAGE(pBt) ){ |
| 2535 | nFin--; |
| 2536 | } |
| 2537 | while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){ |
| 2538 | nFin--; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2539 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2540 | |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2541 | for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){ |
| 2542 | rc = incrVacuumStep(pBt, nFin, iFree); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2543 | } |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2544 | if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2545 | rc = SQLITE_OK; |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2546 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 2547 | put4byte(&pBt->pPage1->aData[32], 0); |
| 2548 | put4byte(&pBt->pPage1->aData[36], 0); |
| 2549 | sqlite3PagerTruncateImage(pBt->pPager, nFin); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2550 | } |
| 2551 | if( rc!=SQLITE_OK ){ |
| 2552 | sqlite3PagerRollback(pPager); |
| 2553 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2554 | } |
| 2555 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2556 | assert( nRef==sqlite3PagerRefcount(pPager) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2557 | return rc; |
| 2558 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2559 | |
shane | 831c329 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 2560 | #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2561 | |
| 2562 | /* |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2563 | ** This routine does the first phase of a two-phase commit. This routine |
| 2564 | ** causes a rollback journal to be created (if it does not already exist) |
| 2565 | ** and populated with enough information so that if a power loss occurs |
| 2566 | ** the database can be restored to its original state by playing back |
| 2567 | ** the journal. Then the contents of the journal are flushed out to |
| 2568 | ** the disk. After the journal is safely on oxide, the changes to the |
| 2569 | ** database are written into the database file and flushed to oxide. |
| 2570 | ** At the end of this call, the rollback journal still exists on the |
| 2571 | ** disk and we are still holding all locks, so the transaction has not |
| 2572 | ** committed. See sqlite3BtreeCommit() for the second phase of the |
| 2573 | ** commit process. |
| 2574 | ** |
| 2575 | ** This call is a no-op if no write-transaction is currently active on pBt. |
| 2576 | ** |
| 2577 | ** Otherwise, sync the database file for the btree pBt. zMaster points to |
| 2578 | ** the name of a master journal file that should be written into the |
| 2579 | ** individual journal file, or is NULL, indicating no master journal file |
| 2580 | ** (single database transaction). |
| 2581 | ** |
| 2582 | ** When this is called, the master journal should already have been |
| 2583 | ** created, populated with this journal pointer and synced to disk. |
| 2584 | ** |
| 2585 | ** Once this is routine has returned, the only thing required to commit |
| 2586 | ** the write-transaction for this database file is to delete the journal. |
| 2587 | */ |
| 2588 | int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){ |
| 2589 | int rc = SQLITE_OK; |
| 2590 | if( p->inTrans==TRANS_WRITE ){ |
| 2591 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2592 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2593 | pBt->db = p->db; |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2594 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2595 | if( pBt->autoVacuum ){ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2596 | rc = autoVacuumCommit(pBt); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2597 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2598 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2599 | return rc; |
| 2600 | } |
| 2601 | } |
| 2602 | #endif |
drh | 49b9d33 | 2009-01-02 18:10:42 +0000 | [diff] [blame] | 2603 | rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2604 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2605 | } |
| 2606 | return rc; |
| 2607 | } |
| 2608 | |
| 2609 | /* |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2610 | ** Commit the transaction currently in progress. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2611 | ** |
drh | 6e34599 | 2007-03-30 11:12:08 +0000 | [diff] [blame] | 2612 | ** This routine implements the second phase of a 2-phase commit. The |
| 2613 | ** sqlite3BtreeSync() routine does the first phase and should be invoked |
| 2614 | ** prior to calling this routine. The sqlite3BtreeSync() routine did |
| 2615 | ** all the work of writing information out to disk and flushing the |
| 2616 | ** contents so that they are written onto the disk platter. All this |
| 2617 | ** routine has to do is delete or truncate the rollback journal |
| 2618 | ** (which causes the transaction to commit) and drop locks. |
| 2619 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2620 | ** This will release the write lock on the database file. If there |
| 2621 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2622 | */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2623 | int sqlite3BtreeCommitPhaseTwo(Btree *p){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2624 | BtShared *pBt = p->pBt; |
| 2625 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2626 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2627 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2628 | btreeIntegrity(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2629 | |
| 2630 | /* If the handle has a write-transaction open, commit the shared-btrees |
| 2631 | ** transaction and set the shared state to TRANS_READ. |
| 2632 | */ |
| 2633 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2634 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2635 | assert( pBt->inTransaction==TRANS_WRITE ); |
| 2636 | assert( pBt->nTransaction>0 ); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2637 | rc = sqlite3PagerCommitPhaseTwo(pBt->pPager); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2638 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2639 | sqlite3BtreeLeave(p); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2640 | return rc; |
| 2641 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2642 | pBt->inTransaction = TRANS_READ; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2643 | } |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 2644 | clearAllSharedCacheTableLocks(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2645 | |
| 2646 | /* If the handle has any kind of transaction open, decrement the transaction |
| 2647 | ** count of the shared btree. If the transaction count reaches 0, set |
| 2648 | ** the shared state to TRANS_NONE. The unlockBtreeIfUnused() call below |
| 2649 | ** will unlock the pager. |
| 2650 | */ |
| 2651 | if( p->inTrans!=TRANS_NONE ){ |
| 2652 | pBt->nTransaction--; |
| 2653 | if( 0==pBt->nTransaction ){ |
| 2654 | pBt->inTransaction = TRANS_NONE; |
| 2655 | } |
| 2656 | } |
| 2657 | |
| 2658 | /* Set the handles current transaction state to TRANS_NONE and unlock |
| 2659 | ** the pager if this call closed the only read or write transaction. |
| 2660 | */ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 2661 | btreeClearHasContent(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2662 | p->inTrans = TRANS_NONE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2663 | unlockBtreeIfUnused(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2664 | |
| 2665 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2666 | sqlite3BtreeLeave(p); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2667 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2668 | } |
| 2669 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2670 | /* |
| 2671 | ** Do both phases of a commit. |
| 2672 | */ |
| 2673 | int sqlite3BtreeCommit(Btree *p){ |
| 2674 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2675 | sqlite3BtreeEnter(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2676 | rc = sqlite3BtreeCommitPhaseOne(p, 0); |
| 2677 | if( rc==SQLITE_OK ){ |
| 2678 | rc = sqlite3BtreeCommitPhaseTwo(p); |
| 2679 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2680 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2681 | return rc; |
| 2682 | } |
| 2683 | |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2684 | #ifndef NDEBUG |
| 2685 | /* |
| 2686 | ** Return the number of write-cursors open on this handle. This is for use |
| 2687 | ** in assert() expressions, so it is only compiled if NDEBUG is not |
| 2688 | ** defined. |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2689 | ** |
| 2690 | ** For the purposes of this routine, a write-cursor is any cursor that |
| 2691 | ** is capable of writing to the databse. That means the cursor was |
| 2692 | ** originally opened for writing and the cursor has not be disabled |
| 2693 | ** by having its state changed to CURSOR_FAULT. |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2694 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2695 | static int countWriteCursors(BtShared *pBt){ |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2696 | BtCursor *pCur; |
| 2697 | int r = 0; |
| 2698 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2699 | if( pCur->wrFlag && pCur->eState!=CURSOR_FAULT ) r++; |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2700 | } |
| 2701 | return r; |
| 2702 | } |
| 2703 | #endif |
| 2704 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2705 | /* |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2706 | ** This routine sets the state to CURSOR_FAULT and the error |
| 2707 | ** code to errCode for every cursor on BtShared that pBtree |
| 2708 | ** references. |
| 2709 | ** |
| 2710 | ** Every cursor is tripped, including cursors that belong |
| 2711 | ** to other database connections that happen to be sharing |
| 2712 | ** the cache with pBtree. |
| 2713 | ** |
| 2714 | ** This routine gets called when a rollback occurs. |
| 2715 | ** All cursors using the same cache must be tripped |
| 2716 | ** to prevent them from trying to use the btree after |
| 2717 | ** the rollback. The rollback may have deleted tables |
| 2718 | ** or moved root pages, so it is not sufficient to |
| 2719 | ** save the state of the cursor. The cursor must be |
| 2720 | ** invalidated. |
| 2721 | */ |
| 2722 | void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){ |
| 2723 | BtCursor *p; |
| 2724 | sqlite3BtreeEnter(pBtree); |
| 2725 | for(p=pBtree->pBt->pCursor; p; p=p->pNext){ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 2726 | int i; |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 2727 | sqlite3BtreeClearCursor(p); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2728 | p->eState = CURSOR_FAULT; |
| 2729 | p->skip = errCode; |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 2730 | for(i=0; i<=p->iPage; i++){ |
| 2731 | releasePage(p->apPage[i]); |
| 2732 | p->apPage[i] = 0; |
| 2733 | } |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2734 | } |
| 2735 | sqlite3BtreeLeave(pBtree); |
| 2736 | } |
| 2737 | |
| 2738 | /* |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2739 | ** Rollback the transaction in progress. All cursors will be |
| 2740 | ** invalided by this operation. Any attempt to use a cursor |
| 2741 | ** that was open at the beginning of this operation will result |
| 2742 | ** in an error. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2743 | ** |
| 2744 | ** This will release the write lock on the database file. If there |
| 2745 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2746 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2747 | int sqlite3BtreeRollback(Btree *p){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2748 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2749 | BtShared *pBt = p->pBt; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2750 | MemPage *pPage1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2751 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2752 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2753 | pBt->db = p->db; |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2754 | rc = saveAllCursors(pBt, 0, 0); |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2755 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2756 | if( rc!=SQLITE_OK ){ |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 2757 | /* This is a horrible situation. An IO or malloc() error occurred whilst |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2758 | ** trying to save cursor positions. If this is an automatic rollback (as |
| 2759 | ** the result of a constraint, malloc() failure or IO error) then |
| 2760 | ** the cache may be internally inconsistent (not contain valid trees) so |
| 2761 | ** we cannot simply return the error to the caller. Instead, abort |
| 2762 | ** all queries that may be using any of the cursors that failed to save. |
| 2763 | */ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2764 | sqlite3BtreeTripAllCursors(p, rc); |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2765 | } |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2766 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2767 | btreeIntegrity(p); |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 2768 | clearAllSharedCacheTableLocks(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2769 | |
| 2770 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2771 | int rc2; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2772 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2773 | assert( TRANS_WRITE==pBt->inTransaction ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2774 | rc2 = sqlite3PagerRollback(pBt->pPager); |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2775 | if( rc2!=SQLITE_OK ){ |
| 2776 | rc = rc2; |
| 2777 | } |
| 2778 | |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2779 | /* The rollback may have destroyed the pPage1->aData value. So |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2780 | ** call sqlite3BtreeGetPage() on page 1 again to make |
| 2781 | ** sure pPage1->aData is set correctly. */ |
| 2782 | if( sqlite3BtreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2783 | releasePage(pPage1); |
| 2784 | } |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2785 | assert( countWriteCursors(pBt)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2786 | pBt->inTransaction = TRANS_READ; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2787 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2788 | |
| 2789 | if( p->inTrans!=TRANS_NONE ){ |
| 2790 | assert( pBt->nTransaction>0 ); |
| 2791 | pBt->nTransaction--; |
| 2792 | if( 0==pBt->nTransaction ){ |
| 2793 | pBt->inTransaction = TRANS_NONE; |
| 2794 | } |
| 2795 | } |
| 2796 | |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 2797 | btreeClearHasContent(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2798 | p->inTrans = TRANS_NONE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2799 | unlockBtreeIfUnused(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2800 | |
| 2801 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2802 | sqlite3BtreeLeave(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2803 | return rc; |
| 2804 | } |
| 2805 | |
| 2806 | /* |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame^] | 2807 | ** Start a statement subtransaction. The subtransaction can can be rolled |
| 2808 | ** back independently of the main transaction. You must start a transaction |
| 2809 | ** before starting a subtransaction. The subtransaction is ended automatically |
| 2810 | ** if the main transaction commits or rolls back. |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2811 | ** |
| 2812 | ** Statement subtransactions are used around individual SQL statements |
| 2813 | ** that are contained within a BEGIN...COMMIT block. If a constraint |
| 2814 | ** error occurs within the statement, the effect of that one statement |
| 2815 | ** can be rolled back without having to rollback the entire transaction. |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame^] | 2816 | ** |
| 2817 | ** A statement sub-transaction is implemented as an anonymous savepoint. The |
| 2818 | ** value passed as the second parameter is the total number of savepoints, |
| 2819 | ** including the new anonymous savepoint, open on the B-Tree. i.e. if there |
| 2820 | ** are no active savepoints and no other statement-transactions open, |
| 2821 | ** iStatement is 1. This anonymous savepoint can be released or rolled back |
| 2822 | ** using the sqlite3BtreeSavepoint() function. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2823 | */ |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame^] | 2824 | int sqlite3BtreeBeginStmt(Btree *p, int iStatement){ |
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( p->inTrans==TRANS_WRITE ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 2830 | assert( pBt->readOnly==0 ); |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame^] | 2831 | assert( iStatement>0 ); |
| 2832 | assert( iStatement>p->db->nSavepoint ); |
| 2833 | if( NEVER(p->inTrans!=TRANS_WRITE || pBt->readOnly) ){ |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 2834 | rc = SQLITE_INTERNAL; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2835 | }else{ |
| 2836 | assert( pBt->inTransaction==TRANS_WRITE ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 2837 | /* At the pager level, a statement transaction is a savepoint with |
| 2838 | ** an index greater than all savepoints created explicitly using |
| 2839 | ** SQL statements. It is illegal to open, release or rollback any |
| 2840 | ** such savepoints while the statement transaction savepoint is active. |
| 2841 | */ |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame^] | 2842 | rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement); |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2843 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2844 | sqlite3BtreeLeave(p); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2845 | return rc; |
| 2846 | } |
| 2847 | |
| 2848 | /* |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2849 | ** The second argument to this function, op, is always SAVEPOINT_ROLLBACK |
| 2850 | ** or SAVEPOINT_RELEASE. This function either releases or rolls back the |
danielk1977 | 12dd549 | 2008-12-18 15:45:07 +0000 | [diff] [blame] | 2851 | ** savepoint identified by parameter iSavepoint, depending on the value |
| 2852 | ** of op. |
| 2853 | ** |
| 2854 | ** Normally, iSavepoint is greater than or equal to zero. However, if op is |
| 2855 | ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the |
| 2856 | ** contents of the entire transaction are rolled back. This is different |
| 2857 | ** from a normal transaction rollback, as no locks are released and the |
| 2858 | ** transaction remains open. |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2859 | */ |
| 2860 | int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){ |
| 2861 | int rc = SQLITE_OK; |
| 2862 | if( p && p->inTrans==TRANS_WRITE ){ |
| 2863 | BtShared *pBt = p->pBt; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2864 | assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK ); |
| 2865 | assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) ); |
| 2866 | sqlite3BtreeEnter(p); |
| 2867 | pBt->db = p->db; |
| 2868 | rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint); |
drh | 9f0bbf9 | 2009-01-02 21:08:09 +0000 | [diff] [blame] | 2869 | if( rc==SQLITE_OK ){ |
| 2870 | rc = newDatabase(pBt); |
| 2871 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2872 | sqlite3BtreeLeave(p); |
| 2873 | } |
| 2874 | return rc; |
| 2875 | } |
| 2876 | |
| 2877 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2878 | ** Create a new cursor for the BTree whose root is on the page |
| 2879 | ** iTable. The act of acquiring a cursor gets a read lock on |
| 2880 | ** the database file. |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 2881 | ** |
| 2882 | ** If wrFlag==0, then the cursor can only be used for reading. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2883 | ** If wrFlag==1, then the cursor can be used for reading or for |
| 2884 | ** writing if other conditions for writing are also met. These |
| 2885 | ** are the conditions that must be met in order for writing to |
| 2886 | ** be allowed: |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2887 | ** |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2888 | ** 1: The cursor must have been opened with wrFlag==1 |
| 2889 | ** |
drh | fe5d71d | 2007-03-19 11:54:10 +0000 | [diff] [blame] | 2890 | ** 2: Other database connections that share the same pager cache |
| 2891 | ** but which are not in the READ_UNCOMMITTED state may not have |
| 2892 | ** cursors open with wrFlag==0 on the same table. Otherwise |
| 2893 | ** the changes made by this write cursor would be visible to |
| 2894 | ** the read cursors in the other database connection. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2895 | ** |
| 2896 | ** 3: The database must be writable (not on read-only media) |
| 2897 | ** |
| 2898 | ** 4: There must be an active transaction. |
| 2899 | ** |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2900 | ** No checking is done to make sure that page iTable really is the |
| 2901 | ** root page of a b-tree. If it is not, then the cursor acquired |
| 2902 | ** will not work correctly. |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2903 | ** |
| 2904 | ** It is assumed that the sqlite3BtreeCursorSize() bytes of memory |
| 2905 | ** pointed to by pCur have been zeroed by the caller. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2906 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2907 | static int btreeCursor( |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2908 | Btree *p, /* The btree */ |
| 2909 | int iTable, /* Root page of table to open */ |
| 2910 | int wrFlag, /* 1 to write. 0 read-only */ |
| 2911 | struct KeyInfo *pKeyInfo, /* First arg to comparison function */ |
| 2912 | BtCursor *pCur /* Space for new cursor */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2913 | ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2914 | int rc; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 2915 | Pgno nPage; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2916 | BtShared *pBt = p->pBt; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2917 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2918 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 2919 | assert( wrFlag==0 || wrFlag==1 ); |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2920 | if( wrFlag ){ |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 2921 | assert( !pBt->readOnly ); |
| 2922 | if( NEVER(pBt->readOnly) ){ |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2923 | return SQLITE_READONLY; |
| 2924 | } |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2925 | rc = checkForReadConflicts(p, iTable, 0, 0); |
| 2926 | if( rc!=SQLITE_OK ){ |
| 2927 | assert( rc==SQLITE_LOCKED_SHAREDCACHE ); |
| 2928 | return rc; |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2929 | } |
drh | a0c9a11 | 2004-03-10 13:42:37 +0000 | [diff] [blame] | 2930 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2931 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2932 | if( pBt->pPage1==0 ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2933 | rc = lockBtreeWithRetry(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2934 | if( rc!=SQLITE_OK ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2935 | return rc; |
| 2936 | } |
| 2937 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2938 | pCur->pgnoRoot = (Pgno)iTable; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 2939 | rc = sqlite3PagerPagecount(pBt->pPager, (int *)&nPage); |
| 2940 | if( rc!=SQLITE_OK ){ |
| 2941 | return rc; |
| 2942 | } |
| 2943 | if( iTable==1 && nPage==0 ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2944 | rc = SQLITE_EMPTY; |
| 2945 | goto create_cursor_exception; |
| 2946 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2947 | rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2948 | if( rc!=SQLITE_OK ){ |
| 2949 | goto create_cursor_exception; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2950 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2951 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2952 | /* Now that no other errors can occur, finish filling in the BtCursor |
| 2953 | ** variables, link the cursor into the BtShared list and set *ppCur (the |
| 2954 | ** output argument to this function). |
| 2955 | */ |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2956 | pCur->pKeyInfo = pKeyInfo; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2957 | pCur->pBtree = p; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 2958 | pCur->pBt = pBt; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 2959 | pCur->wrFlag = (u8)wrFlag; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2960 | pCur->pNext = pBt->pCursor; |
| 2961 | if( pCur->pNext ){ |
| 2962 | pCur->pNext->pPrev = pCur; |
| 2963 | } |
| 2964 | pBt->pCursor = pCur; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2965 | pCur->eState = CURSOR_INVALID; |
drh | 7f75122 | 2009-03-17 22:33:00 +0000 | [diff] [blame] | 2966 | pCur->cachedRowid = 0; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2967 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2968 | return SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2969 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2970 | create_cursor_exception: |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2971 | releasePage(pCur->apPage[0]); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2972 | unlockBtreeIfUnused(pBt); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2973 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2974 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2975 | int sqlite3BtreeCursor( |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2976 | Btree *p, /* The btree */ |
| 2977 | int iTable, /* Root page of table to open */ |
| 2978 | int wrFlag, /* 1 to write. 0 read-only */ |
| 2979 | struct KeyInfo *pKeyInfo, /* First arg to xCompare() */ |
| 2980 | BtCursor *pCur /* Write new cursor here */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2981 | ){ |
| 2982 | int rc; |
| 2983 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2984 | p->pBt->db = p->db; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2985 | rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2986 | sqlite3BtreeLeave(p); |
| 2987 | return rc; |
| 2988 | } |
drh | 7f75122 | 2009-03-17 22:33:00 +0000 | [diff] [blame] | 2989 | |
| 2990 | /* |
| 2991 | ** Return the size of a BtCursor object in bytes. |
| 2992 | ** |
| 2993 | ** This interfaces is needed so that users of cursors can preallocate |
| 2994 | ** sufficient storage to hold a cursor. The BtCursor object is opaque |
| 2995 | ** to users so they cannot do the sizeof() themselves - they must call |
| 2996 | ** this routine. |
| 2997 | */ |
| 2998 | int sqlite3BtreeCursorSize(void){ |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2999 | return sizeof(BtCursor); |
| 3000 | } |
| 3001 | |
drh | 7f75122 | 2009-03-17 22:33:00 +0000 | [diff] [blame] | 3002 | /* |
| 3003 | ** Set the cached rowid value of every cursor in the same database file |
| 3004 | ** as pCur and having the same root page number as pCur. The value is |
| 3005 | ** set to iRowid. |
| 3006 | ** |
| 3007 | ** Only positive rowid values are considered valid for this cache. |
| 3008 | ** The cache is initialized to zero, indicating an invalid cache. |
| 3009 | ** A btree will work fine with zero or negative rowids. We just cannot |
| 3010 | ** cache zero or negative rowids, which means tables that use zero or |
| 3011 | ** negative rowids might run a little slower. But in practice, zero |
| 3012 | ** or negative rowids are very uncommon so this should not be a problem. |
| 3013 | */ |
| 3014 | void sqlite3BtreeSetCachedRowid(BtCursor *pCur, sqlite3_int64 iRowid){ |
| 3015 | BtCursor *p; |
| 3016 | for(p=pCur->pBt->pCursor; p; p=p->pNext){ |
| 3017 | if( p->pgnoRoot==pCur->pgnoRoot ) p->cachedRowid = iRowid; |
| 3018 | } |
| 3019 | assert( pCur->cachedRowid==iRowid ); |
| 3020 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3021 | |
drh | 7f75122 | 2009-03-17 22:33:00 +0000 | [diff] [blame] | 3022 | /* |
| 3023 | ** Return the cached rowid for the given cursor. A negative or zero |
| 3024 | ** return value indicates that the rowid cache is invalid and should be |
| 3025 | ** ignored. If the rowid cache has never before been set, then a |
| 3026 | ** zero is returned. |
| 3027 | */ |
| 3028 | sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor *pCur){ |
| 3029 | return pCur->cachedRowid; |
| 3030 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3031 | |
| 3032 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3033 | ** Close a cursor. The read lock on the database file is released |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3034 | ** when the last cursor is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3035 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3036 | int sqlite3BtreeCloseCursor(BtCursor *pCur){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 3037 | Btree *pBtree = pCur->pBtree; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3038 | if( pBtree ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3039 | int i; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3040 | BtShared *pBt = pCur->pBt; |
| 3041 | sqlite3BtreeEnter(pBtree); |
| 3042 | pBt->db = pBtree->db; |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 3043 | sqlite3BtreeClearCursor(pCur); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3044 | if( pCur->pPrev ){ |
| 3045 | pCur->pPrev->pNext = pCur->pNext; |
| 3046 | }else{ |
| 3047 | pBt->pCursor = pCur->pNext; |
| 3048 | } |
| 3049 | if( pCur->pNext ){ |
| 3050 | pCur->pNext->pPrev = pCur->pPrev; |
| 3051 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3052 | for(i=0; i<=pCur->iPage; i++){ |
| 3053 | releasePage(pCur->apPage[i]); |
| 3054 | } |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3055 | unlockBtreeIfUnused(pBt); |
| 3056 | invalidateOverflowCache(pCur); |
| 3057 | /* sqlite3_free(pCur); */ |
| 3058 | sqlite3BtreeLeave(pBtree); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3059 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3060 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3061 | } |
| 3062 | |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3063 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3064 | ** Make a temporary cursor by filling in the fields of pTempCur. |
| 3065 | ** The temporary cursor is not on the cursor list for the Btree. |
| 3066 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3067 | void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3068 | int i; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3069 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3070 | memcpy(pTempCur, pCur, sizeof(BtCursor)); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3071 | pTempCur->pNext = 0; |
| 3072 | pTempCur->pPrev = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3073 | for(i=0; i<=pTempCur->iPage; i++){ |
| 3074 | sqlite3PagerRef(pTempCur->apPage[i]->pDbPage); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3075 | } |
danielk1977 | 36e2093 | 2008-11-26 07:40:30 +0000 | [diff] [blame] | 3076 | assert( pTempCur->pKey==0 ); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3077 | } |
| 3078 | |
| 3079 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3080 | ** Delete a temporary cursor such as was made by the CreateTemporaryCursor() |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3081 | ** function above. |
| 3082 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3083 | void sqlite3BtreeReleaseTempCursor(BtCursor *pCur){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3084 | int i; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3085 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3086 | for(i=0; i<=pCur->iPage; i++){ |
| 3087 | sqlite3PagerUnref(pCur->apPage[i]->pDbPage); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3088 | } |
danielk1977 | 36e2093 | 2008-11-26 07:40:30 +0000 | [diff] [blame] | 3089 | sqlite3_free(pCur->pKey); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3090 | } |
| 3091 | |
drh | 7f75122 | 2009-03-17 22:33:00 +0000 | [diff] [blame] | 3092 | |
| 3093 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3094 | /* |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3095 | ** Make sure the BtCursor* given in the argument has a valid |
| 3096 | ** BtCursor.info structure. If it is not already valid, call |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3097 | ** sqlite3BtreeParseCell() to fill it in. |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 3098 | ** |
| 3099 | ** BtCursor.info is a cache of the information in the current cell. |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3100 | ** Using this cache reduces the number of calls to sqlite3BtreeParseCell(). |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3101 | ** |
| 3102 | ** 2007-06-25: There is a bug in some versions of MSVC that cause the |
| 3103 | ** compiler to crash when getCellInfo() is implemented as a macro. |
| 3104 | ** But there is a measureable speed advantage to using the macro on gcc |
| 3105 | ** (when less compiler optimizations like -Os or -O0 are used and the |
| 3106 | ** compiler is not doing agressive inlining.) So we use a real function |
| 3107 | ** for MSVC and a macro for everything else. Ticket #2457. |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3108 | */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3109 | #ifndef NDEBUG |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3110 | static void assertCellInfo(BtCursor *pCur){ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3111 | CellInfo info; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3112 | int iPage = pCur->iPage; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 3113 | memset(&info, 0, sizeof(info)); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3114 | sqlite3BtreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3115 | assert( memcmp(&info, &pCur->info, sizeof(info))==0 ); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3116 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3117 | #else |
| 3118 | #define assertCellInfo(x) |
| 3119 | #endif |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3120 | #ifdef _MSC_VER |
| 3121 | /* Use a real function in MSVC to work around bugs in that compiler. */ |
| 3122 | static void getCellInfo(BtCursor *pCur){ |
| 3123 | if( pCur->info.nSize==0 ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3124 | int iPage = pCur->iPage; |
| 3125 | sqlite3BtreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3126 | pCur->validNKey = 1; |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3127 | }else{ |
| 3128 | assertCellInfo(pCur); |
| 3129 | } |
| 3130 | } |
| 3131 | #else /* if not _MSC_VER */ |
| 3132 | /* Use a macro in all other compilers so that the function is inlined */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3133 | #define getCellInfo(pCur) \ |
| 3134 | if( pCur->info.nSize==0 ){ \ |
| 3135 | int iPage = pCur->iPage; \ |
| 3136 | sqlite3BtreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); \ |
| 3137 | pCur->validNKey = 1; \ |
| 3138 | }else{ \ |
| 3139 | assertCellInfo(pCur); \ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3140 | } |
| 3141 | #endif /* _MSC_VER */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3142 | |
| 3143 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3144 | ** Set *pSize to the size of the buffer needed to hold the value of |
| 3145 | ** the key for the current entry. If the cursor is not pointing |
| 3146 | ** to a valid entry, *pSize is set to 0. |
| 3147 | ** |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3148 | ** For a table with the INTKEY flag set, this routine returns the key |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3149 | ** itself, not the number of bytes in the key. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3150 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3151 | int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3152 | int rc; |
| 3153 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3154 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 3155 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3156 | if( rc==SQLITE_OK ){ |
| 3157 | assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID ); |
| 3158 | if( pCur->eState==CURSOR_INVALID ){ |
| 3159 | *pSize = 0; |
| 3160 | }else{ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3161 | getCellInfo(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3162 | *pSize = pCur->info.nKey; |
| 3163 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3164 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3165 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3166 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3167 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3168 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3169 | ** Set *pSize to the number of bytes of data in the entry the |
| 3170 | ** cursor currently points to. Always return SQLITE_OK. |
| 3171 | ** Failure is not possible. If the cursor is not currently |
| 3172 | ** pointing to an entry (which can happen, for example, if |
| 3173 | ** the database is empty) then *pSize is set to 0. |
| 3174 | */ |
| 3175 | int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3176 | int rc; |
| 3177 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3178 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 3179 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3180 | if( rc==SQLITE_OK ){ |
| 3181 | assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID ); |
| 3182 | if( pCur->eState==CURSOR_INVALID ){ |
| 3183 | /* Not pointing at a valid entry - set *pSize to 0. */ |
| 3184 | *pSize = 0; |
| 3185 | }else{ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3186 | getCellInfo(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3187 | *pSize = pCur->info.nData; |
| 3188 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3189 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3190 | return rc; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3191 | } |
| 3192 | |
| 3193 | /* |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3194 | ** Given the page number of an overflow page in the database (parameter |
| 3195 | ** ovfl), this function finds the page number of the next page in the |
| 3196 | ** linked list of overflow pages. If possible, it uses the auto-vacuum |
| 3197 | ** pointer-map data instead of reading the content of page ovfl to do so. |
| 3198 | ** |
| 3199 | ** If an error occurs an SQLite error code is returned. Otherwise: |
| 3200 | ** |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3201 | ** The page number of the next overflow page in the linked list is |
| 3202 | ** written to *pPgnoNext. If page ovfl is the last page in its linked |
| 3203 | ** list, *pPgnoNext is set to zero. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3204 | ** |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3205 | ** If ppPage is not NULL, and a reference to the MemPage object corresponding |
| 3206 | ** to page number pOvfl was obtained, then *ppPage is set to point to that |
| 3207 | ** reference. It is the responsibility of the caller to call releasePage() |
| 3208 | ** on *ppPage to free the reference. In no reference was obtained (because |
| 3209 | ** the pointer-map was used to obtain the value for *pPgnoNext), then |
| 3210 | ** *ppPage is set to zero. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3211 | */ |
| 3212 | static int getOverflowPage( |
| 3213 | BtShared *pBt, |
| 3214 | Pgno ovfl, /* Overflow page */ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3215 | MemPage **ppPage, /* OUT: MemPage handle (may be NULL) */ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3216 | Pgno *pPgnoNext /* OUT: Next overflow page number */ |
| 3217 | ){ |
| 3218 | Pgno next = 0; |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3219 | MemPage *pPage = 0; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 3220 | int rc = SQLITE_OK; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3221 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3222 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3223 | assert(pPgnoNext); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3224 | |
| 3225 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3226 | /* Try to find the next page in the overflow list using the |
| 3227 | ** autovacuum pointer-map pages. Guess that the next page in |
| 3228 | ** the overflow list is page number (ovfl+1). If that guess turns |
| 3229 | ** out to be wrong, fall back to loading the data of page |
| 3230 | ** number ovfl to determine the next page number. |
| 3231 | */ |
| 3232 | if( pBt->autoVacuum ){ |
| 3233 | Pgno pgno; |
| 3234 | Pgno iGuess = ovfl+1; |
| 3235 | u8 eType; |
| 3236 | |
| 3237 | while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){ |
| 3238 | iGuess++; |
| 3239 | } |
| 3240 | |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 3241 | if( iGuess<=pagerPagecount(pBt) ){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3242 | rc = ptrmapGet(pBt, iGuess, &eType, &pgno); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3243 | if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3244 | next = iGuess; |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3245 | rc = SQLITE_DONE; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3246 | } |
| 3247 | } |
| 3248 | } |
| 3249 | #endif |
| 3250 | |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3251 | if( rc==SQLITE_OK ){ |
| 3252 | rc = sqlite3BtreeGetPage(pBt, ovfl, &pPage, 0); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3253 | assert(rc==SQLITE_OK || pPage==0); |
| 3254 | if( next==0 && rc==SQLITE_OK ){ |
| 3255 | next = get4byte(pPage->aData); |
| 3256 | } |
danielk1977 | 443c059 | 2009-01-16 15:21:05 +0000 | [diff] [blame] | 3257 | } |
danielk1977 | 45d6882 | 2009-01-16 16:23:38 +0000 | [diff] [blame] | 3258 | |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3259 | *pPgnoNext = next; |
| 3260 | if( ppPage ){ |
| 3261 | *ppPage = pPage; |
| 3262 | }else{ |
| 3263 | releasePage(pPage); |
| 3264 | } |
| 3265 | return (rc==SQLITE_DONE ? SQLITE_OK : rc); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3266 | } |
| 3267 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3268 | /* |
| 3269 | ** Copy data from a buffer to a page, or from a page to a buffer. |
| 3270 | ** |
| 3271 | ** pPayload is a pointer to data stored on database page pDbPage. |
| 3272 | ** If argument eOp is false, then nByte bytes of data are copied |
| 3273 | ** from pPayload to the buffer pointed at by pBuf. If eOp is true, |
| 3274 | ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes |
| 3275 | ** of data are copied from the buffer pBuf to pPayload. |
| 3276 | ** |
| 3277 | ** SQLITE_OK is returned on success, otherwise an error code. |
| 3278 | */ |
| 3279 | static int copyPayload( |
| 3280 | void *pPayload, /* Pointer to page data */ |
| 3281 | void *pBuf, /* Pointer to buffer */ |
| 3282 | int nByte, /* Number of bytes to copy */ |
| 3283 | int eOp, /* 0 -> copy from page, 1 -> copy to page */ |
| 3284 | DbPage *pDbPage /* Page containing pPayload */ |
| 3285 | ){ |
| 3286 | if( eOp ){ |
| 3287 | /* Copy data from buffer to page (a write operation) */ |
| 3288 | int rc = sqlite3PagerWrite(pDbPage); |
| 3289 | if( rc!=SQLITE_OK ){ |
| 3290 | return rc; |
| 3291 | } |
| 3292 | memcpy(pPayload, pBuf, nByte); |
| 3293 | }else{ |
| 3294 | /* Copy data from page to buffer (a read operation) */ |
| 3295 | memcpy(pBuf, pPayload, nByte); |
| 3296 | } |
| 3297 | return SQLITE_OK; |
| 3298 | } |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3299 | |
| 3300 | /* |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3301 | ** This function is used to read or overwrite payload information |
| 3302 | ** for the entry that the pCur cursor is pointing to. If the eOp |
| 3303 | ** parameter is 0, this is a read operation (data copied into |
| 3304 | ** buffer pBuf). If it is non-zero, a write (data copied from |
| 3305 | ** buffer pBuf). |
| 3306 | ** |
| 3307 | ** A total of "amt" bytes are read or written beginning at "offset". |
| 3308 | ** Data is read to or from the buffer pBuf. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3309 | ** |
| 3310 | ** This routine does not make a distinction between key and data. |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3311 | ** It just reads or writes bytes from the payload area. Data might |
| 3312 | ** appear on the main page or be scattered out on multiple overflow |
| 3313 | ** pages. |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3314 | ** |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 3315 | ** If the BtCursor.isIncrblobHandle flag is set, and the current |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3316 | ** cursor entry uses one or more overflow pages, this function |
| 3317 | ** allocates space for and lazily popluates the overflow page-list |
| 3318 | ** cache array (BtCursor.aOverflow). Subsequent calls use this |
| 3319 | ** cache to make seeking to the supplied offset more efficient. |
| 3320 | ** |
| 3321 | ** Once an overflow page-list cache has been allocated, it may be |
| 3322 | ** invalidated if some other cursor writes to the same table, or if |
| 3323 | ** the cursor is moved to a different row. Additionally, in auto-vacuum |
| 3324 | ** mode, the following events may invalidate an overflow page-list cache. |
| 3325 | ** |
| 3326 | ** * An incremental vacuum, |
| 3327 | ** * A commit in auto_vacuum="full" mode, |
| 3328 | ** * Creating a table (may require moving an overflow page). |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3329 | */ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3330 | static int accessPayload( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3331 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 3332 | u32 offset, /* Begin reading this far into payload */ |
| 3333 | u32 amt, /* Read this many bytes */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3334 | unsigned char *pBuf, /* Write the bytes into this buffer */ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3335 | int skipKey, /* offset begins at data if this is true */ |
| 3336 | int eOp /* zero to read. non-zero to write. */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3337 | ){ |
| 3338 | unsigned char *aPayload; |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3339 | int rc = SQLITE_OK; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3340 | u32 nKey; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3341 | int iIdx = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3342 | MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */ |
danielk1977 | 0d06541 | 2008-11-12 18:21:36 +0000 | [diff] [blame] | 3343 | BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3344 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3345 | assert( pPage ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3346 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3347 | assert( pCur->aiIdx[pCur->iPage]<pPage->nCell ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3348 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3349 | |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3350 | getCellInfo(pCur); |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3351 | aPayload = pCur->info.pCell + pCur->info.nHeader; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3352 | nKey = (pPage->intKey ? 0 : (int)pCur->info.nKey); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3353 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3354 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3355 | offset += nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3356 | } |
danielk1977 | 0d06541 | 2008-11-12 18:21:36 +0000 | [diff] [blame] | 3357 | if( offset+amt > nKey+pCur->info.nData |
| 3358 | || &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize] |
| 3359 | ){ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3360 | /* 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] | 3361 | return SQLITE_CORRUPT_BKPT; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3362 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3363 | |
| 3364 | /* Check if data must be read/written to/from the btree page itself. */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3365 | if( offset<pCur->info.nLocal ){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3366 | int a = amt; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3367 | if( a+offset>pCur->info.nLocal ){ |
| 3368 | a = pCur->info.nLocal - offset; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3369 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3370 | rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3371 | offset = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3372 | pBuf += a; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3373 | amt -= a; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 3374 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3375 | offset -= pCur->info.nLocal; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3376 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3377 | |
| 3378 | if( rc==SQLITE_OK && amt>0 ){ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 3379 | const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3380 | Pgno nextPage; |
| 3381 | |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3382 | nextPage = get4byte(&aPayload[pCur->info.nLocal]); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3383 | |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3384 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 3385 | /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[] |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3386 | ** has not been allocated, allocate it now. The array is sized at |
| 3387 | ** one entry for each overflow page in the overflow chain. The |
| 3388 | ** page number of the first overflow page is stored in aOverflow[0], |
| 3389 | ** etc. A value of 0 in the aOverflow[] array means "not yet known" |
| 3390 | ** (the cache is lazily populated). |
| 3391 | */ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 3392 | if( pCur->isIncrblobHandle && !pCur->aOverflow ){ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3393 | int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3394 | pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl); |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3395 | if( nOvfl && !pCur->aOverflow ){ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3396 | rc = SQLITE_NOMEM; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3397 | } |
| 3398 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3399 | |
| 3400 | /* If the overflow page-list cache has been allocated and the |
| 3401 | ** entry for the first required overflow page is valid, skip |
| 3402 | ** directly to it. |
| 3403 | */ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3404 | if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){ |
| 3405 | iIdx = (offset/ovflSize); |
| 3406 | nextPage = pCur->aOverflow[iIdx]; |
| 3407 | offset = (offset%ovflSize); |
| 3408 | } |
| 3409 | #endif |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3410 | |
| 3411 | for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){ |
| 3412 | |
| 3413 | #ifndef SQLITE_OMIT_INCRBLOB |
| 3414 | /* If required, populate the overflow page-list cache. */ |
| 3415 | if( pCur->aOverflow ){ |
| 3416 | assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage); |
| 3417 | pCur->aOverflow[iIdx] = nextPage; |
| 3418 | } |
| 3419 | #endif |
| 3420 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3421 | if( offset>=ovflSize ){ |
| 3422 | /* The only reason to read this page is to obtain the page |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3423 | ** number for the next page in the overflow chain. The page |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 3424 | ** data is not required. So first try to lookup the overflow |
| 3425 | ** page-list cache, if any, then fall back to the getOverflowPage() |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3426 | ** function. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3427 | */ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3428 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3429 | if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){ |
| 3430 | nextPage = pCur->aOverflow[iIdx+1]; |
| 3431 | } else |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3432 | #endif |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3433 | rc = getOverflowPage(pBt, nextPage, 0, &nextPage); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3434 | offset -= ovflSize; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3435 | }else{ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3436 | /* Need to read this page properly. It contains some of the |
| 3437 | ** range of data that is being read (eOp==0) or written (eOp!=0). |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3438 | */ |
| 3439 | DbPage *pDbPage; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3440 | int a = amt; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3441 | rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3442 | if( rc==SQLITE_OK ){ |
| 3443 | aPayload = sqlite3PagerGetData(pDbPage); |
| 3444 | nextPage = get4byte(aPayload); |
| 3445 | if( a + offset > ovflSize ){ |
| 3446 | a = ovflSize - offset; |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3447 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3448 | rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage); |
| 3449 | sqlite3PagerUnref(pDbPage); |
| 3450 | offset = 0; |
| 3451 | amt -= a; |
| 3452 | pBuf += a; |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3453 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3454 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3455 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3456 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3457 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3458 | if( rc==SQLITE_OK && amt>0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3459 | return SQLITE_CORRUPT_BKPT; |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 3460 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3461 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3462 | } |
| 3463 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3464 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3465 | ** Read part of the key associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3466 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3467 | ** begins at "offset". |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3468 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3469 | ** Return SQLITE_OK on success or an error code if anything goes |
| 3470 | ** wrong. An error is returned if "offset+amt" is larger than |
| 3471 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3472 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3473 | int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3474 | int rc; |
| 3475 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3476 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 3477 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3478 | if( rc==SQLITE_OK ){ |
| 3479 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3480 | assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] ); |
| 3481 | if( pCur->apPage[0]->intKey ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3482 | return SQLITE_CORRUPT_BKPT; |
| 3483 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3484 | assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3485 | rc = accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0, 0); |
drh | 6575a22 | 2005-03-10 17:06:34 +0000 | [diff] [blame] | 3486 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3487 | return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3488 | } |
| 3489 | |
| 3490 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3491 | ** Read part of the data associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3492 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3493 | ** begins at "offset". |
| 3494 | ** |
| 3495 | ** Return SQLITE_OK on success or an error code if anything goes |
| 3496 | ** wrong. An error is returned if "offset+amt" is larger than |
| 3497 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3498 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3499 | int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3500 | int rc; |
| 3501 | |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 3502 | #ifndef SQLITE_OMIT_INCRBLOB |
| 3503 | if ( pCur->eState==CURSOR_INVALID ){ |
| 3504 | return SQLITE_ABORT; |
| 3505 | } |
| 3506 | #endif |
| 3507 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3508 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 3509 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3510 | if( rc==SQLITE_OK ){ |
| 3511 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3512 | assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] ); |
| 3513 | assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3514 | rc = accessPayload(pCur, offset, amt, pBuf, 1, 0); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3515 | } |
| 3516 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3517 | } |
| 3518 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3519 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3520 | ** Return a pointer to payload information from the entry that the |
| 3521 | ** pCur cursor is pointing to. The pointer is to the beginning of |
| 3522 | ** 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] | 3523 | ** skipKey==1. The number of bytes of available key/data is written |
| 3524 | ** into *pAmt. If *pAmt==0, then the value returned will not be |
| 3525 | ** a valid pointer. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3526 | ** |
| 3527 | ** This routine is an optimization. It is common for the entire key |
| 3528 | ** and data to fit on the local page and for there to be no overflow |
| 3529 | ** pages. When that is so, this routine can be used to access the |
| 3530 | ** key and data without making a copy. If the key and/or data spills |
drh | 7f75122 | 2009-03-17 22:33:00 +0000 | [diff] [blame] | 3531 | ** onto overflow pages, then accessPayload() must be used to reassemble |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3532 | ** the key/data and copy it into a preallocated buffer. |
| 3533 | ** |
| 3534 | ** The pointer returned by this routine looks directly into the cached |
| 3535 | ** page of the database. The data might change or move the next time |
| 3536 | ** any btree routine is called. |
| 3537 | */ |
| 3538 | static const unsigned char *fetchPayload( |
| 3539 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3540 | int *pAmt, /* Write the number of available bytes here */ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3541 | int skipKey /* read beginning at data if this is true */ |
| 3542 | ){ |
| 3543 | unsigned char *aPayload; |
| 3544 | MemPage *pPage; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3545 | u32 nKey; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 3546 | u32 nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3547 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3548 | assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3549 | assert( pCur->eState==CURSOR_VALID ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3550 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3551 | pPage = pCur->apPage[pCur->iPage]; |
| 3552 | assert( pCur->aiIdx[pCur->iPage]<pPage->nCell ); |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3553 | getCellInfo(pCur); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3554 | aPayload = pCur->info.pCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3555 | aPayload += pCur->info.nHeader; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3556 | if( pPage->intKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3557 | nKey = 0; |
| 3558 | }else{ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3559 | nKey = (int)pCur->info.nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3560 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3561 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3562 | aPayload += nKey; |
| 3563 | nLocal = pCur->info.nLocal - nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3564 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3565 | nLocal = pCur->info.nLocal; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3566 | if( nLocal>nKey ){ |
| 3567 | nLocal = nKey; |
| 3568 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3569 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3570 | *pAmt = nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3571 | return aPayload; |
| 3572 | } |
| 3573 | |
| 3574 | |
| 3575 | /* |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3576 | ** For the entry that cursor pCur is point to, return as |
| 3577 | ** many bytes of the key or data as are available on the local |
| 3578 | ** b-tree page. Write the number of available bytes into *pAmt. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3579 | ** |
| 3580 | ** The pointer returned is ephemeral. The key/data may move |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3581 | ** or be destroyed on the next call to any Btree routine, |
| 3582 | ** including calls from other threads against the same cache. |
| 3583 | ** Hence, a mutex on the BtShared should be held prior to calling |
| 3584 | ** this routine. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3585 | ** |
| 3586 | ** These routines is used to get quick access to key and data |
| 3587 | ** in the common case where no overflow pages are used. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3588 | */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3589 | const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3590 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3591 | if( pCur->eState==CURSOR_VALID ){ |
| 3592 | return (const void*)fetchPayload(pCur, pAmt, 0); |
| 3593 | } |
| 3594 | return 0; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3595 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3596 | const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3597 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3598 | if( pCur->eState==CURSOR_VALID ){ |
| 3599 | return (const void*)fetchPayload(pCur, pAmt, 1); |
| 3600 | } |
| 3601 | return 0; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3602 | } |
| 3603 | |
| 3604 | |
| 3605 | /* |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3606 | ** 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] | 3607 | ** page number of the child page to move to. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3608 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3609 | static int moveToChild(BtCursor *pCur, u32 newPgno){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3610 | int rc; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3611 | int i = pCur->iPage; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3612 | MemPage *pNewPage; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 3613 | BtShared *pBt = pCur->pBt; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3614 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3615 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3616 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3617 | assert( pCur->iPage<BTCURSOR_MAX_DEPTH ); |
| 3618 | if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){ |
| 3619 | return SQLITE_CORRUPT_BKPT; |
| 3620 | } |
| 3621 | rc = getAndInitPage(pBt, newPgno, &pNewPage); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3622 | if( rc ) return rc; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3623 | pCur->apPage[i+1] = pNewPage; |
| 3624 | pCur->aiIdx[i+1] = 0; |
| 3625 | pCur->iPage++; |
| 3626 | |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3627 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3628 | pCur->validNKey = 0; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 3629 | if( pNewPage->nCell<1 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3630 | return SQLITE_CORRUPT_BKPT; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 3631 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3632 | return SQLITE_OK; |
| 3633 | } |
| 3634 | |
danielk1977 | bf93c56 | 2008-09-29 15:53:25 +0000 | [diff] [blame] | 3635 | #ifndef NDEBUG |
| 3636 | /* |
| 3637 | ** Page pParent is an internal (non-leaf) tree page. This function |
| 3638 | ** asserts that page number iChild is the left-child if the iIdx'th |
| 3639 | ** cell in page pParent. Or, if iIdx is equal to the total number of |
| 3640 | ** cells in pParent, that page number iChild is the right-child of |
| 3641 | ** the page. |
| 3642 | */ |
| 3643 | static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){ |
| 3644 | assert( iIdx<=pParent->nCell ); |
| 3645 | if( iIdx==pParent->nCell ){ |
| 3646 | assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild ); |
| 3647 | }else{ |
| 3648 | assert( get4byte(findCell(pParent, iIdx))==iChild ); |
| 3649 | } |
| 3650 | } |
| 3651 | #else |
| 3652 | # define assertParentIndex(x,y,z) |
| 3653 | #endif |
| 3654 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3655 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3656 | ** Move the cursor up to the parent page. |
| 3657 | ** |
| 3658 | ** pCur->idx is set to the cell index that contains the pointer |
| 3659 | ** to the page we are coming from. If we are coming from the |
| 3660 | ** right-most child page then pCur->idx is set to one more than |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3661 | ** the largest cell index. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3662 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3663 | void sqlite3BtreeMoveToParent(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3664 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3665 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3666 | assert( pCur->iPage>0 ); |
| 3667 | assert( pCur->apPage[pCur->iPage] ); |
danielk1977 | bf93c56 | 2008-09-29 15:53:25 +0000 | [diff] [blame] | 3668 | assertParentIndex( |
| 3669 | pCur->apPage[pCur->iPage-1], |
| 3670 | pCur->aiIdx[pCur->iPage-1], |
| 3671 | pCur->apPage[pCur->iPage]->pgno |
| 3672 | ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3673 | releasePage(pCur->apPage[pCur->iPage]); |
| 3674 | pCur->iPage--; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3675 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3676 | pCur->validNKey = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3677 | } |
| 3678 | |
| 3679 | /* |
| 3680 | ** Move the cursor to the root page |
| 3681 | */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3682 | static int moveToRoot(BtCursor *pCur){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3683 | MemPage *pRoot; |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3684 | int rc = SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3685 | Btree *p = pCur->pBtree; |
| 3686 | BtShared *pBt = p->pBt; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3687 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3688 | assert( cursorHoldsMutex(pCur) ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 3689 | assert( CURSOR_INVALID < CURSOR_REQUIRESEEK ); |
| 3690 | assert( CURSOR_VALID < CURSOR_REQUIRESEEK ); |
| 3691 | assert( CURSOR_FAULT > CURSOR_REQUIRESEEK ); |
| 3692 | if( pCur->eState>=CURSOR_REQUIRESEEK ){ |
| 3693 | if( pCur->eState==CURSOR_FAULT ){ |
| 3694 | return pCur->skip; |
| 3695 | } |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 3696 | sqlite3BtreeClearCursor(pCur); |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 3697 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3698 | |
| 3699 | if( pCur->iPage>=0 ){ |
| 3700 | int i; |
| 3701 | for(i=1; i<=pCur->iPage; i++){ |
| 3702 | releasePage(pCur->apPage[i]); |
danielk1977 | d9f6c53 | 2008-09-19 16:39:38 +0000 | [diff] [blame] | 3703 | } |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3704 | }else{ |
| 3705 | if( |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3706 | SQLITE_OK!=(rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0])) |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3707 | ){ |
| 3708 | pCur->eState = CURSOR_INVALID; |
| 3709 | return rc; |
| 3710 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3711 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3712 | |
| 3713 | pRoot = pCur->apPage[0]; |
| 3714 | assert( pRoot->pgno==pCur->pgnoRoot ); |
| 3715 | pCur->iPage = 0; |
| 3716 | pCur->aiIdx[0] = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3717 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3718 | pCur->atLast = 0; |
| 3719 | pCur->validNKey = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3720 | |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3721 | if( pRoot->nCell==0 && !pRoot->leaf ){ |
| 3722 | Pgno subpage; |
| 3723 | assert( pRoot->pgno==1 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3724 | subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3725 | assert( subpage>0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3726 | pCur->eState = CURSOR_VALID; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3727 | rc = moveToChild(pCur, subpage); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3728 | }else{ |
| 3729 | pCur->eState = ((pRoot->nCell>0)?CURSOR_VALID:CURSOR_INVALID); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3730 | } |
| 3731 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3732 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3733 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3734 | /* |
| 3735 | ** Move the cursor down to the left-most leaf entry beneath the |
| 3736 | ** entry to which it is currently pointing. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3737 | ** |
| 3738 | ** The left-most leaf is the one with the smallest key - the first |
| 3739 | ** in ascending order. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3740 | */ |
| 3741 | static int moveToLeftmost(BtCursor *pCur){ |
| 3742 | Pgno pgno; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3743 | int rc = SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3744 | MemPage *pPage; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3745 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3746 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3747 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3748 | while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){ |
| 3749 | assert( pCur->aiIdx[pCur->iPage]<pPage->nCell ); |
| 3750 | pgno = get4byte(findCell(pPage, pCur->aiIdx[pCur->iPage])); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3751 | rc = moveToChild(pCur, pgno); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3752 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3753 | return rc; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3754 | } |
| 3755 | |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3756 | /* |
| 3757 | ** Move the cursor down to the right-most leaf entry beneath the |
| 3758 | ** page to which it is currently pointing. Notice the difference |
| 3759 | ** between moveToLeftmost() and moveToRightmost(). moveToLeftmost() |
| 3760 | ** finds the left-most entry beneath the *entry* whereas moveToRightmost() |
| 3761 | ** finds the right-most entry beneath the *page*. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3762 | ** |
| 3763 | ** The right-most entry is the one with the largest key - the last |
| 3764 | ** key in ascending order. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3765 | */ |
| 3766 | static int moveToRightmost(BtCursor *pCur){ |
| 3767 | Pgno pgno; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3768 | int rc = SQLITE_OK; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 3769 | MemPage *pPage = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3770 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3771 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3772 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3773 | while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3774 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3775 | pCur->aiIdx[pCur->iPage] = pPage->nCell; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3776 | rc = moveToChild(pCur, pgno); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3777 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3778 | if( rc==SQLITE_OK ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3779 | pCur->aiIdx[pCur->iPage] = pPage->nCell-1; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3780 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3781 | pCur->validNKey = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3782 | } |
danielk1977 | 518002e | 2008-09-05 05:02:46 +0000 | [diff] [blame] | 3783 | return rc; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3784 | } |
| 3785 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3786 | /* Move the cursor to the first entry in the table. Return SQLITE_OK |
| 3787 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 3788 | ** or set *pRes to 1 if the table is empty. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3789 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3790 | int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3791 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3792 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3793 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3794 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3795 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3796 | if( rc==SQLITE_OK ){ |
| 3797 | if( pCur->eState==CURSOR_INVALID ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3798 | assert( pCur->apPage[pCur->iPage]->nCell==0 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3799 | *pRes = 1; |
| 3800 | rc = SQLITE_OK; |
| 3801 | }else{ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3802 | assert( pCur->apPage[pCur->iPage]->nCell>0 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3803 | *pRes = 0; |
| 3804 | rc = moveToLeftmost(pCur); |
| 3805 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3806 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3807 | return rc; |
| 3808 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3809 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3810 | /* Move the cursor to the last entry in the table. Return SQLITE_OK |
| 3811 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 3812 | ** or set *pRes to 1 if the table is empty. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3813 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3814 | int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3815 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3816 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3817 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3818 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3819 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3820 | if( rc==SQLITE_OK ){ |
| 3821 | if( CURSOR_INVALID==pCur->eState ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3822 | assert( pCur->apPage[pCur->iPage]->nCell==0 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3823 | *pRes = 1; |
| 3824 | }else{ |
| 3825 | assert( pCur->eState==CURSOR_VALID ); |
| 3826 | *pRes = 0; |
| 3827 | rc = moveToRightmost(pCur); |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3828 | getCellInfo(pCur); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3829 | pCur->atLast = rc==SQLITE_OK ?1:0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3830 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3831 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3832 | return rc; |
| 3833 | } |
| 3834 | |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 3835 | /* Move the cursor so that it points to an entry near the key |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3836 | ** specified by pIdxKey or intKey. Return a success code. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3837 | ** |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3838 | ** For INTKEY tables, the intKey parameter is used. pIdxKey |
| 3839 | ** must be NULL. For index tables, pIdxKey is used and intKey |
| 3840 | ** is ignored. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3841 | ** |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3842 | ** If an exact match is not found, then the cursor is always |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3843 | ** left pointing at a leaf page which would hold the entry if it |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3844 | ** were present. The cursor might point to an entry that comes |
| 3845 | ** before or after the key. |
| 3846 | ** |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 3847 | ** An integer is written into *pRes which is the result of |
| 3848 | ** comparing the key with the entry to which the cursor is |
| 3849 | ** pointing. The meaning of the integer written into |
| 3850 | ** *pRes is as follows: |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3851 | ** |
| 3852 | ** *pRes<0 The cursor is left pointing at an entry that |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 3853 | ** is smaller than intKey/pIdxKey or if the table is empty |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3854 | ** and the cursor is therefore left point to nothing. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3855 | ** |
| 3856 | ** *pRes==0 The cursor is left pointing at an entry that |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 3857 | ** exactly matches intKey/pIdxKey. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3858 | ** |
| 3859 | ** *pRes>0 The cursor is left pointing at an entry that |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 3860 | ** is larger than intKey/pIdxKey. |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3861 | ** |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3862 | */ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3863 | int sqlite3BtreeMovetoUnpacked( |
| 3864 | BtCursor *pCur, /* The cursor to be moved */ |
| 3865 | UnpackedRecord *pIdxKey, /* Unpacked index key */ |
| 3866 | i64 intKey, /* The table key */ |
| 3867 | int biasRight, /* If true, bias the search to the high end */ |
| 3868 | int *pRes /* Write search results here */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3869 | ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3870 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3871 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3872 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3873 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3874 | |
| 3875 | /* If the cursor is already positioned at the point we are trying |
| 3876 | ** to move to, then just return without doing any work */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3877 | if( pCur->eState==CURSOR_VALID && pCur->validNKey |
| 3878 | && pCur->apPage[0]->intKey |
| 3879 | ){ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3880 | if( pCur->info.nKey==intKey ){ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3881 | *pRes = 0; |
| 3882 | return SQLITE_OK; |
| 3883 | } |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3884 | if( pCur->atLast && pCur->info.nKey<intKey ){ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3885 | *pRes = -1; |
| 3886 | return SQLITE_OK; |
| 3887 | } |
| 3888 | } |
| 3889 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3890 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3891 | if( rc ){ |
| 3892 | return rc; |
| 3893 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3894 | assert( pCur->apPage[pCur->iPage] ); |
| 3895 | assert( pCur->apPage[pCur->iPage]->isInit ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3896 | if( pCur->eState==CURSOR_INVALID ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3897 | *pRes = -1; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3898 | assert( pCur->apPage[pCur->iPage]->nCell==0 ); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3899 | return SQLITE_OK; |
| 3900 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3901 | assert( pCur->apPage[0]->intKey || pIdxKey ); |
drh | 1468438 | 2006-11-30 13:05:29 +0000 | [diff] [blame] | 3902 | for(;;){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3903 | int lwr, upr; |
| 3904 | Pgno chldPg; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3905 | MemPage *pPage = pCur->apPage[pCur->iPage]; |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3906 | int c = -1; /* pRes return if table is empty must be -1 */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3907 | lwr = 0; |
| 3908 | upr = pPage->nCell-1; |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 3909 | if( (!pPage->intKey && pIdxKey==0) || upr<0 ){ |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3910 | rc = SQLITE_CORRUPT_BKPT; |
| 3911 | goto moveto_finish; |
drh | 4eec4c1 | 2005-01-21 00:22:37 +0000 | [diff] [blame] | 3912 | } |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3913 | if( biasRight ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3914 | pCur->aiIdx[pCur->iPage] = (u16)upr; |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3915 | }else{ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3916 | pCur->aiIdx[pCur->iPage] = (u16)((upr+lwr)/2); |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3917 | } |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 3918 | for(;;){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3919 | void *pCellKey; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3920 | i64 nCellKey; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3921 | int idx = pCur->aiIdx[pCur->iPage]; |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3922 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3923 | pCur->validNKey = 1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3924 | if( pPage->intKey ){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3925 | u8 *pCell; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3926 | pCell = findCell(pPage, idx) + pPage->childPtrSize; |
drh | d172f86 | 2006-01-12 15:01:15 +0000 | [diff] [blame] | 3927 | if( pPage->hasData ){ |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 3928 | u32 dummy; |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 3929 | pCell += getVarint32(pCell, dummy); |
drh | d172f86 | 2006-01-12 15:01:15 +0000 | [diff] [blame] | 3930 | } |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3931 | getVarint(pCell, (u64*)&nCellKey); |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3932 | if( nCellKey==intKey ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3933 | c = 0; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3934 | }else if( nCellKey<intKey ){ |
drh | 41eb9e9 | 2008-04-02 18:33:07 +0000 | [diff] [blame] | 3935 | c = -1; |
| 3936 | }else{ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 3937 | assert( nCellKey>intKey ); |
drh | 41eb9e9 | 2008-04-02 18:33:07 +0000 | [diff] [blame] | 3938 | c = +1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3939 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3940 | }else{ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3941 | int available; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3942 | pCellKey = (void *)fetchPayload(pCur, &available, 0); |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3943 | nCellKey = pCur->info.nKey; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3944 | if( available>=nCellKey ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3945 | c = sqlite3VdbeRecordCompare((int)nCellKey, pCellKey, pIdxKey); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3946 | }else{ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3947 | pCellKey = sqlite3Malloc( (int)nCellKey ); |
danielk1977 | 6507ecb | 2008-03-25 09:56:44 +0000 | [diff] [blame] | 3948 | if( pCellKey==0 ){ |
| 3949 | rc = SQLITE_NOMEM; |
| 3950 | goto moveto_finish; |
| 3951 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3952 | rc = sqlite3BtreeKey(pCur, 0, (int)nCellKey, (void*)pCellKey); |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 3953 | c = sqlite3VdbeRecordCompare((int)nCellKey, pCellKey, pIdxKey); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 3954 | sqlite3_free(pCellKey); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3955 | if( rc ) goto moveto_finish; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3956 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3957 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3958 | if( c==0 ){ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3959 | pCur->info.nKey = nCellKey; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 3960 | if( pPage->intKey && !pPage->leaf ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3961 | lwr = idx; |
drh | fc70e6f | 2004-05-12 21:11:27 +0000 | [diff] [blame] | 3962 | upr = lwr - 1; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3963 | break; |
| 3964 | }else{ |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 3965 | *pRes = 0; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3966 | rc = SQLITE_OK; |
| 3967 | goto moveto_finish; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3968 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3969 | } |
| 3970 | if( c<0 ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3971 | lwr = idx+1; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3972 | }else{ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3973 | upr = idx-1; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3974 | } |
drh | f1d68b3 | 2007-03-29 04:43:26 +0000 | [diff] [blame] | 3975 | if( lwr>upr ){ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3976 | pCur->info.nKey = nCellKey; |
drh | f1d68b3 | 2007-03-29 04:43:26 +0000 | [diff] [blame] | 3977 | break; |
| 3978 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3979 | pCur->aiIdx[pCur->iPage] = (u16)((lwr+upr)/2); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3980 | } |
| 3981 | assert( lwr==upr+1 ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3982 | assert( pPage->isInit ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3983 | if( pPage->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3984 | chldPg = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3985 | }else if( lwr>=pPage->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3986 | chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3987 | }else{ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3988 | chldPg = get4byte(findCell(pPage, lwr)); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3989 | } |
| 3990 | if( chldPg==0 ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3991 | assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3992 | if( pRes ) *pRes = c; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3993 | rc = SQLITE_OK; |
| 3994 | goto moveto_finish; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3995 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3996 | pCur->aiIdx[pCur->iPage] = (u16)lwr; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3997 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3998 | pCur->validNKey = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3999 | rc = moveToChild(pCur, chldPg); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 4000 | if( rc ) goto moveto_finish; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4001 | } |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 4002 | moveto_finish: |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4003 | return rc; |
| 4004 | } |
| 4005 | |
| 4006 | /* |
| 4007 | ** In this version of BtreeMoveto, pKey is a packed index record |
| 4008 | ** such as is generated by the OP_MakeRecord opcode. Unpack the |
| 4009 | ** record and then call BtreeMovetoUnpacked() to do the work. |
| 4010 | */ |
| 4011 | int sqlite3BtreeMoveto( |
| 4012 | BtCursor *pCur, /* Cursor open on the btree to be searched */ |
| 4013 | const void *pKey, /* Packed key if the btree is an index */ |
| 4014 | i64 nKey, /* Integer key for tables. Size of pKey for indices */ |
| 4015 | int bias, /* Bias search to the high end */ |
| 4016 | int *pRes /* Write search results here */ |
| 4017 | ){ |
| 4018 | int rc; /* Status code */ |
| 4019 | UnpackedRecord *pIdxKey; /* Unpacked index key */ |
drh | 23f79d0 | 2008-08-20 22:06:47 +0000 | [diff] [blame] | 4020 | UnpackedRecord aSpace[16]; /* Temp space for pIdxKey - to avoid a malloc */ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4021 | |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 4022 | if( pKey ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4023 | assert( nKey==(i64)(int)nKey ); |
| 4024 | pIdxKey = sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, |
drh | 23f79d0 | 2008-08-20 22:06:47 +0000 | [diff] [blame] | 4025 | aSpace, sizeof(aSpace)); |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4026 | if( pIdxKey==0 ) return SQLITE_NOMEM; |
| 4027 | }else{ |
| 4028 | pIdxKey = 0; |
| 4029 | } |
| 4030 | rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes); |
| 4031 | if( pKey ){ |
| 4032 | sqlite3VdbeDeleteUnpackedRecord(pIdxKey); |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 4033 | } |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 4034 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4035 | } |
| 4036 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4037 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4038 | /* |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 4039 | ** Return TRUE if the cursor is not pointing at an entry of the table. |
| 4040 | ** |
| 4041 | ** TRUE will be returned after a call to sqlite3BtreeNext() moves |
| 4042 | ** past the last entry in the table or sqlite3BtreePrev() moves past |
| 4043 | ** the first entry. TRUE is also returned if the table is empty. |
| 4044 | */ |
| 4045 | int sqlite3BtreeEof(BtCursor *pCur){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4046 | /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries |
| 4047 | ** have been deleted? This API will need to change to return an error code |
| 4048 | ** as well as the boolean result value. |
| 4049 | */ |
| 4050 | return (CURSOR_VALID!=pCur->eState); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 4051 | } |
| 4052 | |
| 4053 | /* |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 4054 | ** Return the database connection handle for a cursor. |
| 4055 | */ |
| 4056 | sqlite3 *sqlite3BtreeCursorDb(const BtCursor *pCur){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 4057 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
| 4058 | return pCur->pBtree->db; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 4059 | } |
| 4060 | |
| 4061 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4062 | ** Advance the cursor to the next entry in the database. If |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4063 | ** successful then set *pRes=0. If the cursor |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4064 | ** was already pointing to the last entry in the database before |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4065 | ** this routine was called, then set *pRes=1. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4066 | */ |
drh | d094db1 | 2008-04-03 21:46:57 +0000 | [diff] [blame] | 4067 | int sqlite3BtreeNext(BtCursor *pCur, int *pRes){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4068 | int rc; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4069 | int idx; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 4070 | MemPage *pPage; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4071 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4072 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 4073 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4074 | if( rc!=SQLITE_OK ){ |
| 4075 | return rc; |
| 4076 | } |
drh | 8c4d3a6 | 2007-04-06 01:03:32 +0000 | [diff] [blame] | 4077 | assert( pRes!=0 ); |
drh | 8c4d3a6 | 2007-04-06 01:03:32 +0000 | [diff] [blame] | 4078 | if( CURSOR_INVALID==pCur->eState ){ |
| 4079 | *pRes = 1; |
| 4080 | return SQLITE_OK; |
| 4081 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4082 | if( pCur->skip>0 ){ |
| 4083 | pCur->skip = 0; |
| 4084 | *pRes = 0; |
| 4085 | return SQLITE_OK; |
| 4086 | } |
| 4087 | pCur->skip = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4088 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4089 | pPage = pCur->apPage[pCur->iPage]; |
| 4090 | idx = ++pCur->aiIdx[pCur->iPage]; |
| 4091 | assert( pPage->isInit ); |
| 4092 | assert( idx<=pPage->nCell ); |
danielk1977 | 6a43f9b | 2004-11-16 04:57:24 +0000 | [diff] [blame] | 4093 | |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 4094 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 4095 | pCur->validNKey = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4096 | if( idx>=pPage->nCell ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4097 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4098 | rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4099 | if( rc ) return rc; |
| 4100 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4101 | *pRes = 0; |
| 4102 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4103 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4104 | do{ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4105 | if( pCur->iPage==0 ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4106 | *pRes = 1; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4107 | pCur->eState = CURSOR_INVALID; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4108 | return SQLITE_OK; |
| 4109 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4110 | sqlite3BtreeMoveToParent(pCur); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4111 | pPage = pCur->apPage[pCur->iPage]; |
| 4112 | }while( pCur->aiIdx[pCur->iPage]>=pPage->nCell ); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4113 | *pRes = 0; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 4114 | if( pPage->intKey ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4115 | rc = sqlite3BtreeNext(pCur, pRes); |
| 4116 | }else{ |
| 4117 | rc = SQLITE_OK; |
| 4118 | } |
| 4119 | return rc; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4120 | } |
| 4121 | *pRes = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4122 | if( pPage->leaf ){ |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4123 | return SQLITE_OK; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4124 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4125 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4126 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4127 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4128 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4129 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4130 | /* |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4131 | ** 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] | 4132 | ** successful then set *pRes=0. If the cursor |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4133 | ** was already pointing to the first entry in the database before |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4134 | ** this routine was called, then set *pRes=1. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4135 | */ |
drh | d094db1 | 2008-04-03 21:46:57 +0000 | [diff] [blame] | 4136 | int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){ |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4137 | int rc; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4138 | MemPage *pPage; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4139 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4140 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 4141 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4142 | if( rc!=SQLITE_OK ){ |
| 4143 | return rc; |
| 4144 | } |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 4145 | pCur->atLast = 0; |
drh | 8c4d3a6 | 2007-04-06 01:03:32 +0000 | [diff] [blame] | 4146 | if( CURSOR_INVALID==pCur->eState ){ |
| 4147 | *pRes = 1; |
| 4148 | return SQLITE_OK; |
| 4149 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4150 | if( pCur->skip<0 ){ |
| 4151 | pCur->skip = 0; |
| 4152 | *pRes = 0; |
| 4153 | return SQLITE_OK; |
| 4154 | } |
| 4155 | pCur->skip = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4156 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4157 | pPage = pCur->apPage[pCur->iPage]; |
| 4158 | assert( pPage->isInit ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4159 | if( !pPage->leaf ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4160 | int idx = pCur->aiIdx[pCur->iPage]; |
| 4161 | rc = moveToChild(pCur, get4byte(findCell(pPage, idx))); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4162 | if( rc ){ |
| 4163 | return rc; |
| 4164 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4165 | rc = moveToRightmost(pCur); |
| 4166 | }else{ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4167 | while( pCur->aiIdx[pCur->iPage]==0 ){ |
| 4168 | if( pCur->iPage==0 ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4169 | pCur->eState = CURSOR_INVALID; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 4170 | *pRes = 1; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4171 | return SQLITE_OK; |
| 4172 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4173 | sqlite3BtreeMoveToParent(pCur); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4174 | } |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 4175 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 4176 | pCur->validNKey = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4177 | |
| 4178 | pCur->aiIdx[pCur->iPage]--; |
| 4179 | pPage = pCur->apPage[pCur->iPage]; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 4180 | if( pPage->intKey && !pPage->leaf ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4181 | rc = sqlite3BtreePrevious(pCur, pRes); |
| 4182 | }else{ |
| 4183 | rc = SQLITE_OK; |
| 4184 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4185 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4186 | *pRes = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4187 | return rc; |
| 4188 | } |
| 4189 | |
| 4190 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4191 | ** Allocate a new page from the database file. |
| 4192 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4193 | ** The new page is marked as dirty. (In other words, sqlite3PagerWrite() |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4194 | ** has already been called on the new page.) The new page has also |
| 4195 | ** been referenced and the calling routine is responsible for calling |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4196 | ** sqlite3PagerUnref() on the new page when it is done. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4197 | ** |
| 4198 | ** SQLITE_OK is returned on success. Any other return value indicates |
| 4199 | ** an error. *ppPage and *pPgno are undefined in the event of an error. |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4200 | ** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned. |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 4201 | ** |
drh | 199e3cf | 2002-07-18 11:01:47 +0000 | [diff] [blame] | 4202 | ** If the "nearby" parameter is not 0, then a (feeble) effort is made to |
| 4203 | ** 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] | 4204 | ** attempt to keep related pages close to each other in the database file, |
| 4205 | ** which in turn can make database access faster. |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4206 | ** |
| 4207 | ** If the "exact" parameter is not 0, and the page-number nearby exists |
| 4208 | ** anywhere on the free-list, then it is guarenteed to be returned. This |
| 4209 | ** is only used by auto-vacuum databases when allocating a new table. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4210 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4211 | static int allocateBtreePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4212 | BtShared *pBt, |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4213 | MemPage **ppPage, |
| 4214 | Pgno *pPgno, |
| 4215 | Pgno nearby, |
| 4216 | u8 exact |
| 4217 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4218 | MemPage *pPage1; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4219 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4220 | int n; /* Number of pages on the freelist */ |
| 4221 | int k; /* Number of leaves on the trunk of the freelist */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4222 | MemPage *pTrunk = 0; |
| 4223 | MemPage *pPrevTrunk = 0; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4224 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4225 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4226 | pPage1 = pBt->pPage1; |
| 4227 | n = get4byte(&pPage1->aData[36]); |
| 4228 | if( n>0 ){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4229 | /* There are pages on the freelist. Reuse one of those pages. */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4230 | Pgno iTrunk; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4231 | u8 searchList = 0; /* If the free-list must be searched for 'nearby' */ |
| 4232 | |
| 4233 | /* If the 'exact' parameter was true and a query of the pointer-map |
| 4234 | ** shows that the page 'nearby' is somewhere on the free-list, then |
| 4235 | ** the entire-list will be searched for that page. |
| 4236 | */ |
| 4237 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 4238 | if( exact && nearby<=pagerPagecount(pBt) ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4239 | u8 eType; |
| 4240 | assert( nearby>0 ); |
| 4241 | assert( pBt->autoVacuum ); |
| 4242 | rc = ptrmapGet(pBt, nearby, &eType, 0); |
| 4243 | if( rc ) return rc; |
| 4244 | if( eType==PTRMAP_FREEPAGE ){ |
| 4245 | searchList = 1; |
| 4246 | } |
| 4247 | *pPgno = nearby; |
| 4248 | } |
| 4249 | #endif |
| 4250 | |
| 4251 | /* Decrement the free-list count by 1. Set iTrunk to the index of the |
| 4252 | ** first free-list trunk page. iPrevTrunk is initially 1. |
| 4253 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4254 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4255 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4256 | put4byte(&pPage1->aData[36], n-1); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4257 | |
| 4258 | /* The code within this loop is run only once if the 'searchList' variable |
| 4259 | ** is not true. Otherwise, it runs once for each trunk-page on the |
| 4260 | ** free-list until the page 'nearby' is located. |
| 4261 | */ |
| 4262 | do { |
| 4263 | pPrevTrunk = pTrunk; |
| 4264 | if( pPrevTrunk ){ |
| 4265 | iTrunk = get4byte(&pPrevTrunk->aData[0]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 4266 | }else{ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4267 | iTrunk = get4byte(&pPage1->aData[32]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 4268 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4269 | rc = sqlite3BtreeGetPage(pBt, iTrunk, &pTrunk, 0); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4270 | if( rc ){ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4271 | pTrunk = 0; |
| 4272 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4273 | } |
| 4274 | |
| 4275 | k = get4byte(&pTrunk->aData[4]); |
| 4276 | if( k==0 && !searchList ){ |
| 4277 | /* The trunk has no leaves and the list is not being searched. |
| 4278 | ** So extract the trunk page itself and use it as the newly |
| 4279 | ** allocated page */ |
| 4280 | assert( pPrevTrunk==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4281 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4282 | if( rc ){ |
| 4283 | goto end_allocate_page; |
| 4284 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4285 | *pPgno = iTrunk; |
| 4286 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 4287 | *ppPage = pTrunk; |
| 4288 | pTrunk = 0; |
| 4289 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
drh | 45b1fac | 2008-07-04 17:52:42 +0000 | [diff] [blame] | 4290 | }else if( k>pBt->usableSize/4 - 2 ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4291 | /* Value of k is out of range. Database corruption */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4292 | rc = SQLITE_CORRUPT_BKPT; |
| 4293 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4294 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4295 | }else if( searchList && nearby==iTrunk ){ |
| 4296 | /* The list is being searched and this trunk page is the page |
| 4297 | ** to allocate, regardless of whether it has leaves. |
| 4298 | */ |
| 4299 | assert( *pPgno==iTrunk ); |
| 4300 | *ppPage = pTrunk; |
| 4301 | searchList = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4302 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4303 | if( rc ){ |
| 4304 | goto end_allocate_page; |
| 4305 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4306 | if( k==0 ){ |
| 4307 | if( !pPrevTrunk ){ |
| 4308 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 4309 | }else{ |
| 4310 | memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4); |
| 4311 | } |
| 4312 | }else{ |
| 4313 | /* The trunk page is required by the caller but it contains |
| 4314 | ** pointers to free-list leaves. The first leaf becomes a trunk |
| 4315 | ** page in this case. |
| 4316 | */ |
| 4317 | MemPage *pNewTrunk; |
| 4318 | Pgno iNewTrunk = get4byte(&pTrunk->aData[8]); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4319 | rc = sqlite3BtreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4320 | if( rc!=SQLITE_OK ){ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4321 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4322 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4323 | rc = sqlite3PagerWrite(pNewTrunk->pDbPage); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4324 | if( rc!=SQLITE_OK ){ |
| 4325 | releasePage(pNewTrunk); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4326 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4327 | } |
| 4328 | memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4); |
| 4329 | put4byte(&pNewTrunk->aData[4], k-1); |
| 4330 | memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4331 | releasePage(pNewTrunk); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4332 | if( !pPrevTrunk ){ |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 4333 | assert( sqlite3PagerIswriteable(pPage1->pDbPage) ); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4334 | put4byte(&pPage1->aData[32], iNewTrunk); |
| 4335 | }else{ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4336 | rc = sqlite3PagerWrite(pPrevTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4337 | if( rc ){ |
| 4338 | goto end_allocate_page; |
| 4339 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4340 | put4byte(&pPrevTrunk->aData[0], iNewTrunk); |
| 4341 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4342 | } |
| 4343 | pTrunk = 0; |
| 4344 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
| 4345 | #endif |
| 4346 | }else{ |
| 4347 | /* Extract a leaf from the trunk */ |
| 4348 | int closest; |
| 4349 | Pgno iPage; |
| 4350 | unsigned char *aData = pTrunk->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4351 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4352 | if( rc ){ |
| 4353 | goto end_allocate_page; |
| 4354 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4355 | if( nearby>0 ){ |
| 4356 | int i, dist; |
| 4357 | closest = 0; |
| 4358 | dist = get4byte(&aData[8]) - nearby; |
| 4359 | if( dist<0 ) dist = -dist; |
| 4360 | for(i=1; i<k; i++){ |
| 4361 | int d2 = get4byte(&aData[8+i*4]) - nearby; |
| 4362 | if( d2<0 ) d2 = -d2; |
| 4363 | if( d2<dist ){ |
| 4364 | closest = i; |
| 4365 | dist = d2; |
| 4366 | } |
| 4367 | } |
| 4368 | }else{ |
| 4369 | closest = 0; |
| 4370 | } |
| 4371 | |
| 4372 | iPage = get4byte(&aData[8+closest*4]); |
| 4373 | if( !searchList || iPage==nearby ){ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4374 | int noContent; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 4375 | Pgno nPage; |
shane | 1f9e6aa | 2008-06-09 19:27:11 +0000 | [diff] [blame] | 4376 | *pPgno = iPage; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 4377 | nPage = pagerPagecount(pBt); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 4378 | if( *pPgno>nPage ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4379 | /* Free page off the end of the file */ |
danielk1977 | 43e377a | 2008-05-05 12:09:32 +0000 | [diff] [blame] | 4380 | rc = SQLITE_CORRUPT_BKPT; |
| 4381 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4382 | } |
| 4383 | TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d" |
| 4384 | ": %d more free pages\n", |
| 4385 | *pPgno, closest+1, k, pTrunk->pgno, n-1)); |
| 4386 | if( closest<k-1 ){ |
| 4387 | memcpy(&aData[8+closest*4], &aData[4+k*4], 4); |
| 4388 | } |
| 4389 | put4byte(&aData[4], k-1); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 4390 | assert( sqlite3PagerIswriteable(pTrunk->pDbPage) ); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4391 | noContent = !btreeGetHasContent(pBt, *pPgno); |
| 4392 | rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, noContent); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4393 | if( rc==SQLITE_OK ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4394 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 4395 | if( rc!=SQLITE_OK ){ |
| 4396 | releasePage(*ppPage); |
| 4397 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4398 | } |
| 4399 | searchList = 0; |
| 4400 | } |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 4401 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4402 | releasePage(pPrevTrunk); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4403 | pPrevTrunk = 0; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4404 | }while( searchList ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4405 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4406 | /* There are no pages on the freelist, so create a new page at the |
| 4407 | ** end of the file */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 4408 | int nPage = pagerPagecount(pBt); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 4409 | *pPgno = nPage + 1; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4410 | |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4411 | if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ |
| 4412 | (*pPgno)++; |
| 4413 | } |
| 4414 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4415 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 4416 | if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, *pPgno) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4417 | /* If *pPgno refers to a pointer-map page, allocate two new pages |
| 4418 | ** at the end of the file instead of one. The first allocated page |
| 4419 | ** becomes a new pointer-map page, the second is used by the caller. |
| 4420 | */ |
| 4421 | TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno)); |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4422 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4423 | (*pPgno)++; |
drh | 7219043 | 2008-01-31 14:54:43 +0000 | [diff] [blame] | 4424 | if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ (*pPgno)++; } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4425 | } |
| 4426 | #endif |
| 4427 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4428 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4429 | rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 0); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4430 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4431 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 4432 | if( rc!=SQLITE_OK ){ |
| 4433 | releasePage(*ppPage); |
| 4434 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4435 | TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4436 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4437 | |
| 4438 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4439 | |
| 4440 | end_allocate_page: |
| 4441 | releasePage(pTrunk); |
| 4442 | releasePage(pPrevTrunk); |
danielk1977 | b247c21 | 2008-11-21 09:09:01 +0000 | [diff] [blame] | 4443 | if( rc==SQLITE_OK ){ |
| 4444 | if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){ |
| 4445 | releasePage(*ppPage); |
| 4446 | return SQLITE_CORRUPT_BKPT; |
| 4447 | } |
| 4448 | (*ppPage)->isInit = 0; |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 4449 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4450 | return rc; |
| 4451 | } |
| 4452 | |
| 4453 | /* |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4454 | ** This function is used to add page iPage to the database file free-list. |
| 4455 | ** 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] | 4456 | ** |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4457 | ** The value passed as the second argument to this function is optional. |
| 4458 | ** If the caller happens to have a pointer to the MemPage object |
| 4459 | ** corresponding to page iPage handy, it may pass it as the second value. |
| 4460 | ** Otherwise, it may pass NULL. |
| 4461 | ** |
| 4462 | ** If a pointer to a MemPage object is passed as the second argument, |
| 4463 | ** its reference count is not altered by this function. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4464 | */ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4465 | static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){ |
| 4466 | MemPage *pTrunk = 0; /* Free-list trunk page */ |
| 4467 | Pgno iTrunk = 0; /* Page number of free-list trunk page */ |
| 4468 | MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */ |
| 4469 | MemPage *pPage; /* Page being freed. May be NULL. */ |
| 4470 | int rc; /* Return Code */ |
| 4471 | int nFree; /* Initial number of pages on free-list */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4472 | |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4473 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 4474 | assert( iPage>1 ); |
| 4475 | assert( !pMemPage || pMemPage->pgno==iPage ); |
| 4476 | |
| 4477 | if( pMemPage ){ |
| 4478 | pPage = pMemPage; |
| 4479 | sqlite3PagerRef(pPage->pDbPage); |
| 4480 | }else{ |
| 4481 | pPage = btreePageLookup(pBt, iPage); |
| 4482 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4483 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4484 | /* Increment the free page count on pPage1 */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4485 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4486 | if( rc ) goto freepage_out; |
| 4487 | nFree = get4byte(&pPage1->aData[36]); |
| 4488 | put4byte(&pPage1->aData[36], nFree+1); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4489 | |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4490 | #ifdef SQLITE_SECURE_DELETE |
| 4491 | /* If the SQLITE_SECURE_DELETE compile-time option is enabled, then |
| 4492 | ** always fully overwrite deleted information with zeros. |
| 4493 | */ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4494 | if( (!pPage && (rc = sqlite3BtreeGetPage(pBt, iPage, &pPage, 0))) |
| 4495 | || (rc = sqlite3PagerWrite(pPage->pDbPage)) |
| 4496 | ){ |
| 4497 | goto freepage_out; |
| 4498 | } |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4499 | memset(pPage->aData, 0, pPage->pBt->pageSize); |
| 4500 | #endif |
| 4501 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4502 | /* If the database supports auto-vacuum, write an entry in the pointer-map |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4503 | ** to indicate that the page is free. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4504 | */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 4505 | if( ISAUTOVACUUM ){ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4506 | rc = ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0); |
| 4507 | if( rc ) goto freepage_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4508 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4509 | |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4510 | /* Now manipulate the actual database free-list structure. There are two |
| 4511 | ** possibilities. If the free-list is currently empty, or if the first |
| 4512 | ** trunk page in the free-list is full, then this page will become a |
| 4513 | ** new free-list trunk page. Otherwise, it will become a leaf of the |
| 4514 | ** first trunk page in the current free-list. This block tests if it |
| 4515 | ** is possible to add the page as a new free-list leaf. |
| 4516 | */ |
| 4517 | if( nFree!=0 ){ |
| 4518 | int nLeaf; /* Initial number of leaf cells on trunk page */ |
| 4519 | |
| 4520 | iTrunk = get4byte(&pPage1->aData[32]); |
| 4521 | rc = sqlite3BtreeGetPage(pBt, iTrunk, &pTrunk, 0); |
| 4522 | if( rc!=SQLITE_OK ){ |
| 4523 | goto freepage_out; |
| 4524 | } |
| 4525 | |
| 4526 | nLeaf = get4byte(&pTrunk->aData[4]); |
| 4527 | if( nLeaf<0 ){ |
| 4528 | rc = SQLITE_CORRUPT_BKPT; |
| 4529 | goto freepage_out; |
| 4530 | } |
| 4531 | if( nLeaf<pBt->usableSize/4 - 8 ){ |
| 4532 | /* In this case there is room on the trunk page to insert the page |
| 4533 | ** being freed as a new leaf. |
drh | 45b1fac | 2008-07-04 17:52:42 +0000 | [diff] [blame] | 4534 | ** |
| 4535 | ** Note that the trunk page is not really full until it contains |
| 4536 | ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have |
| 4537 | ** coded. But due to a coding error in versions of SQLite prior to |
| 4538 | ** 3.6.0, databases with freelist trunk pages holding more than |
| 4539 | ** usableSize/4 - 8 entries will be reported as corrupt. In order |
| 4540 | ** to maintain backwards compatibility with older versions of SQLite, |
| 4541 | ** we will contain to restrict the number of entries to usableSize/4 - 8 |
| 4542 | ** for now. At some point in the future (once everyone has upgraded |
| 4543 | ** to 3.6.0 or later) we should consider fixing the conditional above |
| 4544 | ** to read "usableSize/4-2" instead of "usableSize/4-8". |
| 4545 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4546 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 4547 | if( rc==SQLITE_OK ){ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4548 | put4byte(&pTrunk->aData[4], nLeaf+1); |
| 4549 | put4byte(&pTrunk->aData[8+nLeaf*4], iPage); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4550 | #ifndef SQLITE_SECURE_DELETE |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4551 | if( pPage ){ |
| 4552 | sqlite3PagerDontWrite(pPage->pDbPage); |
| 4553 | } |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4554 | #endif |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4555 | rc = btreeSetHasContent(pBt, iPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 4556 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4557 | 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] | 4558 | goto freepage_out; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4559 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4560 | } |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4561 | |
| 4562 | /* If control flows to this point, then it was not possible to add the |
| 4563 | ** the page being freed as a leaf page of the first trunk in the free-list. |
| 4564 | ** Possibly because the free-list is empty, or possibly because the |
| 4565 | ** first trunk in the free-list is full. Either way, the page being freed |
| 4566 | ** will become the new first trunk page in the free-list. |
| 4567 | */ |
shane | 63207ab | 2009-02-04 01:49:30 +0000 | [diff] [blame] | 4568 | if( ((!pPage) && (0 != (rc = sqlite3BtreeGetPage(pBt, iPage, &pPage, 0)))) |
| 4569 | || (0 != (rc = sqlite3PagerWrite(pPage->pDbPage))) |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4570 | ){ |
| 4571 | goto freepage_out; |
| 4572 | } |
| 4573 | put4byte(pPage->aData, iTrunk); |
| 4574 | put4byte(&pPage->aData[4], 0); |
| 4575 | put4byte(&pPage1->aData[32], iPage); |
| 4576 | TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk)); |
| 4577 | |
| 4578 | freepage_out: |
| 4579 | if( pPage ){ |
| 4580 | pPage->isInit = 0; |
| 4581 | } |
| 4582 | releasePage(pPage); |
| 4583 | releasePage(pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4584 | return rc; |
| 4585 | } |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4586 | static int freePage(MemPage *pPage){ |
| 4587 | return freePage2(pPage->pBt, pPage, pPage->pgno); |
| 4588 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4589 | |
| 4590 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4591 | ** Free any overflow pages associated with the given Cell. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4592 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4593 | static int clearCell(MemPage *pPage, unsigned char *pCell){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4594 | BtShared *pBt = pPage->pBt; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4595 | CellInfo info; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4596 | Pgno ovflPgno; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4597 | int rc; |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 4598 | int nOvfl; |
shane | 63207ab | 2009-02-04 01:49:30 +0000 | [diff] [blame] | 4599 | u16 ovflPageSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4600 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4601 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4602 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4603 | if( info.iOverflow==0 ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4604 | return SQLITE_OK; /* No overflow pages. Return without doing anything */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4605 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4606 | ovflPgno = get4byte(&pCell[info.iOverflow]); |
shane | 63207ab | 2009-02-04 01:49:30 +0000 | [diff] [blame] | 4607 | assert( pBt->usableSize > 4 ); |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 4608 | ovflPageSize = pBt->usableSize - 4; |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 4609 | nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize; |
| 4610 | assert( ovflPgno==0 || nOvfl>0 ); |
| 4611 | while( nOvfl-- ){ |
shane | 63207ab | 2009-02-04 01:49:30 +0000 | [diff] [blame] | 4612 | Pgno iNext = 0; |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4613 | MemPage *pOvfl = 0; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 4614 | if( ovflPgno==0 || ovflPgno>pagerPagecount(pBt) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 4615 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 4616 | } |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4617 | if( nOvfl ){ |
| 4618 | rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext); |
| 4619 | if( rc ) return rc; |
| 4620 | } |
| 4621 | rc = freePage2(pBt, pOvfl, ovflPgno); |
| 4622 | if( pOvfl ){ |
| 4623 | sqlite3PagerUnref(pOvfl->pDbPage); |
| 4624 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4625 | if( rc ) return rc; |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4626 | ovflPgno = iNext; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4627 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4628 | return SQLITE_OK; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4629 | } |
| 4630 | |
| 4631 | /* |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4632 | ** Create the byte sequence used to represent a cell on page pPage |
| 4633 | ** and write that byte sequence into pCell[]. Overflow pages are |
| 4634 | ** allocated and filled in as necessary. The calling procedure |
| 4635 | ** is responsible for making sure sufficient space has been allocated |
| 4636 | ** for pCell[]. |
| 4637 | ** |
| 4638 | ** Note that pCell does not necessary need to point to the pPage->aData |
| 4639 | ** area. pCell might point to some temporary storage. The cell will |
| 4640 | ** be constructed in this temporary area then copied into pPage->aData |
| 4641 | ** later. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4642 | */ |
| 4643 | static int fillInCell( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4644 | MemPage *pPage, /* The page that contains the cell */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4645 | unsigned char *pCell, /* Complete text of the cell */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 4646 | const void *pKey, i64 nKey, /* The key */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4647 | const void *pData,int nData, /* The data */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4648 | int nZero, /* Extra zero bytes to append to pData */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4649 | int *pnSize /* Write cell size here */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4650 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4651 | int nPayload; |
drh | 8c6fa9b | 2004-05-26 00:01:53 +0000 | [diff] [blame] | 4652 | const u8 *pSrc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4653 | int nSrc, n, rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4654 | int spaceLeft; |
| 4655 | MemPage *pOvfl = 0; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4656 | MemPage *pToRelease = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4657 | unsigned char *pPrior; |
| 4658 | unsigned char *pPayload; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4659 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4660 | Pgno pgnoOvfl = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4661 | int nHeader; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4662 | CellInfo info; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4663 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4664 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4665 | |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 4666 | /* pPage is not necessarily writeable since pCell might be auxiliary |
| 4667 | ** buffer space that is separate from the pPage buffer area */ |
| 4668 | assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize] |
| 4669 | || sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 4670 | |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4671 | /* Fill in the header. */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4672 | nHeader = 0; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4673 | if( !pPage->leaf ){ |
| 4674 | nHeader += 4; |
| 4675 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4676 | if( pPage->hasData ){ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4677 | nHeader += putVarint(&pCell[nHeader], nData+nZero); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4678 | }else{ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4679 | nData = nZero = 0; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4680 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4681 | nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4682 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4683 | assert( info.nHeader==nHeader ); |
| 4684 | assert( info.nKey==nKey ); |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 4685 | assert( info.nData==(u32)(nData+nZero) ); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4686 | |
| 4687 | /* Fill in the payload */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4688 | nPayload = nData + nZero; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4689 | if( pPage->intKey ){ |
| 4690 | pSrc = pData; |
| 4691 | nSrc = nData; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4692 | nData = 0; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4693 | }else{ |
drh | 20abac2 | 2009-01-28 20:21:17 +0000 | [diff] [blame] | 4694 | if( nKey>0x7fffffff || pKey==0 ){ |
| 4695 | return SQLITE_CORRUPT; |
| 4696 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4697 | nPayload += (int)nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4698 | pSrc = pKey; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4699 | nSrc = (int)nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4700 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4701 | *pnSize = info.nSize; |
| 4702 | spaceLeft = info.nLocal; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4703 | pPayload = &pCell[nHeader]; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4704 | pPrior = &pCell[info.iOverflow]; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4705 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4706 | while( nPayload>0 ){ |
| 4707 | if( spaceLeft==0 ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4708 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4709 | Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */ |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4710 | if( pBt->autoVacuum ){ |
| 4711 | do{ |
| 4712 | pgnoOvfl++; |
| 4713 | } while( |
| 4714 | PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt) |
| 4715 | ); |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4716 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4717 | #endif |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4718 | rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4719 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4720 | /* If the database supports auto-vacuum, and the second or subsequent |
| 4721 | ** overflow page is being allocated, add an entry to the pointer-map |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 4722 | ** for that page now. |
| 4723 | ** |
| 4724 | ** If this is the first overflow page, then write a partial entry |
| 4725 | ** to the pointer-map. If we write nothing to this pointer-map slot, |
| 4726 | ** then the optimistic overflow chain processing in clearCell() |
| 4727 | ** may misinterpret the uninitialised values and delete the |
| 4728 | ** wrong pages from the database. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4729 | */ |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 4730 | if( pBt->autoVacuum && rc==SQLITE_OK ){ |
| 4731 | u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1); |
| 4732 | rc = ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap); |
danielk1977 | 89a4be8 | 2007-05-23 13:34:32 +0000 | [diff] [blame] | 4733 | if( rc ){ |
| 4734 | releasePage(pOvfl); |
| 4735 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4736 | } |
| 4737 | #endif |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4738 | if( rc ){ |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4739 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4740 | return rc; |
| 4741 | } |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 4742 | |
| 4743 | /* If pToRelease is not zero than pPrior points into the data area |
| 4744 | ** of pToRelease. Make sure pToRelease is still writeable. */ |
| 4745 | assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) ); |
| 4746 | |
| 4747 | /* If pPrior is part of the data area of pPage, then make sure pPage |
| 4748 | ** is still writeable */ |
| 4749 | assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize] |
| 4750 | || sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 4751 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4752 | put4byte(pPrior, pgnoOvfl); |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4753 | releasePage(pToRelease); |
| 4754 | pToRelease = pOvfl; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4755 | pPrior = pOvfl->aData; |
| 4756 | put4byte(pPrior, 0); |
| 4757 | pPayload = &pOvfl->aData[4]; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4758 | spaceLeft = pBt->usableSize - 4; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4759 | } |
| 4760 | n = nPayload; |
| 4761 | if( n>spaceLeft ) n = spaceLeft; |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 4762 | |
| 4763 | /* If pToRelease is not zero than pPayload points into the data area |
| 4764 | ** of pToRelease. Make sure pToRelease is still writeable. */ |
| 4765 | assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) ); |
| 4766 | |
| 4767 | /* If pPayload is part of the data area of pPage, then make sure pPage |
| 4768 | ** is still writeable */ |
| 4769 | assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize] |
| 4770 | || sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 4771 | |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4772 | if( nSrc>0 ){ |
| 4773 | if( n>nSrc ) n = nSrc; |
| 4774 | assert( pSrc ); |
| 4775 | memcpy(pPayload, pSrc, n); |
| 4776 | }else{ |
| 4777 | memset(pPayload, 0, n); |
| 4778 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4779 | nPayload -= n; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4780 | pPayload += n; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4781 | pSrc += n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4782 | nSrc -= n; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4783 | spaceLeft -= n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4784 | if( nSrc==0 ){ |
| 4785 | nSrc = nData; |
| 4786 | pSrc = pData; |
| 4787 | } |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4788 | } |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4789 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4790 | return SQLITE_OK; |
| 4791 | } |
| 4792 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4793 | /* |
| 4794 | ** Remove the i-th cell from pPage. This routine effects pPage only. |
| 4795 | ** The cell content is not freed or deallocated. It is assumed that |
| 4796 | ** the cell content has been copied someplace else. This routine just |
| 4797 | ** removes the reference to the cell from pPage. |
| 4798 | ** |
| 4799 | ** "sz" must be the number of bytes in the cell. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4800 | */ |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 4801 | static int dropCell(MemPage *pPage, int idx, int sz){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4802 | int i; /* Loop counter */ |
| 4803 | int pc; /* Offset to cell content of cell being deleted */ |
| 4804 | u8 *data; /* pPage->aData */ |
| 4805 | u8 *ptr; /* Used to move bytes around within data[] */ |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 4806 | int rc; /* The return code */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4807 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4808 | assert( idx>=0 && idx<pPage->nCell ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4809 | assert( sz==cellSize(pPage, idx) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4810 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4811 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4812 | data = pPage->aData; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4813 | ptr = &data[pPage->cellOffset + 2*idx]; |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 4814 | pc = get2byte(ptr); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 4815 | if( (pc<pPage->hdrOffset+6+(pPage->leaf?0:4)) |
| 4816 | || (pc+sz>pPage->pBt->usableSize) ){ |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 4817 | return SQLITE_CORRUPT_BKPT; |
| 4818 | } |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 4819 | rc = freeSpace(pPage, pc, sz); |
| 4820 | if( rc!=SQLITE_OK ){ |
| 4821 | return rc; |
| 4822 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4823 | for(i=idx+1; i<pPage->nCell; i++, ptr+=2){ |
| 4824 | ptr[0] = ptr[2]; |
| 4825 | ptr[1] = ptr[3]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4826 | } |
| 4827 | pPage->nCell--; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4828 | put2byte(&data[pPage->hdrOffset+3], pPage->nCell); |
| 4829 | pPage->nFree += 2; |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 4830 | return SQLITE_OK; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4831 | } |
| 4832 | |
| 4833 | /* |
| 4834 | ** Insert a new cell on pPage at cell index "i". pCell points to the |
| 4835 | ** content of the cell. |
| 4836 | ** |
| 4837 | ** 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] | 4838 | ** will not fit, then make a copy of the cell content into pTemp if |
| 4839 | ** pTemp is not null. Regardless of pTemp, allocate a new entry |
| 4840 | ** in pPage->aOvfl[] and make it point to the cell content (either |
| 4841 | ** in pTemp or the original pCell) and also record its index. |
| 4842 | ** Allocating a new entry in pPage->aCell[] implies that |
| 4843 | ** pPage->nOverflow is incremented. |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4844 | ** |
| 4845 | ** If nSkip is non-zero, then do not copy the first nSkip bytes of the |
| 4846 | ** cell. The caller will overwrite them after this function returns. If |
drh | 4b238df | 2005-01-08 15:43:18 +0000 | [diff] [blame] | 4847 | ** 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] | 4848 | ** (but pCell+nSkip is always valid). |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4849 | */ |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4850 | static int insertCell( |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4851 | MemPage *pPage, /* Page into which we are copying */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4852 | int i, /* New cell becomes the i-th cell of the page */ |
| 4853 | u8 *pCell, /* Content of the new cell */ |
| 4854 | int sz, /* Bytes of content in pCell */ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4855 | u8 *pTemp, /* Temp storage space for pCell, if needed */ |
| 4856 | u8 nSkip /* Do not write the first nSkip bytes of the cell */ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4857 | ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4858 | int idx; /* Where to write new cell content in data[] */ |
| 4859 | int j; /* Loop counter */ |
| 4860 | int top; /* First byte of content for any cell in data[] */ |
| 4861 | int end; /* First byte past the last cell pointer in data[] */ |
| 4862 | int ins; /* Index in data[] where new cell pointer is inserted */ |
| 4863 | int hdr; /* Offset into data[] of the page header */ |
| 4864 | int cellOffset; /* Address of first cell pointer in data[] */ |
| 4865 | u8 *data; /* The content of the whole page */ |
| 4866 | u8 *ptr; /* Used for moving information around in data[] */ |
| 4867 | |
| 4868 | assert( i>=0 && i<=pPage->nCell+pPage->nOverflow ); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4869 | assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=5460 ); |
| 4870 | assert( pPage->nOverflow<=ArraySize(pPage->aOvfl) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4871 | assert( sz==cellSizePtr(pPage, pCell) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4872 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4873 | if( pPage->nOverflow || sz+2>pPage->nFree ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4874 | if( pTemp ){ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4875 | memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4876 | pCell = pTemp; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4877 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4878 | j = pPage->nOverflow++; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 4879 | assert( j<(int)(sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0])) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4880 | pPage->aOvfl[j].pCell = pCell; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4881 | pPage->aOvfl[j].idx = (u16)i; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4882 | pPage->nFree = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4883 | }else{ |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4884 | int rc = sqlite3PagerWrite(pPage->pDbPage); |
| 4885 | if( rc!=SQLITE_OK ){ |
| 4886 | return rc; |
| 4887 | } |
| 4888 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4889 | data = pPage->aData; |
| 4890 | hdr = pPage->hdrOffset; |
| 4891 | top = get2byte(&data[hdr+5]); |
| 4892 | cellOffset = pPage->cellOffset; |
| 4893 | end = cellOffset + 2*pPage->nCell + 2; |
| 4894 | ins = cellOffset + 2*i; |
| 4895 | if( end > top - sz ){ |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 4896 | rc = defragmentPage(pPage); |
| 4897 | if( rc!=SQLITE_OK ){ |
| 4898 | return rc; |
| 4899 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4900 | top = get2byte(&data[hdr+5]); |
| 4901 | assert( end + sz <= top ); |
| 4902 | } |
| 4903 | idx = allocateSpace(pPage, sz); |
| 4904 | assert( idx>0 ); |
| 4905 | assert( end <= get2byte(&data[hdr+5]) ); |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 4906 | if (idx+sz > pPage->pBt->usableSize) { |
shane | 34ac18d | 2008-11-11 22:18:20 +0000 | [diff] [blame] | 4907 | return SQLITE_CORRUPT_BKPT; |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 4908 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4909 | pPage->nCell++; |
| 4910 | pPage->nFree -= 2; |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4911 | memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4912 | for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){ |
| 4913 | ptr[0] = ptr[-2]; |
| 4914 | ptr[1] = ptr[-1]; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4915 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4916 | put2byte(&data[ins], idx); |
| 4917 | put2byte(&data[hdr+3], pPage->nCell); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4918 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4919 | if( pPage->pBt->autoVacuum ){ |
| 4920 | /* The cell may contain a pointer to an overflow page. If so, write |
| 4921 | ** the entry for the overflow page into the pointer map. |
| 4922 | */ |
| 4923 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4924 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 4925 | assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4926 | if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ |
| 4927 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4928 | rc = ptrmapPut(pPage->pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4929 | if( rc!=SQLITE_OK ) return rc; |
| 4930 | } |
| 4931 | } |
| 4932 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4933 | } |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4934 | |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4935 | return SQLITE_OK; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4936 | } |
| 4937 | |
| 4938 | /* |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4939 | ** Add a list of cells to a page. The page should be initially empty. |
| 4940 | ** The cells are guaranteed to fit on the page. |
| 4941 | */ |
| 4942 | static void assemblePage( |
| 4943 | MemPage *pPage, /* The page to be assemblied */ |
| 4944 | int nCell, /* The number of cells to add to this page */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4945 | u8 **apCell, /* Pointers to cell bodies */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 4946 | u16 *aSize /* Sizes of the cells */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4947 | ){ |
| 4948 | int i; /* Loop counter */ |
| 4949 | int totalSize; /* Total size of all cells */ |
| 4950 | int hdr; /* Index of page header */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4951 | int cellptr; /* Address of next cell pointer */ |
| 4952 | int cellbody; /* Address of next cell body */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4953 | u8 *data; /* Data for the page */ |
| 4954 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4955 | assert( pPage->nOverflow==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4956 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4957 | assert( nCell>=0 && nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=5460 ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4958 | totalSize = 0; |
| 4959 | for(i=0; i<nCell; i++){ |
| 4960 | totalSize += aSize[i]; |
| 4961 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4962 | assert( totalSize+2*nCell<=pPage->nFree ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4963 | assert( pPage->nCell==0 ); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 4964 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4965 | cellptr = pPage->cellOffset; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4966 | data = pPage->aData; |
| 4967 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4968 | put2byte(&data[hdr+3], nCell); |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 4969 | if( nCell ){ |
| 4970 | cellbody = allocateSpace(pPage, totalSize); |
| 4971 | assert( cellbody>0 ); |
| 4972 | assert( pPage->nFree >= 2*nCell ); |
| 4973 | pPage->nFree -= 2*nCell; |
| 4974 | for(i=0; i<nCell; i++){ |
| 4975 | put2byte(&data[cellptr], cellbody); |
| 4976 | memcpy(&data[cellbody], apCell[i], aSize[i]); |
| 4977 | cellptr += 2; |
| 4978 | cellbody += aSize[i]; |
| 4979 | } |
| 4980 | assert( cellbody==pPage->pBt->usableSize ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4981 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4982 | pPage->nCell = (u16)nCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4983 | } |
| 4984 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4985 | /* |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4986 | ** The following parameters determine how many adjacent pages get involved |
| 4987 | ** in a balancing operation. NN is the number of neighbors on either side |
| 4988 | ** of the page that participate in the balancing operation. NB is the |
| 4989 | ** total number of pages that participate, including the target page and |
| 4990 | ** NN neighbors on either side. |
| 4991 | ** |
| 4992 | ** The minimum value of NN is 1 (of course). Increasing NN above 1 |
| 4993 | ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance |
| 4994 | ** in exchange for a larger degradation in INSERT and UPDATE performance. |
| 4995 | ** The value of NN appears to give the best results overall. |
| 4996 | */ |
| 4997 | #define NN 1 /* Number of neighbors on either side of pPage */ |
| 4998 | #define NB (NN*2+1) /* Total pages involved in the balance */ |
| 4999 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5000 | /* Forward reference */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5001 | static int balance(BtCursor*, int); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5002 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 5003 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 5004 | /* |
| 5005 | ** This version of balance() handles the common special case where |
| 5006 | ** a new entry is being inserted on the extreme right-end of the |
| 5007 | ** tree, in other words, when the new entry will become the largest |
| 5008 | ** entry in the tree. |
| 5009 | ** |
| 5010 | ** Instead of trying balance the 3 right-most leaf pages, just add |
| 5011 | ** a new page to the right-hand side and put the one new entry in |
| 5012 | ** that page. This leaves the right side of the tree somewhat |
| 5013 | ** unbalanced. But odds are that we will be inserting new entries |
| 5014 | ** at the end soon afterwards so the nearly empty page will quickly |
| 5015 | ** fill up. On average. |
| 5016 | ** |
| 5017 | ** pPage is the leaf page which is the right-most page in the tree. |
| 5018 | ** pParent is its parent. pPage must have a single overflow entry |
| 5019 | ** which is also the right-most entry on the page. |
| 5020 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5021 | static int balance_quick(BtCursor *pCur){ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5022 | int rc; |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 5023 | MemPage *pNew = 0; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5024 | Pgno pgnoNew; |
| 5025 | u8 *pCell; |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5026 | u16 szCell; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5027 | CellInfo info; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5028 | MemPage *pPage = pCur->apPage[pCur->iPage]; |
| 5029 | MemPage *pParent = pCur->apPage[pCur->iPage-1]; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5030 | BtShared *pBt = pPage->pBt; |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5031 | int parentIdx = pParent->nCell; /* pParent new divider cell index */ |
| 5032 | int parentSize; /* Size of new divider cell */ |
| 5033 | u8 parentCell[64]; /* Space for the new divider cell */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5034 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5035 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5036 | |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5037 | /* Allocate a new page. Insert the overflow cell from pPage |
| 5038 | ** into it. Then remove the overflow cell from pPage. |
| 5039 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5040 | rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0); |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 5041 | if( rc==SQLITE_OK ){ |
| 5042 | pCell = pPage->aOvfl[0].pCell; |
| 5043 | szCell = cellSizePtr(pPage, pCell); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 5044 | assert( sqlite3PagerIswriteable(pNew->pDbPage) ); |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 5045 | zeroPage(pNew, pPage->aData[0]); |
| 5046 | assemblePage(pNew, 1, &pCell, &szCell); |
| 5047 | pPage->nOverflow = 0; |
| 5048 | |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 5049 | /* pPage is currently the right-child of pParent. Change this |
| 5050 | ** so that the right-child is the new page allocated above and |
| 5051 | ** pPage is the next-to-right child. |
| 5052 | ** |
| 5053 | ** Ignore the return value of the call to fillInCell(). fillInCell() |
| 5054 | ** may only return other than SQLITE_OK if it is required to allocate |
| 5055 | ** one or more overflow pages. Since an internal table B-Tree cell |
| 5056 | ** may never spill over onto an overflow page (it is a maximum of |
| 5057 | ** 13 bytes in size), it is not neccessary to check the return code. |
| 5058 | ** |
| 5059 | ** Similarly, the insertCell() function cannot fail if the page |
| 5060 | ** being inserted into is already writable and the cell does not |
| 5061 | ** contain an overflow pointer. So ignore this return code too. |
| 5062 | */ |
| 5063 | assert( pPage->nCell>0 ); |
| 5064 | pCell = findCell(pPage, pPage->nCell-1); |
| 5065 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
| 5066 | fillInCell(pParent, parentCell, 0, info.nKey, 0, 0, 0, &parentSize); |
| 5067 | assert( parentSize<64 ); |
| 5068 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
| 5069 | insertCell(pParent, parentIdx, parentCell, parentSize, 0, 4); |
| 5070 | put4byte(findOverflowCell(pParent,parentIdx), pPage->pgno); |
| 5071 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew); |
| 5072 | |
| 5073 | /* If this is an auto-vacuum database, update the pointer map |
| 5074 | ** with entries for the new page, and any pointer from the |
| 5075 | ** cell on the page to an overflow page. |
| 5076 | */ |
| 5077 | if( ISAUTOVACUUM ){ |
| 5078 | rc = ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno); |
| 5079 | if( rc==SQLITE_OK ){ |
| 5080 | rc = ptrmapPutOvfl(pNew, 0); |
| 5081 | } |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5082 | } |
danielk1977 | e08a3c4 | 2008-09-18 18:17:03 +0000 | [diff] [blame] | 5083 | |
| 5084 | /* Release the reference to the new page. */ |
| 5085 | releasePage(pNew); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5086 | } |
| 5087 | |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 5088 | /* At this point the pPage->nFree variable is not set correctly with |
| 5089 | ** respect to the content of the page (because it was set to 0 by |
| 5090 | ** insertCell). So call sqlite3BtreeInitPage() to make sure it is |
| 5091 | ** correct. |
| 5092 | ** |
| 5093 | ** This has to be done even if an error will be returned. Normally, if |
| 5094 | ** an error occurs during tree balancing, the contents of MemPage are |
| 5095 | ** not important, as they will be recalculated when the page is rolled |
| 5096 | ** back. But here, in balance_quick(), it is possible that pPage has |
| 5097 | ** not yet been marked dirty or written into the journal file. Therefore |
| 5098 | ** it will not be rolled back and so it is important to make sure that |
| 5099 | ** the page data and contents of MemPage are consistent. |
| 5100 | */ |
| 5101 | pPage->isInit = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5102 | sqlite3BtreeInitPage(pPage); |
danielk1977 | a4124bd | 2008-12-23 10:37:47 +0000 | [diff] [blame] | 5103 | assert( pPage->nOverflow==0 ); |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 5104 | |
danielk1977 | e08a3c4 | 2008-09-18 18:17:03 +0000 | [diff] [blame] | 5105 | /* If everything else succeeded, balance the parent page, in |
| 5106 | ** case the divider cell inserted caused it to become overfull. |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5107 | */ |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 5108 | if( rc==SQLITE_OK ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5109 | releasePage(pPage); |
| 5110 | pCur->iPage--; |
| 5111 | rc = balance(pCur, 0); |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 5112 | } |
| 5113 | return rc; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5114 | } |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 5115 | #endif /* SQLITE_OMIT_QUICKBALANCE */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5116 | |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5117 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 5118 | ** This routine redistributes Cells on pPage and up to NN*2 siblings |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5119 | ** 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] | 5120 | ** Usually NN siblings on either side of pPage is used in the balancing, |
| 5121 | ** though more siblings might come from one side if pPage is the first |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 5122 | ** 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] | 5123 | ** (something which can only happen if pPage is the root page or a |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5124 | ** child of root) then all available siblings participate in the balancing. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5125 | ** |
drh | 0c6cc4e | 2004-06-15 02:13:26 +0000 | [diff] [blame] | 5126 | ** The number of siblings of pPage might be increased or decreased by one or |
| 5127 | ** 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] | 5128 | ** is special and is allowed to be nearly empty. If pPage is |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5129 | ** the root page, then the depth of the tree might be increased |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5130 | ** or decreased by one, as necessary, to keep the root page from being |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 5131 | ** overfull or completely empty. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5132 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5133 | ** Note that when this routine is called, some of the Cells on pPage |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5134 | ** might not actually be stored in pPage->aData[]. This can happen |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5135 | ** 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] | 5136 | ** make sure all Cells for pPage once again fit in pPage->aData[]. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5137 | ** |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5138 | ** In the course of balancing the siblings of pPage, the parent of pPage |
| 5139 | ** might become overfull or underfull. If that happens, then this routine |
| 5140 | ** is called recursively on the parent. |
| 5141 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5142 | ** If this routine fails for any reason, it might leave the database |
| 5143 | ** in a corrupted state. So if this routine fails, the database should |
| 5144 | ** be rolled back. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5145 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5146 | static int balance_nonroot(BtCursor *pCur){ |
| 5147 | MemPage *pPage; /* The over or underfull page to balance */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5148 | MemPage *pParent; /* The parent of pPage */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5149 | BtShared *pBt; /* The whole database */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5150 | int nCell = 0; /* Number of cells in apCell[] */ |
| 5151 | int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */ |
danielk1977 | a4124bd | 2008-12-23 10:37:47 +0000 | [diff] [blame] | 5152 | int nOld = 0; /* Number of pages in apOld[] */ |
| 5153 | int nNew = 0; /* Number of pages in apNew[] */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5154 | int nDiv; /* Number of cells in apDiv[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5155 | int i, j, k; /* Loop counters */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5156 | int idx; /* Index of pPage in pParent->aCell[] */ |
| 5157 | int nxDiv; /* Next divider slot in pParent->aCell[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5158 | int rc; /* The return code */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5159 | int leafCorrection; /* 4 if pPage is a leaf. 0 if not */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5160 | int leafData; /* True if pPage is a leaf of a LEAFDATA tree */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5161 | int usableSpace; /* Bytes in pPage beyond the header */ |
| 5162 | int pageFlags; /* Value of pPage->aData[0] */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5163 | int subtotal; /* Subtotal of bytes in cells on one page */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5164 | int iSpace1 = 0; /* First unused byte of aSpace1[] */ |
| 5165 | int iSpace2 = 0; /* First unused byte of aSpace2[] */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5166 | int szScratch; /* Size of scratch memory requested */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5167 | MemPage *apOld[NB]; /* pPage and up to two siblings */ |
| 5168 | Pgno pgnoOld[NB]; /* Page numbers for each page in apOld[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5169 | MemPage *apCopy[NB]; /* Private copies of apOld[] pages */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5170 | MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ |
| 5171 | Pgno pgnoNew[NB+2]; /* Page numbers for each page in apNew[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5172 | u8 *apDiv[NB]; /* Divider cells in pParent */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5173 | int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */ |
| 5174 | int szNew[NB+2]; /* Combined size of cells place on i-th page */ |
danielk1977 | 50f059b | 2005-03-29 02:54:03 +0000 | [diff] [blame] | 5175 | u8 **apCell = 0; /* All cells begin balanced */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5176 | u16 *szCell; /* Local size of all cells in apCell[] */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5177 | u8 *aCopy[NB]; /* Space for holding data of apCopy[] */ |
| 5178 | u8 *aSpace1; /* Space for copies of dividers cells before balance */ |
| 5179 | u8 *aSpace2 = 0; /* Space for overflow dividers cells after balance */ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5180 | u8 *aFrom = 0; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5181 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5182 | pPage = pCur->apPage[pCur->iPage]; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5183 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 5184 | VVA_ONLY( pCur->pagesShuffled = 1 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5185 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5186 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5187 | ** Find the parent page. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5188 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5189 | assert( pCur->iPage>0 ); |
| 5190 | assert( pPage->isInit ); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 5191 | assert( sqlite3PagerIswriteable(pPage->pDbPage) || pPage->nOverflow==1 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5192 | pBt = pPage->pBt; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5193 | pParent = pCur->apPage[pCur->iPage-1]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5194 | assert( pParent ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5195 | if( SQLITE_OK!=(rc = sqlite3PagerWrite(pParent->pDbPage)) ){ |
danielk1977 | a4124bd | 2008-12-23 10:37:47 +0000 | [diff] [blame] | 5196 | goto balance_cleanup; |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 5197 | } |
danielk1977 | 474b7cc | 2008-07-09 11:49:46 +0000 | [diff] [blame] | 5198 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5199 | TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno)); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5200 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 5201 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 5202 | /* |
| 5203 | ** A special case: If a new entry has just been inserted into a |
| 5204 | ** 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] | 5205 | ** 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] | 5206 | ** largest key) then use the special balance_quick() routine for |
| 5207 | ** balancing. balance_quick() is much faster and results in a tighter |
| 5208 | ** packing of data in the common case. |
| 5209 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5210 | if( pPage->leaf && |
| 5211 | pPage->intKey && |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5212 | pPage->nOverflow==1 && |
| 5213 | pPage->aOvfl[0].idx==pPage->nCell && |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5214 | pParent->pgno!=1 && |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5215 | get4byte(&pParent->aData[pParent->hdrOffset+8])==pPage->pgno |
| 5216 | ){ |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 5217 | assert( pPage->intKey ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5218 | /* |
| 5219 | ** TODO: Check the siblings to the left of pPage. It may be that |
| 5220 | ** they are not full and no new page is required. |
| 5221 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5222 | return balance_quick(pCur); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5223 | } |
| 5224 | #endif |
| 5225 | |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 5226 | if( SQLITE_OK!=(rc = sqlite3PagerWrite(pPage->pDbPage)) ){ |
danielk1977 | a4124bd | 2008-12-23 10:37:47 +0000 | [diff] [blame] | 5227 | goto balance_cleanup; |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 5228 | } |
| 5229 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5230 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5231 | ** Find the cell in the parent page whose left child points back |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5232 | ** to pPage. The "idx" variable is the index of that cell. If pPage |
| 5233 | ** is the rightmost child of pParent then set idx to pParent->nCell |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5234 | */ |
danielk1977 | bf93c56 | 2008-09-29 15:53:25 +0000 | [diff] [blame] | 5235 | idx = pCur->aiIdx[pCur->iPage-1]; |
| 5236 | assertParentIndex(pParent, idx, pPage->pgno); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5237 | |
| 5238 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5239 | ** Find sibling pages to pPage and the cells in pParent that divide |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5240 | ** the siblings. An attempt is made to find NN siblings on either |
| 5241 | ** side of pPage. More siblings are taken from one side, however, if |
| 5242 | ** pPage there are fewer than NN siblings on the other side. If pParent |
| 5243 | ** has NB or fewer children then all children of pParent are taken. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5244 | */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5245 | nxDiv = idx - NN; |
| 5246 | if( nxDiv + NB > pParent->nCell ){ |
| 5247 | nxDiv = pParent->nCell - NB + 1; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5248 | } |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5249 | if( nxDiv<0 ){ |
| 5250 | nxDiv = 0; |
| 5251 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5252 | nDiv = 0; |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5253 | for(i=0, k=nxDiv; i<NB; i++, k++){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5254 | if( k<pParent->nCell ){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5255 | apDiv[i] = findCell(pParent, k); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5256 | nDiv++; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5257 | assert( !pParent->leaf ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5258 | pgnoOld[i] = get4byte(apDiv[i]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5259 | }else if( k==pParent->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5260 | pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+8]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5261 | }else{ |
| 5262 | break; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5263 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5264 | rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i]); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5265 | if( rc ) goto balance_cleanup; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5266 | /* apOld[i]->idxParent = k; */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5267 | apCopy[i] = 0; |
| 5268 | assert( i==nOld ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5269 | nOld++; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5270 | nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5271 | } |
| 5272 | |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5273 | /* Make nMaxCells a multiple of 4 in order to preserve 8-byte |
drh | 8d97f1f | 2005-05-05 18:14:13 +0000 | [diff] [blame] | 5274 | ** alignment */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5275 | nMaxCells = (nMaxCells + 3)&~3; |
drh | 8d97f1f | 2005-05-05 18:14:13 +0000 | [diff] [blame] | 5276 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5277 | /* |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5278 | ** Allocate space for memory structures |
| 5279 | */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5280 | szScratch = |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5281 | nMaxCells*sizeof(u8*) /* apCell */ |
| 5282 | + nMaxCells*sizeof(u16) /* szCell */ |
| 5283 | + (ROUND8(sizeof(MemPage))+pBt->pageSize)*NB /* aCopy */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5284 | + pBt->pageSize /* aSpace1 */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5285 | + (ISAUTOVACUUM ? nMaxCells : 0); /* aFrom */ |
| 5286 | apCell = sqlite3ScratchMalloc( szScratch ); |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5287 | if( apCell==0 ){ |
| 5288 | rc = SQLITE_NOMEM; |
| 5289 | goto balance_cleanup; |
| 5290 | } |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5291 | szCell = (u16*)&apCell[nMaxCells]; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5292 | aCopy[0] = (u8*)&szCell[nMaxCells]; |
drh | 66e8008 | 2008-12-16 13:46:29 +0000 | [diff] [blame] | 5293 | assert( ((aCopy[0] - (u8*)0) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5294 | for(i=1; i<NB; i++){ |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 5295 | aCopy[i] = &aCopy[i-1][pBt->pageSize+ROUND8(sizeof(MemPage))]; |
drh | 66e8008 | 2008-12-16 13:46:29 +0000 | [diff] [blame] | 5296 | assert( ((aCopy[i] - (u8*)0) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5297 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5298 | aSpace1 = &aCopy[NB-1][pBt->pageSize+ROUND8(sizeof(MemPage))]; |
drh | 66e8008 | 2008-12-16 13:46:29 +0000 | [diff] [blame] | 5299 | assert( ((aSpace1 - (u8*)0) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5300 | if( ISAUTOVACUUM ){ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5301 | aFrom = &aSpace1[pBt->pageSize]; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5302 | } |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5303 | aSpace2 = sqlite3PageMalloc(pBt->pageSize); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5304 | if( aSpace2==0 ){ |
| 5305 | rc = SQLITE_NOMEM; |
| 5306 | goto balance_cleanup; |
| 5307 | } |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5308 | |
| 5309 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5310 | ** Make copies of the content of pPage and its siblings into aOld[]. |
| 5311 | ** The rest of this function will use data from the copies rather |
| 5312 | ** that the original pages since the original pages will be in the |
| 5313 | ** process of being overwritten. |
| 5314 | */ |
| 5315 | for(i=0; i<nOld; i++){ |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 5316 | MemPage *p = apCopy[i] = (MemPage*)aCopy[i]; |
| 5317 | memcpy(p, apOld[i], sizeof(MemPage)); |
| 5318 | p->aData = (void*)&p[1]; |
| 5319 | memcpy(p->aData, apOld[i]->aData, pBt->pageSize); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5320 | } |
| 5321 | |
| 5322 | /* |
| 5323 | ** Load pointers to all cells on sibling pages and the divider cells |
| 5324 | ** into the local apCell[] array. Make copies of the divider cells |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5325 | ** into space obtained form aSpace1[] and remove the the divider Cells |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5326 | ** from pParent. |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5327 | ** |
| 5328 | ** If the siblings are on leaf pages, then the child pointers of the |
| 5329 | ** divider cells are stripped from the cells before they are copied |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5330 | ** into aSpace1[]. In this way, all cells in apCell[] are without |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5331 | ** child pointers. If siblings are not leaves, then all cell in |
| 5332 | ** apCell[] include child pointers. Either way, all cells in apCell[] |
| 5333 | ** are alike. |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5334 | ** |
| 5335 | ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf. |
| 5336 | ** leafData: 1 if pPage holds key+data and pParent holds only keys. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5337 | */ |
| 5338 | nCell = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5339 | leafCorrection = pPage->leaf*4; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 5340 | leafData = pPage->hasData; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5341 | for(i=0; i<nOld; i++){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5342 | MemPage *pOld = apCopy[i]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5343 | int limit = pOld->nCell+pOld->nOverflow; |
| 5344 | for(j=0; j<limit; j++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5345 | assert( nCell<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5346 | apCell[nCell] = findOverflowCell(pOld, j); |
| 5347 | szCell[nCell] = cellSizePtr(pOld, apCell[nCell]); |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5348 | if( ISAUTOVACUUM ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5349 | int a; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 5350 | aFrom[nCell] = (u8)i; assert( i>=0 && i<6 ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5351 | for(a=0; a<pOld->nOverflow; a++){ |
| 5352 | if( pOld->aOvfl[a].pCell==apCell[nCell] ){ |
| 5353 | aFrom[nCell] = 0xFF; |
| 5354 | break; |
| 5355 | } |
| 5356 | } |
| 5357 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5358 | nCell++; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5359 | } |
| 5360 | if( i<nOld-1 ){ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5361 | u16 sz = cellSizePtr(pParent, apDiv[i]); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5362 | if( leafData ){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5363 | /* With the LEAFDATA flag, pParent cells hold only INTKEYs that |
| 5364 | ** are duplicates of keys on the child pages. We need to remove |
| 5365 | ** the divider cells from pParent, but the dividers cells are not |
| 5366 | ** added to apCell[] because they are duplicates of child cells. |
| 5367 | */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5368 | dropCell(pParent, nxDiv, sz); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5369 | }else{ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5370 | u8 *pTemp; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5371 | assert( nCell<nMaxCells ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5372 | szCell[nCell] = sz; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5373 | pTemp = &aSpace1[iSpace1]; |
| 5374 | iSpace1 += sz; |
| 5375 | assert( sz<=pBt->pageSize/4 ); |
| 5376 | assert( iSpace1<=pBt->pageSize ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5377 | memcpy(pTemp, apDiv[i], sz); |
| 5378 | apCell[nCell] = pTemp+leafCorrection; |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5379 | if( ISAUTOVACUUM ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5380 | aFrom[nCell] = 0xFF; |
| 5381 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5382 | dropCell(pParent, nxDiv, sz); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 5383 | assert( leafCorrection==0 || leafCorrection==4 ); |
| 5384 | szCell[nCell] -= (u16)leafCorrection; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5385 | assert( get4byte(pTemp)==pgnoOld[i] ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5386 | if( !pOld->leaf ){ |
| 5387 | assert( leafCorrection==0 ); |
| 5388 | /* The right pointer of the child page pOld becomes the left |
| 5389 | ** pointer of the divider cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5390 | memcpy(apCell[nCell], &pOld->aData[pOld->hdrOffset+8], 4); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5391 | }else{ |
| 5392 | assert( leafCorrection==4 ); |
danielk1977 | 39c9604 | 2007-05-12 10:41:47 +0000 | [diff] [blame] | 5393 | if( szCell[nCell]<4 ){ |
| 5394 | /* Do not allow any cells smaller than 4 bytes. */ |
| 5395 | szCell[nCell] = 4; |
| 5396 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5397 | } |
| 5398 | nCell++; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5399 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5400 | } |
| 5401 | } |
| 5402 | |
| 5403 | /* |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5404 | ** Figure out the number of pages needed to hold all nCell cells. |
| 5405 | ** Store this number in "k". Also compute szNew[] which is the total |
| 5406 | ** 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] | 5407 | ** in apCell[] of the cell that divides page i from page i+1. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5408 | ** cntNew[k] should equal nCell. |
| 5409 | ** |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5410 | ** Values computed by this block: |
| 5411 | ** |
| 5412 | ** k: The total number of sibling pages |
| 5413 | ** szNew[i]: Spaced used on the i-th sibling page. |
| 5414 | ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to |
| 5415 | ** the right of the i-th sibling page. |
| 5416 | ** usableSpace: Number of bytes of space available on each sibling. |
| 5417 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5418 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5419 | usableSpace = pBt->usableSize - 12 + leafCorrection; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5420 | for(subtotal=k=i=0; i<nCell; i++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5421 | assert( i<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5422 | subtotal += szCell[i] + 2; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5423 | if( subtotal > usableSpace ){ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5424 | szNew[k] = subtotal - szCell[i]; |
| 5425 | cntNew[k] = i; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5426 | if( leafData ){ i--; } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5427 | subtotal = 0; |
| 5428 | k++; |
| 5429 | } |
| 5430 | } |
| 5431 | szNew[k] = subtotal; |
| 5432 | cntNew[k] = nCell; |
| 5433 | k++; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5434 | |
| 5435 | /* |
| 5436 | ** The packing computed by the previous block is biased toward the siblings |
| 5437 | ** on the left side. The left siblings are always nearly full, while the |
| 5438 | ** right-most sibling might be nearly empty. This block of code attempts |
| 5439 | ** to adjust the packing of siblings to get a better balance. |
| 5440 | ** |
| 5441 | ** This adjustment is more than an optimization. The packing above might |
| 5442 | ** be so out of balance as to be illegal. For example, the right-most |
| 5443 | ** sibling might be completely empty. This adjustment is not optional. |
| 5444 | */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5445 | for(i=k-1; i>0; i--){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5446 | int szRight = szNew[i]; /* Size of sibling on the right */ |
| 5447 | int szLeft = szNew[i-1]; /* Size of sibling on the left */ |
| 5448 | int r; /* Index of right-most cell in left sibling */ |
| 5449 | int d; /* Index of first cell to the left of right sibling */ |
| 5450 | |
| 5451 | r = cntNew[i-1] - 1; |
| 5452 | d = r + 1 - leafData; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5453 | assert( d<nMaxCells ); |
| 5454 | assert( r<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5455 | while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){ |
| 5456 | szRight += szCell[d] + 2; |
| 5457 | szLeft -= szCell[r] + 2; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5458 | cntNew[i-1]--; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5459 | r = cntNew[i-1] - 1; |
| 5460 | d = r + 1 - leafData; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5461 | } |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5462 | szNew[i] = szRight; |
| 5463 | szNew[i-1] = szLeft; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5464 | } |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 5465 | |
| 5466 | /* Either we found one or more cells (cntnew[0])>0) or we are the |
| 5467 | ** a virtual root page. A virtual root page is when the real root |
| 5468 | ** page is page 1 and we are the only child of that page. |
| 5469 | */ |
| 5470 | assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5471 | |
| 5472 | /* |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5473 | ** Allocate k new pages. Reuse old pages where possible. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5474 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5475 | assert( pPage->pgno>1 ); |
| 5476 | pageFlags = pPage->aData[0]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5477 | for(i=0; i<k; i++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5478 | MemPage *pNew; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5479 | if( i<nOld ){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5480 | pNew = apNew[i] = apOld[i]; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5481 | pgnoNew[i] = pgnoOld[i]; |
| 5482 | apOld[i] = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5483 | rc = sqlite3PagerWrite(pNew->pDbPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 5484 | nNew++; |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 5485 | if( rc ) goto balance_cleanup; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5486 | }else{ |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 5487 | assert( i>0 ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5488 | rc = allocateBtreePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5489 | if( rc ) goto balance_cleanup; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5490 | apNew[i] = pNew; |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 5491 | nNew++; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5492 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5493 | } |
| 5494 | |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5495 | /* Free any old pages that were not reused as new pages. |
| 5496 | */ |
| 5497 | while( i<nOld ){ |
| 5498 | rc = freePage(apOld[i]); |
| 5499 | if( rc ) goto balance_cleanup; |
| 5500 | releasePage(apOld[i]); |
| 5501 | apOld[i] = 0; |
| 5502 | i++; |
| 5503 | } |
| 5504 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5505 | /* |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5506 | ** Put the new pages in accending order. This helps to |
| 5507 | ** keep entries in the disk file in order so that a scan |
| 5508 | ** of the table is a linear scan through the file. That |
| 5509 | ** in turn helps the operating system to deliver pages |
| 5510 | ** from the disk more rapidly. |
| 5511 | ** |
| 5512 | ** An O(n^2) insertion sort algorithm is used, but since |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5513 | ** n is never more than NB (a small constant), that should |
| 5514 | ** not be a problem. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5515 | ** |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5516 | ** When NB==3, this one optimization makes the database |
| 5517 | ** about 25% faster for large insertions and deletions. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5518 | */ |
| 5519 | for(i=0; i<k-1; i++){ |
| 5520 | int minV = pgnoNew[i]; |
| 5521 | int minI = i; |
| 5522 | for(j=i+1; j<k; j++){ |
drh | 7d02cb7 | 2003-06-04 16:24:39 +0000 | [diff] [blame] | 5523 | if( pgnoNew[j]<(unsigned)minV ){ |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5524 | minI = j; |
| 5525 | minV = pgnoNew[j]; |
| 5526 | } |
| 5527 | } |
| 5528 | if( minI>i ){ |
| 5529 | int t; |
| 5530 | MemPage *pT; |
| 5531 | t = pgnoNew[i]; |
| 5532 | pT = apNew[i]; |
| 5533 | pgnoNew[i] = pgnoNew[minI]; |
| 5534 | apNew[i] = apNew[minI]; |
| 5535 | pgnoNew[minI] = t; |
| 5536 | apNew[minI] = pT; |
| 5537 | } |
| 5538 | } |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5539 | 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] | 5540 | pgnoOld[0], |
| 5541 | nOld>=2 ? pgnoOld[1] : 0, |
| 5542 | nOld>=3 ? pgnoOld[2] : 0, |
drh | 10c0fa6 | 2004-05-18 12:50:17 +0000 | [diff] [blame] | 5543 | pgnoNew[0], szNew[0], |
| 5544 | nNew>=2 ? pgnoNew[1] : 0, nNew>=2 ? szNew[1] : 0, |
| 5545 | nNew>=3 ? pgnoNew[2] : 0, nNew>=3 ? szNew[2] : 0, |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5546 | nNew>=4 ? pgnoNew[3] : 0, nNew>=4 ? szNew[3] : 0, |
| 5547 | nNew>=5 ? pgnoNew[4] : 0, nNew>=5 ? szNew[4] : 0)); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5548 | |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5549 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5550 | ** Evenly distribute the data in apCell[] across the new pages. |
| 5551 | ** Insert divider cells into pParent as necessary. |
| 5552 | */ |
| 5553 | j = 0; |
| 5554 | for(i=0; i<nNew; i++){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5555 | /* Assemble the new sibling page. */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5556 | MemPage *pNew = apNew[i]; |
drh | 19642e5 | 2005-03-29 13:17:45 +0000 | [diff] [blame] | 5557 | assert( j<nMaxCells ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5558 | assert( pNew->pgno==pgnoNew[i] ); |
drh | 1013148 | 2008-07-11 03:34:09 +0000 | [diff] [blame] | 5559 | zeroPage(pNew, pageFlags); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 5560 | assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]); |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 5561 | assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5562 | assert( pNew->nOverflow==0 ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5563 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5564 | /* If this is an auto-vacuum database, update the pointer map entries |
| 5565 | ** that point to the siblings that were rearranged. These can be: left |
| 5566 | ** children of cells, the right-child of the page, or overflow pages |
| 5567 | ** pointed to by cells. |
| 5568 | */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5569 | if( ISAUTOVACUUM ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5570 | for(k=j; k<cntNew[i]; k++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5571 | assert( k<nMaxCells ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5572 | if( aFrom[k]==0xFF || apCopy[aFrom[k]]->pgno!=pNew->pgno ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5573 | rc = ptrmapPutOvfl(pNew, k-j); |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5574 | if( rc==SQLITE_OK && leafCorrection==0 ){ |
| 5575 | rc = ptrmapPut(pBt, get4byte(apCell[k]), PTRMAP_BTREE, pNew->pgno); |
| 5576 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5577 | if( rc!=SQLITE_OK ){ |
| 5578 | goto balance_cleanup; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5579 | } |
| 5580 | } |
| 5581 | } |
| 5582 | } |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5583 | |
| 5584 | j = cntNew[i]; |
| 5585 | |
| 5586 | /* If the sibling page assembled above was not the right-most sibling, |
| 5587 | ** insert a divider cell into the parent page. |
| 5588 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5589 | if( i<nNew-1 && j<nCell ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5590 | u8 *pCell; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5591 | u8 *pTemp; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5592 | int sz; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5593 | |
| 5594 | assert( j<nMaxCells ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5595 | pCell = apCell[j]; |
| 5596 | sz = szCell[j] + leafCorrection; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5597 | pTemp = &aSpace2[iSpace2]; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5598 | if( !pNew->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5599 | memcpy(&pNew->aData[8], pCell, 4); |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5600 | if( ISAUTOVACUUM |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5601 | && (aFrom[j]==0xFF || apCopy[aFrom[j]]->pgno!=pNew->pgno) |
| 5602 | ){ |
| 5603 | rc = ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno); |
| 5604 | if( rc!=SQLITE_OK ){ |
| 5605 | goto balance_cleanup; |
| 5606 | } |
| 5607 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5608 | }else if( leafData ){ |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 5609 | /* If the tree is a leaf-data tree, and the siblings are leaves, |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5610 | ** then there is no divider cell in apCell[]. Instead, the divider |
| 5611 | ** cell consists of the integer key for the right-most cell of |
| 5612 | ** the sibling-page assembled above only. |
| 5613 | */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5614 | CellInfo info; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5615 | j--; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5616 | sqlite3BtreeParseCellPtr(pNew, apCell[j], &info); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5617 | pCell = pTemp; |
drh | 20abac2 | 2009-01-28 20:21:17 +0000 | [diff] [blame] | 5618 | rc = fillInCell(pParent, pCell, 0, info.nKey, 0, 0, 0, &sz); |
| 5619 | if( rc!=SQLITE_OK ){ |
| 5620 | goto balance_cleanup; |
| 5621 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5622 | pTemp = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5623 | }else{ |
| 5624 | pCell -= 4; |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 5625 | /* Obscure case for non-leaf-data trees: If the cell at pCell was |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 5626 | ** previously stored on a leaf node, and its reported size was 4 |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 5627 | ** bytes, then it may actually be smaller than this |
| 5628 | ** (see sqlite3BtreeParseCellPtr(), 4 bytes is the minimum size of |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 5629 | ** any cell). But it is important to pass the correct size to |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 5630 | ** insertCell(), so reparse the cell now. |
| 5631 | ** |
| 5632 | ** Note that this can never happen in an SQLite data file, as all |
| 5633 | ** cells are at least 4 bytes. It only happens in b-trees used |
| 5634 | ** to evaluate "IN (SELECT ...)" and similar clauses. |
| 5635 | */ |
| 5636 | if( szCell[j]==4 ){ |
| 5637 | assert(leafCorrection==4); |
| 5638 | sz = cellSizePtr(pParent, pCell); |
| 5639 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5640 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5641 | iSpace2 += sz; |
| 5642 | assert( sz<=pBt->pageSize/4 ); |
| 5643 | assert( iSpace2<=pBt->pageSize ); |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 5644 | rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4); |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 5645 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 5646 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5647 | put4byte(findOverflowCell(pParent,nxDiv), pNew->pgno); |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5648 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5649 | /* If this is an auto-vacuum database, and not a leaf-data tree, |
| 5650 | ** then update the pointer map with an entry for the overflow page |
| 5651 | ** that the cell just inserted points to (if any). |
| 5652 | */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5653 | if( ISAUTOVACUUM && !leafData ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5654 | rc = ptrmapPutOvfl(pParent, nxDiv); |
| 5655 | if( rc!=SQLITE_OK ){ |
| 5656 | goto balance_cleanup; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5657 | } |
| 5658 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5659 | j++; |
| 5660 | nxDiv++; |
| 5661 | } |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5662 | |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5663 | /* Set the pointer-map entry for the new sibling page. */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5664 | if( ISAUTOVACUUM ){ |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5665 | rc = ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno); |
| 5666 | if( rc!=SQLITE_OK ){ |
| 5667 | goto balance_cleanup; |
| 5668 | } |
| 5669 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5670 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5671 | assert( j==nCell ); |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 5672 | assert( nOld>0 ); |
| 5673 | assert( nNew>0 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5674 | if( (pageFlags & PTF_LEAF)==0 ){ |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5675 | u8 *zChild = &apCopy[nOld-1]->aData[8]; |
| 5676 | memcpy(&apNew[nNew-1]->aData[8], zChild, 4); |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5677 | if( ISAUTOVACUUM ){ |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5678 | rc = ptrmapPut(pBt, get4byte(zChild), PTRMAP_BTREE, apNew[nNew-1]->pgno); |
| 5679 | if( rc!=SQLITE_OK ){ |
| 5680 | goto balance_cleanup; |
| 5681 | } |
| 5682 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5683 | } |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 5684 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5685 | if( nxDiv==pParent->nCell+pParent->nOverflow ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5686 | /* Right-most sibling is the right-most child of pParent */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5687 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew[nNew-1]); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5688 | }else{ |
| 5689 | /* Right-most sibling is the left child of the first entry in pParent |
| 5690 | ** past the right-most divider entry */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5691 | put4byte(findOverflowCell(pParent, nxDiv), pgnoNew[nNew-1]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5692 | } |
| 5693 | |
| 5694 | /* |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5695 | ** Balance the parent page. Note that the current page (pPage) might |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5696 | ** have been added to the freelist so it might no longer be initialized. |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5697 | ** But the parent page will always be initialized. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5698 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5699 | assert( pParent->isInit ); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5700 | sqlite3ScratchFree(apCell); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5701 | apCell = 0; |
danielk1977 | a4124bd | 2008-12-23 10:37:47 +0000 | [diff] [blame] | 5702 | TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n", |
| 5703 | pPage->pgno, nOld, nNew, nCell)); |
| 5704 | pPage->nOverflow = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5705 | releasePage(pPage); |
| 5706 | pCur->iPage--; |
| 5707 | rc = balance(pCur, 0); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5708 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5709 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5710 | ** Cleanup before returning. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5711 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5712 | balance_cleanup: |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5713 | sqlite3PageFree(aSpace2); |
| 5714 | sqlite3ScratchFree(apCell); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5715 | for(i=0; i<nOld; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5716 | releasePage(apOld[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5717 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5718 | for(i=0; i<nNew; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5719 | releasePage(apNew[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5720 | } |
danielk1977 | a4124bd | 2008-12-23 10:37:47 +0000 | [diff] [blame] | 5721 | pCur->apPage[pCur->iPage]->nOverflow = 0; |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 5722 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5723 | return rc; |
| 5724 | } |
| 5725 | |
| 5726 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5727 | ** This routine is called for the root page of a btree when the root |
| 5728 | ** page contains no cells. This is an opportunity to make the tree |
| 5729 | ** shallower by one level. |
| 5730 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5731 | static int balance_shallower(BtCursor *pCur){ |
| 5732 | MemPage *pPage; /* Root page of B-Tree */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5733 | MemPage *pChild; /* The only child page of pPage */ |
| 5734 | Pgno pgnoChild; /* Page number for pChild */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5735 | int rc = SQLITE_OK; /* Return code from subprocedures */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5736 | BtShared *pBt; /* The main BTree structure */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5737 | int mxCellPerPage; /* Maximum number of cells per page */ |
| 5738 | u8 **apCell; /* All cells from pages being balanced */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5739 | u16 *szCell; /* Local size of all cells */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5740 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5741 | assert( pCur->iPage==0 ); |
| 5742 | pPage = pCur->apPage[0]; |
| 5743 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5744 | assert( pPage->nCell==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5745 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5746 | pBt = pPage->pBt; |
| 5747 | mxCellPerPage = MX_CELL(pBt); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5748 | apCell = sqlite3Malloc( mxCellPerPage*(sizeof(u8*)+sizeof(u16)) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5749 | if( apCell==0 ) return SQLITE_NOMEM; |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5750 | szCell = (u16*)&apCell[mxCellPerPage]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5751 | if( pPage->leaf ){ |
| 5752 | /* The table is completely empty */ |
| 5753 | TRACE(("BALANCE: empty table %d\n", pPage->pgno)); |
| 5754 | }else{ |
| 5755 | /* The root page is empty but has one child. Transfer the |
| 5756 | ** information from that one child into the root page if it |
| 5757 | ** will fit. This reduces the depth of the tree by one. |
| 5758 | ** |
| 5759 | ** If the root page is page 1, it has less space available than |
| 5760 | ** its child (due to the 100 byte header that occurs at the beginning |
| 5761 | ** of the database fle), so it might not be able to hold all of the |
| 5762 | ** information currently contained in the child. If this is the |
| 5763 | ** case, then do not do the transfer. Leave page 1 empty except |
| 5764 | ** for the right-pointer to the child page. The child page becomes |
| 5765 | ** the virtual root of the tree. |
| 5766 | */ |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 5767 | VVA_ONLY( pCur->pagesShuffled = 1 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5768 | pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 5769 | assert( pgnoChild>0 ); |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 5770 | assert( pgnoChild<=pagerPagecount(pPage->pBt) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5771 | rc = sqlite3BtreeGetPage(pPage->pBt, pgnoChild, &pChild, 0); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5772 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5773 | if( pPage->pgno==1 ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5774 | rc = sqlite3BtreeInitPage(pChild); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5775 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5776 | assert( pChild->nOverflow==0 ); |
| 5777 | if( pChild->nFree>=100 ){ |
| 5778 | /* The child information will fit on the root page, so do the |
| 5779 | ** copy */ |
| 5780 | int i; |
| 5781 | zeroPage(pPage, pChild->aData[0]); |
| 5782 | for(i=0; i<pChild->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5783 | apCell[i] = findCell(pChild,i); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5784 | szCell[i] = cellSizePtr(pChild, apCell[i]); |
| 5785 | } |
| 5786 | assemblePage(pPage, pChild->nCell, apCell, szCell); |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 5787 | /* Copy the right-pointer of the child to the parent. */ |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 5788 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 5789 | put4byte(&pPage->aData[pPage->hdrOffset+8], |
| 5790 | get4byte(&pChild->aData[pChild->hdrOffset+8])); |
drh | 9bf9e9c | 2008-12-05 20:01:43 +0000 | [diff] [blame] | 5791 | rc = freePage(pChild); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5792 | TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno)); |
| 5793 | }else{ |
| 5794 | /* The child has more information that will fit on the root. |
| 5795 | ** The tree is already balanced. Do nothing. */ |
| 5796 | TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno)); |
| 5797 | } |
| 5798 | }else{ |
| 5799 | memcpy(pPage->aData, pChild->aData, pPage->pBt->usableSize); |
| 5800 | pPage->isInit = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5801 | rc = sqlite3BtreeInitPage(pPage); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5802 | assert( rc==SQLITE_OK ); |
| 5803 | freePage(pChild); |
| 5804 | TRACE(("BALANCE: transfer child %d into root %d\n", |
| 5805 | pChild->pgno, pPage->pgno)); |
| 5806 | } |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5807 | assert( pPage->nOverflow==0 ); |
shane | 831c329 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 5808 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | 9bf9e9c | 2008-12-05 20:01:43 +0000 | [diff] [blame] | 5809 | if( ISAUTOVACUUM && rc==SQLITE_OK ){ |
danielk1977 | 00a696d | 2008-09-29 16:41:31 +0000 | [diff] [blame] | 5810 | rc = setChildPtrmaps(pPage); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5811 | } |
shane | 831c329 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 5812 | #endif |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5813 | releasePage(pChild); |
| 5814 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5815 | end_shallow_balance: |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5816 | sqlite3_free(apCell); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5817 | return rc; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5818 | } |
| 5819 | |
| 5820 | |
| 5821 | /* |
| 5822 | ** The root page is overfull |
| 5823 | ** |
| 5824 | ** When this happens, Create a new child page and copy the |
| 5825 | ** contents of the root into the child. Then make the root |
| 5826 | ** page an empty page with rightChild pointing to the new |
| 5827 | ** child. Finally, call balance_internal() on the new child |
| 5828 | ** to cause it to split. |
| 5829 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5830 | static int balance_deeper(BtCursor *pCur){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5831 | int rc; /* Return value from subprocedures */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5832 | MemPage *pPage; /* Pointer to the root page */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5833 | MemPage *pChild; /* Pointer to a new child page */ |
| 5834 | Pgno pgnoChild; /* Page number of the new child page */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5835 | BtShared *pBt; /* The BTree */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5836 | int usableSize; /* Total usable size of a page */ |
| 5837 | u8 *data; /* Content of the parent page */ |
| 5838 | u8 *cdata; /* Content of the child page */ |
| 5839 | int hdr; /* Offset to page header in parent */ |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 5840 | int cbrk; /* Offset to content of first cell in parent */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5841 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5842 | assert( pCur->iPage==0 ); |
| 5843 | assert( pCur->apPage[0]->nOverflow>0 ); |
| 5844 | |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 5845 | VVA_ONLY( pCur->pagesShuffled = 1 ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5846 | pPage = pCur->apPage[0]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5847 | pBt = pPage->pBt; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5848 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 5849 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5850 | rc = allocateBtreePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5851 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5852 | assert( sqlite3PagerIswriteable(pChild->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5853 | usableSize = pBt->usableSize; |
| 5854 | data = pPage->aData; |
| 5855 | hdr = pPage->hdrOffset; |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 5856 | cbrk = get2byte(&data[hdr+5]); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5857 | cdata = pChild->aData; |
| 5858 | memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr); |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 5859 | memcpy(&cdata[cbrk], &data[cbrk], usableSize-cbrk); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 5860 | |
| 5861 | assert( pChild->isInit==0 ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5862 | rc = sqlite3BtreeInitPage(pChild); |
| 5863 | if( rc==SQLITE_OK ){ |
| 5864 | int nCopy = pPage->nOverflow*sizeof(pPage->aOvfl[0]); |
| 5865 | memcpy(pChild->aOvfl, pPage->aOvfl, nCopy); |
| 5866 | pChild->nOverflow = pPage->nOverflow; |
| 5867 | if( pChild->nOverflow ){ |
| 5868 | pChild->nFree = 0; |
| 5869 | } |
| 5870 | assert( pChild->nCell==pPage->nCell ); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 5871 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5872 | zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF); |
| 5873 | put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild); |
| 5874 | TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno)); |
| 5875 | if( ISAUTOVACUUM ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5876 | rc = ptrmapPut(pBt, pChild->pgno, PTRMAP_BTREE, pPage->pgno); |
shane | 831c329 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 5877 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5878 | if( rc==SQLITE_OK ){ |
danielk1977 | 00a696d | 2008-09-29 16:41:31 +0000 | [diff] [blame] | 5879 | rc = setChildPtrmaps(pChild); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5880 | } |
drh | 30df009 | 2008-12-23 15:58:06 +0000 | [diff] [blame] | 5881 | if( rc ){ |
| 5882 | pChild->nOverflow = 0; |
| 5883 | } |
shane | 831c329 | 2008-11-10 17:14:58 +0000 | [diff] [blame] | 5884 | #endif |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5885 | } |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 5886 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5887 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5888 | if( rc==SQLITE_OK ){ |
| 5889 | pCur->iPage++; |
| 5890 | pCur->apPage[1] = pChild; |
danielk1977 | bf93c56 | 2008-09-29 15:53:25 +0000 | [diff] [blame] | 5891 | pCur->aiIdx[0] = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5892 | rc = balance_nonroot(pCur); |
| 5893 | }else{ |
| 5894 | releasePage(pChild); |
| 5895 | } |
| 5896 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5897 | return rc; |
| 5898 | } |
| 5899 | |
| 5900 | /* |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5901 | ** The page that pCur currently points to has just been modified in |
| 5902 | ** some way. This function figures out if this modification means the |
| 5903 | ** tree needs to be balanced, and if so calls the appropriate balancing |
| 5904 | ** routine. |
| 5905 | ** |
| 5906 | ** Parameter isInsert is true if a new cell was just inserted into the |
| 5907 | ** page, or false otherwise. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5908 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5909 | static int balance(BtCursor *pCur, int isInsert){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5910 | int rc = SQLITE_OK; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5911 | MemPage *pPage = pCur->apPage[pCur->iPage]; |
| 5912 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5913 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5914 | if( pCur->iPage==0 ){ |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 5915 | rc = sqlite3PagerWrite(pPage->pDbPage); |
| 5916 | if( rc==SQLITE_OK && pPage->nOverflow>0 ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5917 | rc = balance_deeper(pCur); |
danielk1977 | a4124bd | 2008-12-23 10:37:47 +0000 | [diff] [blame] | 5918 | assert( pCur->apPage[0]==pPage ); |
drh | 9bf9e9c | 2008-12-05 20:01:43 +0000 | [diff] [blame] | 5919 | assert( pPage->nOverflow==0 || rc!=SQLITE_OK ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5920 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5921 | if( rc==SQLITE_OK && pPage->nCell==0 ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5922 | rc = balance_shallower(pCur); |
danielk1977 | a4124bd | 2008-12-23 10:37:47 +0000 | [diff] [blame] | 5923 | assert( pCur->apPage[0]==pPage ); |
drh | 9bf9e9c | 2008-12-05 20:01:43 +0000 | [diff] [blame] | 5924 | assert( pPage->nOverflow==0 || rc!=SQLITE_OK ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5925 | } |
| 5926 | }else{ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5927 | if( pPage->nOverflow>0 || |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5928 | (!isInsert && pPage->nFree>pPage->pBt->usableSize*2/3) ){ |
| 5929 | rc = balance_nonroot(pCur); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5930 | } |
| 5931 | } |
| 5932 | return rc; |
| 5933 | } |
| 5934 | |
| 5935 | /* |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 5936 | ** This routine checks all cursors that point to table pgnoRoot. |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5937 | ** If any of those cursors were opened with wrFlag==0 in a different |
| 5938 | ** database connection (a database connection that shares the pager |
| 5939 | ** cache with the current connection) and that other connection |
| 5940 | ** is not in the ReadUncommmitted state, then this routine returns |
| 5941 | ** SQLITE_LOCKED. |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5942 | ** |
drh | 11b57d6 | 2009-02-24 19:21:41 +0000 | [diff] [blame] | 5943 | ** As well as cursors with wrFlag==0, cursors with |
| 5944 | ** isIncrblobHandle==1 are also considered 'read' cursors because |
| 5945 | ** incremental blob cursors are used for both reading and writing. |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 5946 | ** |
| 5947 | ** When pgnoRoot is the root page of an intkey table, this function is also |
| 5948 | ** responsible for invalidating incremental blob cursors when the table row |
| 5949 | ** on which they are opened is deleted or modified. Cursors are invalidated |
| 5950 | ** according to the following rules: |
| 5951 | ** |
| 5952 | ** 1) When BtreeClearTable() is called to completely delete the contents |
| 5953 | ** of a B-Tree table, pExclude is set to zero and parameter iRow is |
| 5954 | ** set to non-zero. In this case all incremental blob cursors open |
| 5955 | ** on the table rooted at pgnoRoot are invalidated. |
| 5956 | ** |
| 5957 | ** 2) When BtreeInsert(), BtreeDelete() or BtreePutData() is called to |
| 5958 | ** modify a table row via an SQL statement, pExclude is set to the |
| 5959 | ** write cursor used to do the modification and parameter iRow is set |
| 5960 | ** to the integer row id of the B-Tree entry being modified. Unless |
| 5961 | ** pExclude is itself an incremental blob cursor, then all incremental |
| 5962 | ** blob cursors open on row iRow of the B-Tree are invalidated. |
| 5963 | ** |
| 5964 | ** 3) If both pExclude and iRow are set to zero, no incremental blob |
| 5965 | ** cursors are invalidated. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5966 | */ |
drh | 11b57d6 | 2009-02-24 19:21:41 +0000 | [diff] [blame] | 5967 | static int checkForReadConflicts( |
| 5968 | Btree *pBtree, /* The database file to check */ |
| 5969 | Pgno pgnoRoot, /* Look for read cursors on this btree */ |
| 5970 | BtCursor *pExclude, /* Ignore this cursor */ |
| 5971 | i64 iRow /* The rowid that might be changing */ |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 5972 | ){ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5973 | BtCursor *p; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5974 | BtShared *pBt = pBtree->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 5975 | sqlite3 *db = pBtree->db; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5976 | assert( sqlite3BtreeHoldsMutex(pBtree) ); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5977 | for(p=pBt->pCursor; p; p=p->pNext){ |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5978 | if( p==pExclude ) continue; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5979 | if( p->pgnoRoot!=pgnoRoot ) continue; |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 5980 | #ifndef SQLITE_OMIT_INCRBLOB |
| 5981 | if( p->isIncrblobHandle && ( |
| 5982 | (!pExclude && iRow) |
| 5983 | || (pExclude && !pExclude->isIncrblobHandle && p->info.nKey==iRow) |
| 5984 | )){ |
| 5985 | p->eState = CURSOR_INVALID; |
| 5986 | } |
| 5987 | #endif |
| 5988 | if( p->eState!=CURSOR_VALID ) continue; |
| 5989 | if( p->wrFlag==0 |
| 5990 | #ifndef SQLITE_OMIT_INCRBLOB |
| 5991 | || p->isIncrblobHandle |
| 5992 | #endif |
| 5993 | ){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 5994 | sqlite3 *dbOther = p->pBtree->db; |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 5995 | assert(dbOther); |
| 5996 | if( dbOther!=db && (dbOther->flags & SQLITE_ReadUncommitted)==0 ){ |
| 5997 | sqlite3ConnectionBlocked(db, dbOther); |
| 5998 | return SQLITE_LOCKED_SHAREDCACHE; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5999 | } |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 6000 | } |
| 6001 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 6002 | return SQLITE_OK; |
| 6003 | } |
| 6004 | |
| 6005 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 6006 | ** Insert a new record into the BTree. The key is given by (pKey,nKey) |
| 6007 | ** 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] | 6008 | ** define what table the record should be inserted into. The cursor |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6009 | ** is left pointing at a random location. |
| 6010 | ** |
| 6011 | ** For an INTKEY table, only the nKey value of the key is used. pKey is |
| 6012 | ** ignored. For a ZERODATA table, the pData and nData are both ignored. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 6013 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 6014 | int sqlite3BtreeInsert( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 6015 | BtCursor *pCur, /* Insert data into the table of this cursor */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 6016 | const void *pKey, i64 nKey, /* The key of the new record */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 6017 | const void *pData, int nData, /* The data of the new record */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 6018 | int nZero, /* Number of extra 0 bytes to append to data */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 6019 | int appendBias /* True if this is likely an append */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 6020 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 6021 | int rc; |
| 6022 | int loc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6023 | int szNew; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6024 | int idx; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 6025 | MemPage *pPage; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6026 | Btree *p = pCur->pBtree; |
| 6027 | BtShared *pBt = p->pBt; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6028 | unsigned char *oldCell; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6029 | unsigned char *newCell = 0; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 6030 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6031 | assert( cursorHoldsMutex(pCur) ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 6032 | assert( pBt->inTransaction==TRANS_WRITE ); |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 6033 | assert( !pBt->readOnly ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 6034 | assert( pCur->wrFlag ); |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 6035 | rc = checkForReadConflicts(pCur->pBtree, pCur->pgnoRoot, pCur, nKey); |
| 6036 | if( rc ){ |
| 6037 | /* The table pCur points to has a read lock */ |
| 6038 | assert( rc==SQLITE_LOCKED_SHAREDCACHE ); |
| 6039 | return rc; |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 6040 | } |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 6041 | if( pCur->eState==CURSOR_FAULT ){ |
| 6042 | return pCur->skip; |
| 6043 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6044 | |
| 6045 | /* Save the positions of any other cursors open on this table */ |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 6046 | sqlite3BtreeClearCursor(pCur); |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 6047 | if( |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 6048 | SQLITE_OK!=(rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur)) || |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 6049 | SQLITE_OK!=(rc = sqlite3BtreeMoveto(pCur, pKey, nKey, appendBias, &loc)) |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 6050 | ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6051 | return rc; |
| 6052 | } |
| 6053 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6054 | pPage = pCur->apPage[pCur->iPage]; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 6055 | assert( pPage->intKey || nKey>=0 ); |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 6056 | assert( pPage->leaf || !pPage->intKey ); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6057 | TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n", |
| 6058 | pCur->pgnoRoot, nKey, nData, pPage->pgno, |
| 6059 | loc==0 ? "overwrite" : "new entry")); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6060 | assert( pPage->isInit ); |
danielk1977 | 52ae724 | 2008-03-25 14:24:56 +0000 | [diff] [blame] | 6061 | allocateTempSpace(pBt); |
| 6062 | newCell = pBt->pTmpSpace; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6063 | if( newCell==0 ) return SQLITE_NOMEM; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 6064 | rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6065 | if( rc ) goto end_insert; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6066 | assert( szNew==cellSizePtr(pPage, newCell) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6067 | assert( szNew<=MX_CELL_SIZE(pBt) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6068 | idx = pCur->aiIdx[pCur->iPage]; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6069 | if( loc==0 && CURSOR_VALID==pCur->eState ){ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 6070 | u16 szOld; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6071 | assert( idx<pPage->nCell ); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 6072 | rc = sqlite3PagerWrite(pPage->pDbPage); |
| 6073 | if( rc ){ |
| 6074 | goto end_insert; |
| 6075 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6076 | oldCell = findCell(pPage, idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6077 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6078 | memcpy(newCell, oldCell, 4); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6079 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6080 | szOld = cellSizePtr(pPage, oldCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6081 | rc = clearCell(pPage, oldCell); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6082 | if( rc ) goto end_insert; |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 6083 | rc = dropCell(pPage, idx, szOld); |
| 6084 | if( rc!=SQLITE_OK ) { |
| 6085 | goto end_insert; |
| 6086 | } |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 6087 | }else if( loc<0 && pPage->nCell>0 ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6088 | assert( pPage->leaf ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6089 | idx = ++pCur->aiIdx[pCur->iPage]; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 6090 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 6091 | pCur->validNKey = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6092 | }else{ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6093 | assert( pPage->leaf ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 6094 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6095 | rc = insertCell(pPage, idx, newCell, szNew, 0, 0); |
drh | 9bf9e9c | 2008-12-05 20:01:43 +0000 | [diff] [blame] | 6096 | if( rc==SQLITE_OK ){ |
| 6097 | rc = balance(pCur, 1); |
| 6098 | } |
| 6099 | |
| 6100 | /* Must make sure nOverflow is reset to zero even if the balance() |
| 6101 | ** fails. Internal data structure corruption will result otherwise. */ |
danielk1977 | a4124bd | 2008-12-23 10:37:47 +0000 | [diff] [blame] | 6102 | pCur->apPage[pCur->iPage]->nOverflow = 0; |
drh | 9bf9e9c | 2008-12-05 20:01:43 +0000 | [diff] [blame] | 6103 | |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 6104 | if( rc==SQLITE_OK ){ |
| 6105 | moveToRoot(pCur); |
| 6106 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6107 | end_insert: |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 6108 | return rc; |
| 6109 | } |
| 6110 | |
| 6111 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6112 | ** Delete the entry that the cursor is pointing to. The cursor |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 6113 | ** is left pointing at a arbitrary location. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 6114 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 6115 | int sqlite3BtreeDelete(BtCursor *pCur){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6116 | MemPage *pPage = pCur->apPage[pCur->iPage]; |
| 6117 | int idx; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6118 | unsigned char *pCell; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 6119 | int rc; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 6120 | Pgno pgnoChild = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6121 | Btree *p = pCur->pBtree; |
| 6122 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6123 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6124 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6125 | assert( pPage->isInit ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 6126 | assert( pBt->inTransaction==TRANS_WRITE ); |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 6127 | assert( !pBt->readOnly ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 6128 | if( pCur->eState==CURSOR_FAULT ){ |
| 6129 | return pCur->skip; |
| 6130 | } |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 6131 | if( NEVER(pCur->aiIdx[pCur->iPage]>=pPage->nCell) ){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 6132 | return SQLITE_ERROR; /* The cursor is not pointing to anything */ |
| 6133 | } |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 6134 | assert( pCur->wrFlag ); |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 6135 | rc = checkForReadConflicts(p, pCur->pgnoRoot, pCur, pCur->info.nKey); |
| 6136 | if( rc!=SQLITE_OK ){ |
| 6137 | /* The table pCur points to has a read lock */ |
| 6138 | assert( rc==SQLITE_LOCKED_SHAREDCACHE ); |
| 6139 | return rc; |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 6140 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6141 | |
| 6142 | /* Restore the current cursor position (a no-op if the cursor is not in |
| 6143 | ** CURSOR_REQUIRESEEK state) and save the positions of any other cursors |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6144 | ** open on the same table. Then call sqlite3PagerWrite() on the page |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6145 | ** that the entry will be deleted from. |
| 6146 | */ |
| 6147 | if( |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 6148 | (rc = restoreCursorPosition(pCur))!=0 || |
drh | d116739 | 2006-01-23 13:00:35 +0000 | [diff] [blame] | 6149 | (rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur))!=0 || |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6150 | (rc = sqlite3PagerWrite(pPage->pDbPage))!=0 |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6151 | ){ |
| 6152 | return rc; |
| 6153 | } |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 6154 | |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 6155 | /* Locate the cell within its page and leave pCell pointing to the |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 6156 | ** data. The clearCell() call frees any overflow pages associated with the |
| 6157 | ** cell. The cell itself is still intact. |
| 6158 | */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6159 | idx = pCur->aiIdx[pCur->iPage]; |
| 6160 | pCell = findCell(pPage, idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6161 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6162 | pgnoChild = get4byte(pCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6163 | } |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 6164 | rc = clearCell(pPage, pCell); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6165 | if( rc ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6166 | return rc; |
| 6167 | } |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 6168 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6169 | if( !pPage->leaf ){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6170 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6171 | ** 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] | 6172 | ** do something we will leave a hole on an internal page. |
| 6173 | ** We have to fill the hole by moving in a cell from a leaf. The |
| 6174 | ** next Cell after the one to be deleted is guaranteed to exist and |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 6175 | ** to be a leaf so we can use it. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 6176 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6177 | BtCursor leafCur; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 6178 | MemPage *pLeafPage = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6179 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6180 | unsigned char *pNext; |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 6181 | int notUsed; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6182 | unsigned char *tempCell = 0; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 6183 | assert( !pPage->intKey ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6184 | sqlite3BtreeGetTempCursor(pCur, &leafCur); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 6185 | rc = sqlite3BtreeNext(&leafCur, ¬Used); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6186 | if( rc==SQLITE_OK ){ |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6187 | assert( leafCur.aiIdx[leafCur.iPage]==0 ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6188 | pLeafPage = leafCur.apPage[leafCur.iPage]; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6189 | rc = sqlite3PagerWrite(pLeafPage->pDbPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6190 | } |
| 6191 | if( rc==SQLITE_OK ){ |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6192 | int leafCursorInvalid = 0; |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 6193 | u16 szNext; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6194 | TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n", |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6195 | pCur->pgnoRoot, pPage->pgno, pLeafPage->pgno)); |
| 6196 | dropCell(pPage, idx, cellSizePtr(pPage, pCell)); |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6197 | pNext = findCell(pLeafPage, 0); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6198 | szNext = cellSizePtr(pLeafPage, pNext); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6199 | assert( MX_CELL_SIZE(pBt)>=szNext+4 ); |
danielk1977 | 52ae724 | 2008-03-25 14:24:56 +0000 | [diff] [blame] | 6200 | allocateTempSpace(pBt); |
| 6201 | tempCell = pBt->pTmpSpace; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6202 | if( tempCell==0 ){ |
| 6203 | rc = SQLITE_NOMEM; |
| 6204 | } |
danielk1977 | 8ea1cfa | 2008-01-01 06:19:02 +0000 | [diff] [blame] | 6205 | if( rc==SQLITE_OK ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6206 | rc = insertCell(pPage, idx, pNext-4, szNext+4, tempCell, 0); |
danielk1977 | 8ea1cfa | 2008-01-01 06:19:02 +0000 | [diff] [blame] | 6207 | } |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6208 | |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 6209 | |
| 6210 | /* The "if" statement in the next code block is critical. The |
| 6211 | ** slightest error in that statement would allow SQLite to operate |
| 6212 | ** correctly most of the time but produce very rare failures. To |
| 6213 | ** guard against this, the following macros help to verify that |
| 6214 | ** the "if" statement is well tested. |
| 6215 | */ |
| 6216 | testcase( pPage->nOverflow==0 && pPage->nFree<pBt->usableSize*2/3 |
| 6217 | && pLeafPage->nFree+2+szNext > pBt->usableSize*2/3 ); |
| 6218 | testcase( pPage->nOverflow==0 && pPage->nFree==pBt->usableSize*2/3 |
| 6219 | && pLeafPage->nFree+2+szNext > pBt->usableSize*2/3 ); |
| 6220 | testcase( pPage->nOverflow==0 && pPage->nFree==pBt->usableSize*2/3+1 |
| 6221 | && pLeafPage->nFree+2+szNext > pBt->usableSize*2/3 ); |
| 6222 | testcase( pPage->nOverflow>0 && pPage->nFree<=pBt->usableSize*2/3 |
| 6223 | && pLeafPage->nFree+2+szNext > pBt->usableSize*2/3 ); |
| 6224 | testcase( (pPage->nOverflow>0 || (pPage->nFree > pBt->usableSize*2/3)) |
| 6225 | && pLeafPage->nFree+2+szNext == pBt->usableSize*2/3 ); |
| 6226 | |
| 6227 | |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6228 | if( (pPage->nOverflow>0 || (pPage->nFree > pBt->usableSize*2/3)) && |
| 6229 | (pLeafPage->nFree+2+szNext > pBt->usableSize*2/3) |
| 6230 | ){ |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 6231 | /* This branch is taken if the internal node is now either overflowing |
| 6232 | ** or underfull and the leaf node will be underfull after the just cell |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6233 | ** copied to the internal node is deleted from it. This is a special |
| 6234 | ** case because the call to balance() to correct the internal node |
| 6235 | ** may change the tree structure and invalidate the contents of |
| 6236 | ** the leafCur.apPage[] and leafCur.aiIdx[] arrays, which will be |
| 6237 | ** used by the balance() required to correct the underfull leaf |
| 6238 | ** node. |
| 6239 | ** |
| 6240 | ** The formula used in the expression above are based on facets of |
| 6241 | ** the SQLite file-format that do not change over time. |
| 6242 | */ |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 6243 | testcase( pPage->nFree==pBt->usableSize*2/3+1 ); |
| 6244 | testcase( pLeafPage->nFree+2+szNext==pBt->usableSize*2/3+1 ); |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6245 | leafCursorInvalid = 1; |
| 6246 | } |
| 6247 | |
danielk1977 | 8ea1cfa | 2008-01-01 06:19:02 +0000 | [diff] [blame] | 6248 | if( rc==SQLITE_OK ){ |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 6249 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6250 | put4byte(findOverflowCell(pPage, idx), pgnoChild); |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 6251 | VVA_ONLY( pCur->pagesShuffled = 0 ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6252 | rc = balance(pCur, 0); |
danielk1977 | 8ea1cfa | 2008-01-01 06:19:02 +0000 | [diff] [blame] | 6253 | } |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6254 | |
| 6255 | if( rc==SQLITE_OK && leafCursorInvalid ){ |
| 6256 | /* The leaf-node is now underfull and so the tree needs to be |
| 6257 | ** rebalanced. However, the balance() operation on the internal |
| 6258 | ** node above may have modified the structure of the B-Tree and |
| 6259 | ** so the current contents of leafCur.apPage[] and leafCur.aiIdx[] |
| 6260 | ** may not be trusted. |
| 6261 | ** |
| 6262 | ** It is not possible to copy the ancestry from pCur, as the same |
| 6263 | ** balance() call has invalidated the pCur->apPage[] and aiIdx[] |
| 6264 | ** arrays. |
drh | 7b68280 | 2008-09-30 14:06:28 +0000 | [diff] [blame] | 6265 | ** |
| 6266 | ** The call to saveCursorPosition() below internally saves the |
| 6267 | ** key that leafCur is currently pointing to. Currently, there |
| 6268 | ** are two copies of that key in the tree - one here on the leaf |
| 6269 | ** page and one on some internal node in the tree. The copy on |
| 6270 | ** the leaf node is always the next key in tree-order after the |
| 6271 | ** copy on the internal node. So, the call to sqlite3BtreeNext() |
| 6272 | ** calls restoreCursorPosition() to point the cursor to the copy |
| 6273 | ** stored on the internal node, then advances to the next entry, |
| 6274 | ** which happens to be the copy of the key on the internal node. |
danielk1977 | a69fda2 | 2008-09-30 16:48:10 +0000 | [diff] [blame] | 6275 | ** Net effect: leafCur is pointing back to the duplicate cell |
| 6276 | ** that needs to be removed, and the leafCur.apPage[] and |
| 6277 | ** leafCur.aiIdx[] arrays are correct. |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6278 | */ |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 6279 | VVA_ONLY( Pgno leafPgno = pLeafPage->pgno ); |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6280 | rc = saveCursorPosition(&leafCur); |
| 6281 | if( rc==SQLITE_OK ){ |
| 6282 | rc = sqlite3BtreeNext(&leafCur, ¬Used); |
| 6283 | } |
| 6284 | pLeafPage = leafCur.apPage[leafCur.iPage]; |
| 6285 | assert( pLeafPage->pgno==leafPgno ); |
| 6286 | assert( leafCur.aiIdx[leafCur.iPage]==0 ); |
| 6287 | } |
| 6288 | |
danielk1977 | 0cd1bbd | 2008-11-26 07:25:52 +0000 | [diff] [blame] | 6289 | if( SQLITE_OK==rc |
| 6290 | && SQLITE_OK==(rc = sqlite3PagerWrite(pLeafPage->pDbPage)) |
| 6291 | ){ |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6292 | dropCell(pLeafPage, 0, szNext); |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 6293 | VVA_ONLY( leafCur.pagesShuffled = 0 ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6294 | rc = balance(&leafCur, 0); |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 6295 | assert( leafCursorInvalid || !leafCur.pagesShuffled |
| 6296 | || !pCur->pagesShuffled ); |
danielk1977 | 8ea1cfa | 2008-01-01 06:19:02 +0000 | [diff] [blame] | 6297 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6298 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6299 | sqlite3BtreeReleaseTempCursor(&leafCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 6300 | }else{ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 6301 | TRACE(("DELETE: table=%d delete from leaf %d\n", |
| 6302 | pCur->pgnoRoot, pPage->pgno)); |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 6303 | rc = dropCell(pPage, idx, cellSizePtr(pPage, pCell)); |
| 6304 | if( rc==SQLITE_OK ){ |
| 6305 | rc = balance(pCur, 0); |
| 6306 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 6307 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6308 | if( rc==SQLITE_OK ){ |
| 6309 | moveToRoot(pCur); |
| 6310 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 6311 | return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 6312 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6313 | |
| 6314 | /* |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 6315 | ** Create a new BTree table. Write into *piTable the page |
| 6316 | ** number for the root page of the new table. |
| 6317 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 6318 | ** The type of type is determined by the flags parameter. Only the |
| 6319 | ** following values of flags are currently in use. Other values for |
| 6320 | ** flags might not work: |
| 6321 | ** |
| 6322 | ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys |
| 6323 | ** BTREE_ZERODATA Used for SQL indices |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6324 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6325 | static int btreeCreateTable(Btree *p, int *piTable, int flags){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6326 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6327 | MemPage *pRoot; |
| 6328 | Pgno pgnoRoot; |
| 6329 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6330 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6331 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 6332 | assert( pBt->inTransaction==TRANS_WRITE ); |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 6333 | assert( !pBt->readOnly ); |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 6334 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6335 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 6336 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6337 | if( rc ){ |
| 6338 | return rc; |
| 6339 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6340 | #else |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6341 | if( pBt->autoVacuum ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6342 | Pgno pgnoMove; /* Move a page here to make room for the root-page */ |
| 6343 | MemPage *pPageMove; /* The page to move to. */ |
| 6344 | |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 6345 | /* Creating a new table may probably require moving an existing database |
| 6346 | ** to make room for the new tables root page. In case this page turns |
| 6347 | ** out to be an overflow page, delete all overflow page-map caches |
| 6348 | ** held by open cursors. |
| 6349 | */ |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 6350 | invalidateAllOverflowCache(pBt); |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 6351 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6352 | /* Read the value of meta[3] from the database to determine where the |
| 6353 | ** root page of the new table should go. meta[3] is the largest root-page |
| 6354 | ** created so far, so the new root-page is (meta[3]+1). |
| 6355 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6356 | rc = sqlite3BtreeGetMeta(p, 4, &pgnoRoot); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6357 | if( rc!=SQLITE_OK ){ |
| 6358 | return rc; |
| 6359 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6360 | pgnoRoot++; |
| 6361 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6362 | /* The new root-page may not be allocated on a pointer-map page, or the |
| 6363 | ** PENDING_BYTE page. |
| 6364 | */ |
drh | 7219043 | 2008-01-31 14:54:43 +0000 | [diff] [blame] | 6365 | while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) || |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6366 | pgnoRoot==PENDING_BYTE_PAGE(pBt) ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6367 | pgnoRoot++; |
| 6368 | } |
| 6369 | assert( pgnoRoot>=3 ); |
| 6370 | |
| 6371 | /* Allocate a page. The page that currently resides at pgnoRoot will |
| 6372 | ** be moved to the allocated page (unless the allocated page happens |
| 6373 | ** to reside at pgnoRoot). |
| 6374 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 6375 | rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6376 | if( rc!=SQLITE_OK ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6377 | return rc; |
| 6378 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6379 | |
| 6380 | if( pgnoMove!=pgnoRoot ){ |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 6381 | /* pgnoRoot is the page that will be used for the root-page of |
| 6382 | ** the new table (assuming an error did not occur). But we were |
| 6383 | ** allocated pgnoMove. If required (i.e. if it was not allocated |
| 6384 | ** by extending the file), the current page at position pgnoMove |
| 6385 | ** is already journaled. |
| 6386 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6387 | u8 eType; |
| 6388 | Pgno iPtrPage; |
| 6389 | |
| 6390 | releasePage(pPageMove); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 6391 | |
| 6392 | /* Move the page currently at pgnoRoot to pgnoMove. */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6393 | rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6394 | if( rc!=SQLITE_OK ){ |
| 6395 | return rc; |
| 6396 | } |
| 6397 | rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage); |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 6398 | if( rc!=SQLITE_OK || eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6399 | releasePage(pRoot); |
| 6400 | return rc; |
| 6401 | } |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 6402 | assert( eType!=PTRMAP_ROOTPAGE ); |
| 6403 | assert( eType!=PTRMAP_FREEPAGE ); |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 6404 | rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6405 | releasePage(pRoot); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 6406 | |
| 6407 | /* Obtain the page at pgnoRoot */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6408 | if( rc!=SQLITE_OK ){ |
| 6409 | return rc; |
| 6410 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6411 | rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6412 | if( rc!=SQLITE_OK ){ |
| 6413 | return rc; |
| 6414 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6415 | rc = sqlite3PagerWrite(pRoot->pDbPage); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6416 | if( rc!=SQLITE_OK ){ |
| 6417 | releasePage(pRoot); |
| 6418 | return rc; |
| 6419 | } |
| 6420 | }else{ |
| 6421 | pRoot = pPageMove; |
| 6422 | } |
| 6423 | |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 6424 | /* Update the pointer-map and meta-data with the new root-page number. */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6425 | rc = ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0); |
| 6426 | if( rc ){ |
| 6427 | releasePage(pRoot); |
| 6428 | return rc; |
| 6429 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6430 | rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6431 | if( rc ){ |
| 6432 | releasePage(pRoot); |
| 6433 | return rc; |
| 6434 | } |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 6435 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6436 | }else{ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 6437 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6438 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6439 | } |
| 6440 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6441 | assert( sqlite3PagerIswriteable(pRoot->pDbPage) ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 6442 | zeroPage(pRoot, flags | PTF_LEAF); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6443 | sqlite3PagerUnref(pRoot->pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6444 | *piTable = (int)pgnoRoot; |
| 6445 | return SQLITE_OK; |
| 6446 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6447 | int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){ |
| 6448 | int rc; |
| 6449 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6450 | p->pBt->db = p->db; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6451 | rc = btreeCreateTable(p, piTable, flags); |
| 6452 | sqlite3BtreeLeave(p); |
| 6453 | return rc; |
| 6454 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6455 | |
| 6456 | /* |
| 6457 | ** Erase the given database page and all its children. Return |
| 6458 | ** the page to the freelist. |
| 6459 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6460 | static int clearDatabasePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6461 | BtShared *pBt, /* The BTree that contains the table */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6462 | Pgno pgno, /* Page number to clear */ |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6463 | int freePageFlag, /* Deallocate page if true */ |
| 6464 | int *pnChange |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6465 | ){ |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6466 | MemPage *pPage = 0; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6467 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6468 | unsigned char *pCell; |
| 6469 | int i; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6470 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6471 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 6472 | if( pgno>pagerPagecount(pBt) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 6473 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 6474 | } |
| 6475 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6476 | rc = getAndInitPage(pBt, pgno, &pPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6477 | if( rc ) goto cleardatabasepage_out; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6478 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 6479 | pCell = findCell(pPage, i); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6480 | if( !pPage->leaf ){ |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 6481 | rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6482 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6483 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6484 | rc = clearCell(pPage, pCell); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6485 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6486 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6487 | if( !pPage->leaf ){ |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 6488 | rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), 1, pnChange); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6489 | if( rc ) goto cleardatabasepage_out; |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6490 | }else if( pnChange ){ |
| 6491 | assert( pPage->intKey ); |
| 6492 | *pnChange += pPage->nCell; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6493 | } |
| 6494 | if( freePageFlag ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6495 | rc = freePage(pPage); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6496 | }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6497 | zeroPage(pPage, pPage->aData[0] | PTF_LEAF); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6498 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6499 | |
| 6500 | cleardatabasepage_out: |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6501 | releasePage(pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6502 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6503 | } |
| 6504 | |
| 6505 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 6506 | ** Delete all information from a single table in the database. iTable is |
| 6507 | ** the page number of the root of the table. After this routine returns, |
| 6508 | ** the root page is empty, but still exists. |
| 6509 | ** |
| 6510 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 6511 | ** read cursors on the table. Open write cursors are moved to the |
| 6512 | ** root of the table. |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6513 | ** |
| 6514 | ** If pnChange is not NULL, then table iTable must be an intkey table. The |
| 6515 | ** integer value pointed to by pnChange is incremented by the number of |
| 6516 | ** entries in the table. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6517 | */ |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6518 | int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6519 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6520 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6521 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6522 | pBt->db = p->db; |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 6523 | assert( p->inTrans==TRANS_WRITE ); |
drh | 11b57d6 | 2009-02-24 19:21:41 +0000 | [diff] [blame] | 6524 | if( (rc = checkForReadConflicts(p, iTable, 0, 1))!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6525 | /* nothing to do */ |
| 6526 | }else if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){ |
| 6527 | /* nothing to do */ |
| 6528 | }else{ |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 6529 | rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6530 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6531 | sqlite3BtreeLeave(p); |
| 6532 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6533 | } |
| 6534 | |
| 6535 | /* |
| 6536 | ** Erase all information in a table and add the root of the table to |
| 6537 | ** the freelist. Except, the root of the principle table (the one on |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 6538 | ** page 1) is never added to the freelist. |
| 6539 | ** |
| 6540 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 6541 | ** cursors on the table. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6542 | ** |
| 6543 | ** If AUTOVACUUM is enabled and the page at iTable is not the last |
| 6544 | ** root page in the database file, then the last root page |
| 6545 | ** in the database file is moved into the slot formerly occupied by |
| 6546 | ** iTable and that last slot formerly occupied by the last root page |
| 6547 | ** is added to the freelist instead of iTable. In this say, all |
| 6548 | ** root pages are kept at the beginning of the database file, which |
| 6549 | ** is necessary for AUTOVACUUM to work right. *piMoved is set to the |
| 6550 | ** page number that used to be the last root page in the file before |
| 6551 | ** the move. If no page gets moved, *piMoved is set to 0. |
| 6552 | ** The last root page is recorded in meta[3] and the value of |
| 6553 | ** meta[3] is updated by this procedure. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6554 | */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 6555 | static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6556 | int rc; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6557 | MemPage *pPage = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6558 | BtShared *pBt = p->pBt; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6559 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6560 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 6561 | assert( p->inTrans==TRANS_WRITE ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6562 | |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 6563 | /* It is illegal to drop a table if any cursors are open on the |
| 6564 | ** database. This is because in auto-vacuum mode the backend may |
| 6565 | ** need to move another root-page to fill a gap left by the deleted |
| 6566 | ** root page. If an open cursor was using this page a problem would |
| 6567 | ** occur. |
| 6568 | */ |
| 6569 | if( pBt->pCursor ){ |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 6570 | sqlite3ConnectionBlocked(p->db, pBt->pCursor->pBtree->db); |
| 6571 | return SQLITE_LOCKED_SHAREDCACHE; |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 6572 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6573 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6574 | rc = sqlite3BtreeGetPage(pBt, (Pgno)iTable, &pPage, 0); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6575 | if( rc ) return rc; |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6576 | rc = sqlite3BtreeClearTable(p, iTable, 0); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6577 | if( rc ){ |
| 6578 | releasePage(pPage); |
| 6579 | return rc; |
| 6580 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6581 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6582 | *piMoved = 0; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6583 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6584 | if( iTable>1 ){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6585 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6586 | rc = freePage(pPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6587 | releasePage(pPage); |
| 6588 | #else |
| 6589 | if( pBt->autoVacuum ){ |
| 6590 | Pgno maxRootPgno; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6591 | rc = sqlite3BtreeGetMeta(p, 4, &maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6592 | if( rc!=SQLITE_OK ){ |
| 6593 | releasePage(pPage); |
| 6594 | return rc; |
| 6595 | } |
| 6596 | |
| 6597 | if( iTable==maxRootPgno ){ |
| 6598 | /* If the table being dropped is the table with the largest root-page |
| 6599 | ** number in the database, put the root page on the free list. |
| 6600 | */ |
| 6601 | rc = freePage(pPage); |
| 6602 | releasePage(pPage); |
| 6603 | if( rc!=SQLITE_OK ){ |
| 6604 | return rc; |
| 6605 | } |
| 6606 | }else{ |
| 6607 | /* The table being dropped does not have the largest root-page |
| 6608 | ** number in the database. So move the page that does into the |
| 6609 | ** gap left by the deleted root-page. |
| 6610 | */ |
| 6611 | MemPage *pMove; |
| 6612 | releasePage(pPage); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6613 | rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6614 | if( rc!=SQLITE_OK ){ |
| 6615 | return rc; |
| 6616 | } |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 6617 | rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6618 | releasePage(pMove); |
| 6619 | if( rc!=SQLITE_OK ){ |
| 6620 | return rc; |
| 6621 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6622 | rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6623 | if( rc!=SQLITE_OK ){ |
| 6624 | return rc; |
| 6625 | } |
| 6626 | rc = freePage(pMove); |
| 6627 | releasePage(pMove); |
| 6628 | if( rc!=SQLITE_OK ){ |
| 6629 | return rc; |
| 6630 | } |
| 6631 | *piMoved = maxRootPgno; |
| 6632 | } |
| 6633 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6634 | /* Set the new 'max-root-page' value in the database header. This |
| 6635 | ** is the old value less one, less one more if that happens to |
| 6636 | ** be a root-page number, less one again if that is the |
| 6637 | ** PENDING_BYTE_PAGE. |
| 6638 | */ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 6639 | maxRootPgno--; |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6640 | if( maxRootPgno==PENDING_BYTE_PAGE(pBt) ){ |
| 6641 | maxRootPgno--; |
| 6642 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6643 | if( maxRootPgno==PTRMAP_PAGENO(pBt, maxRootPgno) ){ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 6644 | maxRootPgno--; |
| 6645 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6646 | assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) ); |
| 6647 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6648 | rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6649 | }else{ |
| 6650 | rc = freePage(pPage); |
| 6651 | releasePage(pPage); |
| 6652 | } |
| 6653 | #endif |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6654 | }else{ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6655 | /* If sqlite3BtreeDropTable was called on page 1. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6656 | zeroPage(pPage, PTF_INTKEY|PTF_LEAF ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6657 | releasePage(pPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6658 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6659 | return rc; |
| 6660 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6661 | int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){ |
| 6662 | int rc; |
| 6663 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6664 | p->pBt->db = p->db; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6665 | rc = btreeDropTable(p, iTable, piMoved); |
| 6666 | sqlite3BtreeLeave(p); |
| 6667 | return rc; |
| 6668 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6669 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 6670 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6671 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6672 | ** Read the meta-information out of a database file. Meta[0] |
| 6673 | ** is the number of free pages currently in the database. Meta[1] |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 6674 | ** through meta[15] are available for use by higher layers. Meta[0] |
| 6675 | ** is read-only, the others are read/write. |
| 6676 | ** |
| 6677 | ** The schema layer numbers meta values differently. At the schema |
| 6678 | ** layer (and the SetCookie and ReadCookie opcodes) the number of |
| 6679 | ** 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] | 6680 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6681 | int sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){ |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 6682 | DbPage *pDbPage = 0; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6683 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6684 | unsigned char *pP1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6685 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6686 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6687 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6688 | pBt->db = p->db; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6689 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6690 | /* Reading a meta-data value requires a read-lock on page 1 (and hence |
| 6691 | ** the sqlite_master table. We grab this lock regardless of whether or |
| 6692 | ** not the SQLITE_ReadUncommitted flag is set (the table rooted at page |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 6693 | ** 1 is treated as a special case by querySharedCacheTableLock() |
| 6694 | ** and setSharedCacheTableLock()). |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6695 | */ |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 6696 | rc = querySharedCacheTableLock(p, 1, READ_LOCK); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6697 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6698 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6699 | return rc; |
| 6700 | } |
| 6701 | |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6702 | assert( idx>=0 && idx<=15 ); |
danielk1977 | d9f6c53 | 2008-09-19 16:39:38 +0000 | [diff] [blame] | 6703 | if( pBt->pPage1 ){ |
| 6704 | /* The b-tree is already holding a reference to page 1 of the database |
| 6705 | ** file. In this case the required meta-data value can be read directly |
| 6706 | ** from the page data of this reference. This is slightly faster than |
| 6707 | ** requesting a new reference from the pager layer. |
| 6708 | */ |
| 6709 | pP1 = (unsigned char *)pBt->pPage1->aData; |
| 6710 | }else{ |
| 6711 | /* The b-tree does not have a reference to page 1 of the database file. |
| 6712 | ** Obtain one from the pager layer. |
| 6713 | */ |
danielk1977 | ea89730 | 2008-09-19 15:10:58 +0000 | [diff] [blame] | 6714 | rc = sqlite3PagerGet(pBt->pPager, 1, &pDbPage); |
| 6715 | if( rc ){ |
| 6716 | sqlite3BtreeLeave(p); |
| 6717 | return rc; |
| 6718 | } |
| 6719 | pP1 = (unsigned char *)sqlite3PagerGetData(pDbPage); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6720 | } |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6721 | *pMeta = get4byte(&pP1[36 + idx*4]); |
danielk1977 | ea89730 | 2008-09-19 15:10:58 +0000 | [diff] [blame] | 6722 | |
danielk1977 | d9f6c53 | 2008-09-19 16:39:38 +0000 | [diff] [blame] | 6723 | /* If the b-tree is not holding a reference to page 1, then one was |
| 6724 | ** requested from the pager layer in the above block. Release it now. |
| 6725 | */ |
danielk1977 | ea89730 | 2008-09-19 15:10:58 +0000 | [diff] [blame] | 6726 | if( !pBt->pPage1 ){ |
| 6727 | sqlite3PagerUnref(pDbPage); |
| 6728 | } |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 6729 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6730 | /* If autovacuumed is disabled in this build but we are trying to |
| 6731 | ** access an autovacuumed database, then make the database readonly. |
| 6732 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6733 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 6734 | if( idx==4 && *pMeta>0 ) pBt->readOnly = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6735 | #endif |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 6736 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6737 | /* Grab the read-lock on page 1. */ |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 6738 | rc = setSharedCacheTableLock(p, 1, READ_LOCK); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6739 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6740 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6741 | } |
| 6742 | |
| 6743 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6744 | ** Write meta-information back into the database. Meta[0] is |
| 6745 | ** read-only and may not be written. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6746 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6747 | int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){ |
| 6748 | BtShared *pBt = p->pBt; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6749 | unsigned char *pP1; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6750 | int rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6751 | assert( idx>=1 && idx<=15 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6752 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6753 | pBt->db = p->db; |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 6754 | assert( p->inTrans==TRANS_WRITE ); |
| 6755 | assert( pBt->pPage1!=0 ); |
| 6756 | pP1 = pBt->pPage1->aData; |
| 6757 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 6758 | if( rc==SQLITE_OK ){ |
| 6759 | put4byte(&pP1[36 + idx*4], iMeta); |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 +0000 | [diff] [blame] | 6760 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 6761 | if( idx==7 ){ |
| 6762 | assert( pBt->autoVacuum || iMeta==0 ); |
| 6763 | assert( iMeta==0 || iMeta==1 ); |
| 6764 | pBt->incrVacuum = (u8)iMeta; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6765 | } |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 6766 | #endif |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 6767 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6768 | sqlite3BtreeLeave(p); |
| 6769 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6770 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 6771 | |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 6772 | /* |
| 6773 | ** Return the flag byte at the beginning of the page that the cursor |
| 6774 | ** is currently pointing to. |
| 6775 | */ |
| 6776 | int sqlite3BtreeFlags(BtCursor *pCur){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6777 | /* TODO: What about CURSOR_REQUIRESEEK state? Probably need to call |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 6778 | ** restoreCursorPosition() here. |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6779 | */ |
danielk1977 | e448dc4 | 2008-01-02 11:50:51 +0000 | [diff] [blame] | 6780 | MemPage *pPage; |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 6781 | restoreCursorPosition(pCur); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6782 | pPage = pCur->apPage[pCur->iPage]; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6783 | assert( cursorHoldsMutex(pCur) ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 6784 | assert( pPage!=0 ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 6785 | assert( pPage->pBt==pCur->pBt ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 6786 | return pPage->aData[pPage->hdrOffset]; |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 6787 | } |
| 6788 | |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 6789 | #ifndef SQLITE_OMIT_BTREECOUNT |
| 6790 | /* |
| 6791 | ** The first argument, pCur, is a cursor opened on some b-tree. Count the |
| 6792 | ** number of entries in the b-tree and write the result to *pnEntry. |
| 6793 | ** |
| 6794 | ** SQLITE_OK is returned if the operation is successfully executed. |
| 6795 | ** Otherwise, if an error is encountered (i.e. an IO error or database |
| 6796 | ** corruption) an SQLite error code is returned. |
| 6797 | */ |
| 6798 | int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){ |
| 6799 | i64 nEntry = 0; /* Value to return in *pnEntry */ |
| 6800 | int rc; /* Return code */ |
| 6801 | rc = moveToRoot(pCur); |
| 6802 | |
| 6803 | /* Unless an error occurs, the following loop runs one iteration for each |
| 6804 | ** page in the B-Tree structure (not including overflow pages). |
| 6805 | */ |
| 6806 | while( rc==SQLITE_OK ){ |
| 6807 | int iIdx; /* Index of child node in parent */ |
| 6808 | MemPage *pPage; /* Current page of the b-tree */ |
| 6809 | |
| 6810 | /* If this is a leaf page or the tree is not an int-key tree, then |
| 6811 | ** this page contains countable entries. Increment the entry counter |
| 6812 | ** accordingly. |
| 6813 | */ |
| 6814 | pPage = pCur->apPage[pCur->iPage]; |
| 6815 | if( pPage->leaf || !pPage->intKey ){ |
| 6816 | nEntry += pPage->nCell; |
| 6817 | } |
| 6818 | |
| 6819 | /* pPage is a leaf node. This loop navigates the cursor so that it |
| 6820 | ** points to the first interior cell that it points to the parent of |
| 6821 | ** the next page in the tree that has not yet been visited. The |
| 6822 | ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell |
| 6823 | ** of the page, or to the number of cells in the page if the next page |
| 6824 | ** to visit is the right-child of its parent. |
| 6825 | ** |
| 6826 | ** If all pages in the tree have been visited, return SQLITE_OK to the |
| 6827 | ** caller. |
| 6828 | */ |
| 6829 | if( pPage->leaf ){ |
| 6830 | do { |
| 6831 | if( pCur->iPage==0 ){ |
| 6832 | /* All pages of the b-tree have been visited. Return successfully. */ |
| 6833 | *pnEntry = nEntry; |
| 6834 | return SQLITE_OK; |
| 6835 | } |
| 6836 | sqlite3BtreeMoveToParent(pCur); |
| 6837 | }while ( pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell ); |
| 6838 | |
| 6839 | pCur->aiIdx[pCur->iPage]++; |
| 6840 | pPage = pCur->apPage[pCur->iPage]; |
| 6841 | } |
| 6842 | |
| 6843 | /* Descend to the child node of the cell that the cursor currently |
| 6844 | ** points at. This is the right-child if (iIdx==pPage->nCell). |
| 6845 | */ |
| 6846 | iIdx = pCur->aiIdx[pCur->iPage]; |
| 6847 | if( iIdx==pPage->nCell ){ |
| 6848 | rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); |
| 6849 | }else{ |
| 6850 | rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx))); |
| 6851 | } |
| 6852 | } |
| 6853 | |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 6854 | /* An error has occurred. Return an error code. */ |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 6855 | return rc; |
| 6856 | } |
| 6857 | #endif |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6858 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6859 | /* |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6860 | ** Return the pager associated with a BTree. This routine is used for |
| 6861 | ** testing and debugging only. |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6862 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6863 | Pager *sqlite3BtreePager(Btree *p){ |
| 6864 | return p->pBt->pPager; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6865 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6866 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6867 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6868 | /* |
| 6869 | ** Append a message to the error message string. |
| 6870 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6871 | static void checkAppendMsg( |
| 6872 | IntegrityCk *pCheck, |
| 6873 | char *zMsg1, |
| 6874 | const char *zFormat, |
| 6875 | ... |
| 6876 | ){ |
| 6877 | va_list ap; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6878 | if( !pCheck->mxErr ) return; |
| 6879 | pCheck->mxErr--; |
| 6880 | pCheck->nErr++; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6881 | va_start(ap, zFormat); |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 6882 | if( pCheck->errMsg.nChar ){ |
| 6883 | sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6884 | } |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 6885 | if( zMsg1 ){ |
| 6886 | sqlite3StrAccumAppend(&pCheck->errMsg, zMsg1, -1); |
| 6887 | } |
| 6888 | sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap); |
| 6889 | va_end(ap); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 6890 | if( pCheck->errMsg.mallocFailed ){ |
| 6891 | pCheck->mallocFailed = 1; |
| 6892 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6893 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6894 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6895 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6896 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6897 | /* |
| 6898 | ** Add 1 to the reference count for page iPage. If this is the second |
| 6899 | ** reference to the page, add an error message to pCheck->zErrMsg. |
| 6900 | ** Return 1 if there are 2 ore more references to the page and 0 if |
| 6901 | ** if this is the first reference to the page. |
| 6902 | ** |
| 6903 | ** Also check that the page number is in bounds. |
| 6904 | */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 6905 | static int checkRef(IntegrityCk *pCheck, Pgno iPage, char *zContext){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6906 | if( iPage==0 ) return 1; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 6907 | if( iPage>pCheck->nPage ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6908 | checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6909 | return 1; |
| 6910 | } |
| 6911 | if( pCheck->anRef[iPage]==1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6912 | checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6913 | return 1; |
| 6914 | } |
| 6915 | return (pCheck->anRef[iPage]++)>1; |
| 6916 | } |
| 6917 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6918 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6919 | /* |
| 6920 | ** Check that the entry in the pointer-map for page iChild maps to |
| 6921 | ** page iParent, pointer type ptrType. If not, append an error message |
| 6922 | ** to pCheck. |
| 6923 | */ |
| 6924 | static void checkPtrmap( |
| 6925 | IntegrityCk *pCheck, /* Integrity check context */ |
| 6926 | Pgno iChild, /* Child page number */ |
| 6927 | u8 eType, /* Expected pointer map type */ |
| 6928 | Pgno iParent, /* Expected pointer map parent page number */ |
| 6929 | char *zContext /* Context description (used for error msg) */ |
| 6930 | ){ |
| 6931 | int rc; |
| 6932 | u8 ePtrmapType; |
| 6933 | Pgno iPtrmapParent; |
| 6934 | |
| 6935 | rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent); |
| 6936 | if( rc!=SQLITE_OK ){ |
drh | e43ba70 | 2008-12-05 22:40:08 +0000 | [diff] [blame] | 6937 | if( rc==SQLITE_NOMEM ) pCheck->mallocFailed = 1; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6938 | checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild); |
| 6939 | return; |
| 6940 | } |
| 6941 | |
| 6942 | if( ePtrmapType!=eType || iPtrmapParent!=iParent ){ |
| 6943 | checkAppendMsg(pCheck, zContext, |
| 6944 | "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", |
| 6945 | iChild, eType, iParent, ePtrmapType, iPtrmapParent); |
| 6946 | } |
| 6947 | } |
| 6948 | #endif |
| 6949 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6950 | /* |
| 6951 | ** Check the integrity of the freelist or of an overflow page list. |
| 6952 | ** Verify that the number of pages on the list is N. |
| 6953 | */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6954 | static void checkList( |
| 6955 | IntegrityCk *pCheck, /* Integrity checking context */ |
| 6956 | int isFreeList, /* True for a freelist. False for overflow page list */ |
| 6957 | int iPage, /* Page number for first page in the list */ |
| 6958 | int N, /* Expected number of pages in the list */ |
| 6959 | char *zContext /* Context for error messages */ |
| 6960 | ){ |
| 6961 | int i; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6962 | int expected = N; |
| 6963 | int iFirst = iPage; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6964 | while( N-- > 0 && pCheck->mxErr ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6965 | DbPage *pOvflPage; |
| 6966 | unsigned char *pOvflData; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6967 | if( iPage<1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6968 | checkAppendMsg(pCheck, zContext, |
| 6969 | "%d of %d pages missing from overflow list starting at %d", |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6970 | N+1, expected, iFirst); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6971 | break; |
| 6972 | } |
| 6973 | if( checkRef(pCheck, iPage, zContext) ) break; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6974 | if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6975 | checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6976 | break; |
| 6977 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6978 | pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6979 | if( isFreeList ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6980 | int n = get4byte(&pOvflData[4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6981 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6982 | if( pCheck->pBt->autoVacuum ){ |
| 6983 | checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext); |
| 6984 | } |
| 6985 | #endif |
drh | 45b1fac | 2008-07-04 17:52:42 +0000 | [diff] [blame] | 6986 | if( n>pCheck->pBt->usableSize/4-2 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6987 | checkAppendMsg(pCheck, zContext, |
| 6988 | "freelist leaf count too big on page %d", iPage); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 6989 | N--; |
| 6990 | }else{ |
| 6991 | for(i=0; i<n; i++){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6992 | Pgno iFreePage = get4byte(&pOvflData[8+i*4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6993 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6994 | if( pCheck->pBt->autoVacuum ){ |
| 6995 | checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext); |
| 6996 | } |
| 6997 | #endif |
| 6998 | checkRef(pCheck, iFreePage, zContext); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 6999 | } |
| 7000 | N -= n; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 7001 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 7002 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7003 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 7004 | else{ |
| 7005 | /* If this database supports auto-vacuum and iPage is not the last |
| 7006 | ** page in this overflow list, check that the pointer-map entry for |
| 7007 | ** the following page matches iPage. |
| 7008 | */ |
| 7009 | if( pCheck->pBt->autoVacuum && N>0 ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7010 | i = get4byte(pOvflData); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 7011 | checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext); |
| 7012 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7013 | } |
| 7014 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7015 | iPage = get4byte(pOvflData); |
| 7016 | sqlite3PagerUnref(pOvflPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7017 | } |
| 7018 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 7019 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7020 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 7021 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7022 | /* |
| 7023 | ** Do various sanity checks on a single page of a tree. Return |
| 7024 | ** the tree depth. Root pages return 0. Parents of root pages |
| 7025 | ** return 1, and so forth. |
| 7026 | ** |
| 7027 | ** These checks are done: |
| 7028 | ** |
| 7029 | ** 1. Make sure that cells and freeblocks do not overlap |
| 7030 | ** but combine to completely cover the page. |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 7031 | ** NO 2. Make sure cell keys are in order. |
| 7032 | ** NO 3. Make sure no key is less than or equal to zLowerBound. |
| 7033 | ** NO 4. Make sure no key is greater than or equal to zUpperBound. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7034 | ** 5. Check the integrity of overflow pages. |
| 7035 | ** 6. Recursively call checkTreePage on all children. |
| 7036 | ** 7. Verify that the depth of all children is the same. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 7037 | ** 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] | 7038 | ** the root of the tree. |
| 7039 | */ |
| 7040 | static int checkTreePage( |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 7041 | IntegrityCk *pCheck, /* Context for the sanity check */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7042 | int iPage, /* Page number of the page to check */ |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 7043 | char *zParentContext /* Parent context */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7044 | ){ |
| 7045 | MemPage *pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 7046 | int i, rc, depth, d2, pgno, cnt; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 7047 | int hdr, cellStart; |
| 7048 | int nCell; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 7049 | u8 *data; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7050 | BtShared *pBt; |
drh | 4f26bb6 | 2005-09-08 14:17:20 +0000 | [diff] [blame] | 7051 | int usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7052 | char zContext[100]; |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 7053 | char *hit = 0; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7054 | |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 7055 | sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage); |
danielk1977 | ef73ee9 | 2004-11-06 12:26:07 +0000 | [diff] [blame] | 7056 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7057 | /* Check that the page exists |
| 7058 | */ |
drh | d9cb6ac | 2005-10-20 07:28:17 +0000 | [diff] [blame] | 7059 | pBt = pCheck->pBt; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 7060 | usableSize = pBt->usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7061 | if( iPage==0 ) return 0; |
| 7062 | if( checkRef(pCheck, iPage, zParentContext) ) return 0; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 7063 | if( (rc = sqlite3BtreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){ |
drh | e43ba70 | 2008-12-05 22:40:08 +0000 | [diff] [blame] | 7064 | if( rc==SQLITE_NOMEM ) pCheck->mallocFailed = 1; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7065 | checkAppendMsg(pCheck, zContext, |
| 7066 | "unable to get the page. error code=%d", rc); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7067 | return 0; |
| 7068 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 7069 | if( (rc = sqlite3BtreeInitPage(pPage))!=0 ){ |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 7070 | assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 7071 | checkAppendMsg(pCheck, zContext, |
| 7072 | "sqlite3BtreeInitPage() returns error code %d", rc); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 7073 | releasePage(pPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7074 | return 0; |
| 7075 | } |
| 7076 | |
| 7077 | /* Check out all the cells. |
| 7078 | */ |
| 7079 | depth = 0; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 7080 | for(i=0; i<pPage->nCell && pCheck->mxErr; i++){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 7081 | u8 *pCell; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 7082 | u32 sz; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 7083 | CellInfo info; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7084 | |
| 7085 | /* Check payload overflow pages |
| 7086 | */ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 7087 | sqlite3_snprintf(sizeof(zContext), zContext, |
| 7088 | "On tree page %d cell %d: ", iPage, i); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 7089 | pCell = findCell(pPage,i); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 7090 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 7091 | sz = info.nData; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 7092 | if( !pPage->intKey ) sz += (int)info.nKey; |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 7093 | assert( sz==info.nPayload ); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 7094 | if( sz>info.nLocal ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 7095 | int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7096 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
| 7097 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 7098 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 7099 | checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7100 | } |
| 7101 | #endif |
| 7102 | checkList(pCheck, 0, pgnoOvfl, nPage, zContext); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7103 | } |
| 7104 | |
| 7105 | /* Check sanity of left child page. |
| 7106 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 7107 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 7108 | pgno = get4byte(pCell); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7109 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 7110 | if( pBt->autoVacuum ){ |
| 7111 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext); |
| 7112 | } |
| 7113 | #endif |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 7114 | d2 = checkTreePage(pCheck, pgno, zContext); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 7115 | if( i>0 && d2!=depth ){ |
| 7116 | checkAppendMsg(pCheck, zContext, "Child page depth differs"); |
| 7117 | } |
| 7118 | depth = d2; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7119 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7120 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 7121 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 7122 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 7123 | sqlite3_snprintf(sizeof(zContext), zContext, |
| 7124 | "On page %d at right child: ", iPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7125 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 7126 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 7127 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7128 | } |
| 7129 | #endif |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 7130 | checkTreePage(pCheck, pgno, zContext); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 7131 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7132 | |
| 7133 | /* Check for complete coverage of the page |
| 7134 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 7135 | data = pPage->aData; |
| 7136 | hdr = pPage->hdrOffset; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 7137 | hit = sqlite3PageMalloc( pBt->pageSize ); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 7138 | if( hit==0 ){ |
| 7139 | pCheck->mallocFailed = 1; |
| 7140 | }else{ |
shane | 5780ebd | 2008-11-11 17:36:30 +0000 | [diff] [blame] | 7141 | u16 contentOffset = get2byte(&data[hdr+5]); |
| 7142 | if (contentOffset > usableSize) { |
| 7143 | checkAppendMsg(pCheck, 0, |
| 7144 | "Corruption detected in header on page %d",iPage,0); |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 7145 | goto check_page_abort; |
shane | 5780ebd | 2008-11-11 17:36:30 +0000 | [diff] [blame] | 7146 | } |
| 7147 | memset(hit+contentOffset, 0, usableSize-contentOffset); |
| 7148 | memset(hit, 1, contentOffset); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7149 | nCell = get2byte(&data[hdr+3]); |
| 7150 | cellStart = hdr + 12 - 4*pPage->leaf; |
| 7151 | for(i=0; i<nCell; i++){ |
| 7152 | int pc = get2byte(&data[cellStart+i*2]); |
danielk1977 | daca543 | 2008-08-25 11:57:16 +0000 | [diff] [blame] | 7153 | u16 size = 1024; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7154 | int j; |
danielk1977 | daca543 | 2008-08-25 11:57:16 +0000 | [diff] [blame] | 7155 | if( pc<=usableSize ){ |
| 7156 | size = cellSizePtr(pPage, &data[pc]); |
| 7157 | } |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 7158 | if( (pc+size-1)>=usableSize || pc<0 ){ |
| 7159 | checkAppendMsg(pCheck, 0, |
| 7160 | "Corruption detected in cell %d on page %d",i,iPage,0); |
| 7161 | }else{ |
| 7162 | for(j=pc+size-1; j>=pc; j--) hit[j]++; |
| 7163 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7164 | } |
| 7165 | for(cnt=0, i=get2byte(&data[hdr+1]); i>0 && i<usableSize && cnt<10000; |
| 7166 | cnt++){ |
| 7167 | int size = get2byte(&data[i+2]); |
| 7168 | int j; |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 7169 | if( (i+size-1)>=usableSize || i<0 ){ |
| 7170 | checkAppendMsg(pCheck, 0, |
| 7171 | "Corruption detected in cell %d on page %d",i,iPage,0); |
| 7172 | }else{ |
| 7173 | for(j=i+size-1; j>=i; j--) hit[j]++; |
| 7174 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7175 | i = get2byte(&data[i]); |
| 7176 | } |
| 7177 | for(i=cnt=0; i<usableSize; i++){ |
| 7178 | if( hit[i]==0 ){ |
| 7179 | cnt++; |
| 7180 | }else if( hit[i]>1 ){ |
| 7181 | checkAppendMsg(pCheck, 0, |
| 7182 | "Multiple uses for byte %d of page %d", i, iPage); |
| 7183 | break; |
| 7184 | } |
| 7185 | } |
| 7186 | if( cnt!=data[hdr+7] ){ |
| 7187 | checkAppendMsg(pCheck, 0, |
| 7188 | "Fragmented space is %d byte reported as %d on page %d", |
| 7189 | cnt, data[hdr+7], iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7190 | } |
| 7191 | } |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 7192 | check_page_abort: |
| 7193 | if (hit) sqlite3PageFree(hit); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 7194 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7195 | releasePage(pPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 7196 | return depth+1; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7197 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 7198 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7199 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 7200 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7201 | /* |
| 7202 | ** This routine does a complete check of the given BTree file. aRoot[] is |
| 7203 | ** an array of pages numbers were each page number is the root page of |
| 7204 | ** a table. nRoot is the number of entries in aRoot. |
| 7205 | ** |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 7206 | ** Write the number of error seen in *pnErr. Except for some memory |
drh | e43ba70 | 2008-12-05 22:40:08 +0000 | [diff] [blame] | 7207 | ** allocation errors, an error message held in memory obtained from |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 7208 | ** 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] | 7209 | ** returned. If a memory allocation error occurs, NULL is returned. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7210 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 7211 | char *sqlite3BtreeIntegrityCheck( |
| 7212 | Btree *p, /* The btree to be checked */ |
| 7213 | int *aRoot, /* An array of root pages numbers for individual trees */ |
| 7214 | int nRoot, /* Number of entries in aRoot[] */ |
| 7215 | int mxErr, /* Stop reporting errors after this many */ |
| 7216 | int *pnErr /* Write number of errors seen to this variable */ |
| 7217 | ){ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 7218 | Pgno i; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7219 | int nRef; |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 7220 | IntegrityCk sCheck; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7221 | BtShared *pBt = p->pBt; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 7222 | char zErr[100]; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7223 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7224 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7225 | pBt->db = p->db; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7226 | nRef = sqlite3PagerRefcount(pBt->pPager); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7227 | if( lockBtreeWithRetry(p)!=SQLITE_OK ){ |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 7228 | *pnErr = 1; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7229 | sqlite3BtreeLeave(p); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 7230 | return sqlite3DbStrDup(0, "cannot acquire a read lock on the database"); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 7231 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7232 | sCheck.pBt = pBt; |
| 7233 | sCheck.pPager = pBt->pPager; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 7234 | sCheck.nPage = pagerPagecount(sCheck.pBt); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 7235 | sCheck.mxErr = mxErr; |
| 7236 | sCheck.nErr = 0; |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 7237 | sCheck.mallocFailed = 0; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 7238 | *pnErr = 0; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 7239 | if( sCheck.nPage==0 ){ |
| 7240 | unlockBtreeIfUnused(pBt); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7241 | sqlite3BtreeLeave(p); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 7242 | return 0; |
| 7243 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 7244 | sCheck.anRef = sqlite3Malloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) ); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 7245 | if( !sCheck.anRef ){ |
| 7246 | unlockBtreeIfUnused(pBt); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 7247 | *pnErr = 1; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7248 | sqlite3BtreeLeave(p); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 7249 | return 0; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 7250 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 7251 | for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; } |
drh | 42cac6d | 2004-11-20 20:31:11 +0000 | [diff] [blame] | 7252 | i = PENDING_BYTE_PAGE(pBt); |
drh | 1f59571 | 2004-06-15 01:40:29 +0000 | [diff] [blame] | 7253 | if( i<=sCheck.nPage ){ |
| 7254 | sCheck.anRef[i] = 1; |
| 7255 | } |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 7256 | sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), 20000); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7257 | |
| 7258 | /* Check the integrity of the freelist |
| 7259 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 7260 | checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), |
| 7261 | get4byte(&pBt->pPage1->aData[36]), "Main freelist: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7262 | |
| 7263 | /* Check all the tables. |
| 7264 | */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 7265 | for(i=0; (int)i<nRoot && sCheck.mxErr; i++){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 7266 | if( aRoot[i]==0 ) continue; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 7267 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 7268 | if( pBt->autoVacuum && aRoot[i]>1 ){ |
| 7269 | checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0); |
| 7270 | } |
| 7271 | #endif |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 7272 | checkTreePage(&sCheck, aRoot[i], "List of tree roots: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7273 | } |
| 7274 | |
| 7275 | /* Make sure every page in the file is referenced |
| 7276 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 7277 | for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7278 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7279 | if( sCheck.anRef[i]==0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7280 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7281 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7282 | #else |
| 7283 | /* If the database supports auto-vacuum, make sure no tables contain |
| 7284 | ** references to pointer-map pages. |
| 7285 | */ |
| 7286 | if( sCheck.anRef[i]==0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 7287 | (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7288 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
| 7289 | } |
| 7290 | if( sCheck.anRef[i]!=0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 7291 | (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7292 | checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i); |
| 7293 | } |
| 7294 | #endif |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7295 | } |
| 7296 | |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 7297 | /* Make sure this analysis did not leave any unref() pages. |
| 7298 | ** This is an internal consistency check; an integrity check |
| 7299 | ** of the integrity check. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7300 | */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 7301 | unlockBtreeIfUnused(pBt); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 7302 | if( NEVER(nRef != sqlite3PagerRefcount(pBt->pPager)) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7303 | checkAppendMsg(&sCheck, 0, |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7304 | "Outstanding page count goes from %d to %d during this analysis", |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7305 | nRef, sqlite3PagerRefcount(pBt->pPager) |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7306 | ); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7307 | } |
| 7308 | |
| 7309 | /* Clean up and report errors. |
| 7310 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7311 | sqlite3BtreeLeave(p); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 7312 | sqlite3_free(sCheck.anRef); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 7313 | if( sCheck.mallocFailed ){ |
| 7314 | sqlite3StrAccumReset(&sCheck.errMsg); |
| 7315 | *pnErr = sCheck.nErr+1; |
| 7316 | return 0; |
| 7317 | } |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 7318 | *pnErr = sCheck.nErr; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 7319 | if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg); |
| 7320 | return sqlite3StrAccumFinish(&sCheck.errMsg); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7321 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 7322 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 7323 | |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 7324 | /* |
| 7325 | ** Return the full pathname of the underlying database file. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 7326 | ** |
| 7327 | ** The pager filename is invariant as long as the pager is |
| 7328 | ** open so it is safe to access without the BtShared mutex. |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 7329 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7330 | const char *sqlite3BtreeGetFilename(Btree *p){ |
| 7331 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7332 | return sqlite3PagerFilename(p->pBt->pPager); |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 7333 | } |
| 7334 | |
| 7335 | /* |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 7336 | ** Return the pathname of the journal file for this database. The return |
| 7337 | ** value of this routine is the same regardless of whether the journal file |
| 7338 | ** has been created or not. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 7339 | ** |
| 7340 | ** The pager journal filename is invariant as long as the pager is |
| 7341 | ** open so it is safe to access without the BtShared mutex. |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 7342 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7343 | const char *sqlite3BtreeGetJournalname(Btree *p){ |
| 7344 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7345 | return sqlite3PagerJournalname(p->pBt->pPager); |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 7346 | } |
| 7347 | |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 7348 | /* |
| 7349 | ** Return non-zero if a transaction is active. |
| 7350 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7351 | int sqlite3BtreeIsInTrans(Btree *p){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7352 | assert( p==0 || sqlite3_mutex_held(p->db->mutex) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7353 | return (p && (p->inTrans==TRANS_WRITE)); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 7354 | } |
| 7355 | |
| 7356 | /* |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 7357 | ** Return non-zero if a read (or write) transaction is active. |
| 7358 | */ |
| 7359 | int sqlite3BtreeIsInReadTrans(Btree *p){ |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 7360 | assert( p ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7361 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 7362 | return p->inTrans!=TRANS_NONE; |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 7363 | } |
| 7364 | |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 7365 | int sqlite3BtreeIsInBackup(Btree *p){ |
| 7366 | assert( p ); |
| 7367 | assert( sqlite3_mutex_held(p->db->mutex) ); |
| 7368 | return p->nBackup!=0; |
| 7369 | } |
| 7370 | |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 7371 | /* |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7372 | ** This function returns a pointer to a blob of memory associated with |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 7373 | ** 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] | 7374 | ** purposes (for example, to store a high-level schema associated with |
| 7375 | ** the shared-btree). The btree layer manages reference counting issues. |
| 7376 | ** |
| 7377 | ** The first time this is called on a shared-btree, nBytes bytes of memory |
| 7378 | ** are allocated, zeroed, and returned to the caller. For each subsequent |
| 7379 | ** call the nBytes parameter is ignored and a pointer to the same blob |
| 7380 | ** of memory returned. |
| 7381 | ** |
danielk1977 | 171bfed | 2008-06-23 09:50:50 +0000 | [diff] [blame] | 7382 | ** If the nBytes parameter is 0 and the blob of memory has not yet been |
| 7383 | ** allocated, a null pointer is returned. If the blob has already been |
| 7384 | ** allocated, it is returned as normal. |
| 7385 | ** |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7386 | ** Just before the shared-btree is closed, the function passed as the |
| 7387 | ** xFree argument when the memory allocation was made is invoked on the |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 7388 | ** blob of allocated memory. This function should not call sqlite3_free() |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7389 | ** on the memory, the btree layer does that. |
| 7390 | */ |
| 7391 | void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){ |
| 7392 | BtShared *pBt = p->pBt; |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7393 | sqlite3BtreeEnter(p); |
danielk1977 | 171bfed | 2008-06-23 09:50:50 +0000 | [diff] [blame] | 7394 | if( !pBt->pSchema && nBytes ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 7395 | pBt->pSchema = sqlite3MallocZero(nBytes); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7396 | pBt->xFreeSchema = xFree; |
| 7397 | } |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7398 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7399 | return pBt->pSchema; |
| 7400 | } |
| 7401 | |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 7402 | /* |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 7403 | ** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared |
| 7404 | ** btree as the argument handle holds an exclusive lock on the |
| 7405 | ** sqlite_master table. Otherwise SQLITE_OK. |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 7406 | */ |
| 7407 | int sqlite3BtreeSchemaLocked(Btree *p){ |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7408 | int rc; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7409 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7410 | sqlite3BtreeEnter(p); |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 7411 | rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK); |
| 7412 | assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7413 | sqlite3BtreeLeave(p); |
| 7414 | return rc; |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 7415 | } |
| 7416 | |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 7417 | |
| 7418 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 7419 | /* |
| 7420 | ** Obtain a lock on the table whose root page is iTab. The |
| 7421 | ** lock is a write lock if isWritelock is true or a read lock |
| 7422 | ** if it is false. |
| 7423 | */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7424 | int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){ |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 7425 | int rc = SQLITE_OK; |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 7426 | if( p->sharable ){ |
| 7427 | u8 lockType = READ_LOCK + isWriteLock; |
| 7428 | assert( READ_LOCK+1==WRITE_LOCK ); |
| 7429 | assert( isWriteLock==0 || isWriteLock==1 ); |
| 7430 | sqlite3BtreeEnter(p); |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 7431 | rc = querySharedCacheTableLock(p, iTab, lockType); |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 7432 | if( rc==SQLITE_OK ){ |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 7433 | rc = setSharedCacheTableLock(p, iTab, lockType); |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 7434 | } |
| 7435 | sqlite3BtreeLeave(p); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7436 | } |
| 7437 | return rc; |
| 7438 | } |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 7439 | #endif |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 7440 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7441 | #ifndef SQLITE_OMIT_INCRBLOB |
| 7442 | /* |
| 7443 | ** Argument pCsr must be a cursor opened for writing on an |
| 7444 | ** INTKEY table currently pointing at a valid table entry. |
| 7445 | ** This function modifies the data stored as part of that entry. |
| 7446 | ** Only the data content may only be modified, it is not possible |
| 7447 | ** to change the length of the data stored. |
| 7448 | */ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7449 | int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){ |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 7450 | int rc; |
| 7451 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7452 | assert( cursorHoldsMutex(pCsr) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7453 | assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) ); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7454 | assert(pCsr->isIncrblobHandle); |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 7455 | |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 7456 | restoreCursorPosition(pCsr); |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 7457 | assert( pCsr->eState!=CURSOR_REQUIRESEEK ); |
| 7458 | if( pCsr->eState!=CURSOR_VALID ){ |
| 7459 | return SQLITE_ABORT; |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7460 | } |
| 7461 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7462 | /* Check some preconditions: |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7463 | ** (a) the cursor is open for writing, |
| 7464 | ** (b) there is no read-lock on the table being modified and |
| 7465 | ** (c) the cursor points at a valid row of an intKey table. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7466 | */ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7467 | if( !pCsr->wrFlag ){ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7468 | return SQLITE_READONLY; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7469 | } |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 7470 | assert( !pCsr->pBt->readOnly |
| 7471 | && pCsr->pBt->inTransaction==TRANS_WRITE ); |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 7472 | rc = checkForReadConflicts(pCsr->pBtree, pCsr->pgnoRoot, pCsr, 0); |
| 7473 | if( rc!=SQLITE_OK ){ |
| 7474 | /* The table pCur points to has a read lock */ |
| 7475 | assert( rc==SQLITE_LOCKED_SHAREDCACHE ); |
| 7476 | return rc; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7477 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 7478 | if( pCsr->eState==CURSOR_INVALID || !pCsr->apPage[pCsr->iPage]->intKey ){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7479 | return SQLITE_ERROR; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7480 | } |
| 7481 | |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 7482 | return accessPayload(pCsr, offset, amt, (unsigned char *)z, 0, 1); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7483 | } |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7484 | |
| 7485 | /* |
| 7486 | ** 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] | 7487 | ** overflow list for the current row. This is used by cursors opened |
| 7488 | ** for incremental blob IO only. |
| 7489 | ** |
| 7490 | ** This function sets a flag only. The actual page location cache |
| 7491 | ** (stored in BtCursor.aOverflow[]) is allocated and used by function |
| 7492 | ** accessPayload() (the worker function for sqlite3BtreeData() and |
| 7493 | ** sqlite3BtreePutData()). |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7494 | */ |
| 7495 | void sqlite3BtreeCacheOverflow(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7496 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7497 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7498 | assert(!pCur->isIncrblobHandle); |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7499 | assert(!pCur->aOverflow); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7500 | pCur->isIncrblobHandle = 1; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7501 | } |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7502 | #endif |