drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 2 | ** 2004 April 6 |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 12 | ** This file implements a external (disk-based) database using BTrees. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 13 | ** See the header comment on "btreeInt.h" for additional information. |
| 14 | ** Including a description of file format and an overview of operation. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 15 | */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 16 | #include "btreeInt.h" |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 17 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 18 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 19 | ** The header string that appears at the beginning of every |
| 20 | ** SQLite database. |
drh | 556b2a2 | 2005-06-14 16:04:05 +0000 | [diff] [blame] | 21 | */ |
drh | 556b2a2 | 2005-06-14 16:04:05 +0000 | [diff] [blame] | 22 | static const char zMagicHeader[] = SQLITE_FILE_HEADER; |
drh | 08ed44e | 2001-04-29 23:32:55 +0000 | [diff] [blame] | 23 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 24 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 25 | ** Set this global variable to 1 to enable tracing using the TRACE |
| 26 | ** macro. |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 27 | */ |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 28 | #if 0 |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 29 | int sqlite3BtreeTrace=1; /* True to enable tracing */ |
drh | e8f52c5 | 2008-07-12 14:52:20 +0000 | [diff] [blame] | 30 | # define TRACE(X) if(sqlite3BtreeTrace){printf X;fflush(stdout);} |
| 31 | #else |
| 32 | # define TRACE(X) |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 33 | #endif |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 34 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 35 | |
| 36 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 37 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 38 | /* |
danielk1977 | 502b4e0 | 2008-09-02 14:07:24 +0000 | [diff] [blame] | 39 | ** A list of BtShared objects that are eligible for participation |
| 40 | ** in shared cache. This variable has file scope during normal builds, |
| 41 | ** but the test harness needs to access it so we make it global for |
| 42 | ** test builds. |
drh | 7555d8e | 2009-03-20 13:15:30 +0000 | [diff] [blame] | 43 | ** |
| 44 | ** Access to this variable is protected by SQLITE_MUTEX_STATIC_MASTER. |
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 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 68 | |
| 69 | #ifdef SQLITE_OMIT_SHARED_CACHE |
| 70 | /* |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 71 | ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(), |
| 72 | ** and clearAllSharedCacheTableLocks() |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 73 | ** manipulate entries in the BtShared.pLock linked list used to store |
| 74 | ** shared-cache table level locks. If the library is compiled with the |
| 75 | ** shared-cache feature disabled, then there is only ever one user |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 76 | ** of each BtShared structure and so this locking is not necessary. |
| 77 | ** So define the lock related functions as no-ops. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 78 | */ |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 79 | #define querySharedCacheTableLock(a,b,c) SQLITE_OK |
| 80 | #define setSharedCacheTableLock(a,b,c) SQLITE_OK |
| 81 | #define clearAllSharedCacheTableLocks(a) |
danielk1977 | 94b3073 | 2009-07-02 17:21:57 +0000 | [diff] [blame] | 82 | #define downgradeAllSharedCacheTableLocks(a) |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 83 | #define hasSharedCacheTableLock(a,b,c,d) 1 |
| 84 | #define hasReadConflicts(a, b) 0 |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 85 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 86 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 87 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 88 | |
| 89 | #ifdef SQLITE_DEBUG |
| 90 | /* |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 91 | **** This function is only used as part of an assert() statement. *** |
| 92 | ** |
| 93 | ** Check to see if pBtree holds the required locks to read or write to the |
| 94 | ** table with root page iRoot. Return 1 if it does and 0 if not. |
| 95 | ** |
| 96 | ** For example, when writing to a table with root-page iRoot via |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 97 | ** Btree connection pBtree: |
| 98 | ** |
| 99 | ** assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) ); |
| 100 | ** |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 101 | ** When writing to an index that resides in a sharable database, the |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 102 | ** caller should have first obtained a lock specifying the root page of |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 103 | ** the corresponding table. This makes things a bit more complicated, |
| 104 | ** as this module treats each table as a separate structure. To determine |
| 105 | ** the table corresponding to the index being written, this |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 106 | ** function has to search through the database schema. |
| 107 | ** |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 108 | ** Instead of a lock on the table/index rooted at page iRoot, the caller may |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 109 | ** hold a write-lock on the schema table (root page 1). This is also |
| 110 | ** acceptable. |
| 111 | */ |
| 112 | static int hasSharedCacheTableLock( |
| 113 | Btree *pBtree, /* Handle that must hold lock */ |
| 114 | Pgno iRoot, /* Root page of b-tree */ |
| 115 | int isIndex, /* True if iRoot is the root of an index b-tree */ |
| 116 | int eLockType /* Required lock type (READ_LOCK or WRITE_LOCK) */ |
| 117 | ){ |
| 118 | Schema *pSchema = (Schema *)pBtree->pBt->pSchema; |
| 119 | Pgno iTab = 0; |
| 120 | BtLock *pLock; |
| 121 | |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 122 | /* If this database is not shareable, or if the client is reading |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 123 | ** and has the read-uncommitted flag set, then no lock is required. |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 124 | ** Return true immediately. |
| 125 | */ |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 126 | if( (pBtree->sharable==0) |
| 127 | || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommitted)) |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 128 | ){ |
| 129 | return 1; |
| 130 | } |
| 131 | |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 132 | /* If the client is reading or writing an index and the schema is |
| 133 | ** not loaded, then it is too difficult to actually check to see if |
| 134 | ** the correct locks are held. So do not bother - just return true. |
| 135 | ** This case does not come up very often anyhow. |
| 136 | */ |
| 137 | if( isIndex && (!pSchema || (pSchema->flags&DB_SchemaLoaded)==0) ){ |
| 138 | return 1; |
| 139 | } |
| 140 | |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 141 | /* Figure out the root-page that the lock should be held on. For table |
| 142 | ** b-trees, this is just the root page of the b-tree being read or |
| 143 | ** written. For index b-trees, it is the root page of the associated |
| 144 | ** table. */ |
| 145 | if( isIndex ){ |
| 146 | HashElem *p; |
| 147 | for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){ |
| 148 | Index *pIdx = (Index *)sqliteHashData(p); |
shane | 5eff7cf | 2009-08-10 03:57:58 +0000 | [diff] [blame] | 149 | if( pIdx->tnum==(int)iRoot ){ |
| 150 | iTab = pIdx->pTable->tnum; |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | }else{ |
| 154 | iTab = iRoot; |
| 155 | } |
| 156 | |
| 157 | /* Search for the required lock. Either a write-lock on root-page iTab, a |
| 158 | ** write-lock on the schema table, or (if the client is reading) a |
| 159 | ** read-lock on iTab will suffice. Return 1 if any of these are found. */ |
| 160 | for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){ |
| 161 | if( pLock->pBtree==pBtree |
| 162 | && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1)) |
| 163 | && pLock->eLock>=eLockType |
| 164 | ){ |
| 165 | return 1; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /* Failed to find the required lock. */ |
| 170 | return 0; |
| 171 | } |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 172 | #endif /* SQLITE_DEBUG */ |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 173 | |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 174 | #ifdef SQLITE_DEBUG |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 175 | /* |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 176 | **** This function may be used as part of assert() statements only. **** |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 177 | ** |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 178 | ** Return true if it would be illegal for pBtree to write into the |
| 179 | ** table or index rooted at iRoot because other shared connections are |
| 180 | ** simultaneously reading that same table or index. |
| 181 | ** |
| 182 | ** It is illegal for pBtree to write if some other Btree object that |
| 183 | ** shares the same BtShared object is currently reading or writing |
| 184 | ** the iRoot table. Except, if the other Btree object has the |
| 185 | ** read-uncommitted flag set, then it is OK for the other object to |
| 186 | ** have a read cursor. |
| 187 | ** |
| 188 | ** For example, before writing to any part of the table or index |
| 189 | ** rooted at page iRoot, one should call: |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 190 | ** |
| 191 | ** assert( !hasReadConflicts(pBtree, iRoot) ); |
| 192 | */ |
| 193 | static int hasReadConflicts(Btree *pBtree, Pgno iRoot){ |
| 194 | BtCursor *p; |
| 195 | for(p=pBtree->pBt->pCursor; p; p=p->pNext){ |
| 196 | if( p->pgnoRoot==iRoot |
| 197 | && p->pBtree!=pBtree |
| 198 | && 0==(p->pBtree->db->flags & SQLITE_ReadUncommitted) |
| 199 | ){ |
| 200 | return 1; |
| 201 | } |
| 202 | } |
| 203 | return 0; |
| 204 | } |
| 205 | #endif /* #ifdef SQLITE_DEBUG */ |
| 206 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 207 | /* |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 208 | ** Query to see if Btree handle p may obtain a lock of type eLock |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 209 | ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 210 | ** SQLITE_OK if the lock may be obtained (by calling |
| 211 | ** setSharedCacheTableLock()), or SQLITE_LOCKED if not. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 212 | */ |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 213 | static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 214 | BtShared *pBt = p->pBt; |
| 215 | BtLock *pIter; |
| 216 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 217 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 218 | assert( eLock==READ_LOCK || eLock==WRITE_LOCK ); |
| 219 | assert( p->db!=0 ); |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 220 | assert( !(p->db->flags&SQLITE_ReadUncommitted)||eLock==WRITE_LOCK||iTab==1 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 221 | |
danielk1977 | 5b413d7 | 2009-04-01 09:41:54 +0000 | [diff] [blame] | 222 | /* If requesting a write-lock, then the Btree must have an open write |
| 223 | ** transaction on this file. And, obviously, for this to be so there |
| 224 | ** must be an open write transaction on the file itself. |
| 225 | */ |
| 226 | assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) ); |
| 227 | assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE ); |
| 228 | |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 229 | /* This routine is a no-op if the shared-cache is not enabled */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 230 | if( !p->sharable ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 231 | return SQLITE_OK; |
| 232 | } |
| 233 | |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 234 | /* If some other connection is holding an exclusive lock, the |
| 235 | ** requested lock may not be obtained. |
| 236 | */ |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 237 | if( pBt->pWriter!=p && pBt->isExclusive ){ |
| 238 | sqlite3ConnectionBlocked(p->db, pBt->pWriter->db); |
| 239 | return SQLITE_LOCKED_SHAREDCACHE; |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 240 | } |
| 241 | |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 242 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 243 | /* The condition (pIter->eLock!=eLock) in the following if(...) |
| 244 | ** statement is a simplification of: |
| 245 | ** |
| 246 | ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK) |
| 247 | ** |
| 248 | ** since we know that if eLock==WRITE_LOCK, then no other connection |
| 249 | ** may hold a WRITE_LOCK on any table in this file (since there can |
| 250 | ** only be a single writer). |
| 251 | */ |
| 252 | assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK ); |
| 253 | assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK); |
| 254 | if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){ |
| 255 | sqlite3ConnectionBlocked(p->db, pIter->pBtree->db); |
| 256 | if( eLock==WRITE_LOCK ){ |
| 257 | assert( p==pBt->pWriter ); |
| 258 | pBt->isPending = 1; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 259 | } |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 260 | return SQLITE_LOCKED_SHAREDCACHE; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | return SQLITE_OK; |
| 264 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 265 | #endif /* !SQLITE_OMIT_SHARED_CACHE */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 266 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 267 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 268 | /* |
| 269 | ** Add a lock on the table with root-page iTable to the shared-btree used |
| 270 | ** by Btree handle p. Parameter eLock must be either READ_LOCK or |
| 271 | ** WRITE_LOCK. |
| 272 | ** |
danielk1977 | 9d10486 | 2009-07-09 08:27:14 +0000 | [diff] [blame] | 273 | ** This function assumes the following: |
| 274 | ** |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 275 | ** (a) The specified Btree object p is connected to a sharable |
| 276 | ** database (one with the BtShared.sharable flag set), and |
danielk1977 | 9d10486 | 2009-07-09 08:27:14 +0000 | [diff] [blame] | 277 | ** |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 278 | ** (b) No other Btree objects hold a lock that conflicts |
danielk1977 | 9d10486 | 2009-07-09 08:27:14 +0000 | [diff] [blame] | 279 | ** with the requested lock (i.e. querySharedCacheTableLock() has |
| 280 | ** already been called and returned SQLITE_OK). |
| 281 | ** |
| 282 | ** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM |
| 283 | ** is returned if a malloc attempt fails. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 284 | */ |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 285 | static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 286 | BtShared *pBt = p->pBt; |
| 287 | BtLock *pLock = 0; |
| 288 | BtLock *pIter; |
| 289 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 290 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 291 | assert( eLock==READ_LOCK || eLock==WRITE_LOCK ); |
| 292 | assert( p->db!=0 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 293 | |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 294 | /* A connection with the read-uncommitted flag set will never try to |
| 295 | ** obtain a read-lock using this function. The only read-lock obtained |
| 296 | ** by a connection in read-uncommitted mode is on the sqlite_master |
| 297 | ** table, and that lock is obtained in BtreeBeginTrans(). */ |
| 298 | assert( 0==(p->db->flags&SQLITE_ReadUncommitted) || eLock==WRITE_LOCK ); |
| 299 | |
danielk1977 | 9d10486 | 2009-07-09 08:27:14 +0000 | [diff] [blame] | 300 | /* This function should only be called on a sharable b-tree after it |
| 301 | ** has been determined that no other b-tree holds a conflicting lock. */ |
| 302 | assert( p->sharable ); |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 303 | assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 304 | |
| 305 | /* First search the list for an existing lock on this table. */ |
| 306 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 307 | if( pIter->iTable==iTable && pIter->pBtree==p ){ |
| 308 | pLock = pIter; |
| 309 | break; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | /* If the above search did not find a BtLock struct associating Btree p |
| 314 | ** with table iTable, allocate one and link it into the list. |
| 315 | */ |
| 316 | if( !pLock ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 317 | pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock)); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 318 | if( !pLock ){ |
| 319 | return SQLITE_NOMEM; |
| 320 | } |
| 321 | pLock->iTable = iTable; |
| 322 | pLock->pBtree = p; |
| 323 | pLock->pNext = pBt->pLock; |
| 324 | pBt->pLock = pLock; |
| 325 | } |
| 326 | |
| 327 | /* Set the BtLock.eLock variable to the maximum of the current lock |
| 328 | ** and the requested lock. This means if a write-lock was already held |
| 329 | ** and a read-lock requested, we don't incorrectly downgrade the lock. |
| 330 | */ |
| 331 | assert( WRITE_LOCK>READ_LOCK ); |
danielk1977 | 5118b91 | 2005-12-30 16:31:53 +0000 | [diff] [blame] | 332 | if( eLock>pLock->eLock ){ |
| 333 | pLock->eLock = eLock; |
| 334 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 335 | |
| 336 | return SQLITE_OK; |
| 337 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 338 | #endif /* !SQLITE_OMIT_SHARED_CACHE */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 339 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 340 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 341 | /* |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 342 | ** Release all the table locks (locks obtained via calls to |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 343 | ** the setSharedCacheTableLock() procedure) held by Btree object p. |
danielk1977 | fa542f1 | 2009-04-02 18:28:08 +0000 | [diff] [blame] | 344 | ** |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 345 | ** This function assumes that Btree p has an open read or write |
danielk1977 | fa542f1 | 2009-04-02 18:28:08 +0000 | [diff] [blame] | 346 | ** transaction. If it does not, then the BtShared.isPending variable |
| 347 | ** may be incorrectly cleared. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 348 | */ |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 349 | static void clearAllSharedCacheTableLocks(Btree *p){ |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 350 | BtShared *pBt = p->pBt; |
| 351 | BtLock **ppIter = &pBt->pLock; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 352 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 353 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 354 | assert( p->sharable || 0==*ppIter ); |
danielk1977 | fa542f1 | 2009-04-02 18:28:08 +0000 | [diff] [blame] | 355 | assert( p->inTrans>0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 356 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 357 | while( *ppIter ){ |
| 358 | BtLock *pLock = *ppIter; |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 359 | assert( pBt->isExclusive==0 || pBt->pWriter==pLock->pBtree ); |
danielk1977 | fa542f1 | 2009-04-02 18:28:08 +0000 | [diff] [blame] | 360 | assert( pLock->pBtree->inTrans>=pLock->eLock ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 361 | if( pLock->pBtree==p ){ |
| 362 | *ppIter = pLock->pNext; |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 363 | assert( pLock->iTable!=1 || pLock==&p->lock ); |
| 364 | if( pLock->iTable!=1 ){ |
| 365 | sqlite3_free(pLock); |
| 366 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 367 | }else{ |
| 368 | ppIter = &pLock->pNext; |
| 369 | } |
| 370 | } |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 371 | |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 372 | assert( pBt->isPending==0 || pBt->pWriter ); |
| 373 | if( pBt->pWriter==p ){ |
| 374 | pBt->pWriter = 0; |
| 375 | pBt->isExclusive = 0; |
| 376 | pBt->isPending = 0; |
| 377 | }else if( pBt->nTransaction==2 ){ |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 378 | /* This function is called when Btree p is concluding its |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 379 | ** transaction. If there currently exists a writer, and p is not |
| 380 | ** that writer, then the number of locks held by connections other |
| 381 | ** than the writer must be about to drop to zero. In this case |
| 382 | ** set the isPending flag to 0. |
| 383 | ** |
| 384 | ** If there is not currently a writer, then BtShared.isPending must |
| 385 | ** be zero already. So this next line is harmless in that case. |
| 386 | */ |
| 387 | pBt->isPending = 0; |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 388 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 389 | } |
danielk1977 | 94b3073 | 2009-07-02 17:21:57 +0000 | [diff] [blame] | 390 | |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 391 | /* |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 392 | ** This function changes all write-locks held by Btree p into read-locks. |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 393 | */ |
danielk1977 | 94b3073 | 2009-07-02 17:21:57 +0000 | [diff] [blame] | 394 | static void downgradeAllSharedCacheTableLocks(Btree *p){ |
| 395 | BtShared *pBt = p->pBt; |
| 396 | if( pBt->pWriter==p ){ |
| 397 | BtLock *pLock; |
| 398 | pBt->pWriter = 0; |
| 399 | pBt->isExclusive = 0; |
| 400 | pBt->isPending = 0; |
| 401 | for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){ |
| 402 | assert( pLock->eLock==READ_LOCK || pLock->pBtree==p ); |
| 403 | pLock->eLock = READ_LOCK; |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 408 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| 409 | |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 410 | static void releasePage(MemPage *pPage); /* Forward reference */ |
| 411 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 412 | /* |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 413 | ***** This routine is used inside of assert() only **** |
| 414 | ** |
| 415 | ** Verify that the cursor holds the mutex on its BtShared |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 416 | */ |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 417 | #ifdef SQLITE_DEBUG |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 418 | static int cursorHoldsMutex(BtCursor *p){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 419 | return sqlite3_mutex_held(p->pBt->mutex); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 420 | } |
| 421 | #endif |
| 422 | |
| 423 | |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 424 | #ifndef SQLITE_OMIT_INCRBLOB |
| 425 | /* |
| 426 | ** Invalidate the overflow page-list cache for cursor pCur, if any. |
| 427 | */ |
| 428 | static void invalidateOverflowCache(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 429 | assert( cursorHoldsMutex(pCur) ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 430 | sqlite3_free(pCur->aOverflow); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 431 | pCur->aOverflow = 0; |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | ** Invalidate the overflow page-list cache for all cursors opened |
| 436 | ** on the shared btree structure pBt. |
| 437 | */ |
| 438 | static void invalidateAllOverflowCache(BtShared *pBt){ |
| 439 | BtCursor *p; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 440 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 441 | for(p=pBt->pCursor; p; p=p->pNext){ |
| 442 | invalidateOverflowCache(p); |
| 443 | } |
| 444 | } |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 445 | |
| 446 | /* |
| 447 | ** This function is called before modifying the contents of a table |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 448 | ** to invalidate any incrblob cursors that are open on the |
drh | eeb844a | 2009-08-08 18:01:07 +0000 | [diff] [blame] | 449 | ** row or one of the rows being modified. |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 450 | ** |
| 451 | ** If argument isClearTable is true, then the entire contents of the |
| 452 | ** table is about to be deleted. In this case invalidate all incrblob |
| 453 | ** cursors open on any row within the table with root-page pgnoRoot. |
| 454 | ** |
| 455 | ** Otherwise, if argument isClearTable is false, then the row with |
| 456 | ** rowid iRow is being replaced or deleted. In this case invalidate |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 457 | ** only those incrblob cursors open on that specific row. |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 458 | */ |
| 459 | static void invalidateIncrblobCursors( |
| 460 | Btree *pBtree, /* The database file to check */ |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 461 | i64 iRow, /* The rowid that might be changing */ |
| 462 | int isClearTable /* True if all rows are being deleted */ |
| 463 | ){ |
| 464 | BtCursor *p; |
| 465 | BtShared *pBt = pBtree->pBt; |
| 466 | assert( sqlite3BtreeHoldsMutex(pBtree) ); |
| 467 | for(p=pBt->pCursor; p; p=p->pNext){ |
| 468 | if( p->isIncrblobHandle && (isClearTable || p->info.nKey==iRow) ){ |
| 469 | p->eState = CURSOR_INVALID; |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 474 | #else |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 475 | /* Stub functions when INCRBLOB is omitted */ |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 476 | #define invalidateOverflowCache(x) |
| 477 | #define invalidateAllOverflowCache(x) |
drh | eeb844a | 2009-08-08 18:01:07 +0000 | [diff] [blame] | 478 | #define invalidateIncrblobCursors(x,y,z) |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 479 | #endif /* SQLITE_OMIT_INCRBLOB */ |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 480 | |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 481 | /* |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 482 | ** Set bit pgno of the BtShared.pHasContent bitvec. This is called |
| 483 | ** when a page that previously contained data becomes a free-list leaf |
| 484 | ** page. |
| 485 | ** |
| 486 | ** The BtShared.pHasContent bitvec exists to work around an obscure |
| 487 | ** bug caused by the interaction of two useful IO optimizations surrounding |
| 488 | ** free-list leaf pages: |
| 489 | ** |
| 490 | ** 1) When all data is deleted from a page and the page becomes |
| 491 | ** a free-list leaf page, the page is not written to the database |
| 492 | ** (as free-list leaf pages contain no meaningful data). Sometimes |
| 493 | ** such a page is not even journalled (as it will not be modified, |
| 494 | ** why bother journalling it?). |
| 495 | ** |
| 496 | ** 2) When a free-list leaf page is reused, its content is not read |
| 497 | ** from the database or written to the journal file (why should it |
| 498 | ** be, if it is not at all meaningful?). |
| 499 | ** |
| 500 | ** By themselves, these optimizations work fine and provide a handy |
| 501 | ** performance boost to bulk delete or insert operations. However, if |
| 502 | ** a page is moved to the free-list and then reused within the same |
| 503 | ** transaction, a problem comes up. If the page is not journalled when |
| 504 | ** it is moved to the free-list and it is also not journalled when it |
| 505 | ** is extracted from the free-list and reused, then the original data |
| 506 | ** may be lost. In the event of a rollback, it may not be possible |
| 507 | ** to restore the database to its original configuration. |
| 508 | ** |
| 509 | ** The solution is the BtShared.pHasContent bitvec. Whenever a page is |
| 510 | ** moved to become a free-list leaf page, the corresponding bit is |
| 511 | ** set in the bitvec. Whenever a leaf page is extracted from the free-list, |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 512 | ** optimization 2 above is omitted if the corresponding bit is already |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 513 | ** set in BtShared.pHasContent. The contents of the bitvec are cleared |
| 514 | ** at the end of every transaction. |
| 515 | */ |
| 516 | static int btreeSetHasContent(BtShared *pBt, Pgno pgno){ |
| 517 | int rc = SQLITE_OK; |
| 518 | if( !pBt->pHasContent ){ |
drh | 4c301aa | 2009-07-15 17:25:45 +0000 | [diff] [blame] | 519 | int nPage = 100; |
| 520 | sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 521 | /* If sqlite3PagerPagecount() fails there is no harm because the |
| 522 | ** nPage variable is unchanged from its default value of 100 */ |
| 523 | pBt->pHasContent = sqlite3BitvecCreate((u32)nPage); |
| 524 | if( !pBt->pHasContent ){ |
| 525 | rc = SQLITE_NOMEM; |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){ |
| 529 | rc = sqlite3BitvecSet(pBt->pHasContent, pgno); |
| 530 | } |
| 531 | return rc; |
| 532 | } |
| 533 | |
| 534 | /* |
| 535 | ** Query the BtShared.pHasContent vector. |
| 536 | ** |
| 537 | ** This function is called when a free-list leaf page is removed from the |
| 538 | ** free-list for reuse. It returns false if it is safe to retrieve the |
| 539 | ** page from the pager layer with the 'no-content' flag set. True otherwise. |
| 540 | */ |
| 541 | static int btreeGetHasContent(BtShared *pBt, Pgno pgno){ |
| 542 | Bitvec *p = pBt->pHasContent; |
| 543 | return (p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTest(p, pgno))); |
| 544 | } |
| 545 | |
| 546 | /* |
| 547 | ** Clear (destroy) the BtShared.pHasContent bitvec. This should be |
| 548 | ** invoked at the conclusion of each write-transaction. |
| 549 | */ |
| 550 | static void btreeClearHasContent(BtShared *pBt){ |
| 551 | sqlite3BitvecDestroy(pBt->pHasContent); |
| 552 | pBt->pHasContent = 0; |
| 553 | } |
| 554 | |
| 555 | /* |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 556 | ** Save the current cursor position in the variables BtCursor.nKey |
| 557 | ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK. |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 558 | ** |
| 559 | ** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID) |
| 560 | ** prior to calling this routine. |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 561 | */ |
| 562 | static int saveCursorPosition(BtCursor *pCur){ |
| 563 | int rc; |
| 564 | |
| 565 | assert( CURSOR_VALID==pCur->eState ); |
| 566 | assert( 0==pCur->pKey ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 567 | assert( cursorHoldsMutex(pCur) ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 568 | |
| 569 | rc = sqlite3BtreeKeySize(pCur, &pCur->nKey); |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 570 | assert( rc==SQLITE_OK ); /* KeySize() cannot fail */ |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 571 | |
| 572 | /* If this is an intKey table, then the above call to BtreeKeySize() |
| 573 | ** stores the integer key in pCur->nKey. In this case this value is |
| 574 | ** all that is required. Otherwise, if pCur is not open on an intKey |
| 575 | ** table, then malloc space for and store the pCur->nKey bytes of key |
| 576 | ** data. |
| 577 | */ |
drh | 4c301aa | 2009-07-15 17:25:45 +0000 | [diff] [blame] | 578 | if( 0==pCur->apPage[0]->intKey ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 579 | void *pKey = sqlite3Malloc( (int)pCur->nKey ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 580 | if( pKey ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 581 | rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 582 | if( rc==SQLITE_OK ){ |
| 583 | pCur->pKey = pKey; |
| 584 | }else{ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 585 | sqlite3_free(pKey); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 586 | } |
| 587 | }else{ |
| 588 | rc = SQLITE_NOMEM; |
| 589 | } |
| 590 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 591 | assert( !pCur->apPage[0]->intKey || !pCur->pKey ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 592 | |
| 593 | if( rc==SQLITE_OK ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 594 | int i; |
| 595 | for(i=0; i<=pCur->iPage; i++){ |
| 596 | releasePage(pCur->apPage[i]); |
| 597 | pCur->apPage[i] = 0; |
| 598 | } |
| 599 | pCur->iPage = -1; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 600 | pCur->eState = CURSOR_REQUIRESEEK; |
| 601 | } |
| 602 | |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 603 | invalidateOverflowCache(pCur); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 604 | return rc; |
| 605 | } |
| 606 | |
| 607 | /* |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 608 | ** Save the positions of all cursors (except pExcept) that are open on |
| 609 | ** the table with root-page iRoot. Usually, this is called just before cursor |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 610 | ** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()). |
| 611 | */ |
| 612 | static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){ |
| 613 | BtCursor *p; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 614 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 615 | assert( pExcept==0 || pExcept->pBt==pBt ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 616 | for(p=pBt->pCursor; p; p=p->pNext){ |
| 617 | if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) && |
| 618 | p->eState==CURSOR_VALID ){ |
| 619 | int rc = saveCursorPosition(p); |
| 620 | if( SQLITE_OK!=rc ){ |
| 621 | return rc; |
| 622 | } |
| 623 | } |
| 624 | } |
| 625 | return SQLITE_OK; |
| 626 | } |
| 627 | |
| 628 | /* |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 629 | ** Clear the current cursor position. |
| 630 | */ |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 631 | void sqlite3BtreeClearCursor(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 632 | assert( cursorHoldsMutex(pCur) ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 633 | sqlite3_free(pCur->pKey); |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 634 | pCur->pKey = 0; |
| 635 | pCur->eState = CURSOR_INVALID; |
| 636 | } |
| 637 | |
| 638 | /* |
danielk1977 | 3509a65 | 2009-07-06 18:56:13 +0000 | [diff] [blame] | 639 | ** In this version of BtreeMoveto, pKey is a packed index record |
| 640 | ** such as is generated by the OP_MakeRecord opcode. Unpack the |
| 641 | ** record and then call BtreeMovetoUnpacked() to do the work. |
| 642 | */ |
| 643 | static int btreeMoveto( |
| 644 | BtCursor *pCur, /* Cursor open on the btree to be searched */ |
| 645 | const void *pKey, /* Packed key if the btree is an index */ |
| 646 | i64 nKey, /* Integer key for tables. Size of pKey for indices */ |
| 647 | int bias, /* Bias search to the high end */ |
| 648 | int *pRes /* Write search results here */ |
| 649 | ){ |
| 650 | int rc; /* Status code */ |
| 651 | UnpackedRecord *pIdxKey; /* Unpacked index key */ |
| 652 | char aSpace[150]; /* Temp space for pIdxKey - to avoid a malloc */ |
| 653 | |
| 654 | if( pKey ){ |
| 655 | assert( nKey==(i64)(int)nKey ); |
| 656 | pIdxKey = sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, |
| 657 | aSpace, sizeof(aSpace)); |
| 658 | if( pIdxKey==0 ) return SQLITE_NOMEM; |
| 659 | }else{ |
| 660 | pIdxKey = 0; |
| 661 | } |
| 662 | rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes); |
| 663 | if( pKey ){ |
| 664 | sqlite3VdbeDeleteUnpackedRecord(pIdxKey); |
| 665 | } |
| 666 | return rc; |
| 667 | } |
| 668 | |
| 669 | /* |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 670 | ** Restore the cursor to the position it was in (or as close to as possible) |
| 671 | ** when saveCursorPosition() was called. Note that this call deletes the |
| 672 | ** saved position info stored by saveCursorPosition(), so there can be |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 673 | ** at most one effective restoreCursorPosition() call after each |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 674 | ** saveCursorPosition(). |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 675 | */ |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 676 | static int btreeRestoreCursorPosition(BtCursor *pCur){ |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 677 | int rc; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 678 | assert( cursorHoldsMutex(pCur) ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 679 | assert( pCur->eState>=CURSOR_REQUIRESEEK ); |
| 680 | if( pCur->eState==CURSOR_FAULT ){ |
drh | 4c301aa | 2009-07-15 17:25:45 +0000 | [diff] [blame] | 681 | return pCur->skipNext; |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 682 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 683 | pCur->eState = CURSOR_INVALID; |
drh | 4c301aa | 2009-07-15 17:25:45 +0000 | [diff] [blame] | 684 | rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skipNext); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 685 | if( rc==SQLITE_OK ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 686 | sqlite3_free(pCur->pKey); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 687 | pCur->pKey = 0; |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 688 | assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 689 | } |
| 690 | return rc; |
| 691 | } |
| 692 | |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 693 | #define restoreCursorPosition(p) \ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 694 | (p->eState>=CURSOR_REQUIRESEEK ? \ |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 695 | btreeRestoreCursorPosition(p) : \ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 696 | SQLITE_OK) |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 697 | |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 698 | /* |
| 699 | ** Determine whether or not a cursor has moved from the position it |
drh | dfe88ec | 2008-11-03 20:55:06 +0000 | [diff] [blame] | 700 | ** was last placed at. Cursors can move when the row they are pointing |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 701 | ** at is deleted out from under them. |
| 702 | ** |
| 703 | ** This routine returns an error code if something goes wrong. The |
| 704 | ** integer *pHasMoved is set to one if the cursor has moved and 0 if not. |
| 705 | */ |
| 706 | int sqlite3BtreeCursorHasMoved(BtCursor *pCur, int *pHasMoved){ |
| 707 | int rc; |
| 708 | |
| 709 | rc = restoreCursorPosition(pCur); |
| 710 | if( rc ){ |
| 711 | *pHasMoved = 1; |
| 712 | return rc; |
| 713 | } |
drh | 4c301aa | 2009-07-15 17:25:45 +0000 | [diff] [blame] | 714 | if( pCur->eState!=CURSOR_VALID || pCur->skipNext!=0 ){ |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 715 | *pHasMoved = 1; |
| 716 | }else{ |
| 717 | *pHasMoved = 0; |
| 718 | } |
| 719 | return SQLITE_OK; |
| 720 | } |
| 721 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 722 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 723 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 724 | ** Given a page number of a regular database page, return the page |
| 725 | ** number for the pointer-map page that contains the entry for the |
| 726 | ** input page number. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 727 | */ |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 728 | static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 729 | int nPagesPerMapPage; |
| 730 | Pgno iPtrMap, ret; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 731 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 732 | nPagesPerMapPage = (pBt->usableSize/5)+1; |
| 733 | iPtrMap = (pgno-2)/nPagesPerMapPage; |
| 734 | ret = (iPtrMap*nPagesPerMapPage) + 2; |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 735 | if( ret==PENDING_BYTE_PAGE(pBt) ){ |
| 736 | ret++; |
| 737 | } |
| 738 | return ret; |
| 739 | } |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 740 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 741 | /* |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 742 | ** Write an entry into the pointer map. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 743 | ** |
| 744 | ** This routine updates the pointer map entry for page number 'key' |
| 745 | ** so that it maps to type 'eType' and parent page number 'pgno'. |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 746 | ** |
| 747 | ** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is |
| 748 | ** a no-op. If an error occurs, the appropriate error code is written |
| 749 | ** into *pRC. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 750 | */ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 751 | static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 752 | DbPage *pDbPage; /* The pointer map page */ |
| 753 | u8 *pPtrmap; /* The pointer map data */ |
| 754 | Pgno iPtrmap; /* The pointer map page number */ |
| 755 | int offset; /* Offset in pointer map page */ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 756 | int rc; /* Return code from subfunctions */ |
| 757 | |
| 758 | if( *pRC ) return; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 759 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 760 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 761 | /* The master-journal page number must never be used as a pointer map page */ |
| 762 | assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) ); |
| 763 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 764 | assert( pBt->autoVacuum ); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 765 | if( key==0 ){ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 766 | *pRC = SQLITE_CORRUPT_BKPT; |
| 767 | return; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 768 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 769 | iPtrmap = PTRMAP_PAGENO(pBt, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 770 | rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 771 | if( rc!=SQLITE_OK ){ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 772 | *pRC = rc; |
| 773 | return; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 774 | } |
danielk1977 | 8c666b1 | 2008-07-18 09:34:57 +0000 | [diff] [blame] | 775 | offset = PTRMAP_PTROFFSET(iPtrmap, key); |
drh | acfc72b | 2009-06-05 18:44:15 +0000 | [diff] [blame] | 776 | if( offset<0 ){ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 777 | *pRC = SQLITE_CORRUPT_BKPT; |
drh | 4925a55 | 2009-07-07 11:39:58 +0000 | [diff] [blame] | 778 | goto ptrmap_exit; |
drh | acfc72b | 2009-06-05 18:44:15 +0000 | [diff] [blame] | 779 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 780 | pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 781 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 782 | if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){ |
| 783 | TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent)); |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 784 | *pRC= rc = sqlite3PagerWrite(pDbPage); |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 785 | if( rc==SQLITE_OK ){ |
| 786 | pPtrmap[offset] = eType; |
| 787 | put4byte(&pPtrmap[offset+1], parent); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 788 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 789 | } |
| 790 | |
drh | 4925a55 | 2009-07-07 11:39:58 +0000 | [diff] [blame] | 791 | ptrmap_exit: |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 792 | sqlite3PagerUnref(pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | /* |
| 796 | ** Read an entry from the pointer map. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 797 | ** |
| 798 | ** This routine retrieves the pointer map entry for page 'key', writing |
| 799 | ** the type and parent page number to *pEType and *pPgno respectively. |
| 800 | ** An error code is returned if something goes wrong, otherwise SQLITE_OK. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 801 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 802 | static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 803 | DbPage *pDbPage; /* The pointer map page */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 804 | int iPtrmap; /* Pointer map page index */ |
| 805 | u8 *pPtrmap; /* Pointer map page data */ |
| 806 | int offset; /* Offset of entry in pointer map */ |
| 807 | int rc; |
| 808 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 809 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 810 | |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 811 | iPtrmap = PTRMAP_PAGENO(pBt, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 812 | rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 813 | if( rc!=0 ){ |
| 814 | return rc; |
| 815 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 816 | pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 817 | |
danielk1977 | 8c666b1 | 2008-07-18 09:34:57 +0000 | [diff] [blame] | 818 | offset = PTRMAP_PTROFFSET(iPtrmap, key); |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 819 | assert( pEType!=0 ); |
| 820 | *pEType = pPtrmap[offset]; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 821 | if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 822 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 823 | sqlite3PagerUnref(pDbPage); |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 824 | if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 825 | return SQLITE_OK; |
| 826 | } |
| 827 | |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 828 | #else /* if defined SQLITE_OMIT_AUTOVACUUM */ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 829 | #define ptrmapPut(w,x,y,z,rc) |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 830 | #define ptrmapGet(w,x,y,z) SQLITE_OK |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 831 | #define ptrmapPutOvflPtr(x, y, rc) |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 832 | #endif |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 833 | |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 834 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 835 | ** Given a btree page and a cell index (0 means the first cell on |
| 836 | ** the page, 1 means the second cell, and so forth) return a pointer |
| 837 | ** to the cell content. |
| 838 | ** |
| 839 | ** This routine works only for pages that do not contain overflow cells. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 840 | */ |
drh | 1688c86 | 2008-07-18 02:44:17 +0000 | [diff] [blame] | 841 | #define findCell(P,I) \ |
| 842 | ((P)->aData + ((P)->maskPage & get2byte(&(P)->aData[(P)->cellOffset+2*(I)]))) |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 843 | |
| 844 | /* |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 845 | ** This a more complex version of findCell() that works for |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 846 | ** pages that do contain overflow cells. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 847 | */ |
| 848 | static u8 *findOverflowCell(MemPage *pPage, int iCell){ |
| 849 | int i; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 850 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 851 | for(i=pPage->nOverflow-1; i>=0; i--){ |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 852 | int k; |
| 853 | struct _OvflCell *pOvfl; |
| 854 | pOvfl = &pPage->aOvfl[i]; |
| 855 | k = pOvfl->idx; |
| 856 | if( k<=iCell ){ |
| 857 | if( k==iCell ){ |
| 858 | return pOvfl->pCell; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 859 | } |
| 860 | iCell--; |
| 861 | } |
| 862 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 863 | return findCell(pPage, iCell); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | /* |
| 867 | ** Parse a cell content block and fill in the CellInfo structure. There |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 868 | ** are two versions of this function. btreeParseCell() takes a |
| 869 | ** cell index as the second argument and btreeParseCellPtr() |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 870 | ** takes a pointer to the body of the cell as its second argument. |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 871 | ** |
| 872 | ** Within this file, the parseCell() macro can be called instead of |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 873 | ** btreeParseCellPtr(). Using some compilers, this will be faster. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 874 | */ |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 875 | static void btreeParseCellPtr( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 876 | MemPage *pPage, /* Page containing the cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 877 | u8 *pCell, /* Pointer to the cell text. */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 878 | CellInfo *pInfo /* Fill in this structure */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 879 | ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 880 | u16 n; /* Number bytes in cell content header */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 881 | u32 nPayload; /* Number of bytes of cell payload */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 882 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 883 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 884 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 885 | pInfo->pCell = pCell; |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 886 | assert( pPage->leaf==0 || pPage->leaf==1 ); |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 887 | n = pPage->childPtrSize; |
| 888 | assert( n==4-4*pPage->leaf ); |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 889 | if( pPage->intKey ){ |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 890 | if( pPage->hasData ){ |
| 891 | n += getVarint32(&pCell[n], nPayload); |
| 892 | }else{ |
| 893 | nPayload = 0; |
| 894 | } |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 895 | n += getVarint(&pCell[n], (u64*)&pInfo->nKey); |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 896 | pInfo->nData = nPayload; |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 897 | }else{ |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 898 | pInfo->nData = 0; |
| 899 | n += getVarint32(&pCell[n], nPayload); |
| 900 | pInfo->nKey = nPayload; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 901 | } |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 902 | pInfo->nPayload = nPayload; |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 903 | pInfo->nHeader = n; |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 904 | testcase( nPayload==pPage->maxLocal ); |
| 905 | testcase( nPayload==pPage->maxLocal+1 ); |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 906 | if( likely(nPayload<=pPage->maxLocal) ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 907 | /* This is the (easy) common case where the entire payload fits |
| 908 | ** on the local page. No overflow is required. |
| 909 | */ |
| 910 | int nSize; /* Total size of cell content in bytes */ |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 911 | nSize = nPayload + n; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 912 | pInfo->nLocal = (u16)nPayload; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 913 | pInfo->iOverflow = 0; |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 914 | if( (nSize & ~3)==0 ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 915 | nSize = 4; /* Minimum cell size is 4 */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 916 | } |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 917 | pInfo->nSize = (u16)nSize; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 918 | }else{ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 919 | /* If the payload will not fit completely on the local page, we have |
| 920 | ** to decide how much to store locally and how much to spill onto |
| 921 | ** overflow pages. The strategy is to minimize the amount of unused |
| 922 | ** space on overflow pages while keeping the amount of local storage |
| 923 | ** in between minLocal and maxLocal. |
| 924 | ** |
| 925 | ** Warning: changing the way overflow payload is distributed in any |
| 926 | ** way will result in an incompatible file format. |
| 927 | */ |
| 928 | int minLocal; /* Minimum amount of payload held locally */ |
| 929 | int maxLocal; /* Maximum amount of payload held locally */ |
| 930 | int surplus; /* Overflow payload available for local storage */ |
| 931 | |
| 932 | minLocal = pPage->minLocal; |
| 933 | maxLocal = pPage->maxLocal; |
| 934 | surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4); |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 935 | testcase( surplus==maxLocal ); |
| 936 | testcase( surplus==maxLocal+1 ); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 937 | if( surplus <= maxLocal ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 938 | pInfo->nLocal = (u16)surplus; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 939 | }else{ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 940 | pInfo->nLocal = (u16)minLocal; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 941 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 942 | pInfo->iOverflow = (u16)(pInfo->nLocal + n); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 943 | pInfo->nSize = pInfo->iOverflow + 4; |
| 944 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 945 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 946 | #define parseCell(pPage, iCell, pInfo) \ |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 947 | btreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo)) |
| 948 | static void btreeParseCell( |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 949 | MemPage *pPage, /* Page containing the cell */ |
| 950 | int iCell, /* The cell index. First cell is 0 */ |
| 951 | CellInfo *pInfo /* Fill in this structure */ |
| 952 | ){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 953 | parseCell(pPage, iCell, pInfo); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 954 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 955 | |
| 956 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 957 | ** Compute the total number of bytes that a Cell needs in the cell |
| 958 | ** data area of the btree-page. The return number includes the cell |
| 959 | ** data header and the local payload, but not any overflow page or |
| 960 | ** the space used by the cell pointer. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 961 | */ |
danielk1977 | ae5558b | 2009-04-29 11:31:47 +0000 | [diff] [blame] | 962 | static u16 cellSizePtr(MemPage *pPage, u8 *pCell){ |
| 963 | u8 *pIter = &pCell[pPage->childPtrSize]; |
| 964 | u32 nSize; |
| 965 | |
| 966 | #ifdef SQLITE_DEBUG |
| 967 | /* The value returned by this function should always be the same as |
| 968 | ** the (CellInfo.nSize) value found by doing a full parse of the |
| 969 | ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of |
| 970 | ** this function verifies that this invariant is not violated. */ |
| 971 | CellInfo debuginfo; |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 972 | btreeParseCellPtr(pPage, pCell, &debuginfo); |
danielk1977 | ae5558b | 2009-04-29 11:31:47 +0000 | [diff] [blame] | 973 | #endif |
| 974 | |
| 975 | if( pPage->intKey ){ |
| 976 | u8 *pEnd; |
| 977 | if( pPage->hasData ){ |
| 978 | pIter += getVarint32(pIter, nSize); |
| 979 | }else{ |
| 980 | nSize = 0; |
| 981 | } |
| 982 | |
| 983 | /* pIter now points at the 64-bit integer key value, a variable length |
| 984 | ** integer. The following block moves pIter to point at the first byte |
| 985 | ** past the end of the key value. */ |
| 986 | pEnd = &pIter[9]; |
| 987 | while( (*pIter++)&0x80 && pIter<pEnd ); |
| 988 | }else{ |
| 989 | pIter += getVarint32(pIter, nSize); |
| 990 | } |
| 991 | |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 992 | testcase( nSize==pPage->maxLocal ); |
| 993 | testcase( nSize==pPage->maxLocal+1 ); |
danielk1977 | ae5558b | 2009-04-29 11:31:47 +0000 | [diff] [blame] | 994 | if( nSize>pPage->maxLocal ){ |
| 995 | int minLocal = pPage->minLocal; |
| 996 | nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4); |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 997 | testcase( nSize==pPage->maxLocal ); |
| 998 | testcase( nSize==pPage->maxLocal+1 ); |
danielk1977 | ae5558b | 2009-04-29 11:31:47 +0000 | [diff] [blame] | 999 | if( nSize>pPage->maxLocal ){ |
| 1000 | nSize = minLocal; |
| 1001 | } |
| 1002 | nSize += 4; |
| 1003 | } |
shane | 75ac1de | 2009-06-09 18:58:52 +0000 | [diff] [blame] | 1004 | nSize += (u32)(pIter - pCell); |
danielk1977 | ae5558b | 2009-04-29 11:31:47 +0000 | [diff] [blame] | 1005 | |
| 1006 | /* The minimum size of any cell is 4 bytes. */ |
| 1007 | if( nSize<4 ){ |
| 1008 | nSize = 4; |
| 1009 | } |
| 1010 | |
| 1011 | assert( nSize==debuginfo.nSize ); |
shane | 60a4b53 | 2009-05-06 18:57:09 +0000 | [diff] [blame] | 1012 | return (u16)nSize; |
danielk1977 | ae5558b | 2009-04-29 11:31:47 +0000 | [diff] [blame] | 1013 | } |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 1014 | |
| 1015 | #ifdef SQLITE_DEBUG |
| 1016 | /* This variation on cellSizePtr() is used inside of assert() statements |
| 1017 | ** only. */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 1018 | static u16 cellSize(MemPage *pPage, int iCell){ |
danielk1977 | ae5558b | 2009-04-29 11:31:47 +0000 | [diff] [blame] | 1019 | return cellSizePtr(pPage, findCell(pPage, iCell)); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1020 | } |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1021 | #endif |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1022 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 1023 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1024 | /* |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 1025 | ** If the cell pCell, part of page pPage contains a pointer |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 1026 | ** to an overflow page, insert an entry into the pointer-map |
| 1027 | ** for the overflow page. |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 1028 | */ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 1029 | static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){ |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 1030 | CellInfo info; |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 1031 | if( *pRC ) return; |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 1032 | assert( pCell!=0 ); |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 1033 | btreeParseCellPtr(pPage, pCell, &info); |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 1034 | assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 1035 | if( info.iOverflow ){ |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 1036 | Pgno ovfl = get4byte(&pCell[info.iOverflow]); |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 1037 | ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 1038 | } |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 1039 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 1040 | #endif |
| 1041 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 1042 | |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1043 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1044 | ** Defragment the page given. All Cells are moved to the |
drh | 3a4a2d4 | 2005-11-24 14:24:28 +0000 | [diff] [blame] | 1045 | ** end of the page and all free space is collected into one |
| 1046 | ** big FreeBlk that occurs in between the header and cell |
drh | 31beae9 | 2005-11-24 14:34:36 +0000 | [diff] [blame] | 1047 | ** pointer array and the cell content area. |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 1048 | */ |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 1049 | static int defragmentPage(MemPage *pPage){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1050 | int i; /* Loop counter */ |
| 1051 | int pc; /* Address of a i-th cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1052 | int hdr; /* Offset to the page header */ |
| 1053 | int size; /* Size of a cell */ |
| 1054 | int usableSize; /* Number of usable bytes on a page */ |
| 1055 | int cellOffset; /* Offset to the cell pointer array */ |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 1056 | int cbrk; /* Offset to the cell content area */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1057 | int nCell; /* Number of cells on the page */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1058 | unsigned char *data; /* The page data */ |
| 1059 | unsigned char *temp; /* Temp area for cell content */ |
drh | 1714662 | 2009-07-07 17:38:38 +0000 | [diff] [blame] | 1060 | int iCellFirst; /* First allowable cell index */ |
| 1061 | int iCellLast; /* Last possible cell index */ |
| 1062 | |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1063 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1064 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1065 | assert( pPage->pBt!=0 ); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1066 | assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1067 | assert( pPage->nOverflow==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1068 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 26b7994 | 2007-11-28 16:19:56 +0000 | [diff] [blame] | 1069 | temp = sqlite3PagerTempSpace(pPage->pBt->pPager); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1070 | data = pPage->aData; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1071 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1072 | cellOffset = pPage->cellOffset; |
| 1073 | nCell = pPage->nCell; |
| 1074 | assert( nCell==get2byte(&data[hdr+3]) ); |
| 1075 | usableSize = pPage->pBt->usableSize; |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 1076 | cbrk = get2byte(&data[hdr+5]); |
| 1077 | memcpy(&temp[cbrk], &data[cbrk], usableSize - cbrk); |
| 1078 | cbrk = usableSize; |
drh | 1714662 | 2009-07-07 17:38:38 +0000 | [diff] [blame] | 1079 | iCellFirst = cellOffset + 2*nCell; |
| 1080 | iCellLast = usableSize - 4; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1081 | for(i=0; i<nCell; i++){ |
| 1082 | u8 *pAddr; /* The i-th cell pointer */ |
| 1083 | pAddr = &data[cellOffset + i*2]; |
| 1084 | pc = get2byte(pAddr); |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1085 | testcase( pc==iCellFirst ); |
| 1086 | testcase( pc==iCellLast ); |
drh | 1714662 | 2009-07-07 17:38:38 +0000 | [diff] [blame] | 1087 | #if !defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK) |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 1088 | /* These conditions have already been verified in btreeInitPage() |
drh | 1714662 | 2009-07-07 17:38:38 +0000 | [diff] [blame] | 1089 | ** if SQLITE_ENABLE_OVERSIZE_CELL_CHECK is defined |
| 1090 | */ |
| 1091 | if( pc<iCellFirst || pc>iCellLast ){ |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 1092 | return SQLITE_CORRUPT_BKPT; |
| 1093 | } |
drh | 1714662 | 2009-07-07 17:38:38 +0000 | [diff] [blame] | 1094 | #endif |
| 1095 | assert( pc>=iCellFirst && pc<=iCellLast ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1096 | size = cellSizePtr(pPage, &temp[pc]); |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 1097 | cbrk -= size; |
drh | 1714662 | 2009-07-07 17:38:38 +0000 | [diff] [blame] | 1098 | #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK) |
| 1099 | if( cbrk<iCellFirst ){ |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 1100 | return SQLITE_CORRUPT_BKPT; |
| 1101 | } |
drh | 1714662 | 2009-07-07 17:38:38 +0000 | [diff] [blame] | 1102 | #else |
| 1103 | if( cbrk<iCellFirst || pc+size>usableSize ){ |
| 1104 | return SQLITE_CORRUPT_BKPT; |
| 1105 | } |
| 1106 | #endif |
drh | 7157e1d | 2009-07-09 13:25:32 +0000 | [diff] [blame] | 1107 | assert( cbrk+size<=usableSize && cbrk>=iCellFirst ); |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1108 | testcase( cbrk+size==usableSize ); |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1109 | testcase( pc+size==usableSize ); |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 1110 | memcpy(&data[cbrk], &temp[pc], size); |
| 1111 | put2byte(pAddr, cbrk); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1112 | } |
drh | 1714662 | 2009-07-07 17:38:38 +0000 | [diff] [blame] | 1113 | assert( cbrk>=iCellFirst ); |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 1114 | put2byte(&data[hdr+5], cbrk); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1115 | data[hdr+1] = 0; |
| 1116 | data[hdr+2] = 0; |
| 1117 | data[hdr+7] = 0; |
drh | 1714662 | 2009-07-07 17:38:38 +0000 | [diff] [blame] | 1118 | memset(&data[iCellFirst], 0, cbrk-iCellFirst); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 1119 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 1714662 | 2009-07-07 17:38:38 +0000 | [diff] [blame] | 1120 | if( cbrk-iCellFirst!=pPage->nFree ){ |
danielk1977 | 360e634 | 2008-11-12 08:49:51 +0000 | [diff] [blame] | 1121 | return SQLITE_CORRUPT_BKPT; |
| 1122 | } |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 1123 | return SQLITE_OK; |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1126 | /* |
danielk1977 | 6011a75 | 2009-04-01 16:25:32 +0000 | [diff] [blame] | 1127 | ** Allocate nByte bytes of space from within the B-Tree page passed |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1128 | ** as the first argument. Write into *pIdx the index into pPage->aData[] |
| 1129 | ** of the first byte of allocated space. Return either SQLITE_OK or |
| 1130 | ** an error code (usually SQLITE_CORRUPT). |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1131 | ** |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1132 | ** The caller guarantees that there is sufficient space to make the |
| 1133 | ** allocation. This routine might need to defragment in order to bring |
| 1134 | ** all the space together, however. This routine will avoid using |
| 1135 | ** the first two bytes past the cell pointer area since presumably this |
| 1136 | ** allocation is being made in order to insert a new cell, so we will |
| 1137 | ** also end up needing a new cell pointer. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1138 | */ |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1139 | static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){ |
danielk1977 | 6011a75 | 2009-04-01 16:25:32 +0000 | [diff] [blame] | 1140 | const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */ |
| 1141 | u8 * const data = pPage->aData; /* Local cache of pPage->aData */ |
| 1142 | int nFrag; /* Number of fragmented bytes on pPage */ |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1143 | int top; /* First byte of cell content area */ |
| 1144 | int gap; /* First byte of gap between cell pointers and cell content */ |
| 1145 | int rc; /* Integer return code */ |
drh | 00ce394 | 2009-12-06 03:35:51 +0000 | [diff] [blame] | 1146 | int usableSize; /* Usable size of the page */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1147 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1148 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1149 | assert( pPage->pBt ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1150 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 1151 | assert( nByte>=0 ); /* Minimum cell size is 4 */ |
| 1152 | assert( pPage->nFree>=nByte ); |
| 1153 | assert( pPage->nOverflow==0 ); |
drh | 00ce394 | 2009-12-06 03:35:51 +0000 | [diff] [blame] | 1154 | usableSize = pPage->pBt->usableSize; |
| 1155 | assert( nByte < usableSize-8 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1156 | |
| 1157 | nFrag = data[hdr+7]; |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1158 | assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf ); |
| 1159 | gap = pPage->cellOffset + 2*pPage->nCell; |
| 1160 | top = get2byte(&data[hdr+5]); |
drh | 7157e1d | 2009-07-09 13:25:32 +0000 | [diff] [blame] | 1161 | if( gap>top ) return SQLITE_CORRUPT_BKPT; |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1162 | testcase( gap+2==top ); |
| 1163 | testcase( gap+1==top ); |
| 1164 | testcase( gap==top ); |
| 1165 | |
danielk1977 | 6011a75 | 2009-04-01 16:25:32 +0000 | [diff] [blame] | 1166 | if( nFrag>=60 ){ |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1167 | /* Always defragment highly fragmented pages */ |
| 1168 | rc = defragmentPage(pPage); |
| 1169 | if( rc ) return rc; |
| 1170 | top = get2byte(&data[hdr+5]); |
| 1171 | }else if( gap+2<=top ){ |
danielk1977 | 6011a75 | 2009-04-01 16:25:32 +0000 | [diff] [blame] | 1172 | /* Search the freelist looking for a free slot big enough to satisfy |
| 1173 | ** the request. The allocation is made from the first free slot in |
| 1174 | ** the list that is large enough to accomadate it. |
| 1175 | */ |
| 1176 | int pc, addr; |
| 1177 | for(addr=hdr+1; (pc = get2byte(&data[addr]))>0; addr=pc){ |
drh | 00ce394 | 2009-12-06 03:35:51 +0000 | [diff] [blame] | 1178 | int size; /* Size of the free slot */ |
| 1179 | if( pc>usableSize-4 || pc<addr+4 ){ |
| 1180 | return SQLITE_CORRUPT_BKPT; |
| 1181 | } |
| 1182 | size = get2byte(&data[pc+2]); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1183 | if( size>=nByte ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1184 | int x = size - nByte; |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1185 | testcase( x==4 ); |
| 1186 | testcase( x==3 ); |
danielk1977 | 6011a75 | 2009-04-01 16:25:32 +0000 | [diff] [blame] | 1187 | if( x<4 ){ |
danielk1977 | fad9194 | 2009-04-29 17:49:59 +0000 | [diff] [blame] | 1188 | /* Remove the slot from the free-list. Update the number of |
| 1189 | ** fragmented bytes within the page. */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1190 | memcpy(&data[addr], &data[pc], 2); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1191 | data[hdr+7] = (u8)(nFrag + x); |
drh | 00ce394 | 2009-12-06 03:35:51 +0000 | [diff] [blame] | 1192 | }else if( size+pc > usableSize ){ |
| 1193 | return SQLITE_CORRUPT_BKPT; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1194 | }else{ |
danielk1977 | fad9194 | 2009-04-29 17:49:59 +0000 | [diff] [blame] | 1195 | /* The slot remains on the free-list. Reduce its size to account |
| 1196 | ** for the portion used by the new allocation. */ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1197 | put2byte(&data[pc+2], x); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1198 | } |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1199 | *pIdx = pc + x; |
| 1200 | return SQLITE_OK; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1201 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1202 | } |
| 1203 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1204 | |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1205 | /* Check to make sure there is enough space in the gap to satisfy |
| 1206 | ** the allocation. If not, defragment. |
| 1207 | */ |
| 1208 | testcase( gap+2+nByte==top ); |
| 1209 | if( gap+2+nByte>top ){ |
| 1210 | rc = defragmentPage(pPage); |
| 1211 | if( rc ) return rc; |
| 1212 | top = get2byte(&data[hdr+5]); |
| 1213 | assert( gap+nByte<=top ); |
| 1214 | } |
| 1215 | |
| 1216 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1217 | /* Allocate memory from the gap in between the cell pointer array |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 1218 | ** and the cell content area. The btreeInitPage() call has already |
| 1219 | ** validated the freelist. Given that the freelist is valid, there |
| 1220 | ** is no way that the allocation can extend off the end of the page. |
| 1221 | ** The assert() below verifies the previous sentence. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1222 | */ |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1223 | top -= nByte; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1224 | put2byte(&data[hdr+5], top); |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 1225 | assert( top+nByte <= pPage->pBt->usableSize ); |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1226 | *pIdx = top; |
| 1227 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1231 | ** Return a section of the pPage->aData to the freelist. |
| 1232 | ** The first byte of the new free block is pPage->aDisk[start] |
| 1233 | ** and the size of the block is "size" bytes. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1234 | ** |
| 1235 | ** Most of the effort here is involved in coalesing adjacent |
| 1236 | ** free blocks into a single big free block. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1237 | */ |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 1238 | static int freeSpace(MemPage *pPage, int start, int size){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1239 | int addr, pbegin, hdr; |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1240 | int iLast; /* Largest possible freeblock offset */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1241 | unsigned char *data = pPage->aData; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1242 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1243 | assert( pPage->pBt!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1244 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | c046e3e | 2009-07-15 11:26:44 +0000 | [diff] [blame] | 1245 | assert( start>=pPage->hdrOffset+6+pPage->childPtrSize ); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1246 | assert( (start + size)<=pPage->pBt->usableSize ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1247 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 1248 | assert( size>=0 ); /* Minimum cell size is 4 */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1249 | |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 1250 | if( pPage->pBt->secureDelete ){ |
| 1251 | /* Overwrite deleted information with zeros when the secure_delete |
| 1252 | ** option is enabled */ |
| 1253 | memset(&data[start], 0, size); |
| 1254 | } |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 1255 | |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1256 | /* Add the space back into the linked list of freeblocks. Note that |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 1257 | ** even though the freeblock list was checked by btreeInitPage(), |
| 1258 | ** btreeInitPage() did not detect overlapping cells or |
drh | b908d76 | 2009-07-08 16:54:40 +0000 | [diff] [blame] | 1259 | ** freeblocks that overlapped cells. Nor does it detect when the |
| 1260 | ** cell content area exceeds the value in the page header. If these |
| 1261 | ** situations arise, then subsequent insert operations might corrupt |
| 1262 | ** the freelist. So we do need to check for corruption while scanning |
| 1263 | ** the freelist. |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1264 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1265 | hdr = pPage->hdrOffset; |
| 1266 | addr = hdr + 1; |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1267 | iLast = pPage->pBt->usableSize - 4; |
drh | 35a25da | 2009-07-08 15:14:50 +0000 | [diff] [blame] | 1268 | assert( start<=iLast ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1269 | while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){ |
drh | 35a25da | 2009-07-08 15:14:50 +0000 | [diff] [blame] | 1270 | if( pbegin<addr+4 ){ |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 1271 | return SQLITE_CORRUPT_BKPT; |
| 1272 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1273 | addr = pbegin; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1274 | } |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1275 | if( pbegin>iLast ){ |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 1276 | return SQLITE_CORRUPT_BKPT; |
| 1277 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1278 | assert( pbegin>addr || pbegin==0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1279 | put2byte(&data[addr], start); |
| 1280 | put2byte(&data[start], pbegin); |
| 1281 | put2byte(&data[start+2], size); |
shane | 36840fd | 2009-06-26 16:32:13 +0000 | [diff] [blame] | 1282 | pPage->nFree = pPage->nFree + (u16)size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1283 | |
| 1284 | /* Coalesce adjacent free blocks */ |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1285 | addr = hdr + 1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1286 | while( (pbegin = get2byte(&data[addr]))>0 ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1287 | int pnext, psize, x; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1288 | assert( pbegin>addr ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1289 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1290 | pnext = get2byte(&data[pbegin]); |
| 1291 | psize = get2byte(&data[pbegin+2]); |
| 1292 | if( pbegin + psize + 3 >= pnext && pnext>0 ){ |
| 1293 | int frag = pnext - (pbegin+psize); |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1294 | if( (frag<0) || (frag>(int)data[hdr+7]) ){ |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 1295 | return SQLITE_CORRUPT_BKPT; |
| 1296 | } |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1297 | data[hdr+7] -= (u8)frag; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1298 | x = get2byte(&data[pnext]); |
| 1299 | put2byte(&data[pbegin], x); |
| 1300 | x = pnext + get2byte(&data[pnext+2]) - pbegin; |
| 1301 | put2byte(&data[pbegin+2], x); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1302 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1303 | addr = pbegin; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1304 | } |
| 1305 | } |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1306 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1307 | /* If the cell content area begins with a freeblock, remove it. */ |
| 1308 | if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){ |
| 1309 | int top; |
| 1310 | pbegin = get2byte(&data[hdr+1]); |
| 1311 | memcpy(&data[hdr+1], &data[pbegin], 2); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1312 | top = get2byte(&data[hdr+5]) + get2byte(&data[pbegin+2]); |
| 1313 | put2byte(&data[hdr+5], top); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1314 | } |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 1315 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 1316 | return SQLITE_OK; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1320 | ** Decode the flags byte (the first byte of the header) for a page |
| 1321 | ** and initialize fields of the MemPage structure accordingly. |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1322 | ** |
| 1323 | ** Only the following combinations are supported. Anything different |
| 1324 | ** indicates a corrupt database files: |
| 1325 | ** |
| 1326 | ** PTF_ZERODATA |
| 1327 | ** PTF_ZERODATA | PTF_LEAF |
| 1328 | ** PTF_LEAFDATA | PTF_INTKEY |
| 1329 | ** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1330 | */ |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1331 | static int decodeFlags(MemPage *pPage, int flagByte){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1332 | BtShared *pBt; /* A copy of pPage->pBt */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1333 | |
| 1334 | assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1335 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1336 | pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 ); |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1337 | flagByte &= ~PTF_LEAF; |
| 1338 | pPage->childPtrSize = 4-4*pPage->leaf; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1339 | pBt = pPage->pBt; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1340 | if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){ |
| 1341 | pPage->intKey = 1; |
| 1342 | pPage->hasData = pPage->leaf; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1343 | pPage->maxLocal = pBt->maxLeaf; |
| 1344 | pPage->minLocal = pBt->minLeaf; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1345 | }else if( flagByte==PTF_ZERODATA ){ |
| 1346 | pPage->intKey = 0; |
| 1347 | pPage->hasData = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1348 | pPage->maxLocal = pBt->maxLocal; |
| 1349 | pPage->minLocal = pBt->minLocal; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1350 | }else{ |
| 1351 | return SQLITE_CORRUPT_BKPT; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1352 | } |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1353 | return SQLITE_OK; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1357 | ** Initialize the auxiliary information for a disk block. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1358 | ** |
| 1359 | ** Return SQLITE_OK on success. If we see that the page does |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1360 | ** not contain a well-formed database page, then return |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1361 | ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not |
| 1362 | ** guarantee that the page is well-formed. It only shows that |
| 1363 | ** we failed to detect any corruption. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1364 | */ |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 1365 | static int btreeInitPage(MemPage *pPage){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1366 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1367 | assert( pPage->pBt!=0 ); |
| 1368 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1369 | assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1370 | assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) ); |
| 1371 | assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1372 | |
| 1373 | if( !pPage->isInit ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1374 | u16 pc; /* Address of a freeblock within pPage->aData[] */ |
| 1375 | u8 hdr; /* Offset to beginning of page header */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1376 | u8 *data; /* Equal to pPage->aData */ |
| 1377 | BtShared *pBt; /* The main btree structure */ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1378 | u16 usableSize; /* Amount of usable space on each page */ |
| 1379 | u16 cellOffset; /* Offset from start of page to first cell pointer */ |
| 1380 | u16 nFree; /* Number of unused bytes on the page */ |
| 1381 | u16 top; /* First byte of the cell content area */ |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1382 | int iCellFirst; /* First allowable cell or freeblock offset */ |
| 1383 | int iCellLast; /* Last possible cell or freeblock offset */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1384 | |
| 1385 | pBt = pPage->pBt; |
| 1386 | |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1387 | hdr = pPage->hdrOffset; |
| 1388 | data = pPage->aData; |
| 1389 | if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT; |
| 1390 | assert( pBt->pageSize>=512 && pBt->pageSize<=32768 ); |
| 1391 | pPage->maskPage = pBt->pageSize - 1; |
| 1392 | pPage->nOverflow = 0; |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1393 | usableSize = pBt->usableSize; |
| 1394 | pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf; |
| 1395 | top = get2byte(&data[hdr+5]); |
| 1396 | pPage->nCell = get2byte(&data[hdr+3]); |
| 1397 | if( pPage->nCell>MX_CELL(pBt) ){ |
| 1398 | /* To many cells for a single page. The page must be corrupt */ |
| 1399 | return SQLITE_CORRUPT_BKPT; |
| 1400 | } |
drh | b908d76 | 2009-07-08 16:54:40 +0000 | [diff] [blame] | 1401 | testcase( pPage->nCell==MX_CELL(pBt) ); |
drh | 69e931e | 2009-06-03 21:04:35 +0000 | [diff] [blame] | 1402 | |
shane | 5eff7cf | 2009-08-10 03:57:58 +0000 | [diff] [blame] | 1403 | /* A malformed database page might cause us to read past the end |
drh | 69e931e | 2009-06-03 21:04:35 +0000 | [diff] [blame] | 1404 | ** of page when parsing a cell. |
| 1405 | ** |
| 1406 | ** The following block of code checks early to see if a cell extends |
| 1407 | ** past the end of a page boundary and causes SQLITE_CORRUPT to be |
| 1408 | ** returned if it does. |
| 1409 | */ |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1410 | iCellFirst = cellOffset + 2*pPage->nCell; |
| 1411 | iCellLast = usableSize - 4; |
drh | 3b2a3fa | 2009-06-09 13:42:24 +0000 | [diff] [blame] | 1412 | #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK) |
drh | 69e931e | 2009-06-03 21:04:35 +0000 | [diff] [blame] | 1413 | { |
drh | 69e931e | 2009-06-03 21:04:35 +0000 | [diff] [blame] | 1414 | int i; /* Index into the cell pointer array */ |
| 1415 | int sz; /* Size of a cell */ |
| 1416 | |
drh | 69e931e | 2009-06-03 21:04:35 +0000 | [diff] [blame] | 1417 | if( !pPage->leaf ) iCellLast--; |
| 1418 | for(i=0; i<pPage->nCell; i++){ |
| 1419 | pc = get2byte(&data[cellOffset+i*2]); |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1420 | testcase( pc==iCellFirst ); |
| 1421 | testcase( pc==iCellLast ); |
drh | 69e931e | 2009-06-03 21:04:35 +0000 | [diff] [blame] | 1422 | if( pc<iCellFirst || pc>iCellLast ){ |
| 1423 | return SQLITE_CORRUPT_BKPT; |
| 1424 | } |
| 1425 | sz = cellSizePtr(pPage, &data[pc]); |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1426 | testcase( pc+sz==usableSize ); |
drh | 69e931e | 2009-06-03 21:04:35 +0000 | [diff] [blame] | 1427 | if( pc+sz>usableSize ){ |
| 1428 | return SQLITE_CORRUPT_BKPT; |
| 1429 | } |
| 1430 | } |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1431 | if( !pPage->leaf ) iCellLast++; |
drh | 69e931e | 2009-06-03 21:04:35 +0000 | [diff] [blame] | 1432 | } |
| 1433 | #endif |
| 1434 | |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1435 | /* Compute the total free space on the page */ |
| 1436 | pc = get2byte(&data[hdr+1]); |
danielk1977 | 93c829c | 2009-06-03 17:26:17 +0000 | [diff] [blame] | 1437 | nFree = data[hdr+7] + top; |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1438 | while( pc>0 ){ |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 1439 | u16 next, size; |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1440 | if( pc<iCellFirst || pc>iCellLast ){ |
dan | 4361e79 | 2009-08-14 17:01:22 +0000 | [diff] [blame] | 1441 | /* Start of free block is off the page */ |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1442 | return SQLITE_CORRUPT_BKPT; |
| 1443 | } |
| 1444 | next = get2byte(&data[pc]); |
| 1445 | size = get2byte(&data[pc+2]); |
dan | 4361e79 | 2009-08-14 17:01:22 +0000 | [diff] [blame] | 1446 | if( (next>0 && next<=pc+size+3) || pc+size>usableSize ){ |
| 1447 | /* Free blocks must be in ascending order. And the last byte of |
| 1448 | ** the free-block must lie on the database page. */ |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1449 | return SQLITE_CORRUPT_BKPT; |
| 1450 | } |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 1451 | nFree = nFree + size; |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1452 | pc = next; |
| 1453 | } |
danielk1977 | 93c829c | 2009-06-03 17:26:17 +0000 | [diff] [blame] | 1454 | |
| 1455 | /* At this point, nFree contains the sum of the offset to the start |
| 1456 | ** of the cell-content area plus the number of free bytes within |
| 1457 | ** the cell-content area. If this is greater than the usable-size |
| 1458 | ** of the page, then the page must be corrupted. This check also |
| 1459 | ** serves to verify that the offset to the start of the cell-content |
| 1460 | ** area, according to the page header, lies within the page. |
| 1461 | */ |
| 1462 | if( nFree>usableSize ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1463 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1464 | } |
shane | 5eff7cf | 2009-08-10 03:57:58 +0000 | [diff] [blame] | 1465 | pPage->nFree = (u16)(nFree - iCellFirst); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1466 | pPage->isInit = 1; |
| 1467 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1468 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1472 | ** Set up a raw page so that it looks like a database page holding |
| 1473 | ** no entries. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1474 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1475 | static void zeroPage(MemPage *pPage, int flags){ |
| 1476 | unsigned char *data = pPage->aData; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1477 | BtShared *pBt = pPage->pBt; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1478 | u8 hdr = pPage->hdrOffset; |
| 1479 | u16 first; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1480 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1481 | assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1482 | assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); |
| 1483 | assert( sqlite3PagerGetData(pPage->pDbPage) == data ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1484 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1485 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 1486 | if( pBt->secureDelete ){ |
| 1487 | memset(&data[hdr], 0, pBt->usableSize - hdr); |
| 1488 | } |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 1489 | data[hdr] = (char)flags; |
| 1490 | first = hdr + 8 + 4*((flags&PTF_LEAF)==0 ?1:0); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1491 | memset(&data[hdr+1], 0, 4); |
| 1492 | data[hdr+7] = 0; |
| 1493 | put2byte(&data[hdr+5], pBt->usableSize); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1494 | pPage->nFree = pBt->usableSize - first; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1495 | decodeFlags(pPage, flags); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1496 | pPage->hdrOffset = hdr; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1497 | pPage->cellOffset = first; |
| 1498 | pPage->nOverflow = 0; |
drh | 1688c86 | 2008-07-18 02:44:17 +0000 | [diff] [blame] | 1499 | assert( pBt->pageSize>=512 && pBt->pageSize<=32768 ); |
| 1500 | pPage->maskPage = pBt->pageSize - 1; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1501 | pPage->nCell = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1502 | pPage->isInit = 1; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1505 | |
| 1506 | /* |
| 1507 | ** Convert a DbPage obtained from the pager into a MemPage used by |
| 1508 | ** the btree layer. |
| 1509 | */ |
| 1510 | static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){ |
| 1511 | MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage); |
| 1512 | pPage->aData = sqlite3PagerGetData(pDbPage); |
| 1513 | pPage->pDbPage = pDbPage; |
| 1514 | pPage->pBt = pBt; |
| 1515 | pPage->pgno = pgno; |
| 1516 | pPage->hdrOffset = pPage->pgno==1 ? 100 : 0; |
| 1517 | return pPage; |
| 1518 | } |
| 1519 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1520 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1521 | ** Get a page from the pager. Initialize the MemPage.pBt and |
| 1522 | ** MemPage.aData elements if needed. |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 1523 | ** |
| 1524 | ** If the noContent flag is set, it means that we do not care about |
| 1525 | ** the content of the page at this time. So do not go to the disk |
| 1526 | ** to fetch the content. Just fill in the content with zeros for now. |
| 1527 | ** If in the future we call sqlite3PagerWrite() on this page, that |
| 1528 | ** means we have started to be concerned about content and the disk |
| 1529 | ** read should occur at that point. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1530 | */ |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 1531 | static int btreeGetPage( |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1532 | BtShared *pBt, /* The btree */ |
| 1533 | Pgno pgno, /* Number of the page to fetch */ |
| 1534 | MemPage **ppPage, /* Return the page in this parameter */ |
| 1535 | int noContent /* Do not load page content if true */ |
| 1536 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1537 | int rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1538 | DbPage *pDbPage; |
| 1539 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1540 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 1541 | rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1542 | if( rc ) return rc; |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1543 | *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1544 | return SQLITE_OK; |
| 1545 | } |
| 1546 | |
| 1547 | /* |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 1548 | ** Retrieve a page from the pager cache. If the requested page is not |
| 1549 | ** already in the pager cache return NULL. Initialize the MemPage.pBt and |
| 1550 | ** MemPage.aData elements if needed. |
| 1551 | */ |
| 1552 | static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){ |
| 1553 | DbPage *pDbPage; |
| 1554 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 1555 | pDbPage = sqlite3PagerLookup(pBt->pPager, pgno); |
| 1556 | if( pDbPage ){ |
| 1557 | return btreePageFromDbPage(pDbPage, pgno, pBt); |
| 1558 | } |
| 1559 | return 0; |
| 1560 | } |
| 1561 | |
| 1562 | /* |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 1563 | ** Return the size of the database file in pages. If there is any kind of |
| 1564 | ** error, return ((unsigned int)-1). |
danielk1977 | 67fd7a9 | 2008-09-10 17:53:35 +0000 | [diff] [blame] | 1565 | */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 1566 | static Pgno pagerPagecount(BtShared *pBt){ |
| 1567 | int nPage = -1; |
danielk1977 | 67fd7a9 | 2008-09-10 17:53:35 +0000 | [diff] [blame] | 1568 | int rc; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 1569 | assert( pBt->pPage1 ); |
| 1570 | rc = sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 1571 | assert( rc==SQLITE_OK || nPage==-1 ); |
| 1572 | return (Pgno)nPage; |
danielk1977 | 67fd7a9 | 2008-09-10 17:53:35 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | /* |
danielk1977 | 89bc4bc | 2009-07-21 19:25:24 +0000 | [diff] [blame] | 1576 | ** Get a page from the pager and initialize it. This routine is just a |
| 1577 | ** convenience wrapper around separate calls to btreeGetPage() and |
| 1578 | ** btreeInitPage(). |
| 1579 | ** |
| 1580 | ** If an error occurs, then the value *ppPage is set to is undefined. It |
| 1581 | ** may remain unchanged, or it may be set to an invalid value. |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1582 | */ |
| 1583 | static int getAndInitPage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1584 | BtShared *pBt, /* The database file */ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1585 | Pgno pgno, /* Number of the page to get */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1586 | MemPage **ppPage /* Write the page pointer here */ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1587 | ){ |
| 1588 | int rc; |
danielk1977 | 89bc4bc | 2009-07-21 19:25:24 +0000 | [diff] [blame] | 1589 | TESTONLY( Pgno iLastPg = pagerPagecount(pBt); ) |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1590 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 89bc4bc | 2009-07-21 19:25:24 +0000 | [diff] [blame] | 1591 | |
| 1592 | rc = btreeGetPage(pBt, pgno, ppPage, 0); |
| 1593 | if( rc==SQLITE_OK ){ |
| 1594 | rc = btreeInitPage(*ppPage); |
| 1595 | if( rc!=SQLITE_OK ){ |
| 1596 | releasePage(*ppPage); |
| 1597 | } |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1598 | } |
danielk1977 | 9f580ad | 2008-09-10 14:45:57 +0000 | [diff] [blame] | 1599 | |
danielk1977 | 89bc4bc | 2009-07-21 19:25:24 +0000 | [diff] [blame] | 1600 | /* If the requested page number was either 0 or greater than the page |
| 1601 | ** number of the last page in the database, this function should return |
| 1602 | ** SQLITE_CORRUPT or some other error (i.e. SQLITE_FULL). Check that this |
| 1603 | ** is the case. */ |
| 1604 | assert( (pgno>0 && pgno<=iLastPg) || rc!=SQLITE_OK ); |
| 1605 | testcase( pgno==0 ); |
| 1606 | testcase( pgno==iLastPg ); |
| 1607 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1608 | return rc; |
| 1609 | } |
| 1610 | |
| 1611 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1612 | ** Release a MemPage. This should be called once for each prior |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 1613 | ** call to btreeGetPage. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1614 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1615 | static void releasePage(MemPage *pPage){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1616 | if( pPage ){ |
| 1617 | assert( pPage->aData ); |
| 1618 | assert( pPage->pBt ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1619 | assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); |
| 1620 | assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1621 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1622 | sqlite3PagerUnref(pPage->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1623 | } |
| 1624 | } |
| 1625 | |
| 1626 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1627 | ** During a rollback, when the pager reloads information into the cache |
| 1628 | ** so that the cache is restored to its original state at the start of |
| 1629 | ** the transaction, for each page restored this routine is called. |
| 1630 | ** |
| 1631 | ** This routine needs to reset the extra data section at the end of the |
| 1632 | ** page to agree with the restored data. |
| 1633 | */ |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1634 | static void pageReinit(DbPage *pData){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1635 | MemPage *pPage; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1636 | pPage = (MemPage *)sqlite3PagerGetExtra(pData); |
danielk1977 | d217e6f | 2009-04-01 17:13:51 +0000 | [diff] [blame] | 1637 | assert( sqlite3PagerPageRefcount(pData)>0 ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1638 | if( pPage->isInit ){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1639 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1640 | pPage->isInit = 0; |
danielk1977 | d217e6f | 2009-04-01 17:13:51 +0000 | [diff] [blame] | 1641 | if( sqlite3PagerPageRefcount(pData)>1 ){ |
drh | 5e8d887 | 2009-03-30 17:19:48 +0000 | [diff] [blame] | 1642 | /* pPage might not be a btree page; it might be an overflow page |
| 1643 | ** or ptrmap page or a free page. In those cases, the following |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 1644 | ** call to btreeInitPage() will likely return SQLITE_CORRUPT. |
drh | 5e8d887 | 2009-03-30 17:19:48 +0000 | [diff] [blame] | 1645 | ** But no harm is done by this. And it is very important that |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 1646 | ** btreeInitPage() be called on every btree page so we make |
drh | 5e8d887 | 2009-03-30 17:19:48 +0000 | [diff] [blame] | 1647 | ** the call for every page that comes in for re-initing. */ |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 1648 | btreeInitPage(pPage); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1649 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1650 | } |
| 1651 | } |
| 1652 | |
| 1653 | /* |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1654 | ** Invoke the busy handler for a btree. |
| 1655 | */ |
danielk1977 | 1ceedd3 | 2008-11-19 10:22:33 +0000 | [diff] [blame] | 1656 | static int btreeInvokeBusyHandler(void *pArg){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1657 | BtShared *pBt = (BtShared*)pArg; |
| 1658 | assert( pBt->db ); |
| 1659 | assert( sqlite3_mutex_held(pBt->db->mutex) ); |
| 1660 | return sqlite3InvokeBusyHandler(&pBt->db->busyHandler); |
| 1661 | } |
| 1662 | |
| 1663 | /* |
drh | ad3e010 | 2004-09-03 23:32:18 +0000 | [diff] [blame] | 1664 | ** Open a database file. |
| 1665 | ** |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1666 | ** zFilename is the name of the database file. If zFilename is NULL |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1667 | ** a new database with a random name is created. This randomly named |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1668 | ** database file will be deleted when sqlite3BtreeClose() is called. |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1669 | ** If zFilename is ":memory:" then an in-memory database is created |
| 1670 | ** that is automatically destroyed when it is closed. |
drh | c47fd8e | 2009-04-30 13:30:32 +0000 | [diff] [blame] | 1671 | ** |
| 1672 | ** If the database is already opened in the same database connection |
| 1673 | ** and we are in shared cache mode, then the open will fail with an |
| 1674 | ** SQLITE_CONSTRAINT error. We cannot allow two or more BtShared |
| 1675 | ** objects in the same database connection since doing so will lead |
| 1676 | ** to problems with locking. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1677 | */ |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1678 | int sqlite3BtreeOpen( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1679 | const char *zFilename, /* Name of the file containing the BTree database */ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1680 | sqlite3 *db, /* Associated database handle */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1681 | Btree **ppBtree, /* Pointer to new Btree object written here */ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1682 | int flags, /* Options */ |
| 1683 | int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1684 | ){ |
drh | 7555d8e | 2009-03-20 13:15:30 +0000 | [diff] [blame] | 1685 | sqlite3_vfs *pVfs; /* The VFS to use for this btree */ |
| 1686 | BtShared *pBt = 0; /* Shared part of btree structure */ |
| 1687 | Btree *p; /* Handle to return */ |
| 1688 | sqlite3_mutex *mutexOpen = 0; /* Prevents a race condition. Ticket #3537 */ |
| 1689 | int rc = SQLITE_OK; /* Result code from this function */ |
| 1690 | u8 nReserve; /* Byte of unused space on each page */ |
| 1691 | unsigned char zDbHeader[100]; /* Database header content */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1692 | |
| 1693 | /* Set the variable isMemdb to true for an in-memory database, or |
| 1694 | ** false for a file-based database. This symbol is only required if |
| 1695 | ** either of the shared-data or autovacuum features are compiled |
| 1696 | ** into the library. |
| 1697 | */ |
| 1698 | #if !defined(SQLITE_OMIT_SHARED_CACHE) || !defined(SQLITE_OMIT_AUTOVACUUM) |
| 1699 | #ifdef SQLITE_OMIT_MEMORYDB |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 1700 | const int isMemdb = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1701 | #else |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 1702 | const int isMemdb = zFilename && !strcmp(zFilename, ":memory:"); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1703 | #endif |
| 1704 | #endif |
| 1705 | |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1706 | assert( db!=0 ); |
| 1707 | assert( sqlite3_mutex_held(db->mutex) ); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1708 | |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1709 | pVfs = db->pVfs; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1710 | p = sqlite3MallocZero(sizeof(Btree)); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1711 | if( !p ){ |
| 1712 | return SQLITE_NOMEM; |
| 1713 | } |
| 1714 | p->inTrans = TRANS_NONE; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1715 | p->db = db; |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 1716 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 1717 | p->lock.pBtree = p; |
| 1718 | p->lock.iTable = 1; |
| 1719 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1720 | |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 1721 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1722 | /* |
| 1723 | ** If this Btree is a candidate for shared cache, try to find an |
| 1724 | ** existing BtShared object that we can share with |
| 1725 | */ |
danielk1977 | 20c6cc2 | 2009-04-01 18:03:00 +0000 | [diff] [blame] | 1726 | if( isMemdb==0 && zFilename && zFilename[0] ){ |
drh | f1f1268 | 2009-09-09 14:17:52 +0000 | [diff] [blame] | 1727 | if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 1728 | int nFullPathname = pVfs->mxPathname+1; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1729 | char *zFullPathname = sqlite3Malloc(nFullPathname); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1730 | sqlite3_mutex *mutexShared; |
| 1731 | p->sharable = 1; |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1732 | if( !zFullPathname ){ |
| 1733 | sqlite3_free(p); |
| 1734 | return SQLITE_NOMEM; |
| 1735 | } |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 1736 | sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname); |
drh | 7555d8e | 2009-03-20 13:15:30 +0000 | [diff] [blame] | 1737 | mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN); |
| 1738 | sqlite3_mutex_enter(mutexOpen); |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 1739 | mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1740 | sqlite3_mutex_enter(mutexShared); |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 1741 | for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1742 | assert( pBt->nRef>0 ); |
| 1743 | if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager)) |
| 1744 | && sqlite3PagerVfs(pBt->pPager)==pVfs ){ |
drh | c47fd8e | 2009-04-30 13:30:32 +0000 | [diff] [blame] | 1745 | int iDb; |
| 1746 | for(iDb=db->nDb-1; iDb>=0; iDb--){ |
| 1747 | Btree *pExisting = db->aDb[iDb].pBt; |
| 1748 | if( pExisting && pExisting->pBt==pBt ){ |
| 1749 | sqlite3_mutex_leave(mutexShared); |
| 1750 | sqlite3_mutex_leave(mutexOpen); |
| 1751 | sqlite3_free(zFullPathname); |
| 1752 | sqlite3_free(p); |
| 1753 | return SQLITE_CONSTRAINT; |
| 1754 | } |
| 1755 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1756 | p->pBt = pBt; |
| 1757 | pBt->nRef++; |
| 1758 | break; |
| 1759 | } |
| 1760 | } |
| 1761 | sqlite3_mutex_leave(mutexShared); |
| 1762 | sqlite3_free(zFullPathname); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1763 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1764 | #ifdef SQLITE_DEBUG |
| 1765 | else{ |
| 1766 | /* In debug mode, we mark all persistent databases as sharable |
| 1767 | ** even when they are not. This exercises the locking code and |
| 1768 | ** gives more opportunity for asserts(sqlite3_mutex_held()) |
| 1769 | ** statements to find locking problems. |
| 1770 | */ |
| 1771 | p->sharable = 1; |
| 1772 | } |
| 1773 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1774 | } |
| 1775 | #endif |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1776 | if( pBt==0 ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1777 | /* |
| 1778 | ** The following asserts make sure that structures used by the btree are |
| 1779 | ** the right size. This is to guard against size changes that result |
| 1780 | ** when compiling on a different architecture. |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 1781 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1782 | assert( sizeof(i64)==8 || sizeof(i64)==4 ); |
| 1783 | assert( sizeof(u64)==8 || sizeof(u64)==4 ); |
| 1784 | assert( sizeof(u32)==4 ); |
| 1785 | assert( sizeof(u16)==2 ); |
| 1786 | assert( sizeof(Pgno)==4 ); |
| 1787 | |
| 1788 | pBt = sqlite3MallocZero( sizeof(*pBt) ); |
| 1789 | if( pBt==0 ){ |
| 1790 | rc = SQLITE_NOMEM; |
| 1791 | goto btree_open_out; |
| 1792 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1793 | rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename, |
drh | 4775ecd | 2009-07-24 19:01:19 +0000 | [diff] [blame] | 1794 | EXTRA_SIZE, flags, vfsFlags, pageReinit); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1795 | if( rc==SQLITE_OK ){ |
| 1796 | rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader); |
| 1797 | } |
| 1798 | if( rc!=SQLITE_OK ){ |
| 1799 | goto btree_open_out; |
| 1800 | } |
danielk1977 | 2a50ff0 | 2009-04-10 09:47:06 +0000 | [diff] [blame] | 1801 | pBt->db = db; |
danielk1977 | 1ceedd3 | 2008-11-19 10:22:33 +0000 | [diff] [blame] | 1802 | sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1803 | p->pBt = pBt; |
| 1804 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1805 | pBt->pCursor = 0; |
| 1806 | pBt->pPage1 = 0; |
| 1807 | pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager); |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 1808 | #ifdef SQLITE_SECURE_DELETE |
| 1809 | pBt->secureDelete = 1; |
| 1810 | #endif |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1811 | pBt->pageSize = get2byte(&zDbHeader[16]); |
| 1812 | if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE |
| 1813 | || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1814 | pBt->pageSize = 0; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1815 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1816 | /* If the magic name ":memory:" will create an in-memory database, then |
| 1817 | ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if |
| 1818 | ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if |
| 1819 | ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a |
| 1820 | ** regular file-name. In this case the auto-vacuum applies as per normal. |
| 1821 | */ |
| 1822 | if( zFilename && !isMemdb ){ |
| 1823 | pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0); |
| 1824 | pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0); |
| 1825 | } |
| 1826 | #endif |
| 1827 | nReserve = 0; |
| 1828 | }else{ |
| 1829 | nReserve = zDbHeader[20]; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1830 | pBt->pageSizeFixed = 1; |
| 1831 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1832 | pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0); |
| 1833 | pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0); |
| 1834 | #endif |
| 1835 | } |
drh | fa9601a | 2009-06-18 17:22:39 +0000 | [diff] [blame] | 1836 | rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve); |
drh | c0b6181 | 2009-04-30 01:22:41 +0000 | [diff] [blame] | 1837 | if( rc ) goto btree_open_out; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1838 | pBt->usableSize = pBt->pageSize - nReserve; |
| 1839 | assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1840 | |
| 1841 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
| 1842 | /* Add the new BtShared object to the linked list sharable BtShareds. |
| 1843 | */ |
| 1844 | if( p->sharable ){ |
| 1845 | sqlite3_mutex *mutexShared; |
| 1846 | pBt->nRef = 1; |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 1847 | mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 1848 | if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){ |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 1849 | pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST); |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 1850 | if( pBt->mutex==0 ){ |
| 1851 | rc = SQLITE_NOMEM; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1852 | db->mallocFailed = 0; |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 1853 | goto btree_open_out; |
| 1854 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1855 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1856 | sqlite3_mutex_enter(mutexShared); |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 1857 | pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList); |
| 1858 | GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1859 | sqlite3_mutex_leave(mutexShared); |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1860 | } |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1861 | #endif |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1862 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1863 | |
drh | cfed7bc | 2006-03-13 14:28:05 +0000 | [diff] [blame] | 1864 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1865 | /* If the new Btree uses a sharable pBtShared, then link the new |
| 1866 | ** Btree into the list of all sharable Btrees for the same connection. |
drh | abddb0c | 2007-08-20 13:14:28 +0000 | [diff] [blame] | 1867 | ** The list is kept in ascending order by pBt address. |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 1868 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1869 | if( p->sharable ){ |
| 1870 | int i; |
| 1871 | Btree *pSib; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1872 | for(i=0; i<db->nDb; i++){ |
| 1873 | if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1874 | while( pSib->pPrev ){ pSib = pSib->pPrev; } |
| 1875 | if( p->pBt<pSib->pBt ){ |
| 1876 | p->pNext = pSib; |
| 1877 | p->pPrev = 0; |
| 1878 | pSib->pPrev = p; |
| 1879 | }else{ |
drh | abddb0c | 2007-08-20 13:14:28 +0000 | [diff] [blame] | 1880 | while( pSib->pNext && pSib->pNext->pBt<p->pBt ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1881 | pSib = pSib->pNext; |
| 1882 | } |
| 1883 | p->pNext = pSib->pNext; |
| 1884 | p->pPrev = pSib; |
| 1885 | if( p->pNext ){ |
| 1886 | p->pNext->pPrev = p; |
| 1887 | } |
| 1888 | pSib->pNext = p; |
| 1889 | } |
| 1890 | break; |
| 1891 | } |
| 1892 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1893 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1894 | #endif |
| 1895 | *ppBtree = p; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1896 | |
| 1897 | btree_open_out: |
| 1898 | if( rc!=SQLITE_OK ){ |
| 1899 | if( pBt && pBt->pPager ){ |
| 1900 | sqlite3PagerClose(pBt->pPager); |
| 1901 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1902 | sqlite3_free(pBt); |
| 1903 | sqlite3_free(p); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1904 | *ppBtree = 0; |
| 1905 | } |
drh | 7555d8e | 2009-03-20 13:15:30 +0000 | [diff] [blame] | 1906 | if( mutexOpen ){ |
| 1907 | assert( sqlite3_mutex_held(mutexOpen) ); |
| 1908 | sqlite3_mutex_leave(mutexOpen); |
| 1909 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1910 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1911 | } |
| 1912 | |
| 1913 | /* |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1914 | ** Decrement the BtShared.nRef counter. When it reaches zero, |
| 1915 | ** remove the BtShared structure from the sharing list. Return |
| 1916 | ** true if the BtShared.nRef counter reaches zero and return |
| 1917 | ** false if it is still positive. |
| 1918 | */ |
| 1919 | static int removeFromSharingList(BtShared *pBt){ |
| 1920 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 1921 | sqlite3_mutex *pMaster; |
| 1922 | BtShared *pList; |
| 1923 | int removed = 0; |
| 1924 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1925 | assert( sqlite3_mutex_notheld(pBt->mutex) ); |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 1926 | pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1927 | sqlite3_mutex_enter(pMaster); |
| 1928 | pBt->nRef--; |
| 1929 | if( pBt->nRef<=0 ){ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 1930 | if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){ |
| 1931 | GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1932 | }else{ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 1933 | pList = GLOBAL(BtShared*,sqlite3SharedCacheList); |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 1934 | while( ALWAYS(pList) && pList->pNext!=pBt ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1935 | pList=pList->pNext; |
| 1936 | } |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 1937 | if( ALWAYS(pList) ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1938 | pList->pNext = pBt->pNext; |
| 1939 | } |
| 1940 | } |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 1941 | if( SQLITE_THREADSAFE ){ |
| 1942 | sqlite3_mutex_free(pBt->mutex); |
| 1943 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1944 | removed = 1; |
| 1945 | } |
| 1946 | sqlite3_mutex_leave(pMaster); |
| 1947 | return removed; |
| 1948 | #else |
| 1949 | return 1; |
| 1950 | #endif |
| 1951 | } |
| 1952 | |
| 1953 | /* |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1954 | ** Make sure pBt->pTmpSpace points to an allocation of |
| 1955 | ** MX_CELL_SIZE(pBt) bytes. |
| 1956 | */ |
| 1957 | static void allocateTempSpace(BtShared *pBt){ |
| 1958 | if( !pBt->pTmpSpace ){ |
| 1959 | pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize ); |
| 1960 | } |
| 1961 | } |
| 1962 | |
| 1963 | /* |
| 1964 | ** Free the pBt->pTmpSpace allocation |
| 1965 | */ |
| 1966 | static void freeTempSpace(BtShared *pBt){ |
| 1967 | sqlite3PageFree( pBt->pTmpSpace); |
| 1968 | pBt->pTmpSpace = 0; |
| 1969 | } |
| 1970 | |
| 1971 | /* |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1972 | ** Close an open database and invalidate all cursors. |
| 1973 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1974 | int sqlite3BtreeClose(Btree *p){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1975 | BtShared *pBt = p->pBt; |
| 1976 | BtCursor *pCur; |
| 1977 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1978 | /* Close all cursors opened via this handle. */ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1979 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1980 | sqlite3BtreeEnter(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1981 | pCur = pBt->pCursor; |
| 1982 | while( pCur ){ |
| 1983 | BtCursor *pTmp = pCur; |
| 1984 | pCur = pCur->pNext; |
| 1985 | if( pTmp->pBtree==p ){ |
| 1986 | sqlite3BtreeCloseCursor(pTmp); |
| 1987 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1988 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1989 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 1990 | /* Rollback any active transaction and free the handle structure. |
| 1991 | ** The call to sqlite3BtreeRollback() drops any table-locks held by |
| 1992 | ** this handle. |
| 1993 | */ |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 1994 | sqlite3BtreeRollback(p); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1995 | sqlite3BtreeLeave(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1996 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1997 | /* If there are still other outstanding references to the shared-btree |
| 1998 | ** structure, return now. The remainder of this procedure cleans |
| 1999 | ** up the shared-btree. |
| 2000 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2001 | assert( p->wantToLock==0 && p->locked==0 ); |
| 2002 | if( !p->sharable || removeFromSharingList(pBt) ){ |
| 2003 | /* The pBt is no longer on the sharing list, so we can access |
| 2004 | ** it without having to hold the mutex. |
| 2005 | ** |
| 2006 | ** Clean out and delete the BtShared object. |
| 2007 | */ |
| 2008 | assert( !pBt->pCursor ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2009 | sqlite3PagerClose(pBt->pPager); |
| 2010 | if( pBt->xFreeSchema && pBt->pSchema ){ |
| 2011 | pBt->xFreeSchema(pBt->pSchema); |
| 2012 | } |
| 2013 | sqlite3_free(pBt->pSchema); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 2014 | freeTempSpace(pBt); |
drh | 65bbf29 | 2008-06-19 01:03:17 +0000 | [diff] [blame] | 2015 | sqlite3_free(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2016 | } |
| 2017 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2018 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | cab5ed7 | 2007-08-22 11:41:18 +0000 | [diff] [blame] | 2019 | assert( p->wantToLock==0 ); |
| 2020 | assert( p->locked==0 ); |
| 2021 | if( p->pPrev ) p->pPrev->pNext = p->pNext; |
| 2022 | if( p->pNext ) p->pNext->pPrev = p->pPrev; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2023 | #endif |
| 2024 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2025 | sqlite3_free(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2026 | return SQLITE_OK; |
| 2027 | } |
| 2028 | |
| 2029 | /* |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 2030 | ** Change the limit on the number of pages allowed in the cache. |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 2031 | ** |
| 2032 | ** The maximum number of cache pages is set to the absolute |
| 2033 | ** value of mxPage. If mxPage is negative, the pager will |
| 2034 | ** operate asynchronously - it will not stop to do fsync()s |
| 2035 | ** to insure data is written to the disk surface before |
| 2036 | ** continuing. Transactions still work if synchronous is off, |
| 2037 | ** and the database cannot be corrupted if this program |
| 2038 | ** crashes. But if the operating system crashes or there is |
| 2039 | ** an abrupt power failure when synchronous is off, the database |
| 2040 | ** could be left in an inconsistent and unrecoverable state. |
| 2041 | ** Synchronous is on by default so database corruption is not |
| 2042 | ** normally a worry. |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 2043 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2044 | int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){ |
| 2045 | BtShared *pBt = p->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2046 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2047 | sqlite3BtreeEnter(p); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2048 | sqlite3PagerSetCachesize(pBt->pPager, mxPage); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2049 | sqlite3BtreeLeave(p); |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 2050 | return SQLITE_OK; |
| 2051 | } |
| 2052 | |
| 2053 | /* |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 2054 | ** Change the way data is synced to disk in order to increase or decrease |
| 2055 | ** how well the database resists damage due to OS crashes and power |
| 2056 | ** failures. Level 1 is the same as asynchronous (no syncs() occur and |
| 2057 | ** there is a high probability of damage) Level 2 is the default. There |
| 2058 | ** is a very low but non-zero probability of damage. Level 3 reduces the |
| 2059 | ** probability of damage to near zero but with a write performance reduction. |
| 2060 | */ |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 2061 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
drh | ac530b1 | 2006-02-11 01:25:50 +0000 | [diff] [blame] | 2062 | int sqlite3BtreeSetSafetyLevel(Btree *p, int level, int fullSync){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2063 | BtShared *pBt = p->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2064 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2065 | sqlite3BtreeEnter(p); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2066 | sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2067 | sqlite3BtreeLeave(p); |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 2068 | return SQLITE_OK; |
| 2069 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 2070 | #endif |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 2071 | |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 2072 | /* |
| 2073 | ** Return TRUE if the given btree is set to safety level 1. In other |
| 2074 | ** words, return TRUE if no sync() occurs on the disk files. |
| 2075 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2076 | int sqlite3BtreeSyncDisabled(Btree *p){ |
| 2077 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2078 | int rc; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2079 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2080 | sqlite3BtreeEnter(p); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 2081 | assert( pBt && pBt->pPager ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2082 | rc = sqlite3PagerNosync(pBt->pPager); |
| 2083 | sqlite3BtreeLeave(p); |
| 2084 | return rc; |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 2087 | #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 2088 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2089 | ** Change the default pages size and the number of reserved bytes per page. |
drh | ce4869f | 2009-04-02 20:16:58 +0000 | [diff] [blame] | 2090 | ** Or, if the page size has already been fixed, return SQLITE_READONLY |
| 2091 | ** without changing anything. |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 2092 | ** |
| 2093 | ** The page size must be a power of 2 between 512 and 65536. If the page |
| 2094 | ** size supplied does not meet this constraint then the page size is not |
| 2095 | ** changed. |
| 2096 | ** |
| 2097 | ** Page sizes are constrained to be a power of two so that the region |
| 2098 | ** of the database file used for locking (beginning at PENDING_BYTE, |
| 2099 | ** the first byte past the 1GB boundary, 0x40000000) needs to occur |
| 2100 | ** at the beginning of a page. |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 2101 | ** |
| 2102 | ** If parameter nReserve is less than zero, then the number of reserved |
| 2103 | ** bytes per page is left unchanged. |
drh | ce4869f | 2009-04-02 20:16:58 +0000 | [diff] [blame] | 2104 | ** |
| 2105 | ** If the iFix!=0 then the pageSizeFixed flag is set so that the page size |
| 2106 | ** and autovacuum mode can no longer be changed. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2107 | */ |
drh | ce4869f | 2009-04-02 20:16:58 +0000 | [diff] [blame] | 2108 | int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2109 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2110 | BtShared *pBt = p->pBt; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 2111 | assert( nReserve>=-1 && nReserve<=255 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2112 | sqlite3BtreeEnter(p); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2113 | if( pBt->pageSizeFixed ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2114 | sqlite3BtreeLeave(p); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2115 | return SQLITE_READONLY; |
| 2116 | } |
| 2117 | if( nReserve<0 ){ |
| 2118 | nReserve = pBt->pageSize - pBt->usableSize; |
| 2119 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 2120 | assert( nReserve>=0 && nReserve<=255 ); |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 2121 | if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE && |
| 2122 | ((pageSize-1)&pageSize)==0 ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 2123 | assert( (pageSize & 7)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2124 | assert( !pBt->pPage1 && !pBt->pCursor ); |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 2125 | pBt->pageSize = (u16)pageSize; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 2126 | freeTempSpace(pBt); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2127 | } |
drh | fa9601a | 2009-06-18 17:22:39 +0000 | [diff] [blame] | 2128 | rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 2129 | pBt->usableSize = pBt->pageSize - (u16)nReserve; |
drh | ce4869f | 2009-04-02 20:16:58 +0000 | [diff] [blame] | 2130 | if( iFix ) pBt->pageSizeFixed = 1; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2131 | sqlite3BtreeLeave(p); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2132 | return rc; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2133 | } |
| 2134 | |
| 2135 | /* |
| 2136 | ** Return the currently defined page size |
| 2137 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2138 | int sqlite3BtreeGetPageSize(Btree *p){ |
| 2139 | return p->pBt->pageSize; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2140 | } |
drh | 7f75122 | 2009-03-17 22:33:00 +0000 | [diff] [blame] | 2141 | |
| 2142 | /* |
| 2143 | ** Return the number of bytes of space at the end of every page that |
| 2144 | ** are intentually left unused. This is the "reserved" space that is |
| 2145 | ** sometimes used by extensions. |
| 2146 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2147 | int sqlite3BtreeGetReserve(Btree *p){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2148 | int n; |
| 2149 | sqlite3BtreeEnter(p); |
| 2150 | n = p->pBt->pageSize - p->pBt->usableSize; |
| 2151 | sqlite3BtreeLeave(p); |
| 2152 | return n; |
drh | 2011d5f | 2004-07-22 02:40:37 +0000 | [diff] [blame] | 2153 | } |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 2154 | |
| 2155 | /* |
| 2156 | ** Set the maximum page count for a database if mxPage is positive. |
| 2157 | ** No changes are made if mxPage is 0 or negative. |
| 2158 | ** Regardless of the value of mxPage, return the maximum page count. |
| 2159 | */ |
| 2160 | int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2161 | int n; |
| 2162 | sqlite3BtreeEnter(p); |
| 2163 | n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage); |
| 2164 | sqlite3BtreeLeave(p); |
| 2165 | return n; |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 2166 | } |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 2167 | |
| 2168 | /* |
| 2169 | ** Set the secureDelete flag if newFlag is 0 or 1. If newFlag is -1, |
| 2170 | ** then make no changes. Always return the value of the secureDelete |
| 2171 | ** setting after the change. |
| 2172 | */ |
| 2173 | int sqlite3BtreeSecureDelete(Btree *p, int newFlag){ |
| 2174 | int b; |
drh | af034ed | 2010-02-12 19:46:26 +0000 | [diff] [blame] | 2175 | if( p==0 ) return 0; |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 2176 | sqlite3BtreeEnter(p); |
| 2177 | if( newFlag>=0 ){ |
| 2178 | p->pBt->secureDelete = (newFlag!=0) ? 1 : 0; |
| 2179 | } |
| 2180 | b = p->pBt->secureDelete; |
| 2181 | sqlite3BtreeLeave(p); |
| 2182 | return b; |
| 2183 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 2184 | #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2185 | |
| 2186 | /* |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 2187 | ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum' |
| 2188 | ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it |
| 2189 | ** is disabled. The default value for the auto-vacuum property is |
| 2190 | ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro. |
| 2191 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2192 | int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 2193 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 2194 | return SQLITE_READONLY; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 2195 | #else |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2196 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2197 | int rc = SQLITE_OK; |
drh | 076d466 | 2009-02-18 20:31:18 +0000 | [diff] [blame] | 2198 | u8 av = (u8)autoVacuum; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2199 | |
| 2200 | sqlite3BtreeEnter(p); |
drh | 076d466 | 2009-02-18 20:31:18 +0000 | [diff] [blame] | 2201 | if( pBt->pageSizeFixed && (av ?1:0)!=pBt->autoVacuum ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2202 | rc = SQLITE_READONLY; |
| 2203 | }else{ |
drh | 076d466 | 2009-02-18 20:31:18 +0000 | [diff] [blame] | 2204 | pBt->autoVacuum = av ?1:0; |
| 2205 | pBt->incrVacuum = av==2 ?1:0; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 2206 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2207 | sqlite3BtreeLeave(p); |
| 2208 | return rc; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 2209 | #endif |
| 2210 | } |
| 2211 | |
| 2212 | /* |
| 2213 | ** Return the value of the 'auto-vacuum' property. If auto-vacuum is |
| 2214 | ** enabled 1 is returned. Otherwise 0. |
| 2215 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2216 | int sqlite3BtreeGetAutoVacuum(Btree *p){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 2217 | #ifdef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2218 | return BTREE_AUTOVACUUM_NONE; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 2219 | #else |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2220 | int rc; |
| 2221 | sqlite3BtreeEnter(p); |
| 2222 | rc = ( |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2223 | (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE: |
| 2224 | (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL: |
| 2225 | BTREE_AUTOVACUUM_INCR |
| 2226 | ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2227 | sqlite3BtreeLeave(p); |
| 2228 | return rc; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 2229 | #endif |
| 2230 | } |
| 2231 | |
| 2232 | |
| 2233 | /* |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2234 | ** Get a reference to pPage1 of the database file. This will |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 2235 | ** also acquire a readlock on that file. |
| 2236 | ** |
| 2237 | ** SQLITE_OK is returned on success. If the file is not a |
| 2238 | ** well-formed database file, then SQLITE_CORRUPT is returned. |
| 2239 | ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 2240 | ** is returned if we run out of memory. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 2241 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2242 | static int lockBtree(BtShared *pBt){ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 2243 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2244 | MemPage *pPage1; |
danielk1977 | 93f7af9 | 2008-05-09 16:57:50 +0000 | [diff] [blame] | 2245 | int nPage; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2246 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2247 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 295dc10 | 2009-04-01 19:07:03 +0000 | [diff] [blame] | 2248 | assert( pBt->pPage1==0 ); |
danielk1977 | 89bc4bc | 2009-07-21 19:25:24 +0000 | [diff] [blame] | 2249 | rc = sqlite3PagerSharedLock(pBt->pPager); |
| 2250 | if( rc!=SQLITE_OK ) return rc; |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 2251 | rc = btreeGetPage(pBt, 1, &pPage1, 0); |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 2252 | if( rc!=SQLITE_OK ) return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 2253 | |
| 2254 | /* Do some checking to help insure the file we opened really is |
| 2255 | ** a valid database file. |
| 2256 | */ |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2257 | rc = sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 2258 | if( rc!=SQLITE_OK ){ |
danielk1977 | 93f7af9 | 2008-05-09 16:57:50 +0000 | [diff] [blame] | 2259 | goto page1_init_failed; |
| 2260 | }else if( nPage>0 ){ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 2261 | int pageSize; |
| 2262 | int usableSize; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2263 | u8 *page1 = pPage1->aData; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2264 | rc = SQLITE_NOTADB; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2265 | if( memcmp(page1, zMagicHeader, 16)!=0 ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2266 | goto page1_init_failed; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 2267 | } |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 2268 | if( page1[18]>1 ){ |
| 2269 | pBt->readOnly = 1; |
| 2270 | } |
| 2271 | if( page1[19]>1 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2272 | goto page1_init_failed; |
| 2273 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 2274 | |
| 2275 | /* The maximum embedded fraction must be exactly 25%. And the minimum |
| 2276 | ** embedded fraction must be 12.5% for both leaf-data and non-leaf-data. |
| 2277 | ** The original design allowed these amounts to vary, but as of |
| 2278 | ** version 3.6.0, we require them to be fixed. |
| 2279 | */ |
| 2280 | if( memcmp(&page1[21], "\100\040\040",3)!=0 ){ |
| 2281 | goto page1_init_failed; |
| 2282 | } |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 2283 | pageSize = get2byte(&page1[16]); |
drh | 7dc385e | 2007-09-06 23:39:36 +0000 | [diff] [blame] | 2284 | if( ((pageSize-1)&pageSize)!=0 || pageSize<512 || |
| 2285 | (SQLITE_MAX_PAGE_SIZE<32768 && pageSize>SQLITE_MAX_PAGE_SIZE) |
| 2286 | ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 2287 | goto page1_init_failed; |
| 2288 | } |
| 2289 | assert( (pageSize & 7)==0 ); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 2290 | usableSize = pageSize - page1[20]; |
| 2291 | if( pageSize!=pBt->pageSize ){ |
| 2292 | /* After reading the first page of the database assuming a page size |
| 2293 | ** of BtShared.pageSize, we have discovered that the page-size is |
| 2294 | ** actually pageSize. Unlock the database, leave pBt->pPage1 at |
| 2295 | ** zero and return SQLITE_OK. The caller will call this function |
| 2296 | ** again with the correct page-size. |
| 2297 | */ |
| 2298 | releasePage(pPage1); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 2299 | pBt->usableSize = (u16)usableSize; |
| 2300 | pBt->pageSize = (u16)pageSize; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 2301 | freeTempSpace(pBt); |
drh | fa9601a | 2009-06-18 17:22:39 +0000 | [diff] [blame] | 2302 | rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, |
| 2303 | pageSize-usableSize); |
drh | 5e48393 | 2009-07-10 16:51:30 +0000 | [diff] [blame] | 2304 | return rc; |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 2305 | } |
drh | b33e1b9 | 2009-06-18 11:29:20 +0000 | [diff] [blame] | 2306 | if( usableSize<480 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2307 | goto page1_init_failed; |
| 2308 | } |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 2309 | pBt->pageSize = (u16)pageSize; |
| 2310 | pBt->usableSize = (u16)usableSize; |
drh | 057cd3a | 2005-02-15 16:23:02 +0000 | [diff] [blame] | 2311 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2312 | pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0); |
danielk1977 | 27b1f95 | 2007-06-25 08:16:58 +0000 | [diff] [blame] | 2313 | pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0); |
drh | 057cd3a | 2005-02-15 16:23:02 +0000 | [diff] [blame] | 2314 | #endif |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 2315 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2316 | |
| 2317 | /* maxLocal is the maximum amount of payload to store locally for |
| 2318 | ** a cell. Make sure it is small enough so that at least minFanout |
| 2319 | ** cells can will fit on one page. We assume a 10-byte page header. |
| 2320 | ** Besides the payload, the cell must store: |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2321 | ** 2-byte pointer to the cell |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2322 | ** 4-byte child pointer |
| 2323 | ** 9-byte nKey value |
| 2324 | ** 4-byte nData value |
| 2325 | ** 4-byte overflow page pointer |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2326 | ** So a cell consists of a 2-byte poiner, a header which is as much as |
| 2327 | ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow |
| 2328 | ** page pointer. |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2329 | */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 2330 | pBt->maxLocal = (pBt->usableSize-12)*64/255 - 23; |
| 2331 | pBt->minLocal = (pBt->usableSize-12)*32/255 - 23; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2332 | pBt->maxLeaf = pBt->usableSize - 35; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 2333 | pBt->minLeaf = (pBt->usableSize-12)*32/255 - 23; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 2334 | assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2335 | pBt->pPage1 = pPage1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2336 | return SQLITE_OK; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 2337 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2338 | page1_init_failed: |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2339 | releasePage(pPage1); |
| 2340 | pBt->pPage1 = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2341 | return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 2342 | } |
| 2343 | |
| 2344 | /* |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 2345 | ** If there are no outstanding cursors and we are not in the middle |
| 2346 | ** of a transaction but there is a read lock on the database, then |
| 2347 | ** this routine unrefs the first page of the database file which |
| 2348 | ** has the effect of releasing the read lock. |
| 2349 | ** |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 2350 | ** If there is a transaction in progress, this routine is a no-op. |
| 2351 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2352 | static void unlockBtreeIfUnused(BtShared *pBt){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2353 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 1bc9ee9 | 2009-07-04 15:41:02 +0000 | [diff] [blame] | 2354 | assert( pBt->pCursor==0 || pBt->inTransaction>TRANS_NONE ); |
| 2355 | if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){ |
danielk1977 | c1761e8 | 2009-06-25 09:40:03 +0000 | [diff] [blame] | 2356 | assert( pBt->pPage1->aData ); |
| 2357 | assert( sqlite3PagerRefcount(pBt->pPager)==1 ); |
| 2358 | assert( pBt->pPage1->aData ); |
| 2359 | releasePage(pBt->pPage1); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2360 | pBt->pPage1 = 0; |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 2361 | } |
| 2362 | } |
| 2363 | |
| 2364 | /* |
drh | e39f2f9 | 2009-07-23 01:43:59 +0000 | [diff] [blame] | 2365 | ** If pBt points to an empty file then convert that empty file |
| 2366 | ** into a new empty database by initializing the first page of |
| 2367 | ** the database. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2368 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2369 | static int newDatabase(BtShared *pBt){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 2370 | MemPage *pP1; |
| 2371 | unsigned char *data; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2372 | int rc; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2373 | int nPage; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2374 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2375 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2376 | rc = sqlite3PagerPagecount(pBt->pPager, &nPage); |
drh | 313aa57 | 2009-12-03 19:40:00 +0000 | [diff] [blame] | 2377 | if( rc!=SQLITE_OK || nPage>0 ){ |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2378 | return rc; |
| 2379 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2380 | pP1 = pBt->pPage1; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 2381 | assert( pP1!=0 ); |
| 2382 | data = pP1->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2383 | rc = sqlite3PagerWrite(pP1->pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2384 | if( rc ) return rc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 2385 | memcpy(data, zMagicHeader, sizeof(zMagicHeader)); |
| 2386 | assert( sizeof(zMagicHeader)==16 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2387 | put2byte(&data[16], pBt->pageSize); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 2388 | data[18] = 1; |
| 2389 | data[19] = 1; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 2390 | assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize); |
| 2391 | data[20] = (u8)(pBt->pageSize - pBt->usableSize); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 2392 | data[21] = 64; |
| 2393 | data[22] = 32; |
| 2394 | data[23] = 32; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2395 | memset(&data[24], 0, 100-24); |
drh | e6c4381 | 2004-05-14 12:17:46 +0000 | [diff] [blame] | 2396 | zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA ); |
drh | f2a611c | 2004-09-05 00:33:43 +0000 | [diff] [blame] | 2397 | pBt->pageSizeFixed = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2398 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2399 | assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 ); |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 2400 | assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 ); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2401 | put4byte(&data[36 + 4*4], pBt->autoVacuum); |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 2402 | put4byte(&data[36 + 7*4], pBt->incrVacuum); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2403 | #endif |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2404 | return SQLITE_OK; |
| 2405 | } |
| 2406 | |
| 2407 | /* |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2408 | ** Attempt to start a new transaction. A write-transaction |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 2409 | ** is started if the second argument is nonzero, otherwise a read- |
| 2410 | ** transaction. If the second argument is 2 or more and exclusive |
| 2411 | ** transaction is started, meaning that no other process is allowed |
| 2412 | ** to access the database. A preexisting transaction may not be |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2413 | ** upgraded to exclusive by calling this routine a second time - the |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 2414 | ** exclusivity flag only works for a new transaction. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2415 | ** |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2416 | ** A write-transaction must be started before attempting any |
| 2417 | ** changes to the database. None of the following routines |
| 2418 | ** will work unless a transaction is started first: |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2419 | ** |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 2420 | ** sqlite3BtreeCreateTable() |
| 2421 | ** sqlite3BtreeCreateIndex() |
| 2422 | ** sqlite3BtreeClearTable() |
| 2423 | ** sqlite3BtreeDropTable() |
| 2424 | ** sqlite3BtreeInsert() |
| 2425 | ** sqlite3BtreeDelete() |
| 2426 | ** sqlite3BtreeUpdateMeta() |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 2427 | ** |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2428 | ** If an initial attempt to acquire the lock fails because of lock contention |
| 2429 | ** and the database was previously unlocked, then invoke the busy handler |
| 2430 | ** if there is one. But if there was previously a read-lock, do not |
| 2431 | ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is |
| 2432 | ** returned when there is already a read-lock in order to avoid a deadlock. |
| 2433 | ** |
| 2434 | ** Suppose there are two processes A and B. A has a read lock and B has |
| 2435 | ** a reserved lock. B tries to promote to exclusive but is blocked because |
| 2436 | ** of A's read lock. A tries to promote to reserved but is blocked by B. |
| 2437 | ** One or the other of the two processes must give way or there can be |
| 2438 | ** no progress. By returning SQLITE_BUSY and not invoking the busy callback |
| 2439 | ** when A already has a read lock, we encourage A to give up and let B |
| 2440 | ** proceed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2441 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2442 | int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2443 | sqlite3 *pBlock = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2444 | BtShared *pBt = p->pBt; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2445 | int rc = SQLITE_OK; |
| 2446 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2447 | sqlite3BtreeEnter(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2448 | btreeIntegrity(p); |
| 2449 | |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2450 | /* If the btree is already in a write-transaction, or it |
| 2451 | ** is already in a read-transaction and a read-transaction |
| 2452 | ** is requested, this is a no-op. |
| 2453 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2454 | if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2455 | goto trans_begun; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2456 | } |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2457 | |
| 2458 | /* Write transactions are not possible on a read-only database */ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2459 | if( pBt->readOnly && wrflag ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2460 | rc = SQLITE_READONLY; |
| 2461 | goto trans_begun; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2462 | } |
| 2463 | |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2464 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2465 | /* If another database handle has already opened a write transaction |
| 2466 | ** on this shared-btree structure and a second write transaction is |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2467 | ** requested, return SQLITE_LOCKED. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2468 | */ |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2469 | if( (wrflag && pBt->inTransaction==TRANS_WRITE) || pBt->isPending ){ |
| 2470 | pBlock = pBt->pWriter->db; |
| 2471 | }else if( wrflag>1 ){ |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 2472 | BtLock *pIter; |
| 2473 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 2474 | if( pIter->pBtree!=p ){ |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2475 | pBlock = pIter->pBtree->db; |
| 2476 | break; |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 2477 | } |
| 2478 | } |
| 2479 | } |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2480 | if( pBlock ){ |
| 2481 | sqlite3ConnectionBlocked(p->db, pBlock); |
| 2482 | rc = SQLITE_LOCKED_SHAREDCACHE; |
| 2483 | goto trans_begun; |
| 2484 | } |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 2485 | #endif |
| 2486 | |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 2487 | /* Any read-only or read-write transaction implies a read-lock on |
| 2488 | ** page 1. So if some other shared-cache client already has a write-lock |
| 2489 | ** on page 1, the transaction cannot be opened. */ |
drh | 4c301aa | 2009-07-15 17:25:45 +0000 | [diff] [blame] | 2490 | rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK); |
| 2491 | if( SQLITE_OK!=rc ) goto trans_begun; |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 2492 | |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2493 | do { |
danielk1977 | 295dc10 | 2009-04-01 19:07:03 +0000 | [diff] [blame] | 2494 | /* Call lockBtree() until either pBt->pPage1 is populated or |
| 2495 | ** lockBtree() returns something other than SQLITE_OK. lockBtree() |
| 2496 | ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after |
| 2497 | ** reading page 1 it discovers that the page-size of the database |
| 2498 | ** file is not pBt->pageSize. In this case lockBtree() will update |
| 2499 | ** pBt->pageSize to the page-size of the file on disk. |
| 2500 | */ |
| 2501 | while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) ); |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 2502 | |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2503 | if( rc==SQLITE_OK && wrflag ){ |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 2504 | if( pBt->readOnly ){ |
| 2505 | rc = SQLITE_READONLY; |
| 2506 | }else{ |
danielk1977 | d829335 | 2009-04-30 09:10:37 +0000 | [diff] [blame] | 2507 | rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db)); |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 2508 | if( rc==SQLITE_OK ){ |
| 2509 | rc = newDatabase(pBt); |
| 2510 | } |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2511 | } |
| 2512 | } |
| 2513 | |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 2514 | if( rc!=SQLITE_OK ){ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2515 | unlockBtreeIfUnused(pBt); |
| 2516 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2517 | }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE && |
danielk1977 | 1ceedd3 | 2008-11-19 10:22:33 +0000 | [diff] [blame] | 2518 | btreeInvokeBusyHandler(pBt) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2519 | |
| 2520 | if( rc==SQLITE_OK ){ |
| 2521 | if( p->inTrans==TRANS_NONE ){ |
| 2522 | pBt->nTransaction++; |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 2523 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 2524 | if( p->sharable ){ |
| 2525 | assert( p->lock.pBtree==p && p->lock.iTable==1 ); |
| 2526 | p->lock.eLock = READ_LOCK; |
| 2527 | p->lock.pNext = pBt->pLock; |
| 2528 | pBt->pLock = &p->lock; |
| 2529 | } |
| 2530 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2531 | } |
| 2532 | p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ); |
| 2533 | if( p->inTrans>pBt->inTransaction ){ |
| 2534 | pBt->inTransaction = p->inTrans; |
| 2535 | } |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 2536 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2537 | if( wrflag ){ |
| 2538 | assert( !pBt->pWriter ); |
| 2539 | pBt->pWriter = p; |
shane | ca18d20 | 2009-03-23 02:34:32 +0000 | [diff] [blame] | 2540 | pBt->isExclusive = (u8)(wrflag>1); |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 2541 | } |
| 2542 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2543 | } |
| 2544 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2545 | |
| 2546 | trans_begun: |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2547 | if( rc==SQLITE_OK && wrflag ){ |
danielk1977 | 12dd549 | 2008-12-18 15:45:07 +0000 | [diff] [blame] | 2548 | /* This call makes sure that the pager has the correct number of |
| 2549 | ** open savepoints. If the second parameter is greater than 0 and |
| 2550 | ** the sub-journal is not already open, then it will be opened here. |
| 2551 | */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2552 | rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint); |
| 2553 | } |
danielk1977 | 12dd549 | 2008-12-18 15:45:07 +0000 | [diff] [blame] | 2554 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2555 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2556 | sqlite3BtreeLeave(p); |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 2557 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2558 | } |
| 2559 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2560 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2561 | |
| 2562 | /* |
| 2563 | ** Set the pointer-map entries for all children of page pPage. Also, if |
| 2564 | ** pPage contains cells that point to overflow pages, set the pointer |
| 2565 | ** map entries for the overflow pages as well. |
| 2566 | */ |
| 2567 | static int setChildPtrmaps(MemPage *pPage){ |
| 2568 | int i; /* Counter variable */ |
| 2569 | int nCell; /* Number of cells in page pPage */ |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 2570 | int rc; /* Return code */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2571 | BtShared *pBt = pPage->pBt; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 2572 | u8 isInitOrig = pPage->isInit; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2573 | Pgno pgno = pPage->pgno; |
| 2574 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2575 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 2576 | rc = btreeInitPage(pPage); |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 2577 | if( rc!=SQLITE_OK ){ |
| 2578 | goto set_child_ptrmaps_out; |
| 2579 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2580 | nCell = pPage->nCell; |
| 2581 | |
| 2582 | for(i=0; i<nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2583 | u8 *pCell = findCell(pPage, i); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2584 | |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 2585 | ptrmapPutOvflPtr(pPage, pCell, &rc); |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 2586 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2587 | if( !pPage->leaf ){ |
| 2588 | Pgno childPgno = get4byte(pCell); |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 2589 | ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2590 | } |
| 2591 | } |
| 2592 | |
| 2593 | if( !pPage->leaf ){ |
| 2594 | Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 2595 | ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2596 | } |
| 2597 | |
| 2598 | set_child_ptrmaps_out: |
| 2599 | pPage->isInit = isInitOrig; |
| 2600 | return rc; |
| 2601 | } |
| 2602 | |
| 2603 | /* |
drh | f3aed59 | 2009-07-08 18:12:49 +0000 | [diff] [blame] | 2604 | ** Somewhere on pPage is a pointer to page iFrom. Modify this pointer so |
| 2605 | ** that it points to iTo. Parameter eType describes the type of pointer to |
| 2606 | ** be modified, as follows: |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2607 | ** |
| 2608 | ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child |
| 2609 | ** page of pPage. |
| 2610 | ** |
| 2611 | ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow |
| 2612 | ** page pointed to by one of the cells on pPage. |
| 2613 | ** |
| 2614 | ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next |
| 2615 | ** overflow page in the list. |
| 2616 | */ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2617 | static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2618 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 2619 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2620 | if( eType==PTRMAP_OVERFLOW2 ){ |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 2621 | /* 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] | 2622 | if( get4byte(pPage->aData)!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2623 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2624 | } |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 2625 | put4byte(pPage->aData, iTo); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2626 | }else{ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 2627 | u8 isInitOrig = pPage->isInit; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2628 | int i; |
| 2629 | int nCell; |
| 2630 | |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 2631 | btreeInitPage(pPage); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2632 | nCell = pPage->nCell; |
| 2633 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2634 | for(i=0; i<nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2635 | u8 *pCell = findCell(pPage, i); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2636 | if( eType==PTRMAP_OVERFLOW1 ){ |
| 2637 | CellInfo info; |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 2638 | btreeParseCellPtr(pPage, pCell, &info); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2639 | if( info.iOverflow ){ |
| 2640 | if( iFrom==get4byte(&pCell[info.iOverflow]) ){ |
| 2641 | put4byte(&pCell[info.iOverflow], iTo); |
| 2642 | break; |
| 2643 | } |
| 2644 | } |
| 2645 | }else{ |
| 2646 | if( get4byte(pCell)==iFrom ){ |
| 2647 | put4byte(pCell, iTo); |
| 2648 | break; |
| 2649 | } |
| 2650 | } |
| 2651 | } |
| 2652 | |
| 2653 | if( i==nCell ){ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2654 | if( eType!=PTRMAP_BTREE || |
| 2655 | get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2656 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2657 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2658 | put4byte(&pPage->aData[pPage->hdrOffset+8], iTo); |
| 2659 | } |
| 2660 | |
| 2661 | pPage->isInit = isInitOrig; |
| 2662 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2663 | return SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2664 | } |
| 2665 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2666 | |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2667 | /* |
| 2668 | ** Move the open database page pDbPage to location iFreePage in the |
| 2669 | ** database. The pDbPage reference remains valid. |
drh | e64ca7b | 2009-07-16 18:21:17 +0000 | [diff] [blame] | 2670 | ** |
| 2671 | ** The isCommit flag indicates that there is no need to remember that |
| 2672 | ** the journal needs to be sync()ed before database page pDbPage->pgno |
| 2673 | ** can be written to. The caller has already promised not to write to that |
| 2674 | ** page. |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2675 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2676 | static int relocatePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2677 | BtShared *pBt, /* Btree */ |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2678 | MemPage *pDbPage, /* Open page to move */ |
| 2679 | u8 eType, /* Pointer map 'type' entry for pDbPage */ |
| 2680 | Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */ |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 2681 | Pgno iFreePage, /* The location to move pDbPage to */ |
drh | e64ca7b | 2009-07-16 18:21:17 +0000 | [diff] [blame] | 2682 | int isCommit /* isCommit flag passed to sqlite3PagerMovepage */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2683 | ){ |
| 2684 | MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */ |
| 2685 | Pgno iDbPage = pDbPage->pgno; |
| 2686 | Pager *pPager = pBt->pPager; |
| 2687 | int rc; |
| 2688 | |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2689 | assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || |
| 2690 | eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2691 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 2692 | assert( pDbPage->pBt==pBt ); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2693 | |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 2694 | /* Move page iDbPage from its current location to page number iFreePage */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2695 | TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", |
| 2696 | iDbPage, iFreePage, iPtrPage, eType)); |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 2697 | rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2698 | if( rc!=SQLITE_OK ){ |
| 2699 | return rc; |
| 2700 | } |
| 2701 | pDbPage->pgno = iFreePage; |
| 2702 | |
| 2703 | /* If pDbPage was a btree-page, then it may have child pages and/or cells |
| 2704 | ** that point to overflow pages. The pointer map entries for all these |
| 2705 | ** pages need to be changed. |
| 2706 | ** |
| 2707 | ** If pDbPage is an overflow page, then the first 4 bytes may store a |
| 2708 | ** pointer to a subsequent overflow page. If this is the case, then |
| 2709 | ** the pointer map needs to be updated for the subsequent overflow page. |
| 2710 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2711 | if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2712 | rc = setChildPtrmaps(pDbPage); |
| 2713 | if( rc!=SQLITE_OK ){ |
| 2714 | return rc; |
| 2715 | } |
| 2716 | }else{ |
| 2717 | Pgno nextOvfl = get4byte(pDbPage->aData); |
| 2718 | if( nextOvfl!=0 ){ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 2719 | ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage, &rc); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2720 | if( rc!=SQLITE_OK ){ |
| 2721 | return rc; |
| 2722 | } |
| 2723 | } |
| 2724 | } |
| 2725 | |
| 2726 | /* Fix the database pointer on page iPtrPage that pointed at iDbPage so |
| 2727 | ** that it points at iFreePage. Also fix the pointer map entry for |
| 2728 | ** iPtrPage. |
| 2729 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2730 | if( eType!=PTRMAP_ROOTPAGE ){ |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 2731 | rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2732 | if( rc!=SQLITE_OK ){ |
| 2733 | return rc; |
| 2734 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2735 | rc = sqlite3PagerWrite(pPtrPage->pDbPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2736 | if( rc!=SQLITE_OK ){ |
| 2737 | releasePage(pPtrPage); |
| 2738 | return rc; |
| 2739 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2740 | rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2741 | releasePage(pPtrPage); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2742 | if( rc==SQLITE_OK ){ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 2743 | ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2744 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2745 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2746 | return rc; |
| 2747 | } |
| 2748 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2749 | /* Forward declaration required by incrVacuumStep(). */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2750 | static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2751 | |
| 2752 | /* |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2753 | ** Perform a single step of an incremental-vacuum. If successful, |
| 2754 | ** return SQLITE_OK. If there is no work to do (and therefore no |
| 2755 | ** point in calling this function again), return SQLITE_DONE. |
| 2756 | ** |
| 2757 | ** More specificly, this function attempts to re-organize the |
| 2758 | ** database so that the last page of the file currently in use |
| 2759 | ** is no longer in use. |
| 2760 | ** |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 2761 | ** If the nFin parameter is non-zero, this function assumes |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2762 | ** that the caller will keep calling incrVacuumStep() until |
| 2763 | ** it returns SQLITE_DONE or an error, and that nFin is the |
| 2764 | ** number of pages the database file will contain after this |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 2765 | ** process is complete. If nFin is zero, it is assumed that |
| 2766 | ** incrVacuumStep() will be called a finite amount of times |
| 2767 | ** which may or may not empty the freelist. A full autovacuum |
| 2768 | ** has nFin>0. A "PRAGMA incremental_vacuum" has nFin==0. |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2769 | */ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2770 | static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2771 | Pgno nFreeList; /* Number of pages still on the free-list */ |
| 2772 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2773 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | fa542f1 | 2009-04-02 18:28:08 +0000 | [diff] [blame] | 2774 | assert( iLastPg>nFin ); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2775 | |
| 2776 | if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){ |
| 2777 | int rc; |
| 2778 | u8 eType; |
| 2779 | Pgno iPtrPage; |
| 2780 | |
| 2781 | nFreeList = get4byte(&pBt->pPage1->aData[36]); |
danielk1977 | fa542f1 | 2009-04-02 18:28:08 +0000 | [diff] [blame] | 2782 | if( nFreeList==0 ){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2783 | return SQLITE_DONE; |
| 2784 | } |
| 2785 | |
| 2786 | rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage); |
| 2787 | if( rc!=SQLITE_OK ){ |
| 2788 | return rc; |
| 2789 | } |
| 2790 | if( eType==PTRMAP_ROOTPAGE ){ |
| 2791 | return SQLITE_CORRUPT_BKPT; |
| 2792 | } |
| 2793 | |
| 2794 | if( eType==PTRMAP_FREEPAGE ){ |
| 2795 | if( nFin==0 ){ |
| 2796 | /* Remove the page from the files free-list. This is not required |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 2797 | ** if nFin is non-zero. In that case, the free-list will be |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2798 | ** truncated to zero after this function returns, so it doesn't |
| 2799 | ** matter if it still contains some garbage entries. |
| 2800 | */ |
| 2801 | Pgno iFreePg; |
| 2802 | MemPage *pFreePg; |
| 2803 | rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1); |
| 2804 | if( rc!=SQLITE_OK ){ |
| 2805 | return rc; |
| 2806 | } |
| 2807 | assert( iFreePg==iLastPg ); |
| 2808 | releasePage(pFreePg); |
| 2809 | } |
| 2810 | } else { |
| 2811 | Pgno iFreePg; /* Index of free page to move pLastPg to */ |
| 2812 | MemPage *pLastPg; |
| 2813 | |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 2814 | rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2815 | if( rc!=SQLITE_OK ){ |
| 2816 | return rc; |
| 2817 | } |
| 2818 | |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 2819 | /* If nFin is zero, this loop runs exactly once and page pLastPg |
| 2820 | ** is swapped with the first free page pulled off the free list. |
| 2821 | ** |
| 2822 | ** On the other hand, if nFin is greater than zero, then keep |
| 2823 | ** looping until a free-page located within the first nFin pages |
| 2824 | ** of the file is found. |
| 2825 | */ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2826 | do { |
| 2827 | MemPage *pFreePg; |
| 2828 | rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0); |
| 2829 | if( rc!=SQLITE_OK ){ |
| 2830 | releasePage(pLastPg); |
| 2831 | return rc; |
| 2832 | } |
| 2833 | releasePage(pFreePg); |
| 2834 | }while( nFin!=0 && iFreePg>nFin ); |
| 2835 | assert( iFreePg<iLastPg ); |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 2836 | |
| 2837 | rc = sqlite3PagerWrite(pLastPg->pDbPage); |
danielk1977 | 662278e | 2007-11-05 15:30:12 +0000 | [diff] [blame] | 2838 | if( rc==SQLITE_OK ){ |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 2839 | rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, nFin!=0); |
danielk1977 | 662278e | 2007-11-05 15:30:12 +0000 | [diff] [blame] | 2840 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2841 | releasePage(pLastPg); |
| 2842 | if( rc!=SQLITE_OK ){ |
| 2843 | return rc; |
danielk1977 | 662278e | 2007-11-05 15:30:12 +0000 | [diff] [blame] | 2844 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2845 | } |
| 2846 | } |
| 2847 | |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2848 | if( nFin==0 ){ |
| 2849 | iLastPg--; |
| 2850 | while( iLastPg==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, iLastPg) ){ |
danielk1977 | f402778 | 2009-03-30 18:50:04 +0000 | [diff] [blame] | 2851 | if( PTRMAP_ISPAGE(pBt, iLastPg) ){ |
| 2852 | MemPage *pPg; |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 2853 | int rc = btreeGetPage(pBt, iLastPg, &pPg, 0); |
danielk1977 | f402778 | 2009-03-30 18:50:04 +0000 | [diff] [blame] | 2854 | if( rc!=SQLITE_OK ){ |
| 2855 | return rc; |
| 2856 | } |
| 2857 | rc = sqlite3PagerWrite(pPg->pDbPage); |
| 2858 | releasePage(pPg); |
| 2859 | if( rc!=SQLITE_OK ){ |
| 2860 | return rc; |
| 2861 | } |
| 2862 | } |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2863 | iLastPg--; |
| 2864 | } |
| 2865 | sqlite3PagerTruncateImage(pBt->pPager, iLastPg); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2866 | } |
| 2867 | return SQLITE_OK; |
| 2868 | } |
| 2869 | |
| 2870 | /* |
| 2871 | ** A write-transaction must be opened before calling this function. |
| 2872 | ** It performs a single unit of work towards an incremental vacuum. |
| 2873 | ** |
| 2874 | ** If the incremental vacuum is finished after this function has run, |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 2875 | ** SQLITE_DONE is returned. If it is not finished, but no error occurred, |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2876 | ** SQLITE_OK is returned. Otherwise an SQLite error code. |
| 2877 | */ |
| 2878 | int sqlite3BtreeIncrVacuum(Btree *p){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2879 | int rc; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2880 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2881 | |
| 2882 | sqlite3BtreeEnter(p); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2883 | assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE ); |
| 2884 | if( !pBt->autoVacuum ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2885 | rc = SQLITE_DONE; |
| 2886 | }else{ |
| 2887 | invalidateAllOverflowCache(pBt); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 2888 | rc = incrVacuumStep(pBt, 0, pagerPagecount(pBt)); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2889 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2890 | sqlite3BtreeLeave(p); |
| 2891 | return rc; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2892 | } |
| 2893 | |
| 2894 | /* |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2895 | ** This routine is called prior to sqlite3PagerCommit when a transaction |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2896 | ** is commited for an auto-vacuum database. |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 2897 | ** |
| 2898 | ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages |
| 2899 | ** the database file should be truncated to during the commit process. |
| 2900 | ** i.e. the database has been reorganized so that only the first *pnTrunc |
| 2901 | ** pages are in use. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2902 | */ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2903 | static int autoVacuumCommit(BtShared *pBt){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2904 | int rc = SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2905 | Pager *pPager = pBt->pPager; |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 2906 | VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2907 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2908 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 2909 | invalidateAllOverflowCache(pBt); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2910 | assert(pBt->autoVacuum); |
| 2911 | if( !pBt->incrVacuum ){ |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 2912 | Pgno nFin; /* Number of pages in database after autovacuuming */ |
| 2913 | Pgno nFree; /* Number of pages on the freelist initially */ |
drh | 41d628c | 2009-07-11 17:04:08 +0000 | [diff] [blame] | 2914 | Pgno nPtrmap; /* Number of PtrMap pages to be freed */ |
| 2915 | Pgno iFree; /* The next page to be freed */ |
| 2916 | int nEntry; /* Number of entries on one ptrmap page */ |
| 2917 | Pgno nOrig; /* Database size before freeing */ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2918 | |
drh | 41d628c | 2009-07-11 17:04:08 +0000 | [diff] [blame] | 2919 | nOrig = pagerPagecount(pBt); |
danielk1977 | ef165ce | 2009-04-06 17:50:03 +0000 | [diff] [blame] | 2920 | if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){ |
| 2921 | /* It is not possible to create a database for which the final page |
| 2922 | ** is either a pointer-map page or the pending-byte page. If one |
| 2923 | ** is encountered, this indicates corruption. |
| 2924 | */ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2925 | return SQLITE_CORRUPT_BKPT; |
| 2926 | } |
danielk1977 | ef165ce | 2009-04-06 17:50:03 +0000 | [diff] [blame] | 2927 | |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2928 | nFree = get4byte(&pBt->pPage1->aData[36]); |
drh | 41d628c | 2009-07-11 17:04:08 +0000 | [diff] [blame] | 2929 | nEntry = pBt->usableSize/5; |
| 2930 | nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry; |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2931 | nFin = nOrig - nFree - nPtrmap; |
danielk1977 | ef165ce | 2009-04-06 17:50:03 +0000 | [diff] [blame] | 2932 | if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2933 | nFin--; |
| 2934 | } |
| 2935 | while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){ |
| 2936 | nFin--; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2937 | } |
drh | c5e47ac | 2009-06-04 00:11:56 +0000 | [diff] [blame] | 2938 | if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2939 | |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2940 | for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){ |
| 2941 | rc = incrVacuumStep(pBt, nFin, iFree); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2942 | } |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2943 | if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2944 | rc = SQLITE_OK; |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2945 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 2946 | put4byte(&pBt->pPage1->aData[32], 0); |
| 2947 | put4byte(&pBt->pPage1->aData[36], 0); |
| 2948 | sqlite3PagerTruncateImage(pBt->pPager, nFin); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2949 | } |
| 2950 | if( rc!=SQLITE_OK ){ |
| 2951 | sqlite3PagerRollback(pPager); |
| 2952 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2953 | } |
| 2954 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2955 | assert( nRef==sqlite3PagerRefcount(pPager) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2956 | return rc; |
| 2957 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2958 | |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 2959 | #else /* ifndef SQLITE_OMIT_AUTOVACUUM */ |
| 2960 | # define setChildPtrmaps(x) SQLITE_OK |
| 2961 | #endif |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2962 | |
| 2963 | /* |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2964 | ** This routine does the first phase of a two-phase commit. This routine |
| 2965 | ** causes a rollback journal to be created (if it does not already exist) |
| 2966 | ** and populated with enough information so that if a power loss occurs |
| 2967 | ** the database can be restored to its original state by playing back |
| 2968 | ** the journal. Then the contents of the journal are flushed out to |
| 2969 | ** the disk. After the journal is safely on oxide, the changes to the |
| 2970 | ** database are written into the database file and flushed to oxide. |
| 2971 | ** At the end of this call, the rollback journal still exists on the |
| 2972 | ** disk and we are still holding all locks, so the transaction has not |
drh | 51898cf | 2009-04-19 20:51:06 +0000 | [diff] [blame] | 2973 | ** committed. See sqlite3BtreeCommitPhaseTwo() for the second phase of the |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2974 | ** commit process. |
| 2975 | ** |
| 2976 | ** This call is a no-op if no write-transaction is currently active on pBt. |
| 2977 | ** |
| 2978 | ** Otherwise, sync the database file for the btree pBt. zMaster points to |
| 2979 | ** the name of a master journal file that should be written into the |
| 2980 | ** individual journal file, or is NULL, indicating no master journal file |
| 2981 | ** (single database transaction). |
| 2982 | ** |
| 2983 | ** When this is called, the master journal should already have been |
| 2984 | ** created, populated with this journal pointer and synced to disk. |
| 2985 | ** |
| 2986 | ** Once this is routine has returned, the only thing required to commit |
| 2987 | ** the write-transaction for this database file is to delete the journal. |
| 2988 | */ |
| 2989 | int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){ |
| 2990 | int rc = SQLITE_OK; |
| 2991 | if( p->inTrans==TRANS_WRITE ){ |
| 2992 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2993 | sqlite3BtreeEnter(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2994 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2995 | if( pBt->autoVacuum ){ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 2996 | rc = autoVacuumCommit(pBt); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2997 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2998 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2999 | return rc; |
| 3000 | } |
| 3001 | } |
| 3002 | #endif |
drh | 49b9d33 | 2009-01-02 18:10:42 +0000 | [diff] [blame] | 3003 | rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3004 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3005 | } |
| 3006 | return rc; |
| 3007 | } |
| 3008 | |
| 3009 | /* |
danielk1977 | 94b3073 | 2009-07-02 17:21:57 +0000 | [diff] [blame] | 3010 | ** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback() |
| 3011 | ** at the conclusion of a transaction. |
| 3012 | */ |
| 3013 | static void btreeEndTransaction(Btree *p){ |
| 3014 | BtShared *pBt = p->pBt; |
danielk1977 | 94b3073 | 2009-07-02 17:21:57 +0000 | [diff] [blame] | 3015 | assert( sqlite3BtreeHoldsMutex(p) ); |
| 3016 | |
danielk1977 | 94b3073 | 2009-07-02 17:21:57 +0000 | [diff] [blame] | 3017 | btreeClearHasContent(pBt); |
dan | fa401de | 2009-10-16 14:55:03 +0000 | [diff] [blame] | 3018 | if( p->inTrans>TRANS_NONE && p->db->activeVdbeCnt>1 ){ |
| 3019 | /* If there are other active statements that belong to this database |
| 3020 | ** handle, downgrade to a read-only transaction. The other statements |
| 3021 | ** may still be reading from the database. */ |
danielk1977 | 94b3073 | 2009-07-02 17:21:57 +0000 | [diff] [blame] | 3022 | downgradeAllSharedCacheTableLocks(p); |
| 3023 | p->inTrans = TRANS_READ; |
| 3024 | }else{ |
| 3025 | /* If the handle had any kind of transaction open, decrement the |
| 3026 | ** transaction count of the shared btree. If the transaction count |
| 3027 | ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused() |
| 3028 | ** call below will unlock the pager. */ |
| 3029 | if( p->inTrans!=TRANS_NONE ){ |
| 3030 | clearAllSharedCacheTableLocks(p); |
| 3031 | pBt->nTransaction--; |
| 3032 | if( 0==pBt->nTransaction ){ |
| 3033 | pBt->inTransaction = TRANS_NONE; |
| 3034 | } |
| 3035 | } |
| 3036 | |
| 3037 | /* Set the current transaction state to TRANS_NONE and unlock the |
| 3038 | ** pager if this call closed the only read or write transaction. */ |
| 3039 | p->inTrans = TRANS_NONE; |
| 3040 | unlockBtreeIfUnused(pBt); |
| 3041 | } |
| 3042 | |
| 3043 | btreeIntegrity(p); |
| 3044 | } |
| 3045 | |
| 3046 | /* |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3047 | ** Commit the transaction currently in progress. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3048 | ** |
drh | 6e34599 | 2007-03-30 11:12:08 +0000 | [diff] [blame] | 3049 | ** This routine implements the second phase of a 2-phase commit. The |
drh | 51898cf | 2009-04-19 20:51:06 +0000 | [diff] [blame] | 3050 | ** sqlite3BtreeCommitPhaseOne() routine does the first phase and should |
| 3051 | ** be invoked prior to calling this routine. The sqlite3BtreeCommitPhaseOne() |
| 3052 | ** routine did all the work of writing information out to disk and flushing the |
drh | 6e34599 | 2007-03-30 11:12:08 +0000 | [diff] [blame] | 3053 | ** contents so that they are written onto the disk platter. All this |
drh | 51898cf | 2009-04-19 20:51:06 +0000 | [diff] [blame] | 3054 | ** routine has to do is delete or truncate or zero the header in the |
| 3055 | ** the rollback journal (which causes the transaction to commit) and |
| 3056 | ** drop locks. |
drh | 6e34599 | 2007-03-30 11:12:08 +0000 | [diff] [blame] | 3057 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3058 | ** This will release the write lock on the database file. If there |
| 3059 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3060 | */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3061 | int sqlite3BtreeCommitPhaseTwo(Btree *p){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3062 | BtShared *pBt = p->pBt; |
| 3063 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3064 | sqlite3BtreeEnter(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3065 | btreeIntegrity(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3066 | |
| 3067 | /* If the handle has a write-transaction open, commit the shared-btrees |
| 3068 | ** transaction and set the shared state to TRANS_READ. |
| 3069 | */ |
| 3070 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 3071 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3072 | assert( pBt->inTransaction==TRANS_WRITE ); |
| 3073 | assert( pBt->nTransaction>0 ); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3074 | rc = sqlite3PagerCommitPhaseTwo(pBt->pPager); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 3075 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3076 | sqlite3BtreeLeave(p); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 3077 | return rc; |
| 3078 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3079 | pBt->inTransaction = TRANS_READ; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 3080 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3081 | |
danielk1977 | 94b3073 | 2009-07-02 17:21:57 +0000 | [diff] [blame] | 3082 | btreeEndTransaction(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3083 | sqlite3BtreeLeave(p); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 3084 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3085 | } |
| 3086 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3087 | /* |
| 3088 | ** Do both phases of a commit. |
| 3089 | */ |
| 3090 | int sqlite3BtreeCommit(Btree *p){ |
| 3091 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3092 | sqlite3BtreeEnter(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3093 | rc = sqlite3BtreeCommitPhaseOne(p, 0); |
| 3094 | if( rc==SQLITE_OK ){ |
| 3095 | rc = sqlite3BtreeCommitPhaseTwo(p); |
| 3096 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3097 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3098 | return rc; |
| 3099 | } |
| 3100 | |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 3101 | #ifndef NDEBUG |
| 3102 | /* |
| 3103 | ** Return the number of write-cursors open on this handle. This is for use |
| 3104 | ** in assert() expressions, so it is only compiled if NDEBUG is not |
| 3105 | ** defined. |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 3106 | ** |
| 3107 | ** For the purposes of this routine, a write-cursor is any cursor that |
| 3108 | ** is capable of writing to the databse. That means the cursor was |
| 3109 | ** originally opened for writing and the cursor has not be disabled |
| 3110 | ** by having its state changed to CURSOR_FAULT. |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 3111 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3112 | static int countWriteCursors(BtShared *pBt){ |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 3113 | BtCursor *pCur; |
| 3114 | int r = 0; |
| 3115 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 3116 | if( pCur->wrFlag && pCur->eState!=CURSOR_FAULT ) r++; |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 3117 | } |
| 3118 | return r; |
| 3119 | } |
| 3120 | #endif |
| 3121 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3122 | /* |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 3123 | ** This routine sets the state to CURSOR_FAULT and the error |
| 3124 | ** code to errCode for every cursor on BtShared that pBtree |
| 3125 | ** references. |
| 3126 | ** |
| 3127 | ** Every cursor is tripped, including cursors that belong |
| 3128 | ** to other database connections that happen to be sharing |
| 3129 | ** the cache with pBtree. |
| 3130 | ** |
| 3131 | ** This routine gets called when a rollback occurs. |
| 3132 | ** All cursors using the same cache must be tripped |
| 3133 | ** to prevent them from trying to use the btree after |
| 3134 | ** the rollback. The rollback may have deleted tables |
| 3135 | ** or moved root pages, so it is not sufficient to |
| 3136 | ** save the state of the cursor. The cursor must be |
| 3137 | ** invalidated. |
| 3138 | */ |
| 3139 | void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){ |
| 3140 | BtCursor *p; |
| 3141 | sqlite3BtreeEnter(pBtree); |
| 3142 | for(p=pBtree->pBt->pCursor; p; p=p->pNext){ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 3143 | int i; |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 3144 | sqlite3BtreeClearCursor(p); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 3145 | p->eState = CURSOR_FAULT; |
drh | 4c301aa | 2009-07-15 17:25:45 +0000 | [diff] [blame] | 3146 | p->skipNext = errCode; |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 3147 | for(i=0; i<=p->iPage; i++){ |
| 3148 | releasePage(p->apPage[i]); |
| 3149 | p->apPage[i] = 0; |
| 3150 | } |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 3151 | } |
| 3152 | sqlite3BtreeLeave(pBtree); |
| 3153 | } |
| 3154 | |
| 3155 | /* |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3156 | ** Rollback the transaction in progress. All cursors will be |
| 3157 | ** invalided by this operation. Any attempt to use a cursor |
| 3158 | ** that was open at the beginning of this operation will result |
| 3159 | ** in an error. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3160 | ** |
| 3161 | ** This will release the write lock on the database file. If there |
| 3162 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3163 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3164 | int sqlite3BtreeRollback(Btree *p){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 3165 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3166 | BtShared *pBt = p->pBt; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3167 | MemPage *pPage1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3168 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3169 | sqlite3BtreeEnter(p); |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 3170 | rc = saveAllCursors(pBt, 0, 0); |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 3171 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 3172 | if( rc!=SQLITE_OK ){ |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 3173 | /* This is a horrible situation. An IO or malloc() error occurred whilst |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 3174 | ** trying to save cursor positions. If this is an automatic rollback (as |
| 3175 | ** the result of a constraint, malloc() failure or IO error) then |
| 3176 | ** the cache may be internally inconsistent (not contain valid trees) so |
| 3177 | ** we cannot simply return the error to the caller. Instead, abort |
| 3178 | ** all queries that may be using any of the cursors that failed to save. |
| 3179 | */ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 3180 | sqlite3BtreeTripAllCursors(p, rc); |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 3181 | } |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 3182 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3183 | btreeIntegrity(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3184 | |
| 3185 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 3186 | int rc2; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3187 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 3188 | assert( TRANS_WRITE==pBt->inTransaction ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3189 | rc2 = sqlite3PagerRollback(pBt->pPager); |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 3190 | if( rc2!=SQLITE_OK ){ |
| 3191 | rc = rc2; |
| 3192 | } |
| 3193 | |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3194 | /* The rollback may have destroyed the pPage1->aData value. So |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 3195 | ** call btreeGetPage() on page 1 again to make |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3196 | ** sure pPage1->aData is set correctly. */ |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 3197 | if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3198 | releasePage(pPage1); |
| 3199 | } |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 3200 | assert( countWriteCursors(pBt)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3201 | pBt->inTransaction = TRANS_READ; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3202 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3203 | |
danielk1977 | 94b3073 | 2009-07-02 17:21:57 +0000 | [diff] [blame] | 3204 | btreeEndTransaction(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3205 | sqlite3BtreeLeave(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3206 | return rc; |
| 3207 | } |
| 3208 | |
| 3209 | /* |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 3210 | ** Start a statement subtransaction. The subtransaction can can be rolled |
| 3211 | ** back independently of the main transaction. You must start a transaction |
| 3212 | ** before starting a subtransaction. The subtransaction is ended automatically |
| 3213 | ** if the main transaction commits or rolls back. |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 3214 | ** |
| 3215 | ** Statement subtransactions are used around individual SQL statements |
| 3216 | ** that are contained within a BEGIN...COMMIT block. If a constraint |
| 3217 | ** error occurs within the statement, the effect of that one statement |
| 3218 | ** can be rolled back without having to rollback the entire transaction. |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 3219 | ** |
| 3220 | ** A statement sub-transaction is implemented as an anonymous savepoint. The |
| 3221 | ** value passed as the second parameter is the total number of savepoints, |
| 3222 | ** including the new anonymous savepoint, open on the B-Tree. i.e. if there |
| 3223 | ** are no active savepoints and no other statement-transactions open, |
| 3224 | ** iStatement is 1. This anonymous savepoint can be released or rolled back |
| 3225 | ** using the sqlite3BtreeSavepoint() function. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 3226 | */ |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 3227 | int sqlite3BtreeBeginStmt(Btree *p, int iStatement){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 3228 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3229 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3230 | sqlite3BtreeEnter(p); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 3231 | assert( p->inTrans==TRANS_WRITE ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 3232 | assert( pBt->readOnly==0 ); |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 3233 | assert( iStatement>0 ); |
| 3234 | assert( iStatement>p->db->nSavepoint ); |
| 3235 | if( NEVER(p->inTrans!=TRANS_WRITE || pBt->readOnly) ){ |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 3236 | rc = SQLITE_INTERNAL; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3237 | }else{ |
| 3238 | assert( pBt->inTransaction==TRANS_WRITE ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 3239 | /* At the pager level, a statement transaction is a savepoint with |
| 3240 | ** an index greater than all savepoints created explicitly using |
| 3241 | ** SQL statements. It is illegal to open, release or rollback any |
| 3242 | ** such savepoints while the statement transaction savepoint is active. |
| 3243 | */ |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 3244 | rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement); |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 3245 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3246 | sqlite3BtreeLeave(p); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 3247 | return rc; |
| 3248 | } |
| 3249 | |
| 3250 | /* |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3251 | ** The second argument to this function, op, is always SAVEPOINT_ROLLBACK |
| 3252 | ** or SAVEPOINT_RELEASE. This function either releases or rolls back the |
danielk1977 | 12dd549 | 2008-12-18 15:45:07 +0000 | [diff] [blame] | 3253 | ** savepoint identified by parameter iSavepoint, depending on the value |
| 3254 | ** of op. |
| 3255 | ** |
| 3256 | ** Normally, iSavepoint is greater than or equal to zero. However, if op is |
| 3257 | ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the |
| 3258 | ** contents of the entire transaction are rolled back. This is different |
| 3259 | ** from a normal transaction rollback, as no locks are released and the |
| 3260 | ** transaction remains open. |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3261 | */ |
| 3262 | int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){ |
| 3263 | int rc = SQLITE_OK; |
| 3264 | if( p && p->inTrans==TRANS_WRITE ){ |
| 3265 | BtShared *pBt = p->pBt; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3266 | assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK ); |
| 3267 | assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) ); |
| 3268 | sqlite3BtreeEnter(p); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3269 | rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint); |
drh | 9f0bbf9 | 2009-01-02 21:08:09 +0000 | [diff] [blame] | 3270 | if( rc==SQLITE_OK ){ |
| 3271 | rc = newDatabase(pBt); |
| 3272 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3273 | sqlite3BtreeLeave(p); |
| 3274 | } |
| 3275 | return rc; |
| 3276 | } |
| 3277 | |
| 3278 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3279 | ** Create a new cursor for the BTree whose root is on the page |
danielk1977 | 3e8add9 | 2009-07-04 17:16:00 +0000 | [diff] [blame] | 3280 | ** iTable. If a read-only cursor is requested, it is assumed that |
| 3281 | ** the caller already has at least a read-only transaction open |
| 3282 | ** on the database already. If a write-cursor is requested, then |
| 3283 | ** the caller is assumed to have an open write transaction. |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 3284 | ** |
| 3285 | ** If wrFlag==0, then the cursor can only be used for reading. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3286 | ** If wrFlag==1, then the cursor can be used for reading or for |
| 3287 | ** writing if other conditions for writing are also met. These |
| 3288 | ** are the conditions that must be met in order for writing to |
| 3289 | ** be allowed: |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 3290 | ** |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3291 | ** 1: The cursor must have been opened with wrFlag==1 |
| 3292 | ** |
drh | fe5d71d | 2007-03-19 11:54:10 +0000 | [diff] [blame] | 3293 | ** 2: Other database connections that share the same pager cache |
| 3294 | ** but which are not in the READ_UNCOMMITTED state may not have |
| 3295 | ** cursors open with wrFlag==0 on the same table. Otherwise |
| 3296 | ** the changes made by this write cursor would be visible to |
| 3297 | ** the read cursors in the other database connection. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3298 | ** |
| 3299 | ** 3: The database must be writable (not on read-only media) |
| 3300 | ** |
| 3301 | ** 4: There must be an active transaction. |
| 3302 | ** |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 3303 | ** No checking is done to make sure that page iTable really is the |
| 3304 | ** root page of a b-tree. If it is not, then the cursor acquired |
| 3305 | ** will not work correctly. |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3306 | ** |
drh | f25a507 | 2009-11-18 23:01:25 +0000 | [diff] [blame] | 3307 | ** It is assumed that the sqlite3BtreeCursorZero() has been called |
| 3308 | ** on pCur to initialize the memory space prior to invoking this routine. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3309 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3310 | static int btreeCursor( |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3311 | Btree *p, /* The btree */ |
| 3312 | int iTable, /* Root page of table to open */ |
| 3313 | int wrFlag, /* 1 to write. 0 read-only */ |
| 3314 | struct KeyInfo *pKeyInfo, /* First arg to comparison function */ |
| 3315 | BtCursor *pCur /* Space for new cursor */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3316 | ){ |
danielk1977 | 3e8add9 | 2009-07-04 17:16:00 +0000 | [diff] [blame] | 3317 | BtShared *pBt = p->pBt; /* Shared b-tree handle */ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3318 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3319 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3320 | assert( wrFlag==0 || wrFlag==1 ); |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 3321 | |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 3322 | /* The following assert statements verify that if this is a sharable |
| 3323 | ** b-tree database, the connection is holding the required table locks, |
| 3324 | ** and that no other connection has any open cursor that conflicts with |
| 3325 | ** this lock. */ |
| 3326 | assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, wrFlag+1) ); |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 3327 | assert( wrFlag==0 || !hasReadConflicts(p, iTable) ); |
| 3328 | |
danielk1977 | 3e8add9 | 2009-07-04 17:16:00 +0000 | [diff] [blame] | 3329 | /* Assert that the caller has opened the required transaction. */ |
| 3330 | assert( p->inTrans>TRANS_NONE ); |
| 3331 | assert( wrFlag==0 || p->inTrans==TRANS_WRITE ); |
| 3332 | assert( pBt->pPage1 && pBt->pPage1->aData ); |
| 3333 | |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 3334 | if( NEVER(wrFlag && pBt->readOnly) ){ |
| 3335 | return SQLITE_READONLY; |
drh | a0c9a11 | 2004-03-10 13:42:37 +0000 | [diff] [blame] | 3336 | } |
danielk1977 | 3e8add9 | 2009-07-04 17:16:00 +0000 | [diff] [blame] | 3337 | if( iTable==1 && pagerPagecount(pBt)==0 ){ |
| 3338 | return SQLITE_EMPTY; |
| 3339 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3340 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3341 | /* Now that no other errors can occur, finish filling in the BtCursor |
danielk1977 | 3e8add9 | 2009-07-04 17:16:00 +0000 | [diff] [blame] | 3342 | ** variables and link the cursor into the BtShared list. */ |
danielk1977 | 172114a | 2009-07-07 15:47:12 +0000 | [diff] [blame] | 3343 | pCur->pgnoRoot = (Pgno)iTable; |
| 3344 | pCur->iPage = -1; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3345 | pCur->pKeyInfo = pKeyInfo; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3346 | pCur->pBtree = p; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 3347 | pCur->pBt = pBt; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3348 | pCur->wrFlag = (u8)wrFlag; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3349 | pCur->pNext = pBt->pCursor; |
| 3350 | if( pCur->pNext ){ |
| 3351 | pCur->pNext->pPrev = pCur; |
| 3352 | } |
| 3353 | pBt->pCursor = pCur; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3354 | pCur->eState = CURSOR_INVALID; |
drh | 7f75122 | 2009-03-17 22:33:00 +0000 | [diff] [blame] | 3355 | pCur->cachedRowid = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3356 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3357 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3358 | int sqlite3BtreeCursor( |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3359 | Btree *p, /* The btree */ |
| 3360 | int iTable, /* Root page of table to open */ |
| 3361 | int wrFlag, /* 1 to write. 0 read-only */ |
| 3362 | struct KeyInfo *pKeyInfo, /* First arg to xCompare() */ |
| 3363 | BtCursor *pCur /* Write new cursor here */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3364 | ){ |
| 3365 | int rc; |
| 3366 | sqlite3BtreeEnter(p); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3367 | rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3368 | sqlite3BtreeLeave(p); |
| 3369 | return rc; |
| 3370 | } |
drh | 7f75122 | 2009-03-17 22:33:00 +0000 | [diff] [blame] | 3371 | |
| 3372 | /* |
| 3373 | ** Return the size of a BtCursor object in bytes. |
| 3374 | ** |
| 3375 | ** This interfaces is needed so that users of cursors can preallocate |
| 3376 | ** sufficient storage to hold a cursor. The BtCursor object is opaque |
| 3377 | ** to users so they cannot do the sizeof() themselves - they must call |
| 3378 | ** this routine. |
| 3379 | */ |
| 3380 | int sqlite3BtreeCursorSize(void){ |
drh | c54055b | 2009-11-13 17:05:53 +0000 | [diff] [blame] | 3381 | return ROUND8(sizeof(BtCursor)); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3382 | } |
| 3383 | |
drh | 7f75122 | 2009-03-17 22:33:00 +0000 | [diff] [blame] | 3384 | /* |
drh | f25a507 | 2009-11-18 23:01:25 +0000 | [diff] [blame] | 3385 | ** Initialize memory that will be converted into a BtCursor object. |
| 3386 | ** |
| 3387 | ** The simple approach here would be to memset() the entire object |
| 3388 | ** to zero. But it turns out that the apPage[] and aiIdx[] arrays |
| 3389 | ** do not need to be zeroed and they are large, so we can save a lot |
| 3390 | ** of run-time by skipping the initialization of those elements. |
| 3391 | */ |
| 3392 | void sqlite3BtreeCursorZero(BtCursor *p){ |
| 3393 | memset(p, 0, offsetof(BtCursor, iPage)); |
| 3394 | } |
| 3395 | |
| 3396 | /* |
drh | 7f75122 | 2009-03-17 22:33:00 +0000 | [diff] [blame] | 3397 | ** Set the cached rowid value of every cursor in the same database file |
| 3398 | ** as pCur and having the same root page number as pCur. The value is |
| 3399 | ** set to iRowid. |
| 3400 | ** |
| 3401 | ** Only positive rowid values are considered valid for this cache. |
| 3402 | ** The cache is initialized to zero, indicating an invalid cache. |
| 3403 | ** A btree will work fine with zero or negative rowids. We just cannot |
| 3404 | ** cache zero or negative rowids, which means tables that use zero or |
| 3405 | ** negative rowids might run a little slower. But in practice, zero |
| 3406 | ** or negative rowids are very uncommon so this should not be a problem. |
| 3407 | */ |
| 3408 | void sqlite3BtreeSetCachedRowid(BtCursor *pCur, sqlite3_int64 iRowid){ |
| 3409 | BtCursor *p; |
| 3410 | for(p=pCur->pBt->pCursor; p; p=p->pNext){ |
| 3411 | if( p->pgnoRoot==pCur->pgnoRoot ) p->cachedRowid = iRowid; |
| 3412 | } |
| 3413 | assert( pCur->cachedRowid==iRowid ); |
| 3414 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3415 | |
drh | 7f75122 | 2009-03-17 22:33:00 +0000 | [diff] [blame] | 3416 | /* |
| 3417 | ** Return the cached rowid for the given cursor. A negative or zero |
| 3418 | ** return value indicates that the rowid cache is invalid and should be |
| 3419 | ** ignored. If the rowid cache has never before been set, then a |
| 3420 | ** zero is returned. |
| 3421 | */ |
| 3422 | sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor *pCur){ |
| 3423 | return pCur->cachedRowid; |
| 3424 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3425 | |
| 3426 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3427 | ** Close a cursor. The read lock on the database file is released |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3428 | ** when the last cursor is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3429 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3430 | int sqlite3BtreeCloseCursor(BtCursor *pCur){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 3431 | Btree *pBtree = pCur->pBtree; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3432 | if( pBtree ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3433 | int i; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3434 | BtShared *pBt = pCur->pBt; |
| 3435 | sqlite3BtreeEnter(pBtree); |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 3436 | sqlite3BtreeClearCursor(pCur); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3437 | if( pCur->pPrev ){ |
| 3438 | pCur->pPrev->pNext = pCur->pNext; |
| 3439 | }else{ |
| 3440 | pBt->pCursor = pCur->pNext; |
| 3441 | } |
| 3442 | if( pCur->pNext ){ |
| 3443 | pCur->pNext->pPrev = pCur->pPrev; |
| 3444 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3445 | for(i=0; i<=pCur->iPage; i++){ |
| 3446 | releasePage(pCur->apPage[i]); |
| 3447 | } |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3448 | unlockBtreeIfUnused(pBt); |
| 3449 | invalidateOverflowCache(pCur); |
| 3450 | /* sqlite3_free(pCur); */ |
| 3451 | sqlite3BtreeLeave(pBtree); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3452 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3453 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3454 | } |
| 3455 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3456 | /* |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3457 | ** Make sure the BtCursor* given in the argument has a valid |
| 3458 | ** BtCursor.info structure. If it is not already valid, call |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 3459 | ** btreeParseCell() to fill it in. |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 3460 | ** |
| 3461 | ** BtCursor.info is a cache of the information in the current cell. |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 3462 | ** Using this cache reduces the number of calls to btreeParseCell(). |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3463 | ** |
| 3464 | ** 2007-06-25: There is a bug in some versions of MSVC that cause the |
| 3465 | ** compiler to crash when getCellInfo() is implemented as a macro. |
| 3466 | ** But there is a measureable speed advantage to using the macro on gcc |
| 3467 | ** (when less compiler optimizations like -Os or -O0 are used and the |
| 3468 | ** compiler is not doing agressive inlining.) So we use a real function |
| 3469 | ** for MSVC and a macro for everything else. Ticket #2457. |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3470 | */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3471 | #ifndef NDEBUG |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3472 | static void assertCellInfo(BtCursor *pCur){ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3473 | CellInfo info; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3474 | int iPage = pCur->iPage; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 3475 | memset(&info, 0, sizeof(info)); |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 3476 | btreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3477 | assert( memcmp(&info, &pCur->info, sizeof(info))==0 ); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3478 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3479 | #else |
| 3480 | #define assertCellInfo(x) |
| 3481 | #endif |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3482 | #ifdef _MSC_VER |
| 3483 | /* Use a real function in MSVC to work around bugs in that compiler. */ |
| 3484 | static void getCellInfo(BtCursor *pCur){ |
| 3485 | if( pCur->info.nSize==0 ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3486 | int iPage = pCur->iPage; |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 3487 | btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3488 | pCur->validNKey = 1; |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3489 | }else{ |
| 3490 | assertCellInfo(pCur); |
| 3491 | } |
| 3492 | } |
| 3493 | #else /* if not _MSC_VER */ |
| 3494 | /* Use a macro in all other compilers so that the function is inlined */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3495 | #define getCellInfo(pCur) \ |
| 3496 | if( pCur->info.nSize==0 ){ \ |
| 3497 | int iPage = pCur->iPage; \ |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 3498 | btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); \ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3499 | pCur->validNKey = 1; \ |
| 3500 | }else{ \ |
| 3501 | assertCellInfo(pCur); \ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3502 | } |
| 3503 | #endif /* _MSC_VER */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3504 | |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 3505 | #ifndef NDEBUG /* The next routine used only within assert() statements */ |
| 3506 | /* |
| 3507 | ** Return true if the given BtCursor is valid. A valid cursor is one |
| 3508 | ** that is currently pointing to a row in a (non-empty) table. |
| 3509 | ** This is a verification routine is used only within assert() statements. |
| 3510 | */ |
| 3511 | int sqlite3BtreeCursorIsValid(BtCursor *pCur){ |
| 3512 | return pCur && pCur->eState==CURSOR_VALID; |
| 3513 | } |
| 3514 | #endif /* NDEBUG */ |
| 3515 | |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3516 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3517 | ** Set *pSize to the size of the buffer needed to hold the value of |
| 3518 | ** the key for the current entry. If the cursor is not pointing |
| 3519 | ** to a valid entry, *pSize is set to 0. |
| 3520 | ** |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3521 | ** For a table with the INTKEY flag set, this routine returns the key |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3522 | ** itself, not the number of bytes in the key. |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 3523 | ** |
| 3524 | ** The caller must position the cursor prior to invoking this routine. |
| 3525 | ** |
| 3526 | ** This routine cannot fail. It always returns SQLITE_OK. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3527 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3528 | int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3529 | assert( cursorHoldsMutex(pCur) ); |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 3530 | assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID ); |
| 3531 | if( pCur->eState!=CURSOR_VALID ){ |
| 3532 | *pSize = 0; |
| 3533 | }else{ |
| 3534 | getCellInfo(pCur); |
| 3535 | *pSize = pCur->info.nKey; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3536 | } |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 3537 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3538 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3539 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3540 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3541 | ** Set *pSize to the number of bytes of data in the entry the |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 3542 | ** cursor currently points to. |
| 3543 | ** |
| 3544 | ** The caller must guarantee that the cursor is pointing to a non-NULL |
| 3545 | ** valid entry. In other words, the calling procedure must guarantee |
| 3546 | ** that the cursor has Cursor.eState==CURSOR_VALID. |
| 3547 | ** |
| 3548 | ** Failure is not possible. This function always returns SQLITE_OK. |
| 3549 | ** It might just as well be a procedure (returning void) but we continue |
| 3550 | ** to return an integer result code for historical reasons. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3551 | */ |
| 3552 | int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3553 | assert( cursorHoldsMutex(pCur) ); |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 3554 | assert( pCur->eState==CURSOR_VALID ); |
| 3555 | getCellInfo(pCur); |
| 3556 | *pSize = pCur->info.nData; |
| 3557 | return SQLITE_OK; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3558 | } |
| 3559 | |
| 3560 | /* |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3561 | ** Given the page number of an overflow page in the database (parameter |
| 3562 | ** ovfl), this function finds the page number of the next page in the |
| 3563 | ** linked list of overflow pages. If possible, it uses the auto-vacuum |
| 3564 | ** pointer-map data instead of reading the content of page ovfl to do so. |
| 3565 | ** |
| 3566 | ** If an error occurs an SQLite error code is returned. Otherwise: |
| 3567 | ** |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3568 | ** The page number of the next overflow page in the linked list is |
| 3569 | ** written to *pPgnoNext. If page ovfl is the last page in its linked |
| 3570 | ** list, *pPgnoNext is set to zero. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3571 | ** |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3572 | ** If ppPage is not NULL, and a reference to the MemPage object corresponding |
| 3573 | ** to page number pOvfl was obtained, then *ppPage is set to point to that |
| 3574 | ** reference. It is the responsibility of the caller to call releasePage() |
| 3575 | ** on *ppPage to free the reference. In no reference was obtained (because |
| 3576 | ** the pointer-map was used to obtain the value for *pPgnoNext), then |
| 3577 | ** *ppPage is set to zero. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3578 | */ |
| 3579 | static int getOverflowPage( |
drh | fa3be90 | 2009-07-07 02:44:07 +0000 | [diff] [blame] | 3580 | BtShared *pBt, /* The database file */ |
| 3581 | Pgno ovfl, /* Current overflow page number */ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3582 | MemPage **ppPage, /* OUT: MemPage handle (may be NULL) */ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3583 | Pgno *pPgnoNext /* OUT: Next overflow page number */ |
| 3584 | ){ |
| 3585 | Pgno next = 0; |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3586 | MemPage *pPage = 0; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 3587 | int rc = SQLITE_OK; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3588 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3589 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3590 | assert(pPgnoNext); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3591 | |
| 3592 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3593 | /* Try to find the next page in the overflow list using the |
| 3594 | ** autovacuum pointer-map pages. Guess that the next page in |
| 3595 | ** the overflow list is page number (ovfl+1). If that guess turns |
| 3596 | ** out to be wrong, fall back to loading the data of page |
| 3597 | ** number ovfl to determine the next page number. |
| 3598 | */ |
| 3599 | if( pBt->autoVacuum ){ |
| 3600 | Pgno pgno; |
| 3601 | Pgno iGuess = ovfl+1; |
| 3602 | u8 eType; |
| 3603 | |
| 3604 | while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){ |
| 3605 | iGuess++; |
| 3606 | } |
| 3607 | |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 3608 | if( iGuess<=pagerPagecount(pBt) ){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3609 | rc = ptrmapGet(pBt, iGuess, &eType, &pgno); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3610 | if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3611 | next = iGuess; |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3612 | rc = SQLITE_DONE; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3613 | } |
| 3614 | } |
| 3615 | } |
| 3616 | #endif |
| 3617 | |
danielk1977 | d8a3f3d | 2009-07-11 11:45:23 +0000 | [diff] [blame] | 3618 | assert( next==0 || rc==SQLITE_DONE ); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3619 | if( rc==SQLITE_OK ){ |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 3620 | rc = btreeGetPage(pBt, ovfl, &pPage, 0); |
danielk1977 | d8a3f3d | 2009-07-11 11:45:23 +0000 | [diff] [blame] | 3621 | assert( rc==SQLITE_OK || pPage==0 ); |
| 3622 | if( rc==SQLITE_OK ){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3623 | next = get4byte(pPage->aData); |
| 3624 | } |
danielk1977 | 443c059 | 2009-01-16 15:21:05 +0000 | [diff] [blame] | 3625 | } |
danielk1977 | 45d6882 | 2009-01-16 16:23:38 +0000 | [diff] [blame] | 3626 | |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3627 | *pPgnoNext = next; |
| 3628 | if( ppPage ){ |
| 3629 | *ppPage = pPage; |
| 3630 | }else{ |
| 3631 | releasePage(pPage); |
| 3632 | } |
| 3633 | return (rc==SQLITE_DONE ? SQLITE_OK : rc); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3634 | } |
| 3635 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3636 | /* |
| 3637 | ** Copy data from a buffer to a page, or from a page to a buffer. |
| 3638 | ** |
| 3639 | ** pPayload is a pointer to data stored on database page pDbPage. |
| 3640 | ** If argument eOp is false, then nByte bytes of data are copied |
| 3641 | ** from pPayload to the buffer pointed at by pBuf. If eOp is true, |
| 3642 | ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes |
| 3643 | ** of data are copied from the buffer pBuf to pPayload. |
| 3644 | ** |
| 3645 | ** SQLITE_OK is returned on success, otherwise an error code. |
| 3646 | */ |
| 3647 | static int copyPayload( |
| 3648 | void *pPayload, /* Pointer to page data */ |
| 3649 | void *pBuf, /* Pointer to buffer */ |
| 3650 | int nByte, /* Number of bytes to copy */ |
| 3651 | int eOp, /* 0 -> copy from page, 1 -> copy to page */ |
| 3652 | DbPage *pDbPage /* Page containing pPayload */ |
| 3653 | ){ |
| 3654 | if( eOp ){ |
| 3655 | /* Copy data from buffer to page (a write operation) */ |
| 3656 | int rc = sqlite3PagerWrite(pDbPage); |
| 3657 | if( rc!=SQLITE_OK ){ |
| 3658 | return rc; |
| 3659 | } |
| 3660 | memcpy(pPayload, pBuf, nByte); |
| 3661 | }else{ |
| 3662 | /* Copy data from page to buffer (a read operation) */ |
| 3663 | memcpy(pBuf, pPayload, nByte); |
| 3664 | } |
| 3665 | return SQLITE_OK; |
| 3666 | } |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3667 | |
| 3668 | /* |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3669 | ** This function is used to read or overwrite payload information |
| 3670 | ** for the entry that the pCur cursor is pointing to. If the eOp |
| 3671 | ** parameter is 0, this is a read operation (data copied into |
| 3672 | ** buffer pBuf). If it is non-zero, a write (data copied from |
| 3673 | ** buffer pBuf). |
| 3674 | ** |
| 3675 | ** A total of "amt" bytes are read or written beginning at "offset". |
| 3676 | ** Data is read to or from the buffer pBuf. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3677 | ** |
drh | 3bcdfd2 | 2009-07-12 02:32:21 +0000 | [diff] [blame] | 3678 | ** The content being read or written might appear on the main page |
| 3679 | ** or be scattered out on multiple overflow pages. |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3680 | ** |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 3681 | ** If the BtCursor.isIncrblobHandle flag is set, and the current |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3682 | ** cursor entry uses one or more overflow pages, this function |
| 3683 | ** allocates space for and lazily popluates the overflow page-list |
| 3684 | ** cache array (BtCursor.aOverflow). Subsequent calls use this |
| 3685 | ** cache to make seeking to the supplied offset more efficient. |
| 3686 | ** |
| 3687 | ** Once an overflow page-list cache has been allocated, it may be |
| 3688 | ** invalidated if some other cursor writes to the same table, or if |
| 3689 | ** the cursor is moved to a different row. Additionally, in auto-vacuum |
| 3690 | ** mode, the following events may invalidate an overflow page-list cache. |
| 3691 | ** |
| 3692 | ** * An incremental vacuum, |
| 3693 | ** * A commit in auto_vacuum="full" mode, |
| 3694 | ** * Creating a table (may require moving an overflow page). |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3695 | */ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3696 | static int accessPayload( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3697 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 3698 | u32 offset, /* Begin reading this far into payload */ |
| 3699 | u32 amt, /* Read this many bytes */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3700 | unsigned char *pBuf, /* Write the bytes into this buffer */ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3701 | int eOp /* zero to read. non-zero to write. */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3702 | ){ |
| 3703 | unsigned char *aPayload; |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3704 | int rc = SQLITE_OK; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3705 | u32 nKey; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3706 | int iIdx = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3707 | MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */ |
danielk1977 | 0d06541 | 2008-11-12 18:21:36 +0000 | [diff] [blame] | 3708 | BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3709 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3710 | assert( pPage ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3711 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3712 | assert( pCur->aiIdx[pCur->iPage]<pPage->nCell ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3713 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3714 | |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3715 | getCellInfo(pCur); |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3716 | aPayload = pCur->info.pCell + pCur->info.nHeader; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3717 | nKey = (pPage->intKey ? 0 : (int)pCur->info.nKey); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3718 | |
drh | 3bcdfd2 | 2009-07-12 02:32:21 +0000 | [diff] [blame] | 3719 | if( NEVER(offset+amt > nKey+pCur->info.nData) |
danielk1977 | 0d06541 | 2008-11-12 18:21:36 +0000 | [diff] [blame] | 3720 | || &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize] |
| 3721 | ){ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3722 | /* 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] | 3723 | return SQLITE_CORRUPT_BKPT; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3724 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3725 | |
| 3726 | /* Check if data must be read/written to/from the btree page itself. */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3727 | if( offset<pCur->info.nLocal ){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3728 | int a = amt; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3729 | if( a+offset>pCur->info.nLocal ){ |
| 3730 | a = pCur->info.nLocal - offset; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3731 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3732 | rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3733 | offset = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3734 | pBuf += a; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3735 | amt -= a; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 3736 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3737 | offset -= pCur->info.nLocal; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3738 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3739 | |
| 3740 | if( rc==SQLITE_OK && amt>0 ){ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 3741 | const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3742 | Pgno nextPage; |
| 3743 | |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3744 | nextPage = get4byte(&aPayload[pCur->info.nLocal]); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3745 | |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3746 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 3747 | /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[] |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3748 | ** has not been allocated, allocate it now. The array is sized at |
| 3749 | ** one entry for each overflow page in the overflow chain. The |
| 3750 | ** page number of the first overflow page is stored in aOverflow[0], |
| 3751 | ** etc. A value of 0 in the aOverflow[] array means "not yet known" |
| 3752 | ** (the cache is lazily populated). |
| 3753 | */ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 3754 | if( pCur->isIncrblobHandle && !pCur->aOverflow ){ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3755 | int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3756 | pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl); |
drh | 3bcdfd2 | 2009-07-12 02:32:21 +0000 | [diff] [blame] | 3757 | /* nOvfl is always positive. If it were zero, fetchPayload would have |
| 3758 | ** been used instead of this routine. */ |
| 3759 | if( ALWAYS(nOvfl) && !pCur->aOverflow ){ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3760 | rc = SQLITE_NOMEM; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3761 | } |
| 3762 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3763 | |
| 3764 | /* If the overflow page-list cache has been allocated and the |
| 3765 | ** entry for the first required overflow page is valid, skip |
| 3766 | ** directly to it. |
| 3767 | */ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3768 | if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){ |
| 3769 | iIdx = (offset/ovflSize); |
| 3770 | nextPage = pCur->aOverflow[iIdx]; |
| 3771 | offset = (offset%ovflSize); |
| 3772 | } |
| 3773 | #endif |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3774 | |
| 3775 | for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){ |
| 3776 | |
| 3777 | #ifndef SQLITE_OMIT_INCRBLOB |
| 3778 | /* If required, populate the overflow page-list cache. */ |
| 3779 | if( pCur->aOverflow ){ |
| 3780 | assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage); |
| 3781 | pCur->aOverflow[iIdx] = nextPage; |
| 3782 | } |
| 3783 | #endif |
| 3784 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3785 | if( offset>=ovflSize ){ |
| 3786 | /* The only reason to read this page is to obtain the page |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3787 | ** number for the next page in the overflow chain. The page |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 3788 | ** data is not required. So first try to lookup the overflow |
| 3789 | ** page-list cache, if any, then fall back to the getOverflowPage() |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3790 | ** function. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3791 | */ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3792 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3793 | if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){ |
| 3794 | nextPage = pCur->aOverflow[iIdx+1]; |
| 3795 | } else |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3796 | #endif |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3797 | rc = getOverflowPage(pBt, nextPage, 0, &nextPage); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3798 | offset -= ovflSize; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3799 | }else{ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3800 | /* Need to read this page properly. It contains some of the |
| 3801 | ** range of data that is being read (eOp==0) or written (eOp!=0). |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3802 | */ |
| 3803 | DbPage *pDbPage; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3804 | int a = amt; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3805 | rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3806 | if( rc==SQLITE_OK ){ |
| 3807 | aPayload = sqlite3PagerGetData(pDbPage); |
| 3808 | nextPage = get4byte(aPayload); |
| 3809 | if( a + offset > ovflSize ){ |
| 3810 | a = ovflSize - offset; |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3811 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3812 | rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage); |
| 3813 | sqlite3PagerUnref(pDbPage); |
| 3814 | offset = 0; |
| 3815 | amt -= a; |
| 3816 | pBuf += a; |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3817 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3818 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3819 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3820 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3821 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3822 | if( rc==SQLITE_OK && amt>0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3823 | return SQLITE_CORRUPT_BKPT; |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 3824 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3825 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3826 | } |
| 3827 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3828 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3829 | ** Read part of the key associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3830 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3831 | ** begins at "offset". |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3832 | ** |
drh | 5d1a872 | 2009-07-22 18:07:40 +0000 | [diff] [blame] | 3833 | ** The caller must ensure that pCur is pointing to a valid row |
| 3834 | ** in the table. |
| 3835 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3836 | ** Return SQLITE_OK on success or an error code if anything goes |
| 3837 | ** wrong. An error is returned if "offset+amt" is larger than |
| 3838 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3839 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3840 | int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3841 | assert( cursorHoldsMutex(pCur) ); |
drh | 5d1a872 | 2009-07-22 18:07:40 +0000 | [diff] [blame] | 3842 | assert( pCur->eState==CURSOR_VALID ); |
| 3843 | assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] ); |
| 3844 | assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell ); |
| 3845 | return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3846 | } |
| 3847 | |
| 3848 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3849 | ** Read part of the data associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3850 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3851 | ** begins at "offset". |
| 3852 | ** |
| 3853 | ** Return SQLITE_OK on success or an error code if anything goes |
| 3854 | ** wrong. An error is returned if "offset+amt" is larger than |
| 3855 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3856 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3857 | int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3858 | int rc; |
| 3859 | |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 3860 | #ifndef SQLITE_OMIT_INCRBLOB |
| 3861 | if ( pCur->eState==CURSOR_INVALID ){ |
| 3862 | return SQLITE_ABORT; |
| 3863 | } |
| 3864 | #endif |
| 3865 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3866 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 3867 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3868 | if( rc==SQLITE_OK ){ |
| 3869 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3870 | assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] ); |
| 3871 | assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell ); |
drh | fb19268 | 2009-07-11 18:26:28 +0000 | [diff] [blame] | 3872 | rc = accessPayload(pCur, offset, amt, pBuf, 0); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3873 | } |
| 3874 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3875 | } |
| 3876 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3877 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3878 | ** Return a pointer to payload information from the entry that the |
| 3879 | ** pCur cursor is pointing to. The pointer is to the beginning of |
| 3880 | ** 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] | 3881 | ** skipKey==1. The number of bytes of available key/data is written |
| 3882 | ** into *pAmt. If *pAmt==0, then the value returned will not be |
| 3883 | ** a valid pointer. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3884 | ** |
| 3885 | ** This routine is an optimization. It is common for the entire key |
| 3886 | ** and data to fit on the local page and for there to be no overflow |
| 3887 | ** pages. When that is so, this routine can be used to access the |
| 3888 | ** 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] | 3889 | ** onto overflow pages, then accessPayload() must be used to reassemble |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3890 | ** the key/data and copy it into a preallocated buffer. |
| 3891 | ** |
| 3892 | ** The pointer returned by this routine looks directly into the cached |
| 3893 | ** page of the database. The data might change or move the next time |
| 3894 | ** any btree routine is called. |
| 3895 | */ |
| 3896 | static const unsigned char *fetchPayload( |
| 3897 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3898 | int *pAmt, /* Write the number of available bytes here */ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3899 | int skipKey /* read beginning at data if this is true */ |
| 3900 | ){ |
| 3901 | unsigned char *aPayload; |
| 3902 | MemPage *pPage; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3903 | u32 nKey; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 3904 | u32 nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3905 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3906 | assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3907 | assert( pCur->eState==CURSOR_VALID ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3908 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3909 | pPage = pCur->apPage[pCur->iPage]; |
| 3910 | assert( pCur->aiIdx[pCur->iPage]<pPage->nCell ); |
drh | fe3313f | 2009-07-21 19:02:20 +0000 | [diff] [blame] | 3911 | if( NEVER(pCur->info.nSize==0) ){ |
| 3912 | btreeParseCell(pCur->apPage[pCur->iPage], pCur->aiIdx[pCur->iPage], |
| 3913 | &pCur->info); |
| 3914 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3915 | aPayload = pCur->info.pCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3916 | aPayload += pCur->info.nHeader; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3917 | if( pPage->intKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3918 | nKey = 0; |
| 3919 | }else{ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3920 | nKey = (int)pCur->info.nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3921 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3922 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3923 | aPayload += nKey; |
| 3924 | nLocal = pCur->info.nLocal - nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3925 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3926 | nLocal = pCur->info.nLocal; |
drh | fe3313f | 2009-07-21 19:02:20 +0000 | [diff] [blame] | 3927 | assert( nLocal<=nKey ); |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3928 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3929 | *pAmt = nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3930 | return aPayload; |
| 3931 | } |
| 3932 | |
| 3933 | |
| 3934 | /* |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3935 | ** For the entry that cursor pCur is point to, return as |
| 3936 | ** many bytes of the key or data as are available on the local |
| 3937 | ** b-tree page. Write the number of available bytes into *pAmt. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3938 | ** |
| 3939 | ** The pointer returned is ephemeral. The key/data may move |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3940 | ** or be destroyed on the next call to any Btree routine, |
| 3941 | ** including calls from other threads against the same cache. |
| 3942 | ** Hence, a mutex on the BtShared should be held prior to calling |
| 3943 | ** this routine. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3944 | ** |
| 3945 | ** These routines is used to get quick access to key and data |
| 3946 | ** in the common case where no overflow pages are used. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3947 | */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3948 | const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){ |
drh | fe3313f | 2009-07-21 19:02:20 +0000 | [diff] [blame] | 3949 | const void *p = 0; |
danielk1977 | 4b0aa4c | 2009-05-28 11:05:57 +0000 | [diff] [blame] | 3950 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3951 | assert( cursorHoldsMutex(pCur) ); |
drh | fe3313f | 2009-07-21 19:02:20 +0000 | [diff] [blame] | 3952 | if( ALWAYS(pCur->eState==CURSOR_VALID) ){ |
| 3953 | p = (const void*)fetchPayload(pCur, pAmt, 0); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3954 | } |
drh | fe3313f | 2009-07-21 19:02:20 +0000 | [diff] [blame] | 3955 | return p; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3956 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3957 | const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){ |
drh | fe3313f | 2009-07-21 19:02:20 +0000 | [diff] [blame] | 3958 | const void *p = 0; |
danielk1977 | 4b0aa4c | 2009-05-28 11:05:57 +0000 | [diff] [blame] | 3959 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3960 | assert( cursorHoldsMutex(pCur) ); |
drh | fe3313f | 2009-07-21 19:02:20 +0000 | [diff] [blame] | 3961 | if( ALWAYS(pCur->eState==CURSOR_VALID) ){ |
| 3962 | p = (const void*)fetchPayload(pCur, pAmt, 1); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3963 | } |
drh | fe3313f | 2009-07-21 19:02:20 +0000 | [diff] [blame] | 3964 | return p; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3965 | } |
| 3966 | |
| 3967 | |
| 3968 | /* |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3969 | ** 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] | 3970 | ** page number of the child page to move to. |
danielk1977 | a299d61 | 2009-07-13 11:22:10 +0000 | [diff] [blame] | 3971 | ** |
| 3972 | ** This function returns SQLITE_CORRUPT if the page-header flags field of |
| 3973 | ** the new child page does not match the flags field of the parent (i.e. |
| 3974 | ** if an intkey page appears to be the parent of a non-intkey page, or |
| 3975 | ** vice-versa). |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3976 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3977 | static int moveToChild(BtCursor *pCur, u32 newPgno){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3978 | int rc; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3979 | int i = pCur->iPage; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3980 | MemPage *pNewPage; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 3981 | BtShared *pBt = pCur->pBt; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3982 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3983 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3984 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3985 | assert( pCur->iPage<BTCURSOR_MAX_DEPTH ); |
| 3986 | if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){ |
| 3987 | return SQLITE_CORRUPT_BKPT; |
| 3988 | } |
| 3989 | rc = getAndInitPage(pBt, newPgno, &pNewPage); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3990 | if( rc ) return rc; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3991 | pCur->apPage[i+1] = pNewPage; |
| 3992 | pCur->aiIdx[i+1] = 0; |
| 3993 | pCur->iPage++; |
| 3994 | |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3995 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3996 | pCur->validNKey = 0; |
danielk1977 | bd5969a | 2009-07-11 17:39:42 +0000 | [diff] [blame] | 3997 | if( pNewPage->nCell<1 || pNewPage->intKey!=pCur->apPage[i]->intKey ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3998 | return SQLITE_CORRUPT_BKPT; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 3999 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4000 | return SQLITE_OK; |
| 4001 | } |
| 4002 | |
danielk1977 | bf93c56 | 2008-09-29 15:53:25 +0000 | [diff] [blame] | 4003 | #ifndef NDEBUG |
| 4004 | /* |
| 4005 | ** Page pParent is an internal (non-leaf) tree page. This function |
| 4006 | ** asserts that page number iChild is the left-child if the iIdx'th |
| 4007 | ** cell in page pParent. Or, if iIdx is equal to the total number of |
| 4008 | ** cells in pParent, that page number iChild is the right-child of |
| 4009 | ** the page. |
| 4010 | */ |
| 4011 | static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){ |
| 4012 | assert( iIdx<=pParent->nCell ); |
| 4013 | if( iIdx==pParent->nCell ){ |
| 4014 | assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild ); |
| 4015 | }else{ |
| 4016 | assert( get4byte(findCell(pParent, iIdx))==iChild ); |
| 4017 | } |
| 4018 | } |
| 4019 | #else |
| 4020 | # define assertParentIndex(x,y,z) |
| 4021 | #endif |
| 4022 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4023 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4024 | ** Move the cursor up to the parent page. |
| 4025 | ** |
| 4026 | ** pCur->idx is set to the cell index that contains the pointer |
| 4027 | ** to the page we are coming from. If we are coming from the |
| 4028 | ** right-most child page then pCur->idx is set to one more than |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4029 | ** the largest cell index. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4030 | */ |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 4031 | static void moveToParent(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4032 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4033 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4034 | assert( pCur->iPage>0 ); |
| 4035 | assert( pCur->apPage[pCur->iPage] ); |
danielk1977 | bf93c56 | 2008-09-29 15:53:25 +0000 | [diff] [blame] | 4036 | assertParentIndex( |
| 4037 | pCur->apPage[pCur->iPage-1], |
| 4038 | pCur->aiIdx[pCur->iPage-1], |
| 4039 | pCur->apPage[pCur->iPage]->pgno |
| 4040 | ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4041 | releasePage(pCur->apPage[pCur->iPage]); |
| 4042 | pCur->iPage--; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 4043 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 4044 | pCur->validNKey = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4045 | } |
| 4046 | |
| 4047 | /* |
danielk1977 | 8f880a8 | 2009-07-13 09:41:45 +0000 | [diff] [blame] | 4048 | ** Move the cursor to point to the root page of its b-tree structure. |
| 4049 | ** |
| 4050 | ** If the table has a virtual root page, then the cursor is moved to point |
| 4051 | ** to the virtual root page instead of the actual root page. A table has a |
| 4052 | ** virtual root page when the actual root page contains no cells and a |
| 4053 | ** single child page. This can only happen with the table rooted at page 1. |
| 4054 | ** |
| 4055 | ** If the b-tree structure is empty, the cursor state is set to |
| 4056 | ** CURSOR_INVALID. Otherwise, the cursor is set to point to the first |
| 4057 | ** cell located on the root (or virtual root) page and the cursor state |
| 4058 | ** is set to CURSOR_VALID. |
| 4059 | ** |
| 4060 | ** If this function returns successfully, it may be assumed that the |
| 4061 | ** page-header flags indicate that the [virtual] root-page is the expected |
| 4062 | ** kind of b-tree page (i.e. if when opening the cursor the caller did not |
| 4063 | ** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D, |
| 4064 | ** indicating a table b-tree, or if the caller did specify a KeyInfo |
| 4065 | ** structure the flags byte is set to 0x02 or 0x0A, indicating an index |
| 4066 | ** b-tree). |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4067 | */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4068 | static int moveToRoot(BtCursor *pCur){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4069 | MemPage *pRoot; |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 4070 | int rc = SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4071 | Btree *p = pCur->pBtree; |
| 4072 | BtShared *pBt = p->pBt; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4073 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4074 | assert( cursorHoldsMutex(pCur) ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 4075 | assert( CURSOR_INVALID < CURSOR_REQUIRESEEK ); |
| 4076 | assert( CURSOR_VALID < CURSOR_REQUIRESEEK ); |
| 4077 | assert( CURSOR_FAULT > CURSOR_REQUIRESEEK ); |
| 4078 | if( pCur->eState>=CURSOR_REQUIRESEEK ){ |
| 4079 | if( pCur->eState==CURSOR_FAULT ){ |
drh | 4c301aa | 2009-07-15 17:25:45 +0000 | [diff] [blame] | 4080 | assert( pCur->skipNext!=SQLITE_OK ); |
| 4081 | return pCur->skipNext; |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 4082 | } |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 4083 | sqlite3BtreeClearCursor(pCur); |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 4084 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4085 | |
| 4086 | if( pCur->iPage>=0 ){ |
| 4087 | int i; |
| 4088 | for(i=1; i<=pCur->iPage; i++){ |
| 4089 | releasePage(pCur->apPage[i]); |
danielk1977 | d9f6c53 | 2008-09-19 16:39:38 +0000 | [diff] [blame] | 4090 | } |
danielk1977 | 172114a | 2009-07-07 15:47:12 +0000 | [diff] [blame] | 4091 | pCur->iPage = 0; |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 4092 | }else{ |
drh | 4c301aa | 2009-07-15 17:25:45 +0000 | [diff] [blame] | 4093 | rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]); |
| 4094 | if( rc!=SQLITE_OK ){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 4095 | pCur->eState = CURSOR_INVALID; |
| 4096 | return rc; |
| 4097 | } |
danielk1977 | 172114a | 2009-07-07 15:47:12 +0000 | [diff] [blame] | 4098 | pCur->iPage = 0; |
| 4099 | |
| 4100 | /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor |
| 4101 | ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is |
| 4102 | ** NULL, the caller expects a table b-tree. If this is not the case, |
| 4103 | ** return an SQLITE_CORRUPT error. */ |
| 4104 | assert( pCur->apPage[0]->intKey==1 || pCur->apPage[0]->intKey==0 ); |
| 4105 | if( (pCur->pKeyInfo==0)!=pCur->apPage[0]->intKey ){ |
| 4106 | return SQLITE_CORRUPT_BKPT; |
| 4107 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 4108 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4109 | |
danielk1977 | 8f880a8 | 2009-07-13 09:41:45 +0000 | [diff] [blame] | 4110 | /* Assert that the root page is of the correct type. This must be the |
| 4111 | ** case as the call to this function that loaded the root-page (either |
| 4112 | ** this call or a previous invocation) would have detected corruption |
| 4113 | ** if the assumption were not true, and it is not possible for the flags |
| 4114 | ** byte to have been modified while this cursor is holding a reference |
| 4115 | ** to the page. */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4116 | pRoot = pCur->apPage[0]; |
| 4117 | assert( pRoot->pgno==pCur->pgnoRoot ); |
danielk1977 | 8f880a8 | 2009-07-13 09:41:45 +0000 | [diff] [blame] | 4118 | assert( pRoot->isInit && (pCur->pKeyInfo==0)==pRoot->intKey ); |
| 4119 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4120 | pCur->aiIdx[0] = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 4121 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 4122 | pCur->atLast = 0; |
| 4123 | pCur->validNKey = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4124 | |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 4125 | if( pRoot->nCell==0 && !pRoot->leaf ){ |
| 4126 | Pgno subpage; |
drh | c85240d | 2009-06-04 16:14:33 +0000 | [diff] [blame] | 4127 | if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4128 | subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4129 | pCur->eState = CURSOR_VALID; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4130 | rc = moveToChild(pCur, subpage); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4131 | }else{ |
| 4132 | pCur->eState = ((pRoot->nCell>0)?CURSOR_VALID:CURSOR_INVALID); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 4133 | } |
| 4134 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4135 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 4136 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4137 | /* |
| 4138 | ** Move the cursor down to the left-most leaf entry beneath the |
| 4139 | ** entry to which it is currently pointing. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 4140 | ** |
| 4141 | ** The left-most leaf is the one with the smallest key - the first |
| 4142 | ** in ascending order. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4143 | */ |
| 4144 | static int moveToLeftmost(BtCursor *pCur){ |
| 4145 | Pgno pgno; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4146 | int rc = SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4147 | MemPage *pPage; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4148 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4149 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4150 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4151 | while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){ |
| 4152 | assert( pCur->aiIdx[pCur->iPage]<pPage->nCell ); |
| 4153 | pgno = get4byte(findCell(pPage, pCur->aiIdx[pCur->iPage])); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4154 | rc = moveToChild(pCur, pgno); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4155 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4156 | return rc; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4157 | } |
| 4158 | |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4159 | /* |
| 4160 | ** Move the cursor down to the right-most leaf entry beneath the |
| 4161 | ** page to which it is currently pointing. Notice the difference |
| 4162 | ** between moveToLeftmost() and moveToRightmost(). moveToLeftmost() |
| 4163 | ** finds the left-most entry beneath the *entry* whereas moveToRightmost() |
| 4164 | ** finds the right-most entry beneath the *page*. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 4165 | ** |
| 4166 | ** The right-most entry is the one with the largest key - the last |
| 4167 | ** key in ascending order. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4168 | */ |
| 4169 | static int moveToRightmost(BtCursor *pCur){ |
| 4170 | Pgno pgno; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4171 | int rc = SQLITE_OK; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 4172 | MemPage *pPage = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4173 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4174 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4175 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4176 | while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4177 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4178 | pCur->aiIdx[pCur->iPage] = pPage->nCell; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4179 | rc = moveToChild(pCur, pgno); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4180 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4181 | if( rc==SQLITE_OK ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4182 | pCur->aiIdx[pCur->iPage] = pPage->nCell-1; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4183 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 4184 | pCur->validNKey = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4185 | } |
danielk1977 | 518002e | 2008-09-05 05:02:46 +0000 | [diff] [blame] | 4186 | return rc; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4187 | } |
| 4188 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4189 | /* Move the cursor to the first entry in the table. Return SQLITE_OK |
| 4190 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 4191 | ** or set *pRes to 1 if the table is empty. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4192 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4193 | int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4194 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4195 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4196 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 4197 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4198 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4199 | if( rc==SQLITE_OK ){ |
| 4200 | if( pCur->eState==CURSOR_INVALID ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4201 | assert( pCur->apPage[pCur->iPage]->nCell==0 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4202 | *pRes = 1; |
| 4203 | rc = SQLITE_OK; |
| 4204 | }else{ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4205 | assert( pCur->apPage[pCur->iPage]->nCell>0 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4206 | *pRes = 0; |
| 4207 | rc = moveToLeftmost(pCur); |
| 4208 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4209 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4210 | return rc; |
| 4211 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4212 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4213 | /* Move the cursor to the last entry in the table. Return SQLITE_OK |
| 4214 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 4215 | ** or set *pRes to 1 if the table is empty. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4216 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4217 | int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4218 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4219 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4220 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 4221 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
danielk1977 | 3f632d5 | 2009-05-02 10:03:09 +0000 | [diff] [blame] | 4222 | |
| 4223 | /* If the cursor already points to the last entry, this is a no-op. */ |
| 4224 | if( CURSOR_VALID==pCur->eState && pCur->atLast ){ |
| 4225 | #ifdef SQLITE_DEBUG |
| 4226 | /* This block serves to assert() that the cursor really does point |
| 4227 | ** to the last entry in the b-tree. */ |
| 4228 | int ii; |
| 4229 | for(ii=0; ii<pCur->iPage; ii++){ |
| 4230 | assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell ); |
| 4231 | } |
| 4232 | assert( pCur->aiIdx[pCur->iPage]==pCur->apPage[pCur->iPage]->nCell-1 ); |
| 4233 | assert( pCur->apPage[pCur->iPage]->leaf ); |
| 4234 | #endif |
| 4235 | return SQLITE_OK; |
| 4236 | } |
| 4237 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4238 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4239 | if( rc==SQLITE_OK ){ |
| 4240 | if( CURSOR_INVALID==pCur->eState ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4241 | assert( pCur->apPage[pCur->iPage]->nCell==0 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4242 | *pRes = 1; |
| 4243 | }else{ |
| 4244 | assert( pCur->eState==CURSOR_VALID ); |
| 4245 | *pRes = 0; |
| 4246 | rc = moveToRightmost(pCur); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4247 | pCur->atLast = rc==SQLITE_OK ?1:0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4248 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4249 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4250 | return rc; |
| 4251 | } |
| 4252 | |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 4253 | /* Move the cursor so that it points to an entry near the key |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4254 | ** specified by pIdxKey or intKey. Return a success code. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4255 | ** |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4256 | ** For INTKEY tables, the intKey parameter is used. pIdxKey |
| 4257 | ** must be NULL. For index tables, pIdxKey is used and intKey |
| 4258 | ** is ignored. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4259 | ** |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4260 | ** If an exact match is not found, then the cursor is always |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4261 | ** left pointing at a leaf page which would hold the entry if it |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4262 | ** were present. The cursor might point to an entry that comes |
| 4263 | ** before or after the key. |
| 4264 | ** |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 4265 | ** An integer is written into *pRes which is the result of |
| 4266 | ** comparing the key with the entry to which the cursor is |
| 4267 | ** pointing. The meaning of the integer written into |
| 4268 | ** *pRes is as follows: |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4269 | ** |
| 4270 | ** *pRes<0 The cursor is left pointing at an entry that |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 4271 | ** is smaller than intKey/pIdxKey or if the table is empty |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 4272 | ** and the cursor is therefore left point to nothing. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4273 | ** |
| 4274 | ** *pRes==0 The cursor is left pointing at an entry that |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 4275 | ** exactly matches intKey/pIdxKey. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4276 | ** |
| 4277 | ** *pRes>0 The cursor is left pointing at an entry that |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 4278 | ** is larger than intKey/pIdxKey. |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4279 | ** |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 4280 | */ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4281 | int sqlite3BtreeMovetoUnpacked( |
| 4282 | BtCursor *pCur, /* The cursor to be moved */ |
| 4283 | UnpackedRecord *pIdxKey, /* Unpacked index key */ |
| 4284 | i64 intKey, /* The table key */ |
| 4285 | int biasRight, /* If true, bias the search to the high end */ |
| 4286 | int *pRes /* Write search results here */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 4287 | ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4288 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4289 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4290 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 4291 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
danielk1977 | 5cb0963 | 2009-07-09 11:36:01 +0000 | [diff] [blame] | 4292 | assert( pRes ); |
danielk1977 | 3fd7cf5 | 2009-07-13 07:30:52 +0000 | [diff] [blame] | 4293 | assert( (pIdxKey==0)==(pCur->pKeyInfo==0) ); |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 4294 | |
| 4295 | /* If the cursor is already positioned at the point we are trying |
| 4296 | ** to move to, then just return without doing any work */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4297 | if( pCur->eState==CURSOR_VALID && pCur->validNKey |
| 4298 | && pCur->apPage[0]->intKey |
| 4299 | ){ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4300 | if( pCur->info.nKey==intKey ){ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 4301 | *pRes = 0; |
| 4302 | return SQLITE_OK; |
| 4303 | } |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4304 | if( pCur->atLast && pCur->info.nKey<intKey ){ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 4305 | *pRes = -1; |
| 4306 | return SQLITE_OK; |
| 4307 | } |
| 4308 | } |
| 4309 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4310 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4311 | if( rc ){ |
| 4312 | return rc; |
| 4313 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4314 | assert( pCur->apPage[pCur->iPage] ); |
| 4315 | assert( pCur->apPage[pCur->iPage]->isInit ); |
danielk1977 | 171fff3 | 2009-07-11 05:06:51 +0000 | [diff] [blame] | 4316 | assert( pCur->apPage[pCur->iPage]->nCell>0 || pCur->eState==CURSOR_INVALID ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4317 | if( pCur->eState==CURSOR_INVALID ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 4318 | *pRes = -1; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4319 | assert( pCur->apPage[pCur->iPage]->nCell==0 ); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 4320 | return SQLITE_OK; |
| 4321 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4322 | assert( pCur->apPage[0]->intKey || pIdxKey ); |
drh | 1468438 | 2006-11-30 13:05:29 +0000 | [diff] [blame] | 4323 | for(;;){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4324 | int lwr, upr; |
| 4325 | Pgno chldPg; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4326 | MemPage *pPage = pCur->apPage[pCur->iPage]; |
danielk1977 | 171fff3 | 2009-07-11 05:06:51 +0000 | [diff] [blame] | 4327 | int c; |
| 4328 | |
| 4329 | /* pPage->nCell must be greater than zero. If this is the root-page |
| 4330 | ** the cursor would have been INVALID above and this for(;;) loop |
| 4331 | ** not run. If this is not the root-page, then the moveToChild() routine |
danielk1977 | 3fd7cf5 | 2009-07-13 07:30:52 +0000 | [diff] [blame] | 4332 | ** would have already detected db corruption. Similarly, pPage must |
| 4333 | ** be the right kind (index or table) of b-tree page. Otherwise |
| 4334 | ** a moveToChild() or moveToRoot() call would have detected corruption. */ |
danielk1977 | 171fff3 | 2009-07-11 05:06:51 +0000 | [diff] [blame] | 4335 | assert( pPage->nCell>0 ); |
danielk1977 | 3fd7cf5 | 2009-07-13 07:30:52 +0000 | [diff] [blame] | 4336 | assert( pPage->intKey==(pIdxKey==0) ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4337 | lwr = 0; |
| 4338 | upr = pPage->nCell-1; |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 4339 | if( biasRight ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4340 | pCur->aiIdx[pCur->iPage] = (u16)upr; |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 4341 | }else{ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4342 | pCur->aiIdx[pCur->iPage] = (u16)((upr+lwr)/2); |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 4343 | } |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 4344 | for(;;){ |
danielk1977 | 11c327a | 2009-05-04 19:01:26 +0000 | [diff] [blame] | 4345 | int idx = pCur->aiIdx[pCur->iPage]; /* Index of current cell in pPage */ |
| 4346 | u8 *pCell; /* Pointer to current cell in pPage */ |
| 4347 | |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 4348 | pCur->info.nSize = 0; |
danielk1977 | 11c327a | 2009-05-04 19:01:26 +0000 | [diff] [blame] | 4349 | pCell = findCell(pPage, idx) + pPage->childPtrSize; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4350 | if( pPage->intKey ){ |
danielk1977 | 11c327a | 2009-05-04 19:01:26 +0000 | [diff] [blame] | 4351 | i64 nCellKey; |
drh | d172f86 | 2006-01-12 15:01:15 +0000 | [diff] [blame] | 4352 | if( pPage->hasData ){ |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 4353 | u32 dummy; |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 4354 | pCell += getVarint32(pCell, dummy); |
drh | d172f86 | 2006-01-12 15:01:15 +0000 | [diff] [blame] | 4355 | } |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 4356 | getVarint(pCell, (u64*)&nCellKey); |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4357 | if( nCellKey==intKey ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4358 | c = 0; |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4359 | }else if( nCellKey<intKey ){ |
drh | 41eb9e9 | 2008-04-02 18:33:07 +0000 | [diff] [blame] | 4360 | c = -1; |
| 4361 | }else{ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4362 | assert( nCellKey>intKey ); |
drh | 41eb9e9 | 2008-04-02 18:33:07 +0000 | [diff] [blame] | 4363 | c = +1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4364 | } |
danielk1977 | 11c327a | 2009-05-04 19:01:26 +0000 | [diff] [blame] | 4365 | pCur->validNKey = 1; |
| 4366 | pCur->info.nKey = nCellKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4367 | }else{ |
danielk1977 | 11c327a | 2009-05-04 19:01:26 +0000 | [diff] [blame] | 4368 | /* The maximum supported page-size is 32768 bytes. This means that |
| 4369 | ** the maximum number of record bytes stored on an index B-Tree |
| 4370 | ** page is at most 8198 bytes, which may be stored as a 2-byte |
| 4371 | ** varint. This information is used to attempt to avoid parsing |
| 4372 | ** the entire cell by checking for the cases where the record is |
| 4373 | ** stored entirely within the b-tree page by inspecting the first |
| 4374 | ** 2 bytes of the cell. |
| 4375 | */ |
| 4376 | int nCell = pCell[0]; |
| 4377 | if( !(nCell & 0x80) && nCell<=pPage->maxLocal ){ |
| 4378 | /* This branch runs if the record-size field of the cell is a |
| 4379 | ** single byte varint and the record fits entirely on the main |
| 4380 | ** b-tree page. */ |
| 4381 | c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[1], pIdxKey); |
| 4382 | }else if( !(pCell[1] & 0x80) |
| 4383 | && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal |
| 4384 | ){ |
| 4385 | /* The record-size field is a 2 byte varint and the record |
| 4386 | ** fits entirely on the main b-tree page. */ |
| 4387 | c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[2], pIdxKey); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 4388 | }else{ |
danielk1977 | 11c327a | 2009-05-04 19:01:26 +0000 | [diff] [blame] | 4389 | /* The record flows over onto one or more overflow pages. In |
| 4390 | ** this case the whole cell needs to be parsed, a buffer allocated |
| 4391 | ** and accessPayload() used to retrieve the record into the |
| 4392 | ** buffer before VdbeRecordCompare() can be called. */ |
| 4393 | void *pCellKey; |
| 4394 | u8 * const pCellBody = pCell - pPage->childPtrSize; |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 4395 | btreeParseCellPtr(pPage, pCellBody, &pCur->info); |
shane | 60a4b53 | 2009-05-06 18:57:09 +0000 | [diff] [blame] | 4396 | nCell = (int)pCur->info.nKey; |
danielk1977 | 11c327a | 2009-05-04 19:01:26 +0000 | [diff] [blame] | 4397 | pCellKey = sqlite3Malloc( nCell ); |
danielk1977 | 6507ecb | 2008-03-25 09:56:44 +0000 | [diff] [blame] | 4398 | if( pCellKey==0 ){ |
| 4399 | rc = SQLITE_NOMEM; |
| 4400 | goto moveto_finish; |
| 4401 | } |
drh | fb19268 | 2009-07-11 18:26:28 +0000 | [diff] [blame] | 4402 | rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0); |
drh | ec9b31f | 2009-08-25 13:53:49 +0000 | [diff] [blame] | 4403 | if( rc ){ |
| 4404 | sqlite3_free(pCellKey); |
| 4405 | goto moveto_finish; |
| 4406 | } |
danielk1977 | 11c327a | 2009-05-04 19:01:26 +0000 | [diff] [blame] | 4407 | c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 4408 | sqlite3_free(pCellKey); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 4409 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4410 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4411 | if( c==0 ){ |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 4412 | if( pPage->intKey && !pPage->leaf ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4413 | lwr = idx; |
drh | fc70e6f | 2004-05-12 21:11:27 +0000 | [diff] [blame] | 4414 | upr = lwr - 1; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4415 | break; |
| 4416 | }else{ |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 4417 | *pRes = 0; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 4418 | rc = SQLITE_OK; |
| 4419 | goto moveto_finish; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4420 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4421 | } |
| 4422 | if( c<0 ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4423 | lwr = idx+1; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4424 | }else{ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4425 | upr = idx-1; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4426 | } |
drh | f1d68b3 | 2007-03-29 04:43:26 +0000 | [diff] [blame] | 4427 | if( lwr>upr ){ |
| 4428 | break; |
| 4429 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4430 | pCur->aiIdx[pCur->iPage] = (u16)((lwr+upr)/2); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4431 | } |
| 4432 | assert( lwr==upr+1 ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4433 | assert( pPage->isInit ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4434 | if( pPage->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4435 | chldPg = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4436 | }else if( lwr>=pPage->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4437 | chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4438 | }else{ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 4439 | chldPg = get4byte(findCell(pPage, lwr)); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4440 | } |
| 4441 | if( chldPg==0 ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4442 | assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell ); |
danielk1977 | 5cb0963 | 2009-07-09 11:36:01 +0000 | [diff] [blame] | 4443 | *pRes = c; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 4444 | rc = SQLITE_OK; |
| 4445 | goto moveto_finish; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4446 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4447 | pCur->aiIdx[pCur->iPage] = (u16)lwr; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 4448 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 4449 | pCur->validNKey = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4450 | rc = moveToChild(pCur, chldPg); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 4451 | if( rc ) goto moveto_finish; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4452 | } |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 4453 | moveto_finish: |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4454 | return rc; |
| 4455 | } |
| 4456 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4457 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4458 | /* |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 4459 | ** Return TRUE if the cursor is not pointing at an entry of the table. |
| 4460 | ** |
| 4461 | ** TRUE will be returned after a call to sqlite3BtreeNext() moves |
| 4462 | ** past the last entry in the table or sqlite3BtreePrev() moves past |
| 4463 | ** the first entry. TRUE is also returned if the table is empty. |
| 4464 | */ |
| 4465 | int sqlite3BtreeEof(BtCursor *pCur){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4466 | /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries |
| 4467 | ** have been deleted? This API will need to change to return an error code |
| 4468 | ** as well as the boolean result value. |
| 4469 | */ |
| 4470 | return (CURSOR_VALID!=pCur->eState); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 4471 | } |
| 4472 | |
| 4473 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4474 | ** Advance the cursor to the next entry in the database. If |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4475 | ** successful then set *pRes=0. If the cursor |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4476 | ** was already pointing to the last entry in the database before |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4477 | ** this routine was called, then set *pRes=1. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4478 | */ |
drh | d094db1 | 2008-04-03 21:46:57 +0000 | [diff] [blame] | 4479 | int sqlite3BtreeNext(BtCursor *pCur, int *pRes){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4480 | int rc; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4481 | int idx; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 4482 | MemPage *pPage; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4483 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4484 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 4485 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4486 | if( rc!=SQLITE_OK ){ |
| 4487 | return rc; |
| 4488 | } |
drh | 8c4d3a6 | 2007-04-06 01:03:32 +0000 | [diff] [blame] | 4489 | assert( pRes!=0 ); |
drh | 8c4d3a6 | 2007-04-06 01:03:32 +0000 | [diff] [blame] | 4490 | if( CURSOR_INVALID==pCur->eState ){ |
| 4491 | *pRes = 1; |
| 4492 | return SQLITE_OK; |
| 4493 | } |
drh | 4c301aa | 2009-07-15 17:25:45 +0000 | [diff] [blame] | 4494 | if( pCur->skipNext>0 ){ |
| 4495 | pCur->skipNext = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4496 | *pRes = 0; |
| 4497 | return SQLITE_OK; |
| 4498 | } |
drh | 4c301aa | 2009-07-15 17:25:45 +0000 | [diff] [blame] | 4499 | pCur->skipNext = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4500 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4501 | pPage = pCur->apPage[pCur->iPage]; |
| 4502 | idx = ++pCur->aiIdx[pCur->iPage]; |
| 4503 | assert( pPage->isInit ); |
| 4504 | assert( idx<=pPage->nCell ); |
danielk1977 | 6a43f9b | 2004-11-16 04:57:24 +0000 | [diff] [blame] | 4505 | |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 4506 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 4507 | pCur->validNKey = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4508 | if( idx>=pPage->nCell ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4509 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4510 | rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4511 | if( rc ) return rc; |
| 4512 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4513 | *pRes = 0; |
| 4514 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4515 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4516 | do{ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4517 | if( pCur->iPage==0 ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4518 | *pRes = 1; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4519 | pCur->eState = CURSOR_INVALID; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4520 | return SQLITE_OK; |
| 4521 | } |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 4522 | moveToParent(pCur); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4523 | pPage = pCur->apPage[pCur->iPage]; |
| 4524 | }while( pCur->aiIdx[pCur->iPage]>=pPage->nCell ); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4525 | *pRes = 0; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 4526 | if( pPage->intKey ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4527 | rc = sqlite3BtreeNext(pCur, pRes); |
| 4528 | }else{ |
| 4529 | rc = SQLITE_OK; |
| 4530 | } |
| 4531 | return rc; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4532 | } |
| 4533 | *pRes = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4534 | if( pPage->leaf ){ |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4535 | return SQLITE_OK; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4536 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4537 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4538 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4539 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4540 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4541 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4542 | /* |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4543 | ** 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] | 4544 | ** successful then set *pRes=0. If the cursor |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4545 | ** was already pointing to the first entry in the database before |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4546 | ** this routine was called, then set *pRes=1. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4547 | */ |
drh | d094db1 | 2008-04-03 21:46:57 +0000 | [diff] [blame] | 4548 | int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){ |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4549 | int rc; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4550 | MemPage *pPage; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4551 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4552 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 4553 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4554 | if( rc!=SQLITE_OK ){ |
| 4555 | return rc; |
| 4556 | } |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 4557 | pCur->atLast = 0; |
drh | 8c4d3a6 | 2007-04-06 01:03:32 +0000 | [diff] [blame] | 4558 | if( CURSOR_INVALID==pCur->eState ){ |
| 4559 | *pRes = 1; |
| 4560 | return SQLITE_OK; |
| 4561 | } |
drh | 4c301aa | 2009-07-15 17:25:45 +0000 | [diff] [blame] | 4562 | if( pCur->skipNext<0 ){ |
| 4563 | pCur->skipNext = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4564 | *pRes = 0; |
| 4565 | return SQLITE_OK; |
| 4566 | } |
drh | 4c301aa | 2009-07-15 17:25:45 +0000 | [diff] [blame] | 4567 | pCur->skipNext = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4568 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4569 | pPage = pCur->apPage[pCur->iPage]; |
| 4570 | assert( pPage->isInit ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4571 | if( !pPage->leaf ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4572 | int idx = pCur->aiIdx[pCur->iPage]; |
| 4573 | rc = moveToChild(pCur, get4byte(findCell(pPage, idx))); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4574 | if( rc ){ |
| 4575 | return rc; |
| 4576 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4577 | rc = moveToRightmost(pCur); |
| 4578 | }else{ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4579 | while( pCur->aiIdx[pCur->iPage]==0 ){ |
| 4580 | if( pCur->iPage==0 ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4581 | pCur->eState = CURSOR_INVALID; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 4582 | *pRes = 1; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4583 | return SQLITE_OK; |
| 4584 | } |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 4585 | moveToParent(pCur); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4586 | } |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 4587 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 4588 | pCur->validNKey = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4589 | |
| 4590 | pCur->aiIdx[pCur->iPage]--; |
| 4591 | pPage = pCur->apPage[pCur->iPage]; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 4592 | if( pPage->intKey && !pPage->leaf ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4593 | rc = sqlite3BtreePrevious(pCur, pRes); |
| 4594 | }else{ |
| 4595 | rc = SQLITE_OK; |
| 4596 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4597 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4598 | *pRes = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4599 | return rc; |
| 4600 | } |
| 4601 | |
| 4602 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4603 | ** Allocate a new page from the database file. |
| 4604 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4605 | ** The new page is marked as dirty. (In other words, sqlite3PagerWrite() |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4606 | ** has already been called on the new page.) The new page has also |
| 4607 | ** been referenced and the calling routine is responsible for calling |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4608 | ** sqlite3PagerUnref() on the new page when it is done. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4609 | ** |
| 4610 | ** SQLITE_OK is returned on success. Any other return value indicates |
| 4611 | ** an error. *ppPage and *pPgno are undefined in the event of an error. |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4612 | ** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned. |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 4613 | ** |
drh | 199e3cf | 2002-07-18 11:01:47 +0000 | [diff] [blame] | 4614 | ** If the "nearby" parameter is not 0, then a (feeble) effort is made to |
| 4615 | ** 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] | 4616 | ** attempt to keep related pages close to each other in the database file, |
| 4617 | ** which in turn can make database access faster. |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4618 | ** |
| 4619 | ** If the "exact" parameter is not 0, and the page-number nearby exists |
| 4620 | ** anywhere on the free-list, then it is guarenteed to be returned. This |
| 4621 | ** is only used by auto-vacuum databases when allocating a new table. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4622 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4623 | static int allocateBtreePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4624 | BtShared *pBt, |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4625 | MemPage **ppPage, |
| 4626 | Pgno *pPgno, |
| 4627 | Pgno nearby, |
| 4628 | u8 exact |
| 4629 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4630 | MemPage *pPage1; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4631 | int rc; |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 4632 | u32 n; /* Number of pages on the freelist */ |
drh | 042d6a1 | 2009-06-17 13:57:16 +0000 | [diff] [blame] | 4633 | u32 k; /* Number of leaves on the trunk of the freelist */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4634 | MemPage *pTrunk = 0; |
| 4635 | MemPage *pPrevTrunk = 0; |
drh | 1662b5a | 2009-06-04 19:06:09 +0000 | [diff] [blame] | 4636 | Pgno mxPage; /* Total size of the database file */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4637 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4638 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4639 | pPage1 = pBt->pPage1; |
drh | 1662b5a | 2009-06-04 19:06:09 +0000 | [diff] [blame] | 4640 | mxPage = pagerPagecount(pBt); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4641 | n = get4byte(&pPage1->aData[36]); |
drh | df35a08 | 2009-07-09 02:24:35 +0000 | [diff] [blame] | 4642 | testcase( n==mxPage-1 ); |
| 4643 | if( n>=mxPage ){ |
drh | 1662b5a | 2009-06-04 19:06:09 +0000 | [diff] [blame] | 4644 | return SQLITE_CORRUPT_BKPT; |
| 4645 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4646 | if( n>0 ){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4647 | /* There are pages on the freelist. Reuse one of those pages. */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4648 | Pgno iTrunk; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4649 | u8 searchList = 0; /* If the free-list must be searched for 'nearby' */ |
| 4650 | |
| 4651 | /* If the 'exact' parameter was true and a query of the pointer-map |
| 4652 | ** shows that the page 'nearby' is somewhere on the free-list, then |
| 4653 | ** the entire-list will be searched for that page. |
| 4654 | */ |
| 4655 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | 1662b5a | 2009-06-04 19:06:09 +0000 | [diff] [blame] | 4656 | if( exact && nearby<=mxPage ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4657 | u8 eType; |
| 4658 | assert( nearby>0 ); |
| 4659 | assert( pBt->autoVacuum ); |
| 4660 | rc = ptrmapGet(pBt, nearby, &eType, 0); |
| 4661 | if( rc ) return rc; |
| 4662 | if( eType==PTRMAP_FREEPAGE ){ |
| 4663 | searchList = 1; |
| 4664 | } |
| 4665 | *pPgno = nearby; |
| 4666 | } |
| 4667 | #endif |
| 4668 | |
| 4669 | /* Decrement the free-list count by 1. Set iTrunk to the index of the |
| 4670 | ** first free-list trunk page. iPrevTrunk is initially 1. |
| 4671 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4672 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4673 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4674 | put4byte(&pPage1->aData[36], n-1); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4675 | |
| 4676 | /* The code within this loop is run only once if the 'searchList' variable |
| 4677 | ** is not true. Otherwise, it runs once for each trunk-page on the |
| 4678 | ** free-list until the page 'nearby' is located. |
| 4679 | */ |
| 4680 | do { |
| 4681 | pPrevTrunk = pTrunk; |
| 4682 | if( pPrevTrunk ){ |
| 4683 | iTrunk = get4byte(&pPrevTrunk->aData[0]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 4684 | }else{ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4685 | iTrunk = get4byte(&pPage1->aData[32]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 4686 | } |
drh | df35a08 | 2009-07-09 02:24:35 +0000 | [diff] [blame] | 4687 | testcase( iTrunk==mxPage ); |
drh | 1662b5a | 2009-06-04 19:06:09 +0000 | [diff] [blame] | 4688 | if( iTrunk>mxPage ){ |
| 4689 | rc = SQLITE_CORRUPT_BKPT; |
| 4690 | }else{ |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 4691 | rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0); |
drh | 1662b5a | 2009-06-04 19:06:09 +0000 | [diff] [blame] | 4692 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4693 | if( rc ){ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4694 | pTrunk = 0; |
| 4695 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4696 | } |
| 4697 | |
| 4698 | k = get4byte(&pTrunk->aData[4]); |
| 4699 | if( k==0 && !searchList ){ |
| 4700 | /* The trunk has no leaves and the list is not being searched. |
| 4701 | ** So extract the trunk page itself and use it as the newly |
| 4702 | ** allocated page */ |
| 4703 | assert( pPrevTrunk==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4704 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4705 | if( rc ){ |
| 4706 | goto end_allocate_page; |
| 4707 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4708 | *pPgno = iTrunk; |
| 4709 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 4710 | *ppPage = pTrunk; |
| 4711 | pTrunk = 0; |
| 4712 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
drh | 042d6a1 | 2009-06-17 13:57:16 +0000 | [diff] [blame] | 4713 | }else if( k>(u32)(pBt->usableSize/4 - 2) ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4714 | /* Value of k is out of range. Database corruption */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4715 | rc = SQLITE_CORRUPT_BKPT; |
| 4716 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4717 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4718 | }else if( searchList && nearby==iTrunk ){ |
| 4719 | /* The list is being searched and this trunk page is the page |
| 4720 | ** to allocate, regardless of whether it has leaves. |
| 4721 | */ |
| 4722 | assert( *pPgno==iTrunk ); |
| 4723 | *ppPage = pTrunk; |
| 4724 | searchList = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4725 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4726 | if( rc ){ |
| 4727 | goto end_allocate_page; |
| 4728 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4729 | if( k==0 ){ |
| 4730 | if( !pPrevTrunk ){ |
| 4731 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 4732 | }else{ |
| 4733 | memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4); |
| 4734 | } |
| 4735 | }else{ |
| 4736 | /* The trunk page is required by the caller but it contains |
| 4737 | ** pointers to free-list leaves. The first leaf becomes a trunk |
| 4738 | ** page in this case. |
| 4739 | */ |
| 4740 | MemPage *pNewTrunk; |
| 4741 | Pgno iNewTrunk = get4byte(&pTrunk->aData[8]); |
drh | 1662b5a | 2009-06-04 19:06:09 +0000 | [diff] [blame] | 4742 | if( iNewTrunk>mxPage ){ |
| 4743 | rc = SQLITE_CORRUPT_BKPT; |
| 4744 | goto end_allocate_page; |
| 4745 | } |
drh | df35a08 | 2009-07-09 02:24:35 +0000 | [diff] [blame] | 4746 | testcase( iNewTrunk==mxPage ); |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 4747 | rc = btreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4748 | if( rc!=SQLITE_OK ){ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4749 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4750 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4751 | rc = sqlite3PagerWrite(pNewTrunk->pDbPage); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4752 | if( rc!=SQLITE_OK ){ |
| 4753 | releasePage(pNewTrunk); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4754 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4755 | } |
| 4756 | memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4); |
| 4757 | put4byte(&pNewTrunk->aData[4], k-1); |
| 4758 | memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4759 | releasePage(pNewTrunk); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4760 | if( !pPrevTrunk ){ |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 4761 | assert( sqlite3PagerIswriteable(pPage1->pDbPage) ); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4762 | put4byte(&pPage1->aData[32], iNewTrunk); |
| 4763 | }else{ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4764 | rc = sqlite3PagerWrite(pPrevTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4765 | if( rc ){ |
| 4766 | goto end_allocate_page; |
| 4767 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4768 | put4byte(&pPrevTrunk->aData[0], iNewTrunk); |
| 4769 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4770 | } |
| 4771 | pTrunk = 0; |
| 4772 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
| 4773 | #endif |
danielk1977 | e576521 | 2009-06-17 11:13:28 +0000 | [diff] [blame] | 4774 | }else if( k>0 ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4775 | /* Extract a leaf from the trunk */ |
drh | 042d6a1 | 2009-06-17 13:57:16 +0000 | [diff] [blame] | 4776 | u32 closest; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4777 | Pgno iPage; |
| 4778 | unsigned char *aData = pTrunk->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4779 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4780 | if( rc ){ |
| 4781 | goto end_allocate_page; |
| 4782 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4783 | if( nearby>0 ){ |
drh | 042d6a1 | 2009-06-17 13:57:16 +0000 | [diff] [blame] | 4784 | u32 i; |
| 4785 | int dist; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4786 | closest = 0; |
| 4787 | dist = get4byte(&aData[8]) - nearby; |
| 4788 | if( dist<0 ) dist = -dist; |
| 4789 | for(i=1; i<k; i++){ |
| 4790 | int d2 = get4byte(&aData[8+i*4]) - nearby; |
| 4791 | if( d2<0 ) d2 = -d2; |
| 4792 | if( d2<dist ){ |
| 4793 | closest = i; |
| 4794 | dist = d2; |
| 4795 | } |
| 4796 | } |
| 4797 | }else{ |
| 4798 | closest = 0; |
| 4799 | } |
| 4800 | |
| 4801 | iPage = get4byte(&aData[8+closest*4]); |
drh | df35a08 | 2009-07-09 02:24:35 +0000 | [diff] [blame] | 4802 | testcase( iPage==mxPage ); |
drh | 1662b5a | 2009-06-04 19:06:09 +0000 | [diff] [blame] | 4803 | if( iPage>mxPage ){ |
| 4804 | rc = SQLITE_CORRUPT_BKPT; |
| 4805 | goto end_allocate_page; |
| 4806 | } |
drh | df35a08 | 2009-07-09 02:24:35 +0000 | [diff] [blame] | 4807 | testcase( iPage==mxPage ); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4808 | if( !searchList || iPage==nearby ){ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4809 | int noContent; |
shane | 1f9e6aa | 2008-06-09 19:27:11 +0000 | [diff] [blame] | 4810 | *pPgno = iPage; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4811 | TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d" |
| 4812 | ": %d more free pages\n", |
| 4813 | *pPgno, closest+1, k, pTrunk->pgno, n-1)); |
| 4814 | if( closest<k-1 ){ |
| 4815 | memcpy(&aData[8+closest*4], &aData[4+k*4], 4); |
| 4816 | } |
| 4817 | put4byte(&aData[4], k-1); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 4818 | assert( sqlite3PagerIswriteable(pTrunk->pDbPage) ); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4819 | noContent = !btreeGetHasContent(pBt, *pPgno); |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 4820 | rc = btreeGetPage(pBt, *pPgno, ppPage, noContent); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4821 | if( rc==SQLITE_OK ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4822 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 4823 | if( rc!=SQLITE_OK ){ |
| 4824 | releasePage(*ppPage); |
| 4825 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4826 | } |
| 4827 | searchList = 0; |
| 4828 | } |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 4829 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4830 | releasePage(pPrevTrunk); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4831 | pPrevTrunk = 0; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4832 | }while( searchList ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4833 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4834 | /* There are no pages on the freelist, so create a new page at the |
| 4835 | ** end of the file */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 4836 | int nPage = pagerPagecount(pBt); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 4837 | *pPgno = nPage + 1; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4838 | |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4839 | if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ |
| 4840 | (*pPgno)++; |
| 4841 | } |
| 4842 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4843 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 4844 | if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, *pPgno) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4845 | /* If *pPgno refers to a pointer-map page, allocate two new pages |
| 4846 | ** at the end of the file instead of one. The first allocated page |
| 4847 | ** becomes a new pointer-map page, the second is used by the caller. |
| 4848 | */ |
danielk1977 | ac86169 | 2009-03-28 10:54:22 +0000 | [diff] [blame] | 4849 | MemPage *pPg = 0; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4850 | TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno)); |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4851 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 4852 | rc = btreeGetPage(pBt, *pPgno, &pPg, 0); |
danielk1977 | ac86169 | 2009-03-28 10:54:22 +0000 | [diff] [blame] | 4853 | if( rc==SQLITE_OK ){ |
| 4854 | rc = sqlite3PagerWrite(pPg->pDbPage); |
| 4855 | releasePage(pPg); |
| 4856 | } |
| 4857 | if( rc ) return rc; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4858 | (*pPgno)++; |
drh | 7219043 | 2008-01-31 14:54:43 +0000 | [diff] [blame] | 4859 | if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ (*pPgno)++; } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4860 | } |
| 4861 | #endif |
| 4862 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4863 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 4864 | rc = btreeGetPage(pBt, *pPgno, ppPage, 0); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4865 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4866 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 4867 | if( rc!=SQLITE_OK ){ |
| 4868 | releasePage(*ppPage); |
| 4869 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4870 | TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4871 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4872 | |
| 4873 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4874 | |
| 4875 | end_allocate_page: |
| 4876 | releasePage(pTrunk); |
| 4877 | releasePage(pPrevTrunk); |
danielk1977 | b247c21 | 2008-11-21 09:09:01 +0000 | [diff] [blame] | 4878 | if( rc==SQLITE_OK ){ |
| 4879 | if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){ |
| 4880 | releasePage(*ppPage); |
| 4881 | return SQLITE_CORRUPT_BKPT; |
| 4882 | } |
| 4883 | (*ppPage)->isInit = 0; |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 4884 | }else{ |
| 4885 | *ppPage = 0; |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 4886 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4887 | return rc; |
| 4888 | } |
| 4889 | |
| 4890 | /* |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4891 | ** This function is used to add page iPage to the database file free-list. |
| 4892 | ** 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] | 4893 | ** |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4894 | ** The value passed as the second argument to this function is optional. |
| 4895 | ** If the caller happens to have a pointer to the MemPage object |
| 4896 | ** corresponding to page iPage handy, it may pass it as the second value. |
| 4897 | ** Otherwise, it may pass NULL. |
| 4898 | ** |
| 4899 | ** If a pointer to a MemPage object is passed as the second argument, |
| 4900 | ** its reference count is not altered by this function. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4901 | */ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4902 | static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){ |
| 4903 | MemPage *pTrunk = 0; /* Free-list trunk page */ |
| 4904 | Pgno iTrunk = 0; /* Page number of free-list trunk page */ |
| 4905 | MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */ |
| 4906 | MemPage *pPage; /* Page being freed. May be NULL. */ |
| 4907 | int rc; /* Return Code */ |
| 4908 | int nFree; /* Initial number of pages on free-list */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4909 | |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4910 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 4911 | assert( iPage>1 ); |
| 4912 | assert( !pMemPage || pMemPage->pgno==iPage ); |
| 4913 | |
| 4914 | if( pMemPage ){ |
| 4915 | pPage = pMemPage; |
| 4916 | sqlite3PagerRef(pPage->pDbPage); |
| 4917 | }else{ |
| 4918 | pPage = btreePageLookup(pBt, iPage); |
| 4919 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4920 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4921 | /* Increment the free page count on pPage1 */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4922 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4923 | if( rc ) goto freepage_out; |
| 4924 | nFree = get4byte(&pPage1->aData[36]); |
| 4925 | put4byte(&pPage1->aData[36], nFree+1); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4926 | |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 4927 | if( pBt->secureDelete ){ |
| 4928 | /* If the secure_delete option is enabled, then |
| 4929 | ** always fully overwrite deleted information with zeros. |
| 4930 | */ |
shaneh | 84f4b2f | 2010-02-26 01:46:54 +0000 | [diff] [blame^] | 4931 | if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) ) |
| 4932 | || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0) |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 4933 | ){ |
| 4934 | goto freepage_out; |
| 4935 | } |
| 4936 | memset(pPage->aData, 0, pPage->pBt->pageSize); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4937 | } |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4938 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4939 | /* If the database supports auto-vacuum, write an entry in the pointer-map |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4940 | ** to indicate that the page is free. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4941 | */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 4942 | if( ISAUTOVACUUM ){ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 4943 | ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4944 | if( rc ) goto freepage_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4945 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4946 | |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4947 | /* Now manipulate the actual database free-list structure. There are two |
| 4948 | ** possibilities. If the free-list is currently empty, or if the first |
| 4949 | ** trunk page in the free-list is full, then this page will become a |
| 4950 | ** new free-list trunk page. Otherwise, it will become a leaf of the |
| 4951 | ** first trunk page in the current free-list. This block tests if it |
| 4952 | ** is possible to add the page as a new free-list leaf. |
| 4953 | */ |
| 4954 | if( nFree!=0 ){ |
drh | c046e3e | 2009-07-15 11:26:44 +0000 | [diff] [blame] | 4955 | u32 nLeaf; /* Initial number of leaf cells on trunk page */ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4956 | |
| 4957 | iTrunk = get4byte(&pPage1->aData[32]); |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 4958 | rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4959 | if( rc!=SQLITE_OK ){ |
| 4960 | goto freepage_out; |
| 4961 | } |
| 4962 | |
| 4963 | nLeaf = get4byte(&pTrunk->aData[4]); |
drh | eeb844a | 2009-08-08 18:01:07 +0000 | [diff] [blame] | 4964 | assert( pBt->usableSize>32 ); |
| 4965 | if( nLeaf > (u32)pBt->usableSize/4 - 2 ){ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4966 | rc = SQLITE_CORRUPT_BKPT; |
| 4967 | goto freepage_out; |
| 4968 | } |
drh | eeb844a | 2009-08-08 18:01:07 +0000 | [diff] [blame] | 4969 | if( nLeaf < (u32)pBt->usableSize/4 - 8 ){ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4970 | /* In this case there is room on the trunk page to insert the page |
| 4971 | ** being freed as a new leaf. |
drh | 45b1fac | 2008-07-04 17:52:42 +0000 | [diff] [blame] | 4972 | ** |
| 4973 | ** Note that the trunk page is not really full until it contains |
| 4974 | ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have |
| 4975 | ** coded. But due to a coding error in versions of SQLite prior to |
| 4976 | ** 3.6.0, databases with freelist trunk pages holding more than |
| 4977 | ** usableSize/4 - 8 entries will be reported as corrupt. In order |
| 4978 | ** to maintain backwards compatibility with older versions of SQLite, |
drh | c046e3e | 2009-07-15 11:26:44 +0000 | [diff] [blame] | 4979 | ** we will continue to restrict the number of entries to usableSize/4 - 8 |
drh | 45b1fac | 2008-07-04 17:52:42 +0000 | [diff] [blame] | 4980 | ** for now. At some point in the future (once everyone has upgraded |
| 4981 | ** to 3.6.0 or later) we should consider fixing the conditional above |
| 4982 | ** to read "usableSize/4-2" instead of "usableSize/4-8". |
| 4983 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4984 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 4985 | if( rc==SQLITE_OK ){ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4986 | put4byte(&pTrunk->aData[4], nLeaf+1); |
| 4987 | put4byte(&pTrunk->aData[8+nLeaf*4], iPage); |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 4988 | if( pPage && !pBt->secureDelete ){ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4989 | sqlite3PagerDontWrite(pPage->pDbPage); |
| 4990 | } |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4991 | rc = btreeSetHasContent(pBt, iPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 4992 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4993 | 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] | 4994 | goto freepage_out; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4995 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4996 | } |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4997 | |
| 4998 | /* If control flows to this point, then it was not possible to add the |
| 4999 | ** the page being freed as a leaf page of the first trunk in the free-list. |
| 5000 | ** Possibly because the free-list is empty, or possibly because the |
| 5001 | ** first trunk in the free-list is full. Either way, the page being freed |
| 5002 | ** will become the new first trunk page in the free-list. |
| 5003 | */ |
drh | c046e3e | 2009-07-15 11:26:44 +0000 | [diff] [blame] | 5004 | if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){ |
| 5005 | goto freepage_out; |
| 5006 | } |
| 5007 | rc = sqlite3PagerWrite(pPage->pDbPage); |
| 5008 | if( rc!=SQLITE_OK ){ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5009 | goto freepage_out; |
| 5010 | } |
| 5011 | put4byte(pPage->aData, iTrunk); |
| 5012 | put4byte(&pPage->aData[4], 0); |
| 5013 | put4byte(&pPage1->aData[32], iPage); |
| 5014 | TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk)); |
| 5015 | |
| 5016 | freepage_out: |
| 5017 | if( pPage ){ |
| 5018 | pPage->isInit = 0; |
| 5019 | } |
| 5020 | releasePage(pPage); |
| 5021 | releasePage(pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5022 | return rc; |
| 5023 | } |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 5024 | static void freePage(MemPage *pPage, int *pRC){ |
| 5025 | if( (*pRC)==SQLITE_OK ){ |
| 5026 | *pRC = freePage2(pPage->pBt, pPage, pPage->pgno); |
| 5027 | } |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5028 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5029 | |
| 5030 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5031 | ** Free any overflow pages associated with the given Cell. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5032 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5033 | static int clearCell(MemPage *pPage, unsigned char *pCell){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5034 | BtShared *pBt = pPage->pBt; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5035 | CellInfo info; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5036 | Pgno ovflPgno; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5037 | int rc; |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 5038 | int nOvfl; |
shane | 63207ab | 2009-02-04 01:49:30 +0000 | [diff] [blame] | 5039 | u16 ovflPageSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5040 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5041 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 5042 | btreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5043 | if( info.iOverflow==0 ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5044 | return SQLITE_OK; /* No overflow pages. Return without doing anything */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5045 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5046 | ovflPgno = get4byte(&pCell[info.iOverflow]); |
shane | 63207ab | 2009-02-04 01:49:30 +0000 | [diff] [blame] | 5047 | assert( pBt->usableSize > 4 ); |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 5048 | ovflPageSize = pBt->usableSize - 4; |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 5049 | nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize; |
| 5050 | assert( ovflPgno==0 || nOvfl>0 ); |
| 5051 | while( nOvfl-- ){ |
shane | 63207ab | 2009-02-04 01:49:30 +0000 | [diff] [blame] | 5052 | Pgno iNext = 0; |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5053 | MemPage *pOvfl = 0; |
danielk1977 | e589a67 | 2009-04-11 16:06:15 +0000 | [diff] [blame] | 5054 | if( ovflPgno<2 || ovflPgno>pagerPagecount(pBt) ){ |
| 5055 | /* 0 is not a legal page number and page 1 cannot be an |
| 5056 | ** overflow page. Therefore if ovflPgno<2 or past the end of the |
| 5057 | ** file the database must be corrupt. */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 5058 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 5059 | } |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5060 | if( nOvfl ){ |
| 5061 | rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext); |
| 5062 | if( rc ) return rc; |
| 5063 | } |
dan | 887d4b2 | 2010-02-25 12:09:16 +0000 | [diff] [blame] | 5064 | |
| 5065 | if( (pOvfl || (pOvfl = btreePageLookup(pBt, ovflPgno))) |
| 5066 | && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1 |
| 5067 | ){ |
| 5068 | /* There is no reason any cursor should have an outstanding reference |
| 5069 | ** to an overflow page belonging to a cell that is being deleted/updated. |
| 5070 | ** So if there exists more than one reference to this page, then it |
| 5071 | ** must not really be an overflow page and the database must be corrupt. |
| 5072 | ** It is helpful to detect this before calling freePage2(), as |
| 5073 | ** freePage2() may zero the page contents if secure-delete mode is |
| 5074 | ** enabled. If this 'overflow' page happens to be a page that the |
| 5075 | ** caller is iterating through or using in some other way, this |
| 5076 | ** can be problematic. |
| 5077 | */ |
| 5078 | rc = SQLITE_CORRUPT_BKPT; |
| 5079 | }else{ |
| 5080 | rc = freePage2(pBt, pOvfl, ovflPgno); |
| 5081 | } |
| 5082 | |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5083 | if( pOvfl ){ |
| 5084 | sqlite3PagerUnref(pOvfl->pDbPage); |
| 5085 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5086 | if( rc ) return rc; |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5087 | ovflPgno = iNext; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5088 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5089 | return SQLITE_OK; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5090 | } |
| 5091 | |
| 5092 | /* |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5093 | ** Create the byte sequence used to represent a cell on page pPage |
| 5094 | ** and write that byte sequence into pCell[]. Overflow pages are |
| 5095 | ** allocated and filled in as necessary. The calling procedure |
| 5096 | ** is responsible for making sure sufficient space has been allocated |
| 5097 | ** for pCell[]. |
| 5098 | ** |
| 5099 | ** Note that pCell does not necessary need to point to the pPage->aData |
| 5100 | ** area. pCell might point to some temporary storage. The cell will |
| 5101 | ** be constructed in this temporary area then copied into pPage->aData |
| 5102 | ** later. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5103 | */ |
| 5104 | static int fillInCell( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5105 | MemPage *pPage, /* The page that contains the cell */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5106 | unsigned char *pCell, /* Complete text of the cell */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 5107 | const void *pKey, i64 nKey, /* The key */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5108 | const void *pData,int nData, /* The data */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5109 | int nZero, /* Extra zero bytes to append to pData */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5110 | int *pnSize /* Write cell size here */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5111 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5112 | int nPayload; |
drh | 8c6fa9b | 2004-05-26 00:01:53 +0000 | [diff] [blame] | 5113 | const u8 *pSrc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5114 | int nSrc, n, rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5115 | int spaceLeft; |
| 5116 | MemPage *pOvfl = 0; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 5117 | MemPage *pToRelease = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5118 | unsigned char *pPrior; |
| 5119 | unsigned char *pPayload; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5120 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5121 | Pgno pgnoOvfl = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5122 | int nHeader; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5123 | CellInfo info; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5124 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5125 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5126 | |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 5127 | /* pPage is not necessarily writeable since pCell might be auxiliary |
| 5128 | ** buffer space that is separate from the pPage buffer area */ |
| 5129 | assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize] |
| 5130 | || sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 5131 | |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5132 | /* Fill in the header. */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5133 | nHeader = 0; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5134 | if( !pPage->leaf ){ |
| 5135 | nHeader += 4; |
| 5136 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5137 | if( pPage->hasData ){ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5138 | nHeader += putVarint(&pCell[nHeader], nData+nZero); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5139 | }else{ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5140 | nData = nZero = 0; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5141 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5142 | nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey); |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 5143 | btreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5144 | assert( info.nHeader==nHeader ); |
| 5145 | assert( info.nKey==nKey ); |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 5146 | assert( info.nData==(u32)(nData+nZero) ); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5147 | |
| 5148 | /* Fill in the payload */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5149 | nPayload = nData + nZero; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5150 | if( pPage->intKey ){ |
| 5151 | pSrc = pData; |
| 5152 | nSrc = nData; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5153 | nData = 0; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 5154 | }else{ |
danielk1977 | 31d31b8 | 2009-07-13 13:18:07 +0000 | [diff] [blame] | 5155 | if( NEVER(nKey>0x7fffffff || pKey==0) ){ |
| 5156 | return SQLITE_CORRUPT_BKPT; |
drh | 20abac2 | 2009-01-28 20:21:17 +0000 | [diff] [blame] | 5157 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 5158 | nPayload += (int)nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5159 | pSrc = pKey; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 5160 | nSrc = (int)nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5161 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5162 | *pnSize = info.nSize; |
| 5163 | spaceLeft = info.nLocal; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5164 | pPayload = &pCell[nHeader]; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5165 | pPrior = &pCell[info.iOverflow]; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5166 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5167 | while( nPayload>0 ){ |
| 5168 | if( spaceLeft==0 ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5169 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5170 | Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */ |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 5171 | if( pBt->autoVacuum ){ |
| 5172 | do{ |
| 5173 | pgnoOvfl++; |
| 5174 | } while( |
| 5175 | PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt) |
| 5176 | ); |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 5177 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5178 | #endif |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 5179 | rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5180 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 5181 | /* If the database supports auto-vacuum, and the second or subsequent |
| 5182 | ** overflow page is being allocated, add an entry to the pointer-map |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 5183 | ** for that page now. |
| 5184 | ** |
| 5185 | ** If this is the first overflow page, then write a partial entry |
| 5186 | ** to the pointer-map. If we write nothing to this pointer-map slot, |
| 5187 | ** then the optimistic overflow chain processing in clearCell() |
| 5188 | ** may misinterpret the uninitialised values and delete the |
| 5189 | ** wrong pages from the database. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5190 | */ |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 5191 | if( pBt->autoVacuum && rc==SQLITE_OK ){ |
| 5192 | u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1); |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 5193 | ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc); |
danielk1977 | 89a4be8 | 2007-05-23 13:34:32 +0000 | [diff] [blame] | 5194 | if( rc ){ |
| 5195 | releasePage(pOvfl); |
| 5196 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5197 | } |
| 5198 | #endif |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5199 | if( rc ){ |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 5200 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5201 | return rc; |
| 5202 | } |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 5203 | |
| 5204 | /* If pToRelease is not zero than pPrior points into the data area |
| 5205 | ** of pToRelease. Make sure pToRelease is still writeable. */ |
| 5206 | assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) ); |
| 5207 | |
| 5208 | /* If pPrior is part of the data area of pPage, then make sure pPage |
| 5209 | ** is still writeable */ |
| 5210 | assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize] |
| 5211 | || sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 5212 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5213 | put4byte(pPrior, pgnoOvfl); |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 5214 | releasePage(pToRelease); |
| 5215 | pToRelease = pOvfl; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5216 | pPrior = pOvfl->aData; |
| 5217 | put4byte(pPrior, 0); |
| 5218 | pPayload = &pOvfl->aData[4]; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5219 | spaceLeft = pBt->usableSize - 4; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5220 | } |
| 5221 | n = nPayload; |
| 5222 | if( n>spaceLeft ) n = spaceLeft; |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 5223 | |
| 5224 | /* If pToRelease is not zero than pPayload points into the data area |
| 5225 | ** of pToRelease. Make sure pToRelease is still writeable. */ |
| 5226 | assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) ); |
| 5227 | |
| 5228 | /* If pPayload is part of the data area of pPage, then make sure pPage |
| 5229 | ** is still writeable */ |
| 5230 | assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize] |
| 5231 | || sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 5232 | |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5233 | if( nSrc>0 ){ |
| 5234 | if( n>nSrc ) n = nSrc; |
| 5235 | assert( pSrc ); |
| 5236 | memcpy(pPayload, pSrc, n); |
| 5237 | }else{ |
| 5238 | memset(pPayload, 0, n); |
| 5239 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5240 | nPayload -= n; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 5241 | pPayload += n; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 5242 | pSrc += n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5243 | nSrc -= n; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5244 | spaceLeft -= n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5245 | if( nSrc==0 ){ |
| 5246 | nSrc = nData; |
| 5247 | pSrc = pData; |
| 5248 | } |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 5249 | } |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 5250 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5251 | return SQLITE_OK; |
| 5252 | } |
| 5253 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5254 | /* |
| 5255 | ** Remove the i-th cell from pPage. This routine effects pPage only. |
| 5256 | ** The cell content is not freed or deallocated. It is assumed that |
| 5257 | ** the cell content has been copied someplace else. This routine just |
| 5258 | ** removes the reference to the cell from pPage. |
| 5259 | ** |
| 5260 | ** "sz" must be the number of bytes in the cell. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5261 | */ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 5262 | static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5263 | int i; /* Loop counter */ |
| 5264 | int pc; /* Offset to cell content of cell being deleted */ |
| 5265 | u8 *data; /* pPage->aData */ |
| 5266 | u8 *ptr; /* Used to move bytes around within data[] */ |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 5267 | int rc; /* The return code */ |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 5268 | int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5269 | |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 5270 | if( *pRC ) return; |
| 5271 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5272 | assert( idx>=0 && idx<pPage->nCell ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5273 | assert( sz==cellSize(pPage, idx) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5274 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5275 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5276 | data = pPage->aData; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5277 | ptr = &data[pPage->cellOffset + 2*idx]; |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 5278 | pc = get2byte(ptr); |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 5279 | hdr = pPage->hdrOffset; |
| 5280 | testcase( pc==get2byte(&data[hdr+5]) ); |
| 5281 | testcase( pc+sz==pPage->pBt->usableSize ); |
| 5282 | if( pc < get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 5283 | *pRC = SQLITE_CORRUPT_BKPT; |
| 5284 | return; |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 5285 | } |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 5286 | rc = freeSpace(pPage, pc, sz); |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 5287 | if( rc ){ |
| 5288 | *pRC = rc; |
| 5289 | return; |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 5290 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5291 | for(i=idx+1; i<pPage->nCell; i++, ptr+=2){ |
| 5292 | ptr[0] = ptr[2]; |
| 5293 | ptr[1] = ptr[3]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5294 | } |
| 5295 | pPage->nCell--; |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 5296 | put2byte(&data[hdr+3], pPage->nCell); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5297 | pPage->nFree += 2; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5298 | } |
| 5299 | |
| 5300 | /* |
| 5301 | ** Insert a new cell on pPage at cell index "i". pCell points to the |
| 5302 | ** content of the cell. |
| 5303 | ** |
| 5304 | ** 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] | 5305 | ** will not fit, then make a copy of the cell content into pTemp if |
| 5306 | ** pTemp is not null. Regardless of pTemp, allocate a new entry |
| 5307 | ** in pPage->aOvfl[] and make it point to the cell content (either |
| 5308 | ** in pTemp or the original pCell) and also record its index. |
| 5309 | ** Allocating a new entry in pPage->aCell[] implies that |
| 5310 | ** pPage->nOverflow is incremented. |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 5311 | ** |
| 5312 | ** If nSkip is non-zero, then do not copy the first nSkip bytes of the |
| 5313 | ** cell. The caller will overwrite them after this function returns. If |
drh | 4b238df | 2005-01-08 15:43:18 +0000 | [diff] [blame] | 5314 | ** 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] | 5315 | ** (but pCell+nSkip is always valid). |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5316 | */ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 5317 | static void insertCell( |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5318 | MemPage *pPage, /* Page into which we are copying */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5319 | int i, /* New cell becomes the i-th cell of the page */ |
| 5320 | u8 *pCell, /* Content of the new cell */ |
| 5321 | int sz, /* Bytes of content in pCell */ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 5322 | u8 *pTemp, /* Temp storage space for pCell, if needed */ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 5323 | Pgno iChild, /* If non-zero, replace first 4 bytes with this value */ |
| 5324 | int *pRC /* Read and write return code from here */ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5325 | ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5326 | int idx; /* Where to write new cell content in data[] */ |
| 5327 | int j; /* Loop counter */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5328 | int end; /* First byte past the last cell pointer in data[] */ |
| 5329 | int ins; /* Index in data[] where new cell pointer is inserted */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5330 | int cellOffset; /* Address of first cell pointer in data[] */ |
| 5331 | u8 *data; /* The content of the whole page */ |
| 5332 | u8 *ptr; /* Used for moving information around in data[] */ |
| 5333 | |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5334 | int nSkip = (iChild ? 4 : 0); |
| 5335 | |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 5336 | if( *pRC ) return; |
| 5337 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5338 | assert( i>=0 && i<=pPage->nCell+pPage->nOverflow ); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 5339 | assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=5460 ); |
| 5340 | assert( pPage->nOverflow<=ArraySize(pPage->aOvfl) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5341 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | c9b9b8a | 2009-12-03 21:26:52 +0000 | [diff] [blame] | 5342 | /* The cell should normally be sized correctly. However, when moving a |
| 5343 | ** malformed cell from a leaf page to an interior page, if the cell size |
| 5344 | ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size |
| 5345 | ** might be less than 8 (leaf-size + pointer) on the interior node. Hence |
| 5346 | ** the term after the || in the following assert(). */ |
| 5347 | assert( sz==cellSizePtr(pPage, pCell) || (sz==8 && iChild>0) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5348 | if( pPage->nOverflow || sz+2>pPage->nFree ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5349 | if( pTemp ){ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 5350 | memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5351 | pCell = pTemp; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5352 | } |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5353 | if( iChild ){ |
| 5354 | put4byte(pCell, iChild); |
| 5355 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5356 | j = pPage->nOverflow++; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 5357 | assert( j<(int)(sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0])) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5358 | pPage->aOvfl[j].pCell = pCell; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 5359 | pPage->aOvfl[j].idx = (u16)i; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5360 | }else{ |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 5361 | int rc = sqlite3PagerWrite(pPage->pDbPage); |
| 5362 | if( rc!=SQLITE_OK ){ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 5363 | *pRC = rc; |
| 5364 | return; |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 5365 | } |
| 5366 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5367 | data = pPage->aData; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5368 | cellOffset = pPage->cellOffset; |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 5369 | end = cellOffset + 2*pPage->nCell; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5370 | ins = cellOffset + 2*i; |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 5371 | rc = allocateSpace(pPage, sz, &idx); |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 5372 | if( rc ){ *pRC = rc; return; } |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 5373 | /* The allocateSpace() routine guarantees the following two properties |
| 5374 | ** if it returns success */ |
| 5375 | assert( idx >= end+2 ); |
| 5376 | assert( idx+sz <= pPage->pBt->usableSize ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5377 | pPage->nCell++; |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 5378 | pPage->nFree -= (u16)(2 + sz); |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 5379 | memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5380 | if( iChild ){ |
| 5381 | put4byte(&data[idx], iChild); |
| 5382 | } |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 5383 | for(j=end, ptr=&data[j]; j>ins; j-=2, ptr-=2){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5384 | ptr[0] = ptr[-2]; |
| 5385 | ptr[1] = ptr[-1]; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5386 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5387 | put2byte(&data[ins], idx); |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 5388 | put2byte(&data[pPage->hdrOffset+3], pPage->nCell); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 5389 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5390 | if( pPage->pBt->autoVacuum ){ |
| 5391 | /* The cell may contain a pointer to an overflow page. If so, write |
| 5392 | ** the entry for the overflow page into the pointer map. |
| 5393 | */ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 5394 | ptrmapPutOvflPtr(pPage, pCell, pRC); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 5395 | } |
| 5396 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5397 | } |
| 5398 | } |
| 5399 | |
| 5400 | /* |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 5401 | ** Add a list of cells to a page. The page should be initially empty. |
| 5402 | ** The cells are guaranteed to fit on the page. |
| 5403 | */ |
| 5404 | static void assemblePage( |
| 5405 | MemPage *pPage, /* The page to be assemblied */ |
| 5406 | int nCell, /* The number of cells to add to this page */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5407 | u8 **apCell, /* Pointers to cell bodies */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5408 | u16 *aSize /* Sizes of the cells */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 5409 | ){ |
| 5410 | int i; /* Loop counter */ |
danielk1977 | fad9194 | 2009-04-29 17:49:59 +0000 | [diff] [blame] | 5411 | u8 *pCellptr; /* Address of next cell pointer */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5412 | int cellbody; /* Address of next cell body */ |
danielk1977 | fad9194 | 2009-04-29 17:49:59 +0000 | [diff] [blame] | 5413 | u8 * const data = pPage->aData; /* Pointer to data for pPage */ |
| 5414 | const int hdr = pPage->hdrOffset; /* Offset of header on pPage */ |
| 5415 | const int nUsable = pPage->pBt->usableSize; /* Usable size of page */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 5416 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5417 | assert( pPage->nOverflow==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5418 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 5419 | assert( nCell>=0 && nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=5460 ); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 5420 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
danielk1977 | fad9194 | 2009-04-29 17:49:59 +0000 | [diff] [blame] | 5421 | |
| 5422 | /* Check that the page has just been zeroed by zeroPage() */ |
| 5423 | assert( pPage->nCell==0 ); |
| 5424 | assert( get2byte(&data[hdr+5])==nUsable ); |
| 5425 | |
| 5426 | pCellptr = &data[pPage->cellOffset + nCell*2]; |
| 5427 | cellbody = nUsable; |
| 5428 | for(i=nCell-1; i>=0; i--){ |
| 5429 | pCellptr -= 2; |
| 5430 | cellbody -= aSize[i]; |
| 5431 | put2byte(pCellptr, cellbody); |
| 5432 | memcpy(&data[cellbody], apCell[i], aSize[i]); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 5433 | } |
danielk1977 | fad9194 | 2009-04-29 17:49:59 +0000 | [diff] [blame] | 5434 | put2byte(&data[hdr+3], nCell); |
| 5435 | put2byte(&data[hdr+5], cellbody); |
| 5436 | pPage->nFree -= (nCell*2 + nUsable - cellbody); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 5437 | pPage->nCell = (u16)nCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 5438 | } |
| 5439 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5440 | /* |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5441 | ** The following parameters determine how many adjacent pages get involved |
| 5442 | ** in a balancing operation. NN is the number of neighbors on either side |
| 5443 | ** of the page that participate in the balancing operation. NB is the |
| 5444 | ** total number of pages that participate, including the target page and |
| 5445 | ** NN neighbors on either side. |
| 5446 | ** |
| 5447 | ** The minimum value of NN is 1 (of course). Increasing NN above 1 |
| 5448 | ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance |
| 5449 | ** in exchange for a larger degradation in INSERT and UPDATE performance. |
| 5450 | ** The value of NN appears to give the best results overall. |
| 5451 | */ |
| 5452 | #define NN 1 /* Number of neighbors on either side of pPage */ |
| 5453 | #define NB (NN*2+1) /* Total pages involved in the balance */ |
| 5454 | |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5455 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 5456 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 5457 | /* |
| 5458 | ** This version of balance() handles the common special case where |
| 5459 | ** a new entry is being inserted on the extreme right-end of the |
| 5460 | ** tree, in other words, when the new entry will become the largest |
| 5461 | ** entry in the tree. |
| 5462 | ** |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 5463 | ** Instead of trying to balance the 3 right-most leaf pages, just add |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 5464 | ** a new page to the right-hand side and put the one new entry in |
| 5465 | ** that page. This leaves the right side of the tree somewhat |
| 5466 | ** unbalanced. But odds are that we will be inserting new entries |
| 5467 | ** at the end soon afterwards so the nearly empty page will quickly |
| 5468 | ** fill up. On average. |
| 5469 | ** |
| 5470 | ** pPage is the leaf page which is the right-most page in the tree. |
| 5471 | ** pParent is its parent. pPage must have a single overflow entry |
| 5472 | ** which is also the right-most entry on the page. |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 5473 | ** |
| 5474 | ** The pSpace buffer is used to store a temporary copy of the divider |
| 5475 | ** cell that will be inserted into pParent. Such a cell consists of a 4 |
| 5476 | ** byte page number followed by a variable length integer. In other |
| 5477 | ** words, at most 13 bytes. Hence the pSpace buffer must be at |
| 5478 | ** least 13 bytes in size. |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 5479 | */ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 5480 | static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){ |
| 5481 | BtShared *const pBt = pPage->pBt; /* B-Tree Database */ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5482 | MemPage *pNew; /* Newly allocated page */ |
danielk1977 | 6f235cc | 2009-06-04 14:46:08 +0000 | [diff] [blame] | 5483 | int rc; /* Return Code */ |
| 5484 | Pgno pgnoNew; /* Page number of pNew */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5485 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5486 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 5487 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
danielk1977 | e56b60e | 2009-06-10 09:11:06 +0000 | [diff] [blame] | 5488 | assert( pPage->nOverflow==1 ); |
| 5489 | |
drh | 5d1a872 | 2009-07-22 18:07:40 +0000 | [diff] [blame] | 5490 | if( pPage->nCell<=0 ) return SQLITE_CORRUPT_BKPT; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5491 | |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 5492 | /* Allocate a new page. This page will become the right-sibling of |
| 5493 | ** pPage. Make the parent page writable, so that the new divider cell |
| 5494 | ** may be inserted. If both these operations are successful, proceed. |
| 5495 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5496 | rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5497 | |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 5498 | if( rc==SQLITE_OK ){ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 5499 | |
| 5500 | u8 *pOut = &pSpace[4]; |
danielk1977 | 6f235cc | 2009-06-04 14:46:08 +0000 | [diff] [blame] | 5501 | u8 *pCell = pPage->aOvfl[0].pCell; |
| 5502 | u16 szCell = cellSizePtr(pPage, pCell); |
| 5503 | u8 *pStop; |
| 5504 | |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 5505 | assert( sqlite3PagerIswriteable(pNew->pDbPage) ); |
danielk1977 | e56b60e | 2009-06-10 09:11:06 +0000 | [diff] [blame] | 5506 | assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) ); |
| 5507 | zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF); |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 5508 | assemblePage(pNew, 1, &pCell, &szCell); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5509 | |
| 5510 | /* If this is an auto-vacuum database, update the pointer map |
| 5511 | ** with entries for the new page, and any pointer from the |
| 5512 | ** cell on the page to an overflow page. If either of these |
| 5513 | ** operations fails, the return code is set, but the contents |
| 5514 | ** of the parent page are still manipulated by thh code below. |
| 5515 | ** That is Ok, at this point the parent page is guaranteed to |
| 5516 | ** be marked as dirty. Returning an error code will cause a |
| 5517 | ** rollback, undoing any changes made to the parent page. |
| 5518 | */ |
| 5519 | if( ISAUTOVACUUM ){ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 5520 | ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc); |
| 5521 | if( szCell>pNew->minLocal ){ |
| 5522 | ptrmapPutOvflPtr(pNew, pCell, &rc); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5523 | } |
| 5524 | } |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 5525 | |
danielk1977 | 6f235cc | 2009-06-04 14:46:08 +0000 | [diff] [blame] | 5526 | /* Create a divider cell to insert into pParent. The divider cell |
| 5527 | ** consists of a 4-byte page number (the page number of pPage) and |
| 5528 | ** a variable length key value (which must be the same value as the |
| 5529 | ** largest key on pPage). |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 5530 | ** |
danielk1977 | 6f235cc | 2009-06-04 14:46:08 +0000 | [diff] [blame] | 5531 | ** To find the largest key value on pPage, first find the right-most |
| 5532 | ** cell on pPage. The first two fields of this cell are the |
| 5533 | ** record-length (a variable length integer at most 32-bits in size) |
| 5534 | ** and the key value (a variable length integer, may have any value). |
| 5535 | ** The first of the while(...) loops below skips over the record-length |
| 5536 | ** field. The second while(...) loop copies the key value from the |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 5537 | ** cell on pPage into the pSpace buffer. |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 5538 | */ |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 5539 | pCell = findCell(pPage, pPage->nCell-1); |
danielk1977 | 6f235cc | 2009-06-04 14:46:08 +0000 | [diff] [blame] | 5540 | pStop = &pCell[9]; |
| 5541 | while( (*(pCell++)&0x80) && pCell<pStop ); |
| 5542 | pStop = &pCell[9]; |
| 5543 | while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop ); |
| 5544 | |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5545 | /* Insert the new divider cell into pParent. */ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 5546 | insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace), |
| 5547 | 0, pPage->pgno, &rc); |
danielk1977 | 6f235cc | 2009-06-04 14:46:08 +0000 | [diff] [blame] | 5548 | |
| 5549 | /* Set the right-child pointer of pParent to point to the new page. */ |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 5550 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew); |
| 5551 | |
danielk1977 | e08a3c4 | 2008-09-18 18:17:03 +0000 | [diff] [blame] | 5552 | /* Release the reference to the new page. */ |
| 5553 | releasePage(pNew); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5554 | } |
| 5555 | |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 5556 | return rc; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5557 | } |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 5558 | #endif /* SQLITE_OMIT_QUICKBALANCE */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5559 | |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5560 | #if 0 |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5561 | /* |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5562 | ** This function does not contribute anything to the operation of SQLite. |
| 5563 | ** it is sometimes activated temporarily while debugging code responsible |
| 5564 | ** for setting pointer-map entries. |
| 5565 | */ |
| 5566 | static int ptrmapCheckPages(MemPage **apPage, int nPage){ |
| 5567 | int i, j; |
| 5568 | for(i=0; i<nPage; i++){ |
| 5569 | Pgno n; |
| 5570 | u8 e; |
| 5571 | MemPage *pPage = apPage[i]; |
| 5572 | BtShared *pBt = pPage->pBt; |
| 5573 | assert( pPage->isInit ); |
| 5574 | |
| 5575 | for(j=0; j<pPage->nCell; j++){ |
| 5576 | CellInfo info; |
| 5577 | u8 *z; |
| 5578 | |
| 5579 | z = findCell(pPage, j); |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 5580 | btreeParseCellPtr(pPage, z, &info); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5581 | if( info.iOverflow ){ |
| 5582 | Pgno ovfl = get4byte(&z[info.iOverflow]); |
| 5583 | ptrmapGet(pBt, ovfl, &e, &n); |
| 5584 | assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 ); |
| 5585 | } |
| 5586 | if( !pPage->leaf ){ |
| 5587 | Pgno child = get4byte(z); |
| 5588 | ptrmapGet(pBt, child, &e, &n); |
| 5589 | assert( n==pPage->pgno && e==PTRMAP_BTREE ); |
| 5590 | } |
| 5591 | } |
| 5592 | if( !pPage->leaf ){ |
| 5593 | Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 5594 | ptrmapGet(pBt, child, &e, &n); |
| 5595 | assert( n==pPage->pgno && e==PTRMAP_BTREE ); |
| 5596 | } |
| 5597 | } |
| 5598 | return 1; |
| 5599 | } |
| 5600 | #endif |
| 5601 | |
danielk1977 | cd581a7 | 2009-06-23 15:43:39 +0000 | [diff] [blame] | 5602 | /* |
| 5603 | ** This function is used to copy the contents of the b-tree node stored |
| 5604 | ** on page pFrom to page pTo. If page pFrom was not a leaf page, then |
| 5605 | ** the pointer-map entries for each child page are updated so that the |
| 5606 | ** parent page stored in the pointer map is page pTo. If pFrom contained |
| 5607 | ** any cells with overflow page pointers, then the corresponding pointer |
| 5608 | ** map entries are also updated so that the parent page is page pTo. |
| 5609 | ** |
| 5610 | ** If pFrom is currently carrying any overflow cells (entries in the |
| 5611 | ** MemPage.aOvfl[] array), they are not copied to pTo. |
| 5612 | ** |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 5613 | ** Before returning, page pTo is reinitialized using btreeInitPage(). |
danielk1977 | cd581a7 | 2009-06-23 15:43:39 +0000 | [diff] [blame] | 5614 | ** |
| 5615 | ** The performance of this function is not critical. It is only used by |
| 5616 | ** the balance_shallower() and balance_deeper() procedures, neither of |
| 5617 | ** which are called often under normal circumstances. |
| 5618 | */ |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 5619 | static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){ |
| 5620 | if( (*pRC)==SQLITE_OK ){ |
| 5621 | BtShared * const pBt = pFrom->pBt; |
| 5622 | u8 * const aFrom = pFrom->aData; |
| 5623 | u8 * const aTo = pTo->aData; |
| 5624 | int const iFromHdr = pFrom->hdrOffset; |
| 5625 | int const iToHdr = ((pTo->pgno==1) ? 100 : 0); |
drh | dc9b5f8 | 2009-12-05 18:34:08 +0000 | [diff] [blame] | 5626 | int rc; |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 5627 | int iData; |
| 5628 | |
| 5629 | |
| 5630 | assert( pFrom->isInit ); |
| 5631 | assert( pFrom->nFree>=iToHdr ); |
| 5632 | assert( get2byte(&aFrom[iFromHdr+5])<=pBt->usableSize ); |
| 5633 | |
| 5634 | /* Copy the b-tree node content from page pFrom to page pTo. */ |
| 5635 | iData = get2byte(&aFrom[iFromHdr+5]); |
| 5636 | memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData); |
| 5637 | memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell); |
| 5638 | |
| 5639 | /* Reinitialize page pTo so that the contents of the MemPage structure |
dan | 89e060e | 2009-12-05 18:03:50 +0000 | [diff] [blame] | 5640 | ** match the new data. The initialization of pTo can actually fail under |
| 5641 | ** fairly obscure circumstances, even though it is a copy of initialized |
| 5642 | ** page pFrom. |
| 5643 | */ |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 5644 | pTo->isInit = 0; |
dan | 89e060e | 2009-12-05 18:03:50 +0000 | [diff] [blame] | 5645 | rc = btreeInitPage(pTo); |
| 5646 | if( rc!=SQLITE_OK ){ |
| 5647 | *pRC = rc; |
| 5648 | return; |
| 5649 | } |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 5650 | |
| 5651 | /* If this is an auto-vacuum database, update the pointer-map entries |
| 5652 | ** for any b-tree or overflow pages that pTo now contains the pointers to. |
| 5653 | */ |
| 5654 | if( ISAUTOVACUUM ){ |
| 5655 | *pRC = setChildPtrmaps(pTo); |
| 5656 | } |
danielk1977 | cd581a7 | 2009-06-23 15:43:39 +0000 | [diff] [blame] | 5657 | } |
danielk1977 | cd581a7 | 2009-06-23 15:43:39 +0000 | [diff] [blame] | 5658 | } |
| 5659 | |
| 5660 | /* |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5661 | ** This routine redistributes cells on the iParentIdx'th child of pParent |
| 5662 | ** (hereafter "the page") and up to 2 siblings so that all pages have about the |
| 5663 | ** same amount of free space. Usually a single sibling on either side of the |
| 5664 | ** page are used in the balancing, though both siblings might come from one |
| 5665 | ** side if the page is the first or last child of its parent. If the page |
| 5666 | ** has fewer than 2 siblings (something which can only happen if the page |
| 5667 | ** is a root page or a child of a root page) then all available siblings |
| 5668 | ** participate in the balancing. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5669 | ** |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5670 | ** The number of siblings of the page might be increased or decreased by |
| 5671 | ** one or two in an effort to keep pages nearly full but not over full. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5672 | ** |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5673 | ** Note that when this routine is called, some of the cells on the page |
| 5674 | ** might not actually be stored in MemPage.aData[]. This can happen |
| 5675 | ** if the page is overfull. This routine ensures that all cells allocated |
| 5676 | ** to the page and its siblings fit into MemPage.aData[] before returning. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5677 | ** |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5678 | ** In the course of balancing the page and its siblings, cells may be |
| 5679 | ** inserted into or removed from the parent page (pParent). Doing so |
| 5680 | ** may cause the parent page to become overfull or underfull. If this |
| 5681 | ** happens, it is the responsibility of the caller to invoke the correct |
| 5682 | ** balancing routine to fix this problem (see the balance() routine). |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5683 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5684 | ** If this routine fails for any reason, it might leave the database |
danielk1977 | 6067a9b | 2009-06-09 09:41:00 +0000 | [diff] [blame] | 5685 | ** in a corrupted state. So if this routine fails, the database should |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5686 | ** be rolled back. |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5687 | ** |
| 5688 | ** The third argument to this function, aOvflSpace, is a pointer to a |
drh | cd09c53 | 2009-07-20 19:30:00 +0000 | [diff] [blame] | 5689 | ** buffer big enough to hold one page. If while inserting cells into the parent |
| 5690 | ** page (pParent) the parent page becomes overfull, this buffer is |
| 5691 | ** used to store the parent's overflow cells. Because this function inserts |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5692 | ** a maximum of four divider cells into the parent page, and the maximum |
| 5693 | ** size of a cell stored within an internal node is always less than 1/4 |
| 5694 | ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large |
| 5695 | ** enough for all overflow cells. |
| 5696 | ** |
| 5697 | ** If aOvflSpace is set to a null pointer, this function returns |
| 5698 | ** SQLITE_NOMEM. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5699 | */ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5700 | static int balance_nonroot( |
| 5701 | MemPage *pParent, /* Parent page of siblings being balanced */ |
| 5702 | int iParentIdx, /* Index of "the page" in pParent */ |
danielk1977 | cd581a7 | 2009-06-23 15:43:39 +0000 | [diff] [blame] | 5703 | u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */ |
| 5704 | int isRoot /* True if pParent is a root-page */ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5705 | ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5706 | BtShared *pBt; /* The whole database */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5707 | int nCell = 0; /* Number of cells in apCell[] */ |
| 5708 | int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */ |
danielk1977 | a4124bd | 2008-12-23 10:37:47 +0000 | [diff] [blame] | 5709 | int nNew = 0; /* Number of pages in apNew[] */ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5710 | int nOld; /* Number of pages in apOld[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5711 | int i, j, k; /* Loop counters */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5712 | int nxDiv; /* Next divider slot in pParent->aCell[] */ |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 5713 | int rc = SQLITE_OK; /* The return code */ |
shane | 36840fd | 2009-06-26 16:32:13 +0000 | [diff] [blame] | 5714 | u16 leafCorrection; /* 4 if pPage is a leaf. 0 if not */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5715 | int leafData; /* True if pPage is a leaf of a LEAFDATA tree */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5716 | int usableSpace; /* Bytes in pPage beyond the header */ |
| 5717 | int pageFlags; /* Value of pPage->aData[0] */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5718 | int subtotal; /* Subtotal of bytes in cells on one page */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5719 | int iSpace1 = 0; /* First unused byte of aSpace1[] */ |
danielk1977 | 6067a9b | 2009-06-09 09:41:00 +0000 | [diff] [blame] | 5720 | int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5721 | int szScratch; /* Size of scratch memory requested */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5722 | MemPage *apOld[NB]; /* pPage and up to two siblings */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5723 | MemPage *apCopy[NB]; /* Private copies of apOld[] pages */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5724 | MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5725 | u8 *pRight; /* Location in parent of right-sibling pointer */ |
| 5726 | u8 *apDiv[NB-1]; /* Divider cells in pParent */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5727 | int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */ |
| 5728 | int szNew[NB+2]; /* Combined size of cells place on i-th page */ |
danielk1977 | 50f059b | 2005-03-29 02:54:03 +0000 | [diff] [blame] | 5729 | u8 **apCell = 0; /* All cells begin balanced */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5730 | u16 *szCell; /* Local size of all cells in apCell[] */ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5731 | u8 *aSpace1; /* Space for copies of dividers cells */ |
| 5732 | Pgno pgno; /* Temp var to store a page number in */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5733 | |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 5734 | pBt = pParent->pBt; |
| 5735 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 5736 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
danielk1977 | 474b7cc | 2008-07-09 11:49:46 +0000 | [diff] [blame] | 5737 | |
danielk1977 | e576521 | 2009-06-17 11:13:28 +0000 | [diff] [blame] | 5738 | #if 0 |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5739 | TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno)); |
danielk1977 | e576521 | 2009-06-17 11:13:28 +0000 | [diff] [blame] | 5740 | #endif |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5741 | |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5742 | /* At this point pParent may have at most one overflow cell. And if |
| 5743 | ** this overflow cell is present, it must be the cell with |
| 5744 | ** index iParentIdx. This scenario comes about when this function |
drh | cd09c53 | 2009-07-20 19:30:00 +0000 | [diff] [blame] | 5745 | ** is called (indirectly) from sqlite3BtreeDelete(). |
| 5746 | */ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5747 | assert( pParent->nOverflow==0 || pParent->nOverflow==1 ); |
| 5748 | assert( pParent->nOverflow==0 || pParent->aOvfl[0].idx==iParentIdx ); |
| 5749 | |
danielk1977 | 11a8a86 | 2009-06-17 11:49:52 +0000 | [diff] [blame] | 5750 | if( !aOvflSpace ){ |
| 5751 | return SQLITE_NOMEM; |
| 5752 | } |
| 5753 | |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 5754 | /* Find the sibling pages to balance. Also locate the cells in pParent |
| 5755 | ** that divide the siblings. An attempt is made to find NN siblings on |
| 5756 | ** either side of pPage. More siblings are taken from one side, however, |
| 5757 | ** if there are fewer than NN siblings on the other side. If pParent |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5758 | ** has NB or fewer children then all children of pParent are taken. |
| 5759 | ** |
| 5760 | ** This loop also drops the divider cells from the parent page. This |
| 5761 | ** way, the remainder of the function does not have to deal with any |
drh | cd09c53 | 2009-07-20 19:30:00 +0000 | [diff] [blame] | 5762 | ** overflow cells in the parent page, since if any existed they will |
| 5763 | ** have already been removed. |
| 5764 | */ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5765 | i = pParent->nOverflow + pParent->nCell; |
| 5766 | if( i<2 ){ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5767 | nxDiv = 0; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5768 | nOld = i+1; |
| 5769 | }else{ |
| 5770 | nOld = 3; |
| 5771 | if( iParentIdx==0 ){ |
| 5772 | nxDiv = 0; |
| 5773 | }else if( iParentIdx==i ){ |
| 5774 | nxDiv = i-2; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5775 | }else{ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5776 | nxDiv = iParentIdx-1; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5777 | } |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5778 | i = 2; |
| 5779 | } |
| 5780 | if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){ |
| 5781 | pRight = &pParent->aData[pParent->hdrOffset+8]; |
| 5782 | }else{ |
| 5783 | pRight = findCell(pParent, i+nxDiv-pParent->nOverflow); |
| 5784 | } |
| 5785 | pgno = get4byte(pRight); |
| 5786 | while( 1 ){ |
| 5787 | rc = getAndInitPage(pBt, pgno, &apOld[i]); |
| 5788 | if( rc ){ |
danielk1977 | 89bc4bc | 2009-07-21 19:25:24 +0000 | [diff] [blame] | 5789 | memset(apOld, 0, (i+1)*sizeof(MemPage*)); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5790 | goto balance_cleanup; |
| 5791 | } |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5792 | nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5793 | if( (i--)==0 ) break; |
| 5794 | |
drh | cd09c53 | 2009-07-20 19:30:00 +0000 | [diff] [blame] | 5795 | if( i+nxDiv==pParent->aOvfl[0].idx && pParent->nOverflow ){ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5796 | apDiv[i] = pParent->aOvfl[0].pCell; |
| 5797 | pgno = get4byte(apDiv[i]); |
| 5798 | szNew[i] = cellSizePtr(pParent, apDiv[i]); |
| 5799 | pParent->nOverflow = 0; |
| 5800 | }else{ |
| 5801 | apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow); |
| 5802 | pgno = get4byte(apDiv[i]); |
| 5803 | szNew[i] = cellSizePtr(pParent, apDiv[i]); |
| 5804 | |
| 5805 | /* Drop the cell from the parent page. apDiv[i] still points to |
| 5806 | ** the cell within the parent, even though it has been dropped. |
| 5807 | ** This is safe because dropping a cell only overwrites the first |
| 5808 | ** four bytes of it, and this function does not need the first |
| 5809 | ** four bytes of the divider cell. So the pointer is safe to use |
danielk1977 | 11a8a86 | 2009-06-17 11:49:52 +0000 | [diff] [blame] | 5810 | ** later on. |
| 5811 | ** |
| 5812 | ** Unless SQLite is compiled in secure-delete mode. In this case, |
| 5813 | ** the dropCell() routine will overwrite the entire cell with zeroes. |
| 5814 | ** In this case, temporarily copy the cell into the aOvflSpace[] |
| 5815 | ** buffer. It will be copied out again as soon as the aSpace[] buffer |
| 5816 | ** is allocated. */ |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 5817 | if( pBt->secureDelete ){ |
| 5818 | memcpy(&aOvflSpace[apDiv[i]-pParent->aData], apDiv[i], szNew[i]); |
| 5819 | apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData]; |
| 5820 | } |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 5821 | dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5822 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5823 | } |
| 5824 | |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5825 | /* Make nMaxCells a multiple of 4 in order to preserve 8-byte |
drh | 8d97f1f | 2005-05-05 18:14:13 +0000 | [diff] [blame] | 5826 | ** alignment */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5827 | nMaxCells = (nMaxCells + 3)&~3; |
drh | 8d97f1f | 2005-05-05 18:14:13 +0000 | [diff] [blame] | 5828 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5829 | /* |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5830 | ** Allocate space for memory structures |
| 5831 | */ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5832 | k = pBt->pageSize + ROUND8(sizeof(MemPage)); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5833 | szScratch = |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5834 | nMaxCells*sizeof(u8*) /* apCell */ |
| 5835 | + nMaxCells*sizeof(u16) /* szCell */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5836 | + pBt->pageSize /* aSpace1 */ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5837 | + k*nOld; /* Page copies (apCopy) */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 5838 | apCell = sqlite3ScratchMalloc( szScratch ); |
danielk1977 | 11a8a86 | 2009-06-17 11:49:52 +0000 | [diff] [blame] | 5839 | if( apCell==0 ){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5840 | rc = SQLITE_NOMEM; |
| 5841 | goto balance_cleanup; |
| 5842 | } |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5843 | szCell = (u16*)&apCell[nMaxCells]; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5844 | aSpace1 = (u8*)&szCell[nMaxCells]; |
drh | ea598cb | 2009-04-05 12:22:08 +0000 | [diff] [blame] | 5845 | assert( EIGHT_BYTE_ALIGNMENT(aSpace1) ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5846 | |
| 5847 | /* |
| 5848 | ** Load pointers to all cells on sibling pages and the divider cells |
| 5849 | ** into the local apCell[] array. Make copies of the divider cells |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5850 | ** into space obtained from aSpace1[] and remove the the divider Cells |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5851 | ** from pParent. |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5852 | ** |
| 5853 | ** If the siblings are on leaf pages, then the child pointers of the |
| 5854 | ** divider cells are stripped from the cells before they are copied |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 5855 | ** into aSpace1[]. In this way, all cells in apCell[] are without |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5856 | ** child pointers. If siblings are not leaves, then all cell in |
| 5857 | ** apCell[] include child pointers. Either way, all cells in apCell[] |
| 5858 | ** are alike. |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5859 | ** |
| 5860 | ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf. |
| 5861 | ** leafData: 1 if pPage holds key+data and pParent holds only keys. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5862 | */ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 5863 | leafCorrection = apOld[0]->leaf*4; |
| 5864 | leafData = apOld[0]->hasData; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5865 | for(i=0; i<nOld; i++){ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5866 | int limit; |
| 5867 | |
| 5868 | /* Before doing anything else, take a copy of the i'th original sibling |
| 5869 | ** The rest of this function will use data from the copies rather |
| 5870 | ** that the original pages since the original pages will be in the |
| 5871 | ** process of being overwritten. */ |
| 5872 | MemPage *pOld = apCopy[i] = (MemPage*)&aSpace1[pBt->pageSize + k*i]; |
| 5873 | memcpy(pOld, apOld[i], sizeof(MemPage)); |
| 5874 | pOld->aData = (void*)&pOld[1]; |
| 5875 | memcpy(pOld->aData, apOld[i]->aData, pBt->pageSize); |
| 5876 | |
| 5877 | limit = pOld->nCell+pOld->nOverflow; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5878 | for(j=0; j<limit; j++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5879 | assert( nCell<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5880 | apCell[nCell] = findOverflowCell(pOld, j); |
| 5881 | szCell[nCell] = cellSizePtr(pOld, apCell[nCell]); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5882 | nCell++; |
| 5883 | } |
| 5884 | if( i<nOld-1 && !leafData){ |
shane | 36840fd | 2009-06-26 16:32:13 +0000 | [diff] [blame] | 5885 | u16 sz = (u16)szNew[i]; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5886 | u8 *pTemp; |
| 5887 | assert( nCell<nMaxCells ); |
| 5888 | szCell[nCell] = sz; |
| 5889 | pTemp = &aSpace1[iSpace1]; |
| 5890 | iSpace1 += sz; |
| 5891 | assert( sz<=pBt->pageSize/4 ); |
| 5892 | assert( iSpace1<=pBt->pageSize ); |
| 5893 | memcpy(pTemp, apDiv[i], sz); |
| 5894 | apCell[nCell] = pTemp+leafCorrection; |
| 5895 | assert( leafCorrection==0 || leafCorrection==4 ); |
shane | 36840fd | 2009-06-26 16:32:13 +0000 | [diff] [blame] | 5896 | szCell[nCell] = szCell[nCell] - leafCorrection; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5897 | if( !pOld->leaf ){ |
| 5898 | assert( leafCorrection==0 ); |
| 5899 | assert( pOld->hdrOffset==0 ); |
| 5900 | /* The right pointer of the child page pOld becomes the left |
| 5901 | ** pointer of the divider cell */ |
| 5902 | memcpy(apCell[nCell], &pOld->aData[8], 4); |
| 5903 | }else{ |
| 5904 | assert( leafCorrection==4 ); |
| 5905 | if( szCell[nCell]<4 ){ |
| 5906 | /* Do not allow any cells smaller than 4 bytes. */ |
| 5907 | szCell[nCell] = 4; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5908 | } |
| 5909 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5910 | nCell++; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5911 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5912 | } |
| 5913 | |
| 5914 | /* |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5915 | ** Figure out the number of pages needed to hold all nCell cells. |
| 5916 | ** Store this number in "k". Also compute szNew[] which is the total |
| 5917 | ** 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] | 5918 | ** in apCell[] of the cell that divides page i from page i+1. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5919 | ** cntNew[k] should equal nCell. |
| 5920 | ** |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5921 | ** Values computed by this block: |
| 5922 | ** |
| 5923 | ** k: The total number of sibling pages |
| 5924 | ** szNew[i]: Spaced used on the i-th sibling page. |
| 5925 | ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to |
| 5926 | ** the right of the i-th sibling page. |
| 5927 | ** usableSpace: Number of bytes of space available on each sibling. |
| 5928 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5929 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5930 | usableSpace = pBt->usableSize - 12 + leafCorrection; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5931 | for(subtotal=k=i=0; i<nCell; i++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5932 | assert( i<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5933 | subtotal += szCell[i] + 2; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5934 | if( subtotal > usableSpace ){ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5935 | szNew[k] = subtotal - szCell[i]; |
| 5936 | cntNew[k] = i; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5937 | if( leafData ){ i--; } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5938 | subtotal = 0; |
| 5939 | k++; |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 5940 | if( k>NB+1 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5941 | } |
| 5942 | } |
| 5943 | szNew[k] = subtotal; |
| 5944 | cntNew[k] = nCell; |
| 5945 | k++; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5946 | |
| 5947 | /* |
| 5948 | ** The packing computed by the previous block is biased toward the siblings |
| 5949 | ** on the left side. The left siblings are always nearly full, while the |
| 5950 | ** right-most sibling might be nearly empty. This block of code attempts |
| 5951 | ** to adjust the packing of siblings to get a better balance. |
| 5952 | ** |
| 5953 | ** This adjustment is more than an optimization. The packing above might |
| 5954 | ** be so out of balance as to be illegal. For example, the right-most |
| 5955 | ** sibling might be completely empty. This adjustment is not optional. |
| 5956 | */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5957 | for(i=k-1; i>0; i--){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5958 | int szRight = szNew[i]; /* Size of sibling on the right */ |
| 5959 | int szLeft = szNew[i-1]; /* Size of sibling on the left */ |
| 5960 | int r; /* Index of right-most cell in left sibling */ |
| 5961 | int d; /* Index of first cell to the left of right sibling */ |
| 5962 | |
| 5963 | r = cntNew[i-1] - 1; |
| 5964 | d = r + 1 - leafData; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5965 | assert( d<nMaxCells ); |
| 5966 | assert( r<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5967 | while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){ |
| 5968 | szRight += szCell[d] + 2; |
| 5969 | szLeft -= szCell[r] + 2; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5970 | cntNew[i-1]--; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5971 | r = cntNew[i-1] - 1; |
| 5972 | d = r + 1 - leafData; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5973 | } |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5974 | szNew[i] = szRight; |
| 5975 | szNew[i-1] = szLeft; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5976 | } |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 5977 | |
danielk1977 | 6f235cc | 2009-06-04 14:46:08 +0000 | [diff] [blame] | 5978 | /* Either we found one or more cells (cntnew[0])>0) or pPage is |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 5979 | ** a virtual root page. A virtual root page is when the real root |
| 5980 | ** page is page 1 and we are the only child of that page. |
| 5981 | */ |
| 5982 | assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5983 | |
danielk1977 | e576521 | 2009-06-17 11:13:28 +0000 | [diff] [blame] | 5984 | TRACE(("BALANCE: old: %d %d %d ", |
| 5985 | apOld[0]->pgno, |
| 5986 | nOld>=2 ? apOld[1]->pgno : 0, |
| 5987 | nOld>=3 ? apOld[2]->pgno : 0 |
| 5988 | )); |
| 5989 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5990 | /* |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5991 | ** Allocate k new pages. Reuse old pages where possible. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5992 | */ |
drh | eac7442 | 2009-06-14 12:47:11 +0000 | [diff] [blame] | 5993 | if( apOld[0]->pgno<=1 ){ |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 5994 | rc = SQLITE_CORRUPT_BKPT; |
drh | eac7442 | 2009-06-14 12:47:11 +0000 | [diff] [blame] | 5995 | goto balance_cleanup; |
| 5996 | } |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 5997 | pageFlags = apOld[0]->aData[0]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5998 | for(i=0; i<k; i++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5999 | MemPage *pNew; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 6000 | if( i<nOld ){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6001 | pNew = apNew[i] = apOld[i]; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 6002 | apOld[i] = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6003 | rc = sqlite3PagerWrite(pNew->pDbPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 6004 | nNew++; |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 6005 | if( rc ) goto balance_cleanup; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 6006 | }else{ |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 6007 | assert( i>0 ); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6008 | rc = allocateBtreePage(pBt, &pNew, &pgno, pgno, 0); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 6009 | if( rc ) goto balance_cleanup; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6010 | apNew[i] = pNew; |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 6011 | nNew++; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6012 | |
| 6013 | /* Set the pointer-map entry for the new sibling page. */ |
| 6014 | if( ISAUTOVACUUM ){ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6015 | ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6016 | if( rc!=SQLITE_OK ){ |
| 6017 | goto balance_cleanup; |
| 6018 | } |
| 6019 | } |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 6020 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6021 | } |
| 6022 | |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 6023 | /* Free any old pages that were not reused as new pages. |
| 6024 | */ |
| 6025 | while( i<nOld ){ |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 6026 | freePage(apOld[i], &rc); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 6027 | if( rc ) goto balance_cleanup; |
| 6028 | releasePage(apOld[i]); |
| 6029 | apOld[i] = 0; |
| 6030 | i++; |
| 6031 | } |
| 6032 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6033 | /* |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 6034 | ** Put the new pages in accending order. This helps to |
| 6035 | ** keep entries in the disk file in order so that a scan |
| 6036 | ** of the table is a linear scan through the file. That |
| 6037 | ** in turn helps the operating system to deliver pages |
| 6038 | ** from the disk more rapidly. |
| 6039 | ** |
| 6040 | ** An O(n^2) insertion sort algorithm is used, but since |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 6041 | ** n is never more than NB (a small constant), that should |
| 6042 | ** not be a problem. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 6043 | ** |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 6044 | ** When NB==3, this one optimization makes the database |
| 6045 | ** about 25% faster for large insertions and deletions. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 6046 | */ |
| 6047 | for(i=0; i<k-1; i++){ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6048 | int minV = apNew[i]->pgno; |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 6049 | int minI = i; |
| 6050 | for(j=i+1; j<k; j++){ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6051 | if( apNew[j]->pgno<(unsigned)minV ){ |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 6052 | minI = j; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6053 | minV = apNew[j]->pgno; |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 6054 | } |
| 6055 | } |
| 6056 | if( minI>i ){ |
| 6057 | int t; |
| 6058 | MemPage *pT; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6059 | t = apNew[i]->pgno; |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 6060 | pT = apNew[i]; |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 6061 | apNew[i] = apNew[minI]; |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 6062 | apNew[minI] = pT; |
| 6063 | } |
| 6064 | } |
danielk1977 | e576521 | 2009-06-17 11:13:28 +0000 | [diff] [blame] | 6065 | TRACE(("new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n", |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6066 | apNew[0]->pgno, szNew[0], |
| 6067 | nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0, |
| 6068 | nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0, |
| 6069 | nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0, |
| 6070 | nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0)); |
| 6071 | |
| 6072 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
| 6073 | put4byte(pRight, apNew[nNew-1]->pgno); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 6074 | |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 6075 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6076 | ** Evenly distribute the data in apCell[] across the new pages. |
| 6077 | ** Insert divider cells into pParent as necessary. |
| 6078 | */ |
| 6079 | j = 0; |
| 6080 | for(i=0; i<nNew; i++){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 6081 | /* Assemble the new sibling page. */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6082 | MemPage *pNew = apNew[i]; |
drh | 19642e5 | 2005-03-29 13:17:45 +0000 | [diff] [blame] | 6083 | assert( j<nMaxCells ); |
drh | 1013148 | 2008-07-11 03:34:09 +0000 | [diff] [blame] | 6084 | zeroPage(pNew, pageFlags); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 6085 | assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]); |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 6086 | assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6087 | assert( pNew->nOverflow==0 ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 6088 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 6089 | j = cntNew[i]; |
| 6090 | |
| 6091 | /* If the sibling page assembled above was not the right-most sibling, |
| 6092 | ** insert a divider cell into the parent page. |
| 6093 | */ |
danielk1977 | 1c3d2bf | 2009-06-23 16:40:17 +0000 | [diff] [blame] | 6094 | assert( i<nNew-1 || j==nCell ); |
| 6095 | if( j<nCell ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 6096 | u8 *pCell; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 6097 | u8 *pTemp; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 6098 | int sz; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 6099 | |
| 6100 | assert( j<nMaxCells ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 6101 | pCell = apCell[j]; |
| 6102 | sz = szCell[j] + leafCorrection; |
danielk1977 | 6067a9b | 2009-06-09 09:41:00 +0000 | [diff] [blame] | 6103 | pTemp = &aOvflSpace[iOvflSpace]; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6104 | if( !pNew->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6105 | memcpy(&pNew->aData[8], pCell, 4); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 6106 | }else if( leafData ){ |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 6107 | /* If the tree is a leaf-data tree, and the siblings are leaves, |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 6108 | ** then there is no divider cell in apCell[]. Instead, the divider |
| 6109 | ** cell consists of the integer key for the right-most cell of |
| 6110 | ** the sibling-page assembled above only. |
| 6111 | */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 6112 | CellInfo info; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 6113 | j--; |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 6114 | btreeParseCellPtr(pNew, apCell[j], &info); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 6115 | pCell = pTemp; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6116 | sz = 4 + putVarint(&pCell[4], info.nKey); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 6117 | pTemp = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6118 | }else{ |
| 6119 | pCell -= 4; |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 6120 | /* Obscure case for non-leaf-data trees: If the cell at pCell was |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 6121 | ** previously stored on a leaf node, and its reported size was 4 |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 6122 | ** bytes, then it may actually be smaller than this |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 6123 | ** (see btreeParseCellPtr(), 4 bytes is the minimum size of |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 6124 | ** any cell). But it is important to pass the correct size to |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 6125 | ** insertCell(), so reparse the cell now. |
| 6126 | ** |
| 6127 | ** Note that this can never happen in an SQLite data file, as all |
| 6128 | ** cells are at least 4 bytes. It only happens in b-trees used |
| 6129 | ** to evaluate "IN (SELECT ...)" and similar clauses. |
| 6130 | */ |
| 6131 | if( szCell[j]==4 ){ |
| 6132 | assert(leafCorrection==4); |
| 6133 | sz = cellSizePtr(pParent, pCell); |
| 6134 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6135 | } |
danielk1977 | 6067a9b | 2009-06-09 09:41:00 +0000 | [diff] [blame] | 6136 | iOvflSpace += sz; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 6137 | assert( sz<=pBt->pageSize/4 ); |
danielk1977 | 6067a9b | 2009-06-09 09:41:00 +0000 | [diff] [blame] | 6138 | assert( iOvflSpace<=pBt->pageSize ); |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6139 | insertCell(pParent, nxDiv, pCell, sz, pTemp, pNew->pgno, &rc); |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 6140 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 6141 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 6142 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6143 | j++; |
| 6144 | nxDiv++; |
| 6145 | } |
| 6146 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6147 | assert( j==nCell ); |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 6148 | assert( nOld>0 ); |
| 6149 | assert( nNew>0 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6150 | if( (pageFlags & PTF_LEAF)==0 ){ |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 6151 | u8 *zChild = &apCopy[nOld-1]->aData[8]; |
| 6152 | memcpy(&apNew[nNew-1]->aData[8], zChild, 4); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6153 | } |
| 6154 | |
danielk1977 | 13bd99f | 2009-06-24 05:40:34 +0000 | [diff] [blame] | 6155 | if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){ |
| 6156 | /* The root page of the b-tree now contains no cells. The only sibling |
| 6157 | ** page is the right-child of the parent. Copy the contents of the |
| 6158 | ** child page into the parent, decreasing the overall height of the |
| 6159 | ** b-tree structure by one. This is described as the "balance-shallower" |
| 6160 | ** sub-algorithm in some documentation. |
| 6161 | ** |
| 6162 | ** If this is an auto-vacuum database, the call to copyNodeContent() |
| 6163 | ** sets all pointer-map entries corresponding to database image pages |
| 6164 | ** for which the pointer is stored within the content being copied. |
| 6165 | ** |
| 6166 | ** The second assert below verifies that the child page is defragmented |
| 6167 | ** (it must be, as it was just reconstructed using assemblePage()). This |
| 6168 | ** is important if the parent page happens to be page 1 of the database |
| 6169 | ** image. */ |
| 6170 | assert( nNew==1 ); |
| 6171 | assert( apNew[0]->nFree == |
| 6172 | (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2) |
| 6173 | ); |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 6174 | copyNodeContent(apNew[0], pParent, &rc); |
| 6175 | freePage(apNew[0], &rc); |
danielk1977 | 13bd99f | 2009-06-24 05:40:34 +0000 | [diff] [blame] | 6176 | }else if( ISAUTOVACUUM ){ |
| 6177 | /* Fix the pointer-map entries for all the cells that were shifted around. |
| 6178 | ** There are several different types of pointer-map entries that need to |
| 6179 | ** be dealt with by this routine. Some of these have been set already, but |
| 6180 | ** many have not. The following is a summary: |
| 6181 | ** |
| 6182 | ** 1) The entries associated with new sibling pages that were not |
| 6183 | ** siblings when this function was called. These have already |
| 6184 | ** been set. We don't need to worry about old siblings that were |
| 6185 | ** moved to the free-list - the freePage() code has taken care |
| 6186 | ** of those. |
| 6187 | ** |
| 6188 | ** 2) The pointer-map entries associated with the first overflow |
| 6189 | ** page in any overflow chains used by new divider cells. These |
| 6190 | ** have also already been taken care of by the insertCell() code. |
| 6191 | ** |
| 6192 | ** 3) If the sibling pages are not leaves, then the child pages of |
| 6193 | ** cells stored on the sibling pages may need to be updated. |
| 6194 | ** |
| 6195 | ** 4) If the sibling pages are not internal intkey nodes, then any |
| 6196 | ** overflow pages used by these cells may need to be updated |
| 6197 | ** (internal intkey nodes never contain pointers to overflow pages). |
| 6198 | ** |
| 6199 | ** 5) If the sibling pages are not leaves, then the pointer-map |
| 6200 | ** entries for the right-child pages of each sibling may need |
| 6201 | ** to be updated. |
| 6202 | ** |
| 6203 | ** Cases 1 and 2 are dealt with above by other code. The next |
| 6204 | ** block deals with cases 3 and 4 and the one after that, case 5. Since |
| 6205 | ** setting a pointer map entry is a relatively expensive operation, this |
| 6206 | ** code only sets pointer map entries for child or overflow pages that have |
| 6207 | ** actually moved between pages. */ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6208 | MemPage *pNew = apNew[0]; |
| 6209 | MemPage *pOld = apCopy[0]; |
| 6210 | int nOverflow = pOld->nOverflow; |
| 6211 | int iNextOld = pOld->nCell + nOverflow; |
| 6212 | int iOverflow = (nOverflow ? pOld->aOvfl[0].idx : -1); |
| 6213 | j = 0; /* Current 'old' sibling page */ |
| 6214 | k = 0; /* Current 'new' sibling page */ |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 6215 | for(i=0; i<nCell; i++){ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6216 | int isDivider = 0; |
| 6217 | while( i==iNextOld ){ |
| 6218 | /* Cell i is the cell immediately following the last cell on old |
| 6219 | ** sibling page j. If the siblings are not leaf pages of an |
| 6220 | ** intkey b-tree, then cell i was a divider cell. */ |
| 6221 | pOld = apCopy[++j]; |
| 6222 | iNextOld = i + !leafData + pOld->nCell + pOld->nOverflow; |
| 6223 | if( pOld->nOverflow ){ |
| 6224 | nOverflow = pOld->nOverflow; |
| 6225 | iOverflow = i + !leafData + pOld->aOvfl[0].idx; |
| 6226 | } |
| 6227 | isDivider = !leafData; |
| 6228 | } |
| 6229 | |
| 6230 | assert(nOverflow>0 || iOverflow<i ); |
| 6231 | assert(nOverflow<2 || pOld->aOvfl[0].idx==pOld->aOvfl[1].idx-1); |
| 6232 | assert(nOverflow<3 || pOld->aOvfl[1].idx==pOld->aOvfl[2].idx-1); |
| 6233 | if( i==iOverflow ){ |
| 6234 | isDivider = 1; |
| 6235 | if( (--nOverflow)>0 ){ |
| 6236 | iOverflow++; |
| 6237 | } |
| 6238 | } |
| 6239 | |
| 6240 | if( i==cntNew[k] ){ |
| 6241 | /* Cell i is the cell immediately following the last cell on new |
| 6242 | ** sibling page k. If the siblings are not leaf pages of an |
| 6243 | ** intkey b-tree, then cell i is a divider cell. */ |
| 6244 | pNew = apNew[++k]; |
| 6245 | if( !leafData ) continue; |
| 6246 | } |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6247 | assert( j<nOld ); |
| 6248 | assert( k<nNew ); |
| 6249 | |
| 6250 | /* If the cell was originally divider cell (and is not now) or |
| 6251 | ** an overflow cell, or if the cell was located on a different sibling |
| 6252 | ** page before the balancing, then the pointer map entries associated |
| 6253 | ** with any child or overflow pages need to be updated. */ |
| 6254 | if( isDivider || pOld->pgno!=pNew->pgno ){ |
| 6255 | if( !leafCorrection ){ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6256 | ptrmapPut(pBt, get4byte(apCell[i]), PTRMAP_BTREE, pNew->pgno, &rc); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6257 | } |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6258 | if( szCell[i]>pNew->minLocal ){ |
| 6259 | ptrmapPutOvflPtr(pNew, apCell[i], &rc); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6260 | } |
| 6261 | } |
| 6262 | } |
| 6263 | |
| 6264 | if( !leafCorrection ){ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6265 | for(i=0; i<nNew; i++){ |
| 6266 | u32 key = get4byte(&apNew[i]->aData[8]); |
| 6267 | ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6268 | } |
| 6269 | } |
| 6270 | |
| 6271 | #if 0 |
| 6272 | /* The ptrmapCheckPages() contains assert() statements that verify that |
| 6273 | ** all pointer map pages are set correctly. This is helpful while |
| 6274 | ** debugging. This is usually disabled because a corrupt database may |
| 6275 | ** cause an assert() statement to fail. */ |
| 6276 | ptrmapCheckPages(apNew, nNew); |
| 6277 | ptrmapCheckPages(&pParent, 1); |
| 6278 | #endif |
| 6279 | } |
| 6280 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6281 | assert( pParent->isInit ); |
danielk1977 | e576521 | 2009-06-17 11:13:28 +0000 | [diff] [blame] | 6282 | TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n", |
| 6283 | nOld, nNew, nCell)); |
danielk1977 | cd581a7 | 2009-06-23 15:43:39 +0000 | [diff] [blame] | 6284 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6285 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6286 | ** Cleanup before returning. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6287 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6288 | balance_cleanup: |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 6289 | sqlite3ScratchFree(apCell); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6290 | for(i=0; i<nOld; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 6291 | releasePage(apOld[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6292 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6293 | for(i=0; i<nNew; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 6294 | releasePage(apNew[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6295 | } |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 6296 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6297 | return rc; |
| 6298 | } |
| 6299 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6300 | |
| 6301 | /* |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6302 | ** This function is called when the root page of a b-tree structure is |
| 6303 | ** overfull (has one or more overflow pages). |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6304 | ** |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6305 | ** A new child page is allocated and the contents of the current root |
| 6306 | ** page, including overflow cells, are copied into the child. The root |
| 6307 | ** page is then overwritten to make it an empty page with the right-child |
| 6308 | ** pointer pointing to the new page. |
| 6309 | ** |
| 6310 | ** Before returning, all pointer-map entries corresponding to pages |
| 6311 | ** that the new child-page now contains pointers to are updated. The |
| 6312 | ** entry corresponding to the new right-child pointer of the root |
| 6313 | ** page is also updated. |
| 6314 | ** |
| 6315 | ** If successful, *ppChild is set to contain a reference to the child |
| 6316 | ** page and SQLITE_OK is returned. In this case the caller is required |
| 6317 | ** to call releasePage() on *ppChild exactly once. If an error occurs, |
| 6318 | ** an error code is returned and *ppChild is set to 0. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6319 | */ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6320 | static int balance_deeper(MemPage *pRoot, MemPage **ppChild){ |
| 6321 | int rc; /* Return value from subprocedures */ |
| 6322 | MemPage *pChild = 0; /* Pointer to a new child page */ |
shane | 5eff7cf | 2009-08-10 03:57:58 +0000 | [diff] [blame] | 6323 | Pgno pgnoChild = 0; /* Page number of the new child page */ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6324 | BtShared *pBt = pRoot->pBt; /* The BTree */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6325 | |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6326 | assert( pRoot->nOverflow>0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6327 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 6328 | |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6329 | /* Make pRoot, the root page of the b-tree, writable. Allocate a new |
| 6330 | ** page that will become the new right-child of pPage. Copy the contents |
| 6331 | ** of the node stored on pRoot into the new child page. |
| 6332 | */ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6333 | rc = sqlite3PagerWrite(pRoot->pDbPage); |
| 6334 | if( rc==SQLITE_OK ){ |
| 6335 | rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0); |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 6336 | copyNodeContent(pRoot, pChild, &rc); |
| 6337 | if( ISAUTOVACUUM ){ |
| 6338 | ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc); |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6339 | } |
| 6340 | } |
| 6341 | if( rc ){ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6342 | *ppChild = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6343 | releasePage(pChild); |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6344 | return rc; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6345 | } |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6346 | assert( sqlite3PagerIswriteable(pChild->pDbPage) ); |
| 6347 | assert( sqlite3PagerIswriteable(pRoot->pDbPage) ); |
| 6348 | assert( pChild->nCell==pRoot->nCell ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6349 | |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6350 | TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno)); |
| 6351 | |
| 6352 | /* Copy the overflow cells from pRoot to pChild */ |
| 6353 | memcpy(pChild->aOvfl, pRoot->aOvfl, pRoot->nOverflow*sizeof(pRoot->aOvfl[0])); |
| 6354 | pChild->nOverflow = pRoot->nOverflow; |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6355 | |
| 6356 | /* Zero the contents of pRoot. Then install pChild as the right-child. */ |
| 6357 | zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF); |
| 6358 | put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild); |
| 6359 | |
| 6360 | *ppChild = pChild; |
| 6361 | return SQLITE_OK; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6362 | } |
| 6363 | |
| 6364 | /* |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6365 | ** The page that pCur currently points to has just been modified in |
| 6366 | ** some way. This function figures out if this modification means the |
| 6367 | ** tree needs to be balanced, and if so calls the appropriate balancing |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6368 | ** routine. Balancing routines are: |
| 6369 | ** |
| 6370 | ** balance_quick() |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6371 | ** balance_deeper() |
| 6372 | ** balance_nonroot() |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6373 | */ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6374 | static int balance(BtCursor *pCur){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6375 | int rc = SQLITE_OK; |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6376 | const int nMin = pCur->pBt->usableSize * 2 / 3; |
| 6377 | u8 aBalanceQuickSpace[13]; |
| 6378 | u8 *pFree = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6379 | |
shane | 75ac1de | 2009-06-09 18:58:52 +0000 | [diff] [blame] | 6380 | TESTONLY( int balance_quick_called = 0 ); |
| 6381 | TESTONLY( int balance_deeper_called = 0 ); |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6382 | |
| 6383 | do { |
| 6384 | int iPage = pCur->iPage; |
| 6385 | MemPage *pPage = pCur->apPage[iPage]; |
| 6386 | |
| 6387 | if( iPage==0 ){ |
| 6388 | if( pPage->nOverflow ){ |
| 6389 | /* The root page of the b-tree is overfull. In this case call the |
| 6390 | ** balance_deeper() function to create a new child for the root-page |
| 6391 | ** and copy the current contents of the root-page to it. The |
| 6392 | ** next iteration of the do-loop will balance the child page. |
| 6393 | */ |
| 6394 | assert( (balance_deeper_called++)==0 ); |
| 6395 | rc = balance_deeper(pPage, &pCur->apPage[1]); |
| 6396 | if( rc==SQLITE_OK ){ |
| 6397 | pCur->iPage = 1; |
| 6398 | pCur->aiIdx[0] = 0; |
| 6399 | pCur->aiIdx[1] = 0; |
| 6400 | assert( pCur->apPage[1]->nOverflow ); |
| 6401 | } |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6402 | }else{ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6403 | break; |
| 6404 | } |
| 6405 | }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){ |
| 6406 | break; |
| 6407 | }else{ |
| 6408 | MemPage * const pParent = pCur->apPage[iPage-1]; |
| 6409 | int const iIdx = pCur->aiIdx[iPage-1]; |
| 6410 | |
| 6411 | rc = sqlite3PagerWrite(pParent->pDbPage); |
| 6412 | if( rc==SQLITE_OK ){ |
| 6413 | #ifndef SQLITE_OMIT_QUICKBALANCE |
| 6414 | if( pPage->hasData |
| 6415 | && pPage->nOverflow==1 |
| 6416 | && pPage->aOvfl[0].idx==pPage->nCell |
| 6417 | && pParent->pgno!=1 |
| 6418 | && pParent->nCell==iIdx |
| 6419 | ){ |
| 6420 | /* Call balance_quick() to create a new sibling of pPage on which |
| 6421 | ** to store the overflow cell. balance_quick() inserts a new cell |
| 6422 | ** into pParent, which may cause pParent overflow. If this |
| 6423 | ** happens, the next interation of the do-loop will balance pParent |
| 6424 | ** use either balance_nonroot() or balance_deeper(). Until this |
| 6425 | ** happens, the overflow cell is stored in the aBalanceQuickSpace[] |
| 6426 | ** buffer. |
| 6427 | ** |
| 6428 | ** The purpose of the following assert() is to check that only a |
| 6429 | ** single call to balance_quick() is made for each call to this |
| 6430 | ** function. If this were not verified, a subtle bug involving reuse |
| 6431 | ** of the aBalanceQuickSpace[] might sneak in. |
| 6432 | */ |
| 6433 | assert( (balance_quick_called++)==0 ); |
| 6434 | rc = balance_quick(pParent, pPage, aBalanceQuickSpace); |
| 6435 | }else |
| 6436 | #endif |
| 6437 | { |
| 6438 | /* In this case, call balance_nonroot() to redistribute cells |
| 6439 | ** between pPage and up to 2 of its sibling pages. This involves |
| 6440 | ** modifying the contents of pParent, which may cause pParent to |
| 6441 | ** become overfull or underfull. The next iteration of the do-loop |
| 6442 | ** will balance the parent page to correct this. |
| 6443 | ** |
| 6444 | ** If the parent page becomes overfull, the overflow cell or cells |
| 6445 | ** are stored in the pSpace buffer allocated immediately below. |
| 6446 | ** A subsequent iteration of the do-loop will deal with this by |
| 6447 | ** calling balance_nonroot() (balance_deeper() may be called first, |
| 6448 | ** but it doesn't deal with overflow cells - just moves them to a |
| 6449 | ** different page). Once this subsequent call to balance_nonroot() |
| 6450 | ** has completed, it is safe to release the pSpace buffer used by |
| 6451 | ** the previous call, as the overflow cell data will have been |
| 6452 | ** copied either into the body of a database page or into the new |
| 6453 | ** pSpace buffer passed to the latter call to balance_nonroot(). |
| 6454 | */ |
| 6455 | u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize); |
danielk1977 | cd581a7 | 2009-06-23 15:43:39 +0000 | [diff] [blame] | 6456 | rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1); |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6457 | if( pFree ){ |
| 6458 | /* If pFree is not NULL, it points to the pSpace buffer used |
| 6459 | ** by a previous call to balance_nonroot(). Its contents are |
| 6460 | ** now stored either on real database pages or within the |
| 6461 | ** new pSpace buffer, so it may be safely freed here. */ |
| 6462 | sqlite3PageFree(pFree); |
| 6463 | } |
| 6464 | |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6465 | /* The pSpace buffer will be freed after the next call to |
| 6466 | ** balance_nonroot(), or just before this function returns, whichever |
| 6467 | ** comes first. */ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6468 | pFree = pSpace; |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6469 | } |
| 6470 | } |
| 6471 | |
| 6472 | pPage->nOverflow = 0; |
| 6473 | |
| 6474 | /* The next iteration of the do-loop balances the parent page. */ |
| 6475 | releasePage(pPage); |
| 6476 | pCur->iPage--; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6477 | } |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6478 | }while( rc==SQLITE_OK ); |
| 6479 | |
| 6480 | if( pFree ){ |
| 6481 | sqlite3PageFree(pFree); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6482 | } |
| 6483 | return rc; |
| 6484 | } |
| 6485 | |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 6486 | |
| 6487 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 6488 | ** Insert a new record into the BTree. The key is given by (pKey,nKey) |
| 6489 | ** 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] | 6490 | ** define what table the record should be inserted into. The cursor |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6491 | ** is left pointing at a random location. |
| 6492 | ** |
| 6493 | ** For an INTKEY table, only the nKey value of the key is used. pKey is |
| 6494 | ** ignored. For a ZERODATA table, the pData and nData are both ignored. |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 6495 | ** |
| 6496 | ** If the seekResult parameter is non-zero, then a successful call to |
danielk1977 | 3509a65 | 2009-07-06 18:56:13 +0000 | [diff] [blame] | 6497 | ** MovetoUnpacked() to seek cursor pCur to (pKey, nKey) has already |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 6498 | ** been performed. seekResult is the search result returned (a negative |
| 6499 | ** number if pCur points at an entry that is smaller than (pKey, nKey), or |
| 6500 | ** a positive value if pCur points at an etry that is larger than |
| 6501 | ** (pKey, nKey)). |
| 6502 | ** |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 6503 | ** If the seekResult parameter is non-zero, then the caller guarantees that |
| 6504 | ** cursor pCur is pointing at the existing copy of a row that is to be |
| 6505 | ** overwritten. If the seekResult parameter is 0, then cursor pCur may |
| 6506 | ** point to any entry or to no entry at all and so this function has to seek |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 6507 | ** the cursor before the new key can be inserted. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 6508 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 6509 | int sqlite3BtreeInsert( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 6510 | BtCursor *pCur, /* Insert data into the table of this cursor */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 6511 | const void *pKey, i64 nKey, /* The key of the new record */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 6512 | const void *pData, int nData, /* The data of the new record */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 6513 | int nZero, /* Number of extra 0 bytes to append to data */ |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 6514 | int appendBias, /* True if this is likely an append */ |
danielk1977 | 3509a65 | 2009-07-06 18:56:13 +0000 | [diff] [blame] | 6515 | int seekResult /* Result of prior MovetoUnpacked() call */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 6516 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 6517 | int rc; |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 6518 | int loc = seekResult; /* -1: before desired location +1: after */ |
drh | 1d452e1 | 2009-11-01 19:26:59 +0000 | [diff] [blame] | 6519 | int szNew = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6520 | int idx; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 6521 | MemPage *pPage; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6522 | Btree *p = pCur->pBtree; |
| 6523 | BtShared *pBt = p->pBt; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6524 | unsigned char *oldCell; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6525 | unsigned char *newCell = 0; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 6526 | |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6527 | if( pCur->eState==CURSOR_FAULT ){ |
| 6528 | assert( pCur->skipNext!=SQLITE_OK ); |
| 6529 | return pCur->skipNext; |
| 6530 | } |
| 6531 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6532 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | 31d31b8 | 2009-07-13 13:18:07 +0000 | [diff] [blame] | 6533 | assert( pCur->wrFlag && pBt->inTransaction==TRANS_WRITE && !pBt->readOnly ); |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 6534 | assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) ); |
| 6535 | |
danielk1977 | 31d31b8 | 2009-07-13 13:18:07 +0000 | [diff] [blame] | 6536 | /* Assert that the caller has been consistent. If this cursor was opened |
| 6537 | ** expecting an index b-tree, then the caller should be inserting blob |
| 6538 | ** keys with no associated data. If the cursor was opened expecting an |
| 6539 | ** intkey table, the caller should be inserting integer keys with a |
| 6540 | ** blob of associated data. */ |
| 6541 | assert( (pKey==0)==(pCur->pKeyInfo==0) ); |
| 6542 | |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 6543 | /* If this is an insert into a table b-tree, invalidate any incrblob |
| 6544 | ** cursors open on the row being replaced (assuming this is a replace |
| 6545 | ** operation - if it is not, the following is a no-op). */ |
| 6546 | if( pCur->pKeyInfo==0 ){ |
drh | eeb844a | 2009-08-08 18:01:07 +0000 | [diff] [blame] | 6547 | invalidateIncrblobCursors(p, nKey, 0); |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 6548 | } |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 6549 | |
danielk1977 | 9c3acf3 | 2009-05-02 07:36:49 +0000 | [diff] [blame] | 6550 | /* Save the positions of any other cursors open on this table. |
| 6551 | ** |
danielk1977 | 3509a65 | 2009-07-06 18:56:13 +0000 | [diff] [blame] | 6552 | ** In some cases, the call to btreeMoveto() below is a no-op. For |
danielk1977 | 9c3acf3 | 2009-05-02 07:36:49 +0000 | [diff] [blame] | 6553 | ** example, when inserting data into a table with auto-generated integer |
| 6554 | ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the |
| 6555 | ** integer key to use. It then calls this function to actually insert the |
danielk1977 | 3509a65 | 2009-07-06 18:56:13 +0000 | [diff] [blame] | 6556 | ** data into the intkey B-Tree. In this case btreeMoveto() recognizes |
danielk1977 | 9c3acf3 | 2009-05-02 07:36:49 +0000 | [diff] [blame] | 6557 | ** that the cursor is already where it needs to be and returns without |
| 6558 | ** doing any work. To avoid thwarting these optimizations, it is important |
| 6559 | ** not to clear the cursor here. |
| 6560 | */ |
drh | 4c301aa | 2009-07-15 17:25:45 +0000 | [diff] [blame] | 6561 | rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur); |
| 6562 | if( rc ) return rc; |
| 6563 | if( !loc ){ |
| 6564 | rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc); |
| 6565 | if( rc ) return rc; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6566 | } |
danielk1977 | b980d221 | 2009-06-22 18:03:51 +0000 | [diff] [blame] | 6567 | assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6568 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6569 | pPage = pCur->apPage[pCur->iPage]; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 6570 | assert( pPage->intKey || nKey>=0 ); |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 6571 | assert( pPage->leaf || !pPage->intKey ); |
danielk1977 | 8f880a8 | 2009-07-13 09:41:45 +0000 | [diff] [blame] | 6572 | |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6573 | TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n", |
| 6574 | pCur->pgnoRoot, nKey, nData, pPage->pgno, |
| 6575 | loc==0 ? "overwrite" : "new entry")); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6576 | assert( pPage->isInit ); |
danielk1977 | 52ae724 | 2008-03-25 14:24:56 +0000 | [diff] [blame] | 6577 | allocateTempSpace(pBt); |
| 6578 | newCell = pBt->pTmpSpace; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6579 | if( newCell==0 ) return SQLITE_NOMEM; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 6580 | rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6581 | if( rc ) goto end_insert; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6582 | assert( szNew==cellSizePtr(pPage, newCell) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6583 | assert( szNew<=MX_CELL_SIZE(pBt) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6584 | idx = pCur->aiIdx[pCur->iPage]; |
danielk1977 | b980d221 | 2009-06-22 18:03:51 +0000 | [diff] [blame] | 6585 | if( loc==0 ){ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 6586 | u16 szOld; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6587 | assert( idx<pPage->nCell ); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 6588 | rc = sqlite3PagerWrite(pPage->pDbPage); |
| 6589 | if( rc ){ |
| 6590 | goto end_insert; |
| 6591 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6592 | oldCell = findCell(pPage, idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6593 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6594 | memcpy(newCell, oldCell, 4); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6595 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6596 | szOld = cellSizePtr(pPage, oldCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6597 | rc = clearCell(pPage, oldCell); |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6598 | dropCell(pPage, idx, szOld, &rc); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6599 | if( rc ) goto end_insert; |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 6600 | }else if( loc<0 && pPage->nCell>0 ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6601 | assert( pPage->leaf ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6602 | idx = ++pCur->aiIdx[pCur->iPage]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6603 | }else{ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6604 | assert( pPage->leaf ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 6605 | } |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6606 | insertCell(pPage, idx, newCell, szNew, 0, 0, &rc); |
danielk1977 | 3f632d5 | 2009-05-02 10:03:09 +0000 | [diff] [blame] | 6607 | assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 ); |
drh | 9bf9e9c | 2008-12-05 20:01:43 +0000 | [diff] [blame] | 6608 | |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6609 | /* If no error has occured and pPage has an overflow cell, call balance() |
| 6610 | ** to redistribute the cells within the tree. Since balance() may move |
| 6611 | ** the cursor, zero the BtCursor.info.nSize and BtCursor.validNKey |
| 6612 | ** variables. |
danielk1977 | 3f632d5 | 2009-05-02 10:03:09 +0000 | [diff] [blame] | 6613 | ** |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6614 | ** Previous versions of SQLite called moveToRoot() to move the cursor |
| 6615 | ** back to the root page as balance() used to invalidate the contents |
danielk1977 | 54109bb | 2009-06-23 11:22:29 +0000 | [diff] [blame] | 6616 | ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that, |
| 6617 | ** set the cursor state to "invalid". This makes common insert operations |
| 6618 | ** slightly faster. |
danielk1977 | 3f632d5 | 2009-05-02 10:03:09 +0000 | [diff] [blame] | 6619 | ** |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6620 | ** There is a subtle but important optimization here too. When inserting |
| 6621 | ** multiple records into an intkey b-tree using a single cursor (as can |
| 6622 | ** happen while processing an "INSERT INTO ... SELECT" statement), it |
| 6623 | ** is advantageous to leave the cursor pointing to the last entry in |
| 6624 | ** the b-tree if possible. If the cursor is left pointing to the last |
| 6625 | ** entry in the table, and the next row inserted has an integer key |
| 6626 | ** larger than the largest existing key, it is possible to insert the |
| 6627 | ** row without seeking the cursor. This can be a big performance boost. |
danielk1977 | 3f632d5 | 2009-05-02 10:03:09 +0000 | [diff] [blame] | 6628 | */ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6629 | pCur->info.nSize = 0; |
| 6630 | pCur->validNKey = 0; |
| 6631 | if( rc==SQLITE_OK && pPage->nOverflow ){ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6632 | rc = balance(pCur); |
| 6633 | |
| 6634 | /* Must make sure nOverflow is reset to zero even if the balance() |
danielk1977 | 54109bb | 2009-06-23 11:22:29 +0000 | [diff] [blame] | 6635 | ** fails. Internal data structure corruption will result otherwise. |
| 6636 | ** Also, set the cursor state to invalid. This stops saveCursorPosition() |
| 6637 | ** from trying to save the current position of the cursor. */ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6638 | pCur->apPage[pCur->iPage]->nOverflow = 0; |
danielk1977 | 54109bb | 2009-06-23 11:22:29 +0000 | [diff] [blame] | 6639 | pCur->eState = CURSOR_INVALID; |
danielk1977 | 3f632d5 | 2009-05-02 10:03:09 +0000 | [diff] [blame] | 6640 | } |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6641 | assert( pCur->apPage[pCur->iPage]->nOverflow==0 ); |
drh | 9bf9e9c | 2008-12-05 20:01:43 +0000 | [diff] [blame] | 6642 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6643 | end_insert: |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 6644 | return rc; |
| 6645 | } |
| 6646 | |
| 6647 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6648 | ** Delete the entry that the cursor is pointing to. The cursor |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 6649 | ** is left pointing at a arbitrary location. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 6650 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 6651 | int sqlite3BtreeDelete(BtCursor *pCur){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6652 | Btree *p = pCur->pBtree; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6653 | BtShared *pBt = p->pBt; |
| 6654 | int rc; /* Return code */ |
| 6655 | MemPage *pPage; /* Page to delete cell from */ |
| 6656 | unsigned char *pCell; /* Pointer to cell to delete */ |
| 6657 | int iCellIdx; /* Index of cell to delete */ |
| 6658 | int iCellDepth; /* Depth of node containing pCell */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6659 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6660 | assert( cursorHoldsMutex(pCur) ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 6661 | assert( pBt->inTransaction==TRANS_WRITE ); |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 6662 | assert( !pBt->readOnly ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 6663 | assert( pCur->wrFlag ); |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 6664 | assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) ); |
| 6665 | assert( !hasReadConflicts(p, pCur->pgnoRoot) ); |
| 6666 | |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6667 | if( NEVER(pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell) |
| 6668 | || NEVER(pCur->eState!=CURSOR_VALID) |
| 6669 | ){ |
| 6670 | return SQLITE_ERROR; /* Something has gone awry. */ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 6671 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6672 | |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 6673 | /* If this is a delete operation to remove a row from a table b-tree, |
| 6674 | ** invalidate any incrblob cursors open on the row being deleted. */ |
| 6675 | if( pCur->pKeyInfo==0 ){ |
drh | eeb844a | 2009-08-08 18:01:07 +0000 | [diff] [blame] | 6676 | invalidateIncrblobCursors(p, pCur->info.nKey, 0); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6677 | } |
| 6678 | |
| 6679 | iCellDepth = pCur->iPage; |
| 6680 | iCellIdx = pCur->aiIdx[iCellDepth]; |
| 6681 | pPage = pCur->apPage[iCellDepth]; |
| 6682 | pCell = findCell(pPage, iCellIdx); |
| 6683 | |
| 6684 | /* If the page containing the entry to delete is not a leaf page, move |
| 6685 | ** the cursor to the largest entry in the tree that is smaller than |
| 6686 | ** the entry being deleted. This cell will replace the cell being deleted |
| 6687 | ** from the internal node. The 'previous' entry is used for this instead |
| 6688 | ** of the 'next' entry, as the previous entry is always a part of the |
| 6689 | ** sub-tree headed by the child page of the cell being deleted. This makes |
| 6690 | ** balancing the tree following the delete operation easier. */ |
| 6691 | if( !pPage->leaf ){ |
| 6692 | int notUsed; |
drh | 4c301aa | 2009-07-15 17:25:45 +0000 | [diff] [blame] | 6693 | rc = sqlite3BtreePrevious(pCur, ¬Used); |
| 6694 | if( rc ) return rc; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6695 | } |
| 6696 | |
| 6697 | /* Save the positions of any other cursors open on this table before |
| 6698 | ** making any modifications. Make the page containing the entry to be |
| 6699 | ** deleted writable. Then free any overflow pages associated with the |
drh | a4ec1d4 | 2009-07-11 13:13:11 +0000 | [diff] [blame] | 6700 | ** entry and finally remove the cell itself from within the page. |
| 6701 | */ |
| 6702 | rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur); |
| 6703 | if( rc ) return rc; |
| 6704 | rc = sqlite3PagerWrite(pPage->pDbPage); |
| 6705 | if( rc ) return rc; |
| 6706 | rc = clearCell(pPage, pCell); |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6707 | dropCell(pPage, iCellIdx, cellSizePtr(pPage, pCell), &rc); |
drh | a4ec1d4 | 2009-07-11 13:13:11 +0000 | [diff] [blame] | 6708 | if( rc ) return rc; |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 6709 | |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6710 | /* If the cell deleted was not located on a leaf page, then the cursor |
| 6711 | ** is currently pointing to the largest entry in the sub-tree headed |
| 6712 | ** by the child-page of the cell that was just deleted from an internal |
| 6713 | ** node. The cell from the leaf node needs to be moved to the internal |
| 6714 | ** node to replace the deleted cell. */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6715 | if( !pPage->leaf ){ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6716 | MemPage *pLeaf = pCur->apPage[pCur->iPage]; |
| 6717 | int nCell; |
| 6718 | Pgno n = pCur->apPage[iCellDepth+1]->pgno; |
| 6719 | unsigned char *pTmp; |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 6720 | |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6721 | pCell = findCell(pLeaf, pLeaf->nCell-1); |
| 6722 | nCell = cellSizePtr(pLeaf, pCell); |
| 6723 | assert( MX_CELL_SIZE(pBt)>=nCell ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6724 | |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6725 | allocateTempSpace(pBt); |
| 6726 | pTmp = pBt->pTmpSpace; |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 6727 | |
drh | a4ec1d4 | 2009-07-11 13:13:11 +0000 | [diff] [blame] | 6728 | rc = sqlite3PagerWrite(pLeaf->pDbPage); |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6729 | insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc); |
| 6730 | dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc); |
drh | a4ec1d4 | 2009-07-11 13:13:11 +0000 | [diff] [blame] | 6731 | if( rc ) return rc; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 6732 | } |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6733 | |
| 6734 | /* Balance the tree. If the entry deleted was located on a leaf page, |
| 6735 | ** then the cursor still points to that page. In this case the first |
| 6736 | ** call to balance() repairs the tree, and the if(...) condition is |
| 6737 | ** never true. |
| 6738 | ** |
| 6739 | ** Otherwise, if the entry deleted was on an internal node page, then |
| 6740 | ** pCur is pointing to the leaf page from which a cell was removed to |
| 6741 | ** replace the cell deleted from the internal node. This is slightly |
| 6742 | ** tricky as the leaf node may be underfull, and the internal node may |
| 6743 | ** be either under or overfull. In this case run the balancing algorithm |
| 6744 | ** on the leaf node first. If the balance proceeds far enough up the |
| 6745 | ** tree that we can be sure that any problem in the internal node has |
| 6746 | ** been corrected, so be it. Otherwise, after balancing the leaf node, |
| 6747 | ** walk the cursor up the tree to the internal node and balance it as |
| 6748 | ** well. */ |
| 6749 | rc = balance(pCur); |
| 6750 | if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){ |
| 6751 | while( pCur->iPage>iCellDepth ){ |
| 6752 | releasePage(pCur->apPage[pCur->iPage--]); |
| 6753 | } |
| 6754 | rc = balance(pCur); |
| 6755 | } |
| 6756 | |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6757 | if( rc==SQLITE_OK ){ |
| 6758 | moveToRoot(pCur); |
| 6759 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 6760 | return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 6761 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6762 | |
| 6763 | /* |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 6764 | ** Create a new BTree table. Write into *piTable the page |
| 6765 | ** number for the root page of the new table. |
| 6766 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 6767 | ** The type of type is determined by the flags parameter. Only the |
| 6768 | ** following values of flags are currently in use. Other values for |
| 6769 | ** flags might not work: |
| 6770 | ** |
| 6771 | ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys |
| 6772 | ** BTREE_ZERODATA Used for SQL indices |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6773 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6774 | static int btreeCreateTable(Btree *p, int *piTable, int flags){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6775 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6776 | MemPage *pRoot; |
| 6777 | Pgno pgnoRoot; |
| 6778 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6779 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6780 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 6781 | assert( pBt->inTransaction==TRANS_WRITE ); |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 6782 | assert( !pBt->readOnly ); |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 6783 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6784 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 6785 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6786 | if( rc ){ |
| 6787 | return rc; |
| 6788 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6789 | #else |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6790 | if( pBt->autoVacuum ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6791 | Pgno pgnoMove; /* Move a page here to make room for the root-page */ |
| 6792 | MemPage *pPageMove; /* The page to move to. */ |
| 6793 | |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 6794 | /* Creating a new table may probably require moving an existing database |
| 6795 | ** to make room for the new tables root page. In case this page turns |
| 6796 | ** out to be an overflow page, delete all overflow page-map caches |
| 6797 | ** held by open cursors. |
| 6798 | */ |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 6799 | invalidateAllOverflowCache(pBt); |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 6800 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6801 | /* Read the value of meta[3] from the database to determine where the |
| 6802 | ** root page of the new table should go. meta[3] is the largest root-page |
| 6803 | ** created so far, so the new root-page is (meta[3]+1). |
| 6804 | */ |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 6805 | sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &pgnoRoot); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6806 | pgnoRoot++; |
| 6807 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6808 | /* The new root-page may not be allocated on a pointer-map page, or the |
| 6809 | ** PENDING_BYTE page. |
| 6810 | */ |
drh | 7219043 | 2008-01-31 14:54:43 +0000 | [diff] [blame] | 6811 | while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) || |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6812 | pgnoRoot==PENDING_BYTE_PAGE(pBt) ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6813 | pgnoRoot++; |
| 6814 | } |
| 6815 | assert( pgnoRoot>=3 ); |
| 6816 | |
| 6817 | /* Allocate a page. The page that currently resides at pgnoRoot will |
| 6818 | ** be moved to the allocated page (unless the allocated page happens |
| 6819 | ** to reside at pgnoRoot). |
| 6820 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 6821 | rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6822 | if( rc!=SQLITE_OK ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6823 | return rc; |
| 6824 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6825 | |
| 6826 | if( pgnoMove!=pgnoRoot ){ |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 6827 | /* pgnoRoot is the page that will be used for the root-page of |
| 6828 | ** the new table (assuming an error did not occur). But we were |
| 6829 | ** allocated pgnoMove. If required (i.e. if it was not allocated |
| 6830 | ** by extending the file), the current page at position pgnoMove |
| 6831 | ** is already journaled. |
| 6832 | */ |
drh | eeb844a | 2009-08-08 18:01:07 +0000 | [diff] [blame] | 6833 | u8 eType = 0; |
| 6834 | Pgno iPtrPage = 0; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6835 | |
| 6836 | releasePage(pPageMove); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 6837 | |
| 6838 | /* Move the page currently at pgnoRoot to pgnoMove. */ |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 6839 | rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6840 | if( rc!=SQLITE_OK ){ |
| 6841 | return rc; |
| 6842 | } |
| 6843 | rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage); |
drh | 27731d7 | 2009-06-22 12:05:10 +0000 | [diff] [blame] | 6844 | if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){ |
| 6845 | rc = SQLITE_CORRUPT_BKPT; |
| 6846 | } |
| 6847 | if( rc!=SQLITE_OK ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6848 | releasePage(pRoot); |
| 6849 | return rc; |
| 6850 | } |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 6851 | assert( eType!=PTRMAP_ROOTPAGE ); |
| 6852 | assert( eType!=PTRMAP_FREEPAGE ); |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 6853 | rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6854 | releasePage(pRoot); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 6855 | |
| 6856 | /* Obtain the page at pgnoRoot */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6857 | if( rc!=SQLITE_OK ){ |
| 6858 | return rc; |
| 6859 | } |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 6860 | rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6861 | if( rc!=SQLITE_OK ){ |
| 6862 | return rc; |
| 6863 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6864 | rc = sqlite3PagerWrite(pRoot->pDbPage); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6865 | if( rc!=SQLITE_OK ){ |
| 6866 | releasePage(pRoot); |
| 6867 | return rc; |
| 6868 | } |
| 6869 | }else{ |
| 6870 | pRoot = pPageMove; |
| 6871 | } |
| 6872 | |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 6873 | /* Update the pointer-map and meta-data with the new root-page number. */ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6874 | ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0, &rc); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6875 | if( rc ){ |
| 6876 | releasePage(pRoot); |
| 6877 | return rc; |
| 6878 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6879 | rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6880 | if( rc ){ |
| 6881 | releasePage(pRoot); |
| 6882 | return rc; |
| 6883 | } |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 6884 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6885 | }else{ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 6886 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6887 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6888 | } |
| 6889 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6890 | assert( sqlite3PagerIswriteable(pRoot->pDbPage) ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 6891 | zeroPage(pRoot, flags | PTF_LEAF); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6892 | sqlite3PagerUnref(pRoot->pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6893 | *piTable = (int)pgnoRoot; |
| 6894 | return SQLITE_OK; |
| 6895 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6896 | int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){ |
| 6897 | int rc; |
| 6898 | sqlite3BtreeEnter(p); |
| 6899 | rc = btreeCreateTable(p, piTable, flags); |
| 6900 | sqlite3BtreeLeave(p); |
| 6901 | return rc; |
| 6902 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6903 | |
| 6904 | /* |
| 6905 | ** Erase the given database page and all its children. Return |
| 6906 | ** the page to the freelist. |
| 6907 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6908 | static int clearDatabasePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6909 | BtShared *pBt, /* The BTree that contains the table */ |
drh | 7ab641f | 2009-11-24 02:37:02 +0000 | [diff] [blame] | 6910 | Pgno pgno, /* Page number to clear */ |
| 6911 | int freePageFlag, /* Deallocate page if true */ |
| 6912 | int *pnChange /* Add number of Cells freed to this counter */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6913 | ){ |
danielk1977 | 146ba99 | 2009-07-22 14:08:13 +0000 | [diff] [blame] | 6914 | MemPage *pPage; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6915 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6916 | unsigned char *pCell; |
| 6917 | int i; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6918 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6919 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 6920 | if( pgno>pagerPagecount(pBt) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 6921 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 6922 | } |
| 6923 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6924 | rc = getAndInitPage(pBt, pgno, &pPage); |
danielk1977 | 146ba99 | 2009-07-22 14:08:13 +0000 | [diff] [blame] | 6925 | if( rc ) return rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6926 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 6927 | pCell = findCell(pPage, i); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6928 | if( !pPage->leaf ){ |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 6929 | rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6930 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6931 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6932 | rc = clearCell(pPage, pCell); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6933 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6934 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6935 | if( !pPage->leaf ){ |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 6936 | rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), 1, pnChange); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6937 | if( rc ) goto cleardatabasepage_out; |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6938 | }else if( pnChange ){ |
| 6939 | assert( pPage->intKey ); |
| 6940 | *pnChange += pPage->nCell; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6941 | } |
| 6942 | if( freePageFlag ){ |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 6943 | freePage(pPage, &rc); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6944 | }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6945 | zeroPage(pPage, pPage->aData[0] | PTF_LEAF); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6946 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6947 | |
| 6948 | cleardatabasepage_out: |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6949 | releasePage(pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6950 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6951 | } |
| 6952 | |
| 6953 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 6954 | ** Delete all information from a single table in the database. iTable is |
| 6955 | ** the page number of the root of the table. After this routine returns, |
| 6956 | ** the root page is empty, but still exists. |
| 6957 | ** |
| 6958 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 6959 | ** read cursors on the table. Open write cursors are moved to the |
| 6960 | ** root of the table. |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6961 | ** |
| 6962 | ** If pnChange is not NULL, then table iTable must be an intkey table. The |
| 6963 | ** integer value pointed to by pnChange is incremented by the number of |
| 6964 | ** entries in the table. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6965 | */ |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 6966 | int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6967 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6968 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6969 | sqlite3BtreeEnter(p); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 6970 | assert( p->inTrans==TRANS_WRITE ); |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 6971 | |
| 6972 | /* Invalidate all incrblob cursors open on table iTable (assuming iTable |
| 6973 | ** is the root of a table b-tree - if it is not, the following call is |
| 6974 | ** a no-op). */ |
drh | eeb844a | 2009-08-08 18:01:07 +0000 | [diff] [blame] | 6975 | invalidateIncrblobCursors(p, 0, 1); |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 6976 | |
drh | c046e3e | 2009-07-15 11:26:44 +0000 | [diff] [blame] | 6977 | rc = saveAllCursors(pBt, (Pgno)iTable, 0); |
| 6978 | if( SQLITE_OK==rc ){ |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 6979 | rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6980 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6981 | sqlite3BtreeLeave(p); |
| 6982 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6983 | } |
| 6984 | |
| 6985 | /* |
| 6986 | ** Erase all information in a table and add the root of the table to |
| 6987 | ** the freelist. Except, the root of the principle table (the one on |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 6988 | ** page 1) is never added to the freelist. |
| 6989 | ** |
| 6990 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 6991 | ** cursors on the table. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6992 | ** |
| 6993 | ** If AUTOVACUUM is enabled and the page at iTable is not the last |
| 6994 | ** root page in the database file, then the last root page |
| 6995 | ** in the database file is moved into the slot formerly occupied by |
| 6996 | ** iTable and that last slot formerly occupied by the last root page |
| 6997 | ** is added to the freelist instead of iTable. In this say, all |
| 6998 | ** root pages are kept at the beginning of the database file, which |
| 6999 | ** is necessary for AUTOVACUUM to work right. *piMoved is set to the |
| 7000 | ** page number that used to be the last root page in the file before |
| 7001 | ** the move. If no page gets moved, *piMoved is set to 0. |
| 7002 | ** The last root page is recorded in meta[3] and the value of |
| 7003 | ** meta[3] is updated by this procedure. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7004 | */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 7005 | static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7006 | int rc; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 7007 | MemPage *pPage = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7008 | BtShared *pBt = p->pBt; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 7009 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7010 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 7011 | assert( p->inTrans==TRANS_WRITE ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 7012 | |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 7013 | /* It is illegal to drop a table if any cursors are open on the |
| 7014 | ** database. This is because in auto-vacuum mode the backend may |
| 7015 | ** need to move another root-page to fill a gap left by the deleted |
| 7016 | ** root page. If an open cursor was using this page a problem would |
| 7017 | ** occur. |
drh | c046e3e | 2009-07-15 11:26:44 +0000 | [diff] [blame] | 7018 | ** |
| 7019 | ** This error is caught long before control reaches this point. |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 7020 | */ |
drh | c046e3e | 2009-07-15 11:26:44 +0000 | [diff] [blame] | 7021 | if( NEVER(pBt->pCursor) ){ |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 7022 | sqlite3ConnectionBlocked(p->db, pBt->pCursor->pBtree->db); |
| 7023 | return SQLITE_LOCKED_SHAREDCACHE; |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 7024 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 7025 | |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 7026 | rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 7027 | if( rc ) return rc; |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 7028 | rc = sqlite3BtreeClearTable(p, iTable, 0); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 7029 | if( rc ){ |
| 7030 | releasePage(pPage); |
| 7031 | return rc; |
| 7032 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 7033 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 7034 | *piMoved = 0; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 7035 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7036 | if( iTable>1 ){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 7037 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 7038 | freePage(pPage, &rc); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 7039 | releasePage(pPage); |
| 7040 | #else |
| 7041 | if( pBt->autoVacuum ){ |
| 7042 | Pgno maxRootPgno; |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 7043 | sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 7044 | |
| 7045 | if( iTable==maxRootPgno ){ |
| 7046 | /* If the table being dropped is the table with the largest root-page |
| 7047 | ** number in the database, put the root page on the free list. |
| 7048 | */ |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 7049 | freePage(pPage, &rc); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 7050 | releasePage(pPage); |
| 7051 | if( rc!=SQLITE_OK ){ |
| 7052 | return rc; |
| 7053 | } |
| 7054 | }else{ |
| 7055 | /* The table being dropped does not have the largest root-page |
| 7056 | ** number in the database. So move the page that does into the |
| 7057 | ** gap left by the deleted root-page. |
| 7058 | */ |
| 7059 | MemPage *pMove; |
| 7060 | releasePage(pPage); |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 7061 | rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 7062 | if( rc!=SQLITE_OK ){ |
| 7063 | return rc; |
| 7064 | } |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 7065 | rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 7066 | releasePage(pMove); |
| 7067 | if( rc!=SQLITE_OK ){ |
| 7068 | return rc; |
| 7069 | } |
drh | fe3313f | 2009-07-21 19:02:20 +0000 | [diff] [blame] | 7070 | pMove = 0; |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 7071 | rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0); |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 7072 | freePage(pMove, &rc); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 7073 | releasePage(pMove); |
| 7074 | if( rc!=SQLITE_OK ){ |
| 7075 | return rc; |
| 7076 | } |
| 7077 | *piMoved = maxRootPgno; |
| 7078 | } |
| 7079 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 7080 | /* Set the new 'max-root-page' value in the database header. This |
| 7081 | ** is the old value less one, less one more if that happens to |
| 7082 | ** be a root-page number, less one again if that is the |
| 7083 | ** PENDING_BYTE_PAGE. |
| 7084 | */ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 7085 | maxRootPgno--; |
drh | e184965 | 2009-07-15 18:15:22 +0000 | [diff] [blame] | 7086 | while( maxRootPgno==PENDING_BYTE_PAGE(pBt) |
| 7087 | || PTRMAP_ISPAGE(pBt, maxRootPgno) ){ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 7088 | maxRootPgno--; |
| 7089 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 7090 | assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) ); |
| 7091 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7092 | rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 7093 | }else{ |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 7094 | freePage(pPage, &rc); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 7095 | releasePage(pPage); |
| 7096 | } |
| 7097 | #endif |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 7098 | }else{ |
drh | c046e3e | 2009-07-15 11:26:44 +0000 | [diff] [blame] | 7099 | /* If sqlite3BtreeDropTable was called on page 1. |
| 7100 | ** This really never should happen except in a corrupt |
| 7101 | ** database. |
| 7102 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 7103 | zeroPage(pPage, PTF_INTKEY|PTF_LEAF ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 7104 | releasePage(pPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7105 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7106 | return rc; |
| 7107 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7108 | int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){ |
| 7109 | int rc; |
| 7110 | sqlite3BtreeEnter(p); |
| 7111 | rc = btreeDropTable(p, iTable, piMoved); |
| 7112 | sqlite3BtreeLeave(p); |
| 7113 | return rc; |
| 7114 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7115 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 7116 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7117 | /* |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 7118 | ** This function may only be called if the b-tree connection already |
| 7119 | ** has a read or write transaction open on the database. |
| 7120 | ** |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 7121 | ** Read the meta-information out of a database file. Meta[0] |
| 7122 | ** is the number of free pages currently in the database. Meta[1] |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 7123 | ** through meta[15] are available for use by higher layers. Meta[0] |
| 7124 | ** is read-only, the others are read/write. |
| 7125 | ** |
| 7126 | ** The schema layer numbers meta values differently. At the schema |
| 7127 | ** layer (and the SetCookie and ReadCookie opcodes) the number of |
| 7128 | ** 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] | 7129 | */ |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 7130 | void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7131 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7132 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7133 | sqlite3BtreeEnter(p); |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 7134 | assert( p->inTrans>TRANS_NONE ); |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 7135 | assert( SQLITE_OK==querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK) ); |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 7136 | assert( pBt->pPage1 ); |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 7137 | assert( idx>=0 && idx<=15 ); |
danielk1977 | ea89730 | 2008-09-19 15:10:58 +0000 | [diff] [blame] | 7138 | |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 7139 | *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]); |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 7140 | |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 7141 | /* If auto-vacuum is disabled in this build and this is an auto-vacuum |
| 7142 | ** database, mark the database as read-only. */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7143 | #ifdef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 7144 | if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ) pBt->readOnly = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7145 | #endif |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 7146 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7147 | sqlite3BtreeLeave(p); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7148 | } |
| 7149 | |
| 7150 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 7151 | ** Write meta-information back into the database. Meta[0] is |
| 7152 | ** read-only and may not be written. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7153 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7154 | int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){ |
| 7155 | BtShared *pBt = p->pBt; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7156 | unsigned char *pP1; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 7157 | int rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 7158 | assert( idx>=1 && idx<=15 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7159 | sqlite3BtreeEnter(p); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 7160 | assert( p->inTrans==TRANS_WRITE ); |
| 7161 | assert( pBt->pPage1!=0 ); |
| 7162 | pP1 = pBt->pPage1->aData; |
| 7163 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 7164 | if( rc==SQLITE_OK ){ |
| 7165 | put4byte(&pP1[36 + idx*4], iMeta); |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 +0000 | [diff] [blame] | 7166 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 7167 | if( idx==BTREE_INCR_VACUUM ){ |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 7168 | assert( pBt->autoVacuum || iMeta==0 ); |
| 7169 | assert( iMeta==0 || iMeta==1 ); |
| 7170 | pBt->incrVacuum = (u8)iMeta; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7171 | } |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 7172 | #endif |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 7173 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7174 | sqlite3BtreeLeave(p); |
| 7175 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7176 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 7177 | |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 7178 | #ifndef SQLITE_OMIT_BTREECOUNT |
| 7179 | /* |
| 7180 | ** The first argument, pCur, is a cursor opened on some b-tree. Count the |
| 7181 | ** number of entries in the b-tree and write the result to *pnEntry. |
| 7182 | ** |
| 7183 | ** SQLITE_OK is returned if the operation is successfully executed. |
| 7184 | ** Otherwise, if an error is encountered (i.e. an IO error or database |
| 7185 | ** corruption) an SQLite error code is returned. |
| 7186 | */ |
| 7187 | int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){ |
| 7188 | i64 nEntry = 0; /* Value to return in *pnEntry */ |
| 7189 | int rc; /* Return code */ |
| 7190 | rc = moveToRoot(pCur); |
| 7191 | |
| 7192 | /* Unless an error occurs, the following loop runs one iteration for each |
| 7193 | ** page in the B-Tree structure (not including overflow pages). |
| 7194 | */ |
| 7195 | while( rc==SQLITE_OK ){ |
| 7196 | int iIdx; /* Index of child node in parent */ |
| 7197 | MemPage *pPage; /* Current page of the b-tree */ |
| 7198 | |
| 7199 | /* If this is a leaf page or the tree is not an int-key tree, then |
| 7200 | ** this page contains countable entries. Increment the entry counter |
| 7201 | ** accordingly. |
| 7202 | */ |
| 7203 | pPage = pCur->apPage[pCur->iPage]; |
| 7204 | if( pPage->leaf || !pPage->intKey ){ |
| 7205 | nEntry += pPage->nCell; |
| 7206 | } |
| 7207 | |
| 7208 | /* pPage is a leaf node. This loop navigates the cursor so that it |
| 7209 | ** points to the first interior cell that it points to the parent of |
| 7210 | ** the next page in the tree that has not yet been visited. The |
| 7211 | ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell |
| 7212 | ** of the page, or to the number of cells in the page if the next page |
| 7213 | ** to visit is the right-child of its parent. |
| 7214 | ** |
| 7215 | ** If all pages in the tree have been visited, return SQLITE_OK to the |
| 7216 | ** caller. |
| 7217 | */ |
| 7218 | if( pPage->leaf ){ |
| 7219 | do { |
| 7220 | if( pCur->iPage==0 ){ |
| 7221 | /* All pages of the b-tree have been visited. Return successfully. */ |
| 7222 | *pnEntry = nEntry; |
| 7223 | return SQLITE_OK; |
| 7224 | } |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 7225 | moveToParent(pCur); |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 7226 | }while ( pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell ); |
| 7227 | |
| 7228 | pCur->aiIdx[pCur->iPage]++; |
| 7229 | pPage = pCur->apPage[pCur->iPage]; |
| 7230 | } |
| 7231 | |
| 7232 | /* Descend to the child node of the cell that the cursor currently |
| 7233 | ** points at. This is the right-child if (iIdx==pPage->nCell). |
| 7234 | */ |
| 7235 | iIdx = pCur->aiIdx[pCur->iPage]; |
| 7236 | if( iIdx==pPage->nCell ){ |
| 7237 | rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); |
| 7238 | }else{ |
| 7239 | rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx))); |
| 7240 | } |
| 7241 | } |
| 7242 | |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 7243 | /* An error has occurred. Return an error code. */ |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 7244 | return rc; |
| 7245 | } |
| 7246 | #endif |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 7247 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 7248 | /* |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7249 | ** Return the pager associated with a BTree. This routine is used for |
| 7250 | ** testing and debugging only. |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 7251 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7252 | Pager *sqlite3BtreePager(Btree *p){ |
| 7253 | return p->pBt->pPager; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 7254 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7255 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 7256 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7257 | /* |
| 7258 | ** Append a message to the error message string. |
| 7259 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7260 | static void checkAppendMsg( |
| 7261 | IntegrityCk *pCheck, |
| 7262 | char *zMsg1, |
| 7263 | const char *zFormat, |
| 7264 | ... |
| 7265 | ){ |
| 7266 | va_list ap; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 7267 | if( !pCheck->mxErr ) return; |
| 7268 | pCheck->mxErr--; |
| 7269 | pCheck->nErr++; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7270 | va_start(ap, zFormat); |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 7271 | if( pCheck->errMsg.nChar ){ |
| 7272 | sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7273 | } |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 7274 | if( zMsg1 ){ |
| 7275 | sqlite3StrAccumAppend(&pCheck->errMsg, zMsg1, -1); |
| 7276 | } |
| 7277 | sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap); |
| 7278 | va_end(ap); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 7279 | if( pCheck->errMsg.mallocFailed ){ |
| 7280 | pCheck->mallocFailed = 1; |
| 7281 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7282 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 7283 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7284 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 7285 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7286 | /* |
| 7287 | ** Add 1 to the reference count for page iPage. If this is the second |
| 7288 | ** reference to the page, add an error message to pCheck->zErrMsg. |
| 7289 | ** Return 1 if there are 2 ore more references to the page and 0 if |
| 7290 | ** if this is the first reference to the page. |
| 7291 | ** |
| 7292 | ** Also check that the page number is in bounds. |
| 7293 | */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 7294 | static int checkRef(IntegrityCk *pCheck, Pgno iPage, char *zContext){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7295 | if( iPage==0 ) return 1; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 7296 | if( iPage>pCheck->nPage ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7297 | checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7298 | return 1; |
| 7299 | } |
| 7300 | if( pCheck->anRef[iPage]==1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7301 | checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7302 | return 1; |
| 7303 | } |
| 7304 | return (pCheck->anRef[iPage]++)>1; |
| 7305 | } |
| 7306 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7307 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 7308 | /* |
| 7309 | ** Check that the entry in the pointer-map for page iChild maps to |
| 7310 | ** page iParent, pointer type ptrType. If not, append an error message |
| 7311 | ** to pCheck. |
| 7312 | */ |
| 7313 | static void checkPtrmap( |
| 7314 | IntegrityCk *pCheck, /* Integrity check context */ |
| 7315 | Pgno iChild, /* Child page number */ |
| 7316 | u8 eType, /* Expected pointer map type */ |
| 7317 | Pgno iParent, /* Expected pointer map parent page number */ |
| 7318 | char *zContext /* Context description (used for error msg) */ |
| 7319 | ){ |
| 7320 | int rc; |
| 7321 | u8 ePtrmapType; |
| 7322 | Pgno iPtrmapParent; |
| 7323 | |
| 7324 | rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent); |
| 7325 | if( rc!=SQLITE_OK ){ |
drh | b56cd55 | 2009-05-01 13:16:54 +0000 | [diff] [blame] | 7326 | if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->mallocFailed = 1; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7327 | checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild); |
| 7328 | return; |
| 7329 | } |
| 7330 | |
| 7331 | if( ePtrmapType!=eType || iPtrmapParent!=iParent ){ |
| 7332 | checkAppendMsg(pCheck, zContext, |
| 7333 | "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", |
| 7334 | iChild, eType, iParent, ePtrmapType, iPtrmapParent); |
| 7335 | } |
| 7336 | } |
| 7337 | #endif |
| 7338 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7339 | /* |
| 7340 | ** Check the integrity of the freelist or of an overflow page list. |
| 7341 | ** Verify that the number of pages on the list is N. |
| 7342 | */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 7343 | static void checkList( |
| 7344 | IntegrityCk *pCheck, /* Integrity checking context */ |
| 7345 | int isFreeList, /* True for a freelist. False for overflow page list */ |
| 7346 | int iPage, /* Page number for first page in the list */ |
| 7347 | int N, /* Expected number of pages in the list */ |
| 7348 | char *zContext /* Context for error messages */ |
| 7349 | ){ |
| 7350 | int i; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 7351 | int expected = N; |
| 7352 | int iFirst = iPage; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 7353 | while( N-- > 0 && pCheck->mxErr ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7354 | DbPage *pOvflPage; |
| 7355 | unsigned char *pOvflData; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7356 | if( iPage<1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7357 | checkAppendMsg(pCheck, zContext, |
| 7358 | "%d of %d pages missing from overflow list starting at %d", |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 7359 | N+1, expected, iFirst); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7360 | break; |
| 7361 | } |
| 7362 | if( checkRef(pCheck, iPage, zContext) ) break; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7363 | if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7364 | checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7365 | break; |
| 7366 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7367 | pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 7368 | if( isFreeList ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7369 | int n = get4byte(&pOvflData[4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 7370 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 7371 | if( pCheck->pBt->autoVacuum ){ |
| 7372 | checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext); |
| 7373 | } |
| 7374 | #endif |
drh | 45b1fac | 2008-07-04 17:52:42 +0000 | [diff] [blame] | 7375 | if( n>pCheck->pBt->usableSize/4-2 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7376 | checkAppendMsg(pCheck, zContext, |
| 7377 | "freelist leaf count too big on page %d", iPage); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 7378 | N--; |
| 7379 | }else{ |
| 7380 | for(i=0; i<n; i++){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7381 | Pgno iFreePage = get4byte(&pOvflData[8+i*4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 7382 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 7383 | if( pCheck->pBt->autoVacuum ){ |
| 7384 | checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext); |
| 7385 | } |
| 7386 | #endif |
| 7387 | checkRef(pCheck, iFreePage, zContext); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 7388 | } |
| 7389 | N -= n; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 7390 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 7391 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7392 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 7393 | else{ |
| 7394 | /* If this database supports auto-vacuum and iPage is not the last |
| 7395 | ** page in this overflow list, check that the pointer-map entry for |
| 7396 | ** the following page matches iPage. |
| 7397 | */ |
| 7398 | if( pCheck->pBt->autoVacuum && N>0 ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7399 | i = get4byte(pOvflData); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 7400 | checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext); |
| 7401 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7402 | } |
| 7403 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7404 | iPage = get4byte(pOvflData); |
| 7405 | sqlite3PagerUnref(pOvflPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7406 | } |
| 7407 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 7408 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7409 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 7410 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7411 | /* |
| 7412 | ** Do various sanity checks on a single page of a tree. Return |
| 7413 | ** the tree depth. Root pages return 0. Parents of root pages |
| 7414 | ** return 1, and so forth. |
| 7415 | ** |
| 7416 | ** These checks are done: |
| 7417 | ** |
| 7418 | ** 1. Make sure that cells and freeblocks do not overlap |
| 7419 | ** but combine to completely cover the page. |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 7420 | ** NO 2. Make sure cell keys are in order. |
| 7421 | ** NO 3. Make sure no key is less than or equal to zLowerBound. |
| 7422 | ** NO 4. Make sure no key is greater than or equal to zUpperBound. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7423 | ** 5. Check the integrity of overflow pages. |
| 7424 | ** 6. Recursively call checkTreePage on all children. |
| 7425 | ** 7. Verify that the depth of all children is the same. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 7426 | ** 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] | 7427 | ** the root of the tree. |
| 7428 | */ |
| 7429 | static int checkTreePage( |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 7430 | IntegrityCk *pCheck, /* Context for the sanity check */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7431 | int iPage, /* Page number of the page to check */ |
shaneh | 195475d | 2010-02-19 04:28:08 +0000 | [diff] [blame] | 7432 | char *zParentContext, /* Parent context */ |
| 7433 | i64 *pnParentMinKey, |
| 7434 | i64 *pnParentMaxKey |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7435 | ){ |
| 7436 | MemPage *pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 7437 | int i, rc, depth, d2, pgno, cnt; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 7438 | int hdr, cellStart; |
| 7439 | int nCell; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 7440 | u8 *data; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7441 | BtShared *pBt; |
drh | 4f26bb6 | 2005-09-08 14:17:20 +0000 | [diff] [blame] | 7442 | int usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7443 | char zContext[100]; |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 7444 | char *hit = 0; |
shaneh | 195475d | 2010-02-19 04:28:08 +0000 | [diff] [blame] | 7445 | i64 nMinKey = 0; |
| 7446 | i64 nMaxKey = 0; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7447 | |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 7448 | sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage); |
danielk1977 | ef73ee9 | 2004-11-06 12:26:07 +0000 | [diff] [blame] | 7449 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7450 | /* Check that the page exists |
| 7451 | */ |
drh | d9cb6ac | 2005-10-20 07:28:17 +0000 | [diff] [blame] | 7452 | pBt = pCheck->pBt; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 7453 | usableSize = pBt->usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7454 | if( iPage==0 ) return 0; |
| 7455 | if( checkRef(pCheck, iPage, zParentContext) ) return 0; |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 7456 | if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7457 | checkAppendMsg(pCheck, zContext, |
| 7458 | "unable to get the page. error code=%d", rc); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7459 | return 0; |
| 7460 | } |
danielk1977 | 93caf5a | 2009-07-11 06:55:33 +0000 | [diff] [blame] | 7461 | |
| 7462 | /* Clear MemPage.isInit to make sure the corruption detection code in |
| 7463 | ** btreeInitPage() is executed. */ |
| 7464 | pPage->isInit = 0; |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 7465 | if( (rc = btreeInitPage(pPage))!=0 ){ |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 7466 | assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 7467 | checkAppendMsg(pCheck, zContext, |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 7468 | "btreeInitPage() returns error code %d", rc); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 7469 | releasePage(pPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7470 | return 0; |
| 7471 | } |
| 7472 | |
| 7473 | /* Check out all the cells. |
| 7474 | */ |
| 7475 | depth = 0; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 7476 | for(i=0; i<pPage->nCell && pCheck->mxErr; i++){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 7477 | u8 *pCell; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 7478 | u32 sz; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 7479 | CellInfo info; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7480 | |
| 7481 | /* Check payload overflow pages |
| 7482 | */ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 7483 | sqlite3_snprintf(sizeof(zContext), zContext, |
| 7484 | "On tree page %d cell %d: ", iPage, i); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 7485 | pCell = findCell(pPage,i); |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 7486 | btreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 7487 | sz = info.nData; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 7488 | if( !pPage->intKey ) sz += (int)info.nKey; |
shaneh | 195475d | 2010-02-19 04:28:08 +0000 | [diff] [blame] | 7489 | /* For intKey pages, check that the keys are in order. |
| 7490 | */ |
| 7491 | else if( i==0 ) nMinKey = nMaxKey = info.nKey; |
| 7492 | else{ |
| 7493 | if( info.nKey <= nMaxKey ){ |
| 7494 | checkAppendMsg(pCheck, zContext, |
| 7495 | "Rowid %lld out of order (previous was %lld)", info.nKey, nMaxKey); |
| 7496 | } |
| 7497 | nMaxKey = info.nKey; |
| 7498 | } |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 7499 | assert( sz==info.nPayload ); |
danielk1977 | 5be31f5 | 2009-03-30 13:53:43 +0000 | [diff] [blame] | 7500 | if( (sz>info.nLocal) |
| 7501 | && (&pCell[info.iOverflow]<=&pPage->aData[pBt->usableSize]) |
| 7502 | ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 7503 | int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7504 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
| 7505 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 7506 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 7507 | checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7508 | } |
| 7509 | #endif |
| 7510 | checkList(pCheck, 0, pgnoOvfl, nPage, zContext); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7511 | } |
| 7512 | |
| 7513 | /* Check sanity of left child page. |
| 7514 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 7515 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 7516 | pgno = get4byte(pCell); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7517 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 7518 | if( pBt->autoVacuum ){ |
| 7519 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext); |
| 7520 | } |
| 7521 | #endif |
shaneh | 195475d | 2010-02-19 04:28:08 +0000 | [diff] [blame] | 7522 | d2 = checkTreePage(pCheck, pgno, zContext, &nMinKey, i==0 ? NULL : &nMaxKey); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 7523 | if( i>0 && d2!=depth ){ |
| 7524 | checkAppendMsg(pCheck, zContext, "Child page depth differs"); |
| 7525 | } |
| 7526 | depth = d2; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7527 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7528 | } |
shaneh | 195475d | 2010-02-19 04:28:08 +0000 | [diff] [blame] | 7529 | |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 7530 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 7531 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 7532 | sqlite3_snprintf(sizeof(zContext), zContext, |
| 7533 | "On page %d at right child: ", iPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7534 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 7535 | if( pBt->autoVacuum ){ |
shaneh | 195475d | 2010-02-19 04:28:08 +0000 | [diff] [blame] | 7536 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7537 | } |
| 7538 | #endif |
shaneh | 195475d | 2010-02-19 04:28:08 +0000 | [diff] [blame] | 7539 | checkTreePage(pCheck, pgno, zContext, NULL, !pPage->nCell ? NULL : &nMaxKey); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 7540 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7541 | |
shaneh | 195475d | 2010-02-19 04:28:08 +0000 | [diff] [blame] | 7542 | /* For intKey leaf pages, check that the min/max keys are in order |
| 7543 | ** with any left/parent/right pages. |
| 7544 | */ |
| 7545 | if( pPage->leaf && pPage->intKey ){ |
| 7546 | /* if we are a left child page */ |
| 7547 | if( pnParentMinKey ){ |
| 7548 | /* if we are the left most child page */ |
| 7549 | if( !pnParentMaxKey ){ |
| 7550 | if( nMaxKey > *pnParentMinKey ){ |
| 7551 | checkAppendMsg(pCheck, zContext, |
| 7552 | "Rowid %lld out of order (max larger than parent min of %lld)", |
| 7553 | nMaxKey, *pnParentMinKey); |
| 7554 | } |
| 7555 | }else{ |
| 7556 | if( nMinKey <= *pnParentMinKey ){ |
| 7557 | checkAppendMsg(pCheck, zContext, |
| 7558 | "Rowid %lld out of order (min less than parent min of %lld)", |
| 7559 | nMinKey, *pnParentMinKey); |
| 7560 | } |
| 7561 | if( nMaxKey > *pnParentMaxKey ){ |
| 7562 | checkAppendMsg(pCheck, zContext, |
| 7563 | "Rowid %lld out of order (max larger than parent max of %lld)", |
| 7564 | nMaxKey, *pnParentMaxKey); |
| 7565 | } |
| 7566 | *pnParentMinKey = nMaxKey; |
| 7567 | } |
| 7568 | /* else if we're a right child page */ |
| 7569 | } else if( pnParentMaxKey ){ |
| 7570 | if( nMinKey <= *pnParentMaxKey ){ |
| 7571 | checkAppendMsg(pCheck, zContext, |
| 7572 | "Rowid %lld out of order (min less than parent max of %lld)", |
| 7573 | nMinKey, *pnParentMaxKey); |
| 7574 | } |
| 7575 | } |
| 7576 | } |
| 7577 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7578 | /* Check for complete coverage of the page |
| 7579 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 7580 | data = pPage->aData; |
| 7581 | hdr = pPage->hdrOffset; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 7582 | hit = sqlite3PageMalloc( pBt->pageSize ); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 7583 | if( hit==0 ){ |
| 7584 | pCheck->mallocFailed = 1; |
| 7585 | }else{ |
shane | 5780ebd | 2008-11-11 17:36:30 +0000 | [diff] [blame] | 7586 | u16 contentOffset = get2byte(&data[hdr+5]); |
drh | d7c7ecd | 2009-07-14 17:48:06 +0000 | [diff] [blame] | 7587 | assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */ |
shane | 5780ebd | 2008-11-11 17:36:30 +0000 | [diff] [blame] | 7588 | memset(hit+contentOffset, 0, usableSize-contentOffset); |
| 7589 | memset(hit, 1, contentOffset); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7590 | nCell = get2byte(&data[hdr+3]); |
| 7591 | cellStart = hdr + 12 - 4*pPage->leaf; |
| 7592 | for(i=0; i<nCell; i++){ |
| 7593 | int pc = get2byte(&data[cellStart+i*2]); |
danielk1977 | daca543 | 2008-08-25 11:57:16 +0000 | [diff] [blame] | 7594 | u16 size = 1024; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7595 | int j; |
drh | 8c2bbb6 | 2009-07-10 02:52:20 +0000 | [diff] [blame] | 7596 | if( pc<=usableSize-4 ){ |
danielk1977 | daca543 | 2008-08-25 11:57:16 +0000 | [diff] [blame] | 7597 | size = cellSizePtr(pPage, &data[pc]); |
| 7598 | } |
drh | d7c7ecd | 2009-07-14 17:48:06 +0000 | [diff] [blame] | 7599 | if( (pc+size-1)>=usableSize ){ |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 7600 | checkAppendMsg(pCheck, 0, |
shaneh | 195475d | 2010-02-19 04:28:08 +0000 | [diff] [blame] | 7601 | "Corruption detected in cell %d on page %d",i,iPage); |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 7602 | }else{ |
| 7603 | for(j=pc+size-1; j>=pc; j--) hit[j]++; |
| 7604 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7605 | } |
drh | 8c2bbb6 | 2009-07-10 02:52:20 +0000 | [diff] [blame] | 7606 | i = get2byte(&data[hdr+1]); |
| 7607 | while( i>0 ){ |
| 7608 | int size, j; |
| 7609 | assert( i<=usableSize-4 ); /* Enforced by btreeInitPage() */ |
| 7610 | size = get2byte(&data[i+2]); |
| 7611 | assert( i+size<=usableSize ); /* Enforced by btreeInitPage() */ |
| 7612 | for(j=i+size-1; j>=i; j--) hit[j]++; |
| 7613 | j = get2byte(&data[i]); |
| 7614 | assert( j==0 || j>i+size ); /* Enforced by btreeInitPage() */ |
| 7615 | assert( j<=usableSize-4 ); /* Enforced by btreeInitPage() */ |
| 7616 | i = j; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7617 | } |
| 7618 | for(i=cnt=0; i<usableSize; i++){ |
| 7619 | if( hit[i]==0 ){ |
| 7620 | cnt++; |
| 7621 | }else if( hit[i]>1 ){ |
| 7622 | checkAppendMsg(pCheck, 0, |
| 7623 | "Multiple uses for byte %d of page %d", i, iPage); |
| 7624 | break; |
| 7625 | } |
| 7626 | } |
| 7627 | if( cnt!=data[hdr+7] ){ |
| 7628 | checkAppendMsg(pCheck, 0, |
drh | 8c2bbb6 | 2009-07-10 02:52:20 +0000 | [diff] [blame] | 7629 | "Fragmentation of %d bytes reported as %d on page %d", |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7630 | cnt, data[hdr+7], iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7631 | } |
| 7632 | } |
drh | 8c2bbb6 | 2009-07-10 02:52:20 +0000 | [diff] [blame] | 7633 | sqlite3PageFree(hit); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7634 | releasePage(pPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 7635 | return depth+1; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7636 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 7637 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7638 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 7639 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7640 | /* |
| 7641 | ** This routine does a complete check of the given BTree file. aRoot[] is |
| 7642 | ** an array of pages numbers were each page number is the root page of |
| 7643 | ** a table. nRoot is the number of entries in aRoot. |
| 7644 | ** |
danielk1977 | 3509a65 | 2009-07-06 18:56:13 +0000 | [diff] [blame] | 7645 | ** A read-only or read-write transaction must be opened before calling |
| 7646 | ** this function. |
| 7647 | ** |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 7648 | ** Write the number of error seen in *pnErr. Except for some memory |
drh | e43ba70 | 2008-12-05 22:40:08 +0000 | [diff] [blame] | 7649 | ** allocation errors, an error message held in memory obtained from |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 7650 | ** 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] | 7651 | ** returned. If a memory allocation error occurs, NULL is returned. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7652 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 7653 | char *sqlite3BtreeIntegrityCheck( |
| 7654 | Btree *p, /* The btree to be checked */ |
| 7655 | int *aRoot, /* An array of root pages numbers for individual trees */ |
| 7656 | int nRoot, /* Number of entries in aRoot[] */ |
| 7657 | int mxErr, /* Stop reporting errors after this many */ |
| 7658 | int *pnErr /* Write number of errors seen to this variable */ |
| 7659 | ){ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 7660 | Pgno i; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7661 | int nRef; |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 7662 | IntegrityCk sCheck; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7663 | BtShared *pBt = p->pBt; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 7664 | char zErr[100]; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7665 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7666 | sqlite3BtreeEnter(p); |
danielk1977 | 3509a65 | 2009-07-06 18:56:13 +0000 | [diff] [blame] | 7667 | assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7668 | nRef = sqlite3PagerRefcount(pBt->pPager); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7669 | sCheck.pBt = pBt; |
| 7670 | sCheck.pPager = pBt->pPager; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 7671 | sCheck.nPage = pagerPagecount(sCheck.pBt); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 7672 | sCheck.mxErr = mxErr; |
| 7673 | sCheck.nErr = 0; |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 7674 | sCheck.mallocFailed = 0; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 7675 | *pnErr = 0; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 7676 | if( sCheck.nPage==0 ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7677 | sqlite3BtreeLeave(p); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 7678 | return 0; |
| 7679 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 7680 | sCheck.anRef = sqlite3Malloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) ); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 7681 | if( !sCheck.anRef ){ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 7682 | *pnErr = 1; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7683 | sqlite3BtreeLeave(p); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 7684 | return 0; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 7685 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 7686 | for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; } |
drh | 42cac6d | 2004-11-20 20:31:11 +0000 | [diff] [blame] | 7687 | i = PENDING_BYTE_PAGE(pBt); |
drh | 1f59571 | 2004-06-15 01:40:29 +0000 | [diff] [blame] | 7688 | if( i<=sCheck.nPage ){ |
| 7689 | sCheck.anRef[i] = 1; |
| 7690 | } |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 7691 | sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), 20000); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7692 | |
| 7693 | /* Check the integrity of the freelist |
| 7694 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 7695 | checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), |
| 7696 | get4byte(&pBt->pPage1->aData[36]), "Main freelist: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7697 | |
| 7698 | /* Check all the tables. |
| 7699 | */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 7700 | for(i=0; (int)i<nRoot && sCheck.mxErr; i++){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 7701 | if( aRoot[i]==0 ) continue; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 7702 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 7703 | if( pBt->autoVacuum && aRoot[i]>1 ){ |
| 7704 | checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0); |
| 7705 | } |
| 7706 | #endif |
shaneh | 195475d | 2010-02-19 04:28:08 +0000 | [diff] [blame] | 7707 | checkTreePage(&sCheck, aRoot[i], "List of tree roots: ", NULL, NULL); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7708 | } |
| 7709 | |
| 7710 | /* Make sure every page in the file is referenced |
| 7711 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 7712 | for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7713 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7714 | if( sCheck.anRef[i]==0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7715 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7716 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7717 | #else |
| 7718 | /* If the database supports auto-vacuum, make sure no tables contain |
| 7719 | ** references to pointer-map pages. |
| 7720 | */ |
| 7721 | if( sCheck.anRef[i]==0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 7722 | (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7723 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
| 7724 | } |
| 7725 | if( sCheck.anRef[i]!=0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 7726 | (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7727 | checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i); |
| 7728 | } |
| 7729 | #endif |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7730 | } |
| 7731 | |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 7732 | /* Make sure this analysis did not leave any unref() pages. |
| 7733 | ** This is an internal consistency check; an integrity check |
| 7734 | ** of the integrity check. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7735 | */ |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 7736 | if( NEVER(nRef != sqlite3PagerRefcount(pBt->pPager)) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7737 | checkAppendMsg(&sCheck, 0, |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7738 | "Outstanding page count goes from %d to %d during this analysis", |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7739 | nRef, sqlite3PagerRefcount(pBt->pPager) |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7740 | ); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7741 | } |
| 7742 | |
| 7743 | /* Clean up and report errors. |
| 7744 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7745 | sqlite3BtreeLeave(p); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 7746 | sqlite3_free(sCheck.anRef); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 7747 | if( sCheck.mallocFailed ){ |
| 7748 | sqlite3StrAccumReset(&sCheck.errMsg); |
| 7749 | *pnErr = sCheck.nErr+1; |
| 7750 | return 0; |
| 7751 | } |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 7752 | *pnErr = sCheck.nErr; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 7753 | if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg); |
| 7754 | return sqlite3StrAccumFinish(&sCheck.errMsg); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7755 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 7756 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 7757 | |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 7758 | /* |
| 7759 | ** Return the full pathname of the underlying database file. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 7760 | ** |
| 7761 | ** The pager filename is invariant as long as the pager is |
| 7762 | ** open so it is safe to access without the BtShared mutex. |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 7763 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7764 | const char *sqlite3BtreeGetFilename(Btree *p){ |
| 7765 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7766 | return sqlite3PagerFilename(p->pBt->pPager); |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 7767 | } |
| 7768 | |
| 7769 | /* |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 7770 | ** Return the pathname of the journal file for this database. The return |
| 7771 | ** value of this routine is the same regardless of whether the journal file |
| 7772 | ** has been created or not. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 7773 | ** |
| 7774 | ** The pager journal filename is invariant as long as the pager is |
| 7775 | ** open so it is safe to access without the BtShared mutex. |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 7776 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7777 | const char *sqlite3BtreeGetJournalname(Btree *p){ |
| 7778 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7779 | return sqlite3PagerJournalname(p->pBt->pPager); |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 7780 | } |
| 7781 | |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 7782 | /* |
| 7783 | ** Return non-zero if a transaction is active. |
| 7784 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7785 | int sqlite3BtreeIsInTrans(Btree *p){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7786 | assert( p==0 || sqlite3_mutex_held(p->db->mutex) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7787 | return (p && (p->inTrans==TRANS_WRITE)); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 7788 | } |
| 7789 | |
| 7790 | /* |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 7791 | ** Return non-zero if a read (or write) transaction is active. |
| 7792 | */ |
| 7793 | int sqlite3BtreeIsInReadTrans(Btree *p){ |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 7794 | assert( p ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7795 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 7796 | return p->inTrans!=TRANS_NONE; |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 7797 | } |
| 7798 | |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 7799 | int sqlite3BtreeIsInBackup(Btree *p){ |
| 7800 | assert( p ); |
| 7801 | assert( sqlite3_mutex_held(p->db->mutex) ); |
| 7802 | return p->nBackup!=0; |
| 7803 | } |
| 7804 | |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 7805 | /* |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7806 | ** This function returns a pointer to a blob of memory associated with |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 7807 | ** 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] | 7808 | ** purposes (for example, to store a high-level schema associated with |
| 7809 | ** the shared-btree). The btree layer manages reference counting issues. |
| 7810 | ** |
| 7811 | ** The first time this is called on a shared-btree, nBytes bytes of memory |
| 7812 | ** are allocated, zeroed, and returned to the caller. For each subsequent |
| 7813 | ** call the nBytes parameter is ignored and a pointer to the same blob |
| 7814 | ** of memory returned. |
| 7815 | ** |
danielk1977 | 171bfed | 2008-06-23 09:50:50 +0000 | [diff] [blame] | 7816 | ** If the nBytes parameter is 0 and the blob of memory has not yet been |
| 7817 | ** allocated, a null pointer is returned. If the blob has already been |
| 7818 | ** allocated, it is returned as normal. |
| 7819 | ** |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7820 | ** Just before the shared-btree is closed, the function passed as the |
| 7821 | ** xFree argument when the memory allocation was made is invoked on the |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 7822 | ** blob of allocated memory. This function should not call sqlite3_free() |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7823 | ** on the memory, the btree layer does that. |
| 7824 | */ |
| 7825 | void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){ |
| 7826 | BtShared *pBt = p->pBt; |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7827 | sqlite3BtreeEnter(p); |
danielk1977 | 171bfed | 2008-06-23 09:50:50 +0000 | [diff] [blame] | 7828 | if( !pBt->pSchema && nBytes ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 7829 | pBt->pSchema = sqlite3MallocZero(nBytes); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7830 | pBt->xFreeSchema = xFree; |
| 7831 | } |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7832 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7833 | return pBt->pSchema; |
| 7834 | } |
| 7835 | |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 7836 | /* |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 7837 | ** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared |
| 7838 | ** btree as the argument handle holds an exclusive lock on the |
| 7839 | ** sqlite_master table. Otherwise SQLITE_OK. |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 7840 | */ |
| 7841 | int sqlite3BtreeSchemaLocked(Btree *p){ |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7842 | int rc; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7843 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7844 | sqlite3BtreeEnter(p); |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 7845 | rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK); |
| 7846 | assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7847 | sqlite3BtreeLeave(p); |
| 7848 | return rc; |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 7849 | } |
| 7850 | |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 7851 | |
| 7852 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 7853 | /* |
| 7854 | ** Obtain a lock on the table whose root page is iTab. The |
| 7855 | ** lock is a write lock if isWritelock is true or a read lock |
| 7856 | ** if it is false. |
| 7857 | */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7858 | int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){ |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 7859 | int rc = SQLITE_OK; |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 7860 | assert( p->inTrans!=TRANS_NONE ); |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 7861 | if( p->sharable ){ |
| 7862 | u8 lockType = READ_LOCK + isWriteLock; |
| 7863 | assert( READ_LOCK+1==WRITE_LOCK ); |
| 7864 | assert( isWriteLock==0 || isWriteLock==1 ); |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 7865 | |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 7866 | sqlite3BtreeEnter(p); |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 7867 | rc = querySharedCacheTableLock(p, iTab, lockType); |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 7868 | if( rc==SQLITE_OK ){ |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 7869 | rc = setSharedCacheTableLock(p, iTab, lockType); |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 7870 | } |
| 7871 | sqlite3BtreeLeave(p); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7872 | } |
| 7873 | return rc; |
| 7874 | } |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 7875 | #endif |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 7876 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7877 | #ifndef SQLITE_OMIT_INCRBLOB |
| 7878 | /* |
| 7879 | ** Argument pCsr must be a cursor opened for writing on an |
| 7880 | ** INTKEY table currently pointing at a valid table entry. |
| 7881 | ** This function modifies the data stored as part of that entry. |
danielk1977 | ecaecf9 | 2009-07-08 08:05:35 +0000 | [diff] [blame] | 7882 | ** |
| 7883 | ** Only the data content may only be modified, it is not possible to |
| 7884 | ** change the length of the data stored. If this function is called with |
| 7885 | ** parameters that attempt to write past the end of the existing data, |
| 7886 | ** no modifications are made and SQLITE_CORRUPT is returned. |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7887 | */ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7888 | int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){ |
danielk1977 | c9000e6 | 2009-07-08 13:55:28 +0000 | [diff] [blame] | 7889 | int rc; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7890 | assert( cursorHoldsMutex(pCsr) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7891 | assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) ); |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 7892 | assert( pCsr->isIncrblobHandle ); |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 7893 | |
danielk1977 | c9000e6 | 2009-07-08 13:55:28 +0000 | [diff] [blame] | 7894 | rc = restoreCursorPosition(pCsr); |
| 7895 | if( rc!=SQLITE_OK ){ |
| 7896 | return rc; |
| 7897 | } |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 7898 | assert( pCsr->eState!=CURSOR_REQUIRESEEK ); |
| 7899 | if( pCsr->eState!=CURSOR_VALID ){ |
| 7900 | return SQLITE_ABORT; |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7901 | } |
| 7902 | |
danielk1977 | c9000e6 | 2009-07-08 13:55:28 +0000 | [diff] [blame] | 7903 | /* Check some assumptions: |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7904 | ** (a) the cursor is open for writing, |
danielk1977 | c9000e6 | 2009-07-08 13:55:28 +0000 | [diff] [blame] | 7905 | ** (b) there is a read/write transaction open, |
| 7906 | ** (c) the connection holds a write-lock on the table (if required), |
| 7907 | ** (d) there are no conflicting read-locks, and |
| 7908 | ** (e) the cursor points at a valid row of an intKey table. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7909 | */ |
danielk1977 | 4f02960 | 2009-07-08 18:45:37 +0000 | [diff] [blame] | 7910 | if( !pCsr->wrFlag ){ |
| 7911 | return SQLITE_READONLY; |
| 7912 | } |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 7913 | assert( !pCsr->pBt->readOnly && pCsr->pBt->inTransaction==TRANS_WRITE ); |
| 7914 | assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) ); |
| 7915 | assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) ); |
danielk1977 | c9000e6 | 2009-07-08 13:55:28 +0000 | [diff] [blame] | 7916 | assert( pCsr->apPage[pCsr->iPage]->intKey ); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7917 | |
drh | fb19268 | 2009-07-11 18:26:28 +0000 | [diff] [blame] | 7918 | return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7919 | } |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7920 | |
| 7921 | /* |
| 7922 | ** 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] | 7923 | ** overflow list for the current row. This is used by cursors opened |
| 7924 | ** for incremental blob IO only. |
| 7925 | ** |
| 7926 | ** This function sets a flag only. The actual page location cache |
| 7927 | ** (stored in BtCursor.aOverflow[]) is allocated and used by function |
| 7928 | ** accessPayload() (the worker function for sqlite3BtreeData() and |
| 7929 | ** sqlite3BtreePutData()). |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7930 | */ |
| 7931 | void sqlite3BtreeCacheOverflow(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7932 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7933 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7934 | assert(!pCur->isIncrblobHandle); |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7935 | assert(!pCur->aOverflow); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7936 | pCur->isIncrblobHandle = 1; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7937 | } |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7938 | #endif |