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 | ************************************************************************* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 12 | ** This file implements an 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 | 5d433ce | 2010-08-14 16:02:52 +0000 | [diff] [blame] | 35 | /* |
| 36 | ** Extract a 2-byte big-endian integer from an array of unsigned bytes. |
| 37 | ** But if the value is zero, make it 65536. |
| 38 | ** |
| 39 | ** This routine is used to extract the "offset to cell content area" value |
| 40 | ** from the header of a btree page. If the page size is 65536 and the page |
| 41 | ** is empty, the offset should be 65536, but the 2-byte value stores zero. |
| 42 | ** This routine makes the necessary adjustment to 65536. |
| 43 | */ |
| 44 | #define get2byteNotZero(X) (((((int)get2byte(X))-1)&0xffff)+1) |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 45 | |
dan | 09ff9e1 | 2013-03-11 11:49:03 +0000 | [diff] [blame] | 46 | /* |
| 47 | ** Values passed as the 5th argument to allocateBtreePage() |
| 48 | */ |
| 49 | #define BTALLOC_ANY 0 /* Allocate any page */ |
| 50 | #define BTALLOC_EXACT 1 /* Allocate exact page if possible */ |
| 51 | #define BTALLOC_LE 2 /* Allocate any page <= the parameter */ |
| 52 | |
| 53 | /* |
| 54 | ** Macro IfNotOmitAV(x) returns (x) if SQLITE_OMIT_AUTOVACUUM is not |
| 55 | ** defined, or 0 if it is. For example: |
| 56 | ** |
| 57 | ** bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum); |
| 58 | */ |
| 59 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 60 | #define IfNotOmitAV(expr) (expr) |
| 61 | #else |
| 62 | #define IfNotOmitAV(expr) 0 |
| 63 | #endif |
| 64 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 65 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 66 | /* |
danielk1977 | 502b4e0 | 2008-09-02 14:07:24 +0000 | [diff] [blame] | 67 | ** A list of BtShared objects that are eligible for participation |
| 68 | ** in shared cache. This variable has file scope during normal builds, |
| 69 | ** but the test harness needs to access it so we make it global for |
| 70 | ** test builds. |
drh | 7555d8e | 2009-03-20 13:15:30 +0000 | [diff] [blame] | 71 | ** |
| 72 | ** Access to this variable is protected by SQLITE_MUTEX_STATIC_MASTER. |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 73 | */ |
| 74 | #ifdef SQLITE_TEST |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 75 | BtShared *SQLITE_WSD sqlite3SharedCacheList = 0; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 76 | #else |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 77 | static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 78 | #endif |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 79 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| 80 | |
| 81 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 82 | /* |
| 83 | ** Enable or disable the shared pager and schema features. |
| 84 | ** |
| 85 | ** This routine has no effect on existing database connections. |
| 86 | ** The shared cache setting effects only future calls to |
| 87 | ** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2(). |
| 88 | */ |
| 89 | int sqlite3_enable_shared_cache(int enable){ |
danielk1977 | 502b4e0 | 2008-09-02 14:07:24 +0000 | [diff] [blame] | 90 | sqlite3GlobalConfig.sharedCacheEnabled = enable; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 91 | return SQLITE_OK; |
| 92 | } |
| 93 | #endif |
| 94 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 95 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 96 | |
| 97 | #ifdef SQLITE_OMIT_SHARED_CACHE |
| 98 | /* |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 99 | ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(), |
| 100 | ** and clearAllSharedCacheTableLocks() |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 101 | ** manipulate entries in the BtShared.pLock linked list used to store |
| 102 | ** shared-cache table level locks. If the library is compiled with the |
| 103 | ** shared-cache feature disabled, then there is only ever one user |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 104 | ** of each BtShared structure and so this locking is not necessary. |
| 105 | ** So define the lock related functions as no-ops. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 106 | */ |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 107 | #define querySharedCacheTableLock(a,b,c) SQLITE_OK |
| 108 | #define setSharedCacheTableLock(a,b,c) SQLITE_OK |
| 109 | #define clearAllSharedCacheTableLocks(a) |
danielk1977 | 94b3073 | 2009-07-02 17:21:57 +0000 | [diff] [blame] | 110 | #define downgradeAllSharedCacheTableLocks(a) |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 111 | #define hasSharedCacheTableLock(a,b,c,d) 1 |
| 112 | #define hasReadConflicts(a, b) 0 |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 113 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 114 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 115 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 116 | |
| 117 | #ifdef SQLITE_DEBUG |
| 118 | /* |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 119 | **** This function is only used as part of an assert() statement. *** |
| 120 | ** |
| 121 | ** Check to see if pBtree holds the required locks to read or write to the |
| 122 | ** table with root page iRoot. Return 1 if it does and 0 if not. |
| 123 | ** |
| 124 | ** For example, when writing to a table with root-page iRoot via |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 125 | ** Btree connection pBtree: |
| 126 | ** |
| 127 | ** assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) ); |
| 128 | ** |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 129 | ** When writing to an index that resides in a sharable database, the |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 130 | ** caller should have first obtained a lock specifying the root page of |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 131 | ** the corresponding table. This makes things a bit more complicated, |
| 132 | ** as this module treats each table as a separate structure. To determine |
| 133 | ** the table corresponding to the index being written, this |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 134 | ** function has to search through the database schema. |
| 135 | ** |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 136 | ** 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] | 137 | ** hold a write-lock on the schema table (root page 1). This is also |
| 138 | ** acceptable. |
| 139 | */ |
| 140 | static int hasSharedCacheTableLock( |
| 141 | Btree *pBtree, /* Handle that must hold lock */ |
| 142 | Pgno iRoot, /* Root page of b-tree */ |
| 143 | int isIndex, /* True if iRoot is the root of an index b-tree */ |
| 144 | int eLockType /* Required lock type (READ_LOCK or WRITE_LOCK) */ |
| 145 | ){ |
| 146 | Schema *pSchema = (Schema *)pBtree->pBt->pSchema; |
| 147 | Pgno iTab = 0; |
| 148 | BtLock *pLock; |
| 149 | |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 150 | /* If this database is not shareable, or if the client is reading |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 151 | ** and has the read-uncommitted flag set, then no lock is required. |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 152 | ** Return true immediately. |
| 153 | */ |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 154 | if( (pBtree->sharable==0) |
| 155 | || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommitted)) |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 156 | ){ |
| 157 | return 1; |
| 158 | } |
| 159 | |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 160 | /* If the client is reading or writing an index and the schema is |
| 161 | ** not loaded, then it is too difficult to actually check to see if |
| 162 | ** the correct locks are held. So do not bother - just return true. |
| 163 | ** This case does not come up very often anyhow. |
| 164 | */ |
drh | 2c5e35f | 2014-08-05 11:04:21 +0000 | [diff] [blame] | 165 | if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){ |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 166 | return 1; |
| 167 | } |
| 168 | |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 169 | /* Figure out the root-page that the lock should be held on. For table |
| 170 | ** b-trees, this is just the root page of the b-tree being read or |
| 171 | ** written. For index b-trees, it is the root page of the associated |
| 172 | ** table. */ |
| 173 | if( isIndex ){ |
| 174 | HashElem *p; |
| 175 | for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){ |
| 176 | Index *pIdx = (Index *)sqliteHashData(p); |
shane | 5eff7cf | 2009-08-10 03:57:58 +0000 | [diff] [blame] | 177 | if( pIdx->tnum==(int)iRoot ){ |
| 178 | iTab = pIdx->pTable->tnum; |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | }else{ |
| 182 | iTab = iRoot; |
| 183 | } |
| 184 | |
| 185 | /* Search for the required lock. Either a write-lock on root-page iTab, a |
| 186 | ** write-lock on the schema table, or (if the client is reading) a |
| 187 | ** read-lock on iTab will suffice. Return 1 if any of these are found. */ |
| 188 | for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){ |
| 189 | if( pLock->pBtree==pBtree |
| 190 | && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1)) |
| 191 | && pLock->eLock>=eLockType |
| 192 | ){ |
| 193 | return 1; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /* Failed to find the required lock. */ |
| 198 | return 0; |
| 199 | } |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 200 | #endif /* SQLITE_DEBUG */ |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 201 | |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 202 | #ifdef SQLITE_DEBUG |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 203 | /* |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 204 | **** This function may be used as part of assert() statements only. **** |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 205 | ** |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 206 | ** Return true if it would be illegal for pBtree to write into the |
| 207 | ** table or index rooted at iRoot because other shared connections are |
| 208 | ** simultaneously reading that same table or index. |
| 209 | ** |
| 210 | ** It is illegal for pBtree to write if some other Btree object that |
| 211 | ** shares the same BtShared object is currently reading or writing |
| 212 | ** the iRoot table. Except, if the other Btree object has the |
| 213 | ** read-uncommitted flag set, then it is OK for the other object to |
| 214 | ** have a read cursor. |
| 215 | ** |
| 216 | ** For example, before writing to any part of the table or index |
| 217 | ** rooted at page iRoot, one should call: |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 218 | ** |
| 219 | ** assert( !hasReadConflicts(pBtree, iRoot) ); |
| 220 | */ |
| 221 | static int hasReadConflicts(Btree *pBtree, Pgno iRoot){ |
| 222 | BtCursor *p; |
| 223 | for(p=pBtree->pBt->pCursor; p; p=p->pNext){ |
| 224 | if( p->pgnoRoot==iRoot |
| 225 | && p->pBtree!=pBtree |
| 226 | && 0==(p->pBtree->db->flags & SQLITE_ReadUncommitted) |
| 227 | ){ |
| 228 | return 1; |
| 229 | } |
| 230 | } |
| 231 | return 0; |
| 232 | } |
| 233 | #endif /* #ifdef SQLITE_DEBUG */ |
| 234 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 235 | /* |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 236 | ** 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] | 237 | ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 238 | ** SQLITE_OK if the lock may be obtained (by calling |
| 239 | ** setSharedCacheTableLock()), or SQLITE_LOCKED if not. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 240 | */ |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 241 | static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 242 | BtShared *pBt = p->pBt; |
| 243 | BtLock *pIter; |
| 244 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 245 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 246 | assert( eLock==READ_LOCK || eLock==WRITE_LOCK ); |
| 247 | assert( p->db!=0 ); |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 248 | assert( !(p->db->flags&SQLITE_ReadUncommitted)||eLock==WRITE_LOCK||iTab==1 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 249 | |
danielk1977 | 5b413d7 | 2009-04-01 09:41:54 +0000 | [diff] [blame] | 250 | /* If requesting a write-lock, then the Btree must have an open write |
| 251 | ** transaction on this file. And, obviously, for this to be so there |
| 252 | ** must be an open write transaction on the file itself. |
| 253 | */ |
| 254 | assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) ); |
| 255 | assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE ); |
| 256 | |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 257 | /* This routine is a no-op if the shared-cache is not enabled */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 258 | if( !p->sharable ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 259 | return SQLITE_OK; |
| 260 | } |
| 261 | |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 262 | /* If some other connection is holding an exclusive lock, the |
| 263 | ** requested lock may not be obtained. |
| 264 | */ |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 265 | if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){ |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 266 | sqlite3ConnectionBlocked(p->db, pBt->pWriter->db); |
| 267 | return SQLITE_LOCKED_SHAREDCACHE; |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 268 | } |
| 269 | |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 270 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 271 | /* The condition (pIter->eLock!=eLock) in the following if(...) |
| 272 | ** statement is a simplification of: |
| 273 | ** |
| 274 | ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK) |
| 275 | ** |
| 276 | ** since we know that if eLock==WRITE_LOCK, then no other connection |
| 277 | ** may hold a WRITE_LOCK on any table in this file (since there can |
| 278 | ** only be a single writer). |
| 279 | */ |
| 280 | assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK ); |
| 281 | assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK); |
| 282 | if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){ |
| 283 | sqlite3ConnectionBlocked(p->db, pIter->pBtree->db); |
| 284 | if( eLock==WRITE_LOCK ){ |
| 285 | assert( p==pBt->pWriter ); |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 286 | pBt->btsFlags |= BTS_PENDING; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 287 | } |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 288 | return SQLITE_LOCKED_SHAREDCACHE; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | return SQLITE_OK; |
| 292 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 293 | #endif /* !SQLITE_OMIT_SHARED_CACHE */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 294 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 295 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 296 | /* |
| 297 | ** Add a lock on the table with root-page iTable to the shared-btree used |
| 298 | ** by Btree handle p. Parameter eLock must be either READ_LOCK or |
| 299 | ** WRITE_LOCK. |
| 300 | ** |
danielk1977 | 9d10486 | 2009-07-09 08:27:14 +0000 | [diff] [blame] | 301 | ** This function assumes the following: |
| 302 | ** |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 303 | ** (a) The specified Btree object p is connected to a sharable |
| 304 | ** database (one with the BtShared.sharable flag set), and |
danielk1977 | 9d10486 | 2009-07-09 08:27:14 +0000 | [diff] [blame] | 305 | ** |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 306 | ** (b) No other Btree objects hold a lock that conflicts |
danielk1977 | 9d10486 | 2009-07-09 08:27:14 +0000 | [diff] [blame] | 307 | ** with the requested lock (i.e. querySharedCacheTableLock() has |
| 308 | ** already been called and returned SQLITE_OK). |
| 309 | ** |
| 310 | ** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM |
| 311 | ** is returned if a malloc attempt fails. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 312 | */ |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 313 | static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 314 | BtShared *pBt = p->pBt; |
| 315 | BtLock *pLock = 0; |
| 316 | BtLock *pIter; |
| 317 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 318 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 319 | assert( eLock==READ_LOCK || eLock==WRITE_LOCK ); |
| 320 | assert( p->db!=0 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 321 | |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 322 | /* A connection with the read-uncommitted flag set will never try to |
| 323 | ** obtain a read-lock using this function. The only read-lock obtained |
| 324 | ** by a connection in read-uncommitted mode is on the sqlite_master |
| 325 | ** table, and that lock is obtained in BtreeBeginTrans(). */ |
| 326 | assert( 0==(p->db->flags&SQLITE_ReadUncommitted) || eLock==WRITE_LOCK ); |
| 327 | |
danielk1977 | 9d10486 | 2009-07-09 08:27:14 +0000 | [diff] [blame] | 328 | /* This function should only be called on a sharable b-tree after it |
| 329 | ** has been determined that no other b-tree holds a conflicting lock. */ |
| 330 | assert( p->sharable ); |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 331 | assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 332 | |
| 333 | /* First search the list for an existing lock on this table. */ |
| 334 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 335 | if( pIter->iTable==iTable && pIter->pBtree==p ){ |
| 336 | pLock = pIter; |
| 337 | break; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | /* If the above search did not find a BtLock struct associating Btree p |
| 342 | ** with table iTable, allocate one and link it into the list. |
| 343 | */ |
| 344 | if( !pLock ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 345 | pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock)); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 346 | if( !pLock ){ |
| 347 | return SQLITE_NOMEM; |
| 348 | } |
| 349 | pLock->iTable = iTable; |
| 350 | pLock->pBtree = p; |
| 351 | pLock->pNext = pBt->pLock; |
| 352 | pBt->pLock = pLock; |
| 353 | } |
| 354 | |
| 355 | /* Set the BtLock.eLock variable to the maximum of the current lock |
| 356 | ** and the requested lock. This means if a write-lock was already held |
| 357 | ** and a read-lock requested, we don't incorrectly downgrade the lock. |
| 358 | */ |
| 359 | assert( WRITE_LOCK>READ_LOCK ); |
danielk1977 | 5118b91 | 2005-12-30 16:31:53 +0000 | [diff] [blame] | 360 | if( eLock>pLock->eLock ){ |
| 361 | pLock->eLock = eLock; |
| 362 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 363 | |
| 364 | return SQLITE_OK; |
| 365 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 366 | #endif /* !SQLITE_OMIT_SHARED_CACHE */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 367 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 368 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 369 | /* |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 370 | ** Release all the table locks (locks obtained via calls to |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 371 | ** the setSharedCacheTableLock() procedure) held by Btree object p. |
danielk1977 | fa542f1 | 2009-04-02 18:28:08 +0000 | [diff] [blame] | 372 | ** |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 373 | ** This function assumes that Btree p has an open read or write |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 374 | ** transaction. If it does not, then the BTS_PENDING flag |
danielk1977 | fa542f1 | 2009-04-02 18:28:08 +0000 | [diff] [blame] | 375 | ** may be incorrectly cleared. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 376 | */ |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 377 | static void clearAllSharedCacheTableLocks(Btree *p){ |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 378 | BtShared *pBt = p->pBt; |
| 379 | BtLock **ppIter = &pBt->pLock; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 380 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 381 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 382 | assert( p->sharable || 0==*ppIter ); |
danielk1977 | fa542f1 | 2009-04-02 18:28:08 +0000 | [diff] [blame] | 383 | assert( p->inTrans>0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 384 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 385 | while( *ppIter ){ |
| 386 | BtLock *pLock = *ppIter; |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 387 | assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree ); |
danielk1977 | fa542f1 | 2009-04-02 18:28:08 +0000 | [diff] [blame] | 388 | assert( pLock->pBtree->inTrans>=pLock->eLock ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 389 | if( pLock->pBtree==p ){ |
| 390 | *ppIter = pLock->pNext; |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 391 | assert( pLock->iTable!=1 || pLock==&p->lock ); |
| 392 | if( pLock->iTable!=1 ){ |
| 393 | sqlite3_free(pLock); |
| 394 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 395 | }else{ |
| 396 | ppIter = &pLock->pNext; |
| 397 | } |
| 398 | } |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 399 | |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 400 | assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter ); |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 401 | if( pBt->pWriter==p ){ |
| 402 | pBt->pWriter = 0; |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 403 | pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING); |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 404 | }else if( pBt->nTransaction==2 ){ |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 405 | /* This function is called when Btree p is concluding its |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 406 | ** transaction. If there currently exists a writer, and p is not |
| 407 | ** that writer, then the number of locks held by connections other |
| 408 | ** than the writer must be about to drop to zero. In this case |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 409 | ** set the BTS_PENDING flag to 0. |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 410 | ** |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 411 | ** If there is not currently a writer, then BTS_PENDING must |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 412 | ** be zero already. So this next line is harmless in that case. |
| 413 | */ |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 414 | pBt->btsFlags &= ~BTS_PENDING; |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 415 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 416 | } |
danielk1977 | 94b3073 | 2009-07-02 17:21:57 +0000 | [diff] [blame] | 417 | |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 418 | /* |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 419 | ** This function changes all write-locks held by Btree p into read-locks. |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 420 | */ |
danielk1977 | 94b3073 | 2009-07-02 17:21:57 +0000 | [diff] [blame] | 421 | static void downgradeAllSharedCacheTableLocks(Btree *p){ |
| 422 | BtShared *pBt = p->pBt; |
| 423 | if( pBt->pWriter==p ){ |
| 424 | BtLock *pLock; |
| 425 | pBt->pWriter = 0; |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 426 | pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING); |
danielk1977 | 94b3073 | 2009-07-02 17:21:57 +0000 | [diff] [blame] | 427 | for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){ |
| 428 | assert( pLock->eLock==READ_LOCK || pLock->pBtree==p ); |
| 429 | pLock->eLock = READ_LOCK; |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 434 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| 435 | |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 436 | static void releasePage(MemPage *pPage); /* Forward reference */ |
| 437 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 438 | /* |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 439 | ***** This routine is used inside of assert() only **** |
| 440 | ** |
| 441 | ** Verify that the cursor holds the mutex on its BtShared |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 442 | */ |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 443 | #ifdef SQLITE_DEBUG |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 444 | static int cursorHoldsMutex(BtCursor *p){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 445 | return sqlite3_mutex_held(p->pBt->mutex); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 446 | } |
| 447 | #endif |
| 448 | |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 449 | /* |
dan | 5a500af | 2014-03-11 20:33:04 +0000 | [diff] [blame] | 450 | ** Invalidate the overflow cache of the cursor passed as the first argument. |
| 451 | ** on the shared btree structure pBt. |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 452 | */ |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 453 | #define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl) |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 454 | |
| 455 | /* |
| 456 | ** Invalidate the overflow page-list cache for all cursors opened |
| 457 | ** on the shared btree structure pBt. |
| 458 | */ |
| 459 | static void invalidateAllOverflowCache(BtShared *pBt){ |
| 460 | BtCursor *p; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 461 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 462 | for(p=pBt->pCursor; p; p=p->pNext){ |
| 463 | invalidateOverflowCache(p); |
| 464 | } |
| 465 | } |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 466 | |
dan | 5a500af | 2014-03-11 20:33:04 +0000 | [diff] [blame] | 467 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 468 | /* |
| 469 | ** This function is called before modifying the contents of a table |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 470 | ** to invalidate any incrblob cursors that are open on the |
drh | eeb844a | 2009-08-08 18:01:07 +0000 | [diff] [blame] | 471 | ** row or one of the rows being modified. |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 472 | ** |
| 473 | ** If argument isClearTable is true, then the entire contents of the |
| 474 | ** table is about to be deleted. In this case invalidate all incrblob |
| 475 | ** cursors open on any row within the table with root-page pgnoRoot. |
| 476 | ** |
| 477 | ** Otherwise, if argument isClearTable is false, then the row with |
| 478 | ** rowid iRow is being replaced or deleted. In this case invalidate |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 479 | ** only those incrblob cursors open on that specific row. |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 480 | */ |
| 481 | static void invalidateIncrblobCursors( |
| 482 | Btree *pBtree, /* The database file to check */ |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 483 | i64 iRow, /* The rowid that might be changing */ |
| 484 | int isClearTable /* True if all rows are being deleted */ |
| 485 | ){ |
| 486 | BtCursor *p; |
| 487 | BtShared *pBt = pBtree->pBt; |
| 488 | assert( sqlite3BtreeHoldsMutex(pBtree) ); |
| 489 | for(p=pBt->pCursor; p; p=p->pNext){ |
drh | 3f38740 | 2014-09-24 01:23:00 +0000 | [diff] [blame] | 490 | if( (p->curFlags & BTCF_Incrblob)!=0 |
| 491 | && (isClearTable || p->info.nKey==iRow) |
| 492 | ){ |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 493 | p->eState = CURSOR_INVALID; |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 498 | #else |
dan | 5a500af | 2014-03-11 20:33:04 +0000 | [diff] [blame] | 499 | /* Stub function when INCRBLOB is omitted */ |
drh | eeb844a | 2009-08-08 18:01:07 +0000 | [diff] [blame] | 500 | #define invalidateIncrblobCursors(x,y,z) |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 501 | #endif /* SQLITE_OMIT_INCRBLOB */ |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 502 | |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 503 | /* |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 504 | ** Set bit pgno of the BtShared.pHasContent bitvec. This is called |
| 505 | ** when a page that previously contained data becomes a free-list leaf |
| 506 | ** page. |
| 507 | ** |
| 508 | ** The BtShared.pHasContent bitvec exists to work around an obscure |
| 509 | ** bug caused by the interaction of two useful IO optimizations surrounding |
| 510 | ** free-list leaf pages: |
| 511 | ** |
| 512 | ** 1) When all data is deleted from a page and the page becomes |
| 513 | ** a free-list leaf page, the page is not written to the database |
| 514 | ** (as free-list leaf pages contain no meaningful data). Sometimes |
| 515 | ** such a page is not even journalled (as it will not be modified, |
| 516 | ** why bother journalling it?). |
| 517 | ** |
| 518 | ** 2) When a free-list leaf page is reused, its content is not read |
| 519 | ** from the database or written to the journal file (why should it |
| 520 | ** be, if it is not at all meaningful?). |
| 521 | ** |
| 522 | ** By themselves, these optimizations work fine and provide a handy |
| 523 | ** performance boost to bulk delete or insert operations. However, if |
| 524 | ** a page is moved to the free-list and then reused within the same |
| 525 | ** transaction, a problem comes up. If the page is not journalled when |
| 526 | ** it is moved to the free-list and it is also not journalled when it |
| 527 | ** is extracted from the free-list and reused, then the original data |
| 528 | ** may be lost. In the event of a rollback, it may not be possible |
| 529 | ** to restore the database to its original configuration. |
| 530 | ** |
| 531 | ** The solution is the BtShared.pHasContent bitvec. Whenever a page is |
| 532 | ** moved to become a free-list leaf page, the corresponding bit is |
| 533 | ** 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] | 534 | ** optimization 2 above is omitted if the corresponding bit is already |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 535 | ** set in BtShared.pHasContent. The contents of the bitvec are cleared |
| 536 | ** at the end of every transaction. |
| 537 | */ |
| 538 | static int btreeSetHasContent(BtShared *pBt, Pgno pgno){ |
| 539 | int rc = SQLITE_OK; |
| 540 | if( !pBt->pHasContent ){ |
drh | dd3cd97 | 2010-03-27 17:12:36 +0000 | [diff] [blame] | 541 | assert( pgno<=pBt->nPage ); |
| 542 | pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage); |
drh | 4c301aa | 2009-07-15 17:25:45 +0000 | [diff] [blame] | 543 | if( !pBt->pHasContent ){ |
| 544 | rc = SQLITE_NOMEM; |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){ |
| 548 | rc = sqlite3BitvecSet(pBt->pHasContent, pgno); |
| 549 | } |
| 550 | return rc; |
| 551 | } |
| 552 | |
| 553 | /* |
| 554 | ** Query the BtShared.pHasContent vector. |
| 555 | ** |
| 556 | ** This function is called when a free-list leaf page is removed from the |
| 557 | ** free-list for reuse. It returns false if it is safe to retrieve the |
| 558 | ** page from the pager layer with the 'no-content' flag set. True otherwise. |
| 559 | */ |
| 560 | static int btreeGetHasContent(BtShared *pBt, Pgno pgno){ |
| 561 | Bitvec *p = pBt->pHasContent; |
| 562 | return (p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTest(p, pgno))); |
| 563 | } |
| 564 | |
| 565 | /* |
| 566 | ** Clear (destroy) the BtShared.pHasContent bitvec. This should be |
| 567 | ** invoked at the conclusion of each write-transaction. |
| 568 | */ |
| 569 | static void btreeClearHasContent(BtShared *pBt){ |
| 570 | sqlite3BitvecDestroy(pBt->pHasContent); |
| 571 | pBt->pHasContent = 0; |
| 572 | } |
| 573 | |
| 574 | /* |
drh | 138eeeb | 2013-03-27 03:15:23 +0000 | [diff] [blame] | 575 | ** Release all of the apPage[] pages for a cursor. |
| 576 | */ |
| 577 | static void btreeReleaseAllCursorPages(BtCursor *pCur){ |
| 578 | int i; |
| 579 | for(i=0; i<=pCur->iPage; i++){ |
| 580 | releasePage(pCur->apPage[i]); |
| 581 | pCur->apPage[i] = 0; |
| 582 | } |
| 583 | pCur->iPage = -1; |
| 584 | } |
| 585 | |
| 586 | |
| 587 | /* |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 588 | ** Save the current cursor position in the variables BtCursor.nKey |
| 589 | ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK. |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 590 | ** |
| 591 | ** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID) |
| 592 | ** prior to calling this routine. |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 593 | */ |
| 594 | static int saveCursorPosition(BtCursor *pCur){ |
| 595 | int rc; |
| 596 | |
| 597 | assert( CURSOR_VALID==pCur->eState ); |
| 598 | assert( 0==pCur->pKey ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 599 | assert( cursorHoldsMutex(pCur) ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 600 | |
| 601 | rc = sqlite3BtreeKeySize(pCur, &pCur->nKey); |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 602 | assert( rc==SQLITE_OK ); /* KeySize() cannot fail */ |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 603 | |
| 604 | /* If this is an intKey table, then the above call to BtreeKeySize() |
| 605 | ** stores the integer key in pCur->nKey. In this case this value is |
| 606 | ** all that is required. Otherwise, if pCur is not open on an intKey |
| 607 | ** table, then malloc space for and store the pCur->nKey bytes of key |
| 608 | ** data. |
| 609 | */ |
drh | 4c301aa | 2009-07-15 17:25:45 +0000 | [diff] [blame] | 610 | if( 0==pCur->apPage[0]->intKey ){ |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 611 | void *pKey = sqlite3Malloc( pCur->nKey ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 612 | if( pKey ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 613 | rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 614 | if( rc==SQLITE_OK ){ |
| 615 | pCur->pKey = pKey; |
| 616 | }else{ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 617 | sqlite3_free(pKey); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 618 | } |
| 619 | }else{ |
| 620 | rc = SQLITE_NOMEM; |
| 621 | } |
| 622 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 623 | assert( !pCur->apPage[0]->intKey || !pCur->pKey ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 624 | |
| 625 | if( rc==SQLITE_OK ){ |
drh | 138eeeb | 2013-03-27 03:15:23 +0000 | [diff] [blame] | 626 | btreeReleaseAllCursorPages(pCur); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 627 | pCur->eState = CURSOR_REQUIRESEEK; |
| 628 | } |
| 629 | |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 630 | invalidateOverflowCache(pCur); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 631 | return rc; |
| 632 | } |
| 633 | |
drh | 637f3d8 | 2014-08-22 22:26:07 +0000 | [diff] [blame] | 634 | /* Forward reference */ |
| 635 | static int SQLITE_NOINLINE saveCursorsOnList(BtCursor*,Pgno,BtCursor*); |
| 636 | |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 637 | /* |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 638 | ** Save the positions of all cursors (except pExcept) that are open on |
drh | 637f3d8 | 2014-08-22 22:26:07 +0000 | [diff] [blame] | 639 | ** the table with root-page iRoot. "Saving the cursor position" means that |
| 640 | ** the location in the btree is remembered in such a way that it can be |
| 641 | ** moved back to the same spot after the btree has been modified. This |
| 642 | ** routine is called just before cursor pExcept is used to modify the |
| 643 | ** table, for example in BtreeDelete() or BtreeInsert(). |
| 644 | ** |
| 645 | ** Implementation note: This routine merely checks to see if any cursors |
| 646 | ** need to be saved. It calls out to saveCursorsOnList() in the (unusual) |
| 647 | ** event that cursors are in need to being saved. |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 648 | */ |
| 649 | static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){ |
drh | 3bdffdd | 2014-08-23 19:08:09 +0000 | [diff] [blame] | 650 | BtCursor *p; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 651 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 652 | assert( pExcept==0 || pExcept->pBt==pBt ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 653 | for(p=pBt->pCursor; p; p=p->pNext){ |
drh | 637f3d8 | 2014-08-22 22:26:07 +0000 | [diff] [blame] | 654 | if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break; |
| 655 | } |
| 656 | return p ? saveCursorsOnList(p, iRoot, pExcept) : SQLITE_OK; |
| 657 | } |
| 658 | |
| 659 | /* This helper routine to saveAllCursors does the actual work of saving |
| 660 | ** the cursors if and when a cursor is found that actually requires saving. |
| 661 | ** The common case is that no cursors need to be saved, so this routine is |
| 662 | ** broken out from its caller to avoid unnecessary stack pointer movement. |
| 663 | */ |
| 664 | static int SQLITE_NOINLINE saveCursorsOnList( |
drh | 3f38740 | 2014-09-24 01:23:00 +0000 | [diff] [blame] | 665 | BtCursor *p, /* The first cursor that needs saving */ |
| 666 | Pgno iRoot, /* Only save cursor with this iRoot. Save all if zero */ |
| 667 | BtCursor *pExcept /* Do not save this cursor */ |
drh | 637f3d8 | 2014-08-22 22:26:07 +0000 | [diff] [blame] | 668 | ){ |
| 669 | do{ |
drh | 138eeeb | 2013-03-27 03:15:23 +0000 | [diff] [blame] | 670 | if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){ |
| 671 | if( p->eState==CURSOR_VALID ){ |
| 672 | int rc = saveCursorPosition(p); |
| 673 | if( SQLITE_OK!=rc ){ |
| 674 | return rc; |
| 675 | } |
| 676 | }else{ |
| 677 | testcase( p->iPage>0 ); |
| 678 | btreeReleaseAllCursorPages(p); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 679 | } |
| 680 | } |
drh | 637f3d8 | 2014-08-22 22:26:07 +0000 | [diff] [blame] | 681 | p = p->pNext; |
| 682 | }while( p ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 683 | return SQLITE_OK; |
| 684 | } |
| 685 | |
| 686 | /* |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 687 | ** Clear the current cursor position. |
| 688 | */ |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 689 | void sqlite3BtreeClearCursor(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 690 | assert( cursorHoldsMutex(pCur) ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 691 | sqlite3_free(pCur->pKey); |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 692 | pCur->pKey = 0; |
| 693 | pCur->eState = CURSOR_INVALID; |
| 694 | } |
| 695 | |
| 696 | /* |
danielk1977 | 3509a65 | 2009-07-06 18:56:13 +0000 | [diff] [blame] | 697 | ** In this version of BtreeMoveto, pKey is a packed index record |
| 698 | ** such as is generated by the OP_MakeRecord opcode. Unpack the |
| 699 | ** record and then call BtreeMovetoUnpacked() to do the work. |
| 700 | */ |
| 701 | static int btreeMoveto( |
| 702 | BtCursor *pCur, /* Cursor open on the btree to be searched */ |
| 703 | const void *pKey, /* Packed key if the btree is an index */ |
| 704 | i64 nKey, /* Integer key for tables. Size of pKey for indices */ |
| 705 | int bias, /* Bias search to the high end */ |
| 706 | int *pRes /* Write search results here */ |
| 707 | ){ |
| 708 | int rc; /* Status code */ |
| 709 | UnpackedRecord *pIdxKey; /* Unpacked index key */ |
drh | b413922 | 2013-11-06 14:36:08 +0000 | [diff] [blame] | 710 | char aSpace[200]; /* Temp space for pIdxKey - to avoid a malloc */ |
dan | 03e9cfc | 2011-09-05 14:20:27 +0000 | [diff] [blame] | 711 | char *pFree = 0; |
danielk1977 | 3509a65 | 2009-07-06 18:56:13 +0000 | [diff] [blame] | 712 | |
| 713 | if( pKey ){ |
| 714 | assert( nKey==(i64)(int)nKey ); |
dan | 03e9cfc | 2011-09-05 14:20:27 +0000 | [diff] [blame] | 715 | pIdxKey = sqlite3VdbeAllocUnpackedRecord( |
| 716 | pCur->pKeyInfo, aSpace, sizeof(aSpace), &pFree |
| 717 | ); |
danielk1977 | 3509a65 | 2009-07-06 18:56:13 +0000 | [diff] [blame] | 718 | if( pIdxKey==0 ) return SQLITE_NOMEM; |
mistachkin | 0fe5f95 | 2011-09-14 18:19:08 +0000 | [diff] [blame] | 719 | sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, pIdxKey); |
drh | 094b758 | 2013-11-30 12:49:28 +0000 | [diff] [blame] | 720 | if( pIdxKey->nField==0 ){ |
| 721 | sqlite3DbFree(pCur->pKeyInfo->db, pFree); |
| 722 | return SQLITE_CORRUPT_BKPT; |
| 723 | } |
danielk1977 | 3509a65 | 2009-07-06 18:56:13 +0000 | [diff] [blame] | 724 | }else{ |
| 725 | pIdxKey = 0; |
| 726 | } |
| 727 | rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes); |
dan | 42acb3e | 2011-09-05 20:16:38 +0000 | [diff] [blame] | 728 | if( pFree ){ |
dan | 03e9cfc | 2011-09-05 14:20:27 +0000 | [diff] [blame] | 729 | sqlite3DbFree(pCur->pKeyInfo->db, pFree); |
danielk1977 | 3509a65 | 2009-07-06 18:56:13 +0000 | [diff] [blame] | 730 | } |
| 731 | return rc; |
| 732 | } |
| 733 | |
| 734 | /* |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 735 | ** Restore the cursor to the position it was in (or as close to as possible) |
| 736 | ** when saveCursorPosition() was called. Note that this call deletes the |
| 737 | ** saved position info stored by saveCursorPosition(), so there can be |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 738 | ** at most one effective restoreCursorPosition() call after each |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 739 | ** saveCursorPosition(). |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 740 | */ |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 741 | static int btreeRestoreCursorPosition(BtCursor *pCur){ |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 742 | int rc; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 743 | assert( cursorHoldsMutex(pCur) ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 744 | assert( pCur->eState>=CURSOR_REQUIRESEEK ); |
| 745 | if( pCur->eState==CURSOR_FAULT ){ |
drh | 4c301aa | 2009-07-15 17:25:45 +0000 | [diff] [blame] | 746 | return pCur->skipNext; |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 747 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 748 | pCur->eState = CURSOR_INVALID; |
drh | 4c301aa | 2009-07-15 17:25:45 +0000 | [diff] [blame] | 749 | rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skipNext); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 750 | if( rc==SQLITE_OK ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 751 | sqlite3_free(pCur->pKey); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 752 | pCur->pKey = 0; |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 753 | assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID ); |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 754 | if( pCur->skipNext && pCur->eState==CURSOR_VALID ){ |
| 755 | pCur->eState = CURSOR_SKIPNEXT; |
| 756 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 757 | } |
| 758 | return rc; |
| 759 | } |
| 760 | |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 761 | #define restoreCursorPosition(p) \ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 762 | (p->eState>=CURSOR_REQUIRESEEK ? \ |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 763 | btreeRestoreCursorPosition(p) : \ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 764 | SQLITE_OK) |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 765 | |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 766 | /* |
drh | 6848dad | 2014-08-22 23:33:03 +0000 | [diff] [blame] | 767 | ** Determine whether or not a cursor has moved from the position where |
| 768 | ** it was last placed, or has been invalidated for any other reason. |
| 769 | ** Cursors can move when the row they are pointing at is deleted out |
| 770 | ** from under them, for example. Cursor might also move if a btree |
| 771 | ** is rebalanced. |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 772 | ** |
drh | 6848dad | 2014-08-22 23:33:03 +0000 | [diff] [blame] | 773 | ** Calling this routine with a NULL cursor pointer returns false. |
drh | 86dd371 | 2014-03-25 11:00:21 +0000 | [diff] [blame] | 774 | ** |
drh | 6848dad | 2014-08-22 23:33:03 +0000 | [diff] [blame] | 775 | ** Use the separate sqlite3BtreeCursorRestore() routine to restore a cursor |
| 776 | ** back to where it ought to be if this routine returns true. |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 777 | */ |
drh | 6848dad | 2014-08-22 23:33:03 +0000 | [diff] [blame] | 778 | int sqlite3BtreeCursorHasMoved(BtCursor *pCur){ |
drh | c22284f | 2014-10-13 16:02:20 +0000 | [diff] [blame] | 779 | return pCur->eState!=CURSOR_VALID; |
drh | 6848dad | 2014-08-22 23:33:03 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | /* |
| 783 | ** This routine restores a cursor back to its original position after it |
| 784 | ** has been moved by some outside activity (such as a btree rebalance or |
| 785 | ** a row having been deleted out from under the cursor). |
| 786 | ** |
| 787 | ** On success, the *pDifferentRow parameter is false if the cursor is left |
| 788 | ** pointing at exactly the same row. *pDifferntRow is the row the cursor |
| 789 | ** was pointing to has been deleted, forcing the cursor to point to some |
| 790 | ** nearby row. |
| 791 | ** |
| 792 | ** This routine should only be called for a cursor that just returned |
| 793 | ** TRUE from sqlite3BtreeCursorHasMoved(). |
| 794 | */ |
| 795 | int sqlite3BtreeCursorRestore(BtCursor *pCur, int *pDifferentRow){ |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 796 | int rc; |
| 797 | |
drh | 6848dad | 2014-08-22 23:33:03 +0000 | [diff] [blame] | 798 | assert( pCur!=0 ); |
| 799 | assert( pCur->eState!=CURSOR_VALID ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 800 | rc = restoreCursorPosition(pCur); |
| 801 | if( rc ){ |
drh | 6848dad | 2014-08-22 23:33:03 +0000 | [diff] [blame] | 802 | *pDifferentRow = 1; |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 803 | return rc; |
| 804 | } |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 805 | if( pCur->eState!=CURSOR_VALID || NEVER(pCur->skipNext!=0) ){ |
drh | 6848dad | 2014-08-22 23:33:03 +0000 | [diff] [blame] | 806 | *pDifferentRow = 1; |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 807 | }else{ |
drh | 6848dad | 2014-08-22 23:33:03 +0000 | [diff] [blame] | 808 | *pDifferentRow = 0; |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 809 | } |
| 810 | return SQLITE_OK; |
| 811 | } |
| 812 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 813 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 814 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 815 | ** Given a page number of a regular database page, return the page |
| 816 | ** number for the pointer-map page that contains the entry for the |
| 817 | ** input page number. |
drh | 5f77b2e | 2010-08-21 15:09:37 +0000 | [diff] [blame] | 818 | ** |
| 819 | ** Return 0 (not a valid page) for pgno==1 since there is |
| 820 | ** no pointer map associated with page 1. The integrity_check logic |
| 821 | ** requires that ptrmapPageno(*,1)!=1. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 822 | */ |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 823 | static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 824 | int nPagesPerMapPage; |
| 825 | Pgno iPtrMap, ret; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 826 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 5f77b2e | 2010-08-21 15:09:37 +0000 | [diff] [blame] | 827 | if( pgno<2 ) return 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 828 | nPagesPerMapPage = (pBt->usableSize/5)+1; |
| 829 | iPtrMap = (pgno-2)/nPagesPerMapPage; |
| 830 | ret = (iPtrMap*nPagesPerMapPage) + 2; |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 831 | if( ret==PENDING_BYTE_PAGE(pBt) ){ |
| 832 | ret++; |
| 833 | } |
| 834 | return ret; |
| 835 | } |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 836 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 837 | /* |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 838 | ** Write an entry into the pointer map. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 839 | ** |
| 840 | ** This routine updates the pointer map entry for page number 'key' |
| 841 | ** so that it maps to type 'eType' and parent page number 'pgno'. |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 842 | ** |
| 843 | ** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is |
| 844 | ** a no-op. If an error occurs, the appropriate error code is written |
| 845 | ** into *pRC. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 846 | */ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 847 | static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 848 | DbPage *pDbPage; /* The pointer map page */ |
| 849 | u8 *pPtrmap; /* The pointer map data */ |
| 850 | Pgno iPtrmap; /* The pointer map page number */ |
| 851 | int offset; /* Offset in pointer map page */ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 852 | int rc; /* Return code from subfunctions */ |
| 853 | |
| 854 | if( *pRC ) return; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 855 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 856 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 857 | /* The master-journal page number must never be used as a pointer map page */ |
| 858 | assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) ); |
| 859 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 860 | assert( pBt->autoVacuum ); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 861 | if( key==0 ){ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 862 | *pRC = SQLITE_CORRUPT_BKPT; |
| 863 | return; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 864 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 865 | iPtrmap = PTRMAP_PAGENO(pBt, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 866 | rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 867 | if( rc!=SQLITE_OK ){ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 868 | *pRC = rc; |
| 869 | return; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 870 | } |
danielk1977 | 8c666b1 | 2008-07-18 09:34:57 +0000 | [diff] [blame] | 871 | offset = PTRMAP_PTROFFSET(iPtrmap, key); |
drh | acfc72b | 2009-06-05 18:44:15 +0000 | [diff] [blame] | 872 | if( offset<0 ){ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 873 | *pRC = SQLITE_CORRUPT_BKPT; |
drh | 4925a55 | 2009-07-07 11:39:58 +0000 | [diff] [blame] | 874 | goto ptrmap_exit; |
drh | acfc72b | 2009-06-05 18:44:15 +0000 | [diff] [blame] | 875 | } |
drh | fc24373 | 2011-05-17 15:21:56 +0000 | [diff] [blame] | 876 | assert( offset <= (int)pBt->usableSize-5 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 877 | pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 878 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 879 | if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){ |
| 880 | TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent)); |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 881 | *pRC= rc = sqlite3PagerWrite(pDbPage); |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 882 | if( rc==SQLITE_OK ){ |
| 883 | pPtrmap[offset] = eType; |
| 884 | put4byte(&pPtrmap[offset+1], parent); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 885 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 886 | } |
| 887 | |
drh | 4925a55 | 2009-07-07 11:39:58 +0000 | [diff] [blame] | 888 | ptrmap_exit: |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 889 | sqlite3PagerUnref(pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | /* |
| 893 | ** Read an entry from the pointer map. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 894 | ** |
| 895 | ** This routine retrieves the pointer map entry for page 'key', writing |
| 896 | ** the type and parent page number to *pEType and *pPgno respectively. |
| 897 | ** An error code is returned if something goes wrong, otherwise SQLITE_OK. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 898 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 899 | static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 900 | DbPage *pDbPage; /* The pointer map page */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 901 | int iPtrmap; /* Pointer map page index */ |
| 902 | u8 *pPtrmap; /* Pointer map page data */ |
| 903 | int offset; /* Offset of entry in pointer map */ |
| 904 | int rc; |
| 905 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 906 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 907 | |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 908 | iPtrmap = PTRMAP_PAGENO(pBt, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 909 | rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 910 | if( rc!=0 ){ |
| 911 | return rc; |
| 912 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 913 | pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 914 | |
danielk1977 | 8c666b1 | 2008-07-18 09:34:57 +0000 | [diff] [blame] | 915 | offset = PTRMAP_PTROFFSET(iPtrmap, key); |
drh | fc24373 | 2011-05-17 15:21:56 +0000 | [diff] [blame] | 916 | if( offset<0 ){ |
| 917 | sqlite3PagerUnref(pDbPage); |
| 918 | return SQLITE_CORRUPT_BKPT; |
| 919 | } |
| 920 | assert( offset <= (int)pBt->usableSize-5 ); |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 921 | assert( pEType!=0 ); |
| 922 | *pEType = pPtrmap[offset]; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 923 | if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 924 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 925 | sqlite3PagerUnref(pDbPage); |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 926 | if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 927 | return SQLITE_OK; |
| 928 | } |
| 929 | |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 930 | #else /* if defined SQLITE_OMIT_AUTOVACUUM */ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 931 | #define ptrmapPut(w,x,y,z,rc) |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 932 | #define ptrmapGet(w,x,y,z) SQLITE_OK |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 933 | #define ptrmapPutOvflPtr(x, y, rc) |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 934 | #endif |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 935 | |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 936 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 937 | ** Given a btree page and a cell index (0 means the first cell on |
| 938 | ** the page, 1 means the second cell, and so forth) return a pointer |
| 939 | ** to the cell content. |
| 940 | ** |
| 941 | ** This routine works only for pages that do not contain overflow cells. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 942 | */ |
drh | 1688c86 | 2008-07-18 02:44:17 +0000 | [diff] [blame] | 943 | #define findCell(P,I) \ |
drh | 3def235 | 2011-11-11 00:27:15 +0000 | [diff] [blame] | 944 | ((P)->aData + ((P)->maskPage & get2byte(&(P)->aCellIdx[2*(I)]))) |
drh | 68f2a57 | 2011-06-03 17:50:49 +0000 | [diff] [blame] | 945 | #define findCellv2(D,M,O,I) (D+(M&get2byte(D+(O+2*(I))))) |
| 946 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 947 | |
| 948 | /* |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 949 | ** This a more complex version of findCell() that works for |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 950 | ** pages that do contain overflow cells. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 951 | */ |
| 952 | static u8 *findOverflowCell(MemPage *pPage, int iCell){ |
| 953 | int i; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 954 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 955 | for(i=pPage->nOverflow-1; i>=0; i--){ |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 956 | int k; |
drh | 2cbd78b | 2012-02-02 19:37:18 +0000 | [diff] [blame] | 957 | k = pPage->aiOvfl[i]; |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 958 | if( k<=iCell ){ |
| 959 | if( k==iCell ){ |
drh | 2cbd78b | 2012-02-02 19:37:18 +0000 | [diff] [blame] | 960 | return pPage->apOvfl[i]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 961 | } |
| 962 | iCell--; |
| 963 | } |
| 964 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 965 | return findCell(pPage, iCell); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | /* |
| 969 | ** Parse a cell content block and fill in the CellInfo structure. There |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 970 | ** are two versions of this function. btreeParseCell() takes a |
| 971 | ** cell index as the second argument and btreeParseCellPtr() |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 972 | ** takes a pointer to the body of the cell as its second argument. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 973 | */ |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 974 | static void btreeParseCellPtr( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 975 | MemPage *pPage, /* Page containing the cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 976 | u8 *pCell, /* Pointer to the cell text. */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 977 | CellInfo *pInfo /* Fill in this structure */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 978 | ){ |
drh | 3e28ff5 | 2014-09-24 00:59:08 +0000 | [diff] [blame] | 979 | u8 *pIter; /* For scanning through pCell */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 980 | u32 nPayload; /* Number of bytes of cell payload */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 981 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 982 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 983 | assert( pPage->leaf==0 || pPage->leaf==1 ); |
drh | 3e28ff5 | 2014-09-24 00:59:08 +0000 | [diff] [blame] | 984 | if( pPage->intKeyLeaf ){ |
| 985 | assert( pPage->childPtrSize==0 ); |
| 986 | pIter = pCell + getVarint32(pCell, nPayload); |
drh | ab1cc58 | 2014-09-23 21:25:19 +0000 | [diff] [blame] | 987 | pIter += getVarint(pIter, (u64*)&pInfo->nKey); |
drh | 3e28ff5 | 2014-09-24 00:59:08 +0000 | [diff] [blame] | 988 | }else if( pPage->noPayload ){ |
| 989 | assert( pPage->childPtrSize==4 ); |
| 990 | pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey); |
| 991 | pInfo->nPayload = 0; |
| 992 | pInfo->nLocal = 0; |
| 993 | pInfo->iOverflow = 0; |
| 994 | pInfo->pPayload = 0; |
| 995 | return; |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 996 | }else{ |
drh | 3e28ff5 | 2014-09-24 00:59:08 +0000 | [diff] [blame] | 997 | pIter = pCell + pPage->childPtrSize; |
drh | ab1cc58 | 2014-09-23 21:25:19 +0000 | [diff] [blame] | 998 | pIter += getVarint32(pIter, nPayload); |
drh | 79df1f4 | 2008-07-18 00:57:33 +0000 | [diff] [blame] | 999 | pInfo->nKey = nPayload; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 1000 | } |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 1001 | pInfo->nPayload = nPayload; |
drh | ab1cc58 | 2014-09-23 21:25:19 +0000 | [diff] [blame] | 1002 | pInfo->pPayload = pIter; |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1003 | testcase( nPayload==pPage->maxLocal ); |
| 1004 | testcase( nPayload==pPage->maxLocal+1 ); |
drh | ab1cc58 | 2014-09-23 21:25:19 +0000 | [diff] [blame] | 1005 | if( nPayload<=pPage->maxLocal ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1006 | /* This is the (easy) common case where the entire payload fits |
| 1007 | ** on the local page. No overflow is required. |
| 1008 | */ |
drh | ab1cc58 | 2014-09-23 21:25:19 +0000 | [diff] [blame] | 1009 | pInfo->nSize = nPayload + (u16)(pIter - pCell); |
| 1010 | if( pInfo->nSize<4 ) pInfo->nSize = 4; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1011 | pInfo->nLocal = (u16)nPayload; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 1012 | pInfo->iOverflow = 0; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 1013 | }else{ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1014 | /* If the payload will not fit completely on the local page, we have |
| 1015 | ** to decide how much to store locally and how much to spill onto |
| 1016 | ** overflow pages. The strategy is to minimize the amount of unused |
| 1017 | ** space on overflow pages while keeping the amount of local storage |
| 1018 | ** in between minLocal and maxLocal. |
| 1019 | ** |
| 1020 | ** Warning: changing the way overflow payload is distributed in any |
| 1021 | ** way will result in an incompatible file format. |
| 1022 | */ |
| 1023 | int minLocal; /* Minimum amount of payload held locally */ |
| 1024 | int maxLocal; /* Maximum amount of payload held locally */ |
| 1025 | int surplus; /* Overflow payload available for local storage */ |
| 1026 | |
| 1027 | minLocal = pPage->minLocal; |
| 1028 | maxLocal = pPage->maxLocal; |
| 1029 | surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4); |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1030 | testcase( surplus==maxLocal ); |
| 1031 | testcase( surplus==maxLocal+1 ); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 1032 | if( surplus <= maxLocal ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1033 | pInfo->nLocal = (u16)surplus; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 1034 | }else{ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1035 | pInfo->nLocal = (u16)minLocal; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 1036 | } |
drh | ab1cc58 | 2014-09-23 21:25:19 +0000 | [diff] [blame] | 1037 | pInfo->iOverflow = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 1038 | pInfo->nSize = pInfo->iOverflow + 4; |
| 1039 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1040 | } |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 1041 | static void btreeParseCell( |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1042 | MemPage *pPage, /* Page containing the cell */ |
| 1043 | int iCell, /* The cell index. First cell is 0 */ |
| 1044 | CellInfo *pInfo /* Fill in this structure */ |
| 1045 | ){ |
drh | c468383 | 2014-09-23 23:12:53 +0000 | [diff] [blame] | 1046 | btreeParseCellPtr(pPage, findCell(pPage, iCell), pInfo); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1047 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1048 | |
| 1049 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1050 | ** Compute the total number of bytes that a Cell needs in the cell |
| 1051 | ** data area of the btree-page. The return number includes the cell |
| 1052 | ** data header and the local payload, but not any overflow page or |
| 1053 | ** the space used by the cell pointer. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1054 | */ |
danielk1977 | ae5558b | 2009-04-29 11:31:47 +0000 | [diff] [blame] | 1055 | static u16 cellSizePtr(MemPage *pPage, u8 *pCell){ |
drh | 3f38740 | 2014-09-24 01:23:00 +0000 | [diff] [blame] | 1056 | u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */ |
| 1057 | u8 *pEnd; /* End mark for a varint */ |
| 1058 | u32 nSize; /* Size value to return */ |
danielk1977 | ae5558b | 2009-04-29 11:31:47 +0000 | [diff] [blame] | 1059 | |
| 1060 | #ifdef SQLITE_DEBUG |
| 1061 | /* The value returned by this function should always be the same as |
| 1062 | ** the (CellInfo.nSize) value found by doing a full parse of the |
| 1063 | ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of |
| 1064 | ** this function verifies that this invariant is not violated. */ |
| 1065 | CellInfo debuginfo; |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 1066 | btreeParseCellPtr(pPage, pCell, &debuginfo); |
danielk1977 | ae5558b | 2009-04-29 11:31:47 +0000 | [diff] [blame] | 1067 | #endif |
| 1068 | |
drh | 3e28ff5 | 2014-09-24 00:59:08 +0000 | [diff] [blame] | 1069 | if( pPage->noPayload ){ |
| 1070 | pEnd = &pIter[9]; |
| 1071 | while( (*pIter++)&0x80 && pIter<pEnd ); |
| 1072 | assert( pPage->childPtrSize==4 ); |
| 1073 | return (u16)(pIter - pCell); |
drh | dc41d60 | 2014-09-22 19:51:35 +0000 | [diff] [blame] | 1074 | } |
drh | 3e28ff5 | 2014-09-24 00:59:08 +0000 | [diff] [blame] | 1075 | nSize = *pIter; |
| 1076 | if( nSize>=0x80 ){ |
| 1077 | pEnd = &pIter[9]; |
| 1078 | nSize &= 0x7f; |
| 1079 | do{ |
| 1080 | nSize = (nSize<<7) | (*++pIter & 0x7f); |
| 1081 | }while( *(pIter)>=0x80 && pIter<pEnd ); |
| 1082 | } |
| 1083 | pIter++; |
drh | dc41d60 | 2014-09-22 19:51:35 +0000 | [diff] [blame] | 1084 | if( pPage->intKey ){ |
danielk1977 | ae5558b | 2009-04-29 11:31:47 +0000 | [diff] [blame] | 1085 | /* pIter now points at the 64-bit integer key value, a variable length |
| 1086 | ** integer. The following block moves pIter to point at the first byte |
| 1087 | ** past the end of the key value. */ |
| 1088 | pEnd = &pIter[9]; |
| 1089 | while( (*pIter++)&0x80 && pIter<pEnd ); |
danielk1977 | ae5558b | 2009-04-29 11:31:47 +0000 | [diff] [blame] | 1090 | } |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1091 | testcase( nSize==pPage->maxLocal ); |
| 1092 | testcase( nSize==pPage->maxLocal+1 ); |
drh | 3e28ff5 | 2014-09-24 00:59:08 +0000 | [diff] [blame] | 1093 | if( nSize<=pPage->maxLocal ){ |
| 1094 | nSize += (u32)(pIter - pCell); |
| 1095 | if( nSize<4 ) nSize = 4; |
| 1096 | }else{ |
danielk1977 | ae5558b | 2009-04-29 11:31:47 +0000 | [diff] [blame] | 1097 | int minLocal = pPage->minLocal; |
| 1098 | nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4); |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1099 | testcase( nSize==pPage->maxLocal ); |
| 1100 | testcase( nSize==pPage->maxLocal+1 ); |
danielk1977 | ae5558b | 2009-04-29 11:31:47 +0000 | [diff] [blame] | 1101 | if( nSize>pPage->maxLocal ){ |
| 1102 | nSize = minLocal; |
| 1103 | } |
drh | 3e28ff5 | 2014-09-24 00:59:08 +0000 | [diff] [blame] | 1104 | nSize += 4 + (u16)(pIter - pCell); |
danielk1977 | ae5558b | 2009-04-29 11:31:47 +0000 | [diff] [blame] | 1105 | } |
drh | dc41d60 | 2014-09-22 19:51:35 +0000 | [diff] [blame] | 1106 | assert( nSize==debuginfo.nSize || CORRUPT_DB ); |
shane | 60a4b53 | 2009-05-06 18:57:09 +0000 | [diff] [blame] | 1107 | return (u16)nSize; |
danielk1977 | ae5558b | 2009-04-29 11:31:47 +0000 | [diff] [blame] | 1108 | } |
drh | 0ee3dbe | 2009-10-16 15:05:18 +0000 | [diff] [blame] | 1109 | |
| 1110 | #ifdef SQLITE_DEBUG |
| 1111 | /* This variation on cellSizePtr() is used inside of assert() statements |
| 1112 | ** only. */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 1113 | static u16 cellSize(MemPage *pPage, int iCell){ |
danielk1977 | ae5558b | 2009-04-29 11:31:47 +0000 | [diff] [blame] | 1114 | return cellSizePtr(pPage, findCell(pPage, iCell)); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1115 | } |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1116 | #endif |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1117 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 1118 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1119 | /* |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 1120 | ** If the cell pCell, part of page pPage contains a pointer |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 1121 | ** to an overflow page, insert an entry into the pointer-map |
| 1122 | ** for the overflow page. |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 1123 | */ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 1124 | static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){ |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 1125 | CellInfo info; |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 1126 | if( *pRC ) return; |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 1127 | assert( pCell!=0 ); |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 1128 | btreeParseCellPtr(pPage, pCell, &info); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 1129 | if( info.iOverflow ){ |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 1130 | Pgno ovfl = get4byte(&pCell[info.iOverflow]); |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 1131 | ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 1132 | } |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 1133 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 1134 | #endif |
| 1135 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 1136 | |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1137 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1138 | ** Defragment the page given. All Cells are moved to the |
drh | 3a4a2d4 | 2005-11-24 14:24:28 +0000 | [diff] [blame] | 1139 | ** end of the page and all free space is collected into one |
| 1140 | ** big FreeBlk that occurs in between the header and cell |
drh | 31beae9 | 2005-11-24 14:34:36 +0000 | [diff] [blame] | 1141 | ** pointer array and the cell content area. |
drh | fdab026 | 2014-11-20 15:30:50 +0000 | [diff] [blame] | 1142 | ** |
| 1143 | ** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a |
| 1144 | ** b-tree page so that there are no freeblocks or fragment bytes, all |
| 1145 | ** unused bytes are contained in the unallocated space region, and all |
| 1146 | ** cells are packed tightly at the end of the page. |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 1147 | */ |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 1148 | static int defragmentPage(MemPage *pPage){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1149 | int i; /* Loop counter */ |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 1150 | int pc; /* Address of the i-th cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1151 | int hdr; /* Offset to the page header */ |
| 1152 | int size; /* Size of a cell */ |
| 1153 | int usableSize; /* Number of usable bytes on a page */ |
| 1154 | int cellOffset; /* Offset to the cell pointer array */ |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 1155 | int cbrk; /* Offset to the cell content area */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1156 | int nCell; /* Number of cells on the page */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1157 | unsigned char *data; /* The page data */ |
| 1158 | unsigned char *temp; /* Temp area for cell content */ |
drh | 588400b | 2014-09-27 05:00:25 +0000 | [diff] [blame] | 1159 | unsigned char *src; /* Source of content */ |
drh | 1714662 | 2009-07-07 17:38:38 +0000 | [diff] [blame] | 1160 | int iCellFirst; /* First allowable cell index */ |
| 1161 | int iCellLast; /* Last possible cell index */ |
| 1162 | |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1163 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1164 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1165 | assert( pPage->pBt!=0 ); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1166 | assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1167 | assert( pPage->nOverflow==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1168 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 588400b | 2014-09-27 05:00:25 +0000 | [diff] [blame] | 1169 | temp = 0; |
| 1170 | src = data = pPage->aData; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1171 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1172 | cellOffset = pPage->cellOffset; |
| 1173 | nCell = pPage->nCell; |
| 1174 | assert( nCell==get2byte(&data[hdr+3]) ); |
| 1175 | usableSize = pPage->pBt->usableSize; |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 1176 | cbrk = usableSize; |
drh | 1714662 | 2009-07-07 17:38:38 +0000 | [diff] [blame] | 1177 | iCellFirst = cellOffset + 2*nCell; |
| 1178 | iCellLast = usableSize - 4; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1179 | for(i=0; i<nCell; i++){ |
| 1180 | u8 *pAddr; /* The i-th cell pointer */ |
| 1181 | pAddr = &data[cellOffset + i*2]; |
| 1182 | pc = get2byte(pAddr); |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1183 | testcase( pc==iCellFirst ); |
| 1184 | testcase( pc==iCellLast ); |
drh | 1714662 | 2009-07-07 17:38:38 +0000 | [diff] [blame] | 1185 | #if !defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK) |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 1186 | /* These conditions have already been verified in btreeInitPage() |
drh | 1714662 | 2009-07-07 17:38:38 +0000 | [diff] [blame] | 1187 | ** if SQLITE_ENABLE_OVERSIZE_CELL_CHECK is defined |
| 1188 | */ |
| 1189 | if( pc<iCellFirst || pc>iCellLast ){ |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 1190 | return SQLITE_CORRUPT_BKPT; |
| 1191 | } |
drh | 1714662 | 2009-07-07 17:38:38 +0000 | [diff] [blame] | 1192 | #endif |
| 1193 | assert( pc>=iCellFirst && pc<=iCellLast ); |
drh | 588400b | 2014-09-27 05:00:25 +0000 | [diff] [blame] | 1194 | size = cellSizePtr(pPage, &src[pc]); |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 1195 | cbrk -= size; |
drh | 1714662 | 2009-07-07 17:38:38 +0000 | [diff] [blame] | 1196 | #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK) |
| 1197 | if( cbrk<iCellFirst ){ |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 1198 | return SQLITE_CORRUPT_BKPT; |
| 1199 | } |
drh | 1714662 | 2009-07-07 17:38:38 +0000 | [diff] [blame] | 1200 | #else |
| 1201 | if( cbrk<iCellFirst || pc+size>usableSize ){ |
| 1202 | return SQLITE_CORRUPT_BKPT; |
| 1203 | } |
| 1204 | #endif |
drh | 7157e1d | 2009-07-09 13:25:32 +0000 | [diff] [blame] | 1205 | assert( cbrk+size<=usableSize && cbrk>=iCellFirst ); |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1206 | testcase( cbrk+size==usableSize ); |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1207 | testcase( pc+size==usableSize ); |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 1208 | put2byte(pAddr, cbrk); |
drh | 588400b | 2014-09-27 05:00:25 +0000 | [diff] [blame] | 1209 | if( temp==0 ){ |
| 1210 | int x; |
| 1211 | if( cbrk==pc ) continue; |
| 1212 | temp = sqlite3PagerTempSpace(pPage->pBt->pPager); |
| 1213 | x = get2byte(&data[hdr+5]); |
| 1214 | memcpy(&temp[x], &data[x], (cbrk+size) - x); |
| 1215 | src = temp; |
| 1216 | } |
| 1217 | memcpy(&data[cbrk], &src[pc], size); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1218 | } |
drh | 1714662 | 2009-07-07 17:38:38 +0000 | [diff] [blame] | 1219 | assert( cbrk>=iCellFirst ); |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 1220 | put2byte(&data[hdr+5], cbrk); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1221 | data[hdr+1] = 0; |
| 1222 | data[hdr+2] = 0; |
| 1223 | data[hdr+7] = 0; |
drh | 1714662 | 2009-07-07 17:38:38 +0000 | [diff] [blame] | 1224 | memset(&data[iCellFirst], 0, cbrk-iCellFirst); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 1225 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 1714662 | 2009-07-07 17:38:38 +0000 | [diff] [blame] | 1226 | if( cbrk-iCellFirst!=pPage->nFree ){ |
danielk1977 | 360e634 | 2008-11-12 08:49:51 +0000 | [diff] [blame] | 1227 | return SQLITE_CORRUPT_BKPT; |
| 1228 | } |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 1229 | return SQLITE_OK; |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1232 | /* |
dan | 8e9ba0c | 2014-10-14 17:27:04 +0000 | [diff] [blame] | 1233 | ** Search the free-list on page pPg for space to store a cell nByte bytes in |
| 1234 | ** size. If one can be found, return a pointer to the space and remove it |
| 1235 | ** from the free-list. |
| 1236 | ** |
| 1237 | ** If no suitable space can be found on the free-list, return NULL. |
| 1238 | ** |
drh | ba0f999 | 2014-10-30 20:48:44 +0000 | [diff] [blame] | 1239 | ** This function may detect corruption within pPg. If corruption is |
| 1240 | ** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned. |
dan | 61e94c9 | 2014-10-27 08:02:16 +0000 | [diff] [blame] | 1241 | ** |
| 1242 | ** If a slot of at least nByte bytes is found but cannot be used because |
| 1243 | ** there are already at least 60 fragmented bytes on the page, return NULL. |
| 1244 | ** In this case, if pbDefrag parameter is not NULL, set *pbDefrag to true. |
dan | 8e9ba0c | 2014-10-14 17:27:04 +0000 | [diff] [blame] | 1245 | */ |
dan | 61e94c9 | 2014-10-27 08:02:16 +0000 | [diff] [blame] | 1246 | static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc, int *pbDefrag){ |
dan | 8e9ba0c | 2014-10-14 17:27:04 +0000 | [diff] [blame] | 1247 | const int hdr = pPg->hdrOffset; |
| 1248 | u8 * const aData = pPg->aData; |
| 1249 | int iAddr; |
| 1250 | int pc; |
| 1251 | int usableSize = pPg->pBt->usableSize; |
| 1252 | |
| 1253 | for(iAddr=hdr+1; (pc = get2byte(&aData[iAddr]))>0; iAddr=pc){ |
| 1254 | int size; /* Size of the free slot */ |
drh | 113762a | 2014-11-19 16:36:25 +0000 | [diff] [blame] | 1255 | /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of |
| 1256 | ** increasing offset. */ |
dan | 8e9ba0c | 2014-10-14 17:27:04 +0000 | [diff] [blame] | 1257 | if( pc>usableSize-4 || pc<iAddr+4 ){ |
drh | ba0f999 | 2014-10-30 20:48:44 +0000 | [diff] [blame] | 1258 | *pRc = SQLITE_CORRUPT_BKPT; |
dan | 8e9ba0c | 2014-10-14 17:27:04 +0000 | [diff] [blame] | 1259 | return 0; |
| 1260 | } |
drh | 113762a | 2014-11-19 16:36:25 +0000 | [diff] [blame] | 1261 | /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each |
| 1262 | ** freeblock form a big-endian integer which is the size of the freeblock |
| 1263 | ** in bytes, including the 4-byte header. */ |
dan | 8e9ba0c | 2014-10-14 17:27:04 +0000 | [diff] [blame] | 1264 | size = get2byte(&aData[pc+2]); |
| 1265 | if( size>=nByte ){ |
| 1266 | int x = size - nByte; |
| 1267 | testcase( x==4 ); |
| 1268 | testcase( x==3 ); |
| 1269 | if( x<4 ){ |
drh | fdab026 | 2014-11-20 15:30:50 +0000 | [diff] [blame] | 1270 | /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total |
| 1271 | ** number of bytes in fragments may not exceed 60. */ |
dan | 61e94c9 | 2014-10-27 08:02:16 +0000 | [diff] [blame] | 1272 | if( aData[hdr+7]>=60 ){ |
| 1273 | if( pbDefrag ) *pbDefrag = 1; |
| 1274 | return 0; |
| 1275 | } |
dan | 8e9ba0c | 2014-10-14 17:27:04 +0000 | [diff] [blame] | 1276 | /* Remove the slot from the free-list. Update the number of |
| 1277 | ** fragmented bytes within the page. */ |
| 1278 | memcpy(&aData[iAddr], &aData[pc], 2); |
| 1279 | aData[hdr+7] += (u8)x; |
| 1280 | }else if( size+pc > usableSize ){ |
drh | ba0f999 | 2014-10-30 20:48:44 +0000 | [diff] [blame] | 1281 | *pRc = SQLITE_CORRUPT_BKPT; |
dan | 8e9ba0c | 2014-10-14 17:27:04 +0000 | [diff] [blame] | 1282 | return 0; |
| 1283 | }else{ |
| 1284 | /* The slot remains on the free-list. Reduce its size to account |
| 1285 | ** for the portion used by the new allocation. */ |
| 1286 | put2byte(&aData[pc+2], x); |
| 1287 | } |
| 1288 | return &aData[pc + x]; |
| 1289 | } |
| 1290 | } |
| 1291 | |
| 1292 | return 0; |
| 1293 | } |
| 1294 | |
| 1295 | /* |
danielk1977 | 6011a75 | 2009-04-01 16:25:32 +0000 | [diff] [blame] | 1296 | ** Allocate nByte bytes of space from within the B-Tree page passed |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1297 | ** as the first argument. Write into *pIdx the index into pPage->aData[] |
| 1298 | ** of the first byte of allocated space. Return either SQLITE_OK or |
| 1299 | ** an error code (usually SQLITE_CORRUPT). |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1300 | ** |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1301 | ** The caller guarantees that there is sufficient space to make the |
| 1302 | ** allocation. This routine might need to defragment in order to bring |
| 1303 | ** all the space together, however. This routine will avoid using |
| 1304 | ** the first two bytes past the cell pointer area since presumably this |
| 1305 | ** allocation is being made in order to insert a new cell, so we will |
| 1306 | ** also end up needing a new cell pointer. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1307 | */ |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1308 | static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){ |
danielk1977 | 6011a75 | 2009-04-01 16:25:32 +0000 | [diff] [blame] | 1309 | const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */ |
| 1310 | u8 * const data = pPage->aData; /* Local cache of pPage->aData */ |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1311 | int top; /* First byte of cell content area */ |
drh | fefa094 | 2014-11-05 21:21:08 +0000 | [diff] [blame] | 1312 | int rc = SQLITE_OK; /* Integer return code */ |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1313 | int gap; /* First byte of gap between cell pointers and cell content */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1314 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1315 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1316 | assert( pPage->pBt ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1317 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 1318 | assert( nByte>=0 ); /* Minimum cell size is 4 */ |
| 1319 | assert( pPage->nFree>=nByte ); |
| 1320 | assert( pPage->nOverflow==0 ); |
mistachkin | a95d8ca | 2014-10-27 19:42:02 +0000 | [diff] [blame] | 1321 | assert( nByte < (int)(pPage->pBt->usableSize-8) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1322 | |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1323 | assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf ); |
| 1324 | gap = pPage->cellOffset + 2*pPage->nCell; |
drh | 75b31dc | 2014-08-20 00:54:46 +0000 | [diff] [blame] | 1325 | assert( gap<=65536 ); |
drh | fdab026 | 2014-11-20 15:30:50 +0000 | [diff] [blame] | 1326 | /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size |
| 1327 | ** and the reserved space is zero (the usual value for reserved space) |
| 1328 | ** then the cell content offset of an empty page wants to be 65536. |
| 1329 | ** However, that integer is too large to be stored in a 2-byte unsigned |
| 1330 | ** integer, so a value of 0 is used in its place. */ |
| 1331 | top = get2byteNotZero(&data[hdr+5]); |
| 1332 | if( gap>top ) return SQLITE_CORRUPT_BKPT; |
drh | 4c04f3c | 2014-08-20 11:56:14 +0000 | [diff] [blame] | 1333 | |
| 1334 | /* If there is enough space between gap and top for one more cell pointer |
| 1335 | ** array entry offset, and if the freelist is not empty, then search the |
| 1336 | ** freelist looking for a free slot big enough to satisfy the request. |
| 1337 | */ |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1338 | testcase( gap+2==top ); |
| 1339 | testcase( gap+1==top ); |
| 1340 | testcase( gap==top ); |
drh | 4c04f3c | 2014-08-20 11:56:14 +0000 | [diff] [blame] | 1341 | if( gap+2<=top && (data[hdr+1] || data[hdr+2]) ){ |
dan | 61e94c9 | 2014-10-27 08:02:16 +0000 | [diff] [blame] | 1342 | int bDefrag = 0; |
| 1343 | u8 *pSpace = pageFindSlot(pPage, nByte, &rc, &bDefrag); |
dan | 8e9ba0c | 2014-10-14 17:27:04 +0000 | [diff] [blame] | 1344 | if( rc ) return rc; |
dan | 61e94c9 | 2014-10-27 08:02:16 +0000 | [diff] [blame] | 1345 | if( bDefrag ) goto defragment_page; |
dan | 8e9ba0c | 2014-10-14 17:27:04 +0000 | [diff] [blame] | 1346 | if( pSpace ){ |
drh | fefa094 | 2014-11-05 21:21:08 +0000 | [diff] [blame] | 1347 | assert( pSpace>=data && (pSpace - data)<65536 ); |
| 1348 | *pIdx = (int)(pSpace - data); |
dan | 8e9ba0c | 2014-10-14 17:27:04 +0000 | [diff] [blame] | 1349 | return SQLITE_OK; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1350 | } |
| 1351 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1352 | |
drh | 4c04f3c | 2014-08-20 11:56:14 +0000 | [diff] [blame] | 1353 | /* The request could not be fulfilled using a freelist slot. Check |
| 1354 | ** to see if defragmentation is necessary. |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1355 | */ |
| 1356 | testcase( gap+2+nByte==top ); |
| 1357 | if( gap+2+nByte>top ){ |
dan | 61e94c9 | 2014-10-27 08:02:16 +0000 | [diff] [blame] | 1358 | defragment_page: |
drh | 1fd2d7d | 2014-12-02 16:16:47 +0000 | [diff] [blame] | 1359 | assert( pPage->nCell>0 || CORRUPT_DB ); |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1360 | rc = defragmentPage(pPage); |
| 1361 | if( rc ) return rc; |
drh | 5d433ce | 2010-08-14 16:02:52 +0000 | [diff] [blame] | 1362 | top = get2byteNotZero(&data[hdr+5]); |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1363 | assert( gap+nByte<=top ); |
| 1364 | } |
| 1365 | |
| 1366 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1367 | /* Allocate memory from the gap in between the cell pointer array |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 1368 | ** and the cell content area. The btreeInitPage() call has already |
| 1369 | ** validated the freelist. Given that the freelist is valid, there |
| 1370 | ** is no way that the allocation can extend off the end of the page. |
| 1371 | ** The assert() below verifies the previous sentence. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1372 | */ |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1373 | top -= nByte; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1374 | put2byte(&data[hdr+5], top); |
drh | fcd71b6 | 2011-04-05 22:08:24 +0000 | [diff] [blame] | 1375 | assert( top+nByte <= (int)pPage->pBt->usableSize ); |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1376 | *pIdx = top; |
| 1377 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1381 | ** Return a section of the pPage->aData to the freelist. |
drh | 7fb9164 | 2014-08-20 14:37:09 +0000 | [diff] [blame] | 1382 | ** The first byte of the new free block is pPage->aData[iStart] |
| 1383 | ** and the size of the block is iSize bytes. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1384 | ** |
drh | 5f5c753 | 2014-08-20 17:56:27 +0000 | [diff] [blame] | 1385 | ** Adjacent freeblocks are coalesced. |
| 1386 | ** |
| 1387 | ** Note that even though the freeblock list was checked by btreeInitPage(), |
| 1388 | ** that routine will not detect overlap between cells or freeblocks. Nor |
| 1389 | ** does it detect cells or freeblocks that encrouch into the reserved bytes |
| 1390 | ** at the end of the page. So do additional corruption checks inside this |
| 1391 | ** routine and return SQLITE_CORRUPT if any problems are found. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1392 | */ |
drh | 5f5c753 | 2014-08-20 17:56:27 +0000 | [diff] [blame] | 1393 | static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){ |
drh | 3f38740 | 2014-09-24 01:23:00 +0000 | [diff] [blame] | 1394 | u16 iPtr; /* Address of ptr to next freeblock */ |
drh | 5f5c753 | 2014-08-20 17:56:27 +0000 | [diff] [blame] | 1395 | u16 iFreeBlk; /* Address of the next freeblock */ |
| 1396 | u8 hdr; /* Page header size. 0 or 100 */ |
| 1397 | u8 nFrag = 0; /* Reduction in fragmentation */ |
| 1398 | u16 iOrigSize = iSize; /* Original value of iSize */ |
| 1399 | u32 iLast = pPage->pBt->usableSize-4; /* Largest possible freeblock offset */ |
| 1400 | u32 iEnd = iStart + iSize; /* First byte past the iStart buffer */ |
drh | 7fb9164 | 2014-08-20 14:37:09 +0000 | [diff] [blame] | 1401 | unsigned char *data = pPage->aData; /* Page content */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1402 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1403 | assert( pPage->pBt!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1404 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 7fb9164 | 2014-08-20 14:37:09 +0000 | [diff] [blame] | 1405 | assert( iStart>=pPage->hdrOffset+6+pPage->childPtrSize ); |
dan | 23eba45 | 2014-10-24 18:43:57 +0000 | [diff] [blame] | 1406 | assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1407 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 7fb9164 | 2014-08-20 14:37:09 +0000 | [diff] [blame] | 1408 | assert( iSize>=4 ); /* Minimum cell size is 4 */ |
drh | 5f5c753 | 2014-08-20 17:56:27 +0000 | [diff] [blame] | 1409 | assert( iStart<=iLast ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1410 | |
drh | 5f5c753 | 2014-08-20 17:56:27 +0000 | [diff] [blame] | 1411 | /* Overwrite deleted information with zeros when the secure_delete |
| 1412 | ** option is enabled */ |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 1413 | if( pPage->pBt->btsFlags & BTS_SECURE_DELETE ){ |
drh | 7fb9164 | 2014-08-20 14:37:09 +0000 | [diff] [blame] | 1414 | memset(&data[iStart], 0, iSize); |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 1415 | } |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 1416 | |
drh | 5f5c753 | 2014-08-20 17:56:27 +0000 | [diff] [blame] | 1417 | /* The list of freeblocks must be in ascending order. Find the |
| 1418 | ** spot on the list where iStart should be inserted. |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1419 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1420 | hdr = pPage->hdrOffset; |
drh | 7fb9164 | 2014-08-20 14:37:09 +0000 | [diff] [blame] | 1421 | iPtr = hdr + 1; |
drh | 7bc4c45 | 2014-08-20 18:43:44 +0000 | [diff] [blame] | 1422 | if( data[iPtr+1]==0 && data[iPtr]==0 ){ |
| 1423 | iFreeBlk = 0; /* Shortcut for the case when the freelist is empty */ |
| 1424 | }else{ |
| 1425 | while( (iFreeBlk = get2byte(&data[iPtr]))>0 && iFreeBlk<iStart ){ |
| 1426 | if( iFreeBlk<iPtr+4 ) return SQLITE_CORRUPT_BKPT; |
| 1427 | iPtr = iFreeBlk; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1428 | } |
drh | 7bc4c45 | 2014-08-20 18:43:44 +0000 | [diff] [blame] | 1429 | if( iFreeBlk>iLast ) return SQLITE_CORRUPT_BKPT; |
| 1430 | assert( iFreeBlk>iPtr || iFreeBlk==0 ); |
| 1431 | |
| 1432 | /* At this point: |
| 1433 | ** iFreeBlk: First freeblock after iStart, or zero if none |
| 1434 | ** iPtr: The address of a pointer iFreeBlk |
| 1435 | ** |
| 1436 | ** Check to see if iFreeBlk should be coalesced onto the end of iStart. |
| 1437 | */ |
| 1438 | if( iFreeBlk && iEnd+3>=iFreeBlk ){ |
| 1439 | nFrag = iFreeBlk - iEnd; |
| 1440 | if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_BKPT; |
| 1441 | iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]); |
| 1442 | iSize = iEnd - iStart; |
| 1443 | iFreeBlk = get2byte(&data[iFreeBlk]); |
| 1444 | } |
| 1445 | |
drh | 3f38740 | 2014-09-24 01:23:00 +0000 | [diff] [blame] | 1446 | /* If iPtr is another freeblock (that is, if iPtr is not the freelist |
| 1447 | ** pointer in the page header) then check to see if iStart should be |
| 1448 | ** coalesced onto the end of iPtr. |
drh | 7bc4c45 | 2014-08-20 18:43:44 +0000 | [diff] [blame] | 1449 | */ |
| 1450 | if( iPtr>hdr+1 ){ |
| 1451 | int iPtrEnd = iPtr + get2byte(&data[iPtr+2]); |
| 1452 | if( iPtrEnd+3>=iStart ){ |
| 1453 | if( iPtrEnd>iStart ) return SQLITE_CORRUPT_BKPT; |
| 1454 | nFrag += iStart - iPtrEnd; |
| 1455 | iSize = iEnd - iPtr; |
| 1456 | iStart = iPtr; |
| 1457 | } |
| 1458 | } |
| 1459 | if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_BKPT; |
| 1460 | data[hdr+7] -= nFrag; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1461 | } |
drh | 7bc4c45 | 2014-08-20 18:43:44 +0000 | [diff] [blame] | 1462 | if( iStart==get2byte(&data[hdr+5]) ){ |
drh | 5f5c753 | 2014-08-20 17:56:27 +0000 | [diff] [blame] | 1463 | /* The new freeblock is at the beginning of the cell content area, |
| 1464 | ** so just extend the cell content area rather than create another |
| 1465 | ** freelist entry */ |
drh | 7bc4c45 | 2014-08-20 18:43:44 +0000 | [diff] [blame] | 1466 | if( iPtr!=hdr+1 ) return SQLITE_CORRUPT_BKPT; |
drh | 5f5c753 | 2014-08-20 17:56:27 +0000 | [diff] [blame] | 1467 | put2byte(&data[hdr+1], iFreeBlk); |
| 1468 | put2byte(&data[hdr+5], iEnd); |
| 1469 | }else{ |
| 1470 | /* Insert the new freeblock into the freelist */ |
| 1471 | put2byte(&data[iPtr], iStart); |
| 1472 | put2byte(&data[iStart], iFreeBlk); |
| 1473 | put2byte(&data[iStart+2], iSize); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1474 | } |
drh | 5f5c753 | 2014-08-20 17:56:27 +0000 | [diff] [blame] | 1475 | pPage->nFree += iOrigSize; |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 1476 | return SQLITE_OK; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1480 | ** Decode the flags byte (the first byte of the header) for a page |
| 1481 | ** and initialize fields of the MemPage structure accordingly. |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1482 | ** |
| 1483 | ** Only the following combinations are supported. Anything different |
| 1484 | ** indicates a corrupt database files: |
| 1485 | ** |
| 1486 | ** PTF_ZERODATA |
| 1487 | ** PTF_ZERODATA | PTF_LEAF |
| 1488 | ** PTF_LEAFDATA | PTF_INTKEY |
| 1489 | ** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1490 | */ |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1491 | static int decodeFlags(MemPage *pPage, int flagByte){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1492 | BtShared *pBt; /* A copy of pPage->pBt */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1493 | |
| 1494 | assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1495 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1496 | pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 ); |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1497 | flagByte &= ~PTF_LEAF; |
| 1498 | pPage->childPtrSize = 4-4*pPage->leaf; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1499 | pBt = pPage->pBt; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1500 | if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){ |
drh | fdab026 | 2014-11-20 15:30:50 +0000 | [diff] [blame] | 1501 | /* EVIDENCE-OF: R-03640-13415 A value of 5 means the page is an interior |
| 1502 | ** table b-tree page. */ |
| 1503 | assert( (PTF_LEAFDATA|PTF_INTKEY)==5 ); |
| 1504 | /* EVIDENCE-OF: R-20501-61796 A value of 13 means the page is a leaf |
| 1505 | ** table b-tree page. */ |
| 1506 | assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 ); |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1507 | pPage->intKey = 1; |
drh | 3e28ff5 | 2014-09-24 00:59:08 +0000 | [diff] [blame] | 1508 | pPage->intKeyLeaf = pPage->leaf; |
| 1509 | pPage->noPayload = !pPage->leaf; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1510 | pPage->maxLocal = pBt->maxLeaf; |
| 1511 | pPage->minLocal = pBt->minLeaf; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1512 | }else if( flagByte==PTF_ZERODATA ){ |
drh | fdab026 | 2014-11-20 15:30:50 +0000 | [diff] [blame] | 1513 | /* EVIDENCE-OF: R-27225-53936 A value of 2 means the page is an interior |
| 1514 | ** index b-tree page. */ |
| 1515 | assert( (PTF_ZERODATA)==2 ); |
| 1516 | /* EVIDENCE-OF: R-16571-11615 A value of 10 means the page is a leaf |
| 1517 | ** index b-tree page. */ |
| 1518 | assert( (PTF_ZERODATA|PTF_LEAF)==10 ); |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1519 | pPage->intKey = 0; |
drh | 3e28ff5 | 2014-09-24 00:59:08 +0000 | [diff] [blame] | 1520 | pPage->intKeyLeaf = 0; |
| 1521 | pPage->noPayload = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1522 | pPage->maxLocal = pBt->maxLocal; |
| 1523 | pPage->minLocal = pBt->minLocal; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1524 | }else{ |
drh | fdab026 | 2014-11-20 15:30:50 +0000 | [diff] [blame] | 1525 | /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is |
| 1526 | ** an error. */ |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1527 | return SQLITE_CORRUPT_BKPT; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1528 | } |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 1529 | pPage->max1bytePayload = pBt->max1bytePayload; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1530 | return SQLITE_OK; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1534 | ** Initialize the auxiliary information for a disk block. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1535 | ** |
| 1536 | ** Return SQLITE_OK on success. If we see that the page does |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1537 | ** not contain a well-formed database page, then return |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1538 | ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not |
| 1539 | ** guarantee that the page is well-formed. It only shows that |
| 1540 | ** we failed to detect any corruption. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1541 | */ |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 1542 | static int btreeInitPage(MemPage *pPage){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1543 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1544 | assert( pPage->pBt!=0 ); |
| 1545 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1546 | assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1547 | assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) ); |
| 1548 | assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1549 | |
| 1550 | if( !pPage->isInit ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1551 | u16 pc; /* Address of a freeblock within pPage->aData[] */ |
| 1552 | u8 hdr; /* Offset to beginning of page header */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1553 | u8 *data; /* Equal to pPage->aData */ |
| 1554 | BtShared *pBt; /* The main btree structure */ |
drh | b2eced5 | 2010-08-12 02:41:12 +0000 | [diff] [blame] | 1555 | int usableSize; /* Amount of usable space on each page */ |
shaneh | 1df2db7 | 2010-08-18 02:28:48 +0000 | [diff] [blame] | 1556 | u16 cellOffset; /* Offset from start of page to first cell pointer */ |
drh | b2eced5 | 2010-08-12 02:41:12 +0000 | [diff] [blame] | 1557 | int nFree; /* Number of unused bytes on the page */ |
| 1558 | int top; /* First byte of the cell content area */ |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1559 | int iCellFirst; /* First allowable cell or freeblock offset */ |
| 1560 | int iCellLast; /* Last possible cell or freeblock offset */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1561 | |
| 1562 | pBt = pPage->pBt; |
| 1563 | |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1564 | hdr = pPage->hdrOffset; |
| 1565 | data = pPage->aData; |
drh | fdab026 | 2014-11-20 15:30:50 +0000 | [diff] [blame] | 1566 | /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating |
| 1567 | ** the b-tree page type. */ |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1568 | if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT; |
drh | b2eced5 | 2010-08-12 02:41:12 +0000 | [diff] [blame] | 1569 | assert( pBt->pageSize>=512 && pBt->pageSize<=65536 ); |
| 1570 | pPage->maskPage = (u16)(pBt->pageSize - 1); |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1571 | pPage->nOverflow = 0; |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1572 | usableSize = pBt->usableSize; |
drh | fdab026 | 2014-11-20 15:30:50 +0000 | [diff] [blame] | 1573 | pPage->cellOffset = cellOffset = hdr + 8 + pPage->childPtrSize; |
drh | 3def235 | 2011-11-11 00:27:15 +0000 | [diff] [blame] | 1574 | pPage->aDataEnd = &data[usableSize]; |
| 1575 | pPage->aCellIdx = &data[cellOffset]; |
drh | fdab026 | 2014-11-20 15:30:50 +0000 | [diff] [blame] | 1576 | /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates |
| 1577 | ** the start of the cell content area. A zero value for this integer is |
| 1578 | ** interpreted as 65536. */ |
drh | 5d433ce | 2010-08-14 16:02:52 +0000 | [diff] [blame] | 1579 | top = get2byteNotZero(&data[hdr+5]); |
drh | fdab026 | 2014-11-20 15:30:50 +0000 | [diff] [blame] | 1580 | /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the |
| 1581 | ** number of cells on the page. */ |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1582 | pPage->nCell = get2byte(&data[hdr+3]); |
| 1583 | if( pPage->nCell>MX_CELL(pBt) ){ |
| 1584 | /* To many cells for a single page. The page must be corrupt */ |
| 1585 | return SQLITE_CORRUPT_BKPT; |
| 1586 | } |
drh | b908d76 | 2009-07-08 16:54:40 +0000 | [diff] [blame] | 1587 | testcase( pPage->nCell==MX_CELL(pBt) ); |
drh | fdab026 | 2014-11-20 15:30:50 +0000 | [diff] [blame] | 1588 | /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only |
| 1589 | ** possible for a root page of a table that contains no rows) then the |
| 1590 | ** offset to the cell content area will equal the page size minus the |
| 1591 | ** bytes of reserved space. */ |
| 1592 | assert( pPage->nCell>0 || top==usableSize || CORRUPT_DB ); |
drh | 69e931e | 2009-06-03 21:04:35 +0000 | [diff] [blame] | 1593 | |
shane | 5eff7cf | 2009-08-10 03:57:58 +0000 | [diff] [blame] | 1594 | /* A malformed database page might cause us to read past the end |
drh | 69e931e | 2009-06-03 21:04:35 +0000 | [diff] [blame] | 1595 | ** of page when parsing a cell. |
| 1596 | ** |
| 1597 | ** The following block of code checks early to see if a cell extends |
| 1598 | ** past the end of a page boundary and causes SQLITE_CORRUPT to be |
| 1599 | ** returned if it does. |
| 1600 | */ |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1601 | iCellFirst = cellOffset + 2*pPage->nCell; |
| 1602 | iCellLast = usableSize - 4; |
drh | 3b2a3fa | 2009-06-09 13:42:24 +0000 | [diff] [blame] | 1603 | #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK) |
drh | 69e931e | 2009-06-03 21:04:35 +0000 | [diff] [blame] | 1604 | { |
drh | 69e931e | 2009-06-03 21:04:35 +0000 | [diff] [blame] | 1605 | int i; /* Index into the cell pointer array */ |
| 1606 | int sz; /* Size of a cell */ |
| 1607 | |
drh | 69e931e | 2009-06-03 21:04:35 +0000 | [diff] [blame] | 1608 | if( !pPage->leaf ) iCellLast--; |
| 1609 | for(i=0; i<pPage->nCell; i++){ |
| 1610 | pc = get2byte(&data[cellOffset+i*2]); |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1611 | testcase( pc==iCellFirst ); |
| 1612 | testcase( pc==iCellLast ); |
drh | 69e931e | 2009-06-03 21:04:35 +0000 | [diff] [blame] | 1613 | if( pc<iCellFirst || pc>iCellLast ){ |
| 1614 | return SQLITE_CORRUPT_BKPT; |
| 1615 | } |
| 1616 | sz = cellSizePtr(pPage, &data[pc]); |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1617 | testcase( pc+sz==usableSize ); |
drh | 69e931e | 2009-06-03 21:04:35 +0000 | [diff] [blame] | 1618 | if( pc+sz>usableSize ){ |
| 1619 | return SQLITE_CORRUPT_BKPT; |
| 1620 | } |
| 1621 | } |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1622 | if( !pPage->leaf ) iCellLast++; |
drh | 69e931e | 2009-06-03 21:04:35 +0000 | [diff] [blame] | 1623 | } |
| 1624 | #endif |
| 1625 | |
drh | fdab026 | 2014-11-20 15:30:50 +0000 | [diff] [blame] | 1626 | /* Compute the total free space on the page |
| 1627 | ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the |
| 1628 | ** start of the first freeblock on the page, or is zero if there are no |
| 1629 | ** freeblocks. */ |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1630 | pc = get2byte(&data[hdr+1]); |
drh | fdab026 | 2014-11-20 15:30:50 +0000 | [diff] [blame] | 1631 | nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */ |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1632 | while( pc>0 ){ |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 1633 | u16 next, size; |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1634 | if( pc<iCellFirst || pc>iCellLast ){ |
drh | fdab026 | 2014-11-20 15:30:50 +0000 | [diff] [blame] | 1635 | /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will |
| 1636 | ** always be at least one cell before the first freeblock. |
| 1637 | ** |
| 1638 | ** Or, the freeblock is off the end of the page |
| 1639 | */ |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1640 | return SQLITE_CORRUPT_BKPT; |
| 1641 | } |
| 1642 | next = get2byte(&data[pc]); |
| 1643 | size = get2byte(&data[pc+2]); |
dan | 4361e79 | 2009-08-14 17:01:22 +0000 | [diff] [blame] | 1644 | if( (next>0 && next<=pc+size+3) || pc+size>usableSize ){ |
| 1645 | /* Free blocks must be in ascending order. And the last byte of |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 1646 | ** the free-block must lie on the database page. */ |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1647 | return SQLITE_CORRUPT_BKPT; |
| 1648 | } |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 1649 | nFree = nFree + size; |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1650 | pc = next; |
| 1651 | } |
danielk1977 | 93c829c | 2009-06-03 17:26:17 +0000 | [diff] [blame] | 1652 | |
| 1653 | /* At this point, nFree contains the sum of the offset to the start |
| 1654 | ** of the cell-content area plus the number of free bytes within |
| 1655 | ** the cell-content area. If this is greater than the usable-size |
| 1656 | ** of the page, then the page must be corrupted. This check also |
| 1657 | ** serves to verify that the offset to the start of the cell-content |
| 1658 | ** area, according to the page header, lies within the page. |
| 1659 | */ |
| 1660 | if( nFree>usableSize ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1661 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1662 | } |
shane | 5eff7cf | 2009-08-10 03:57:58 +0000 | [diff] [blame] | 1663 | pPage->nFree = (u16)(nFree - iCellFirst); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1664 | pPage->isInit = 1; |
| 1665 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1666 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1670 | ** Set up a raw page so that it looks like a database page holding |
| 1671 | ** no entries. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1672 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1673 | static void zeroPage(MemPage *pPage, int flags){ |
| 1674 | unsigned char *data = pPage->aData; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1675 | BtShared *pBt = pPage->pBt; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1676 | u8 hdr = pPage->hdrOffset; |
| 1677 | u16 first; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1678 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1679 | assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1680 | assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); |
| 1681 | assert( sqlite3PagerGetData(pPage->pDbPage) == data ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1682 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1683 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 1684 | if( pBt->btsFlags & BTS_SECURE_DELETE ){ |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 1685 | memset(&data[hdr], 0, pBt->usableSize - hdr); |
| 1686 | } |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 1687 | data[hdr] = (char)flags; |
drh | fe48599 | 2014-02-12 23:52:16 +0000 | [diff] [blame] | 1688 | first = hdr + ((flags&PTF_LEAF)==0 ? 12 : 8); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1689 | memset(&data[hdr+1], 0, 4); |
| 1690 | data[hdr+7] = 0; |
| 1691 | put2byte(&data[hdr+5], pBt->usableSize); |
shaneh | 1df2db7 | 2010-08-18 02:28:48 +0000 | [diff] [blame] | 1692 | pPage->nFree = (u16)(pBt->usableSize - first); |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1693 | decodeFlags(pPage, flags); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1694 | pPage->cellOffset = first; |
drh | 3def235 | 2011-11-11 00:27:15 +0000 | [diff] [blame] | 1695 | pPage->aDataEnd = &data[pBt->usableSize]; |
| 1696 | pPage->aCellIdx = &data[first]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1697 | pPage->nOverflow = 0; |
drh | b2eced5 | 2010-08-12 02:41:12 +0000 | [diff] [blame] | 1698 | assert( pBt->pageSize>=512 && pBt->pageSize<=65536 ); |
| 1699 | pPage->maskPage = (u16)(pBt->pageSize - 1); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1700 | pPage->nCell = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1701 | pPage->isInit = 1; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1704 | |
| 1705 | /* |
| 1706 | ** Convert a DbPage obtained from the pager into a MemPage used by |
| 1707 | ** the btree layer. |
| 1708 | */ |
| 1709 | static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){ |
| 1710 | MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage); |
| 1711 | pPage->aData = sqlite3PagerGetData(pDbPage); |
| 1712 | pPage->pDbPage = pDbPage; |
| 1713 | pPage->pBt = pBt; |
| 1714 | pPage->pgno = pgno; |
| 1715 | pPage->hdrOffset = pPage->pgno==1 ? 100 : 0; |
| 1716 | return pPage; |
| 1717 | } |
| 1718 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1719 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1720 | ** Get a page from the pager. Initialize the MemPage.pBt and |
| 1721 | ** MemPage.aData elements if needed. |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 1722 | ** |
| 1723 | ** If the noContent flag is set, it means that we do not care about |
| 1724 | ** the content of the page at this time. So do not go to the disk |
| 1725 | ** to fetch the content. Just fill in the content with zeros for now. |
| 1726 | ** If in the future we call sqlite3PagerWrite() on this page, that |
| 1727 | ** means we have started to be concerned about content and the disk |
| 1728 | ** read should occur at that point. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1729 | */ |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 1730 | static int btreeGetPage( |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1731 | BtShared *pBt, /* The btree */ |
| 1732 | Pgno pgno, /* Number of the page to fetch */ |
| 1733 | MemPage **ppPage, /* Return the page in this parameter */ |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 1734 | int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1735 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1736 | int rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1737 | DbPage *pDbPage; |
| 1738 | |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 1739 | assert( flags==0 || flags==PAGER_GET_NOCONTENT || flags==PAGER_GET_READONLY ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1740 | assert( sqlite3_mutex_held(pBt->mutex) ); |
dan | 11dcd11 | 2013-03-15 18:29:18 +0000 | [diff] [blame] | 1741 | rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, flags); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1742 | if( rc ) return rc; |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1743 | *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1744 | return SQLITE_OK; |
| 1745 | } |
| 1746 | |
| 1747 | /* |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 1748 | ** Retrieve a page from the pager cache. If the requested page is not |
| 1749 | ** already in the pager cache return NULL. Initialize the MemPage.pBt and |
| 1750 | ** MemPage.aData elements if needed. |
| 1751 | */ |
| 1752 | static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){ |
| 1753 | DbPage *pDbPage; |
| 1754 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 1755 | pDbPage = sqlite3PagerLookup(pBt->pPager, pgno); |
| 1756 | if( pDbPage ){ |
| 1757 | return btreePageFromDbPage(pDbPage, pgno, pBt); |
| 1758 | } |
| 1759 | return 0; |
| 1760 | } |
| 1761 | |
| 1762 | /* |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 1763 | ** Return the size of the database file in pages. If there is any kind of |
| 1764 | ** error, return ((unsigned int)-1). |
danielk1977 | 67fd7a9 | 2008-09-10 17:53:35 +0000 | [diff] [blame] | 1765 | */ |
drh | b129915 | 2010-03-30 22:58:33 +0000 | [diff] [blame] | 1766 | static Pgno btreePagecount(BtShared *pBt){ |
| 1767 | return pBt->nPage; |
| 1768 | } |
| 1769 | u32 sqlite3BtreeLastPage(Btree *p){ |
| 1770 | assert( sqlite3BtreeHoldsMutex(p) ); |
| 1771 | assert( ((p->pBt->nPage)&0x8000000)==0 ); |
drh | eac5bd7 | 2014-07-25 21:35:39 +0000 | [diff] [blame] | 1772 | return btreePagecount(p->pBt); |
danielk1977 | 67fd7a9 | 2008-09-10 17:53:35 +0000 | [diff] [blame] | 1773 | } |
| 1774 | |
| 1775 | /* |
danielk1977 | 89bc4bc | 2009-07-21 19:25:24 +0000 | [diff] [blame] | 1776 | ** Get a page from the pager and initialize it. This routine is just a |
| 1777 | ** convenience wrapper around separate calls to btreeGetPage() and |
| 1778 | ** btreeInitPage(). |
| 1779 | ** |
| 1780 | ** If an error occurs, then the value *ppPage is set to is undefined. It |
| 1781 | ** may remain unchanged, or it may be set to an invalid value. |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1782 | */ |
| 1783 | static int getAndInitPage( |
dan | 11dcd11 | 2013-03-15 18:29:18 +0000 | [diff] [blame] | 1784 | BtShared *pBt, /* The database file */ |
| 1785 | Pgno pgno, /* Number of the page to get */ |
| 1786 | MemPage **ppPage, /* Write the page pointer here */ |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 1787 | int bReadonly /* PAGER_GET_READONLY or 0 */ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1788 | ){ |
| 1789 | int rc; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1790 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 1791 | assert( bReadonly==PAGER_GET_READONLY || bReadonly==0 ); |
danielk1977 | 89bc4bc | 2009-07-21 19:25:24 +0000 | [diff] [blame] | 1792 | |
dan | ba3cbf3 | 2010-06-30 04:29:03 +0000 | [diff] [blame] | 1793 | if( pgno>btreePagecount(pBt) ){ |
| 1794 | rc = SQLITE_CORRUPT_BKPT; |
| 1795 | }else{ |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 1796 | rc = btreeGetPage(pBt, pgno, ppPage, bReadonly); |
drh | 29f2bad | 2013-12-09 01:04:54 +0000 | [diff] [blame] | 1797 | if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){ |
dan | ba3cbf3 | 2010-06-30 04:29:03 +0000 | [diff] [blame] | 1798 | rc = btreeInitPage(*ppPage); |
| 1799 | if( rc!=SQLITE_OK ){ |
| 1800 | releasePage(*ppPage); |
| 1801 | } |
danielk1977 | 89bc4bc | 2009-07-21 19:25:24 +0000 | [diff] [blame] | 1802 | } |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1803 | } |
dan | ba3cbf3 | 2010-06-30 04:29:03 +0000 | [diff] [blame] | 1804 | |
| 1805 | testcase( pgno==0 ); |
| 1806 | assert( pgno!=0 || rc==SQLITE_CORRUPT ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1807 | return rc; |
| 1808 | } |
| 1809 | |
| 1810 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1811 | ** Release a MemPage. This should be called once for each prior |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 1812 | ** call to btreeGetPage. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1813 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1814 | static void releasePage(MemPage *pPage){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1815 | if( pPage ){ |
| 1816 | assert( pPage->aData ); |
| 1817 | assert( pPage->pBt ); |
drh | da8a330 | 2013-12-13 19:35:21 +0000 | [diff] [blame] | 1818 | assert( pPage->pDbPage!=0 ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1819 | assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); |
| 1820 | assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1821 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | da8a330 | 2013-12-13 19:35:21 +0000 | [diff] [blame] | 1822 | sqlite3PagerUnrefNotNull(pPage->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1823 | } |
| 1824 | } |
| 1825 | |
| 1826 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1827 | ** During a rollback, when the pager reloads information into the cache |
| 1828 | ** so that the cache is restored to its original state at the start of |
| 1829 | ** the transaction, for each page restored this routine is called. |
| 1830 | ** |
| 1831 | ** This routine needs to reset the extra data section at the end of the |
| 1832 | ** page to agree with the restored data. |
| 1833 | */ |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1834 | static void pageReinit(DbPage *pData){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1835 | MemPage *pPage; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1836 | pPage = (MemPage *)sqlite3PagerGetExtra(pData); |
danielk1977 | d217e6f | 2009-04-01 17:13:51 +0000 | [diff] [blame] | 1837 | assert( sqlite3PagerPageRefcount(pData)>0 ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1838 | if( pPage->isInit ){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1839 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1840 | pPage->isInit = 0; |
danielk1977 | d217e6f | 2009-04-01 17:13:51 +0000 | [diff] [blame] | 1841 | if( sqlite3PagerPageRefcount(pData)>1 ){ |
drh | 5e8d887 | 2009-03-30 17:19:48 +0000 | [diff] [blame] | 1842 | /* pPage might not be a btree page; it might be an overflow page |
| 1843 | ** or ptrmap page or a free page. In those cases, the following |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 1844 | ** call to btreeInitPage() will likely return SQLITE_CORRUPT. |
drh | 5e8d887 | 2009-03-30 17:19:48 +0000 | [diff] [blame] | 1845 | ** But no harm is done by this. And it is very important that |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 1846 | ** btreeInitPage() be called on every btree page so we make |
drh | 5e8d887 | 2009-03-30 17:19:48 +0000 | [diff] [blame] | 1847 | ** the call for every page that comes in for re-initing. */ |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 1848 | btreeInitPage(pPage); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1849 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1850 | } |
| 1851 | } |
| 1852 | |
| 1853 | /* |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1854 | ** Invoke the busy handler for a btree. |
| 1855 | */ |
danielk1977 | 1ceedd3 | 2008-11-19 10:22:33 +0000 | [diff] [blame] | 1856 | static int btreeInvokeBusyHandler(void *pArg){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1857 | BtShared *pBt = (BtShared*)pArg; |
| 1858 | assert( pBt->db ); |
| 1859 | assert( sqlite3_mutex_held(pBt->db->mutex) ); |
| 1860 | return sqlite3InvokeBusyHandler(&pBt->db->busyHandler); |
| 1861 | } |
| 1862 | |
| 1863 | /* |
drh | ad3e010 | 2004-09-03 23:32:18 +0000 | [diff] [blame] | 1864 | ** Open a database file. |
| 1865 | ** |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1866 | ** zFilename is the name of the database file. If zFilename is NULL |
drh | 75c014c | 2010-08-30 15:02:28 +0000 | [diff] [blame] | 1867 | ** then an ephemeral database is created. The ephemeral database might |
| 1868 | ** be exclusively in memory, or it might use a disk-based memory cache. |
| 1869 | ** Either way, the ephemeral database will be automatically deleted |
| 1870 | ** when sqlite3BtreeClose() is called. |
| 1871 | ** |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1872 | ** If zFilename is ":memory:" then an in-memory database is created |
| 1873 | ** that is automatically destroyed when it is closed. |
drh | c47fd8e | 2009-04-30 13:30:32 +0000 | [diff] [blame] | 1874 | ** |
drh | 33f111d | 2012-01-17 15:29:14 +0000 | [diff] [blame] | 1875 | ** The "flags" parameter is a bitmask that might contain bits like |
| 1876 | ** BTREE_OMIT_JOURNAL and/or BTREE_MEMORY. |
drh | 75c014c | 2010-08-30 15:02:28 +0000 | [diff] [blame] | 1877 | ** |
drh | c47fd8e | 2009-04-30 13:30:32 +0000 | [diff] [blame] | 1878 | ** If the database is already opened in the same database connection |
| 1879 | ** and we are in shared cache mode, then the open will fail with an |
| 1880 | ** SQLITE_CONSTRAINT error. We cannot allow two or more BtShared |
| 1881 | ** objects in the same database connection since doing so will lead |
| 1882 | ** to problems with locking. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1883 | */ |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1884 | int sqlite3BtreeOpen( |
dan | 3a6d8ae | 2011-04-23 15:54:54 +0000 | [diff] [blame] | 1885 | sqlite3_vfs *pVfs, /* VFS to use for this b-tree */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1886 | const char *zFilename, /* Name of the file containing the BTree database */ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1887 | sqlite3 *db, /* Associated database handle */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1888 | Btree **ppBtree, /* Pointer to new Btree object written here */ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1889 | int flags, /* Options */ |
| 1890 | int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1891 | ){ |
drh | 7555d8e | 2009-03-20 13:15:30 +0000 | [diff] [blame] | 1892 | BtShared *pBt = 0; /* Shared part of btree structure */ |
| 1893 | Btree *p; /* Handle to return */ |
| 1894 | sqlite3_mutex *mutexOpen = 0; /* Prevents a race condition. Ticket #3537 */ |
| 1895 | int rc = SQLITE_OK; /* Result code from this function */ |
| 1896 | u8 nReserve; /* Byte of unused space on each page */ |
| 1897 | unsigned char zDbHeader[100]; /* Database header content */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1898 | |
drh | 75c014c | 2010-08-30 15:02:28 +0000 | [diff] [blame] | 1899 | /* True if opening an ephemeral, temporary database */ |
| 1900 | const int isTempDb = zFilename==0 || zFilename[0]==0; |
| 1901 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1902 | /* Set the variable isMemdb to true for an in-memory database, or |
drh | b0a7c9c | 2010-12-06 21:09:59 +0000 | [diff] [blame] | 1903 | ** false for a file-based database. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1904 | */ |
drh | b0a7c9c | 2010-12-06 21:09:59 +0000 | [diff] [blame] | 1905 | #ifdef SQLITE_OMIT_MEMORYDB |
| 1906 | const int isMemdb = 0; |
| 1907 | #else |
| 1908 | const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0) |
drh | 9c67b2a | 2012-05-28 13:58:00 +0000 | [diff] [blame] | 1909 | || (isTempDb && sqlite3TempInMemory(db)) |
| 1910 | || (vfsFlags & SQLITE_OPEN_MEMORY)!=0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1911 | #endif |
| 1912 | |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1913 | assert( db!=0 ); |
dan | 3a6d8ae | 2011-04-23 15:54:54 +0000 | [diff] [blame] | 1914 | assert( pVfs!=0 ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1915 | assert( sqlite3_mutex_held(db->mutex) ); |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 1916 | assert( (flags&0xff)==flags ); /* flags fit in 8 bits */ |
| 1917 | |
| 1918 | /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */ |
| 1919 | assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 ); |
| 1920 | |
| 1921 | /* A BTREE_SINGLE database is always a temporary and/or ephemeral */ |
| 1922 | assert( (flags & BTREE_SINGLE)==0 || isTempDb ); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1923 | |
drh | 75c014c | 2010-08-30 15:02:28 +0000 | [diff] [blame] | 1924 | if( isMemdb ){ |
| 1925 | flags |= BTREE_MEMORY; |
| 1926 | } |
| 1927 | if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){ |
| 1928 | vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB; |
| 1929 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1930 | p = sqlite3MallocZero(sizeof(Btree)); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1931 | if( !p ){ |
| 1932 | return SQLITE_NOMEM; |
| 1933 | } |
| 1934 | p->inTrans = TRANS_NONE; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1935 | p->db = db; |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 1936 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 1937 | p->lock.pBtree = p; |
| 1938 | p->lock.iTable = 1; |
| 1939 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1940 | |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 1941 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1942 | /* |
| 1943 | ** If this Btree is a candidate for shared cache, try to find an |
| 1944 | ** existing BtShared object that we can share with |
| 1945 | */ |
drh | 4ab9d25 | 2012-05-26 20:08:49 +0000 | [diff] [blame] | 1946 | if( isTempDb==0 && (isMemdb==0 || (vfsFlags&SQLITE_OPEN_URI)!=0) ){ |
drh | f1f1268 | 2009-09-09 14:17:52 +0000 | [diff] [blame] | 1947 | if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 1948 | int nFullPathname = pVfs->mxPathname+1; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1949 | char *zFullPathname = sqlite3Malloc(nFullPathname); |
drh | 30ddce6 | 2011-10-15 00:16:30 +0000 | [diff] [blame] | 1950 | MUTEX_LOGIC( sqlite3_mutex *mutexShared; ) |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1951 | p->sharable = 1; |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1952 | if( !zFullPathname ){ |
| 1953 | sqlite3_free(p); |
| 1954 | return SQLITE_NOMEM; |
| 1955 | } |
drh | afc8b7f | 2012-05-26 18:06:38 +0000 | [diff] [blame] | 1956 | if( isMemdb ){ |
| 1957 | memcpy(zFullPathname, zFilename, sqlite3Strlen30(zFilename)+1); |
| 1958 | }else{ |
| 1959 | rc = sqlite3OsFullPathname(pVfs, zFilename, |
| 1960 | nFullPathname, zFullPathname); |
| 1961 | if( rc ){ |
| 1962 | sqlite3_free(zFullPathname); |
| 1963 | sqlite3_free(p); |
| 1964 | return rc; |
| 1965 | } |
drh | 070ad6b | 2011-11-17 11:43:19 +0000 | [diff] [blame] | 1966 | } |
drh | 30ddce6 | 2011-10-15 00:16:30 +0000 | [diff] [blame] | 1967 | #if SQLITE_THREADSAFE |
drh | 7555d8e | 2009-03-20 13:15:30 +0000 | [diff] [blame] | 1968 | mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN); |
| 1969 | sqlite3_mutex_enter(mutexOpen); |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 1970 | mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1971 | sqlite3_mutex_enter(mutexShared); |
drh | 30ddce6 | 2011-10-15 00:16:30 +0000 | [diff] [blame] | 1972 | #endif |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 1973 | for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1974 | assert( pBt->nRef>0 ); |
drh | d4e0bb0 | 2012-05-27 01:19:04 +0000 | [diff] [blame] | 1975 | if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0)) |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1976 | && sqlite3PagerVfs(pBt->pPager)==pVfs ){ |
drh | c47fd8e | 2009-04-30 13:30:32 +0000 | [diff] [blame] | 1977 | int iDb; |
| 1978 | for(iDb=db->nDb-1; iDb>=0; iDb--){ |
| 1979 | Btree *pExisting = db->aDb[iDb].pBt; |
| 1980 | if( pExisting && pExisting->pBt==pBt ){ |
| 1981 | sqlite3_mutex_leave(mutexShared); |
| 1982 | sqlite3_mutex_leave(mutexOpen); |
| 1983 | sqlite3_free(zFullPathname); |
| 1984 | sqlite3_free(p); |
| 1985 | return SQLITE_CONSTRAINT; |
| 1986 | } |
| 1987 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1988 | p->pBt = pBt; |
| 1989 | pBt->nRef++; |
| 1990 | break; |
| 1991 | } |
| 1992 | } |
| 1993 | sqlite3_mutex_leave(mutexShared); |
| 1994 | sqlite3_free(zFullPathname); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1995 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1996 | #ifdef SQLITE_DEBUG |
| 1997 | else{ |
| 1998 | /* In debug mode, we mark all persistent databases as sharable |
| 1999 | ** even when they are not. This exercises the locking code and |
| 2000 | ** gives more opportunity for asserts(sqlite3_mutex_held()) |
| 2001 | ** statements to find locking problems. |
| 2002 | */ |
| 2003 | p->sharable = 1; |
| 2004 | } |
| 2005 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2006 | } |
| 2007 | #endif |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2008 | if( pBt==0 ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2009 | /* |
| 2010 | ** The following asserts make sure that structures used by the btree are |
| 2011 | ** the right size. This is to guard against size changes that result |
| 2012 | ** when compiling on a different architecture. |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 2013 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2014 | assert( sizeof(i64)==8 || sizeof(i64)==4 ); |
| 2015 | assert( sizeof(u64)==8 || sizeof(u64)==4 ); |
| 2016 | assert( sizeof(u32)==4 ); |
| 2017 | assert( sizeof(u16)==2 ); |
| 2018 | assert( sizeof(Pgno)==4 ); |
| 2019 | |
| 2020 | pBt = sqlite3MallocZero( sizeof(*pBt) ); |
| 2021 | if( pBt==0 ){ |
| 2022 | rc = SQLITE_NOMEM; |
| 2023 | goto btree_open_out; |
| 2024 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 2025 | rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename, |
drh | 4775ecd | 2009-07-24 19:01:19 +0000 | [diff] [blame] | 2026 | EXTRA_SIZE, flags, vfsFlags, pageReinit); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2027 | if( rc==SQLITE_OK ){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 2028 | sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2029 | rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader); |
| 2030 | } |
| 2031 | if( rc!=SQLITE_OK ){ |
| 2032 | goto btree_open_out; |
| 2033 | } |
shaneh | bd2aaf9 | 2010-09-01 02:38:21 +0000 | [diff] [blame] | 2034 | pBt->openFlags = (u8)flags; |
danielk1977 | 2a50ff0 | 2009-04-10 09:47:06 +0000 | [diff] [blame] | 2035 | pBt->db = db; |
danielk1977 | 1ceedd3 | 2008-11-19 10:22:33 +0000 | [diff] [blame] | 2036 | sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2037 | p->pBt = pBt; |
| 2038 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2039 | pBt->pCursor = 0; |
| 2040 | pBt->pPage1 = 0; |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2041 | if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY; |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 2042 | #ifdef SQLITE_SECURE_DELETE |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2043 | pBt->btsFlags |= BTS_SECURE_DELETE; |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 2044 | #endif |
drh | 113762a | 2014-11-19 16:36:25 +0000 | [diff] [blame] | 2045 | /* EVIDENCE-OF: R-51873-39618 The page size for a database file is |
| 2046 | ** determined by the 2-byte integer located at an offset of 16 bytes from |
| 2047 | ** the beginning of the database file. */ |
drh | b2eced5 | 2010-08-12 02:41:12 +0000 | [diff] [blame] | 2048 | pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2049 | if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE |
| 2050 | || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2051 | pBt->pageSize = 0; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2052 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2053 | /* If the magic name ":memory:" will create an in-memory database, then |
| 2054 | ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if |
| 2055 | ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if |
| 2056 | ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a |
| 2057 | ** regular file-name. In this case the auto-vacuum applies as per normal. |
| 2058 | */ |
| 2059 | if( zFilename && !isMemdb ){ |
| 2060 | pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0); |
| 2061 | pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0); |
| 2062 | } |
| 2063 | #endif |
| 2064 | nReserve = 0; |
| 2065 | }else{ |
drh | 113762a | 2014-11-19 16:36:25 +0000 | [diff] [blame] | 2066 | /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is |
| 2067 | ** determined by the one-byte unsigned integer found at an offset of 20 |
| 2068 | ** into the database file header. */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2069 | nReserve = zDbHeader[20]; |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2070 | pBt->btsFlags |= BTS_PAGESIZE_FIXED; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2071 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2072 | pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0); |
| 2073 | pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0); |
| 2074 | #endif |
| 2075 | } |
drh | fa9601a | 2009-06-18 17:22:39 +0000 | [diff] [blame] | 2076 | rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve); |
drh | c0b6181 | 2009-04-30 01:22:41 +0000 | [diff] [blame] | 2077 | if( rc ) goto btree_open_out; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2078 | pBt->usableSize = pBt->pageSize - nReserve; |
| 2079 | assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2080 | |
| 2081 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
| 2082 | /* Add the new BtShared object to the linked list sharable BtShareds. |
| 2083 | */ |
| 2084 | if( p->sharable ){ |
drh | 30ddce6 | 2011-10-15 00:16:30 +0000 | [diff] [blame] | 2085 | MUTEX_LOGIC( sqlite3_mutex *mutexShared; ) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2086 | pBt->nRef = 1; |
drh | 30ddce6 | 2011-10-15 00:16:30 +0000 | [diff] [blame] | 2087 | MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);) |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 2088 | if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){ |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 2089 | pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST); |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 2090 | if( pBt->mutex==0 ){ |
| 2091 | rc = SQLITE_NOMEM; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2092 | db->mallocFailed = 0; |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 2093 | goto btree_open_out; |
| 2094 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 2095 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2096 | sqlite3_mutex_enter(mutexShared); |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 2097 | pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList); |
| 2098 | GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2099 | sqlite3_mutex_leave(mutexShared); |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 2100 | } |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 2101 | #endif |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2102 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2103 | |
drh | cfed7bc | 2006-03-13 14:28:05 +0000 | [diff] [blame] | 2104 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2105 | /* If the new Btree uses a sharable pBtShared, then link the new |
| 2106 | ** Btree into the list of all sharable Btrees for the same connection. |
drh | abddb0c | 2007-08-20 13:14:28 +0000 | [diff] [blame] | 2107 | ** The list is kept in ascending order by pBt address. |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 2108 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2109 | if( p->sharable ){ |
| 2110 | int i; |
| 2111 | Btree *pSib; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2112 | for(i=0; i<db->nDb; i++){ |
| 2113 | if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2114 | while( pSib->pPrev ){ pSib = pSib->pPrev; } |
| 2115 | if( p->pBt<pSib->pBt ){ |
| 2116 | p->pNext = pSib; |
| 2117 | p->pPrev = 0; |
| 2118 | pSib->pPrev = p; |
| 2119 | }else{ |
drh | abddb0c | 2007-08-20 13:14:28 +0000 | [diff] [blame] | 2120 | while( pSib->pNext && pSib->pNext->pBt<p->pBt ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2121 | pSib = pSib->pNext; |
| 2122 | } |
| 2123 | p->pNext = pSib->pNext; |
| 2124 | p->pPrev = pSib; |
| 2125 | if( p->pNext ){ |
| 2126 | p->pNext->pPrev = p; |
| 2127 | } |
| 2128 | pSib->pNext = p; |
| 2129 | } |
| 2130 | break; |
| 2131 | } |
| 2132 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2133 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2134 | #endif |
| 2135 | *ppBtree = p; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2136 | |
| 2137 | btree_open_out: |
| 2138 | if( rc!=SQLITE_OK ){ |
| 2139 | if( pBt && pBt->pPager ){ |
| 2140 | sqlite3PagerClose(pBt->pPager); |
| 2141 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2142 | sqlite3_free(pBt); |
| 2143 | sqlite3_free(p); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2144 | *ppBtree = 0; |
drh | 75c014c | 2010-08-30 15:02:28 +0000 | [diff] [blame] | 2145 | }else{ |
| 2146 | /* If the B-Tree was successfully opened, set the pager-cache size to the |
| 2147 | ** default value. Except, when opening on an existing shared pager-cache, |
| 2148 | ** do not change the pager-cache size. |
| 2149 | */ |
| 2150 | if( sqlite3BtreeSchema(p, 0, 0)==0 ){ |
| 2151 | sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE); |
| 2152 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2153 | } |
drh | 7555d8e | 2009-03-20 13:15:30 +0000 | [diff] [blame] | 2154 | if( mutexOpen ){ |
| 2155 | assert( sqlite3_mutex_held(mutexOpen) ); |
| 2156 | sqlite3_mutex_leave(mutexOpen); |
| 2157 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2158 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2159 | } |
| 2160 | |
| 2161 | /* |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2162 | ** Decrement the BtShared.nRef counter. When it reaches zero, |
| 2163 | ** remove the BtShared structure from the sharing list. Return |
| 2164 | ** true if the BtShared.nRef counter reaches zero and return |
| 2165 | ** false if it is still positive. |
| 2166 | */ |
| 2167 | static int removeFromSharingList(BtShared *pBt){ |
| 2168 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | 30ddce6 | 2011-10-15 00:16:30 +0000 | [diff] [blame] | 2169 | MUTEX_LOGIC( sqlite3_mutex *pMaster; ) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2170 | BtShared *pList; |
| 2171 | int removed = 0; |
| 2172 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2173 | assert( sqlite3_mutex_notheld(pBt->mutex) ); |
drh | 30ddce6 | 2011-10-15 00:16:30 +0000 | [diff] [blame] | 2174 | MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); ) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2175 | sqlite3_mutex_enter(pMaster); |
| 2176 | pBt->nRef--; |
| 2177 | if( pBt->nRef<=0 ){ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 2178 | if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){ |
| 2179 | GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2180 | }else{ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 2181 | pList = GLOBAL(BtShared*,sqlite3SharedCacheList); |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 2182 | while( ALWAYS(pList) && pList->pNext!=pBt ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2183 | pList=pList->pNext; |
| 2184 | } |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 2185 | if( ALWAYS(pList) ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2186 | pList->pNext = pBt->pNext; |
| 2187 | } |
| 2188 | } |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 2189 | if( SQLITE_THREADSAFE ){ |
| 2190 | sqlite3_mutex_free(pBt->mutex); |
| 2191 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2192 | removed = 1; |
| 2193 | } |
| 2194 | sqlite3_mutex_leave(pMaster); |
| 2195 | return removed; |
| 2196 | #else |
| 2197 | return 1; |
| 2198 | #endif |
| 2199 | } |
| 2200 | |
| 2201 | /* |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 2202 | ** Make sure pBt->pTmpSpace points to an allocation of |
drh | 92787cf | 2014-10-15 11:55:51 +0000 | [diff] [blame] | 2203 | ** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child |
| 2204 | ** pointer. |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 2205 | */ |
| 2206 | static void allocateTempSpace(BtShared *pBt){ |
| 2207 | if( !pBt->pTmpSpace ){ |
| 2208 | pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize ); |
dan | 14285b7 | 2013-10-16 11:39:07 +0000 | [diff] [blame] | 2209 | |
| 2210 | /* One of the uses of pBt->pTmpSpace is to format cells before |
| 2211 | ** inserting them into a leaf page (function fillInCell()). If |
| 2212 | ** a cell is less than 4 bytes in size, it is rounded up to 4 bytes |
| 2213 | ** by the various routines that manipulate binary cells. Which |
| 2214 | ** can mean that fillInCell() only initializes the first 2 or 3 |
| 2215 | ** bytes of pTmpSpace, but that the first 4 bytes are copied from |
| 2216 | ** it into a database page. This is not actually a problem, but it |
| 2217 | ** does cause a valgrind error when the 1 or 2 bytes of unitialized |
| 2218 | ** data is passed to system call write(). So to avoid this error, |
drh | 92787cf | 2014-10-15 11:55:51 +0000 | [diff] [blame] | 2219 | ** zero the first 4 bytes of temp space here. |
| 2220 | ** |
| 2221 | ** Also: Provide four bytes of initialized space before the |
| 2222 | ** beginning of pTmpSpace as an area available to prepend the |
| 2223 | ** left-child pointer to the beginning of a cell. |
| 2224 | */ |
| 2225 | if( pBt->pTmpSpace ){ |
| 2226 | memset(pBt->pTmpSpace, 0, 8); |
| 2227 | pBt->pTmpSpace += 4; |
| 2228 | } |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 2229 | } |
| 2230 | } |
| 2231 | |
| 2232 | /* |
| 2233 | ** Free the pBt->pTmpSpace allocation |
| 2234 | */ |
| 2235 | static void freeTempSpace(BtShared *pBt){ |
drh | 92787cf | 2014-10-15 11:55:51 +0000 | [diff] [blame] | 2236 | if( pBt->pTmpSpace ){ |
| 2237 | pBt->pTmpSpace -= 4; |
| 2238 | sqlite3PageFree(pBt->pTmpSpace); |
| 2239 | pBt->pTmpSpace = 0; |
| 2240 | } |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 2241 | } |
| 2242 | |
| 2243 | /* |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2244 | ** Close an open database and invalidate all cursors. |
| 2245 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2246 | int sqlite3BtreeClose(Btree *p){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2247 | BtShared *pBt = p->pBt; |
| 2248 | BtCursor *pCur; |
| 2249 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2250 | /* Close all cursors opened via this handle. */ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2251 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2252 | sqlite3BtreeEnter(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2253 | pCur = pBt->pCursor; |
| 2254 | while( pCur ){ |
| 2255 | BtCursor *pTmp = pCur; |
| 2256 | pCur = pCur->pNext; |
| 2257 | if( pTmp->pBtree==p ){ |
| 2258 | sqlite3BtreeCloseCursor(pTmp); |
| 2259 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2260 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2261 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2262 | /* Rollback any active transaction and free the handle structure. |
| 2263 | ** The call to sqlite3BtreeRollback() drops any table-locks held by |
| 2264 | ** this handle. |
| 2265 | */ |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 2266 | sqlite3BtreeRollback(p, SQLITE_OK, 0); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2267 | sqlite3BtreeLeave(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2268 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2269 | /* If there are still other outstanding references to the shared-btree |
| 2270 | ** structure, return now. The remainder of this procedure cleans |
| 2271 | ** up the shared-btree. |
| 2272 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2273 | assert( p->wantToLock==0 && p->locked==0 ); |
| 2274 | if( !p->sharable || removeFromSharingList(pBt) ){ |
| 2275 | /* The pBt is no longer on the sharing list, so we can access |
| 2276 | ** it without having to hold the mutex. |
| 2277 | ** |
| 2278 | ** Clean out and delete the BtShared object. |
| 2279 | */ |
| 2280 | assert( !pBt->pCursor ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2281 | sqlite3PagerClose(pBt->pPager); |
| 2282 | if( pBt->xFreeSchema && pBt->pSchema ){ |
| 2283 | pBt->xFreeSchema(pBt->pSchema); |
| 2284 | } |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 2285 | sqlite3DbFree(0, pBt->pSchema); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 2286 | freeTempSpace(pBt); |
drh | 65bbf29 | 2008-06-19 01:03:17 +0000 | [diff] [blame] | 2287 | sqlite3_free(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2288 | } |
| 2289 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2290 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | cab5ed7 | 2007-08-22 11:41:18 +0000 | [diff] [blame] | 2291 | assert( p->wantToLock==0 ); |
| 2292 | assert( p->locked==0 ); |
| 2293 | if( p->pPrev ) p->pPrev->pNext = p->pNext; |
| 2294 | if( p->pNext ) p->pNext->pPrev = p->pPrev; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2295 | #endif |
| 2296 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2297 | sqlite3_free(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2298 | return SQLITE_OK; |
| 2299 | } |
| 2300 | |
| 2301 | /* |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 2302 | ** Change the limit on the number of pages allowed in the cache. |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 2303 | ** |
| 2304 | ** The maximum number of cache pages is set to the absolute |
| 2305 | ** value of mxPage. If mxPage is negative, the pager will |
| 2306 | ** operate asynchronously - it will not stop to do fsync()s |
| 2307 | ** to insure data is written to the disk surface before |
| 2308 | ** continuing. Transactions still work if synchronous is off, |
| 2309 | ** and the database cannot be corrupted if this program |
| 2310 | ** crashes. But if the operating system crashes or there is |
| 2311 | ** an abrupt power failure when synchronous is off, the database |
| 2312 | ** could be left in an inconsistent and unrecoverable state. |
| 2313 | ** Synchronous is on by default so database corruption is not |
| 2314 | ** normally a worry. |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 2315 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2316 | int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){ |
| 2317 | BtShared *pBt = p->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2318 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2319 | sqlite3BtreeEnter(p); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2320 | sqlite3PagerSetCachesize(pBt->pPager, mxPage); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2321 | sqlite3BtreeLeave(p); |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 2322 | return SQLITE_OK; |
| 2323 | } |
| 2324 | |
drh | 18c7e40 | 2014-03-14 11:46:10 +0000 | [diff] [blame] | 2325 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 2326 | /* |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 2327 | ** Change the limit on the amount of the database file that may be |
| 2328 | ** memory mapped. |
| 2329 | */ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 2330 | int sqlite3BtreeSetMmapLimit(Btree *p, sqlite3_int64 szMmap){ |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 2331 | BtShared *pBt = p->pBt; |
| 2332 | assert( sqlite3_mutex_held(p->db->mutex) ); |
| 2333 | sqlite3BtreeEnter(p); |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 2334 | sqlite3PagerSetMmapLimit(pBt->pPager, szMmap); |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 2335 | sqlite3BtreeLeave(p); |
| 2336 | return SQLITE_OK; |
| 2337 | } |
drh | 18c7e40 | 2014-03-14 11:46:10 +0000 | [diff] [blame] | 2338 | #endif /* SQLITE_MAX_MMAP_SIZE>0 */ |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 2339 | |
| 2340 | /* |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 2341 | ** Change the way data is synced to disk in order to increase or decrease |
| 2342 | ** how well the database resists damage due to OS crashes and power |
| 2343 | ** failures. Level 1 is the same as asynchronous (no syncs() occur and |
| 2344 | ** there is a high probability of damage) Level 2 is the default. There |
| 2345 | ** is a very low but non-zero probability of damage. Level 3 reduces the |
| 2346 | ** probability of damage to near zero but with a write performance reduction. |
| 2347 | */ |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 2348 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
drh | 40c3941 | 2013-08-16 20:42:20 +0000 | [diff] [blame] | 2349 | int sqlite3BtreeSetPagerFlags( |
drh | c97d846 | 2010-11-19 18:23:35 +0000 | [diff] [blame] | 2350 | Btree *p, /* The btree to set the safety level on */ |
drh | 40c3941 | 2013-08-16 20:42:20 +0000 | [diff] [blame] | 2351 | unsigned pgFlags /* Various PAGER_* flags */ |
drh | c97d846 | 2010-11-19 18:23:35 +0000 | [diff] [blame] | 2352 | ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2353 | BtShared *pBt = p->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2354 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2355 | sqlite3BtreeEnter(p); |
drh | 40c3941 | 2013-08-16 20:42:20 +0000 | [diff] [blame] | 2356 | sqlite3PagerSetFlags(pBt->pPager, pgFlags); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2357 | sqlite3BtreeLeave(p); |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 2358 | return SQLITE_OK; |
| 2359 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 2360 | #endif |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 2361 | |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 2362 | /* |
| 2363 | ** Return TRUE if the given btree is set to safety level 1. In other |
| 2364 | ** words, return TRUE if no sync() occurs on the disk files. |
| 2365 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2366 | int sqlite3BtreeSyncDisabled(Btree *p){ |
| 2367 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2368 | int rc; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2369 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2370 | sqlite3BtreeEnter(p); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 2371 | assert( pBt && pBt->pPager ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2372 | rc = sqlite3PagerNosync(pBt->pPager); |
| 2373 | sqlite3BtreeLeave(p); |
| 2374 | return rc; |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 2375 | } |
| 2376 | |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 2377 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2378 | ** Change the default pages size and the number of reserved bytes per page. |
drh | ce4869f | 2009-04-02 20:16:58 +0000 | [diff] [blame] | 2379 | ** Or, if the page size has already been fixed, return SQLITE_READONLY |
| 2380 | ** without changing anything. |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 2381 | ** |
| 2382 | ** The page size must be a power of 2 between 512 and 65536. If the page |
| 2383 | ** size supplied does not meet this constraint then the page size is not |
| 2384 | ** changed. |
| 2385 | ** |
| 2386 | ** Page sizes are constrained to be a power of two so that the region |
| 2387 | ** of the database file used for locking (beginning at PENDING_BYTE, |
| 2388 | ** the first byte past the 1GB boundary, 0x40000000) needs to occur |
| 2389 | ** at the beginning of a page. |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 2390 | ** |
| 2391 | ** If parameter nReserve is less than zero, then the number of reserved |
| 2392 | ** bytes per page is left unchanged. |
drh | ce4869f | 2009-04-02 20:16:58 +0000 | [diff] [blame] | 2393 | ** |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2394 | ** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size |
drh | ce4869f | 2009-04-02 20:16:58 +0000 | [diff] [blame] | 2395 | ** and autovacuum mode can no longer be changed. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2396 | */ |
drh | ce4869f | 2009-04-02 20:16:58 +0000 | [diff] [blame] | 2397 | int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2398 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2399 | BtShared *pBt = p->pBt; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 2400 | assert( nReserve>=-1 && nReserve<=255 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2401 | sqlite3BtreeEnter(p); |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2402 | if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2403 | sqlite3BtreeLeave(p); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2404 | return SQLITE_READONLY; |
| 2405 | } |
| 2406 | if( nReserve<0 ){ |
| 2407 | nReserve = pBt->pageSize - pBt->usableSize; |
| 2408 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 2409 | assert( nReserve>=0 && nReserve<=255 ); |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 2410 | if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE && |
| 2411 | ((pageSize-1)&pageSize)==0 ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 2412 | assert( (pageSize & 7)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2413 | assert( !pBt->pPage1 && !pBt->pCursor ); |
drh | b2eced5 | 2010-08-12 02:41:12 +0000 | [diff] [blame] | 2414 | pBt->pageSize = (u32)pageSize; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 2415 | freeTempSpace(pBt); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2416 | } |
drh | fa9601a | 2009-06-18 17:22:39 +0000 | [diff] [blame] | 2417 | rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 2418 | pBt->usableSize = pBt->pageSize - (u16)nReserve; |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2419 | if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2420 | sqlite3BtreeLeave(p); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2421 | return rc; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2422 | } |
| 2423 | |
| 2424 | /* |
| 2425 | ** Return the currently defined page size |
| 2426 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2427 | int sqlite3BtreeGetPageSize(Btree *p){ |
| 2428 | return p->pBt->pageSize; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2429 | } |
drh | 7f75122 | 2009-03-17 22:33:00 +0000 | [diff] [blame] | 2430 | |
drh | a1f3853 | 2012-10-01 12:44:26 +0000 | [diff] [blame] | 2431 | #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_DEBUG) |
dan | 0094f37 | 2012-09-28 20:23:42 +0000 | [diff] [blame] | 2432 | /* |
| 2433 | ** This function is similar to sqlite3BtreeGetReserve(), except that it |
| 2434 | ** may only be called if it is guaranteed that the b-tree mutex is already |
| 2435 | ** held. |
| 2436 | ** |
| 2437 | ** This is useful in one special case in the backup API code where it is |
| 2438 | ** known that the shared b-tree mutex is held, but the mutex on the |
| 2439 | ** database handle that owns *p is not. In this case if sqlite3BtreeEnter() |
| 2440 | ** were to be called, it might collide with some other operation on the |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 2441 | ** database handle that owns *p, causing undefined behavior. |
dan | 0094f37 | 2012-09-28 20:23:42 +0000 | [diff] [blame] | 2442 | */ |
| 2443 | int sqlite3BtreeGetReserveNoMutex(Btree *p){ |
| 2444 | assert( sqlite3_mutex_held(p->pBt->mutex) ); |
| 2445 | return p->pBt->pageSize - p->pBt->usableSize; |
| 2446 | } |
drh | a1f3853 | 2012-10-01 12:44:26 +0000 | [diff] [blame] | 2447 | #endif /* SQLITE_HAS_CODEC || SQLITE_DEBUG */ |
dan | 0094f37 | 2012-09-28 20:23:42 +0000 | [diff] [blame] | 2448 | |
dan | bb2b441 | 2011-04-06 17:54:31 +0000 | [diff] [blame] | 2449 | #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) |
drh | 7f75122 | 2009-03-17 22:33:00 +0000 | [diff] [blame] | 2450 | /* |
| 2451 | ** Return the number of bytes of space at the end of every page that |
| 2452 | ** are intentually left unused. This is the "reserved" space that is |
| 2453 | ** sometimes used by extensions. |
| 2454 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2455 | int sqlite3BtreeGetReserve(Btree *p){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2456 | int n; |
| 2457 | sqlite3BtreeEnter(p); |
| 2458 | n = p->pBt->pageSize - p->pBt->usableSize; |
| 2459 | sqlite3BtreeLeave(p); |
| 2460 | return n; |
drh | 2011d5f | 2004-07-22 02:40:37 +0000 | [diff] [blame] | 2461 | } |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 2462 | |
| 2463 | /* |
| 2464 | ** Set the maximum page count for a database if mxPage is positive. |
| 2465 | ** No changes are made if mxPage is 0 or negative. |
| 2466 | ** Regardless of the value of mxPage, return the maximum page count. |
| 2467 | */ |
| 2468 | int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2469 | int n; |
| 2470 | sqlite3BtreeEnter(p); |
| 2471 | n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage); |
| 2472 | sqlite3BtreeLeave(p); |
| 2473 | return n; |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 2474 | } |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 2475 | |
| 2476 | /* |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2477 | ** Set the BTS_SECURE_DELETE flag if newFlag is 0 or 1. If newFlag is -1, |
| 2478 | ** then make no changes. Always return the value of the BTS_SECURE_DELETE |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 2479 | ** setting after the change. |
| 2480 | */ |
| 2481 | int sqlite3BtreeSecureDelete(Btree *p, int newFlag){ |
| 2482 | int b; |
drh | af034ed | 2010-02-12 19:46:26 +0000 | [diff] [blame] | 2483 | if( p==0 ) return 0; |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 2484 | sqlite3BtreeEnter(p); |
| 2485 | if( newFlag>=0 ){ |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2486 | p->pBt->btsFlags &= ~BTS_SECURE_DELETE; |
| 2487 | if( newFlag ) p->pBt->btsFlags |= BTS_SECURE_DELETE; |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 2488 | } |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2489 | b = (p->pBt->btsFlags & BTS_SECURE_DELETE)!=0; |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 2490 | sqlite3BtreeLeave(p); |
| 2491 | return b; |
| 2492 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 2493 | #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2494 | |
| 2495 | /* |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 2496 | ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum' |
| 2497 | ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it |
| 2498 | ** is disabled. The default value for the auto-vacuum property is |
| 2499 | ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro. |
| 2500 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2501 | int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 2502 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 2503 | return SQLITE_READONLY; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 2504 | #else |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2505 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2506 | int rc = SQLITE_OK; |
drh | 076d466 | 2009-02-18 20:31:18 +0000 | [diff] [blame] | 2507 | u8 av = (u8)autoVacuum; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2508 | |
| 2509 | sqlite3BtreeEnter(p); |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2510 | if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2511 | rc = SQLITE_READONLY; |
| 2512 | }else{ |
drh | 076d466 | 2009-02-18 20:31:18 +0000 | [diff] [blame] | 2513 | pBt->autoVacuum = av ?1:0; |
| 2514 | pBt->incrVacuum = av==2 ?1:0; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 2515 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2516 | sqlite3BtreeLeave(p); |
| 2517 | return rc; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 2518 | #endif |
| 2519 | } |
| 2520 | |
| 2521 | /* |
| 2522 | ** Return the value of the 'auto-vacuum' property. If auto-vacuum is |
| 2523 | ** enabled 1 is returned. Otherwise 0. |
| 2524 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2525 | int sqlite3BtreeGetAutoVacuum(Btree *p){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 2526 | #ifdef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2527 | return BTREE_AUTOVACUUM_NONE; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 2528 | #else |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2529 | int rc; |
| 2530 | sqlite3BtreeEnter(p); |
| 2531 | rc = ( |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2532 | (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE: |
| 2533 | (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL: |
| 2534 | BTREE_AUTOVACUUM_INCR |
| 2535 | ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2536 | sqlite3BtreeLeave(p); |
| 2537 | return rc; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 2538 | #endif |
| 2539 | } |
| 2540 | |
| 2541 | |
| 2542 | /* |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2543 | ** Get a reference to pPage1 of the database file. This will |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 2544 | ** also acquire a readlock on that file. |
| 2545 | ** |
| 2546 | ** SQLITE_OK is returned on success. If the file is not a |
| 2547 | ** well-formed database file, then SQLITE_CORRUPT is returned. |
| 2548 | ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 2549 | ** is returned if we run out of memory. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 2550 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2551 | static int lockBtree(BtShared *pBt){ |
drh | c2a4bab | 2010-04-02 12:46:45 +0000 | [diff] [blame] | 2552 | int rc; /* Result code from subfunctions */ |
| 2553 | MemPage *pPage1; /* Page 1 of the database file */ |
| 2554 | int nPage; /* Number of pages in the database */ |
| 2555 | int nPageFile = 0; /* Number of pages in the database file */ |
| 2556 | int nPageHeader; /* Number of pages in the database according to hdr */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2557 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2558 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 295dc10 | 2009-04-01 19:07:03 +0000 | [diff] [blame] | 2559 | assert( pBt->pPage1==0 ); |
danielk1977 | 89bc4bc | 2009-07-21 19:25:24 +0000 | [diff] [blame] | 2560 | rc = sqlite3PagerSharedLock(pBt->pPager); |
| 2561 | if( rc!=SQLITE_OK ) return rc; |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 2562 | rc = btreeGetPage(pBt, 1, &pPage1, 0); |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 2563 | if( rc!=SQLITE_OK ) return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 2564 | |
| 2565 | /* Do some checking to help insure the file we opened really is |
| 2566 | ** a valid database file. |
| 2567 | */ |
drh | c2a4bab | 2010-04-02 12:46:45 +0000 | [diff] [blame] | 2568 | nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData); |
drh | 8fb8b53 | 2010-08-14 17:12:04 +0000 | [diff] [blame] | 2569 | sqlite3PagerPagecount(pBt->pPager, &nPageFile); |
drh | b28e59b | 2010-06-17 02:13:39 +0000 | [diff] [blame] | 2570 | if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){ |
drh | c2a4bab | 2010-04-02 12:46:45 +0000 | [diff] [blame] | 2571 | nPage = nPageFile; |
drh | 97b59a5 | 2010-03-31 02:31:33 +0000 | [diff] [blame] | 2572 | } |
| 2573 | if( nPage>0 ){ |
drh | 43b18e1 | 2010-08-17 19:40:08 +0000 | [diff] [blame] | 2574 | u32 pageSize; |
| 2575 | u32 usableSize; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2576 | u8 *page1 = pPage1->aData; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2577 | rc = SQLITE_NOTADB; |
drh | 113762a | 2014-11-19 16:36:25 +0000 | [diff] [blame] | 2578 | /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins |
| 2579 | ** with the following 16 bytes (in hex): 53 51 4c 69 74 65 20 66 6f 72 6d |
| 2580 | ** 61 74 20 33 00. */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2581 | if( memcmp(page1, zMagicHeader, 16)!=0 ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2582 | goto page1_init_failed; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 2583 | } |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 2584 | |
| 2585 | #ifdef SQLITE_OMIT_WAL |
| 2586 | if( page1[18]>1 ){ |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2587 | pBt->btsFlags |= BTS_READ_ONLY; |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 2588 | } |
| 2589 | if( page1[19]>1 ){ |
| 2590 | goto page1_init_failed; |
| 2591 | } |
| 2592 | #else |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 2593 | if( page1[18]>2 ){ |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2594 | pBt->btsFlags |= BTS_READ_ONLY; |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 2595 | } |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 2596 | if( page1[19]>2 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2597 | goto page1_init_failed; |
| 2598 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 2599 | |
dan | a470aeb | 2010-04-21 11:43:38 +0000 | [diff] [blame] | 2600 | /* If the write version is set to 2, this database should be accessed |
| 2601 | ** in WAL mode. If the log is not already open, open it now. Then |
| 2602 | ** return SQLITE_OK and return without populating BtShared.pPage1. |
| 2603 | ** The caller detects this and calls this function again. This is |
| 2604 | ** required as the version of page 1 currently in the page1 buffer |
| 2605 | ** may not be the latest version - there may be a newer one in the log |
| 2606 | ** file. |
| 2607 | */ |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2608 | if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){ |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 2609 | int isOpen = 0; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 2610 | rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen); |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 2611 | if( rc!=SQLITE_OK ){ |
| 2612 | goto page1_init_failed; |
| 2613 | }else if( isOpen==0 ){ |
| 2614 | releasePage(pPage1); |
| 2615 | return SQLITE_OK; |
| 2616 | } |
dan | 8b5444b | 2010-04-27 14:37:47 +0000 | [diff] [blame] | 2617 | rc = SQLITE_NOTADB; |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 2618 | } |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 2619 | #endif |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 2620 | |
drh | 113762a | 2014-11-19 16:36:25 +0000 | [diff] [blame] | 2621 | /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload |
| 2622 | ** fractions and the leaf payload fraction values must be 64, 32, and 32. |
| 2623 | ** |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 2624 | ** The original design allowed these amounts to vary, but as of |
| 2625 | ** version 3.6.0, we require them to be fixed. |
| 2626 | */ |
| 2627 | if( memcmp(&page1[21], "\100\040\040",3)!=0 ){ |
| 2628 | goto page1_init_failed; |
| 2629 | } |
drh | 113762a | 2014-11-19 16:36:25 +0000 | [diff] [blame] | 2630 | /* EVIDENCE-OF: R-51873-39618 The page size for a database file is |
| 2631 | ** determined by the 2-byte integer located at an offset of 16 bytes from |
| 2632 | ** the beginning of the database file. */ |
drh | b2eced5 | 2010-08-12 02:41:12 +0000 | [diff] [blame] | 2633 | pageSize = (page1[16]<<8) | (page1[17]<<16); |
drh | 113762a | 2014-11-19 16:36:25 +0000 | [diff] [blame] | 2634 | /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two |
| 2635 | ** between 512 and 65536 inclusive. */ |
drh | b2eced5 | 2010-08-12 02:41:12 +0000 | [diff] [blame] | 2636 | if( ((pageSize-1)&pageSize)!=0 |
| 2637 | || pageSize>SQLITE_MAX_PAGE_SIZE |
| 2638 | || pageSize<=256 |
drh | 7dc385e | 2007-09-06 23:39:36 +0000 | [diff] [blame] | 2639 | ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 2640 | goto page1_init_failed; |
| 2641 | } |
| 2642 | assert( (pageSize & 7)==0 ); |
drh | 113762a | 2014-11-19 16:36:25 +0000 | [diff] [blame] | 2643 | /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte |
| 2644 | ** integer at offset 20 is the number of bytes of space at the end of |
| 2645 | ** each page to reserve for extensions. |
| 2646 | ** |
| 2647 | ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is |
| 2648 | ** determined by the one-byte unsigned integer found at an offset of 20 |
| 2649 | ** into the database file header. */ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 2650 | usableSize = pageSize - page1[20]; |
shaneh | 1df2db7 | 2010-08-18 02:28:48 +0000 | [diff] [blame] | 2651 | if( (u32)pageSize!=pBt->pageSize ){ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 2652 | /* After reading the first page of the database assuming a page size |
| 2653 | ** of BtShared.pageSize, we have discovered that the page-size is |
| 2654 | ** actually pageSize. Unlock the database, leave pBt->pPage1 at |
| 2655 | ** zero and return SQLITE_OK. The caller will call this function |
| 2656 | ** again with the correct page-size. |
| 2657 | */ |
| 2658 | releasePage(pPage1); |
drh | 43b18e1 | 2010-08-17 19:40:08 +0000 | [diff] [blame] | 2659 | pBt->usableSize = usableSize; |
| 2660 | pBt->pageSize = pageSize; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 2661 | freeTempSpace(pBt); |
drh | fa9601a | 2009-06-18 17:22:39 +0000 | [diff] [blame] | 2662 | rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, |
| 2663 | pageSize-usableSize); |
drh | 5e48393 | 2009-07-10 16:51:30 +0000 | [diff] [blame] | 2664 | return rc; |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 2665 | } |
dan | ecac670 | 2011-02-09 18:19:20 +0000 | [diff] [blame] | 2666 | if( (pBt->db->flags & SQLITE_RecoveryMode)==0 && nPage>nPageFile ){ |
drh | c2a4bab | 2010-04-02 12:46:45 +0000 | [diff] [blame] | 2667 | rc = SQLITE_CORRUPT_BKPT; |
| 2668 | goto page1_init_failed; |
| 2669 | } |
drh | 113762a | 2014-11-19 16:36:25 +0000 | [diff] [blame] | 2670 | /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to |
| 2671 | ** be less than 480. In other words, if the page size is 512, then the |
| 2672 | ** reserved space size cannot exceed 32. */ |
drh | b33e1b9 | 2009-06-18 11:29:20 +0000 | [diff] [blame] | 2673 | if( usableSize<480 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2674 | goto page1_init_failed; |
| 2675 | } |
drh | 43b18e1 | 2010-08-17 19:40:08 +0000 | [diff] [blame] | 2676 | pBt->pageSize = pageSize; |
| 2677 | pBt->usableSize = usableSize; |
drh | 057cd3a | 2005-02-15 16:23:02 +0000 | [diff] [blame] | 2678 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2679 | pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0); |
danielk1977 | 27b1f95 | 2007-06-25 08:16:58 +0000 | [diff] [blame] | 2680 | pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0); |
drh | 057cd3a | 2005-02-15 16:23:02 +0000 | [diff] [blame] | 2681 | #endif |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 2682 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2683 | |
| 2684 | /* maxLocal is the maximum amount of payload to store locally for |
| 2685 | ** a cell. Make sure it is small enough so that at least minFanout |
| 2686 | ** cells can will fit on one page. We assume a 10-byte page header. |
| 2687 | ** Besides the payload, the cell must store: |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2688 | ** 2-byte pointer to the cell |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2689 | ** 4-byte child pointer |
| 2690 | ** 9-byte nKey value |
| 2691 | ** 4-byte nData value |
| 2692 | ** 4-byte overflow page pointer |
drh | e22e03e | 2010-08-18 21:19:03 +0000 | [diff] [blame] | 2693 | ** So a cell consists of a 2-byte pointer, a header which is as much as |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2694 | ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow |
| 2695 | ** page pointer. |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2696 | */ |
shaneh | 1df2db7 | 2010-08-18 02:28:48 +0000 | [diff] [blame] | 2697 | pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23); |
| 2698 | pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23); |
| 2699 | pBt->maxLeaf = (u16)(pBt->usableSize - 35); |
| 2700 | pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23); |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2701 | if( pBt->maxLocal>127 ){ |
| 2702 | pBt->max1bytePayload = 127; |
| 2703 | }else{ |
mistachkin | 0547e2f | 2012-01-08 00:54:02 +0000 | [diff] [blame] | 2704 | pBt->max1bytePayload = (u8)pBt->maxLocal; |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2705 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 2706 | assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2707 | pBt->pPage1 = pPage1; |
drh | dd3cd97 | 2010-03-27 17:12:36 +0000 | [diff] [blame] | 2708 | pBt->nPage = nPage; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2709 | return SQLITE_OK; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 2710 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2711 | page1_init_failed: |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2712 | releasePage(pPage1); |
| 2713 | pBt->pPage1 = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2714 | return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 2715 | } |
| 2716 | |
drh | 85ec3b6 | 2013-05-14 23:12:06 +0000 | [diff] [blame] | 2717 | #ifndef NDEBUG |
| 2718 | /* |
| 2719 | ** Return the number of cursors open on pBt. This is for use |
| 2720 | ** in assert() expressions, so it is only compiled if NDEBUG is not |
| 2721 | ** defined. |
| 2722 | ** |
| 2723 | ** Only write cursors are counted if wrOnly is true. If wrOnly is |
| 2724 | ** false then all cursors are counted. |
| 2725 | ** |
| 2726 | ** For the purposes of this routine, a cursor is any cursor that |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 2727 | ** is capable of reading or writing to the database. Cursors that |
drh | 85ec3b6 | 2013-05-14 23:12:06 +0000 | [diff] [blame] | 2728 | ** have been tripped into the CURSOR_FAULT state are not counted. |
| 2729 | */ |
| 2730 | static int countValidCursors(BtShared *pBt, int wrOnly){ |
| 2731 | BtCursor *pCur; |
| 2732 | int r = 0; |
| 2733 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 2734 | if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0) |
| 2735 | && pCur->eState!=CURSOR_FAULT ) r++; |
drh | 85ec3b6 | 2013-05-14 23:12:06 +0000 | [diff] [blame] | 2736 | } |
| 2737 | return r; |
| 2738 | } |
| 2739 | #endif |
| 2740 | |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 2741 | /* |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 2742 | ** If there are no outstanding cursors and we are not in the middle |
| 2743 | ** of a transaction but there is a read lock on the database, then |
| 2744 | ** this routine unrefs the first page of the database file which |
| 2745 | ** has the effect of releasing the read lock. |
| 2746 | ** |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 2747 | ** If there is a transaction in progress, this routine is a no-op. |
| 2748 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2749 | static void unlockBtreeIfUnused(BtShared *pBt){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2750 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 85ec3b6 | 2013-05-14 23:12:06 +0000 | [diff] [blame] | 2751 | assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE ); |
danielk1977 | 1bc9ee9 | 2009-07-04 15:41:02 +0000 | [diff] [blame] | 2752 | if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){ |
drh | b2325b7 | 2014-09-24 18:31:07 +0000 | [diff] [blame] | 2753 | MemPage *pPage1 = pBt->pPage1; |
| 2754 | assert( pPage1->aData ); |
danielk1977 | c1761e8 | 2009-06-25 09:40:03 +0000 | [diff] [blame] | 2755 | assert( sqlite3PagerRefcount(pBt->pPager)==1 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2756 | pBt->pPage1 = 0; |
drh | b2325b7 | 2014-09-24 18:31:07 +0000 | [diff] [blame] | 2757 | releasePage(pPage1); |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 2758 | } |
| 2759 | } |
| 2760 | |
| 2761 | /* |
drh | e39f2f9 | 2009-07-23 01:43:59 +0000 | [diff] [blame] | 2762 | ** If pBt points to an empty file then convert that empty file |
| 2763 | ** into a new empty database by initializing the first page of |
| 2764 | ** the database. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2765 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2766 | static int newDatabase(BtShared *pBt){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 2767 | MemPage *pP1; |
| 2768 | unsigned char *data; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2769 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2770 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2771 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | dd3cd97 | 2010-03-27 17:12:36 +0000 | [diff] [blame] | 2772 | if( pBt->nPage>0 ){ |
| 2773 | return SQLITE_OK; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2774 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2775 | pP1 = pBt->pPage1; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 2776 | assert( pP1!=0 ); |
| 2777 | data = pP1->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2778 | rc = sqlite3PagerWrite(pP1->pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2779 | if( rc ) return rc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 2780 | memcpy(data, zMagicHeader, sizeof(zMagicHeader)); |
| 2781 | assert( sizeof(zMagicHeader)==16 ); |
shaneh | 1df2db7 | 2010-08-18 02:28:48 +0000 | [diff] [blame] | 2782 | data[16] = (u8)((pBt->pageSize>>8)&0xff); |
| 2783 | data[17] = (u8)((pBt->pageSize>>16)&0xff); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 2784 | data[18] = 1; |
| 2785 | data[19] = 1; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 2786 | assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize); |
| 2787 | data[20] = (u8)(pBt->pageSize - pBt->usableSize); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 2788 | data[21] = 64; |
| 2789 | data[22] = 32; |
| 2790 | data[23] = 32; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2791 | memset(&data[24], 0, 100-24); |
drh | e6c4381 | 2004-05-14 12:17:46 +0000 | [diff] [blame] | 2792 | zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA ); |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2793 | pBt->btsFlags |= BTS_PAGESIZE_FIXED; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2794 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2795 | assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 ); |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 2796 | assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 ); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2797 | put4byte(&data[36 + 4*4], pBt->autoVacuum); |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 2798 | put4byte(&data[36 + 7*4], pBt->incrVacuum); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2799 | #endif |
drh | dd3cd97 | 2010-03-27 17:12:36 +0000 | [diff] [blame] | 2800 | pBt->nPage = 1; |
| 2801 | data[31] = 1; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2802 | return SQLITE_OK; |
| 2803 | } |
| 2804 | |
| 2805 | /* |
dan | b483eba | 2012-10-13 19:58:11 +0000 | [diff] [blame] | 2806 | ** Initialize the first page of the database file (creating a database |
| 2807 | ** consisting of a single page and no schema objects). Return SQLITE_OK |
| 2808 | ** if successful, or an SQLite error code otherwise. |
| 2809 | */ |
| 2810 | int sqlite3BtreeNewDb(Btree *p){ |
| 2811 | int rc; |
| 2812 | sqlite3BtreeEnter(p); |
| 2813 | p->pBt->nPage = 0; |
| 2814 | rc = newDatabase(p->pBt); |
| 2815 | sqlite3BtreeLeave(p); |
| 2816 | return rc; |
| 2817 | } |
| 2818 | |
| 2819 | /* |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2820 | ** Attempt to start a new transaction. A write-transaction |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 2821 | ** is started if the second argument is nonzero, otherwise a read- |
| 2822 | ** transaction. If the second argument is 2 or more and exclusive |
| 2823 | ** transaction is started, meaning that no other process is allowed |
| 2824 | ** to access the database. A preexisting transaction may not be |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2825 | ** upgraded to exclusive by calling this routine a second time - the |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 2826 | ** exclusivity flag only works for a new transaction. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2827 | ** |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2828 | ** A write-transaction must be started before attempting any |
| 2829 | ** changes to the database. None of the following routines |
| 2830 | ** will work unless a transaction is started first: |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2831 | ** |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 2832 | ** sqlite3BtreeCreateTable() |
| 2833 | ** sqlite3BtreeCreateIndex() |
| 2834 | ** sqlite3BtreeClearTable() |
| 2835 | ** sqlite3BtreeDropTable() |
| 2836 | ** sqlite3BtreeInsert() |
| 2837 | ** sqlite3BtreeDelete() |
| 2838 | ** sqlite3BtreeUpdateMeta() |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 2839 | ** |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2840 | ** If an initial attempt to acquire the lock fails because of lock contention |
| 2841 | ** and the database was previously unlocked, then invoke the busy handler |
| 2842 | ** if there is one. But if there was previously a read-lock, do not |
| 2843 | ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is |
| 2844 | ** returned when there is already a read-lock in order to avoid a deadlock. |
| 2845 | ** |
| 2846 | ** Suppose there are two processes A and B. A has a read lock and B has |
| 2847 | ** a reserved lock. B tries to promote to exclusive but is blocked because |
| 2848 | ** of A's read lock. A tries to promote to reserved but is blocked by B. |
| 2849 | ** One or the other of the two processes must give way or there can be |
| 2850 | ** no progress. By returning SQLITE_BUSY and not invoking the busy callback |
| 2851 | ** when A already has a read lock, we encourage A to give up and let B |
| 2852 | ** proceed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2853 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2854 | int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2855 | sqlite3 *pBlock = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2856 | BtShared *pBt = p->pBt; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2857 | int rc = SQLITE_OK; |
| 2858 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2859 | sqlite3BtreeEnter(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2860 | btreeIntegrity(p); |
| 2861 | |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2862 | /* If the btree is already in a write-transaction, or it |
| 2863 | ** is already in a read-transaction and a read-transaction |
| 2864 | ** is requested, this is a no-op. |
| 2865 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2866 | if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2867 | goto trans_begun; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2868 | } |
dan | 56c517a | 2013-09-26 11:04:33 +0000 | [diff] [blame] | 2869 | assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 ); |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2870 | |
| 2871 | /* Write transactions are not possible on a read-only database */ |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2872 | if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2873 | rc = SQLITE_READONLY; |
| 2874 | goto trans_begun; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2875 | } |
| 2876 | |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2877 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2878 | /* If another database handle has already opened a write transaction |
| 2879 | ** on this shared-btree structure and a second write transaction is |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2880 | ** requested, return SQLITE_LOCKED. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2881 | */ |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2882 | if( (wrflag && pBt->inTransaction==TRANS_WRITE) |
| 2883 | || (pBt->btsFlags & BTS_PENDING)!=0 |
| 2884 | ){ |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2885 | pBlock = pBt->pWriter->db; |
| 2886 | }else if( wrflag>1 ){ |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 2887 | BtLock *pIter; |
| 2888 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 2889 | if( pIter->pBtree!=p ){ |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2890 | pBlock = pIter->pBtree->db; |
| 2891 | break; |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 2892 | } |
| 2893 | } |
| 2894 | } |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2895 | if( pBlock ){ |
| 2896 | sqlite3ConnectionBlocked(p->db, pBlock); |
| 2897 | rc = SQLITE_LOCKED_SHAREDCACHE; |
| 2898 | goto trans_begun; |
| 2899 | } |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 2900 | #endif |
| 2901 | |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 2902 | /* Any read-only or read-write transaction implies a read-lock on |
| 2903 | ** page 1. So if some other shared-cache client already has a write-lock |
| 2904 | ** on page 1, the transaction cannot be opened. */ |
drh | 4c301aa | 2009-07-15 17:25:45 +0000 | [diff] [blame] | 2905 | rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK); |
| 2906 | if( SQLITE_OK!=rc ) goto trans_begun; |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 2907 | |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2908 | pBt->btsFlags &= ~BTS_INITIALLY_EMPTY; |
| 2909 | if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY; |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2910 | do { |
danielk1977 | 295dc10 | 2009-04-01 19:07:03 +0000 | [diff] [blame] | 2911 | /* Call lockBtree() until either pBt->pPage1 is populated or |
| 2912 | ** lockBtree() returns something other than SQLITE_OK. lockBtree() |
| 2913 | ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after |
| 2914 | ** reading page 1 it discovers that the page-size of the database |
| 2915 | ** file is not pBt->pageSize. In this case lockBtree() will update |
| 2916 | ** pBt->pageSize to the page-size of the file on disk. |
| 2917 | */ |
| 2918 | while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) ); |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 2919 | |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2920 | if( rc==SQLITE_OK && wrflag ){ |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2921 | if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){ |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 2922 | rc = SQLITE_READONLY; |
| 2923 | }else{ |
danielk1977 | d829335 | 2009-04-30 09:10:37 +0000 | [diff] [blame] | 2924 | rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db)); |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 2925 | if( rc==SQLITE_OK ){ |
| 2926 | rc = newDatabase(pBt); |
| 2927 | } |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2928 | } |
| 2929 | } |
| 2930 | |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 2931 | if( rc!=SQLITE_OK ){ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2932 | unlockBtreeIfUnused(pBt); |
| 2933 | } |
dan | f9b7671 | 2010-06-01 14:12:45 +0000 | [diff] [blame] | 2934 | }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE && |
danielk1977 | 1ceedd3 | 2008-11-19 10:22:33 +0000 | [diff] [blame] | 2935 | btreeInvokeBusyHandler(pBt) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2936 | |
| 2937 | if( rc==SQLITE_OK ){ |
| 2938 | if( p->inTrans==TRANS_NONE ){ |
| 2939 | pBt->nTransaction++; |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 2940 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 2941 | if( p->sharable ){ |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2942 | assert( p->lock.pBtree==p && p->lock.iTable==1 ); |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 2943 | p->lock.eLock = READ_LOCK; |
| 2944 | p->lock.pNext = pBt->pLock; |
| 2945 | pBt->pLock = &p->lock; |
| 2946 | } |
| 2947 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2948 | } |
| 2949 | p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ); |
| 2950 | if( p->inTrans>pBt->inTransaction ){ |
| 2951 | pBt->inTransaction = p->inTrans; |
| 2952 | } |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2953 | if( wrflag ){ |
dan | 59257dc | 2010-08-04 11:34:31 +0000 | [diff] [blame] | 2954 | MemPage *pPage1 = pBt->pPage1; |
| 2955 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2956 | assert( !pBt->pWriter ); |
| 2957 | pBt->pWriter = p; |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2958 | pBt->btsFlags &= ~BTS_EXCLUSIVE; |
| 2959 | if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE; |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 2960 | #endif |
dan | 59257dc | 2010-08-04 11:34:31 +0000 | [diff] [blame] | 2961 | |
| 2962 | /* If the db-size header field is incorrect (as it may be if an old |
| 2963 | ** client has been writing the database file), update it now. Doing |
| 2964 | ** this sooner rather than later means the database size can safely |
| 2965 | ** re-read the database size from page 1 if a savepoint or transaction |
| 2966 | ** rollback occurs within the transaction. |
| 2967 | */ |
| 2968 | if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){ |
| 2969 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
| 2970 | if( rc==SQLITE_OK ){ |
| 2971 | put4byte(&pPage1->aData[28], pBt->nPage); |
| 2972 | } |
| 2973 | } |
| 2974 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2975 | } |
| 2976 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2977 | |
| 2978 | trans_begun: |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2979 | if( rc==SQLITE_OK && wrflag ){ |
danielk1977 | 12dd549 | 2008-12-18 15:45:07 +0000 | [diff] [blame] | 2980 | /* This call makes sure that the pager has the correct number of |
| 2981 | ** open savepoints. If the second parameter is greater than 0 and |
| 2982 | ** the sub-journal is not already open, then it will be opened here. |
| 2983 | */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2984 | rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint); |
| 2985 | } |
danielk1977 | 12dd549 | 2008-12-18 15:45:07 +0000 | [diff] [blame] | 2986 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2987 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2988 | sqlite3BtreeLeave(p); |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 2989 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2990 | } |
| 2991 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2992 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2993 | |
| 2994 | /* |
| 2995 | ** Set the pointer-map entries for all children of page pPage. Also, if |
| 2996 | ** pPage contains cells that point to overflow pages, set the pointer |
| 2997 | ** map entries for the overflow pages as well. |
| 2998 | */ |
| 2999 | static int setChildPtrmaps(MemPage *pPage){ |
| 3000 | int i; /* Counter variable */ |
| 3001 | int nCell; /* Number of cells in page pPage */ |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 3002 | int rc; /* Return code */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3003 | BtShared *pBt = pPage->pBt; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3004 | u8 isInitOrig = pPage->isInit; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3005 | Pgno pgno = pPage->pgno; |
| 3006 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3007 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 3008 | rc = btreeInitPage(pPage); |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 3009 | if( rc!=SQLITE_OK ){ |
| 3010 | goto set_child_ptrmaps_out; |
| 3011 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3012 | nCell = pPage->nCell; |
| 3013 | |
| 3014 | for(i=0; i<nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3015 | u8 *pCell = findCell(pPage, i); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3016 | |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 3017 | ptrmapPutOvflPtr(pPage, pCell, &rc); |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 3018 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3019 | if( !pPage->leaf ){ |
| 3020 | Pgno childPgno = get4byte(pCell); |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 3021 | ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3022 | } |
| 3023 | } |
| 3024 | |
| 3025 | if( !pPage->leaf ){ |
| 3026 | Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 3027 | ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3028 | } |
| 3029 | |
| 3030 | set_child_ptrmaps_out: |
| 3031 | pPage->isInit = isInitOrig; |
| 3032 | return rc; |
| 3033 | } |
| 3034 | |
| 3035 | /* |
drh | f3aed59 | 2009-07-08 18:12:49 +0000 | [diff] [blame] | 3036 | ** Somewhere on pPage is a pointer to page iFrom. Modify this pointer so |
| 3037 | ** that it points to iTo. Parameter eType describes the type of pointer to |
| 3038 | ** be modified, as follows: |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3039 | ** |
| 3040 | ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child |
| 3041 | ** page of pPage. |
| 3042 | ** |
| 3043 | ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow |
| 3044 | ** page pointed to by one of the cells on pPage. |
| 3045 | ** |
| 3046 | ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next |
| 3047 | ** overflow page in the list. |
| 3048 | */ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 3049 | static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3050 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 3051 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3052 | if( eType==PTRMAP_OVERFLOW2 ){ |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 3053 | /* 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] | 3054 | if( get4byte(pPage->aData)!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3055 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 3056 | } |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 3057 | put4byte(pPage->aData, iTo); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3058 | }else{ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3059 | u8 isInitOrig = pPage->isInit; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3060 | int i; |
| 3061 | int nCell; |
| 3062 | |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 3063 | btreeInitPage(pPage); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3064 | nCell = pPage->nCell; |
| 3065 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3066 | for(i=0; i<nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3067 | u8 *pCell = findCell(pPage, i); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3068 | if( eType==PTRMAP_OVERFLOW1 ){ |
| 3069 | CellInfo info; |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 3070 | btreeParseCellPtr(pPage, pCell, &info); |
drh | e42a9b4 | 2011-08-31 13:27:19 +0000 | [diff] [blame] | 3071 | if( info.iOverflow |
| 3072 | && pCell+info.iOverflow+3<=pPage->aData+pPage->maskPage |
| 3073 | && iFrom==get4byte(&pCell[info.iOverflow]) |
| 3074 | ){ |
| 3075 | put4byte(&pCell[info.iOverflow], iTo); |
| 3076 | break; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3077 | } |
| 3078 | }else{ |
| 3079 | if( get4byte(pCell)==iFrom ){ |
| 3080 | put4byte(pCell, iTo); |
| 3081 | break; |
| 3082 | } |
| 3083 | } |
| 3084 | } |
| 3085 | |
| 3086 | if( i==nCell ){ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 3087 | if( eType!=PTRMAP_BTREE || |
| 3088 | get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3089 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 3090 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3091 | put4byte(&pPage->aData[pPage->hdrOffset+8], iTo); |
| 3092 | } |
| 3093 | |
| 3094 | pPage->isInit = isInitOrig; |
| 3095 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 3096 | return SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3097 | } |
| 3098 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 3099 | |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 3100 | /* |
| 3101 | ** Move the open database page pDbPage to location iFreePage in the |
| 3102 | ** database. The pDbPage reference remains valid. |
drh | e64ca7b | 2009-07-16 18:21:17 +0000 | [diff] [blame] | 3103 | ** |
| 3104 | ** The isCommit flag indicates that there is no need to remember that |
| 3105 | ** the journal needs to be sync()ed before database page pDbPage->pgno |
| 3106 | ** can be written to. The caller has already promised not to write to that |
| 3107 | ** page. |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 3108 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 3109 | static int relocatePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3110 | BtShared *pBt, /* Btree */ |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 3111 | MemPage *pDbPage, /* Open page to move */ |
| 3112 | u8 eType, /* Pointer map 'type' entry for pDbPage */ |
| 3113 | Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */ |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 3114 | Pgno iFreePage, /* The location to move pDbPage to */ |
drh | e64ca7b | 2009-07-16 18:21:17 +0000 | [diff] [blame] | 3115 | int isCommit /* isCommit flag passed to sqlite3PagerMovepage */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 3116 | ){ |
| 3117 | MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */ |
| 3118 | Pgno iDbPage = pDbPage->pgno; |
| 3119 | Pager *pPager = pBt->pPager; |
| 3120 | int rc; |
| 3121 | |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3122 | assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || |
| 3123 | eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3124 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 3125 | assert( pDbPage->pBt==pBt ); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 3126 | |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 3127 | /* Move page iDbPage from its current location to page number iFreePage */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 3128 | TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", |
| 3129 | iDbPage, iFreePage, iPtrPage, eType)); |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 3130 | rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 3131 | if( rc!=SQLITE_OK ){ |
| 3132 | return rc; |
| 3133 | } |
| 3134 | pDbPage->pgno = iFreePage; |
| 3135 | |
| 3136 | /* If pDbPage was a btree-page, then it may have child pages and/or cells |
| 3137 | ** that point to overflow pages. The pointer map entries for all these |
| 3138 | ** pages need to be changed. |
| 3139 | ** |
| 3140 | ** If pDbPage is an overflow page, then the first 4 bytes may store a |
| 3141 | ** pointer to a subsequent overflow page. If this is the case, then |
| 3142 | ** the pointer map needs to be updated for the subsequent overflow page. |
| 3143 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3144 | if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 3145 | rc = setChildPtrmaps(pDbPage); |
| 3146 | if( rc!=SQLITE_OK ){ |
| 3147 | return rc; |
| 3148 | } |
| 3149 | }else{ |
| 3150 | Pgno nextOvfl = get4byte(pDbPage->aData); |
| 3151 | if( nextOvfl!=0 ){ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 3152 | ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage, &rc); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 3153 | if( rc!=SQLITE_OK ){ |
| 3154 | return rc; |
| 3155 | } |
| 3156 | } |
| 3157 | } |
| 3158 | |
| 3159 | /* Fix the database pointer on page iPtrPage that pointed at iDbPage so |
| 3160 | ** that it points at iFreePage. Also fix the pointer map entry for |
| 3161 | ** iPtrPage. |
| 3162 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3163 | if( eType!=PTRMAP_ROOTPAGE ){ |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 3164 | rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3165 | if( rc!=SQLITE_OK ){ |
| 3166 | return rc; |
| 3167 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3168 | rc = sqlite3PagerWrite(pPtrPage->pDbPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3169 | if( rc!=SQLITE_OK ){ |
| 3170 | releasePage(pPtrPage); |
| 3171 | return rc; |
| 3172 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 3173 | rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 3174 | releasePage(pPtrPage); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 3175 | if( rc==SQLITE_OK ){ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 3176 | ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 3177 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 3178 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 3179 | return rc; |
| 3180 | } |
| 3181 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3182 | /* Forward declaration required by incrVacuumStep(). */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 3183 | static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3184 | |
| 3185 | /* |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3186 | ** Perform a single step of an incremental-vacuum. If successful, return |
| 3187 | ** SQLITE_OK. If there is no work to do (and therefore no point in |
| 3188 | ** calling this function again), return SQLITE_DONE. Or, if an error |
| 3189 | ** occurs, return some other error code. |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3190 | ** |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 3191 | ** More specifically, this function attempts to re-organize the database so |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3192 | ** that the last page of the file currently in use is no longer in use. |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3193 | ** |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3194 | ** Parameter nFin is the number of pages that this database would contain |
| 3195 | ** were this function called until it returns SQLITE_DONE. |
| 3196 | ** |
| 3197 | ** If the bCommit parameter is non-zero, this function assumes that the |
| 3198 | ** caller will keep calling incrVacuumStep() until it returns SQLITE_DONE |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 3199 | ** or an error. bCommit is passed true for an auto-vacuum-on-commit |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3200 | ** operation, or false for an incremental vacuum. |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3201 | */ |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3202 | static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg, int bCommit){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3203 | Pgno nFreeList; /* Number of pages still on the free-list */ |
drh | dd3cd97 | 2010-03-27 17:12:36 +0000 | [diff] [blame] | 3204 | int rc; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3205 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3206 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | fa542f1 | 2009-04-02 18:28:08 +0000 | [diff] [blame] | 3207 | assert( iLastPg>nFin ); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3208 | |
| 3209 | if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3210 | u8 eType; |
| 3211 | Pgno iPtrPage; |
| 3212 | |
| 3213 | nFreeList = get4byte(&pBt->pPage1->aData[36]); |
danielk1977 | fa542f1 | 2009-04-02 18:28:08 +0000 | [diff] [blame] | 3214 | if( nFreeList==0 ){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3215 | return SQLITE_DONE; |
| 3216 | } |
| 3217 | |
| 3218 | rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage); |
| 3219 | if( rc!=SQLITE_OK ){ |
| 3220 | return rc; |
| 3221 | } |
| 3222 | if( eType==PTRMAP_ROOTPAGE ){ |
| 3223 | return SQLITE_CORRUPT_BKPT; |
| 3224 | } |
| 3225 | |
| 3226 | if( eType==PTRMAP_FREEPAGE ){ |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3227 | if( bCommit==0 ){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3228 | /* Remove the page from the files free-list. This is not required |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3229 | ** if bCommit is non-zero. In that case, the free-list will be |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3230 | ** truncated to zero after this function returns, so it doesn't |
| 3231 | ** matter if it still contains some garbage entries. |
| 3232 | */ |
| 3233 | Pgno iFreePg; |
| 3234 | MemPage *pFreePg; |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3235 | rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, BTALLOC_EXACT); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3236 | if( rc!=SQLITE_OK ){ |
| 3237 | return rc; |
| 3238 | } |
| 3239 | assert( iFreePg==iLastPg ); |
| 3240 | releasePage(pFreePg); |
| 3241 | } |
| 3242 | } else { |
| 3243 | Pgno iFreePg; /* Index of free page to move pLastPg to */ |
| 3244 | MemPage *pLastPg; |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3245 | u8 eMode = BTALLOC_ANY; /* Mode parameter for allocateBtreePage() */ |
| 3246 | Pgno iNear = 0; /* nearby parameter for allocateBtreePage() */ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3247 | |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 3248 | rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3249 | if( rc!=SQLITE_OK ){ |
| 3250 | return rc; |
| 3251 | } |
| 3252 | |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3253 | /* If bCommit is zero, this loop runs exactly once and page pLastPg |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 3254 | ** is swapped with the first free page pulled off the free list. |
| 3255 | ** |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3256 | ** On the other hand, if bCommit is greater than zero, then keep |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 3257 | ** looping until a free-page located within the first nFin pages |
| 3258 | ** of the file is found. |
| 3259 | */ |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3260 | if( bCommit==0 ){ |
| 3261 | eMode = BTALLOC_LE; |
| 3262 | iNear = nFin; |
| 3263 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3264 | do { |
| 3265 | MemPage *pFreePg; |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3266 | rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iNear, eMode); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3267 | if( rc!=SQLITE_OK ){ |
| 3268 | releasePage(pLastPg); |
| 3269 | return rc; |
| 3270 | } |
| 3271 | releasePage(pFreePg); |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3272 | }while( bCommit && iFreePg>nFin ); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3273 | assert( iFreePg<iLastPg ); |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 3274 | |
dan | e1df4e3 | 2013-03-05 11:27:04 +0000 | [diff] [blame] | 3275 | rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, bCommit); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3276 | releasePage(pLastPg); |
| 3277 | if( rc!=SQLITE_OK ){ |
| 3278 | return rc; |
danielk1977 | 662278e | 2007-11-05 15:30:12 +0000 | [diff] [blame] | 3279 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3280 | } |
| 3281 | } |
| 3282 | |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3283 | if( bCommit==0 ){ |
dan | bc1a3c6 | 2013-02-23 16:40:46 +0000 | [diff] [blame] | 3284 | do { |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3285 | iLastPg--; |
dan | bc1a3c6 | 2013-02-23 16:40:46 +0000 | [diff] [blame] | 3286 | }while( iLastPg==PENDING_BYTE_PAGE(pBt) || PTRMAP_ISPAGE(pBt, iLastPg) ); |
| 3287 | pBt->bDoTruncate = 1; |
drh | dd3cd97 | 2010-03-27 17:12:36 +0000 | [diff] [blame] | 3288 | pBt->nPage = iLastPg; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3289 | } |
| 3290 | return SQLITE_OK; |
| 3291 | } |
| 3292 | |
| 3293 | /* |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3294 | ** The database opened by the first argument is an auto-vacuum database |
| 3295 | ** nOrig pages in size containing nFree free pages. Return the expected |
| 3296 | ** size of the database in pages following an auto-vacuum operation. |
| 3297 | */ |
| 3298 | static Pgno finalDbSize(BtShared *pBt, Pgno nOrig, Pgno nFree){ |
| 3299 | int nEntry; /* Number of entries on one ptrmap page */ |
| 3300 | Pgno nPtrmap; /* Number of PtrMap pages to be freed */ |
| 3301 | Pgno nFin; /* Return value */ |
| 3302 | |
| 3303 | nEntry = pBt->usableSize/5; |
| 3304 | nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry; |
| 3305 | nFin = nOrig - nFree - nPtrmap; |
| 3306 | if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){ |
| 3307 | nFin--; |
| 3308 | } |
| 3309 | while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){ |
| 3310 | nFin--; |
| 3311 | } |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3312 | |
| 3313 | return nFin; |
| 3314 | } |
| 3315 | |
| 3316 | /* |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3317 | ** A write-transaction must be opened before calling this function. |
| 3318 | ** It performs a single unit of work towards an incremental vacuum. |
| 3319 | ** |
| 3320 | ** If the incremental vacuum is finished after this function has run, |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 3321 | ** SQLITE_DONE is returned. If it is not finished, but no error occurred, |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3322 | ** SQLITE_OK is returned. Otherwise an SQLite error code. |
| 3323 | */ |
| 3324 | int sqlite3BtreeIncrVacuum(Btree *p){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3325 | int rc; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3326 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3327 | |
| 3328 | sqlite3BtreeEnter(p); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3329 | assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE ); |
| 3330 | if( !pBt->autoVacuum ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3331 | rc = SQLITE_DONE; |
| 3332 | }else{ |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3333 | Pgno nOrig = btreePagecount(pBt); |
| 3334 | Pgno nFree = get4byte(&pBt->pPage1->aData[36]); |
| 3335 | Pgno nFin = finalDbSize(pBt, nOrig, nFree); |
| 3336 | |
dan | 9138471 | 2013-02-24 11:50:43 +0000 | [diff] [blame] | 3337 | if( nOrig<nFin ){ |
| 3338 | rc = SQLITE_CORRUPT_BKPT; |
| 3339 | }else if( nFree>0 ){ |
dan | 11dcd11 | 2013-03-15 18:29:18 +0000 | [diff] [blame] | 3340 | rc = saveAllCursors(pBt, 0, 0); |
| 3341 | if( rc==SQLITE_OK ){ |
| 3342 | invalidateAllOverflowCache(pBt); |
| 3343 | rc = incrVacuumStep(pBt, nFin, nOrig, 0); |
| 3344 | } |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3345 | if( rc==SQLITE_OK ){ |
| 3346 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 3347 | put4byte(&pBt->pPage1->aData[28], pBt->nPage); |
| 3348 | } |
| 3349 | }else{ |
| 3350 | rc = SQLITE_DONE; |
drh | dd3cd97 | 2010-03-27 17:12:36 +0000 | [diff] [blame] | 3351 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3352 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3353 | sqlite3BtreeLeave(p); |
| 3354 | return rc; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3355 | } |
| 3356 | |
| 3357 | /* |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3358 | ** This routine is called prior to sqlite3PagerCommit when a transaction |
drh | f7b5496 | 2013-05-28 12:11:54 +0000 | [diff] [blame] | 3359 | ** is committed for an auto-vacuum database. |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3360 | ** |
| 3361 | ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages |
| 3362 | ** the database file should be truncated to during the commit process. |
| 3363 | ** i.e. the database has been reorganized so that only the first *pnTrunc |
| 3364 | ** pages are in use. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3365 | */ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3366 | static int autoVacuumCommit(BtShared *pBt){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3367 | int rc = SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3368 | Pager *pPager = pBt->pPager; |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 3369 | VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3370 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3371 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 3372 | invalidateAllOverflowCache(pBt); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3373 | assert(pBt->autoVacuum); |
| 3374 | if( !pBt->incrVacuum ){ |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 3375 | Pgno nFin; /* Number of pages in database after autovacuuming */ |
| 3376 | Pgno nFree; /* Number of pages on the freelist initially */ |
drh | 41d628c | 2009-07-11 17:04:08 +0000 | [diff] [blame] | 3377 | Pgno iFree; /* The next page to be freed */ |
drh | 41d628c | 2009-07-11 17:04:08 +0000 | [diff] [blame] | 3378 | Pgno nOrig; /* Database size before freeing */ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3379 | |
drh | b129915 | 2010-03-30 22:58:33 +0000 | [diff] [blame] | 3380 | nOrig = btreePagecount(pBt); |
danielk1977 | ef165ce | 2009-04-06 17:50:03 +0000 | [diff] [blame] | 3381 | if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){ |
| 3382 | /* It is not possible to create a database for which the final page |
| 3383 | ** is either a pointer-map page or the pending-byte page. If one |
| 3384 | ** is encountered, this indicates corruption. |
| 3385 | */ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3386 | return SQLITE_CORRUPT_BKPT; |
| 3387 | } |
danielk1977 | ef165ce | 2009-04-06 17:50:03 +0000 | [diff] [blame] | 3388 | |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3389 | nFree = get4byte(&pBt->pPage1->aData[36]); |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3390 | nFin = finalDbSize(pBt, nOrig, nFree); |
drh | c5e47ac | 2009-06-04 00:11:56 +0000 | [diff] [blame] | 3391 | if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT; |
dan | 0aed84d | 2013-03-26 14:16:20 +0000 | [diff] [blame] | 3392 | if( nFin<nOrig ){ |
| 3393 | rc = saveAllCursors(pBt, 0, 0); |
| 3394 | } |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3395 | for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){ |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3396 | rc = incrVacuumStep(pBt, nFin, iFree, 1); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3397 | } |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3398 | if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3399 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 3400 | put4byte(&pBt->pPage1->aData[32], 0); |
| 3401 | put4byte(&pBt->pPage1->aData[36], 0); |
drh | dd3cd97 | 2010-03-27 17:12:36 +0000 | [diff] [blame] | 3402 | put4byte(&pBt->pPage1->aData[28], nFin); |
dan | bc1a3c6 | 2013-02-23 16:40:46 +0000 | [diff] [blame] | 3403 | pBt->bDoTruncate = 1; |
drh | dd3cd97 | 2010-03-27 17:12:36 +0000 | [diff] [blame] | 3404 | pBt->nPage = nFin; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3405 | } |
| 3406 | if( rc!=SQLITE_OK ){ |
| 3407 | sqlite3PagerRollback(pPager); |
| 3408 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3409 | } |
| 3410 | |
dan | 0aed84d | 2013-03-26 14:16:20 +0000 | [diff] [blame] | 3411 | assert( nRef>=sqlite3PagerRefcount(pPager) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3412 | return rc; |
| 3413 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3414 | |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 3415 | #else /* ifndef SQLITE_OMIT_AUTOVACUUM */ |
| 3416 | # define setChildPtrmaps(x) SQLITE_OK |
| 3417 | #endif |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3418 | |
| 3419 | /* |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3420 | ** This routine does the first phase of a two-phase commit. This routine |
| 3421 | ** causes a rollback journal to be created (if it does not already exist) |
| 3422 | ** and populated with enough information so that if a power loss occurs |
| 3423 | ** the database can be restored to its original state by playing back |
| 3424 | ** the journal. Then the contents of the journal are flushed out to |
| 3425 | ** the disk. After the journal is safely on oxide, the changes to the |
| 3426 | ** database are written into the database file and flushed to oxide. |
| 3427 | ** At the end of this call, the rollback journal still exists on the |
| 3428 | ** disk and we are still holding all locks, so the transaction has not |
drh | 51898cf | 2009-04-19 20:51:06 +0000 | [diff] [blame] | 3429 | ** committed. See sqlite3BtreeCommitPhaseTwo() for the second phase of the |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3430 | ** commit process. |
| 3431 | ** |
| 3432 | ** This call is a no-op if no write-transaction is currently active on pBt. |
| 3433 | ** |
| 3434 | ** Otherwise, sync the database file for the btree pBt. zMaster points to |
| 3435 | ** the name of a master journal file that should be written into the |
| 3436 | ** individual journal file, or is NULL, indicating no master journal file |
| 3437 | ** (single database transaction). |
| 3438 | ** |
| 3439 | ** When this is called, the master journal should already have been |
| 3440 | ** created, populated with this journal pointer and synced to disk. |
| 3441 | ** |
| 3442 | ** Once this is routine has returned, the only thing required to commit |
| 3443 | ** the write-transaction for this database file is to delete the journal. |
| 3444 | */ |
| 3445 | int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){ |
| 3446 | int rc = SQLITE_OK; |
| 3447 | if( p->inTrans==TRANS_WRITE ){ |
| 3448 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3449 | sqlite3BtreeEnter(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3450 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3451 | if( pBt->autoVacuum ){ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3452 | rc = autoVacuumCommit(pBt); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3453 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3454 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3455 | return rc; |
| 3456 | } |
| 3457 | } |
dan | bc1a3c6 | 2013-02-23 16:40:46 +0000 | [diff] [blame] | 3458 | if( pBt->bDoTruncate ){ |
| 3459 | sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage); |
| 3460 | } |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3461 | #endif |
drh | 49b9d33 | 2009-01-02 18:10:42 +0000 | [diff] [blame] | 3462 | rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3463 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3464 | } |
| 3465 | return rc; |
| 3466 | } |
| 3467 | |
| 3468 | /* |
danielk1977 | 94b3073 | 2009-07-02 17:21:57 +0000 | [diff] [blame] | 3469 | ** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback() |
| 3470 | ** at the conclusion of a transaction. |
| 3471 | */ |
| 3472 | static void btreeEndTransaction(Btree *p){ |
| 3473 | BtShared *pBt = p->pBt; |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 3474 | sqlite3 *db = p->db; |
danielk1977 | 94b3073 | 2009-07-02 17:21:57 +0000 | [diff] [blame] | 3475 | assert( sqlite3BtreeHoldsMutex(p) ); |
| 3476 | |
dan | bc1a3c6 | 2013-02-23 16:40:46 +0000 | [diff] [blame] | 3477 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3478 | pBt->bDoTruncate = 0; |
| 3479 | #endif |
dan | c0537fe | 2013-06-28 19:41:43 +0000 | [diff] [blame] | 3480 | if( p->inTrans>TRANS_NONE && db->nVdbeRead>1 ){ |
dan | fa401de | 2009-10-16 14:55:03 +0000 | [diff] [blame] | 3481 | /* If there are other active statements that belong to this database |
| 3482 | ** handle, downgrade to a read-only transaction. The other statements |
| 3483 | ** may still be reading from the database. */ |
danielk1977 | 94b3073 | 2009-07-02 17:21:57 +0000 | [diff] [blame] | 3484 | downgradeAllSharedCacheTableLocks(p); |
| 3485 | p->inTrans = TRANS_READ; |
| 3486 | }else{ |
| 3487 | /* If the handle had any kind of transaction open, decrement the |
| 3488 | ** transaction count of the shared btree. If the transaction count |
| 3489 | ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused() |
| 3490 | ** call below will unlock the pager. */ |
| 3491 | if( p->inTrans!=TRANS_NONE ){ |
| 3492 | clearAllSharedCacheTableLocks(p); |
| 3493 | pBt->nTransaction--; |
| 3494 | if( 0==pBt->nTransaction ){ |
| 3495 | pBt->inTransaction = TRANS_NONE; |
| 3496 | } |
| 3497 | } |
| 3498 | |
| 3499 | /* Set the current transaction state to TRANS_NONE and unlock the |
| 3500 | ** pager if this call closed the only read or write transaction. */ |
| 3501 | p->inTrans = TRANS_NONE; |
| 3502 | unlockBtreeIfUnused(pBt); |
| 3503 | } |
| 3504 | |
| 3505 | btreeIntegrity(p); |
| 3506 | } |
| 3507 | |
| 3508 | /* |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3509 | ** Commit the transaction currently in progress. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3510 | ** |
drh | 6e34599 | 2007-03-30 11:12:08 +0000 | [diff] [blame] | 3511 | ** This routine implements the second phase of a 2-phase commit. The |
drh | 51898cf | 2009-04-19 20:51:06 +0000 | [diff] [blame] | 3512 | ** sqlite3BtreeCommitPhaseOne() routine does the first phase and should |
| 3513 | ** be invoked prior to calling this routine. The sqlite3BtreeCommitPhaseOne() |
| 3514 | ** 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] | 3515 | ** contents so that they are written onto the disk platter. All this |
drh | 51898cf | 2009-04-19 20:51:06 +0000 | [diff] [blame] | 3516 | ** routine has to do is delete or truncate or zero the header in the |
| 3517 | ** the rollback journal (which causes the transaction to commit) and |
| 3518 | ** drop locks. |
drh | 6e34599 | 2007-03-30 11:12:08 +0000 | [diff] [blame] | 3519 | ** |
dan | 60939d0 | 2011-03-29 15:40:55 +0000 | [diff] [blame] | 3520 | ** Normally, if an error occurs while the pager layer is attempting to |
| 3521 | ** finalize the underlying journal file, this function returns an error and |
| 3522 | ** the upper layer will attempt a rollback. However, if the second argument |
| 3523 | ** is non-zero then this b-tree transaction is part of a multi-file |
| 3524 | ** transaction. In this case, the transaction has already been committed |
| 3525 | ** (by deleting a master journal file) and the caller will ignore this |
| 3526 | ** functions return code. So, even if an error occurs in the pager layer, |
| 3527 | ** reset the b-tree objects internal state to indicate that the write |
| 3528 | ** transaction has been closed. This is quite safe, as the pager will have |
| 3529 | ** transitioned to the error state. |
| 3530 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3531 | ** This will release the write lock on the database file. If there |
| 3532 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3533 | */ |
dan | 60939d0 | 2011-03-29 15:40:55 +0000 | [diff] [blame] | 3534 | int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3535 | |
drh | 075ed30 | 2010-10-14 01:17:30 +0000 | [diff] [blame] | 3536 | if( p->inTrans==TRANS_NONE ) return SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3537 | sqlite3BtreeEnter(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3538 | btreeIntegrity(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3539 | |
| 3540 | /* If the handle has a write-transaction open, commit the shared-btrees |
| 3541 | ** transaction and set the shared state to TRANS_READ. |
| 3542 | */ |
| 3543 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 3544 | int rc; |
drh | 075ed30 | 2010-10-14 01:17:30 +0000 | [diff] [blame] | 3545 | BtShared *pBt = p->pBt; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3546 | assert( pBt->inTransaction==TRANS_WRITE ); |
| 3547 | assert( pBt->nTransaction>0 ); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3548 | rc = sqlite3PagerCommitPhaseTwo(pBt->pPager); |
dan | 60939d0 | 2011-03-29 15:40:55 +0000 | [diff] [blame] | 3549 | if( rc!=SQLITE_OK && bCleanup==0 ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3550 | sqlite3BtreeLeave(p); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 3551 | return rc; |
| 3552 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3553 | pBt->inTransaction = TRANS_READ; |
dan | bf0e57a | 2013-05-14 20:36:31 +0000 | [diff] [blame] | 3554 | btreeClearHasContent(pBt); |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 3555 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3556 | |
danielk1977 | 94b3073 | 2009-07-02 17:21:57 +0000 | [diff] [blame] | 3557 | btreeEndTransaction(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3558 | sqlite3BtreeLeave(p); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 3559 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3560 | } |
| 3561 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3562 | /* |
| 3563 | ** Do both phases of a commit. |
| 3564 | */ |
| 3565 | int sqlite3BtreeCommit(Btree *p){ |
| 3566 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3567 | sqlite3BtreeEnter(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3568 | rc = sqlite3BtreeCommitPhaseOne(p, 0); |
| 3569 | if( rc==SQLITE_OK ){ |
dan | 60939d0 | 2011-03-29 15:40:55 +0000 | [diff] [blame] | 3570 | rc = sqlite3BtreeCommitPhaseTwo(p, 0); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3571 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3572 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3573 | return rc; |
| 3574 | } |
| 3575 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3576 | /* |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 3577 | ** This routine sets the state to CURSOR_FAULT and the error |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3578 | ** code to errCode for every cursor on any BtShared that pBtree |
| 3579 | ** references. Or if the writeOnly flag is set to 1, then only |
| 3580 | ** trip write cursors and leave read cursors unchanged. |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 3581 | ** |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3582 | ** Every cursor is a candidate to be tripped, including cursors |
| 3583 | ** that belong to other database connections that happen to be |
| 3584 | ** sharing the cache with pBtree. |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 3585 | ** |
dan | 8023104 | 2014-11-12 14:56:02 +0000 | [diff] [blame] | 3586 | ** This routine gets called when a rollback occurs. If the writeOnly |
| 3587 | ** flag is true, then only write-cursors need be tripped - read-only |
| 3588 | ** cursors save their current positions so that they may continue |
| 3589 | ** following the rollback. Or, if writeOnly is false, all cursors are |
| 3590 | ** tripped. In general, writeOnly is false if the transaction being |
| 3591 | ** rolled back modified the database schema. In this case b-tree root |
| 3592 | ** pages may be moved or deleted from the database altogether, making |
| 3593 | ** it unsafe for read cursors to continue. |
| 3594 | ** |
| 3595 | ** If the writeOnly flag is true and an error is encountered while |
| 3596 | ** saving the current position of a read-only cursor, all cursors, |
| 3597 | ** including all read-cursors are tripped. |
| 3598 | ** |
| 3599 | ** SQLITE_OK is returned if successful, or if an error occurs while |
| 3600 | ** saving a cursor position, an SQLite error code. |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 3601 | */ |
dan | 8023104 | 2014-11-12 14:56:02 +0000 | [diff] [blame] | 3602 | int sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int writeOnly){ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 3603 | BtCursor *p; |
dan | 8023104 | 2014-11-12 14:56:02 +0000 | [diff] [blame] | 3604 | int rc = SQLITE_OK; |
| 3605 | |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3606 | assert( (writeOnly==0 || writeOnly==1) && BTCF_WriteFlag==1 ); |
dan | 8023104 | 2014-11-12 14:56:02 +0000 | [diff] [blame] | 3607 | if( pBtree ){ |
| 3608 | sqlite3BtreeEnter(pBtree); |
| 3609 | for(p=pBtree->pBt->pCursor; p; p=p->pNext){ |
| 3610 | int i; |
| 3611 | if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){ |
| 3612 | if( p->eState==CURSOR_VALID ){ |
drh | bea3b97 | 2014-11-18 20:22:05 +0000 | [diff] [blame] | 3613 | rc = saveCursorPosition(p); |
dan | 8023104 | 2014-11-12 14:56:02 +0000 | [diff] [blame] | 3614 | if( rc!=SQLITE_OK ){ |
| 3615 | (void)sqlite3BtreeTripAllCursors(pBtree, rc, 0); |
| 3616 | break; |
| 3617 | } |
| 3618 | } |
| 3619 | }else{ |
| 3620 | sqlite3BtreeClearCursor(p); |
| 3621 | p->eState = CURSOR_FAULT; |
| 3622 | p->skipNext = errCode; |
| 3623 | } |
| 3624 | for(i=0; i<=p->iPage; i++){ |
| 3625 | releasePage(p->apPage[i]); |
| 3626 | p->apPage[i] = 0; |
| 3627 | } |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 3628 | } |
dan | 8023104 | 2014-11-12 14:56:02 +0000 | [diff] [blame] | 3629 | sqlite3BtreeLeave(pBtree); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 3630 | } |
dan | 8023104 | 2014-11-12 14:56:02 +0000 | [diff] [blame] | 3631 | return rc; |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 3632 | } |
| 3633 | |
| 3634 | /* |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3635 | ** Rollback the transaction in progress. |
| 3636 | ** |
| 3637 | ** If tripCode is not SQLITE_OK then cursors will be invalidated (tripped). |
| 3638 | ** Only write cursors are tripped if writeOnly is true but all cursors are |
| 3639 | ** tripped if writeOnly is false. Any attempt to use |
| 3640 | ** a tripped cursor will result in an error. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3641 | ** |
| 3642 | ** This will release the write lock on the database file. If there |
| 3643 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3644 | */ |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3645 | int sqlite3BtreeRollback(Btree *p, int tripCode, int writeOnly){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 3646 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3647 | BtShared *pBt = p->pBt; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3648 | MemPage *pPage1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3649 | |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3650 | assert( writeOnly==1 || writeOnly==0 ); |
| 3651 | assert( tripCode==SQLITE_ABORT_ROLLBACK || tripCode==SQLITE_OK ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3652 | sqlite3BtreeEnter(p); |
drh | 0f198a7 | 2012-02-13 16:43:16 +0000 | [diff] [blame] | 3653 | if( tripCode==SQLITE_OK ){ |
| 3654 | rc = tripCode = saveAllCursors(pBt, 0, 0); |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 3655 | if( rc ) writeOnly = 0; |
drh | 0f198a7 | 2012-02-13 16:43:16 +0000 | [diff] [blame] | 3656 | }else{ |
| 3657 | rc = SQLITE_OK; |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 3658 | } |
drh | 0f198a7 | 2012-02-13 16:43:16 +0000 | [diff] [blame] | 3659 | if( tripCode ){ |
dan | 8023104 | 2014-11-12 14:56:02 +0000 | [diff] [blame] | 3660 | int rc2 = sqlite3BtreeTripAllCursors(p, tripCode, writeOnly); |
| 3661 | assert( rc==SQLITE_OK || (writeOnly==0 && rc2==SQLITE_OK) ); |
| 3662 | if( rc2!=SQLITE_OK ) rc = rc2; |
drh | 0f198a7 | 2012-02-13 16:43:16 +0000 | [diff] [blame] | 3663 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3664 | btreeIntegrity(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3665 | |
| 3666 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 3667 | int rc2; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3668 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 3669 | assert( TRANS_WRITE==pBt->inTransaction ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3670 | rc2 = sqlite3PagerRollback(pBt->pPager); |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 3671 | if( rc2!=SQLITE_OK ){ |
| 3672 | rc = rc2; |
| 3673 | } |
| 3674 | |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3675 | /* The rollback may have destroyed the pPage1->aData value. So |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 3676 | ** call btreeGetPage() on page 1 again to make |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3677 | ** sure pPage1->aData is set correctly. */ |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 3678 | if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){ |
drh | 1f5b467 | 2010-04-01 02:22:19 +0000 | [diff] [blame] | 3679 | int nPage = get4byte(28+(u8*)pPage1->aData); |
| 3680 | testcase( nPage==0 ); |
| 3681 | if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 3682 | testcase( pBt->nPage!=nPage ); |
| 3683 | pBt->nPage = nPage; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3684 | releasePage(pPage1); |
| 3685 | } |
drh | 85ec3b6 | 2013-05-14 23:12:06 +0000 | [diff] [blame] | 3686 | assert( countValidCursors(pBt, 1)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3687 | pBt->inTransaction = TRANS_READ; |
dan | bf0e57a | 2013-05-14 20:36:31 +0000 | [diff] [blame] | 3688 | btreeClearHasContent(pBt); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3689 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3690 | |
danielk1977 | 94b3073 | 2009-07-02 17:21:57 +0000 | [diff] [blame] | 3691 | btreeEndTransaction(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3692 | sqlite3BtreeLeave(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3693 | return rc; |
| 3694 | } |
| 3695 | |
| 3696 | /* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 3697 | ** Start a statement subtransaction. The subtransaction can be rolled |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 3698 | ** back independently of the main transaction. You must start a transaction |
| 3699 | ** before starting a subtransaction. The subtransaction is ended automatically |
| 3700 | ** if the main transaction commits or rolls back. |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 3701 | ** |
| 3702 | ** Statement subtransactions are used around individual SQL statements |
| 3703 | ** that are contained within a BEGIN...COMMIT block. If a constraint |
| 3704 | ** error occurs within the statement, the effect of that one statement |
| 3705 | ** can be rolled back without having to rollback the entire transaction. |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 3706 | ** |
| 3707 | ** A statement sub-transaction is implemented as an anonymous savepoint. The |
| 3708 | ** value passed as the second parameter is the total number of savepoints, |
| 3709 | ** including the new anonymous savepoint, open on the B-Tree. i.e. if there |
| 3710 | ** are no active savepoints and no other statement-transactions open, |
| 3711 | ** iStatement is 1. This anonymous savepoint can be released or rolled back |
| 3712 | ** using the sqlite3BtreeSavepoint() function. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 3713 | */ |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 3714 | int sqlite3BtreeBeginStmt(Btree *p, int iStatement){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 3715 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3716 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3717 | sqlite3BtreeEnter(p); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 3718 | assert( p->inTrans==TRANS_WRITE ); |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 3719 | assert( (pBt->btsFlags & BTS_READ_ONLY)==0 ); |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 3720 | assert( iStatement>0 ); |
| 3721 | assert( iStatement>p->db->nSavepoint ); |
drh | 5e0ccc2 | 2010-03-29 19:36:52 +0000 | [diff] [blame] | 3722 | assert( pBt->inTransaction==TRANS_WRITE ); |
| 3723 | /* At the pager level, a statement transaction is a savepoint with |
| 3724 | ** an index greater than all savepoints created explicitly using |
| 3725 | ** SQL statements. It is illegal to open, release or rollback any |
| 3726 | ** such savepoints while the statement transaction savepoint is active. |
| 3727 | */ |
| 3728 | rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3729 | sqlite3BtreeLeave(p); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 3730 | return rc; |
| 3731 | } |
| 3732 | |
| 3733 | /* |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3734 | ** The second argument to this function, op, is always SAVEPOINT_ROLLBACK |
| 3735 | ** or SAVEPOINT_RELEASE. This function either releases or rolls back the |
danielk1977 | 12dd549 | 2008-12-18 15:45:07 +0000 | [diff] [blame] | 3736 | ** savepoint identified by parameter iSavepoint, depending on the value |
| 3737 | ** of op. |
| 3738 | ** |
| 3739 | ** Normally, iSavepoint is greater than or equal to zero. However, if op is |
| 3740 | ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the |
| 3741 | ** contents of the entire transaction are rolled back. This is different |
| 3742 | ** from a normal transaction rollback, as no locks are released and the |
| 3743 | ** transaction remains open. |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3744 | */ |
| 3745 | int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){ |
| 3746 | int rc = SQLITE_OK; |
| 3747 | if( p && p->inTrans==TRANS_WRITE ){ |
| 3748 | BtShared *pBt = p->pBt; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3749 | assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK ); |
| 3750 | assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) ); |
| 3751 | sqlite3BtreeEnter(p); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3752 | rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint); |
drh | 9f0bbf9 | 2009-01-02 21:08:09 +0000 | [diff] [blame] | 3753 | if( rc==SQLITE_OK ){ |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 3754 | if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){ |
| 3755 | pBt->nPage = 0; |
| 3756 | } |
drh | 9f0bbf9 | 2009-01-02 21:08:09 +0000 | [diff] [blame] | 3757 | rc = newDatabase(pBt); |
drh | dd3cd97 | 2010-03-27 17:12:36 +0000 | [diff] [blame] | 3758 | pBt->nPage = get4byte(28 + pBt->pPage1->aData); |
drh | b9b49bf | 2010-08-05 03:21:39 +0000 | [diff] [blame] | 3759 | |
| 3760 | /* The database size was written into the offset 28 of the header |
| 3761 | ** when the transaction started, so we know that the value at offset |
| 3762 | ** 28 is nonzero. */ |
| 3763 | assert( pBt->nPage>0 ); |
drh | 9f0bbf9 | 2009-01-02 21:08:09 +0000 | [diff] [blame] | 3764 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3765 | sqlite3BtreeLeave(p); |
| 3766 | } |
| 3767 | return rc; |
| 3768 | } |
| 3769 | |
| 3770 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3771 | ** Create a new cursor for the BTree whose root is on the page |
danielk1977 | 3e8add9 | 2009-07-04 17:16:00 +0000 | [diff] [blame] | 3772 | ** iTable. If a read-only cursor is requested, it is assumed that |
| 3773 | ** the caller already has at least a read-only transaction open |
| 3774 | ** on the database already. If a write-cursor is requested, then |
| 3775 | ** the caller is assumed to have an open write transaction. |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 3776 | ** |
| 3777 | ** If wrFlag==0, then the cursor can only be used for reading. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3778 | ** If wrFlag==1, then the cursor can be used for reading or for |
| 3779 | ** writing if other conditions for writing are also met. These |
| 3780 | ** are the conditions that must be met in order for writing to |
| 3781 | ** be allowed: |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 3782 | ** |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3783 | ** 1: The cursor must have been opened with wrFlag==1 |
| 3784 | ** |
drh | fe5d71d | 2007-03-19 11:54:10 +0000 | [diff] [blame] | 3785 | ** 2: Other database connections that share the same pager cache |
| 3786 | ** but which are not in the READ_UNCOMMITTED state may not have |
| 3787 | ** cursors open with wrFlag==0 on the same table. Otherwise |
| 3788 | ** the changes made by this write cursor would be visible to |
| 3789 | ** the read cursors in the other database connection. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3790 | ** |
| 3791 | ** 3: The database must be writable (not on read-only media) |
| 3792 | ** |
| 3793 | ** 4: There must be an active transaction. |
| 3794 | ** |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 3795 | ** No checking is done to make sure that page iTable really is the |
| 3796 | ** root page of a b-tree. If it is not, then the cursor acquired |
| 3797 | ** will not work correctly. |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3798 | ** |
drh | f25a507 | 2009-11-18 23:01:25 +0000 | [diff] [blame] | 3799 | ** It is assumed that the sqlite3BtreeCursorZero() has been called |
| 3800 | ** on pCur to initialize the memory space prior to invoking this routine. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3801 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3802 | static int btreeCursor( |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3803 | Btree *p, /* The btree */ |
| 3804 | int iTable, /* Root page of table to open */ |
| 3805 | int wrFlag, /* 1 to write. 0 read-only */ |
| 3806 | struct KeyInfo *pKeyInfo, /* First arg to comparison function */ |
| 3807 | BtCursor *pCur /* Space for new cursor */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3808 | ){ |
danielk1977 | 3e8add9 | 2009-07-04 17:16:00 +0000 | [diff] [blame] | 3809 | BtShared *pBt = p->pBt; /* Shared b-tree handle */ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3810 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3811 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3812 | assert( wrFlag==0 || wrFlag==1 ); |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 3813 | |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 3814 | /* The following assert statements verify that if this is a sharable |
| 3815 | ** b-tree database, the connection is holding the required table locks, |
| 3816 | ** and that no other connection has any open cursor that conflicts with |
| 3817 | ** this lock. */ |
| 3818 | assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, wrFlag+1) ); |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 3819 | assert( wrFlag==0 || !hasReadConflicts(p, iTable) ); |
| 3820 | |
danielk1977 | 3e8add9 | 2009-07-04 17:16:00 +0000 | [diff] [blame] | 3821 | /* Assert that the caller has opened the required transaction. */ |
| 3822 | assert( p->inTrans>TRANS_NONE ); |
| 3823 | assert( wrFlag==0 || p->inTrans==TRANS_WRITE ); |
| 3824 | assert( pBt->pPage1 && pBt->pPage1->aData ); |
| 3825 | |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 3826 | if( NEVER(wrFlag && (pBt->btsFlags & BTS_READ_ONLY)!=0) ){ |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 3827 | return SQLITE_READONLY; |
drh | a0c9a11 | 2004-03-10 13:42:37 +0000 | [diff] [blame] | 3828 | } |
drh | 3fbb022 | 2014-09-24 19:47:27 +0000 | [diff] [blame] | 3829 | if( wrFlag ){ |
| 3830 | allocateTempSpace(pBt); |
| 3831 | if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM; |
| 3832 | } |
drh | b129915 | 2010-03-30 22:58:33 +0000 | [diff] [blame] | 3833 | if( iTable==1 && btreePagecount(pBt)==0 ){ |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 3834 | assert( wrFlag==0 ); |
| 3835 | iTable = 0; |
danielk1977 | 3e8add9 | 2009-07-04 17:16:00 +0000 | [diff] [blame] | 3836 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3837 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3838 | /* Now that no other errors can occur, finish filling in the BtCursor |
danielk1977 | 3e8add9 | 2009-07-04 17:16:00 +0000 | [diff] [blame] | 3839 | ** variables and link the cursor into the BtShared list. */ |
danielk1977 | 172114a | 2009-07-07 15:47:12 +0000 | [diff] [blame] | 3840 | pCur->pgnoRoot = (Pgno)iTable; |
| 3841 | pCur->iPage = -1; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3842 | pCur->pKeyInfo = pKeyInfo; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3843 | pCur->pBtree = p; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 3844 | pCur->pBt = pBt; |
drh | 4c41718 | 2014-03-31 23:57:41 +0000 | [diff] [blame] | 3845 | assert( wrFlag==0 || wrFlag==BTCF_WriteFlag ); |
| 3846 | pCur->curFlags = wrFlag; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3847 | pCur->pNext = pBt->pCursor; |
| 3848 | if( pCur->pNext ){ |
| 3849 | pCur->pNext->pPrev = pCur; |
| 3850 | } |
| 3851 | pBt->pCursor = pCur; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3852 | pCur->eState = CURSOR_INVALID; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3853 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3854 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3855 | int sqlite3BtreeCursor( |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3856 | Btree *p, /* The btree */ |
| 3857 | int iTable, /* Root page of table to open */ |
| 3858 | int wrFlag, /* 1 to write. 0 read-only */ |
| 3859 | struct KeyInfo *pKeyInfo, /* First arg to xCompare() */ |
| 3860 | BtCursor *pCur /* Write new cursor here */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3861 | ){ |
| 3862 | int rc; |
| 3863 | sqlite3BtreeEnter(p); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3864 | rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3865 | sqlite3BtreeLeave(p); |
| 3866 | return rc; |
| 3867 | } |
drh | 7f75122 | 2009-03-17 22:33:00 +0000 | [diff] [blame] | 3868 | |
| 3869 | /* |
| 3870 | ** Return the size of a BtCursor object in bytes. |
| 3871 | ** |
| 3872 | ** This interfaces is needed so that users of cursors can preallocate |
| 3873 | ** sufficient storage to hold a cursor. The BtCursor object is opaque |
| 3874 | ** to users so they cannot do the sizeof() themselves - they must call |
| 3875 | ** this routine. |
| 3876 | */ |
| 3877 | int sqlite3BtreeCursorSize(void){ |
drh | c54055b | 2009-11-13 17:05:53 +0000 | [diff] [blame] | 3878 | return ROUND8(sizeof(BtCursor)); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3879 | } |
| 3880 | |
drh | 7f75122 | 2009-03-17 22:33:00 +0000 | [diff] [blame] | 3881 | /* |
drh | f25a507 | 2009-11-18 23:01:25 +0000 | [diff] [blame] | 3882 | ** Initialize memory that will be converted into a BtCursor object. |
| 3883 | ** |
| 3884 | ** The simple approach here would be to memset() the entire object |
| 3885 | ** to zero. But it turns out that the apPage[] and aiIdx[] arrays |
| 3886 | ** do not need to be zeroed and they are large, so we can save a lot |
| 3887 | ** of run-time by skipping the initialization of those elements. |
| 3888 | */ |
| 3889 | void sqlite3BtreeCursorZero(BtCursor *p){ |
| 3890 | memset(p, 0, offsetof(BtCursor, iPage)); |
| 3891 | } |
| 3892 | |
| 3893 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3894 | ** Close a cursor. The read lock on the database file is released |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3895 | ** when the last cursor is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3896 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3897 | int sqlite3BtreeCloseCursor(BtCursor *pCur){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 3898 | Btree *pBtree = pCur->pBtree; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3899 | if( pBtree ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3900 | int i; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3901 | BtShared *pBt = pCur->pBt; |
| 3902 | sqlite3BtreeEnter(pBtree); |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 3903 | sqlite3BtreeClearCursor(pCur); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3904 | if( pCur->pPrev ){ |
| 3905 | pCur->pPrev->pNext = pCur->pNext; |
| 3906 | }else{ |
| 3907 | pBt->pCursor = pCur->pNext; |
| 3908 | } |
| 3909 | if( pCur->pNext ){ |
| 3910 | pCur->pNext->pPrev = pCur->pPrev; |
| 3911 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3912 | for(i=0; i<=pCur->iPage; i++){ |
| 3913 | releasePage(pCur->apPage[i]); |
| 3914 | } |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3915 | unlockBtreeIfUnused(pBt); |
dan | 5a500af | 2014-03-11 20:33:04 +0000 | [diff] [blame] | 3916 | sqlite3DbFree(pBtree->db, pCur->aOverflow); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3917 | /* sqlite3_free(pCur); */ |
| 3918 | sqlite3BtreeLeave(pBtree); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3919 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3920 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3921 | } |
| 3922 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3923 | /* |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3924 | ** Make sure the BtCursor* given in the argument has a valid |
| 3925 | ** BtCursor.info structure. If it is not already valid, call |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 3926 | ** btreeParseCell() to fill it in. |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 3927 | ** |
| 3928 | ** BtCursor.info is a cache of the information in the current cell. |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 3929 | ** Using this cache reduces the number of calls to btreeParseCell(). |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3930 | ** |
| 3931 | ** 2007-06-25: There is a bug in some versions of MSVC that cause the |
| 3932 | ** compiler to crash when getCellInfo() is implemented as a macro. |
| 3933 | ** But there is a measureable speed advantage to using the macro on gcc |
| 3934 | ** (when less compiler optimizations like -Os or -O0 are used and the |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 3935 | ** compiler is not doing aggressive inlining.) So we use a real function |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3936 | ** for MSVC and a macro for everything else. Ticket #2457. |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3937 | */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3938 | #ifndef NDEBUG |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3939 | static void assertCellInfo(BtCursor *pCur){ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3940 | CellInfo info; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3941 | int iPage = pCur->iPage; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 3942 | memset(&info, 0, sizeof(info)); |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 3943 | btreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info); |
dan | 7df42ab | 2014-01-20 18:25:44 +0000 | [diff] [blame] | 3944 | assert( CORRUPT_DB || memcmp(&info, &pCur->info, sizeof(info))==0 ); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3945 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3946 | #else |
| 3947 | #define assertCellInfo(x) |
| 3948 | #endif |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3949 | #ifdef _MSC_VER |
| 3950 | /* Use a real function in MSVC to work around bugs in that compiler. */ |
| 3951 | static void getCellInfo(BtCursor *pCur){ |
| 3952 | if( pCur->info.nSize==0 ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3953 | int iPage = pCur->iPage; |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 3954 | btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 3955 | pCur->curFlags |= BTCF_ValidNKey; |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3956 | }else{ |
| 3957 | assertCellInfo(pCur); |
| 3958 | } |
| 3959 | } |
| 3960 | #else /* if not _MSC_VER */ |
| 3961 | /* Use a macro in all other compilers so that the function is inlined */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3962 | #define getCellInfo(pCur) \ |
| 3963 | if( pCur->info.nSize==0 ){ \ |
| 3964 | int iPage = pCur->iPage; \ |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 3965 | btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); \ |
| 3966 | pCur->curFlags |= BTCF_ValidNKey; \ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3967 | }else{ \ |
| 3968 | assertCellInfo(pCur); \ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3969 | } |
| 3970 | #endif /* _MSC_VER */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3971 | |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 3972 | #ifndef NDEBUG /* The next routine used only within assert() statements */ |
| 3973 | /* |
| 3974 | ** Return true if the given BtCursor is valid. A valid cursor is one |
| 3975 | ** that is currently pointing to a row in a (non-empty) table. |
| 3976 | ** This is a verification routine is used only within assert() statements. |
| 3977 | */ |
| 3978 | int sqlite3BtreeCursorIsValid(BtCursor *pCur){ |
| 3979 | return pCur && pCur->eState==CURSOR_VALID; |
| 3980 | } |
| 3981 | #endif /* NDEBUG */ |
| 3982 | |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3983 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3984 | ** Set *pSize to the size of the buffer needed to hold the value of |
| 3985 | ** the key for the current entry. If the cursor is not pointing |
| 3986 | ** to a valid entry, *pSize is set to 0. |
| 3987 | ** |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3988 | ** For a table with the INTKEY flag set, this routine returns the key |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3989 | ** itself, not the number of bytes in the key. |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 3990 | ** |
| 3991 | ** The caller must position the cursor prior to invoking this routine. |
| 3992 | ** |
| 3993 | ** This routine cannot fail. It always returns SQLITE_OK. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3994 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3995 | int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3996 | assert( cursorHoldsMutex(pCur) ); |
drh | c5352b9 | 2014-11-17 20:33:07 +0000 | [diff] [blame] | 3997 | assert( pCur->eState==CURSOR_VALID ); |
| 3998 | getCellInfo(pCur); |
| 3999 | *pSize = pCur->info.nKey; |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 4000 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 4001 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 4002 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4003 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 4004 | ** Set *pSize to the number of bytes of data in the entry the |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 4005 | ** cursor currently points to. |
| 4006 | ** |
| 4007 | ** The caller must guarantee that the cursor is pointing to a non-NULL |
| 4008 | ** valid entry. In other words, the calling procedure must guarantee |
| 4009 | ** that the cursor has Cursor.eState==CURSOR_VALID. |
| 4010 | ** |
| 4011 | ** Failure is not possible. This function always returns SQLITE_OK. |
| 4012 | ** It might just as well be a procedure (returning void) but we continue |
| 4013 | ** to return an integer result code for historical reasons. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 4014 | */ |
| 4015 | int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4016 | assert( cursorHoldsMutex(pCur) ); |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 4017 | assert( pCur->eState==CURSOR_VALID ); |
drh | 3e28ff5 | 2014-09-24 00:59:08 +0000 | [diff] [blame] | 4018 | assert( pCur->apPage[pCur->iPage]->intKeyLeaf==1 ); |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 4019 | getCellInfo(pCur); |
drh | ab1cc58 | 2014-09-23 21:25:19 +0000 | [diff] [blame] | 4020 | *pSize = pCur->info.nPayload; |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 4021 | return SQLITE_OK; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 4022 | } |
| 4023 | |
| 4024 | /* |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 4025 | ** Given the page number of an overflow page in the database (parameter |
| 4026 | ** ovfl), this function finds the page number of the next page in the |
| 4027 | ** linked list of overflow pages. If possible, it uses the auto-vacuum |
| 4028 | ** pointer-map data instead of reading the content of page ovfl to do so. |
| 4029 | ** |
| 4030 | ** If an error occurs an SQLite error code is returned. Otherwise: |
| 4031 | ** |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4032 | ** The page number of the next overflow page in the linked list is |
| 4033 | ** written to *pPgnoNext. If page ovfl is the last page in its linked |
| 4034 | ** list, *pPgnoNext is set to zero. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 4035 | ** |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4036 | ** If ppPage is not NULL, and a reference to the MemPage object corresponding |
| 4037 | ** to page number pOvfl was obtained, then *ppPage is set to point to that |
| 4038 | ** reference. It is the responsibility of the caller to call releasePage() |
| 4039 | ** on *ppPage to free the reference. In no reference was obtained (because |
| 4040 | ** the pointer-map was used to obtain the value for *pPgnoNext), then |
| 4041 | ** *ppPage is set to zero. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 4042 | */ |
| 4043 | static int getOverflowPage( |
drh | fa3be90 | 2009-07-07 02:44:07 +0000 | [diff] [blame] | 4044 | BtShared *pBt, /* The database file */ |
| 4045 | Pgno ovfl, /* Current overflow page number */ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4046 | MemPage **ppPage, /* OUT: MemPage handle (may be NULL) */ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 4047 | Pgno *pPgnoNext /* OUT: Next overflow page number */ |
| 4048 | ){ |
| 4049 | Pgno next = 0; |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4050 | MemPage *pPage = 0; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 4051 | int rc = SQLITE_OK; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 4052 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4053 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4054 | assert(pPgnoNext); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 4055 | |
| 4056 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4057 | /* Try to find the next page in the overflow list using the |
| 4058 | ** autovacuum pointer-map pages. Guess that the next page in |
| 4059 | ** the overflow list is page number (ovfl+1). If that guess turns |
| 4060 | ** out to be wrong, fall back to loading the data of page |
| 4061 | ** number ovfl to determine the next page number. |
| 4062 | */ |
| 4063 | if( pBt->autoVacuum ){ |
| 4064 | Pgno pgno; |
| 4065 | Pgno iGuess = ovfl+1; |
| 4066 | u8 eType; |
| 4067 | |
| 4068 | while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){ |
| 4069 | iGuess++; |
| 4070 | } |
| 4071 | |
drh | b129915 | 2010-03-30 22:58:33 +0000 | [diff] [blame] | 4072 | if( iGuess<=btreePagecount(pBt) ){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 4073 | rc = ptrmapGet(pBt, iGuess, &eType, &pgno); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4074 | if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 4075 | next = iGuess; |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4076 | rc = SQLITE_DONE; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 4077 | } |
| 4078 | } |
| 4079 | } |
| 4080 | #endif |
| 4081 | |
danielk1977 | d8a3f3d | 2009-07-11 11:45:23 +0000 | [diff] [blame] | 4082 | assert( next==0 || rc==SQLITE_DONE ); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4083 | if( rc==SQLITE_OK ){ |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 4084 | rc = btreeGetPage(pBt, ovfl, &pPage, (ppPage==0) ? PAGER_GET_READONLY : 0); |
danielk1977 | d8a3f3d | 2009-07-11 11:45:23 +0000 | [diff] [blame] | 4085 | assert( rc==SQLITE_OK || pPage==0 ); |
| 4086 | if( rc==SQLITE_OK ){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 4087 | next = get4byte(pPage->aData); |
| 4088 | } |
danielk1977 | 443c059 | 2009-01-16 15:21:05 +0000 | [diff] [blame] | 4089 | } |
danielk1977 | 45d6882 | 2009-01-16 16:23:38 +0000 | [diff] [blame] | 4090 | |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 4091 | *pPgnoNext = next; |
| 4092 | if( ppPage ){ |
| 4093 | *ppPage = pPage; |
| 4094 | }else{ |
| 4095 | releasePage(pPage); |
| 4096 | } |
| 4097 | return (rc==SQLITE_DONE ? SQLITE_OK : rc); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 4098 | } |
| 4099 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4100 | /* |
| 4101 | ** Copy data from a buffer to a page, or from a page to a buffer. |
| 4102 | ** |
| 4103 | ** pPayload is a pointer to data stored on database page pDbPage. |
| 4104 | ** If argument eOp is false, then nByte bytes of data are copied |
| 4105 | ** from pPayload to the buffer pointed at by pBuf. If eOp is true, |
| 4106 | ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes |
| 4107 | ** of data are copied from the buffer pBuf to pPayload. |
| 4108 | ** |
| 4109 | ** SQLITE_OK is returned on success, otherwise an error code. |
| 4110 | */ |
| 4111 | static int copyPayload( |
| 4112 | void *pPayload, /* Pointer to page data */ |
| 4113 | void *pBuf, /* Pointer to buffer */ |
| 4114 | int nByte, /* Number of bytes to copy */ |
| 4115 | int eOp, /* 0 -> copy from page, 1 -> copy to page */ |
| 4116 | DbPage *pDbPage /* Page containing pPayload */ |
| 4117 | ){ |
| 4118 | if( eOp ){ |
| 4119 | /* Copy data from buffer to page (a write operation) */ |
| 4120 | int rc = sqlite3PagerWrite(pDbPage); |
| 4121 | if( rc!=SQLITE_OK ){ |
| 4122 | return rc; |
| 4123 | } |
| 4124 | memcpy(pPayload, pBuf, nByte); |
| 4125 | }else{ |
| 4126 | /* Copy data from page to buffer (a read operation) */ |
| 4127 | memcpy(pBuf, pPayload, nByte); |
| 4128 | } |
| 4129 | return SQLITE_OK; |
| 4130 | } |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 4131 | |
| 4132 | /* |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 4133 | ** This function is used to read or overwrite payload information |
dan | 5a500af | 2014-03-11 20:33:04 +0000 | [diff] [blame] | 4134 | ** for the entry that the pCur cursor is pointing to. The eOp |
| 4135 | ** argument is interpreted as follows: |
| 4136 | ** |
| 4137 | ** 0: The operation is a read. Populate the overflow cache. |
| 4138 | ** 1: The operation is a write. Populate the overflow cache. |
| 4139 | ** 2: The operation is a read. Do not populate the overflow cache. |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 4140 | ** |
| 4141 | ** A total of "amt" bytes are read or written beginning at "offset". |
| 4142 | ** Data is read to or from the buffer pBuf. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4143 | ** |
drh | 3bcdfd2 | 2009-07-12 02:32:21 +0000 | [diff] [blame] | 4144 | ** The content being read or written might appear on the main page |
| 4145 | ** or be scattered out on multiple overflow pages. |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4146 | ** |
dan | 5a500af | 2014-03-11 20:33:04 +0000 | [diff] [blame] | 4147 | ** If the current cursor entry uses one or more overflow pages and the |
| 4148 | ** eOp argument is not 2, this function may allocate space for and lazily |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 4149 | ** populates the overflow page-list cache array (BtCursor.aOverflow). |
dan | 5a500af | 2014-03-11 20:33:04 +0000 | [diff] [blame] | 4150 | ** Subsequent calls use this cache to make seeking to the supplied offset |
| 4151 | ** more efficient. |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4152 | ** |
| 4153 | ** Once an overflow page-list cache has been allocated, it may be |
| 4154 | ** invalidated if some other cursor writes to the same table, or if |
| 4155 | ** the cursor is moved to a different row. Additionally, in auto-vacuum |
| 4156 | ** mode, the following events may invalidate an overflow page-list cache. |
| 4157 | ** |
| 4158 | ** * An incremental vacuum, |
| 4159 | ** * A commit in auto_vacuum="full" mode, |
| 4160 | ** * Creating a table (may require moving an overflow page). |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4161 | */ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 4162 | static int accessPayload( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4163 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 4164 | u32 offset, /* Begin reading this far into payload */ |
| 4165 | u32 amt, /* Read this many bytes */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4166 | unsigned char *pBuf, /* Write the bytes into this buffer */ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 4167 | int eOp /* zero to read. non-zero to write. */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4168 | ){ |
| 4169 | unsigned char *aPayload; |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4170 | int rc = SQLITE_OK; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 4171 | int iIdx = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4172 | MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */ |
danielk1977 | 0d06541 | 2008-11-12 18:21:36 +0000 | [diff] [blame] | 4173 | BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */ |
drh | 4c41718 | 2014-03-31 23:57:41 +0000 | [diff] [blame] | 4174 | #ifdef SQLITE_DIRECT_OVERFLOW_READ |
dan | 9501a64 | 2014-10-01 12:01:10 +0000 | [diff] [blame] | 4175 | unsigned char * const pBufStart = pBuf; |
drh | 3f38740 | 2014-09-24 01:23:00 +0000 | [diff] [blame] | 4176 | int bEnd; /* True if reading to end of data */ |
drh | 4c41718 | 2014-03-31 23:57:41 +0000 | [diff] [blame] | 4177 | #endif |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4178 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4179 | assert( pPage ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4180 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4181 | assert( pCur->aiIdx[pCur->iPage]<pPage->nCell ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4182 | assert( cursorHoldsMutex(pCur) ); |
drh | 3f38740 | 2014-09-24 01:23:00 +0000 | [diff] [blame] | 4183 | assert( eOp!=2 || offset==0 ); /* Always start from beginning for eOp==2 */ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4184 | |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 4185 | getCellInfo(pCur); |
drh | ab1cc58 | 2014-09-23 21:25:19 +0000 | [diff] [blame] | 4186 | aPayload = pCur->info.pPayload; |
drh | 4c41718 | 2014-03-31 23:57:41 +0000 | [diff] [blame] | 4187 | #ifdef SQLITE_DIRECT_OVERFLOW_READ |
drh | ab1cc58 | 2014-09-23 21:25:19 +0000 | [diff] [blame] | 4188 | bEnd = offset+amt==pCur->info.nPayload; |
drh | 4c41718 | 2014-03-31 23:57:41 +0000 | [diff] [blame] | 4189 | #endif |
drh | ab1cc58 | 2014-09-23 21:25:19 +0000 | [diff] [blame] | 4190 | assert( offset+amt <= pCur->info.nPayload ); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4191 | |
drh | ab1cc58 | 2014-09-23 21:25:19 +0000 | [diff] [blame] | 4192 | if( &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize] ){ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4193 | /* 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] | 4194 | return SQLITE_CORRUPT_BKPT; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4195 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4196 | |
| 4197 | /* Check if data must be read/written to/from the btree page itself. */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4198 | if( offset<pCur->info.nLocal ){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 4199 | int a = amt; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4200 | if( a+offset>pCur->info.nLocal ){ |
| 4201 | a = pCur->info.nLocal - offset; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 4202 | } |
dan | 5a500af | 2014-03-11 20:33:04 +0000 | [diff] [blame] | 4203 | rc = copyPayload(&aPayload[offset], pBuf, a, (eOp & 0x01), pPage->pDbPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 4204 | offset = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4205 | pBuf += a; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 4206 | amt -= a; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4207 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4208 | offset -= pCur->info.nLocal; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4209 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4210 | |
| 4211 | if( rc==SQLITE_OK && amt>0 ){ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 4212 | const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4213 | Pgno nextPage; |
| 4214 | |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4215 | nextPage = get4byte(&aPayload[pCur->info.nLocal]); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4216 | |
drh | a38c951 | 2014-04-01 01:24:34 +0000 | [diff] [blame] | 4217 | /* If the BtCursor.aOverflow[] has not been allocated, allocate it now. |
| 4218 | ** Except, do not allocate aOverflow[] for eOp==2. |
| 4219 | ** |
| 4220 | ** The aOverflow[] array is sized at one entry for each overflow page |
| 4221 | ** in the overflow chain. The page number of the first overflow page is |
| 4222 | ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array |
| 4223 | ** means "not yet known" (the cache is lazily populated). |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4224 | */ |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 4225 | if( eOp!=2 && (pCur->curFlags & BTCF_ValidOvfl)==0 ){ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 4226 | int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize; |
dan | 5a500af | 2014-03-11 20:33:04 +0000 | [diff] [blame] | 4227 | if( nOvfl>pCur->nOvflAlloc ){ |
| 4228 | Pgno *aNew = (Pgno*)sqlite3DbRealloc( |
| 4229 | pCur->pBtree->db, pCur->aOverflow, nOvfl*2*sizeof(Pgno) |
| 4230 | ); |
| 4231 | if( aNew==0 ){ |
| 4232 | rc = SQLITE_NOMEM; |
| 4233 | }else{ |
| 4234 | pCur->nOvflAlloc = nOvfl*2; |
| 4235 | pCur->aOverflow = aNew; |
| 4236 | } |
| 4237 | } |
| 4238 | if( rc==SQLITE_OK ){ |
| 4239 | memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno)); |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 4240 | pCur->curFlags |= BTCF_ValidOvfl; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 4241 | } |
| 4242 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4243 | |
| 4244 | /* If the overflow page-list cache has been allocated and the |
| 4245 | ** entry for the first required overflow page is valid, skip |
| 4246 | ** directly to it. |
| 4247 | */ |
drh | 3f38740 | 2014-09-24 01:23:00 +0000 | [diff] [blame] | 4248 | if( (pCur->curFlags & BTCF_ValidOvfl)!=0 |
| 4249 | && pCur->aOverflow[offset/ovflSize] |
| 4250 | ){ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 4251 | iIdx = (offset/ovflSize); |
| 4252 | nextPage = pCur->aOverflow[iIdx]; |
| 4253 | offset = (offset%ovflSize); |
| 4254 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4255 | |
| 4256 | for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){ |
| 4257 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4258 | /* If required, populate the overflow page-list cache. */ |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 4259 | if( (pCur->curFlags & BTCF_ValidOvfl)!=0 ){ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4260 | assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage); |
| 4261 | pCur->aOverflow[iIdx] = nextPage; |
| 4262 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4263 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 4264 | if( offset>=ovflSize ){ |
| 4265 | /* The only reason to read this page is to obtain the page |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4266 | ** number for the next page in the overflow chain. The page |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 4267 | ** data is not required. So first try to lookup the overflow |
| 4268 | ** page-list cache, if any, then fall back to the getOverflowPage() |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4269 | ** function. |
drh | a38c951 | 2014-04-01 01:24:34 +0000 | [diff] [blame] | 4270 | ** |
| 4271 | ** Note that the aOverflow[] array must be allocated because eOp!=2 |
| 4272 | ** here. If eOp==2, then offset==0 and this branch is never taken. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 4273 | */ |
drh | a38c951 | 2014-04-01 01:24:34 +0000 | [diff] [blame] | 4274 | assert( eOp!=2 ); |
| 4275 | assert( pCur->curFlags & BTCF_ValidOvfl ); |
| 4276 | if( pCur->aOverflow[iIdx+1] ){ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4277 | nextPage = pCur->aOverflow[iIdx+1]; |
drh | a38c951 | 2014-04-01 01:24:34 +0000 | [diff] [blame] | 4278 | }else{ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4279 | rc = getOverflowPage(pBt, nextPage, 0, &nextPage); |
drh | a38c951 | 2014-04-01 01:24:34 +0000 | [diff] [blame] | 4280 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4281 | offset -= ovflSize; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 4282 | }else{ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 4283 | /* Need to read this page properly. It contains some of the |
| 4284 | ** range of data that is being read (eOp==0) or written (eOp!=0). |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 4285 | */ |
dan | f4ba109 | 2011-10-08 14:57:07 +0000 | [diff] [blame] | 4286 | #ifdef SQLITE_DIRECT_OVERFLOW_READ |
| 4287 | sqlite3_file *fd; |
| 4288 | #endif |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 4289 | int a = amt; |
dan | f4ba109 | 2011-10-08 14:57:07 +0000 | [diff] [blame] | 4290 | if( a + offset > ovflSize ){ |
| 4291 | a = ovflSize - offset; |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 4292 | } |
dan | f4ba109 | 2011-10-08 14:57:07 +0000 | [diff] [blame] | 4293 | |
| 4294 | #ifdef SQLITE_DIRECT_OVERFLOW_READ |
| 4295 | /* If all the following are true: |
| 4296 | ** |
| 4297 | ** 1) this is a read operation, and |
| 4298 | ** 2) data is required from the start of this overflow page, and |
| 4299 | ** 3) the database is file-backed, and |
| 4300 | ** 4) there is no open write-transaction, and |
| 4301 | ** 5) the database is not a WAL database, |
dan | 9bc21b5 | 2014-03-20 18:56:35 +0000 | [diff] [blame] | 4302 | ** 6) all data from the page is being read. |
dan | 9501a64 | 2014-10-01 12:01:10 +0000 | [diff] [blame] | 4303 | ** 7) at least 4 bytes have already been read into the output buffer |
dan | f4ba109 | 2011-10-08 14:57:07 +0000 | [diff] [blame] | 4304 | ** |
| 4305 | ** then data can be read directly from the database file into the |
| 4306 | ** output buffer, bypassing the page-cache altogether. This speeds |
| 4307 | ** up loading large records that span many overflow pages. |
| 4308 | */ |
dan | 5a500af | 2014-03-11 20:33:04 +0000 | [diff] [blame] | 4309 | if( (eOp&0x01)==0 /* (1) */ |
dan | f4ba109 | 2011-10-08 14:57:07 +0000 | [diff] [blame] | 4310 | && offset==0 /* (2) */ |
dan | 9bc21b5 | 2014-03-20 18:56:35 +0000 | [diff] [blame] | 4311 | && (bEnd || a==ovflSize) /* (6) */ |
dan | f4ba109 | 2011-10-08 14:57:07 +0000 | [diff] [blame] | 4312 | && pBt->inTransaction==TRANS_READ /* (4) */ |
| 4313 | && (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (3) */ |
| 4314 | && pBt->pPage1->aData[19]==0x01 /* (5) */ |
dan | 9501a64 | 2014-10-01 12:01:10 +0000 | [diff] [blame] | 4315 | && &pBuf[-4]>=pBufStart /* (7) */ |
dan | f4ba109 | 2011-10-08 14:57:07 +0000 | [diff] [blame] | 4316 | ){ |
| 4317 | u8 aSave[4]; |
| 4318 | u8 *aWrite = &pBuf[-4]; |
dan | 9501a64 | 2014-10-01 12:01:10 +0000 | [diff] [blame] | 4319 | assert( aWrite>=pBufStart ); /* hence (7) */ |
dan | f4ba109 | 2011-10-08 14:57:07 +0000 | [diff] [blame] | 4320 | memcpy(aSave, aWrite, 4); |
dan | 27d47fb | 2011-12-21 17:00:16 +0000 | [diff] [blame] | 4321 | rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1)); |
dan | f4ba109 | 2011-10-08 14:57:07 +0000 | [diff] [blame] | 4322 | nextPage = get4byte(aWrite); |
| 4323 | memcpy(aWrite, aSave, 4); |
| 4324 | }else |
| 4325 | #endif |
| 4326 | |
| 4327 | { |
| 4328 | DbPage *pDbPage; |
dan | 11dcd11 | 2013-03-15 18:29:18 +0000 | [diff] [blame] | 4329 | rc = sqlite3PagerAcquire(pBt->pPager, nextPage, &pDbPage, |
dan | 5a500af | 2014-03-11 20:33:04 +0000 | [diff] [blame] | 4330 | ((eOp&0x01)==0 ? PAGER_GET_READONLY : 0) |
dan | 11dcd11 | 2013-03-15 18:29:18 +0000 | [diff] [blame] | 4331 | ); |
dan | f4ba109 | 2011-10-08 14:57:07 +0000 | [diff] [blame] | 4332 | if( rc==SQLITE_OK ){ |
| 4333 | aPayload = sqlite3PagerGetData(pDbPage); |
| 4334 | nextPage = get4byte(aPayload); |
dan | 5a500af | 2014-03-11 20:33:04 +0000 | [diff] [blame] | 4335 | rc = copyPayload(&aPayload[offset+4], pBuf, a, (eOp&0x01), pDbPage); |
dan | f4ba109 | 2011-10-08 14:57:07 +0000 | [diff] [blame] | 4336 | sqlite3PagerUnref(pDbPage); |
| 4337 | offset = 0; |
| 4338 | } |
| 4339 | } |
| 4340 | amt -= a; |
| 4341 | pBuf += a; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 4342 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 4343 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 4344 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 4345 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4346 | if( rc==SQLITE_OK && amt>0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 4347 | return SQLITE_CORRUPT_BKPT; |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 4348 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4349 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 4350 | } |
| 4351 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4352 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4353 | ** Read part of the key associated with cursor pCur. Exactly |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 4354 | ** "amt" bytes will be transferred into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4355 | ** begins at "offset". |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4356 | ** |
drh | 5d1a872 | 2009-07-22 18:07:40 +0000 | [diff] [blame] | 4357 | ** The caller must ensure that pCur is pointing to a valid row |
| 4358 | ** in the table. |
| 4359 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4360 | ** Return SQLITE_OK on success or an error code if anything goes |
| 4361 | ** wrong. An error is returned if "offset+amt" is larger than |
| 4362 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4363 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4364 | int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4365 | assert( cursorHoldsMutex(pCur) ); |
drh | 5d1a872 | 2009-07-22 18:07:40 +0000 | [diff] [blame] | 4366 | assert( pCur->eState==CURSOR_VALID ); |
| 4367 | assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] ); |
| 4368 | assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell ); |
| 4369 | return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4370 | } |
| 4371 | |
| 4372 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4373 | ** Read part of the data associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4374 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4375 | ** begins at "offset". |
| 4376 | ** |
| 4377 | ** Return SQLITE_OK on success or an error code if anything goes |
| 4378 | ** wrong. An error is returned if "offset+amt" is larger than |
| 4379 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4380 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4381 | int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4382 | int rc; |
| 4383 | |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 4384 | #ifndef SQLITE_OMIT_INCRBLOB |
| 4385 | if ( pCur->eState==CURSOR_INVALID ){ |
| 4386 | return SQLITE_ABORT; |
| 4387 | } |
| 4388 | #endif |
| 4389 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4390 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 4391 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4392 | if( rc==SQLITE_OK ){ |
| 4393 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4394 | assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] ); |
| 4395 | assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell ); |
drh | fb19268 | 2009-07-11 18:26:28 +0000 | [diff] [blame] | 4396 | rc = accessPayload(pCur, offset, amt, pBuf, 0); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4397 | } |
| 4398 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 4399 | } |
| 4400 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4401 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 4402 | ** Return a pointer to payload information from the entry that the |
| 4403 | ** pCur cursor is pointing to. The pointer is to the beginning of |
drh | 2a8d226 | 2013-12-09 20:43:22 +0000 | [diff] [blame] | 4404 | ** the key if index btrees (pPage->intKey==0) and is the data for |
| 4405 | ** table btrees (pPage->intKey==1). The number of bytes of available |
| 4406 | ** key/data is written into *pAmt. If *pAmt==0, then the value |
| 4407 | ** returned will not be a valid pointer. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 4408 | ** |
| 4409 | ** This routine is an optimization. It is common for the entire key |
| 4410 | ** and data to fit on the local page and for there to be no overflow |
| 4411 | ** pages. When that is so, this routine can be used to access the |
| 4412 | ** 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] | 4413 | ** onto overflow pages, then accessPayload() must be used to reassemble |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 4414 | ** the key/data and copy it into a preallocated buffer. |
| 4415 | ** |
| 4416 | ** The pointer returned by this routine looks directly into the cached |
| 4417 | ** page of the database. The data might change or move the next time |
| 4418 | ** any btree routine is called. |
| 4419 | */ |
drh | 2a8d226 | 2013-12-09 20:43:22 +0000 | [diff] [blame] | 4420 | static const void *fetchPayload( |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 4421 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
drh | 2a8d226 | 2013-12-09 20:43:22 +0000 | [diff] [blame] | 4422 | u32 *pAmt /* Write the number of available bytes here */ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 4423 | ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4424 | assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4425 | assert( pCur->eState==CURSOR_VALID ); |
drh | 2a8d226 | 2013-12-09 20:43:22 +0000 | [diff] [blame] | 4426 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4427 | assert( cursorHoldsMutex(pCur) ); |
drh | 2a8d226 | 2013-12-09 20:43:22 +0000 | [diff] [blame] | 4428 | assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell ); |
drh | 86dd371 | 2014-03-25 11:00:21 +0000 | [diff] [blame] | 4429 | assert( pCur->info.nSize>0 ); |
drh | 2a8d226 | 2013-12-09 20:43:22 +0000 | [diff] [blame] | 4430 | *pAmt = pCur->info.nLocal; |
drh | ab1cc58 | 2014-09-23 21:25:19 +0000 | [diff] [blame] | 4431 | return (void*)pCur->info.pPayload; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 4432 | } |
| 4433 | |
| 4434 | |
| 4435 | /* |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 4436 | ** For the entry that cursor pCur is point to, return as |
| 4437 | ** many bytes of the key or data as are available on the local |
| 4438 | ** b-tree page. Write the number of available bytes into *pAmt. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 4439 | ** |
| 4440 | ** The pointer returned is ephemeral. The key/data may move |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4441 | ** or be destroyed on the next call to any Btree routine, |
| 4442 | ** including calls from other threads against the same cache. |
| 4443 | ** Hence, a mutex on the BtShared should be held prior to calling |
| 4444 | ** this routine. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 4445 | ** |
| 4446 | ** These routines is used to get quick access to key and data |
| 4447 | ** in the common case where no overflow pages are used. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 4448 | */ |
drh | 501932c | 2013-11-21 21:59:53 +0000 | [diff] [blame] | 4449 | const void *sqlite3BtreeKeyFetch(BtCursor *pCur, u32 *pAmt){ |
drh | 2a8d226 | 2013-12-09 20:43:22 +0000 | [diff] [blame] | 4450 | return fetchPayload(pCur, pAmt); |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 4451 | } |
drh | 501932c | 2013-11-21 21:59:53 +0000 | [diff] [blame] | 4452 | const void *sqlite3BtreeDataFetch(BtCursor *pCur, u32 *pAmt){ |
drh | 2a8d226 | 2013-12-09 20:43:22 +0000 | [diff] [blame] | 4453 | return fetchPayload(pCur, pAmt); |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 4454 | } |
| 4455 | |
| 4456 | |
| 4457 | /* |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4458 | ** 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] | 4459 | ** page number of the child page to move to. |
danielk1977 | a299d61 | 2009-07-13 11:22:10 +0000 | [diff] [blame] | 4460 | ** |
| 4461 | ** This function returns SQLITE_CORRUPT if the page-header flags field of |
| 4462 | ** the new child page does not match the flags field of the parent (i.e. |
| 4463 | ** if an intkey page appears to be the parent of a non-intkey page, or |
| 4464 | ** vice-versa). |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4465 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4466 | static int moveToChild(BtCursor *pCur, u32 newPgno){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4467 | int rc; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4468 | int i = pCur->iPage; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4469 | MemPage *pNewPage; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 4470 | BtShared *pBt = pCur->pBt; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4471 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4472 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4473 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4474 | assert( pCur->iPage<BTCURSOR_MAX_DEPTH ); |
dan | 11dcd11 | 2013-03-15 18:29:18 +0000 | [diff] [blame] | 4475 | assert( pCur->iPage>=0 ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4476 | if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){ |
| 4477 | return SQLITE_CORRUPT_BKPT; |
| 4478 | } |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 4479 | rc = getAndInitPage(pBt, newPgno, &pNewPage, |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 4480 | (pCur->curFlags & BTCF_WriteFlag)==0 ? PAGER_GET_READONLY : 0); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4481 | if( rc ) return rc; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4482 | pCur->apPage[i+1] = pNewPage; |
| 4483 | pCur->aiIdx[i+1] = 0; |
| 4484 | pCur->iPage++; |
| 4485 | |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 4486 | pCur->info.nSize = 0; |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 4487 | pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); |
danielk1977 | bd5969a | 2009-07-11 17:39:42 +0000 | [diff] [blame] | 4488 | if( pNewPage->nCell<1 || pNewPage->intKey!=pCur->apPage[i]->intKey ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 4489 | return SQLITE_CORRUPT_BKPT; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 4490 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4491 | return SQLITE_OK; |
| 4492 | } |
| 4493 | |
dan | bb246c4 | 2012-01-12 14:25:55 +0000 | [diff] [blame] | 4494 | #if 0 |
danielk1977 | bf93c56 | 2008-09-29 15:53:25 +0000 | [diff] [blame] | 4495 | /* |
| 4496 | ** Page pParent is an internal (non-leaf) tree page. This function |
| 4497 | ** asserts that page number iChild is the left-child if the iIdx'th |
| 4498 | ** cell in page pParent. Or, if iIdx is equal to the total number of |
| 4499 | ** cells in pParent, that page number iChild is the right-child of |
| 4500 | ** the page. |
| 4501 | */ |
| 4502 | static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){ |
| 4503 | assert( iIdx<=pParent->nCell ); |
| 4504 | if( iIdx==pParent->nCell ){ |
| 4505 | assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild ); |
| 4506 | }else{ |
| 4507 | assert( get4byte(findCell(pParent, iIdx))==iChild ); |
| 4508 | } |
| 4509 | } |
| 4510 | #else |
| 4511 | # define assertParentIndex(x,y,z) |
| 4512 | #endif |
| 4513 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4514 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4515 | ** Move the cursor up to the parent page. |
| 4516 | ** |
| 4517 | ** pCur->idx is set to the cell index that contains the pointer |
| 4518 | ** to the page we are coming from. If we are coming from the |
| 4519 | ** right-most child page then pCur->idx is set to one more than |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4520 | ** the largest cell index. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4521 | */ |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 4522 | static void moveToParent(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4523 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4524 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4525 | assert( pCur->iPage>0 ); |
| 4526 | assert( pCur->apPage[pCur->iPage] ); |
dan | bb246c4 | 2012-01-12 14:25:55 +0000 | [diff] [blame] | 4527 | |
| 4528 | /* UPDATE: It is actually possible for the condition tested by the assert |
| 4529 | ** below to be untrue if the database file is corrupt. This can occur if |
| 4530 | ** one cursor has modified page pParent while a reference to it is held |
| 4531 | ** by a second cursor. Which can only happen if a single page is linked |
| 4532 | ** into more than one b-tree structure in a corrupt database. */ |
| 4533 | #if 0 |
danielk1977 | bf93c56 | 2008-09-29 15:53:25 +0000 | [diff] [blame] | 4534 | assertParentIndex( |
| 4535 | pCur->apPage[pCur->iPage-1], |
| 4536 | pCur->aiIdx[pCur->iPage-1], |
| 4537 | pCur->apPage[pCur->iPage]->pgno |
| 4538 | ); |
dan | bb246c4 | 2012-01-12 14:25:55 +0000 | [diff] [blame] | 4539 | #endif |
dan | 6c2688c | 2012-01-12 15:05:03 +0000 | [diff] [blame] | 4540 | testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell ); |
dan | bb246c4 | 2012-01-12 14:25:55 +0000 | [diff] [blame] | 4541 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4542 | releasePage(pCur->apPage[pCur->iPage]); |
| 4543 | pCur->iPage--; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 4544 | pCur->info.nSize = 0; |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 4545 | pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4546 | } |
| 4547 | |
| 4548 | /* |
danielk1977 | 8f880a8 | 2009-07-13 09:41:45 +0000 | [diff] [blame] | 4549 | ** Move the cursor to point to the root page of its b-tree structure. |
| 4550 | ** |
| 4551 | ** If the table has a virtual root page, then the cursor is moved to point |
| 4552 | ** to the virtual root page instead of the actual root page. A table has a |
| 4553 | ** virtual root page when the actual root page contains no cells and a |
| 4554 | ** single child page. This can only happen with the table rooted at page 1. |
| 4555 | ** |
| 4556 | ** If the b-tree structure is empty, the cursor state is set to |
| 4557 | ** CURSOR_INVALID. Otherwise, the cursor is set to point to the first |
| 4558 | ** cell located on the root (or virtual root) page and the cursor state |
| 4559 | ** is set to CURSOR_VALID. |
| 4560 | ** |
| 4561 | ** If this function returns successfully, it may be assumed that the |
| 4562 | ** page-header flags indicate that the [virtual] root-page is the expected |
| 4563 | ** kind of b-tree page (i.e. if when opening the cursor the caller did not |
| 4564 | ** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D, |
| 4565 | ** indicating a table b-tree, or if the caller did specify a KeyInfo |
| 4566 | ** structure the flags byte is set to 0x02 or 0x0A, indicating an index |
| 4567 | ** b-tree). |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4568 | */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4569 | static int moveToRoot(BtCursor *pCur){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4570 | MemPage *pRoot; |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 4571 | int rc = SQLITE_OK; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4572 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4573 | assert( cursorHoldsMutex(pCur) ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 4574 | assert( CURSOR_INVALID < CURSOR_REQUIRESEEK ); |
| 4575 | assert( CURSOR_VALID < CURSOR_REQUIRESEEK ); |
| 4576 | assert( CURSOR_FAULT > CURSOR_REQUIRESEEK ); |
| 4577 | if( pCur->eState>=CURSOR_REQUIRESEEK ){ |
| 4578 | if( pCur->eState==CURSOR_FAULT ){ |
drh | 4c301aa | 2009-07-15 17:25:45 +0000 | [diff] [blame] | 4579 | assert( pCur->skipNext!=SQLITE_OK ); |
| 4580 | return pCur->skipNext; |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 4581 | } |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 4582 | sqlite3BtreeClearCursor(pCur); |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 4583 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4584 | |
| 4585 | if( pCur->iPage>=0 ){ |
drh | 4e8fe3f | 2013-12-06 23:25:27 +0000 | [diff] [blame] | 4586 | while( pCur->iPage ) releasePage(pCur->apPage[pCur->iPage--]); |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 4587 | }else if( pCur->pgnoRoot==0 ){ |
| 4588 | pCur->eState = CURSOR_INVALID; |
| 4589 | return SQLITE_OK; |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 4590 | }else{ |
drh | 4e8fe3f | 2013-12-06 23:25:27 +0000 | [diff] [blame] | 4591 | rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->apPage[0], |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 4592 | (pCur->curFlags & BTCF_WriteFlag)==0 ? PAGER_GET_READONLY : 0); |
drh | 4c301aa | 2009-07-15 17:25:45 +0000 | [diff] [blame] | 4593 | if( rc!=SQLITE_OK ){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 4594 | pCur->eState = CURSOR_INVALID; |
| 4595 | return rc; |
| 4596 | } |
danielk1977 | 172114a | 2009-07-07 15:47:12 +0000 | [diff] [blame] | 4597 | pCur->iPage = 0; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 4598 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4599 | pRoot = pCur->apPage[0]; |
| 4600 | assert( pRoot->pgno==pCur->pgnoRoot ); |
dan | 7df42ab | 2014-01-20 18:25:44 +0000 | [diff] [blame] | 4601 | |
| 4602 | /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor |
| 4603 | ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is |
| 4604 | ** NULL, the caller expects a table b-tree. If this is not the case, |
| 4605 | ** return an SQLITE_CORRUPT error. |
| 4606 | ** |
| 4607 | ** Earlier versions of SQLite assumed that this test could not fail |
| 4608 | ** if the root page was already loaded when this function was called (i.e. |
| 4609 | ** if pCur->iPage>=0). But this is not so if the database is corrupted |
| 4610 | ** in such a way that page pRoot is linked into a second b-tree table |
| 4611 | ** (or the freelist). */ |
| 4612 | assert( pRoot->intKey==1 || pRoot->intKey==0 ); |
| 4613 | if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){ |
| 4614 | return SQLITE_CORRUPT_BKPT; |
| 4615 | } |
danielk1977 | 8f880a8 | 2009-07-13 09:41:45 +0000 | [diff] [blame] | 4616 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4617 | pCur->aiIdx[0] = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 4618 | pCur->info.nSize = 0; |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 4619 | pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4620 | |
drh | 4e8fe3f | 2013-12-06 23:25:27 +0000 | [diff] [blame] | 4621 | if( pRoot->nCell>0 ){ |
| 4622 | pCur->eState = CURSOR_VALID; |
| 4623 | }else if( !pRoot->leaf ){ |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 4624 | Pgno subpage; |
drh | c85240d | 2009-06-04 16:14:33 +0000 | [diff] [blame] | 4625 | if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4626 | subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4627 | pCur->eState = CURSOR_VALID; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4628 | rc = moveToChild(pCur, subpage); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4629 | }else{ |
drh | 4e8fe3f | 2013-12-06 23:25:27 +0000 | [diff] [blame] | 4630 | pCur->eState = CURSOR_INVALID; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 4631 | } |
| 4632 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4633 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 4634 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4635 | /* |
| 4636 | ** Move the cursor down to the left-most leaf entry beneath the |
| 4637 | ** entry to which it is currently pointing. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 4638 | ** |
| 4639 | ** The left-most leaf is the one with the smallest key - the first |
| 4640 | ** in ascending order. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4641 | */ |
| 4642 | static int moveToLeftmost(BtCursor *pCur){ |
| 4643 | Pgno pgno; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4644 | int rc = SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4645 | MemPage *pPage; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4646 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4647 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4648 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4649 | while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){ |
| 4650 | assert( pCur->aiIdx[pCur->iPage]<pPage->nCell ); |
| 4651 | pgno = get4byte(findCell(pPage, pCur->aiIdx[pCur->iPage])); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4652 | rc = moveToChild(pCur, pgno); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4653 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4654 | return rc; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4655 | } |
| 4656 | |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4657 | /* |
| 4658 | ** Move the cursor down to the right-most leaf entry beneath the |
| 4659 | ** page to which it is currently pointing. Notice the difference |
| 4660 | ** between moveToLeftmost() and moveToRightmost(). moveToLeftmost() |
| 4661 | ** finds the left-most entry beneath the *entry* whereas moveToRightmost() |
| 4662 | ** finds the right-most entry beneath the *page*. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 4663 | ** |
| 4664 | ** The right-most entry is the one with the largest key - the last |
| 4665 | ** key in ascending order. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4666 | */ |
| 4667 | static int moveToRightmost(BtCursor *pCur){ |
| 4668 | Pgno pgno; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4669 | int rc = SQLITE_OK; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 4670 | MemPage *pPage = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4671 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4672 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4673 | assert( pCur->eState==CURSOR_VALID ); |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 4674 | while( !(pPage = pCur->apPage[pCur->iPage])->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4675 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4676 | pCur->aiIdx[pCur->iPage] = pPage->nCell; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4677 | rc = moveToChild(pCur, pgno); |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 4678 | if( rc ) return rc; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4679 | } |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 4680 | pCur->aiIdx[pCur->iPage] = pPage->nCell-1; |
| 4681 | assert( pCur->info.nSize==0 ); |
| 4682 | assert( (pCur->curFlags & BTCF_ValidNKey)==0 ); |
| 4683 | return SQLITE_OK; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4684 | } |
| 4685 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4686 | /* Move the cursor to the first entry in the table. Return SQLITE_OK |
| 4687 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 4688 | ** or set *pRes to 1 if the table is empty. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4689 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4690 | int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4691 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4692 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4693 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 4694 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4695 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4696 | if( rc==SQLITE_OK ){ |
| 4697 | if( pCur->eState==CURSOR_INVALID ){ |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 4698 | assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4699 | *pRes = 1; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4700 | }else{ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4701 | assert( pCur->apPage[pCur->iPage]->nCell>0 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4702 | *pRes = 0; |
| 4703 | rc = moveToLeftmost(pCur); |
| 4704 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4705 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4706 | return rc; |
| 4707 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4708 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4709 | /* Move the cursor to the last entry in the table. Return SQLITE_OK |
| 4710 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 4711 | ** or set *pRes to 1 if the table is empty. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4712 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4713 | int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4714 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4715 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4716 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 4717 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
danielk1977 | 3f632d5 | 2009-05-02 10:03:09 +0000 | [diff] [blame] | 4718 | |
| 4719 | /* If the cursor already points to the last entry, this is a no-op. */ |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 4720 | if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){ |
danielk1977 | 3f632d5 | 2009-05-02 10:03:09 +0000 | [diff] [blame] | 4721 | #ifdef SQLITE_DEBUG |
| 4722 | /* This block serves to assert() that the cursor really does point |
| 4723 | ** to the last entry in the b-tree. */ |
| 4724 | int ii; |
| 4725 | for(ii=0; ii<pCur->iPage; ii++){ |
| 4726 | assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell ); |
| 4727 | } |
| 4728 | assert( pCur->aiIdx[pCur->iPage]==pCur->apPage[pCur->iPage]->nCell-1 ); |
| 4729 | assert( pCur->apPage[pCur->iPage]->leaf ); |
| 4730 | #endif |
| 4731 | return SQLITE_OK; |
| 4732 | } |
| 4733 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4734 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4735 | if( rc==SQLITE_OK ){ |
| 4736 | if( CURSOR_INVALID==pCur->eState ){ |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 4737 | assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4738 | *pRes = 1; |
| 4739 | }else{ |
| 4740 | assert( pCur->eState==CURSOR_VALID ); |
| 4741 | *pRes = 0; |
| 4742 | rc = moveToRightmost(pCur); |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 4743 | if( rc==SQLITE_OK ){ |
| 4744 | pCur->curFlags |= BTCF_AtLast; |
| 4745 | }else{ |
| 4746 | pCur->curFlags &= ~BTCF_AtLast; |
| 4747 | } |
| 4748 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4749 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4750 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4751 | return rc; |
| 4752 | } |
| 4753 | |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 4754 | /* Move the cursor so that it points to an entry near the key |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4755 | ** specified by pIdxKey or intKey. Return a success code. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4756 | ** |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4757 | ** For INTKEY tables, the intKey parameter is used. pIdxKey |
| 4758 | ** must be NULL. For index tables, pIdxKey is used and intKey |
| 4759 | ** is ignored. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4760 | ** |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4761 | ** If an exact match is not found, then the cursor is always |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4762 | ** left pointing at a leaf page which would hold the entry if it |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4763 | ** were present. The cursor might point to an entry that comes |
| 4764 | ** before or after the key. |
| 4765 | ** |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 4766 | ** An integer is written into *pRes which is the result of |
| 4767 | ** comparing the key with the entry to which the cursor is |
| 4768 | ** pointing. The meaning of the integer written into |
| 4769 | ** *pRes is as follows: |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4770 | ** |
| 4771 | ** *pRes<0 The cursor is left pointing at an entry that |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 4772 | ** is smaller than intKey/pIdxKey or if the table is empty |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 4773 | ** and the cursor is therefore left point to nothing. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4774 | ** |
| 4775 | ** *pRes==0 The cursor is left pointing at an entry that |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 4776 | ** exactly matches intKey/pIdxKey. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4777 | ** |
| 4778 | ** *pRes>0 The cursor is left pointing at an entry that |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 4779 | ** is larger than intKey/pIdxKey. |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4780 | ** |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 4781 | */ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4782 | int sqlite3BtreeMovetoUnpacked( |
| 4783 | BtCursor *pCur, /* The cursor to be moved */ |
| 4784 | UnpackedRecord *pIdxKey, /* Unpacked index key */ |
| 4785 | i64 intKey, /* The table key */ |
| 4786 | int biasRight, /* If true, bias the search to the high end */ |
| 4787 | int *pRes /* Write search results here */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 4788 | ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4789 | int rc; |
dan | 3b9330f | 2014-02-27 20:44:18 +0000 | [diff] [blame] | 4790 | RecordCompare xRecordCompare; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4791 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4792 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 4793 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
danielk1977 | 5cb0963 | 2009-07-09 11:36:01 +0000 | [diff] [blame] | 4794 | assert( pRes ); |
danielk1977 | 3fd7cf5 | 2009-07-13 07:30:52 +0000 | [diff] [blame] | 4795 | assert( (pIdxKey==0)==(pCur->pKeyInfo==0) ); |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 4796 | |
| 4797 | /* If the cursor is already positioned at the point we are trying |
| 4798 | ** to move to, then just return without doing any work */ |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 4799 | if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0 |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4800 | && pCur->apPage[0]->intKey |
| 4801 | ){ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4802 | if( pCur->info.nKey==intKey ){ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 4803 | *pRes = 0; |
| 4804 | return SQLITE_OK; |
| 4805 | } |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 4806 | if( (pCur->curFlags & BTCF_AtLast)!=0 && pCur->info.nKey<intKey ){ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 4807 | *pRes = -1; |
| 4808 | return SQLITE_OK; |
| 4809 | } |
| 4810 | } |
| 4811 | |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 4812 | if( pIdxKey ){ |
| 4813 | xRecordCompare = sqlite3VdbeFindCompare(pIdxKey); |
dan | 38fdead | 2014-04-01 10:19:02 +0000 | [diff] [blame] | 4814 | pIdxKey->errCode = 0; |
dan | 3b9330f | 2014-02-27 20:44:18 +0000 | [diff] [blame] | 4815 | assert( pIdxKey->default_rc==1 |
| 4816 | || pIdxKey->default_rc==0 |
| 4817 | || pIdxKey->default_rc==-1 |
| 4818 | ); |
drh | 13a747e | 2014-03-03 21:46:55 +0000 | [diff] [blame] | 4819 | }else{ |
drh | b6e8fd1 | 2014-03-06 01:56:33 +0000 | [diff] [blame] | 4820 | xRecordCompare = 0; /* All keys are integers */ |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 4821 | } |
| 4822 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4823 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4824 | if( rc ){ |
| 4825 | return rc; |
| 4826 | } |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 4827 | assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage] ); |
| 4828 | assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->isInit ); |
| 4829 | assert( pCur->eState==CURSOR_INVALID || pCur->apPage[pCur->iPage]->nCell>0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4830 | if( pCur->eState==CURSOR_INVALID ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 4831 | *pRes = -1; |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 4832 | assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 ); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 4833 | return SQLITE_OK; |
| 4834 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4835 | assert( pCur->apPage[0]->intKey || pIdxKey ); |
drh | 1468438 | 2006-11-30 13:05:29 +0000 | [diff] [blame] | 4836 | for(;;){ |
drh | ec3e6b1 | 2013-11-25 02:38:55 +0000 | [diff] [blame] | 4837 | int lwr, upr, idx, c; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4838 | Pgno chldPg; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4839 | MemPage *pPage = pCur->apPage[pCur->iPage]; |
drh | ec3e6b1 | 2013-11-25 02:38:55 +0000 | [diff] [blame] | 4840 | u8 *pCell; /* Pointer to current cell in pPage */ |
danielk1977 | 171fff3 | 2009-07-11 05:06:51 +0000 | [diff] [blame] | 4841 | |
| 4842 | /* pPage->nCell must be greater than zero. If this is the root-page |
| 4843 | ** the cursor would have been INVALID above and this for(;;) loop |
| 4844 | ** not run. If this is not the root-page, then the moveToChild() routine |
danielk1977 | 3fd7cf5 | 2009-07-13 07:30:52 +0000 | [diff] [blame] | 4845 | ** would have already detected db corruption. Similarly, pPage must |
| 4846 | ** be the right kind (index or table) of b-tree page. Otherwise |
| 4847 | ** a moveToChild() or moveToRoot() call would have detected corruption. */ |
danielk1977 | 171fff3 | 2009-07-11 05:06:51 +0000 | [diff] [blame] | 4848 | assert( pPage->nCell>0 ); |
danielk1977 | 3fd7cf5 | 2009-07-13 07:30:52 +0000 | [diff] [blame] | 4849 | assert( pPage->intKey==(pIdxKey==0) ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4850 | lwr = 0; |
| 4851 | upr = pPage->nCell-1; |
drh | ebf10b1 | 2013-11-25 17:38:26 +0000 | [diff] [blame] | 4852 | assert( biasRight==0 || biasRight==1 ); |
| 4853 | idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */ |
drh | d793f44 | 2013-11-25 14:10:15 +0000 | [diff] [blame] | 4854 | pCur->aiIdx[pCur->iPage] = (u16)idx; |
dan | a4660bd | 2014-03-04 16:05:25 +0000 | [diff] [blame] | 4855 | if( xRecordCompare==0 ){ |
drh | ec3e6b1 | 2013-11-25 02:38:55 +0000 | [diff] [blame] | 4856 | for(;;){ |
danielk1977 | 11c327a | 2009-05-04 19:01:26 +0000 | [diff] [blame] | 4857 | i64 nCellKey; |
drh | ec3e6b1 | 2013-11-25 02:38:55 +0000 | [diff] [blame] | 4858 | pCell = findCell(pPage, idx) + pPage->childPtrSize; |
drh | 3e28ff5 | 2014-09-24 00:59:08 +0000 | [diff] [blame] | 4859 | if( pPage->intKeyLeaf ){ |
drh | 9b2fc61 | 2013-11-25 20:14:13 +0000 | [diff] [blame] | 4860 | while( 0x80 <= *(pCell++) ){ |
| 4861 | if( pCell>=pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT; |
| 4862 | } |
drh | d172f86 | 2006-01-12 15:01:15 +0000 | [diff] [blame] | 4863 | } |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 4864 | getVarint(pCell, (u64*)&nCellKey); |
drh | bb933ef | 2013-11-25 15:01:38 +0000 | [diff] [blame] | 4865 | if( nCellKey<intKey ){ |
| 4866 | lwr = idx+1; |
| 4867 | if( lwr>upr ){ c = -1; break; } |
| 4868 | }else if( nCellKey>intKey ){ |
| 4869 | upr = idx-1; |
| 4870 | if( lwr>upr ){ c = +1; break; } |
| 4871 | }else{ |
| 4872 | assert( nCellKey==intKey ); |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 4873 | pCur->curFlags |= BTCF_ValidNKey; |
drh | ec3e6b1 | 2013-11-25 02:38:55 +0000 | [diff] [blame] | 4874 | pCur->info.nKey = nCellKey; |
drh | d793f44 | 2013-11-25 14:10:15 +0000 | [diff] [blame] | 4875 | pCur->aiIdx[pCur->iPage] = (u16)idx; |
drh | ec3e6b1 | 2013-11-25 02:38:55 +0000 | [diff] [blame] | 4876 | if( !pPage->leaf ){ |
| 4877 | lwr = idx; |
drh | ebf10b1 | 2013-11-25 17:38:26 +0000 | [diff] [blame] | 4878 | goto moveto_next_layer; |
drh | ec3e6b1 | 2013-11-25 02:38:55 +0000 | [diff] [blame] | 4879 | }else{ |
| 4880 | *pRes = 0; |
| 4881 | rc = SQLITE_OK; |
| 4882 | goto moveto_finish; |
| 4883 | } |
drh | d793f44 | 2013-11-25 14:10:15 +0000 | [diff] [blame] | 4884 | } |
drh | ebf10b1 | 2013-11-25 17:38:26 +0000 | [diff] [blame] | 4885 | assert( lwr+upr>=0 ); |
| 4886 | idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */ |
drh | ec3e6b1 | 2013-11-25 02:38:55 +0000 | [diff] [blame] | 4887 | } |
| 4888 | }else{ |
| 4889 | for(;;){ |
| 4890 | int nCell; |
drh | ec3e6b1 | 2013-11-25 02:38:55 +0000 | [diff] [blame] | 4891 | pCell = findCell(pPage, idx) + pPage->childPtrSize; |
| 4892 | |
drh | b2eced5 | 2010-08-12 02:41:12 +0000 | [diff] [blame] | 4893 | /* The maximum supported page-size is 65536 bytes. This means that |
danielk1977 | 11c327a | 2009-05-04 19:01:26 +0000 | [diff] [blame] | 4894 | ** the maximum number of record bytes stored on an index B-Tree |
drh | b2eced5 | 2010-08-12 02:41:12 +0000 | [diff] [blame] | 4895 | ** page is less than 16384 bytes and may be stored as a 2-byte |
danielk1977 | 11c327a | 2009-05-04 19:01:26 +0000 | [diff] [blame] | 4896 | ** varint. This information is used to attempt to avoid parsing |
| 4897 | ** the entire cell by checking for the cases where the record is |
| 4898 | ** stored entirely within the b-tree page by inspecting the first |
| 4899 | ** 2 bytes of the cell. |
| 4900 | */ |
drh | ec3e6b1 | 2013-11-25 02:38:55 +0000 | [diff] [blame] | 4901 | nCell = pCell[0]; |
drh | 72b8ef6 | 2013-12-06 22:44:51 +0000 | [diff] [blame] | 4902 | if( nCell<=pPage->max1bytePayload ){ |
danielk1977 | 11c327a | 2009-05-04 19:01:26 +0000 | [diff] [blame] | 4903 | /* This branch runs if the record-size field of the cell is a |
| 4904 | ** single byte varint and the record fits entirely on the main |
| 4905 | ** b-tree page. */ |
drh | 3def235 | 2011-11-11 00:27:15 +0000 | [diff] [blame] | 4906 | testcase( pCell+nCell+1==pPage->aDataEnd ); |
drh | 75179de | 2014-09-16 14:37:35 +0000 | [diff] [blame] | 4907 | c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey); |
danielk1977 | 11c327a | 2009-05-04 19:01:26 +0000 | [diff] [blame] | 4908 | }else if( !(pCell[1] & 0x80) |
| 4909 | && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal |
| 4910 | ){ |
| 4911 | /* The record-size field is a 2 byte varint and the record |
| 4912 | ** fits entirely on the main b-tree page. */ |
drh | 3def235 | 2011-11-11 00:27:15 +0000 | [diff] [blame] | 4913 | testcase( pCell+nCell+2==pPage->aDataEnd ); |
drh | 75179de | 2014-09-16 14:37:35 +0000 | [diff] [blame] | 4914 | c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 4915 | }else{ |
danielk1977 | 11c327a | 2009-05-04 19:01:26 +0000 | [diff] [blame] | 4916 | /* The record flows over onto one or more overflow pages. In |
| 4917 | ** this case the whole cell needs to be parsed, a buffer allocated |
| 4918 | ** and accessPayload() used to retrieve the record into the |
| 4919 | ** buffer before VdbeRecordCompare() can be called. */ |
| 4920 | void *pCellKey; |
| 4921 | u8 * const pCellBody = pCell - pPage->childPtrSize; |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 4922 | btreeParseCellPtr(pPage, pCellBody, &pCur->info); |
shane | 60a4b53 | 2009-05-06 18:57:09 +0000 | [diff] [blame] | 4923 | nCell = (int)pCur->info.nKey; |
danielk1977 | 11c327a | 2009-05-04 19:01:26 +0000 | [diff] [blame] | 4924 | pCellKey = sqlite3Malloc( nCell ); |
danielk1977 | 6507ecb | 2008-03-25 09:56:44 +0000 | [diff] [blame] | 4925 | if( pCellKey==0 ){ |
| 4926 | rc = SQLITE_NOMEM; |
| 4927 | goto moveto_finish; |
| 4928 | } |
drh | d793f44 | 2013-11-25 14:10:15 +0000 | [diff] [blame] | 4929 | pCur->aiIdx[pCur->iPage] = (u16)idx; |
dan | 5a500af | 2014-03-11 20:33:04 +0000 | [diff] [blame] | 4930 | rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 2); |
drh | ec9b31f | 2009-08-25 13:53:49 +0000 | [diff] [blame] | 4931 | if( rc ){ |
| 4932 | sqlite3_free(pCellKey); |
| 4933 | goto moveto_finish; |
| 4934 | } |
drh | 75179de | 2014-09-16 14:37:35 +0000 | [diff] [blame] | 4935 | c = xRecordCompare(nCell, pCellKey, pIdxKey); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 4936 | sqlite3_free(pCellKey); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 4937 | } |
dan | 38fdead | 2014-04-01 10:19:02 +0000 | [diff] [blame] | 4938 | assert( |
| 4939 | (pIdxKey->errCode!=SQLITE_CORRUPT || c==0) |
dan | a7bf23c | 2014-05-02 17:12:41 +0000 | [diff] [blame] | 4940 | && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed) |
dan | 38fdead | 2014-04-01 10:19:02 +0000 | [diff] [blame] | 4941 | ); |
drh | bb933ef | 2013-11-25 15:01:38 +0000 | [diff] [blame] | 4942 | if( c<0 ){ |
| 4943 | lwr = idx+1; |
| 4944 | }else if( c>0 ){ |
| 4945 | upr = idx-1; |
| 4946 | }else{ |
| 4947 | assert( c==0 ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 4948 | *pRes = 0; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 4949 | rc = SQLITE_OK; |
drh | d793f44 | 2013-11-25 14:10:15 +0000 | [diff] [blame] | 4950 | pCur->aiIdx[pCur->iPage] = (u16)idx; |
dan | 38fdead | 2014-04-01 10:19:02 +0000 | [diff] [blame] | 4951 | if( pIdxKey->errCode ) rc = SQLITE_CORRUPT; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 4952 | goto moveto_finish; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4953 | } |
drh | ebf10b1 | 2013-11-25 17:38:26 +0000 | [diff] [blame] | 4954 | if( lwr>upr ) break; |
| 4955 | assert( lwr+upr>=0 ); |
| 4956 | idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2 */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4957 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4958 | } |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 4959 | assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4960 | assert( pPage->isInit ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4961 | if( pPage->leaf ){ |
drh | ec3e6b1 | 2013-11-25 02:38:55 +0000 | [diff] [blame] | 4962 | assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell ); |
drh | bb933ef | 2013-11-25 15:01:38 +0000 | [diff] [blame] | 4963 | pCur->aiIdx[pCur->iPage] = (u16)idx; |
drh | ec3e6b1 | 2013-11-25 02:38:55 +0000 | [diff] [blame] | 4964 | *pRes = c; |
| 4965 | rc = SQLITE_OK; |
| 4966 | goto moveto_finish; |
drh | ebf10b1 | 2013-11-25 17:38:26 +0000 | [diff] [blame] | 4967 | } |
| 4968 | moveto_next_layer: |
| 4969 | if( lwr>=pPage->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4970 | chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4971 | }else{ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 4972 | chldPg = get4byte(findCell(pPage, lwr)); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4973 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4974 | pCur->aiIdx[pCur->iPage] = (u16)lwr; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4975 | rc = moveToChild(pCur, chldPg); |
drh | ec3e6b1 | 2013-11-25 02:38:55 +0000 | [diff] [blame] | 4976 | if( rc ) break; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4977 | } |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 4978 | moveto_finish: |
drh | d2022b0 | 2013-11-25 16:23:52 +0000 | [diff] [blame] | 4979 | pCur->info.nSize = 0; |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 4980 | pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4981 | return rc; |
| 4982 | } |
| 4983 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4984 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4985 | /* |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 4986 | ** Return TRUE if the cursor is not pointing at an entry of the table. |
| 4987 | ** |
| 4988 | ** TRUE will be returned after a call to sqlite3BtreeNext() moves |
| 4989 | ** past the last entry in the table or sqlite3BtreePrev() moves past |
| 4990 | ** the first entry. TRUE is also returned if the table is empty. |
| 4991 | */ |
| 4992 | int sqlite3BtreeEof(BtCursor *pCur){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4993 | /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries |
| 4994 | ** have been deleted? This API will need to change to return an error code |
| 4995 | ** as well as the boolean result value. |
| 4996 | */ |
| 4997 | return (CURSOR_VALID!=pCur->eState); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 4998 | } |
| 4999 | |
| 5000 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 5001 | ** Advance the cursor to the next entry in the database. If |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 5002 | ** successful then set *pRes=0. If the cursor |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 5003 | ** was already pointing to the last entry in the database before |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 5004 | ** this routine was called, then set *pRes=1. |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 5005 | ** |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 5006 | ** The main entry point is sqlite3BtreeNext(). That routine is optimized |
| 5007 | ** for the common case of merely incrementing the cell counter BtCursor.aiIdx |
| 5008 | ** to the next cell on the current page. The (slower) btreeNext() helper |
| 5009 | ** routine is called when it is necessary to move to a different page or |
| 5010 | ** to restore the cursor. |
| 5011 | ** |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 5012 | ** The calling function will set *pRes to 0 or 1. The initial *pRes value |
| 5013 | ** will be 1 if the cursor being stepped corresponds to an SQL index and |
| 5014 | ** if this routine could have been skipped if that SQL index had been |
| 5015 | ** a unique index. Otherwise the caller will have set *pRes to zero. |
| 5016 | ** Zero is the common case. The btree implementation is free to use the |
| 5017 | ** initial *pRes value as a hint to improve performance, but the current |
| 5018 | ** SQLite btree implementation does not. (Note that the comdb2 btree |
| 5019 | ** implementation does use this hint, however.) |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 5020 | */ |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 5021 | static SQLITE_NOINLINE int btreeNext(BtCursor *pCur, int *pRes){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 5022 | int rc; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5023 | int idx; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 5024 | MemPage *pPage; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5025 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5026 | assert( cursorHoldsMutex(pCur) ); |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 5027 | assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID ); |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 5028 | assert( *pRes==0 ); |
drh | f66f26a | 2013-08-19 20:04:10 +0000 | [diff] [blame] | 5029 | if( pCur->eState!=CURSOR_VALID ){ |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 5030 | assert( (pCur->curFlags & BTCF_ValidOvfl)==0 ); |
drh | f66f26a | 2013-08-19 20:04:10 +0000 | [diff] [blame] | 5031 | rc = restoreCursorPosition(pCur); |
| 5032 | if( rc!=SQLITE_OK ){ |
| 5033 | return rc; |
| 5034 | } |
| 5035 | if( CURSOR_INVALID==pCur->eState ){ |
| 5036 | *pRes = 1; |
| 5037 | return SQLITE_OK; |
| 5038 | } |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 5039 | if( pCur->skipNext ){ |
| 5040 | assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT ); |
| 5041 | pCur->eState = CURSOR_VALID; |
| 5042 | if( pCur->skipNext>0 ){ |
| 5043 | pCur->skipNext = 0; |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 5044 | return SQLITE_OK; |
| 5045 | } |
drh | f66f26a | 2013-08-19 20:04:10 +0000 | [diff] [blame] | 5046 | pCur->skipNext = 0; |
drh | f66f26a | 2013-08-19 20:04:10 +0000 | [diff] [blame] | 5047 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5048 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5049 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5050 | pPage = pCur->apPage[pCur->iPage]; |
| 5051 | idx = ++pCur->aiIdx[pCur->iPage]; |
| 5052 | assert( pPage->isInit ); |
dan | bb246c4 | 2012-01-12 14:25:55 +0000 | [diff] [blame] | 5053 | |
| 5054 | /* If the database file is corrupt, it is possible for the value of idx |
| 5055 | ** to be invalid here. This can only occur if a second cursor modifies |
| 5056 | ** the page while cursor pCur is holding a reference to it. Which can |
| 5057 | ** only happen if the database is corrupt in such a way as to link the |
| 5058 | ** page into more than one b-tree structure. */ |
| 5059 | testcase( idx>pPage->nCell ); |
danielk1977 | 6a43f9b | 2004-11-16 04:57:24 +0000 | [diff] [blame] | 5060 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5061 | if( idx>=pPage->nCell ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5062 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5063 | rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 5064 | if( rc ) return rc; |
| 5065 | return moveToLeftmost(pCur); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 5066 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5067 | do{ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5068 | if( pCur->iPage==0 ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 5069 | *pRes = 1; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5070 | pCur->eState = CURSOR_INVALID; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5071 | return SQLITE_OK; |
| 5072 | } |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 5073 | moveToParent(pCur); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5074 | pPage = pCur->apPage[pCur->iPage]; |
| 5075 | }while( pCur->aiIdx[pCur->iPage]>=pPage->nCell ); |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 5076 | if( pPage->intKey ){ |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 5077 | return sqlite3BtreeNext(pCur, pRes); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5078 | }else{ |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 5079 | return SQLITE_OK; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5080 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 5081 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5082 | if( pPage->leaf ){ |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 5083 | return SQLITE_OK; |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 5084 | }else{ |
| 5085 | return moveToLeftmost(pCur); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 5086 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 5087 | } |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 5088 | int sqlite3BtreeNext(BtCursor *pCur, int *pRes){ |
| 5089 | MemPage *pPage; |
| 5090 | assert( cursorHoldsMutex(pCur) ); |
| 5091 | assert( pRes!=0 ); |
| 5092 | assert( *pRes==0 || *pRes==1 ); |
| 5093 | assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID ); |
| 5094 | pCur->info.nSize = 0; |
| 5095 | pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); |
| 5096 | *pRes = 0; |
| 5097 | if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur, pRes); |
| 5098 | pPage = pCur->apPage[pCur->iPage]; |
| 5099 | if( (++pCur->aiIdx[pCur->iPage])>=pPage->nCell ){ |
| 5100 | pCur->aiIdx[pCur->iPage]--; |
| 5101 | return btreeNext(pCur, pRes); |
| 5102 | } |
| 5103 | if( pPage->leaf ){ |
| 5104 | return SQLITE_OK; |
| 5105 | }else{ |
| 5106 | return moveToLeftmost(pCur); |
| 5107 | } |
| 5108 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 5109 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5110 | /* |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 5111 | ** 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] | 5112 | ** successful then set *pRes=0. If the cursor |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 5113 | ** was already pointing to the first entry in the database before |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 5114 | ** this routine was called, then set *pRes=1. |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 5115 | ** |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 5116 | ** The main entry point is sqlite3BtreePrevious(). That routine is optimized |
| 5117 | ** for the common case of merely decrementing the cell counter BtCursor.aiIdx |
drh | 3f38740 | 2014-09-24 01:23:00 +0000 | [diff] [blame] | 5118 | ** to the previous cell on the current page. The (slower) btreePrevious() |
| 5119 | ** helper routine is called when it is necessary to move to a different page |
| 5120 | ** or to restore the cursor. |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 5121 | ** |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 5122 | ** The calling function will set *pRes to 0 or 1. The initial *pRes value |
| 5123 | ** will be 1 if the cursor being stepped corresponds to an SQL index and |
| 5124 | ** if this routine could have been skipped if that SQL index had been |
| 5125 | ** a unique index. Otherwise the caller will have set *pRes to zero. |
| 5126 | ** Zero is the common case. The btree implementation is free to use the |
| 5127 | ** initial *pRes value as a hint to improve performance, but the current |
| 5128 | ** SQLite btree implementation does not. (Note that the comdb2 btree |
| 5129 | ** implementation does use this hint, however.) |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 5130 | */ |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 5131 | static SQLITE_NOINLINE int btreePrevious(BtCursor *pCur, int *pRes){ |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 5132 | int rc; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 5133 | MemPage *pPage; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5134 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5135 | assert( cursorHoldsMutex(pCur) ); |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 5136 | assert( pRes!=0 ); |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 5137 | assert( *pRes==0 ); |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 5138 | assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID ); |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 5139 | assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 ); |
| 5140 | assert( pCur->info.nSize==0 ); |
drh | f66f26a | 2013-08-19 20:04:10 +0000 | [diff] [blame] | 5141 | if( pCur->eState!=CURSOR_VALID ){ |
drh | 7682a47 | 2014-09-29 15:00:28 +0000 | [diff] [blame] | 5142 | rc = restoreCursorPosition(pCur); |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 5143 | if( rc!=SQLITE_OK ){ |
| 5144 | return rc; |
drh | f66f26a | 2013-08-19 20:04:10 +0000 | [diff] [blame] | 5145 | } |
| 5146 | if( CURSOR_INVALID==pCur->eState ){ |
| 5147 | *pRes = 1; |
| 5148 | return SQLITE_OK; |
| 5149 | } |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 5150 | if( pCur->skipNext ){ |
| 5151 | assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT ); |
| 5152 | pCur->eState = CURSOR_VALID; |
| 5153 | if( pCur->skipNext<0 ){ |
| 5154 | pCur->skipNext = 0; |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 5155 | return SQLITE_OK; |
| 5156 | } |
drh | f66f26a | 2013-08-19 20:04:10 +0000 | [diff] [blame] | 5157 | pCur->skipNext = 0; |
drh | f66f26a | 2013-08-19 20:04:10 +0000 | [diff] [blame] | 5158 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5159 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5160 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5161 | pPage = pCur->apPage[pCur->iPage]; |
| 5162 | assert( pPage->isInit ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5163 | if( !pPage->leaf ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5164 | int idx = pCur->aiIdx[pCur->iPage]; |
| 5165 | rc = moveToChild(pCur, get4byte(findCell(pPage, idx))); |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 5166 | if( rc ) return rc; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 5167 | rc = moveToRightmost(pCur); |
| 5168 | }else{ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5169 | while( pCur->aiIdx[pCur->iPage]==0 ){ |
| 5170 | if( pCur->iPage==0 ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5171 | pCur->eState = CURSOR_INVALID; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 5172 | *pRes = 1; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 5173 | return SQLITE_OK; |
| 5174 | } |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 5175 | moveToParent(pCur); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 5176 | } |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 5177 | assert( pCur->info.nSize==0 ); |
| 5178 | assert( (pCur->curFlags & (BTCF_ValidNKey|BTCF_ValidOvfl))==0 ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5179 | |
| 5180 | pCur->aiIdx[pCur->iPage]--; |
| 5181 | pPage = pCur->apPage[pCur->iPage]; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 5182 | if( pPage->intKey && !pPage->leaf ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5183 | rc = sqlite3BtreePrevious(pCur, pRes); |
| 5184 | }else{ |
| 5185 | rc = SQLITE_OK; |
| 5186 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 5187 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 5188 | return rc; |
| 5189 | } |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 5190 | int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){ |
| 5191 | assert( cursorHoldsMutex(pCur) ); |
| 5192 | assert( pRes!=0 ); |
| 5193 | assert( *pRes==0 || *pRes==1 ); |
| 5194 | assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID ); |
| 5195 | *pRes = 0; |
| 5196 | pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey); |
| 5197 | pCur->info.nSize = 0; |
| 5198 | if( pCur->eState!=CURSOR_VALID |
| 5199 | || pCur->aiIdx[pCur->iPage]==0 |
| 5200 | || pCur->apPage[pCur->iPage]->leaf==0 |
| 5201 | ){ |
| 5202 | return btreePrevious(pCur, pRes); |
| 5203 | } |
| 5204 | pCur->aiIdx[pCur->iPage]--; |
| 5205 | return SQLITE_OK; |
| 5206 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 5207 | |
| 5208 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5209 | ** Allocate a new page from the database file. |
| 5210 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5211 | ** The new page is marked as dirty. (In other words, sqlite3PagerWrite() |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5212 | ** has already been called on the new page.) The new page has also |
| 5213 | ** been referenced and the calling routine is responsible for calling |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5214 | ** sqlite3PagerUnref() on the new page when it is done. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5215 | ** |
| 5216 | ** SQLITE_OK is returned on success. Any other return value indicates |
| 5217 | ** an error. *ppPage and *pPgno are undefined in the event of an error. |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5218 | ** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned. |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 5219 | ** |
drh | 82e647d | 2013-03-02 03:25:55 +0000 | [diff] [blame] | 5220 | ** If the "nearby" parameter is not 0, then an effort is made to |
drh | 199e3cf | 2002-07-18 11:01:47 +0000 | [diff] [blame] | 5221 | ** 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] | 5222 | ** attempt to keep related pages close to each other in the database file, |
| 5223 | ** which in turn can make database access faster. |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5224 | ** |
drh | 82e647d | 2013-03-02 03:25:55 +0000 | [diff] [blame] | 5225 | ** If the eMode parameter is BTALLOC_EXACT and the nearby page exists |
| 5226 | ** anywhere on the free-list, then it is guaranteed to be returned. If |
| 5227 | ** eMode is BTALLOC_LT then the page returned will be less than or equal |
| 5228 | ** to nearby if any such page exists. If eMode is BTALLOC_ANY then there |
| 5229 | ** are no restrictions on which page is returned. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5230 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5231 | static int allocateBtreePage( |
drh | 82e647d | 2013-03-02 03:25:55 +0000 | [diff] [blame] | 5232 | BtShared *pBt, /* The btree */ |
| 5233 | MemPage **ppPage, /* Store pointer to the allocated page here */ |
| 5234 | Pgno *pPgno, /* Store the page number here */ |
| 5235 | Pgno nearby, /* Search for a page near this one */ |
| 5236 | u8 eMode /* BTALLOC_EXACT, BTALLOC_LT, or BTALLOC_ANY */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5237 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5238 | MemPage *pPage1; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5239 | int rc; |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 5240 | u32 n; /* Number of pages on the freelist */ |
drh | 042d6a1 | 2009-06-17 13:57:16 +0000 | [diff] [blame] | 5241 | u32 k; /* Number of leaves on the trunk of the freelist */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 5242 | MemPage *pTrunk = 0; |
| 5243 | MemPage *pPrevTrunk = 0; |
drh | 1662b5a | 2009-06-04 19:06:09 +0000 | [diff] [blame] | 5244 | Pgno mxPage; /* Total size of the database file */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 5245 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5246 | assert( sqlite3_mutex_held(pBt->mutex) ); |
dan | 09ff9e1 | 2013-03-11 11:49:03 +0000 | [diff] [blame] | 5247 | assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5248 | pPage1 = pBt->pPage1; |
drh | b129915 | 2010-03-30 22:58:33 +0000 | [diff] [blame] | 5249 | mxPage = btreePagecount(pBt); |
drh | 113762a | 2014-11-19 16:36:25 +0000 | [diff] [blame] | 5250 | /* EVIDENCE-OF: R-05119-02637 The 4-byte big-endian integer at offset 36 |
| 5251 | ** stores stores the total number of pages on the freelist. */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5252 | n = get4byte(&pPage1->aData[36]); |
drh | df35a08 | 2009-07-09 02:24:35 +0000 | [diff] [blame] | 5253 | testcase( n==mxPage-1 ); |
| 5254 | if( n>=mxPage ){ |
drh | 1662b5a | 2009-06-04 19:06:09 +0000 | [diff] [blame] | 5255 | return SQLITE_CORRUPT_BKPT; |
| 5256 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5257 | if( n>0 ){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5258 | /* There are pages on the freelist. Reuse one of those pages. */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5259 | Pgno iTrunk; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5260 | u8 searchList = 0; /* If the free-list must be searched for 'nearby' */ |
| 5261 | |
drh | 82e647d | 2013-03-02 03:25:55 +0000 | [diff] [blame] | 5262 | /* If eMode==BTALLOC_EXACT and a query of the pointer-map |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5263 | ** shows that the page 'nearby' is somewhere on the free-list, then |
| 5264 | ** the entire-list will be searched for that page. |
| 5265 | */ |
| 5266 | #ifndef SQLITE_OMIT_AUTOVACUUM |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 5267 | if( eMode==BTALLOC_EXACT ){ |
| 5268 | if( nearby<=mxPage ){ |
| 5269 | u8 eType; |
| 5270 | assert( nearby>0 ); |
| 5271 | assert( pBt->autoVacuum ); |
| 5272 | rc = ptrmapGet(pBt, nearby, &eType, 0); |
| 5273 | if( rc ) return rc; |
| 5274 | if( eType==PTRMAP_FREEPAGE ){ |
| 5275 | searchList = 1; |
| 5276 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5277 | } |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 5278 | }else if( eMode==BTALLOC_LE ){ |
| 5279 | searchList = 1; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5280 | } |
| 5281 | #endif |
| 5282 | |
| 5283 | /* Decrement the free-list count by 1. Set iTrunk to the index of the |
| 5284 | ** first free-list trunk page. iPrevTrunk is initially 1. |
| 5285 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5286 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5287 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5288 | put4byte(&pPage1->aData[36], n-1); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5289 | |
| 5290 | /* The code within this loop is run only once if the 'searchList' variable |
| 5291 | ** is not true. Otherwise, it runs once for each trunk-page on the |
drh | 82e647d | 2013-03-02 03:25:55 +0000 | [diff] [blame] | 5292 | ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT) |
| 5293 | ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT) |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5294 | */ |
| 5295 | do { |
| 5296 | pPrevTrunk = pTrunk; |
| 5297 | if( pPrevTrunk ){ |
drh | 113762a | 2014-11-19 16:36:25 +0000 | [diff] [blame] | 5298 | /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page |
| 5299 | ** is the page number of the next freelist trunk page in the list or |
| 5300 | ** zero if this is the last freelist trunk page. */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5301 | iTrunk = get4byte(&pPrevTrunk->aData[0]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 5302 | }else{ |
drh | 113762a | 2014-11-19 16:36:25 +0000 | [diff] [blame] | 5303 | /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32 |
| 5304 | ** stores the page number of the first page of the freelist, or zero if |
| 5305 | ** the freelist is empty. */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5306 | iTrunk = get4byte(&pPage1->aData[32]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 5307 | } |
drh | df35a08 | 2009-07-09 02:24:35 +0000 | [diff] [blame] | 5308 | testcase( iTrunk==mxPage ); |
drh | 1662b5a | 2009-06-04 19:06:09 +0000 | [diff] [blame] | 5309 | if( iTrunk>mxPage ){ |
| 5310 | rc = SQLITE_CORRUPT_BKPT; |
| 5311 | }else{ |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 5312 | rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0); |
drh | 1662b5a | 2009-06-04 19:06:09 +0000 | [diff] [blame] | 5313 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5314 | if( rc ){ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 5315 | pTrunk = 0; |
| 5316 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5317 | } |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 5318 | assert( pTrunk!=0 ); |
| 5319 | assert( pTrunk->aData!=0 ); |
drh | 113762a | 2014-11-19 16:36:25 +0000 | [diff] [blame] | 5320 | /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page |
| 5321 | ** is the number of leaf page pointers to follow. */ |
| 5322 | k = get4byte(&pTrunk->aData[4]); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5323 | if( k==0 && !searchList ){ |
| 5324 | /* The trunk has no leaves and the list is not being searched. |
| 5325 | ** So extract the trunk page itself and use it as the newly |
| 5326 | ** allocated page */ |
| 5327 | assert( pPrevTrunk==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5328 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 5329 | if( rc ){ |
| 5330 | goto end_allocate_page; |
| 5331 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5332 | *pPgno = iTrunk; |
| 5333 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 5334 | *ppPage = pTrunk; |
| 5335 | pTrunk = 0; |
| 5336 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
drh | 042d6a1 | 2009-06-17 13:57:16 +0000 | [diff] [blame] | 5337 | }else if( k>(u32)(pBt->usableSize/4 - 2) ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5338 | /* Value of k is out of range. Database corruption */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 5339 | rc = SQLITE_CORRUPT_BKPT; |
| 5340 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5341 | #ifndef SQLITE_OMIT_AUTOVACUUM |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 5342 | }else if( searchList |
| 5343 | && (nearby==iTrunk || (iTrunk<nearby && eMode==BTALLOC_LE)) |
| 5344 | ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5345 | /* The list is being searched and this trunk page is the page |
| 5346 | ** to allocate, regardless of whether it has leaves. |
| 5347 | */ |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 5348 | *pPgno = iTrunk; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5349 | *ppPage = pTrunk; |
| 5350 | searchList = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5351 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 5352 | if( rc ){ |
| 5353 | goto end_allocate_page; |
| 5354 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5355 | if( k==0 ){ |
| 5356 | if( !pPrevTrunk ){ |
| 5357 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 5358 | }else{ |
dan | f48c355 | 2010-08-23 15:41:24 +0000 | [diff] [blame] | 5359 | rc = sqlite3PagerWrite(pPrevTrunk->pDbPage); |
| 5360 | if( rc!=SQLITE_OK ){ |
| 5361 | goto end_allocate_page; |
| 5362 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5363 | memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4); |
| 5364 | } |
| 5365 | }else{ |
| 5366 | /* The trunk page is required by the caller but it contains |
| 5367 | ** pointers to free-list leaves. The first leaf becomes a trunk |
| 5368 | ** page in this case. |
| 5369 | */ |
| 5370 | MemPage *pNewTrunk; |
| 5371 | Pgno iNewTrunk = get4byte(&pTrunk->aData[8]); |
drh | 1662b5a | 2009-06-04 19:06:09 +0000 | [diff] [blame] | 5372 | if( iNewTrunk>mxPage ){ |
| 5373 | rc = SQLITE_CORRUPT_BKPT; |
| 5374 | goto end_allocate_page; |
| 5375 | } |
drh | df35a08 | 2009-07-09 02:24:35 +0000 | [diff] [blame] | 5376 | testcase( iNewTrunk==mxPage ); |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 5377 | rc = btreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5378 | if( rc!=SQLITE_OK ){ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 5379 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5380 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5381 | rc = sqlite3PagerWrite(pNewTrunk->pDbPage); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5382 | if( rc!=SQLITE_OK ){ |
| 5383 | releasePage(pNewTrunk); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 5384 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5385 | } |
| 5386 | memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4); |
| 5387 | put4byte(&pNewTrunk->aData[4], k-1); |
| 5388 | memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 5389 | releasePage(pNewTrunk); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5390 | if( !pPrevTrunk ){ |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 5391 | assert( sqlite3PagerIswriteable(pPage1->pDbPage) ); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5392 | put4byte(&pPage1->aData[32], iNewTrunk); |
| 5393 | }else{ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5394 | rc = sqlite3PagerWrite(pPrevTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 5395 | if( rc ){ |
| 5396 | goto end_allocate_page; |
| 5397 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5398 | put4byte(&pPrevTrunk->aData[0], iNewTrunk); |
| 5399 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5400 | } |
| 5401 | pTrunk = 0; |
| 5402 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
| 5403 | #endif |
danielk1977 | e576521 | 2009-06-17 11:13:28 +0000 | [diff] [blame] | 5404 | }else if( k>0 ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5405 | /* Extract a leaf from the trunk */ |
drh | 042d6a1 | 2009-06-17 13:57:16 +0000 | [diff] [blame] | 5406 | u32 closest; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5407 | Pgno iPage; |
| 5408 | unsigned char *aData = pTrunk->aData; |
| 5409 | if( nearby>0 ){ |
drh | 042d6a1 | 2009-06-17 13:57:16 +0000 | [diff] [blame] | 5410 | u32 i; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5411 | closest = 0; |
dan | f38b65a | 2013-02-22 20:57:47 +0000 | [diff] [blame] | 5412 | if( eMode==BTALLOC_LE ){ |
| 5413 | for(i=0; i<k; i++){ |
| 5414 | iPage = get4byte(&aData[8+i*4]); |
dan | 87ade19 | 2013-02-23 17:49:16 +0000 | [diff] [blame] | 5415 | if( iPage<=nearby ){ |
dan | f38b65a | 2013-02-22 20:57:47 +0000 | [diff] [blame] | 5416 | closest = i; |
| 5417 | break; |
| 5418 | } |
| 5419 | } |
| 5420 | }else{ |
| 5421 | int dist; |
| 5422 | dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby); |
| 5423 | for(i=1; i<k; i++){ |
| 5424 | int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby); |
| 5425 | if( d2<dist ){ |
| 5426 | closest = i; |
| 5427 | dist = d2; |
| 5428 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5429 | } |
| 5430 | } |
| 5431 | }else{ |
| 5432 | closest = 0; |
| 5433 | } |
| 5434 | |
| 5435 | iPage = get4byte(&aData[8+closest*4]); |
drh | df35a08 | 2009-07-09 02:24:35 +0000 | [diff] [blame] | 5436 | testcase( iPage==mxPage ); |
drh | 1662b5a | 2009-06-04 19:06:09 +0000 | [diff] [blame] | 5437 | if( iPage>mxPage ){ |
| 5438 | rc = SQLITE_CORRUPT_BKPT; |
| 5439 | goto end_allocate_page; |
| 5440 | } |
drh | df35a08 | 2009-07-09 02:24:35 +0000 | [diff] [blame] | 5441 | testcase( iPage==mxPage ); |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 5442 | if( !searchList |
| 5443 | || (iPage==nearby || (iPage<nearby && eMode==BTALLOC_LE)) |
| 5444 | ){ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5445 | int noContent; |
shane | 1f9e6aa | 2008-06-09 19:27:11 +0000 | [diff] [blame] | 5446 | *pPgno = iPage; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5447 | TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d" |
| 5448 | ": %d more free pages\n", |
| 5449 | *pPgno, closest+1, k, pTrunk->pgno, n-1)); |
drh | 93b4fc7 | 2011-04-07 14:47:01 +0000 | [diff] [blame] | 5450 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
| 5451 | if( rc ) goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5452 | if( closest<k-1 ){ |
| 5453 | memcpy(&aData[8+closest*4], &aData[4+k*4], 4); |
| 5454 | } |
| 5455 | put4byte(&aData[4], k-1); |
drh | 3f38740 | 2014-09-24 01:23:00 +0000 | [diff] [blame] | 5456 | noContent = !btreeGetHasContent(pBt, *pPgno)? PAGER_GET_NOCONTENT : 0; |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 5457 | rc = btreeGetPage(pBt, *pPgno, ppPage, noContent); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5458 | if( rc==SQLITE_OK ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5459 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 5460 | if( rc!=SQLITE_OK ){ |
| 5461 | releasePage(*ppPage); |
| 5462 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5463 | } |
| 5464 | searchList = 0; |
| 5465 | } |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 5466 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5467 | releasePage(pPrevTrunk); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 5468 | pPrevTrunk = 0; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5469 | }while( searchList ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5470 | }else{ |
dan | bc1a3c6 | 2013-02-23 16:40:46 +0000 | [diff] [blame] | 5471 | /* There are no pages on the freelist, so append a new page to the |
| 5472 | ** database image. |
| 5473 | ** |
| 5474 | ** Normally, new pages allocated by this block can be requested from the |
| 5475 | ** pager layer with the 'no-content' flag set. This prevents the pager |
| 5476 | ** from trying to read the pages content from disk. However, if the |
| 5477 | ** current transaction has already run one or more incremental-vacuum |
| 5478 | ** steps, then the page we are about to allocate may contain content |
| 5479 | ** that is required in the event of a rollback. In this case, do |
| 5480 | ** not set the no-content flag. This causes the pager to load and journal |
| 5481 | ** the current page content before overwriting it. |
| 5482 | ** |
| 5483 | ** Note that the pager will not actually attempt to load or journal |
| 5484 | ** content for any page that really does lie past the end of the database |
| 5485 | ** file on disk. So the effects of disabling the no-content optimization |
| 5486 | ** here are confined to those pages that lie between the end of the |
| 5487 | ** database image and the end of the database file. |
| 5488 | */ |
drh | 3f38740 | 2014-09-24 01:23:00 +0000 | [diff] [blame] | 5489 | int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0; |
dan | bc1a3c6 | 2013-02-23 16:40:46 +0000 | [diff] [blame] | 5490 | |
drh | dd3cd97 | 2010-03-27 17:12:36 +0000 | [diff] [blame] | 5491 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 5492 | if( rc ) return rc; |
| 5493 | pBt->nPage++; |
| 5494 | if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++; |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5495 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5496 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | dd3cd97 | 2010-03-27 17:12:36 +0000 | [diff] [blame] | 5497 | if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5498 | /* If *pPgno refers to a pointer-map page, allocate two new pages |
| 5499 | ** at the end of the file instead of one. The first allocated page |
| 5500 | ** becomes a new pointer-map page, the second is used by the caller. |
| 5501 | */ |
danielk1977 | ac86169 | 2009-03-28 10:54:22 +0000 | [diff] [blame] | 5502 | MemPage *pPg = 0; |
drh | dd3cd97 | 2010-03-27 17:12:36 +0000 | [diff] [blame] | 5503 | TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage)); |
| 5504 | assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) ); |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 5505 | rc = btreeGetPage(pBt, pBt->nPage, &pPg, bNoContent); |
danielk1977 | ac86169 | 2009-03-28 10:54:22 +0000 | [diff] [blame] | 5506 | if( rc==SQLITE_OK ){ |
| 5507 | rc = sqlite3PagerWrite(pPg->pDbPage); |
| 5508 | releasePage(pPg); |
| 5509 | } |
| 5510 | if( rc ) return rc; |
drh | dd3cd97 | 2010-03-27 17:12:36 +0000 | [diff] [blame] | 5511 | pBt->nPage++; |
| 5512 | if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5513 | } |
| 5514 | #endif |
drh | dd3cd97 | 2010-03-27 17:12:36 +0000 | [diff] [blame] | 5515 | put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage); |
| 5516 | *pPgno = pBt->nPage; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5517 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5518 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 5519 | rc = btreeGetPage(pBt, *pPgno, ppPage, bNoContent); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5520 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5521 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 5522 | if( rc!=SQLITE_OK ){ |
| 5523 | releasePage(*ppPage); |
| 5524 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5525 | TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5526 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5527 | |
| 5528 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 5529 | |
| 5530 | end_allocate_page: |
| 5531 | releasePage(pTrunk); |
| 5532 | releasePage(pPrevTrunk); |
danielk1977 | b247c21 | 2008-11-21 09:09:01 +0000 | [diff] [blame] | 5533 | if( rc==SQLITE_OK ){ |
| 5534 | if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){ |
| 5535 | releasePage(*ppPage); |
dan | 7df42ab | 2014-01-20 18:25:44 +0000 | [diff] [blame] | 5536 | *ppPage = 0; |
danielk1977 | b247c21 | 2008-11-21 09:09:01 +0000 | [diff] [blame] | 5537 | return SQLITE_CORRUPT_BKPT; |
| 5538 | } |
| 5539 | (*ppPage)->isInit = 0; |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 5540 | }else{ |
| 5541 | *ppPage = 0; |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 5542 | } |
drh | 93b4fc7 | 2011-04-07 14:47:01 +0000 | [diff] [blame] | 5543 | assert( rc!=SQLITE_OK || sqlite3PagerIswriteable((*ppPage)->pDbPage) ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5544 | return rc; |
| 5545 | } |
| 5546 | |
| 5547 | /* |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5548 | ** This function is used to add page iPage to the database file free-list. |
| 5549 | ** 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] | 5550 | ** |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5551 | ** The value passed as the second argument to this function is optional. |
| 5552 | ** If the caller happens to have a pointer to the MemPage object |
| 5553 | ** corresponding to page iPage handy, it may pass it as the second value. |
| 5554 | ** Otherwise, it may pass NULL. |
| 5555 | ** |
| 5556 | ** If a pointer to a MemPage object is passed as the second argument, |
| 5557 | ** its reference count is not altered by this function. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5558 | */ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5559 | static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){ |
| 5560 | MemPage *pTrunk = 0; /* Free-list trunk page */ |
| 5561 | Pgno iTrunk = 0; /* Page number of free-list trunk page */ |
| 5562 | MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */ |
| 5563 | MemPage *pPage; /* Page being freed. May be NULL. */ |
| 5564 | int rc; /* Return Code */ |
| 5565 | int nFree; /* Initial number of pages on free-list */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5566 | |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5567 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 5568 | assert( iPage>1 ); |
| 5569 | assert( !pMemPage || pMemPage->pgno==iPage ); |
| 5570 | |
| 5571 | if( pMemPage ){ |
| 5572 | pPage = pMemPage; |
| 5573 | sqlite3PagerRef(pPage->pDbPage); |
| 5574 | }else{ |
| 5575 | pPage = btreePageLookup(pBt, iPage); |
| 5576 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5577 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5578 | /* Increment the free page count on pPage1 */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5579 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5580 | if( rc ) goto freepage_out; |
| 5581 | nFree = get4byte(&pPage1->aData[36]); |
| 5582 | put4byte(&pPage1->aData[36], nFree+1); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5583 | |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 5584 | if( pBt->btsFlags & BTS_SECURE_DELETE ){ |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 5585 | /* If the secure_delete option is enabled, then |
| 5586 | ** always fully overwrite deleted information with zeros. |
| 5587 | */ |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 5588 | if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) ) |
shaneh | 84f4b2f | 2010-02-26 01:46:54 +0000 | [diff] [blame] | 5589 | || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0) |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 5590 | ){ |
| 5591 | goto freepage_out; |
| 5592 | } |
| 5593 | memset(pPage->aData, 0, pPage->pBt->pageSize); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5594 | } |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 5595 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5596 | /* If the database supports auto-vacuum, write an entry in the pointer-map |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5597 | ** to indicate that the page is free. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5598 | */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5599 | if( ISAUTOVACUUM ){ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 5600 | ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5601 | if( rc ) goto freepage_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5602 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5603 | |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5604 | /* Now manipulate the actual database free-list structure. There are two |
| 5605 | ** possibilities. If the free-list is currently empty, or if the first |
| 5606 | ** trunk page in the free-list is full, then this page will become a |
| 5607 | ** new free-list trunk page. Otherwise, it will become a leaf of the |
| 5608 | ** first trunk page in the current free-list. This block tests if it |
| 5609 | ** is possible to add the page as a new free-list leaf. |
| 5610 | */ |
| 5611 | if( nFree!=0 ){ |
drh | c046e3e | 2009-07-15 11:26:44 +0000 | [diff] [blame] | 5612 | u32 nLeaf; /* Initial number of leaf cells on trunk page */ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5613 | |
| 5614 | iTrunk = get4byte(&pPage1->aData[32]); |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 5615 | rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5616 | if( rc!=SQLITE_OK ){ |
| 5617 | goto freepage_out; |
| 5618 | } |
| 5619 | |
| 5620 | nLeaf = get4byte(&pTrunk->aData[4]); |
drh | eeb844a | 2009-08-08 18:01:07 +0000 | [diff] [blame] | 5621 | assert( pBt->usableSize>32 ); |
| 5622 | if( nLeaf > (u32)pBt->usableSize/4 - 2 ){ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5623 | rc = SQLITE_CORRUPT_BKPT; |
| 5624 | goto freepage_out; |
| 5625 | } |
drh | eeb844a | 2009-08-08 18:01:07 +0000 | [diff] [blame] | 5626 | if( nLeaf < (u32)pBt->usableSize/4 - 8 ){ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5627 | /* In this case there is room on the trunk page to insert the page |
| 5628 | ** being freed as a new leaf. |
drh | 45b1fac | 2008-07-04 17:52:42 +0000 | [diff] [blame] | 5629 | ** |
| 5630 | ** Note that the trunk page is not really full until it contains |
| 5631 | ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have |
| 5632 | ** coded. But due to a coding error in versions of SQLite prior to |
| 5633 | ** 3.6.0, databases with freelist trunk pages holding more than |
| 5634 | ** usableSize/4 - 8 entries will be reported as corrupt. In order |
| 5635 | ** to maintain backwards compatibility with older versions of SQLite, |
drh | c046e3e | 2009-07-15 11:26:44 +0000 | [diff] [blame] | 5636 | ** we will continue to restrict the number of entries to usableSize/4 - 8 |
drh | 45b1fac | 2008-07-04 17:52:42 +0000 | [diff] [blame] | 5637 | ** for now. At some point in the future (once everyone has upgraded |
| 5638 | ** to 3.6.0 or later) we should consider fixing the conditional above |
| 5639 | ** to read "usableSize/4-2" instead of "usableSize/4-8". |
drh | 113762a | 2014-11-19 16:36:25 +0000 | [diff] [blame] | 5640 | ** |
| 5641 | ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still |
| 5642 | ** avoid using the last six entries in the freelist trunk page array in |
| 5643 | ** order that database files created by newer versions of SQLite can be |
| 5644 | ** read by older versions of SQLite. |
drh | 45b1fac | 2008-07-04 17:52:42 +0000 | [diff] [blame] | 5645 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5646 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 5647 | if( rc==SQLITE_OK ){ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5648 | put4byte(&pTrunk->aData[4], nLeaf+1); |
| 5649 | put4byte(&pTrunk->aData[8+nLeaf*4], iPage); |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 5650 | if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5651 | sqlite3PagerDontWrite(pPage->pDbPage); |
| 5652 | } |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5653 | rc = btreeSetHasContent(pBt, iPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 5654 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5655 | 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] | 5656 | goto freepage_out; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5657 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5658 | } |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5659 | |
| 5660 | /* If control flows to this point, then it was not possible to add the |
| 5661 | ** the page being freed as a leaf page of the first trunk in the free-list. |
| 5662 | ** Possibly because the free-list is empty, or possibly because the |
| 5663 | ** first trunk in the free-list is full. Either way, the page being freed |
| 5664 | ** will become the new first trunk page in the free-list. |
| 5665 | */ |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 5666 | if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){ |
drh | c046e3e | 2009-07-15 11:26:44 +0000 | [diff] [blame] | 5667 | goto freepage_out; |
| 5668 | } |
| 5669 | rc = sqlite3PagerWrite(pPage->pDbPage); |
| 5670 | if( rc!=SQLITE_OK ){ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5671 | goto freepage_out; |
| 5672 | } |
| 5673 | put4byte(pPage->aData, iTrunk); |
| 5674 | put4byte(&pPage->aData[4], 0); |
| 5675 | put4byte(&pPage1->aData[32], iPage); |
| 5676 | TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk)); |
| 5677 | |
| 5678 | freepage_out: |
| 5679 | if( pPage ){ |
| 5680 | pPage->isInit = 0; |
| 5681 | } |
| 5682 | releasePage(pPage); |
| 5683 | releasePage(pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5684 | return rc; |
| 5685 | } |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 5686 | static void freePage(MemPage *pPage, int *pRC){ |
| 5687 | if( (*pRC)==SQLITE_OK ){ |
| 5688 | *pRC = freePage2(pPage->pBt, pPage, pPage->pgno); |
| 5689 | } |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5690 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5691 | |
| 5692 | /* |
drh | 9bfdc25 | 2014-09-24 02:05:41 +0000 | [diff] [blame] | 5693 | ** Free any overflow pages associated with the given Cell. Write the |
| 5694 | ** local Cell size (the number of bytes on the original page, omitting |
| 5695 | ** overflow) into *pnSize. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5696 | */ |
drh | 9bfdc25 | 2014-09-24 02:05:41 +0000 | [diff] [blame] | 5697 | static int clearCell( |
| 5698 | MemPage *pPage, /* The page that contains the Cell */ |
| 5699 | unsigned char *pCell, /* First byte of the Cell */ |
| 5700 | u16 *pnSize /* Write the size of the Cell here */ |
| 5701 | ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5702 | BtShared *pBt = pPage->pBt; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5703 | CellInfo info; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5704 | Pgno ovflPgno; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5705 | int rc; |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 5706 | int nOvfl; |
shaneh | 1df2db7 | 2010-08-18 02:28:48 +0000 | [diff] [blame] | 5707 | u32 ovflPageSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5708 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5709 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 5710 | btreeParseCellPtr(pPage, pCell, &info); |
drh | 9bfdc25 | 2014-09-24 02:05:41 +0000 | [diff] [blame] | 5711 | *pnSize = info.nSize; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5712 | if( info.iOverflow==0 ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5713 | return SQLITE_OK; /* No overflow pages. Return without doing anything */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5714 | } |
drh | e42a9b4 | 2011-08-31 13:27:19 +0000 | [diff] [blame] | 5715 | if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){ |
mistachkin | 70a1b71 | 2012-09-28 18:13:35 +0000 | [diff] [blame] | 5716 | return SQLITE_CORRUPT_BKPT; /* Cell extends past end of page */ |
drh | e42a9b4 | 2011-08-31 13:27:19 +0000 | [diff] [blame] | 5717 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5718 | ovflPgno = get4byte(&pCell[info.iOverflow]); |
shane | 63207ab | 2009-02-04 01:49:30 +0000 | [diff] [blame] | 5719 | assert( pBt->usableSize > 4 ); |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 5720 | ovflPageSize = pBt->usableSize - 4; |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 5721 | nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize; |
| 5722 | assert( ovflPgno==0 || nOvfl>0 ); |
| 5723 | while( nOvfl-- ){ |
shane | 63207ab | 2009-02-04 01:49:30 +0000 | [diff] [blame] | 5724 | Pgno iNext = 0; |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5725 | MemPage *pOvfl = 0; |
drh | b129915 | 2010-03-30 22:58:33 +0000 | [diff] [blame] | 5726 | if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){ |
danielk1977 | e589a67 | 2009-04-11 16:06:15 +0000 | [diff] [blame] | 5727 | /* 0 is not a legal page number and page 1 cannot be an |
| 5728 | ** overflow page. Therefore if ovflPgno<2 or past the end of the |
| 5729 | ** file the database must be corrupt. */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 5730 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 5731 | } |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5732 | if( nOvfl ){ |
| 5733 | rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext); |
| 5734 | if( rc ) return rc; |
| 5735 | } |
dan | 887d4b2 | 2010-02-25 12:09:16 +0000 | [diff] [blame] | 5736 | |
shaneh | 1da207e | 2010-03-09 14:41:12 +0000 | [diff] [blame] | 5737 | if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) ) |
dan | 887d4b2 | 2010-02-25 12:09:16 +0000 | [diff] [blame] | 5738 | && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1 |
| 5739 | ){ |
| 5740 | /* There is no reason any cursor should have an outstanding reference |
| 5741 | ** to an overflow page belonging to a cell that is being deleted/updated. |
| 5742 | ** So if there exists more than one reference to this page, then it |
| 5743 | ** must not really be an overflow page and the database must be corrupt. |
| 5744 | ** It is helpful to detect this before calling freePage2(), as |
| 5745 | ** freePage2() may zero the page contents if secure-delete mode is |
| 5746 | ** enabled. If this 'overflow' page happens to be a page that the |
| 5747 | ** caller is iterating through or using in some other way, this |
| 5748 | ** can be problematic. |
| 5749 | */ |
| 5750 | rc = SQLITE_CORRUPT_BKPT; |
| 5751 | }else{ |
| 5752 | rc = freePage2(pBt, pOvfl, ovflPgno); |
| 5753 | } |
| 5754 | |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5755 | if( pOvfl ){ |
| 5756 | sqlite3PagerUnref(pOvfl->pDbPage); |
| 5757 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5758 | if( rc ) return rc; |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5759 | ovflPgno = iNext; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5760 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5761 | return SQLITE_OK; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5762 | } |
| 5763 | |
| 5764 | /* |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5765 | ** Create the byte sequence used to represent a cell on page pPage |
| 5766 | ** and write that byte sequence into pCell[]. Overflow pages are |
| 5767 | ** allocated and filled in as necessary. The calling procedure |
| 5768 | ** is responsible for making sure sufficient space has been allocated |
| 5769 | ** for pCell[]. |
| 5770 | ** |
| 5771 | ** Note that pCell does not necessary need to point to the pPage->aData |
| 5772 | ** area. pCell might point to some temporary storage. The cell will |
| 5773 | ** be constructed in this temporary area then copied into pPage->aData |
| 5774 | ** later. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5775 | */ |
| 5776 | static int fillInCell( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5777 | MemPage *pPage, /* The page that contains the cell */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5778 | unsigned char *pCell, /* Complete text of the cell */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 5779 | const void *pKey, i64 nKey, /* The key */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5780 | const void *pData,int nData, /* The data */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5781 | int nZero, /* Extra zero bytes to append to pData */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5782 | int *pnSize /* Write cell size here */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5783 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5784 | int nPayload; |
drh | 8c6fa9b | 2004-05-26 00:01:53 +0000 | [diff] [blame] | 5785 | const u8 *pSrc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5786 | int nSrc, n, rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5787 | int spaceLeft; |
| 5788 | MemPage *pOvfl = 0; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 5789 | MemPage *pToRelease = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5790 | unsigned char *pPrior; |
| 5791 | unsigned char *pPayload; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5792 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5793 | Pgno pgnoOvfl = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5794 | int nHeader; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5795 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5796 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5797 | |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 5798 | /* pPage is not necessarily writeable since pCell might be auxiliary |
| 5799 | ** buffer space that is separate from the pPage buffer area */ |
| 5800 | assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize] |
| 5801 | || sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 5802 | |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5803 | /* Fill in the header. */ |
drh | 6200c88 | 2014-09-23 22:36:25 +0000 | [diff] [blame] | 5804 | nHeader = pPage->childPtrSize; |
| 5805 | nPayload = nData + nZero; |
drh | 3e28ff5 | 2014-09-24 00:59:08 +0000 | [diff] [blame] | 5806 | if( pPage->intKeyLeaf ){ |
drh | 6200c88 | 2014-09-23 22:36:25 +0000 | [diff] [blame] | 5807 | nHeader += putVarint32(&pCell[nHeader], nPayload); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5808 | }else{ |
drh | 6200c88 | 2014-09-23 22:36:25 +0000 | [diff] [blame] | 5809 | assert( nData==0 ); |
| 5810 | assert( nZero==0 ); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5811 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5812 | nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5813 | |
drh | 6200c88 | 2014-09-23 22:36:25 +0000 | [diff] [blame] | 5814 | /* Fill in the payload size */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5815 | if( pPage->intKey ){ |
| 5816 | pSrc = pData; |
| 5817 | nSrc = nData; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5818 | nData = 0; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 5819 | }else{ |
danielk1977 | 31d31b8 | 2009-07-13 13:18:07 +0000 | [diff] [blame] | 5820 | if( NEVER(nKey>0x7fffffff || pKey==0) ){ |
| 5821 | return SQLITE_CORRUPT_BKPT; |
drh | 20abac2 | 2009-01-28 20:21:17 +0000 | [diff] [blame] | 5822 | } |
drh | 6200c88 | 2014-09-23 22:36:25 +0000 | [diff] [blame] | 5823 | nPayload = (int)nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5824 | pSrc = pKey; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 5825 | nSrc = (int)nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5826 | } |
drh | 6200c88 | 2014-09-23 22:36:25 +0000 | [diff] [blame] | 5827 | if( nPayload<=pPage->maxLocal ){ |
| 5828 | n = nHeader + nPayload; |
| 5829 | testcase( n==3 ); |
| 5830 | testcase( n==4 ); |
| 5831 | if( n<4 ) n = 4; |
| 5832 | *pnSize = n; |
| 5833 | spaceLeft = nPayload; |
| 5834 | pPrior = pCell; |
| 5835 | }else{ |
| 5836 | int mn = pPage->minLocal; |
| 5837 | n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4); |
| 5838 | testcase( n==pPage->maxLocal ); |
| 5839 | testcase( n==pPage->maxLocal+1 ); |
| 5840 | if( n > pPage->maxLocal ) n = mn; |
| 5841 | spaceLeft = n; |
| 5842 | *pnSize = n + nHeader + 4; |
| 5843 | pPrior = &pCell[nHeader+n]; |
| 5844 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5845 | pPayload = &pCell[nHeader]; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5846 | |
drh | 6200c88 | 2014-09-23 22:36:25 +0000 | [diff] [blame] | 5847 | /* At this point variables should be set as follows: |
| 5848 | ** |
| 5849 | ** nPayload Total payload size in bytes |
| 5850 | ** pPayload Begin writing payload here |
| 5851 | ** spaceLeft Space available at pPayload. If nPayload>spaceLeft, |
| 5852 | ** that means content must spill into overflow pages. |
| 5853 | ** *pnSize Size of the local cell (not counting overflow pages) |
| 5854 | ** pPrior Where to write the pgno of the first overflow page |
| 5855 | ** |
| 5856 | ** Use a call to btreeParseCellPtr() to verify that the values above |
| 5857 | ** were computed correctly. |
| 5858 | */ |
| 5859 | #if SQLITE_DEBUG |
| 5860 | { |
| 5861 | CellInfo info; |
| 5862 | btreeParseCellPtr(pPage, pCell, &info); |
| 5863 | assert( nHeader=(int)(info.pPayload - pCell) ); |
| 5864 | assert( info.nKey==nKey ); |
| 5865 | assert( *pnSize == info.nSize ); |
| 5866 | assert( spaceLeft == info.nLocal ); |
| 5867 | assert( pPrior == &pCell[info.iOverflow] ); |
| 5868 | } |
| 5869 | #endif |
| 5870 | |
| 5871 | /* Write the payload into the local Cell and any extra into overflow pages */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5872 | while( nPayload>0 ){ |
| 5873 | if( spaceLeft==0 ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5874 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5875 | Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */ |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 5876 | if( pBt->autoVacuum ){ |
| 5877 | do{ |
| 5878 | pgnoOvfl++; |
| 5879 | } while( |
| 5880 | PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt) |
| 5881 | ); |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 5882 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5883 | #endif |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 5884 | rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5885 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 5886 | /* If the database supports auto-vacuum, and the second or subsequent |
| 5887 | ** overflow page is being allocated, add an entry to the pointer-map |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 5888 | ** for that page now. |
| 5889 | ** |
| 5890 | ** If this is the first overflow page, then write a partial entry |
| 5891 | ** to the pointer-map. If we write nothing to this pointer-map slot, |
| 5892 | ** then the optimistic overflow chain processing in clearCell() |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 5893 | ** may misinterpret the uninitialized values and delete the |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 5894 | ** wrong pages from the database. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5895 | */ |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 5896 | if( pBt->autoVacuum && rc==SQLITE_OK ){ |
| 5897 | u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1); |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 5898 | ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc); |
danielk1977 | 89a4be8 | 2007-05-23 13:34:32 +0000 | [diff] [blame] | 5899 | if( rc ){ |
| 5900 | releasePage(pOvfl); |
| 5901 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5902 | } |
| 5903 | #endif |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5904 | if( rc ){ |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 5905 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5906 | return rc; |
| 5907 | } |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 5908 | |
| 5909 | /* If pToRelease is not zero than pPrior points into the data area |
| 5910 | ** of pToRelease. Make sure pToRelease is still writeable. */ |
| 5911 | assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) ); |
| 5912 | |
| 5913 | /* If pPrior is part of the data area of pPage, then make sure pPage |
| 5914 | ** is still writeable */ |
| 5915 | assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize] |
| 5916 | || sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 5917 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5918 | put4byte(pPrior, pgnoOvfl); |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 5919 | releasePage(pToRelease); |
| 5920 | pToRelease = pOvfl; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5921 | pPrior = pOvfl->aData; |
| 5922 | put4byte(pPrior, 0); |
| 5923 | pPayload = &pOvfl->aData[4]; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5924 | spaceLeft = pBt->usableSize - 4; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5925 | } |
| 5926 | n = nPayload; |
| 5927 | if( n>spaceLeft ) n = spaceLeft; |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 5928 | |
| 5929 | /* If pToRelease is not zero than pPayload points into the data area |
| 5930 | ** of pToRelease. Make sure pToRelease is still writeable. */ |
| 5931 | assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) ); |
| 5932 | |
| 5933 | /* If pPayload is part of the data area of pPage, then make sure pPage |
| 5934 | ** is still writeable */ |
| 5935 | assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize] |
| 5936 | || sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 5937 | |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5938 | if( nSrc>0 ){ |
| 5939 | if( n>nSrc ) n = nSrc; |
| 5940 | assert( pSrc ); |
| 5941 | memcpy(pPayload, pSrc, n); |
| 5942 | }else{ |
| 5943 | memset(pPayload, 0, n); |
| 5944 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5945 | nPayload -= n; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 5946 | pPayload += n; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 5947 | pSrc += n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5948 | nSrc -= n; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5949 | spaceLeft -= n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5950 | if( nSrc==0 ){ |
| 5951 | nSrc = nData; |
| 5952 | pSrc = pData; |
| 5953 | } |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 5954 | } |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 5955 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5956 | return SQLITE_OK; |
| 5957 | } |
| 5958 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5959 | /* |
| 5960 | ** Remove the i-th cell from pPage. This routine effects pPage only. |
| 5961 | ** The cell content is not freed or deallocated. It is assumed that |
| 5962 | ** the cell content has been copied someplace else. This routine just |
| 5963 | ** removes the reference to the cell from pPage. |
| 5964 | ** |
| 5965 | ** "sz" must be the number of bytes in the cell. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5966 | */ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 5967 | static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){ |
drh | 43b18e1 | 2010-08-17 19:40:08 +0000 | [diff] [blame] | 5968 | u32 pc; /* Offset to cell content of cell being deleted */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5969 | u8 *data; /* pPage->aData */ |
| 5970 | u8 *ptr; /* Used to move bytes around within data[] */ |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 5971 | int rc; /* The return code */ |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 5972 | int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5973 | |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 5974 | if( *pRC ) return; |
| 5975 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5976 | assert( idx>=0 && idx<pPage->nCell ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5977 | assert( sz==cellSize(pPage, idx) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5978 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5979 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5980 | data = pPage->aData; |
drh | 3def235 | 2011-11-11 00:27:15 +0000 | [diff] [blame] | 5981 | ptr = &pPage->aCellIdx[2*idx]; |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 5982 | pc = get2byte(ptr); |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 5983 | hdr = pPage->hdrOffset; |
| 5984 | testcase( pc==get2byte(&data[hdr+5]) ); |
| 5985 | testcase( pc+sz==pPage->pBt->usableSize ); |
drh | 43b18e1 | 2010-08-17 19:40:08 +0000 | [diff] [blame] | 5986 | if( pc < (u32)get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 5987 | *pRC = SQLITE_CORRUPT_BKPT; |
| 5988 | return; |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 5989 | } |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 5990 | rc = freeSpace(pPage, pc, sz); |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 5991 | if( rc ){ |
| 5992 | *pRC = rc; |
| 5993 | return; |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 5994 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5995 | pPage->nCell--; |
drh | fdab026 | 2014-11-20 15:30:50 +0000 | [diff] [blame] | 5996 | if( pPage->nCell==0 ){ |
| 5997 | memset(&data[hdr+1], 0, 4); |
| 5998 | data[hdr+7] = 0; |
| 5999 | put2byte(&data[hdr+5], pPage->pBt->usableSize); |
| 6000 | pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset |
| 6001 | - pPage->childPtrSize - 8; |
| 6002 | }else{ |
| 6003 | memmove(ptr, ptr+2, 2*(pPage->nCell - idx)); |
| 6004 | put2byte(&data[hdr+3], pPage->nCell); |
| 6005 | pPage->nFree += 2; |
| 6006 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6007 | } |
| 6008 | |
| 6009 | /* |
| 6010 | ** Insert a new cell on pPage at cell index "i". pCell points to the |
| 6011 | ** content of the cell. |
| 6012 | ** |
| 6013 | ** 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] | 6014 | ** will not fit, then make a copy of the cell content into pTemp if |
| 6015 | ** pTemp is not null. Regardless of pTemp, allocate a new entry |
drh | 2cbd78b | 2012-02-02 19:37:18 +0000 | [diff] [blame] | 6016 | ** in pPage->apOvfl[] and make it point to the cell content (either |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6017 | ** in pTemp or the original pCell) and also record its index. |
| 6018 | ** Allocating a new entry in pPage->aCell[] implies that |
| 6019 | ** pPage->nOverflow is incremented. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6020 | */ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6021 | static void insertCell( |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 6022 | MemPage *pPage, /* Page into which we are copying */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6023 | int i, /* New cell becomes the i-th cell of the page */ |
| 6024 | u8 *pCell, /* Content of the new cell */ |
| 6025 | int sz, /* Bytes of content in pCell */ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 6026 | u8 *pTemp, /* Temp storage space for pCell, if needed */ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6027 | Pgno iChild, /* If non-zero, replace first 4 bytes with this value */ |
| 6028 | int *pRC /* Read and write return code from here */ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 6029 | ){ |
drh | 383d30f | 2010-02-26 13:07:37 +0000 | [diff] [blame] | 6030 | int idx = 0; /* Where to write new cell content in data[] */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6031 | int j; /* Loop counter */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6032 | int end; /* First byte past the last cell pointer in data[] */ |
| 6033 | int ins; /* Index in data[] where new cell pointer is inserted */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6034 | int cellOffset; /* Address of first cell pointer in data[] */ |
| 6035 | u8 *data; /* The content of the whole page */ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6036 | |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6037 | if( *pRC ) return; |
| 6038 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6039 | assert( i>=0 && i<=pPage->nCell+pPage->nOverflow ); |
dan | f216e32 | 2014-08-14 19:53:37 +0000 | [diff] [blame] | 6040 | assert( MX_CELL(pPage->pBt)<=10921 ); |
| 6041 | assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB ); |
drh | 2cbd78b | 2012-02-02 19:37:18 +0000 | [diff] [blame] | 6042 | assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) ); |
| 6043 | assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6044 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | c9b9b8a | 2009-12-03 21:26:52 +0000 | [diff] [blame] | 6045 | /* The cell should normally be sized correctly. However, when moving a |
| 6046 | ** malformed cell from a leaf page to an interior page, if the cell size |
| 6047 | ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size |
| 6048 | ** might be less than 8 (leaf-size + pointer) on the interior node. Hence |
| 6049 | ** the term after the || in the following assert(). */ |
| 6050 | assert( sz==cellSizePtr(pPage, pCell) || (sz==8 && iChild>0) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6051 | if( pPage->nOverflow || sz+2>pPage->nFree ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 6052 | if( pTemp ){ |
drh | d6176c4 | 2014-10-11 17:22:55 +0000 | [diff] [blame] | 6053 | memcpy(pTemp, pCell, sz); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6054 | pCell = pTemp; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 6055 | } |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6056 | if( iChild ){ |
| 6057 | put4byte(pCell, iChild); |
| 6058 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6059 | j = pPage->nOverflow++; |
drh | 2cbd78b | 2012-02-02 19:37:18 +0000 | [diff] [blame] | 6060 | assert( j<(int)(sizeof(pPage->apOvfl)/sizeof(pPage->apOvfl[0])) ); |
| 6061 | pPage->apOvfl[j] = pCell; |
| 6062 | pPage->aiOvfl[j] = (u16)i; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6063 | }else{ |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 6064 | int rc = sqlite3PagerWrite(pPage->pDbPage); |
| 6065 | if( rc!=SQLITE_OK ){ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6066 | *pRC = rc; |
| 6067 | return; |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 6068 | } |
| 6069 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6070 | data = pPage->aData; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6071 | cellOffset = pPage->cellOffset; |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 6072 | end = cellOffset + 2*pPage->nCell; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6073 | ins = cellOffset + 2*i; |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 6074 | rc = allocateSpace(pPage, sz, &idx); |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6075 | if( rc ){ *pRC = rc; return; } |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 6076 | /* The allocateSpace() routine guarantees the following two properties |
| 6077 | ** if it returns success */ |
| 6078 | assert( idx >= end+2 ); |
drh | fcd71b6 | 2011-04-05 22:08:24 +0000 | [diff] [blame] | 6079 | assert( idx+sz <= (int)pPage->pBt->usableSize ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6080 | pPage->nCell++; |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 6081 | pPage->nFree -= (u16)(2 + sz); |
drh | d6176c4 | 2014-10-11 17:22:55 +0000 | [diff] [blame] | 6082 | memcpy(&data[idx], pCell, sz); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6083 | if( iChild ){ |
| 6084 | put4byte(&data[idx], iChild); |
| 6085 | } |
drh | 8f51883 | 2013-12-09 02:32:19 +0000 | [diff] [blame] | 6086 | memmove(&data[ins+2], &data[ins], end-ins); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6087 | put2byte(&data[ins], idx); |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 6088 | put2byte(&data[pPage->hdrOffset+3], pPage->nCell); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 6089 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6090 | if( pPage->pBt->autoVacuum ){ |
| 6091 | /* The cell may contain a pointer to an overflow page. If so, write |
| 6092 | ** the entry for the overflow page into the pointer map. |
| 6093 | */ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6094 | ptrmapPutOvflPtr(pPage, pCell, pRC); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 6095 | } |
| 6096 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6097 | } |
| 6098 | } |
| 6099 | |
| 6100 | /* |
dan | 8e9ba0c | 2014-10-14 17:27:04 +0000 | [diff] [blame] | 6101 | ** Array apCell[] contains pointers to nCell b-tree page cells. The |
| 6102 | ** szCell[] array contains the size in bytes of each cell. This function |
| 6103 | ** replaces the current contents of page pPg with the contents of the cell |
| 6104 | ** array. |
| 6105 | ** |
| 6106 | ** Some of the cells in apCell[] may currently be stored in pPg. This |
| 6107 | ** function works around problems caused by this by making a copy of any |
| 6108 | ** such cells before overwriting the page data. |
| 6109 | ** |
| 6110 | ** The MemPage.nFree field is invalidated by this function. It is the |
| 6111 | ** responsibility of the caller to set it correctly. |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 6112 | */ |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 6113 | static void rebuildPage( |
| 6114 | MemPage *pPg, /* Edit this page */ |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 6115 | int nCell, /* Final number of cells on page */ |
dan | 09c6840 | 2014-10-11 20:00:24 +0000 | [diff] [blame] | 6116 | u8 **apCell, /* Array of cells */ |
| 6117 | u16 *szCell /* Array of cell sizes */ |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 6118 | ){ |
| 6119 | const int hdr = pPg->hdrOffset; /* Offset of header on pPg */ |
| 6120 | u8 * const aData = pPg->aData; /* Pointer to data for pPg */ |
| 6121 | const int usableSize = pPg->pBt->usableSize; |
| 6122 | u8 * const pEnd = &aData[usableSize]; |
| 6123 | int i; |
| 6124 | u8 *pCellptr = pPg->aCellIdx; |
| 6125 | u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager); |
| 6126 | u8 *pData; |
| 6127 | |
| 6128 | i = get2byte(&aData[hdr+5]); |
| 6129 | memcpy(&pTmp[i], &aData[i], usableSize - i); |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 6130 | |
dan | 8e9ba0c | 2014-10-14 17:27:04 +0000 | [diff] [blame] | 6131 | pData = pEnd; |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 6132 | for(i=0; i<nCell; i++){ |
| 6133 | u8 *pCell = apCell[i]; |
| 6134 | if( pCell>aData && pCell<pEnd ){ |
| 6135 | pCell = &pTmp[pCell - aData]; |
| 6136 | } |
| 6137 | pData -= szCell[i]; |
| 6138 | memcpy(pData, pCell, szCell[i]); |
| 6139 | put2byte(pCellptr, (pData - aData)); |
| 6140 | pCellptr += 2; |
| 6141 | assert( szCell[i]==cellSizePtr(pPg, pCell) ); |
| 6142 | } |
| 6143 | |
dan | d7b545b | 2014-10-13 18:03:27 +0000 | [diff] [blame] | 6144 | /* The pPg->nFree field is now set incorrectly. The caller will fix it. */ |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 6145 | pPg->nCell = nCell; |
| 6146 | pPg->nOverflow = 0; |
| 6147 | |
| 6148 | put2byte(&aData[hdr+1], 0); |
| 6149 | put2byte(&aData[hdr+3], pPg->nCell); |
| 6150 | put2byte(&aData[hdr+5], pData - aData); |
| 6151 | aData[hdr+7] = 0x00; |
| 6152 | } |
| 6153 | |
dan | 8e9ba0c | 2014-10-14 17:27:04 +0000 | [diff] [blame] | 6154 | /* |
| 6155 | ** Array apCell[] contains nCell pointers to b-tree cells. Array szCell |
| 6156 | ** contains the size in bytes of each such cell. This function attempts to |
| 6157 | ** add the cells stored in the array to page pPg. If it cannot (because |
| 6158 | ** the page needs to be defragmented before the cells will fit), non-zero |
| 6159 | ** is returned. Otherwise, if the cells are added successfully, zero is |
| 6160 | ** returned. |
| 6161 | ** |
| 6162 | ** Argument pCellptr points to the first entry in the cell-pointer array |
| 6163 | ** (part of page pPg) to populate. After cell apCell[0] is written to the |
| 6164 | ** page body, a 16-bit offset is written to pCellptr. And so on, for each |
| 6165 | ** cell in the array. It is the responsibility of the caller to ensure |
| 6166 | ** that it is safe to overwrite this part of the cell-pointer array. |
| 6167 | ** |
| 6168 | ** When this function is called, *ppData points to the start of the |
| 6169 | ** content area on page pPg. If the size of the content area is extended, |
| 6170 | ** *ppData is updated to point to the new start of the content area |
| 6171 | ** before returning. |
| 6172 | ** |
| 6173 | ** Finally, argument pBegin points to the byte immediately following the |
| 6174 | ** end of the space required by this page for the cell-pointer area (for |
| 6175 | ** all cells - not just those inserted by the current call). If the content |
| 6176 | ** area must be extended to before this point in order to accomodate all |
| 6177 | ** cells in apCell[], then the cells do not fit and non-zero is returned. |
| 6178 | */ |
dan | d7b545b | 2014-10-13 18:03:27 +0000 | [diff] [blame] | 6179 | static int pageInsertArray( |
dan | 8e9ba0c | 2014-10-14 17:27:04 +0000 | [diff] [blame] | 6180 | MemPage *pPg, /* Page to add cells to */ |
| 6181 | u8 *pBegin, /* End of cell-pointer array */ |
| 6182 | u8 **ppData, /* IN/OUT: Page content -area pointer */ |
| 6183 | u8 *pCellptr, /* Pointer to cell-pointer area */ |
| 6184 | int nCell, /* Number of cells to add to pPg */ |
dan | d7b545b | 2014-10-13 18:03:27 +0000 | [diff] [blame] | 6185 | u8 **apCell, /* Array of cells */ |
| 6186 | u16 *szCell /* Array of cell sizes */ |
| 6187 | ){ |
| 6188 | int i; |
| 6189 | u8 *aData = pPg->aData; |
| 6190 | u8 *pData = *ppData; |
dan | 8e9ba0c | 2014-10-14 17:27:04 +0000 | [diff] [blame] | 6191 | const int bFreelist = aData[1] || aData[2]; |
dan | 23eba45 | 2014-10-24 18:43:57 +0000 | [diff] [blame] | 6192 | assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */ |
dan | d7b545b | 2014-10-13 18:03:27 +0000 | [diff] [blame] | 6193 | for(i=0; i<nCell; i++){ |
| 6194 | int sz = szCell[i]; |
drh | ba0f999 | 2014-10-30 20:48:44 +0000 | [diff] [blame] | 6195 | int rc; |
dan | d7b545b | 2014-10-13 18:03:27 +0000 | [diff] [blame] | 6196 | u8 *pSlot; |
drh | ba0f999 | 2014-10-30 20:48:44 +0000 | [diff] [blame] | 6197 | if( bFreelist==0 || (pSlot = pageFindSlot(pPg, sz, &rc, 0))==0 ){ |
dan | d7b545b | 2014-10-13 18:03:27 +0000 | [diff] [blame] | 6198 | pData -= sz; |
| 6199 | if( pData<pBegin ) return 1; |
| 6200 | pSlot = pData; |
| 6201 | } |
| 6202 | memcpy(pSlot, apCell[i], sz); |
| 6203 | put2byte(pCellptr, (pSlot - aData)); |
| 6204 | pCellptr += 2; |
| 6205 | } |
| 6206 | *ppData = pData; |
| 6207 | return 0; |
| 6208 | } |
| 6209 | |
dan | 8e9ba0c | 2014-10-14 17:27:04 +0000 | [diff] [blame] | 6210 | /* |
| 6211 | ** Array apCell[] contains nCell pointers to b-tree cells. Array szCell |
| 6212 | ** contains the size in bytes of each such cell. This function adds the |
| 6213 | ** space associated with each cell in the array that is currently stored |
| 6214 | ** within the body of pPg to the pPg free-list. The cell-pointers and other |
| 6215 | ** fields of the page are not updated. |
| 6216 | ** |
| 6217 | ** This function returns the total number of cells added to the free-list. |
| 6218 | */ |
dan | d7b545b | 2014-10-13 18:03:27 +0000 | [diff] [blame] | 6219 | static int pageFreeArray( |
| 6220 | MemPage *pPg, /* Page to edit */ |
| 6221 | int nCell, /* Cells to delete */ |
| 6222 | u8 **apCell, /* Array of cells */ |
| 6223 | u16 *szCell /* Array of cell sizes */ |
| 6224 | ){ |
| 6225 | u8 * const aData = pPg->aData; |
| 6226 | u8 * const pEnd = &aData[pPg->pBt->usableSize]; |
dan | 89ca0b3 | 2014-10-25 20:36:28 +0000 | [diff] [blame] | 6227 | u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize]; |
dan | d7b545b | 2014-10-13 18:03:27 +0000 | [diff] [blame] | 6228 | int nRet = 0; |
| 6229 | int i; |
| 6230 | u8 *pFree = 0; |
| 6231 | int szFree = 0; |
| 6232 | |
| 6233 | for(i=0; i<nCell; i++){ |
| 6234 | u8 *pCell = apCell[i]; |
dan | 89ca0b3 | 2014-10-25 20:36:28 +0000 | [diff] [blame] | 6235 | if( pCell>=pStart && pCell<pEnd ){ |
dan | d7b545b | 2014-10-13 18:03:27 +0000 | [diff] [blame] | 6236 | int sz = szCell[i]; |
| 6237 | if( pFree!=(pCell + sz) ){ |
drh | fefa094 | 2014-11-05 21:21:08 +0000 | [diff] [blame] | 6238 | if( pFree ){ |
| 6239 | assert( pFree>aData && (pFree - aData)<65536 ); |
| 6240 | freeSpace(pPg, (u16)(pFree - aData), szFree); |
| 6241 | } |
dan | d7b545b | 2014-10-13 18:03:27 +0000 | [diff] [blame] | 6242 | pFree = pCell; |
| 6243 | szFree = sz; |
dan | 89ca0b3 | 2014-10-25 20:36:28 +0000 | [diff] [blame] | 6244 | if( pFree+sz>pEnd ) return 0; |
dan | d7b545b | 2014-10-13 18:03:27 +0000 | [diff] [blame] | 6245 | }else{ |
| 6246 | pFree = pCell; |
| 6247 | szFree += sz; |
| 6248 | } |
| 6249 | nRet++; |
| 6250 | } |
| 6251 | } |
drh | fefa094 | 2014-11-05 21:21:08 +0000 | [diff] [blame] | 6252 | if( pFree ){ |
| 6253 | assert( pFree>aData && (pFree - aData)<65536 ); |
| 6254 | freeSpace(pPg, (u16)(pFree - aData), szFree); |
| 6255 | } |
dan | d7b545b | 2014-10-13 18:03:27 +0000 | [diff] [blame] | 6256 | return nRet; |
| 6257 | } |
| 6258 | |
dan | d7b545b | 2014-10-13 18:03:27 +0000 | [diff] [blame] | 6259 | /* |
drh | 5ab6377 | 2014-11-27 03:46:04 +0000 | [diff] [blame] | 6260 | ** apCell[] and szCell[] contains pointers to and sizes of all cells in the |
| 6261 | ** pages being balanced. The current page, pPg, has pPg->nCell cells starting |
| 6262 | ** with apCell[iOld]. After balancing, this page should hold nNew cells |
| 6263 | ** starting at apCell[iNew]. |
| 6264 | ** |
| 6265 | ** This routine makes the necessary adjustments to pPg so that it contains |
| 6266 | ** the correct cells after being balanced. |
| 6267 | ** |
dan | d7b545b | 2014-10-13 18:03:27 +0000 | [diff] [blame] | 6268 | ** The pPg->nFree field is invalid when this function returns. It is the |
| 6269 | ** responsibility of the caller to set it correctly. |
| 6270 | */ |
dan | 09c6840 | 2014-10-11 20:00:24 +0000 | [diff] [blame] | 6271 | static void editPage( |
| 6272 | MemPage *pPg, /* Edit this page */ |
| 6273 | int iOld, /* Index of first cell currently on page */ |
| 6274 | int iNew, /* Index of new first cell on page */ |
| 6275 | int nNew, /* Final number of cells on page */ |
| 6276 | u8 **apCell, /* Array of cells */ |
| 6277 | u16 *szCell /* Array of cell sizes */ |
| 6278 | ){ |
dan | d7b545b | 2014-10-13 18:03:27 +0000 | [diff] [blame] | 6279 | u8 * const aData = pPg->aData; |
| 6280 | const int hdr = pPg->hdrOffset; |
| 6281 | u8 *pBegin = &pPg->aCellIdx[nNew * 2]; |
| 6282 | int nCell = pPg->nCell; /* Cells stored on pPg */ |
| 6283 | u8 *pData; |
| 6284 | u8 *pCellptr; |
| 6285 | int i; |
| 6286 | int iOldEnd = iOld + pPg->nCell + pPg->nOverflow; |
| 6287 | int iNewEnd = iNew + nNew; |
dan | 09c6840 | 2014-10-11 20:00:24 +0000 | [diff] [blame] | 6288 | |
| 6289 | #ifdef SQLITE_DEBUG |
dan | d7b545b | 2014-10-13 18:03:27 +0000 | [diff] [blame] | 6290 | u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager); |
| 6291 | memcpy(pTmp, aData, pPg->pBt->usableSize); |
dan | 09c6840 | 2014-10-11 20:00:24 +0000 | [diff] [blame] | 6292 | #endif |
| 6293 | |
dan | d7b545b | 2014-10-13 18:03:27 +0000 | [diff] [blame] | 6294 | /* Remove cells from the start and end of the page */ |
| 6295 | if( iOld<iNew ){ |
| 6296 | int nShift = pageFreeArray( |
| 6297 | pPg, iNew-iOld, &apCell[iOld], &szCell[iOld] |
| 6298 | ); |
| 6299 | memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2); |
| 6300 | nCell -= nShift; |
| 6301 | } |
| 6302 | if( iNewEnd < iOldEnd ){ |
| 6303 | nCell -= pageFreeArray( |
| 6304 | pPg, iOldEnd-iNewEnd, &apCell[iNewEnd], &szCell[iNewEnd] |
| 6305 | ); |
| 6306 | } |
dan | 09c6840 | 2014-10-11 20:00:24 +0000 | [diff] [blame] | 6307 | |
drh | 5ab6377 | 2014-11-27 03:46:04 +0000 | [diff] [blame] | 6308 | pData = &aData[get2byteNotZero(&aData[hdr+5])]; |
dan | d7b545b | 2014-10-13 18:03:27 +0000 | [diff] [blame] | 6309 | if( pData<pBegin ) goto editpage_fail; |
| 6310 | |
| 6311 | /* Add cells to the start of the page */ |
| 6312 | if( iNew<iOld ){ |
drh | 5ab6377 | 2014-11-27 03:46:04 +0000 | [diff] [blame] | 6313 | int nAdd = MIN(nNew,iOld-iNew); |
| 6314 | assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB ); |
dan | d7b545b | 2014-10-13 18:03:27 +0000 | [diff] [blame] | 6315 | pCellptr = pPg->aCellIdx; |
| 6316 | memmove(&pCellptr[nAdd*2], pCellptr, nCell*2); |
| 6317 | if( pageInsertArray( |
| 6318 | pPg, pBegin, &pData, pCellptr, |
| 6319 | nAdd, &apCell[iNew], &szCell[iNew] |
| 6320 | ) ) goto editpage_fail; |
| 6321 | nCell += nAdd; |
| 6322 | } |
| 6323 | |
| 6324 | /* Add any overflow cells */ |
| 6325 | for(i=0; i<pPg->nOverflow; i++){ |
| 6326 | int iCell = (iOld + pPg->aiOvfl[i]) - iNew; |
| 6327 | if( iCell>=0 && iCell<nNew ){ |
drh | fefa094 | 2014-11-05 21:21:08 +0000 | [diff] [blame] | 6328 | pCellptr = &pPg->aCellIdx[iCell * 2]; |
dan | d7b545b | 2014-10-13 18:03:27 +0000 | [diff] [blame] | 6329 | memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2); |
| 6330 | nCell++; |
| 6331 | if( pageInsertArray( |
| 6332 | pPg, pBegin, &pData, pCellptr, |
| 6333 | 1, &apCell[iCell + iNew], &szCell[iCell + iNew] |
| 6334 | ) ) goto editpage_fail; |
dan | 09c6840 | 2014-10-11 20:00:24 +0000 | [diff] [blame] | 6335 | } |
dan | d7b545b | 2014-10-13 18:03:27 +0000 | [diff] [blame] | 6336 | } |
dan | 09c6840 | 2014-10-11 20:00:24 +0000 | [diff] [blame] | 6337 | |
dan | d7b545b | 2014-10-13 18:03:27 +0000 | [diff] [blame] | 6338 | /* Append cells to the end of the page */ |
| 6339 | pCellptr = &pPg->aCellIdx[nCell*2]; |
| 6340 | if( pageInsertArray( |
| 6341 | pPg, pBegin, &pData, pCellptr, |
| 6342 | nNew-nCell, &apCell[iNew+nCell], &szCell[iNew+nCell] |
| 6343 | ) ) goto editpage_fail; |
dan | 09c6840 | 2014-10-11 20:00:24 +0000 | [diff] [blame] | 6344 | |
dan | d7b545b | 2014-10-13 18:03:27 +0000 | [diff] [blame] | 6345 | pPg->nCell = nNew; |
| 6346 | pPg->nOverflow = 0; |
dan | 09c6840 | 2014-10-11 20:00:24 +0000 | [diff] [blame] | 6347 | |
dan | d7b545b | 2014-10-13 18:03:27 +0000 | [diff] [blame] | 6348 | put2byte(&aData[hdr+3], pPg->nCell); |
| 6349 | put2byte(&aData[hdr+5], pData - aData); |
dan | 09c6840 | 2014-10-11 20:00:24 +0000 | [diff] [blame] | 6350 | |
| 6351 | #ifdef SQLITE_DEBUG |
dan | 23eba45 | 2014-10-24 18:43:57 +0000 | [diff] [blame] | 6352 | for(i=0; i<nNew && !CORRUPT_DB; i++){ |
dan | d7b545b | 2014-10-13 18:03:27 +0000 | [diff] [blame] | 6353 | u8 *pCell = apCell[i+iNew]; |
| 6354 | int iOff = get2byte(&pPg->aCellIdx[i*2]); |
| 6355 | if( pCell>=aData && pCell<&aData[pPg->pBt->usableSize] ){ |
| 6356 | pCell = &pTmp[pCell - aData]; |
dan | 09c6840 | 2014-10-11 20:00:24 +0000 | [diff] [blame] | 6357 | } |
dan | d7b545b | 2014-10-13 18:03:27 +0000 | [diff] [blame] | 6358 | assert( 0==memcmp(pCell, &aData[iOff], szCell[i+iNew]) ); |
| 6359 | } |
dan | 09c6840 | 2014-10-11 20:00:24 +0000 | [diff] [blame] | 6360 | #endif |
| 6361 | |
dan | d7b545b | 2014-10-13 18:03:27 +0000 | [diff] [blame] | 6362 | return; |
dan | 09c6840 | 2014-10-11 20:00:24 +0000 | [diff] [blame] | 6363 | editpage_fail: |
dan | 09c6840 | 2014-10-11 20:00:24 +0000 | [diff] [blame] | 6364 | /* Unable to edit this page. Rebuild it from scratch instead. */ |
| 6365 | rebuildPage(pPg, nNew, &apCell[iNew], &szCell[iNew]); |
| 6366 | } |
| 6367 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6368 | /* |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 6369 | ** The following parameters determine how many adjacent pages get involved |
| 6370 | ** in a balancing operation. NN is the number of neighbors on either side |
| 6371 | ** of the page that participate in the balancing operation. NB is the |
| 6372 | ** total number of pages that participate, including the target page and |
| 6373 | ** NN neighbors on either side. |
| 6374 | ** |
| 6375 | ** The minimum value of NN is 1 (of course). Increasing NN above 1 |
| 6376 | ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance |
| 6377 | ** in exchange for a larger degradation in INSERT and UPDATE performance. |
| 6378 | ** The value of NN appears to give the best results overall. |
| 6379 | */ |
| 6380 | #define NN 1 /* Number of neighbors on either side of pPage */ |
| 6381 | #define NB (NN*2+1) /* Total pages involved in the balance */ |
| 6382 | |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 6383 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 6384 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 6385 | /* |
| 6386 | ** This version of balance() handles the common special case where |
| 6387 | ** a new entry is being inserted on the extreme right-end of the |
| 6388 | ** tree, in other words, when the new entry will become the largest |
| 6389 | ** entry in the tree. |
| 6390 | ** |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 6391 | ** Instead of trying to balance the 3 right-most leaf pages, just add |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 6392 | ** a new page to the right-hand side and put the one new entry in |
| 6393 | ** that page. This leaves the right side of the tree somewhat |
| 6394 | ** unbalanced. But odds are that we will be inserting new entries |
| 6395 | ** at the end soon afterwards so the nearly empty page will quickly |
| 6396 | ** fill up. On average. |
| 6397 | ** |
| 6398 | ** pPage is the leaf page which is the right-most page in the tree. |
| 6399 | ** pParent is its parent. pPage must have a single overflow entry |
| 6400 | ** which is also the right-most entry on the page. |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6401 | ** |
| 6402 | ** The pSpace buffer is used to store a temporary copy of the divider |
| 6403 | ** cell that will be inserted into pParent. Such a cell consists of a 4 |
| 6404 | ** byte page number followed by a variable length integer. In other |
| 6405 | ** words, at most 13 bytes. Hence the pSpace buffer must be at |
| 6406 | ** least 13 bytes in size. |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 6407 | */ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6408 | static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){ |
| 6409 | BtShared *const pBt = pPage->pBt; /* B-Tree Database */ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6410 | MemPage *pNew; /* Newly allocated page */ |
danielk1977 | 6f235cc | 2009-06-04 14:46:08 +0000 | [diff] [blame] | 6411 | int rc; /* Return Code */ |
| 6412 | Pgno pgnoNew; /* Page number of pNew */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 6413 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6414 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6415 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
danielk1977 | e56b60e | 2009-06-10 09:11:06 +0000 | [diff] [blame] | 6416 | assert( pPage->nOverflow==1 ); |
| 6417 | |
drh | 5d433ce | 2010-08-14 16:02:52 +0000 | [diff] [blame] | 6418 | /* This error condition is now caught prior to reaching this function */ |
drh | 1fd2d7d | 2014-12-02 16:16:47 +0000 | [diff] [blame] | 6419 | if( NEVER(pPage->nCell==0) ) return SQLITE_CORRUPT_BKPT; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6420 | |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6421 | /* Allocate a new page. This page will become the right-sibling of |
| 6422 | ** pPage. Make the parent page writable, so that the new divider cell |
| 6423 | ** may be inserted. If both these operations are successful, proceed. |
| 6424 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 6425 | rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6426 | |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 6427 | if( rc==SQLITE_OK ){ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6428 | |
| 6429 | u8 *pOut = &pSpace[4]; |
drh | 2cbd78b | 2012-02-02 19:37:18 +0000 | [diff] [blame] | 6430 | u8 *pCell = pPage->apOvfl[0]; |
danielk1977 | 6f235cc | 2009-06-04 14:46:08 +0000 | [diff] [blame] | 6431 | u16 szCell = cellSizePtr(pPage, pCell); |
| 6432 | u8 *pStop; |
| 6433 | |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 6434 | assert( sqlite3PagerIswriteable(pNew->pDbPage) ); |
danielk1977 | e56b60e | 2009-06-10 09:11:06 +0000 | [diff] [blame] | 6435 | assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) ); |
| 6436 | zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF); |
dan | 8e9ba0c | 2014-10-14 17:27:04 +0000 | [diff] [blame] | 6437 | rebuildPage(pNew, 1, &pCell, &szCell); |
| 6438 | pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6439 | |
| 6440 | /* If this is an auto-vacuum database, update the pointer map |
| 6441 | ** with entries for the new page, and any pointer from the |
| 6442 | ** cell on the page to an overflow page. If either of these |
| 6443 | ** operations fails, the return code is set, but the contents |
| 6444 | ** of the parent page are still manipulated by thh code below. |
| 6445 | ** That is Ok, at this point the parent page is guaranteed to |
| 6446 | ** be marked as dirty. Returning an error code will cause a |
| 6447 | ** rollback, undoing any changes made to the parent page. |
| 6448 | */ |
| 6449 | if( ISAUTOVACUUM ){ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6450 | ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc); |
| 6451 | if( szCell>pNew->minLocal ){ |
| 6452 | ptrmapPutOvflPtr(pNew, pCell, &rc); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6453 | } |
| 6454 | } |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 6455 | |
danielk1977 | 6f235cc | 2009-06-04 14:46:08 +0000 | [diff] [blame] | 6456 | /* Create a divider cell to insert into pParent. The divider cell |
| 6457 | ** consists of a 4-byte page number (the page number of pPage) and |
| 6458 | ** a variable length key value (which must be the same value as the |
| 6459 | ** largest key on pPage). |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 6460 | ** |
danielk1977 | 6f235cc | 2009-06-04 14:46:08 +0000 | [diff] [blame] | 6461 | ** To find the largest key value on pPage, first find the right-most |
| 6462 | ** cell on pPage. The first two fields of this cell are the |
| 6463 | ** record-length (a variable length integer at most 32-bits in size) |
| 6464 | ** and the key value (a variable length integer, may have any value). |
| 6465 | ** The first of the while(...) loops below skips over the record-length |
| 6466 | ** field. The second while(...) loop copies the key value from the |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6467 | ** cell on pPage into the pSpace buffer. |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 6468 | */ |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 6469 | pCell = findCell(pPage, pPage->nCell-1); |
danielk1977 | 6f235cc | 2009-06-04 14:46:08 +0000 | [diff] [blame] | 6470 | pStop = &pCell[9]; |
| 6471 | while( (*(pCell++)&0x80) && pCell<pStop ); |
| 6472 | pStop = &pCell[9]; |
| 6473 | while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop ); |
| 6474 | |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6475 | /* Insert the new divider cell into pParent. */ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6476 | insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace), |
| 6477 | 0, pPage->pgno, &rc); |
danielk1977 | 6f235cc | 2009-06-04 14:46:08 +0000 | [diff] [blame] | 6478 | |
| 6479 | /* Set the right-child pointer of pParent to point to the new page. */ |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 6480 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew); |
| 6481 | |
danielk1977 | e08a3c4 | 2008-09-18 18:17:03 +0000 | [diff] [blame] | 6482 | /* Release the reference to the new page. */ |
| 6483 | releasePage(pNew); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 6484 | } |
| 6485 | |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 6486 | return rc; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 6487 | } |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 6488 | #endif /* SQLITE_OMIT_QUICKBALANCE */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6489 | |
dan | e6593d8 | 2014-10-24 16:40:49 +0000 | [diff] [blame] | 6490 | #if 0 |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 6491 | /* |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6492 | ** This function does not contribute anything to the operation of SQLite. |
| 6493 | ** it is sometimes activated temporarily while debugging code responsible |
| 6494 | ** for setting pointer-map entries. |
| 6495 | */ |
| 6496 | static int ptrmapCheckPages(MemPage **apPage, int nPage){ |
| 6497 | int i, j; |
| 6498 | for(i=0; i<nPage; i++){ |
| 6499 | Pgno n; |
| 6500 | u8 e; |
| 6501 | MemPage *pPage = apPage[i]; |
| 6502 | BtShared *pBt = pPage->pBt; |
| 6503 | assert( pPage->isInit ); |
| 6504 | |
| 6505 | for(j=0; j<pPage->nCell; j++){ |
| 6506 | CellInfo info; |
| 6507 | u8 *z; |
| 6508 | |
| 6509 | z = findCell(pPage, j); |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 6510 | btreeParseCellPtr(pPage, z, &info); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6511 | if( info.iOverflow ){ |
| 6512 | Pgno ovfl = get4byte(&z[info.iOverflow]); |
| 6513 | ptrmapGet(pBt, ovfl, &e, &n); |
| 6514 | assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 ); |
| 6515 | } |
| 6516 | if( !pPage->leaf ){ |
| 6517 | Pgno child = get4byte(z); |
| 6518 | ptrmapGet(pBt, child, &e, &n); |
| 6519 | assert( n==pPage->pgno && e==PTRMAP_BTREE ); |
| 6520 | } |
| 6521 | } |
| 6522 | if( !pPage->leaf ){ |
| 6523 | Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 6524 | ptrmapGet(pBt, child, &e, &n); |
| 6525 | assert( n==pPage->pgno && e==PTRMAP_BTREE ); |
| 6526 | } |
| 6527 | } |
| 6528 | return 1; |
| 6529 | } |
| 6530 | #endif |
| 6531 | |
danielk1977 | cd581a7 | 2009-06-23 15:43:39 +0000 | [diff] [blame] | 6532 | /* |
| 6533 | ** This function is used to copy the contents of the b-tree node stored |
| 6534 | ** on page pFrom to page pTo. If page pFrom was not a leaf page, then |
| 6535 | ** the pointer-map entries for each child page are updated so that the |
| 6536 | ** parent page stored in the pointer map is page pTo. If pFrom contained |
| 6537 | ** any cells with overflow page pointers, then the corresponding pointer |
| 6538 | ** map entries are also updated so that the parent page is page pTo. |
| 6539 | ** |
| 6540 | ** If pFrom is currently carrying any overflow cells (entries in the |
drh | 2cbd78b | 2012-02-02 19:37:18 +0000 | [diff] [blame] | 6541 | ** MemPage.apOvfl[] array), they are not copied to pTo. |
danielk1977 | cd581a7 | 2009-06-23 15:43:39 +0000 | [diff] [blame] | 6542 | ** |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 6543 | ** Before returning, page pTo is reinitialized using btreeInitPage(). |
danielk1977 | cd581a7 | 2009-06-23 15:43:39 +0000 | [diff] [blame] | 6544 | ** |
| 6545 | ** The performance of this function is not critical. It is only used by |
| 6546 | ** the balance_shallower() and balance_deeper() procedures, neither of |
| 6547 | ** which are called often under normal circumstances. |
| 6548 | */ |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 6549 | static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){ |
| 6550 | if( (*pRC)==SQLITE_OK ){ |
| 6551 | BtShared * const pBt = pFrom->pBt; |
| 6552 | u8 * const aFrom = pFrom->aData; |
| 6553 | u8 * const aTo = pTo->aData; |
| 6554 | int const iFromHdr = pFrom->hdrOffset; |
| 6555 | int const iToHdr = ((pTo->pgno==1) ? 100 : 0); |
drh | dc9b5f8 | 2009-12-05 18:34:08 +0000 | [diff] [blame] | 6556 | int rc; |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 6557 | int iData; |
| 6558 | |
| 6559 | |
| 6560 | assert( pFrom->isInit ); |
| 6561 | assert( pFrom->nFree>=iToHdr ); |
drh | fcd71b6 | 2011-04-05 22:08:24 +0000 | [diff] [blame] | 6562 | assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize ); |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 6563 | |
| 6564 | /* Copy the b-tree node content from page pFrom to page pTo. */ |
| 6565 | iData = get2byte(&aFrom[iFromHdr+5]); |
| 6566 | memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData); |
| 6567 | memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell); |
| 6568 | |
| 6569 | /* Reinitialize page pTo so that the contents of the MemPage structure |
dan | 89e060e | 2009-12-05 18:03:50 +0000 | [diff] [blame] | 6570 | ** match the new data. The initialization of pTo can actually fail under |
| 6571 | ** fairly obscure circumstances, even though it is a copy of initialized |
| 6572 | ** page pFrom. |
| 6573 | */ |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 6574 | pTo->isInit = 0; |
dan | 89e060e | 2009-12-05 18:03:50 +0000 | [diff] [blame] | 6575 | rc = btreeInitPage(pTo); |
| 6576 | if( rc!=SQLITE_OK ){ |
| 6577 | *pRC = rc; |
| 6578 | return; |
| 6579 | } |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 6580 | |
| 6581 | /* If this is an auto-vacuum database, update the pointer-map entries |
| 6582 | ** for any b-tree or overflow pages that pTo now contains the pointers to. |
| 6583 | */ |
| 6584 | if( ISAUTOVACUUM ){ |
| 6585 | *pRC = setChildPtrmaps(pTo); |
| 6586 | } |
danielk1977 | cd581a7 | 2009-06-23 15:43:39 +0000 | [diff] [blame] | 6587 | } |
danielk1977 | cd581a7 | 2009-06-23 15:43:39 +0000 | [diff] [blame] | 6588 | } |
| 6589 | |
| 6590 | /* |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6591 | ** This routine redistributes cells on the iParentIdx'th child of pParent |
| 6592 | ** (hereafter "the page") and up to 2 siblings so that all pages have about the |
| 6593 | ** same amount of free space. Usually a single sibling on either side of the |
| 6594 | ** page are used in the balancing, though both siblings might come from one |
| 6595 | ** side if the page is the first or last child of its parent. If the page |
| 6596 | ** has fewer than 2 siblings (something which can only happen if the page |
| 6597 | ** is a root page or a child of a root page) then all available siblings |
| 6598 | ** participate in the balancing. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6599 | ** |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6600 | ** The number of siblings of the page might be increased or decreased by |
| 6601 | ** 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] | 6602 | ** |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6603 | ** Note that when this routine is called, some of the cells on the page |
| 6604 | ** might not actually be stored in MemPage.aData[]. This can happen |
| 6605 | ** if the page is overfull. This routine ensures that all cells allocated |
| 6606 | ** to the page and its siblings fit into MemPage.aData[] before returning. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6607 | ** |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6608 | ** In the course of balancing the page and its siblings, cells may be |
| 6609 | ** inserted into or removed from the parent page (pParent). Doing so |
| 6610 | ** may cause the parent page to become overfull or underfull. If this |
| 6611 | ** happens, it is the responsibility of the caller to invoke the correct |
| 6612 | ** balancing routine to fix this problem (see the balance() routine). |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 6613 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6614 | ** If this routine fails for any reason, it might leave the database |
danielk1977 | 6067a9b | 2009-06-09 09:41:00 +0000 | [diff] [blame] | 6615 | ** in a corrupted state. So if this routine fails, the database should |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6616 | ** be rolled back. |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6617 | ** |
| 6618 | ** The third argument to this function, aOvflSpace, is a pointer to a |
drh | cd09c53 | 2009-07-20 19:30:00 +0000 | [diff] [blame] | 6619 | ** buffer big enough to hold one page. If while inserting cells into the parent |
| 6620 | ** page (pParent) the parent page becomes overfull, this buffer is |
| 6621 | ** used to store the parent's overflow cells. Because this function inserts |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6622 | ** a maximum of four divider cells into the parent page, and the maximum |
| 6623 | ** size of a cell stored within an internal node is always less than 1/4 |
| 6624 | ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large |
| 6625 | ** enough for all overflow cells. |
| 6626 | ** |
| 6627 | ** If aOvflSpace is set to a null pointer, this function returns |
| 6628 | ** SQLITE_NOMEM. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6629 | */ |
mistachkin | e7c5416 | 2012-10-02 22:54:27 +0000 | [diff] [blame] | 6630 | #if defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_M_ARM) |
| 6631 | #pragma optimize("", off) |
| 6632 | #endif |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6633 | static int balance_nonroot( |
| 6634 | MemPage *pParent, /* Parent page of siblings being balanced */ |
| 6635 | int iParentIdx, /* Index of "the page" in pParent */ |
danielk1977 | cd581a7 | 2009-06-23 15:43:39 +0000 | [diff] [blame] | 6636 | u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */ |
dan | 428c218 | 2012-08-06 18:50:11 +0000 | [diff] [blame] | 6637 | int isRoot, /* True if pParent is a root-page */ |
| 6638 | int bBulk /* True if this call is part of a bulk load */ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6639 | ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6640 | BtShared *pBt; /* The whole database */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 6641 | int nCell = 0; /* Number of cells in apCell[] */ |
| 6642 | int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */ |
danielk1977 | a4124bd | 2008-12-23 10:37:47 +0000 | [diff] [blame] | 6643 | int nNew = 0; /* Number of pages in apNew[] */ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6644 | int nOld; /* Number of pages in apOld[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6645 | int i, j, k; /* Loop counters */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6646 | int nxDiv; /* Next divider slot in pParent->aCell[] */ |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 6647 | int rc = SQLITE_OK; /* The return code */ |
shane | 36840fd | 2009-06-26 16:32:13 +0000 | [diff] [blame] | 6648 | u16 leafCorrection; /* 4 if pPage is a leaf. 0 if not */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 6649 | int leafData; /* True if pPage is a leaf of a LEAFDATA tree */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 6650 | int usableSpace; /* Bytes in pPage beyond the header */ |
| 6651 | int pageFlags; /* Value of pPage->aData[0] */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6652 | int subtotal; /* Subtotal of bytes in cells on one page */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 6653 | int iSpace1 = 0; /* First unused byte of aSpace1[] */ |
danielk1977 | 6067a9b | 2009-06-09 09:41:00 +0000 | [diff] [blame] | 6654 | int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 6655 | int szScratch; /* Size of scratch memory requested */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 6656 | MemPage *apOld[NB]; /* pPage and up to two siblings */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 6657 | MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6658 | u8 *pRight; /* Location in parent of right-sibling pointer */ |
| 6659 | u8 *apDiv[NB-1]; /* Divider cells in pParent */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 6660 | int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */ |
dan | 09c6840 | 2014-10-11 20:00:24 +0000 | [diff] [blame] | 6661 | int cntOld[NB+2]; /* Old index in aCell[] after i-th page */ |
drh | 2a0df92 | 2014-10-30 23:14:56 +0000 | [diff] [blame] | 6662 | int szNew[NB+2]; /* Combined size of cells placed on i-th page */ |
danielk1977 | 50f059b | 2005-03-29 02:54:03 +0000 | [diff] [blame] | 6663 | u8 **apCell = 0; /* All cells begin balanced */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 6664 | u16 *szCell; /* Local size of all cells in apCell[] */ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6665 | u8 *aSpace1; /* Space for copies of dividers cells */ |
| 6666 | Pgno pgno; /* Temp var to store a page number in */ |
dan | e6593d8 | 2014-10-24 16:40:49 +0000 | [diff] [blame] | 6667 | u8 abDone[NB+2]; /* True after i'th new page is populated */ |
| 6668 | Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */ |
drh | 00fe08a | 2014-10-31 00:05:23 +0000 | [diff] [blame] | 6669 | Pgno aPgOrder[NB+2]; /* Copy of aPgno[] used for sorting pages */ |
dan | e6593d8 | 2014-10-24 16:40:49 +0000 | [diff] [blame] | 6670 | u16 aPgFlags[NB+2]; /* flags field of new pages before shuffling */ |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 6671 | |
| 6672 | memset(abDone, 0, sizeof(abDone)); |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6673 | pBt = pParent->pBt; |
| 6674 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 6675 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
danielk1977 | 474b7cc | 2008-07-09 11:49:46 +0000 | [diff] [blame] | 6676 | |
danielk1977 | e576521 | 2009-06-17 11:13:28 +0000 | [diff] [blame] | 6677 | #if 0 |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6678 | TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno)); |
danielk1977 | e576521 | 2009-06-17 11:13:28 +0000 | [diff] [blame] | 6679 | #endif |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6680 | |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6681 | /* At this point pParent may have at most one overflow cell. And if |
| 6682 | ** this overflow cell is present, it must be the cell with |
| 6683 | ** index iParentIdx. This scenario comes about when this function |
drh | cd09c53 | 2009-07-20 19:30:00 +0000 | [diff] [blame] | 6684 | ** is called (indirectly) from sqlite3BtreeDelete(). |
| 6685 | */ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6686 | assert( pParent->nOverflow==0 || pParent->nOverflow==1 ); |
drh | 2cbd78b | 2012-02-02 19:37:18 +0000 | [diff] [blame] | 6687 | assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx ); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6688 | |
danielk1977 | 11a8a86 | 2009-06-17 11:49:52 +0000 | [diff] [blame] | 6689 | if( !aOvflSpace ){ |
| 6690 | return SQLITE_NOMEM; |
| 6691 | } |
| 6692 | |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6693 | /* Find the sibling pages to balance. Also locate the cells in pParent |
| 6694 | ** that divide the siblings. An attempt is made to find NN siblings on |
| 6695 | ** either side of pPage. More siblings are taken from one side, however, |
| 6696 | ** if there are fewer than NN siblings on the other side. If pParent |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6697 | ** has NB or fewer children then all children of pParent are taken. |
| 6698 | ** |
| 6699 | ** This loop also drops the divider cells from the parent page. This |
| 6700 | ** way, the remainder of the function does not have to deal with any |
drh | cd09c53 | 2009-07-20 19:30:00 +0000 | [diff] [blame] | 6701 | ** overflow cells in the parent page, since if any existed they will |
| 6702 | ** have already been removed. |
| 6703 | */ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6704 | i = pParent->nOverflow + pParent->nCell; |
| 6705 | if( i<2 ){ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 6706 | nxDiv = 0; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6707 | }else{ |
dan | 7d6885a | 2012-08-08 14:04:56 +0000 | [diff] [blame] | 6708 | assert( bBulk==0 || bBulk==1 ); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6709 | if( iParentIdx==0 ){ |
| 6710 | nxDiv = 0; |
| 6711 | }else if( iParentIdx==i ){ |
dan | 7d6885a | 2012-08-08 14:04:56 +0000 | [diff] [blame] | 6712 | nxDiv = i-2+bBulk; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6713 | }else{ |
dan | 7d6885a | 2012-08-08 14:04:56 +0000 | [diff] [blame] | 6714 | assert( bBulk==0 ); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6715 | nxDiv = iParentIdx-1; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6716 | } |
dan | 7d6885a | 2012-08-08 14:04:56 +0000 | [diff] [blame] | 6717 | i = 2-bBulk; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6718 | } |
dan | 7d6885a | 2012-08-08 14:04:56 +0000 | [diff] [blame] | 6719 | nOld = i+1; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6720 | if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){ |
| 6721 | pRight = &pParent->aData[pParent->hdrOffset+8]; |
| 6722 | }else{ |
| 6723 | pRight = findCell(pParent, i+nxDiv-pParent->nOverflow); |
| 6724 | } |
| 6725 | pgno = get4byte(pRight); |
| 6726 | while( 1 ){ |
dan | 11dcd11 | 2013-03-15 18:29:18 +0000 | [diff] [blame] | 6727 | rc = getAndInitPage(pBt, pgno, &apOld[i], 0); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6728 | if( rc ){ |
danielk1977 | 89bc4bc | 2009-07-21 19:25:24 +0000 | [diff] [blame] | 6729 | memset(apOld, 0, (i+1)*sizeof(MemPage*)); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6730 | goto balance_cleanup; |
| 6731 | } |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 6732 | nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6733 | if( (i--)==0 ) break; |
| 6734 | |
drh | 2cbd78b | 2012-02-02 19:37:18 +0000 | [diff] [blame] | 6735 | if( i+nxDiv==pParent->aiOvfl[0] && pParent->nOverflow ){ |
| 6736 | apDiv[i] = pParent->apOvfl[0]; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6737 | pgno = get4byte(apDiv[i]); |
| 6738 | szNew[i] = cellSizePtr(pParent, apDiv[i]); |
| 6739 | pParent->nOverflow = 0; |
| 6740 | }else{ |
| 6741 | apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow); |
| 6742 | pgno = get4byte(apDiv[i]); |
| 6743 | szNew[i] = cellSizePtr(pParent, apDiv[i]); |
| 6744 | |
| 6745 | /* Drop the cell from the parent page. apDiv[i] still points to |
| 6746 | ** the cell within the parent, even though it has been dropped. |
| 6747 | ** This is safe because dropping a cell only overwrites the first |
| 6748 | ** four bytes of it, and this function does not need the first |
| 6749 | ** four bytes of the divider cell. So the pointer is safe to use |
danielk1977 | 11a8a86 | 2009-06-17 11:49:52 +0000 | [diff] [blame] | 6750 | ** later on. |
| 6751 | ** |
drh | 8a575d9 | 2011-10-12 17:00:28 +0000 | [diff] [blame] | 6752 | ** But not if we are in secure-delete mode. In secure-delete mode, |
danielk1977 | 11a8a86 | 2009-06-17 11:49:52 +0000 | [diff] [blame] | 6753 | ** the dropCell() routine will overwrite the entire cell with zeroes. |
| 6754 | ** In this case, temporarily copy the cell into the aOvflSpace[] |
| 6755 | ** buffer. It will be copied out again as soon as the aSpace[] buffer |
| 6756 | ** is allocated. */ |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 6757 | if( pBt->btsFlags & BTS_SECURE_DELETE ){ |
drh | 8a575d9 | 2011-10-12 17:00:28 +0000 | [diff] [blame] | 6758 | int iOff; |
| 6759 | |
| 6760 | iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData); |
drh | 43b18e1 | 2010-08-17 19:40:08 +0000 | [diff] [blame] | 6761 | if( (iOff+szNew[i])>(int)pBt->usableSize ){ |
dan | 2ed11e7 | 2010-02-26 15:09:19 +0000 | [diff] [blame] | 6762 | rc = SQLITE_CORRUPT_BKPT; |
| 6763 | memset(apOld, 0, (i+1)*sizeof(MemPage*)); |
| 6764 | goto balance_cleanup; |
| 6765 | }else{ |
| 6766 | memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]); |
| 6767 | apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData]; |
| 6768 | } |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 6769 | } |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6770 | dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6771 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6772 | } |
| 6773 | |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 6774 | /* Make nMaxCells a multiple of 4 in order to preserve 8-byte |
drh | 8d97f1f | 2005-05-05 18:14:13 +0000 | [diff] [blame] | 6775 | ** alignment */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 6776 | nMaxCells = (nMaxCells + 3)&~3; |
drh | 8d97f1f | 2005-05-05 18:14:13 +0000 | [diff] [blame] | 6777 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6778 | /* |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 6779 | ** Allocate space for memory structures |
| 6780 | */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 6781 | szScratch = |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 6782 | nMaxCells*sizeof(u8*) /* apCell */ |
| 6783 | + nMaxCells*sizeof(u16) /* szCell */ |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 6784 | + pBt->pageSize; /* aSpace1 */ |
drh | 5279d34 | 2014-11-04 13:41:32 +0000 | [diff] [blame] | 6785 | |
drh | cbd55b0 | 2014-11-04 14:22:27 +0000 | [diff] [blame] | 6786 | /* EVIDENCE-OF: R-28375-38319 SQLite will never request a scratch buffer |
| 6787 | ** that is more than 6 times the database page size. */ |
drh | 5279d34 | 2014-11-04 13:41:32 +0000 | [diff] [blame] | 6788 | assert( szScratch<=6*pBt->pageSize ); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 6789 | apCell = sqlite3ScratchMalloc( szScratch ); |
danielk1977 | 11a8a86 | 2009-06-17 11:49:52 +0000 | [diff] [blame] | 6790 | if( apCell==0 ){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 6791 | rc = SQLITE_NOMEM; |
| 6792 | goto balance_cleanup; |
| 6793 | } |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 6794 | szCell = (u16*)&apCell[nMaxCells]; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6795 | aSpace1 = (u8*)&szCell[nMaxCells]; |
drh | ea598cb | 2009-04-05 12:22:08 +0000 | [diff] [blame] | 6796 | assert( EIGHT_BYTE_ALIGNMENT(aSpace1) ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6797 | |
| 6798 | /* |
| 6799 | ** Load pointers to all cells on sibling pages and the divider cells |
| 6800 | ** into the local apCell[] array. Make copies of the divider cells |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 6801 | ** into space obtained from aSpace1[]. The divider cells have already |
| 6802 | ** been removed from pParent. |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6803 | ** |
| 6804 | ** If the siblings are on leaf pages, then the child pointers of the |
| 6805 | ** divider cells are stripped from the cells before they are copied |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 6806 | ** into aSpace1[]. In this way, all cells in apCell[] are without |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6807 | ** child pointers. If siblings are not leaves, then all cell in |
| 6808 | ** apCell[] include child pointers. Either way, all cells in apCell[] |
| 6809 | ** are alike. |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 6810 | ** |
| 6811 | ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf. |
| 6812 | ** leafData: 1 if pPage holds key+data and pParent holds only keys. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6813 | */ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6814 | leafCorrection = apOld[0]->leaf*4; |
drh | 3e28ff5 | 2014-09-24 00:59:08 +0000 | [diff] [blame] | 6815 | leafData = apOld[0]->intKeyLeaf; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6816 | for(i=0; i<nOld; i++){ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6817 | int limit; |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 6818 | MemPage *pOld = apOld[i]; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6819 | |
| 6820 | limit = pOld->nCell+pOld->nOverflow; |
drh | 68f2a57 | 2011-06-03 17:50:49 +0000 | [diff] [blame] | 6821 | if( pOld->nOverflow>0 ){ |
| 6822 | for(j=0; j<limit; j++){ |
| 6823 | assert( nCell<nMaxCells ); |
| 6824 | apCell[nCell] = findOverflowCell(pOld, j); |
| 6825 | szCell[nCell] = cellSizePtr(pOld, apCell[nCell]); |
| 6826 | nCell++; |
| 6827 | } |
| 6828 | }else{ |
| 6829 | u8 *aData = pOld->aData; |
| 6830 | u16 maskPage = pOld->maskPage; |
| 6831 | u16 cellOffset = pOld->cellOffset; |
| 6832 | for(j=0; j<limit; j++){ |
| 6833 | assert( nCell<nMaxCells ); |
| 6834 | apCell[nCell] = findCellv2(aData, maskPage, cellOffset, j); |
| 6835 | szCell[nCell] = cellSizePtr(pOld, apCell[nCell]); |
| 6836 | nCell++; |
| 6837 | } |
| 6838 | } |
dan | 09c6840 | 2014-10-11 20:00:24 +0000 | [diff] [blame] | 6839 | cntOld[i] = nCell; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6840 | if( i<nOld-1 && !leafData){ |
shane | 36840fd | 2009-06-26 16:32:13 +0000 | [diff] [blame] | 6841 | u16 sz = (u16)szNew[i]; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6842 | u8 *pTemp; |
| 6843 | assert( nCell<nMaxCells ); |
| 6844 | szCell[nCell] = sz; |
| 6845 | pTemp = &aSpace1[iSpace1]; |
| 6846 | iSpace1 += sz; |
drh | e22e03e | 2010-08-18 21:19:03 +0000 | [diff] [blame] | 6847 | assert( sz<=pBt->maxLocal+23 ); |
drh | fcd71b6 | 2011-04-05 22:08:24 +0000 | [diff] [blame] | 6848 | assert( iSpace1 <= (int)pBt->pageSize ); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6849 | memcpy(pTemp, apDiv[i], sz); |
| 6850 | apCell[nCell] = pTemp+leafCorrection; |
| 6851 | assert( leafCorrection==0 || leafCorrection==4 ); |
shane | 36840fd | 2009-06-26 16:32:13 +0000 | [diff] [blame] | 6852 | szCell[nCell] = szCell[nCell] - leafCorrection; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6853 | if( !pOld->leaf ){ |
| 6854 | assert( leafCorrection==0 ); |
| 6855 | assert( pOld->hdrOffset==0 ); |
| 6856 | /* The right pointer of the child page pOld becomes the left |
| 6857 | ** pointer of the divider cell */ |
| 6858 | memcpy(apCell[nCell], &pOld->aData[8], 4); |
| 6859 | }else{ |
| 6860 | assert( leafCorrection==4 ); |
| 6861 | if( szCell[nCell]<4 ){ |
dan | 8f1eb8a | 2014-12-06 14:56:49 +0000 | [diff] [blame^] | 6862 | /* Do not allow any cells smaller than 4 bytes. If a smaller cell |
| 6863 | ** does exist, pad it with 0x00 bytes. */ |
| 6864 | assert( szCell[nCell]==3 ); |
| 6865 | assert( apCell[nCell]==&pTemp[iSpace1-3] ); |
| 6866 | pTemp[iSpace1++] = 0x00; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6867 | szCell[nCell] = 4; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 6868 | } |
| 6869 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6870 | nCell++; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6871 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6872 | } |
| 6873 | |
| 6874 | /* |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6875 | ** Figure out the number of pages needed to hold all nCell cells. |
| 6876 | ** Store this number in "k". Also compute szNew[] which is the total |
| 6877 | ** 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] | 6878 | ** in apCell[] of the cell that divides page i from page i+1. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6879 | ** cntNew[k] should equal nCell. |
| 6880 | ** |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 6881 | ** Values computed by this block: |
| 6882 | ** |
| 6883 | ** k: The total number of sibling pages |
| 6884 | ** szNew[i]: Spaced used on the i-th sibling page. |
| 6885 | ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to |
| 6886 | ** the right of the i-th sibling page. |
| 6887 | ** usableSpace: Number of bytes of space available on each sibling. |
| 6888 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6889 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6890 | usableSpace = pBt->usableSize - 12 + leafCorrection; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6891 | for(subtotal=k=i=0; i<nCell; i++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 6892 | assert( i<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6893 | subtotal += szCell[i] + 2; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6894 | if( subtotal > usableSpace ){ |
dan | d7b545b | 2014-10-13 18:03:27 +0000 | [diff] [blame] | 6895 | szNew[k] = subtotal - szCell[i] - 2; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6896 | cntNew[k] = i; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 6897 | if( leafData ){ i--; } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6898 | subtotal = 0; |
| 6899 | k++; |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 6900 | if( k>NB+1 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6901 | } |
| 6902 | } |
| 6903 | szNew[k] = subtotal; |
| 6904 | cntNew[k] = nCell; |
| 6905 | k++; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 6906 | |
| 6907 | /* |
| 6908 | ** The packing computed by the previous block is biased toward the siblings |
drh | 2a0df92 | 2014-10-30 23:14:56 +0000 | [diff] [blame] | 6909 | ** on the left side (siblings with smaller keys). The left siblings are |
| 6910 | ** always nearly full, while the right-most sibling might be nearly empty. |
| 6911 | ** The next block of code attempts to adjust the packing of siblings to |
| 6912 | ** get a better balance. |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 6913 | ** |
| 6914 | ** This adjustment is more than an optimization. The packing above might |
| 6915 | ** be so out of balance as to be illegal. For example, the right-most |
| 6916 | ** sibling might be completely empty. This adjustment is not optional. |
| 6917 | */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6918 | for(i=k-1; i>0; i--){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 6919 | int szRight = szNew[i]; /* Size of sibling on the right */ |
| 6920 | int szLeft = szNew[i-1]; /* Size of sibling on the left */ |
| 6921 | int r; /* Index of right-most cell in left sibling */ |
| 6922 | int d; /* Index of first cell to the left of right sibling */ |
| 6923 | |
| 6924 | r = cntNew[i-1] - 1; |
| 6925 | d = r + 1 - leafData; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 6926 | assert( d<nMaxCells ); |
| 6927 | assert( r<nMaxCells ); |
dan | f64cc49 | 2012-08-08 11:55:15 +0000 | [diff] [blame] | 6928 | while( szRight==0 |
| 6929 | || (!bBulk && szRight+szCell[d]+2<=szLeft-(szCell[r]+2)) |
| 6930 | ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6931 | szRight += szCell[d] + 2; |
| 6932 | szLeft -= szCell[r] + 2; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6933 | cntNew[i-1]--; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 6934 | r = cntNew[i-1] - 1; |
| 6935 | d = r + 1 - leafData; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6936 | } |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 6937 | szNew[i] = szRight; |
| 6938 | szNew[i-1] = szLeft; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6939 | } |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 6940 | |
drh | 2a0df92 | 2014-10-30 23:14:56 +0000 | [diff] [blame] | 6941 | /* Sanity check: For a non-corrupt database file one of the follwing |
| 6942 | ** must be true: |
| 6943 | ** (1) We found one or more cells (cntNew[0])>0), or |
| 6944 | ** (2) pPage is a virtual root page. A virtual root page is when |
| 6945 | ** the real root page is page 1 and we are the only child of |
| 6946 | ** that page. |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 6947 | */ |
drh | 2a0df92 | 2014-10-30 23:14:56 +0000 | [diff] [blame] | 6948 | assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB); |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 6949 | TRACE(("BALANCE: old: %d(nc=%d) %d(nc=%d) %d(nc=%d)\n", |
| 6950 | apOld[0]->pgno, apOld[0]->nCell, |
| 6951 | nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0, |
| 6952 | nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0 |
danielk1977 | e576521 | 2009-06-17 11:13:28 +0000 | [diff] [blame] | 6953 | )); |
| 6954 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6955 | /* |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 6956 | ** Allocate k new pages. Reuse old pages where possible. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6957 | */ |
drh | eac7442 | 2009-06-14 12:47:11 +0000 | [diff] [blame] | 6958 | if( apOld[0]->pgno<=1 ){ |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 6959 | rc = SQLITE_CORRUPT_BKPT; |
drh | eac7442 | 2009-06-14 12:47:11 +0000 | [diff] [blame] | 6960 | goto balance_cleanup; |
| 6961 | } |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6962 | pageFlags = apOld[0]->aData[0]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6963 | for(i=0; i<k; i++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6964 | MemPage *pNew; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 6965 | if( i<nOld ){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6966 | pNew = apNew[i] = apOld[i]; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 6967 | apOld[i] = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6968 | rc = sqlite3PagerWrite(pNew->pDbPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 6969 | nNew++; |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 6970 | if( rc ) goto balance_cleanup; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 6971 | }else{ |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 6972 | assert( i>0 ); |
dan | 428c218 | 2012-08-06 18:50:11 +0000 | [diff] [blame] | 6973 | rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 6974 | if( rc ) goto balance_cleanup; |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 6975 | zeroPage(pNew, pageFlags); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6976 | apNew[i] = pNew; |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 6977 | nNew++; |
dan | 09c6840 | 2014-10-11 20:00:24 +0000 | [diff] [blame] | 6978 | cntOld[i] = nCell; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6979 | |
| 6980 | /* Set the pointer-map entry for the new sibling page. */ |
| 6981 | if( ISAUTOVACUUM ){ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6982 | ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6983 | if( rc!=SQLITE_OK ){ |
| 6984 | goto balance_cleanup; |
| 6985 | } |
| 6986 | } |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 6987 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6988 | } |
| 6989 | |
| 6990 | /* |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 6991 | ** Reassign page numbers so that the new pages are in ascending order. |
| 6992 | ** This helps to keep entries in the disk file in order so that a scan |
| 6993 | ** of the table is closer to a linear scan through the file. That in turn |
| 6994 | ** helps the operating system to deliver pages from the disk more rapidly. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 6995 | ** |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 6996 | ** An O(n^2) insertion sort algorithm is used, but since n is never more |
| 6997 | ** than (NB+2) (a small constant), that should not be a problem. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 6998 | ** |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 6999 | ** When NB==3, this one optimization makes the database about 25% faster |
| 7000 | ** for large insertions and deletions. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 7001 | */ |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 7002 | for(i=0; i<nNew; i++){ |
drh | 00fe08a | 2014-10-31 00:05:23 +0000 | [diff] [blame] | 7003 | aPgOrder[i] = aPgno[i] = apNew[i]->pgno; |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 7004 | aPgFlags[i] = apNew[i]->pDbPage->flags; |
dan | 89ca0b3 | 2014-10-25 20:36:28 +0000 | [diff] [blame] | 7005 | for(j=0; j<i; j++){ |
| 7006 | if( aPgno[j]==aPgno[i] ){ |
| 7007 | /* This branch is taken if the set of sibling pages somehow contains |
| 7008 | ** duplicate entries. This can happen if the database is corrupt. |
| 7009 | ** It would be simpler to detect this as part of the loop below, but |
drh | ba0f999 | 2014-10-30 20:48:44 +0000 | [diff] [blame] | 7010 | ** we do the detection here in order to avoid populating the pager |
| 7011 | ** cache with two separate objects associated with the same |
| 7012 | ** page number. */ |
dan | 89ca0b3 | 2014-10-25 20:36:28 +0000 | [diff] [blame] | 7013 | assert( CORRUPT_DB ); |
| 7014 | rc = SQLITE_CORRUPT_BKPT; |
| 7015 | goto balance_cleanup; |
| 7016 | } |
| 7017 | } |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 7018 | } |
| 7019 | for(i=0; i<nNew; i++){ |
dan | 31f4e99 | 2014-10-24 20:57:03 +0000 | [diff] [blame] | 7020 | int iBest = 0; /* aPgno[] index of page number to use */ |
dan | 31f4e99 | 2014-10-24 20:57:03 +0000 | [diff] [blame] | 7021 | for(j=1; j<nNew; j++){ |
drh | 00fe08a | 2014-10-31 00:05:23 +0000 | [diff] [blame] | 7022 | if( aPgOrder[j]<aPgOrder[iBest] ) iBest = j; |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 7023 | } |
drh | 00fe08a | 2014-10-31 00:05:23 +0000 | [diff] [blame] | 7024 | pgno = aPgOrder[iBest]; |
| 7025 | aPgOrder[iBest] = 0xffffffff; |
dan | 31f4e99 | 2014-10-24 20:57:03 +0000 | [diff] [blame] | 7026 | if( iBest!=i ){ |
| 7027 | if( iBest>i ){ |
| 7028 | sqlite3PagerRekey(apNew[iBest]->pDbPage, pBt->nPage+iBest+1, 0); |
| 7029 | } |
| 7030 | sqlite3PagerRekey(apNew[i]->pDbPage, pgno, aPgFlags[iBest]); |
| 7031 | apNew[i]->pgno = pgno; |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 7032 | } |
| 7033 | } |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 7034 | |
| 7035 | TRACE(("BALANCE: new: %d(%d nc=%d) %d(%d nc=%d) %d(%d nc=%d) " |
| 7036 | "%d(%d nc=%d) %d(%d nc=%d)\n", |
| 7037 | apNew[0]->pgno, szNew[0], cntNew[0], |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 7038 | nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0, |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 7039 | nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0, |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 7040 | nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0, |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 7041 | nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0, |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 7042 | nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0, |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 7043 | nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0, |
| 7044 | nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0, |
| 7045 | nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0 |
| 7046 | )); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 7047 | |
| 7048 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
| 7049 | put4byte(pRight, apNew[nNew-1]->pgno); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 7050 | |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 7051 | /* If the sibling pages are not leaves, ensure that the right-child pointer |
| 7052 | ** of the right-most new sibling page is set to the value that was |
| 7053 | ** originally in the same field of the right-most old sibling page. */ |
| 7054 | if( (pageFlags & PTF_LEAF)==0 && nOld!=nNew ){ |
| 7055 | MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1]; |
| 7056 | memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4); |
| 7057 | } |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 7058 | |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 7059 | /* Make any required updates to pointer map entries associated with |
| 7060 | ** cells stored on sibling pages following the balance operation. Pointer |
| 7061 | ** map entries associated with divider cells are set by the insertCell() |
| 7062 | ** routine. The associated pointer map entries are: |
| 7063 | ** |
| 7064 | ** a) if the cell contains a reference to an overflow chain, the |
| 7065 | ** entry associated with the first page in the overflow chain, and |
| 7066 | ** |
| 7067 | ** b) if the sibling pages are not leaves, the child page associated |
| 7068 | ** with the cell. |
| 7069 | ** |
| 7070 | ** If the sibling pages are not leaves, then the pointer map entry |
| 7071 | ** associated with the right-child of each sibling may also need to be |
| 7072 | ** updated. This happens below, after the sibling pages have been |
| 7073 | ** populated, not here. |
| 7074 | */ |
| 7075 | if( ISAUTOVACUUM ){ |
| 7076 | MemPage *pNew = apNew[0]; |
| 7077 | u8 *aOld = pNew->aData; |
| 7078 | int cntOldNext = pNew->nCell + pNew->nOverflow; |
| 7079 | int usableSize = pBt->usableSize; |
| 7080 | int iNew = 0; |
| 7081 | int iOld = 0; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 7082 | |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 7083 | for(i=0; i<nCell; i++){ |
| 7084 | u8 *pCell = apCell[i]; |
| 7085 | if( i==cntOldNext ){ |
| 7086 | MemPage *pOld = (++iOld)<nNew ? apNew[iOld] : apOld[iOld]; |
| 7087 | cntOldNext += pOld->nCell + pOld->nOverflow + !leafData; |
| 7088 | aOld = pOld->aData; |
| 7089 | } |
| 7090 | if( i==cntNew[iNew] ){ |
| 7091 | pNew = apNew[++iNew]; |
| 7092 | if( !leafData ) continue; |
| 7093 | } |
| 7094 | |
| 7095 | /* Cell pCell is destined for new sibling page pNew. Originally, it |
drh | ba0f999 | 2014-10-30 20:48:44 +0000 | [diff] [blame] | 7096 | ** was either part of sibling page iOld (possibly an overflow cell), |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 7097 | ** or else the divider cell to the left of sibling page iOld. So, |
| 7098 | ** if sibling page iOld had the same page number as pNew, and if |
| 7099 | ** pCell really was a part of sibling page iOld (not a divider or |
| 7100 | ** overflow cell), we can skip updating the pointer map entries. */ |
drh | d52d52b | 2014-12-06 02:05:44 +0000 | [diff] [blame] | 7101 | if( iOld>=nNew |
| 7102 | || pNew->pgno!=aPgno[iOld] |
| 7103 | || pCell<aOld |
| 7104 | || pCell>=&aOld[usableSize] |
| 7105 | ){ |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 7106 | if( !leafCorrection ){ |
| 7107 | ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc); |
| 7108 | } |
| 7109 | if( szCell[i]>pNew->minLocal ){ |
| 7110 | ptrmapPutOvflPtr(pNew, pCell, &rc); |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 7111 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7112 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 7113 | } |
| 7114 | } |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 7115 | |
| 7116 | /* Insert new divider cells into pParent. */ |
| 7117 | for(i=0; i<nNew-1; i++){ |
| 7118 | u8 *pCell; |
| 7119 | u8 *pTemp; |
| 7120 | int sz; |
| 7121 | MemPage *pNew = apNew[i]; |
| 7122 | j = cntNew[i]; |
| 7123 | |
| 7124 | assert( j<nMaxCells ); |
| 7125 | pCell = apCell[j]; |
| 7126 | sz = szCell[j] + leafCorrection; |
| 7127 | pTemp = &aOvflSpace[iOvflSpace]; |
| 7128 | if( !pNew->leaf ){ |
| 7129 | memcpy(&pNew->aData[8], pCell, 4); |
| 7130 | }else if( leafData ){ |
| 7131 | /* If the tree is a leaf-data tree, and the siblings are leaves, |
| 7132 | ** then there is no divider cell in apCell[]. Instead, the divider |
| 7133 | ** cell consists of the integer key for the right-most cell of |
| 7134 | ** the sibling-page assembled above only. |
| 7135 | */ |
| 7136 | CellInfo info; |
| 7137 | j--; |
| 7138 | btreeParseCellPtr(pNew, apCell[j], &info); |
| 7139 | pCell = pTemp; |
| 7140 | sz = 4 + putVarint(&pCell[4], info.nKey); |
| 7141 | pTemp = 0; |
| 7142 | }else{ |
| 7143 | pCell -= 4; |
| 7144 | /* Obscure case for non-leaf-data trees: If the cell at pCell was |
| 7145 | ** previously stored on a leaf node, and its reported size was 4 |
| 7146 | ** bytes, then it may actually be smaller than this |
| 7147 | ** (see btreeParseCellPtr(), 4 bytes is the minimum size of |
| 7148 | ** any cell). But it is important to pass the correct size to |
| 7149 | ** insertCell(), so reparse the cell now. |
| 7150 | ** |
| 7151 | ** Note that this can never happen in an SQLite data file, as all |
| 7152 | ** cells are at least 4 bytes. It only happens in b-trees used |
| 7153 | ** to evaluate "IN (SELECT ...)" and similar clauses. |
| 7154 | */ |
| 7155 | if( szCell[j]==4 ){ |
| 7156 | assert(leafCorrection==4); |
| 7157 | sz = cellSizePtr(pParent, pCell); |
| 7158 | } |
| 7159 | } |
| 7160 | iOvflSpace += sz; |
| 7161 | assert( sz<=pBt->maxLocal+23 ); |
| 7162 | assert( iOvflSpace <= (int)pBt->pageSize ); |
| 7163 | insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc); |
| 7164 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
| 7165 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
| 7166 | } |
| 7167 | |
| 7168 | /* Now update the actual sibling pages. The order in which they are updated |
| 7169 | ** is important, as this code needs to avoid disrupting any page from which |
| 7170 | ** cells may still to be read. In practice, this means: |
| 7171 | ** |
drh | d836d42 | 2014-10-31 14:26:36 +0000 | [diff] [blame] | 7172 | ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1]) |
| 7173 | ** then it is not safe to update page apNew[iPg] until after |
| 7174 | ** the left-hand sibling apNew[iPg-1] has been updated. |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 7175 | ** |
drh | d836d42 | 2014-10-31 14:26:36 +0000 | [diff] [blame] | 7176 | ** (2) If cells are moving right (from apNew[iPg] to apNew[iPg+1]) |
| 7177 | ** then it is not safe to update page apNew[iPg] until after |
| 7178 | ** the right-hand sibling apNew[iPg+1] has been updated. |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 7179 | ** |
| 7180 | ** If neither of the above apply, the page is safe to update. |
drh | d836d42 | 2014-10-31 14:26:36 +0000 | [diff] [blame] | 7181 | ** |
| 7182 | ** The iPg value in the following loop starts at nNew-1 goes down |
| 7183 | ** to 0, then back up to nNew-1 again, thus making two passes over |
| 7184 | ** the pages. On the initial downward pass, only condition (1) above |
| 7185 | ** needs to be tested because (2) will always be true from the previous |
| 7186 | ** step. On the upward pass, both conditions are always true, so the |
| 7187 | ** upwards pass simply processes pages that were missed on the downward |
| 7188 | ** pass. |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 7189 | */ |
drh | bec021b | 2014-10-31 12:22:00 +0000 | [diff] [blame] | 7190 | for(i=1-nNew; i<nNew; i++){ |
| 7191 | int iPg = i<0 ? -i : i; |
drh | bec021b | 2014-10-31 12:22:00 +0000 | [diff] [blame] | 7192 | assert( iPg>=0 && iPg<nNew ); |
drh | d836d42 | 2014-10-31 14:26:36 +0000 | [diff] [blame] | 7193 | if( abDone[iPg] ) continue; /* Skip pages already processed */ |
| 7194 | if( i>=0 /* On the upwards pass, or... */ |
| 7195 | || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */ |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 7196 | ){ |
dan | 09c6840 | 2014-10-11 20:00:24 +0000 | [diff] [blame] | 7197 | int iNew; |
| 7198 | int iOld; |
| 7199 | int nNewCell; |
| 7200 | |
drh | d836d42 | 2014-10-31 14:26:36 +0000 | [diff] [blame] | 7201 | /* Verify condition (1): If cells are moving left, update iPg |
| 7202 | ** only after iPg-1 has already been updated. */ |
| 7203 | assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] ); |
| 7204 | |
| 7205 | /* Verify condition (2): If cells are moving right, update iPg |
| 7206 | ** only after iPg+1 has already been updated. */ |
| 7207 | assert( cntNew[iPg]>=cntOld[iPg] || abDone[iPg+1] ); |
| 7208 | |
dan | 09c6840 | 2014-10-11 20:00:24 +0000 | [diff] [blame] | 7209 | if( iPg==0 ){ |
| 7210 | iNew = iOld = 0; |
| 7211 | nNewCell = cntNew[0]; |
| 7212 | }else{ |
| 7213 | iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : nCell; |
| 7214 | iNew = cntNew[iPg-1] + !leafData; |
| 7215 | nNewCell = cntNew[iPg] - iNew; |
| 7216 | } |
| 7217 | |
| 7218 | editPage(apNew[iPg], iOld, iNew, nNewCell, apCell, szCell); |
drh | d836d42 | 2014-10-31 14:26:36 +0000 | [diff] [blame] | 7219 | abDone[iPg]++; |
dan | d7b545b | 2014-10-13 18:03:27 +0000 | [diff] [blame] | 7220 | apNew[iPg]->nFree = usableSpace-szNew[iPg]; |
dan | 09c6840 | 2014-10-11 20:00:24 +0000 | [diff] [blame] | 7221 | assert( apNew[iPg]->nOverflow==0 ); |
| 7222 | assert( apNew[iPg]->nCell==nNewCell ); |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 7223 | } |
| 7224 | } |
drh | d836d42 | 2014-10-31 14:26:36 +0000 | [diff] [blame] | 7225 | |
| 7226 | /* All pages have been processed exactly once */ |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 7227 | assert( memcmp(abDone, "\01\01\01\01\01", nNew)==0 ); |
| 7228 | |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 7229 | assert( nOld>0 ); |
| 7230 | assert( nNew>0 ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 7231 | |
danielk1977 | 13bd99f | 2009-06-24 05:40:34 +0000 | [diff] [blame] | 7232 | if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){ |
| 7233 | /* The root page of the b-tree now contains no cells. The only sibling |
| 7234 | ** page is the right-child of the parent. Copy the contents of the |
| 7235 | ** child page into the parent, decreasing the overall height of the |
| 7236 | ** b-tree structure by one. This is described as the "balance-shallower" |
| 7237 | ** sub-algorithm in some documentation. |
| 7238 | ** |
| 7239 | ** If this is an auto-vacuum database, the call to copyNodeContent() |
| 7240 | ** sets all pointer-map entries corresponding to database image pages |
| 7241 | ** for which the pointer is stored within the content being copied. |
| 7242 | ** |
drh | 768f290 | 2014-10-31 02:51:41 +0000 | [diff] [blame] | 7243 | ** It is critical that the child page be defragmented before being |
| 7244 | ** copied into the parent, because if the parent is page 1 then it will |
| 7245 | ** by smaller than the child due to the database header, and so all the |
| 7246 | ** free space needs to be up front. |
| 7247 | */ |
danielk1977 | 13bd99f | 2009-06-24 05:40:34 +0000 | [diff] [blame] | 7248 | assert( nNew==1 ); |
dan | 89ca0b3 | 2014-10-25 20:36:28 +0000 | [diff] [blame] | 7249 | rc = defragmentPage(apNew[0]); |
drh | 768f290 | 2014-10-31 02:51:41 +0000 | [diff] [blame] | 7250 | testcase( rc!=SQLITE_OK ); |
| 7251 | assert( apNew[0]->nFree == |
| 7252 | (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2) |
| 7253 | || rc!=SQLITE_OK |
| 7254 | ); |
| 7255 | copyNodeContent(apNew[0], pParent, &rc); |
| 7256 | freePage(apNew[0], &rc); |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 7257 | }else if( ISAUTOVACUUM && !leafCorrection ){ |
| 7258 | /* Fix the pointer map entries associated with the right-child of each |
| 7259 | ** sibling page. All other pointer map entries have already been taken |
| 7260 | ** care of. */ |
| 7261 | for(i=0; i<nNew; i++){ |
| 7262 | u32 key = get4byte(&apNew[i]->aData[8]); |
| 7263 | ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 7264 | } |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 7265 | } |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 7266 | |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 7267 | assert( pParent->isInit ); |
| 7268 | TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n", |
| 7269 | nOld, nNew, nCell)); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 7270 | |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 7271 | /* Free any old pages that were not reused as new pages. |
| 7272 | */ |
| 7273 | for(i=nNew; i<nOld; i++){ |
| 7274 | freePage(apOld[i], &rc); |
| 7275 | } |
| 7276 | |
dan | e6593d8 | 2014-10-24 16:40:49 +0000 | [diff] [blame] | 7277 | #if 0 |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 7278 | if( ISAUTOVACUUM && rc==SQLITE_OK && apNew[0]->isInit ){ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 7279 | /* The ptrmapCheckPages() contains assert() statements that verify that |
| 7280 | ** all pointer map pages are set correctly. This is helpful while |
| 7281 | ** debugging. This is usually disabled because a corrupt database may |
| 7282 | ** cause an assert() statement to fail. */ |
| 7283 | ptrmapCheckPages(apNew, nNew); |
| 7284 | ptrmapCheckPages(&pParent, 1); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 7285 | } |
dan | 33ea486 | 2014-10-09 19:35:37 +0000 | [diff] [blame] | 7286 | #endif |
danielk1977 | cd581a7 | 2009-06-23 15:43:39 +0000 | [diff] [blame] | 7287 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7288 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 7289 | ** Cleanup before returning. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7290 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 7291 | balance_cleanup: |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 7292 | sqlite3ScratchFree(apCell); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7293 | for(i=0; i<nOld; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 7294 | releasePage(apOld[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7295 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 7296 | for(i=0; i<nNew; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 7297 | releasePage(apNew[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7298 | } |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 7299 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7300 | return rc; |
| 7301 | } |
mistachkin | e7c5416 | 2012-10-02 22:54:27 +0000 | [diff] [blame] | 7302 | #if defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_M_ARM) |
| 7303 | #pragma optimize("", on) |
| 7304 | #endif |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7305 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 7306 | |
| 7307 | /* |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7308 | ** This function is called when the root page of a b-tree structure is |
| 7309 | ** overfull (has one or more overflow pages). |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 7310 | ** |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7311 | ** A new child page is allocated and the contents of the current root |
| 7312 | ** page, including overflow cells, are copied into the child. The root |
| 7313 | ** page is then overwritten to make it an empty page with the right-child |
| 7314 | ** pointer pointing to the new page. |
| 7315 | ** |
| 7316 | ** Before returning, all pointer-map entries corresponding to pages |
| 7317 | ** that the new child-page now contains pointers to are updated. The |
| 7318 | ** entry corresponding to the new right-child pointer of the root |
| 7319 | ** page is also updated. |
| 7320 | ** |
| 7321 | ** If successful, *ppChild is set to contain a reference to the child |
| 7322 | ** page and SQLITE_OK is returned. In this case the caller is required |
| 7323 | ** to call releasePage() on *ppChild exactly once. If an error occurs, |
| 7324 | ** an error code is returned and *ppChild is set to 0. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 7325 | */ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7326 | static int balance_deeper(MemPage *pRoot, MemPage **ppChild){ |
| 7327 | int rc; /* Return value from subprocedures */ |
| 7328 | MemPage *pChild = 0; /* Pointer to a new child page */ |
shane | 5eff7cf | 2009-08-10 03:57:58 +0000 | [diff] [blame] | 7329 | Pgno pgnoChild = 0; /* Page number of the new child page */ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7330 | BtShared *pBt = pRoot->pBt; /* The BTree */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 7331 | |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7332 | assert( pRoot->nOverflow>0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7333 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 7334 | |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7335 | /* Make pRoot, the root page of the b-tree, writable. Allocate a new |
| 7336 | ** page that will become the new right-child of pPage. Copy the contents |
| 7337 | ** of the node stored on pRoot into the new child page. |
| 7338 | */ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 7339 | rc = sqlite3PagerWrite(pRoot->pDbPage); |
| 7340 | if( rc==SQLITE_OK ){ |
| 7341 | rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0); |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 7342 | copyNodeContent(pRoot, pChild, &rc); |
| 7343 | if( ISAUTOVACUUM ){ |
| 7344 | ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc); |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 7345 | } |
| 7346 | } |
| 7347 | if( rc ){ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7348 | *ppChild = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 7349 | releasePage(pChild); |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7350 | return rc; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 7351 | } |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7352 | assert( sqlite3PagerIswriteable(pChild->pDbPage) ); |
| 7353 | assert( sqlite3PagerIswriteable(pRoot->pDbPage) ); |
| 7354 | assert( pChild->nCell==pRoot->nCell ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 7355 | |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7356 | TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno)); |
| 7357 | |
| 7358 | /* Copy the overflow cells from pRoot to pChild */ |
drh | 2cbd78b | 2012-02-02 19:37:18 +0000 | [diff] [blame] | 7359 | memcpy(pChild->aiOvfl, pRoot->aiOvfl, |
| 7360 | pRoot->nOverflow*sizeof(pRoot->aiOvfl[0])); |
| 7361 | memcpy(pChild->apOvfl, pRoot->apOvfl, |
| 7362 | pRoot->nOverflow*sizeof(pRoot->apOvfl[0])); |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7363 | pChild->nOverflow = pRoot->nOverflow; |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7364 | |
| 7365 | /* Zero the contents of pRoot. Then install pChild as the right-child. */ |
| 7366 | zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF); |
| 7367 | put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild); |
| 7368 | |
| 7369 | *ppChild = pChild; |
| 7370 | return SQLITE_OK; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 7371 | } |
| 7372 | |
| 7373 | /* |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 7374 | ** The page that pCur currently points to has just been modified in |
| 7375 | ** some way. This function figures out if this modification means the |
| 7376 | ** tree needs to be balanced, and if so calls the appropriate balancing |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7377 | ** routine. Balancing routines are: |
| 7378 | ** |
| 7379 | ** balance_quick() |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7380 | ** balance_deeper() |
| 7381 | ** balance_nonroot() |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 7382 | */ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7383 | static int balance(BtCursor *pCur){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 7384 | int rc = SQLITE_OK; |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7385 | const int nMin = pCur->pBt->usableSize * 2 / 3; |
| 7386 | u8 aBalanceQuickSpace[13]; |
| 7387 | u8 *pFree = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 7388 | |
shane | 75ac1de | 2009-06-09 18:58:52 +0000 | [diff] [blame] | 7389 | TESTONLY( int balance_quick_called = 0 ); |
| 7390 | TESTONLY( int balance_deeper_called = 0 ); |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7391 | |
| 7392 | do { |
| 7393 | int iPage = pCur->iPage; |
| 7394 | MemPage *pPage = pCur->apPage[iPage]; |
| 7395 | |
| 7396 | if( iPage==0 ){ |
| 7397 | if( pPage->nOverflow ){ |
| 7398 | /* The root page of the b-tree is overfull. In this case call the |
| 7399 | ** balance_deeper() function to create a new child for the root-page |
| 7400 | ** and copy the current contents of the root-page to it. The |
| 7401 | ** next iteration of the do-loop will balance the child page. |
| 7402 | */ |
| 7403 | assert( (balance_deeper_called++)==0 ); |
| 7404 | rc = balance_deeper(pPage, &pCur->apPage[1]); |
| 7405 | if( rc==SQLITE_OK ){ |
| 7406 | pCur->iPage = 1; |
| 7407 | pCur->aiIdx[0] = 0; |
| 7408 | pCur->aiIdx[1] = 0; |
| 7409 | assert( pCur->apPage[1]->nOverflow ); |
| 7410 | } |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7411 | }else{ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7412 | break; |
| 7413 | } |
| 7414 | }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){ |
| 7415 | break; |
| 7416 | }else{ |
| 7417 | MemPage * const pParent = pCur->apPage[iPage-1]; |
| 7418 | int const iIdx = pCur->aiIdx[iPage-1]; |
| 7419 | |
| 7420 | rc = sqlite3PagerWrite(pParent->pDbPage); |
| 7421 | if( rc==SQLITE_OK ){ |
| 7422 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | 3e28ff5 | 2014-09-24 00:59:08 +0000 | [diff] [blame] | 7423 | if( pPage->intKeyLeaf |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7424 | && pPage->nOverflow==1 |
drh | 2cbd78b | 2012-02-02 19:37:18 +0000 | [diff] [blame] | 7425 | && pPage->aiOvfl[0]==pPage->nCell |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7426 | && pParent->pgno!=1 |
| 7427 | && pParent->nCell==iIdx |
| 7428 | ){ |
| 7429 | /* Call balance_quick() to create a new sibling of pPage on which |
| 7430 | ** to store the overflow cell. balance_quick() inserts a new cell |
| 7431 | ** into pParent, which may cause pParent overflow. If this |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 7432 | ** happens, the next iteration of the do-loop will balance pParent |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7433 | ** use either balance_nonroot() or balance_deeper(). Until this |
| 7434 | ** happens, the overflow cell is stored in the aBalanceQuickSpace[] |
| 7435 | ** buffer. |
| 7436 | ** |
| 7437 | ** The purpose of the following assert() is to check that only a |
| 7438 | ** single call to balance_quick() is made for each call to this |
| 7439 | ** function. If this were not verified, a subtle bug involving reuse |
| 7440 | ** of the aBalanceQuickSpace[] might sneak in. |
| 7441 | */ |
| 7442 | assert( (balance_quick_called++)==0 ); |
| 7443 | rc = balance_quick(pParent, pPage, aBalanceQuickSpace); |
| 7444 | }else |
| 7445 | #endif |
| 7446 | { |
| 7447 | /* In this case, call balance_nonroot() to redistribute cells |
| 7448 | ** between pPage and up to 2 of its sibling pages. This involves |
| 7449 | ** modifying the contents of pParent, which may cause pParent to |
| 7450 | ** become overfull or underfull. The next iteration of the do-loop |
| 7451 | ** will balance the parent page to correct this. |
| 7452 | ** |
| 7453 | ** If the parent page becomes overfull, the overflow cell or cells |
| 7454 | ** are stored in the pSpace buffer allocated immediately below. |
| 7455 | ** A subsequent iteration of the do-loop will deal with this by |
| 7456 | ** calling balance_nonroot() (balance_deeper() may be called first, |
| 7457 | ** but it doesn't deal with overflow cells - just moves them to a |
| 7458 | ** different page). Once this subsequent call to balance_nonroot() |
| 7459 | ** has completed, it is safe to release the pSpace buffer used by |
| 7460 | ** the previous call, as the overflow cell data will have been |
| 7461 | ** copied either into the body of a database page or into the new |
| 7462 | ** pSpace buffer passed to the latter call to balance_nonroot(). |
| 7463 | */ |
| 7464 | u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize); |
dan | 428c218 | 2012-08-06 18:50:11 +0000 | [diff] [blame] | 7465 | rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1, pCur->hints); |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7466 | if( pFree ){ |
| 7467 | /* If pFree is not NULL, it points to the pSpace buffer used |
| 7468 | ** by a previous call to balance_nonroot(). Its contents are |
| 7469 | ** now stored either on real database pages or within the |
| 7470 | ** new pSpace buffer, so it may be safely freed here. */ |
| 7471 | sqlite3PageFree(pFree); |
| 7472 | } |
| 7473 | |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 7474 | /* The pSpace buffer will be freed after the next call to |
| 7475 | ** balance_nonroot(), or just before this function returns, whichever |
| 7476 | ** comes first. */ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7477 | pFree = pSpace; |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7478 | } |
| 7479 | } |
| 7480 | |
| 7481 | pPage->nOverflow = 0; |
| 7482 | |
| 7483 | /* The next iteration of the do-loop balances the parent page. */ |
| 7484 | releasePage(pPage); |
| 7485 | pCur->iPage--; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 7486 | } |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7487 | }while( rc==SQLITE_OK ); |
| 7488 | |
| 7489 | if( pFree ){ |
| 7490 | sqlite3PageFree(pFree); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 7491 | } |
| 7492 | return rc; |
| 7493 | } |
| 7494 | |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 7495 | |
| 7496 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 7497 | ** Insert a new record into the BTree. The key is given by (pKey,nKey) |
| 7498 | ** 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] | 7499 | ** define what table the record should be inserted into. The cursor |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7500 | ** is left pointing at a random location. |
| 7501 | ** |
| 7502 | ** For an INTKEY table, only the nKey value of the key is used. pKey is |
| 7503 | ** ignored. For a ZERODATA table, the pData and nData are both ignored. |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 7504 | ** |
| 7505 | ** If the seekResult parameter is non-zero, then a successful call to |
danielk1977 | 3509a65 | 2009-07-06 18:56:13 +0000 | [diff] [blame] | 7506 | ** MovetoUnpacked() to seek cursor pCur to (pKey, nKey) has already |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 7507 | ** been performed. seekResult is the search result returned (a negative |
| 7508 | ** number if pCur points at an entry that is smaller than (pKey, nKey), or |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 7509 | ** a positive value if pCur points at an entry that is larger than |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 7510 | ** (pKey, nKey)). |
| 7511 | ** |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 7512 | ** If the seekResult parameter is non-zero, then the caller guarantees that |
| 7513 | ** cursor pCur is pointing at the existing copy of a row that is to be |
| 7514 | ** overwritten. If the seekResult parameter is 0, then cursor pCur may |
| 7515 | ** 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] | 7516 | ** the cursor before the new key can be inserted. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 7517 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 7518 | int sqlite3BtreeInsert( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 7519 | BtCursor *pCur, /* Insert data into the table of this cursor */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 7520 | const void *pKey, i64 nKey, /* The key of the new record */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 7521 | const void *pData, int nData, /* The data of the new record */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 7522 | int nZero, /* Number of extra 0 bytes to append to data */ |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 7523 | int appendBias, /* True if this is likely an append */ |
danielk1977 | 3509a65 | 2009-07-06 18:56:13 +0000 | [diff] [blame] | 7524 | int seekResult /* Result of prior MovetoUnpacked() call */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 7525 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 7526 | int rc; |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 7527 | int loc = seekResult; /* -1: before desired location +1: after */ |
drh | 1d452e1 | 2009-11-01 19:26:59 +0000 | [diff] [blame] | 7528 | int szNew = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 7529 | int idx; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 7530 | MemPage *pPage; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7531 | Btree *p = pCur->pBtree; |
| 7532 | BtShared *pBt = p->pBt; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 7533 | unsigned char *oldCell; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7534 | unsigned char *newCell = 0; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 7535 | |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 7536 | if( pCur->eState==CURSOR_FAULT ){ |
| 7537 | assert( pCur->skipNext!=SQLITE_OK ); |
| 7538 | return pCur->skipNext; |
| 7539 | } |
| 7540 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7541 | assert( cursorHoldsMutex(pCur) ); |
drh | 3f38740 | 2014-09-24 01:23:00 +0000 | [diff] [blame] | 7542 | assert( (pCur->curFlags & BTCF_WriteFlag)!=0 |
| 7543 | && pBt->inTransaction==TRANS_WRITE |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 7544 | && (pBt->btsFlags & BTS_READ_ONLY)==0 ); |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 7545 | assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) ); |
| 7546 | |
danielk1977 | 31d31b8 | 2009-07-13 13:18:07 +0000 | [diff] [blame] | 7547 | /* Assert that the caller has been consistent. If this cursor was opened |
| 7548 | ** expecting an index b-tree, then the caller should be inserting blob |
| 7549 | ** keys with no associated data. If the cursor was opened expecting an |
| 7550 | ** intkey table, the caller should be inserting integer keys with a |
| 7551 | ** blob of associated data. */ |
| 7552 | assert( (pKey==0)==(pCur->pKeyInfo==0) ); |
| 7553 | |
danielk1977 | 9c3acf3 | 2009-05-02 07:36:49 +0000 | [diff] [blame] | 7554 | /* Save the positions of any other cursors open on this table. |
| 7555 | ** |
danielk1977 | 3509a65 | 2009-07-06 18:56:13 +0000 | [diff] [blame] | 7556 | ** In some cases, the call to btreeMoveto() below is a no-op. For |
danielk1977 | 9c3acf3 | 2009-05-02 07:36:49 +0000 | [diff] [blame] | 7557 | ** example, when inserting data into a table with auto-generated integer |
| 7558 | ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the |
| 7559 | ** integer key to use. It then calls this function to actually insert the |
danielk1977 | 3509a65 | 2009-07-06 18:56:13 +0000 | [diff] [blame] | 7560 | ** data into the intkey B-Tree. In this case btreeMoveto() recognizes |
danielk1977 | 9c3acf3 | 2009-05-02 07:36:49 +0000 | [diff] [blame] | 7561 | ** that the cursor is already where it needs to be and returns without |
| 7562 | ** doing any work. To avoid thwarting these optimizations, it is important |
| 7563 | ** not to clear the cursor here. |
| 7564 | */ |
drh | 4c301aa | 2009-07-15 17:25:45 +0000 | [diff] [blame] | 7565 | rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur); |
| 7566 | if( rc ) return rc; |
drh | d60f4f4 | 2012-03-23 14:23:52 +0000 | [diff] [blame] | 7567 | |
drh | d60f4f4 | 2012-03-23 14:23:52 +0000 | [diff] [blame] | 7568 | if( pCur->pKeyInfo==0 ){ |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 7569 | /* If this is an insert into a table b-tree, invalidate any incrblob |
| 7570 | ** cursors open on the row being replaced */ |
drh | d60f4f4 | 2012-03-23 14:23:52 +0000 | [diff] [blame] | 7571 | invalidateIncrblobCursors(p, nKey, 0); |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 7572 | |
| 7573 | /* If the cursor is currently on the last row and we are appending a |
| 7574 | ** new row onto the end, set the "loc" to avoid an unnecessary btreeMoveto() |
| 7575 | ** call */ |
drh | 3f38740 | 2014-09-24 01:23:00 +0000 | [diff] [blame] | 7576 | if( (pCur->curFlags&BTCF_ValidNKey)!=0 && nKey>0 |
| 7577 | && pCur->info.nKey==nKey-1 ){ |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 7578 | loc = -1; |
| 7579 | } |
drh | d60f4f4 | 2012-03-23 14:23:52 +0000 | [diff] [blame] | 7580 | } |
| 7581 | |
drh | 4c301aa | 2009-07-15 17:25:45 +0000 | [diff] [blame] | 7582 | if( !loc ){ |
| 7583 | rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc); |
| 7584 | if( rc ) return rc; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7585 | } |
danielk1977 | b980d221 | 2009-06-22 18:03:51 +0000 | [diff] [blame] | 7586 | assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7587 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 7588 | pPage = pCur->apPage[pCur->iPage]; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 7589 | assert( pPage->intKey || nKey>=0 ); |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 7590 | assert( pPage->leaf || !pPage->intKey ); |
danielk1977 | 8f880a8 | 2009-07-13 09:41:45 +0000 | [diff] [blame] | 7591 | |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 7592 | TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n", |
| 7593 | pCur->pgnoRoot, nKey, nData, pPage->pgno, |
| 7594 | loc==0 ? "overwrite" : "new entry")); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 7595 | assert( pPage->isInit ); |
danielk1977 | 52ae724 | 2008-03-25 14:24:56 +0000 | [diff] [blame] | 7596 | newCell = pBt->pTmpSpace; |
drh | 3fbb022 | 2014-09-24 19:47:27 +0000 | [diff] [blame] | 7597 | assert( newCell!=0 ); |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 7598 | rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7599 | if( rc ) goto end_insert; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 7600 | assert( szNew==cellSizePtr(pPage, newCell) ); |
drh | fcd71b6 | 2011-04-05 22:08:24 +0000 | [diff] [blame] | 7601 | assert( szNew <= MX_CELL_SIZE(pBt) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 7602 | idx = pCur->aiIdx[pCur->iPage]; |
danielk1977 | b980d221 | 2009-06-22 18:03:51 +0000 | [diff] [blame] | 7603 | if( loc==0 ){ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 7604 | u16 szOld; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 7605 | assert( idx<pPage->nCell ); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 7606 | rc = sqlite3PagerWrite(pPage->pDbPage); |
| 7607 | if( rc ){ |
| 7608 | goto end_insert; |
| 7609 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 7610 | oldCell = findCell(pPage, idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7611 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 7612 | memcpy(newCell, oldCell, 4); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7613 | } |
drh | 9bfdc25 | 2014-09-24 02:05:41 +0000 | [diff] [blame] | 7614 | rc = clearCell(pPage, oldCell, &szOld); |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 7615 | dropCell(pPage, idx, szOld, &rc); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7616 | if( rc ) goto end_insert; |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 7617 | }else if( loc<0 && pPage->nCell>0 ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7618 | assert( pPage->leaf ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 7619 | idx = ++pCur->aiIdx[pCur->iPage]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 7620 | }else{ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7621 | assert( pPage->leaf ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 7622 | } |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 7623 | insertCell(pPage, idx, newCell, szNew, 0, 0, &rc); |
danielk1977 | 3f632d5 | 2009-05-02 10:03:09 +0000 | [diff] [blame] | 7624 | assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 ); |
drh | 9bf9e9c | 2008-12-05 20:01:43 +0000 | [diff] [blame] | 7625 | |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 7626 | /* If no error has occurred and pPage has an overflow cell, call balance() |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7627 | ** to redistribute the cells within the tree. Since balance() may move |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 7628 | ** the cursor, zero the BtCursor.info.nSize and BTCF_ValidNKey |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7629 | ** variables. |
danielk1977 | 3f632d5 | 2009-05-02 10:03:09 +0000 | [diff] [blame] | 7630 | ** |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7631 | ** Previous versions of SQLite called moveToRoot() to move the cursor |
| 7632 | ** back to the root page as balance() used to invalidate the contents |
danielk1977 | 54109bb | 2009-06-23 11:22:29 +0000 | [diff] [blame] | 7633 | ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that, |
| 7634 | ** set the cursor state to "invalid". This makes common insert operations |
| 7635 | ** slightly faster. |
danielk1977 | 3f632d5 | 2009-05-02 10:03:09 +0000 | [diff] [blame] | 7636 | ** |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7637 | ** There is a subtle but important optimization here too. When inserting |
| 7638 | ** multiple records into an intkey b-tree using a single cursor (as can |
| 7639 | ** happen while processing an "INSERT INTO ... SELECT" statement), it |
| 7640 | ** is advantageous to leave the cursor pointing to the last entry in |
| 7641 | ** the b-tree if possible. If the cursor is left pointing to the last |
| 7642 | ** entry in the table, and the next row inserted has an integer key |
| 7643 | ** larger than the largest existing key, it is possible to insert the |
| 7644 | ** row without seeking the cursor. This can be a big performance boost. |
danielk1977 | 3f632d5 | 2009-05-02 10:03:09 +0000 | [diff] [blame] | 7645 | */ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7646 | pCur->info.nSize = 0; |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7647 | if( rc==SQLITE_OK && pPage->nOverflow ){ |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 7648 | pCur->curFlags &= ~(BTCF_ValidNKey); |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7649 | rc = balance(pCur); |
| 7650 | |
| 7651 | /* Must make sure nOverflow is reset to zero even if the balance() |
danielk1977 | 54109bb | 2009-06-23 11:22:29 +0000 | [diff] [blame] | 7652 | ** fails. Internal data structure corruption will result otherwise. |
| 7653 | ** Also, set the cursor state to invalid. This stops saveCursorPosition() |
| 7654 | ** from trying to save the current position of the cursor. */ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7655 | pCur->apPage[pCur->iPage]->nOverflow = 0; |
danielk1977 | 54109bb | 2009-06-23 11:22:29 +0000 | [diff] [blame] | 7656 | pCur->eState = CURSOR_INVALID; |
danielk1977 | 3f632d5 | 2009-05-02 10:03:09 +0000 | [diff] [blame] | 7657 | } |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7658 | assert( pCur->apPage[pCur->iPage]->nOverflow==0 ); |
drh | 9bf9e9c | 2008-12-05 20:01:43 +0000 | [diff] [blame] | 7659 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7660 | end_insert: |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 7661 | return rc; |
| 7662 | } |
| 7663 | |
| 7664 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7665 | ** Delete the entry that the cursor is pointing to. The cursor |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 7666 | ** is left pointing at an arbitrary location. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 7667 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 7668 | int sqlite3BtreeDelete(BtCursor *pCur){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7669 | Btree *p = pCur->pBtree; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 7670 | BtShared *pBt = p->pBt; |
| 7671 | int rc; /* Return code */ |
| 7672 | MemPage *pPage; /* Page to delete cell from */ |
| 7673 | unsigned char *pCell; /* Pointer to cell to delete */ |
| 7674 | int iCellIdx; /* Index of cell to delete */ |
| 7675 | int iCellDepth; /* Depth of node containing pCell */ |
drh | 9bfdc25 | 2014-09-24 02:05:41 +0000 | [diff] [blame] | 7676 | u16 szCell; /* Size of the cell being deleted */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7677 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7678 | assert( cursorHoldsMutex(pCur) ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 7679 | assert( pBt->inTransaction==TRANS_WRITE ); |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 7680 | assert( (pBt->btsFlags & BTS_READ_ONLY)==0 ); |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 7681 | assert( pCur->curFlags & BTCF_WriteFlag ); |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 7682 | assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) ); |
| 7683 | assert( !hasReadConflicts(p, pCur->pgnoRoot) ); |
| 7684 | |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 7685 | if( NEVER(pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell) |
| 7686 | || NEVER(pCur->eState!=CURSOR_VALID) |
| 7687 | ){ |
| 7688 | return SQLITE_ERROR; /* Something has gone awry. */ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 7689 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7690 | |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 7691 | iCellDepth = pCur->iPage; |
| 7692 | iCellIdx = pCur->aiIdx[iCellDepth]; |
| 7693 | pPage = pCur->apPage[iCellDepth]; |
| 7694 | pCell = findCell(pPage, iCellIdx); |
| 7695 | |
| 7696 | /* If the page containing the entry to delete is not a leaf page, move |
| 7697 | ** the cursor to the largest entry in the tree that is smaller than |
| 7698 | ** the entry being deleted. This cell will replace the cell being deleted |
| 7699 | ** from the internal node. The 'previous' entry is used for this instead |
| 7700 | ** of the 'next' entry, as the previous entry is always a part of the |
| 7701 | ** sub-tree headed by the child page of the cell being deleted. This makes |
| 7702 | ** balancing the tree following the delete operation easier. */ |
| 7703 | if( !pPage->leaf ){ |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 7704 | int notUsed = 0; |
drh | 4c301aa | 2009-07-15 17:25:45 +0000 | [diff] [blame] | 7705 | rc = sqlite3BtreePrevious(pCur, ¬Used); |
| 7706 | if( rc ) return rc; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 7707 | } |
| 7708 | |
| 7709 | /* Save the positions of any other cursors open on this table before |
| 7710 | ** making any modifications. Make the page containing the entry to be |
| 7711 | ** deleted writable. Then free any overflow pages associated with the |
drh | a4ec1d4 | 2009-07-11 13:13:11 +0000 | [diff] [blame] | 7712 | ** entry and finally remove the cell itself from within the page. |
| 7713 | */ |
| 7714 | rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur); |
| 7715 | if( rc ) return rc; |
drh | d60f4f4 | 2012-03-23 14:23:52 +0000 | [diff] [blame] | 7716 | |
| 7717 | /* If this is a delete operation to remove a row from a table b-tree, |
| 7718 | ** invalidate any incrblob cursors open on the row being deleted. */ |
| 7719 | if( pCur->pKeyInfo==0 ){ |
| 7720 | invalidateIncrblobCursors(p, pCur->info.nKey, 0); |
| 7721 | } |
| 7722 | |
drh | a4ec1d4 | 2009-07-11 13:13:11 +0000 | [diff] [blame] | 7723 | rc = sqlite3PagerWrite(pPage->pDbPage); |
| 7724 | if( rc ) return rc; |
drh | 9bfdc25 | 2014-09-24 02:05:41 +0000 | [diff] [blame] | 7725 | rc = clearCell(pPage, pCell, &szCell); |
| 7726 | dropCell(pPage, iCellIdx, szCell, &rc); |
drh | a4ec1d4 | 2009-07-11 13:13:11 +0000 | [diff] [blame] | 7727 | if( rc ) return rc; |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 7728 | |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 7729 | /* If the cell deleted was not located on a leaf page, then the cursor |
| 7730 | ** is currently pointing to the largest entry in the sub-tree headed |
| 7731 | ** by the child-page of the cell that was just deleted from an internal |
| 7732 | ** node. The cell from the leaf node needs to be moved to the internal |
| 7733 | ** node to replace the deleted cell. */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7734 | if( !pPage->leaf ){ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 7735 | MemPage *pLeaf = pCur->apPage[pCur->iPage]; |
| 7736 | int nCell; |
| 7737 | Pgno n = pCur->apPage[iCellDepth+1]->pgno; |
| 7738 | unsigned char *pTmp; |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 7739 | |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 7740 | pCell = findCell(pLeaf, pLeaf->nCell-1); |
| 7741 | nCell = cellSizePtr(pLeaf, pCell); |
drh | fcd71b6 | 2011-04-05 22:08:24 +0000 | [diff] [blame] | 7742 | assert( MX_CELL_SIZE(pBt) >= nCell ); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 7743 | pTmp = pBt->pTmpSpace; |
drh | 3fbb022 | 2014-09-24 19:47:27 +0000 | [diff] [blame] | 7744 | assert( pTmp!=0 ); |
drh | a4ec1d4 | 2009-07-11 13:13:11 +0000 | [diff] [blame] | 7745 | rc = sqlite3PagerWrite(pLeaf->pDbPage); |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 7746 | insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc); |
| 7747 | dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc); |
drh | a4ec1d4 | 2009-07-11 13:13:11 +0000 | [diff] [blame] | 7748 | if( rc ) return rc; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 7749 | } |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 7750 | |
| 7751 | /* Balance the tree. If the entry deleted was located on a leaf page, |
| 7752 | ** then the cursor still points to that page. In this case the first |
| 7753 | ** call to balance() repairs the tree, and the if(...) condition is |
| 7754 | ** never true. |
| 7755 | ** |
| 7756 | ** Otherwise, if the entry deleted was on an internal node page, then |
| 7757 | ** pCur is pointing to the leaf page from which a cell was removed to |
| 7758 | ** replace the cell deleted from the internal node. This is slightly |
| 7759 | ** tricky as the leaf node may be underfull, and the internal node may |
| 7760 | ** be either under or overfull. In this case run the balancing algorithm |
| 7761 | ** on the leaf node first. If the balance proceeds far enough up the |
| 7762 | ** tree that we can be sure that any problem in the internal node has |
| 7763 | ** been corrected, so be it. Otherwise, after balancing the leaf node, |
| 7764 | ** walk the cursor up the tree to the internal node and balance it as |
| 7765 | ** well. */ |
| 7766 | rc = balance(pCur); |
| 7767 | if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){ |
| 7768 | while( pCur->iPage>iCellDepth ){ |
| 7769 | releasePage(pCur->apPage[pCur->iPage--]); |
| 7770 | } |
| 7771 | rc = balance(pCur); |
| 7772 | } |
| 7773 | |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 7774 | if( rc==SQLITE_OK ){ |
| 7775 | moveToRoot(pCur); |
| 7776 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 7777 | return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 7778 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7779 | |
| 7780 | /* |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 7781 | ** Create a new BTree table. Write into *piTable the page |
| 7782 | ** number for the root page of the new table. |
| 7783 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 7784 | ** The type of type is determined by the flags parameter. Only the |
| 7785 | ** following values of flags are currently in use. Other values for |
| 7786 | ** flags might not work: |
| 7787 | ** |
| 7788 | ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys |
| 7789 | ** BTREE_ZERODATA Used for SQL indices |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7790 | */ |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 7791 | static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7792 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7793 | MemPage *pRoot; |
| 7794 | Pgno pgnoRoot; |
| 7795 | int rc; |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 7796 | int ptfFlags; /* Page-type flage for the root page of new table */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7797 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7798 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 7799 | assert( pBt->inTransaction==TRANS_WRITE ); |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 7800 | assert( (pBt->btsFlags & BTS_READ_ONLY)==0 ); |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 7801 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7802 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 7803 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7804 | if( rc ){ |
| 7805 | return rc; |
| 7806 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7807 | #else |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 7808 | if( pBt->autoVacuum ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7809 | Pgno pgnoMove; /* Move a page here to make room for the root-page */ |
| 7810 | MemPage *pPageMove; /* The page to move to. */ |
| 7811 | |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 7812 | /* Creating a new table may probably require moving an existing database |
| 7813 | ** to make room for the new tables root page. In case this page turns |
| 7814 | ** out to be an overflow page, delete all overflow page-map caches |
| 7815 | ** held by open cursors. |
| 7816 | */ |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 7817 | invalidateAllOverflowCache(pBt); |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 7818 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7819 | /* Read the value of meta[3] from the database to determine where the |
| 7820 | ** root page of the new table should go. meta[3] is the largest root-page |
| 7821 | ** created so far, so the new root-page is (meta[3]+1). |
| 7822 | */ |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 7823 | sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &pgnoRoot); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7824 | pgnoRoot++; |
| 7825 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 7826 | /* The new root-page may not be allocated on a pointer-map page, or the |
| 7827 | ** PENDING_BYTE page. |
| 7828 | */ |
drh | 7219043 | 2008-01-31 14:54:43 +0000 | [diff] [blame] | 7829 | while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) || |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 7830 | pgnoRoot==PENDING_BYTE_PAGE(pBt) ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7831 | pgnoRoot++; |
| 7832 | } |
| 7833 | assert( pgnoRoot>=3 ); |
| 7834 | |
| 7835 | /* Allocate a page. The page that currently resides at pgnoRoot will |
| 7836 | ** be moved to the allocated page (unless the allocated page happens |
| 7837 | ** to reside at pgnoRoot). |
| 7838 | */ |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 7839 | rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, BTALLOC_EXACT); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7840 | if( rc!=SQLITE_OK ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 7841 | return rc; |
| 7842 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7843 | |
| 7844 | if( pgnoMove!=pgnoRoot ){ |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 7845 | /* pgnoRoot is the page that will be used for the root-page of |
| 7846 | ** the new table (assuming an error did not occur). But we were |
| 7847 | ** allocated pgnoMove. If required (i.e. if it was not allocated |
| 7848 | ** by extending the file), the current page at position pgnoMove |
| 7849 | ** is already journaled. |
| 7850 | */ |
drh | eeb844a | 2009-08-08 18:01:07 +0000 | [diff] [blame] | 7851 | u8 eType = 0; |
| 7852 | Pgno iPtrPage = 0; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7853 | |
dan | f7679ad | 2013-04-03 11:38:36 +0000 | [diff] [blame] | 7854 | /* Save the positions of any open cursors. This is required in |
| 7855 | ** case they are holding a reference to an xFetch reference |
| 7856 | ** corresponding to page pgnoRoot. */ |
| 7857 | rc = saveAllCursors(pBt, 0, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7858 | releasePage(pPageMove); |
dan | f7679ad | 2013-04-03 11:38:36 +0000 | [diff] [blame] | 7859 | if( rc!=SQLITE_OK ){ |
| 7860 | return rc; |
| 7861 | } |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 7862 | |
| 7863 | /* Move the page currently at pgnoRoot to pgnoMove. */ |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 7864 | rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7865 | if( rc!=SQLITE_OK ){ |
| 7866 | return rc; |
| 7867 | } |
| 7868 | rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage); |
drh | 27731d7 | 2009-06-22 12:05:10 +0000 | [diff] [blame] | 7869 | if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){ |
| 7870 | rc = SQLITE_CORRUPT_BKPT; |
| 7871 | } |
| 7872 | if( rc!=SQLITE_OK ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7873 | releasePage(pRoot); |
| 7874 | return rc; |
| 7875 | } |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 7876 | assert( eType!=PTRMAP_ROOTPAGE ); |
| 7877 | assert( eType!=PTRMAP_FREEPAGE ); |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 7878 | rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7879 | releasePage(pRoot); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 7880 | |
| 7881 | /* Obtain the page at pgnoRoot */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7882 | if( rc!=SQLITE_OK ){ |
| 7883 | return rc; |
| 7884 | } |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 7885 | rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7886 | if( rc!=SQLITE_OK ){ |
| 7887 | return rc; |
| 7888 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7889 | rc = sqlite3PagerWrite(pRoot->pDbPage); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7890 | if( rc!=SQLITE_OK ){ |
| 7891 | releasePage(pRoot); |
| 7892 | return rc; |
| 7893 | } |
| 7894 | }else{ |
| 7895 | pRoot = pPageMove; |
| 7896 | } |
| 7897 | |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 7898 | /* Update the pointer-map and meta-data with the new root-page number. */ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 7899 | ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0, &rc); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7900 | if( rc ){ |
| 7901 | releasePage(pRoot); |
| 7902 | return rc; |
| 7903 | } |
drh | bf59283 | 2010-03-30 15:51:12 +0000 | [diff] [blame] | 7904 | |
| 7905 | /* When the new root page was allocated, page 1 was made writable in |
| 7906 | ** order either to increase the database filesize, or to decrement the |
| 7907 | ** freelist count. Hence, the sqlite3BtreeUpdateMeta() call cannot fail. |
| 7908 | */ |
| 7909 | assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7910 | rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot); |
drh | bf59283 | 2010-03-30 15:51:12 +0000 | [diff] [blame] | 7911 | if( NEVER(rc) ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7912 | releasePage(pRoot); |
| 7913 | return rc; |
| 7914 | } |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 7915 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7916 | }else{ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 7917 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7918 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 7919 | } |
| 7920 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7921 | assert( sqlite3PagerIswriteable(pRoot->pDbPage) ); |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 7922 | if( createTabFlags & BTREE_INTKEY ){ |
| 7923 | ptfFlags = PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF; |
| 7924 | }else{ |
| 7925 | ptfFlags = PTF_ZERODATA | PTF_LEAF; |
| 7926 | } |
| 7927 | zeroPage(pRoot, ptfFlags); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7928 | sqlite3PagerUnref(pRoot->pDbPage); |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 7929 | assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7930 | *piTable = (int)pgnoRoot; |
| 7931 | return SQLITE_OK; |
| 7932 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7933 | int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){ |
| 7934 | int rc; |
| 7935 | sqlite3BtreeEnter(p); |
| 7936 | rc = btreeCreateTable(p, piTable, flags); |
| 7937 | sqlite3BtreeLeave(p); |
| 7938 | return rc; |
| 7939 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7940 | |
| 7941 | /* |
| 7942 | ** Erase the given database page and all its children. Return |
| 7943 | ** the page to the freelist. |
| 7944 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7945 | static int clearDatabasePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7946 | BtShared *pBt, /* The BTree that contains the table */ |
drh | 7ab641f | 2009-11-24 02:37:02 +0000 | [diff] [blame] | 7947 | Pgno pgno, /* Page number to clear */ |
| 7948 | int freePageFlag, /* Deallocate page if true */ |
| 7949 | int *pnChange /* Add number of Cells freed to this counter */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7950 | ){ |
danielk1977 | 146ba99 | 2009-07-22 14:08:13 +0000 | [diff] [blame] | 7951 | MemPage *pPage; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7952 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7953 | unsigned char *pCell; |
| 7954 | int i; |
dan | 8ce7184 | 2014-01-14 20:14:09 +0000 | [diff] [blame] | 7955 | int hdr; |
drh | 9bfdc25 | 2014-09-24 02:05:41 +0000 | [diff] [blame] | 7956 | u16 szCell; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7957 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7958 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | b129915 | 2010-03-30 22:58:33 +0000 | [diff] [blame] | 7959 | if( pgno>btreePagecount(pBt) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 7960 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 7961 | } |
| 7962 | |
dan | 11dcd11 | 2013-03-15 18:29:18 +0000 | [diff] [blame] | 7963 | rc = getAndInitPage(pBt, pgno, &pPage, 0); |
danielk1977 | 146ba99 | 2009-07-22 14:08:13 +0000 | [diff] [blame] | 7964 | if( rc ) return rc; |
dan | 8ce7184 | 2014-01-14 20:14:09 +0000 | [diff] [blame] | 7965 | hdr = pPage->hdrOffset; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7966 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 7967 | pCell = findCell(pPage, i); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7968 | if( !pPage->leaf ){ |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 7969 | rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 7970 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7971 | } |
drh | 9bfdc25 | 2014-09-24 02:05:41 +0000 | [diff] [blame] | 7972 | rc = clearCell(pPage, pCell, &szCell); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 7973 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7974 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 7975 | if( !pPage->leaf ){ |
dan | 8ce7184 | 2014-01-14 20:14:09 +0000 | [diff] [blame] | 7976 | rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 7977 | if( rc ) goto cleardatabasepage_out; |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 7978 | }else if( pnChange ){ |
| 7979 | assert( pPage->intKey ); |
| 7980 | *pnChange += pPage->nCell; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 7981 | } |
| 7982 | if( freePageFlag ){ |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 7983 | freePage(pPage, &rc); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7984 | }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){ |
dan | 8ce7184 | 2014-01-14 20:14:09 +0000 | [diff] [blame] | 7985 | zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 7986 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 7987 | |
| 7988 | cleardatabasepage_out: |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7989 | releasePage(pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 7990 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7991 | } |
| 7992 | |
| 7993 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 7994 | ** Delete all information from a single table in the database. iTable is |
| 7995 | ** the page number of the root of the table. After this routine returns, |
| 7996 | ** the root page is empty, but still exists. |
| 7997 | ** |
| 7998 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 7999 | ** read cursors on the table. Open write cursors are moved to the |
| 8000 | ** root of the table. |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 8001 | ** |
| 8002 | ** If pnChange is not NULL, then table iTable must be an intkey table. The |
| 8003 | ** integer value pointed to by pnChange is incremented by the number of |
| 8004 | ** entries in the table. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 8005 | */ |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 8006 | int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 8007 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 8008 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 8009 | sqlite3BtreeEnter(p); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 8010 | assert( p->inTrans==TRANS_WRITE ); |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 8011 | |
drh | c046e3e | 2009-07-15 11:26:44 +0000 | [diff] [blame] | 8012 | rc = saveAllCursors(pBt, (Pgno)iTable, 0); |
drh | d60f4f4 | 2012-03-23 14:23:52 +0000 | [diff] [blame] | 8013 | |
drh | c046e3e | 2009-07-15 11:26:44 +0000 | [diff] [blame] | 8014 | if( SQLITE_OK==rc ){ |
drh | d60f4f4 | 2012-03-23 14:23:52 +0000 | [diff] [blame] | 8015 | /* Invalidate all incrblob cursors open on table iTable (assuming iTable |
| 8016 | ** is the root of a table b-tree - if it is not, the following call is |
| 8017 | ** a no-op). */ |
| 8018 | invalidateIncrblobCursors(p, 0, 1); |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 8019 | rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 8020 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 8021 | sqlite3BtreeLeave(p); |
| 8022 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 8023 | } |
| 8024 | |
| 8025 | /* |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 8026 | ** Delete all information from the single table that pCur is open on. |
| 8027 | ** |
| 8028 | ** This routine only work for pCur on an ephemeral table. |
| 8029 | */ |
| 8030 | int sqlite3BtreeClearTableOfCursor(BtCursor *pCur){ |
| 8031 | return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0); |
| 8032 | } |
| 8033 | |
| 8034 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 8035 | ** Erase all information in a table and add the root of the table to |
| 8036 | ** the freelist. Except, the root of the principle table (the one on |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 8037 | ** page 1) is never added to the freelist. |
| 8038 | ** |
| 8039 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 8040 | ** cursors on the table. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 8041 | ** |
| 8042 | ** If AUTOVACUUM is enabled and the page at iTable is not the last |
| 8043 | ** root page in the database file, then the last root page |
| 8044 | ** in the database file is moved into the slot formerly occupied by |
| 8045 | ** iTable and that last slot formerly occupied by the last root page |
| 8046 | ** is added to the freelist instead of iTable. In this say, all |
| 8047 | ** root pages are kept at the beginning of the database file, which |
| 8048 | ** is necessary for AUTOVACUUM to work right. *piMoved is set to the |
| 8049 | ** page number that used to be the last root page in the file before |
| 8050 | ** the move. If no page gets moved, *piMoved is set to 0. |
| 8051 | ** The last root page is recorded in meta[3] and the value of |
| 8052 | ** meta[3] is updated by this procedure. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 8053 | */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 8054 | static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 8055 | int rc; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 8056 | MemPage *pPage = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 8057 | BtShared *pBt = p->pBt; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 8058 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 8059 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 8060 | assert( p->inTrans==TRANS_WRITE ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 8061 | |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 8062 | /* It is illegal to drop a table if any cursors are open on the |
| 8063 | ** database. This is because in auto-vacuum mode the backend may |
| 8064 | ** need to move another root-page to fill a gap left by the deleted |
| 8065 | ** root page. If an open cursor was using this page a problem would |
| 8066 | ** occur. |
drh | c046e3e | 2009-07-15 11:26:44 +0000 | [diff] [blame] | 8067 | ** |
| 8068 | ** This error is caught long before control reaches this point. |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 8069 | */ |
drh | c046e3e | 2009-07-15 11:26:44 +0000 | [diff] [blame] | 8070 | if( NEVER(pBt->pCursor) ){ |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 8071 | sqlite3ConnectionBlocked(p->db, pBt->pCursor->pBtree->db); |
| 8072 | return SQLITE_LOCKED_SHAREDCACHE; |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 8073 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 8074 | |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 8075 | rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 8076 | if( rc ) return rc; |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 8077 | rc = sqlite3BtreeClearTable(p, iTable, 0); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 8078 | if( rc ){ |
| 8079 | releasePage(pPage); |
| 8080 | return rc; |
| 8081 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 8082 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 8083 | *piMoved = 0; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 8084 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 8085 | if( iTable>1 ){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 8086 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 8087 | freePage(pPage, &rc); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 8088 | releasePage(pPage); |
| 8089 | #else |
| 8090 | if( pBt->autoVacuum ){ |
| 8091 | Pgno maxRootPgno; |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 8092 | sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 8093 | |
| 8094 | if( iTable==maxRootPgno ){ |
| 8095 | /* If the table being dropped is the table with the largest root-page |
| 8096 | ** number in the database, put the root page on the free list. |
| 8097 | */ |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 8098 | freePage(pPage, &rc); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 8099 | releasePage(pPage); |
| 8100 | if( rc!=SQLITE_OK ){ |
| 8101 | return rc; |
| 8102 | } |
| 8103 | }else{ |
| 8104 | /* The table being dropped does not have the largest root-page |
| 8105 | ** number in the database. So move the page that does into the |
| 8106 | ** gap left by the deleted root-page. |
| 8107 | */ |
| 8108 | MemPage *pMove; |
| 8109 | releasePage(pPage); |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 8110 | rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 8111 | if( rc!=SQLITE_OK ){ |
| 8112 | return rc; |
| 8113 | } |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 8114 | rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 8115 | releasePage(pMove); |
| 8116 | if( rc!=SQLITE_OK ){ |
| 8117 | return rc; |
| 8118 | } |
drh | fe3313f | 2009-07-21 19:02:20 +0000 | [diff] [blame] | 8119 | pMove = 0; |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 8120 | rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0); |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 8121 | freePage(pMove, &rc); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 8122 | releasePage(pMove); |
| 8123 | if( rc!=SQLITE_OK ){ |
| 8124 | return rc; |
| 8125 | } |
| 8126 | *piMoved = maxRootPgno; |
| 8127 | } |
| 8128 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 8129 | /* Set the new 'max-root-page' value in the database header. This |
| 8130 | ** is the old value less one, less one more if that happens to |
| 8131 | ** be a root-page number, less one again if that is the |
| 8132 | ** PENDING_BYTE_PAGE. |
| 8133 | */ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 8134 | maxRootPgno--; |
drh | e184965 | 2009-07-15 18:15:22 +0000 | [diff] [blame] | 8135 | while( maxRootPgno==PENDING_BYTE_PAGE(pBt) |
| 8136 | || PTRMAP_ISPAGE(pBt, maxRootPgno) ){ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 8137 | maxRootPgno--; |
| 8138 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 8139 | assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) ); |
| 8140 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 8141 | rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 8142 | }else{ |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 8143 | freePage(pPage, &rc); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 8144 | releasePage(pPage); |
| 8145 | } |
| 8146 | #endif |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 8147 | }else{ |
drh | c046e3e | 2009-07-15 11:26:44 +0000 | [diff] [blame] | 8148 | /* If sqlite3BtreeDropTable was called on page 1. |
| 8149 | ** This really never should happen except in a corrupt |
| 8150 | ** database. |
| 8151 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 8152 | zeroPage(pPage, PTF_INTKEY|PTF_LEAF ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 8153 | releasePage(pPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 8154 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 8155 | return rc; |
| 8156 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 8157 | int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){ |
| 8158 | int rc; |
| 8159 | sqlite3BtreeEnter(p); |
dan | 7733a4d | 2011-09-02 18:03:16 +0000 | [diff] [blame] | 8160 | rc = btreeDropTable(p, iTable, piMoved); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 8161 | sqlite3BtreeLeave(p); |
| 8162 | return rc; |
| 8163 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 8164 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 8165 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 8166 | /* |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 8167 | ** This function may only be called if the b-tree connection already |
| 8168 | ** has a read or write transaction open on the database. |
| 8169 | ** |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 8170 | ** Read the meta-information out of a database file. Meta[0] |
| 8171 | ** is the number of free pages currently in the database. Meta[1] |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 8172 | ** through meta[15] are available for use by higher layers. Meta[0] |
| 8173 | ** is read-only, the others are read/write. |
| 8174 | ** |
| 8175 | ** The schema layer numbers meta values differently. At the schema |
| 8176 | ** layer (and the SetCookie and ReadCookie opcodes) the number of |
| 8177 | ** 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] | 8178 | */ |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 8179 | void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 8180 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 8181 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 8182 | sqlite3BtreeEnter(p); |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 8183 | assert( p->inTrans>TRANS_NONE ); |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 8184 | assert( SQLITE_OK==querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK) ); |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 8185 | assert( pBt->pPage1 ); |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 8186 | assert( idx>=0 && idx<=15 ); |
danielk1977 | ea89730 | 2008-09-19 15:10:58 +0000 | [diff] [blame] | 8187 | |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 8188 | *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]); |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 8189 | |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 8190 | /* If auto-vacuum is disabled in this build and this is an auto-vacuum |
| 8191 | ** database, mark the database as read-only. */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 8192 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 8193 | if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ){ |
| 8194 | pBt->btsFlags |= BTS_READ_ONLY; |
| 8195 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 8196 | #endif |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 8197 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 8198 | sqlite3BtreeLeave(p); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 8199 | } |
| 8200 | |
| 8201 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 8202 | ** Write meta-information back into the database. Meta[0] is |
| 8203 | ** read-only and may not be written. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 8204 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 8205 | int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){ |
| 8206 | BtShared *pBt = p->pBt; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 8207 | unsigned char *pP1; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 8208 | int rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 8209 | assert( idx>=1 && idx<=15 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 8210 | sqlite3BtreeEnter(p); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 8211 | assert( p->inTrans==TRANS_WRITE ); |
| 8212 | assert( pBt->pPage1!=0 ); |
| 8213 | pP1 = pBt->pPage1->aData; |
| 8214 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 8215 | if( rc==SQLITE_OK ){ |
| 8216 | put4byte(&pP1[36 + idx*4], iMeta); |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 +0000 | [diff] [blame] | 8217 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 8218 | if( idx==BTREE_INCR_VACUUM ){ |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 8219 | assert( pBt->autoVacuum || iMeta==0 ); |
| 8220 | assert( iMeta==0 || iMeta==1 ); |
| 8221 | pBt->incrVacuum = (u8)iMeta; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 8222 | } |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 8223 | #endif |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 8224 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 8225 | sqlite3BtreeLeave(p); |
| 8226 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 8227 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 8228 | |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 8229 | #ifndef SQLITE_OMIT_BTREECOUNT |
| 8230 | /* |
| 8231 | ** The first argument, pCur, is a cursor opened on some b-tree. Count the |
| 8232 | ** number of entries in the b-tree and write the result to *pnEntry. |
| 8233 | ** |
| 8234 | ** SQLITE_OK is returned if the operation is successfully executed. |
| 8235 | ** Otherwise, if an error is encountered (i.e. an IO error or database |
| 8236 | ** corruption) an SQLite error code is returned. |
| 8237 | */ |
| 8238 | int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){ |
| 8239 | i64 nEntry = 0; /* Value to return in *pnEntry */ |
| 8240 | int rc; /* Return code */ |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 8241 | |
| 8242 | if( pCur->pgnoRoot==0 ){ |
| 8243 | *pnEntry = 0; |
| 8244 | return SQLITE_OK; |
| 8245 | } |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 8246 | rc = moveToRoot(pCur); |
| 8247 | |
| 8248 | /* Unless an error occurs, the following loop runs one iteration for each |
| 8249 | ** page in the B-Tree structure (not including overflow pages). |
| 8250 | */ |
| 8251 | while( rc==SQLITE_OK ){ |
| 8252 | int iIdx; /* Index of child node in parent */ |
| 8253 | MemPage *pPage; /* Current page of the b-tree */ |
| 8254 | |
| 8255 | /* If this is a leaf page or the tree is not an int-key tree, then |
| 8256 | ** this page contains countable entries. Increment the entry counter |
| 8257 | ** accordingly. |
| 8258 | */ |
| 8259 | pPage = pCur->apPage[pCur->iPage]; |
| 8260 | if( pPage->leaf || !pPage->intKey ){ |
| 8261 | nEntry += pPage->nCell; |
| 8262 | } |
| 8263 | |
| 8264 | /* pPage is a leaf node. This loop navigates the cursor so that it |
| 8265 | ** points to the first interior cell that it points to the parent of |
| 8266 | ** the next page in the tree that has not yet been visited. The |
| 8267 | ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell |
| 8268 | ** of the page, or to the number of cells in the page if the next page |
| 8269 | ** to visit is the right-child of its parent. |
| 8270 | ** |
| 8271 | ** If all pages in the tree have been visited, return SQLITE_OK to the |
| 8272 | ** caller. |
| 8273 | */ |
| 8274 | if( pPage->leaf ){ |
| 8275 | do { |
| 8276 | if( pCur->iPage==0 ){ |
| 8277 | /* All pages of the b-tree have been visited. Return successfully. */ |
| 8278 | *pnEntry = nEntry; |
| 8279 | return SQLITE_OK; |
| 8280 | } |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 8281 | moveToParent(pCur); |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 8282 | }while ( pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell ); |
| 8283 | |
| 8284 | pCur->aiIdx[pCur->iPage]++; |
| 8285 | pPage = pCur->apPage[pCur->iPage]; |
| 8286 | } |
| 8287 | |
| 8288 | /* Descend to the child node of the cell that the cursor currently |
| 8289 | ** points at. This is the right-child if (iIdx==pPage->nCell). |
| 8290 | */ |
| 8291 | iIdx = pCur->aiIdx[pCur->iPage]; |
| 8292 | if( iIdx==pPage->nCell ){ |
| 8293 | rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); |
| 8294 | }else{ |
| 8295 | rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx))); |
| 8296 | } |
| 8297 | } |
| 8298 | |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 8299 | /* An error has occurred. Return an error code. */ |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 8300 | return rc; |
| 8301 | } |
| 8302 | #endif |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 8303 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 8304 | /* |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8305 | ** Return the pager associated with a BTree. This routine is used for |
| 8306 | ** testing and debugging only. |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 8307 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 8308 | Pager *sqlite3BtreePager(Btree *p){ |
| 8309 | return p->pBt->pPager; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 8310 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8311 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 8312 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8313 | /* |
| 8314 | ** Append a message to the error message string. |
| 8315 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 8316 | static void checkAppendMsg( |
| 8317 | IntegrityCk *pCheck, |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 8318 | const char *zFormat, |
| 8319 | ... |
| 8320 | ){ |
| 8321 | va_list ap; |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8322 | char zBuf[200]; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 8323 | if( !pCheck->mxErr ) return; |
| 8324 | pCheck->mxErr--; |
| 8325 | pCheck->nErr++; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 8326 | va_start(ap, zFormat); |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 8327 | if( pCheck->errMsg.nChar ){ |
| 8328 | sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8329 | } |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8330 | if( pCheck->zPfx ){ |
| 8331 | sqlite3_snprintf(sizeof(zBuf), zBuf, pCheck->zPfx, pCheck->v1, pCheck->v2); |
| 8332 | sqlite3StrAccumAppendAll(&pCheck->errMsg, zBuf); |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 8333 | } |
| 8334 | sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap); |
| 8335 | va_end(ap); |
drh | b49bc86 | 2013-08-21 21:12:10 +0000 | [diff] [blame] | 8336 | if( pCheck->errMsg.accError==STRACCUM_NOMEM ){ |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 8337 | pCheck->mallocFailed = 1; |
| 8338 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8339 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 8340 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8341 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 8342 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
dan | 1235bb1 | 2012-04-03 17:43:28 +0000 | [diff] [blame] | 8343 | |
| 8344 | /* |
| 8345 | ** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that |
| 8346 | ** corresponds to page iPg is already set. |
| 8347 | */ |
| 8348 | static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){ |
| 8349 | assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 ); |
| 8350 | return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07))); |
| 8351 | } |
| 8352 | |
| 8353 | /* |
| 8354 | ** Set the bit in the IntegrityCk.aPgRef[] array that corresponds to page iPg. |
| 8355 | */ |
| 8356 | static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){ |
| 8357 | assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 ); |
| 8358 | pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07)); |
| 8359 | } |
| 8360 | |
| 8361 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8362 | /* |
| 8363 | ** Add 1 to the reference count for page iPage. If this is the second |
| 8364 | ** reference to the page, add an error message to pCheck->zErrMsg. |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 8365 | ** Return 1 if there are 2 or more references to the page and 0 if |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8366 | ** if this is the first reference to the page. |
| 8367 | ** |
| 8368 | ** Also check that the page number is in bounds. |
| 8369 | */ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8370 | static int checkRef(IntegrityCk *pCheck, Pgno iPage){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8371 | if( iPage==0 ) return 1; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 8372 | if( iPage>pCheck->nPage ){ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8373 | checkAppendMsg(pCheck, "invalid page number %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8374 | return 1; |
| 8375 | } |
dan | 1235bb1 | 2012-04-03 17:43:28 +0000 | [diff] [blame] | 8376 | if( getPageReferenced(pCheck, iPage) ){ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8377 | checkAppendMsg(pCheck, "2nd reference to page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8378 | return 1; |
| 8379 | } |
dan | 1235bb1 | 2012-04-03 17:43:28 +0000 | [diff] [blame] | 8380 | setPageReferenced(pCheck, iPage); |
| 8381 | return 0; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8382 | } |
| 8383 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 8384 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 8385 | /* |
| 8386 | ** Check that the entry in the pointer-map for page iChild maps to |
| 8387 | ** page iParent, pointer type ptrType. If not, append an error message |
| 8388 | ** to pCheck. |
| 8389 | */ |
| 8390 | static void checkPtrmap( |
| 8391 | IntegrityCk *pCheck, /* Integrity check context */ |
| 8392 | Pgno iChild, /* Child page number */ |
| 8393 | u8 eType, /* Expected pointer map type */ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8394 | Pgno iParent /* Expected pointer map parent page number */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 8395 | ){ |
| 8396 | int rc; |
| 8397 | u8 ePtrmapType; |
| 8398 | Pgno iPtrmapParent; |
| 8399 | |
| 8400 | rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent); |
| 8401 | if( rc!=SQLITE_OK ){ |
drh | b56cd55 | 2009-05-01 13:16:54 +0000 | [diff] [blame] | 8402 | if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->mallocFailed = 1; |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8403 | checkAppendMsg(pCheck, "Failed to read ptrmap key=%d", iChild); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 8404 | return; |
| 8405 | } |
| 8406 | |
| 8407 | if( ePtrmapType!=eType || iPtrmapParent!=iParent ){ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8408 | checkAppendMsg(pCheck, |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 8409 | "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", |
| 8410 | iChild, eType, iParent, ePtrmapType, iPtrmapParent); |
| 8411 | } |
| 8412 | } |
| 8413 | #endif |
| 8414 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8415 | /* |
| 8416 | ** Check the integrity of the freelist or of an overflow page list. |
| 8417 | ** Verify that the number of pages on the list is N. |
| 8418 | */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 8419 | static void checkList( |
| 8420 | IntegrityCk *pCheck, /* Integrity checking context */ |
| 8421 | int isFreeList, /* True for a freelist. False for overflow page list */ |
| 8422 | int iPage, /* Page number for first page in the list */ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8423 | int N /* Expected number of pages in the list */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 8424 | ){ |
| 8425 | int i; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 8426 | int expected = N; |
| 8427 | int iFirst = iPage; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 8428 | while( N-- > 0 && pCheck->mxErr ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 8429 | DbPage *pOvflPage; |
| 8430 | unsigned char *pOvflData; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8431 | if( iPage<1 ){ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8432 | checkAppendMsg(pCheck, |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 8433 | "%d of %d pages missing from overflow list starting at %d", |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 8434 | N+1, expected, iFirst); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8435 | break; |
| 8436 | } |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8437 | if( checkRef(pCheck, iPage) ) break; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 8438 | if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8439 | checkAppendMsg(pCheck, "failed to get page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8440 | break; |
| 8441 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 8442 | pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 8443 | if( isFreeList ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 8444 | int n = get4byte(&pOvflData[4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 8445 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 8446 | if( pCheck->pBt->autoVacuum ){ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8447 | checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 8448 | } |
| 8449 | #endif |
drh | 43b18e1 | 2010-08-17 19:40:08 +0000 | [diff] [blame] | 8450 | if( n>(int)pCheck->pBt->usableSize/4-2 ){ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8451 | checkAppendMsg(pCheck, |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 8452 | "freelist leaf count too big on page %d", iPage); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 8453 | N--; |
| 8454 | }else{ |
| 8455 | for(i=0; i<n; i++){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 8456 | Pgno iFreePage = get4byte(&pOvflData[8+i*4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 8457 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 8458 | if( pCheck->pBt->autoVacuum ){ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8459 | checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 8460 | } |
| 8461 | #endif |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8462 | checkRef(pCheck, iFreePage); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 8463 | } |
| 8464 | N -= n; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 8465 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 8466 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 8467 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 8468 | else{ |
| 8469 | /* If this database supports auto-vacuum and iPage is not the last |
| 8470 | ** page in this overflow list, check that the pointer-map entry for |
| 8471 | ** the following page matches iPage. |
| 8472 | */ |
| 8473 | if( pCheck->pBt->autoVacuum && N>0 ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 8474 | i = get4byte(pOvflData); |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8475 | checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 8476 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 8477 | } |
| 8478 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 8479 | iPage = get4byte(pOvflData); |
| 8480 | sqlite3PagerUnref(pOvflPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8481 | } |
| 8482 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 8483 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8484 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 8485 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8486 | /* |
| 8487 | ** Do various sanity checks on a single page of a tree. Return |
| 8488 | ** the tree depth. Root pages return 0. Parents of root pages |
| 8489 | ** return 1, and so forth. |
| 8490 | ** |
| 8491 | ** These checks are done: |
| 8492 | ** |
| 8493 | ** 1. Make sure that cells and freeblocks do not overlap |
| 8494 | ** but combine to completely cover the page. |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 8495 | ** NO 2. Make sure cell keys are in order. |
| 8496 | ** NO 3. Make sure no key is less than or equal to zLowerBound. |
| 8497 | ** NO 4. Make sure no key is greater than or equal to zUpperBound. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8498 | ** 5. Check the integrity of overflow pages. |
| 8499 | ** 6. Recursively call checkTreePage on all children. |
| 8500 | ** 7. Verify that the depth of all children is the same. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 8501 | ** 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] | 8502 | ** the root of the tree. |
| 8503 | */ |
| 8504 | static int checkTreePage( |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 8505 | IntegrityCk *pCheck, /* Context for the sanity check */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8506 | int iPage, /* Page number of the page to check */ |
shaneh | 195475d | 2010-02-19 04:28:08 +0000 | [diff] [blame] | 8507 | i64 *pnParentMinKey, |
| 8508 | i64 *pnParentMaxKey |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8509 | ){ |
| 8510 | MemPage *pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 8511 | int i, rc, depth, d2, pgno, cnt; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 8512 | int hdr, cellStart; |
| 8513 | int nCell; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 8514 | u8 *data; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 8515 | BtShared *pBt; |
drh | 4f26bb6 | 2005-09-08 14:17:20 +0000 | [diff] [blame] | 8516 | int usableSize; |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 8517 | char *hit = 0; |
shaneh | 195475d | 2010-02-19 04:28:08 +0000 | [diff] [blame] | 8518 | i64 nMinKey = 0; |
| 8519 | i64 nMaxKey = 0; |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8520 | const char *saved_zPfx = pCheck->zPfx; |
| 8521 | int saved_v1 = pCheck->v1; |
| 8522 | int saved_v2 = pCheck->v2; |
danielk1977 | ef73ee9 | 2004-11-06 12:26:07 +0000 | [diff] [blame] | 8523 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8524 | /* Check that the page exists |
| 8525 | */ |
drh | d9cb6ac | 2005-10-20 07:28:17 +0000 | [diff] [blame] | 8526 | pBt = pCheck->pBt; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 8527 | usableSize = pBt->usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8528 | if( iPage==0 ) return 0; |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8529 | if( checkRef(pCheck, iPage) ) return 0; |
| 8530 | pCheck->zPfx = "Page %d: "; |
| 8531 | pCheck->v1 = iPage; |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 8532 | if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8533 | checkAppendMsg(pCheck, |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 8534 | "unable to get the page. error code=%d", rc); |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8535 | depth = -1; |
| 8536 | goto end_of_check; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8537 | } |
danielk1977 | 93caf5a | 2009-07-11 06:55:33 +0000 | [diff] [blame] | 8538 | |
| 8539 | /* Clear MemPage.isInit to make sure the corruption detection code in |
| 8540 | ** btreeInitPage() is executed. */ |
| 8541 | pPage->isInit = 0; |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 8542 | if( (rc = btreeInitPage(pPage))!=0 ){ |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 8543 | assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8544 | checkAppendMsg(pCheck, |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 8545 | "btreeInitPage() returns error code %d", rc); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 8546 | releasePage(pPage); |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8547 | depth = -1; |
| 8548 | goto end_of_check; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8549 | } |
| 8550 | |
| 8551 | /* Check out all the cells. |
| 8552 | */ |
| 8553 | depth = 0; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 8554 | for(i=0; i<pPage->nCell && pCheck->mxErr; i++){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 8555 | u8 *pCell; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 8556 | u32 sz; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 8557 | CellInfo info; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8558 | |
| 8559 | /* Check payload overflow pages |
| 8560 | */ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8561 | pCheck->zPfx = "On tree page %d cell %d: "; |
| 8562 | pCheck->v1 = iPage; |
| 8563 | pCheck->v2 = i; |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 8564 | pCell = findCell(pPage,i); |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 8565 | btreeParseCellPtr(pPage, pCell, &info); |
drh | ab1cc58 | 2014-09-23 21:25:19 +0000 | [diff] [blame] | 8566 | sz = info.nPayload; |
shaneh | 195475d | 2010-02-19 04:28:08 +0000 | [diff] [blame] | 8567 | /* For intKey pages, check that the keys are in order. |
| 8568 | */ |
drh | ab1cc58 | 2014-09-23 21:25:19 +0000 | [diff] [blame] | 8569 | if( pPage->intKey ){ |
| 8570 | if( i==0 ){ |
| 8571 | nMinKey = nMaxKey = info.nKey; |
| 8572 | }else if( info.nKey <= nMaxKey ){ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8573 | checkAppendMsg(pCheck, |
drh | ab1cc58 | 2014-09-23 21:25:19 +0000 | [diff] [blame] | 8574 | "Rowid %lld out of order (previous was %lld)", info.nKey, nMaxKey); |
shaneh | 195475d | 2010-02-19 04:28:08 +0000 | [diff] [blame] | 8575 | } |
| 8576 | nMaxKey = info.nKey; |
| 8577 | } |
danielk1977 | 5be31f5 | 2009-03-30 13:53:43 +0000 | [diff] [blame] | 8578 | if( (sz>info.nLocal) |
| 8579 | && (&pCell[info.iOverflow]<=&pPage->aData[pBt->usableSize]) |
| 8580 | ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 8581 | int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 8582 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
| 8583 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 8584 | if( pBt->autoVacuum ){ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8585 | checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 8586 | } |
| 8587 | #endif |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8588 | checkList(pCheck, 0, pgnoOvfl, nPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8589 | } |
| 8590 | |
| 8591 | /* Check sanity of left child page. |
| 8592 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 8593 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 8594 | pgno = get4byte(pCell); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 8595 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 8596 | if( pBt->autoVacuum ){ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8597 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 8598 | } |
| 8599 | #endif |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8600 | d2 = checkTreePage(pCheck, pgno, &nMinKey, i==0?NULL:&nMaxKey); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 8601 | if( i>0 && d2!=depth ){ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8602 | checkAppendMsg(pCheck, "Child page depth differs"); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 8603 | } |
| 8604 | depth = d2; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8605 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8606 | } |
shaneh | 195475d | 2010-02-19 04:28:08 +0000 | [diff] [blame] | 8607 | |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 8608 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 8609 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8610 | pCheck->zPfx = "On page %d at right child: "; |
| 8611 | pCheck->v1 = iPage; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 8612 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 8613 | if( pBt->autoVacuum ){ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8614 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 8615 | } |
| 8616 | #endif |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8617 | checkTreePage(pCheck, pgno, NULL, !pPage->nCell?NULL:&nMaxKey); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 8618 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8619 | |
shaneh | 195475d | 2010-02-19 04:28:08 +0000 | [diff] [blame] | 8620 | /* For intKey leaf pages, check that the min/max keys are in order |
| 8621 | ** with any left/parent/right pages. |
| 8622 | */ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8623 | pCheck->zPfx = "Page %d: "; |
| 8624 | pCheck->v1 = iPage; |
shaneh | 195475d | 2010-02-19 04:28:08 +0000 | [diff] [blame] | 8625 | if( pPage->leaf && pPage->intKey ){ |
| 8626 | /* if we are a left child page */ |
| 8627 | if( pnParentMinKey ){ |
| 8628 | /* if we are the left most child page */ |
| 8629 | if( !pnParentMaxKey ){ |
| 8630 | if( nMaxKey > *pnParentMinKey ){ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8631 | checkAppendMsg(pCheck, |
shaneh | 195475d | 2010-02-19 04:28:08 +0000 | [diff] [blame] | 8632 | "Rowid %lld out of order (max larger than parent min of %lld)", |
| 8633 | nMaxKey, *pnParentMinKey); |
| 8634 | } |
| 8635 | }else{ |
| 8636 | if( nMinKey <= *pnParentMinKey ){ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8637 | checkAppendMsg(pCheck, |
shaneh | 195475d | 2010-02-19 04:28:08 +0000 | [diff] [blame] | 8638 | "Rowid %lld out of order (min less than parent min of %lld)", |
| 8639 | nMinKey, *pnParentMinKey); |
| 8640 | } |
| 8641 | if( nMaxKey > *pnParentMaxKey ){ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8642 | checkAppendMsg(pCheck, |
shaneh | 195475d | 2010-02-19 04:28:08 +0000 | [diff] [blame] | 8643 | "Rowid %lld out of order (max larger than parent max of %lld)", |
| 8644 | nMaxKey, *pnParentMaxKey); |
| 8645 | } |
| 8646 | *pnParentMinKey = nMaxKey; |
| 8647 | } |
| 8648 | /* else if we're a right child page */ |
| 8649 | } else if( pnParentMaxKey ){ |
| 8650 | if( nMinKey <= *pnParentMaxKey ){ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8651 | checkAppendMsg(pCheck, |
shaneh | 195475d | 2010-02-19 04:28:08 +0000 | [diff] [blame] | 8652 | "Rowid %lld out of order (min less than parent max of %lld)", |
| 8653 | nMinKey, *pnParentMaxKey); |
| 8654 | } |
| 8655 | } |
| 8656 | } |
| 8657 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8658 | /* Check for complete coverage of the page |
| 8659 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 8660 | data = pPage->aData; |
| 8661 | hdr = pPage->hdrOffset; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 8662 | hit = sqlite3PageMalloc( pBt->pageSize ); |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8663 | pCheck->zPfx = 0; |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 8664 | if( hit==0 ){ |
| 8665 | pCheck->mallocFailed = 1; |
| 8666 | }else{ |
drh | 5d433ce | 2010-08-14 16:02:52 +0000 | [diff] [blame] | 8667 | int contentOffset = get2byteNotZero(&data[hdr+5]); |
drh | d7c7ecd | 2009-07-14 17:48:06 +0000 | [diff] [blame] | 8668 | assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */ |
shane | 5780ebd | 2008-11-11 17:36:30 +0000 | [diff] [blame] | 8669 | memset(hit+contentOffset, 0, usableSize-contentOffset); |
| 8670 | memset(hit, 1, contentOffset); |
drh | fdab026 | 2014-11-20 15:30:50 +0000 | [diff] [blame] | 8671 | /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the |
| 8672 | ** number of cells on the page. */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 8673 | nCell = get2byte(&data[hdr+3]); |
drh | fdab026 | 2014-11-20 15:30:50 +0000 | [diff] [blame] | 8674 | /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page |
| 8675 | ** immediately follows the b-tree page header. */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 8676 | cellStart = hdr + 12 - 4*pPage->leaf; |
drh | fdab026 | 2014-11-20 15:30:50 +0000 | [diff] [blame] | 8677 | /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte |
| 8678 | ** integer offsets to the cell contents. */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 8679 | for(i=0; i<nCell; i++){ |
| 8680 | int pc = get2byte(&data[cellStart+i*2]); |
drh | 9b78f79 | 2010-08-14 21:21:24 +0000 | [diff] [blame] | 8681 | u32 size = 65536; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 8682 | int j; |
drh | 8c2bbb6 | 2009-07-10 02:52:20 +0000 | [diff] [blame] | 8683 | if( pc<=usableSize-4 ){ |
danielk1977 | daca543 | 2008-08-25 11:57:16 +0000 | [diff] [blame] | 8684 | size = cellSizePtr(pPage, &data[pc]); |
| 8685 | } |
drh | 43b18e1 | 2010-08-17 19:40:08 +0000 | [diff] [blame] | 8686 | if( (int)(pc+size-1)>=usableSize ){ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8687 | pCheck->zPfx = 0; |
| 8688 | checkAppendMsg(pCheck, |
shaneh | 195475d | 2010-02-19 04:28:08 +0000 | [diff] [blame] | 8689 | "Corruption detected in cell %d on page %d",i,iPage); |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 8690 | }else{ |
| 8691 | for(j=pc+size-1; j>=pc; j--) hit[j]++; |
| 8692 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 8693 | } |
drh | fdab026 | 2014-11-20 15:30:50 +0000 | [diff] [blame] | 8694 | /* EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header |
| 8695 | ** is the offset of the first freeblock, or zero if there are no |
| 8696 | ** freeblocks on the page. */ |
drh | 8c2bbb6 | 2009-07-10 02:52:20 +0000 | [diff] [blame] | 8697 | i = get2byte(&data[hdr+1]); |
| 8698 | while( i>0 ){ |
| 8699 | int size, j; |
| 8700 | assert( i<=usableSize-4 ); /* Enforced by btreeInitPage() */ |
| 8701 | size = get2byte(&data[i+2]); |
| 8702 | assert( i+size<=usableSize ); /* Enforced by btreeInitPage() */ |
| 8703 | for(j=i+size-1; j>=i; j--) hit[j]++; |
drh | fdab026 | 2014-11-20 15:30:50 +0000 | [diff] [blame] | 8704 | /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a |
| 8705 | ** big-endian integer which is the offset in the b-tree page of the next |
| 8706 | ** freeblock in the chain, or zero if the freeblock is the last on the |
| 8707 | ** chain. */ |
drh | 8c2bbb6 | 2009-07-10 02:52:20 +0000 | [diff] [blame] | 8708 | j = get2byte(&data[i]); |
drh | fdab026 | 2014-11-20 15:30:50 +0000 | [diff] [blame] | 8709 | /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of |
| 8710 | ** increasing offset. */ |
drh | 8c2bbb6 | 2009-07-10 02:52:20 +0000 | [diff] [blame] | 8711 | assert( j==0 || j>i+size ); /* Enforced by btreeInitPage() */ |
| 8712 | assert( j<=usableSize-4 ); /* Enforced by btreeInitPage() */ |
| 8713 | i = j; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 8714 | } |
| 8715 | for(i=cnt=0; i<usableSize; i++){ |
| 8716 | if( hit[i]==0 ){ |
| 8717 | cnt++; |
| 8718 | }else if( hit[i]>1 ){ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8719 | checkAppendMsg(pCheck, |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 8720 | "Multiple uses for byte %d of page %d", i, iPage); |
| 8721 | break; |
| 8722 | } |
| 8723 | } |
drh | fdab026 | 2014-11-20 15:30:50 +0000 | [diff] [blame] | 8724 | /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments |
| 8725 | ** is stored in the fifth field of the b-tree page header. |
| 8726 | ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the |
| 8727 | ** number of fragmented free bytes within the cell content area. |
| 8728 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 8729 | if( cnt!=data[hdr+7] ){ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8730 | checkAppendMsg(pCheck, |
drh | 8c2bbb6 | 2009-07-10 02:52:20 +0000 | [diff] [blame] | 8731 | "Fragmentation of %d bytes reported as %d on page %d", |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 8732 | cnt, data[hdr+7], iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8733 | } |
| 8734 | } |
drh | 8c2bbb6 | 2009-07-10 02:52:20 +0000 | [diff] [blame] | 8735 | sqlite3PageFree(hit); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 8736 | releasePage(pPage); |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8737 | |
| 8738 | end_of_check: |
| 8739 | pCheck->zPfx = saved_zPfx; |
| 8740 | pCheck->v1 = saved_v1; |
| 8741 | pCheck->v2 = saved_v2; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 8742 | return depth+1; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8743 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 8744 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8745 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 8746 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8747 | /* |
| 8748 | ** This routine does a complete check of the given BTree file. aRoot[] is |
| 8749 | ** an array of pages numbers were each page number is the root page of |
| 8750 | ** a table. nRoot is the number of entries in aRoot. |
| 8751 | ** |
danielk1977 | 3509a65 | 2009-07-06 18:56:13 +0000 | [diff] [blame] | 8752 | ** A read-only or read-write transaction must be opened before calling |
| 8753 | ** this function. |
| 8754 | ** |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 8755 | ** Write the number of error seen in *pnErr. Except for some memory |
drh | e43ba70 | 2008-12-05 22:40:08 +0000 | [diff] [blame] | 8756 | ** allocation errors, an error message held in memory obtained from |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 8757 | ** 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] | 8758 | ** returned. If a memory allocation error occurs, NULL is returned. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8759 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 8760 | char *sqlite3BtreeIntegrityCheck( |
| 8761 | Btree *p, /* The btree to be checked */ |
| 8762 | int *aRoot, /* An array of root pages numbers for individual trees */ |
| 8763 | int nRoot, /* Number of entries in aRoot[] */ |
| 8764 | int mxErr, /* Stop reporting errors after this many */ |
| 8765 | int *pnErr /* Write number of errors seen to this variable */ |
| 8766 | ){ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 8767 | Pgno i; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8768 | int nRef; |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 8769 | IntegrityCk sCheck; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 8770 | BtShared *pBt = p->pBt; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 8771 | char zErr[100]; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8772 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 8773 | sqlite3BtreeEnter(p); |
danielk1977 | 3509a65 | 2009-07-06 18:56:13 +0000 | [diff] [blame] | 8774 | assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 8775 | nRef = sqlite3PagerRefcount(pBt->pPager); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8776 | sCheck.pBt = pBt; |
| 8777 | sCheck.pPager = pBt->pPager; |
drh | b129915 | 2010-03-30 22:58:33 +0000 | [diff] [blame] | 8778 | sCheck.nPage = btreePagecount(sCheck.pBt); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 8779 | sCheck.mxErr = mxErr; |
| 8780 | sCheck.nErr = 0; |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 8781 | sCheck.mallocFailed = 0; |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8782 | sCheck.zPfx = 0; |
| 8783 | sCheck.v1 = 0; |
| 8784 | sCheck.v2 = 0; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 8785 | *pnErr = 0; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 8786 | if( sCheck.nPage==0 ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 8787 | sqlite3BtreeLeave(p); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 8788 | return 0; |
| 8789 | } |
dan | 1235bb1 | 2012-04-03 17:43:28 +0000 | [diff] [blame] | 8790 | |
| 8791 | sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1); |
| 8792 | if( !sCheck.aPgRef ){ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 8793 | *pnErr = 1; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 8794 | sqlite3BtreeLeave(p); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 8795 | return 0; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 8796 | } |
drh | 42cac6d | 2004-11-20 20:31:11 +0000 | [diff] [blame] | 8797 | i = PENDING_BYTE_PAGE(pBt); |
dan | 1235bb1 | 2012-04-03 17:43:28 +0000 | [diff] [blame] | 8798 | if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i); |
drh | 32055c2 | 2012-12-12 14:30:03 +0000 | [diff] [blame] | 8799 | sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), SQLITE_MAX_LENGTH); |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 8800 | sCheck.errMsg.useMalloc = 2; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8801 | |
| 8802 | /* Check the integrity of the freelist |
| 8803 | */ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8804 | sCheck.zPfx = "Main freelist: "; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 8805 | checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8806 | get4byte(&pBt->pPage1->aData[36])); |
| 8807 | sCheck.zPfx = 0; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8808 | |
| 8809 | /* Check all the tables. |
| 8810 | */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 8811 | for(i=0; (int)i<nRoot && sCheck.mxErr; i++){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 8812 | if( aRoot[i]==0 ) continue; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 8813 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 8814 | if( pBt->autoVacuum && aRoot[i]>1 ){ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8815 | checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 8816 | } |
| 8817 | #endif |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8818 | sCheck.zPfx = "List of tree roots: "; |
| 8819 | checkTreePage(&sCheck, aRoot[i], NULL, NULL); |
| 8820 | sCheck.zPfx = 0; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8821 | } |
| 8822 | |
| 8823 | /* Make sure every page in the file is referenced |
| 8824 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 8825 | for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 8826 | #ifdef SQLITE_OMIT_AUTOVACUUM |
dan | 1235bb1 | 2012-04-03 17:43:28 +0000 | [diff] [blame] | 8827 | if( getPageReferenced(&sCheck, i)==0 ){ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8828 | checkAppendMsg(&sCheck, "Page %d is never used", i); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8829 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 8830 | #else |
| 8831 | /* If the database supports auto-vacuum, make sure no tables contain |
| 8832 | ** references to pointer-map pages. |
| 8833 | */ |
dan | 1235bb1 | 2012-04-03 17:43:28 +0000 | [diff] [blame] | 8834 | if( getPageReferenced(&sCheck, i)==0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 8835 | (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8836 | checkAppendMsg(&sCheck, "Page %d is never used", i); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 8837 | } |
dan | 1235bb1 | 2012-04-03 17:43:28 +0000 | [diff] [blame] | 8838 | if( getPageReferenced(&sCheck, i)!=0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 8839 | (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8840 | checkAppendMsg(&sCheck, "Pointer map page %d is referenced", i); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 8841 | } |
| 8842 | #endif |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8843 | } |
| 8844 | |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 8845 | /* Make sure this analysis did not leave any unref() pages. |
| 8846 | ** This is an internal consistency check; an integrity check |
| 8847 | ** of the integrity check. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8848 | */ |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 8849 | if( NEVER(nRef != sqlite3PagerRefcount(pBt->pPager)) ){ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 8850 | checkAppendMsg(&sCheck, |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8851 | "Outstanding page count goes from %d to %d during this analysis", |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 8852 | nRef, sqlite3PagerRefcount(pBt->pPager) |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8853 | ); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8854 | } |
| 8855 | |
| 8856 | /* Clean up and report errors. |
| 8857 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 8858 | sqlite3BtreeLeave(p); |
dan | 1235bb1 | 2012-04-03 17:43:28 +0000 | [diff] [blame] | 8859 | sqlite3_free(sCheck.aPgRef); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 8860 | if( sCheck.mallocFailed ){ |
| 8861 | sqlite3StrAccumReset(&sCheck.errMsg); |
| 8862 | *pnErr = sCheck.nErr+1; |
| 8863 | return 0; |
| 8864 | } |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 8865 | *pnErr = sCheck.nErr; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 8866 | if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg); |
| 8867 | return sqlite3StrAccumFinish(&sCheck.errMsg); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8868 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 8869 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 8870 | |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 8871 | /* |
drh | d4e0bb0 | 2012-05-27 01:19:04 +0000 | [diff] [blame] | 8872 | ** Return the full pathname of the underlying database file. Return |
| 8873 | ** an empty string if the database is in-memory or a TEMP database. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 8874 | ** |
| 8875 | ** The pager filename is invariant as long as the pager is |
| 8876 | ** open so it is safe to access without the BtShared mutex. |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 8877 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 8878 | const char *sqlite3BtreeGetFilename(Btree *p){ |
| 8879 | assert( p->pBt->pPager!=0 ); |
drh | d4e0bb0 | 2012-05-27 01:19:04 +0000 | [diff] [blame] | 8880 | return sqlite3PagerFilename(p->pBt->pPager, 1); |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 8881 | } |
| 8882 | |
| 8883 | /* |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 8884 | ** Return the pathname of the journal file for this database. The return |
| 8885 | ** value of this routine is the same regardless of whether the journal file |
| 8886 | ** has been created or not. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 8887 | ** |
| 8888 | ** The pager journal filename is invariant as long as the pager is |
| 8889 | ** open so it is safe to access without the BtShared mutex. |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 8890 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 8891 | const char *sqlite3BtreeGetJournalname(Btree *p){ |
| 8892 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 8893 | return sqlite3PagerJournalname(p->pBt->pPager); |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 8894 | } |
| 8895 | |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 8896 | /* |
| 8897 | ** Return non-zero if a transaction is active. |
| 8898 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 8899 | int sqlite3BtreeIsInTrans(Btree *p){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 8900 | assert( p==0 || sqlite3_mutex_held(p->db->mutex) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 8901 | return (p && (p->inTrans==TRANS_WRITE)); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 8902 | } |
| 8903 | |
dan | a550f2d | 2010-08-02 10:47:05 +0000 | [diff] [blame] | 8904 | #ifndef SQLITE_OMIT_WAL |
| 8905 | /* |
| 8906 | ** Run a checkpoint on the Btree passed as the first argument. |
| 8907 | ** |
| 8908 | ** Return SQLITE_LOCKED if this or any other connection has an open |
| 8909 | ** transaction on the shared-cache the argument Btree is connected to. |
dan | a58f26f | 2010-11-16 18:56:51 +0000 | [diff] [blame] | 8910 | ** |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 8911 | ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART. |
dan | a550f2d | 2010-08-02 10:47:05 +0000 | [diff] [blame] | 8912 | */ |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 8913 | int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){ |
dan | a550f2d | 2010-08-02 10:47:05 +0000 | [diff] [blame] | 8914 | int rc = SQLITE_OK; |
| 8915 | if( p ){ |
| 8916 | BtShared *pBt = p->pBt; |
| 8917 | sqlite3BtreeEnter(p); |
| 8918 | if( pBt->inTransaction!=TRANS_NONE ){ |
| 8919 | rc = SQLITE_LOCKED; |
| 8920 | }else{ |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 8921 | rc = sqlite3PagerCheckpoint(pBt->pPager, eMode, pnLog, pnCkpt); |
dan | a550f2d | 2010-08-02 10:47:05 +0000 | [diff] [blame] | 8922 | } |
| 8923 | sqlite3BtreeLeave(p); |
| 8924 | } |
| 8925 | return rc; |
| 8926 | } |
| 8927 | #endif |
| 8928 | |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 8929 | /* |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 8930 | ** Return non-zero if a read (or write) transaction is active. |
| 8931 | */ |
| 8932 | int sqlite3BtreeIsInReadTrans(Btree *p){ |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 8933 | assert( p ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 8934 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 8935 | return p->inTrans!=TRANS_NONE; |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 8936 | } |
| 8937 | |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 8938 | int sqlite3BtreeIsInBackup(Btree *p){ |
| 8939 | assert( p ); |
| 8940 | assert( sqlite3_mutex_held(p->db->mutex) ); |
| 8941 | return p->nBackup!=0; |
| 8942 | } |
| 8943 | |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 8944 | /* |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 8945 | ** This function returns a pointer to a blob of memory associated with |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 8946 | ** 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] | 8947 | ** purposes (for example, to store a high-level schema associated with |
| 8948 | ** the shared-btree). The btree layer manages reference counting issues. |
| 8949 | ** |
| 8950 | ** The first time this is called on a shared-btree, nBytes bytes of memory |
| 8951 | ** are allocated, zeroed, and returned to the caller. For each subsequent |
| 8952 | ** call the nBytes parameter is ignored and a pointer to the same blob |
| 8953 | ** of memory returned. |
| 8954 | ** |
danielk1977 | 171bfed | 2008-06-23 09:50:50 +0000 | [diff] [blame] | 8955 | ** If the nBytes parameter is 0 and the blob of memory has not yet been |
| 8956 | ** allocated, a null pointer is returned. If the blob has already been |
| 8957 | ** allocated, it is returned as normal. |
| 8958 | ** |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 8959 | ** Just before the shared-btree is closed, the function passed as the |
| 8960 | ** xFree argument when the memory allocation was made is invoked on the |
drh | 4fa7d7c | 2011-04-03 02:41:00 +0000 | [diff] [blame] | 8961 | ** blob of allocated memory. The xFree function should not call sqlite3_free() |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 8962 | ** on the memory, the btree layer does that. |
| 8963 | */ |
| 8964 | void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){ |
| 8965 | BtShared *pBt = p->pBt; |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 8966 | sqlite3BtreeEnter(p); |
danielk1977 | 171bfed | 2008-06-23 09:50:50 +0000 | [diff] [blame] | 8967 | if( !pBt->pSchema && nBytes ){ |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 8968 | pBt->pSchema = sqlite3DbMallocZero(0, nBytes); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 8969 | pBt->xFreeSchema = xFree; |
| 8970 | } |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 8971 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 8972 | return pBt->pSchema; |
| 8973 | } |
| 8974 | |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 8975 | /* |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 8976 | ** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared |
| 8977 | ** btree as the argument handle holds an exclusive lock on the |
| 8978 | ** sqlite_master table. Otherwise SQLITE_OK. |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 8979 | */ |
| 8980 | int sqlite3BtreeSchemaLocked(Btree *p){ |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 8981 | int rc; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 8982 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 8983 | sqlite3BtreeEnter(p); |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 8984 | rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK); |
| 8985 | assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 8986 | sqlite3BtreeLeave(p); |
| 8987 | return rc; |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 8988 | } |
| 8989 | |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 8990 | |
| 8991 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 8992 | /* |
| 8993 | ** Obtain a lock on the table whose root page is iTab. The |
| 8994 | ** lock is a write lock if isWritelock is true or a read lock |
| 8995 | ** if it is false. |
| 8996 | */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 8997 | int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){ |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 8998 | int rc = SQLITE_OK; |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 8999 | assert( p->inTrans!=TRANS_NONE ); |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 9000 | if( p->sharable ){ |
| 9001 | u8 lockType = READ_LOCK + isWriteLock; |
| 9002 | assert( READ_LOCK+1==WRITE_LOCK ); |
| 9003 | assert( isWriteLock==0 || isWriteLock==1 ); |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 9004 | |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 9005 | sqlite3BtreeEnter(p); |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 9006 | rc = querySharedCacheTableLock(p, iTab, lockType); |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 9007 | if( rc==SQLITE_OK ){ |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 9008 | rc = setSharedCacheTableLock(p, iTab, lockType); |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 9009 | } |
| 9010 | sqlite3BtreeLeave(p); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 9011 | } |
| 9012 | return rc; |
| 9013 | } |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 9014 | #endif |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 9015 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 9016 | #ifndef SQLITE_OMIT_INCRBLOB |
| 9017 | /* |
| 9018 | ** Argument pCsr must be a cursor opened for writing on an |
| 9019 | ** INTKEY table currently pointing at a valid table entry. |
| 9020 | ** This function modifies the data stored as part of that entry. |
danielk1977 | ecaecf9 | 2009-07-08 08:05:35 +0000 | [diff] [blame] | 9021 | ** |
| 9022 | ** Only the data content may only be modified, it is not possible to |
| 9023 | ** change the length of the data stored. If this function is called with |
| 9024 | ** parameters that attempt to write past the end of the existing data, |
| 9025 | ** no modifications are made and SQLITE_CORRUPT is returned. |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 9026 | */ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 9027 | int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){ |
danielk1977 | c9000e6 | 2009-07-08 13:55:28 +0000 | [diff] [blame] | 9028 | int rc; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 9029 | assert( cursorHoldsMutex(pCsr) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 9030 | assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) ); |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 9031 | assert( pCsr->curFlags & BTCF_Incrblob ); |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 9032 | |
danielk1977 | c9000e6 | 2009-07-08 13:55:28 +0000 | [diff] [blame] | 9033 | rc = restoreCursorPosition(pCsr); |
| 9034 | if( rc!=SQLITE_OK ){ |
| 9035 | return rc; |
| 9036 | } |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 9037 | assert( pCsr->eState!=CURSOR_REQUIRESEEK ); |
| 9038 | if( pCsr->eState!=CURSOR_VALID ){ |
| 9039 | return SQLITE_ABORT; |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 9040 | } |
| 9041 | |
dan | 227a1c4 | 2013-04-03 11:17:39 +0000 | [diff] [blame] | 9042 | /* Save the positions of all other cursors open on this table. This is |
| 9043 | ** required in case any of them are holding references to an xFetch |
| 9044 | ** version of the b-tree page modified by the accessPayload call below. |
drh | 370c9f4 | 2013-04-03 20:04:04 +0000 | [diff] [blame] | 9045 | ** |
drh | 3f38740 | 2014-09-24 01:23:00 +0000 | [diff] [blame] | 9046 | ** Note that pCsr must be open on a INTKEY table and saveCursorPosition() |
drh | 370c9f4 | 2013-04-03 20:04:04 +0000 | [diff] [blame] | 9047 | ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence |
| 9048 | ** saveAllCursors can only return SQLITE_OK. |
dan | 227a1c4 | 2013-04-03 11:17:39 +0000 | [diff] [blame] | 9049 | */ |
drh | 370c9f4 | 2013-04-03 20:04:04 +0000 | [diff] [blame] | 9050 | VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr); |
| 9051 | assert( rc==SQLITE_OK ); |
dan | 227a1c4 | 2013-04-03 11:17:39 +0000 | [diff] [blame] | 9052 | |
danielk1977 | c9000e6 | 2009-07-08 13:55:28 +0000 | [diff] [blame] | 9053 | /* Check some assumptions: |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 9054 | ** (a) the cursor is open for writing, |
danielk1977 | c9000e6 | 2009-07-08 13:55:28 +0000 | [diff] [blame] | 9055 | ** (b) there is a read/write transaction open, |
| 9056 | ** (c) the connection holds a write-lock on the table (if required), |
| 9057 | ** (d) there are no conflicting read-locks, and |
| 9058 | ** (e) the cursor points at a valid row of an intKey table. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 9059 | */ |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 9060 | if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){ |
danielk1977 | 4f02960 | 2009-07-08 18:45:37 +0000 | [diff] [blame] | 9061 | return SQLITE_READONLY; |
| 9062 | } |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 9063 | assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0 |
| 9064 | && pCsr->pBt->inTransaction==TRANS_WRITE ); |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 9065 | assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) ); |
| 9066 | assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) ); |
danielk1977 | c9000e6 | 2009-07-08 13:55:28 +0000 | [diff] [blame] | 9067 | assert( pCsr->apPage[pCsr->iPage]->intKey ); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 9068 | |
drh | fb19268 | 2009-07-11 18:26:28 +0000 | [diff] [blame] | 9069 | return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 9070 | } |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 9071 | |
| 9072 | /* |
dan | 5a500af | 2014-03-11 20:33:04 +0000 | [diff] [blame] | 9073 | ** Mark this cursor as an incremental blob cursor. |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 9074 | */ |
dan | 5a500af | 2014-03-11 20:33:04 +0000 | [diff] [blame] | 9075 | void sqlite3BtreeIncrblobCursor(BtCursor *pCur){ |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 9076 | pCur->curFlags |= BTCF_Incrblob; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 9077 | } |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 9078 | #endif |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 9079 | |
| 9080 | /* |
| 9081 | ** Set both the "read version" (single byte at byte offset 18) and |
| 9082 | ** "write version" (single byte at byte offset 19) fields in the database |
| 9083 | ** header to iVersion. |
| 9084 | */ |
| 9085 | int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){ |
| 9086 | BtShared *pBt = pBtree->pBt; |
| 9087 | int rc; /* Return code */ |
| 9088 | |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 9089 | assert( iVersion==1 || iVersion==2 ); |
| 9090 | |
dan | b978002 | 2010-04-21 18:37:57 +0000 | [diff] [blame] | 9091 | /* If setting the version fields to 1, do not automatically open the |
| 9092 | ** WAL connection, even if the version fields are currently set to 2. |
| 9093 | */ |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 9094 | pBt->btsFlags &= ~BTS_NO_WAL; |
| 9095 | if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL; |
dan | b978002 | 2010-04-21 18:37:57 +0000 | [diff] [blame] | 9096 | |
| 9097 | rc = sqlite3BtreeBeginTrans(pBtree, 0); |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 9098 | if( rc==SQLITE_OK ){ |
| 9099 | u8 *aData = pBt->pPage1->aData; |
dan | b978002 | 2010-04-21 18:37:57 +0000 | [diff] [blame] | 9100 | if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){ |
dan | ede6eb8 | 2010-04-22 06:27:04 +0000 | [diff] [blame] | 9101 | rc = sqlite3BtreeBeginTrans(pBtree, 2); |
dan | b978002 | 2010-04-21 18:37:57 +0000 | [diff] [blame] | 9102 | if( rc==SQLITE_OK ){ |
| 9103 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 9104 | if( rc==SQLITE_OK ){ |
| 9105 | aData[18] = (u8)iVersion; |
| 9106 | aData[19] = (u8)iVersion; |
| 9107 | } |
| 9108 | } |
| 9109 | } |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 9110 | } |
| 9111 | |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 9112 | pBt->btsFlags &= ~BTS_NO_WAL; |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 9113 | return rc; |
| 9114 | } |
dan | 428c218 | 2012-08-06 18:50:11 +0000 | [diff] [blame] | 9115 | |
| 9116 | /* |
| 9117 | ** set the mask of hint flags for cursor pCsr. Currently the only valid |
| 9118 | ** values are 0 and BTREE_BULKLOAD. |
| 9119 | */ |
| 9120 | void sqlite3BtreeCursorHints(BtCursor *pCsr, unsigned int mask){ |
| 9121 | assert( mask==BTREE_BULKLOAD || mask==0 ); |
| 9122 | pCsr->hints = mask; |
| 9123 | } |
drh | 781597f | 2014-05-21 08:21:07 +0000 | [diff] [blame] | 9124 | |
| 9125 | /* |
| 9126 | ** Return true if the given Btree is read-only. |
| 9127 | */ |
| 9128 | int sqlite3BtreeIsReadonly(Btree *p){ |
| 9129 | return (p->pBt->btsFlags & BTS_READ_ONLY)!=0; |
| 9130 | } |
drh | def6889 | 2014-11-04 12:11:23 +0000 | [diff] [blame] | 9131 | |
| 9132 | /* |
| 9133 | ** Return the size of the header added to each page by this module. |
| 9134 | */ |
| 9135 | int sqlite3HeaderSizeBtree(void){ return sizeof(MemPage); } |