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){ |
| 779 | return pCur && pCur->eState!=CURSOR_VALID; |
| 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 | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 1142 | */ |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 1143 | static int defragmentPage(MemPage *pPage){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1144 | int i; /* Loop counter */ |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 1145 | int pc; /* Address of the i-th cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1146 | int hdr; /* Offset to the page header */ |
| 1147 | int size; /* Size of a cell */ |
| 1148 | int usableSize; /* Number of usable bytes on a page */ |
| 1149 | int cellOffset; /* Offset to the cell pointer array */ |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 1150 | int cbrk; /* Offset to the cell content area */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1151 | int nCell; /* Number of cells on the page */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1152 | unsigned char *data; /* The page data */ |
| 1153 | unsigned char *temp; /* Temp area for cell content */ |
drh | 1714662 | 2009-07-07 17:38:38 +0000 | [diff] [blame] | 1154 | int iCellFirst; /* First allowable cell index */ |
| 1155 | int iCellLast; /* Last possible cell index */ |
| 1156 | |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1157 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1158 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1159 | assert( pPage->pBt!=0 ); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1160 | assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1161 | assert( pPage->nOverflow==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1162 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 26b7994 | 2007-11-28 16:19:56 +0000 | [diff] [blame] | 1163 | temp = sqlite3PagerTempSpace(pPage->pBt->pPager); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1164 | data = pPage->aData; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1165 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1166 | cellOffset = pPage->cellOffset; |
| 1167 | nCell = pPage->nCell; |
| 1168 | assert( nCell==get2byte(&data[hdr+3]) ); |
| 1169 | usableSize = pPage->pBt->usableSize; |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 1170 | cbrk = get2byte(&data[hdr+5]); |
| 1171 | memcpy(&temp[cbrk], &data[cbrk], usableSize - cbrk); |
| 1172 | cbrk = usableSize; |
drh | 1714662 | 2009-07-07 17:38:38 +0000 | [diff] [blame] | 1173 | iCellFirst = cellOffset + 2*nCell; |
| 1174 | iCellLast = usableSize - 4; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1175 | for(i=0; i<nCell; i++){ |
| 1176 | u8 *pAddr; /* The i-th cell pointer */ |
| 1177 | pAddr = &data[cellOffset + i*2]; |
| 1178 | pc = get2byte(pAddr); |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1179 | testcase( pc==iCellFirst ); |
| 1180 | testcase( pc==iCellLast ); |
drh | 1714662 | 2009-07-07 17:38:38 +0000 | [diff] [blame] | 1181 | #if !defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK) |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 1182 | /* These conditions have already been verified in btreeInitPage() |
drh | 1714662 | 2009-07-07 17:38:38 +0000 | [diff] [blame] | 1183 | ** if SQLITE_ENABLE_OVERSIZE_CELL_CHECK is defined |
| 1184 | */ |
| 1185 | if( pc<iCellFirst || pc>iCellLast ){ |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 1186 | return SQLITE_CORRUPT_BKPT; |
| 1187 | } |
drh | 1714662 | 2009-07-07 17:38:38 +0000 | [diff] [blame] | 1188 | #endif |
| 1189 | assert( pc>=iCellFirst && pc<=iCellLast ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1190 | size = cellSizePtr(pPage, &temp[pc]); |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 1191 | cbrk -= size; |
drh | 1714662 | 2009-07-07 17:38:38 +0000 | [diff] [blame] | 1192 | #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK) |
| 1193 | if( cbrk<iCellFirst ){ |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 1194 | return SQLITE_CORRUPT_BKPT; |
| 1195 | } |
drh | 1714662 | 2009-07-07 17:38:38 +0000 | [diff] [blame] | 1196 | #else |
| 1197 | if( cbrk<iCellFirst || pc+size>usableSize ){ |
| 1198 | return SQLITE_CORRUPT_BKPT; |
| 1199 | } |
| 1200 | #endif |
drh | 7157e1d | 2009-07-09 13:25:32 +0000 | [diff] [blame] | 1201 | assert( cbrk+size<=usableSize && cbrk>=iCellFirst ); |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1202 | testcase( cbrk+size==usableSize ); |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1203 | testcase( pc+size==usableSize ); |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 1204 | memcpy(&data[cbrk], &temp[pc], size); |
| 1205 | put2byte(pAddr, cbrk); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1206 | } |
drh | 1714662 | 2009-07-07 17:38:38 +0000 | [diff] [blame] | 1207 | assert( cbrk>=iCellFirst ); |
drh | 281b21d | 2008-08-22 12:57:08 +0000 | [diff] [blame] | 1208 | put2byte(&data[hdr+5], cbrk); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1209 | data[hdr+1] = 0; |
| 1210 | data[hdr+2] = 0; |
| 1211 | data[hdr+7] = 0; |
drh | 1714662 | 2009-07-07 17:38:38 +0000 | [diff] [blame] | 1212 | memset(&data[iCellFirst], 0, cbrk-iCellFirst); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 1213 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 1714662 | 2009-07-07 17:38:38 +0000 | [diff] [blame] | 1214 | if( cbrk-iCellFirst!=pPage->nFree ){ |
danielk1977 | 360e634 | 2008-11-12 08:49:51 +0000 | [diff] [blame] | 1215 | return SQLITE_CORRUPT_BKPT; |
| 1216 | } |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 1217 | return SQLITE_OK; |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1220 | /* |
danielk1977 | 6011a75 | 2009-04-01 16:25:32 +0000 | [diff] [blame] | 1221 | ** Allocate nByte bytes of space from within the B-Tree page passed |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1222 | ** as the first argument. Write into *pIdx the index into pPage->aData[] |
| 1223 | ** of the first byte of allocated space. Return either SQLITE_OK or |
| 1224 | ** an error code (usually SQLITE_CORRUPT). |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1225 | ** |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1226 | ** The caller guarantees that there is sufficient space to make the |
| 1227 | ** allocation. This routine might need to defragment in order to bring |
| 1228 | ** all the space together, however. This routine will avoid using |
| 1229 | ** the first two bytes past the cell pointer area since presumably this |
| 1230 | ** allocation is being made in order to insert a new cell, so we will |
| 1231 | ** also end up needing a new cell pointer. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1232 | */ |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1233 | static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){ |
danielk1977 | 6011a75 | 2009-04-01 16:25:32 +0000 | [diff] [blame] | 1234 | const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */ |
| 1235 | u8 * const data = pPage->aData; /* Local cache of pPage->aData */ |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1236 | int top; /* First byte of cell content area */ |
| 1237 | int gap; /* First byte of gap between cell pointers and cell content */ |
| 1238 | int rc; /* Integer return code */ |
drh | 00ce394 | 2009-12-06 03:35:51 +0000 | [diff] [blame] | 1239 | int usableSize; /* Usable size of the page */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1240 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1241 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1242 | assert( pPage->pBt ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1243 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | fa67c3c | 2008-07-11 02:21:40 +0000 | [diff] [blame] | 1244 | assert( nByte>=0 ); /* Minimum cell size is 4 */ |
| 1245 | assert( pPage->nFree>=nByte ); |
| 1246 | assert( pPage->nOverflow==0 ); |
drh | 00ce394 | 2009-12-06 03:35:51 +0000 | [diff] [blame] | 1247 | usableSize = pPage->pBt->usableSize; |
| 1248 | assert( nByte < usableSize-8 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1249 | |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1250 | assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf ); |
| 1251 | gap = pPage->cellOffset + 2*pPage->nCell; |
drh | 75b31dc | 2014-08-20 00:54:46 +0000 | [diff] [blame] | 1252 | assert( gap<=65536 ); |
| 1253 | top = get2byte(&data[hdr+5]); |
| 1254 | if( gap>top ){ |
| 1255 | if( top==0 ){ |
| 1256 | top = 65536; |
| 1257 | }else{ |
| 1258 | return SQLITE_CORRUPT_BKPT; |
| 1259 | } |
| 1260 | } |
drh | 4c04f3c | 2014-08-20 11:56:14 +0000 | [diff] [blame] | 1261 | |
| 1262 | /* If there is enough space between gap and top for one more cell pointer |
| 1263 | ** array entry offset, and if the freelist is not empty, then search the |
| 1264 | ** freelist looking for a free slot big enough to satisfy the request. |
| 1265 | */ |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1266 | testcase( gap+2==top ); |
| 1267 | testcase( gap+1==top ); |
| 1268 | testcase( gap==top ); |
drh | 4c04f3c | 2014-08-20 11:56:14 +0000 | [diff] [blame] | 1269 | if( gap+2<=top && (data[hdr+1] || data[hdr+2]) ){ |
danielk1977 | 6011a75 | 2009-04-01 16:25:32 +0000 | [diff] [blame] | 1270 | int pc, addr; |
| 1271 | for(addr=hdr+1; (pc = get2byte(&data[addr]))>0; addr=pc){ |
drh | 00ce394 | 2009-12-06 03:35:51 +0000 | [diff] [blame] | 1272 | int size; /* Size of the free slot */ |
| 1273 | if( pc>usableSize-4 || pc<addr+4 ){ |
| 1274 | return SQLITE_CORRUPT_BKPT; |
| 1275 | } |
| 1276 | size = get2byte(&data[pc+2]); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1277 | if( size>=nByte ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1278 | int x = size - nByte; |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1279 | testcase( x==4 ); |
| 1280 | testcase( x==3 ); |
danielk1977 | 6011a75 | 2009-04-01 16:25:32 +0000 | [diff] [blame] | 1281 | if( x<4 ){ |
drh | 4c04f3c | 2014-08-20 11:56:14 +0000 | [diff] [blame] | 1282 | if( data[hdr+7]>=60 ) goto defragment_page; |
danielk1977 | fad9194 | 2009-04-29 17:49:59 +0000 | [diff] [blame] | 1283 | /* Remove the slot from the free-list. Update the number of |
| 1284 | ** fragmented bytes within the page. */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1285 | memcpy(&data[addr], &data[pc], 2); |
drh | 75b31dc | 2014-08-20 00:54:46 +0000 | [diff] [blame] | 1286 | data[hdr+7] += (u8)x; |
drh | 00ce394 | 2009-12-06 03:35:51 +0000 | [diff] [blame] | 1287 | }else if( size+pc > usableSize ){ |
| 1288 | return SQLITE_CORRUPT_BKPT; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1289 | }else{ |
danielk1977 | fad9194 | 2009-04-29 17:49:59 +0000 | [diff] [blame] | 1290 | /* The slot remains on the free-list. Reduce its size to account |
| 1291 | ** for the portion used by the new allocation. */ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1292 | put2byte(&data[pc+2], x); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1293 | } |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1294 | *pIdx = pc + x; |
| 1295 | return SQLITE_OK; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1296 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1297 | } |
| 1298 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1299 | |
drh | 4c04f3c | 2014-08-20 11:56:14 +0000 | [diff] [blame] | 1300 | /* The request could not be fulfilled using a freelist slot. Check |
| 1301 | ** to see if defragmentation is necessary. |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1302 | */ |
| 1303 | testcase( gap+2+nByte==top ); |
| 1304 | if( gap+2+nByte>top ){ |
drh | 4c04f3c | 2014-08-20 11:56:14 +0000 | [diff] [blame] | 1305 | defragment_page: |
drh | 9055526 | 2014-08-20 13:17:43 +0000 | [diff] [blame] | 1306 | testcase( pPage->nCell==0 ); |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1307 | rc = defragmentPage(pPage); |
| 1308 | if( rc ) return rc; |
drh | 5d433ce | 2010-08-14 16:02:52 +0000 | [diff] [blame] | 1309 | top = get2byteNotZero(&data[hdr+5]); |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1310 | assert( gap+nByte<=top ); |
| 1311 | } |
| 1312 | |
| 1313 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1314 | /* Allocate memory from the gap in between the cell pointer array |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 1315 | ** and the cell content area. The btreeInitPage() call has already |
| 1316 | ** validated the freelist. Given that the freelist is valid, there |
| 1317 | ** is no way that the allocation can extend off the end of the page. |
| 1318 | ** The assert() below verifies the previous sentence. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1319 | */ |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1320 | top -= nByte; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1321 | put2byte(&data[hdr+5], top); |
drh | fcd71b6 | 2011-04-05 22:08:24 +0000 | [diff] [blame] | 1322 | assert( top+nByte <= (int)pPage->pBt->usableSize ); |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1323 | *pIdx = top; |
| 1324 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1328 | ** Return a section of the pPage->aData to the freelist. |
drh | 7fb9164 | 2014-08-20 14:37:09 +0000 | [diff] [blame] | 1329 | ** The first byte of the new free block is pPage->aData[iStart] |
| 1330 | ** and the size of the block is iSize bytes. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1331 | ** |
drh | 5f5c753 | 2014-08-20 17:56:27 +0000 | [diff] [blame] | 1332 | ** Adjacent freeblocks are coalesced. |
| 1333 | ** |
| 1334 | ** Note that even though the freeblock list was checked by btreeInitPage(), |
| 1335 | ** that routine will not detect overlap between cells or freeblocks. Nor |
| 1336 | ** does it detect cells or freeblocks that encrouch into the reserved bytes |
| 1337 | ** at the end of the page. So do additional corruption checks inside this |
| 1338 | ** routine and return SQLITE_CORRUPT if any problems are found. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1339 | */ |
drh | 5f5c753 | 2014-08-20 17:56:27 +0000 | [diff] [blame] | 1340 | static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){ |
drh | 3f38740 | 2014-09-24 01:23:00 +0000 | [diff] [blame] | 1341 | u16 iPtr; /* Address of ptr to next freeblock */ |
drh | 5f5c753 | 2014-08-20 17:56:27 +0000 | [diff] [blame] | 1342 | u16 iFreeBlk; /* Address of the next freeblock */ |
| 1343 | u8 hdr; /* Page header size. 0 or 100 */ |
| 1344 | u8 nFrag = 0; /* Reduction in fragmentation */ |
| 1345 | u16 iOrigSize = iSize; /* Original value of iSize */ |
| 1346 | u32 iLast = pPage->pBt->usableSize-4; /* Largest possible freeblock offset */ |
| 1347 | u32 iEnd = iStart + iSize; /* First byte past the iStart buffer */ |
drh | 7fb9164 | 2014-08-20 14:37:09 +0000 | [diff] [blame] | 1348 | unsigned char *data = pPage->aData; /* Page content */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1349 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1350 | assert( pPage->pBt!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1351 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 7fb9164 | 2014-08-20 14:37:09 +0000 | [diff] [blame] | 1352 | assert( iStart>=pPage->hdrOffset+6+pPage->childPtrSize ); |
drh | 5f5c753 | 2014-08-20 17:56:27 +0000 | [diff] [blame] | 1353 | assert( iEnd <= pPage->pBt->usableSize ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1354 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 7fb9164 | 2014-08-20 14:37:09 +0000 | [diff] [blame] | 1355 | assert( iSize>=4 ); /* Minimum cell size is 4 */ |
drh | 5f5c753 | 2014-08-20 17:56:27 +0000 | [diff] [blame] | 1356 | assert( iStart<=iLast ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1357 | |
drh | 5f5c753 | 2014-08-20 17:56:27 +0000 | [diff] [blame] | 1358 | /* Overwrite deleted information with zeros when the secure_delete |
| 1359 | ** option is enabled */ |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 1360 | if( pPage->pBt->btsFlags & BTS_SECURE_DELETE ){ |
drh | 7fb9164 | 2014-08-20 14:37:09 +0000 | [diff] [blame] | 1361 | memset(&data[iStart], 0, iSize); |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 1362 | } |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 1363 | |
drh | 5f5c753 | 2014-08-20 17:56:27 +0000 | [diff] [blame] | 1364 | /* The list of freeblocks must be in ascending order. Find the |
| 1365 | ** spot on the list where iStart should be inserted. |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1366 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1367 | hdr = pPage->hdrOffset; |
drh | 7fb9164 | 2014-08-20 14:37:09 +0000 | [diff] [blame] | 1368 | iPtr = hdr + 1; |
drh | 7bc4c45 | 2014-08-20 18:43:44 +0000 | [diff] [blame] | 1369 | if( data[iPtr+1]==0 && data[iPtr]==0 ){ |
| 1370 | iFreeBlk = 0; /* Shortcut for the case when the freelist is empty */ |
| 1371 | }else{ |
| 1372 | while( (iFreeBlk = get2byte(&data[iPtr]))>0 && iFreeBlk<iStart ){ |
| 1373 | if( iFreeBlk<iPtr+4 ) return SQLITE_CORRUPT_BKPT; |
| 1374 | iPtr = iFreeBlk; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1375 | } |
drh | 7bc4c45 | 2014-08-20 18:43:44 +0000 | [diff] [blame] | 1376 | if( iFreeBlk>iLast ) return SQLITE_CORRUPT_BKPT; |
| 1377 | assert( iFreeBlk>iPtr || iFreeBlk==0 ); |
| 1378 | |
| 1379 | /* At this point: |
| 1380 | ** iFreeBlk: First freeblock after iStart, or zero if none |
| 1381 | ** iPtr: The address of a pointer iFreeBlk |
| 1382 | ** |
| 1383 | ** Check to see if iFreeBlk should be coalesced onto the end of iStart. |
| 1384 | */ |
| 1385 | if( iFreeBlk && iEnd+3>=iFreeBlk ){ |
| 1386 | nFrag = iFreeBlk - iEnd; |
| 1387 | if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_BKPT; |
| 1388 | iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]); |
| 1389 | iSize = iEnd - iStart; |
| 1390 | iFreeBlk = get2byte(&data[iFreeBlk]); |
| 1391 | } |
| 1392 | |
drh | 3f38740 | 2014-09-24 01:23:00 +0000 | [diff] [blame] | 1393 | /* If iPtr is another freeblock (that is, if iPtr is not the freelist |
| 1394 | ** pointer in the page header) then check to see if iStart should be |
| 1395 | ** coalesced onto the end of iPtr. |
drh | 7bc4c45 | 2014-08-20 18:43:44 +0000 | [diff] [blame] | 1396 | */ |
| 1397 | if( iPtr>hdr+1 ){ |
| 1398 | int iPtrEnd = iPtr + get2byte(&data[iPtr+2]); |
| 1399 | if( iPtrEnd+3>=iStart ){ |
| 1400 | if( iPtrEnd>iStart ) return SQLITE_CORRUPT_BKPT; |
| 1401 | nFrag += iStart - iPtrEnd; |
| 1402 | iSize = iEnd - iPtr; |
| 1403 | iStart = iPtr; |
| 1404 | } |
| 1405 | } |
| 1406 | if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_BKPT; |
| 1407 | data[hdr+7] -= nFrag; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1408 | } |
drh | 7bc4c45 | 2014-08-20 18:43:44 +0000 | [diff] [blame] | 1409 | if( iStart==get2byte(&data[hdr+5]) ){ |
drh | 5f5c753 | 2014-08-20 17:56:27 +0000 | [diff] [blame] | 1410 | /* The new freeblock is at the beginning of the cell content area, |
| 1411 | ** so just extend the cell content area rather than create another |
| 1412 | ** freelist entry */ |
drh | 7bc4c45 | 2014-08-20 18:43:44 +0000 | [diff] [blame] | 1413 | if( iPtr!=hdr+1 ) return SQLITE_CORRUPT_BKPT; |
drh | 5f5c753 | 2014-08-20 17:56:27 +0000 | [diff] [blame] | 1414 | put2byte(&data[hdr+1], iFreeBlk); |
| 1415 | put2byte(&data[hdr+5], iEnd); |
| 1416 | }else{ |
| 1417 | /* Insert the new freeblock into the freelist */ |
| 1418 | put2byte(&data[iPtr], iStart); |
| 1419 | put2byte(&data[iStart], iFreeBlk); |
| 1420 | put2byte(&data[iStart+2], iSize); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1421 | } |
drh | 5f5c753 | 2014-08-20 17:56:27 +0000 | [diff] [blame] | 1422 | pPage->nFree += iOrigSize; |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 1423 | return SQLITE_OK; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1424 | } |
| 1425 | |
| 1426 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1427 | ** Decode the flags byte (the first byte of the header) for a page |
| 1428 | ** and initialize fields of the MemPage structure accordingly. |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1429 | ** |
| 1430 | ** Only the following combinations are supported. Anything different |
| 1431 | ** indicates a corrupt database files: |
| 1432 | ** |
| 1433 | ** PTF_ZERODATA |
| 1434 | ** PTF_ZERODATA | PTF_LEAF |
| 1435 | ** PTF_LEAFDATA | PTF_INTKEY |
| 1436 | ** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1437 | */ |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1438 | static int decodeFlags(MemPage *pPage, int flagByte){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1439 | BtShared *pBt; /* A copy of pPage->pBt */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1440 | |
| 1441 | assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1442 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1443 | pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 ); |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1444 | flagByte &= ~PTF_LEAF; |
| 1445 | pPage->childPtrSize = 4-4*pPage->leaf; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1446 | pBt = pPage->pBt; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1447 | if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){ |
| 1448 | pPage->intKey = 1; |
drh | 3e28ff5 | 2014-09-24 00:59:08 +0000 | [diff] [blame] | 1449 | pPage->intKeyLeaf = pPage->leaf; |
| 1450 | pPage->noPayload = !pPage->leaf; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1451 | pPage->maxLocal = pBt->maxLeaf; |
| 1452 | pPage->minLocal = pBt->minLeaf; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1453 | }else if( flagByte==PTF_ZERODATA ){ |
| 1454 | pPage->intKey = 0; |
drh | 3e28ff5 | 2014-09-24 00:59:08 +0000 | [diff] [blame] | 1455 | pPage->intKeyLeaf = 0; |
| 1456 | pPage->noPayload = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1457 | pPage->maxLocal = pBt->maxLocal; |
| 1458 | pPage->minLocal = pBt->minLocal; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1459 | }else{ |
| 1460 | return SQLITE_CORRUPT_BKPT; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1461 | } |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 1462 | pPage->max1bytePayload = pBt->max1bytePayload; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 1463 | return SQLITE_OK; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1467 | ** Initialize the auxiliary information for a disk block. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1468 | ** |
| 1469 | ** Return SQLITE_OK on success. If we see that the page does |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1470 | ** not contain a well-formed database page, then return |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1471 | ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not |
| 1472 | ** guarantee that the page is well-formed. It only shows that |
| 1473 | ** we failed to detect any corruption. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1474 | */ |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 1475 | static int btreeInitPage(MemPage *pPage){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1476 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1477 | assert( pPage->pBt!=0 ); |
| 1478 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1479 | assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1480 | assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) ); |
| 1481 | assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1482 | |
| 1483 | if( !pPage->isInit ){ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1484 | u16 pc; /* Address of a freeblock within pPage->aData[] */ |
| 1485 | u8 hdr; /* Offset to beginning of page header */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1486 | u8 *data; /* Equal to pPage->aData */ |
| 1487 | BtShared *pBt; /* The main btree structure */ |
drh | b2eced5 | 2010-08-12 02:41:12 +0000 | [diff] [blame] | 1488 | int usableSize; /* Amount of usable space on each page */ |
shaneh | 1df2db7 | 2010-08-18 02:28:48 +0000 | [diff] [blame] | 1489 | u16 cellOffset; /* Offset from start of page to first cell pointer */ |
drh | b2eced5 | 2010-08-12 02:41:12 +0000 | [diff] [blame] | 1490 | int nFree; /* Number of unused bytes on the page */ |
| 1491 | int top; /* First byte of the cell content area */ |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1492 | int iCellFirst; /* First allowable cell or freeblock offset */ |
| 1493 | int iCellLast; /* Last possible cell or freeblock offset */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1494 | |
| 1495 | pBt = pPage->pBt; |
| 1496 | |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1497 | hdr = pPage->hdrOffset; |
| 1498 | data = pPage->aData; |
| 1499 | if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT; |
drh | b2eced5 | 2010-08-12 02:41:12 +0000 | [diff] [blame] | 1500 | assert( pBt->pageSize>=512 && pBt->pageSize<=65536 ); |
| 1501 | pPage->maskPage = (u16)(pBt->pageSize - 1); |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1502 | pPage->nOverflow = 0; |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1503 | usableSize = pBt->usableSize; |
| 1504 | pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf; |
drh | 3def235 | 2011-11-11 00:27:15 +0000 | [diff] [blame] | 1505 | pPage->aDataEnd = &data[usableSize]; |
| 1506 | pPage->aCellIdx = &data[cellOffset]; |
drh | 5d433ce | 2010-08-14 16:02:52 +0000 | [diff] [blame] | 1507 | top = get2byteNotZero(&data[hdr+5]); |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1508 | pPage->nCell = get2byte(&data[hdr+3]); |
| 1509 | if( pPage->nCell>MX_CELL(pBt) ){ |
| 1510 | /* To many cells for a single page. The page must be corrupt */ |
| 1511 | return SQLITE_CORRUPT_BKPT; |
| 1512 | } |
drh | b908d76 | 2009-07-08 16:54:40 +0000 | [diff] [blame] | 1513 | testcase( pPage->nCell==MX_CELL(pBt) ); |
drh | 69e931e | 2009-06-03 21:04:35 +0000 | [diff] [blame] | 1514 | |
shane | 5eff7cf | 2009-08-10 03:57:58 +0000 | [diff] [blame] | 1515 | /* A malformed database page might cause us to read past the end |
drh | 69e931e | 2009-06-03 21:04:35 +0000 | [diff] [blame] | 1516 | ** of page when parsing a cell. |
| 1517 | ** |
| 1518 | ** The following block of code checks early to see if a cell extends |
| 1519 | ** past the end of a page boundary and causes SQLITE_CORRUPT to be |
| 1520 | ** returned if it does. |
| 1521 | */ |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1522 | iCellFirst = cellOffset + 2*pPage->nCell; |
| 1523 | iCellLast = usableSize - 4; |
drh | 3b2a3fa | 2009-06-09 13:42:24 +0000 | [diff] [blame] | 1524 | #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK) |
drh | 69e931e | 2009-06-03 21:04:35 +0000 | [diff] [blame] | 1525 | { |
drh | 69e931e | 2009-06-03 21:04:35 +0000 | [diff] [blame] | 1526 | int i; /* Index into the cell pointer array */ |
| 1527 | int sz; /* Size of a cell */ |
| 1528 | |
drh | 69e931e | 2009-06-03 21:04:35 +0000 | [diff] [blame] | 1529 | if( !pPage->leaf ) iCellLast--; |
| 1530 | for(i=0; i<pPage->nCell; i++){ |
| 1531 | pc = get2byte(&data[cellOffset+i*2]); |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1532 | testcase( pc==iCellFirst ); |
| 1533 | testcase( pc==iCellLast ); |
drh | 69e931e | 2009-06-03 21:04:35 +0000 | [diff] [blame] | 1534 | if( pc<iCellFirst || pc>iCellLast ){ |
| 1535 | return SQLITE_CORRUPT_BKPT; |
| 1536 | } |
| 1537 | sz = cellSizePtr(pPage, &data[pc]); |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1538 | testcase( pc+sz==usableSize ); |
drh | 69e931e | 2009-06-03 21:04:35 +0000 | [diff] [blame] | 1539 | if( pc+sz>usableSize ){ |
| 1540 | return SQLITE_CORRUPT_BKPT; |
| 1541 | } |
| 1542 | } |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1543 | if( !pPage->leaf ) iCellLast++; |
drh | 69e931e | 2009-06-03 21:04:35 +0000 | [diff] [blame] | 1544 | } |
| 1545 | #endif |
| 1546 | |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1547 | /* Compute the total free space on the page */ |
| 1548 | pc = get2byte(&data[hdr+1]); |
danielk1977 | 93c829c | 2009-06-03 17:26:17 +0000 | [diff] [blame] | 1549 | nFree = data[hdr+7] + top; |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1550 | while( pc>0 ){ |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 1551 | u16 next, size; |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 1552 | if( pc<iCellFirst || pc>iCellLast ){ |
dan | 4361e79 | 2009-08-14 17:01:22 +0000 | [diff] [blame] | 1553 | /* Start of free block is off the page */ |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1554 | return SQLITE_CORRUPT_BKPT; |
| 1555 | } |
| 1556 | next = get2byte(&data[pc]); |
| 1557 | size = get2byte(&data[pc+2]); |
dan | 4361e79 | 2009-08-14 17:01:22 +0000 | [diff] [blame] | 1558 | if( (next>0 && next<=pc+size+3) || pc+size>usableSize ){ |
| 1559 | /* Free blocks must be in ascending order. And the last byte of |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 1560 | ** the free-block must lie on the database page. */ |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1561 | return SQLITE_CORRUPT_BKPT; |
| 1562 | } |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 1563 | nFree = nFree + size; |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1564 | pc = next; |
| 1565 | } |
danielk1977 | 93c829c | 2009-06-03 17:26:17 +0000 | [diff] [blame] | 1566 | |
| 1567 | /* At this point, nFree contains the sum of the offset to the start |
| 1568 | ** of the cell-content area plus the number of free bytes within |
| 1569 | ** the cell-content area. If this is greater than the usable-size |
| 1570 | ** of the page, then the page must be corrupted. This check also |
| 1571 | ** serves to verify that the offset to the start of the cell-content |
| 1572 | ** area, according to the page header, lies within the page. |
| 1573 | */ |
| 1574 | if( nFree>usableSize ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1575 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1576 | } |
shane | 5eff7cf | 2009-08-10 03:57:58 +0000 | [diff] [blame] | 1577 | pPage->nFree = (u16)(nFree - iCellFirst); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1578 | pPage->isInit = 1; |
| 1579 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1580 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
| 1583 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1584 | ** Set up a raw page so that it looks like a database page holding |
| 1585 | ** no entries. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1586 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1587 | static void zeroPage(MemPage *pPage, int flags){ |
| 1588 | unsigned char *data = pPage->aData; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1589 | BtShared *pBt = pPage->pBt; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 1590 | u8 hdr = pPage->hdrOffset; |
| 1591 | u16 first; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1592 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1593 | assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1594 | assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); |
| 1595 | assert( sqlite3PagerGetData(pPage->pDbPage) == data ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1596 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1597 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 1598 | if( pBt->btsFlags & BTS_SECURE_DELETE ){ |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 1599 | memset(&data[hdr], 0, pBt->usableSize - hdr); |
| 1600 | } |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 1601 | data[hdr] = (char)flags; |
drh | fe48599 | 2014-02-12 23:52:16 +0000 | [diff] [blame] | 1602 | first = hdr + ((flags&PTF_LEAF)==0 ? 12 : 8); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1603 | memset(&data[hdr+1], 0, 4); |
| 1604 | data[hdr+7] = 0; |
| 1605 | put2byte(&data[hdr+5], pBt->usableSize); |
shaneh | 1df2db7 | 2010-08-18 02:28:48 +0000 | [diff] [blame] | 1606 | pPage->nFree = (u16)(pBt->usableSize - first); |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1607 | decodeFlags(pPage, flags); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1608 | pPage->cellOffset = first; |
drh | 3def235 | 2011-11-11 00:27:15 +0000 | [diff] [blame] | 1609 | pPage->aDataEnd = &data[pBt->usableSize]; |
| 1610 | pPage->aCellIdx = &data[first]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1611 | pPage->nOverflow = 0; |
drh | b2eced5 | 2010-08-12 02:41:12 +0000 | [diff] [blame] | 1612 | assert( pBt->pageSize>=512 && pBt->pageSize<=65536 ); |
| 1613 | pPage->maskPage = (u16)(pBt->pageSize - 1); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1614 | pPage->nCell = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1615 | pPage->isInit = 1; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1618 | |
| 1619 | /* |
| 1620 | ** Convert a DbPage obtained from the pager into a MemPage used by |
| 1621 | ** the btree layer. |
| 1622 | */ |
| 1623 | static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){ |
| 1624 | MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage); |
| 1625 | pPage->aData = sqlite3PagerGetData(pDbPage); |
| 1626 | pPage->pDbPage = pDbPage; |
| 1627 | pPage->pBt = pBt; |
| 1628 | pPage->pgno = pgno; |
| 1629 | pPage->hdrOffset = pPage->pgno==1 ? 100 : 0; |
| 1630 | return pPage; |
| 1631 | } |
| 1632 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1633 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1634 | ** Get a page from the pager. Initialize the MemPage.pBt and |
| 1635 | ** MemPage.aData elements if needed. |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 1636 | ** |
| 1637 | ** If the noContent flag is set, it means that we do not care about |
| 1638 | ** the content of the page at this time. So do not go to the disk |
| 1639 | ** to fetch the content. Just fill in the content with zeros for now. |
| 1640 | ** If in the future we call sqlite3PagerWrite() on this page, that |
| 1641 | ** means we have started to be concerned about content and the disk |
| 1642 | ** read should occur at that point. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1643 | */ |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 1644 | static int btreeGetPage( |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1645 | BtShared *pBt, /* The btree */ |
| 1646 | Pgno pgno, /* Number of the page to fetch */ |
| 1647 | MemPage **ppPage, /* Return the page in this parameter */ |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 1648 | int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1649 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1650 | int rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1651 | DbPage *pDbPage; |
| 1652 | |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 1653 | assert( flags==0 || flags==PAGER_GET_NOCONTENT || flags==PAGER_GET_READONLY ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1654 | assert( sqlite3_mutex_held(pBt->mutex) ); |
dan | 11dcd11 | 2013-03-15 18:29:18 +0000 | [diff] [blame] | 1655 | rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, flags); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1656 | if( rc ) return rc; |
drh | 897a820 | 2008-09-18 01:08:15 +0000 | [diff] [blame] | 1657 | *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1658 | return SQLITE_OK; |
| 1659 | } |
| 1660 | |
| 1661 | /* |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 1662 | ** Retrieve a page from the pager cache. If the requested page is not |
| 1663 | ** already in the pager cache return NULL. Initialize the MemPage.pBt and |
| 1664 | ** MemPage.aData elements if needed. |
| 1665 | */ |
| 1666 | static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){ |
| 1667 | DbPage *pDbPage; |
| 1668 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 1669 | pDbPage = sqlite3PagerLookup(pBt->pPager, pgno); |
| 1670 | if( pDbPage ){ |
| 1671 | return btreePageFromDbPage(pDbPage, pgno, pBt); |
| 1672 | } |
| 1673 | return 0; |
| 1674 | } |
| 1675 | |
| 1676 | /* |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 1677 | ** Return the size of the database file in pages. If there is any kind of |
| 1678 | ** error, return ((unsigned int)-1). |
danielk1977 | 67fd7a9 | 2008-09-10 17:53:35 +0000 | [diff] [blame] | 1679 | */ |
drh | b129915 | 2010-03-30 22:58:33 +0000 | [diff] [blame] | 1680 | static Pgno btreePagecount(BtShared *pBt){ |
| 1681 | return pBt->nPage; |
| 1682 | } |
| 1683 | u32 sqlite3BtreeLastPage(Btree *p){ |
| 1684 | assert( sqlite3BtreeHoldsMutex(p) ); |
| 1685 | assert( ((p->pBt->nPage)&0x8000000)==0 ); |
drh | eac5bd7 | 2014-07-25 21:35:39 +0000 | [diff] [blame] | 1686 | return btreePagecount(p->pBt); |
danielk1977 | 67fd7a9 | 2008-09-10 17:53:35 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | /* |
danielk1977 | 89bc4bc | 2009-07-21 19:25:24 +0000 | [diff] [blame] | 1690 | ** Get a page from the pager and initialize it. This routine is just a |
| 1691 | ** convenience wrapper around separate calls to btreeGetPage() and |
| 1692 | ** btreeInitPage(). |
| 1693 | ** |
| 1694 | ** If an error occurs, then the value *ppPage is set to is undefined. It |
| 1695 | ** may remain unchanged, or it may be set to an invalid value. |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1696 | */ |
| 1697 | static int getAndInitPage( |
dan | 11dcd11 | 2013-03-15 18:29:18 +0000 | [diff] [blame] | 1698 | BtShared *pBt, /* The database file */ |
| 1699 | Pgno pgno, /* Number of the page to get */ |
| 1700 | MemPage **ppPage, /* Write the page pointer here */ |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 1701 | int bReadonly /* PAGER_GET_READONLY or 0 */ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1702 | ){ |
| 1703 | int rc; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1704 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 1705 | assert( bReadonly==PAGER_GET_READONLY || bReadonly==0 ); |
danielk1977 | 89bc4bc | 2009-07-21 19:25:24 +0000 | [diff] [blame] | 1706 | |
dan | ba3cbf3 | 2010-06-30 04:29:03 +0000 | [diff] [blame] | 1707 | if( pgno>btreePagecount(pBt) ){ |
| 1708 | rc = SQLITE_CORRUPT_BKPT; |
| 1709 | }else{ |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 1710 | rc = btreeGetPage(pBt, pgno, ppPage, bReadonly); |
drh | 29f2bad | 2013-12-09 01:04:54 +0000 | [diff] [blame] | 1711 | if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){ |
dan | ba3cbf3 | 2010-06-30 04:29:03 +0000 | [diff] [blame] | 1712 | rc = btreeInitPage(*ppPage); |
| 1713 | if( rc!=SQLITE_OK ){ |
| 1714 | releasePage(*ppPage); |
| 1715 | } |
danielk1977 | 89bc4bc | 2009-07-21 19:25:24 +0000 | [diff] [blame] | 1716 | } |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1717 | } |
dan | ba3cbf3 | 2010-06-30 04:29:03 +0000 | [diff] [blame] | 1718 | |
| 1719 | testcase( pgno==0 ); |
| 1720 | assert( pgno!=0 || rc==SQLITE_CORRUPT ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1721 | return rc; |
| 1722 | } |
| 1723 | |
| 1724 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1725 | ** Release a MemPage. This should be called once for each prior |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 1726 | ** call to btreeGetPage. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1727 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1728 | static void releasePage(MemPage *pPage){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1729 | if( pPage ){ |
| 1730 | assert( pPage->aData ); |
| 1731 | assert( pPage->pBt ); |
drh | da8a330 | 2013-12-13 19:35:21 +0000 | [diff] [blame] | 1732 | assert( pPage->pDbPage!=0 ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1733 | assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); |
| 1734 | assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1735 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | da8a330 | 2013-12-13 19:35:21 +0000 | [diff] [blame] | 1736 | sqlite3PagerUnrefNotNull(pPage->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1737 | } |
| 1738 | } |
| 1739 | |
| 1740 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1741 | ** During a rollback, when the pager reloads information into the cache |
| 1742 | ** so that the cache is restored to its original state at the start of |
| 1743 | ** the transaction, for each page restored this routine is called. |
| 1744 | ** |
| 1745 | ** This routine needs to reset the extra data section at the end of the |
| 1746 | ** page to agree with the restored data. |
| 1747 | */ |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 1748 | static void pageReinit(DbPage *pData){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1749 | MemPage *pPage; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1750 | pPage = (MemPage *)sqlite3PagerGetExtra(pData); |
danielk1977 | d217e6f | 2009-04-01 17:13:51 +0000 | [diff] [blame] | 1751 | assert( sqlite3PagerPageRefcount(pData)>0 ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1752 | if( pPage->isInit ){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1753 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1754 | pPage->isInit = 0; |
danielk1977 | d217e6f | 2009-04-01 17:13:51 +0000 | [diff] [blame] | 1755 | if( sqlite3PagerPageRefcount(pData)>1 ){ |
drh | 5e8d887 | 2009-03-30 17:19:48 +0000 | [diff] [blame] | 1756 | /* pPage might not be a btree page; it might be an overflow page |
| 1757 | ** or ptrmap page or a free page. In those cases, the following |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 1758 | ** call to btreeInitPage() will likely return SQLITE_CORRUPT. |
drh | 5e8d887 | 2009-03-30 17:19:48 +0000 | [diff] [blame] | 1759 | ** But no harm is done by this. And it is very important that |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 1760 | ** btreeInitPage() be called on every btree page so we make |
drh | 5e8d887 | 2009-03-30 17:19:48 +0000 | [diff] [blame] | 1761 | ** the call for every page that comes in for re-initing. */ |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 1762 | btreeInitPage(pPage); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1763 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1764 | } |
| 1765 | } |
| 1766 | |
| 1767 | /* |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1768 | ** Invoke the busy handler for a btree. |
| 1769 | */ |
danielk1977 | 1ceedd3 | 2008-11-19 10:22:33 +0000 | [diff] [blame] | 1770 | static int btreeInvokeBusyHandler(void *pArg){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1771 | BtShared *pBt = (BtShared*)pArg; |
| 1772 | assert( pBt->db ); |
| 1773 | assert( sqlite3_mutex_held(pBt->db->mutex) ); |
| 1774 | return sqlite3InvokeBusyHandler(&pBt->db->busyHandler); |
| 1775 | } |
| 1776 | |
| 1777 | /* |
drh | ad3e010 | 2004-09-03 23:32:18 +0000 | [diff] [blame] | 1778 | ** Open a database file. |
| 1779 | ** |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1780 | ** zFilename is the name of the database file. If zFilename is NULL |
drh | 75c014c | 2010-08-30 15:02:28 +0000 | [diff] [blame] | 1781 | ** then an ephemeral database is created. The ephemeral database might |
| 1782 | ** be exclusively in memory, or it might use a disk-based memory cache. |
| 1783 | ** Either way, the ephemeral database will be automatically deleted |
| 1784 | ** when sqlite3BtreeClose() is called. |
| 1785 | ** |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1786 | ** If zFilename is ":memory:" then an in-memory database is created |
| 1787 | ** that is automatically destroyed when it is closed. |
drh | c47fd8e | 2009-04-30 13:30:32 +0000 | [diff] [blame] | 1788 | ** |
drh | 33f111d | 2012-01-17 15:29:14 +0000 | [diff] [blame] | 1789 | ** The "flags" parameter is a bitmask that might contain bits like |
| 1790 | ** BTREE_OMIT_JOURNAL and/or BTREE_MEMORY. |
drh | 75c014c | 2010-08-30 15:02:28 +0000 | [diff] [blame] | 1791 | ** |
drh | c47fd8e | 2009-04-30 13:30:32 +0000 | [diff] [blame] | 1792 | ** If the database is already opened in the same database connection |
| 1793 | ** and we are in shared cache mode, then the open will fail with an |
| 1794 | ** SQLITE_CONSTRAINT error. We cannot allow two or more BtShared |
| 1795 | ** objects in the same database connection since doing so will lead |
| 1796 | ** to problems with locking. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1797 | */ |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1798 | int sqlite3BtreeOpen( |
dan | 3a6d8ae | 2011-04-23 15:54:54 +0000 | [diff] [blame] | 1799 | sqlite3_vfs *pVfs, /* VFS to use for this b-tree */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1800 | const char *zFilename, /* Name of the file containing the BTree database */ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1801 | sqlite3 *db, /* Associated database handle */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1802 | Btree **ppBtree, /* Pointer to new Btree object written here */ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1803 | int flags, /* Options */ |
| 1804 | int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1805 | ){ |
drh | 7555d8e | 2009-03-20 13:15:30 +0000 | [diff] [blame] | 1806 | BtShared *pBt = 0; /* Shared part of btree structure */ |
| 1807 | Btree *p; /* Handle to return */ |
| 1808 | sqlite3_mutex *mutexOpen = 0; /* Prevents a race condition. Ticket #3537 */ |
| 1809 | int rc = SQLITE_OK; /* Result code from this function */ |
| 1810 | u8 nReserve; /* Byte of unused space on each page */ |
| 1811 | unsigned char zDbHeader[100]; /* Database header content */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1812 | |
drh | 75c014c | 2010-08-30 15:02:28 +0000 | [diff] [blame] | 1813 | /* True if opening an ephemeral, temporary database */ |
| 1814 | const int isTempDb = zFilename==0 || zFilename[0]==0; |
| 1815 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1816 | /* Set the variable isMemdb to true for an in-memory database, or |
drh | b0a7c9c | 2010-12-06 21:09:59 +0000 | [diff] [blame] | 1817 | ** false for a file-based database. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1818 | */ |
drh | b0a7c9c | 2010-12-06 21:09:59 +0000 | [diff] [blame] | 1819 | #ifdef SQLITE_OMIT_MEMORYDB |
| 1820 | const int isMemdb = 0; |
| 1821 | #else |
| 1822 | const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0) |
drh | 9c67b2a | 2012-05-28 13:58:00 +0000 | [diff] [blame] | 1823 | || (isTempDb && sqlite3TempInMemory(db)) |
| 1824 | || (vfsFlags & SQLITE_OPEN_MEMORY)!=0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1825 | #endif |
| 1826 | |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1827 | assert( db!=0 ); |
dan | 3a6d8ae | 2011-04-23 15:54:54 +0000 | [diff] [blame] | 1828 | assert( pVfs!=0 ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1829 | assert( sqlite3_mutex_held(db->mutex) ); |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 1830 | assert( (flags&0xff)==flags ); /* flags fit in 8 bits */ |
| 1831 | |
| 1832 | /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */ |
| 1833 | assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 ); |
| 1834 | |
| 1835 | /* A BTREE_SINGLE database is always a temporary and/or ephemeral */ |
| 1836 | assert( (flags & BTREE_SINGLE)==0 || isTempDb ); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1837 | |
drh | 75c014c | 2010-08-30 15:02:28 +0000 | [diff] [blame] | 1838 | if( isMemdb ){ |
| 1839 | flags |= BTREE_MEMORY; |
| 1840 | } |
| 1841 | if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){ |
| 1842 | vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB; |
| 1843 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1844 | p = sqlite3MallocZero(sizeof(Btree)); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1845 | if( !p ){ |
| 1846 | return SQLITE_NOMEM; |
| 1847 | } |
| 1848 | p->inTrans = TRANS_NONE; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1849 | p->db = db; |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 1850 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 1851 | p->lock.pBtree = p; |
| 1852 | p->lock.iTable = 1; |
| 1853 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1854 | |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 1855 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1856 | /* |
| 1857 | ** If this Btree is a candidate for shared cache, try to find an |
| 1858 | ** existing BtShared object that we can share with |
| 1859 | */ |
drh | 4ab9d25 | 2012-05-26 20:08:49 +0000 | [diff] [blame] | 1860 | if( isTempDb==0 && (isMemdb==0 || (vfsFlags&SQLITE_OPEN_URI)!=0) ){ |
drh | f1f1268 | 2009-09-09 14:17:52 +0000 | [diff] [blame] | 1861 | if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 1862 | int nFullPathname = pVfs->mxPathname+1; |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 1863 | char *zFullPathname = sqlite3Malloc(nFullPathname); |
drh | 30ddce6 | 2011-10-15 00:16:30 +0000 | [diff] [blame] | 1864 | MUTEX_LOGIC( sqlite3_mutex *mutexShared; ) |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1865 | p->sharable = 1; |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1866 | if( !zFullPathname ){ |
| 1867 | sqlite3_free(p); |
| 1868 | return SQLITE_NOMEM; |
| 1869 | } |
drh | afc8b7f | 2012-05-26 18:06:38 +0000 | [diff] [blame] | 1870 | if( isMemdb ){ |
| 1871 | memcpy(zFullPathname, zFilename, sqlite3Strlen30(zFilename)+1); |
| 1872 | }else{ |
| 1873 | rc = sqlite3OsFullPathname(pVfs, zFilename, |
| 1874 | nFullPathname, zFullPathname); |
| 1875 | if( rc ){ |
| 1876 | sqlite3_free(zFullPathname); |
| 1877 | sqlite3_free(p); |
| 1878 | return rc; |
| 1879 | } |
drh | 070ad6b | 2011-11-17 11:43:19 +0000 | [diff] [blame] | 1880 | } |
drh | 30ddce6 | 2011-10-15 00:16:30 +0000 | [diff] [blame] | 1881 | #if SQLITE_THREADSAFE |
drh | 7555d8e | 2009-03-20 13:15:30 +0000 | [diff] [blame] | 1882 | mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN); |
| 1883 | sqlite3_mutex_enter(mutexOpen); |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 1884 | mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1885 | sqlite3_mutex_enter(mutexShared); |
drh | 30ddce6 | 2011-10-15 00:16:30 +0000 | [diff] [blame] | 1886 | #endif |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 1887 | for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1888 | assert( pBt->nRef>0 ); |
drh | d4e0bb0 | 2012-05-27 01:19:04 +0000 | [diff] [blame] | 1889 | if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0)) |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1890 | && sqlite3PagerVfs(pBt->pPager)==pVfs ){ |
drh | c47fd8e | 2009-04-30 13:30:32 +0000 | [diff] [blame] | 1891 | int iDb; |
| 1892 | for(iDb=db->nDb-1; iDb>=0; iDb--){ |
| 1893 | Btree *pExisting = db->aDb[iDb].pBt; |
| 1894 | if( pExisting && pExisting->pBt==pBt ){ |
| 1895 | sqlite3_mutex_leave(mutexShared); |
| 1896 | sqlite3_mutex_leave(mutexOpen); |
| 1897 | sqlite3_free(zFullPathname); |
| 1898 | sqlite3_free(p); |
| 1899 | return SQLITE_CONSTRAINT; |
| 1900 | } |
| 1901 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1902 | p->pBt = pBt; |
| 1903 | pBt->nRef++; |
| 1904 | break; |
| 1905 | } |
| 1906 | } |
| 1907 | sqlite3_mutex_leave(mutexShared); |
| 1908 | sqlite3_free(zFullPathname); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1909 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1910 | #ifdef SQLITE_DEBUG |
| 1911 | else{ |
| 1912 | /* In debug mode, we mark all persistent databases as sharable |
| 1913 | ** even when they are not. This exercises the locking code and |
| 1914 | ** gives more opportunity for asserts(sqlite3_mutex_held()) |
| 1915 | ** statements to find locking problems. |
| 1916 | */ |
| 1917 | p->sharable = 1; |
| 1918 | } |
| 1919 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1920 | } |
| 1921 | #endif |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1922 | if( pBt==0 ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1923 | /* |
| 1924 | ** The following asserts make sure that structures used by the btree are |
| 1925 | ** the right size. This is to guard against size changes that result |
| 1926 | ** when compiling on a different architecture. |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 1927 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1928 | assert( sizeof(i64)==8 || sizeof(i64)==4 ); |
| 1929 | assert( sizeof(u64)==8 || sizeof(u64)==4 ); |
| 1930 | assert( sizeof(u32)==4 ); |
| 1931 | assert( sizeof(u16)==2 ); |
| 1932 | assert( sizeof(Pgno)==4 ); |
| 1933 | |
| 1934 | pBt = sqlite3MallocZero( sizeof(*pBt) ); |
| 1935 | if( pBt==0 ){ |
| 1936 | rc = SQLITE_NOMEM; |
| 1937 | goto btree_open_out; |
| 1938 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 1939 | rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename, |
drh | 4775ecd | 2009-07-24 19:01:19 +0000 | [diff] [blame] | 1940 | EXTRA_SIZE, flags, vfsFlags, pageReinit); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1941 | if( rc==SQLITE_OK ){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 1942 | sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1943 | rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader); |
| 1944 | } |
| 1945 | if( rc!=SQLITE_OK ){ |
| 1946 | goto btree_open_out; |
| 1947 | } |
shaneh | bd2aaf9 | 2010-09-01 02:38:21 +0000 | [diff] [blame] | 1948 | pBt->openFlags = (u8)flags; |
danielk1977 | 2a50ff0 | 2009-04-10 09:47:06 +0000 | [diff] [blame] | 1949 | pBt->db = db; |
danielk1977 | 1ceedd3 | 2008-11-19 10:22:33 +0000 | [diff] [blame] | 1950 | sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1951 | p->pBt = pBt; |
| 1952 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1953 | pBt->pCursor = 0; |
| 1954 | pBt->pPage1 = 0; |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 1955 | if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY; |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 1956 | #ifdef SQLITE_SECURE_DELETE |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 1957 | pBt->btsFlags |= BTS_SECURE_DELETE; |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 1958 | #endif |
drh | b2eced5 | 2010-08-12 02:41:12 +0000 | [diff] [blame] | 1959 | pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1960 | if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE |
| 1961 | || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1962 | pBt->pageSize = 0; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1963 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1964 | /* If the magic name ":memory:" will create an in-memory database, then |
| 1965 | ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if |
| 1966 | ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if |
| 1967 | ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a |
| 1968 | ** regular file-name. In this case the auto-vacuum applies as per normal. |
| 1969 | */ |
| 1970 | if( zFilename && !isMemdb ){ |
| 1971 | pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0); |
| 1972 | pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0); |
| 1973 | } |
| 1974 | #endif |
| 1975 | nReserve = 0; |
| 1976 | }else{ |
| 1977 | nReserve = zDbHeader[20]; |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 1978 | pBt->btsFlags |= BTS_PAGESIZE_FIXED; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1979 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1980 | pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0); |
| 1981 | pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0); |
| 1982 | #endif |
| 1983 | } |
drh | fa9601a | 2009-06-18 17:22:39 +0000 | [diff] [blame] | 1984 | rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve); |
drh | c0b6181 | 2009-04-30 01:22:41 +0000 | [diff] [blame] | 1985 | if( rc ) goto btree_open_out; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1986 | pBt->usableSize = pBt->pageSize - nReserve; |
| 1987 | assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1988 | |
| 1989 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
| 1990 | /* Add the new BtShared object to the linked list sharable BtShareds. |
| 1991 | */ |
| 1992 | if( p->sharable ){ |
drh | 30ddce6 | 2011-10-15 00:16:30 +0000 | [diff] [blame] | 1993 | MUTEX_LOGIC( sqlite3_mutex *mutexShared; ) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1994 | pBt->nRef = 1; |
drh | 30ddce6 | 2011-10-15 00:16:30 +0000 | [diff] [blame] | 1995 | MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);) |
danielk1977 | 075c23a | 2008-09-01 18:34:20 +0000 | [diff] [blame] | 1996 | if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){ |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 1997 | pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST); |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 1998 | if( pBt->mutex==0 ){ |
| 1999 | rc = SQLITE_NOMEM; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2000 | db->mallocFailed = 0; |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 2001 | goto btree_open_out; |
| 2002 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 2003 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2004 | sqlite3_mutex_enter(mutexShared); |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 2005 | pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList); |
| 2006 | GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2007 | sqlite3_mutex_leave(mutexShared); |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 2008 | } |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 2009 | #endif |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2010 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2011 | |
drh | cfed7bc | 2006-03-13 14:28:05 +0000 | [diff] [blame] | 2012 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2013 | /* If the new Btree uses a sharable pBtShared, then link the new |
| 2014 | ** Btree into the list of all sharable Btrees for the same connection. |
drh | abddb0c | 2007-08-20 13:14:28 +0000 | [diff] [blame] | 2015 | ** The list is kept in ascending order by pBt address. |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 2016 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2017 | if( p->sharable ){ |
| 2018 | int i; |
| 2019 | Btree *pSib; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2020 | for(i=0; i<db->nDb; i++){ |
| 2021 | if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2022 | while( pSib->pPrev ){ pSib = pSib->pPrev; } |
| 2023 | if( p->pBt<pSib->pBt ){ |
| 2024 | p->pNext = pSib; |
| 2025 | p->pPrev = 0; |
| 2026 | pSib->pPrev = p; |
| 2027 | }else{ |
drh | abddb0c | 2007-08-20 13:14:28 +0000 | [diff] [blame] | 2028 | while( pSib->pNext && pSib->pNext->pBt<p->pBt ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2029 | pSib = pSib->pNext; |
| 2030 | } |
| 2031 | p->pNext = pSib->pNext; |
| 2032 | p->pPrev = pSib; |
| 2033 | if( p->pNext ){ |
| 2034 | p->pNext->pPrev = p; |
| 2035 | } |
| 2036 | pSib->pNext = p; |
| 2037 | } |
| 2038 | break; |
| 2039 | } |
| 2040 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2041 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2042 | #endif |
| 2043 | *ppBtree = p; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2044 | |
| 2045 | btree_open_out: |
| 2046 | if( rc!=SQLITE_OK ){ |
| 2047 | if( pBt && pBt->pPager ){ |
| 2048 | sqlite3PagerClose(pBt->pPager); |
| 2049 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2050 | sqlite3_free(pBt); |
| 2051 | sqlite3_free(p); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2052 | *ppBtree = 0; |
drh | 75c014c | 2010-08-30 15:02:28 +0000 | [diff] [blame] | 2053 | }else{ |
| 2054 | /* If the B-Tree was successfully opened, set the pager-cache size to the |
| 2055 | ** default value. Except, when opening on an existing shared pager-cache, |
| 2056 | ** do not change the pager-cache size. |
| 2057 | */ |
| 2058 | if( sqlite3BtreeSchema(p, 0, 0)==0 ){ |
| 2059 | sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE); |
| 2060 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2061 | } |
drh | 7555d8e | 2009-03-20 13:15:30 +0000 | [diff] [blame] | 2062 | if( mutexOpen ){ |
| 2063 | assert( sqlite3_mutex_held(mutexOpen) ); |
| 2064 | sqlite3_mutex_leave(mutexOpen); |
| 2065 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2066 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
| 2069 | /* |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2070 | ** Decrement the BtShared.nRef counter. When it reaches zero, |
| 2071 | ** remove the BtShared structure from the sharing list. Return |
| 2072 | ** true if the BtShared.nRef counter reaches zero and return |
| 2073 | ** false if it is still positive. |
| 2074 | */ |
| 2075 | static int removeFromSharingList(BtShared *pBt){ |
| 2076 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | 30ddce6 | 2011-10-15 00:16:30 +0000 | [diff] [blame] | 2077 | MUTEX_LOGIC( sqlite3_mutex *pMaster; ) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2078 | BtShared *pList; |
| 2079 | int removed = 0; |
| 2080 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2081 | assert( sqlite3_mutex_notheld(pBt->mutex) ); |
drh | 30ddce6 | 2011-10-15 00:16:30 +0000 | [diff] [blame] | 2082 | MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); ) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2083 | sqlite3_mutex_enter(pMaster); |
| 2084 | pBt->nRef--; |
| 2085 | if( pBt->nRef<=0 ){ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 2086 | if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){ |
| 2087 | GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2088 | }else{ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 2089 | pList = GLOBAL(BtShared*,sqlite3SharedCacheList); |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 2090 | while( ALWAYS(pList) && pList->pNext!=pBt ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2091 | pList=pList->pNext; |
| 2092 | } |
drh | 34004ce | 2008-07-11 16:15:17 +0000 | [diff] [blame] | 2093 | if( ALWAYS(pList) ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2094 | pList->pNext = pBt->pNext; |
| 2095 | } |
| 2096 | } |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 2097 | if( SQLITE_THREADSAFE ){ |
| 2098 | sqlite3_mutex_free(pBt->mutex); |
| 2099 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2100 | removed = 1; |
| 2101 | } |
| 2102 | sqlite3_mutex_leave(pMaster); |
| 2103 | return removed; |
| 2104 | #else |
| 2105 | return 1; |
| 2106 | #endif |
| 2107 | } |
| 2108 | |
| 2109 | /* |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 2110 | ** Make sure pBt->pTmpSpace points to an allocation of |
| 2111 | ** MX_CELL_SIZE(pBt) bytes. |
| 2112 | */ |
| 2113 | static void allocateTempSpace(BtShared *pBt){ |
| 2114 | if( !pBt->pTmpSpace ){ |
| 2115 | pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize ); |
dan | 14285b7 | 2013-10-16 11:39:07 +0000 | [diff] [blame] | 2116 | |
| 2117 | /* One of the uses of pBt->pTmpSpace is to format cells before |
| 2118 | ** inserting them into a leaf page (function fillInCell()). If |
| 2119 | ** a cell is less than 4 bytes in size, it is rounded up to 4 bytes |
| 2120 | ** by the various routines that manipulate binary cells. Which |
| 2121 | ** can mean that fillInCell() only initializes the first 2 or 3 |
| 2122 | ** bytes of pTmpSpace, but that the first 4 bytes are copied from |
| 2123 | ** it into a database page. This is not actually a problem, but it |
| 2124 | ** does cause a valgrind error when the 1 or 2 bytes of unitialized |
| 2125 | ** data is passed to system call write(). So to avoid this error, |
| 2126 | ** zero the first 4 bytes of temp space here. */ |
| 2127 | if( pBt->pTmpSpace ) memset(pBt->pTmpSpace, 0, 4); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 2128 | } |
| 2129 | } |
| 2130 | |
| 2131 | /* |
| 2132 | ** Free the pBt->pTmpSpace allocation |
| 2133 | */ |
| 2134 | static void freeTempSpace(BtShared *pBt){ |
| 2135 | sqlite3PageFree( pBt->pTmpSpace); |
| 2136 | pBt->pTmpSpace = 0; |
| 2137 | } |
| 2138 | |
| 2139 | /* |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2140 | ** Close an open database and invalidate all cursors. |
| 2141 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2142 | int sqlite3BtreeClose(Btree *p){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2143 | BtShared *pBt = p->pBt; |
| 2144 | BtCursor *pCur; |
| 2145 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2146 | /* Close all cursors opened via this handle. */ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2147 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2148 | sqlite3BtreeEnter(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2149 | pCur = pBt->pCursor; |
| 2150 | while( pCur ){ |
| 2151 | BtCursor *pTmp = pCur; |
| 2152 | pCur = pCur->pNext; |
| 2153 | if( pTmp->pBtree==p ){ |
| 2154 | sqlite3BtreeCloseCursor(pTmp); |
| 2155 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2156 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2157 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2158 | /* Rollback any active transaction and free the handle structure. |
| 2159 | ** The call to sqlite3BtreeRollback() drops any table-locks held by |
| 2160 | ** this handle. |
| 2161 | */ |
drh | 0f198a7 | 2012-02-13 16:43:16 +0000 | [diff] [blame] | 2162 | sqlite3BtreeRollback(p, SQLITE_OK); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2163 | sqlite3BtreeLeave(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2164 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2165 | /* If there are still other outstanding references to the shared-btree |
| 2166 | ** structure, return now. The remainder of this procedure cleans |
| 2167 | ** up the shared-btree. |
| 2168 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2169 | assert( p->wantToLock==0 && p->locked==0 ); |
| 2170 | if( !p->sharable || removeFromSharingList(pBt) ){ |
| 2171 | /* The pBt is no longer on the sharing list, so we can access |
| 2172 | ** it without having to hold the mutex. |
| 2173 | ** |
| 2174 | ** Clean out and delete the BtShared object. |
| 2175 | */ |
| 2176 | assert( !pBt->pCursor ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2177 | sqlite3PagerClose(pBt->pPager); |
| 2178 | if( pBt->xFreeSchema && pBt->pSchema ){ |
| 2179 | pBt->xFreeSchema(pBt->pSchema); |
| 2180 | } |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 2181 | sqlite3DbFree(0, pBt->pSchema); |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 2182 | freeTempSpace(pBt); |
drh | 65bbf29 | 2008-06-19 01:03:17 +0000 | [diff] [blame] | 2183 | sqlite3_free(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2184 | } |
| 2185 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2186 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | cab5ed7 | 2007-08-22 11:41:18 +0000 | [diff] [blame] | 2187 | assert( p->wantToLock==0 ); |
| 2188 | assert( p->locked==0 ); |
| 2189 | if( p->pPrev ) p->pPrev->pNext = p->pNext; |
| 2190 | if( p->pNext ) p->pNext->pPrev = p->pPrev; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2191 | #endif |
| 2192 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 2193 | sqlite3_free(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2194 | return SQLITE_OK; |
| 2195 | } |
| 2196 | |
| 2197 | /* |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 2198 | ** Change the limit on the number of pages allowed in the cache. |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 2199 | ** |
| 2200 | ** The maximum number of cache pages is set to the absolute |
| 2201 | ** value of mxPage. If mxPage is negative, the pager will |
| 2202 | ** operate asynchronously - it will not stop to do fsync()s |
| 2203 | ** to insure data is written to the disk surface before |
| 2204 | ** continuing. Transactions still work if synchronous is off, |
| 2205 | ** and the database cannot be corrupted if this program |
| 2206 | ** crashes. But if the operating system crashes or there is |
| 2207 | ** an abrupt power failure when synchronous is off, the database |
| 2208 | ** could be left in an inconsistent and unrecoverable state. |
| 2209 | ** Synchronous is on by default so database corruption is not |
| 2210 | ** normally a worry. |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 2211 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2212 | int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){ |
| 2213 | BtShared *pBt = p->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2214 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2215 | sqlite3BtreeEnter(p); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2216 | sqlite3PagerSetCachesize(pBt->pPager, mxPage); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2217 | sqlite3BtreeLeave(p); |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 2218 | return SQLITE_OK; |
| 2219 | } |
| 2220 | |
drh | 18c7e40 | 2014-03-14 11:46:10 +0000 | [diff] [blame] | 2221 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 2222 | /* |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 2223 | ** Change the limit on the amount of the database file that may be |
| 2224 | ** memory mapped. |
| 2225 | */ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 2226 | int sqlite3BtreeSetMmapLimit(Btree *p, sqlite3_int64 szMmap){ |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 2227 | BtShared *pBt = p->pBt; |
| 2228 | assert( sqlite3_mutex_held(p->db->mutex) ); |
| 2229 | sqlite3BtreeEnter(p); |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 2230 | sqlite3PagerSetMmapLimit(pBt->pPager, szMmap); |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 2231 | sqlite3BtreeLeave(p); |
| 2232 | return SQLITE_OK; |
| 2233 | } |
drh | 18c7e40 | 2014-03-14 11:46:10 +0000 | [diff] [blame] | 2234 | #endif /* SQLITE_MAX_MMAP_SIZE>0 */ |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 2235 | |
| 2236 | /* |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 2237 | ** Change the way data is synced to disk in order to increase or decrease |
| 2238 | ** how well the database resists damage due to OS crashes and power |
| 2239 | ** failures. Level 1 is the same as asynchronous (no syncs() occur and |
| 2240 | ** there is a high probability of damage) Level 2 is the default. There |
| 2241 | ** is a very low but non-zero probability of damage. Level 3 reduces the |
| 2242 | ** probability of damage to near zero but with a write performance reduction. |
| 2243 | */ |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 2244 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
drh | 40c3941 | 2013-08-16 20:42:20 +0000 | [diff] [blame] | 2245 | int sqlite3BtreeSetPagerFlags( |
drh | c97d846 | 2010-11-19 18:23:35 +0000 | [diff] [blame] | 2246 | Btree *p, /* The btree to set the safety level on */ |
drh | 40c3941 | 2013-08-16 20:42:20 +0000 | [diff] [blame] | 2247 | unsigned pgFlags /* Various PAGER_* flags */ |
drh | c97d846 | 2010-11-19 18:23:35 +0000 | [diff] [blame] | 2248 | ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2249 | BtShared *pBt = p->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2250 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2251 | sqlite3BtreeEnter(p); |
drh | 40c3941 | 2013-08-16 20:42:20 +0000 | [diff] [blame] | 2252 | sqlite3PagerSetFlags(pBt->pPager, pgFlags); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2253 | sqlite3BtreeLeave(p); |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 2254 | return SQLITE_OK; |
| 2255 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 2256 | #endif |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 2257 | |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 2258 | /* |
| 2259 | ** Return TRUE if the given btree is set to safety level 1. In other |
| 2260 | ** words, return TRUE if no sync() occurs on the disk files. |
| 2261 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2262 | int sqlite3BtreeSyncDisabled(Btree *p){ |
| 2263 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2264 | int rc; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2265 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2266 | sqlite3BtreeEnter(p); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 2267 | assert( pBt && pBt->pPager ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2268 | rc = sqlite3PagerNosync(pBt->pPager); |
| 2269 | sqlite3BtreeLeave(p); |
| 2270 | return rc; |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 2273 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2274 | ** Change the default pages size and the number of reserved bytes per page. |
drh | ce4869f | 2009-04-02 20:16:58 +0000 | [diff] [blame] | 2275 | ** Or, if the page size has already been fixed, return SQLITE_READONLY |
| 2276 | ** without changing anything. |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 2277 | ** |
| 2278 | ** The page size must be a power of 2 between 512 and 65536. If the page |
| 2279 | ** size supplied does not meet this constraint then the page size is not |
| 2280 | ** changed. |
| 2281 | ** |
| 2282 | ** Page sizes are constrained to be a power of two so that the region |
| 2283 | ** of the database file used for locking (beginning at PENDING_BYTE, |
| 2284 | ** the first byte past the 1GB boundary, 0x40000000) needs to occur |
| 2285 | ** at the beginning of a page. |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 2286 | ** |
| 2287 | ** If parameter nReserve is less than zero, then the number of reserved |
| 2288 | ** bytes per page is left unchanged. |
drh | ce4869f | 2009-04-02 20:16:58 +0000 | [diff] [blame] | 2289 | ** |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2290 | ** 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] | 2291 | ** and autovacuum mode can no longer be changed. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2292 | */ |
drh | ce4869f | 2009-04-02 20:16:58 +0000 | [diff] [blame] | 2293 | int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2294 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2295 | BtShared *pBt = p->pBt; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 2296 | assert( nReserve>=-1 && nReserve<=255 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2297 | sqlite3BtreeEnter(p); |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2298 | if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2299 | sqlite3BtreeLeave(p); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2300 | return SQLITE_READONLY; |
| 2301 | } |
| 2302 | if( nReserve<0 ){ |
| 2303 | nReserve = pBt->pageSize - pBt->usableSize; |
| 2304 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 2305 | assert( nReserve>=0 && nReserve<=255 ); |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 2306 | if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE && |
| 2307 | ((pageSize-1)&pageSize)==0 ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 2308 | assert( (pageSize & 7)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2309 | assert( !pBt->pPage1 && !pBt->pCursor ); |
drh | b2eced5 | 2010-08-12 02:41:12 +0000 | [diff] [blame] | 2310 | pBt->pageSize = (u32)pageSize; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 2311 | freeTempSpace(pBt); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2312 | } |
drh | fa9601a | 2009-06-18 17:22:39 +0000 | [diff] [blame] | 2313 | rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 2314 | pBt->usableSize = pBt->pageSize - (u16)nReserve; |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2315 | if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2316 | sqlite3BtreeLeave(p); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 2317 | return rc; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2318 | } |
| 2319 | |
| 2320 | /* |
| 2321 | ** Return the currently defined page size |
| 2322 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2323 | int sqlite3BtreeGetPageSize(Btree *p){ |
| 2324 | return p->pBt->pageSize; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2325 | } |
drh | 7f75122 | 2009-03-17 22:33:00 +0000 | [diff] [blame] | 2326 | |
drh | a1f3853 | 2012-10-01 12:44:26 +0000 | [diff] [blame] | 2327 | #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_DEBUG) |
dan | 0094f37 | 2012-09-28 20:23:42 +0000 | [diff] [blame] | 2328 | /* |
| 2329 | ** This function is similar to sqlite3BtreeGetReserve(), except that it |
| 2330 | ** may only be called if it is guaranteed that the b-tree mutex is already |
| 2331 | ** held. |
| 2332 | ** |
| 2333 | ** This is useful in one special case in the backup API code where it is |
| 2334 | ** known that the shared b-tree mutex is held, but the mutex on the |
| 2335 | ** database handle that owns *p is not. In this case if sqlite3BtreeEnter() |
| 2336 | ** were to be called, it might collide with some other operation on the |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 2337 | ** database handle that owns *p, causing undefined behavior. |
dan | 0094f37 | 2012-09-28 20:23:42 +0000 | [diff] [blame] | 2338 | */ |
| 2339 | int sqlite3BtreeGetReserveNoMutex(Btree *p){ |
| 2340 | assert( sqlite3_mutex_held(p->pBt->mutex) ); |
| 2341 | return p->pBt->pageSize - p->pBt->usableSize; |
| 2342 | } |
drh | a1f3853 | 2012-10-01 12:44:26 +0000 | [diff] [blame] | 2343 | #endif /* SQLITE_HAS_CODEC || SQLITE_DEBUG */ |
dan | 0094f37 | 2012-09-28 20:23:42 +0000 | [diff] [blame] | 2344 | |
dan | bb2b441 | 2011-04-06 17:54:31 +0000 | [diff] [blame] | 2345 | #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) |
drh | 7f75122 | 2009-03-17 22:33:00 +0000 | [diff] [blame] | 2346 | /* |
| 2347 | ** Return the number of bytes of space at the end of every page that |
| 2348 | ** are intentually left unused. This is the "reserved" space that is |
| 2349 | ** sometimes used by extensions. |
| 2350 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2351 | int sqlite3BtreeGetReserve(Btree *p){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2352 | int n; |
| 2353 | sqlite3BtreeEnter(p); |
| 2354 | n = p->pBt->pageSize - p->pBt->usableSize; |
| 2355 | sqlite3BtreeLeave(p); |
| 2356 | return n; |
drh | 2011d5f | 2004-07-22 02:40:37 +0000 | [diff] [blame] | 2357 | } |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 2358 | |
| 2359 | /* |
| 2360 | ** Set the maximum page count for a database if mxPage is positive. |
| 2361 | ** No changes are made if mxPage is 0 or negative. |
| 2362 | ** Regardless of the value of mxPage, return the maximum page count. |
| 2363 | */ |
| 2364 | int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2365 | int n; |
| 2366 | sqlite3BtreeEnter(p); |
| 2367 | n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage); |
| 2368 | sqlite3BtreeLeave(p); |
| 2369 | return n; |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 2370 | } |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 2371 | |
| 2372 | /* |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2373 | ** Set the BTS_SECURE_DELETE flag if newFlag is 0 or 1. If newFlag is -1, |
| 2374 | ** then make no changes. Always return the value of the BTS_SECURE_DELETE |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 2375 | ** setting after the change. |
| 2376 | */ |
| 2377 | int sqlite3BtreeSecureDelete(Btree *p, int newFlag){ |
| 2378 | int b; |
drh | af034ed | 2010-02-12 19:46:26 +0000 | [diff] [blame] | 2379 | if( p==0 ) return 0; |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 2380 | sqlite3BtreeEnter(p); |
| 2381 | if( newFlag>=0 ){ |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2382 | p->pBt->btsFlags &= ~BTS_SECURE_DELETE; |
| 2383 | if( newFlag ) p->pBt->btsFlags |= BTS_SECURE_DELETE; |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 2384 | } |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2385 | b = (p->pBt->btsFlags & BTS_SECURE_DELETE)!=0; |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 2386 | sqlite3BtreeLeave(p); |
| 2387 | return b; |
| 2388 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 2389 | #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 2390 | |
| 2391 | /* |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 2392 | ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum' |
| 2393 | ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it |
| 2394 | ** is disabled. The default value for the auto-vacuum property is |
| 2395 | ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro. |
| 2396 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2397 | int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 2398 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 2399 | return SQLITE_READONLY; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 2400 | #else |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2401 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2402 | int rc = SQLITE_OK; |
drh | 076d466 | 2009-02-18 20:31:18 +0000 | [diff] [blame] | 2403 | u8 av = (u8)autoVacuum; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2404 | |
| 2405 | sqlite3BtreeEnter(p); |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2406 | if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2407 | rc = SQLITE_READONLY; |
| 2408 | }else{ |
drh | 076d466 | 2009-02-18 20:31:18 +0000 | [diff] [blame] | 2409 | pBt->autoVacuum = av ?1:0; |
| 2410 | pBt->incrVacuum = av==2 ?1:0; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 2411 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2412 | sqlite3BtreeLeave(p); |
| 2413 | return rc; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 2414 | #endif |
| 2415 | } |
| 2416 | |
| 2417 | /* |
| 2418 | ** Return the value of the 'auto-vacuum' property. If auto-vacuum is |
| 2419 | ** enabled 1 is returned. Otherwise 0. |
| 2420 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2421 | int sqlite3BtreeGetAutoVacuum(Btree *p){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 2422 | #ifdef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2423 | return BTREE_AUTOVACUUM_NONE; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 2424 | #else |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2425 | int rc; |
| 2426 | sqlite3BtreeEnter(p); |
| 2427 | rc = ( |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2428 | (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE: |
| 2429 | (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL: |
| 2430 | BTREE_AUTOVACUUM_INCR |
| 2431 | ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2432 | sqlite3BtreeLeave(p); |
| 2433 | return rc; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 2434 | #endif |
| 2435 | } |
| 2436 | |
| 2437 | |
| 2438 | /* |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2439 | ** Get a reference to pPage1 of the database file. This will |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 2440 | ** also acquire a readlock on that file. |
| 2441 | ** |
| 2442 | ** SQLITE_OK is returned on success. If the file is not a |
| 2443 | ** well-formed database file, then SQLITE_CORRUPT is returned. |
| 2444 | ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 2445 | ** is returned if we run out of memory. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 2446 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2447 | static int lockBtree(BtShared *pBt){ |
drh | c2a4bab | 2010-04-02 12:46:45 +0000 | [diff] [blame] | 2448 | int rc; /* Result code from subfunctions */ |
| 2449 | MemPage *pPage1; /* Page 1 of the database file */ |
| 2450 | int nPage; /* Number of pages in the database */ |
| 2451 | int nPageFile = 0; /* Number of pages in the database file */ |
| 2452 | int nPageHeader; /* Number of pages in the database according to hdr */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2453 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2454 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 295dc10 | 2009-04-01 19:07:03 +0000 | [diff] [blame] | 2455 | assert( pBt->pPage1==0 ); |
danielk1977 | 89bc4bc | 2009-07-21 19:25:24 +0000 | [diff] [blame] | 2456 | rc = sqlite3PagerSharedLock(pBt->pPager); |
| 2457 | if( rc!=SQLITE_OK ) return rc; |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 2458 | rc = btreeGetPage(pBt, 1, &pPage1, 0); |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 2459 | if( rc!=SQLITE_OK ) return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 2460 | |
| 2461 | /* Do some checking to help insure the file we opened really is |
| 2462 | ** a valid database file. |
| 2463 | */ |
drh | c2a4bab | 2010-04-02 12:46:45 +0000 | [diff] [blame] | 2464 | nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData); |
drh | 8fb8b53 | 2010-08-14 17:12:04 +0000 | [diff] [blame] | 2465 | sqlite3PagerPagecount(pBt->pPager, &nPageFile); |
drh | b28e59b | 2010-06-17 02:13:39 +0000 | [diff] [blame] | 2466 | 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] | 2467 | nPage = nPageFile; |
drh | 97b59a5 | 2010-03-31 02:31:33 +0000 | [diff] [blame] | 2468 | } |
| 2469 | if( nPage>0 ){ |
drh | 43b18e1 | 2010-08-17 19:40:08 +0000 | [diff] [blame] | 2470 | u32 pageSize; |
| 2471 | u32 usableSize; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2472 | u8 *page1 = pPage1->aData; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2473 | rc = SQLITE_NOTADB; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2474 | if( memcmp(page1, zMagicHeader, 16)!=0 ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2475 | goto page1_init_failed; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 2476 | } |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 2477 | |
| 2478 | #ifdef SQLITE_OMIT_WAL |
| 2479 | if( page1[18]>1 ){ |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2480 | pBt->btsFlags |= BTS_READ_ONLY; |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 2481 | } |
| 2482 | if( page1[19]>1 ){ |
| 2483 | goto page1_init_failed; |
| 2484 | } |
| 2485 | #else |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 2486 | if( page1[18]>2 ){ |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2487 | pBt->btsFlags |= BTS_READ_ONLY; |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 2488 | } |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 2489 | if( page1[19]>2 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2490 | goto page1_init_failed; |
| 2491 | } |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 2492 | |
dan | a470aeb | 2010-04-21 11:43:38 +0000 | [diff] [blame] | 2493 | /* If the write version is set to 2, this database should be accessed |
| 2494 | ** in WAL mode. If the log is not already open, open it now. Then |
| 2495 | ** return SQLITE_OK and return without populating BtShared.pPage1. |
| 2496 | ** The caller detects this and calls this function again. This is |
| 2497 | ** required as the version of page 1 currently in the page1 buffer |
| 2498 | ** may not be the latest version - there may be a newer one in the log |
| 2499 | ** file. |
| 2500 | */ |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2501 | if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){ |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 2502 | int isOpen = 0; |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 2503 | rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen); |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 2504 | if( rc!=SQLITE_OK ){ |
| 2505 | goto page1_init_failed; |
| 2506 | }else if( isOpen==0 ){ |
| 2507 | releasePage(pPage1); |
| 2508 | return SQLITE_OK; |
| 2509 | } |
dan | 8b5444b | 2010-04-27 14:37:47 +0000 | [diff] [blame] | 2510 | rc = SQLITE_NOTADB; |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 2511 | } |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 2512 | #endif |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 2513 | |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 2514 | /* The maximum embedded fraction must be exactly 25%. And the minimum |
| 2515 | ** embedded fraction must be 12.5% for both leaf-data and non-leaf-data. |
| 2516 | ** The original design allowed these amounts to vary, but as of |
| 2517 | ** version 3.6.0, we require them to be fixed. |
| 2518 | */ |
| 2519 | if( memcmp(&page1[21], "\100\040\040",3)!=0 ){ |
| 2520 | goto page1_init_failed; |
| 2521 | } |
drh | b2eced5 | 2010-08-12 02:41:12 +0000 | [diff] [blame] | 2522 | pageSize = (page1[16]<<8) | (page1[17]<<16); |
| 2523 | if( ((pageSize-1)&pageSize)!=0 |
| 2524 | || pageSize>SQLITE_MAX_PAGE_SIZE |
| 2525 | || pageSize<=256 |
drh | 7dc385e | 2007-09-06 23:39:36 +0000 | [diff] [blame] | 2526 | ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 2527 | goto page1_init_failed; |
| 2528 | } |
| 2529 | assert( (pageSize & 7)==0 ); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 2530 | usableSize = pageSize - page1[20]; |
shaneh | 1df2db7 | 2010-08-18 02:28:48 +0000 | [diff] [blame] | 2531 | if( (u32)pageSize!=pBt->pageSize ){ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 2532 | /* After reading the first page of the database assuming a page size |
| 2533 | ** of BtShared.pageSize, we have discovered that the page-size is |
| 2534 | ** actually pageSize. Unlock the database, leave pBt->pPage1 at |
| 2535 | ** zero and return SQLITE_OK. The caller will call this function |
| 2536 | ** again with the correct page-size. |
| 2537 | */ |
| 2538 | releasePage(pPage1); |
drh | 43b18e1 | 2010-08-17 19:40:08 +0000 | [diff] [blame] | 2539 | pBt->usableSize = usableSize; |
| 2540 | pBt->pageSize = pageSize; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 2541 | freeTempSpace(pBt); |
drh | fa9601a | 2009-06-18 17:22:39 +0000 | [diff] [blame] | 2542 | rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, |
| 2543 | pageSize-usableSize); |
drh | 5e48393 | 2009-07-10 16:51:30 +0000 | [diff] [blame] | 2544 | return rc; |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 2545 | } |
dan | ecac670 | 2011-02-09 18:19:20 +0000 | [diff] [blame] | 2546 | if( (pBt->db->flags & SQLITE_RecoveryMode)==0 && nPage>nPageFile ){ |
drh | c2a4bab | 2010-04-02 12:46:45 +0000 | [diff] [blame] | 2547 | rc = SQLITE_CORRUPT_BKPT; |
| 2548 | goto page1_init_failed; |
| 2549 | } |
drh | b33e1b9 | 2009-06-18 11:29:20 +0000 | [diff] [blame] | 2550 | if( usableSize<480 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2551 | goto page1_init_failed; |
| 2552 | } |
drh | 43b18e1 | 2010-08-17 19:40:08 +0000 | [diff] [blame] | 2553 | pBt->pageSize = pageSize; |
| 2554 | pBt->usableSize = usableSize; |
drh | 057cd3a | 2005-02-15 16:23:02 +0000 | [diff] [blame] | 2555 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2556 | pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0); |
danielk1977 | 27b1f95 | 2007-06-25 08:16:58 +0000 | [diff] [blame] | 2557 | pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0); |
drh | 057cd3a | 2005-02-15 16:23:02 +0000 | [diff] [blame] | 2558 | #endif |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 2559 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2560 | |
| 2561 | /* maxLocal is the maximum amount of payload to store locally for |
| 2562 | ** a cell. Make sure it is small enough so that at least minFanout |
| 2563 | ** cells can will fit on one page. We assume a 10-byte page header. |
| 2564 | ** Besides the payload, the cell must store: |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2565 | ** 2-byte pointer to the cell |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2566 | ** 4-byte child pointer |
| 2567 | ** 9-byte nKey value |
| 2568 | ** 4-byte nData value |
| 2569 | ** 4-byte overflow page pointer |
drh | e22e03e | 2010-08-18 21:19:03 +0000 | [diff] [blame] | 2570 | ** 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] | 2571 | ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow |
| 2572 | ** page pointer. |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2573 | */ |
shaneh | 1df2db7 | 2010-08-18 02:28:48 +0000 | [diff] [blame] | 2574 | pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23); |
| 2575 | pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23); |
| 2576 | pBt->maxLeaf = (u16)(pBt->usableSize - 35); |
| 2577 | pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23); |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2578 | if( pBt->maxLocal>127 ){ |
| 2579 | pBt->max1bytePayload = 127; |
| 2580 | }else{ |
mistachkin | 0547e2f | 2012-01-08 00:54:02 +0000 | [diff] [blame] | 2581 | pBt->max1bytePayload = (u8)pBt->maxLocal; |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2582 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 2583 | assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2584 | pBt->pPage1 = pPage1; |
drh | dd3cd97 | 2010-03-27 17:12:36 +0000 | [diff] [blame] | 2585 | pBt->nPage = nPage; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2586 | return SQLITE_OK; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 2587 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2588 | page1_init_failed: |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2589 | releasePage(pPage1); |
| 2590 | pBt->pPage1 = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2591 | return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 2592 | } |
| 2593 | |
drh | 85ec3b6 | 2013-05-14 23:12:06 +0000 | [diff] [blame] | 2594 | #ifndef NDEBUG |
| 2595 | /* |
| 2596 | ** Return the number of cursors open on pBt. This is for use |
| 2597 | ** in assert() expressions, so it is only compiled if NDEBUG is not |
| 2598 | ** defined. |
| 2599 | ** |
| 2600 | ** Only write cursors are counted if wrOnly is true. If wrOnly is |
| 2601 | ** false then all cursors are counted. |
| 2602 | ** |
| 2603 | ** 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] | 2604 | ** is capable of reading or writing to the database. Cursors that |
drh | 85ec3b6 | 2013-05-14 23:12:06 +0000 | [diff] [blame] | 2605 | ** have been tripped into the CURSOR_FAULT state are not counted. |
| 2606 | */ |
| 2607 | static int countValidCursors(BtShared *pBt, int wrOnly){ |
| 2608 | BtCursor *pCur; |
| 2609 | int r = 0; |
| 2610 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 2611 | if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0) |
| 2612 | && pCur->eState!=CURSOR_FAULT ) r++; |
drh | 85ec3b6 | 2013-05-14 23:12:06 +0000 | [diff] [blame] | 2613 | } |
| 2614 | return r; |
| 2615 | } |
| 2616 | #endif |
| 2617 | |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 2618 | /* |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 2619 | ** If there are no outstanding cursors and we are not in the middle |
| 2620 | ** of a transaction but there is a read lock on the database, then |
| 2621 | ** this routine unrefs the first page of the database file which |
| 2622 | ** has the effect of releasing the read lock. |
| 2623 | ** |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 2624 | ** If there is a transaction in progress, this routine is a no-op. |
| 2625 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2626 | static void unlockBtreeIfUnused(BtShared *pBt){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2627 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 85ec3b6 | 2013-05-14 23:12:06 +0000 | [diff] [blame] | 2628 | assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE ); |
danielk1977 | 1bc9ee9 | 2009-07-04 15:41:02 +0000 | [diff] [blame] | 2629 | if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){ |
danielk1977 | c1761e8 | 2009-06-25 09:40:03 +0000 | [diff] [blame] | 2630 | assert( pBt->pPage1->aData ); |
| 2631 | assert( sqlite3PagerRefcount(pBt->pPager)==1 ); |
| 2632 | assert( pBt->pPage1->aData ); |
| 2633 | releasePage(pBt->pPage1); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2634 | pBt->pPage1 = 0; |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 2635 | } |
| 2636 | } |
| 2637 | |
| 2638 | /* |
drh | e39f2f9 | 2009-07-23 01:43:59 +0000 | [diff] [blame] | 2639 | ** If pBt points to an empty file then convert that empty file |
| 2640 | ** into a new empty database by initializing the first page of |
| 2641 | ** the database. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2642 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2643 | static int newDatabase(BtShared *pBt){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 2644 | MemPage *pP1; |
| 2645 | unsigned char *data; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2646 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2647 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2648 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | dd3cd97 | 2010-03-27 17:12:36 +0000 | [diff] [blame] | 2649 | if( pBt->nPage>0 ){ |
| 2650 | return SQLITE_OK; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame] | 2651 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2652 | pP1 = pBt->pPage1; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 2653 | assert( pP1!=0 ); |
| 2654 | data = pP1->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2655 | rc = sqlite3PagerWrite(pP1->pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2656 | if( rc ) return rc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 2657 | memcpy(data, zMagicHeader, sizeof(zMagicHeader)); |
| 2658 | assert( sizeof(zMagicHeader)==16 ); |
shaneh | 1df2db7 | 2010-08-18 02:28:48 +0000 | [diff] [blame] | 2659 | data[16] = (u8)((pBt->pageSize>>8)&0xff); |
| 2660 | data[17] = (u8)((pBt->pageSize>>16)&0xff); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 2661 | data[18] = 1; |
| 2662 | data[19] = 1; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 2663 | assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize); |
| 2664 | data[20] = (u8)(pBt->pageSize - pBt->usableSize); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 2665 | data[21] = 64; |
| 2666 | data[22] = 32; |
| 2667 | data[23] = 32; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2668 | memset(&data[24], 0, 100-24); |
drh | e6c4381 | 2004-05-14 12:17:46 +0000 | [diff] [blame] | 2669 | zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA ); |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2670 | pBt->btsFlags |= BTS_PAGESIZE_FIXED; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2671 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2672 | assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 ); |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 2673 | assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 ); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2674 | put4byte(&data[36 + 4*4], pBt->autoVacuum); |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 2675 | put4byte(&data[36 + 7*4], pBt->incrVacuum); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2676 | #endif |
drh | dd3cd97 | 2010-03-27 17:12:36 +0000 | [diff] [blame] | 2677 | pBt->nPage = 1; |
| 2678 | data[31] = 1; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2679 | return SQLITE_OK; |
| 2680 | } |
| 2681 | |
| 2682 | /* |
dan | b483eba | 2012-10-13 19:58:11 +0000 | [diff] [blame] | 2683 | ** Initialize the first page of the database file (creating a database |
| 2684 | ** consisting of a single page and no schema objects). Return SQLITE_OK |
| 2685 | ** if successful, or an SQLite error code otherwise. |
| 2686 | */ |
| 2687 | int sqlite3BtreeNewDb(Btree *p){ |
| 2688 | int rc; |
| 2689 | sqlite3BtreeEnter(p); |
| 2690 | p->pBt->nPage = 0; |
| 2691 | rc = newDatabase(p->pBt); |
| 2692 | sqlite3BtreeLeave(p); |
| 2693 | return rc; |
| 2694 | } |
| 2695 | |
| 2696 | /* |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2697 | ** Attempt to start a new transaction. A write-transaction |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 2698 | ** is started if the second argument is nonzero, otherwise a read- |
| 2699 | ** transaction. If the second argument is 2 or more and exclusive |
| 2700 | ** transaction is started, meaning that no other process is allowed |
| 2701 | ** to access the database. A preexisting transaction may not be |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2702 | ** upgraded to exclusive by calling this routine a second time - the |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 2703 | ** exclusivity flag only works for a new transaction. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2704 | ** |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2705 | ** A write-transaction must be started before attempting any |
| 2706 | ** changes to the database. None of the following routines |
| 2707 | ** will work unless a transaction is started first: |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2708 | ** |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 2709 | ** sqlite3BtreeCreateTable() |
| 2710 | ** sqlite3BtreeCreateIndex() |
| 2711 | ** sqlite3BtreeClearTable() |
| 2712 | ** sqlite3BtreeDropTable() |
| 2713 | ** sqlite3BtreeInsert() |
| 2714 | ** sqlite3BtreeDelete() |
| 2715 | ** sqlite3BtreeUpdateMeta() |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 2716 | ** |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2717 | ** If an initial attempt to acquire the lock fails because of lock contention |
| 2718 | ** and the database was previously unlocked, then invoke the busy handler |
| 2719 | ** if there is one. But if there was previously a read-lock, do not |
| 2720 | ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is |
| 2721 | ** returned when there is already a read-lock in order to avoid a deadlock. |
| 2722 | ** |
| 2723 | ** Suppose there are two processes A and B. A has a read lock and B has |
| 2724 | ** a reserved lock. B tries to promote to exclusive but is blocked because |
| 2725 | ** of A's read lock. A tries to promote to reserved but is blocked by B. |
| 2726 | ** One or the other of the two processes must give way or there can be |
| 2727 | ** no progress. By returning SQLITE_BUSY and not invoking the busy callback |
| 2728 | ** when A already has a read lock, we encourage A to give up and let B |
| 2729 | ** proceed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2730 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2731 | int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2732 | sqlite3 *pBlock = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2733 | BtShared *pBt = p->pBt; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2734 | int rc = SQLITE_OK; |
| 2735 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2736 | sqlite3BtreeEnter(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2737 | btreeIntegrity(p); |
| 2738 | |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2739 | /* If the btree is already in a write-transaction, or it |
| 2740 | ** is already in a read-transaction and a read-transaction |
| 2741 | ** is requested, this is a no-op. |
| 2742 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2743 | if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2744 | goto trans_begun; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2745 | } |
dan | 56c517a | 2013-09-26 11:04:33 +0000 | [diff] [blame] | 2746 | assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 ); |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2747 | |
| 2748 | /* Write transactions are not possible on a read-only database */ |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2749 | if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2750 | rc = SQLITE_READONLY; |
| 2751 | goto trans_begun; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2752 | } |
| 2753 | |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2754 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2755 | /* If another database handle has already opened a write transaction |
| 2756 | ** on this shared-btree structure and a second write transaction is |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2757 | ** requested, return SQLITE_LOCKED. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2758 | */ |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2759 | if( (wrflag && pBt->inTransaction==TRANS_WRITE) |
| 2760 | || (pBt->btsFlags & BTS_PENDING)!=0 |
| 2761 | ){ |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2762 | pBlock = pBt->pWriter->db; |
| 2763 | }else if( wrflag>1 ){ |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 2764 | BtLock *pIter; |
| 2765 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 2766 | if( pIter->pBtree!=p ){ |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2767 | pBlock = pIter->pBtree->db; |
| 2768 | break; |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 2769 | } |
| 2770 | } |
| 2771 | } |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2772 | if( pBlock ){ |
| 2773 | sqlite3ConnectionBlocked(p->db, pBlock); |
| 2774 | rc = SQLITE_LOCKED_SHAREDCACHE; |
| 2775 | goto trans_begun; |
| 2776 | } |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 2777 | #endif |
| 2778 | |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 2779 | /* Any read-only or read-write transaction implies a read-lock on |
| 2780 | ** page 1. So if some other shared-cache client already has a write-lock |
| 2781 | ** on page 1, the transaction cannot be opened. */ |
drh | 4c301aa | 2009-07-15 17:25:45 +0000 | [diff] [blame] | 2782 | rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK); |
| 2783 | if( SQLITE_OK!=rc ) goto trans_begun; |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 2784 | |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2785 | pBt->btsFlags &= ~BTS_INITIALLY_EMPTY; |
| 2786 | if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY; |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2787 | do { |
danielk1977 | 295dc10 | 2009-04-01 19:07:03 +0000 | [diff] [blame] | 2788 | /* Call lockBtree() until either pBt->pPage1 is populated or |
| 2789 | ** lockBtree() returns something other than SQLITE_OK. lockBtree() |
| 2790 | ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after |
| 2791 | ** reading page 1 it discovers that the page-size of the database |
| 2792 | ** file is not pBt->pageSize. In this case lockBtree() will update |
| 2793 | ** pBt->pageSize to the page-size of the file on disk. |
| 2794 | */ |
| 2795 | while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) ); |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 2796 | |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2797 | if( rc==SQLITE_OK && wrflag ){ |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2798 | if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){ |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 2799 | rc = SQLITE_READONLY; |
| 2800 | }else{ |
danielk1977 | d829335 | 2009-04-30 09:10:37 +0000 | [diff] [blame] | 2801 | rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db)); |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 2802 | if( rc==SQLITE_OK ){ |
| 2803 | rc = newDatabase(pBt); |
| 2804 | } |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2805 | } |
| 2806 | } |
| 2807 | |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 2808 | if( rc!=SQLITE_OK ){ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2809 | unlockBtreeIfUnused(pBt); |
| 2810 | } |
dan | f9b7671 | 2010-06-01 14:12:45 +0000 | [diff] [blame] | 2811 | }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE && |
danielk1977 | 1ceedd3 | 2008-11-19 10:22:33 +0000 | [diff] [blame] | 2812 | btreeInvokeBusyHandler(pBt) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2813 | |
| 2814 | if( rc==SQLITE_OK ){ |
| 2815 | if( p->inTrans==TRANS_NONE ){ |
| 2816 | pBt->nTransaction++; |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 2817 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 2818 | if( p->sharable ){ |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2819 | assert( p->lock.pBtree==p && p->lock.iTable==1 ); |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 2820 | p->lock.eLock = READ_LOCK; |
| 2821 | p->lock.pNext = pBt->pLock; |
| 2822 | pBt->pLock = &p->lock; |
| 2823 | } |
| 2824 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2825 | } |
| 2826 | p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ); |
| 2827 | if( p->inTrans>pBt->inTransaction ){ |
| 2828 | pBt->inTransaction = p->inTrans; |
| 2829 | } |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2830 | if( wrflag ){ |
dan | 59257dc | 2010-08-04 11:34:31 +0000 | [diff] [blame] | 2831 | MemPage *pPage1 = pBt->pPage1; |
| 2832 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 2833 | assert( !pBt->pWriter ); |
| 2834 | pBt->pWriter = p; |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 2835 | pBt->btsFlags &= ~BTS_EXCLUSIVE; |
| 2836 | if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE; |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 2837 | #endif |
dan | 59257dc | 2010-08-04 11:34:31 +0000 | [diff] [blame] | 2838 | |
| 2839 | /* If the db-size header field is incorrect (as it may be if an old |
| 2840 | ** client has been writing the database file), update it now. Doing |
| 2841 | ** this sooner rather than later means the database size can safely |
| 2842 | ** re-read the database size from page 1 if a savepoint or transaction |
| 2843 | ** rollback occurs within the transaction. |
| 2844 | */ |
| 2845 | if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){ |
| 2846 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
| 2847 | if( rc==SQLITE_OK ){ |
| 2848 | put4byte(&pPage1->aData[28], pBt->nPage); |
| 2849 | } |
| 2850 | } |
| 2851 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2852 | } |
| 2853 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2854 | |
| 2855 | trans_begun: |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2856 | if( rc==SQLITE_OK && wrflag ){ |
danielk1977 | 12dd549 | 2008-12-18 15:45:07 +0000 | [diff] [blame] | 2857 | /* This call makes sure that the pager has the correct number of |
| 2858 | ** open savepoints. If the second parameter is greater than 0 and |
| 2859 | ** the sub-journal is not already open, then it will be opened here. |
| 2860 | */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 2861 | rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint); |
| 2862 | } |
danielk1977 | 12dd549 | 2008-12-18 15:45:07 +0000 | [diff] [blame] | 2863 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2864 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2865 | sqlite3BtreeLeave(p); |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 2866 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2867 | } |
| 2868 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2869 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2870 | |
| 2871 | /* |
| 2872 | ** Set the pointer-map entries for all children of page pPage. Also, if |
| 2873 | ** pPage contains cells that point to overflow pages, set the pointer |
| 2874 | ** map entries for the overflow pages as well. |
| 2875 | */ |
| 2876 | static int setChildPtrmaps(MemPage *pPage){ |
| 2877 | int i; /* Counter variable */ |
| 2878 | int nCell; /* Number of cells in page pPage */ |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 2879 | int rc; /* Return code */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2880 | BtShared *pBt = pPage->pBt; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 2881 | u8 isInitOrig = pPage->isInit; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2882 | Pgno pgno = pPage->pgno; |
| 2883 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2884 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 2885 | rc = btreeInitPage(pPage); |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 2886 | if( rc!=SQLITE_OK ){ |
| 2887 | goto set_child_ptrmaps_out; |
| 2888 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2889 | nCell = pPage->nCell; |
| 2890 | |
| 2891 | for(i=0; i<nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2892 | u8 *pCell = findCell(pPage, i); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2893 | |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 2894 | ptrmapPutOvflPtr(pPage, pCell, &rc); |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 2895 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2896 | if( !pPage->leaf ){ |
| 2897 | Pgno childPgno = get4byte(pCell); |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 2898 | ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2899 | } |
| 2900 | } |
| 2901 | |
| 2902 | if( !pPage->leaf ){ |
| 2903 | Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 2904 | ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2905 | } |
| 2906 | |
| 2907 | set_child_ptrmaps_out: |
| 2908 | pPage->isInit = isInitOrig; |
| 2909 | return rc; |
| 2910 | } |
| 2911 | |
| 2912 | /* |
drh | f3aed59 | 2009-07-08 18:12:49 +0000 | [diff] [blame] | 2913 | ** Somewhere on pPage is a pointer to page iFrom. Modify this pointer so |
| 2914 | ** that it points to iTo. Parameter eType describes the type of pointer to |
| 2915 | ** be modified, as follows: |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2916 | ** |
| 2917 | ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child |
| 2918 | ** page of pPage. |
| 2919 | ** |
| 2920 | ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow |
| 2921 | ** page pointed to by one of the cells on pPage. |
| 2922 | ** |
| 2923 | ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next |
| 2924 | ** overflow page in the list. |
| 2925 | */ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2926 | static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2927 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 2928 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2929 | if( eType==PTRMAP_OVERFLOW2 ){ |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 2930 | /* 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] | 2931 | if( get4byte(pPage->aData)!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2932 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2933 | } |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 2934 | put4byte(pPage->aData, iTo); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2935 | }else{ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 2936 | u8 isInitOrig = pPage->isInit; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2937 | int i; |
| 2938 | int nCell; |
| 2939 | |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 2940 | btreeInitPage(pPage); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2941 | nCell = pPage->nCell; |
| 2942 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2943 | for(i=0; i<nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2944 | u8 *pCell = findCell(pPage, i); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2945 | if( eType==PTRMAP_OVERFLOW1 ){ |
| 2946 | CellInfo info; |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 2947 | btreeParseCellPtr(pPage, pCell, &info); |
drh | e42a9b4 | 2011-08-31 13:27:19 +0000 | [diff] [blame] | 2948 | if( info.iOverflow |
| 2949 | && pCell+info.iOverflow+3<=pPage->aData+pPage->maskPage |
| 2950 | && iFrom==get4byte(&pCell[info.iOverflow]) |
| 2951 | ){ |
| 2952 | put4byte(&pCell[info.iOverflow], iTo); |
| 2953 | break; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2954 | } |
| 2955 | }else{ |
| 2956 | if( get4byte(pCell)==iFrom ){ |
| 2957 | put4byte(pCell, iTo); |
| 2958 | break; |
| 2959 | } |
| 2960 | } |
| 2961 | } |
| 2962 | |
| 2963 | if( i==nCell ){ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2964 | if( eType!=PTRMAP_BTREE || |
| 2965 | get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2966 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2967 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2968 | put4byte(&pPage->aData[pPage->hdrOffset+8], iTo); |
| 2969 | } |
| 2970 | |
| 2971 | pPage->isInit = isInitOrig; |
| 2972 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2973 | return SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2974 | } |
| 2975 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2976 | |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2977 | /* |
| 2978 | ** Move the open database page pDbPage to location iFreePage in the |
| 2979 | ** database. The pDbPage reference remains valid. |
drh | e64ca7b | 2009-07-16 18:21:17 +0000 | [diff] [blame] | 2980 | ** |
| 2981 | ** The isCommit flag indicates that there is no need to remember that |
| 2982 | ** the journal needs to be sync()ed before database page pDbPage->pgno |
| 2983 | ** can be written to. The caller has already promised not to write to that |
| 2984 | ** page. |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2985 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2986 | static int relocatePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2987 | BtShared *pBt, /* Btree */ |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2988 | MemPage *pDbPage, /* Open page to move */ |
| 2989 | u8 eType, /* Pointer map 'type' entry for pDbPage */ |
| 2990 | Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */ |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 2991 | Pgno iFreePage, /* The location to move pDbPage to */ |
drh | e64ca7b | 2009-07-16 18:21:17 +0000 | [diff] [blame] | 2992 | int isCommit /* isCommit flag passed to sqlite3PagerMovepage */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2993 | ){ |
| 2994 | MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */ |
| 2995 | Pgno iDbPage = pDbPage->pgno; |
| 2996 | Pager *pPager = pBt->pPager; |
| 2997 | int rc; |
| 2998 | |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2999 | assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || |
| 3000 | eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3001 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 3002 | assert( pDbPage->pBt==pBt ); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 3003 | |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 3004 | /* Move page iDbPage from its current location to page number iFreePage */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 3005 | TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", |
| 3006 | iDbPage, iFreePage, iPtrPage, eType)); |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 3007 | rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 3008 | if( rc!=SQLITE_OK ){ |
| 3009 | return rc; |
| 3010 | } |
| 3011 | pDbPage->pgno = iFreePage; |
| 3012 | |
| 3013 | /* If pDbPage was a btree-page, then it may have child pages and/or cells |
| 3014 | ** that point to overflow pages. The pointer map entries for all these |
| 3015 | ** pages need to be changed. |
| 3016 | ** |
| 3017 | ** If pDbPage is an overflow page, then the first 4 bytes may store a |
| 3018 | ** pointer to a subsequent overflow page. If this is the case, then |
| 3019 | ** the pointer map needs to be updated for the subsequent overflow page. |
| 3020 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3021 | if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 3022 | rc = setChildPtrmaps(pDbPage); |
| 3023 | if( rc!=SQLITE_OK ){ |
| 3024 | return rc; |
| 3025 | } |
| 3026 | }else{ |
| 3027 | Pgno nextOvfl = get4byte(pDbPage->aData); |
| 3028 | if( nextOvfl!=0 ){ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 3029 | ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage, &rc); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 3030 | if( rc!=SQLITE_OK ){ |
| 3031 | return rc; |
| 3032 | } |
| 3033 | } |
| 3034 | } |
| 3035 | |
| 3036 | /* Fix the database pointer on page iPtrPage that pointed at iDbPage so |
| 3037 | ** that it points at iFreePage. Also fix the pointer map entry for |
| 3038 | ** iPtrPage. |
| 3039 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3040 | if( eType!=PTRMAP_ROOTPAGE ){ |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 3041 | rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3042 | if( rc!=SQLITE_OK ){ |
| 3043 | return rc; |
| 3044 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3045 | rc = sqlite3PagerWrite(pPtrPage->pDbPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 3046 | if( rc!=SQLITE_OK ){ |
| 3047 | releasePage(pPtrPage); |
| 3048 | return rc; |
| 3049 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 3050 | rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 3051 | releasePage(pPtrPage); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 3052 | if( rc==SQLITE_OK ){ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 3053 | ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 3054 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 3055 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 3056 | return rc; |
| 3057 | } |
| 3058 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3059 | /* Forward declaration required by incrVacuumStep(). */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 3060 | static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3061 | |
| 3062 | /* |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3063 | ** Perform a single step of an incremental-vacuum. If successful, return |
| 3064 | ** SQLITE_OK. If there is no work to do (and therefore no point in |
| 3065 | ** calling this function again), return SQLITE_DONE. Or, if an error |
| 3066 | ** occurs, return some other error code. |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3067 | ** |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 3068 | ** More specifically, this function attempts to re-organize the database so |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3069 | ** 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] | 3070 | ** |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3071 | ** Parameter nFin is the number of pages that this database would contain |
| 3072 | ** were this function called until it returns SQLITE_DONE. |
| 3073 | ** |
| 3074 | ** If the bCommit parameter is non-zero, this function assumes that the |
| 3075 | ** caller will keep calling incrVacuumStep() until it returns SQLITE_DONE |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 3076 | ** or an error. bCommit is passed true for an auto-vacuum-on-commit |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3077 | ** operation, or false for an incremental vacuum. |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3078 | */ |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3079 | static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg, int bCommit){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3080 | Pgno nFreeList; /* Number of pages still on the free-list */ |
drh | dd3cd97 | 2010-03-27 17:12:36 +0000 | [diff] [blame] | 3081 | int rc; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3082 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3083 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | fa542f1 | 2009-04-02 18:28:08 +0000 | [diff] [blame] | 3084 | assert( iLastPg>nFin ); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3085 | |
| 3086 | if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3087 | u8 eType; |
| 3088 | Pgno iPtrPage; |
| 3089 | |
| 3090 | nFreeList = get4byte(&pBt->pPage1->aData[36]); |
danielk1977 | fa542f1 | 2009-04-02 18:28:08 +0000 | [diff] [blame] | 3091 | if( nFreeList==0 ){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3092 | return SQLITE_DONE; |
| 3093 | } |
| 3094 | |
| 3095 | rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage); |
| 3096 | if( rc!=SQLITE_OK ){ |
| 3097 | return rc; |
| 3098 | } |
| 3099 | if( eType==PTRMAP_ROOTPAGE ){ |
| 3100 | return SQLITE_CORRUPT_BKPT; |
| 3101 | } |
| 3102 | |
| 3103 | if( eType==PTRMAP_FREEPAGE ){ |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3104 | if( bCommit==0 ){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3105 | /* Remove the page from the files free-list. This is not required |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3106 | ** if bCommit is non-zero. In that case, the free-list will be |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3107 | ** truncated to zero after this function returns, so it doesn't |
| 3108 | ** matter if it still contains some garbage entries. |
| 3109 | */ |
| 3110 | Pgno iFreePg; |
| 3111 | MemPage *pFreePg; |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3112 | rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, BTALLOC_EXACT); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3113 | if( rc!=SQLITE_OK ){ |
| 3114 | return rc; |
| 3115 | } |
| 3116 | assert( iFreePg==iLastPg ); |
| 3117 | releasePage(pFreePg); |
| 3118 | } |
| 3119 | } else { |
| 3120 | Pgno iFreePg; /* Index of free page to move pLastPg to */ |
| 3121 | MemPage *pLastPg; |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3122 | u8 eMode = BTALLOC_ANY; /* Mode parameter for allocateBtreePage() */ |
| 3123 | Pgno iNear = 0; /* nearby parameter for allocateBtreePage() */ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3124 | |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 3125 | rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3126 | if( rc!=SQLITE_OK ){ |
| 3127 | return rc; |
| 3128 | } |
| 3129 | |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3130 | /* If bCommit is zero, this loop runs exactly once and page pLastPg |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 3131 | ** is swapped with the first free page pulled off the free list. |
| 3132 | ** |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3133 | ** On the other hand, if bCommit is greater than zero, then keep |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 3134 | ** looping until a free-page located within the first nFin pages |
| 3135 | ** of the file is found. |
| 3136 | */ |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3137 | if( bCommit==0 ){ |
| 3138 | eMode = BTALLOC_LE; |
| 3139 | iNear = nFin; |
| 3140 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3141 | do { |
| 3142 | MemPage *pFreePg; |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3143 | rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iNear, eMode); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3144 | if( rc!=SQLITE_OK ){ |
| 3145 | releasePage(pLastPg); |
| 3146 | return rc; |
| 3147 | } |
| 3148 | releasePage(pFreePg); |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3149 | }while( bCommit && iFreePg>nFin ); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3150 | assert( iFreePg<iLastPg ); |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 3151 | |
dan | e1df4e3 | 2013-03-05 11:27:04 +0000 | [diff] [blame] | 3152 | rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, bCommit); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3153 | releasePage(pLastPg); |
| 3154 | if( rc!=SQLITE_OK ){ |
| 3155 | return rc; |
danielk1977 | 662278e | 2007-11-05 15:30:12 +0000 | [diff] [blame] | 3156 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3157 | } |
| 3158 | } |
| 3159 | |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3160 | if( bCommit==0 ){ |
dan | bc1a3c6 | 2013-02-23 16:40:46 +0000 | [diff] [blame] | 3161 | do { |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3162 | iLastPg--; |
dan | bc1a3c6 | 2013-02-23 16:40:46 +0000 | [diff] [blame] | 3163 | }while( iLastPg==PENDING_BYTE_PAGE(pBt) || PTRMAP_ISPAGE(pBt, iLastPg) ); |
| 3164 | pBt->bDoTruncate = 1; |
drh | dd3cd97 | 2010-03-27 17:12:36 +0000 | [diff] [blame] | 3165 | pBt->nPage = iLastPg; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3166 | } |
| 3167 | return SQLITE_OK; |
| 3168 | } |
| 3169 | |
| 3170 | /* |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3171 | ** The database opened by the first argument is an auto-vacuum database |
| 3172 | ** nOrig pages in size containing nFree free pages. Return the expected |
| 3173 | ** size of the database in pages following an auto-vacuum operation. |
| 3174 | */ |
| 3175 | static Pgno finalDbSize(BtShared *pBt, Pgno nOrig, Pgno nFree){ |
| 3176 | int nEntry; /* Number of entries on one ptrmap page */ |
| 3177 | Pgno nPtrmap; /* Number of PtrMap pages to be freed */ |
| 3178 | Pgno nFin; /* Return value */ |
| 3179 | |
| 3180 | nEntry = pBt->usableSize/5; |
| 3181 | nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry; |
| 3182 | nFin = nOrig - nFree - nPtrmap; |
| 3183 | if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){ |
| 3184 | nFin--; |
| 3185 | } |
| 3186 | while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){ |
| 3187 | nFin--; |
| 3188 | } |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3189 | |
| 3190 | return nFin; |
| 3191 | } |
| 3192 | |
| 3193 | /* |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3194 | ** A write-transaction must be opened before calling this function. |
| 3195 | ** It performs a single unit of work towards an incremental vacuum. |
| 3196 | ** |
| 3197 | ** If the incremental vacuum is finished after this function has run, |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 3198 | ** SQLITE_DONE is returned. If it is not finished, but no error occurred, |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3199 | ** SQLITE_OK is returned. Otherwise an SQLite error code. |
| 3200 | */ |
| 3201 | int sqlite3BtreeIncrVacuum(Btree *p){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3202 | int rc; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3203 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3204 | |
| 3205 | sqlite3BtreeEnter(p); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3206 | assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE ); |
| 3207 | if( !pBt->autoVacuum ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3208 | rc = SQLITE_DONE; |
| 3209 | }else{ |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3210 | Pgno nOrig = btreePagecount(pBt); |
| 3211 | Pgno nFree = get4byte(&pBt->pPage1->aData[36]); |
| 3212 | Pgno nFin = finalDbSize(pBt, nOrig, nFree); |
| 3213 | |
dan | 9138471 | 2013-02-24 11:50:43 +0000 | [diff] [blame] | 3214 | if( nOrig<nFin ){ |
| 3215 | rc = SQLITE_CORRUPT_BKPT; |
| 3216 | }else if( nFree>0 ){ |
dan | 11dcd11 | 2013-03-15 18:29:18 +0000 | [diff] [blame] | 3217 | rc = saveAllCursors(pBt, 0, 0); |
| 3218 | if( rc==SQLITE_OK ){ |
| 3219 | invalidateAllOverflowCache(pBt); |
| 3220 | rc = incrVacuumStep(pBt, nFin, nOrig, 0); |
| 3221 | } |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3222 | if( rc==SQLITE_OK ){ |
| 3223 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 3224 | put4byte(&pBt->pPage1->aData[28], pBt->nPage); |
| 3225 | } |
| 3226 | }else{ |
| 3227 | rc = SQLITE_DONE; |
drh | dd3cd97 | 2010-03-27 17:12:36 +0000 | [diff] [blame] | 3228 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3229 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3230 | sqlite3BtreeLeave(p); |
| 3231 | return rc; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3232 | } |
| 3233 | |
| 3234 | /* |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3235 | ** This routine is called prior to sqlite3PagerCommit when a transaction |
drh | f7b5496 | 2013-05-28 12:11:54 +0000 | [diff] [blame] | 3236 | ** is committed for an auto-vacuum database. |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 3237 | ** |
| 3238 | ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages |
| 3239 | ** the database file should be truncated to during the commit process. |
| 3240 | ** i.e. the database has been reorganized so that only the first *pnTrunc |
| 3241 | ** pages are in use. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3242 | */ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3243 | static int autoVacuumCommit(BtShared *pBt){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3244 | int rc = SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3245 | Pager *pPager = pBt->pPager; |
drh | f94a173 | 2008-09-30 17:18:17 +0000 | [diff] [blame] | 3246 | VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3247 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3248 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 3249 | invalidateAllOverflowCache(pBt); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3250 | assert(pBt->autoVacuum); |
| 3251 | if( !pBt->incrVacuum ){ |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 3252 | Pgno nFin; /* Number of pages in database after autovacuuming */ |
| 3253 | Pgno nFree; /* Number of pages on the freelist initially */ |
drh | 41d628c | 2009-07-11 17:04:08 +0000 | [diff] [blame] | 3254 | Pgno iFree; /* The next page to be freed */ |
drh | 41d628c | 2009-07-11 17:04:08 +0000 | [diff] [blame] | 3255 | Pgno nOrig; /* Database size before freeing */ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3256 | |
drh | b129915 | 2010-03-30 22:58:33 +0000 | [diff] [blame] | 3257 | nOrig = btreePagecount(pBt); |
danielk1977 | ef165ce | 2009-04-06 17:50:03 +0000 | [diff] [blame] | 3258 | if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){ |
| 3259 | /* It is not possible to create a database for which the final page |
| 3260 | ** is either a pointer-map page or the pending-byte page. If one |
| 3261 | ** is encountered, this indicates corruption. |
| 3262 | */ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3263 | return SQLITE_CORRUPT_BKPT; |
| 3264 | } |
danielk1977 | ef165ce | 2009-04-06 17:50:03 +0000 | [diff] [blame] | 3265 | |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3266 | nFree = get4byte(&pBt->pPage1->aData[36]); |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3267 | nFin = finalDbSize(pBt, nOrig, nFree); |
drh | c5e47ac | 2009-06-04 00:11:56 +0000 | [diff] [blame] | 3268 | if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT; |
dan | 0aed84d | 2013-03-26 14:16:20 +0000 | [diff] [blame] | 3269 | if( nFin<nOrig ){ |
| 3270 | rc = saveAllCursors(pBt, 0, 0); |
| 3271 | } |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3272 | for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){ |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 3273 | rc = incrVacuumStep(pBt, nFin, iFree, 1); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3274 | } |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3275 | if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3276 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 3277 | put4byte(&pBt->pPage1->aData[32], 0); |
| 3278 | put4byte(&pBt->pPage1->aData[36], 0); |
drh | dd3cd97 | 2010-03-27 17:12:36 +0000 | [diff] [blame] | 3279 | put4byte(&pBt->pPage1->aData[28], nFin); |
dan | bc1a3c6 | 2013-02-23 16:40:46 +0000 | [diff] [blame] | 3280 | pBt->bDoTruncate = 1; |
drh | dd3cd97 | 2010-03-27 17:12:36 +0000 | [diff] [blame] | 3281 | pBt->nPage = nFin; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3282 | } |
| 3283 | if( rc!=SQLITE_OK ){ |
| 3284 | sqlite3PagerRollback(pPager); |
| 3285 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3286 | } |
| 3287 | |
dan | 0aed84d | 2013-03-26 14:16:20 +0000 | [diff] [blame] | 3288 | assert( nRef>=sqlite3PagerRefcount(pPager) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3289 | return rc; |
| 3290 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3291 | |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 3292 | #else /* ifndef SQLITE_OMIT_AUTOVACUUM */ |
| 3293 | # define setChildPtrmaps(x) SQLITE_OK |
| 3294 | #endif |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3295 | |
| 3296 | /* |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3297 | ** This routine does the first phase of a two-phase commit. This routine |
| 3298 | ** causes a rollback journal to be created (if it does not already exist) |
| 3299 | ** and populated with enough information so that if a power loss occurs |
| 3300 | ** the database can be restored to its original state by playing back |
| 3301 | ** the journal. Then the contents of the journal are flushed out to |
| 3302 | ** the disk. After the journal is safely on oxide, the changes to the |
| 3303 | ** database are written into the database file and flushed to oxide. |
| 3304 | ** At the end of this call, the rollback journal still exists on the |
| 3305 | ** disk and we are still holding all locks, so the transaction has not |
drh | 51898cf | 2009-04-19 20:51:06 +0000 | [diff] [blame] | 3306 | ** committed. See sqlite3BtreeCommitPhaseTwo() for the second phase of the |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3307 | ** commit process. |
| 3308 | ** |
| 3309 | ** This call is a no-op if no write-transaction is currently active on pBt. |
| 3310 | ** |
| 3311 | ** Otherwise, sync the database file for the btree pBt. zMaster points to |
| 3312 | ** the name of a master journal file that should be written into the |
| 3313 | ** individual journal file, or is NULL, indicating no master journal file |
| 3314 | ** (single database transaction). |
| 3315 | ** |
| 3316 | ** When this is called, the master journal should already have been |
| 3317 | ** created, populated with this journal pointer and synced to disk. |
| 3318 | ** |
| 3319 | ** Once this is routine has returned, the only thing required to commit |
| 3320 | ** the write-transaction for this database file is to delete the journal. |
| 3321 | */ |
| 3322 | int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){ |
| 3323 | int rc = SQLITE_OK; |
| 3324 | if( p->inTrans==TRANS_WRITE ){ |
| 3325 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3326 | sqlite3BtreeEnter(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3327 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3328 | if( pBt->autoVacuum ){ |
danielk1977 | 3460d19 | 2008-12-27 15:23:13 +0000 | [diff] [blame] | 3329 | rc = autoVacuumCommit(pBt); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3330 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3331 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3332 | return rc; |
| 3333 | } |
| 3334 | } |
dan | bc1a3c6 | 2013-02-23 16:40:46 +0000 | [diff] [blame] | 3335 | if( pBt->bDoTruncate ){ |
| 3336 | sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage); |
| 3337 | } |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3338 | #endif |
drh | 49b9d33 | 2009-01-02 18:10:42 +0000 | [diff] [blame] | 3339 | rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3340 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3341 | } |
| 3342 | return rc; |
| 3343 | } |
| 3344 | |
| 3345 | /* |
danielk1977 | 94b3073 | 2009-07-02 17:21:57 +0000 | [diff] [blame] | 3346 | ** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback() |
| 3347 | ** at the conclusion of a transaction. |
| 3348 | */ |
| 3349 | static void btreeEndTransaction(Btree *p){ |
| 3350 | BtShared *pBt = p->pBt; |
drh | 1713afb | 2013-06-28 01:24:57 +0000 | [diff] [blame] | 3351 | sqlite3 *db = p->db; |
danielk1977 | 94b3073 | 2009-07-02 17:21:57 +0000 | [diff] [blame] | 3352 | assert( sqlite3BtreeHoldsMutex(p) ); |
| 3353 | |
dan | bc1a3c6 | 2013-02-23 16:40:46 +0000 | [diff] [blame] | 3354 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3355 | pBt->bDoTruncate = 0; |
| 3356 | #endif |
dan | c0537fe | 2013-06-28 19:41:43 +0000 | [diff] [blame] | 3357 | if( p->inTrans>TRANS_NONE && db->nVdbeRead>1 ){ |
dan | fa401de | 2009-10-16 14:55:03 +0000 | [diff] [blame] | 3358 | /* If there are other active statements that belong to this database |
| 3359 | ** handle, downgrade to a read-only transaction. The other statements |
| 3360 | ** may still be reading from the database. */ |
danielk1977 | 94b3073 | 2009-07-02 17:21:57 +0000 | [diff] [blame] | 3361 | downgradeAllSharedCacheTableLocks(p); |
| 3362 | p->inTrans = TRANS_READ; |
| 3363 | }else{ |
| 3364 | /* If the handle had any kind of transaction open, decrement the |
| 3365 | ** transaction count of the shared btree. If the transaction count |
| 3366 | ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused() |
| 3367 | ** call below will unlock the pager. */ |
| 3368 | if( p->inTrans!=TRANS_NONE ){ |
| 3369 | clearAllSharedCacheTableLocks(p); |
| 3370 | pBt->nTransaction--; |
| 3371 | if( 0==pBt->nTransaction ){ |
| 3372 | pBt->inTransaction = TRANS_NONE; |
| 3373 | } |
| 3374 | } |
| 3375 | |
| 3376 | /* Set the current transaction state to TRANS_NONE and unlock the |
| 3377 | ** pager if this call closed the only read or write transaction. */ |
| 3378 | p->inTrans = TRANS_NONE; |
| 3379 | unlockBtreeIfUnused(pBt); |
| 3380 | } |
| 3381 | |
| 3382 | btreeIntegrity(p); |
| 3383 | } |
| 3384 | |
| 3385 | /* |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3386 | ** Commit the transaction currently in progress. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3387 | ** |
drh | 6e34599 | 2007-03-30 11:12:08 +0000 | [diff] [blame] | 3388 | ** This routine implements the second phase of a 2-phase commit. The |
drh | 51898cf | 2009-04-19 20:51:06 +0000 | [diff] [blame] | 3389 | ** sqlite3BtreeCommitPhaseOne() routine does the first phase and should |
| 3390 | ** be invoked prior to calling this routine. The sqlite3BtreeCommitPhaseOne() |
| 3391 | ** 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] | 3392 | ** contents so that they are written onto the disk platter. All this |
drh | 51898cf | 2009-04-19 20:51:06 +0000 | [diff] [blame] | 3393 | ** routine has to do is delete or truncate or zero the header in the |
| 3394 | ** the rollback journal (which causes the transaction to commit) and |
| 3395 | ** drop locks. |
drh | 6e34599 | 2007-03-30 11:12:08 +0000 | [diff] [blame] | 3396 | ** |
dan | 60939d0 | 2011-03-29 15:40:55 +0000 | [diff] [blame] | 3397 | ** Normally, if an error occurs while the pager layer is attempting to |
| 3398 | ** finalize the underlying journal file, this function returns an error and |
| 3399 | ** the upper layer will attempt a rollback. However, if the second argument |
| 3400 | ** is non-zero then this b-tree transaction is part of a multi-file |
| 3401 | ** transaction. In this case, the transaction has already been committed |
| 3402 | ** (by deleting a master journal file) and the caller will ignore this |
| 3403 | ** functions return code. So, even if an error occurs in the pager layer, |
| 3404 | ** reset the b-tree objects internal state to indicate that the write |
| 3405 | ** transaction has been closed. This is quite safe, as the pager will have |
| 3406 | ** transitioned to the error state. |
| 3407 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3408 | ** This will release the write lock on the database file. If there |
| 3409 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3410 | */ |
dan | 60939d0 | 2011-03-29 15:40:55 +0000 | [diff] [blame] | 3411 | int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3412 | |
drh | 075ed30 | 2010-10-14 01:17:30 +0000 | [diff] [blame] | 3413 | if( p->inTrans==TRANS_NONE ) return SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3414 | sqlite3BtreeEnter(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3415 | btreeIntegrity(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3416 | |
| 3417 | /* If the handle has a write-transaction open, commit the shared-btrees |
| 3418 | ** transaction and set the shared state to TRANS_READ. |
| 3419 | */ |
| 3420 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 3421 | int rc; |
drh | 075ed30 | 2010-10-14 01:17:30 +0000 | [diff] [blame] | 3422 | BtShared *pBt = p->pBt; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3423 | assert( pBt->inTransaction==TRANS_WRITE ); |
| 3424 | assert( pBt->nTransaction>0 ); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3425 | rc = sqlite3PagerCommitPhaseTwo(pBt->pPager); |
dan | 60939d0 | 2011-03-29 15:40:55 +0000 | [diff] [blame] | 3426 | if( rc!=SQLITE_OK && bCleanup==0 ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3427 | sqlite3BtreeLeave(p); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 3428 | return rc; |
| 3429 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3430 | pBt->inTransaction = TRANS_READ; |
dan | bf0e57a | 2013-05-14 20:36:31 +0000 | [diff] [blame] | 3431 | btreeClearHasContent(pBt); |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 3432 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3433 | |
danielk1977 | 94b3073 | 2009-07-02 17:21:57 +0000 | [diff] [blame] | 3434 | btreeEndTransaction(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3435 | sqlite3BtreeLeave(p); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 3436 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3437 | } |
| 3438 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3439 | /* |
| 3440 | ** Do both phases of a commit. |
| 3441 | */ |
| 3442 | int sqlite3BtreeCommit(Btree *p){ |
| 3443 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3444 | sqlite3BtreeEnter(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3445 | rc = sqlite3BtreeCommitPhaseOne(p, 0); |
| 3446 | if( rc==SQLITE_OK ){ |
dan | 60939d0 | 2011-03-29 15:40:55 +0000 | [diff] [blame] | 3447 | rc = sqlite3BtreeCommitPhaseTwo(p, 0); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3448 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3449 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 3450 | return rc; |
| 3451 | } |
| 3452 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3453 | /* |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 3454 | ** This routine sets the state to CURSOR_FAULT and the error |
| 3455 | ** code to errCode for every cursor on BtShared that pBtree |
| 3456 | ** references. |
| 3457 | ** |
| 3458 | ** Every cursor is tripped, including cursors that belong |
| 3459 | ** to other database connections that happen to be sharing |
| 3460 | ** the cache with pBtree. |
| 3461 | ** |
| 3462 | ** This routine gets called when a rollback occurs. |
| 3463 | ** All cursors using the same cache must be tripped |
| 3464 | ** to prevent them from trying to use the btree after |
| 3465 | ** the rollback. The rollback may have deleted tables |
| 3466 | ** or moved root pages, so it is not sufficient to |
| 3467 | ** save the state of the cursor. The cursor must be |
| 3468 | ** invalidated. |
| 3469 | */ |
| 3470 | void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){ |
| 3471 | BtCursor *p; |
drh | 0f198a7 | 2012-02-13 16:43:16 +0000 | [diff] [blame] | 3472 | if( pBtree==0 ) return; |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 3473 | sqlite3BtreeEnter(pBtree); |
| 3474 | for(p=pBtree->pBt->pCursor; p; p=p->pNext){ |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 3475 | int i; |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 3476 | sqlite3BtreeClearCursor(p); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 3477 | p->eState = CURSOR_FAULT; |
drh | 4c301aa | 2009-07-15 17:25:45 +0000 | [diff] [blame] | 3478 | p->skipNext = errCode; |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 3479 | for(i=0; i<=p->iPage; i++){ |
| 3480 | releasePage(p->apPage[i]); |
| 3481 | p->apPage[i] = 0; |
| 3482 | } |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 3483 | } |
| 3484 | sqlite3BtreeLeave(pBtree); |
| 3485 | } |
| 3486 | |
| 3487 | /* |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3488 | ** Rollback the transaction in progress. All cursors will be |
| 3489 | ** invalided by this operation. Any attempt to use a cursor |
| 3490 | ** that was open at the beginning of this operation will result |
| 3491 | ** in an error. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3492 | ** |
| 3493 | ** This will release the write lock on the database file. If there |
| 3494 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3495 | */ |
drh | 0f198a7 | 2012-02-13 16:43:16 +0000 | [diff] [blame] | 3496 | int sqlite3BtreeRollback(Btree *p, int tripCode){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 3497 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3498 | BtShared *pBt = p->pBt; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3499 | MemPage *pPage1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3500 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3501 | sqlite3BtreeEnter(p); |
drh | 0f198a7 | 2012-02-13 16:43:16 +0000 | [diff] [blame] | 3502 | if( tripCode==SQLITE_OK ){ |
| 3503 | rc = tripCode = saveAllCursors(pBt, 0, 0); |
| 3504 | }else{ |
| 3505 | rc = SQLITE_OK; |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 3506 | } |
drh | 0f198a7 | 2012-02-13 16:43:16 +0000 | [diff] [blame] | 3507 | if( tripCode ){ |
| 3508 | sqlite3BtreeTripAllCursors(p, tripCode); |
| 3509 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3510 | btreeIntegrity(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3511 | |
| 3512 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 3513 | int rc2; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3514 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 3515 | assert( TRANS_WRITE==pBt->inTransaction ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3516 | rc2 = sqlite3PagerRollback(pBt->pPager); |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 3517 | if( rc2!=SQLITE_OK ){ |
| 3518 | rc = rc2; |
| 3519 | } |
| 3520 | |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3521 | /* The rollback may have destroyed the pPage1->aData value. So |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 3522 | ** call btreeGetPage() on page 1 again to make |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3523 | ** sure pPage1->aData is set correctly. */ |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 3524 | if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){ |
drh | 1f5b467 | 2010-04-01 02:22:19 +0000 | [diff] [blame] | 3525 | int nPage = get4byte(28+(u8*)pPage1->aData); |
| 3526 | testcase( nPage==0 ); |
| 3527 | if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 3528 | testcase( pBt->nPage!=nPage ); |
| 3529 | pBt->nPage = nPage; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3530 | releasePage(pPage1); |
| 3531 | } |
drh | 85ec3b6 | 2013-05-14 23:12:06 +0000 | [diff] [blame] | 3532 | assert( countValidCursors(pBt, 1)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3533 | pBt->inTransaction = TRANS_READ; |
dan | bf0e57a | 2013-05-14 20:36:31 +0000 | [diff] [blame] | 3534 | btreeClearHasContent(pBt); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3535 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3536 | |
danielk1977 | 94b3073 | 2009-07-02 17:21:57 +0000 | [diff] [blame] | 3537 | btreeEndTransaction(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3538 | sqlite3BtreeLeave(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3539 | return rc; |
| 3540 | } |
| 3541 | |
| 3542 | /* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 3543 | ** Start a statement subtransaction. The subtransaction can be rolled |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 3544 | ** back independently of the main transaction. You must start a transaction |
| 3545 | ** before starting a subtransaction. The subtransaction is ended automatically |
| 3546 | ** if the main transaction commits or rolls back. |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 3547 | ** |
| 3548 | ** Statement subtransactions are used around individual SQL statements |
| 3549 | ** that are contained within a BEGIN...COMMIT block. If a constraint |
| 3550 | ** error occurs within the statement, the effect of that one statement |
| 3551 | ** can be rolled back without having to rollback the entire transaction. |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 3552 | ** |
| 3553 | ** A statement sub-transaction is implemented as an anonymous savepoint. The |
| 3554 | ** value passed as the second parameter is the total number of savepoints, |
| 3555 | ** including the new anonymous savepoint, open on the B-Tree. i.e. if there |
| 3556 | ** are no active savepoints and no other statement-transactions open, |
| 3557 | ** iStatement is 1. This anonymous savepoint can be released or rolled back |
| 3558 | ** using the sqlite3BtreeSavepoint() function. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 3559 | */ |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 3560 | int sqlite3BtreeBeginStmt(Btree *p, int iStatement){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 3561 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3562 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3563 | sqlite3BtreeEnter(p); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 3564 | assert( p->inTrans==TRANS_WRITE ); |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 3565 | assert( (pBt->btsFlags & BTS_READ_ONLY)==0 ); |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 3566 | assert( iStatement>0 ); |
| 3567 | assert( iStatement>p->db->nSavepoint ); |
drh | 5e0ccc2 | 2010-03-29 19:36:52 +0000 | [diff] [blame] | 3568 | assert( pBt->inTransaction==TRANS_WRITE ); |
| 3569 | /* At the pager level, a statement transaction is a savepoint with |
| 3570 | ** an index greater than all savepoints created explicitly using |
| 3571 | ** SQL statements. It is illegal to open, release or rollback any |
| 3572 | ** such savepoints while the statement transaction savepoint is active. |
| 3573 | */ |
| 3574 | rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3575 | sqlite3BtreeLeave(p); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 3576 | return rc; |
| 3577 | } |
| 3578 | |
| 3579 | /* |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3580 | ** The second argument to this function, op, is always SAVEPOINT_ROLLBACK |
| 3581 | ** or SAVEPOINT_RELEASE. This function either releases or rolls back the |
danielk1977 | 12dd549 | 2008-12-18 15:45:07 +0000 | [diff] [blame] | 3582 | ** savepoint identified by parameter iSavepoint, depending on the value |
| 3583 | ** of op. |
| 3584 | ** |
| 3585 | ** Normally, iSavepoint is greater than or equal to zero. However, if op is |
| 3586 | ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the |
| 3587 | ** contents of the entire transaction are rolled back. This is different |
| 3588 | ** from a normal transaction rollback, as no locks are released and the |
| 3589 | ** transaction remains open. |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3590 | */ |
| 3591 | int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){ |
| 3592 | int rc = SQLITE_OK; |
| 3593 | if( p && p->inTrans==TRANS_WRITE ){ |
| 3594 | BtShared *pBt = p->pBt; |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3595 | assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK ); |
| 3596 | assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) ); |
| 3597 | sqlite3BtreeEnter(p); |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3598 | rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint); |
drh | 9f0bbf9 | 2009-01-02 21:08:09 +0000 | [diff] [blame] | 3599 | if( rc==SQLITE_OK ){ |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 3600 | if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){ |
| 3601 | pBt->nPage = 0; |
| 3602 | } |
drh | 9f0bbf9 | 2009-01-02 21:08:09 +0000 | [diff] [blame] | 3603 | rc = newDatabase(pBt); |
drh | dd3cd97 | 2010-03-27 17:12:36 +0000 | [diff] [blame] | 3604 | pBt->nPage = get4byte(28 + pBt->pPage1->aData); |
drh | b9b49bf | 2010-08-05 03:21:39 +0000 | [diff] [blame] | 3605 | |
| 3606 | /* The database size was written into the offset 28 of the header |
| 3607 | ** when the transaction started, so we know that the value at offset |
| 3608 | ** 28 is nonzero. */ |
| 3609 | assert( pBt->nPage>0 ); |
drh | 9f0bbf9 | 2009-01-02 21:08:09 +0000 | [diff] [blame] | 3610 | } |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 3611 | sqlite3BtreeLeave(p); |
| 3612 | } |
| 3613 | return rc; |
| 3614 | } |
| 3615 | |
| 3616 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3617 | ** Create a new cursor for the BTree whose root is on the page |
danielk1977 | 3e8add9 | 2009-07-04 17:16:00 +0000 | [diff] [blame] | 3618 | ** iTable. If a read-only cursor is requested, it is assumed that |
| 3619 | ** the caller already has at least a read-only transaction open |
| 3620 | ** on the database already. If a write-cursor is requested, then |
| 3621 | ** the caller is assumed to have an open write transaction. |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 3622 | ** |
| 3623 | ** If wrFlag==0, then the cursor can only be used for reading. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3624 | ** If wrFlag==1, then the cursor can be used for reading or for |
| 3625 | ** writing if other conditions for writing are also met. These |
| 3626 | ** are the conditions that must be met in order for writing to |
| 3627 | ** be allowed: |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 3628 | ** |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3629 | ** 1: The cursor must have been opened with wrFlag==1 |
| 3630 | ** |
drh | fe5d71d | 2007-03-19 11:54:10 +0000 | [diff] [blame] | 3631 | ** 2: Other database connections that share the same pager cache |
| 3632 | ** but which are not in the READ_UNCOMMITTED state may not have |
| 3633 | ** cursors open with wrFlag==0 on the same table. Otherwise |
| 3634 | ** the changes made by this write cursor would be visible to |
| 3635 | ** the read cursors in the other database connection. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3636 | ** |
| 3637 | ** 3: The database must be writable (not on read-only media) |
| 3638 | ** |
| 3639 | ** 4: There must be an active transaction. |
| 3640 | ** |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 3641 | ** No checking is done to make sure that page iTable really is the |
| 3642 | ** root page of a b-tree. If it is not, then the cursor acquired |
| 3643 | ** will not work correctly. |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3644 | ** |
drh | f25a507 | 2009-11-18 23:01:25 +0000 | [diff] [blame] | 3645 | ** It is assumed that the sqlite3BtreeCursorZero() has been called |
| 3646 | ** on pCur to initialize the memory space prior to invoking this routine. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3647 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3648 | static int btreeCursor( |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3649 | Btree *p, /* The btree */ |
| 3650 | int iTable, /* Root page of table to open */ |
| 3651 | int wrFlag, /* 1 to write. 0 read-only */ |
| 3652 | struct KeyInfo *pKeyInfo, /* First arg to comparison function */ |
| 3653 | BtCursor *pCur /* Space for new cursor */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3654 | ){ |
danielk1977 | 3e8add9 | 2009-07-04 17:16:00 +0000 | [diff] [blame] | 3655 | BtShared *pBt = p->pBt; /* Shared b-tree handle */ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3656 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3657 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 3658 | assert( wrFlag==0 || wrFlag==1 ); |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 3659 | |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 3660 | /* The following assert statements verify that if this is a sharable |
| 3661 | ** b-tree database, the connection is holding the required table locks, |
| 3662 | ** and that no other connection has any open cursor that conflicts with |
| 3663 | ** this lock. */ |
| 3664 | assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, wrFlag+1) ); |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 3665 | assert( wrFlag==0 || !hasReadConflicts(p, iTable) ); |
| 3666 | |
danielk1977 | 3e8add9 | 2009-07-04 17:16:00 +0000 | [diff] [blame] | 3667 | /* Assert that the caller has opened the required transaction. */ |
| 3668 | assert( p->inTrans>TRANS_NONE ); |
| 3669 | assert( wrFlag==0 || p->inTrans==TRANS_WRITE ); |
| 3670 | assert( pBt->pPage1 && pBt->pPage1->aData ); |
| 3671 | |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 3672 | if( NEVER(wrFlag && (pBt->btsFlags & BTS_READ_ONLY)!=0) ){ |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 3673 | return SQLITE_READONLY; |
drh | a0c9a11 | 2004-03-10 13:42:37 +0000 | [diff] [blame] | 3674 | } |
drh | b129915 | 2010-03-30 22:58:33 +0000 | [diff] [blame] | 3675 | if( iTable==1 && btreePagecount(pBt)==0 ){ |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 3676 | assert( wrFlag==0 ); |
| 3677 | iTable = 0; |
danielk1977 | 3e8add9 | 2009-07-04 17:16:00 +0000 | [diff] [blame] | 3678 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3679 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3680 | /* Now that no other errors can occur, finish filling in the BtCursor |
danielk1977 | 3e8add9 | 2009-07-04 17:16:00 +0000 | [diff] [blame] | 3681 | ** variables and link the cursor into the BtShared list. */ |
danielk1977 | 172114a | 2009-07-07 15:47:12 +0000 | [diff] [blame] | 3682 | pCur->pgnoRoot = (Pgno)iTable; |
| 3683 | pCur->iPage = -1; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3684 | pCur->pKeyInfo = pKeyInfo; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3685 | pCur->pBtree = p; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 3686 | pCur->pBt = pBt; |
drh | 4c41718 | 2014-03-31 23:57:41 +0000 | [diff] [blame] | 3687 | assert( wrFlag==0 || wrFlag==BTCF_WriteFlag ); |
| 3688 | pCur->curFlags = wrFlag; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3689 | pCur->pNext = pBt->pCursor; |
| 3690 | if( pCur->pNext ){ |
| 3691 | pCur->pNext->pPrev = pCur; |
| 3692 | } |
| 3693 | pBt->pCursor = pCur; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3694 | pCur->eState = CURSOR_INVALID; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3695 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3696 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3697 | int sqlite3BtreeCursor( |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3698 | Btree *p, /* The btree */ |
| 3699 | int iTable, /* Root page of table to open */ |
| 3700 | int wrFlag, /* 1 to write. 0 read-only */ |
| 3701 | struct KeyInfo *pKeyInfo, /* First arg to xCompare() */ |
| 3702 | BtCursor *pCur /* Write new cursor here */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3703 | ){ |
| 3704 | int rc; |
| 3705 | sqlite3BtreeEnter(p); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3706 | rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3707 | sqlite3BtreeLeave(p); |
| 3708 | return rc; |
| 3709 | } |
drh | 7f75122 | 2009-03-17 22:33:00 +0000 | [diff] [blame] | 3710 | |
| 3711 | /* |
| 3712 | ** Return the size of a BtCursor object in bytes. |
| 3713 | ** |
| 3714 | ** This interfaces is needed so that users of cursors can preallocate |
| 3715 | ** sufficient storage to hold a cursor. The BtCursor object is opaque |
| 3716 | ** to users so they cannot do the sizeof() themselves - they must call |
| 3717 | ** this routine. |
| 3718 | */ |
| 3719 | int sqlite3BtreeCursorSize(void){ |
drh | c54055b | 2009-11-13 17:05:53 +0000 | [diff] [blame] | 3720 | return ROUND8(sizeof(BtCursor)); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3721 | } |
| 3722 | |
drh | 7f75122 | 2009-03-17 22:33:00 +0000 | [diff] [blame] | 3723 | /* |
drh | f25a507 | 2009-11-18 23:01:25 +0000 | [diff] [blame] | 3724 | ** Initialize memory that will be converted into a BtCursor object. |
| 3725 | ** |
| 3726 | ** The simple approach here would be to memset() the entire object |
| 3727 | ** to zero. But it turns out that the apPage[] and aiIdx[] arrays |
| 3728 | ** do not need to be zeroed and they are large, so we can save a lot |
| 3729 | ** of run-time by skipping the initialization of those elements. |
| 3730 | */ |
| 3731 | void sqlite3BtreeCursorZero(BtCursor *p){ |
| 3732 | memset(p, 0, offsetof(BtCursor, iPage)); |
| 3733 | } |
| 3734 | |
| 3735 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3736 | ** Close a cursor. The read lock on the database file is released |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3737 | ** when the last cursor is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3738 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3739 | int sqlite3BtreeCloseCursor(BtCursor *pCur){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 3740 | Btree *pBtree = pCur->pBtree; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3741 | if( pBtree ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3742 | int i; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3743 | BtShared *pBt = pCur->pBt; |
| 3744 | sqlite3BtreeEnter(pBtree); |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 3745 | sqlite3BtreeClearCursor(pCur); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3746 | if( pCur->pPrev ){ |
| 3747 | pCur->pPrev->pNext = pCur->pNext; |
| 3748 | }else{ |
| 3749 | pBt->pCursor = pCur->pNext; |
| 3750 | } |
| 3751 | if( pCur->pNext ){ |
| 3752 | pCur->pNext->pPrev = pCur->pPrev; |
| 3753 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3754 | for(i=0; i<=pCur->iPage; i++){ |
| 3755 | releasePage(pCur->apPage[i]); |
| 3756 | } |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3757 | unlockBtreeIfUnused(pBt); |
dan | 5a500af | 2014-03-11 20:33:04 +0000 | [diff] [blame] | 3758 | sqlite3DbFree(pBtree->db, pCur->aOverflow); |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 3759 | /* sqlite3_free(pCur); */ |
| 3760 | sqlite3BtreeLeave(pBtree); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3761 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3762 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3763 | } |
| 3764 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3765 | /* |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3766 | ** Make sure the BtCursor* given in the argument has a valid |
| 3767 | ** BtCursor.info structure. If it is not already valid, call |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 3768 | ** btreeParseCell() to fill it in. |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 3769 | ** |
| 3770 | ** BtCursor.info is a cache of the information in the current cell. |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 3771 | ** Using this cache reduces the number of calls to btreeParseCell(). |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3772 | ** |
| 3773 | ** 2007-06-25: There is a bug in some versions of MSVC that cause the |
| 3774 | ** compiler to crash when getCellInfo() is implemented as a macro. |
| 3775 | ** But there is a measureable speed advantage to using the macro on gcc |
| 3776 | ** (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] | 3777 | ** compiler is not doing aggressive inlining.) So we use a real function |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3778 | ** for MSVC and a macro for everything else. Ticket #2457. |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3779 | */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3780 | #ifndef NDEBUG |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3781 | static void assertCellInfo(BtCursor *pCur){ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3782 | CellInfo info; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3783 | int iPage = pCur->iPage; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 3784 | memset(&info, 0, sizeof(info)); |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 3785 | btreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info); |
dan | 7df42ab | 2014-01-20 18:25:44 +0000 | [diff] [blame] | 3786 | assert( CORRUPT_DB || memcmp(&info, &pCur->info, sizeof(info))==0 ); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3787 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3788 | #else |
| 3789 | #define assertCellInfo(x) |
| 3790 | #endif |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3791 | #ifdef _MSC_VER |
| 3792 | /* Use a real function in MSVC to work around bugs in that compiler. */ |
| 3793 | static void getCellInfo(BtCursor *pCur){ |
| 3794 | if( pCur->info.nSize==0 ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3795 | int iPage = pCur->iPage; |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 3796 | btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 3797 | pCur->curFlags |= BTCF_ValidNKey; |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3798 | }else{ |
| 3799 | assertCellInfo(pCur); |
| 3800 | } |
| 3801 | } |
| 3802 | #else /* if not _MSC_VER */ |
| 3803 | /* Use a macro in all other compilers so that the function is inlined */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3804 | #define getCellInfo(pCur) \ |
| 3805 | if( pCur->info.nSize==0 ){ \ |
| 3806 | int iPage = pCur->iPage; \ |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 3807 | btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); \ |
| 3808 | pCur->curFlags |= BTCF_ValidNKey; \ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 3809 | }else{ \ |
| 3810 | assertCellInfo(pCur); \ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3811 | } |
| 3812 | #endif /* _MSC_VER */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3813 | |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 3814 | #ifndef NDEBUG /* The next routine used only within assert() statements */ |
| 3815 | /* |
| 3816 | ** Return true if the given BtCursor is valid. A valid cursor is one |
| 3817 | ** that is currently pointing to a row in a (non-empty) table. |
| 3818 | ** This is a verification routine is used only within assert() statements. |
| 3819 | */ |
| 3820 | int sqlite3BtreeCursorIsValid(BtCursor *pCur){ |
| 3821 | return pCur && pCur->eState==CURSOR_VALID; |
| 3822 | } |
| 3823 | #endif /* NDEBUG */ |
| 3824 | |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3825 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3826 | ** Set *pSize to the size of the buffer needed to hold the value of |
| 3827 | ** the key for the current entry. If the cursor is not pointing |
| 3828 | ** to a valid entry, *pSize is set to 0. |
| 3829 | ** |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3830 | ** For a table with the INTKEY flag set, this routine returns the key |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3831 | ** itself, not the number of bytes in the key. |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 3832 | ** |
| 3833 | ** The caller must position the cursor prior to invoking this routine. |
| 3834 | ** |
| 3835 | ** This routine cannot fail. It always returns SQLITE_OK. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 3836 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3837 | int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3838 | assert( cursorHoldsMutex(pCur) ); |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 3839 | assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID ); |
| 3840 | if( pCur->eState!=CURSOR_VALID ){ |
| 3841 | *pSize = 0; |
| 3842 | }else{ |
| 3843 | getCellInfo(pCur); |
| 3844 | *pSize = pCur->info.nKey; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3845 | } |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 3846 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3847 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3848 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3849 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3850 | ** Set *pSize to the number of bytes of data in the entry the |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 3851 | ** cursor currently points to. |
| 3852 | ** |
| 3853 | ** The caller must guarantee that the cursor is pointing to a non-NULL |
| 3854 | ** valid entry. In other words, the calling procedure must guarantee |
| 3855 | ** that the cursor has Cursor.eState==CURSOR_VALID. |
| 3856 | ** |
| 3857 | ** Failure is not possible. This function always returns SQLITE_OK. |
| 3858 | ** It might just as well be a procedure (returning void) but we continue |
| 3859 | ** to return an integer result code for historical reasons. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3860 | */ |
| 3861 | int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3862 | assert( cursorHoldsMutex(pCur) ); |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 3863 | assert( pCur->eState==CURSOR_VALID ); |
drh | 3e28ff5 | 2014-09-24 00:59:08 +0000 | [diff] [blame] | 3864 | assert( pCur->apPage[pCur->iPage]->intKeyLeaf==1 ); |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 3865 | getCellInfo(pCur); |
drh | ab1cc58 | 2014-09-23 21:25:19 +0000 | [diff] [blame] | 3866 | *pSize = pCur->info.nPayload; |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 3867 | return SQLITE_OK; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3868 | } |
| 3869 | |
| 3870 | /* |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3871 | ** Given the page number of an overflow page in the database (parameter |
| 3872 | ** ovfl), this function finds the page number of the next page in the |
| 3873 | ** linked list of overflow pages. If possible, it uses the auto-vacuum |
| 3874 | ** pointer-map data instead of reading the content of page ovfl to do so. |
| 3875 | ** |
| 3876 | ** If an error occurs an SQLite error code is returned. Otherwise: |
| 3877 | ** |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3878 | ** The page number of the next overflow page in the linked list is |
| 3879 | ** written to *pPgnoNext. If page ovfl is the last page in its linked |
| 3880 | ** list, *pPgnoNext is set to zero. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3881 | ** |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3882 | ** If ppPage is not NULL, and a reference to the MemPage object corresponding |
| 3883 | ** to page number pOvfl was obtained, then *ppPage is set to point to that |
| 3884 | ** reference. It is the responsibility of the caller to call releasePage() |
| 3885 | ** on *ppPage to free the reference. In no reference was obtained (because |
| 3886 | ** the pointer-map was used to obtain the value for *pPgnoNext), then |
| 3887 | ** *ppPage is set to zero. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3888 | */ |
| 3889 | static int getOverflowPage( |
drh | fa3be90 | 2009-07-07 02:44:07 +0000 | [diff] [blame] | 3890 | BtShared *pBt, /* The database file */ |
| 3891 | Pgno ovfl, /* Current overflow page number */ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3892 | MemPage **ppPage, /* OUT: MemPage handle (may be NULL) */ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3893 | Pgno *pPgnoNext /* OUT: Next overflow page number */ |
| 3894 | ){ |
| 3895 | Pgno next = 0; |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3896 | MemPage *pPage = 0; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 3897 | int rc = SQLITE_OK; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3898 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3899 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3900 | assert(pPgnoNext); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3901 | |
| 3902 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3903 | /* Try to find the next page in the overflow list using the |
| 3904 | ** autovacuum pointer-map pages. Guess that the next page in |
| 3905 | ** the overflow list is page number (ovfl+1). If that guess turns |
| 3906 | ** out to be wrong, fall back to loading the data of page |
| 3907 | ** number ovfl to determine the next page number. |
| 3908 | */ |
| 3909 | if( pBt->autoVacuum ){ |
| 3910 | Pgno pgno; |
| 3911 | Pgno iGuess = ovfl+1; |
| 3912 | u8 eType; |
| 3913 | |
| 3914 | while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){ |
| 3915 | iGuess++; |
| 3916 | } |
| 3917 | |
drh | b129915 | 2010-03-30 22:58:33 +0000 | [diff] [blame] | 3918 | if( iGuess<=btreePagecount(pBt) ){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3919 | rc = ptrmapGet(pBt, iGuess, &eType, &pgno); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3920 | if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3921 | next = iGuess; |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3922 | rc = SQLITE_DONE; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3923 | } |
| 3924 | } |
| 3925 | } |
| 3926 | #endif |
| 3927 | |
danielk1977 | d8a3f3d | 2009-07-11 11:45:23 +0000 | [diff] [blame] | 3928 | assert( next==0 || rc==SQLITE_DONE ); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3929 | if( rc==SQLITE_OK ){ |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 3930 | rc = btreeGetPage(pBt, ovfl, &pPage, (ppPage==0) ? PAGER_GET_READONLY : 0); |
danielk1977 | d8a3f3d | 2009-07-11 11:45:23 +0000 | [diff] [blame] | 3931 | assert( rc==SQLITE_OK || pPage==0 ); |
| 3932 | if( rc==SQLITE_OK ){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3933 | next = get4byte(pPage->aData); |
| 3934 | } |
danielk1977 | 443c059 | 2009-01-16 15:21:05 +0000 | [diff] [blame] | 3935 | } |
danielk1977 | 45d6882 | 2009-01-16 16:23:38 +0000 | [diff] [blame] | 3936 | |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 3937 | *pPgnoNext = next; |
| 3938 | if( ppPage ){ |
| 3939 | *ppPage = pPage; |
| 3940 | }else{ |
| 3941 | releasePage(pPage); |
| 3942 | } |
| 3943 | return (rc==SQLITE_DONE ? SQLITE_OK : rc); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3944 | } |
| 3945 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3946 | /* |
| 3947 | ** Copy data from a buffer to a page, or from a page to a buffer. |
| 3948 | ** |
| 3949 | ** pPayload is a pointer to data stored on database page pDbPage. |
| 3950 | ** If argument eOp is false, then nByte bytes of data are copied |
| 3951 | ** from pPayload to the buffer pointed at by pBuf. If eOp is true, |
| 3952 | ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes |
| 3953 | ** of data are copied from the buffer pBuf to pPayload. |
| 3954 | ** |
| 3955 | ** SQLITE_OK is returned on success, otherwise an error code. |
| 3956 | */ |
| 3957 | static int copyPayload( |
| 3958 | void *pPayload, /* Pointer to page data */ |
| 3959 | void *pBuf, /* Pointer to buffer */ |
| 3960 | int nByte, /* Number of bytes to copy */ |
| 3961 | int eOp, /* 0 -> copy from page, 1 -> copy to page */ |
| 3962 | DbPage *pDbPage /* Page containing pPayload */ |
| 3963 | ){ |
| 3964 | if( eOp ){ |
| 3965 | /* Copy data from buffer to page (a write operation) */ |
| 3966 | int rc = sqlite3PagerWrite(pDbPage); |
| 3967 | if( rc!=SQLITE_OK ){ |
| 3968 | return rc; |
| 3969 | } |
| 3970 | memcpy(pPayload, pBuf, nByte); |
| 3971 | }else{ |
| 3972 | /* Copy data from page to buffer (a read operation) */ |
| 3973 | memcpy(pBuf, pPayload, nByte); |
| 3974 | } |
| 3975 | return SQLITE_OK; |
| 3976 | } |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3977 | |
| 3978 | /* |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3979 | ** This function is used to read or overwrite payload information |
dan | 5a500af | 2014-03-11 20:33:04 +0000 | [diff] [blame] | 3980 | ** for the entry that the pCur cursor is pointing to. The eOp |
| 3981 | ** argument is interpreted as follows: |
| 3982 | ** |
| 3983 | ** 0: The operation is a read. Populate the overflow cache. |
| 3984 | ** 1: The operation is a write. Populate the overflow cache. |
| 3985 | ** 2: The operation is a read. Do not populate the overflow cache. |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3986 | ** |
| 3987 | ** A total of "amt" bytes are read or written beginning at "offset". |
| 3988 | ** Data is read to or from the buffer pBuf. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3989 | ** |
drh | 3bcdfd2 | 2009-07-12 02:32:21 +0000 | [diff] [blame] | 3990 | ** The content being read or written might appear on the main page |
| 3991 | ** or be scattered out on multiple overflow pages. |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3992 | ** |
dan | 5a500af | 2014-03-11 20:33:04 +0000 | [diff] [blame] | 3993 | ** If the current cursor entry uses one or more overflow pages and the |
| 3994 | ** 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] | 3995 | ** populates the overflow page-list cache array (BtCursor.aOverflow). |
dan | 5a500af | 2014-03-11 20:33:04 +0000 | [diff] [blame] | 3996 | ** Subsequent calls use this cache to make seeking to the supplied offset |
| 3997 | ** more efficient. |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3998 | ** |
| 3999 | ** Once an overflow page-list cache has been allocated, it may be |
| 4000 | ** invalidated if some other cursor writes to the same table, or if |
| 4001 | ** the cursor is moved to a different row. Additionally, in auto-vacuum |
| 4002 | ** mode, the following events may invalidate an overflow page-list cache. |
| 4003 | ** |
| 4004 | ** * An incremental vacuum, |
| 4005 | ** * A commit in auto_vacuum="full" mode, |
| 4006 | ** * Creating a table (may require moving an overflow page). |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4007 | */ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 4008 | static int accessPayload( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4009 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 4010 | u32 offset, /* Begin reading this far into payload */ |
| 4011 | u32 amt, /* Read this many bytes */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4012 | unsigned char *pBuf, /* Write the bytes into this buffer */ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 4013 | int eOp /* zero to read. non-zero to write. */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4014 | ){ |
| 4015 | unsigned char *aPayload; |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4016 | int rc = SQLITE_OK; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 4017 | int iIdx = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4018 | MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */ |
danielk1977 | 0d06541 | 2008-11-12 18:21:36 +0000 | [diff] [blame] | 4019 | BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */ |
drh | 4c41718 | 2014-03-31 23:57:41 +0000 | [diff] [blame] | 4020 | #ifdef SQLITE_DIRECT_OVERFLOW_READ |
drh | 3f38740 | 2014-09-24 01:23:00 +0000 | [diff] [blame] | 4021 | int bEnd; /* True if reading to end of data */ |
drh | 4c41718 | 2014-03-31 23:57:41 +0000 | [diff] [blame] | 4022 | #endif |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4023 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4024 | assert( pPage ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4025 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4026 | assert( pCur->aiIdx[pCur->iPage]<pPage->nCell ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4027 | assert( cursorHoldsMutex(pCur) ); |
drh | 3f38740 | 2014-09-24 01:23:00 +0000 | [diff] [blame] | 4028 | assert( eOp!=2 || offset==0 ); /* Always start from beginning for eOp==2 */ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4029 | |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 4030 | getCellInfo(pCur); |
drh | ab1cc58 | 2014-09-23 21:25:19 +0000 | [diff] [blame] | 4031 | aPayload = pCur->info.pPayload; |
drh | 4c41718 | 2014-03-31 23:57:41 +0000 | [diff] [blame] | 4032 | #ifdef SQLITE_DIRECT_OVERFLOW_READ |
drh | ab1cc58 | 2014-09-23 21:25:19 +0000 | [diff] [blame] | 4033 | bEnd = offset+amt==pCur->info.nPayload; |
drh | 4c41718 | 2014-03-31 23:57:41 +0000 | [diff] [blame] | 4034 | #endif |
drh | ab1cc58 | 2014-09-23 21:25:19 +0000 | [diff] [blame] | 4035 | assert( offset+amt <= pCur->info.nPayload ); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4036 | |
drh | ab1cc58 | 2014-09-23 21:25:19 +0000 | [diff] [blame] | 4037 | if( &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize] ){ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4038 | /* 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] | 4039 | return SQLITE_CORRUPT_BKPT; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4040 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4041 | |
| 4042 | /* Check if data must be read/written to/from the btree page itself. */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4043 | if( offset<pCur->info.nLocal ){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 4044 | int a = amt; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4045 | if( a+offset>pCur->info.nLocal ){ |
| 4046 | a = pCur->info.nLocal - offset; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 4047 | } |
dan | 5a500af | 2014-03-11 20:33:04 +0000 | [diff] [blame] | 4048 | rc = copyPayload(&aPayload[offset], pBuf, a, (eOp & 0x01), pPage->pDbPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 4049 | offset = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4050 | pBuf += a; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 4051 | amt -= a; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4052 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4053 | offset -= pCur->info.nLocal; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4054 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4055 | |
| 4056 | if( rc==SQLITE_OK && amt>0 ){ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 4057 | const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4058 | Pgno nextPage; |
| 4059 | |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4060 | nextPage = get4byte(&aPayload[pCur->info.nLocal]); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4061 | |
drh | a38c951 | 2014-04-01 01:24:34 +0000 | [diff] [blame] | 4062 | /* If the BtCursor.aOverflow[] has not been allocated, allocate it now. |
| 4063 | ** Except, do not allocate aOverflow[] for eOp==2. |
| 4064 | ** |
| 4065 | ** The aOverflow[] array is sized at one entry for each overflow page |
| 4066 | ** in the overflow chain. The page number of the first overflow page is |
| 4067 | ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array |
| 4068 | ** means "not yet known" (the cache is lazily populated). |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4069 | */ |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 4070 | if( eOp!=2 && (pCur->curFlags & BTCF_ValidOvfl)==0 ){ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 4071 | int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize; |
dan | 5a500af | 2014-03-11 20:33:04 +0000 | [diff] [blame] | 4072 | if( nOvfl>pCur->nOvflAlloc ){ |
| 4073 | Pgno *aNew = (Pgno*)sqlite3DbRealloc( |
| 4074 | pCur->pBtree->db, pCur->aOverflow, nOvfl*2*sizeof(Pgno) |
| 4075 | ); |
| 4076 | if( aNew==0 ){ |
| 4077 | rc = SQLITE_NOMEM; |
| 4078 | }else{ |
| 4079 | pCur->nOvflAlloc = nOvfl*2; |
| 4080 | pCur->aOverflow = aNew; |
| 4081 | } |
| 4082 | } |
| 4083 | if( rc==SQLITE_OK ){ |
| 4084 | memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno)); |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 4085 | pCur->curFlags |= BTCF_ValidOvfl; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 4086 | } |
| 4087 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4088 | |
| 4089 | /* If the overflow page-list cache has been allocated and the |
| 4090 | ** entry for the first required overflow page is valid, skip |
| 4091 | ** directly to it. |
| 4092 | */ |
drh | 3f38740 | 2014-09-24 01:23:00 +0000 | [diff] [blame] | 4093 | if( (pCur->curFlags & BTCF_ValidOvfl)!=0 |
| 4094 | && pCur->aOverflow[offset/ovflSize] |
| 4095 | ){ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 4096 | iIdx = (offset/ovflSize); |
| 4097 | nextPage = pCur->aOverflow[iIdx]; |
| 4098 | offset = (offset%ovflSize); |
| 4099 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4100 | |
| 4101 | for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){ |
| 4102 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4103 | /* If required, populate the overflow page-list cache. */ |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 4104 | if( (pCur->curFlags & BTCF_ValidOvfl)!=0 ){ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4105 | assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage); |
| 4106 | pCur->aOverflow[iIdx] = nextPage; |
| 4107 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4108 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 4109 | if( offset>=ovflSize ){ |
| 4110 | /* The only reason to read this page is to obtain the page |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4111 | ** number for the next page in the overflow chain. The page |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 4112 | ** data is not required. So first try to lookup the overflow |
| 4113 | ** page-list cache, if any, then fall back to the getOverflowPage() |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4114 | ** function. |
drh | a38c951 | 2014-04-01 01:24:34 +0000 | [diff] [blame] | 4115 | ** |
| 4116 | ** Note that the aOverflow[] array must be allocated because eOp!=2 |
| 4117 | ** here. If eOp==2, then offset==0 and this branch is never taken. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 4118 | */ |
drh | a38c951 | 2014-04-01 01:24:34 +0000 | [diff] [blame] | 4119 | assert( eOp!=2 ); |
| 4120 | assert( pCur->curFlags & BTCF_ValidOvfl ); |
| 4121 | if( pCur->aOverflow[iIdx+1] ){ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4122 | nextPage = pCur->aOverflow[iIdx+1]; |
drh | a38c951 | 2014-04-01 01:24:34 +0000 | [diff] [blame] | 4123 | }else{ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4124 | rc = getOverflowPage(pBt, nextPage, 0, &nextPage); |
drh | a38c951 | 2014-04-01 01:24:34 +0000 | [diff] [blame] | 4125 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4126 | offset -= ovflSize; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 4127 | }else{ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 4128 | /* Need to read this page properly. It contains some of the |
| 4129 | ** range of data that is being read (eOp==0) or written (eOp!=0). |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 4130 | */ |
dan | f4ba109 | 2011-10-08 14:57:07 +0000 | [diff] [blame] | 4131 | #ifdef SQLITE_DIRECT_OVERFLOW_READ |
| 4132 | sqlite3_file *fd; |
| 4133 | #endif |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 4134 | int a = amt; |
dan | f4ba109 | 2011-10-08 14:57:07 +0000 | [diff] [blame] | 4135 | if( a + offset > ovflSize ){ |
| 4136 | a = ovflSize - offset; |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 4137 | } |
dan | f4ba109 | 2011-10-08 14:57:07 +0000 | [diff] [blame] | 4138 | |
| 4139 | #ifdef SQLITE_DIRECT_OVERFLOW_READ |
| 4140 | /* If all the following are true: |
| 4141 | ** |
| 4142 | ** 1) this is a read operation, and |
| 4143 | ** 2) data is required from the start of this overflow page, and |
| 4144 | ** 3) the database is file-backed, and |
| 4145 | ** 4) there is no open write-transaction, and |
| 4146 | ** 5) the database is not a WAL database, |
dan | 9bc21b5 | 2014-03-20 18:56:35 +0000 | [diff] [blame] | 4147 | ** 6) all data from the page is being read. |
dan | f4ba109 | 2011-10-08 14:57:07 +0000 | [diff] [blame] | 4148 | ** |
| 4149 | ** then data can be read directly from the database file into the |
| 4150 | ** output buffer, bypassing the page-cache altogether. This speeds |
| 4151 | ** up loading large records that span many overflow pages. |
| 4152 | */ |
dan | 5a500af | 2014-03-11 20:33:04 +0000 | [diff] [blame] | 4153 | if( (eOp&0x01)==0 /* (1) */ |
dan | f4ba109 | 2011-10-08 14:57:07 +0000 | [diff] [blame] | 4154 | && offset==0 /* (2) */ |
dan | 9bc21b5 | 2014-03-20 18:56:35 +0000 | [diff] [blame] | 4155 | && (bEnd || a==ovflSize) /* (6) */ |
dan | f4ba109 | 2011-10-08 14:57:07 +0000 | [diff] [blame] | 4156 | && pBt->inTransaction==TRANS_READ /* (4) */ |
| 4157 | && (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (3) */ |
| 4158 | && pBt->pPage1->aData[19]==0x01 /* (5) */ |
| 4159 | ){ |
| 4160 | u8 aSave[4]; |
| 4161 | u8 *aWrite = &pBuf[-4]; |
| 4162 | memcpy(aSave, aWrite, 4); |
dan | 27d47fb | 2011-12-21 17:00:16 +0000 | [diff] [blame] | 4163 | rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1)); |
dan | f4ba109 | 2011-10-08 14:57:07 +0000 | [diff] [blame] | 4164 | nextPage = get4byte(aWrite); |
| 4165 | memcpy(aWrite, aSave, 4); |
| 4166 | }else |
| 4167 | #endif |
| 4168 | |
| 4169 | { |
| 4170 | DbPage *pDbPage; |
dan | 11dcd11 | 2013-03-15 18:29:18 +0000 | [diff] [blame] | 4171 | rc = sqlite3PagerAcquire(pBt->pPager, nextPage, &pDbPage, |
dan | 5a500af | 2014-03-11 20:33:04 +0000 | [diff] [blame] | 4172 | ((eOp&0x01)==0 ? PAGER_GET_READONLY : 0) |
dan | 11dcd11 | 2013-03-15 18:29:18 +0000 | [diff] [blame] | 4173 | ); |
dan | f4ba109 | 2011-10-08 14:57:07 +0000 | [diff] [blame] | 4174 | if( rc==SQLITE_OK ){ |
| 4175 | aPayload = sqlite3PagerGetData(pDbPage); |
| 4176 | nextPage = get4byte(aPayload); |
dan | 5a500af | 2014-03-11 20:33:04 +0000 | [diff] [blame] | 4177 | rc = copyPayload(&aPayload[offset+4], pBuf, a, (eOp&0x01), pDbPage); |
dan | f4ba109 | 2011-10-08 14:57:07 +0000 | [diff] [blame] | 4178 | sqlite3PagerUnref(pDbPage); |
| 4179 | offset = 0; |
| 4180 | } |
| 4181 | } |
| 4182 | amt -= a; |
| 4183 | pBuf += a; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 4184 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 4185 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 4186 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 4187 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4188 | if( rc==SQLITE_OK && amt>0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 4189 | return SQLITE_CORRUPT_BKPT; |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 4190 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 4191 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 4192 | } |
| 4193 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4194 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4195 | ** Read part of the key associated with cursor pCur. Exactly |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 4196 | ** "amt" bytes will be transferred into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4197 | ** begins at "offset". |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4198 | ** |
drh | 5d1a872 | 2009-07-22 18:07:40 +0000 | [diff] [blame] | 4199 | ** The caller must ensure that pCur is pointing to a valid row |
| 4200 | ** in the table. |
| 4201 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4202 | ** Return SQLITE_OK on success or an error code if anything goes |
| 4203 | ** wrong. An error is returned if "offset+amt" is larger than |
| 4204 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4205 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4206 | int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4207 | assert( cursorHoldsMutex(pCur) ); |
drh | 5d1a872 | 2009-07-22 18:07:40 +0000 | [diff] [blame] | 4208 | assert( pCur->eState==CURSOR_VALID ); |
| 4209 | assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] ); |
| 4210 | assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell ); |
| 4211 | return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4212 | } |
| 4213 | |
| 4214 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4215 | ** Read part of the data associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4216 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4217 | ** begins at "offset". |
| 4218 | ** |
| 4219 | ** Return SQLITE_OK on success or an error code if anything goes |
| 4220 | ** wrong. An error is returned if "offset+amt" is larger than |
| 4221 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4222 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4223 | int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4224 | int rc; |
| 4225 | |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 4226 | #ifndef SQLITE_OMIT_INCRBLOB |
| 4227 | if ( pCur->eState==CURSOR_INVALID ){ |
| 4228 | return SQLITE_ABORT; |
| 4229 | } |
| 4230 | #endif |
| 4231 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4232 | assert( cursorHoldsMutex(pCur) ); |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 4233 | rc = restoreCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4234 | if( rc==SQLITE_OK ){ |
| 4235 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4236 | assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] ); |
| 4237 | assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell ); |
drh | fb19268 | 2009-07-11 18:26:28 +0000 | [diff] [blame] | 4238 | rc = accessPayload(pCur, offset, amt, pBuf, 0); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4239 | } |
| 4240 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 4241 | } |
| 4242 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4243 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 4244 | ** Return a pointer to payload information from the entry that the |
| 4245 | ** pCur cursor is pointing to. The pointer is to the beginning of |
drh | 2a8d226 | 2013-12-09 20:43:22 +0000 | [diff] [blame] | 4246 | ** the key if index btrees (pPage->intKey==0) and is the data for |
| 4247 | ** table btrees (pPage->intKey==1). The number of bytes of available |
| 4248 | ** key/data is written into *pAmt. If *pAmt==0, then the value |
| 4249 | ** returned will not be a valid pointer. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 4250 | ** |
| 4251 | ** This routine is an optimization. It is common for the entire key |
| 4252 | ** and data to fit on the local page and for there to be no overflow |
| 4253 | ** pages. When that is so, this routine can be used to access the |
| 4254 | ** 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] | 4255 | ** onto overflow pages, then accessPayload() must be used to reassemble |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 4256 | ** the key/data and copy it into a preallocated buffer. |
| 4257 | ** |
| 4258 | ** The pointer returned by this routine looks directly into the cached |
| 4259 | ** page of the database. The data might change or move the next time |
| 4260 | ** any btree routine is called. |
| 4261 | */ |
drh | 2a8d226 | 2013-12-09 20:43:22 +0000 | [diff] [blame] | 4262 | static const void *fetchPayload( |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 4263 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
drh | 2a8d226 | 2013-12-09 20:43:22 +0000 | [diff] [blame] | 4264 | u32 *pAmt /* Write the number of available bytes here */ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 4265 | ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4266 | assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4267 | assert( pCur->eState==CURSOR_VALID ); |
drh | 2a8d226 | 2013-12-09 20:43:22 +0000 | [diff] [blame] | 4268 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4269 | assert( cursorHoldsMutex(pCur) ); |
drh | 2a8d226 | 2013-12-09 20:43:22 +0000 | [diff] [blame] | 4270 | assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell ); |
drh | 86dd371 | 2014-03-25 11:00:21 +0000 | [diff] [blame] | 4271 | assert( pCur->info.nSize>0 ); |
drh | 2a8d226 | 2013-12-09 20:43:22 +0000 | [diff] [blame] | 4272 | *pAmt = pCur->info.nLocal; |
drh | ab1cc58 | 2014-09-23 21:25:19 +0000 | [diff] [blame] | 4273 | return (void*)pCur->info.pPayload; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 4274 | } |
| 4275 | |
| 4276 | |
| 4277 | /* |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 4278 | ** For the entry that cursor pCur is point to, return as |
| 4279 | ** many bytes of the key or data as are available on the local |
| 4280 | ** b-tree page. Write the number of available bytes into *pAmt. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 4281 | ** |
| 4282 | ** The pointer returned is ephemeral. The key/data may move |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4283 | ** or be destroyed on the next call to any Btree routine, |
| 4284 | ** including calls from other threads against the same cache. |
| 4285 | ** Hence, a mutex on the BtShared should be held prior to calling |
| 4286 | ** this routine. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 4287 | ** |
| 4288 | ** These routines is used to get quick access to key and data |
| 4289 | ** in the common case where no overflow pages are used. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 4290 | */ |
drh | 501932c | 2013-11-21 21:59:53 +0000 | [diff] [blame] | 4291 | const void *sqlite3BtreeKeyFetch(BtCursor *pCur, u32 *pAmt){ |
drh | 2a8d226 | 2013-12-09 20:43:22 +0000 | [diff] [blame] | 4292 | return fetchPayload(pCur, pAmt); |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 4293 | } |
drh | 501932c | 2013-11-21 21:59:53 +0000 | [diff] [blame] | 4294 | const void *sqlite3BtreeDataFetch(BtCursor *pCur, u32 *pAmt){ |
drh | 2a8d226 | 2013-12-09 20:43:22 +0000 | [diff] [blame] | 4295 | return fetchPayload(pCur, pAmt); |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 4296 | } |
| 4297 | |
| 4298 | |
| 4299 | /* |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4300 | ** 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] | 4301 | ** page number of the child page to move to. |
danielk1977 | a299d61 | 2009-07-13 11:22:10 +0000 | [diff] [blame] | 4302 | ** |
| 4303 | ** This function returns SQLITE_CORRUPT if the page-header flags field of |
| 4304 | ** the new child page does not match the flags field of the parent (i.e. |
| 4305 | ** if an intkey page appears to be the parent of a non-intkey page, or |
| 4306 | ** vice-versa). |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4307 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4308 | static int moveToChild(BtCursor *pCur, u32 newPgno){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4309 | int rc; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4310 | int i = pCur->iPage; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4311 | MemPage *pNewPage; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 4312 | BtShared *pBt = pCur->pBt; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4313 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4314 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4315 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4316 | assert( pCur->iPage<BTCURSOR_MAX_DEPTH ); |
dan | 11dcd11 | 2013-03-15 18:29:18 +0000 | [diff] [blame] | 4317 | assert( pCur->iPage>=0 ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4318 | if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){ |
| 4319 | return SQLITE_CORRUPT_BKPT; |
| 4320 | } |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 4321 | rc = getAndInitPage(pBt, newPgno, &pNewPage, |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 4322 | (pCur->curFlags & BTCF_WriteFlag)==0 ? PAGER_GET_READONLY : 0); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4323 | if( rc ) return rc; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4324 | pCur->apPage[i+1] = pNewPage; |
| 4325 | pCur->aiIdx[i+1] = 0; |
| 4326 | pCur->iPage++; |
| 4327 | |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 4328 | pCur->info.nSize = 0; |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 4329 | pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); |
danielk1977 | bd5969a | 2009-07-11 17:39:42 +0000 | [diff] [blame] | 4330 | if( pNewPage->nCell<1 || pNewPage->intKey!=pCur->apPage[i]->intKey ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 4331 | return SQLITE_CORRUPT_BKPT; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 4332 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4333 | return SQLITE_OK; |
| 4334 | } |
| 4335 | |
dan | bb246c4 | 2012-01-12 14:25:55 +0000 | [diff] [blame] | 4336 | #if 0 |
danielk1977 | bf93c56 | 2008-09-29 15:53:25 +0000 | [diff] [blame] | 4337 | /* |
| 4338 | ** Page pParent is an internal (non-leaf) tree page. This function |
| 4339 | ** asserts that page number iChild is the left-child if the iIdx'th |
| 4340 | ** cell in page pParent. Or, if iIdx is equal to the total number of |
| 4341 | ** cells in pParent, that page number iChild is the right-child of |
| 4342 | ** the page. |
| 4343 | */ |
| 4344 | static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){ |
| 4345 | assert( iIdx<=pParent->nCell ); |
| 4346 | if( iIdx==pParent->nCell ){ |
| 4347 | assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild ); |
| 4348 | }else{ |
| 4349 | assert( get4byte(findCell(pParent, iIdx))==iChild ); |
| 4350 | } |
| 4351 | } |
| 4352 | #else |
| 4353 | # define assertParentIndex(x,y,z) |
| 4354 | #endif |
| 4355 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4356 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4357 | ** Move the cursor up to the parent page. |
| 4358 | ** |
| 4359 | ** pCur->idx is set to the cell index that contains the pointer |
| 4360 | ** to the page we are coming from. If we are coming from the |
| 4361 | ** right-most child page then pCur->idx is set to one more than |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4362 | ** the largest cell index. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4363 | */ |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 4364 | static void moveToParent(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4365 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4366 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4367 | assert( pCur->iPage>0 ); |
| 4368 | assert( pCur->apPage[pCur->iPage] ); |
dan | bb246c4 | 2012-01-12 14:25:55 +0000 | [diff] [blame] | 4369 | |
| 4370 | /* UPDATE: It is actually possible for the condition tested by the assert |
| 4371 | ** below to be untrue if the database file is corrupt. This can occur if |
| 4372 | ** one cursor has modified page pParent while a reference to it is held |
| 4373 | ** by a second cursor. Which can only happen if a single page is linked |
| 4374 | ** into more than one b-tree structure in a corrupt database. */ |
| 4375 | #if 0 |
danielk1977 | bf93c56 | 2008-09-29 15:53:25 +0000 | [diff] [blame] | 4376 | assertParentIndex( |
| 4377 | pCur->apPage[pCur->iPage-1], |
| 4378 | pCur->aiIdx[pCur->iPage-1], |
| 4379 | pCur->apPage[pCur->iPage]->pgno |
| 4380 | ); |
dan | bb246c4 | 2012-01-12 14:25:55 +0000 | [diff] [blame] | 4381 | #endif |
dan | 6c2688c | 2012-01-12 15:05:03 +0000 | [diff] [blame] | 4382 | testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell ); |
dan | bb246c4 | 2012-01-12 14:25:55 +0000 | [diff] [blame] | 4383 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4384 | releasePage(pCur->apPage[pCur->iPage]); |
| 4385 | pCur->iPage--; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 4386 | pCur->info.nSize = 0; |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 4387 | pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4388 | } |
| 4389 | |
| 4390 | /* |
danielk1977 | 8f880a8 | 2009-07-13 09:41:45 +0000 | [diff] [blame] | 4391 | ** Move the cursor to point to the root page of its b-tree structure. |
| 4392 | ** |
| 4393 | ** If the table has a virtual root page, then the cursor is moved to point |
| 4394 | ** to the virtual root page instead of the actual root page. A table has a |
| 4395 | ** virtual root page when the actual root page contains no cells and a |
| 4396 | ** single child page. This can only happen with the table rooted at page 1. |
| 4397 | ** |
| 4398 | ** If the b-tree structure is empty, the cursor state is set to |
| 4399 | ** CURSOR_INVALID. Otherwise, the cursor is set to point to the first |
| 4400 | ** cell located on the root (or virtual root) page and the cursor state |
| 4401 | ** is set to CURSOR_VALID. |
| 4402 | ** |
| 4403 | ** If this function returns successfully, it may be assumed that the |
| 4404 | ** page-header flags indicate that the [virtual] root-page is the expected |
| 4405 | ** kind of b-tree page (i.e. if when opening the cursor the caller did not |
| 4406 | ** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D, |
| 4407 | ** indicating a table b-tree, or if the caller did specify a KeyInfo |
| 4408 | ** structure the flags byte is set to 0x02 or 0x0A, indicating an index |
| 4409 | ** b-tree). |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4410 | */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4411 | static int moveToRoot(BtCursor *pCur){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4412 | MemPage *pRoot; |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 4413 | int rc = SQLITE_OK; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4414 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4415 | assert( cursorHoldsMutex(pCur) ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 4416 | assert( CURSOR_INVALID < CURSOR_REQUIRESEEK ); |
| 4417 | assert( CURSOR_VALID < CURSOR_REQUIRESEEK ); |
| 4418 | assert( CURSOR_FAULT > CURSOR_REQUIRESEEK ); |
| 4419 | if( pCur->eState>=CURSOR_REQUIRESEEK ){ |
| 4420 | if( pCur->eState==CURSOR_FAULT ){ |
drh | 4c301aa | 2009-07-15 17:25:45 +0000 | [diff] [blame] | 4421 | assert( pCur->skipNext!=SQLITE_OK ); |
| 4422 | return pCur->skipNext; |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 4423 | } |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 4424 | sqlite3BtreeClearCursor(pCur); |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 4425 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4426 | |
| 4427 | if( pCur->iPage>=0 ){ |
drh | 4e8fe3f | 2013-12-06 23:25:27 +0000 | [diff] [blame] | 4428 | while( pCur->iPage ) releasePage(pCur->apPage[pCur->iPage--]); |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 4429 | }else if( pCur->pgnoRoot==0 ){ |
| 4430 | pCur->eState = CURSOR_INVALID; |
| 4431 | return SQLITE_OK; |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 4432 | }else{ |
drh | 4e8fe3f | 2013-12-06 23:25:27 +0000 | [diff] [blame] | 4433 | rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->apPage[0], |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 4434 | (pCur->curFlags & BTCF_WriteFlag)==0 ? PAGER_GET_READONLY : 0); |
drh | 4c301aa | 2009-07-15 17:25:45 +0000 | [diff] [blame] | 4435 | if( rc!=SQLITE_OK ){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 4436 | pCur->eState = CURSOR_INVALID; |
| 4437 | return rc; |
| 4438 | } |
danielk1977 | 172114a | 2009-07-07 15:47:12 +0000 | [diff] [blame] | 4439 | pCur->iPage = 0; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 4440 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4441 | pRoot = pCur->apPage[0]; |
| 4442 | assert( pRoot->pgno==pCur->pgnoRoot ); |
dan | 7df42ab | 2014-01-20 18:25:44 +0000 | [diff] [blame] | 4443 | |
| 4444 | /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor |
| 4445 | ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is |
| 4446 | ** NULL, the caller expects a table b-tree. If this is not the case, |
| 4447 | ** return an SQLITE_CORRUPT error. |
| 4448 | ** |
| 4449 | ** Earlier versions of SQLite assumed that this test could not fail |
| 4450 | ** if the root page was already loaded when this function was called (i.e. |
| 4451 | ** if pCur->iPage>=0). But this is not so if the database is corrupted |
| 4452 | ** in such a way that page pRoot is linked into a second b-tree table |
| 4453 | ** (or the freelist). */ |
| 4454 | assert( pRoot->intKey==1 || pRoot->intKey==0 ); |
| 4455 | if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){ |
| 4456 | return SQLITE_CORRUPT_BKPT; |
| 4457 | } |
danielk1977 | 8f880a8 | 2009-07-13 09:41:45 +0000 | [diff] [blame] | 4458 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4459 | pCur->aiIdx[0] = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 4460 | pCur->info.nSize = 0; |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 4461 | pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4462 | |
drh | 4e8fe3f | 2013-12-06 23:25:27 +0000 | [diff] [blame] | 4463 | if( pRoot->nCell>0 ){ |
| 4464 | pCur->eState = CURSOR_VALID; |
| 4465 | }else if( !pRoot->leaf ){ |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 4466 | Pgno subpage; |
drh | c85240d | 2009-06-04 16:14:33 +0000 | [diff] [blame] | 4467 | if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4468 | subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4469 | pCur->eState = CURSOR_VALID; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4470 | rc = moveToChild(pCur, subpage); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4471 | }else{ |
drh | 4e8fe3f | 2013-12-06 23:25:27 +0000 | [diff] [blame] | 4472 | pCur->eState = CURSOR_INVALID; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 4473 | } |
| 4474 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4475 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 4476 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4477 | /* |
| 4478 | ** Move the cursor down to the left-most leaf entry beneath the |
| 4479 | ** entry to which it is currently pointing. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 4480 | ** |
| 4481 | ** The left-most leaf is the one with the smallest key - the first |
| 4482 | ** in ascending order. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4483 | */ |
| 4484 | static int moveToLeftmost(BtCursor *pCur){ |
| 4485 | Pgno pgno; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4486 | int rc = SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4487 | MemPage *pPage; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4488 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4489 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4490 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4491 | while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){ |
| 4492 | assert( pCur->aiIdx[pCur->iPage]<pPage->nCell ); |
| 4493 | pgno = get4byte(findCell(pPage, pCur->aiIdx[pCur->iPage])); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4494 | rc = moveToChild(pCur, pgno); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4495 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4496 | return rc; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4497 | } |
| 4498 | |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4499 | /* |
| 4500 | ** Move the cursor down to the right-most leaf entry beneath the |
| 4501 | ** page to which it is currently pointing. Notice the difference |
| 4502 | ** between moveToLeftmost() and moveToRightmost(). moveToLeftmost() |
| 4503 | ** finds the left-most entry beneath the *entry* whereas moveToRightmost() |
| 4504 | ** finds the right-most entry beneath the *page*. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 4505 | ** |
| 4506 | ** The right-most entry is the one with the largest key - the last |
| 4507 | ** key in ascending order. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4508 | */ |
| 4509 | static int moveToRightmost(BtCursor *pCur){ |
| 4510 | Pgno pgno; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4511 | int rc = SQLITE_OK; |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 4512 | MemPage *pPage = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4513 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4514 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4515 | assert( pCur->eState==CURSOR_VALID ); |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 4516 | while( !(pPage = pCur->apPage[pCur->iPage])->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4517 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4518 | pCur->aiIdx[pCur->iPage] = pPage->nCell; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4519 | rc = moveToChild(pCur, pgno); |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 4520 | if( rc ) return rc; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4521 | } |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 4522 | pCur->aiIdx[pCur->iPage] = pPage->nCell-1; |
| 4523 | assert( pCur->info.nSize==0 ); |
| 4524 | assert( (pCur->curFlags & BTCF_ValidNKey)==0 ); |
| 4525 | return SQLITE_OK; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4526 | } |
| 4527 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4528 | /* Move the cursor to the first entry in the table. Return SQLITE_OK |
| 4529 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 4530 | ** or set *pRes to 1 if the table is empty. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4531 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4532 | int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4533 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4534 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4535 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 4536 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4537 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4538 | if( rc==SQLITE_OK ){ |
| 4539 | if( pCur->eState==CURSOR_INVALID ){ |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 4540 | assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4541 | *pRes = 1; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4542 | }else{ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4543 | assert( pCur->apPage[pCur->iPage]->nCell>0 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4544 | *pRes = 0; |
| 4545 | rc = moveToLeftmost(pCur); |
| 4546 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4547 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4548 | return rc; |
| 4549 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4550 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4551 | /* Move the cursor to the last entry in the table. Return SQLITE_OK |
| 4552 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 4553 | ** or set *pRes to 1 if the table is empty. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4554 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4555 | int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4556 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4557 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4558 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 4559 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
danielk1977 | 3f632d5 | 2009-05-02 10:03:09 +0000 | [diff] [blame] | 4560 | |
| 4561 | /* 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] | 4562 | if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){ |
danielk1977 | 3f632d5 | 2009-05-02 10:03:09 +0000 | [diff] [blame] | 4563 | #ifdef SQLITE_DEBUG |
| 4564 | /* This block serves to assert() that the cursor really does point |
| 4565 | ** to the last entry in the b-tree. */ |
| 4566 | int ii; |
| 4567 | for(ii=0; ii<pCur->iPage; ii++){ |
| 4568 | assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell ); |
| 4569 | } |
| 4570 | assert( pCur->aiIdx[pCur->iPage]==pCur->apPage[pCur->iPage]->nCell-1 ); |
| 4571 | assert( pCur->apPage[pCur->iPage]->leaf ); |
| 4572 | #endif |
| 4573 | return SQLITE_OK; |
| 4574 | } |
| 4575 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4576 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4577 | if( rc==SQLITE_OK ){ |
| 4578 | if( CURSOR_INVALID==pCur->eState ){ |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 4579 | assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4580 | *pRes = 1; |
| 4581 | }else{ |
| 4582 | assert( pCur->eState==CURSOR_VALID ); |
| 4583 | *pRes = 0; |
| 4584 | rc = moveToRightmost(pCur); |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 4585 | if( rc==SQLITE_OK ){ |
| 4586 | pCur->curFlags |= BTCF_AtLast; |
| 4587 | }else{ |
| 4588 | pCur->curFlags &= ~BTCF_AtLast; |
| 4589 | } |
| 4590 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4591 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4592 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 4593 | return rc; |
| 4594 | } |
| 4595 | |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 4596 | /* Move the cursor so that it points to an entry near the key |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4597 | ** specified by pIdxKey or intKey. Return a success code. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4598 | ** |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4599 | ** For INTKEY tables, the intKey parameter is used. pIdxKey |
| 4600 | ** must be NULL. For index tables, pIdxKey is used and intKey |
| 4601 | ** is ignored. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4602 | ** |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4603 | ** If an exact match is not found, then the cursor is always |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4604 | ** left pointing at a leaf page which would hold the entry if it |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4605 | ** were present. The cursor might point to an entry that comes |
| 4606 | ** before or after the key. |
| 4607 | ** |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 4608 | ** An integer is written into *pRes which is the result of |
| 4609 | ** comparing the key with the entry to which the cursor is |
| 4610 | ** pointing. The meaning of the integer written into |
| 4611 | ** *pRes is as follows: |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4612 | ** |
| 4613 | ** *pRes<0 The cursor is left pointing at an entry that |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 4614 | ** is smaller than intKey/pIdxKey or if the table is empty |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 4615 | ** and the cursor is therefore left point to nothing. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4616 | ** |
| 4617 | ** *pRes==0 The cursor is left pointing at an entry that |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 4618 | ** exactly matches intKey/pIdxKey. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4619 | ** |
| 4620 | ** *pRes>0 The cursor is left pointing at an entry that |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 4621 | ** is larger than intKey/pIdxKey. |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4622 | ** |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 4623 | */ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4624 | int sqlite3BtreeMovetoUnpacked( |
| 4625 | BtCursor *pCur, /* The cursor to be moved */ |
| 4626 | UnpackedRecord *pIdxKey, /* Unpacked index key */ |
| 4627 | i64 intKey, /* The table key */ |
| 4628 | int biasRight, /* If true, bias the search to the high end */ |
| 4629 | int *pRes /* Write search results here */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 4630 | ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4631 | int rc; |
dan | 3b9330f | 2014-02-27 20:44:18 +0000 | [diff] [blame] | 4632 | RecordCompare xRecordCompare; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4633 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4634 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 4635 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
danielk1977 | 5cb0963 | 2009-07-09 11:36:01 +0000 | [diff] [blame] | 4636 | assert( pRes ); |
danielk1977 | 3fd7cf5 | 2009-07-13 07:30:52 +0000 | [diff] [blame] | 4637 | assert( (pIdxKey==0)==(pCur->pKeyInfo==0) ); |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 4638 | |
| 4639 | /* If the cursor is already positioned at the point we are trying |
| 4640 | ** to move to, then just return without doing any work */ |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 4641 | if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0 |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4642 | && pCur->apPage[0]->intKey |
| 4643 | ){ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4644 | if( pCur->info.nKey==intKey ){ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 4645 | *pRes = 0; |
| 4646 | return SQLITE_OK; |
| 4647 | } |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 4648 | if( (pCur->curFlags & BTCF_AtLast)!=0 && pCur->info.nKey<intKey ){ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 4649 | *pRes = -1; |
| 4650 | return SQLITE_OK; |
| 4651 | } |
| 4652 | } |
| 4653 | |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 4654 | if( pIdxKey ){ |
| 4655 | xRecordCompare = sqlite3VdbeFindCompare(pIdxKey); |
dan | 38fdead | 2014-04-01 10:19:02 +0000 | [diff] [blame] | 4656 | pIdxKey->errCode = 0; |
dan | 3b9330f | 2014-02-27 20:44:18 +0000 | [diff] [blame] | 4657 | assert( pIdxKey->default_rc==1 |
| 4658 | || pIdxKey->default_rc==0 |
| 4659 | || pIdxKey->default_rc==-1 |
| 4660 | ); |
drh | 13a747e | 2014-03-03 21:46:55 +0000 | [diff] [blame] | 4661 | }else{ |
drh | b6e8fd1 | 2014-03-06 01:56:33 +0000 | [diff] [blame] | 4662 | xRecordCompare = 0; /* All keys are integers */ |
dan | 1fed5da | 2014-02-25 21:01:25 +0000 | [diff] [blame] | 4663 | } |
| 4664 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4665 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4666 | if( rc ){ |
| 4667 | return rc; |
| 4668 | } |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 4669 | assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage] ); |
| 4670 | assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->isInit ); |
| 4671 | assert( pCur->eState==CURSOR_INVALID || pCur->apPage[pCur->iPage]->nCell>0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4672 | if( pCur->eState==CURSOR_INVALID ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 4673 | *pRes = -1; |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 4674 | assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 ); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 4675 | return SQLITE_OK; |
| 4676 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4677 | assert( pCur->apPage[0]->intKey || pIdxKey ); |
drh | 1468438 | 2006-11-30 13:05:29 +0000 | [diff] [blame] | 4678 | for(;;){ |
drh | ec3e6b1 | 2013-11-25 02:38:55 +0000 | [diff] [blame] | 4679 | int lwr, upr, idx, c; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4680 | Pgno chldPg; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4681 | MemPage *pPage = pCur->apPage[pCur->iPage]; |
drh | ec3e6b1 | 2013-11-25 02:38:55 +0000 | [diff] [blame] | 4682 | u8 *pCell; /* Pointer to current cell in pPage */ |
danielk1977 | 171fff3 | 2009-07-11 05:06:51 +0000 | [diff] [blame] | 4683 | |
| 4684 | /* pPage->nCell must be greater than zero. If this is the root-page |
| 4685 | ** the cursor would have been INVALID above and this for(;;) loop |
| 4686 | ** not run. If this is not the root-page, then the moveToChild() routine |
danielk1977 | 3fd7cf5 | 2009-07-13 07:30:52 +0000 | [diff] [blame] | 4687 | ** would have already detected db corruption. Similarly, pPage must |
| 4688 | ** be the right kind (index or table) of b-tree page. Otherwise |
| 4689 | ** a moveToChild() or moveToRoot() call would have detected corruption. */ |
danielk1977 | 171fff3 | 2009-07-11 05:06:51 +0000 | [diff] [blame] | 4690 | assert( pPage->nCell>0 ); |
danielk1977 | 3fd7cf5 | 2009-07-13 07:30:52 +0000 | [diff] [blame] | 4691 | assert( pPage->intKey==(pIdxKey==0) ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4692 | lwr = 0; |
| 4693 | upr = pPage->nCell-1; |
drh | ebf10b1 | 2013-11-25 17:38:26 +0000 | [diff] [blame] | 4694 | assert( biasRight==0 || biasRight==1 ); |
| 4695 | idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */ |
drh | d793f44 | 2013-11-25 14:10:15 +0000 | [diff] [blame] | 4696 | pCur->aiIdx[pCur->iPage] = (u16)idx; |
dan | a4660bd | 2014-03-04 16:05:25 +0000 | [diff] [blame] | 4697 | if( xRecordCompare==0 ){ |
drh | 343d754 | 2014-09-24 04:38:14 +0000 | [diff] [blame] | 4698 | u8 bits = 0; |
| 4699 | i64 iLwr = 0, iUpr = 0; |
drh | ec3e6b1 | 2013-11-25 02:38:55 +0000 | [diff] [blame] | 4700 | for(;;){ |
danielk1977 | 11c327a | 2009-05-04 19:01:26 +0000 | [diff] [blame] | 4701 | i64 nCellKey; |
drh | ec3e6b1 | 2013-11-25 02:38:55 +0000 | [diff] [blame] | 4702 | pCell = findCell(pPage, idx) + pPage->childPtrSize; |
drh | 3e28ff5 | 2014-09-24 00:59:08 +0000 | [diff] [blame] | 4703 | if( pPage->intKeyLeaf ){ |
drh | 9b2fc61 | 2013-11-25 20:14:13 +0000 | [diff] [blame] | 4704 | while( 0x80 <= *(pCell++) ){ |
| 4705 | if( pCell>=pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT; |
| 4706 | } |
drh | d172f86 | 2006-01-12 15:01:15 +0000 | [diff] [blame] | 4707 | } |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 4708 | getVarint(pCell, (u64*)&nCellKey); |
drh | bb933ef | 2013-11-25 15:01:38 +0000 | [diff] [blame] | 4709 | if( nCellKey<intKey ){ |
| 4710 | lwr = idx+1; |
| 4711 | if( lwr>upr ){ c = -1; break; } |
drh | 343d754 | 2014-09-24 04:38:14 +0000 | [diff] [blame] | 4712 | iLwr = nCellKey; |
| 4713 | bits |= 1; |
drh | bb933ef | 2013-11-25 15:01:38 +0000 | [diff] [blame] | 4714 | }else if( nCellKey>intKey ){ |
| 4715 | upr = idx-1; |
| 4716 | if( lwr>upr ){ c = +1; break; } |
drh | 343d754 | 2014-09-24 04:38:14 +0000 | [diff] [blame] | 4717 | iUpr = nCellKey; |
| 4718 | bits |= 2; |
drh | bb933ef | 2013-11-25 15:01:38 +0000 | [diff] [blame] | 4719 | }else{ |
| 4720 | assert( nCellKey==intKey ); |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 4721 | pCur->curFlags |= BTCF_ValidNKey; |
drh | ec3e6b1 | 2013-11-25 02:38:55 +0000 | [diff] [blame] | 4722 | pCur->info.nKey = nCellKey; |
drh | d793f44 | 2013-11-25 14:10:15 +0000 | [diff] [blame] | 4723 | pCur->aiIdx[pCur->iPage] = (u16)idx; |
drh | ec3e6b1 | 2013-11-25 02:38:55 +0000 | [diff] [blame] | 4724 | if( !pPage->leaf ){ |
| 4725 | lwr = idx; |
drh | ebf10b1 | 2013-11-25 17:38:26 +0000 | [diff] [blame] | 4726 | goto moveto_next_layer; |
drh | ec3e6b1 | 2013-11-25 02:38:55 +0000 | [diff] [blame] | 4727 | }else{ |
| 4728 | *pRes = 0; |
| 4729 | rc = SQLITE_OK; |
| 4730 | goto moveto_finish; |
| 4731 | } |
drh | d793f44 | 2013-11-25 14:10:15 +0000 | [diff] [blame] | 4732 | } |
drh | 343d754 | 2014-09-24 04:38:14 +0000 | [diff] [blame] | 4733 | assert( lwr>=0 && upr>=lwr ); |
| 4734 | if( bits<3 || upr<=lwr+2 ){ |
| 4735 | idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */ |
| 4736 | }else if( (iUpr - iLwr)>100000 ){ |
| 4737 | i64 spacing = (iUpr-iLwr)/(upr-lwr); |
| 4738 | idx = (intKey-iLwr)/spacing+lwr; |
| 4739 | assert( idx>=lwr && idx<=upr ); |
| 4740 | }else{ |
| 4741 | idx = (intKey-iLwr)*(upr-lwr)/(iUpr-iLwr)+lwr; |
| 4742 | assert( idx>=lwr && idx<=upr ); |
| 4743 | } |
drh | ec3e6b1 | 2013-11-25 02:38:55 +0000 | [diff] [blame] | 4744 | } |
| 4745 | }else{ |
| 4746 | for(;;){ |
| 4747 | int nCell; |
drh | ec3e6b1 | 2013-11-25 02:38:55 +0000 | [diff] [blame] | 4748 | pCell = findCell(pPage, idx) + pPage->childPtrSize; |
| 4749 | |
drh | b2eced5 | 2010-08-12 02:41:12 +0000 | [diff] [blame] | 4750 | /* The maximum supported page-size is 65536 bytes. This means that |
danielk1977 | 11c327a | 2009-05-04 19:01:26 +0000 | [diff] [blame] | 4751 | ** the maximum number of record bytes stored on an index B-Tree |
drh | b2eced5 | 2010-08-12 02:41:12 +0000 | [diff] [blame] | 4752 | ** 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] | 4753 | ** varint. This information is used to attempt to avoid parsing |
| 4754 | ** the entire cell by checking for the cases where the record is |
| 4755 | ** stored entirely within the b-tree page by inspecting the first |
| 4756 | ** 2 bytes of the cell. |
| 4757 | */ |
drh | ec3e6b1 | 2013-11-25 02:38:55 +0000 | [diff] [blame] | 4758 | nCell = pCell[0]; |
drh | 72b8ef6 | 2013-12-06 22:44:51 +0000 | [diff] [blame] | 4759 | if( nCell<=pPage->max1bytePayload ){ |
danielk1977 | 11c327a | 2009-05-04 19:01:26 +0000 | [diff] [blame] | 4760 | /* This branch runs if the record-size field of the cell is a |
| 4761 | ** single byte varint and the record fits entirely on the main |
| 4762 | ** b-tree page. */ |
drh | 3def235 | 2011-11-11 00:27:15 +0000 | [diff] [blame] | 4763 | testcase( pCell+nCell+1==pPage->aDataEnd ); |
drh | 75179de | 2014-09-16 14:37:35 +0000 | [diff] [blame] | 4764 | c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey); |
danielk1977 | 11c327a | 2009-05-04 19:01:26 +0000 | [diff] [blame] | 4765 | }else if( !(pCell[1] & 0x80) |
| 4766 | && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal |
| 4767 | ){ |
| 4768 | /* The record-size field is a 2 byte varint and the record |
| 4769 | ** fits entirely on the main b-tree page. */ |
drh | 3def235 | 2011-11-11 00:27:15 +0000 | [diff] [blame] | 4770 | testcase( pCell+nCell+2==pPage->aDataEnd ); |
drh | 75179de | 2014-09-16 14:37:35 +0000 | [diff] [blame] | 4771 | c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 4772 | }else{ |
danielk1977 | 11c327a | 2009-05-04 19:01:26 +0000 | [diff] [blame] | 4773 | /* The record flows over onto one or more overflow pages. In |
| 4774 | ** this case the whole cell needs to be parsed, a buffer allocated |
| 4775 | ** and accessPayload() used to retrieve the record into the |
| 4776 | ** buffer before VdbeRecordCompare() can be called. */ |
| 4777 | void *pCellKey; |
| 4778 | u8 * const pCellBody = pCell - pPage->childPtrSize; |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 4779 | btreeParseCellPtr(pPage, pCellBody, &pCur->info); |
shane | 60a4b53 | 2009-05-06 18:57:09 +0000 | [diff] [blame] | 4780 | nCell = (int)pCur->info.nKey; |
danielk1977 | 11c327a | 2009-05-04 19:01:26 +0000 | [diff] [blame] | 4781 | pCellKey = sqlite3Malloc( nCell ); |
danielk1977 | 6507ecb | 2008-03-25 09:56:44 +0000 | [diff] [blame] | 4782 | if( pCellKey==0 ){ |
| 4783 | rc = SQLITE_NOMEM; |
| 4784 | goto moveto_finish; |
| 4785 | } |
drh | d793f44 | 2013-11-25 14:10:15 +0000 | [diff] [blame] | 4786 | pCur->aiIdx[pCur->iPage] = (u16)idx; |
dan | 5a500af | 2014-03-11 20:33:04 +0000 | [diff] [blame] | 4787 | rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 2); |
drh | ec9b31f | 2009-08-25 13:53:49 +0000 | [diff] [blame] | 4788 | if( rc ){ |
| 4789 | sqlite3_free(pCellKey); |
| 4790 | goto moveto_finish; |
| 4791 | } |
drh | 75179de | 2014-09-16 14:37:35 +0000 | [diff] [blame] | 4792 | c = xRecordCompare(nCell, pCellKey, pIdxKey); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 4793 | sqlite3_free(pCellKey); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 4794 | } |
dan | 38fdead | 2014-04-01 10:19:02 +0000 | [diff] [blame] | 4795 | assert( |
| 4796 | (pIdxKey->errCode!=SQLITE_CORRUPT || c==0) |
dan | a7bf23c | 2014-05-02 17:12:41 +0000 | [diff] [blame] | 4797 | && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed) |
dan | 38fdead | 2014-04-01 10:19:02 +0000 | [diff] [blame] | 4798 | ); |
drh | bb933ef | 2013-11-25 15:01:38 +0000 | [diff] [blame] | 4799 | if( c<0 ){ |
| 4800 | lwr = idx+1; |
| 4801 | }else if( c>0 ){ |
| 4802 | upr = idx-1; |
| 4803 | }else{ |
| 4804 | assert( c==0 ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 4805 | *pRes = 0; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 4806 | rc = SQLITE_OK; |
drh | d793f44 | 2013-11-25 14:10:15 +0000 | [diff] [blame] | 4807 | pCur->aiIdx[pCur->iPage] = (u16)idx; |
dan | 38fdead | 2014-04-01 10:19:02 +0000 | [diff] [blame] | 4808 | if( pIdxKey->errCode ) rc = SQLITE_CORRUPT; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 4809 | goto moveto_finish; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4810 | } |
drh | ebf10b1 | 2013-11-25 17:38:26 +0000 | [diff] [blame] | 4811 | if( lwr>upr ) break; |
| 4812 | assert( lwr+upr>=0 ); |
| 4813 | idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2 */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4814 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4815 | } |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 4816 | assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4817 | assert( pPage->isInit ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4818 | if( pPage->leaf ){ |
drh | ec3e6b1 | 2013-11-25 02:38:55 +0000 | [diff] [blame] | 4819 | assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell ); |
drh | bb933ef | 2013-11-25 15:01:38 +0000 | [diff] [blame] | 4820 | pCur->aiIdx[pCur->iPage] = (u16)idx; |
drh | ec3e6b1 | 2013-11-25 02:38:55 +0000 | [diff] [blame] | 4821 | *pRes = c; |
| 4822 | rc = SQLITE_OK; |
| 4823 | goto moveto_finish; |
drh | ebf10b1 | 2013-11-25 17:38:26 +0000 | [diff] [blame] | 4824 | } |
| 4825 | moveto_next_layer: |
| 4826 | if( lwr>=pPage->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4827 | chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4828 | }else{ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 4829 | chldPg = get4byte(findCell(pPage, lwr)); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4830 | } |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 4831 | pCur->aiIdx[pCur->iPage] = (u16)lwr; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4832 | rc = moveToChild(pCur, chldPg); |
drh | ec3e6b1 | 2013-11-25 02:38:55 +0000 | [diff] [blame] | 4833 | if( rc ) break; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4834 | } |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 4835 | moveto_finish: |
drh | d2022b0 | 2013-11-25 16:23:52 +0000 | [diff] [blame] | 4836 | pCur->info.nSize = 0; |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 4837 | pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 4838 | return rc; |
| 4839 | } |
| 4840 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4841 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4842 | /* |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 4843 | ** Return TRUE if the cursor is not pointing at an entry of the table. |
| 4844 | ** |
| 4845 | ** TRUE will be returned after a call to sqlite3BtreeNext() moves |
| 4846 | ** past the last entry in the table or sqlite3BtreePrev() moves past |
| 4847 | ** the first entry. TRUE is also returned if the table is empty. |
| 4848 | */ |
| 4849 | int sqlite3BtreeEof(BtCursor *pCur){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4850 | /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries |
| 4851 | ** have been deleted? This API will need to change to return an error code |
| 4852 | ** as well as the boolean result value. |
| 4853 | */ |
| 4854 | return (CURSOR_VALID!=pCur->eState); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 4855 | } |
| 4856 | |
| 4857 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4858 | ** Advance the cursor to the next entry in the database. If |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4859 | ** successful then set *pRes=0. If the cursor |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4860 | ** was already pointing to the last entry in the database before |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4861 | ** this routine was called, then set *pRes=1. |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 4862 | ** |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 4863 | ** The main entry point is sqlite3BtreeNext(). That routine is optimized |
| 4864 | ** for the common case of merely incrementing the cell counter BtCursor.aiIdx |
| 4865 | ** to the next cell on the current page. The (slower) btreeNext() helper |
| 4866 | ** routine is called when it is necessary to move to a different page or |
| 4867 | ** to restore the cursor. |
| 4868 | ** |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 4869 | ** The calling function will set *pRes to 0 or 1. The initial *pRes value |
| 4870 | ** will be 1 if the cursor being stepped corresponds to an SQL index and |
| 4871 | ** if this routine could have been skipped if that SQL index had been |
| 4872 | ** a unique index. Otherwise the caller will have set *pRes to zero. |
| 4873 | ** Zero is the common case. The btree implementation is free to use the |
| 4874 | ** initial *pRes value as a hint to improve performance, but the current |
| 4875 | ** SQLite btree implementation does not. (Note that the comdb2 btree |
| 4876 | ** implementation does use this hint, however.) |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4877 | */ |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 4878 | static SQLITE_NOINLINE int btreeNext(BtCursor *pCur, int *pRes){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4879 | int rc; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4880 | int idx; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 4881 | MemPage *pPage; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4882 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4883 | assert( cursorHoldsMutex(pCur) ); |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 4884 | assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID ); |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 4885 | assert( *pRes==0 ); |
drh | f66f26a | 2013-08-19 20:04:10 +0000 | [diff] [blame] | 4886 | if( pCur->eState!=CURSOR_VALID ){ |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 4887 | assert( (pCur->curFlags & BTCF_ValidOvfl)==0 ); |
drh | f66f26a | 2013-08-19 20:04:10 +0000 | [diff] [blame] | 4888 | rc = restoreCursorPosition(pCur); |
| 4889 | if( rc!=SQLITE_OK ){ |
| 4890 | return rc; |
| 4891 | } |
| 4892 | if( CURSOR_INVALID==pCur->eState ){ |
| 4893 | *pRes = 1; |
| 4894 | return SQLITE_OK; |
| 4895 | } |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 4896 | if( pCur->skipNext ){ |
| 4897 | assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT ); |
| 4898 | pCur->eState = CURSOR_VALID; |
| 4899 | if( pCur->skipNext>0 ){ |
| 4900 | pCur->skipNext = 0; |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 4901 | return SQLITE_OK; |
| 4902 | } |
drh | f66f26a | 2013-08-19 20:04:10 +0000 | [diff] [blame] | 4903 | pCur->skipNext = 0; |
drh | f66f26a | 2013-08-19 20:04:10 +0000 | [diff] [blame] | 4904 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4905 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4906 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4907 | pPage = pCur->apPage[pCur->iPage]; |
| 4908 | idx = ++pCur->aiIdx[pCur->iPage]; |
| 4909 | assert( pPage->isInit ); |
dan | bb246c4 | 2012-01-12 14:25:55 +0000 | [diff] [blame] | 4910 | |
| 4911 | /* If the database file is corrupt, it is possible for the value of idx |
| 4912 | ** to be invalid here. This can only occur if a second cursor modifies |
| 4913 | ** the page while cursor pCur is holding a reference to it. Which can |
| 4914 | ** only happen if the database is corrupt in such a way as to link the |
| 4915 | ** page into more than one b-tree structure. */ |
| 4916 | testcase( idx>pPage->nCell ); |
danielk1977 | 6a43f9b | 2004-11-16 04:57:24 +0000 | [diff] [blame] | 4917 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4918 | if( idx>=pPage->nCell ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4919 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4920 | rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 4921 | if( rc ) return rc; |
| 4922 | return moveToLeftmost(pCur); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4923 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4924 | do{ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4925 | if( pCur->iPage==0 ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4926 | *pRes = 1; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4927 | pCur->eState = CURSOR_INVALID; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4928 | return SQLITE_OK; |
| 4929 | } |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 4930 | moveToParent(pCur); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 4931 | pPage = pCur->apPage[pCur->iPage]; |
| 4932 | }while( pCur->aiIdx[pCur->iPage]>=pPage->nCell ); |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 4933 | if( pPage->intKey ){ |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 4934 | return sqlite3BtreeNext(pCur, pRes); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4935 | }else{ |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 4936 | return SQLITE_OK; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4937 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4938 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4939 | if( pPage->leaf ){ |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4940 | return SQLITE_OK; |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 4941 | }else{ |
| 4942 | return moveToLeftmost(pCur); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4943 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4944 | } |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 4945 | int sqlite3BtreeNext(BtCursor *pCur, int *pRes){ |
| 4946 | MemPage *pPage; |
| 4947 | assert( cursorHoldsMutex(pCur) ); |
| 4948 | assert( pRes!=0 ); |
| 4949 | assert( *pRes==0 || *pRes==1 ); |
| 4950 | assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID ); |
| 4951 | pCur->info.nSize = 0; |
| 4952 | pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); |
| 4953 | *pRes = 0; |
| 4954 | if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur, pRes); |
| 4955 | pPage = pCur->apPage[pCur->iPage]; |
| 4956 | if( (++pCur->aiIdx[pCur->iPage])>=pPage->nCell ){ |
| 4957 | pCur->aiIdx[pCur->iPage]--; |
| 4958 | return btreeNext(pCur, pRes); |
| 4959 | } |
| 4960 | if( pPage->leaf ){ |
| 4961 | return SQLITE_OK; |
| 4962 | }else{ |
| 4963 | return moveToLeftmost(pCur); |
| 4964 | } |
| 4965 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 4966 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4967 | /* |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4968 | ** 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] | 4969 | ** successful then set *pRes=0. If the cursor |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4970 | ** was already pointing to the first entry in the database before |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4971 | ** this routine was called, then set *pRes=1. |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 4972 | ** |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 4973 | ** The main entry point is sqlite3BtreePrevious(). That routine is optimized |
| 4974 | ** for the common case of merely decrementing the cell counter BtCursor.aiIdx |
drh | 3f38740 | 2014-09-24 01:23:00 +0000 | [diff] [blame] | 4975 | ** to the previous cell on the current page. The (slower) btreePrevious() |
| 4976 | ** helper routine is called when it is necessary to move to a different page |
| 4977 | ** or to restore the cursor. |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 4978 | ** |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 4979 | ** The calling function will set *pRes to 0 or 1. The initial *pRes value |
| 4980 | ** will be 1 if the cursor being stepped corresponds to an SQL index and |
| 4981 | ** if this routine could have been skipped if that SQL index had been |
| 4982 | ** a unique index. Otherwise the caller will have set *pRes to zero. |
| 4983 | ** Zero is the common case. The btree implementation is free to use the |
| 4984 | ** initial *pRes value as a hint to improve performance, but the current |
| 4985 | ** SQLite btree implementation does not. (Note that the comdb2 btree |
| 4986 | ** implementation does use this hint, however.) |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4987 | */ |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 4988 | static SQLITE_NOINLINE int btreePrevious(BtCursor *pCur, int *pRes){ |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 4989 | int rc; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 4990 | MemPage *pPage; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 4991 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4992 | assert( cursorHoldsMutex(pCur) ); |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 4993 | assert( pRes!=0 ); |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 4994 | assert( *pRes==0 ); |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 4995 | assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID ); |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 4996 | assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 ); |
| 4997 | assert( pCur->info.nSize==0 ); |
drh | f66f26a | 2013-08-19 20:04:10 +0000 | [diff] [blame] | 4998 | if( pCur->eState!=CURSOR_VALID ){ |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 4999 | assert( pCur->eState>=CURSOR_REQUIRESEEK ); |
| 5000 | rc = btreeRestoreCursorPosition(pCur); |
| 5001 | if( rc!=SQLITE_OK ){ |
| 5002 | return rc; |
drh | f66f26a | 2013-08-19 20:04:10 +0000 | [diff] [blame] | 5003 | } |
| 5004 | if( CURSOR_INVALID==pCur->eState ){ |
| 5005 | *pRes = 1; |
| 5006 | return SQLITE_OK; |
| 5007 | } |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 5008 | if( pCur->skipNext ){ |
| 5009 | assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT ); |
| 5010 | pCur->eState = CURSOR_VALID; |
| 5011 | if( pCur->skipNext<0 ){ |
| 5012 | pCur->skipNext = 0; |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 5013 | return SQLITE_OK; |
| 5014 | } |
drh | f66f26a | 2013-08-19 20:04:10 +0000 | [diff] [blame] | 5015 | pCur->skipNext = 0; |
drh | f66f26a | 2013-08-19 20:04:10 +0000 | [diff] [blame] | 5016 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5017 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5018 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5019 | pPage = pCur->apPage[pCur->iPage]; |
| 5020 | assert( pPage->isInit ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5021 | if( !pPage->leaf ){ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5022 | int idx = pCur->aiIdx[pCur->iPage]; |
| 5023 | rc = moveToChild(pCur, get4byte(findCell(pPage, idx))); |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 5024 | if( rc ) return rc; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 5025 | rc = moveToRightmost(pCur); |
| 5026 | }else{ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5027 | while( pCur->aiIdx[pCur->iPage]==0 ){ |
| 5028 | if( pCur->iPage==0 ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5029 | pCur->eState = CURSOR_INVALID; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 5030 | *pRes = 1; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 5031 | return SQLITE_OK; |
| 5032 | } |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 5033 | moveToParent(pCur); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 5034 | } |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 5035 | assert( pCur->info.nSize==0 ); |
| 5036 | assert( (pCur->curFlags & (BTCF_ValidNKey|BTCF_ValidOvfl))==0 ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 5037 | |
| 5038 | pCur->aiIdx[pCur->iPage]--; |
| 5039 | pPage = pCur->apPage[pCur->iPage]; |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 5040 | if( pPage->intKey && !pPage->leaf ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5041 | rc = sqlite3BtreePrevious(pCur, pRes); |
| 5042 | }else{ |
| 5043 | rc = SQLITE_OK; |
| 5044 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 5045 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 5046 | return rc; |
| 5047 | } |
drh | ee6438d | 2014-09-01 13:29:32 +0000 | [diff] [blame] | 5048 | int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){ |
| 5049 | assert( cursorHoldsMutex(pCur) ); |
| 5050 | assert( pRes!=0 ); |
| 5051 | assert( *pRes==0 || *pRes==1 ); |
| 5052 | assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID ); |
| 5053 | *pRes = 0; |
| 5054 | pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey); |
| 5055 | pCur->info.nSize = 0; |
| 5056 | if( pCur->eState!=CURSOR_VALID |
| 5057 | || pCur->aiIdx[pCur->iPage]==0 |
| 5058 | || pCur->apPage[pCur->iPage]->leaf==0 |
| 5059 | ){ |
| 5060 | return btreePrevious(pCur, pRes); |
| 5061 | } |
| 5062 | pCur->aiIdx[pCur->iPage]--; |
| 5063 | return SQLITE_OK; |
| 5064 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 5065 | |
| 5066 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5067 | ** Allocate a new page from the database file. |
| 5068 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5069 | ** The new page is marked as dirty. (In other words, sqlite3PagerWrite() |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5070 | ** has already been called on the new page.) The new page has also |
| 5071 | ** been referenced and the calling routine is responsible for calling |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5072 | ** sqlite3PagerUnref() on the new page when it is done. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5073 | ** |
| 5074 | ** SQLITE_OK is returned on success. Any other return value indicates |
| 5075 | ** an error. *ppPage and *pPgno are undefined in the event of an error. |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5076 | ** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned. |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 5077 | ** |
drh | 82e647d | 2013-03-02 03:25:55 +0000 | [diff] [blame] | 5078 | ** If the "nearby" parameter is not 0, then an effort is made to |
drh | 199e3cf | 2002-07-18 11:01:47 +0000 | [diff] [blame] | 5079 | ** 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] | 5080 | ** attempt to keep related pages close to each other in the database file, |
| 5081 | ** which in turn can make database access faster. |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5082 | ** |
drh | 82e647d | 2013-03-02 03:25:55 +0000 | [diff] [blame] | 5083 | ** If the eMode parameter is BTALLOC_EXACT and the nearby page exists |
| 5084 | ** anywhere on the free-list, then it is guaranteed to be returned. If |
| 5085 | ** eMode is BTALLOC_LT then the page returned will be less than or equal |
| 5086 | ** to nearby if any such page exists. If eMode is BTALLOC_ANY then there |
| 5087 | ** are no restrictions on which page is returned. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5088 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5089 | static int allocateBtreePage( |
drh | 82e647d | 2013-03-02 03:25:55 +0000 | [diff] [blame] | 5090 | BtShared *pBt, /* The btree */ |
| 5091 | MemPage **ppPage, /* Store pointer to the allocated page here */ |
| 5092 | Pgno *pPgno, /* Store the page number here */ |
| 5093 | Pgno nearby, /* Search for a page near this one */ |
| 5094 | u8 eMode /* BTALLOC_EXACT, BTALLOC_LT, or BTALLOC_ANY */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5095 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5096 | MemPage *pPage1; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5097 | int rc; |
drh | 35cd643 | 2009-06-05 14:17:21 +0000 | [diff] [blame] | 5098 | u32 n; /* Number of pages on the freelist */ |
drh | 042d6a1 | 2009-06-17 13:57:16 +0000 | [diff] [blame] | 5099 | u32 k; /* Number of leaves on the trunk of the freelist */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 5100 | MemPage *pTrunk = 0; |
| 5101 | MemPage *pPrevTrunk = 0; |
drh | 1662b5a | 2009-06-04 19:06:09 +0000 | [diff] [blame] | 5102 | Pgno mxPage; /* Total size of the database file */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 5103 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5104 | assert( sqlite3_mutex_held(pBt->mutex) ); |
dan | 09ff9e1 | 2013-03-11 11:49:03 +0000 | [diff] [blame] | 5105 | assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5106 | pPage1 = pBt->pPage1; |
drh | b129915 | 2010-03-30 22:58:33 +0000 | [diff] [blame] | 5107 | mxPage = btreePagecount(pBt); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5108 | n = get4byte(&pPage1->aData[36]); |
drh | df35a08 | 2009-07-09 02:24:35 +0000 | [diff] [blame] | 5109 | testcase( n==mxPage-1 ); |
| 5110 | if( n>=mxPage ){ |
drh | 1662b5a | 2009-06-04 19:06:09 +0000 | [diff] [blame] | 5111 | return SQLITE_CORRUPT_BKPT; |
| 5112 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5113 | if( n>0 ){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5114 | /* There are pages on the freelist. Reuse one of those pages. */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5115 | Pgno iTrunk; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5116 | u8 searchList = 0; /* If the free-list must be searched for 'nearby' */ |
| 5117 | |
drh | 82e647d | 2013-03-02 03:25:55 +0000 | [diff] [blame] | 5118 | /* If eMode==BTALLOC_EXACT and a query of the pointer-map |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5119 | ** shows that the page 'nearby' is somewhere on the free-list, then |
| 5120 | ** the entire-list will be searched for that page. |
| 5121 | */ |
| 5122 | #ifndef SQLITE_OMIT_AUTOVACUUM |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 5123 | if( eMode==BTALLOC_EXACT ){ |
| 5124 | if( nearby<=mxPage ){ |
| 5125 | u8 eType; |
| 5126 | assert( nearby>0 ); |
| 5127 | assert( pBt->autoVacuum ); |
| 5128 | rc = ptrmapGet(pBt, nearby, &eType, 0); |
| 5129 | if( rc ) return rc; |
| 5130 | if( eType==PTRMAP_FREEPAGE ){ |
| 5131 | searchList = 1; |
| 5132 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5133 | } |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 5134 | }else if( eMode==BTALLOC_LE ){ |
| 5135 | searchList = 1; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5136 | } |
| 5137 | #endif |
| 5138 | |
| 5139 | /* Decrement the free-list count by 1. Set iTrunk to the index of the |
| 5140 | ** first free-list trunk page. iPrevTrunk is initially 1. |
| 5141 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5142 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5143 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5144 | put4byte(&pPage1->aData[36], n-1); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5145 | |
| 5146 | /* The code within this loop is run only once if the 'searchList' variable |
| 5147 | ** is not true. Otherwise, it runs once for each trunk-page on the |
drh | 82e647d | 2013-03-02 03:25:55 +0000 | [diff] [blame] | 5148 | ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT) |
| 5149 | ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT) |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5150 | */ |
| 5151 | do { |
| 5152 | pPrevTrunk = pTrunk; |
| 5153 | if( pPrevTrunk ){ |
| 5154 | iTrunk = get4byte(&pPrevTrunk->aData[0]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 5155 | }else{ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5156 | iTrunk = get4byte(&pPage1->aData[32]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 5157 | } |
drh | df35a08 | 2009-07-09 02:24:35 +0000 | [diff] [blame] | 5158 | testcase( iTrunk==mxPage ); |
drh | 1662b5a | 2009-06-04 19:06:09 +0000 | [diff] [blame] | 5159 | if( iTrunk>mxPage ){ |
| 5160 | rc = SQLITE_CORRUPT_BKPT; |
| 5161 | }else{ |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 5162 | rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0); |
drh | 1662b5a | 2009-06-04 19:06:09 +0000 | [diff] [blame] | 5163 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5164 | if( rc ){ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 5165 | pTrunk = 0; |
| 5166 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5167 | } |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 5168 | assert( pTrunk!=0 ); |
| 5169 | assert( pTrunk->aData!=0 ); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5170 | |
drh | 93b4fc7 | 2011-04-07 14:47:01 +0000 | [diff] [blame] | 5171 | k = get4byte(&pTrunk->aData[4]); /* # of leaves on this trunk page */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5172 | if( k==0 && !searchList ){ |
| 5173 | /* The trunk has no leaves and the list is not being searched. |
| 5174 | ** So extract the trunk page itself and use it as the newly |
| 5175 | ** allocated page */ |
| 5176 | assert( pPrevTrunk==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5177 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 5178 | if( rc ){ |
| 5179 | goto end_allocate_page; |
| 5180 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5181 | *pPgno = iTrunk; |
| 5182 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 5183 | *ppPage = pTrunk; |
| 5184 | pTrunk = 0; |
| 5185 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
drh | 042d6a1 | 2009-06-17 13:57:16 +0000 | [diff] [blame] | 5186 | }else if( k>(u32)(pBt->usableSize/4 - 2) ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5187 | /* Value of k is out of range. Database corruption */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 5188 | rc = SQLITE_CORRUPT_BKPT; |
| 5189 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5190 | #ifndef SQLITE_OMIT_AUTOVACUUM |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 5191 | }else if( searchList |
| 5192 | && (nearby==iTrunk || (iTrunk<nearby && eMode==BTALLOC_LE)) |
| 5193 | ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5194 | /* The list is being searched and this trunk page is the page |
| 5195 | ** to allocate, regardless of whether it has leaves. |
| 5196 | */ |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 5197 | *pPgno = iTrunk; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5198 | *ppPage = pTrunk; |
| 5199 | searchList = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5200 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 5201 | if( rc ){ |
| 5202 | goto end_allocate_page; |
| 5203 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5204 | if( k==0 ){ |
| 5205 | if( !pPrevTrunk ){ |
| 5206 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 5207 | }else{ |
dan | f48c355 | 2010-08-23 15:41:24 +0000 | [diff] [blame] | 5208 | rc = sqlite3PagerWrite(pPrevTrunk->pDbPage); |
| 5209 | if( rc!=SQLITE_OK ){ |
| 5210 | goto end_allocate_page; |
| 5211 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5212 | memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4); |
| 5213 | } |
| 5214 | }else{ |
| 5215 | /* The trunk page is required by the caller but it contains |
| 5216 | ** pointers to free-list leaves. The first leaf becomes a trunk |
| 5217 | ** page in this case. |
| 5218 | */ |
| 5219 | MemPage *pNewTrunk; |
| 5220 | Pgno iNewTrunk = get4byte(&pTrunk->aData[8]); |
drh | 1662b5a | 2009-06-04 19:06:09 +0000 | [diff] [blame] | 5221 | if( iNewTrunk>mxPage ){ |
| 5222 | rc = SQLITE_CORRUPT_BKPT; |
| 5223 | goto end_allocate_page; |
| 5224 | } |
drh | df35a08 | 2009-07-09 02:24:35 +0000 | [diff] [blame] | 5225 | testcase( iNewTrunk==mxPage ); |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 5226 | rc = btreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5227 | if( rc!=SQLITE_OK ){ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 5228 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5229 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5230 | rc = sqlite3PagerWrite(pNewTrunk->pDbPage); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5231 | if( rc!=SQLITE_OK ){ |
| 5232 | releasePage(pNewTrunk); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 5233 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5234 | } |
| 5235 | memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4); |
| 5236 | put4byte(&pNewTrunk->aData[4], k-1); |
| 5237 | memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 5238 | releasePage(pNewTrunk); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5239 | if( !pPrevTrunk ){ |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 5240 | assert( sqlite3PagerIswriteable(pPage1->pDbPage) ); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5241 | put4byte(&pPage1->aData[32], iNewTrunk); |
| 5242 | }else{ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5243 | rc = sqlite3PagerWrite(pPrevTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 5244 | if( rc ){ |
| 5245 | goto end_allocate_page; |
| 5246 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5247 | put4byte(&pPrevTrunk->aData[0], iNewTrunk); |
| 5248 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5249 | } |
| 5250 | pTrunk = 0; |
| 5251 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
| 5252 | #endif |
danielk1977 | e576521 | 2009-06-17 11:13:28 +0000 | [diff] [blame] | 5253 | }else if( k>0 ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5254 | /* Extract a leaf from the trunk */ |
drh | 042d6a1 | 2009-06-17 13:57:16 +0000 | [diff] [blame] | 5255 | u32 closest; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5256 | Pgno iPage; |
| 5257 | unsigned char *aData = pTrunk->aData; |
| 5258 | if( nearby>0 ){ |
drh | 042d6a1 | 2009-06-17 13:57:16 +0000 | [diff] [blame] | 5259 | u32 i; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5260 | closest = 0; |
dan | f38b65a | 2013-02-22 20:57:47 +0000 | [diff] [blame] | 5261 | if( eMode==BTALLOC_LE ){ |
| 5262 | for(i=0; i<k; i++){ |
| 5263 | iPage = get4byte(&aData[8+i*4]); |
dan | 87ade19 | 2013-02-23 17:49:16 +0000 | [diff] [blame] | 5264 | if( iPage<=nearby ){ |
dan | f38b65a | 2013-02-22 20:57:47 +0000 | [diff] [blame] | 5265 | closest = i; |
| 5266 | break; |
| 5267 | } |
| 5268 | } |
| 5269 | }else{ |
| 5270 | int dist; |
| 5271 | dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby); |
| 5272 | for(i=1; i<k; i++){ |
| 5273 | int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby); |
| 5274 | if( d2<dist ){ |
| 5275 | closest = i; |
| 5276 | dist = d2; |
| 5277 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5278 | } |
| 5279 | } |
| 5280 | }else{ |
| 5281 | closest = 0; |
| 5282 | } |
| 5283 | |
| 5284 | iPage = get4byte(&aData[8+closest*4]); |
drh | df35a08 | 2009-07-09 02:24:35 +0000 | [diff] [blame] | 5285 | testcase( iPage==mxPage ); |
drh | 1662b5a | 2009-06-04 19:06:09 +0000 | [diff] [blame] | 5286 | if( iPage>mxPage ){ |
| 5287 | rc = SQLITE_CORRUPT_BKPT; |
| 5288 | goto end_allocate_page; |
| 5289 | } |
drh | df35a08 | 2009-07-09 02:24:35 +0000 | [diff] [blame] | 5290 | testcase( iPage==mxPage ); |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 5291 | if( !searchList |
| 5292 | || (iPage==nearby || (iPage<nearby && eMode==BTALLOC_LE)) |
| 5293 | ){ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5294 | int noContent; |
shane | 1f9e6aa | 2008-06-09 19:27:11 +0000 | [diff] [blame] | 5295 | *pPgno = iPage; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5296 | TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d" |
| 5297 | ": %d more free pages\n", |
| 5298 | *pPgno, closest+1, k, pTrunk->pgno, n-1)); |
drh | 93b4fc7 | 2011-04-07 14:47:01 +0000 | [diff] [blame] | 5299 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
| 5300 | if( rc ) goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5301 | if( closest<k-1 ){ |
| 5302 | memcpy(&aData[8+closest*4], &aData[4+k*4], 4); |
| 5303 | } |
| 5304 | put4byte(&aData[4], k-1); |
drh | 3f38740 | 2014-09-24 01:23:00 +0000 | [diff] [blame] | 5305 | noContent = !btreeGetHasContent(pBt, *pPgno)? PAGER_GET_NOCONTENT : 0; |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 5306 | rc = btreeGetPage(pBt, *pPgno, ppPage, noContent); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5307 | if( rc==SQLITE_OK ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5308 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 5309 | if( rc!=SQLITE_OK ){ |
| 5310 | releasePage(*ppPage); |
| 5311 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5312 | } |
| 5313 | searchList = 0; |
| 5314 | } |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 5315 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5316 | releasePage(pPrevTrunk); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 5317 | pPrevTrunk = 0; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5318 | }while( searchList ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5319 | }else{ |
dan | bc1a3c6 | 2013-02-23 16:40:46 +0000 | [diff] [blame] | 5320 | /* There are no pages on the freelist, so append a new page to the |
| 5321 | ** database image. |
| 5322 | ** |
| 5323 | ** Normally, new pages allocated by this block can be requested from the |
| 5324 | ** pager layer with the 'no-content' flag set. This prevents the pager |
| 5325 | ** from trying to read the pages content from disk. However, if the |
| 5326 | ** current transaction has already run one or more incremental-vacuum |
| 5327 | ** steps, then the page we are about to allocate may contain content |
| 5328 | ** that is required in the event of a rollback. In this case, do |
| 5329 | ** not set the no-content flag. This causes the pager to load and journal |
| 5330 | ** the current page content before overwriting it. |
| 5331 | ** |
| 5332 | ** Note that the pager will not actually attempt to load or journal |
| 5333 | ** content for any page that really does lie past the end of the database |
| 5334 | ** file on disk. So the effects of disabling the no-content optimization |
| 5335 | ** here are confined to those pages that lie between the end of the |
| 5336 | ** database image and the end of the database file. |
| 5337 | */ |
drh | 3f38740 | 2014-09-24 01:23:00 +0000 | [diff] [blame] | 5338 | int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0; |
dan | bc1a3c6 | 2013-02-23 16:40:46 +0000 | [diff] [blame] | 5339 | |
drh | dd3cd97 | 2010-03-27 17:12:36 +0000 | [diff] [blame] | 5340 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 5341 | if( rc ) return rc; |
| 5342 | pBt->nPage++; |
| 5343 | if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++; |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5344 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5345 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | dd3cd97 | 2010-03-27 17:12:36 +0000 | [diff] [blame] | 5346 | if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5347 | /* If *pPgno refers to a pointer-map page, allocate two new pages |
| 5348 | ** at the end of the file instead of one. The first allocated page |
| 5349 | ** becomes a new pointer-map page, the second is used by the caller. |
| 5350 | */ |
danielk1977 | ac86169 | 2009-03-28 10:54:22 +0000 | [diff] [blame] | 5351 | MemPage *pPg = 0; |
drh | dd3cd97 | 2010-03-27 17:12:36 +0000 | [diff] [blame] | 5352 | TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage)); |
| 5353 | assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) ); |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 5354 | rc = btreeGetPage(pBt, pBt->nPage, &pPg, bNoContent); |
danielk1977 | ac86169 | 2009-03-28 10:54:22 +0000 | [diff] [blame] | 5355 | if( rc==SQLITE_OK ){ |
| 5356 | rc = sqlite3PagerWrite(pPg->pDbPage); |
| 5357 | releasePage(pPg); |
| 5358 | } |
| 5359 | if( rc ) return rc; |
drh | dd3cd97 | 2010-03-27 17:12:36 +0000 | [diff] [blame] | 5360 | pBt->nPage++; |
| 5361 | if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5362 | } |
| 5363 | #endif |
drh | dd3cd97 | 2010-03-27 17:12:36 +0000 | [diff] [blame] | 5364 | put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage); |
| 5365 | *pPgno = pBt->nPage; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5366 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5367 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 5368 | rc = btreeGetPage(pBt, *pPgno, ppPage, bNoContent); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5369 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5370 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 5371 | if( rc!=SQLITE_OK ){ |
| 5372 | releasePage(*ppPage); |
| 5373 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5374 | TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5375 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5376 | |
| 5377 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 5378 | |
| 5379 | end_allocate_page: |
| 5380 | releasePage(pTrunk); |
| 5381 | releasePage(pPrevTrunk); |
danielk1977 | b247c21 | 2008-11-21 09:09:01 +0000 | [diff] [blame] | 5382 | if( rc==SQLITE_OK ){ |
| 5383 | if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){ |
| 5384 | releasePage(*ppPage); |
dan | 7df42ab | 2014-01-20 18:25:44 +0000 | [diff] [blame] | 5385 | *ppPage = 0; |
danielk1977 | b247c21 | 2008-11-21 09:09:01 +0000 | [diff] [blame] | 5386 | return SQLITE_CORRUPT_BKPT; |
| 5387 | } |
| 5388 | (*ppPage)->isInit = 0; |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 5389 | }else{ |
| 5390 | *ppPage = 0; |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 5391 | } |
drh | 93b4fc7 | 2011-04-07 14:47:01 +0000 | [diff] [blame] | 5392 | assert( rc!=SQLITE_OK || sqlite3PagerIswriteable((*ppPage)->pDbPage) ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5393 | return rc; |
| 5394 | } |
| 5395 | |
| 5396 | /* |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5397 | ** This function is used to add page iPage to the database file free-list. |
| 5398 | ** 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] | 5399 | ** |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5400 | ** The value passed as the second argument to this function is optional. |
| 5401 | ** If the caller happens to have a pointer to the MemPage object |
| 5402 | ** corresponding to page iPage handy, it may pass it as the second value. |
| 5403 | ** Otherwise, it may pass NULL. |
| 5404 | ** |
| 5405 | ** If a pointer to a MemPage object is passed as the second argument, |
| 5406 | ** its reference count is not altered by this function. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5407 | */ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5408 | static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){ |
| 5409 | MemPage *pTrunk = 0; /* Free-list trunk page */ |
| 5410 | Pgno iTrunk = 0; /* Page number of free-list trunk page */ |
| 5411 | MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */ |
| 5412 | MemPage *pPage; /* Page being freed. May be NULL. */ |
| 5413 | int rc; /* Return Code */ |
| 5414 | int nFree; /* Initial number of pages on free-list */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5415 | |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5416 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 5417 | assert( iPage>1 ); |
| 5418 | assert( !pMemPage || pMemPage->pgno==iPage ); |
| 5419 | |
| 5420 | if( pMemPage ){ |
| 5421 | pPage = pMemPage; |
| 5422 | sqlite3PagerRef(pPage->pDbPage); |
| 5423 | }else{ |
| 5424 | pPage = btreePageLookup(pBt, iPage); |
| 5425 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5426 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5427 | /* Increment the free page count on pPage1 */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5428 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5429 | if( rc ) goto freepage_out; |
| 5430 | nFree = get4byte(&pPage1->aData[36]); |
| 5431 | put4byte(&pPage1->aData[36], nFree+1); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5432 | |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 5433 | if( pBt->btsFlags & BTS_SECURE_DELETE ){ |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 5434 | /* If the secure_delete option is enabled, then |
| 5435 | ** always fully overwrite deleted information with zeros. |
| 5436 | */ |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 5437 | if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) ) |
shaneh | 84f4b2f | 2010-02-26 01:46:54 +0000 | [diff] [blame] | 5438 | || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0) |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 5439 | ){ |
| 5440 | goto freepage_out; |
| 5441 | } |
| 5442 | memset(pPage->aData, 0, pPage->pBt->pageSize); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5443 | } |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 5444 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5445 | /* If the database supports auto-vacuum, write an entry in the pointer-map |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5446 | ** to indicate that the page is free. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5447 | */ |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 5448 | if( ISAUTOVACUUM ){ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 5449 | ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5450 | if( rc ) goto freepage_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5451 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5452 | |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5453 | /* Now manipulate the actual database free-list structure. There are two |
| 5454 | ** possibilities. If the free-list is currently empty, or if the first |
| 5455 | ** trunk page in the free-list is full, then this page will become a |
| 5456 | ** new free-list trunk page. Otherwise, it will become a leaf of the |
| 5457 | ** first trunk page in the current free-list. This block tests if it |
| 5458 | ** is possible to add the page as a new free-list leaf. |
| 5459 | */ |
| 5460 | if( nFree!=0 ){ |
drh | c046e3e | 2009-07-15 11:26:44 +0000 | [diff] [blame] | 5461 | u32 nLeaf; /* Initial number of leaf cells on trunk page */ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5462 | |
| 5463 | iTrunk = get4byte(&pPage1->aData[32]); |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 5464 | rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0); |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5465 | if( rc!=SQLITE_OK ){ |
| 5466 | goto freepage_out; |
| 5467 | } |
| 5468 | |
| 5469 | nLeaf = get4byte(&pTrunk->aData[4]); |
drh | eeb844a | 2009-08-08 18:01:07 +0000 | [diff] [blame] | 5470 | assert( pBt->usableSize>32 ); |
| 5471 | if( nLeaf > (u32)pBt->usableSize/4 - 2 ){ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5472 | rc = SQLITE_CORRUPT_BKPT; |
| 5473 | goto freepage_out; |
| 5474 | } |
drh | eeb844a | 2009-08-08 18:01:07 +0000 | [diff] [blame] | 5475 | if( nLeaf < (u32)pBt->usableSize/4 - 8 ){ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5476 | /* In this case there is room on the trunk page to insert the page |
| 5477 | ** being freed as a new leaf. |
drh | 45b1fac | 2008-07-04 17:52:42 +0000 | [diff] [blame] | 5478 | ** |
| 5479 | ** Note that the trunk page is not really full until it contains |
| 5480 | ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have |
| 5481 | ** coded. But due to a coding error in versions of SQLite prior to |
| 5482 | ** 3.6.0, databases with freelist trunk pages holding more than |
| 5483 | ** usableSize/4 - 8 entries will be reported as corrupt. In order |
| 5484 | ** to maintain backwards compatibility with older versions of SQLite, |
drh | c046e3e | 2009-07-15 11:26:44 +0000 | [diff] [blame] | 5485 | ** we will continue to restrict the number of entries to usableSize/4 - 8 |
drh | 45b1fac | 2008-07-04 17:52:42 +0000 | [diff] [blame] | 5486 | ** for now. At some point in the future (once everyone has upgraded |
| 5487 | ** to 3.6.0 or later) we should consider fixing the conditional above |
| 5488 | ** to read "usableSize/4-2" instead of "usableSize/4-8". |
| 5489 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5490 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 5491 | if( rc==SQLITE_OK ){ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5492 | put4byte(&pTrunk->aData[4], nLeaf+1); |
| 5493 | put4byte(&pTrunk->aData[8+nLeaf*4], iPage); |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 5494 | if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5495 | sqlite3PagerDontWrite(pPage->pDbPage); |
| 5496 | } |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5497 | rc = btreeSetHasContent(pBt, iPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 5498 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5499 | 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] | 5500 | goto freepage_out; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5501 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5502 | } |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5503 | |
| 5504 | /* If control flows to this point, then it was not possible to add the |
| 5505 | ** the page being freed as a leaf page of the first trunk in the free-list. |
| 5506 | ** Possibly because the free-list is empty, or possibly because the |
| 5507 | ** first trunk in the free-list is full. Either way, the page being freed |
| 5508 | ** will become the new first trunk page in the free-list. |
| 5509 | */ |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 5510 | if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){ |
drh | c046e3e | 2009-07-15 11:26:44 +0000 | [diff] [blame] | 5511 | goto freepage_out; |
| 5512 | } |
| 5513 | rc = sqlite3PagerWrite(pPage->pDbPage); |
| 5514 | if( rc!=SQLITE_OK ){ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5515 | goto freepage_out; |
| 5516 | } |
| 5517 | put4byte(pPage->aData, iTrunk); |
| 5518 | put4byte(&pPage->aData[4], 0); |
| 5519 | put4byte(&pPage1->aData[32], iPage); |
| 5520 | TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk)); |
| 5521 | |
| 5522 | freepage_out: |
| 5523 | if( pPage ){ |
| 5524 | pPage->isInit = 0; |
| 5525 | } |
| 5526 | releasePage(pPage); |
| 5527 | releasePage(pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5528 | return rc; |
| 5529 | } |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 5530 | static void freePage(MemPage *pPage, int *pRC){ |
| 5531 | if( (*pRC)==SQLITE_OK ){ |
| 5532 | *pRC = freePage2(pPage->pBt, pPage, pPage->pgno); |
| 5533 | } |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5534 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5535 | |
| 5536 | /* |
drh | 9bfdc25 | 2014-09-24 02:05:41 +0000 | [diff] [blame] | 5537 | ** Free any overflow pages associated with the given Cell. Write the |
| 5538 | ** local Cell size (the number of bytes on the original page, omitting |
| 5539 | ** overflow) into *pnSize. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5540 | */ |
drh | 9bfdc25 | 2014-09-24 02:05:41 +0000 | [diff] [blame] | 5541 | static int clearCell( |
| 5542 | MemPage *pPage, /* The page that contains the Cell */ |
| 5543 | unsigned char *pCell, /* First byte of the Cell */ |
| 5544 | u16 *pnSize /* Write the size of the Cell here */ |
| 5545 | ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5546 | BtShared *pBt = pPage->pBt; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5547 | CellInfo info; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5548 | Pgno ovflPgno; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5549 | int rc; |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 5550 | int nOvfl; |
shaneh | 1df2db7 | 2010-08-18 02:28:48 +0000 | [diff] [blame] | 5551 | u32 ovflPageSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5552 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5553 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 5554 | btreeParseCellPtr(pPage, pCell, &info); |
drh | 9bfdc25 | 2014-09-24 02:05:41 +0000 | [diff] [blame] | 5555 | *pnSize = info.nSize; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5556 | if( info.iOverflow==0 ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5557 | return SQLITE_OK; /* No overflow pages. Return without doing anything */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5558 | } |
drh | e42a9b4 | 2011-08-31 13:27:19 +0000 | [diff] [blame] | 5559 | if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){ |
mistachkin | 70a1b71 | 2012-09-28 18:13:35 +0000 | [diff] [blame] | 5560 | return SQLITE_CORRUPT_BKPT; /* Cell extends past end of page */ |
drh | e42a9b4 | 2011-08-31 13:27:19 +0000 | [diff] [blame] | 5561 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5562 | ovflPgno = get4byte(&pCell[info.iOverflow]); |
shane | 63207ab | 2009-02-04 01:49:30 +0000 | [diff] [blame] | 5563 | assert( pBt->usableSize > 4 ); |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 5564 | ovflPageSize = pBt->usableSize - 4; |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 5565 | nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize; |
| 5566 | assert( ovflPgno==0 || nOvfl>0 ); |
| 5567 | while( nOvfl-- ){ |
shane | 63207ab | 2009-02-04 01:49:30 +0000 | [diff] [blame] | 5568 | Pgno iNext = 0; |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5569 | MemPage *pOvfl = 0; |
drh | b129915 | 2010-03-30 22:58:33 +0000 | [diff] [blame] | 5570 | if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){ |
danielk1977 | e589a67 | 2009-04-11 16:06:15 +0000 | [diff] [blame] | 5571 | /* 0 is not a legal page number and page 1 cannot be an |
| 5572 | ** overflow page. Therefore if ovflPgno<2 or past the end of the |
| 5573 | ** file the database must be corrupt. */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 5574 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 5575 | } |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5576 | if( nOvfl ){ |
| 5577 | rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext); |
| 5578 | if( rc ) return rc; |
| 5579 | } |
dan | 887d4b2 | 2010-02-25 12:09:16 +0000 | [diff] [blame] | 5580 | |
shaneh | 1da207e | 2010-03-09 14:41:12 +0000 | [diff] [blame] | 5581 | if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) ) |
dan | 887d4b2 | 2010-02-25 12:09:16 +0000 | [diff] [blame] | 5582 | && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1 |
| 5583 | ){ |
| 5584 | /* There is no reason any cursor should have an outstanding reference |
| 5585 | ** to an overflow page belonging to a cell that is being deleted/updated. |
| 5586 | ** So if there exists more than one reference to this page, then it |
| 5587 | ** must not really be an overflow page and the database must be corrupt. |
| 5588 | ** It is helpful to detect this before calling freePage2(), as |
| 5589 | ** freePage2() may zero the page contents if secure-delete mode is |
| 5590 | ** enabled. If this 'overflow' page happens to be a page that the |
| 5591 | ** caller is iterating through or using in some other way, this |
| 5592 | ** can be problematic. |
| 5593 | */ |
| 5594 | rc = SQLITE_CORRUPT_BKPT; |
| 5595 | }else{ |
| 5596 | rc = freePage2(pBt, pOvfl, ovflPgno); |
| 5597 | } |
| 5598 | |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5599 | if( pOvfl ){ |
| 5600 | sqlite3PagerUnref(pOvfl->pDbPage); |
| 5601 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5602 | if( rc ) return rc; |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 5603 | ovflPgno = iNext; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5604 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5605 | return SQLITE_OK; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5606 | } |
| 5607 | |
| 5608 | /* |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5609 | ** Create the byte sequence used to represent a cell on page pPage |
| 5610 | ** and write that byte sequence into pCell[]. Overflow pages are |
| 5611 | ** allocated and filled in as necessary. The calling procedure |
| 5612 | ** is responsible for making sure sufficient space has been allocated |
| 5613 | ** for pCell[]. |
| 5614 | ** |
| 5615 | ** Note that pCell does not necessary need to point to the pPage->aData |
| 5616 | ** area. pCell might point to some temporary storage. The cell will |
| 5617 | ** be constructed in this temporary area then copied into pPage->aData |
| 5618 | ** later. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5619 | */ |
| 5620 | static int fillInCell( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5621 | MemPage *pPage, /* The page that contains the cell */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5622 | unsigned char *pCell, /* Complete text of the cell */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 5623 | const void *pKey, i64 nKey, /* The key */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5624 | const void *pData,int nData, /* The data */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5625 | int nZero, /* Extra zero bytes to append to pData */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5626 | int *pnSize /* Write cell size here */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5627 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5628 | int nPayload; |
drh | 8c6fa9b | 2004-05-26 00:01:53 +0000 | [diff] [blame] | 5629 | const u8 *pSrc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5630 | int nSrc, n, rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5631 | int spaceLeft; |
| 5632 | MemPage *pOvfl = 0; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 5633 | MemPage *pToRelease = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5634 | unsigned char *pPrior; |
| 5635 | unsigned char *pPayload; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5636 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5637 | Pgno pgnoOvfl = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5638 | int nHeader; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5639 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5640 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5641 | |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 5642 | /* pPage is not necessarily writeable since pCell might be auxiliary |
| 5643 | ** buffer space that is separate from the pPage buffer area */ |
| 5644 | assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize] |
| 5645 | || sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 5646 | |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5647 | /* Fill in the header. */ |
drh | 6200c88 | 2014-09-23 22:36:25 +0000 | [diff] [blame] | 5648 | nHeader = pPage->childPtrSize; |
| 5649 | nPayload = nData + nZero; |
drh | 3e28ff5 | 2014-09-24 00:59:08 +0000 | [diff] [blame] | 5650 | if( pPage->intKeyLeaf ){ |
drh | 6200c88 | 2014-09-23 22:36:25 +0000 | [diff] [blame] | 5651 | nHeader += putVarint32(&pCell[nHeader], nPayload); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5652 | }else{ |
drh | 6200c88 | 2014-09-23 22:36:25 +0000 | [diff] [blame] | 5653 | assert( nData==0 ); |
| 5654 | assert( nZero==0 ); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5655 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5656 | nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5657 | |
drh | 6200c88 | 2014-09-23 22:36:25 +0000 | [diff] [blame] | 5658 | /* Fill in the payload size */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5659 | if( pPage->intKey ){ |
| 5660 | pSrc = pData; |
| 5661 | nSrc = nData; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5662 | nData = 0; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 5663 | }else{ |
danielk1977 | 31d31b8 | 2009-07-13 13:18:07 +0000 | [diff] [blame] | 5664 | if( NEVER(nKey>0x7fffffff || pKey==0) ){ |
| 5665 | return SQLITE_CORRUPT_BKPT; |
drh | 20abac2 | 2009-01-28 20:21:17 +0000 | [diff] [blame] | 5666 | } |
drh | 6200c88 | 2014-09-23 22:36:25 +0000 | [diff] [blame] | 5667 | nPayload = (int)nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5668 | pSrc = pKey; |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 5669 | nSrc = (int)nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5670 | } |
drh | 6200c88 | 2014-09-23 22:36:25 +0000 | [diff] [blame] | 5671 | if( nPayload<=pPage->maxLocal ){ |
| 5672 | n = nHeader + nPayload; |
| 5673 | testcase( n==3 ); |
| 5674 | testcase( n==4 ); |
| 5675 | if( n<4 ) n = 4; |
| 5676 | *pnSize = n; |
| 5677 | spaceLeft = nPayload; |
| 5678 | pPrior = pCell; |
| 5679 | }else{ |
| 5680 | int mn = pPage->minLocal; |
| 5681 | n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4); |
| 5682 | testcase( n==pPage->maxLocal ); |
| 5683 | testcase( n==pPage->maxLocal+1 ); |
| 5684 | if( n > pPage->maxLocal ) n = mn; |
| 5685 | spaceLeft = n; |
| 5686 | *pnSize = n + nHeader + 4; |
| 5687 | pPrior = &pCell[nHeader+n]; |
| 5688 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5689 | pPayload = &pCell[nHeader]; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5690 | |
drh | 6200c88 | 2014-09-23 22:36:25 +0000 | [diff] [blame] | 5691 | /* At this point variables should be set as follows: |
| 5692 | ** |
| 5693 | ** nPayload Total payload size in bytes |
| 5694 | ** pPayload Begin writing payload here |
| 5695 | ** spaceLeft Space available at pPayload. If nPayload>spaceLeft, |
| 5696 | ** that means content must spill into overflow pages. |
| 5697 | ** *pnSize Size of the local cell (not counting overflow pages) |
| 5698 | ** pPrior Where to write the pgno of the first overflow page |
| 5699 | ** |
| 5700 | ** Use a call to btreeParseCellPtr() to verify that the values above |
| 5701 | ** were computed correctly. |
| 5702 | */ |
| 5703 | #if SQLITE_DEBUG |
| 5704 | { |
| 5705 | CellInfo info; |
| 5706 | btreeParseCellPtr(pPage, pCell, &info); |
| 5707 | assert( nHeader=(int)(info.pPayload - pCell) ); |
| 5708 | assert( info.nKey==nKey ); |
| 5709 | assert( *pnSize == info.nSize ); |
| 5710 | assert( spaceLeft == info.nLocal ); |
| 5711 | assert( pPrior == &pCell[info.iOverflow] ); |
| 5712 | } |
| 5713 | #endif |
| 5714 | |
| 5715 | /* Write the payload into the local Cell and any extra into overflow pages */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5716 | while( nPayload>0 ){ |
| 5717 | if( spaceLeft==0 ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5718 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5719 | Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */ |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 5720 | if( pBt->autoVacuum ){ |
| 5721 | do{ |
| 5722 | pgnoOvfl++; |
| 5723 | } while( |
| 5724 | PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt) |
| 5725 | ); |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 5726 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5727 | #endif |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 5728 | rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5729 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 5730 | /* If the database supports auto-vacuum, and the second or subsequent |
| 5731 | ** overflow page is being allocated, add an entry to the pointer-map |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 5732 | ** for that page now. |
| 5733 | ** |
| 5734 | ** If this is the first overflow page, then write a partial entry |
| 5735 | ** to the pointer-map. If we write nothing to this pointer-map slot, |
| 5736 | ** then the optimistic overflow chain processing in clearCell() |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 5737 | ** may misinterpret the uninitialized values and delete the |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 5738 | ** wrong pages from the database. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5739 | */ |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 5740 | if( pBt->autoVacuum && rc==SQLITE_OK ){ |
| 5741 | u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1); |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 5742 | ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc); |
danielk1977 | 89a4be8 | 2007-05-23 13:34:32 +0000 | [diff] [blame] | 5743 | if( rc ){ |
| 5744 | releasePage(pOvfl); |
| 5745 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5746 | } |
| 5747 | #endif |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5748 | if( rc ){ |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 5749 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5750 | return rc; |
| 5751 | } |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 5752 | |
| 5753 | /* If pToRelease is not zero than pPrior points into the data area |
| 5754 | ** of pToRelease. Make sure pToRelease is still writeable. */ |
| 5755 | assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) ); |
| 5756 | |
| 5757 | /* If pPrior is part of the data area of pPage, then make sure pPage |
| 5758 | ** is still writeable */ |
| 5759 | assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize] |
| 5760 | || sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 5761 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5762 | put4byte(pPrior, pgnoOvfl); |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 5763 | releasePage(pToRelease); |
| 5764 | pToRelease = pOvfl; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5765 | pPrior = pOvfl->aData; |
| 5766 | put4byte(pPrior, 0); |
| 5767 | pPayload = &pOvfl->aData[4]; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5768 | spaceLeft = pBt->usableSize - 4; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5769 | } |
| 5770 | n = nPayload; |
| 5771 | if( n>spaceLeft ) n = spaceLeft; |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 5772 | |
| 5773 | /* If pToRelease is not zero than pPayload points into the data area |
| 5774 | ** of pToRelease. Make sure pToRelease is still writeable. */ |
| 5775 | assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) ); |
| 5776 | |
| 5777 | /* If pPayload is part of the data area of pPage, then make sure pPage |
| 5778 | ** is still writeable */ |
| 5779 | assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize] |
| 5780 | || sqlite3PagerIswriteable(pPage->pDbPage) ); |
| 5781 | |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5782 | if( nSrc>0 ){ |
| 5783 | if( n>nSrc ) n = nSrc; |
| 5784 | assert( pSrc ); |
| 5785 | memcpy(pPayload, pSrc, n); |
| 5786 | }else{ |
| 5787 | memset(pPayload, 0, n); |
| 5788 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5789 | nPayload -= n; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 5790 | pPayload += n; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 5791 | pSrc += n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5792 | nSrc -= n; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5793 | spaceLeft -= n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5794 | if( nSrc==0 ){ |
| 5795 | nSrc = nData; |
| 5796 | pSrc = pData; |
| 5797 | } |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 5798 | } |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 5799 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5800 | return SQLITE_OK; |
| 5801 | } |
| 5802 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5803 | /* |
| 5804 | ** Remove the i-th cell from pPage. This routine effects pPage only. |
| 5805 | ** The cell content is not freed or deallocated. It is assumed that |
| 5806 | ** the cell content has been copied someplace else. This routine just |
| 5807 | ** removes the reference to the cell from pPage. |
| 5808 | ** |
| 5809 | ** "sz" must be the number of bytes in the cell. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5810 | */ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 5811 | static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){ |
drh | 43b18e1 | 2010-08-17 19:40:08 +0000 | [diff] [blame] | 5812 | u32 pc; /* Offset to cell content of cell being deleted */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5813 | u8 *data; /* pPage->aData */ |
| 5814 | u8 *ptr; /* Used to move bytes around within data[] */ |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 5815 | int rc; /* The return code */ |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 5816 | int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5817 | |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 5818 | if( *pRC ) return; |
| 5819 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5820 | assert( idx>=0 && idx<pPage->nCell ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5821 | assert( sz==cellSize(pPage, idx) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5822 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5823 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5824 | data = pPage->aData; |
drh | 3def235 | 2011-11-11 00:27:15 +0000 | [diff] [blame] | 5825 | ptr = &pPage->aCellIdx[2*idx]; |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 5826 | pc = get2byte(ptr); |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 5827 | hdr = pPage->hdrOffset; |
| 5828 | testcase( pc==get2byte(&data[hdr+5]) ); |
| 5829 | testcase( pc+sz==pPage->pBt->usableSize ); |
drh | 43b18e1 | 2010-08-17 19:40:08 +0000 | [diff] [blame] | 5830 | if( pc < (u32)get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 5831 | *pRC = SQLITE_CORRUPT_BKPT; |
| 5832 | return; |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 5833 | } |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 5834 | rc = freeSpace(pPage, pc, sz); |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 5835 | if( rc ){ |
| 5836 | *pRC = rc; |
| 5837 | return; |
shane | dcc50b7 | 2008-11-13 18:29:50 +0000 | [diff] [blame] | 5838 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5839 | pPage->nCell--; |
drh | 9bb7c4f | 2013-12-09 01:58:11 +0000 | [diff] [blame] | 5840 | memmove(ptr, ptr+2, 2*(pPage->nCell - idx)); |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 5841 | put2byte(&data[hdr+3], pPage->nCell); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5842 | pPage->nFree += 2; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5843 | } |
| 5844 | |
| 5845 | /* |
| 5846 | ** Insert a new cell on pPage at cell index "i". pCell points to the |
| 5847 | ** content of the cell. |
| 5848 | ** |
| 5849 | ** 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] | 5850 | ** will not fit, then make a copy of the cell content into pTemp if |
| 5851 | ** pTemp is not null. Regardless of pTemp, allocate a new entry |
drh | 2cbd78b | 2012-02-02 19:37:18 +0000 | [diff] [blame] | 5852 | ** in pPage->apOvfl[] and make it point to the cell content (either |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5853 | ** in pTemp or the original pCell) and also record its index. |
| 5854 | ** Allocating a new entry in pPage->aCell[] implies that |
| 5855 | ** pPage->nOverflow is incremented. |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 5856 | ** |
| 5857 | ** If nSkip is non-zero, then do not copy the first nSkip bytes of the |
| 5858 | ** cell. The caller will overwrite them after this function returns. If |
drh | 4b238df | 2005-01-08 15:43:18 +0000 | [diff] [blame] | 5859 | ** nSkip is non-zero, then pCell may not point to an invalid memory location |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 5860 | ** (but pCell+nSkip is always valid). |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5861 | */ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 5862 | static void insertCell( |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5863 | MemPage *pPage, /* Page into which we are copying */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5864 | int i, /* New cell becomes the i-th cell of the page */ |
| 5865 | u8 *pCell, /* Content of the new cell */ |
| 5866 | int sz, /* Bytes of content in pCell */ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 5867 | u8 *pTemp, /* Temp storage space for pCell, if needed */ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 5868 | Pgno iChild, /* If non-zero, replace first 4 bytes with this value */ |
| 5869 | int *pRC /* Read and write return code from here */ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5870 | ){ |
drh | 383d30f | 2010-02-26 13:07:37 +0000 | [diff] [blame] | 5871 | int idx = 0; /* Where to write new cell content in data[] */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5872 | int j; /* Loop counter */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5873 | int end; /* First byte past the last cell pointer in data[] */ |
| 5874 | int ins; /* Index in data[] where new cell pointer is inserted */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5875 | int cellOffset; /* Address of first cell pointer in data[] */ |
| 5876 | u8 *data; /* The content of the whole page */ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5877 | int nSkip = (iChild ? 4 : 0); |
| 5878 | |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 5879 | if( *pRC ) return; |
| 5880 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5881 | assert( i>=0 && i<=pPage->nCell+pPage->nOverflow ); |
dan | f216e32 | 2014-08-14 19:53:37 +0000 | [diff] [blame] | 5882 | assert( MX_CELL(pPage->pBt)<=10921 ); |
| 5883 | assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB ); |
drh | 2cbd78b | 2012-02-02 19:37:18 +0000 | [diff] [blame] | 5884 | assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) ); |
| 5885 | assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5886 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | c9b9b8a | 2009-12-03 21:26:52 +0000 | [diff] [blame] | 5887 | /* The cell should normally be sized correctly. However, when moving a |
| 5888 | ** malformed cell from a leaf page to an interior page, if the cell size |
| 5889 | ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size |
| 5890 | ** might be less than 8 (leaf-size + pointer) on the interior node. Hence |
| 5891 | ** the term after the || in the following assert(). */ |
| 5892 | assert( sz==cellSizePtr(pPage, pCell) || (sz==8 && iChild>0) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5893 | if( pPage->nOverflow || sz+2>pPage->nFree ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5894 | if( pTemp ){ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 5895 | memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5896 | pCell = pTemp; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5897 | } |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5898 | if( iChild ){ |
| 5899 | put4byte(pCell, iChild); |
| 5900 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5901 | j = pPage->nOverflow++; |
drh | 2cbd78b | 2012-02-02 19:37:18 +0000 | [diff] [blame] | 5902 | assert( j<(int)(sizeof(pPage->apOvfl)/sizeof(pPage->apOvfl[0])) ); |
| 5903 | pPage->apOvfl[j] = pCell; |
| 5904 | pPage->aiOvfl[j] = (u16)i; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5905 | }else{ |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 5906 | int rc = sqlite3PagerWrite(pPage->pDbPage); |
| 5907 | if( rc!=SQLITE_OK ){ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 5908 | *pRC = rc; |
| 5909 | return; |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 5910 | } |
| 5911 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5912 | data = pPage->aData; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5913 | cellOffset = pPage->cellOffset; |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 5914 | end = cellOffset + 2*pPage->nCell; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5915 | ins = cellOffset + 2*i; |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 5916 | rc = allocateSpace(pPage, sz, &idx); |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 5917 | if( rc ){ *pRC = rc; return; } |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 5918 | /* The allocateSpace() routine guarantees the following two properties |
| 5919 | ** if it returns success */ |
| 5920 | assert( idx >= end+2 ); |
drh | fcd71b6 | 2011-04-05 22:08:24 +0000 | [diff] [blame] | 5921 | assert( idx+sz <= (int)pPage->pBt->usableSize ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5922 | pPage->nCell++; |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 5923 | pPage->nFree -= (u16)(2 + sz); |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 5924 | memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 5925 | if( iChild ){ |
| 5926 | put4byte(&data[idx], iChild); |
| 5927 | } |
drh | 8f51883 | 2013-12-09 02:32:19 +0000 | [diff] [blame] | 5928 | memmove(&data[ins+2], &data[ins], end-ins); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5929 | put2byte(&data[ins], idx); |
drh | 0a45c27 | 2009-07-08 01:49:11 +0000 | [diff] [blame] | 5930 | put2byte(&data[pPage->hdrOffset+3], pPage->nCell); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 5931 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5932 | if( pPage->pBt->autoVacuum ){ |
| 5933 | /* The cell may contain a pointer to an overflow page. If so, write |
| 5934 | ** the entry for the overflow page into the pointer map. |
| 5935 | */ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 5936 | ptrmapPutOvflPtr(pPage, pCell, pRC); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 5937 | } |
| 5938 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5939 | } |
| 5940 | } |
| 5941 | |
| 5942 | /* |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 5943 | ** Add a list of cells to a page. The page should be initially empty. |
| 5944 | ** The cells are guaranteed to fit on the page. |
| 5945 | */ |
| 5946 | static void assemblePage( |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 5947 | MemPage *pPage, /* The page to be assembled */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 5948 | int nCell, /* The number of cells to add to this page */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5949 | u8 **apCell, /* Pointers to cell bodies */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5950 | u16 *aSize /* Sizes of the cells */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 5951 | ){ |
| 5952 | int i; /* Loop counter */ |
danielk1977 | fad9194 | 2009-04-29 17:49:59 +0000 | [diff] [blame] | 5953 | u8 *pCellptr; /* Address of next cell pointer */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5954 | int cellbody; /* Address of next cell body */ |
danielk1977 | fad9194 | 2009-04-29 17:49:59 +0000 | [diff] [blame] | 5955 | u8 * const data = pPage->aData; /* Pointer to data for pPage */ |
| 5956 | const int hdr = pPage->hdrOffset; /* Offset of header on pPage */ |
| 5957 | const int nUsable = pPage->pBt->usableSize; /* Usable size of page */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 5958 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5959 | assert( pPage->nOverflow==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5960 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | fcd71b6 | 2011-04-05 22:08:24 +0000 | [diff] [blame] | 5961 | assert( nCell>=0 && nCell<=(int)MX_CELL(pPage->pBt) |
| 5962 | && (int)MX_CELL(pPage->pBt)<=10921); |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 5963 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
danielk1977 | fad9194 | 2009-04-29 17:49:59 +0000 | [diff] [blame] | 5964 | |
| 5965 | /* Check that the page has just been zeroed by zeroPage() */ |
| 5966 | assert( pPage->nCell==0 ); |
drh | 5d433ce | 2010-08-14 16:02:52 +0000 | [diff] [blame] | 5967 | assert( get2byteNotZero(&data[hdr+5])==nUsable ); |
danielk1977 | fad9194 | 2009-04-29 17:49:59 +0000 | [diff] [blame] | 5968 | |
drh | 3def235 | 2011-11-11 00:27:15 +0000 | [diff] [blame] | 5969 | pCellptr = &pPage->aCellIdx[nCell*2]; |
danielk1977 | fad9194 | 2009-04-29 17:49:59 +0000 | [diff] [blame] | 5970 | cellbody = nUsable; |
| 5971 | for(i=nCell-1; i>=0; i--){ |
drh | 61d2fe9 | 2011-06-03 23:28:33 +0000 | [diff] [blame] | 5972 | u16 sz = aSize[i]; |
danielk1977 | fad9194 | 2009-04-29 17:49:59 +0000 | [diff] [blame] | 5973 | pCellptr -= 2; |
drh | 61d2fe9 | 2011-06-03 23:28:33 +0000 | [diff] [blame] | 5974 | cellbody -= sz; |
danielk1977 | fad9194 | 2009-04-29 17:49:59 +0000 | [diff] [blame] | 5975 | put2byte(pCellptr, cellbody); |
drh | 61d2fe9 | 2011-06-03 23:28:33 +0000 | [diff] [blame] | 5976 | memcpy(&data[cellbody], apCell[i], sz); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 5977 | } |
danielk1977 | fad9194 | 2009-04-29 17:49:59 +0000 | [diff] [blame] | 5978 | put2byte(&data[hdr+3], nCell); |
| 5979 | put2byte(&data[hdr+5], cellbody); |
| 5980 | pPage->nFree -= (nCell*2 + nUsable - cellbody); |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 5981 | pPage->nCell = (u16)nCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 5982 | } |
| 5983 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5984 | /* |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5985 | ** The following parameters determine how many adjacent pages get involved |
| 5986 | ** in a balancing operation. NN is the number of neighbors on either side |
| 5987 | ** of the page that participate in the balancing operation. NB is the |
| 5988 | ** total number of pages that participate, including the target page and |
| 5989 | ** NN neighbors on either side. |
| 5990 | ** |
| 5991 | ** The minimum value of NN is 1 (of course). Increasing NN above 1 |
| 5992 | ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance |
| 5993 | ** in exchange for a larger degradation in INSERT and UPDATE performance. |
| 5994 | ** The value of NN appears to give the best results overall. |
| 5995 | */ |
| 5996 | #define NN 1 /* Number of neighbors on either side of pPage */ |
| 5997 | #define NB (NN*2+1) /* Total pages involved in the balance */ |
| 5998 | |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5999 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 6000 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 6001 | /* |
| 6002 | ** This version of balance() handles the common special case where |
| 6003 | ** a new entry is being inserted on the extreme right-end of the |
| 6004 | ** tree, in other words, when the new entry will become the largest |
| 6005 | ** entry in the tree. |
| 6006 | ** |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 6007 | ** Instead of trying to balance the 3 right-most leaf pages, just add |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 6008 | ** a new page to the right-hand side and put the one new entry in |
| 6009 | ** that page. This leaves the right side of the tree somewhat |
| 6010 | ** unbalanced. But odds are that we will be inserting new entries |
| 6011 | ** at the end soon afterwards so the nearly empty page will quickly |
| 6012 | ** fill up. On average. |
| 6013 | ** |
| 6014 | ** pPage is the leaf page which is the right-most page in the tree. |
| 6015 | ** pParent is its parent. pPage must have a single overflow entry |
| 6016 | ** which is also the right-most entry on the page. |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6017 | ** |
| 6018 | ** The pSpace buffer is used to store a temporary copy of the divider |
| 6019 | ** cell that will be inserted into pParent. Such a cell consists of a 4 |
| 6020 | ** byte page number followed by a variable length integer. In other |
| 6021 | ** words, at most 13 bytes. Hence the pSpace buffer must be at |
| 6022 | ** least 13 bytes in size. |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 6023 | */ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6024 | static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){ |
| 6025 | BtShared *const pBt = pPage->pBt; /* B-Tree Database */ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6026 | MemPage *pNew; /* Newly allocated page */ |
danielk1977 | 6f235cc | 2009-06-04 14:46:08 +0000 | [diff] [blame] | 6027 | int rc; /* Return Code */ |
| 6028 | Pgno pgnoNew; /* Page number of pNew */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 6029 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6030 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6031 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
danielk1977 | e56b60e | 2009-06-10 09:11:06 +0000 | [diff] [blame] | 6032 | assert( pPage->nOverflow==1 ); |
| 6033 | |
drh | 5d433ce | 2010-08-14 16:02:52 +0000 | [diff] [blame] | 6034 | /* This error condition is now caught prior to reaching this function */ |
mistachkin | 5f070c7 | 2012-10-18 10:35:19 +0000 | [diff] [blame] | 6035 | if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6036 | |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6037 | /* Allocate a new page. This page will become the right-sibling of |
| 6038 | ** pPage. Make the parent page writable, so that the new divider cell |
| 6039 | ** may be inserted. If both these operations are successful, proceed. |
| 6040 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 6041 | rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6042 | |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 6043 | if( rc==SQLITE_OK ){ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6044 | |
| 6045 | u8 *pOut = &pSpace[4]; |
drh | 2cbd78b | 2012-02-02 19:37:18 +0000 | [diff] [blame] | 6046 | u8 *pCell = pPage->apOvfl[0]; |
danielk1977 | 6f235cc | 2009-06-04 14:46:08 +0000 | [diff] [blame] | 6047 | u16 szCell = cellSizePtr(pPage, pCell); |
| 6048 | u8 *pStop; |
| 6049 | |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 6050 | assert( sqlite3PagerIswriteable(pNew->pDbPage) ); |
danielk1977 | e56b60e | 2009-06-10 09:11:06 +0000 | [diff] [blame] | 6051 | assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) ); |
| 6052 | zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF); |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 6053 | assemblePage(pNew, 1, &pCell, &szCell); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6054 | |
| 6055 | /* If this is an auto-vacuum database, update the pointer map |
| 6056 | ** with entries for the new page, and any pointer from the |
| 6057 | ** cell on the page to an overflow page. If either of these |
| 6058 | ** operations fails, the return code is set, but the contents |
| 6059 | ** of the parent page are still manipulated by thh code below. |
| 6060 | ** That is Ok, at this point the parent page is guaranteed to |
| 6061 | ** be marked as dirty. Returning an error code will cause a |
| 6062 | ** rollback, undoing any changes made to the parent page. |
| 6063 | */ |
| 6064 | if( ISAUTOVACUUM ){ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6065 | ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc); |
| 6066 | if( szCell>pNew->minLocal ){ |
| 6067 | ptrmapPutOvflPtr(pNew, pCell, &rc); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6068 | } |
| 6069 | } |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 6070 | |
danielk1977 | 6f235cc | 2009-06-04 14:46:08 +0000 | [diff] [blame] | 6071 | /* Create a divider cell to insert into pParent. The divider cell |
| 6072 | ** consists of a 4-byte page number (the page number of pPage) and |
| 6073 | ** a variable length key value (which must be the same value as the |
| 6074 | ** largest key on pPage). |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 6075 | ** |
danielk1977 | 6f235cc | 2009-06-04 14:46:08 +0000 | [diff] [blame] | 6076 | ** To find the largest key value on pPage, first find the right-most |
| 6077 | ** cell on pPage. The first two fields of this cell are the |
| 6078 | ** record-length (a variable length integer at most 32-bits in size) |
| 6079 | ** and the key value (a variable length integer, may have any value). |
| 6080 | ** The first of the while(...) loops below skips over the record-length |
| 6081 | ** field. The second while(...) loop copies the key value from the |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6082 | ** cell on pPage into the pSpace buffer. |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 6083 | */ |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 6084 | pCell = findCell(pPage, pPage->nCell-1); |
danielk1977 | 6f235cc | 2009-06-04 14:46:08 +0000 | [diff] [blame] | 6085 | pStop = &pCell[9]; |
| 6086 | while( (*(pCell++)&0x80) && pCell<pStop ); |
| 6087 | pStop = &pCell[9]; |
| 6088 | while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop ); |
| 6089 | |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6090 | /* Insert the new divider cell into pParent. */ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6091 | insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace), |
| 6092 | 0, pPage->pgno, &rc); |
danielk1977 | 6f235cc | 2009-06-04 14:46:08 +0000 | [diff] [blame] | 6093 | |
| 6094 | /* Set the right-child pointer of pParent to point to the new page. */ |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 6095 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew); |
| 6096 | |
danielk1977 | e08a3c4 | 2008-09-18 18:17:03 +0000 | [diff] [blame] | 6097 | /* Release the reference to the new page. */ |
| 6098 | releasePage(pNew); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 6099 | } |
| 6100 | |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 6101 | return rc; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 6102 | } |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 6103 | #endif /* SQLITE_OMIT_QUICKBALANCE */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6104 | |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6105 | #if 0 |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 6106 | /* |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6107 | ** This function does not contribute anything to the operation of SQLite. |
| 6108 | ** it is sometimes activated temporarily while debugging code responsible |
| 6109 | ** for setting pointer-map entries. |
| 6110 | */ |
| 6111 | static int ptrmapCheckPages(MemPage **apPage, int nPage){ |
| 6112 | int i, j; |
| 6113 | for(i=0; i<nPage; i++){ |
| 6114 | Pgno n; |
| 6115 | u8 e; |
| 6116 | MemPage *pPage = apPage[i]; |
| 6117 | BtShared *pBt = pPage->pBt; |
| 6118 | assert( pPage->isInit ); |
| 6119 | |
| 6120 | for(j=0; j<pPage->nCell; j++){ |
| 6121 | CellInfo info; |
| 6122 | u8 *z; |
| 6123 | |
| 6124 | z = findCell(pPage, j); |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 6125 | btreeParseCellPtr(pPage, z, &info); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6126 | if( info.iOverflow ){ |
| 6127 | Pgno ovfl = get4byte(&z[info.iOverflow]); |
| 6128 | ptrmapGet(pBt, ovfl, &e, &n); |
| 6129 | assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 ); |
| 6130 | } |
| 6131 | if( !pPage->leaf ){ |
| 6132 | Pgno child = get4byte(z); |
| 6133 | ptrmapGet(pBt, child, &e, &n); |
| 6134 | assert( n==pPage->pgno && e==PTRMAP_BTREE ); |
| 6135 | } |
| 6136 | } |
| 6137 | if( !pPage->leaf ){ |
| 6138 | Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 6139 | ptrmapGet(pBt, child, &e, &n); |
| 6140 | assert( n==pPage->pgno && e==PTRMAP_BTREE ); |
| 6141 | } |
| 6142 | } |
| 6143 | return 1; |
| 6144 | } |
| 6145 | #endif |
| 6146 | |
danielk1977 | cd581a7 | 2009-06-23 15:43:39 +0000 | [diff] [blame] | 6147 | /* |
| 6148 | ** This function is used to copy the contents of the b-tree node stored |
| 6149 | ** on page pFrom to page pTo. If page pFrom was not a leaf page, then |
| 6150 | ** the pointer-map entries for each child page are updated so that the |
| 6151 | ** parent page stored in the pointer map is page pTo. If pFrom contained |
| 6152 | ** any cells with overflow page pointers, then the corresponding pointer |
| 6153 | ** map entries are also updated so that the parent page is page pTo. |
| 6154 | ** |
| 6155 | ** If pFrom is currently carrying any overflow cells (entries in the |
drh | 2cbd78b | 2012-02-02 19:37:18 +0000 | [diff] [blame] | 6156 | ** MemPage.apOvfl[] array), they are not copied to pTo. |
danielk1977 | cd581a7 | 2009-06-23 15:43:39 +0000 | [diff] [blame] | 6157 | ** |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 6158 | ** Before returning, page pTo is reinitialized using btreeInitPage(). |
danielk1977 | cd581a7 | 2009-06-23 15:43:39 +0000 | [diff] [blame] | 6159 | ** |
| 6160 | ** The performance of this function is not critical. It is only used by |
| 6161 | ** the balance_shallower() and balance_deeper() procedures, neither of |
| 6162 | ** which are called often under normal circumstances. |
| 6163 | */ |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 6164 | static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){ |
| 6165 | if( (*pRC)==SQLITE_OK ){ |
| 6166 | BtShared * const pBt = pFrom->pBt; |
| 6167 | u8 * const aFrom = pFrom->aData; |
| 6168 | u8 * const aTo = pTo->aData; |
| 6169 | int const iFromHdr = pFrom->hdrOffset; |
| 6170 | int const iToHdr = ((pTo->pgno==1) ? 100 : 0); |
drh | dc9b5f8 | 2009-12-05 18:34:08 +0000 | [diff] [blame] | 6171 | int rc; |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 6172 | int iData; |
| 6173 | |
| 6174 | |
| 6175 | assert( pFrom->isInit ); |
| 6176 | assert( pFrom->nFree>=iToHdr ); |
drh | fcd71b6 | 2011-04-05 22:08:24 +0000 | [diff] [blame] | 6177 | assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize ); |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 6178 | |
| 6179 | /* Copy the b-tree node content from page pFrom to page pTo. */ |
| 6180 | iData = get2byte(&aFrom[iFromHdr+5]); |
| 6181 | memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData); |
| 6182 | memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell); |
| 6183 | |
| 6184 | /* Reinitialize page pTo so that the contents of the MemPage structure |
dan | 89e060e | 2009-12-05 18:03:50 +0000 | [diff] [blame] | 6185 | ** match the new data. The initialization of pTo can actually fail under |
| 6186 | ** fairly obscure circumstances, even though it is a copy of initialized |
| 6187 | ** page pFrom. |
| 6188 | */ |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 6189 | pTo->isInit = 0; |
dan | 89e060e | 2009-12-05 18:03:50 +0000 | [diff] [blame] | 6190 | rc = btreeInitPage(pTo); |
| 6191 | if( rc!=SQLITE_OK ){ |
| 6192 | *pRC = rc; |
| 6193 | return; |
| 6194 | } |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 6195 | |
| 6196 | /* If this is an auto-vacuum database, update the pointer-map entries |
| 6197 | ** for any b-tree or overflow pages that pTo now contains the pointers to. |
| 6198 | */ |
| 6199 | if( ISAUTOVACUUM ){ |
| 6200 | *pRC = setChildPtrmaps(pTo); |
| 6201 | } |
danielk1977 | cd581a7 | 2009-06-23 15:43:39 +0000 | [diff] [blame] | 6202 | } |
danielk1977 | cd581a7 | 2009-06-23 15:43:39 +0000 | [diff] [blame] | 6203 | } |
| 6204 | |
| 6205 | /* |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6206 | ** This routine redistributes cells on the iParentIdx'th child of pParent |
| 6207 | ** (hereafter "the page") and up to 2 siblings so that all pages have about the |
| 6208 | ** same amount of free space. Usually a single sibling on either side of the |
| 6209 | ** page are used in the balancing, though both siblings might come from one |
| 6210 | ** side if the page is the first or last child of its parent. If the page |
| 6211 | ** has fewer than 2 siblings (something which can only happen if the page |
| 6212 | ** is a root page or a child of a root page) then all available siblings |
| 6213 | ** participate in the balancing. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6214 | ** |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6215 | ** The number of siblings of the page might be increased or decreased by |
| 6216 | ** 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] | 6217 | ** |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6218 | ** Note that when this routine is called, some of the cells on the page |
| 6219 | ** might not actually be stored in MemPage.aData[]. This can happen |
| 6220 | ** if the page is overfull. This routine ensures that all cells allocated |
| 6221 | ** to the page and its siblings fit into MemPage.aData[] before returning. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6222 | ** |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6223 | ** In the course of balancing the page and its siblings, cells may be |
| 6224 | ** inserted into or removed from the parent page (pParent). Doing so |
| 6225 | ** may cause the parent page to become overfull or underfull. If this |
| 6226 | ** happens, it is the responsibility of the caller to invoke the correct |
| 6227 | ** balancing routine to fix this problem (see the balance() routine). |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 6228 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6229 | ** If this routine fails for any reason, it might leave the database |
danielk1977 | 6067a9b | 2009-06-09 09:41:00 +0000 | [diff] [blame] | 6230 | ** in a corrupted state. So if this routine fails, the database should |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6231 | ** be rolled back. |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6232 | ** |
| 6233 | ** The third argument to this function, aOvflSpace, is a pointer to a |
drh | cd09c53 | 2009-07-20 19:30:00 +0000 | [diff] [blame] | 6234 | ** buffer big enough to hold one page. If while inserting cells into the parent |
| 6235 | ** page (pParent) the parent page becomes overfull, this buffer is |
| 6236 | ** used to store the parent's overflow cells. Because this function inserts |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6237 | ** a maximum of four divider cells into the parent page, and the maximum |
| 6238 | ** size of a cell stored within an internal node is always less than 1/4 |
| 6239 | ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large |
| 6240 | ** enough for all overflow cells. |
| 6241 | ** |
| 6242 | ** If aOvflSpace is set to a null pointer, this function returns |
| 6243 | ** SQLITE_NOMEM. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6244 | */ |
mistachkin | e7c5416 | 2012-10-02 22:54:27 +0000 | [diff] [blame] | 6245 | #if defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_M_ARM) |
| 6246 | #pragma optimize("", off) |
| 6247 | #endif |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6248 | static int balance_nonroot( |
| 6249 | MemPage *pParent, /* Parent page of siblings being balanced */ |
| 6250 | int iParentIdx, /* Index of "the page" in pParent */ |
danielk1977 | cd581a7 | 2009-06-23 15:43:39 +0000 | [diff] [blame] | 6251 | u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */ |
dan | 428c218 | 2012-08-06 18:50:11 +0000 | [diff] [blame] | 6252 | int isRoot, /* True if pParent is a root-page */ |
| 6253 | int bBulk /* True if this call is part of a bulk load */ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6254 | ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6255 | BtShared *pBt; /* The whole database */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 6256 | int nCell = 0; /* Number of cells in apCell[] */ |
| 6257 | int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */ |
danielk1977 | a4124bd | 2008-12-23 10:37:47 +0000 | [diff] [blame] | 6258 | int nNew = 0; /* Number of pages in apNew[] */ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6259 | int nOld; /* Number of pages in apOld[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6260 | int i, j, k; /* Loop counters */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6261 | int nxDiv; /* Next divider slot in pParent->aCell[] */ |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 6262 | int rc = SQLITE_OK; /* The return code */ |
shane | 36840fd | 2009-06-26 16:32:13 +0000 | [diff] [blame] | 6263 | u16 leafCorrection; /* 4 if pPage is a leaf. 0 if not */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 6264 | int leafData; /* True if pPage is a leaf of a LEAFDATA tree */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 6265 | int usableSpace; /* Bytes in pPage beyond the header */ |
| 6266 | int pageFlags; /* Value of pPage->aData[0] */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6267 | int subtotal; /* Subtotal of bytes in cells on one page */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 6268 | int iSpace1 = 0; /* First unused byte of aSpace1[] */ |
danielk1977 | 6067a9b | 2009-06-09 09:41:00 +0000 | [diff] [blame] | 6269 | int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 6270 | int szScratch; /* Size of scratch memory requested */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 6271 | MemPage *apOld[NB]; /* pPage and up to two siblings */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6272 | MemPage *apCopy[NB]; /* Private copies of apOld[] pages */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 6273 | MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6274 | u8 *pRight; /* Location in parent of right-sibling pointer */ |
| 6275 | u8 *apDiv[NB-1]; /* Divider cells in pParent */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 6276 | int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */ |
| 6277 | int szNew[NB+2]; /* Combined size of cells place on i-th page */ |
danielk1977 | 50f059b | 2005-03-29 02:54:03 +0000 | [diff] [blame] | 6278 | u8 **apCell = 0; /* All cells begin balanced */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 6279 | u16 *szCell; /* Local size of all cells in apCell[] */ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6280 | u8 *aSpace1; /* Space for copies of dividers cells */ |
| 6281 | Pgno pgno; /* Temp var to store a page number in */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6282 | |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6283 | pBt = pParent->pBt; |
| 6284 | assert( sqlite3_mutex_held(pBt->mutex) ); |
| 6285 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
danielk1977 | 474b7cc | 2008-07-09 11:49:46 +0000 | [diff] [blame] | 6286 | |
danielk1977 | e576521 | 2009-06-17 11:13:28 +0000 | [diff] [blame] | 6287 | #if 0 |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6288 | TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno)); |
danielk1977 | e576521 | 2009-06-17 11:13:28 +0000 | [diff] [blame] | 6289 | #endif |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6290 | |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6291 | /* At this point pParent may have at most one overflow cell. And if |
| 6292 | ** this overflow cell is present, it must be the cell with |
| 6293 | ** index iParentIdx. This scenario comes about when this function |
drh | cd09c53 | 2009-07-20 19:30:00 +0000 | [diff] [blame] | 6294 | ** is called (indirectly) from sqlite3BtreeDelete(). |
| 6295 | */ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6296 | assert( pParent->nOverflow==0 || pParent->nOverflow==1 ); |
drh | 2cbd78b | 2012-02-02 19:37:18 +0000 | [diff] [blame] | 6297 | assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx ); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6298 | |
danielk1977 | 11a8a86 | 2009-06-17 11:49:52 +0000 | [diff] [blame] | 6299 | if( !aOvflSpace ){ |
| 6300 | return SQLITE_NOMEM; |
| 6301 | } |
| 6302 | |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6303 | /* Find the sibling pages to balance. Also locate the cells in pParent |
| 6304 | ** that divide the siblings. An attempt is made to find NN siblings on |
| 6305 | ** either side of pPage. More siblings are taken from one side, however, |
| 6306 | ** if there are fewer than NN siblings on the other side. If pParent |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6307 | ** has NB or fewer children then all children of pParent are taken. |
| 6308 | ** |
| 6309 | ** This loop also drops the divider cells from the parent page. This |
| 6310 | ** way, the remainder of the function does not have to deal with any |
drh | cd09c53 | 2009-07-20 19:30:00 +0000 | [diff] [blame] | 6311 | ** overflow cells in the parent page, since if any existed they will |
| 6312 | ** have already been removed. |
| 6313 | */ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6314 | i = pParent->nOverflow + pParent->nCell; |
| 6315 | if( i<2 ){ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 6316 | nxDiv = 0; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6317 | }else{ |
dan | 7d6885a | 2012-08-08 14:04:56 +0000 | [diff] [blame] | 6318 | assert( bBulk==0 || bBulk==1 ); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6319 | if( iParentIdx==0 ){ |
| 6320 | nxDiv = 0; |
| 6321 | }else if( iParentIdx==i ){ |
dan | 7d6885a | 2012-08-08 14:04:56 +0000 | [diff] [blame] | 6322 | nxDiv = i-2+bBulk; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6323 | }else{ |
dan | 7d6885a | 2012-08-08 14:04:56 +0000 | [diff] [blame] | 6324 | assert( bBulk==0 ); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6325 | nxDiv = iParentIdx-1; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6326 | } |
dan | 7d6885a | 2012-08-08 14:04:56 +0000 | [diff] [blame] | 6327 | i = 2-bBulk; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6328 | } |
dan | 7d6885a | 2012-08-08 14:04:56 +0000 | [diff] [blame] | 6329 | nOld = i+1; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6330 | if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){ |
| 6331 | pRight = &pParent->aData[pParent->hdrOffset+8]; |
| 6332 | }else{ |
| 6333 | pRight = findCell(pParent, i+nxDiv-pParent->nOverflow); |
| 6334 | } |
| 6335 | pgno = get4byte(pRight); |
| 6336 | while( 1 ){ |
dan | 11dcd11 | 2013-03-15 18:29:18 +0000 | [diff] [blame] | 6337 | rc = getAndInitPage(pBt, pgno, &apOld[i], 0); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6338 | if( rc ){ |
danielk1977 | 89bc4bc | 2009-07-21 19:25:24 +0000 | [diff] [blame] | 6339 | memset(apOld, 0, (i+1)*sizeof(MemPage*)); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6340 | goto balance_cleanup; |
| 6341 | } |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 6342 | nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6343 | if( (i--)==0 ) break; |
| 6344 | |
drh | 2cbd78b | 2012-02-02 19:37:18 +0000 | [diff] [blame] | 6345 | if( i+nxDiv==pParent->aiOvfl[0] && pParent->nOverflow ){ |
| 6346 | apDiv[i] = pParent->apOvfl[0]; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6347 | pgno = get4byte(apDiv[i]); |
| 6348 | szNew[i] = cellSizePtr(pParent, apDiv[i]); |
| 6349 | pParent->nOverflow = 0; |
| 6350 | }else{ |
| 6351 | apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow); |
| 6352 | pgno = get4byte(apDiv[i]); |
| 6353 | szNew[i] = cellSizePtr(pParent, apDiv[i]); |
| 6354 | |
| 6355 | /* Drop the cell from the parent page. apDiv[i] still points to |
| 6356 | ** the cell within the parent, even though it has been dropped. |
| 6357 | ** This is safe because dropping a cell only overwrites the first |
| 6358 | ** four bytes of it, and this function does not need the first |
| 6359 | ** four bytes of the divider cell. So the pointer is safe to use |
danielk1977 | 11a8a86 | 2009-06-17 11:49:52 +0000 | [diff] [blame] | 6360 | ** later on. |
| 6361 | ** |
drh | 8a575d9 | 2011-10-12 17:00:28 +0000 | [diff] [blame] | 6362 | ** But not if we are in secure-delete mode. In secure-delete mode, |
danielk1977 | 11a8a86 | 2009-06-17 11:49:52 +0000 | [diff] [blame] | 6363 | ** the dropCell() routine will overwrite the entire cell with zeroes. |
| 6364 | ** In this case, temporarily copy the cell into the aOvflSpace[] |
| 6365 | ** buffer. It will be copied out again as soon as the aSpace[] buffer |
| 6366 | ** is allocated. */ |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 6367 | if( pBt->btsFlags & BTS_SECURE_DELETE ){ |
drh | 8a575d9 | 2011-10-12 17:00:28 +0000 | [diff] [blame] | 6368 | int iOff; |
| 6369 | |
| 6370 | iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData); |
drh | 43b18e1 | 2010-08-17 19:40:08 +0000 | [diff] [blame] | 6371 | if( (iOff+szNew[i])>(int)pBt->usableSize ){ |
dan | 2ed11e7 | 2010-02-26 15:09:19 +0000 | [diff] [blame] | 6372 | rc = SQLITE_CORRUPT_BKPT; |
| 6373 | memset(apOld, 0, (i+1)*sizeof(MemPage*)); |
| 6374 | goto balance_cleanup; |
| 6375 | }else{ |
| 6376 | memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]); |
| 6377 | apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData]; |
| 6378 | } |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 6379 | } |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6380 | dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6381 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6382 | } |
| 6383 | |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 6384 | /* Make nMaxCells a multiple of 4 in order to preserve 8-byte |
drh | 8d97f1f | 2005-05-05 18:14:13 +0000 | [diff] [blame] | 6385 | ** alignment */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 6386 | nMaxCells = (nMaxCells + 3)&~3; |
drh | 8d97f1f | 2005-05-05 18:14:13 +0000 | [diff] [blame] | 6387 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6388 | /* |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 6389 | ** Allocate space for memory structures |
| 6390 | */ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6391 | k = pBt->pageSize + ROUND8(sizeof(MemPage)); |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 6392 | szScratch = |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 6393 | nMaxCells*sizeof(u8*) /* apCell */ |
| 6394 | + nMaxCells*sizeof(u16) /* szCell */ |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 6395 | + pBt->pageSize /* aSpace1 */ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6396 | + k*nOld; /* Page copies (apCopy) */ |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 6397 | apCell = sqlite3ScratchMalloc( szScratch ); |
danielk1977 | 11a8a86 | 2009-06-17 11:49:52 +0000 | [diff] [blame] | 6398 | if( apCell==0 ){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 6399 | rc = SQLITE_NOMEM; |
| 6400 | goto balance_cleanup; |
| 6401 | } |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 6402 | szCell = (u16*)&apCell[nMaxCells]; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6403 | aSpace1 = (u8*)&szCell[nMaxCells]; |
drh | ea598cb | 2009-04-05 12:22:08 +0000 | [diff] [blame] | 6404 | assert( EIGHT_BYTE_ALIGNMENT(aSpace1) ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6405 | |
| 6406 | /* |
| 6407 | ** Load pointers to all cells on sibling pages and the divider cells |
| 6408 | ** into the local apCell[] array. Make copies of the divider cells |
mistachkin | d557843 | 2012-08-25 10:01:29 +0000 | [diff] [blame] | 6409 | ** into space obtained from aSpace1[] and remove the divider cells |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 6410 | ** from pParent. |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6411 | ** |
| 6412 | ** If the siblings are on leaf pages, then the child pointers of the |
| 6413 | ** divider cells are stripped from the cells before they are copied |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 6414 | ** into aSpace1[]. In this way, all cells in apCell[] are without |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6415 | ** child pointers. If siblings are not leaves, then all cell in |
| 6416 | ** apCell[] include child pointers. Either way, all cells in apCell[] |
| 6417 | ** are alike. |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 6418 | ** |
| 6419 | ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf. |
| 6420 | ** leafData: 1 if pPage holds key+data and pParent holds only keys. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6421 | */ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6422 | leafCorrection = apOld[0]->leaf*4; |
drh | 3e28ff5 | 2014-09-24 00:59:08 +0000 | [diff] [blame] | 6423 | leafData = apOld[0]->intKeyLeaf; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6424 | for(i=0; i<nOld; i++){ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6425 | int limit; |
| 6426 | |
| 6427 | /* Before doing anything else, take a copy of the i'th original sibling |
| 6428 | ** The rest of this function will use data from the copies rather |
| 6429 | ** that the original pages since the original pages will be in the |
| 6430 | ** process of being overwritten. */ |
| 6431 | MemPage *pOld = apCopy[i] = (MemPage*)&aSpace1[pBt->pageSize + k*i]; |
| 6432 | memcpy(pOld, apOld[i], sizeof(MemPage)); |
| 6433 | pOld->aData = (void*)&pOld[1]; |
| 6434 | memcpy(pOld->aData, apOld[i]->aData, pBt->pageSize); |
| 6435 | |
| 6436 | limit = pOld->nCell+pOld->nOverflow; |
drh | 68f2a57 | 2011-06-03 17:50:49 +0000 | [diff] [blame] | 6437 | if( pOld->nOverflow>0 ){ |
| 6438 | for(j=0; j<limit; j++){ |
| 6439 | assert( nCell<nMaxCells ); |
| 6440 | apCell[nCell] = findOverflowCell(pOld, j); |
| 6441 | szCell[nCell] = cellSizePtr(pOld, apCell[nCell]); |
| 6442 | nCell++; |
| 6443 | } |
| 6444 | }else{ |
| 6445 | u8 *aData = pOld->aData; |
| 6446 | u16 maskPage = pOld->maskPage; |
| 6447 | u16 cellOffset = pOld->cellOffset; |
| 6448 | for(j=0; j<limit; j++){ |
| 6449 | assert( nCell<nMaxCells ); |
| 6450 | apCell[nCell] = findCellv2(aData, maskPage, cellOffset, j); |
| 6451 | szCell[nCell] = cellSizePtr(pOld, apCell[nCell]); |
| 6452 | nCell++; |
| 6453 | } |
| 6454 | } |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6455 | if( i<nOld-1 && !leafData){ |
shane | 36840fd | 2009-06-26 16:32:13 +0000 | [diff] [blame] | 6456 | u16 sz = (u16)szNew[i]; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6457 | u8 *pTemp; |
| 6458 | assert( nCell<nMaxCells ); |
| 6459 | szCell[nCell] = sz; |
| 6460 | pTemp = &aSpace1[iSpace1]; |
| 6461 | iSpace1 += sz; |
drh | e22e03e | 2010-08-18 21:19:03 +0000 | [diff] [blame] | 6462 | assert( sz<=pBt->maxLocal+23 ); |
drh | fcd71b6 | 2011-04-05 22:08:24 +0000 | [diff] [blame] | 6463 | assert( iSpace1 <= (int)pBt->pageSize ); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6464 | memcpy(pTemp, apDiv[i], sz); |
| 6465 | apCell[nCell] = pTemp+leafCorrection; |
| 6466 | assert( leafCorrection==0 || leafCorrection==4 ); |
shane | 36840fd | 2009-06-26 16:32:13 +0000 | [diff] [blame] | 6467 | szCell[nCell] = szCell[nCell] - leafCorrection; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6468 | if( !pOld->leaf ){ |
| 6469 | assert( leafCorrection==0 ); |
| 6470 | assert( pOld->hdrOffset==0 ); |
| 6471 | /* The right pointer of the child page pOld becomes the left |
| 6472 | ** pointer of the divider cell */ |
| 6473 | memcpy(apCell[nCell], &pOld->aData[8], 4); |
| 6474 | }else{ |
| 6475 | assert( leafCorrection==4 ); |
| 6476 | if( szCell[nCell]<4 ){ |
| 6477 | /* Do not allow any cells smaller than 4 bytes. */ |
| 6478 | szCell[nCell] = 4; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 6479 | } |
| 6480 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6481 | nCell++; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6482 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6483 | } |
| 6484 | |
| 6485 | /* |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6486 | ** Figure out the number of pages needed to hold all nCell cells. |
| 6487 | ** Store this number in "k". Also compute szNew[] which is the total |
| 6488 | ** 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] | 6489 | ** in apCell[] of the cell that divides page i from page i+1. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6490 | ** cntNew[k] should equal nCell. |
| 6491 | ** |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 6492 | ** Values computed by this block: |
| 6493 | ** |
| 6494 | ** k: The total number of sibling pages |
| 6495 | ** szNew[i]: Spaced used on the i-th sibling page. |
| 6496 | ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to |
| 6497 | ** the right of the i-th sibling page. |
| 6498 | ** usableSpace: Number of bytes of space available on each sibling. |
| 6499 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6500 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6501 | usableSpace = pBt->usableSize - 12 + leafCorrection; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6502 | for(subtotal=k=i=0; i<nCell; i++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 6503 | assert( i<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6504 | subtotal += szCell[i] + 2; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6505 | if( subtotal > usableSpace ){ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6506 | szNew[k] = subtotal - szCell[i]; |
| 6507 | cntNew[k] = i; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 6508 | if( leafData ){ i--; } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6509 | subtotal = 0; |
| 6510 | k++; |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 6511 | if( k>NB+1 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6512 | } |
| 6513 | } |
| 6514 | szNew[k] = subtotal; |
| 6515 | cntNew[k] = nCell; |
| 6516 | k++; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 6517 | |
| 6518 | /* |
| 6519 | ** The packing computed by the previous block is biased toward the siblings |
| 6520 | ** on the left side. The left siblings are always nearly full, while the |
| 6521 | ** right-most sibling might be nearly empty. This block of code attempts |
| 6522 | ** to adjust the packing of siblings to get a better balance. |
| 6523 | ** |
| 6524 | ** This adjustment is more than an optimization. The packing above might |
| 6525 | ** be so out of balance as to be illegal. For example, the right-most |
| 6526 | ** sibling might be completely empty. This adjustment is not optional. |
| 6527 | */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6528 | for(i=k-1; i>0; i--){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 6529 | int szRight = szNew[i]; /* Size of sibling on the right */ |
| 6530 | int szLeft = szNew[i-1]; /* Size of sibling on the left */ |
| 6531 | int r; /* Index of right-most cell in left sibling */ |
| 6532 | int d; /* Index of first cell to the left of right sibling */ |
| 6533 | |
| 6534 | r = cntNew[i-1] - 1; |
| 6535 | d = r + 1 - leafData; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 6536 | assert( d<nMaxCells ); |
| 6537 | assert( r<nMaxCells ); |
dan | f64cc49 | 2012-08-08 11:55:15 +0000 | [diff] [blame] | 6538 | while( szRight==0 |
| 6539 | || (!bBulk && szRight+szCell[d]+2<=szLeft-(szCell[r]+2)) |
| 6540 | ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6541 | szRight += szCell[d] + 2; |
| 6542 | szLeft -= szCell[r] + 2; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6543 | cntNew[i-1]--; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 6544 | r = cntNew[i-1] - 1; |
| 6545 | d = r + 1 - leafData; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6546 | } |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 6547 | szNew[i] = szRight; |
| 6548 | szNew[i-1] = szLeft; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6549 | } |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 6550 | |
danielk1977 | 6f235cc | 2009-06-04 14:46:08 +0000 | [diff] [blame] | 6551 | /* Either we found one or more cells (cntnew[0])>0) or pPage is |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 6552 | ** a virtual root page. A virtual root page is when the real root |
| 6553 | ** page is page 1 and we are the only child of that page. |
drh | 2f32fba | 2012-01-02 16:38:57 +0000 | [diff] [blame] | 6554 | ** |
| 6555 | ** UPDATE: The assert() below is not necessarily true if the database |
| 6556 | ** file is corrupt. The corruption will be detected and reported later |
| 6557 | ** in this procedure so there is no need to act upon it now. |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 6558 | */ |
drh | 2f32fba | 2012-01-02 16:38:57 +0000 | [diff] [blame] | 6559 | #if 0 |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 6560 | assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) ); |
drh | 2f32fba | 2012-01-02 16:38:57 +0000 | [diff] [blame] | 6561 | #endif |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6562 | |
danielk1977 | e576521 | 2009-06-17 11:13:28 +0000 | [diff] [blame] | 6563 | TRACE(("BALANCE: old: %d %d %d ", |
| 6564 | apOld[0]->pgno, |
| 6565 | nOld>=2 ? apOld[1]->pgno : 0, |
| 6566 | nOld>=3 ? apOld[2]->pgno : 0 |
| 6567 | )); |
| 6568 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6569 | /* |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 6570 | ** Allocate k new pages. Reuse old pages where possible. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6571 | */ |
drh | eac7442 | 2009-06-14 12:47:11 +0000 | [diff] [blame] | 6572 | if( apOld[0]->pgno<=1 ){ |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 6573 | rc = SQLITE_CORRUPT_BKPT; |
drh | eac7442 | 2009-06-14 12:47:11 +0000 | [diff] [blame] | 6574 | goto balance_cleanup; |
| 6575 | } |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6576 | pageFlags = apOld[0]->aData[0]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6577 | for(i=0; i<k; i++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6578 | MemPage *pNew; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 6579 | if( i<nOld ){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6580 | pNew = apNew[i] = apOld[i]; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 6581 | apOld[i] = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6582 | rc = sqlite3PagerWrite(pNew->pDbPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 6583 | nNew++; |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 6584 | if( rc ) goto balance_cleanup; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 6585 | }else{ |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 6586 | assert( i>0 ); |
dan | 428c218 | 2012-08-06 18:50:11 +0000 | [diff] [blame] | 6587 | rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 6588 | if( rc ) goto balance_cleanup; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6589 | apNew[i] = pNew; |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 6590 | nNew++; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6591 | |
| 6592 | /* Set the pointer-map entry for the new sibling page. */ |
| 6593 | if( ISAUTOVACUUM ){ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6594 | ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6595 | if( rc!=SQLITE_OK ){ |
| 6596 | goto balance_cleanup; |
| 6597 | } |
| 6598 | } |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 6599 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6600 | } |
| 6601 | |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 6602 | /* Free any old pages that were not reused as new pages. |
| 6603 | */ |
| 6604 | while( i<nOld ){ |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 6605 | freePage(apOld[i], &rc); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 6606 | if( rc ) goto balance_cleanup; |
| 6607 | releasePage(apOld[i]); |
| 6608 | apOld[i] = 0; |
| 6609 | i++; |
| 6610 | } |
| 6611 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6612 | /* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 6613 | ** Put the new pages in ascending order. This helps to |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 6614 | ** keep entries in the disk file in order so that a scan |
| 6615 | ** of the table is a linear scan through the file. That |
| 6616 | ** in turn helps the operating system to deliver pages |
| 6617 | ** from the disk more rapidly. |
| 6618 | ** |
| 6619 | ** An O(n^2) insertion sort algorithm is used, but since |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 6620 | ** n is never more than NB (a small constant), that should |
| 6621 | ** not be a problem. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 6622 | ** |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 6623 | ** When NB==3, this one optimization makes the database |
| 6624 | ** about 25% faster for large insertions and deletions. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 6625 | */ |
| 6626 | for(i=0; i<k-1; i++){ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6627 | int minV = apNew[i]->pgno; |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 6628 | int minI = i; |
| 6629 | for(j=i+1; j<k; j++){ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6630 | if( apNew[j]->pgno<(unsigned)minV ){ |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 6631 | minI = j; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6632 | minV = apNew[j]->pgno; |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 6633 | } |
| 6634 | } |
| 6635 | if( minI>i ){ |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 6636 | MemPage *pT; |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 6637 | pT = apNew[i]; |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 6638 | apNew[i] = apNew[minI]; |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 6639 | apNew[minI] = pT; |
| 6640 | } |
| 6641 | } |
danielk1977 | e576521 | 2009-06-17 11:13:28 +0000 | [diff] [blame] | 6642 | TRACE(("new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n", |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6643 | apNew[0]->pgno, szNew[0], |
| 6644 | nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0, |
| 6645 | nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0, |
| 6646 | nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0, |
| 6647 | nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0)); |
| 6648 | |
| 6649 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
| 6650 | put4byte(pRight, apNew[nNew-1]->pgno); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 6651 | |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 6652 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6653 | ** Evenly distribute the data in apCell[] across the new pages. |
| 6654 | ** Insert divider cells into pParent as necessary. |
| 6655 | */ |
| 6656 | j = 0; |
| 6657 | for(i=0; i<nNew; i++){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 6658 | /* Assemble the new sibling page. */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6659 | MemPage *pNew = apNew[i]; |
drh | 19642e5 | 2005-03-29 13:17:45 +0000 | [diff] [blame] | 6660 | assert( j<nMaxCells ); |
drh | 1013148 | 2008-07-11 03:34:09 +0000 | [diff] [blame] | 6661 | zeroPage(pNew, pageFlags); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 6662 | assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]); |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 6663 | assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6664 | assert( pNew->nOverflow==0 ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 6665 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 6666 | j = cntNew[i]; |
| 6667 | |
| 6668 | /* If the sibling page assembled above was not the right-most sibling, |
| 6669 | ** insert a divider cell into the parent page. |
| 6670 | */ |
danielk1977 | 1c3d2bf | 2009-06-23 16:40:17 +0000 | [diff] [blame] | 6671 | assert( i<nNew-1 || j==nCell ); |
| 6672 | if( j<nCell ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 6673 | u8 *pCell; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 6674 | u8 *pTemp; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 6675 | int sz; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 6676 | |
| 6677 | assert( j<nMaxCells ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 6678 | pCell = apCell[j]; |
| 6679 | sz = szCell[j] + leafCorrection; |
danielk1977 | 6067a9b | 2009-06-09 09:41:00 +0000 | [diff] [blame] | 6680 | pTemp = &aOvflSpace[iOvflSpace]; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6681 | if( !pNew->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6682 | memcpy(&pNew->aData[8], pCell, 4); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 6683 | }else if( leafData ){ |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 6684 | /* If the tree is a leaf-data tree, and the siblings are leaves, |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 6685 | ** then there is no divider cell in apCell[]. Instead, the divider |
| 6686 | ** cell consists of the integer key for the right-most cell of |
| 6687 | ** the sibling-page assembled above only. |
| 6688 | */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 6689 | CellInfo info; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 6690 | j--; |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 6691 | btreeParseCellPtr(pNew, apCell[j], &info); |
drh | e5ae573 | 2008-06-15 02:51:47 +0000 | [diff] [blame] | 6692 | pCell = pTemp; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6693 | sz = 4 + putVarint(&pCell[4], info.nKey); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 6694 | pTemp = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6695 | }else{ |
| 6696 | pCell -= 4; |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 6697 | /* Obscure case for non-leaf-data trees: If the cell at pCell was |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 6698 | ** previously stored on a leaf node, and its reported size was 4 |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 6699 | ** bytes, then it may actually be smaller than this |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 6700 | ** (see btreeParseCellPtr(), 4 bytes is the minimum size of |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 6701 | ** any cell). But it is important to pass the correct size to |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 6702 | ** insertCell(), so reparse the cell now. |
| 6703 | ** |
| 6704 | ** Note that this can never happen in an SQLite data file, as all |
| 6705 | ** cells are at least 4 bytes. It only happens in b-trees used |
| 6706 | ** to evaluate "IN (SELECT ...)" and similar clauses. |
| 6707 | */ |
| 6708 | if( szCell[j]==4 ){ |
| 6709 | assert(leafCorrection==4); |
| 6710 | sz = cellSizePtr(pParent, pCell); |
| 6711 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6712 | } |
danielk1977 | 6067a9b | 2009-06-09 09:41:00 +0000 | [diff] [blame] | 6713 | iOvflSpace += sz; |
drh | e22e03e | 2010-08-18 21:19:03 +0000 | [diff] [blame] | 6714 | assert( sz<=pBt->maxLocal+23 ); |
drh | fcd71b6 | 2011-04-05 22:08:24 +0000 | [diff] [blame] | 6715 | assert( iOvflSpace <= (int)pBt->pageSize ); |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6716 | insertCell(pParent, nxDiv, pCell, sz, pTemp, pNew->pgno, &rc); |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 6717 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | c5053fb | 2008-11-27 02:22:10 +0000 | [diff] [blame] | 6718 | assert( sqlite3PagerIswriteable(pParent->pDbPage) ); |
danielk1977 | 85d90ca | 2008-07-19 14:25:15 +0000 | [diff] [blame] | 6719 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6720 | j++; |
| 6721 | nxDiv++; |
| 6722 | } |
| 6723 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6724 | assert( j==nCell ); |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 6725 | assert( nOld>0 ); |
| 6726 | assert( nNew>0 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6727 | if( (pageFlags & PTF_LEAF)==0 ){ |
danielk1977 | 87c52b5 | 2008-07-19 11:49:07 +0000 | [diff] [blame] | 6728 | u8 *zChild = &apCopy[nOld-1]->aData[8]; |
| 6729 | memcpy(&apNew[nNew-1]->aData[8], zChild, 4); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6730 | } |
| 6731 | |
danielk1977 | 13bd99f | 2009-06-24 05:40:34 +0000 | [diff] [blame] | 6732 | if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){ |
| 6733 | /* The root page of the b-tree now contains no cells. The only sibling |
| 6734 | ** page is the right-child of the parent. Copy the contents of the |
| 6735 | ** child page into the parent, decreasing the overall height of the |
| 6736 | ** b-tree structure by one. This is described as the "balance-shallower" |
| 6737 | ** sub-algorithm in some documentation. |
| 6738 | ** |
| 6739 | ** If this is an auto-vacuum database, the call to copyNodeContent() |
| 6740 | ** sets all pointer-map entries corresponding to database image pages |
| 6741 | ** for which the pointer is stored within the content being copied. |
| 6742 | ** |
| 6743 | ** The second assert below verifies that the child page is defragmented |
| 6744 | ** (it must be, as it was just reconstructed using assemblePage()). This |
| 6745 | ** is important if the parent page happens to be page 1 of the database |
| 6746 | ** image. */ |
| 6747 | assert( nNew==1 ); |
| 6748 | assert( apNew[0]->nFree == |
| 6749 | (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2) |
| 6750 | ); |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 6751 | copyNodeContent(apNew[0], pParent, &rc); |
| 6752 | freePage(apNew[0], &rc); |
danielk1977 | 13bd99f | 2009-06-24 05:40:34 +0000 | [diff] [blame] | 6753 | }else if( ISAUTOVACUUM ){ |
| 6754 | /* Fix the pointer-map entries for all the cells that were shifted around. |
| 6755 | ** There are several different types of pointer-map entries that need to |
| 6756 | ** be dealt with by this routine. Some of these have been set already, but |
| 6757 | ** many have not. The following is a summary: |
| 6758 | ** |
| 6759 | ** 1) The entries associated with new sibling pages that were not |
| 6760 | ** siblings when this function was called. These have already |
| 6761 | ** been set. We don't need to worry about old siblings that were |
| 6762 | ** moved to the free-list - the freePage() code has taken care |
| 6763 | ** of those. |
| 6764 | ** |
| 6765 | ** 2) The pointer-map entries associated with the first overflow |
| 6766 | ** page in any overflow chains used by new divider cells. These |
| 6767 | ** have also already been taken care of by the insertCell() code. |
| 6768 | ** |
| 6769 | ** 3) If the sibling pages are not leaves, then the child pages of |
| 6770 | ** cells stored on the sibling pages may need to be updated. |
| 6771 | ** |
| 6772 | ** 4) If the sibling pages are not internal intkey nodes, then any |
| 6773 | ** overflow pages used by these cells may need to be updated |
| 6774 | ** (internal intkey nodes never contain pointers to overflow pages). |
| 6775 | ** |
| 6776 | ** 5) If the sibling pages are not leaves, then the pointer-map |
| 6777 | ** entries for the right-child pages of each sibling may need |
| 6778 | ** to be updated. |
| 6779 | ** |
| 6780 | ** Cases 1 and 2 are dealt with above by other code. The next |
| 6781 | ** block deals with cases 3 and 4 and the one after that, case 5. Since |
| 6782 | ** setting a pointer map entry is a relatively expensive operation, this |
| 6783 | ** code only sets pointer map entries for child or overflow pages that have |
| 6784 | ** actually moved between pages. */ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6785 | MemPage *pNew = apNew[0]; |
| 6786 | MemPage *pOld = apCopy[0]; |
| 6787 | int nOverflow = pOld->nOverflow; |
| 6788 | int iNextOld = pOld->nCell + nOverflow; |
drh | 2cbd78b | 2012-02-02 19:37:18 +0000 | [diff] [blame] | 6789 | int iOverflow = (nOverflow ? pOld->aiOvfl[0] : -1); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6790 | j = 0; /* Current 'old' sibling page */ |
| 6791 | k = 0; /* Current 'new' sibling page */ |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 6792 | for(i=0; i<nCell; i++){ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6793 | int isDivider = 0; |
| 6794 | while( i==iNextOld ){ |
| 6795 | /* Cell i is the cell immediately following the last cell on old |
| 6796 | ** sibling page j. If the siblings are not leaf pages of an |
| 6797 | ** intkey b-tree, then cell i was a divider cell. */ |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 6798 | assert( j+1 < ArraySize(apCopy) ); |
drh | ec73930 | 2012-08-14 18:43:39 +0000 | [diff] [blame] | 6799 | assert( j+1 < nOld ); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6800 | pOld = apCopy[++j]; |
| 6801 | iNextOld = i + !leafData + pOld->nCell + pOld->nOverflow; |
| 6802 | if( pOld->nOverflow ){ |
| 6803 | nOverflow = pOld->nOverflow; |
drh | 2cbd78b | 2012-02-02 19:37:18 +0000 | [diff] [blame] | 6804 | iOverflow = i + !leafData + pOld->aiOvfl[0]; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6805 | } |
| 6806 | isDivider = !leafData; |
| 6807 | } |
| 6808 | |
| 6809 | assert(nOverflow>0 || iOverflow<i ); |
drh | 2cbd78b | 2012-02-02 19:37:18 +0000 | [diff] [blame] | 6810 | assert(nOverflow<2 || pOld->aiOvfl[0]==pOld->aiOvfl[1]-1); |
| 6811 | assert(nOverflow<3 || pOld->aiOvfl[1]==pOld->aiOvfl[2]-1); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6812 | if( i==iOverflow ){ |
| 6813 | isDivider = 1; |
| 6814 | if( (--nOverflow)>0 ){ |
| 6815 | iOverflow++; |
| 6816 | } |
| 6817 | } |
| 6818 | |
| 6819 | if( i==cntNew[k] ){ |
| 6820 | /* Cell i is the cell immediately following the last cell on new |
| 6821 | ** sibling page k. If the siblings are not leaf pages of an |
| 6822 | ** intkey b-tree, then cell i is a divider cell. */ |
| 6823 | pNew = apNew[++k]; |
| 6824 | if( !leafData ) continue; |
| 6825 | } |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6826 | assert( j<nOld ); |
| 6827 | assert( k<nNew ); |
| 6828 | |
| 6829 | /* If the cell was originally divider cell (and is not now) or |
| 6830 | ** an overflow cell, or if the cell was located on a different sibling |
| 6831 | ** page before the balancing, then the pointer map entries associated |
| 6832 | ** with any child or overflow pages need to be updated. */ |
| 6833 | if( isDivider || pOld->pgno!=pNew->pgno ){ |
| 6834 | if( !leafCorrection ){ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6835 | ptrmapPut(pBt, get4byte(apCell[i]), PTRMAP_BTREE, pNew->pgno, &rc); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6836 | } |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6837 | if( szCell[i]>pNew->minLocal ){ |
| 6838 | ptrmapPutOvflPtr(pNew, apCell[i], &rc); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6839 | } |
| 6840 | } |
| 6841 | } |
| 6842 | |
| 6843 | if( !leafCorrection ){ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6844 | for(i=0; i<nNew; i++){ |
| 6845 | u32 key = get4byte(&apNew[i]->aData[8]); |
| 6846 | ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc); |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 6847 | } |
| 6848 | } |
| 6849 | |
| 6850 | #if 0 |
| 6851 | /* The ptrmapCheckPages() contains assert() statements that verify that |
| 6852 | ** all pointer map pages are set correctly. This is helpful while |
| 6853 | ** debugging. This is usually disabled because a corrupt database may |
| 6854 | ** cause an assert() statement to fail. */ |
| 6855 | ptrmapCheckPages(apNew, nNew); |
| 6856 | ptrmapCheckPages(&pParent, 1); |
| 6857 | #endif |
| 6858 | } |
| 6859 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6860 | assert( pParent->isInit ); |
danielk1977 | e576521 | 2009-06-17 11:13:28 +0000 | [diff] [blame] | 6861 | TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n", |
| 6862 | nOld, nNew, nCell)); |
danielk1977 | cd581a7 | 2009-06-23 15:43:39 +0000 | [diff] [blame] | 6863 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6864 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6865 | ** Cleanup before returning. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6866 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6867 | balance_cleanup: |
drh | facf030 | 2008-06-17 15:12:00 +0000 | [diff] [blame] | 6868 | sqlite3ScratchFree(apCell); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6869 | for(i=0; i<nOld; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 6870 | releasePage(apOld[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6871 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 6872 | for(i=0; i<nNew; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 6873 | releasePage(apNew[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6874 | } |
danielk1977 | eaa06f6 | 2008-09-18 17:34:44 +0000 | [diff] [blame] | 6875 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6876 | return rc; |
| 6877 | } |
mistachkin | e7c5416 | 2012-10-02 22:54:27 +0000 | [diff] [blame] | 6878 | #if defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_M_ARM) |
| 6879 | #pragma optimize("", on) |
| 6880 | #endif |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6881 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6882 | |
| 6883 | /* |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6884 | ** This function is called when the root page of a b-tree structure is |
| 6885 | ** overfull (has one or more overflow pages). |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6886 | ** |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6887 | ** A new child page is allocated and the contents of the current root |
| 6888 | ** page, including overflow cells, are copied into the child. The root |
| 6889 | ** page is then overwritten to make it an empty page with the right-child |
| 6890 | ** pointer pointing to the new page. |
| 6891 | ** |
| 6892 | ** Before returning, all pointer-map entries corresponding to pages |
| 6893 | ** that the new child-page now contains pointers to are updated. The |
| 6894 | ** entry corresponding to the new right-child pointer of the root |
| 6895 | ** page is also updated. |
| 6896 | ** |
| 6897 | ** If successful, *ppChild is set to contain a reference to the child |
| 6898 | ** page and SQLITE_OK is returned. In this case the caller is required |
| 6899 | ** to call releasePage() on *ppChild exactly once. If an error occurs, |
| 6900 | ** an error code is returned and *ppChild is set to 0. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6901 | */ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6902 | static int balance_deeper(MemPage *pRoot, MemPage **ppChild){ |
| 6903 | int rc; /* Return value from subprocedures */ |
| 6904 | MemPage *pChild = 0; /* Pointer to a new child page */ |
shane | 5eff7cf | 2009-08-10 03:57:58 +0000 | [diff] [blame] | 6905 | Pgno pgnoChild = 0; /* Page number of the new child page */ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6906 | BtShared *pBt = pRoot->pBt; /* The BTree */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6907 | |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6908 | assert( pRoot->nOverflow>0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6909 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | bc2ca9e | 2008-11-13 14:28:28 +0000 | [diff] [blame] | 6910 | |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6911 | /* Make pRoot, the root page of the b-tree, writable. Allocate a new |
| 6912 | ** page that will become the new right-child of pPage. Copy the contents |
| 6913 | ** of the node stored on pRoot into the new child page. |
| 6914 | */ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6915 | rc = sqlite3PagerWrite(pRoot->pDbPage); |
| 6916 | if( rc==SQLITE_OK ){ |
| 6917 | rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0); |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 6918 | copyNodeContent(pRoot, pChild, &rc); |
| 6919 | if( ISAUTOVACUUM ){ |
| 6920 | ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc); |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 6921 | } |
| 6922 | } |
| 6923 | if( rc ){ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6924 | *ppChild = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6925 | releasePage(pChild); |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6926 | return rc; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6927 | } |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6928 | assert( sqlite3PagerIswriteable(pChild->pDbPage) ); |
| 6929 | assert( sqlite3PagerIswriteable(pRoot->pDbPage) ); |
| 6930 | assert( pChild->nCell==pRoot->nCell ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6931 | |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6932 | TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno)); |
| 6933 | |
| 6934 | /* Copy the overflow cells from pRoot to pChild */ |
drh | 2cbd78b | 2012-02-02 19:37:18 +0000 | [diff] [blame] | 6935 | memcpy(pChild->aiOvfl, pRoot->aiOvfl, |
| 6936 | pRoot->nOverflow*sizeof(pRoot->aiOvfl[0])); |
| 6937 | memcpy(pChild->apOvfl, pRoot->apOvfl, |
| 6938 | pRoot->nOverflow*sizeof(pRoot->apOvfl[0])); |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6939 | pChild->nOverflow = pRoot->nOverflow; |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6940 | |
| 6941 | /* Zero the contents of pRoot. Then install pChild as the right-child. */ |
| 6942 | zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF); |
| 6943 | put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild); |
| 6944 | |
| 6945 | *ppChild = pChild; |
| 6946 | return SQLITE_OK; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6947 | } |
| 6948 | |
| 6949 | /* |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6950 | ** The page that pCur currently points to has just been modified in |
| 6951 | ** some way. This function figures out if this modification means the |
| 6952 | ** tree needs to be balanced, and if so calls the appropriate balancing |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6953 | ** routine. Balancing routines are: |
| 6954 | ** |
| 6955 | ** balance_quick() |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6956 | ** balance_deeper() |
| 6957 | ** balance_nonroot() |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6958 | */ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6959 | static int balance(BtCursor *pCur){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6960 | int rc = SQLITE_OK; |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6961 | const int nMin = pCur->pBt->usableSize * 2 / 3; |
| 6962 | u8 aBalanceQuickSpace[13]; |
| 6963 | u8 *pFree = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 6964 | |
shane | 75ac1de | 2009-06-09 18:58:52 +0000 | [diff] [blame] | 6965 | TESTONLY( int balance_quick_called = 0 ); |
| 6966 | TESTONLY( int balance_deeper_called = 0 ); |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6967 | |
| 6968 | do { |
| 6969 | int iPage = pCur->iPage; |
| 6970 | MemPage *pPage = pCur->apPage[iPage]; |
| 6971 | |
| 6972 | if( iPage==0 ){ |
| 6973 | if( pPage->nOverflow ){ |
| 6974 | /* The root page of the b-tree is overfull. In this case call the |
| 6975 | ** balance_deeper() function to create a new child for the root-page |
| 6976 | ** and copy the current contents of the root-page to it. The |
| 6977 | ** next iteration of the do-loop will balance the child page. |
| 6978 | */ |
| 6979 | assert( (balance_deeper_called++)==0 ); |
| 6980 | rc = balance_deeper(pPage, &pCur->apPage[1]); |
| 6981 | if( rc==SQLITE_OK ){ |
| 6982 | pCur->iPage = 1; |
| 6983 | pCur->aiIdx[0] = 0; |
| 6984 | pCur->aiIdx[1] = 0; |
| 6985 | assert( pCur->apPage[1]->nOverflow ); |
| 6986 | } |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6987 | }else{ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 6988 | break; |
| 6989 | } |
| 6990 | }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){ |
| 6991 | break; |
| 6992 | }else{ |
| 6993 | MemPage * const pParent = pCur->apPage[iPage-1]; |
| 6994 | int const iIdx = pCur->aiIdx[iPage-1]; |
| 6995 | |
| 6996 | rc = sqlite3PagerWrite(pParent->pDbPage); |
| 6997 | if( rc==SQLITE_OK ){ |
| 6998 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | 3e28ff5 | 2014-09-24 00:59:08 +0000 | [diff] [blame] | 6999 | if( pPage->intKeyLeaf |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7000 | && pPage->nOverflow==1 |
drh | 2cbd78b | 2012-02-02 19:37:18 +0000 | [diff] [blame] | 7001 | && pPage->aiOvfl[0]==pPage->nCell |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7002 | && pParent->pgno!=1 |
| 7003 | && pParent->nCell==iIdx |
| 7004 | ){ |
| 7005 | /* Call balance_quick() to create a new sibling of pPage on which |
| 7006 | ** to store the overflow cell. balance_quick() inserts a new cell |
| 7007 | ** into pParent, which may cause pParent overflow. If this |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 7008 | ** happens, the next iteration of the do-loop will balance pParent |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7009 | ** use either balance_nonroot() or balance_deeper(). Until this |
| 7010 | ** happens, the overflow cell is stored in the aBalanceQuickSpace[] |
| 7011 | ** buffer. |
| 7012 | ** |
| 7013 | ** The purpose of the following assert() is to check that only a |
| 7014 | ** single call to balance_quick() is made for each call to this |
| 7015 | ** function. If this were not verified, a subtle bug involving reuse |
| 7016 | ** of the aBalanceQuickSpace[] might sneak in. |
| 7017 | */ |
| 7018 | assert( (balance_quick_called++)==0 ); |
| 7019 | rc = balance_quick(pParent, pPage, aBalanceQuickSpace); |
| 7020 | }else |
| 7021 | #endif |
| 7022 | { |
| 7023 | /* In this case, call balance_nonroot() to redistribute cells |
| 7024 | ** between pPage and up to 2 of its sibling pages. This involves |
| 7025 | ** modifying the contents of pParent, which may cause pParent to |
| 7026 | ** become overfull or underfull. The next iteration of the do-loop |
| 7027 | ** will balance the parent page to correct this. |
| 7028 | ** |
| 7029 | ** If the parent page becomes overfull, the overflow cell or cells |
| 7030 | ** are stored in the pSpace buffer allocated immediately below. |
| 7031 | ** A subsequent iteration of the do-loop will deal with this by |
| 7032 | ** calling balance_nonroot() (balance_deeper() may be called first, |
| 7033 | ** but it doesn't deal with overflow cells - just moves them to a |
| 7034 | ** different page). Once this subsequent call to balance_nonroot() |
| 7035 | ** has completed, it is safe to release the pSpace buffer used by |
| 7036 | ** the previous call, as the overflow cell data will have been |
| 7037 | ** copied either into the body of a database page or into the new |
| 7038 | ** pSpace buffer passed to the latter call to balance_nonroot(). |
| 7039 | */ |
| 7040 | u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize); |
dan | 428c218 | 2012-08-06 18:50:11 +0000 | [diff] [blame] | 7041 | rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1, pCur->hints); |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7042 | if( pFree ){ |
| 7043 | /* If pFree is not NULL, it points to the pSpace buffer used |
| 7044 | ** by a previous call to balance_nonroot(). Its contents are |
| 7045 | ** now stored either on real database pages or within the |
| 7046 | ** new pSpace buffer, so it may be safely freed here. */ |
| 7047 | sqlite3PageFree(pFree); |
| 7048 | } |
| 7049 | |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 7050 | /* The pSpace buffer will be freed after the next call to |
| 7051 | ** balance_nonroot(), or just before this function returns, whichever |
| 7052 | ** comes first. */ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7053 | pFree = pSpace; |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7054 | } |
| 7055 | } |
| 7056 | |
| 7057 | pPage->nOverflow = 0; |
| 7058 | |
| 7059 | /* The next iteration of the do-loop balances the parent page. */ |
| 7060 | releasePage(pPage); |
| 7061 | pCur->iPage--; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 7062 | } |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7063 | }while( rc==SQLITE_OK ); |
| 7064 | |
| 7065 | if( pFree ){ |
| 7066 | sqlite3PageFree(pFree); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 7067 | } |
| 7068 | return rc; |
| 7069 | } |
| 7070 | |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 7071 | |
| 7072 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 7073 | ** Insert a new record into the BTree. The key is given by (pKey,nKey) |
| 7074 | ** 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] | 7075 | ** define what table the record should be inserted into. The cursor |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7076 | ** is left pointing at a random location. |
| 7077 | ** |
| 7078 | ** For an INTKEY table, only the nKey value of the key is used. pKey is |
| 7079 | ** ignored. For a ZERODATA table, the pData and nData are both ignored. |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 7080 | ** |
| 7081 | ** If the seekResult parameter is non-zero, then a successful call to |
danielk1977 | 3509a65 | 2009-07-06 18:56:13 +0000 | [diff] [blame] | 7082 | ** MovetoUnpacked() to seek cursor pCur to (pKey, nKey) has already |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 7083 | ** been performed. seekResult is the search result returned (a negative |
| 7084 | ** 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] | 7085 | ** a positive value if pCur points at an entry that is larger than |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 7086 | ** (pKey, nKey)). |
| 7087 | ** |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 7088 | ** If the seekResult parameter is non-zero, then the caller guarantees that |
| 7089 | ** cursor pCur is pointing at the existing copy of a row that is to be |
| 7090 | ** overwritten. If the seekResult parameter is 0, then cursor pCur may |
| 7091 | ** 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] | 7092 | ** the cursor before the new key can be inserted. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 7093 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 7094 | int sqlite3BtreeInsert( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 7095 | BtCursor *pCur, /* Insert data into the table of this cursor */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 7096 | const void *pKey, i64 nKey, /* The key of the new record */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 7097 | const void *pData, int nData, /* The data of the new record */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 7098 | int nZero, /* Number of extra 0 bytes to append to data */ |
danielk1977 | de63035 | 2009-05-04 11:42:29 +0000 | [diff] [blame] | 7099 | int appendBias, /* True if this is likely an append */ |
danielk1977 | 3509a65 | 2009-07-06 18:56:13 +0000 | [diff] [blame] | 7100 | int seekResult /* Result of prior MovetoUnpacked() call */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 7101 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 7102 | int rc; |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 7103 | int loc = seekResult; /* -1: before desired location +1: after */ |
drh | 1d452e1 | 2009-11-01 19:26:59 +0000 | [diff] [blame] | 7104 | int szNew = 0; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 7105 | int idx; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 7106 | MemPage *pPage; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7107 | Btree *p = pCur->pBtree; |
| 7108 | BtShared *pBt = p->pBt; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 7109 | unsigned char *oldCell; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7110 | unsigned char *newCell = 0; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 7111 | |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 7112 | if( pCur->eState==CURSOR_FAULT ){ |
| 7113 | assert( pCur->skipNext!=SQLITE_OK ); |
| 7114 | return pCur->skipNext; |
| 7115 | } |
| 7116 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7117 | assert( cursorHoldsMutex(pCur) ); |
drh | 3f38740 | 2014-09-24 01:23:00 +0000 | [diff] [blame] | 7118 | assert( (pCur->curFlags & BTCF_WriteFlag)!=0 |
| 7119 | && pBt->inTransaction==TRANS_WRITE |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 7120 | && (pBt->btsFlags & BTS_READ_ONLY)==0 ); |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 7121 | assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) ); |
| 7122 | |
danielk1977 | 31d31b8 | 2009-07-13 13:18:07 +0000 | [diff] [blame] | 7123 | /* Assert that the caller has been consistent. If this cursor was opened |
| 7124 | ** expecting an index b-tree, then the caller should be inserting blob |
| 7125 | ** keys with no associated data. If the cursor was opened expecting an |
| 7126 | ** intkey table, the caller should be inserting integer keys with a |
| 7127 | ** blob of associated data. */ |
| 7128 | assert( (pKey==0)==(pCur->pKeyInfo==0) ); |
| 7129 | |
danielk1977 | 9c3acf3 | 2009-05-02 07:36:49 +0000 | [diff] [blame] | 7130 | /* Save the positions of any other cursors open on this table. |
| 7131 | ** |
danielk1977 | 3509a65 | 2009-07-06 18:56:13 +0000 | [diff] [blame] | 7132 | ** In some cases, the call to btreeMoveto() below is a no-op. For |
danielk1977 | 9c3acf3 | 2009-05-02 07:36:49 +0000 | [diff] [blame] | 7133 | ** example, when inserting data into a table with auto-generated integer |
| 7134 | ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the |
| 7135 | ** integer key to use. It then calls this function to actually insert the |
danielk1977 | 3509a65 | 2009-07-06 18:56:13 +0000 | [diff] [blame] | 7136 | ** data into the intkey B-Tree. In this case btreeMoveto() recognizes |
danielk1977 | 9c3acf3 | 2009-05-02 07:36:49 +0000 | [diff] [blame] | 7137 | ** that the cursor is already where it needs to be and returns without |
| 7138 | ** doing any work. To avoid thwarting these optimizations, it is important |
| 7139 | ** not to clear the cursor here. |
| 7140 | */ |
drh | 4c301aa | 2009-07-15 17:25:45 +0000 | [diff] [blame] | 7141 | rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur); |
| 7142 | if( rc ) return rc; |
drh | d60f4f4 | 2012-03-23 14:23:52 +0000 | [diff] [blame] | 7143 | |
drh | d60f4f4 | 2012-03-23 14:23:52 +0000 | [diff] [blame] | 7144 | if( pCur->pKeyInfo==0 ){ |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 7145 | /* If this is an insert into a table b-tree, invalidate any incrblob |
| 7146 | ** cursors open on the row being replaced */ |
drh | d60f4f4 | 2012-03-23 14:23:52 +0000 | [diff] [blame] | 7147 | invalidateIncrblobCursors(p, nKey, 0); |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 7148 | |
| 7149 | /* If the cursor is currently on the last row and we are appending a |
| 7150 | ** new row onto the end, set the "loc" to avoid an unnecessary btreeMoveto() |
| 7151 | ** call */ |
drh | 3f38740 | 2014-09-24 01:23:00 +0000 | [diff] [blame] | 7152 | if( (pCur->curFlags&BTCF_ValidNKey)!=0 && nKey>0 |
| 7153 | && pCur->info.nKey==nKey-1 ){ |
drh | e0670b6 | 2014-02-12 21:31:12 +0000 | [diff] [blame] | 7154 | loc = -1; |
| 7155 | } |
drh | d60f4f4 | 2012-03-23 14:23:52 +0000 | [diff] [blame] | 7156 | } |
| 7157 | |
drh | 4c301aa | 2009-07-15 17:25:45 +0000 | [diff] [blame] | 7158 | if( !loc ){ |
| 7159 | rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc); |
| 7160 | if( rc ) return rc; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7161 | } |
danielk1977 | b980d221 | 2009-06-22 18:03:51 +0000 | [diff] [blame] | 7162 | assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7163 | |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 7164 | pPage = pCur->apPage[pCur->iPage]; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 7165 | assert( pPage->intKey || nKey>=0 ); |
drh | 4484522 | 2008-07-17 18:39:57 +0000 | [diff] [blame] | 7166 | assert( pPage->leaf || !pPage->intKey ); |
danielk1977 | 8f880a8 | 2009-07-13 09:41:45 +0000 | [diff] [blame] | 7167 | |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 7168 | TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n", |
| 7169 | pCur->pgnoRoot, nKey, nData, pPage->pgno, |
| 7170 | loc==0 ? "overwrite" : "new entry")); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 7171 | assert( pPage->isInit ); |
danielk1977 | 52ae724 | 2008-03-25 14:24:56 +0000 | [diff] [blame] | 7172 | allocateTempSpace(pBt); |
| 7173 | newCell = pBt->pTmpSpace; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7174 | if( newCell==0 ) return SQLITE_NOMEM; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 7175 | rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7176 | if( rc ) goto end_insert; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 7177 | assert( szNew==cellSizePtr(pPage, newCell) ); |
drh | fcd71b6 | 2011-04-05 22:08:24 +0000 | [diff] [blame] | 7178 | assert( szNew <= MX_CELL_SIZE(pBt) ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 7179 | idx = pCur->aiIdx[pCur->iPage]; |
danielk1977 | b980d221 | 2009-06-22 18:03:51 +0000 | [diff] [blame] | 7180 | if( loc==0 ){ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 7181 | u16 szOld; |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 7182 | assert( idx<pPage->nCell ); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 7183 | rc = sqlite3PagerWrite(pPage->pDbPage); |
| 7184 | if( rc ){ |
| 7185 | goto end_insert; |
| 7186 | } |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 7187 | oldCell = findCell(pPage, idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7188 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 7189 | memcpy(newCell, oldCell, 4); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7190 | } |
drh | 9bfdc25 | 2014-09-24 02:05:41 +0000 | [diff] [blame] | 7191 | rc = clearCell(pPage, oldCell, &szOld); |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 7192 | dropCell(pPage, idx, szOld, &rc); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7193 | if( rc ) goto end_insert; |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 7194 | }else if( loc<0 && pPage->nCell>0 ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7195 | assert( pPage->leaf ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 7196 | idx = ++pCur->aiIdx[pCur->iPage]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 7197 | }else{ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7198 | assert( pPage->leaf ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 7199 | } |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 7200 | insertCell(pPage, idx, newCell, szNew, 0, 0, &rc); |
danielk1977 | 3f632d5 | 2009-05-02 10:03:09 +0000 | [diff] [blame] | 7201 | assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 ); |
drh | 9bf9e9c | 2008-12-05 20:01:43 +0000 | [diff] [blame] | 7202 | |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 7203 | /* If no error has occurred and pPage has an overflow cell, call balance() |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7204 | ** to redistribute the cells within the tree. Since balance() may move |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 7205 | ** the cursor, zero the BtCursor.info.nSize and BTCF_ValidNKey |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7206 | ** variables. |
danielk1977 | 3f632d5 | 2009-05-02 10:03:09 +0000 | [diff] [blame] | 7207 | ** |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7208 | ** Previous versions of SQLite called moveToRoot() to move the cursor |
| 7209 | ** back to the root page as balance() used to invalidate the contents |
danielk1977 | 54109bb | 2009-06-23 11:22:29 +0000 | [diff] [blame] | 7210 | ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that, |
| 7211 | ** set the cursor state to "invalid". This makes common insert operations |
| 7212 | ** slightly faster. |
danielk1977 | 3f632d5 | 2009-05-02 10:03:09 +0000 | [diff] [blame] | 7213 | ** |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7214 | ** There is a subtle but important optimization here too. When inserting |
| 7215 | ** multiple records into an intkey b-tree using a single cursor (as can |
| 7216 | ** happen while processing an "INSERT INTO ... SELECT" statement), it |
| 7217 | ** is advantageous to leave the cursor pointing to the last entry in |
| 7218 | ** the b-tree if possible. If the cursor is left pointing to the last |
| 7219 | ** entry in the table, and the next row inserted has an integer key |
| 7220 | ** larger than the largest existing key, it is possible to insert the |
| 7221 | ** row without seeking the cursor. This can be a big performance boost. |
danielk1977 | 3f632d5 | 2009-05-02 10:03:09 +0000 | [diff] [blame] | 7222 | */ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7223 | pCur->info.nSize = 0; |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7224 | if( rc==SQLITE_OK && pPage->nOverflow ){ |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 7225 | pCur->curFlags &= ~(BTCF_ValidNKey); |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7226 | rc = balance(pCur); |
| 7227 | |
| 7228 | /* Must make sure nOverflow is reset to zero even if the balance() |
danielk1977 | 54109bb | 2009-06-23 11:22:29 +0000 | [diff] [blame] | 7229 | ** fails. Internal data structure corruption will result otherwise. |
| 7230 | ** Also, set the cursor state to invalid. This stops saveCursorPosition() |
| 7231 | ** from trying to save the current position of the cursor. */ |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7232 | pCur->apPage[pCur->iPage]->nOverflow = 0; |
danielk1977 | 54109bb | 2009-06-23 11:22:29 +0000 | [diff] [blame] | 7233 | pCur->eState = CURSOR_INVALID; |
danielk1977 | 3f632d5 | 2009-05-02 10:03:09 +0000 | [diff] [blame] | 7234 | } |
danielk1977 | a50d9aa | 2009-06-08 14:49:45 +0000 | [diff] [blame] | 7235 | assert( pCur->apPage[pCur->iPage]->nOverflow==0 ); |
drh | 9bf9e9c | 2008-12-05 20:01:43 +0000 | [diff] [blame] | 7236 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7237 | end_insert: |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 7238 | return rc; |
| 7239 | } |
| 7240 | |
| 7241 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7242 | ** Delete the entry that the cursor is pointing to. The cursor |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 7243 | ** is left pointing at an arbitrary location. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 7244 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 7245 | int sqlite3BtreeDelete(BtCursor *pCur){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7246 | Btree *p = pCur->pBtree; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 7247 | BtShared *pBt = p->pBt; |
| 7248 | int rc; /* Return code */ |
| 7249 | MemPage *pPage; /* Page to delete cell from */ |
| 7250 | unsigned char *pCell; /* Pointer to cell to delete */ |
| 7251 | int iCellIdx; /* Index of cell to delete */ |
| 7252 | int iCellDepth; /* Depth of node containing pCell */ |
drh | 9bfdc25 | 2014-09-24 02:05:41 +0000 | [diff] [blame] | 7253 | u16 szCell; /* Size of the cell being deleted */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7254 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7255 | assert( cursorHoldsMutex(pCur) ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 7256 | assert( pBt->inTransaction==TRANS_WRITE ); |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 7257 | assert( (pBt->btsFlags & BTS_READ_ONLY)==0 ); |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 7258 | assert( pCur->curFlags & BTCF_WriteFlag ); |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 7259 | assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) ); |
| 7260 | assert( !hasReadConflicts(p, pCur->pgnoRoot) ); |
| 7261 | |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 7262 | if( NEVER(pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell) |
| 7263 | || NEVER(pCur->eState!=CURSOR_VALID) |
| 7264 | ){ |
| 7265 | return SQLITE_ERROR; /* Something has gone awry. */ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 7266 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7267 | |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 7268 | iCellDepth = pCur->iPage; |
| 7269 | iCellIdx = pCur->aiIdx[iCellDepth]; |
| 7270 | pPage = pCur->apPage[iCellDepth]; |
| 7271 | pCell = findCell(pPage, iCellIdx); |
| 7272 | |
| 7273 | /* If the page containing the entry to delete is not a leaf page, move |
| 7274 | ** the cursor to the largest entry in the tree that is smaller than |
| 7275 | ** the entry being deleted. This cell will replace the cell being deleted |
| 7276 | ** from the internal node. The 'previous' entry is used for this instead |
| 7277 | ** of the 'next' entry, as the previous entry is always a part of the |
| 7278 | ** sub-tree headed by the child page of the cell being deleted. This makes |
| 7279 | ** balancing the tree following the delete operation easier. */ |
| 7280 | if( !pPage->leaf ){ |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 7281 | int notUsed = 0; |
drh | 4c301aa | 2009-07-15 17:25:45 +0000 | [diff] [blame] | 7282 | rc = sqlite3BtreePrevious(pCur, ¬Used); |
| 7283 | if( rc ) return rc; |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 7284 | } |
| 7285 | |
| 7286 | /* Save the positions of any other cursors open on this table before |
| 7287 | ** making any modifications. Make the page containing the entry to be |
| 7288 | ** deleted writable. Then free any overflow pages associated with the |
drh | a4ec1d4 | 2009-07-11 13:13:11 +0000 | [diff] [blame] | 7289 | ** entry and finally remove the cell itself from within the page. |
| 7290 | */ |
| 7291 | rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur); |
| 7292 | if( rc ) return rc; |
drh | d60f4f4 | 2012-03-23 14:23:52 +0000 | [diff] [blame] | 7293 | |
| 7294 | /* If this is a delete operation to remove a row from a table b-tree, |
| 7295 | ** invalidate any incrblob cursors open on the row being deleted. */ |
| 7296 | if( pCur->pKeyInfo==0 ){ |
| 7297 | invalidateIncrblobCursors(p, pCur->info.nKey, 0); |
| 7298 | } |
| 7299 | |
drh | a4ec1d4 | 2009-07-11 13:13:11 +0000 | [diff] [blame] | 7300 | rc = sqlite3PagerWrite(pPage->pDbPage); |
| 7301 | if( rc ) return rc; |
drh | 9bfdc25 | 2014-09-24 02:05:41 +0000 | [diff] [blame] | 7302 | rc = clearCell(pPage, pCell, &szCell); |
| 7303 | dropCell(pPage, iCellIdx, szCell, &rc); |
drh | a4ec1d4 | 2009-07-11 13:13:11 +0000 | [diff] [blame] | 7304 | if( rc ) return rc; |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 7305 | |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 7306 | /* If the cell deleted was not located on a leaf page, then the cursor |
| 7307 | ** is currently pointing to the largest entry in the sub-tree headed |
| 7308 | ** by the child-page of the cell that was just deleted from an internal |
| 7309 | ** node. The cell from the leaf node needs to be moved to the internal |
| 7310 | ** node to replace the deleted cell. */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7311 | if( !pPage->leaf ){ |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 7312 | MemPage *pLeaf = pCur->apPage[pCur->iPage]; |
| 7313 | int nCell; |
| 7314 | Pgno n = pCur->apPage[iCellDepth+1]->pgno; |
| 7315 | unsigned char *pTmp; |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 7316 | |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 7317 | pCell = findCell(pLeaf, pLeaf->nCell-1); |
| 7318 | nCell = cellSizePtr(pLeaf, pCell); |
drh | fcd71b6 | 2011-04-05 22:08:24 +0000 | [diff] [blame] | 7319 | assert( MX_CELL_SIZE(pBt) >= nCell ); |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 7320 | |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 7321 | allocateTempSpace(pBt); |
| 7322 | pTmp = pBt->pTmpSpace; |
danielk1977 | 2f78fc6 | 2008-09-30 09:31:45 +0000 | [diff] [blame] | 7323 | |
drh | a4ec1d4 | 2009-07-11 13:13:11 +0000 | [diff] [blame] | 7324 | rc = sqlite3PagerWrite(pLeaf->pDbPage); |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 7325 | insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc); |
| 7326 | dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc); |
drh | a4ec1d4 | 2009-07-11 13:13:11 +0000 | [diff] [blame] | 7327 | if( rc ) return rc; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 7328 | } |
danielk1977 | 4dbaa89 | 2009-06-16 16:50:22 +0000 | [diff] [blame] | 7329 | |
| 7330 | /* Balance the tree. If the entry deleted was located on a leaf page, |
| 7331 | ** then the cursor still points to that page. In this case the first |
| 7332 | ** call to balance() repairs the tree, and the if(...) condition is |
| 7333 | ** never true. |
| 7334 | ** |
| 7335 | ** Otherwise, if the entry deleted was on an internal node page, then |
| 7336 | ** pCur is pointing to the leaf page from which a cell was removed to |
| 7337 | ** replace the cell deleted from the internal node. This is slightly |
| 7338 | ** tricky as the leaf node may be underfull, and the internal node may |
| 7339 | ** be either under or overfull. In this case run the balancing algorithm |
| 7340 | ** on the leaf node first. If the balance proceeds far enough up the |
| 7341 | ** tree that we can be sure that any problem in the internal node has |
| 7342 | ** been corrected, so be it. Otherwise, after balancing the leaf node, |
| 7343 | ** walk the cursor up the tree to the internal node and balance it as |
| 7344 | ** well. */ |
| 7345 | rc = balance(pCur); |
| 7346 | if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){ |
| 7347 | while( pCur->iPage>iCellDepth ){ |
| 7348 | releasePage(pCur->apPage[pCur->iPage--]); |
| 7349 | } |
| 7350 | rc = balance(pCur); |
| 7351 | } |
| 7352 | |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 7353 | if( rc==SQLITE_OK ){ |
| 7354 | moveToRoot(pCur); |
| 7355 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 7356 | return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 7357 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7358 | |
| 7359 | /* |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 7360 | ** Create a new BTree table. Write into *piTable the page |
| 7361 | ** number for the root page of the new table. |
| 7362 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 7363 | ** The type of type is determined by the flags parameter. Only the |
| 7364 | ** following values of flags are currently in use. Other values for |
| 7365 | ** flags might not work: |
| 7366 | ** |
| 7367 | ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys |
| 7368 | ** BTREE_ZERODATA Used for SQL indices |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7369 | */ |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 7370 | static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7371 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7372 | MemPage *pRoot; |
| 7373 | Pgno pgnoRoot; |
| 7374 | int rc; |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 7375 | int ptfFlags; /* Page-type flage for the root page of new table */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7376 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7377 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 7378 | assert( pBt->inTransaction==TRANS_WRITE ); |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 7379 | assert( (pBt->btsFlags & BTS_READ_ONLY)==0 ); |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 7380 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7381 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 7382 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7383 | if( rc ){ |
| 7384 | return rc; |
| 7385 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7386 | #else |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 7387 | if( pBt->autoVacuum ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7388 | Pgno pgnoMove; /* Move a page here to make room for the root-page */ |
| 7389 | MemPage *pPageMove; /* The page to move to. */ |
| 7390 | |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 7391 | /* Creating a new table may probably require moving an existing database |
| 7392 | ** to make room for the new tables root page. In case this page turns |
| 7393 | ** out to be an overflow page, delete all overflow page-map caches |
| 7394 | ** held by open cursors. |
| 7395 | */ |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 7396 | invalidateAllOverflowCache(pBt); |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 7397 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7398 | /* Read the value of meta[3] from the database to determine where the |
| 7399 | ** root page of the new table should go. meta[3] is the largest root-page |
| 7400 | ** created so far, so the new root-page is (meta[3]+1). |
| 7401 | */ |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 7402 | sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &pgnoRoot); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7403 | pgnoRoot++; |
| 7404 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 7405 | /* The new root-page may not be allocated on a pointer-map page, or the |
| 7406 | ** PENDING_BYTE page. |
| 7407 | */ |
drh | 7219043 | 2008-01-31 14:54:43 +0000 | [diff] [blame] | 7408 | while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) || |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 7409 | pgnoRoot==PENDING_BYTE_PAGE(pBt) ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7410 | pgnoRoot++; |
| 7411 | } |
| 7412 | assert( pgnoRoot>=3 ); |
| 7413 | |
| 7414 | /* Allocate a page. The page that currently resides at pgnoRoot will |
| 7415 | ** be moved to the allocated page (unless the allocated page happens |
| 7416 | ** to reside at pgnoRoot). |
| 7417 | */ |
dan | 51f0b6d | 2013-02-22 20:16:34 +0000 | [diff] [blame] | 7418 | rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, BTALLOC_EXACT); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7419 | if( rc!=SQLITE_OK ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 7420 | return rc; |
| 7421 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7422 | |
| 7423 | if( pgnoMove!=pgnoRoot ){ |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 7424 | /* pgnoRoot is the page that will be used for the root-page of |
| 7425 | ** the new table (assuming an error did not occur). But we were |
| 7426 | ** allocated pgnoMove. If required (i.e. if it was not allocated |
| 7427 | ** by extending the file), the current page at position pgnoMove |
| 7428 | ** is already journaled. |
| 7429 | */ |
drh | eeb844a | 2009-08-08 18:01:07 +0000 | [diff] [blame] | 7430 | u8 eType = 0; |
| 7431 | Pgno iPtrPage = 0; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7432 | |
dan | f7679ad | 2013-04-03 11:38:36 +0000 | [diff] [blame] | 7433 | /* Save the positions of any open cursors. This is required in |
| 7434 | ** case they are holding a reference to an xFetch reference |
| 7435 | ** corresponding to page pgnoRoot. */ |
| 7436 | rc = saveAllCursors(pBt, 0, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7437 | releasePage(pPageMove); |
dan | f7679ad | 2013-04-03 11:38:36 +0000 | [diff] [blame] | 7438 | if( rc!=SQLITE_OK ){ |
| 7439 | return rc; |
| 7440 | } |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 7441 | |
| 7442 | /* Move the page currently at pgnoRoot to pgnoMove. */ |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 7443 | rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7444 | if( rc!=SQLITE_OK ){ |
| 7445 | return rc; |
| 7446 | } |
| 7447 | rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage); |
drh | 27731d7 | 2009-06-22 12:05:10 +0000 | [diff] [blame] | 7448 | if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){ |
| 7449 | rc = SQLITE_CORRUPT_BKPT; |
| 7450 | } |
| 7451 | if( rc!=SQLITE_OK ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7452 | releasePage(pRoot); |
| 7453 | return rc; |
| 7454 | } |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 7455 | assert( eType!=PTRMAP_ROOTPAGE ); |
| 7456 | assert( eType!=PTRMAP_FREEPAGE ); |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 7457 | rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7458 | releasePage(pRoot); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 7459 | |
| 7460 | /* Obtain the page at pgnoRoot */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7461 | if( rc!=SQLITE_OK ){ |
| 7462 | return rc; |
| 7463 | } |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 7464 | rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7465 | if( rc!=SQLITE_OK ){ |
| 7466 | return rc; |
| 7467 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7468 | rc = sqlite3PagerWrite(pRoot->pDbPage); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7469 | if( rc!=SQLITE_OK ){ |
| 7470 | releasePage(pRoot); |
| 7471 | return rc; |
| 7472 | } |
| 7473 | }else{ |
| 7474 | pRoot = pPageMove; |
| 7475 | } |
| 7476 | |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 7477 | /* Update the pointer-map and meta-data with the new root-page number. */ |
drh | 98add2e | 2009-07-20 17:11:49 +0000 | [diff] [blame] | 7478 | ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0, &rc); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7479 | if( rc ){ |
| 7480 | releasePage(pRoot); |
| 7481 | return rc; |
| 7482 | } |
drh | bf59283 | 2010-03-30 15:51:12 +0000 | [diff] [blame] | 7483 | |
| 7484 | /* When the new root page was allocated, page 1 was made writable in |
| 7485 | ** order either to increase the database filesize, or to decrement the |
| 7486 | ** freelist count. Hence, the sqlite3BtreeUpdateMeta() call cannot fail. |
| 7487 | */ |
| 7488 | assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7489 | rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot); |
drh | bf59283 | 2010-03-30 15:51:12 +0000 | [diff] [blame] | 7490 | if( NEVER(rc) ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7491 | releasePage(pRoot); |
| 7492 | return rc; |
| 7493 | } |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 7494 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7495 | }else{ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 7496 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7497 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 7498 | } |
| 7499 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7500 | assert( sqlite3PagerIswriteable(pRoot->pDbPage) ); |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 7501 | if( createTabFlags & BTREE_INTKEY ){ |
| 7502 | ptfFlags = PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF; |
| 7503 | }else{ |
| 7504 | ptfFlags = PTF_ZERODATA | PTF_LEAF; |
| 7505 | } |
| 7506 | zeroPage(pRoot, ptfFlags); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7507 | sqlite3PagerUnref(pRoot->pDbPage); |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 7508 | assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7509 | *piTable = (int)pgnoRoot; |
| 7510 | return SQLITE_OK; |
| 7511 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7512 | int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){ |
| 7513 | int rc; |
| 7514 | sqlite3BtreeEnter(p); |
| 7515 | rc = btreeCreateTable(p, piTable, flags); |
| 7516 | sqlite3BtreeLeave(p); |
| 7517 | return rc; |
| 7518 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7519 | |
| 7520 | /* |
| 7521 | ** Erase the given database page and all its children. Return |
| 7522 | ** the page to the freelist. |
| 7523 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7524 | static int clearDatabasePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7525 | BtShared *pBt, /* The BTree that contains the table */ |
drh | 7ab641f | 2009-11-24 02:37:02 +0000 | [diff] [blame] | 7526 | Pgno pgno, /* Page number to clear */ |
| 7527 | int freePageFlag, /* Deallocate page if true */ |
| 7528 | int *pnChange /* Add number of Cells freed to this counter */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7529 | ){ |
danielk1977 | 146ba99 | 2009-07-22 14:08:13 +0000 | [diff] [blame] | 7530 | MemPage *pPage; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7531 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7532 | unsigned char *pCell; |
| 7533 | int i; |
dan | 8ce7184 | 2014-01-14 20:14:09 +0000 | [diff] [blame] | 7534 | int hdr; |
drh | 9bfdc25 | 2014-09-24 02:05:41 +0000 | [diff] [blame] | 7535 | u16 szCell; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7536 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7537 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | b129915 | 2010-03-30 22:58:33 +0000 | [diff] [blame] | 7538 | if( pgno>btreePagecount(pBt) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 7539 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 7540 | } |
| 7541 | |
dan | 11dcd11 | 2013-03-15 18:29:18 +0000 | [diff] [blame] | 7542 | rc = getAndInitPage(pBt, pgno, &pPage, 0); |
danielk1977 | 146ba99 | 2009-07-22 14:08:13 +0000 | [diff] [blame] | 7543 | if( rc ) return rc; |
dan | 8ce7184 | 2014-01-14 20:14:09 +0000 | [diff] [blame] | 7544 | hdr = pPage->hdrOffset; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7545 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 7546 | pCell = findCell(pPage, i); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7547 | if( !pPage->leaf ){ |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 7548 | rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 7549 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7550 | } |
drh | 9bfdc25 | 2014-09-24 02:05:41 +0000 | [diff] [blame] | 7551 | rc = clearCell(pPage, pCell, &szCell); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 7552 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7553 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 7554 | if( !pPage->leaf ){ |
dan | 8ce7184 | 2014-01-14 20:14:09 +0000 | [diff] [blame] | 7555 | rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 7556 | if( rc ) goto cleardatabasepage_out; |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 7557 | }else if( pnChange ){ |
| 7558 | assert( pPage->intKey ); |
| 7559 | *pnChange += pPage->nCell; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 7560 | } |
| 7561 | if( freePageFlag ){ |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 7562 | freePage(pPage, &rc); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 7563 | }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){ |
dan | 8ce7184 | 2014-01-14 20:14:09 +0000 | [diff] [blame] | 7564 | zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 7565 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 7566 | |
| 7567 | cleardatabasepage_out: |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7568 | releasePage(pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 7569 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7570 | } |
| 7571 | |
| 7572 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 7573 | ** Delete all information from a single table in the database. iTable is |
| 7574 | ** the page number of the root of the table. After this routine returns, |
| 7575 | ** the root page is empty, but still exists. |
| 7576 | ** |
| 7577 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 7578 | ** read cursors on the table. Open write cursors are moved to the |
| 7579 | ** root of the table. |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 7580 | ** |
| 7581 | ** If pnChange is not NULL, then table iTable must be an intkey table. The |
| 7582 | ** integer value pointed to by pnChange is incremented by the number of |
| 7583 | ** entries in the table. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7584 | */ |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 7585 | int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7586 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7587 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7588 | sqlite3BtreeEnter(p); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 7589 | assert( p->inTrans==TRANS_WRITE ); |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 7590 | |
drh | c046e3e | 2009-07-15 11:26:44 +0000 | [diff] [blame] | 7591 | rc = saveAllCursors(pBt, (Pgno)iTable, 0); |
drh | d60f4f4 | 2012-03-23 14:23:52 +0000 | [diff] [blame] | 7592 | |
drh | c046e3e | 2009-07-15 11:26:44 +0000 | [diff] [blame] | 7593 | if( SQLITE_OK==rc ){ |
drh | d60f4f4 | 2012-03-23 14:23:52 +0000 | [diff] [blame] | 7594 | /* Invalidate all incrblob cursors open on table iTable (assuming iTable |
| 7595 | ** is the root of a table b-tree - if it is not, the following call is |
| 7596 | ** a no-op). */ |
| 7597 | invalidateIncrblobCursors(p, 0, 1); |
danielk1977 | 62c14b3 | 2008-11-19 09:05:26 +0000 | [diff] [blame] | 7598 | rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7599 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7600 | sqlite3BtreeLeave(p); |
| 7601 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7602 | } |
| 7603 | |
| 7604 | /* |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 7605 | ** Delete all information from the single table that pCur is open on. |
| 7606 | ** |
| 7607 | ** This routine only work for pCur on an ephemeral table. |
| 7608 | */ |
| 7609 | int sqlite3BtreeClearTableOfCursor(BtCursor *pCur){ |
| 7610 | return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0); |
| 7611 | } |
| 7612 | |
| 7613 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7614 | ** Erase all information in a table and add the root of the table to |
| 7615 | ** the freelist. Except, the root of the principle table (the one on |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 7616 | ** page 1) is never added to the freelist. |
| 7617 | ** |
| 7618 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 7619 | ** cursors on the table. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 7620 | ** |
| 7621 | ** If AUTOVACUUM is enabled and the page at iTable is not the last |
| 7622 | ** root page in the database file, then the last root page |
| 7623 | ** in the database file is moved into the slot formerly occupied by |
| 7624 | ** iTable and that last slot formerly occupied by the last root page |
| 7625 | ** is added to the freelist instead of iTable. In this say, all |
| 7626 | ** root pages are kept at the beginning of the database file, which |
| 7627 | ** is necessary for AUTOVACUUM to work right. *piMoved is set to the |
| 7628 | ** page number that used to be the last root page in the file before |
| 7629 | ** the move. If no page gets moved, *piMoved is set to 0. |
| 7630 | ** The last root page is recorded in meta[3] and the value of |
| 7631 | ** meta[3] is updated by this procedure. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7632 | */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 7633 | static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7634 | int rc; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 7635 | MemPage *pPage = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7636 | BtShared *pBt = p->pBt; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 7637 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7638 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 7639 | assert( p->inTrans==TRANS_WRITE ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 7640 | |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 7641 | /* It is illegal to drop a table if any cursors are open on the |
| 7642 | ** database. This is because in auto-vacuum mode the backend may |
| 7643 | ** need to move another root-page to fill a gap left by the deleted |
| 7644 | ** root page. If an open cursor was using this page a problem would |
| 7645 | ** occur. |
drh | c046e3e | 2009-07-15 11:26:44 +0000 | [diff] [blame] | 7646 | ** |
| 7647 | ** This error is caught long before control reaches this point. |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 7648 | */ |
drh | c046e3e | 2009-07-15 11:26:44 +0000 | [diff] [blame] | 7649 | if( NEVER(pBt->pCursor) ){ |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 7650 | sqlite3ConnectionBlocked(p->db, pBt->pCursor->pBtree->db); |
| 7651 | return SQLITE_LOCKED_SHAREDCACHE; |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 7652 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 7653 | |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 7654 | rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 7655 | if( rc ) return rc; |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 7656 | rc = sqlite3BtreeClearTable(p, iTable, 0); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 7657 | if( rc ){ |
| 7658 | releasePage(pPage); |
| 7659 | return rc; |
| 7660 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 7661 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 7662 | *piMoved = 0; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 7663 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7664 | if( iTable>1 ){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 7665 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 7666 | freePage(pPage, &rc); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 7667 | releasePage(pPage); |
| 7668 | #else |
| 7669 | if( pBt->autoVacuum ){ |
| 7670 | Pgno maxRootPgno; |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 7671 | sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 7672 | |
| 7673 | if( iTable==maxRootPgno ){ |
| 7674 | /* If the table being dropped is the table with the largest root-page |
| 7675 | ** number in the database, put the root page on the free list. |
| 7676 | */ |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 7677 | freePage(pPage, &rc); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 7678 | releasePage(pPage); |
| 7679 | if( rc!=SQLITE_OK ){ |
| 7680 | return rc; |
| 7681 | } |
| 7682 | }else{ |
| 7683 | /* The table being dropped does not have the largest root-page |
| 7684 | ** number in the database. So move the page that does into the |
| 7685 | ** gap left by the deleted root-page. |
| 7686 | */ |
| 7687 | MemPage *pMove; |
| 7688 | releasePage(pPage); |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 7689 | rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 7690 | if( rc!=SQLITE_OK ){ |
| 7691 | return rc; |
| 7692 | } |
danielk1977 | 4c99999 | 2008-07-16 18:17:55 +0000 | [diff] [blame] | 7693 | rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 7694 | releasePage(pMove); |
| 7695 | if( rc!=SQLITE_OK ){ |
| 7696 | return rc; |
| 7697 | } |
drh | fe3313f | 2009-07-21 19:02:20 +0000 | [diff] [blame] | 7698 | pMove = 0; |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 7699 | rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0); |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 7700 | freePage(pMove, &rc); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 7701 | releasePage(pMove); |
| 7702 | if( rc!=SQLITE_OK ){ |
| 7703 | return rc; |
| 7704 | } |
| 7705 | *piMoved = maxRootPgno; |
| 7706 | } |
| 7707 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 7708 | /* Set the new 'max-root-page' value in the database header. This |
| 7709 | ** is the old value less one, less one more if that happens to |
| 7710 | ** be a root-page number, less one again if that is the |
| 7711 | ** PENDING_BYTE_PAGE. |
| 7712 | */ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 7713 | maxRootPgno--; |
drh | e184965 | 2009-07-15 18:15:22 +0000 | [diff] [blame] | 7714 | while( maxRootPgno==PENDING_BYTE_PAGE(pBt) |
| 7715 | || PTRMAP_ISPAGE(pBt, maxRootPgno) ){ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 7716 | maxRootPgno--; |
| 7717 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 7718 | assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) ); |
| 7719 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7720 | rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 7721 | }else{ |
drh | c314dc7 | 2009-07-21 11:52:34 +0000 | [diff] [blame] | 7722 | freePage(pPage, &rc); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 7723 | releasePage(pPage); |
| 7724 | } |
| 7725 | #endif |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 7726 | }else{ |
drh | c046e3e | 2009-07-15 11:26:44 +0000 | [diff] [blame] | 7727 | /* If sqlite3BtreeDropTable was called on page 1. |
| 7728 | ** This really never should happen except in a corrupt |
| 7729 | ** database. |
| 7730 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 7731 | zeroPage(pPage, PTF_INTKEY|PTF_LEAF ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 7732 | releasePage(pPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7733 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7734 | return rc; |
| 7735 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7736 | int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){ |
| 7737 | int rc; |
| 7738 | sqlite3BtreeEnter(p); |
dan | 7733a4d | 2011-09-02 18:03:16 +0000 | [diff] [blame] | 7739 | rc = btreeDropTable(p, iTable, piMoved); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7740 | sqlite3BtreeLeave(p); |
| 7741 | return rc; |
| 7742 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7743 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 7744 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7745 | /* |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 7746 | ** This function may only be called if the b-tree connection already |
| 7747 | ** has a read or write transaction open on the database. |
| 7748 | ** |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 7749 | ** Read the meta-information out of a database file. Meta[0] |
| 7750 | ** is the number of free pages currently in the database. Meta[1] |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 7751 | ** through meta[15] are available for use by higher layers. Meta[0] |
| 7752 | ** is read-only, the others are read/write. |
| 7753 | ** |
| 7754 | ** The schema layer numbers meta values differently. At the schema |
| 7755 | ** layer (and the SetCookie and ReadCookie opcodes) the number of |
| 7756 | ** 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] | 7757 | */ |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 7758 | void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7759 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7760 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7761 | sqlite3BtreeEnter(p); |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 7762 | assert( p->inTrans>TRANS_NONE ); |
danielk1977 | e0d9e6f | 2009-07-03 16:25:06 +0000 | [diff] [blame] | 7763 | assert( SQLITE_OK==querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK) ); |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 7764 | assert( pBt->pPage1 ); |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 7765 | assert( idx>=0 && idx<=15 ); |
danielk1977 | ea89730 | 2008-09-19 15:10:58 +0000 | [diff] [blame] | 7766 | |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 7767 | *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]); |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 7768 | |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 7769 | /* If auto-vacuum is disabled in this build and this is an auto-vacuum |
| 7770 | ** database, mark the database as read-only. */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7771 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 7772 | if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ){ |
| 7773 | pBt->btsFlags |= BTS_READ_ONLY; |
| 7774 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 7775 | #endif |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 7776 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7777 | sqlite3BtreeLeave(p); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7778 | } |
| 7779 | |
| 7780 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 7781 | ** Write meta-information back into the database. Meta[0] is |
| 7782 | ** read-only and may not be written. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7783 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7784 | int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){ |
| 7785 | BtShared *pBt = p->pBt; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 7786 | unsigned char *pP1; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 7787 | int rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 7788 | assert( idx>=1 && idx<=15 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7789 | sqlite3BtreeEnter(p); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 7790 | assert( p->inTrans==TRANS_WRITE ); |
| 7791 | assert( pBt->pPage1!=0 ); |
| 7792 | pP1 = pBt->pPage1->aData; |
| 7793 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 7794 | if( rc==SQLITE_OK ){ |
| 7795 | put4byte(&pP1[36 + idx*4], iMeta); |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 +0000 | [diff] [blame] | 7796 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 7797 | if( idx==BTREE_INCR_VACUUM ){ |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 7798 | assert( pBt->autoVacuum || iMeta==0 ); |
| 7799 | assert( iMeta==0 || iMeta==1 ); |
| 7800 | pBt->incrVacuum = (u8)iMeta; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7801 | } |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 7802 | #endif |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 7803 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7804 | sqlite3BtreeLeave(p); |
| 7805 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 7806 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 7807 | |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 7808 | #ifndef SQLITE_OMIT_BTREECOUNT |
| 7809 | /* |
| 7810 | ** The first argument, pCur, is a cursor opened on some b-tree. Count the |
| 7811 | ** number of entries in the b-tree and write the result to *pnEntry. |
| 7812 | ** |
| 7813 | ** SQLITE_OK is returned if the operation is successfully executed. |
| 7814 | ** Otherwise, if an error is encountered (i.e. an IO error or database |
| 7815 | ** corruption) an SQLite error code is returned. |
| 7816 | */ |
| 7817 | int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){ |
| 7818 | i64 nEntry = 0; /* Value to return in *pnEntry */ |
| 7819 | int rc; /* Return code */ |
dan | a205a48 | 2011-08-27 18:48:57 +0000 | [diff] [blame] | 7820 | |
| 7821 | if( pCur->pgnoRoot==0 ){ |
| 7822 | *pnEntry = 0; |
| 7823 | return SQLITE_OK; |
| 7824 | } |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 7825 | rc = moveToRoot(pCur); |
| 7826 | |
| 7827 | /* Unless an error occurs, the following loop runs one iteration for each |
| 7828 | ** page in the B-Tree structure (not including overflow pages). |
| 7829 | */ |
| 7830 | while( rc==SQLITE_OK ){ |
| 7831 | int iIdx; /* Index of child node in parent */ |
| 7832 | MemPage *pPage; /* Current page of the b-tree */ |
| 7833 | |
| 7834 | /* If this is a leaf page or the tree is not an int-key tree, then |
| 7835 | ** this page contains countable entries. Increment the entry counter |
| 7836 | ** accordingly. |
| 7837 | */ |
| 7838 | pPage = pCur->apPage[pCur->iPage]; |
| 7839 | if( pPage->leaf || !pPage->intKey ){ |
| 7840 | nEntry += pPage->nCell; |
| 7841 | } |
| 7842 | |
| 7843 | /* pPage is a leaf node. This loop navigates the cursor so that it |
| 7844 | ** points to the first interior cell that it points to the parent of |
| 7845 | ** the next page in the tree that has not yet been visited. The |
| 7846 | ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell |
| 7847 | ** of the page, or to the number of cells in the page if the next page |
| 7848 | ** to visit is the right-child of its parent. |
| 7849 | ** |
| 7850 | ** If all pages in the tree have been visited, return SQLITE_OK to the |
| 7851 | ** caller. |
| 7852 | */ |
| 7853 | if( pPage->leaf ){ |
| 7854 | do { |
| 7855 | if( pCur->iPage==0 ){ |
| 7856 | /* All pages of the b-tree have been visited. Return successfully. */ |
| 7857 | *pnEntry = nEntry; |
| 7858 | return SQLITE_OK; |
| 7859 | } |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 7860 | moveToParent(pCur); |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 7861 | }while ( pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell ); |
| 7862 | |
| 7863 | pCur->aiIdx[pCur->iPage]++; |
| 7864 | pPage = pCur->apPage[pCur->iPage]; |
| 7865 | } |
| 7866 | |
| 7867 | /* Descend to the child node of the cell that the cursor currently |
| 7868 | ** points at. This is the right-child if (iIdx==pPage->nCell). |
| 7869 | */ |
| 7870 | iIdx = pCur->aiIdx[pCur->iPage]; |
| 7871 | if( iIdx==pPage->nCell ){ |
| 7872 | rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); |
| 7873 | }else{ |
| 7874 | rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx))); |
| 7875 | } |
| 7876 | } |
| 7877 | |
shane | be21779 | 2009-03-05 04:20:31 +0000 | [diff] [blame] | 7878 | /* An error has occurred. Return an error code. */ |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 7879 | return rc; |
| 7880 | } |
| 7881 | #endif |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 7882 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 7883 | /* |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7884 | ** Return the pager associated with a BTree. This routine is used for |
| 7885 | ** testing and debugging only. |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 7886 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7887 | Pager *sqlite3BtreePager(Btree *p){ |
| 7888 | return p->pBt->pPager; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 7889 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7890 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 7891 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7892 | /* |
| 7893 | ** Append a message to the error message string. |
| 7894 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7895 | static void checkAppendMsg( |
| 7896 | IntegrityCk *pCheck, |
| 7897 | char *zMsg1, |
| 7898 | const char *zFormat, |
| 7899 | ... |
| 7900 | ){ |
| 7901 | va_list ap; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 7902 | if( !pCheck->mxErr ) return; |
| 7903 | pCheck->mxErr--; |
| 7904 | pCheck->nErr++; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7905 | va_start(ap, zFormat); |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 7906 | if( pCheck->errMsg.nChar ){ |
| 7907 | sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7908 | } |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 7909 | if( zMsg1 ){ |
drh | a6353a3 | 2013-12-09 19:03:26 +0000 | [diff] [blame] | 7910 | sqlite3StrAccumAppendAll(&pCheck->errMsg, zMsg1); |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 7911 | } |
| 7912 | sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap); |
| 7913 | va_end(ap); |
drh | b49bc86 | 2013-08-21 21:12:10 +0000 | [diff] [blame] | 7914 | if( pCheck->errMsg.accError==STRACCUM_NOMEM ){ |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 7915 | pCheck->mallocFailed = 1; |
| 7916 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7917 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 7918 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7919 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 7920 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
dan | 1235bb1 | 2012-04-03 17:43:28 +0000 | [diff] [blame] | 7921 | |
| 7922 | /* |
| 7923 | ** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that |
| 7924 | ** corresponds to page iPg is already set. |
| 7925 | */ |
| 7926 | static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){ |
| 7927 | assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 ); |
| 7928 | return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07))); |
| 7929 | } |
| 7930 | |
| 7931 | /* |
| 7932 | ** Set the bit in the IntegrityCk.aPgRef[] array that corresponds to page iPg. |
| 7933 | */ |
| 7934 | static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){ |
| 7935 | assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 ); |
| 7936 | pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07)); |
| 7937 | } |
| 7938 | |
| 7939 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7940 | /* |
| 7941 | ** Add 1 to the reference count for page iPage. If this is the second |
| 7942 | ** reference to the page, add an error message to pCheck->zErrMsg. |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 7943 | ** 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] | 7944 | ** if this is the first reference to the page. |
| 7945 | ** |
| 7946 | ** Also check that the page number is in bounds. |
| 7947 | */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 7948 | static int checkRef(IntegrityCk *pCheck, Pgno iPage, char *zContext){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7949 | if( iPage==0 ) return 1; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 7950 | if( iPage>pCheck->nPage ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7951 | checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7952 | return 1; |
| 7953 | } |
dan | 1235bb1 | 2012-04-03 17:43:28 +0000 | [diff] [blame] | 7954 | if( getPageReferenced(pCheck, iPage) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 7955 | checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7956 | return 1; |
| 7957 | } |
dan | 1235bb1 | 2012-04-03 17:43:28 +0000 | [diff] [blame] | 7958 | setPageReferenced(pCheck, iPage); |
| 7959 | return 0; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7960 | } |
| 7961 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7962 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 7963 | /* |
| 7964 | ** Check that the entry in the pointer-map for page iChild maps to |
| 7965 | ** page iParent, pointer type ptrType. If not, append an error message |
| 7966 | ** to pCheck. |
| 7967 | */ |
| 7968 | static void checkPtrmap( |
| 7969 | IntegrityCk *pCheck, /* Integrity check context */ |
| 7970 | Pgno iChild, /* Child page number */ |
| 7971 | u8 eType, /* Expected pointer map type */ |
| 7972 | Pgno iParent, /* Expected pointer map parent page number */ |
| 7973 | char *zContext /* Context description (used for error msg) */ |
| 7974 | ){ |
| 7975 | int rc; |
| 7976 | u8 ePtrmapType; |
| 7977 | Pgno iPtrmapParent; |
| 7978 | |
| 7979 | rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent); |
| 7980 | if( rc!=SQLITE_OK ){ |
drh | b56cd55 | 2009-05-01 13:16:54 +0000 | [diff] [blame] | 7981 | if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->mallocFailed = 1; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 7982 | checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild); |
| 7983 | return; |
| 7984 | } |
| 7985 | |
| 7986 | if( ePtrmapType!=eType || iPtrmapParent!=iParent ){ |
| 7987 | checkAppendMsg(pCheck, zContext, |
| 7988 | "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", |
| 7989 | iChild, eType, iParent, ePtrmapType, iPtrmapParent); |
| 7990 | } |
| 7991 | } |
| 7992 | #endif |
| 7993 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 7994 | /* |
| 7995 | ** Check the integrity of the freelist or of an overflow page list. |
| 7996 | ** Verify that the number of pages on the list is N. |
| 7997 | */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 7998 | static void checkList( |
| 7999 | IntegrityCk *pCheck, /* Integrity checking context */ |
| 8000 | int isFreeList, /* True for a freelist. False for overflow page list */ |
| 8001 | int iPage, /* Page number for first page in the list */ |
| 8002 | int N, /* Expected number of pages in the list */ |
| 8003 | char *zContext /* Context for error messages */ |
| 8004 | ){ |
| 8005 | int i; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 8006 | int expected = N; |
| 8007 | int iFirst = iPage; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 8008 | while( N-- > 0 && pCheck->mxErr ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 8009 | DbPage *pOvflPage; |
| 8010 | unsigned char *pOvflData; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8011 | if( iPage<1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 8012 | checkAppendMsg(pCheck, zContext, |
| 8013 | "%d of %d pages missing from overflow list starting at %d", |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 8014 | N+1, expected, iFirst); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8015 | break; |
| 8016 | } |
| 8017 | if( checkRef(pCheck, iPage, zContext) ) break; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 8018 | if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 8019 | checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8020 | break; |
| 8021 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 8022 | pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 8023 | if( isFreeList ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 8024 | int n = get4byte(&pOvflData[4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 8025 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 8026 | if( pCheck->pBt->autoVacuum ){ |
| 8027 | checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext); |
| 8028 | } |
| 8029 | #endif |
drh | 43b18e1 | 2010-08-17 19:40:08 +0000 | [diff] [blame] | 8030 | if( n>(int)pCheck->pBt->usableSize/4-2 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 8031 | checkAppendMsg(pCheck, zContext, |
| 8032 | "freelist leaf count too big on page %d", iPage); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 8033 | N--; |
| 8034 | }else{ |
| 8035 | for(i=0; i<n; i++){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 8036 | Pgno iFreePage = get4byte(&pOvflData[8+i*4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 8037 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 8038 | if( pCheck->pBt->autoVacuum ){ |
| 8039 | checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext); |
| 8040 | } |
| 8041 | #endif |
| 8042 | checkRef(pCheck, iFreePage, zContext); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 8043 | } |
| 8044 | N -= n; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 8045 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 8046 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 8047 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 8048 | else{ |
| 8049 | /* If this database supports auto-vacuum and iPage is not the last |
| 8050 | ** page in this overflow list, check that the pointer-map entry for |
| 8051 | ** the following page matches iPage. |
| 8052 | */ |
| 8053 | if( pCheck->pBt->autoVacuum && N>0 ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 8054 | i = get4byte(pOvflData); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 8055 | checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext); |
| 8056 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 8057 | } |
| 8058 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 8059 | iPage = get4byte(pOvflData); |
| 8060 | sqlite3PagerUnref(pOvflPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8061 | } |
| 8062 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 8063 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8064 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 8065 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8066 | /* |
| 8067 | ** Do various sanity checks on a single page of a tree. Return |
| 8068 | ** the tree depth. Root pages return 0. Parents of root pages |
| 8069 | ** return 1, and so forth. |
| 8070 | ** |
| 8071 | ** These checks are done: |
| 8072 | ** |
| 8073 | ** 1. Make sure that cells and freeblocks do not overlap |
| 8074 | ** but combine to completely cover the page. |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 8075 | ** NO 2. Make sure cell keys are in order. |
| 8076 | ** NO 3. Make sure no key is less than or equal to zLowerBound. |
| 8077 | ** NO 4. Make sure no key is greater than or equal to zUpperBound. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8078 | ** 5. Check the integrity of overflow pages. |
| 8079 | ** 6. Recursively call checkTreePage on all children. |
| 8080 | ** 7. Verify that the depth of all children is the same. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 8081 | ** 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] | 8082 | ** the root of the tree. |
| 8083 | */ |
| 8084 | static int checkTreePage( |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 8085 | IntegrityCk *pCheck, /* Context for the sanity check */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8086 | int iPage, /* Page number of the page to check */ |
shaneh | 195475d | 2010-02-19 04:28:08 +0000 | [diff] [blame] | 8087 | char *zParentContext, /* Parent context */ |
| 8088 | i64 *pnParentMinKey, |
| 8089 | i64 *pnParentMaxKey |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8090 | ){ |
| 8091 | MemPage *pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 8092 | int i, rc, depth, d2, pgno, cnt; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 8093 | int hdr, cellStart; |
| 8094 | int nCell; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 8095 | u8 *data; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 8096 | BtShared *pBt; |
drh | 4f26bb6 | 2005-09-08 14:17:20 +0000 | [diff] [blame] | 8097 | int usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8098 | char zContext[100]; |
shane | 0af3f89 | 2008-11-12 04:55:34 +0000 | [diff] [blame] | 8099 | char *hit = 0; |
shaneh | 195475d | 2010-02-19 04:28:08 +0000 | [diff] [blame] | 8100 | i64 nMinKey = 0; |
| 8101 | i64 nMaxKey = 0; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8102 | |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 8103 | sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage); |
danielk1977 | ef73ee9 | 2004-11-06 12:26:07 +0000 | [diff] [blame] | 8104 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8105 | /* Check that the page exists |
| 8106 | */ |
drh | d9cb6ac | 2005-10-20 07:28:17 +0000 | [diff] [blame] | 8107 | pBt = pCheck->pBt; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 8108 | usableSize = pBt->usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8109 | if( iPage==0 ) return 0; |
| 8110 | if( checkRef(pCheck, iPage, zParentContext) ) return 0; |
drh | b00fc3b | 2013-08-21 23:42:32 +0000 | [diff] [blame] | 8111 | if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 8112 | checkAppendMsg(pCheck, zContext, |
| 8113 | "unable to get the page. error code=%d", rc); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8114 | return 0; |
| 8115 | } |
danielk1977 | 93caf5a | 2009-07-11 06:55:33 +0000 | [diff] [blame] | 8116 | |
| 8117 | /* Clear MemPage.isInit to make sure the corruption detection code in |
| 8118 | ** btreeInitPage() is executed. */ |
| 8119 | pPage->isInit = 0; |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 8120 | if( (rc = btreeInitPage(pPage))!=0 ){ |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 8121 | assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 8122 | checkAppendMsg(pCheck, zContext, |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 8123 | "btreeInitPage() returns error code %d", rc); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 8124 | releasePage(pPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8125 | return 0; |
| 8126 | } |
| 8127 | |
| 8128 | /* Check out all the cells. |
| 8129 | */ |
| 8130 | depth = 0; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 8131 | for(i=0; i<pPage->nCell && pCheck->mxErr; i++){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 8132 | u8 *pCell; |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 8133 | u32 sz; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 8134 | CellInfo info; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8135 | |
| 8136 | /* Check payload overflow pages |
| 8137 | */ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 8138 | sqlite3_snprintf(sizeof(zContext), zContext, |
| 8139 | "On tree page %d cell %d: ", iPage, i); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 8140 | pCell = findCell(pPage,i); |
danielk1977 | 3054866 | 2009-07-09 05:07:37 +0000 | [diff] [blame] | 8141 | btreeParseCellPtr(pPage, pCell, &info); |
drh | ab1cc58 | 2014-09-23 21:25:19 +0000 | [diff] [blame] | 8142 | sz = info.nPayload; |
shaneh | 195475d | 2010-02-19 04:28:08 +0000 | [diff] [blame] | 8143 | /* For intKey pages, check that the keys are in order. |
| 8144 | */ |
drh | ab1cc58 | 2014-09-23 21:25:19 +0000 | [diff] [blame] | 8145 | if( pPage->intKey ){ |
| 8146 | if( i==0 ){ |
| 8147 | nMinKey = nMaxKey = info.nKey; |
| 8148 | }else if( info.nKey <= nMaxKey ){ |
shaneh | 195475d | 2010-02-19 04:28:08 +0000 | [diff] [blame] | 8149 | checkAppendMsg(pCheck, zContext, |
drh | ab1cc58 | 2014-09-23 21:25:19 +0000 | [diff] [blame] | 8150 | "Rowid %lld out of order (previous was %lld)", info.nKey, nMaxKey); |
shaneh | 195475d | 2010-02-19 04:28:08 +0000 | [diff] [blame] | 8151 | } |
| 8152 | nMaxKey = info.nKey; |
| 8153 | } |
danielk1977 | 5be31f5 | 2009-03-30 13:53:43 +0000 | [diff] [blame] | 8154 | if( (sz>info.nLocal) |
| 8155 | && (&pCell[info.iOverflow]<=&pPage->aData[pBt->usableSize]) |
| 8156 | ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 8157 | int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 8158 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
| 8159 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 8160 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 8161 | checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 8162 | } |
| 8163 | #endif |
| 8164 | checkList(pCheck, 0, pgnoOvfl, nPage, zContext); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8165 | } |
| 8166 | |
| 8167 | /* Check sanity of left child page. |
| 8168 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 8169 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 8170 | pgno = get4byte(pCell); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 8171 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 8172 | if( pBt->autoVacuum ){ |
| 8173 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext); |
| 8174 | } |
| 8175 | #endif |
drh | 3f38740 | 2014-09-24 01:23:00 +0000 | [diff] [blame] | 8176 | d2 = checkTreePage(pCheck, pgno, zContext, &nMinKey, i==0?NULL:&nMaxKey); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 8177 | if( i>0 && d2!=depth ){ |
| 8178 | checkAppendMsg(pCheck, zContext, "Child page depth differs"); |
| 8179 | } |
| 8180 | depth = d2; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8181 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8182 | } |
shaneh | 195475d | 2010-02-19 04:28:08 +0000 | [diff] [blame] | 8183 | |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 8184 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 8185 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 8186 | sqlite3_snprintf(sizeof(zContext), zContext, |
| 8187 | "On page %d at right child: ", iPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 8188 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 8189 | if( pBt->autoVacuum ){ |
shaneh | 195475d | 2010-02-19 04:28:08 +0000 | [diff] [blame] | 8190 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 8191 | } |
| 8192 | #endif |
drh | 3f38740 | 2014-09-24 01:23:00 +0000 | [diff] [blame] | 8193 | checkTreePage(pCheck, pgno, zContext, NULL, !pPage->nCell?NULL:&nMaxKey); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 8194 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8195 | |
shaneh | 195475d | 2010-02-19 04:28:08 +0000 | [diff] [blame] | 8196 | /* For intKey leaf pages, check that the min/max keys are in order |
| 8197 | ** with any left/parent/right pages. |
| 8198 | */ |
| 8199 | if( pPage->leaf && pPage->intKey ){ |
| 8200 | /* if we are a left child page */ |
| 8201 | if( pnParentMinKey ){ |
| 8202 | /* if we are the left most child page */ |
| 8203 | if( !pnParentMaxKey ){ |
| 8204 | if( nMaxKey > *pnParentMinKey ){ |
| 8205 | checkAppendMsg(pCheck, zContext, |
| 8206 | "Rowid %lld out of order (max larger than parent min of %lld)", |
| 8207 | nMaxKey, *pnParentMinKey); |
| 8208 | } |
| 8209 | }else{ |
| 8210 | if( nMinKey <= *pnParentMinKey ){ |
| 8211 | checkAppendMsg(pCheck, zContext, |
| 8212 | "Rowid %lld out of order (min less than parent min of %lld)", |
| 8213 | nMinKey, *pnParentMinKey); |
| 8214 | } |
| 8215 | if( nMaxKey > *pnParentMaxKey ){ |
| 8216 | checkAppendMsg(pCheck, zContext, |
| 8217 | "Rowid %lld out of order (max larger than parent max of %lld)", |
| 8218 | nMaxKey, *pnParentMaxKey); |
| 8219 | } |
| 8220 | *pnParentMinKey = nMaxKey; |
| 8221 | } |
| 8222 | /* else if we're a right child page */ |
| 8223 | } else if( pnParentMaxKey ){ |
| 8224 | if( nMinKey <= *pnParentMaxKey ){ |
| 8225 | checkAppendMsg(pCheck, zContext, |
| 8226 | "Rowid %lld out of order (min less than parent max of %lld)", |
| 8227 | nMinKey, *pnParentMaxKey); |
| 8228 | } |
| 8229 | } |
| 8230 | } |
| 8231 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8232 | /* Check for complete coverage of the page |
| 8233 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 8234 | data = pPage->aData; |
| 8235 | hdr = pPage->hdrOffset; |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 8236 | hit = sqlite3PageMalloc( pBt->pageSize ); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 8237 | if( hit==0 ){ |
| 8238 | pCheck->mallocFailed = 1; |
| 8239 | }else{ |
drh | 5d433ce | 2010-08-14 16:02:52 +0000 | [diff] [blame] | 8240 | int contentOffset = get2byteNotZero(&data[hdr+5]); |
drh | d7c7ecd | 2009-07-14 17:48:06 +0000 | [diff] [blame] | 8241 | assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */ |
shane | 5780ebd | 2008-11-11 17:36:30 +0000 | [diff] [blame] | 8242 | memset(hit+contentOffset, 0, usableSize-contentOffset); |
| 8243 | memset(hit, 1, contentOffset); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 8244 | nCell = get2byte(&data[hdr+3]); |
| 8245 | cellStart = hdr + 12 - 4*pPage->leaf; |
| 8246 | for(i=0; i<nCell; i++){ |
| 8247 | int pc = get2byte(&data[cellStart+i*2]); |
drh | 9b78f79 | 2010-08-14 21:21:24 +0000 | [diff] [blame] | 8248 | u32 size = 65536; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 8249 | int j; |
drh | 8c2bbb6 | 2009-07-10 02:52:20 +0000 | [diff] [blame] | 8250 | if( pc<=usableSize-4 ){ |
danielk1977 | daca543 | 2008-08-25 11:57:16 +0000 | [diff] [blame] | 8251 | size = cellSizePtr(pPage, &data[pc]); |
| 8252 | } |
drh | 43b18e1 | 2010-08-17 19:40:08 +0000 | [diff] [blame] | 8253 | if( (int)(pc+size-1)>=usableSize ){ |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 8254 | checkAppendMsg(pCheck, 0, |
shaneh | 195475d | 2010-02-19 04:28:08 +0000 | [diff] [blame] | 8255 | "Corruption detected in cell %d on page %d",i,iPage); |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 8256 | }else{ |
| 8257 | for(j=pc+size-1; j>=pc; j--) hit[j]++; |
| 8258 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 8259 | } |
drh | 8c2bbb6 | 2009-07-10 02:52:20 +0000 | [diff] [blame] | 8260 | i = get2byte(&data[hdr+1]); |
| 8261 | while( i>0 ){ |
| 8262 | int size, j; |
| 8263 | assert( i<=usableSize-4 ); /* Enforced by btreeInitPage() */ |
| 8264 | size = get2byte(&data[i+2]); |
| 8265 | assert( i+size<=usableSize ); /* Enforced by btreeInitPage() */ |
| 8266 | for(j=i+size-1; j>=i; j--) hit[j]++; |
| 8267 | j = get2byte(&data[i]); |
| 8268 | assert( j==0 || j>i+size ); /* Enforced by btreeInitPage() */ |
| 8269 | assert( j<=usableSize-4 ); /* Enforced by btreeInitPage() */ |
| 8270 | i = j; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 8271 | } |
| 8272 | for(i=cnt=0; i<usableSize; i++){ |
| 8273 | if( hit[i]==0 ){ |
| 8274 | cnt++; |
| 8275 | }else if( hit[i]>1 ){ |
| 8276 | checkAppendMsg(pCheck, 0, |
| 8277 | "Multiple uses for byte %d of page %d", i, iPage); |
| 8278 | break; |
| 8279 | } |
| 8280 | } |
| 8281 | if( cnt!=data[hdr+7] ){ |
| 8282 | checkAppendMsg(pCheck, 0, |
drh | 8c2bbb6 | 2009-07-10 02:52:20 +0000 | [diff] [blame] | 8283 | "Fragmentation of %d bytes reported as %d on page %d", |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 8284 | cnt, data[hdr+7], iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8285 | } |
| 8286 | } |
drh | 8c2bbb6 | 2009-07-10 02:52:20 +0000 | [diff] [blame] | 8287 | sqlite3PageFree(hit); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 8288 | releasePage(pPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 8289 | return depth+1; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8290 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 8291 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8292 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 8293 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8294 | /* |
| 8295 | ** This routine does a complete check of the given BTree file. aRoot[] is |
| 8296 | ** an array of pages numbers were each page number is the root page of |
| 8297 | ** a table. nRoot is the number of entries in aRoot. |
| 8298 | ** |
danielk1977 | 3509a65 | 2009-07-06 18:56:13 +0000 | [diff] [blame] | 8299 | ** A read-only or read-write transaction must be opened before calling |
| 8300 | ** this function. |
| 8301 | ** |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 8302 | ** Write the number of error seen in *pnErr. Except for some memory |
drh | e43ba70 | 2008-12-05 22:40:08 +0000 | [diff] [blame] | 8303 | ** allocation errors, an error message held in memory obtained from |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 8304 | ** 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] | 8305 | ** returned. If a memory allocation error occurs, NULL is returned. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8306 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 8307 | char *sqlite3BtreeIntegrityCheck( |
| 8308 | Btree *p, /* The btree to be checked */ |
| 8309 | int *aRoot, /* An array of root pages numbers for individual trees */ |
| 8310 | int nRoot, /* Number of entries in aRoot[] */ |
| 8311 | int mxErr, /* Stop reporting errors after this many */ |
| 8312 | int *pnErr /* Write number of errors seen to this variable */ |
| 8313 | ){ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 8314 | Pgno i; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8315 | int nRef; |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 8316 | IntegrityCk sCheck; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 8317 | BtShared *pBt = p->pBt; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 8318 | char zErr[100]; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8319 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 8320 | sqlite3BtreeEnter(p); |
danielk1977 | 3509a65 | 2009-07-06 18:56:13 +0000 | [diff] [blame] | 8321 | assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 8322 | nRef = sqlite3PagerRefcount(pBt->pPager); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8323 | sCheck.pBt = pBt; |
| 8324 | sCheck.pPager = pBt->pPager; |
drh | b129915 | 2010-03-30 22:58:33 +0000 | [diff] [blame] | 8325 | sCheck.nPage = btreePagecount(sCheck.pBt); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 8326 | sCheck.mxErr = mxErr; |
| 8327 | sCheck.nErr = 0; |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 8328 | sCheck.mallocFailed = 0; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 8329 | *pnErr = 0; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 8330 | if( sCheck.nPage==0 ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 8331 | sqlite3BtreeLeave(p); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 8332 | return 0; |
| 8333 | } |
dan | 1235bb1 | 2012-04-03 17:43:28 +0000 | [diff] [blame] | 8334 | |
| 8335 | sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1); |
| 8336 | if( !sCheck.aPgRef ){ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 8337 | *pnErr = 1; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 8338 | sqlite3BtreeLeave(p); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 8339 | return 0; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 8340 | } |
drh | 42cac6d | 2004-11-20 20:31:11 +0000 | [diff] [blame] | 8341 | i = PENDING_BYTE_PAGE(pBt); |
dan | 1235bb1 | 2012-04-03 17:43:28 +0000 | [diff] [blame] | 8342 | if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i); |
drh | 32055c2 | 2012-12-12 14:30:03 +0000 | [diff] [blame] | 8343 | sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), SQLITE_MAX_LENGTH); |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 8344 | sCheck.errMsg.useMalloc = 2; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8345 | |
| 8346 | /* Check the integrity of the freelist |
| 8347 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 8348 | checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), |
| 8349 | get4byte(&pBt->pPage1->aData[36]), "Main freelist: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8350 | |
| 8351 | /* Check all the tables. |
| 8352 | */ |
danielk1977 | 89d4004 | 2008-11-17 14:20:56 +0000 | [diff] [blame] | 8353 | for(i=0; (int)i<nRoot && sCheck.mxErr; i++){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 8354 | if( aRoot[i]==0 ) continue; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 8355 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 8356 | if( pBt->autoVacuum && aRoot[i]>1 ){ |
| 8357 | checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0); |
| 8358 | } |
| 8359 | #endif |
shaneh | 195475d | 2010-02-19 04:28:08 +0000 | [diff] [blame] | 8360 | checkTreePage(&sCheck, aRoot[i], "List of tree roots: ", NULL, NULL); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8361 | } |
| 8362 | |
| 8363 | /* Make sure every page in the file is referenced |
| 8364 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 8365 | for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 8366 | #ifdef SQLITE_OMIT_AUTOVACUUM |
dan | 1235bb1 | 2012-04-03 17:43:28 +0000 | [diff] [blame] | 8367 | if( getPageReferenced(&sCheck, i)==0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 8368 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8369 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 8370 | #else |
| 8371 | /* If the database supports auto-vacuum, make sure no tables contain |
| 8372 | ** references to pointer-map pages. |
| 8373 | */ |
dan | 1235bb1 | 2012-04-03 17:43:28 +0000 | [diff] [blame] | 8374 | if( getPageReferenced(&sCheck, i)==0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 8375 | (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 8376 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
| 8377 | } |
dan | 1235bb1 | 2012-04-03 17:43:28 +0000 | [diff] [blame] | 8378 | if( getPageReferenced(&sCheck, i)!=0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 8379 | (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 8380 | checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i); |
| 8381 | } |
| 8382 | #endif |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8383 | } |
| 8384 | |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 8385 | /* Make sure this analysis did not leave any unref() pages. |
| 8386 | ** This is an internal consistency check; an integrity check |
| 8387 | ** of the integrity check. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8388 | */ |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 8389 | if( NEVER(nRef != sqlite3PagerRefcount(pBt->pPager)) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 8390 | checkAppendMsg(&sCheck, 0, |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8391 | "Outstanding page count goes from %d to %d during this analysis", |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 8392 | nRef, sqlite3PagerRefcount(pBt->pPager) |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8393 | ); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8394 | } |
| 8395 | |
| 8396 | /* Clean up and report errors. |
| 8397 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 8398 | sqlite3BtreeLeave(p); |
dan | 1235bb1 | 2012-04-03 17:43:28 +0000 | [diff] [blame] | 8399 | sqlite3_free(sCheck.aPgRef); |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 8400 | if( sCheck.mallocFailed ){ |
| 8401 | sqlite3StrAccumReset(&sCheck.errMsg); |
| 8402 | *pnErr = sCheck.nErr+1; |
| 8403 | return 0; |
| 8404 | } |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 8405 | *pnErr = sCheck.nErr; |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 8406 | if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg); |
| 8407 | return sqlite3StrAccumFinish(&sCheck.errMsg); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 8408 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 8409 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 8410 | |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 8411 | /* |
drh | d4e0bb0 | 2012-05-27 01:19:04 +0000 | [diff] [blame] | 8412 | ** Return the full pathname of the underlying database file. Return |
| 8413 | ** an empty string if the database is in-memory or a TEMP database. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 8414 | ** |
| 8415 | ** The pager filename is invariant as long as the pager is |
| 8416 | ** open so it is safe to access without the BtShared mutex. |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 8417 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 8418 | const char *sqlite3BtreeGetFilename(Btree *p){ |
| 8419 | assert( p->pBt->pPager!=0 ); |
drh | d4e0bb0 | 2012-05-27 01:19:04 +0000 | [diff] [blame] | 8420 | return sqlite3PagerFilename(p->pBt->pPager, 1); |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 8421 | } |
| 8422 | |
| 8423 | /* |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 8424 | ** Return the pathname of the journal file for this database. The return |
| 8425 | ** value of this routine is the same regardless of whether the journal file |
| 8426 | ** has been created or not. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 8427 | ** |
| 8428 | ** The pager journal filename is invariant as long as the pager is |
| 8429 | ** open so it is safe to access without the BtShared mutex. |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 8430 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 8431 | const char *sqlite3BtreeGetJournalname(Btree *p){ |
| 8432 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 8433 | return sqlite3PagerJournalname(p->pBt->pPager); |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 8434 | } |
| 8435 | |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 8436 | /* |
| 8437 | ** Return non-zero if a transaction is active. |
| 8438 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 8439 | int sqlite3BtreeIsInTrans(Btree *p){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 8440 | assert( p==0 || sqlite3_mutex_held(p->db->mutex) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 8441 | return (p && (p->inTrans==TRANS_WRITE)); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 8442 | } |
| 8443 | |
dan | a550f2d | 2010-08-02 10:47:05 +0000 | [diff] [blame] | 8444 | #ifndef SQLITE_OMIT_WAL |
| 8445 | /* |
| 8446 | ** Run a checkpoint on the Btree passed as the first argument. |
| 8447 | ** |
| 8448 | ** Return SQLITE_LOCKED if this or any other connection has an open |
| 8449 | ** transaction on the shared-cache the argument Btree is connected to. |
dan | a58f26f | 2010-11-16 18:56:51 +0000 | [diff] [blame] | 8450 | ** |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 8451 | ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART. |
dan | a550f2d | 2010-08-02 10:47:05 +0000 | [diff] [blame] | 8452 | */ |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 8453 | int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){ |
dan | a550f2d | 2010-08-02 10:47:05 +0000 | [diff] [blame] | 8454 | int rc = SQLITE_OK; |
| 8455 | if( p ){ |
| 8456 | BtShared *pBt = p->pBt; |
| 8457 | sqlite3BtreeEnter(p); |
| 8458 | if( pBt->inTransaction!=TRANS_NONE ){ |
| 8459 | rc = SQLITE_LOCKED; |
| 8460 | }else{ |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 8461 | rc = sqlite3PagerCheckpoint(pBt->pPager, eMode, pnLog, pnCkpt); |
dan | a550f2d | 2010-08-02 10:47:05 +0000 | [diff] [blame] | 8462 | } |
| 8463 | sqlite3BtreeLeave(p); |
| 8464 | } |
| 8465 | return rc; |
| 8466 | } |
| 8467 | #endif |
| 8468 | |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 8469 | /* |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 8470 | ** Return non-zero if a read (or write) transaction is active. |
| 8471 | */ |
| 8472 | int sqlite3BtreeIsInReadTrans(Btree *p){ |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 8473 | assert( p ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 8474 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | 6402250 | 2009-01-09 14:11:04 +0000 | [diff] [blame] | 8475 | return p->inTrans!=TRANS_NONE; |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 8476 | } |
| 8477 | |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 8478 | int sqlite3BtreeIsInBackup(Btree *p){ |
| 8479 | assert( p ); |
| 8480 | assert( sqlite3_mutex_held(p->db->mutex) ); |
| 8481 | return p->nBackup!=0; |
| 8482 | } |
| 8483 | |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 8484 | /* |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 8485 | ** This function returns a pointer to a blob of memory associated with |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 8486 | ** 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] | 8487 | ** purposes (for example, to store a high-level schema associated with |
| 8488 | ** the shared-btree). The btree layer manages reference counting issues. |
| 8489 | ** |
| 8490 | ** The first time this is called on a shared-btree, nBytes bytes of memory |
| 8491 | ** are allocated, zeroed, and returned to the caller. For each subsequent |
| 8492 | ** call the nBytes parameter is ignored and a pointer to the same blob |
| 8493 | ** of memory returned. |
| 8494 | ** |
danielk1977 | 171bfed | 2008-06-23 09:50:50 +0000 | [diff] [blame] | 8495 | ** If the nBytes parameter is 0 and the blob of memory has not yet been |
| 8496 | ** allocated, a null pointer is returned. If the blob has already been |
| 8497 | ** allocated, it is returned as normal. |
| 8498 | ** |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 8499 | ** Just before the shared-btree is closed, the function passed as the |
| 8500 | ** xFree argument when the memory allocation was made is invoked on the |
drh | 4fa7d7c | 2011-04-03 02:41:00 +0000 | [diff] [blame] | 8501 | ** blob of allocated memory. The xFree function should not call sqlite3_free() |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 8502 | ** on the memory, the btree layer does that. |
| 8503 | */ |
| 8504 | void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){ |
| 8505 | BtShared *pBt = p->pBt; |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 8506 | sqlite3BtreeEnter(p); |
danielk1977 | 171bfed | 2008-06-23 09:50:50 +0000 | [diff] [blame] | 8507 | if( !pBt->pSchema && nBytes ){ |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 8508 | pBt->pSchema = sqlite3DbMallocZero(0, nBytes); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 8509 | pBt->xFreeSchema = xFree; |
| 8510 | } |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 8511 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 8512 | return pBt->pSchema; |
| 8513 | } |
| 8514 | |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 8515 | /* |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 8516 | ** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared |
| 8517 | ** btree as the argument handle holds an exclusive lock on the |
| 8518 | ** sqlite_master table. Otherwise SQLITE_OK. |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 8519 | */ |
| 8520 | int sqlite3BtreeSchemaLocked(Btree *p){ |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 8521 | int rc; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 8522 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 8523 | sqlite3BtreeEnter(p); |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 8524 | rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK); |
| 8525 | assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 8526 | sqlite3BtreeLeave(p); |
| 8527 | return rc; |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 8528 | } |
| 8529 | |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 8530 | |
| 8531 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 8532 | /* |
| 8533 | ** Obtain a lock on the table whose root page is iTab. The |
| 8534 | ** lock is a write lock if isWritelock is true or a read lock |
| 8535 | ** if it is false. |
| 8536 | */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 8537 | int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){ |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 8538 | int rc = SQLITE_OK; |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 8539 | assert( p->inTrans!=TRANS_NONE ); |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 8540 | if( p->sharable ){ |
| 8541 | u8 lockType = READ_LOCK + isWriteLock; |
| 8542 | assert( READ_LOCK+1==WRITE_LOCK ); |
| 8543 | assert( isWriteLock==0 || isWriteLock==1 ); |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 8544 | |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 8545 | sqlite3BtreeEnter(p); |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 8546 | rc = querySharedCacheTableLock(p, iTab, lockType); |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 8547 | if( rc==SQLITE_OK ){ |
drh | c25eabe | 2009-02-24 18:57:31 +0000 | [diff] [blame] | 8548 | rc = setSharedCacheTableLock(p, iTab, lockType); |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 8549 | } |
| 8550 | sqlite3BtreeLeave(p); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 8551 | } |
| 8552 | return rc; |
| 8553 | } |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 8554 | #endif |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 8555 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 8556 | #ifndef SQLITE_OMIT_INCRBLOB |
| 8557 | /* |
| 8558 | ** Argument pCsr must be a cursor opened for writing on an |
| 8559 | ** INTKEY table currently pointing at a valid table entry. |
| 8560 | ** This function modifies the data stored as part of that entry. |
danielk1977 | ecaecf9 | 2009-07-08 08:05:35 +0000 | [diff] [blame] | 8561 | ** |
| 8562 | ** Only the data content may only be modified, it is not possible to |
| 8563 | ** change the length of the data stored. If this function is called with |
| 8564 | ** parameters that attempt to write past the end of the existing data, |
| 8565 | ** no modifications are made and SQLITE_CORRUPT is returned. |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 8566 | */ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 8567 | int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){ |
danielk1977 | c9000e6 | 2009-07-08 13:55:28 +0000 | [diff] [blame] | 8568 | int rc; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 8569 | assert( cursorHoldsMutex(pCsr) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 8570 | assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) ); |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 8571 | assert( pCsr->curFlags & BTCF_Incrblob ); |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 8572 | |
danielk1977 | c9000e6 | 2009-07-08 13:55:28 +0000 | [diff] [blame] | 8573 | rc = restoreCursorPosition(pCsr); |
| 8574 | if( rc!=SQLITE_OK ){ |
| 8575 | return rc; |
| 8576 | } |
danielk1977 | 3588ceb | 2008-06-10 17:30:26 +0000 | [diff] [blame] | 8577 | assert( pCsr->eState!=CURSOR_REQUIRESEEK ); |
| 8578 | if( pCsr->eState!=CURSOR_VALID ){ |
| 8579 | return SQLITE_ABORT; |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 8580 | } |
| 8581 | |
dan | 227a1c4 | 2013-04-03 11:17:39 +0000 | [diff] [blame] | 8582 | /* Save the positions of all other cursors open on this table. This is |
| 8583 | ** required in case any of them are holding references to an xFetch |
| 8584 | ** version of the b-tree page modified by the accessPayload call below. |
drh | 370c9f4 | 2013-04-03 20:04:04 +0000 | [diff] [blame] | 8585 | ** |
drh | 3f38740 | 2014-09-24 01:23:00 +0000 | [diff] [blame] | 8586 | ** Note that pCsr must be open on a INTKEY table and saveCursorPosition() |
drh | 370c9f4 | 2013-04-03 20:04:04 +0000 | [diff] [blame] | 8587 | ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence |
| 8588 | ** saveAllCursors can only return SQLITE_OK. |
dan | 227a1c4 | 2013-04-03 11:17:39 +0000 | [diff] [blame] | 8589 | */ |
drh | 370c9f4 | 2013-04-03 20:04:04 +0000 | [diff] [blame] | 8590 | VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr); |
| 8591 | assert( rc==SQLITE_OK ); |
dan | 227a1c4 | 2013-04-03 11:17:39 +0000 | [diff] [blame] | 8592 | |
danielk1977 | c9000e6 | 2009-07-08 13:55:28 +0000 | [diff] [blame] | 8593 | /* Check some assumptions: |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 8594 | ** (a) the cursor is open for writing, |
danielk1977 | c9000e6 | 2009-07-08 13:55:28 +0000 | [diff] [blame] | 8595 | ** (b) there is a read/write transaction open, |
| 8596 | ** (c) the connection holds a write-lock on the table (if required), |
| 8597 | ** (d) there are no conflicting read-locks, and |
| 8598 | ** (e) the cursor points at a valid row of an intKey table. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 8599 | */ |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 8600 | if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){ |
danielk1977 | 4f02960 | 2009-07-08 18:45:37 +0000 | [diff] [blame] | 8601 | return SQLITE_READONLY; |
| 8602 | } |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 8603 | assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0 |
| 8604 | && pCsr->pBt->inTransaction==TRANS_WRITE ); |
danielk1977 | 96d48e9 | 2009-06-29 06:00:37 +0000 | [diff] [blame] | 8605 | assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) ); |
| 8606 | assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) ); |
danielk1977 | c9000e6 | 2009-07-08 13:55:28 +0000 | [diff] [blame] | 8607 | assert( pCsr->apPage[pCsr->iPage]->intKey ); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 8608 | |
drh | fb19268 | 2009-07-11 18:26:28 +0000 | [diff] [blame] | 8609 | return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 8610 | } |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 8611 | |
| 8612 | /* |
dan | 5a500af | 2014-03-11 20:33:04 +0000 | [diff] [blame] | 8613 | ** Mark this cursor as an incremental blob cursor. |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 8614 | */ |
dan | 5a500af | 2014-03-11 20:33:04 +0000 | [diff] [blame] | 8615 | void sqlite3BtreeIncrblobCursor(BtCursor *pCur){ |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 8616 | pCur->curFlags |= BTCF_Incrblob; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 8617 | } |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 8618 | #endif |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 8619 | |
| 8620 | /* |
| 8621 | ** Set both the "read version" (single byte at byte offset 18) and |
| 8622 | ** "write version" (single byte at byte offset 19) fields in the database |
| 8623 | ** header to iVersion. |
| 8624 | */ |
| 8625 | int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){ |
| 8626 | BtShared *pBt = pBtree->pBt; |
| 8627 | int rc; /* Return code */ |
| 8628 | |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 8629 | assert( iVersion==1 || iVersion==2 ); |
| 8630 | |
dan | b978002 | 2010-04-21 18:37:57 +0000 | [diff] [blame] | 8631 | /* If setting the version fields to 1, do not automatically open the |
| 8632 | ** WAL connection, even if the version fields are currently set to 2. |
| 8633 | */ |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 8634 | pBt->btsFlags &= ~BTS_NO_WAL; |
| 8635 | if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL; |
dan | b978002 | 2010-04-21 18:37:57 +0000 | [diff] [blame] | 8636 | |
| 8637 | rc = sqlite3BtreeBeginTrans(pBtree, 0); |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 8638 | if( rc==SQLITE_OK ){ |
| 8639 | u8 *aData = pBt->pPage1->aData; |
dan | b978002 | 2010-04-21 18:37:57 +0000 | [diff] [blame] | 8640 | if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){ |
dan | ede6eb8 | 2010-04-22 06:27:04 +0000 | [diff] [blame] | 8641 | rc = sqlite3BtreeBeginTrans(pBtree, 2); |
dan | b978002 | 2010-04-21 18:37:57 +0000 | [diff] [blame] | 8642 | if( rc==SQLITE_OK ){ |
| 8643 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 8644 | if( rc==SQLITE_OK ){ |
| 8645 | aData[18] = (u8)iVersion; |
| 8646 | aData[19] = (u8)iVersion; |
| 8647 | } |
| 8648 | } |
| 8649 | } |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 8650 | } |
| 8651 | |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 8652 | pBt->btsFlags &= ~BTS_NO_WAL; |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 8653 | return rc; |
| 8654 | } |
dan | 428c218 | 2012-08-06 18:50:11 +0000 | [diff] [blame] | 8655 | |
| 8656 | /* |
| 8657 | ** set the mask of hint flags for cursor pCsr. Currently the only valid |
| 8658 | ** values are 0 and BTREE_BULKLOAD. |
| 8659 | */ |
| 8660 | void sqlite3BtreeCursorHints(BtCursor *pCsr, unsigned int mask){ |
| 8661 | assert( mask==BTREE_BULKLOAD || mask==0 ); |
| 8662 | pCsr->hints = mask; |
| 8663 | } |
drh | 781597f | 2014-05-21 08:21:07 +0000 | [diff] [blame] | 8664 | |
| 8665 | /* |
| 8666 | ** Return true if the given Btree is read-only. |
| 8667 | */ |
| 8668 | int sqlite3BtreeIsReadonly(Btree *p){ |
| 8669 | return (p->pBt->btsFlags & BTS_READ_ONLY)!=0; |
| 8670 | } |