PageRenderTime 109ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 2ms

/drivers/sqlite-wp7/sqlite/btree_c.cs

https://bitbucket.org/digitalizarte/coolstorage
C# | 9518 lines | 6172 code | 602 blank | 2744 comment | 1863 complexity | 577f6e1cb79f3f8dfb87f580fc3d20fd MD5 | raw file
  1. using System;
  2. using System.Diagnostics;
  3. using System.Text;
  4. using i64 = System.Int64;
  5. using u8 = System.Byte;
  6. using u16 = System.UInt16;
  7. using u32 = System.UInt32;
  8. using u64 = System.UInt64;
  9. using sqlite3_int64 = System.Int64;
  10. using Pgno = System.UInt32;
  11. namespace Community.CsharpSqlite
  12. {
  13. using DbPage = Sqlite3.PgHdr;
  14. public partial class Sqlite3
  15. {
  16. /*
  17. ** 2004 April 6
  18. **
  19. ** The author disclaims copyright to this source code. In place of
  20. ** a legal notice, here is a blessing:
  21. **
  22. ** May you do good and not evil.
  23. ** May you find forgiveness for yourself and forgive others.
  24. ** May you share freely, never taking more than you give.
  25. **
  26. ** This file implements a external (disk-based) database using BTrees.
  27. ** See the header comment on "btreeInt.h" for additional information.
  28. ** Including a description of file format and an overview of operation.
  29. *************************************************************************
  30. ** Included in SQLite3 port to C#-SQLite; 2008 Noah B Hart
  31. ** C#-SQLite is an independent reimplementation of the SQLite software library
  32. **
  33. ** SQLITE_SOURCE_ID: 2011-01-28 17:03:50 ed759d5a9edb3bba5f48f243df47be29e3fe8cd7
  34. **
  35. *************************************************************************
  36. */
  37. //#include "btreeInt.h"
  38. /*
  39. ** The header string that appears at the beginning of every
  40. ** SQLite database.
  41. */
  42. static byte[] zMagicHeader = Encoding.UTF8.GetBytes( SQLITE_FILE_HEADER );
  43. /*
  44. ** Set this global variable to 1 to enable tracing using the TRACE
  45. ** macro.
  46. */
  47. #if TRACE
  48. static bool sqlite3BtreeTrace=false; /* True to enable tracing */
  49. //# define TRACE(X) if(sqlite3BtreeTrace){printf X;fflush(stdout);}
  50. static void TRACE(string X, params object[] ap) { if (sqlite3BtreeTrace) printf(X, ap); }
  51. #else
  52. //# define TRACE(X)
  53. static void TRACE( string X, params object[] ap )
  54. {
  55. }
  56. #endif
  57. /*
  58. ** Extract a 2-byte big-endian integer from an array of unsigned bytes.
  59. ** But if the value is zero, make it 65536.
  60. **
  61. ** This routine is used to extract the "offset to cell content area" value
  62. ** from the header of a btree page. If the page size is 65536 and the page
  63. ** is empty, the offset should be 65536, but the 2-byte value stores zero.
  64. ** This routine makes the necessary adjustment to 65536.
  65. */
  66. //#define get2byteNotZero(X) (((((int)get2byte(X))-1)&0xffff)+1)
  67. static int get2byteNotZero( byte[] X, int offset )
  68. {
  69. return ( ( ( ( (int)get2byte( X, offset ) ) - 1 ) & 0xffff ) + 1 );
  70. }
  71. #if !SQLITE_OMIT_SHARED_CACHE
  72. /*
  73. ** A list of BtShared objects that are eligible for participation
  74. ** in shared cache. This variable has file scope during normal builds,
  75. ** but the test harness needs to access it so we make it global for
  76. ** test builds.
  77. **
  78. ** Access to this variable is protected by SQLITE_MUTEX_STATIC_MASTER.
  79. */
  80. #if SQLITE_TEST
  81. BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
  82. #else
  83. static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
  84. #endif
  85. #endif //* SQLITE_OMIT_SHARED_CACHE */
  86. #if !SQLITE_OMIT_SHARED_CACHE
  87. /*
  88. ** Enable or disable the shared pager and schema features.
  89. **
  90. ** This routine has no effect on existing database connections.
  91. ** The shared cache setting effects only future calls to
  92. ** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
  93. */
  94. int sqlite3_enable_shared_cache(int enable){
  95. sqlite3GlobalConfig.sharedCacheEnabled = enable;
  96. return SQLITE_OK;
  97. }
  98. #endif
  99. #if SQLITE_OMIT_SHARED_CACHE
  100. /*
  101. ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(),
  102. ** and clearAllSharedCacheTableLocks()
  103. ** manipulate entries in the BtShared.pLock linked list used to store
  104. ** shared-cache table level locks. If the library is compiled with the
  105. ** shared-cache feature disabled, then there is only ever one user
  106. ** of each BtShared structure and so this locking is not necessary.
  107. ** So define the lock related functions as no-ops.
  108. */
  109. //#define querySharedCacheTableLock(a,b,c) SQLITE_OK
  110. static int querySharedCacheTableLock( Btree p, Pgno iTab, u8 eLock )
  111. {
  112. return SQLITE_OK;
  113. }
  114. //#define setSharedCacheTableLock(a,b,c) SQLITE_OK
  115. //#define clearAllSharedCacheTableLocks(a)
  116. static void clearAllSharedCacheTableLocks( Btree a )
  117. {
  118. }
  119. //#define downgradeAllSharedCacheTableLocks(a)
  120. static void downgradeAllSharedCacheTableLocks( Btree a )
  121. {
  122. }
  123. //#define hasSharedCacheTableLock(a,b,c,d) 1
  124. static bool hasSharedCacheTableLock( Btree a, Pgno b, int c, int d )
  125. {
  126. return true;
  127. }
  128. //#define hasReadConflicts(a, b) 0
  129. static bool hasReadConflicts( Btree a, Pgno b )
  130. {
  131. return false;
  132. }
  133. #endif
  134. #if !SQLITE_OMIT_SHARED_CACHE
  135. #if SQLITE_DEBUG
  136. /*
  137. **** This function is only used as part of an assert() statement. ***
  138. **
  139. ** Check to see if pBtree holds the required locks to read or write to the
  140. ** table with root page iRoot. Return 1 if it does and 0 if not.
  141. **
  142. ** For example, when writing to a table with root-page iRoot via
  143. ** Btree connection pBtree:
  144. **
  145. ** assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) );
  146. **
  147. ** When writing to an index that resides in a sharable database, the
  148. ** caller should have first obtained a lock specifying the root page of
  149. ** the corresponding table. This makes things a bit more complicated,
  150. ** as this module treats each table as a separate structure. To determine
  151. ** the table corresponding to the index being written, this
  152. ** function has to search through the database schema.
  153. **
  154. ** Instead of a lock on the table/index rooted at page iRoot, the caller may
  155. ** hold a write-lock on the schema table (root page 1). This is also
  156. ** acceptable.
  157. */
  158. static int hasSharedCacheTableLock(
  159. Btree pBtree, /* Handle that must hold lock */
  160. Pgno iRoot, /* Root page of b-tree */
  161. int isIndex, /* True if iRoot is the root of an index b-tree */
  162. int eLockType /* Required lock type (READ_LOCK or WRITE_LOCK) */
  163. ){
  164. Schema pSchema = (Schema *)pBtree.pBt.pSchema;
  165. Pgno iTab = 0;
  166. BtLock pLock;
  167. /* If this database is not shareable, or if the client is reading
  168. ** and has the read-uncommitted flag set, then no lock is required.
  169. ** Return true immediately.
  170. */
  171. if( (pBtree.sharable==null)
  172. || (eLockType==READ_LOCK && (pBtree.db.flags & SQLITE_ReadUncommitted))
  173. ){
  174. return 1;
  175. }
  176. /* If the client is reading or writing an index and the schema is
  177. ** not loaded, then it is too difficult to actually check to see if
  178. ** the correct locks are held. So do not bother - just return true.
  179. ** This case does not come up very often anyhow.
  180. */
  181. if( isIndex && (!pSchema || (pSchema->flags&DB_SchemaLoaded)==0) ){
  182. return 1;
  183. }
  184. /* Figure out the root-page that the lock should be held on. For table
  185. ** b-trees, this is just the root page of the b-tree being read or
  186. ** written. For index b-trees, it is the root page of the associated
  187. ** table. */
  188. if( isIndex ){
  189. HashElem p;
  190. for(p=sqliteHashFirst(pSchema.idxHash); p!=null; p=sqliteHashNext(p)){
  191. Index pIdx = (Index *)sqliteHashData(p);
  192. if( pIdx.tnum==(int)iRoot ){
  193. iTab = pIdx.pTable.tnum;
  194. }
  195. }
  196. }else{
  197. iTab = iRoot;
  198. }
  199. /* Search for the required lock. Either a write-lock on root-page iTab, a
  200. ** write-lock on the schema table, or (if the client is reading) a
  201. ** read-lock on iTab will suffice. Return 1 if any of these are found. */
  202. for(pLock=pBtree.pBt.pLock; pLock; pLock=pLock.pNext){
  203. if( pLock.pBtree==pBtree
  204. && (pLock.iTable==iTab || (pLock.eLock==WRITE_LOCK && pLock.iTable==1))
  205. && pLock.eLock>=eLockType
  206. ){
  207. return 1;
  208. }
  209. }
  210. /* Failed to find the required lock. */
  211. return 0;
  212. }
  213. #endif //* SQLITE_DEBUG */
  214. #if SQLITE_DEBUG
  215. /*
  216. ** This function may be used as part of assert() statements only. ****
  217. **
  218. ** Return true if it would be illegal for pBtree to write into the
  219. ** table or index rooted at iRoot because other shared connections are
  220. ** simultaneously reading that same table or index.
  221. **
  222. ** It is illegal for pBtree to write if some other Btree object that
  223. ** shares the same BtShared object is currently reading or writing
  224. ** the iRoot table. Except, if the other Btree object has the
  225. ** read-uncommitted flag set, then it is OK for the other object to
  226. ** have a read cursor.
  227. **
  228. ** For example, before writing to any part of the table or index
  229. ** rooted at page iRoot, one should call:
  230. **
  231. ** assert( !hasReadConflicts(pBtree, iRoot) );
  232. */
  233. static int hasReadConflicts(Btree pBtree, Pgno iRoot){
  234. BtCursor p;
  235. for(p=pBtree.pBt.pCursor; p!=null; p=p.pNext){
  236. if( p.pgnoRoot==iRoot
  237. && p.pBtree!=pBtree
  238. && 0==(p.pBtree.db.flags & SQLITE_ReadUncommitted)
  239. ){
  240. return 1;
  241. }
  242. }
  243. return 0;
  244. }
  245. #endif //* #if SQLITE_DEBUG */
  246. /*
  247. ** Query to see if Btree handle p may obtain a lock of type eLock
  248. ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
  249. ** SQLITE_OK if the lock may be obtained (by calling
  250. ** setSharedCacheTableLock()), or SQLITE_LOCKED if not.
  251. */
  252. static int querySharedCacheTableLock(Btree p, Pgno iTab, u8 eLock){
  253. BtShared pBt = p.pBt;
  254. BtLock pIter;
  255. Debug.Assert( sqlite3BtreeHoldsMutex(p) );
  256. Debug.Assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
  257. Debug.Assert( p.db!=null );
  258. Debug.Assert( !(p.db.flags&SQLITE_ReadUncommitted)||eLock==WRITE_LOCK||iTab==1 );
  259. /* If requesting a write-lock, then the Btree must have an open write
  260. ** transaction on this file. And, obviously, for this to be so there
  261. ** must be an open write transaction on the file itself.
  262. */
  263. Debug.Assert( eLock==READ_LOCK || (p==pBt.pWriter && p.inTrans==TRANS_WRITE) );
  264. Debug.Assert( eLock==READ_LOCK || pBt.inTransaction==TRANS_WRITE );
  265. /* This routine is a no-op if the shared-cache is not enabled */
  266. if( !p.sharable ){
  267. return SQLITE_OK;
  268. }
  269. /* If some other connection is holding an exclusive lock, the
  270. ** requested lock may not be obtained.
  271. */
  272. if( pBt.pWriter!=p && pBt.isExclusive ){
  273. sqlite3ConnectionBlocked(p.db, pBt.pWriter.db);
  274. return SQLITE_LOCKED_SHAREDCACHE;
  275. }
  276. for(pIter=pBt.pLock; pIter; pIter=pIter.pNext){
  277. /* The condition (pIter.eLock!=eLock) in the following if(...)
  278. ** statement is a simplification of:
  279. **
  280. ** (eLock==WRITE_LOCK || pIter.eLock==WRITE_LOCK)
  281. **
  282. ** since we know that if eLock==WRITE_LOCK, then no other connection
  283. ** may hold a WRITE_LOCK on any table in this file (since there can
  284. ** only be a single writer).
  285. */
  286. Debug.Assert( pIter.eLock==READ_LOCK || pIter.eLock==WRITE_LOCK );
  287. Debug.Assert( eLock==READ_LOCK || pIter.pBtree==p || pIter.eLock==READ_LOCK);
  288. if( pIter.pBtree!=p && pIter.iTable==iTab && pIter.eLock!=eLock ){
  289. sqlite3ConnectionBlocked(p.db, pIter.pBtree.db);
  290. if( eLock==WRITE_LOCK ){
  291. Debug.Assert( p==pBt.pWriter );
  292. pBt.isPending = 1;
  293. }
  294. return SQLITE_LOCKED_SHAREDCACHE;
  295. }
  296. }
  297. return SQLITE_OK;
  298. }
  299. #endif //* !SQLITE_OMIT_SHARED_CACHE */
  300. #if !SQLITE_OMIT_SHARED_CACHE
  301. /*
  302. ** Add a lock on the table with root-page iTable to the shared-btree used
  303. ** by Btree handle p. Parameter eLock must be either READ_LOCK or
  304. ** WRITE_LOCK.
  305. **
  306. ** This function assumes the following:
  307. **
  308. ** (a) The specified Btree object p is connected to a sharable
  309. ** database (one with the BtShared.sharable flag set), and
  310. **
  311. ** (b) No other Btree objects hold a lock that conflicts
  312. ** with the requested lock (i.e. querySharedCacheTableLock() has
  313. ** already been called and returned SQLITE_OK).
  314. **
  315. ** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM
  316. ** is returned if a malloc attempt fails.
  317. */
  318. static int setSharedCacheTableLock(Btree p, Pgno iTable, u8 eLock){
  319. BtShared pBt = p.pBt;
  320. BtLock pLock = 0;
  321. BtLock pIter;
  322. Debug.Assert( sqlite3BtreeHoldsMutex(p) );
  323. Debug.Assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
  324. Debug.Assert( p.db!=null );
  325. /* A connection with the read-uncommitted flag set will never try to
  326. ** obtain a read-lock using this function. The only read-lock obtained
  327. ** by a connection in read-uncommitted mode is on the sqlite_master
  328. ** table, and that lock is obtained in BtreeBeginTrans(). */
  329. Debug.Assert( 0==(p.db.flags&SQLITE_ReadUncommitted) || eLock==WRITE_LOCK );
  330. /* This function should only be called on a sharable b-tree after it
  331. ** has been determined that no other b-tree holds a conflicting lock. */
  332. Debug.Assert( p.sharable );
  333. Debug.Assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) );
  334. /* First search the list for an existing lock on this table. */
  335. for(pIter=pBt.pLock; pIter; pIter=pIter.pNext){
  336. if( pIter.iTable==iTable && pIter.pBtree==p ){
  337. pLock = pIter;
  338. break;
  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 ){
  345. pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
  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. /* Set the BtLock.eLock variable to the maximum of the current lock
  355. ** and the requested lock. This means if a write-lock was already held
  356. ** and a read-lock requested, we don't incorrectly downgrade the lock.
  357. */
  358. Debug.Assert( WRITE_LOCK>READ_LOCK );
  359. if( eLock>pLock.eLock ){
  360. pLock.eLock = eLock;
  361. }
  362. return SQLITE_OK;
  363. }
  364. #endif //* !SQLITE_OMIT_SHARED_CACHE */
  365. #if !SQLITE_OMIT_SHARED_CACHE
  366. /*
  367. ** Release all the table locks (locks obtained via calls to
  368. ** the setSharedCacheTableLock() procedure) held by Btree object p.
  369. **
  370. ** This function assumes that Btree p has an open read or write
  371. ** transaction. If it does not, then the BtShared.isPending variable
  372. ** may be incorrectly cleared.
  373. */
  374. static void clearAllSharedCacheTableLocks(Btree p){
  375. BtShared pBt = p.pBt;
  376. BtLock **ppIter = &pBt.pLock;
  377. Debug.Assert( sqlite3BtreeHoldsMutex(p) );
  378. Debug.Assert( p.sharable || 0==*ppIter );
  379. Debug.Assert( p.inTrans>0 );
  380. while( ppIter ){
  381. BtLock pLock = ppIter;
  382. Debug.Assert( pBt.isExclusive==null || pBt.pWriter==pLock.pBtree );
  383. Debug.Assert( pLock.pBtree.inTrans>=pLock.eLock );
  384. if( pLock.pBtree==p ){
  385. ppIter = pLock.pNext;
  386. Debug.Assert( pLock.iTable!=1 || pLock==&p.lock );
  387. if( pLock.iTable!=1 ){
  388. pLock=null;//sqlite3_free(ref pLock);
  389. }
  390. }else{
  391. ppIter = &pLock.pNext;
  392. }
  393. }
  394. Debug.Assert( pBt.isPending==null || pBt.pWriter );
  395. if( pBt.pWriter==p ){
  396. pBt.pWriter = 0;
  397. pBt.isExclusive = 0;
  398. pBt.isPending = 0;
  399. }else if( pBt.nTransaction==2 ){
  400. /* This function is called when Btree p is concluding its
  401. ** transaction. If there currently exists a writer, and p is not
  402. ** that writer, then the number of locks held by connections other
  403. ** than the writer must be about to drop to zero. In this case
  404. ** set the isPending flag to 0.
  405. **
  406. ** If there is not currently a writer, then BtShared.isPending must
  407. ** be zero already. So this next line is harmless in that case.
  408. */
  409. pBt.isPending = 0;
  410. }
  411. }
  412. /*
  413. ** This function changes all write-locks held by Btree p into read-locks.
  414. */
  415. static void downgradeAllSharedCacheTableLocks(Btree p){
  416. BtShared pBt = p.pBt;
  417. if( pBt.pWriter==p ){
  418. BtLock pLock;
  419. pBt.pWriter = 0;
  420. pBt.isExclusive = 0;
  421. pBt.isPending = 0;
  422. for(pLock=pBt.pLock; pLock; pLock=pLock.pNext){
  423. Debug.Assert( pLock.eLock==READ_LOCK || pLock.pBtree==p );
  424. pLock.eLock = READ_LOCK;
  425. }
  426. }
  427. }
  428. #endif //* SQLITE_OMIT_SHARED_CACHE */
  429. //static void releasePage(MemPage pPage); /* Forward reference */
  430. /*
  431. ***** This routine is used inside of assert() only ****
  432. **
  433. ** Verify that the cursor holds the mutex on its BtShared
  434. */
  435. #if SQLITE_DEBUG
  436. static bool cursorHoldsMutex( BtCursor p )
  437. {
  438. return sqlite3_mutex_held( p.pBt.mutex );
  439. }
  440. #else
  441. static bool cursorHoldsMutex(BtCursor p) { return true; }
  442. #endif
  443. #if !SQLITE_OMIT_INCRBLOB
  444. /*
  445. ** Invalidate the overflow page-list cache for cursor pCur, if any.
  446. */
  447. static void invalidateOverflowCache(BtCursor pCur){
  448. Debug.Assert( cursorHoldsMutex(pCur) );
  449. //sqlite3_free(ref pCur.aOverflow);
  450. pCur.aOverflow = null;
  451. }
  452. /*
  453. ** Invalidate the overflow page-list cache for all cursors opened
  454. ** on the shared btree structure pBt.
  455. */
  456. static void invalidateAllOverflowCache(BtShared pBt){
  457. BtCursor p;
  458. Debug.Assert( sqlite3_mutex_held(pBt.mutex) );
  459. for(p=pBt.pCursor; p!=null; p=p.pNext){
  460. invalidateOverflowCache(p);
  461. }
  462. }
  463. /*
  464. ** This function is called before modifying the contents of a table
  465. ** to invalidate any incrblob cursors that are open on the
  466. ** row or one of the rows being modified.
  467. **
  468. ** If argument isClearTable is true, then the entire contents of the
  469. ** table is about to be deleted. In this case invalidate all incrblob
  470. ** cursors open on any row within the table with root-page pgnoRoot.
  471. **
  472. ** Otherwise, if argument isClearTable is false, then the row with
  473. ** rowid iRow is being replaced or deleted. In this case invalidate
  474. ** only those incrblob cursors open on that specific row.
  475. */
  476. static void invalidateIncrblobCursors(
  477. Btree pBtree, /* The database file to check */
  478. i64 iRow, /* The rowid that might be changing */
  479. int isClearTable /* True if all rows are being deleted */
  480. ){
  481. BtCursor p;
  482. BtShared pBt = pBtree.pBt;
  483. Debug.Assert( sqlite3BtreeHoldsMutex(pBtree) );
  484. for(p=pBt.pCursor; p!=null; p=p.pNext){
  485. if( p.isIncrblobHandle && (isClearTable || p.info.nKey==iRow) ){
  486. p.eState = CURSOR_INVALID;
  487. }
  488. }
  489. }
  490. #else
  491. /* Stub functions when INCRBLOB is omitted */
  492. //#define invalidateOverflowCache(x)
  493. static void invalidateOverflowCache( BtCursor pCur )
  494. {
  495. }
  496. //#define invalidateAllOverflowCache(x)
  497. static void invalidateAllOverflowCache( BtShared pBt )
  498. {
  499. }
  500. //#define invalidateIncrblobCursors(x,y,z)
  501. static void invalidateIncrblobCursors( Btree x, i64 y, int z )
  502. {
  503. }
  504. #endif //* SQLITE_OMIT_INCRBLOB */
  505. /*
  506. ** Set bit pgno of the BtShared.pHasContent bitvec. This is called
  507. ** when a page that previously contained data becomes a free-list leaf
  508. ** page.
  509. **
  510. ** The BtShared.pHasContent bitvec exists to work around an obscure
  511. ** bug caused by the interaction of two useful IO optimizations surrounding
  512. ** free-list leaf pages:
  513. **
  514. ** 1) When all data is deleted from a page and the page becomes
  515. ** a free-list leaf page, the page is not written to the database
  516. ** (as free-list leaf pages contain no meaningful data). Sometimes
  517. ** such a page is not even journalled (as it will not be modified,
  518. ** why bother journalling it?).
  519. **
  520. ** 2) When a free-list leaf page is reused, its content is not read
  521. ** from the database or written to the journal file (why should it
  522. ** be, if it is not at all meaningful?).
  523. **
  524. ** By themselves, these optimizations work fine and provide a handy
  525. ** performance boost to bulk delete or insert operations. However, if
  526. ** a page is moved to the free-list and then reused within the same
  527. ** transaction, a problem comes up. If the page is not journalled when
  528. ** it is moved to the free-list and it is also not journalled when it
  529. ** is extracted from the free-list and reused, then the original data
  530. ** may be lost. In the event of a rollback, it may not be possible
  531. ** to restore the database to its original configuration.
  532. **
  533. ** The solution is the BtShared.pHasContent bitvec. Whenever a page is
  534. ** moved to become a free-list leaf page, the corresponding bit is
  535. ** set in the bitvec. Whenever a leaf page is extracted from the free-list,
  536. ** optimization 2 above is omitted if the corresponding bit is already
  537. ** set in BtShared.pHasContent. The contents of the bitvec are cleared
  538. ** at the end of every transaction.
  539. */
  540. static int btreeSetHasContent( BtShared pBt, Pgno pgno )
  541. {
  542. int rc = SQLITE_OK;
  543. if ( null == pBt.pHasContent )
  544. {
  545. Debug.Assert( pgno <= pBt.nPage );
  546. pBt.pHasContent = sqlite3BitvecCreate( pBt.nPage );
  547. if ( null == pBt.pHasContent )
  548. {
  549. rc = SQLITE_NOMEM;
  550. }
  551. }
  552. if ( rc == SQLITE_OK && pgno <= sqlite3BitvecSize( pBt.pHasContent ) )
  553. {
  554. rc = sqlite3BitvecSet( pBt.pHasContent, pgno );
  555. }
  556. return rc;
  557. }
  558. /*
  559. ** Query the BtShared.pHasContent vector.
  560. **
  561. ** This function is called when a free-list leaf page is removed from the
  562. ** free-list for reuse. It returns false if it is safe to retrieve the
  563. ** page from the pager layer with the 'no-content' flag set. True otherwise.
  564. */
  565. static bool btreeGetHasContent( BtShared pBt, Pgno pgno )
  566. {
  567. Bitvec p = pBt.pHasContent;
  568. return ( p != null && ( pgno > sqlite3BitvecSize( p ) || sqlite3BitvecTest( p, pgno ) != 0 ) );
  569. }
  570. /*
  571. ** Clear (destroy) the BtShared.pHasContent bitvec. This should be
  572. ** invoked at the conclusion of each write-transaction.
  573. */
  574. static void btreeClearHasContent( BtShared pBt )
  575. {
  576. sqlite3BitvecDestroy( ref pBt.pHasContent );
  577. pBt.pHasContent = null;
  578. }
  579. /*
  580. ** Save the current cursor position in the variables BtCursor.nKey
  581. ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
  582. **
  583. ** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID)
  584. ** prior to calling this routine.
  585. */
  586. static int saveCursorPosition( BtCursor pCur )
  587. {
  588. int rc;
  589. Debug.Assert( CURSOR_VALID == pCur.eState );
  590. Debug.Assert( null == pCur.pKey );
  591. Debug.Assert( cursorHoldsMutex( pCur ) );
  592. rc = sqlite3BtreeKeySize( pCur, ref pCur.nKey );
  593. Debug.Assert( rc == SQLITE_OK ); /* KeySize() cannot fail */
  594. /* If this is an intKey table, then the above call to BtreeKeySize()
  595. ** stores the integer key in pCur.nKey. In this case this value is
  596. ** all that is required. Otherwise, if pCur is not open on an intKey
  597. ** table, then malloc space for and store the pCur.nKey bytes of key
  598. ** data.
  599. */
  600. if ( 0 == pCur.apPage[0].intKey )
  601. {
  602. byte[] pKey = sqlite3Malloc( (int)pCur.nKey );
  603. //if( pKey !=null){
  604. rc = sqlite3BtreeKey( pCur, 0, (u32)pCur.nKey, pKey );
  605. if ( rc == SQLITE_OK )
  606. {
  607. pCur.pKey = pKey;
  608. }
  609. //else{
  610. // sqlite3_free(ref pKey);
  611. //}
  612. //}else{
  613. // rc = SQLITE_NOMEM;
  614. //}
  615. }
  616. Debug.Assert( 0 == pCur.apPage[0].intKey || null == pCur.pKey );
  617. if ( rc == SQLITE_OK )
  618. {
  619. int i;
  620. for ( i = 0; i <= pCur.iPage; i++ )
  621. {
  622. releasePage( pCur.apPage[i] );
  623. pCur.apPage[i] = null;
  624. }
  625. pCur.iPage = -1;
  626. pCur.eState = CURSOR_REQUIRESEEK;
  627. }
  628. invalidateOverflowCache( pCur );
  629. return rc;
  630. }
  631. /*
  632. ** Save the positions of all cursors (except pExcept) that are open on
  633. ** the table with root-page iRoot. Usually, this is called just before cursor
  634. ** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()).
  635. */
  636. static int saveAllCursors( BtShared pBt, Pgno iRoot, BtCursor pExcept )
  637. {
  638. BtCursor p;
  639. Debug.Assert( sqlite3_mutex_held( pBt.mutex ) );
  640. Debug.Assert( pExcept == null || pExcept.pBt == pBt );
  641. for ( p = pBt.pCursor; p != null; p = p.pNext )
  642. {
  643. if ( p != pExcept && ( 0 == iRoot || p.pgnoRoot == iRoot ) &&
  644. p.eState == CURSOR_VALID )
  645. {
  646. int rc = saveCursorPosition( p );
  647. if ( SQLITE_OK != rc )
  648. {
  649. return rc;
  650. }
  651. }
  652. }
  653. return SQLITE_OK;
  654. }
  655. /*
  656. ** Clear the current cursor position.
  657. */
  658. static void sqlite3BtreeClearCursor( BtCursor pCur )
  659. {
  660. Debug.Assert( cursorHoldsMutex( pCur ) );
  661. sqlite3_free( ref pCur.pKey );
  662. pCur.eState = CURSOR_INVALID;
  663. }
  664. /*
  665. ** In this version of BtreeMoveto, pKey is a packed index record
  666. ** such as is generated by the OP_MakeRecord opcode. Unpack the
  667. ** record and then call BtreeMovetoUnpacked() to do the work.
  668. */
  669. static int btreeMoveto(
  670. BtCursor pCur, /* Cursor open on the btree to be searched */
  671. byte[] pKey, /* Packed key if the btree is an index */
  672. i64 nKey, /* Integer key for tables. Size of pKey for indices */
  673. int bias, /* Bias search to the high end */
  674. ref int pRes /* Write search results here */
  675. )
  676. {
  677. int rc; /* Status code */
  678. UnpackedRecord pIdxKey; /* Unpacked index key */
  679. UnpackedRecord aSpace = new UnpackedRecord();//char aSpace[150]; /* Temp space for pIdxKey - to avoid a malloc */
  680. if ( pKey != null )
  681. {
  682. Debug.Assert( nKey == (i64)(int)nKey );
  683. pIdxKey = sqlite3VdbeRecordUnpack( pCur.pKeyInfo, (int)nKey, pKey,
  684. aSpace, 16 );//sizeof( aSpace ) );
  685. if ( pIdxKey == null )
  686. return SQLITE_NOMEM;
  687. }
  688. else
  689. {
  690. pIdxKey = null;
  691. }
  692. rc = sqlite3BtreeMovetoUnpacked( pCur, pIdxKey, nKey, bias != 0 ? 1 : 0, ref pRes );
  693. if ( pKey != null )
  694. {
  695. sqlite3VdbeDeleteUnpackedRecord( pIdxKey );
  696. }
  697. return rc;
  698. }
  699. /*
  700. ** Restore the cursor to the position it was in (or as close to as possible)
  701. ** when saveCursorPosition() was called. Note that this call deletes the
  702. ** saved position info stored by saveCursorPosition(), so there can be
  703. ** at most one effective restoreCursorPosition() call after each
  704. ** saveCursorPosition().
  705. */
  706. static int btreeRestoreCursorPosition( BtCursor pCur )
  707. {
  708. int rc;
  709. Debug.Assert( cursorHoldsMutex( pCur ) );
  710. Debug.Assert( pCur.eState >= CURSOR_REQUIRESEEK );
  711. if ( pCur.eState == CURSOR_FAULT )
  712. {
  713. return pCur.skipNext;
  714. }
  715. pCur.eState = CURSOR_INVALID;
  716. rc = btreeMoveto( pCur, pCur.pKey, pCur.nKey, 0, ref pCur.skipNext );
  717. if ( rc == SQLITE_OK )
  718. {
  719. //sqlite3_free(ref pCur.pKey);
  720. pCur.pKey = null;
  721. Debug.Assert( pCur.eState == CURSOR_VALID || pCur.eState == CURSOR_INVALID );
  722. }
  723. return rc;
  724. }
  725. //#define restoreCursorPosition(p) \
  726. // (p.eState>=CURSOR_REQUIRESEEK ? \
  727. // btreeRestoreCursorPosition(p) : \
  728. // SQLITE_OK)
  729. static int restoreCursorPosition( BtCursor pCur )
  730. {
  731. if ( pCur.eState >= CURSOR_REQUIRESEEK )
  732. return btreeRestoreCursorPosition( pCur );
  733. else
  734. return SQLITE_OK;
  735. }
  736. /*
  737. ** Determine whether or not a cursor has moved from the position it
  738. ** was last placed at. Cursors can move when the row they are pointing
  739. ** at is deleted out from under them.
  740. **
  741. ** This routine returns an error code if something goes wrong. The
  742. ** integer pHasMoved is set to one if the cursor has moved and 0 if not.
  743. */
  744. static int sqlite3BtreeCursorHasMoved( BtCursor pCur, ref int pHasMoved )
  745. {
  746. int rc;
  747. rc = restoreCursorPosition( pCur );
  748. if ( rc != 0 )
  749. {
  750. pHasMoved = 1;
  751. return rc;
  752. }
  753. if ( pCur.eState != CURSOR_VALID || pCur.skipNext != 0 )
  754. {
  755. pHasMoved = 1;
  756. }
  757. else
  758. {
  759. pHasMoved = 0;
  760. }
  761. return SQLITE_OK;
  762. }
  763. #if !SQLITE_OMIT_AUTOVACUUM
  764. /*
  765. ** Given a page number of a regular database page, return the page
  766. ** number for the pointer-map page that contains the entry for the
  767. ** input page number.
  768. **
  769. ** Return 0 (not a valid page) for pgno==1 since there is
  770. ** no pointer map associated with page 1. The integrity_check logic
  771. ** requires that ptrmapPageno(*,1)!=1.
  772. */
  773. static Pgno ptrmapPageno( BtShared pBt, Pgno pgno )
  774. {
  775. int nPagesPerMapPage;
  776. Pgno iPtrMap, ret;
  777. Debug.Assert( sqlite3_mutex_held( pBt.mutex ) );
  778. if ( pgno < 2 )
  779. return 0;
  780. nPagesPerMapPage = (int)( pBt.usableSize / 5 + 1 );
  781. iPtrMap = (Pgno)( ( pgno - 2 ) / nPagesPerMapPage );
  782. ret = (Pgno)( iPtrMap * nPagesPerMapPage ) + 2;
  783. if ( ret == PENDING_BYTE_PAGE( pBt ) )
  784. {
  785. ret++;
  786. }
  787. return ret;
  788. }
  789. /*
  790. ** Write an entry into the pointer map.
  791. **
  792. ** This routine updates the pointer map entry for page number 'key'
  793. ** so that it maps to type 'eType' and parent page number 'pgno'.
  794. **
  795. ** If pRC is initially non-zero (non-SQLITE_OK) then this routine is
  796. ** a no-op. If an error occurs, the appropriate error code is written
  797. ** into pRC.
  798. */
  799. static void ptrmapPut( BtShared pBt, Pgno key, u8 eType, Pgno parent, ref int pRC )
  800. {
  801. PgHdr pDbPage = new PgHdr(); /* The pointer map page */
  802. u8[] pPtrmap; /* The pointer map data */
  803. Pgno iPtrmap; /* The pointer map page number */
  804. int offset; /* Offset in pointer map page */
  805. int rc; /* Return code from subfunctions */
  806. if ( pRC != 0 )
  807. return;
  808. Debug.Assert( sqlite3_mutex_held( pBt.mutex ) );
  809. /* The master-journal page number must never be used as a pointer map page */
  810. Debug.Assert( false == PTRMAP_ISPAGE( pBt, PENDING_BYTE_PAGE( pBt ) ) );
  811. Debug.Assert( pBt.autoVacuum );
  812. if ( key == 0 )
  813. {
  814. pRC = SQLITE_CORRUPT_BKPT();
  815. return;
  816. }
  817. iPtrmap = PTRMAP_PAGENO( pBt, key );
  818. rc = sqlite3PagerGet( pBt.pPager, iPtrmap, ref pDbPage );
  819. if ( rc != SQLITE_OK )
  820. {
  821. pRC = rc;
  822. return;
  823. }
  824. offset = (int)PTRMAP_PTROFFSET( iPtrmap, key );
  825. if ( offset < 0 )
  826. {
  827. pRC = SQLITE_CORRUPT_BKPT();
  828. goto ptrmap_exit;
  829. }
  830. pPtrmap = sqlite3PagerGetData( pDbPage );
  831. if ( eType != pPtrmap[offset] || sqlite3Get4byte( pPtrmap, offset + 1 ) != parent )
  832. {
  833. TRACE( "PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent );
  834. pRC = rc = sqlite3PagerWrite( pDbPage );
  835. if ( rc == SQLITE_OK )
  836. {
  837. pPtrmap[offset] = eType;
  838. sqlite3Put4byte( pPtrmap, offset + 1, parent );
  839. }
  840. }
  841. ptrmap_exit:
  842. sqlite3PagerUnref( pDbPage );
  843. }
  844. /*
  845. ** Read an entry from the pointer map.
  846. **
  847. ** This routine retrieves the pointer map entry for page 'key', writing
  848. ** the type and parent page number to pEType and pPgno respectively.
  849. ** An error code is returned if something goes wrong, otherwise SQLITE_OK.
  850. */
  851. static int ptrmapGet( BtShared pBt, Pgno key, ref u8 pEType, ref Pgno pPgno )
  852. {
  853. PgHdr pDbPage = new PgHdr();/* The pointer map page */
  854. int iPtrmap; /* Pointer map page index */
  855. u8[] pPtrmap; /* Pointer map page data */
  856. int offset; /* Offset of entry in pointer map */
  857. int rc;
  858. Debug.Assert( sqlite3_mutex_held( pBt.mutex ) );
  859. iPtrmap = (int)PTRMAP_PAGENO( pBt, key );
  860. rc = sqlite3PagerGet( pBt.pPager, (u32)iPtrmap, ref pDbPage );
  861. if ( rc != 0 )
  862. {
  863. return rc;
  864. }
  865. pPtrmap = sqlite3PagerGetData( pDbPage );
  866. offset = (int)PTRMAP_PTROFFSET( (u32)iPtrmap, key );
  867. // Under C# pEType will always exist. No need to test; //
  868. //Debug.Assert( pEType != 0 );
  869. pEType = pPtrmap[offset];
  870. // Under C# pPgno will always exist. No need to test; //
  871. //if ( pPgno != 0 )
  872. pPgno = sqlite3Get4byte( pPtrmap, offset + 1 );
  873. sqlite3PagerUnref( pDbPage );
  874. if ( pEType < 1 || pEType > 5 )
  875. return SQLITE_CORRUPT_BKPT();
  876. return SQLITE_OK;
  877. }
  878. #else //* if defined SQLITE_OMIT_AUTOVACUUM */
  879. //#define ptrmapPut(w,x,y,z,rc)
  880. //#define ptrmapGet(w,x,y,z) SQLITE_OK
  881. //#define ptrmapPutOvflPtr(x, y, rc)
  882. #endif
  883. /*
  884. ** Given a btree page and a cell index (0 means the first cell on
  885. ** the page, 1 means the second cell, and so forth) return a pointer
  886. ** to the cell content.
  887. **
  888. ** This routine works only for pages that do not contain overflow cells.
  889. */
  890. //#define findCell(P,I) \
  891. // ((P).aData + ((P).maskPage & get2byte((P).aData[(P).cellOffset+2*(I)])))
  892. static int findCell( MemPage pPage, int iCell )
  893. {
  894. return get2byte( pPage.aData, pPage.cellOffset + 2 * ( iCell ) );
  895. }
  896. /*
  897. ** This a more complex version of findCell() that works for
  898. ** pages that do contain overflow cells.
  899. */
  900. static int findOverflowCell( MemPage pPage, int iCell )
  901. {
  902. int i;
  903. Debug.Assert( sqlite3_mutex_held( pPage.pBt.mutex ) );
  904. for ( i = pPage.nOverflow - 1; i >= 0; i-- )
  905. {
  906. int k;
  907. _OvflCell pOvfl;
  908. pOvfl = pPage.aOvfl[i];
  909. k = pOvfl.idx;
  910. if ( k <= iCell )
  911. {
  912. if ( k == iCell )
  913. {
  914. //return pOvfl.pCell;
  915. return -i - 1; // Negative Offset means overflow cells
  916. }
  917. iCell--;
  918. }
  919. }
  920. return findCell( pPage, iCell );
  921. }
  922. /*
  923. ** Parse a cell content block and fill in the CellInfo structure. There
  924. ** are two versions of this function. btreeParseCell() takes a
  925. ** cell index as the second argument and btreeParseCellPtr()
  926. ** takes a pointer to the body of the cell as its second argument.
  927. **
  928. ** Within this file, the parseCell() macro can be called instead of
  929. ** btreeParseCellPtr(). Using some compilers, this will be faster.
  930. */
  931. //OVERLOADS
  932. static void btreeParseCellPtr(
  933. MemPage pPage, /* Page containing the cell */
  934. int iCell, /* Pointer to the cell text. */
  935. ref CellInfo pInfo /* Fill in this structure */
  936. )
  937. {
  938. btreeParseCellPtr( pPage, pPage.aData, iCell, ref pInfo );
  939. }
  940. static void btreeParseCellPtr(
  941. MemPage pPage, /* Page containing the cell */
  942. byte[] pCell, /* The actual data */
  943. ref CellInfo pInfo /* Fill in this structure */
  944. )
  945. {
  946. btreeParseCellPtr( pPage, pCell, 0, ref pInfo );
  947. }
  948. static void btreeParseCellPtr(
  949. MemPage pPage, /* Page containing the cell */
  950. u8[] pCell, /* Pointer to the cell text. */
  951. int iCell, /* Pointer to the cell text. */
  952. ref CellInfo pInfo /* Fill in this structure */
  953. )
  954. {
  955. u16 n; /* Number bytes in cell content header */
  956. u32 nPayload = 0; /* Number of bytes of cell payload */
  957. Debug.Assert( sqlite3_mutex_held( pPage.pBt.mutex ) );
  958. if ( pInfo.pCell != pCell )
  959. pInfo.pCell = pCell;
  960. pInfo.iCell = iCell;
  961. Debug.Assert( pPage.leaf == 0 || pPage.leaf == 1 );
  962. n = pPage.childPtrSize;
  963. Debug.Assert( n == 4 - 4 * pPage.leaf );
  964. if ( pPage.intKey != 0 )
  965. {
  966. if ( pPage.hasData != 0 )
  967. {
  968. n += (u16)getVarint32( pCell, iCell + n, ref nPayload );
  969. }
  970. else
  971. {
  972. nPayload = 0;
  973. }
  974. n += (u16)getVarint( pCell, iCell + n, ref pInfo.nKey );
  975. pInfo.nData = nPayload;
  976. }
  977. else
  978. {
  979. pInfo.nData = 0;
  980. n += (u16)getVarint32( pCell, iCell + n, ref nPayload );
  981. pInfo.nKey = nPayload;
  982. }
  983. pInfo.nPayload = nPayload;
  984. pInfo.nHeader = n;
  985. testcase( nPayload == pPage.maxLocal );
  986. testcase( nPayload == pPage.maxLocal + 1 );
  987. if ( likely( nPayload <= pPage.maxLocal ) )
  988. {
  989. /* This is the (easy) common case where the entire payload fits
  990. ** on the local page. No overflow is required.
  991. */
  992. if ( ( pInfo.nSize = (u16)( n + nPayload ) ) < 4 )
  993. pInfo.nSize = 4;
  994. pInfo.nLocal = (u16)nPayload;
  995. pInfo.iOverflow = 0;
  996. }
  997. else
  998. {
  999. /* If the payload will not fit completely on the local page, we have
  1000. ** to decide how much to store locally and how much to spill onto
  1001. ** overflow pages. The strategy is to minimize the amount of unused
  1002. ** space on overflow pages while keeping the amount of local storage
  1003. ** in between minLocal and maxLocal.
  1004. **
  1005. ** Warning: changing the way overflow payload is distributed in any
  1006. ** way will result in an incompatible file format.
  1007. */
  1008. int minLocal; /* Minimum amount of payload held locally */
  1009. int maxLocal; /* Maximum amount of payload held locally */
  1010. int surplus; /* Overflow payload available for local storage */
  1011. minLocal = pPage.minLocal;
  1012. maxLocal = pPage.maxLocal;
  1013. surplus = (int)( minLocal + ( nPayload - minLocal ) % ( pPage.pBt.usableSize - 4 ) );
  1014. testcase( surplus == maxLocal );
  1015. testcase( surplus == maxLocal + 1 );
  1016. if ( surplus <= maxLocal )
  1017. {
  1018. pInfo.nLocal = (u16)surplus;
  1019. }
  1020. else
  1021. {
  1022. pInfo.nLocal = (u16)minLocal;
  1023. }
  1024. pInfo.iOverflow = (u16)( pInfo.nLocal + n );
  1025. pInfo.nSize = (u16)( pInfo.iOverflow + 4 );
  1026. }
  1027. }
  1028. //#define parseCell(pPage, iCell, pInfo) \
  1029. // btreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo))
  1030. static void parseCell( MemPage pPage, int iCell, ref CellInfo pInfo )
  1031. {
  1032. btreeParseCellPtr( pPage, findCell( pPage, iCell ), ref pInfo );
  1033. }
  1034. static void btreeParseCell(
  1035. MemPage pPage, /* Page containing the cell */
  1036. int iCell, /* The cell index. First cell is 0 */
  1037. ref CellInfo pInfo /* Fill in this structure */
  1038. )
  1039. {
  1040. parseCell( pPage, iCell, ref pInfo );
  1041. }
  1042. /*
  1043. ** Compute the total number of bytes that a Cell needs in the cell
  1044. ** data area of the btree-page. The return number includes the cell
  1045. ** data header and the local payload, but not any overflow page or
  1046. ** the space used by the cell pointer.
  1047. */
  1048. // Alternative form for C#
  1049. static u16 cellSizePtr( MemPage pPage, int iCell )
  1050. {
  1051. CellInfo info = new CellInfo();
  1052. byte[] pCell = new byte[13];
  1053. // Minimum Size = (2 bytes of Header or (4) Child Pointer) + (maximum of) 9 bytes data
  1054. if ( iCell < 0 )// Overflow Cell
  1055. Buffer.BlockCopy( pPage.aOvfl[-( iCell + 1 )].pCell, 0, pCell, 0, pCell.Length < pPage.aOvfl[-( iCell + 1 )].pCell.Length ? pCell.Length : pPage.aOvfl[-( iCell + 1 )].pCell.Length );
  1056. else if ( iCell >= pPage.aData.Length + 1 - pCell.Length )
  1057. Buffer.BlockCopy( pPage.aData, iCell, pCell, 0, pPage.aData.Length - iCell );
  1058. else
  1059. Buffer.BlockCopy( pPage.aData, iCell, pCell, 0, pCell.Length );
  1060. btreeParseCellPtr( pPage, pCell, ref info );
  1061. return info.nSize;
  1062. }
  1063. // Alternative form for C#
  1064. static u16 cellSizePtr( MemPage pPage, byte[] pCell, int offset )
  1065. {
  1066. CellInfo info = new CellInfo();
  1067. info.pCell = sqlite3Malloc( pCell.Length );
  1068. Buffer.BlockCopy( pCell, offset, info.pCell, 0, pCell.Length - offset );
  1069. btreeParseCellPtr( pPage, info.pCell, ref info );
  1070. return info.nSize;
  1071. }
  1072. static u16 cellSizePtr( MemPage pPage, u8[] pCell )
  1073. {
  1074. int _pIter = pPage.childPtrSize; //u8 pIter = &pCell[pPage.childPtrSize];
  1075. u32 nSize = 0;
  1076. #if SQLITE_DEBUG || DEBUG
  1077. /* The value returned by this function should always be the same as
  1078. ** the (CellInfo.nSize) value found by doing a full parse of the
  1079. ** cell. If SQLITE_DEBUG is defined, an Debug.Assert() at the bottom of
  1080. ** this function verifies that this invariant is not violated. */
  1081. CellInfo debuginfo = new CellInfo();
  1082. btreeParseCellPtr( pPage, pCell, ref debuginfo );
  1083. #else
  1084. CellInfo debuginfo = new CellInfo();
  1085. #endif
  1086. if ( pPage.intKey != 0 )
  1087. {
  1088. int pEnd;
  1089. if ( pPage.hasData != 0 )
  1090. {
  1091. _pIter += getVarint32( pCell, ref nSize );// pIter += getVarint32( pIter, ref nSize );
  1092. }
  1093. else
  1094. {
  1095. nSize = 0;
  1096. }
  1097. /* pIter now points at the 64-bit integer key value, a variable length
  1098. ** integer. The following block moves pIter to point at the first byte
  1099. ** past the end of the key value. */
  1100. pEnd = _pIter + 9;//pEnd = &pIter[9];
  1101. while ( ( ( pCell[_pIter++] ) & 0x80 ) != 0 && _pIter < pEnd )
  1102. ;//while( (pIter++)&0x80 && pIter<pEnd );
  1103. }
  1104. else
  1105. {
  1106. _pIter += getVarint32( pCell, _pIter, ref nSize ); //pIter += getVarint32( pIter, ref nSize );
  1107. }
  1108. testcase( nSize == pPage.maxLocal );
  1109. testcase( nSize == pPage.maxLocal + 1 );
  1110. if ( nSize > pPage.maxLocal )
  1111. {
  1112. int minLocal = pPage.minLocal;
  1113. nSize = (u32)( minLocal + ( nSize - minLocal ) % ( pPage.pBt.usableSize - 4 ) );
  1114. testcase( nSize == pPage.maxLocal );
  1115. testcase( nSize == pPage.maxLocal + 1 );
  1116. if ( nSize > pPage.maxLocal )
  1117. {
  1118. nSize = (u32)minLocal;
  1119. }
  1120. nSize += 4;
  1121. }
  1122. nSize += (uint)_pIter;//nSize += (u32)(pIter - pCell);
  1123. /* The minimum size of any cell is 4 bytes. */
  1124. if ( nSize < 4 )
  1125. {
  1126. nSize = 4;
  1127. }
  1128. Debug.Assert( nSize == debuginfo.nSize );
  1129. return (u16)nSize;
  1130. }
  1131. #if SQLITE_DEBUG
  1132. /* This variation on cellSizePtr() is used inside of assert() statements
  1133. ** only. */
  1134. static u16 cellSize( MemPage pPage, int iCell )
  1135. {
  1136. return cellSizePtr( pPage, findCell( pPage, iCell ) );
  1137. }
  1138. #else
  1139. static int cellSize(MemPage pPage, int iCell) { return -1; }
  1140. #endif
  1141. #if !SQLITE_OMIT_AUTOVACUUM
  1142. /*
  1143. ** If the cell pCell, part of page pPage contains a pointer
  1144. ** to an overflow page, insert an entry into the pointer-map
  1145. ** for the overflow page.
  1146. */
  1147. static void ptrmapPutOvflPtr( MemPage pPage, int pCell, ref int pRC )
  1148. {
  1149. if ( pRC != 0 )
  1150. return;
  1151. CellInfo info = new CellInfo();
  1152. Debug.Assert( pCell != 0 );
  1153. btreeParseCellPtr( pPage, pCell, ref info );
  1154. Debug.Assert( ( info.nData + ( pPage.intKey != 0 ? 0 : info.nKey ) ) == info.nPayload );
  1155. if ( info.iOverflow != 0 )
  1156. {
  1157. Pgno ovfl = sqlite3Get4byte( pPage.aData, pCell, info.iOverflow );
  1158. ptrmapPut( pPage.pBt, ovfl, PTRMAP_OVERFLOW1, pPage.pgno, ref pRC );
  1159. }
  1160. }
  1161. static void ptrmapPutOvflPtr( MemPage pPage, u8[] pCell, ref int pRC )
  1162. {
  1163. if ( pRC != 0 )
  1164. return;
  1165. CellInfo info = new CellInfo();
  1166. Debug.Assert( pCell != null );
  1167. btreeParseCellPtr( pPage, pCell, ref info );
  1168. Debug.Assert( ( info.nData + ( pPage.intKey != 0 ? 0 : info.nKey ) ) == info.nPayload );
  1169. if ( info.iOverflow != 0 )
  1170. {
  1171. Pgno ovfl = sqlite3Get4byte( pCell, info.iOverflow );
  1172. ptrmapPut( pPage.pBt, ovfl, PTRMAP_OVERFLOW1, pPage.pgno, ref pRC );
  1173. }
  1174. }
  1175. #endif
  1176. /*
  1177. ** Defragment the page given. All Cells are moved to the
  1178. ** end of the page and all free space is collected into one
  1179. ** big FreeBlk that occurs in between the header and cell
  1180. ** pointer array and the cell content area.
  1181. */
  1182. static int defragmentPage( MemPage pPage )
  1183. {
  1184. int i; /* Loop counter */
  1185. int pc; /* Address of a i-th cell */
  1186. int addr; /* Offset of first byte after cell pointer array */
  1187. int hdr; /* Offset to the page header */
  1188. int size; /* Size of a cell */
  1189. int usableSize; /* Number of usable bytes on a page */
  1190. int cellOffset; /* Offset to the cell pointer array */
  1191. int cbrk; /* Offset to the cell content area */
  1192. int nCell; /* Number of cells on the page */
  1193. byte[] data; /* The page data */
  1194. byte[] temp; /* Temp area for cell content */
  1195. int iCellFirst; /* First allowable cell index */
  1196. int iCellLast; /* Last possible cell index */
  1197. Debug.Assert( sqlite3PagerIswriteable( pPage.pDbPage ) );
  1198. Debug.Assert( pPage.pBt != null );
  1199. Debug.Assert( pPage.pBt.usableSize <= SQLITE_MAX_PAGE_SIZE );
  1200. Debug.Assert( pPage.nOverflow == 0 );
  1201. Debug.Assert( sqlite3_mutex_held( pPage.pBt.mutex ) );
  1202. temp = sqlite3PagerTempSpace( pPage.pBt.pPager );
  1203. data = pPage.aData;
  1204. hdr = pPage.hdrOffset;
  1205. cellOffset = pPage.cellOffset;
  1206. nCell = pPage.nCell;
  1207. Debug.Assert( nCell == get2byte( data, hdr + 3 ) );
  1208. usableSize = (int)pPage.pBt.usableSize;
  1209. cbrk = get2byte( data, hdr + 5 );
  1210. Buffer.BlockCopy( data, cbrk, temp, cbrk, usableSize - cbrk );//memcpy( temp[cbrk], ref data[cbrk], usableSize - cbrk );
  1211. cbrk = usableSize;
  1212. iCellFirst = cellOffset + 2 * nCell;
  1213. iCellLast = usableSize - 4;
  1214. for ( i = 0; i < nCell; i++ )
  1215. {
  1216. int pAddr; /* The i-th cell pointer */
  1217. pAddr = cellOffset + i * 2; // &data[cellOffset + i * 2];
  1218. pc = get2byte( data, pAddr );
  1219. testcase( pc == iCellFirst );
  1220. testcase( pc == iCellLast );
  1221. #if !(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
  1222. /* These conditions have already been verified in btreeInitPage()
  1223. ** if SQLITE_ENABLE_OVERSIZE_CELL_CHECK is defined
  1224. */
  1225. if ( pc < iCellFirst || pc > iCellLast )
  1226. {
  1227. return SQLITE_CORRUPT_BKPT();
  1228. }
  1229. #endif
  1230. Debug.Assert( pc >= iCellFirst && pc <= iCellLast );
  1231. size = cellSizePtr( pPage, temp, pc );
  1232. cbrk -= size;
  1233. #if (SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
  1234. if ( cbrk < iCellFirst )
  1235. {
  1236. return SQLITE_CORRUPT_BKPT();
  1237. }
  1238. #else
  1239. if ( cbrk < iCellFirst || pc + size > usableSize )
  1240. {
  1241. return SQLITE_CORRUPT_BKPT();
  1242. }
  1243. #endif
  1244. Debug.Assert( cbrk + size <= usableSize && cbrk >= iCellFirst );
  1245. testcase( cbrk + size == usableSize );
  1246. testcase( pc + size == usableSize );
  1247. Buffer.BlockCopy( temp, pc, data, cbrk, size );//memcpy(data[cbrk], ref temp[pc], size);
  1248. put2byte( data, pAddr, cbrk );
  1249. }
  1250. Debug.Assert( cbrk >= iCellFirst );
  1251. put2byte( data, hdr + 5, cbrk );
  1252. data[hdr + 1] = 0;
  1253. data[hdr + 2] = 0;
  1254. data[hdr + 7] = 0;
  1255. addr = cellOffset + 2 * nCell;
  1256. Array.Clear( data, addr, cbrk - addr ); //memset(data[iCellFirst], 0, cbrk-iCellFirst);
  1257. Debug.Assert( sqlite3PagerIswriteable( pPage.pDbPage ) );
  1258. if ( cbrk - iCellFirst != pPage.nFree )
  1259. {
  1260. return SQLITE_CORRUPT_BKPT();
  1261. }
  1262. return SQLITE_OK;
  1263. }
  1264. /*
  1265. ** Allocate nByte bytes of space from within the B-Tree page passed
  1266. ** as the first argument. Write into pIdx the index into pPage.aData[]
  1267. ** of the first byte of allocated space. Return either SQLITE_OK or
  1268. ** an error code (usually SQLITE_CORRUPT).
  1269. **
  1270. ** The caller guarantees that there is sufficient space to make the
  1271. ** allocation. This routine might need to defragment in order to bring
  1272. ** all the space together, however. This routine will avoid using
  1273. ** the first two bytes past the cell pointer area since presumably this
  1274. ** allocation is being made in order to insert a new cell, so we will
  1275. ** also end up needing a new cell pointer.
  1276. */
  1277. static int allocateSpace( MemPage pPage, int nByte, ref int pIdx )
  1278. {
  1279. int hdr = pPage.hdrOffset; /* Local cache of pPage.hdrOffset */
  1280. u8[] data = pPage.aData; /* Local cache of pPage.aData */
  1281. int nFrag; /* Number of fragmented bytes on pPage */
  1282. int top; /* First byte of cell content area */
  1283. int gap; /* First byte of gap between cell pointers and cell content */
  1284. int rc; /* Integer return code */
  1285. u32 usableSize; /* Usable size of the page */
  1286. Debug.Assert( sqlite3PagerIswriteable( pPage.pDbPage ) );
  1287. Debug.Assert( pPage.pBt != null );
  1288. Debug.Assert( sqlite3_mutex_held( pPage.pBt.mutex ) );
  1289. Debug.Assert( nByte >= 0 ); /* Minimum cell size is 4 */
  1290. Debug.Assert( pPage.nFree >= nByte );
  1291. Debug.Assert( pPage.nOverflow == 0 );
  1292. usableSize = pPage.pBt.usableSize;
  1293. Debug.Assert( nByte < usableSize - 8 );
  1294. nFrag = data[hdr + 7];
  1295. Debug.Assert( pPage.cellOffset == hdr + 12 - 4 * pPage.leaf );
  1296. gap = pPage.cellOffset + 2 * pPage.nCell;
  1297. top = get2byteNotZero( data, hdr + 5 );
  1298. if ( gap > top )
  1299. return SQLITE_CORRUPT_BKPT();
  1300. testcase( gap + 2 == top );
  1301. testcase( gap + 1 == top );
  1302. testcase( gap == top );
  1303. if ( nFrag >= 60 )
  1304. {
  1305. /* Always defragment highly fragmented pages */
  1306. rc = defragmentPage( pPage );
  1307. if ( rc != 0 )
  1308. return rc;
  1309. top = get2byteNotZero( data, hdr + 5 );
  1310. }
  1311. else if ( gap + 2 <= top )
  1312. {
  1313. /* Search the freelist looking for a free slot big enough to satisfy
  1314. ** the request. The allocation is made from the first free slot in
  1315. ** the list that is large enough to accomadate it.
  1316. */
  1317. int pc, addr;
  1318. for ( addr = hdr + 1; ( pc = get2byte( data, addr ) ) > 0; addr = pc )
  1319. {
  1320. int size; /* Size of free slot */
  1321. if ( pc > usableSize - 4 || pc < addr + 4 )
  1322. {
  1323. return SQLITE_CORRUPT_BKPT();
  1324. }
  1325. size = get2byte( data, pc + 2 );
  1326. if ( size >= nByte )
  1327. {
  1328. int x = size - nByte;
  1329. testcase( x == 4 );
  1330. testcase( x == 3 );
  1331. if ( x < 4 )
  1332. {
  1333. /* Remove the slot from the free-list. Update the number of
  1334. ** fragmented bytes within the page. */
  1335. data[addr + 0] = data[pc + 0];
  1336. data[addr + 1] = data[pc + 1]; //memcpy( data[addr], ref data[pc], 2 );
  1337. data[hdr + 7] = (u8)( nFrag + x );
  1338. }
  1339. else if ( size + pc > usableSize )
  1340. {
  1341. return SQLITE_CORRUPT_BKPT();
  1342. }
  1343. else
  1344. {
  1345. /* The slot remains on the free-list. Reduce its size to account
  1346. ** for the portion used by the new allocation. */
  1347. put2byte( data, pc + 2, x );
  1348. }
  1349. pIdx = pc + x;
  1350. return SQLITE_OK;
  1351. }
  1352. }
  1353. }
  1354. /* Check to make sure there is enough space in the gap to satisfy
  1355. ** the allocation. If not, defragment.
  1356. */
  1357. testcase( gap + 2 + nByte == top );
  1358. if ( gap + 2 + nByte > top )
  1359. {
  1360. rc = defragmentPage( pPage );
  1361. if ( rc != 0 )
  1362. return rc;
  1363. top = get2byteNotZero( data, hdr + 5 );
  1364. Debug.Assert( gap + nByte <= top );
  1365. }
  1366. /* Allocate memory from the gap in between the cell pointer array
  1367. ** and the cell content area. The btreeInitPage() call has already
  1368. ** validated the freelist. Given that the freelist is valid, there
  1369. ** is no way that the allocation can extend off the end of the page.
  1370. ** The Debug.Assert() below verifies the previous sentence.
  1371. */
  1372. top -= nByte;
  1373. put2byte( data, hdr + 5, top );
  1374. Debug.Assert( top + nByte <= pPage.pBt.usableSize );
  1375. pIdx = top;
  1376. return SQLITE_OK;
  1377. }
  1378. /*
  1379. ** Return a section of the pPage.aData to the freelist.
  1380. ** The first byte of the new free block is pPage.aDisk[start]
  1381. ** and the size of the block is "size" bytes.
  1382. **
  1383. ** Most of the effort here is involved in coalesing adjacent
  1384. ** free blocks into a single big free block.
  1385. */
  1386. static int freeSpace( MemPage pPage, u32 start, int size )
  1387. {
  1388. return freeSpace( pPage, (int)start, size );
  1389. }
  1390. static int freeSpace( MemPage pPage, int start, int size )
  1391. {
  1392. int addr, pbegin, hdr;
  1393. int iLast; /* Largest possible freeblock offset */
  1394. byte[] data = pPage.aData;
  1395. Debug.Assert( pPage.pBt != null );
  1396. Debug.Assert( sqlite3PagerIswriteable( pPage.pDbPage ) );
  1397. Debug.Assert( start >= pPage.hdrOffset + 6 + pPage.childPtrSize );
  1398. Debug.Assert( ( start + size ) <= pPage.pBt.usableSize );
  1399. Debug.Assert( sqlite3_mutex_held( pPage.pBt.mutex ) );
  1400. Debug.Assert( size >= 0 ); /* Minimum cell size is 4 */
  1401. if ( pPage.pBt.secureDelete )
  1402. {
  1403. /* Overwrite deleted information with zeros when the secure_delete
  1404. ** option is enabled */
  1405. Array.Clear( data, start, size );// memset(&data[start], 0, size);
  1406. }
  1407. /* Add the space back into the linked list of freeblocks. Note that
  1408. ** even though the freeblock list was checked by btreeInitPage(),
  1409. ** btreeInitPage() did not detect overlapping cells or
  1410. ** freeblocks that overlapped cells. Nor does it detect when the
  1411. ** cell content area exceeds the value in the page header. If these
  1412. ** situations arise, then subsequent insert operations might corrupt
  1413. ** the freelist. So we do need to check for corruption while scanning
  1414. ** the freelist.
  1415. */
  1416. hdr = pPage.hdrOffset;
  1417. addr = hdr + 1;
  1418. iLast = (int)pPage.pBt.usableSize - 4;
  1419. Debug.Assert( start <= iLast );
  1420. while ( ( pbegin = get2byte( data, addr ) ) < start && pbegin > 0 )
  1421. {
  1422. if ( pbegin < addr + 4 )
  1423. {
  1424. return SQLITE_CORRUPT_BKPT();
  1425. }
  1426. addr = pbegin;
  1427. }
  1428. if ( pbegin > iLast )
  1429. {
  1430. return SQLITE_CORRUPT_BKPT();
  1431. }
  1432. Debug.Assert( pbegin > addr || pbegin == 0 );
  1433. put2byte( data, addr, start );
  1434. put2byte( data, start, pbegin );
  1435. put2byte( data, start + 2, size );
  1436. pPage.nFree = (u16)( pPage.nFree + size );
  1437. /* Coalesce adjacent free blocks */
  1438. addr = hdr + 1;
  1439. while ( ( pbegin = get2byte( data, addr ) ) > 0 )
  1440. {
  1441. int pnext, psize, x;
  1442. Debug.Assert( pbegin > addr );
  1443. Debug.Assert( pbegin <= pPage.pBt.usableSize - 4 );
  1444. pnext = get2byte( data, pbegin );
  1445. psize = get2byte( data, pbegin + 2 );
  1446. if ( pbegin + psize + 3 >= pnext && pnext > 0 )
  1447. {
  1448. int frag = pnext - ( pbegin + psize );
  1449. if ( ( frag < 0 ) || ( frag > (int)data[hdr + 7] ) )
  1450. {
  1451. return SQLITE_CORRUPT_BKPT();
  1452. }
  1453. data[hdr + 7] -= (u8)frag;
  1454. x = get2byte( data, pnext );
  1455. put2byte( data, pbegin, x );
  1456. x = pnext + get2byte( data, pnext + 2 ) - pbegin;
  1457. put2byte( data, pbegin + 2, x );
  1458. }
  1459. else
  1460. {
  1461. addr = pbegin;
  1462. }
  1463. }
  1464. /* If the cell content area begins with a freeblock, remove it. */
  1465. if ( data[hdr + 1] == data[hdr + 5] && data[hdr + 2] == data[hdr + 6] )
  1466. {
  1467. int top;
  1468. pbegin = get2byte( data, hdr + 1 );
  1469. put2byte( data, hdr + 1, get2byte( data, pbegin ) ); //memcpy( data[hdr + 1], ref data[pbegin], 2 );
  1470. top = get2byte( data, hdr + 5 ) + get2byte( data, pbegin + 2 );
  1471. put2byte( data, hdr + 5, top );
  1472. }
  1473. Debug.Assert( sqlite3PagerIswriteable( pPage.pDbPage ) );
  1474. return SQLITE_OK;
  1475. }
  1476. /*
  1477. ** Decode the flags byte (the first byte of the header) for a page
  1478. ** and initialize fields of the MemPage structure accordingly.
  1479. **
  1480. ** Only the following combinations are supported. Anything different
  1481. ** indicates a corrupt database files:
  1482. **
  1483. ** PTF_ZERODATA
  1484. ** PTF_ZERODATA | PTF_LEAF
  1485. ** PTF_LEAFDATA | PTF_INTKEY
  1486. ** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF
  1487. */
  1488. static int decodeFlags( MemPage pPage, int flagByte )
  1489. {
  1490. BtShared pBt; /* A copy of pPage.pBt */
  1491. Debug.Assert( pPage.hdrOffset == ( pPage.pgno == 1 ? 100 : 0 ) );
  1492. Debug.Assert( sqlite3_mutex_held( pPage.pBt.mutex ) );
  1493. pPage.leaf = (u8)( flagByte >> 3 );
  1494. Debug.Assert( PTF_LEAF == 1 << 3 );
  1495. flagByte &= ~PTF_LEAF;
  1496. pPage.childPtrSize = (u8)( 4 - 4 * pPage.leaf );
  1497. pBt = pPage.pBt;
  1498. if ( flagByte == ( PTF_LEAFDATA | PTF_INTKEY ) )
  1499. {
  1500. pPage.intKey = 1;
  1501. pPage.hasData = pPage.leaf;
  1502. pPage.maxLocal = pBt.maxLeaf;
  1503. pPage.minLocal = pBt.minLeaf;
  1504. }
  1505. else if ( flagByte == PTF_ZERODATA )
  1506. {
  1507. pPage.intKey = 0;
  1508. pPage.hasData = 0;
  1509. pPage.maxLocal = pBt.maxLocal;
  1510. pPage.minLocal = pBt.minLocal;
  1511. }
  1512. else
  1513. {
  1514. return SQLITE_CORRUPT_BKPT();
  1515. }
  1516. return SQLITE_OK;
  1517. }
  1518. /*
  1519. ** Initialize the auxiliary information for a disk block.
  1520. **
  1521. ** Return SQLITE_OK on success. If we see that the page does
  1522. ** not contain a well-formed database page, then return
  1523. ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
  1524. ** guarantee that the page is well-formed. It only shows that
  1525. ** we failed to detect any corruption.
  1526. */
  1527. static int btreeInitPage( MemPage pPage )
  1528. {
  1529. Debug.Assert( pPage.pBt != null );
  1530. Debug.Assert( sqlite3_mutex_held( pPage.pBt.mutex ) );
  1531. Debug.Assert( pPage.pgno == sqlite3PagerPagenumber( pPage.pDbPage ) );
  1532. Debug.Assert( pPage == sqlite3PagerGetExtra( pPage.pDbPage ) );
  1533. Debug.Assert( pPage.aData == sqlite3PagerGetData( pPage.pDbPage ) );
  1534. if ( 0 == pPage.isInit )
  1535. {
  1536. u16 pc; /* Address of a freeblock within pPage.aData[] */
  1537. u8 hdr; /* Offset to beginning of page header */
  1538. u8[] data; /* Equal to pPage.aData */
  1539. BtShared pBt; /* The main btree structure */
  1540. int usableSize; /* Amount of usable space on each page */
  1541. u16 cellOffset; /* Offset from start of page to first cell pointer */
  1542. int nFree; /* Number of unused bytes on the page */
  1543. int top; /* First byte of the cell content area */
  1544. int iCellFirst; /* First allowable cell or freeblock offset */
  1545. int iCellLast; /* Last possible cell or freeblock offset */
  1546. pBt = pPage.pBt;
  1547. hdr = pPage.hdrOffset;
  1548. data = pPage.aData;
  1549. if ( decodeFlags( pPage, data[hdr] ) != 0 )
  1550. return SQLITE_CORRUPT_BKPT();
  1551. Debug.Assert( pBt.pageSize >= 512 && pBt.pageSize <= 65536 );
  1552. pPage.maskPage = (u16)( pBt.pageSize - 1 );
  1553. pPage.nOverflow = 0;
  1554. usableSize = (int)pBt.usableSize;
  1555. pPage.cellOffset = ( cellOffset = (u16)( hdr + 12 - 4 * pPage.leaf ) );
  1556. top = get2byteNotZero( data, hdr + 5 );
  1557. pPage.nCell = (u16)( get2byte( data, hdr + 3 ) );
  1558. if ( pPage.nCell > MX_CELL( pBt ) )
  1559. {
  1560. /* To many cells for a single page. The page must be corrupt */
  1561. return SQLITE_CORRUPT_BKPT();
  1562. }
  1563. testcase( pPage.nCell == MX_CELL( pBt ) );
  1564. /* A malformed database page might cause us to read past the end
  1565. ** of page when parsing a cell.
  1566. **
  1567. ** The following block of code checks early to see if a cell extends
  1568. ** past the end of a page boundary and causes SQLITE_CORRUPT to be
  1569. ** returned if it does.
  1570. */
  1571. iCellFirst = cellOffset + 2 * pPage.nCell;
  1572. iCellLast = usableSize - 4;
  1573. #if (SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
  1574. {
  1575. int i; /* Index into the cell pointer array */
  1576. int sz; /* Size of a cell */
  1577. if ( 0 == pPage.leaf )
  1578. iCellLast--;
  1579. for ( i = 0; i < pPage.nCell; i++ )
  1580. {
  1581. pc = (u16)get2byte( data, cellOffset + i * 2 );
  1582. testcase( pc == iCellFirst );
  1583. testcase( pc == iCellLast );
  1584. if ( pc < iCellFirst || pc > iCellLast )
  1585. {
  1586. return SQLITE_CORRUPT_BKPT();
  1587. }
  1588. sz = cellSizePtr( pPage, data, pc );
  1589. testcase( pc + sz == usableSize );
  1590. if ( pc + sz > usableSize )
  1591. {
  1592. return SQLITE_CORRUPT_BKPT();
  1593. }
  1594. }
  1595. if ( 0 == pPage.leaf )
  1596. iCellLast++;
  1597. }
  1598. #endif
  1599. /* Compute the total free space on the page */
  1600. pc = (u16)get2byte( data, hdr + 1 );
  1601. nFree = (u16)( data[hdr + 7] + top );
  1602. while ( pc > 0 )
  1603. {
  1604. u16 next, size;
  1605. if ( pc < iCellFirst || pc > iCellLast )
  1606. {
  1607. /* Start of free block is off the page */
  1608. return SQLITE_CORRUPT_BKPT();
  1609. }
  1610. next = (u16)get2byte( data, pc );
  1611. size = (u16)get2byte( data, pc + 2 );
  1612. if ( ( next > 0 && next <= pc + size + 3 ) || pc + size > usableSize )
  1613. {
  1614. /* Free blocks must be in ascending order. And the last byte of
  1615. ** the free-block must lie on the database page. */
  1616. return SQLITE_CORRUPT_BKPT();
  1617. }
  1618. nFree = (u16)( nFree + size );
  1619. pc = next;
  1620. }
  1621. /* At this point, nFree contains the sum of the offset to the start
  1622. ** of the cell-content area plus the number of free bytes within
  1623. ** the cell-content area. If this is greater than the usable-size
  1624. ** of the page, then the page must be corrupted. This check also
  1625. ** serves to verify that the offset to the start of the cell-content
  1626. ** area, according to the page header, lies within the page.
  1627. */
  1628. if ( nFree > usableSize )
  1629. {
  1630. return SQLITE_CORRUPT_BKPT();
  1631. }
  1632. pPage.nFree = (u16)( nFree - iCellFirst );
  1633. pPage.isInit = 1;
  1634. }
  1635. return SQLITE_OK;
  1636. }
  1637. /*
  1638. ** Set up a raw page so that it looks like a database page holding
  1639. ** no entries.
  1640. */
  1641. static void zeroPage( MemPage pPage, int flags )
  1642. {
  1643. byte[] data = pPage.aData;
  1644. BtShared pBt = pPage.pBt;
  1645. u8 hdr = pPage.hdrOffset;
  1646. u16 first;
  1647. Debug.Assert( sqlite3PagerPagenumber( pPage.pDbPage ) == pPage.pgno );
  1648. Debug.Assert( sqlite3PagerGetExtra( pPage.pDbPage ) == pPage );
  1649. Debug.Assert( sqlite3PagerGetData( pPage.pDbPage ) == data );
  1650. Debug.Assert( sqlite3PagerIswriteable( pPage.pDbPage ) );
  1651. Debug.Assert( sqlite3_mutex_held( pBt.mutex ) );
  1652. if ( pBt.secureDelete )
  1653. {
  1654. Array.Clear( data, hdr, (int)( pBt.usableSize - hdr ) );//memset(&data[hdr], 0, pBt->usableSize - hdr);
  1655. }
  1656. data[hdr] = (u8)flags;
  1657. first = (u16)( hdr + 8 + 4 * ( ( flags & PTF_LEAF ) == 0 ? 1 : 0 ) );
  1658. Array.Clear( data, hdr + 1, 4 );//memset(data[hdr+1], 0, 4);
  1659. data[hdr + 7] = 0;
  1660. put2byte( data, hdr + 5, pBt.usableSize );
  1661. pPage.nFree = (u16)( pBt.usableSize - first );
  1662. decodeFlags( pPage, flags );
  1663. pPage.hdrOffset = hdr;
  1664. pPage.cellOffset = first;
  1665. pPage.nOverflow = 0;
  1666. Debug.Assert( pBt.pageSize >= 512 && pBt.pageSize <= 65536 );
  1667. pPage.maskPage = (u16)( pBt.pageSize - 1 );
  1668. pPage.nCell = 0;
  1669. pPage.isInit = 1;
  1670. }
  1671. /*
  1672. ** Convert a DbPage obtained from the pager into a MemPage used by
  1673. ** the btree layer.
  1674. */
  1675. static MemPage btreePageFromDbPage( DbPage pDbPage, Pgno pgno, BtShared pBt )
  1676. {
  1677. MemPage pPage = (MemPage)sqlite3PagerGetExtra( pDbPage );
  1678. pPage.aData = sqlite3PagerGetData( pDbPage );
  1679. pPage.pDbPage = pDbPage;
  1680. pPage.pBt = pBt;
  1681. pPage.pgno = pgno;
  1682. pPage.hdrOffset = (u8)( pPage.pgno == 1 ? 100 : 0 );
  1683. return pPage;
  1684. }
  1685. /*
  1686. ** Get a page from the pager. Initialize the MemPage.pBt and
  1687. ** MemPage.aData elements if needed.
  1688. **
  1689. ** If the noContent flag is set, it means that we do not care about
  1690. ** the content of the page at this time. So do not go to the disk
  1691. ** to fetch the content. Just fill in the content with zeros for now.
  1692. ** If in the future we call sqlite3PagerWrite() on this page, that
  1693. ** means we have started to be concerned about content and the disk
  1694. ** read should occur at that point.
  1695. */
  1696. static int btreeGetPage(
  1697. BtShared pBt, /* The btree */
  1698. Pgno pgno, /* Number of the page to fetch */
  1699. ref MemPage ppPage, /* Return the page in this parameter */
  1700. int noContent /* Do not load page content if true */
  1701. )
  1702. {
  1703. int rc;
  1704. DbPage pDbPage = null;
  1705. Debug.Assert( sqlite3_mutex_held( pBt.mutex ) );
  1706. rc = sqlite3PagerAcquire( pBt.pPager, pgno, ref pDbPage, (u8)noContent );
  1707. if ( rc != 0 )
  1708. return rc;
  1709. ppPage = btreePageFromDbPage( pDbPage, pgno, pBt );
  1710. return SQLITE_OK;
  1711. }
  1712. /*
  1713. ** Retrieve a page from the pager cache. If the requested page is not
  1714. ** already in the pager cache return NULL. Initialize the MemPage.pBt and
  1715. ** MemPage.aData elements if needed.
  1716. */
  1717. static MemPage btreePageLookup( BtShared pBt, Pgno pgno )
  1718. {
  1719. DbPage pDbPage;
  1720. Debug.Assert( sqlite3_mutex_held( pBt.mutex ) );
  1721. pDbPage = sqlite3PagerLookup( pBt.pPager, pgno );
  1722. if ( pDbPage )
  1723. {
  1724. return btreePageFromDbPage( pDbPage, pgno, pBt );
  1725. }
  1726. return null;
  1727. }
  1728. /*
  1729. ** Return the size of the database file in pages. If there is any kind of
  1730. ** error, return ((unsigned int)-1).
  1731. */
  1732. static Pgno btreePagecount( BtShared pBt )
  1733. {
  1734. return pBt.nPage;
  1735. }
  1736. static Pgno sqlite3BtreeLastPage( Btree p )
  1737. {
  1738. Debug.Assert( sqlite3BtreeHoldsMutex( p ) );
  1739. Debug.Assert( ( ( p.pBt.nPage ) & 0x8000000 ) == 0 );
  1740. return (Pgno)btreePagecount( p.pBt );
  1741. }
  1742. /*
  1743. ** Get a page from the pager and initialize it. This routine is just a
  1744. ** convenience wrapper around separate calls to btreeGetPage() and
  1745. ** btreeInitPage().
  1746. **
  1747. ** If an error occurs, then the value ppPage is set to is undefined. It
  1748. ** may remain unchanged, or it may be set to an invalid value.
  1749. */
  1750. static int getAndInitPage(
  1751. BtShared pBt, /* The database file */
  1752. Pgno pgno, /* Number of the page to get */
  1753. ref MemPage ppPage /* Write the page pointer here */
  1754. )
  1755. {
  1756. int rc;
  1757. Debug.Assert( sqlite3_mutex_held( pBt.mutex ) );
  1758. if ( pgno > btreePagecount( pBt ) )
  1759. {
  1760. rc = SQLITE_CORRUPT_BKPT();
  1761. }
  1762. else
  1763. {
  1764. rc = btreeGetPage( pBt, pgno, ref ppPage, 0 );
  1765. if ( rc == SQLITE_OK )
  1766. {
  1767. rc = btreeInitPage( ppPage );
  1768. if ( rc != SQLITE_OK )
  1769. {
  1770. releasePage( ppPage );
  1771. }
  1772. }
  1773. }
  1774. testcase( pgno == 0 );
  1775. Debug.Assert( pgno != 0 || rc == SQLITE_CORRUPT );
  1776. return rc;
  1777. }
  1778. /*
  1779. ** Release a MemPage. This should be called once for each prior
  1780. ** call to btreeGetPage.
  1781. */
  1782. static void releasePage( MemPage pPage )
  1783. {
  1784. if ( pPage != null )
  1785. {
  1786. Debug.Assert( pPage.aData != null );
  1787. Debug.Assert( pPage.pBt != null );
  1788. //TODO -- find out why corrupt9 & diskfull fail on this tests
  1789. //Debug.Assert( sqlite3PagerGetExtra( pPage.pDbPage ) == pPage );
  1790. //Debug.Assert( sqlite3PagerGetData( pPage.pDbPage ) == pPage.aData );
  1791. Debug.Assert( sqlite3_mutex_held( pPage.pBt.mutex ) );
  1792. sqlite3PagerUnref( pPage.pDbPage );
  1793. }
  1794. }
  1795. /*
  1796. ** During a rollback, when the pager reloads information into the cache
  1797. ** so that the cache is restored to its original state at the start of
  1798. ** the transaction, for each page restored this routine is called.
  1799. **
  1800. ** This routine needs to reset the extra data section at the end of the
  1801. ** page to agree with the restored data.
  1802. */
  1803. static void pageReinit( DbPage pData )
  1804. {
  1805. MemPage pPage;
  1806. pPage = sqlite3PagerGetExtra( pData );
  1807. Debug.Assert( sqlite3PagerPageRefcount( pData ) > 0 );
  1808. if ( pPage.isInit != 0 )
  1809. {
  1810. Debug.Assert( sqlite3_mutex_held( pPage.pBt.mutex ) );
  1811. pPage.isInit = 0;
  1812. if ( sqlite3PagerPageRefcount( pData ) > 1 )
  1813. {
  1814. /* pPage might not be a btree page; it might be an overflow page
  1815. ** or ptrmap page or a free page. In those cases, the following
  1816. ** call to btreeInitPage() will likely return SQLITE_CORRUPT.
  1817. ** But no harm is done by this. And it is very important that
  1818. ** btreeInitPage() be called on every btree page so we make
  1819. ** the call for every page that comes in for re-initing. */
  1820. btreeInitPage( pPage );
  1821. }
  1822. }
  1823. }
  1824. /*
  1825. ** Invoke the busy handler for a btree.
  1826. */
  1827. static int btreeInvokeBusyHandler( object pArg )
  1828. {
  1829. BtShared pBt = (BtShared)pArg;
  1830. Debug.Assert( pBt.db != null );
  1831. Debug.Assert( sqlite3_mutex_held( pBt.db.mutex ) );
  1832. return sqlite3InvokeBusyHandler( pBt.db.busyHandler );
  1833. }
  1834. /*
  1835. ** Open a database file.
  1836. **
  1837. ** zFilename is the name of the database file. If zFilename is NULL
  1838. ** then an ephemeral database is created. The ephemeral database might
  1839. ** be exclusively in memory, or it might use a disk-based memory cache.
  1840. ** Either way, the ephemeral database will be automatically deleted
  1841. ** when sqlite3BtreeClose() is called.
  1842. **
  1843. ** If zFilename is ":memory:" then an in-memory database is created
  1844. ** that is automatically destroyed when it is closed.
  1845. **
  1846. ** The "flags" parameter is a bitmask that might contain bits
  1847. ** BTREE_OMIT_JOURNAL and/or BTREE_NO_READLOCK. The BTREE_NO_READLOCK
  1848. ** bit is also set if the SQLITE_NoReadlock flags is set in db->flags.
  1849. ** These flags are passed through into sqlite3PagerOpen() and must
  1850. ** be the same values as PAGER_OMIT_JOURNAL and PAGER_NO_READLOCK.
  1851. **
  1852. ** If the database is already opened in the same database connection
  1853. ** and we are in shared cache mode, then the open will fail with an
  1854. ** SQLITE_CONSTRAINT error. We cannot allow two or more BtShared
  1855. ** objects in the same database connection since doing so will lead
  1856. ** to problems with locking.
  1857. */
  1858. static int sqlite3BtreeOpen(
  1859. string zFilename, /* Name of the file containing the BTree database */
  1860. sqlite3 db, /* Associated database handle */
  1861. ref Btree ppBtree, /* Pointer to new Btree object written here */
  1862. int flags, /* Options */
  1863. int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */
  1864. )
  1865. {
  1866. sqlite3_vfs pVfs; /* The VFS to use for this btree */
  1867. BtShared pBt = null; /* Shared part of btree structure */
  1868. Btree p; /* Handle to return */
  1869. sqlite3_mutex mutexOpen = null; /* Prevents a race condition. Ticket #3537 */
  1870. int rc = SQLITE_OK; /* Result code from this function */
  1871. u8 nReserve; /* Byte of unused space on each page */
  1872. byte[] zDbHeader = new byte[100]; /* Database header content */
  1873. /* True if opening an ephemeral, temporary database */
  1874. bool isTempDb = String.IsNullOrEmpty( zFilename );//zFilename==0 || zFilename[0]==0;
  1875. /* Set the variable isMemdb to true for an in-memory database, or
  1876. ** false for a file-based database.
  1877. */
  1878. #if SQLITE_OMIT_MEMORYDB
  1879. bool isMemdb = false;
  1880. #else
  1881. bool isMemdb = ( zFilename == ":memory:" )
  1882. || ( isTempDb && sqlite3TempInMemory( db ) );
  1883. #endif
  1884. Debug.Assert( db != null );
  1885. Debug.Assert( sqlite3_mutex_held( db.mutex ) );
  1886. Debug.Assert( ( flags & 0xff ) == flags ); /* flags fit in 8 bits */
  1887. /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
  1888. Debug.Assert( ( flags & BTREE_UNORDERED ) == 0 || ( flags & BTREE_SINGLE ) != 0 );
  1889. /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
  1890. Debug.Assert( ( flags & BTREE_SINGLE ) == 0 || isTempDb );
  1891. if ( ( db.flags & SQLITE_NoReadlock ) != 0 )
  1892. {
  1893. flags |= BTREE_NO_READLOCK;
  1894. }
  1895. if ( isMemdb )
  1896. {
  1897. flags |= BTREE_MEMORY;
  1898. }
  1899. if ( ( vfsFlags & SQLITE_OPEN_MAIN_DB ) != 0 && ( isMemdb || isTempDb ) )
  1900. {
  1901. vfsFlags = ( vfsFlags & ~SQLITE_OPEN_MAIN_DB ) | SQLITE_OPEN_TEMP_DB;
  1902. }
  1903. pVfs = db.pVfs;
  1904. p = new Btree();//sqlite3MallocZero(sizeof(Btree));
  1905. //if( !p ){
  1906. // return SQLITE_NOMEM;
  1907. //}
  1908. p.inTrans = TRANS_NONE;
  1909. p.db = db;
  1910. #if !SQLITE_OMIT_SHARED_CACHE
  1911. p.lock.pBtree = p;
  1912. p.lock.iTable = 1;
  1913. #endif
  1914. #if !(SQLITE_OMIT_SHARED_CACHE) && !(SQLITE_OMIT_DISKIO)
  1915. /*
  1916. ** If this Btree is a candidate for shared cache, try to find an
  1917. ** existing BtShared object that we can share with
  1918. */
  1919. if( !isMemdb && !isTempDb ){
  1920. if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){
  1921. int nFullPathname = pVfs.mxPathname+1;
  1922. string zFullPathname = sqlite3Malloc(nFullPathname);
  1923. sqlite3_mutex *mutexShared;
  1924. p.sharable = 1;
  1925. if( !zFullPathname ){
  1926. p = null;//sqlite3_free(ref p);
  1927. return SQLITE_NOMEM;
  1928. }
  1929. sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname);
  1930. mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
  1931. sqlite3_mutex_enter(mutexOpen);
  1932. mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
  1933. sqlite3_mutex_enter(mutexShared);
  1934. for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt.pNext){
  1935. Debug.Assert( pBt.nRef>0 );
  1936. if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt.pPager))
  1937. && sqlite3PagerVfs(pBt.pPager)==pVfs ){
  1938. int iDb;
  1939. for(iDb=db.nDb-1; iDb>=0; iDb--){
  1940. Btree pExisting = db.aDb[iDb].pBt;
  1941. if( pExisting && pExisting.pBt==pBt ){
  1942. sqlite3_mutex_leave(mutexShared);
  1943. sqlite3_mutex_leave(mutexOpen);
  1944. zFullPathname = null;//sqlite3_free(ref zFullPathname);
  1945. p=null;//sqlite3_free(ref p);
  1946. return SQLITE_CONSTRAINT;
  1947. }
  1948. }
  1949. p.pBt = pBt;
  1950. pBt.nRef++;
  1951. break;
  1952. }
  1953. }
  1954. sqlite3_mutex_leave(mutexShared);
  1955. zFullPathname=null;//sqlite3_free(ref zFullPathname);
  1956. }
  1957. #if SQLITE_DEBUG
  1958. else{
  1959. /* In debug mode, we mark all persistent databases as sharable
  1960. ** even when they are not. This exercises the locking code and
  1961. ** gives more opportunity for asserts(sqlite3_mutex_held())
  1962. ** statements to find locking problems.
  1963. */
  1964. p.sharable = 1;
  1965. }
  1966. #endif
  1967. }
  1968. #endif
  1969. if ( pBt == null )
  1970. {
  1971. /*
  1972. ** The following asserts make sure that structures used by the btree are
  1973. ** the right size. This is to guard against size changes that result
  1974. ** when compiling on a different architecture.
  1975. */
  1976. Debug.Assert( sizeof( i64 ) == 8 || sizeof( i64 ) == 4 );
  1977. Debug.Assert( sizeof( u64 ) == 8 || sizeof( u64 ) == 4 );
  1978. Debug.Assert( sizeof( u32 ) == 4 );
  1979. Debug.Assert( sizeof( u16 ) == 2 );
  1980. Debug.Assert( sizeof( Pgno ) == 4 );
  1981. pBt = new BtShared();//sqlite3MallocZero( sizeof(pBt) );
  1982. //if( pBt==null ){
  1983. // rc = SQLITE_NOMEM;
  1984. // goto btree_open_out;
  1985. //}
  1986. rc = sqlite3PagerOpen( pVfs, ref pBt.pPager, zFilename,
  1987. EXTRA_SIZE, flags, vfsFlags, pageReinit );
  1988. if ( rc == SQLITE_OK )
  1989. {
  1990. rc = sqlite3PagerReadFileheader( pBt.pPager, zDbHeader.Length, zDbHeader );
  1991. }
  1992. if ( rc != SQLITE_OK )
  1993. {
  1994. goto btree_open_out;
  1995. }
  1996. pBt.openFlags = (u8)flags;
  1997. pBt.db = db;
  1998. sqlite3PagerSetBusyhandler( pBt.pPager, btreeInvokeBusyHandler, pBt );
  1999. p.pBt = pBt;
  2000. pBt.pCursor = null;
  2001. pBt.pPage1 = null;
  2002. pBt.readOnly = sqlite3PagerIsreadonly( pBt.pPager );
  2003. #if SQLITE_SECURE_DELETE
  2004. pBt.secureDelete = true;
  2005. #endif
  2006. pBt.pageSize = (u32)( ( zDbHeader[16] << 8 ) | ( zDbHeader[17] << 16 ) );
  2007. if ( pBt.pageSize < 512 || pBt.pageSize > SQLITE_MAX_PAGE_SIZE
  2008. || ( ( pBt.pageSize - 1 ) & pBt.pageSize ) != 0 )
  2009. {
  2010. pBt.pageSize = 0;
  2011. #if !SQLITE_OMIT_AUTOVACUUM
  2012. /* If the magic name ":memory:" will create an in-memory database, then
  2013. ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
  2014. ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
  2015. ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
  2016. ** regular file-name. In this case the auto-vacuum applies as per normal.
  2017. */
  2018. if ( zFilename != "" && !isMemdb )
  2019. {
  2020. pBt.autoVacuum = ( SQLITE_DEFAULT_AUTOVACUUM != 0 );
  2021. pBt.incrVacuum = ( SQLITE_DEFAULT_AUTOVACUUM == 2 );
  2022. }
  2023. #endif
  2024. nReserve = 0;
  2025. }
  2026. else
  2027. {
  2028. nReserve = zDbHeader[20];
  2029. pBt.pageSizeFixed = true;
  2030. #if !SQLITE_OMIT_AUTOVACUUM
  2031. pBt.autoVacuum = sqlite3Get4byte( zDbHeader, 36 + 4 * 4 ) != 0;
  2032. pBt.incrVacuum = sqlite3Get4byte( zDbHeader, 36 + 7 * 4 ) != 0;
  2033. #endif
  2034. }
  2035. rc = sqlite3PagerSetPagesize( pBt.pPager, ref pBt.pageSize, nReserve );
  2036. if ( rc != 0 )
  2037. goto btree_open_out;
  2038. pBt.usableSize = (u16)( pBt.pageSize - nReserve );
  2039. Debug.Assert( ( pBt.pageSize & 7 ) == 0 ); /* 8-byte alignment of pageSize */
  2040. #if !(SQLITE_OMIT_SHARED_CACHE) && !(SQLITE_OMIT_DISKIO)
  2041. /* Add the new BtShared object to the linked list sharable BtShareds.
  2042. */
  2043. if( p.sharable ){
  2044. sqlite3_mutex *mutexShared;
  2045. pBt.nRef = 1;
  2046. mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
  2047. if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
  2048. pBt.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
  2049. if( pBt.mutex==null ){
  2050. rc = SQLITE_NOMEM;
  2051. db.mallocFailed = 0;
  2052. goto btree_open_out;
  2053. }
  2054. }
  2055. sqlite3_mutex_enter(mutexShared);
  2056. pBt.pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
  2057. GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt;
  2058. sqlite3_mutex_leave(mutexShared);
  2059. }
  2060. #endif
  2061. }
  2062. #if !(SQLITE_OMIT_SHARED_CACHE) && !(SQLITE_OMIT_DISKIO)
  2063. /* If the new Btree uses a sharable pBtShared, then link the new
  2064. ** Btree into the list of all sharable Btrees for the same connection.
  2065. ** The list is kept in ascending order by pBt address.
  2066. */
  2067. if( p.sharable ){
  2068. int i;
  2069. Btree pSib;
  2070. for(i=0; i<db.nDb; i++){
  2071. if( (pSib = db.aDb[i].pBt)!=null && pSib.sharable ){
  2072. while( pSib.pPrev ){ pSib = pSib.pPrev; }
  2073. if( p.pBt<pSib.pBt ){
  2074. p.pNext = pSib;
  2075. p.pPrev = 0;
  2076. pSib.pPrev = p;
  2077. }else{
  2078. while( pSib.pNext && pSib.pNext.pBt<p.pBt ){
  2079. pSib = pSib.pNext;
  2080. }
  2081. p.pNext = pSib.pNext;
  2082. p.pPrev = pSib;
  2083. if( p.pNext ){
  2084. p.pNext.pPrev = p;
  2085. }
  2086. pSib.pNext = p;
  2087. }
  2088. break;
  2089. }
  2090. }
  2091. }
  2092. #endif
  2093. ppBtree = p;
  2094. btree_open_out:
  2095. if ( rc != SQLITE_OK )
  2096. {
  2097. if ( pBt != null && pBt.pPager != null )
  2098. {
  2099. sqlite3PagerClose( pBt.pPager );
  2100. }
  2101. pBt = null; // sqlite3_free(ref pBt);
  2102. p = null; // sqlite3_free(ref p);
  2103. ppBtree = null;
  2104. }
  2105. else
  2106. {
  2107. /* If the B-Tree was successfully opened, set the pager-cache size to the
  2108. ** default value. Except, when opening on an existing shared pager-cache,
  2109. ** do not change the pager-cache size.
  2110. */
  2111. if ( sqlite3BtreeSchema( p, 0, null ) == null )
  2112. {
  2113. sqlite3PagerSetCachesize( p.pBt.pPager, SQLITE_DEFAULT_CACHE_SIZE );
  2114. }
  2115. }
  2116. if ( mutexOpen != null )
  2117. {
  2118. Debug.Assert( sqlite3_mutex_held( mutexOpen ) );
  2119. sqlite3_mutex_leave( mutexOpen );
  2120. }
  2121. return rc;
  2122. }
  2123. /*
  2124. ** Decrement the BtShared.nRef counter. When it reaches zero,
  2125. ** remove the BtShared structure from the sharing list. Return
  2126. ** true if the BtShared.nRef counter reaches zero and return
  2127. ** false if it is still positive.
  2128. */
  2129. static bool removeFromSharingList( BtShared pBt )
  2130. {
  2131. #if !SQLITE_OMIT_SHARED_CACHE
  2132. sqlite3_mutex pMaster;
  2133. BtShared pList;
  2134. bool removed = false;
  2135. Debug.Assert( sqlite3_mutex_notheld(pBt.mutex) );
  2136. pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
  2137. sqlite3_mutex_enter(pMaster);
  2138. pBt.nRef--;
  2139. if( pBt.nRef<=0 ){
  2140. if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){
  2141. GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt.pNext;
  2142. }else{
  2143. pList = GLOBAL(BtShared*,sqlite3SharedCacheList);
  2144. while( ALWAYS(pList) && pList.pNext!=pBt ){
  2145. pList=pList.pNext;
  2146. }
  2147. if( ALWAYS(pList) ){
  2148. pList.pNext = pBt.pNext;
  2149. }
  2150. }
  2151. if( SQLITE_THREADSAFE ){
  2152. sqlite3_mutex_free(pBt.mutex);
  2153. }
  2154. removed = true;
  2155. }
  2156. sqlite3_mutex_leave(pMaster);
  2157. return removed;
  2158. #else
  2159. return true;
  2160. #endif
  2161. }
  2162. /*
  2163. ** Make sure pBt.pTmpSpace points to an allocation of
  2164. ** MX_CELL_SIZE(pBt) bytes.
  2165. */
  2166. static void allocateTempSpace( BtShared pBt )
  2167. {
  2168. if ( null == pBt.pTmpSpace )
  2169. {
  2170. pBt.pTmpSpace = sqlite3Malloc( pBt.pageSize );
  2171. }
  2172. }
  2173. /*
  2174. ** Free the pBt.pTmpSpace allocation
  2175. */
  2176. static void freeTempSpace( BtShared pBt )
  2177. {
  2178. sqlite3PageFree( ref pBt.pTmpSpace );
  2179. }
  2180. /*
  2181. ** Close an open database and invalidate all cursors.
  2182. */
  2183. static int sqlite3BtreeClose( ref Btree p )
  2184. {
  2185. BtShared pBt = p.pBt;
  2186. BtCursor pCur;
  2187. /* Close all cursors opened via this handle. */
  2188. Debug.Assert( sqlite3_mutex_held( p.db.mutex ) );
  2189. sqlite3BtreeEnter( p );
  2190. pCur = pBt.pCursor;
  2191. while ( pCur != null )
  2192. {
  2193. BtCursor pTmp = pCur;
  2194. pCur = pCur.pNext;
  2195. if ( pTmp.pBtree == p )
  2196. {
  2197. sqlite3BtreeCloseCursor( pTmp );
  2198. }
  2199. }
  2200. /* Rollback any active transaction and free the handle structure.
  2201. ** The call to sqlite3BtreeRollback() drops any table-locks held by
  2202. ** this handle.
  2203. */
  2204. sqlite3BtreeRollback( p );
  2205. sqlite3BtreeLeave( p );
  2206. /* If there are still other outstanding references to the shared-btree
  2207. ** structure, return now. The remainder of this procedure cleans
  2208. ** up the shared-btree.
  2209. */
  2210. Debug.Assert( p.wantToLock == 0 && !p.locked );
  2211. if ( !p.sharable || removeFromSharingList( pBt ) )
  2212. {
  2213. /* The pBt is no longer on the sharing list, so we can access
  2214. ** it without having to hold the mutex.
  2215. **
  2216. ** Clean out and delete the BtShared object.
  2217. */
  2218. Debug.Assert( null == pBt.pCursor );
  2219. sqlite3PagerClose( pBt.pPager );
  2220. if ( pBt.xFreeSchema != null && pBt.pSchema != null )
  2221. {
  2222. pBt.xFreeSchema( pBt.pSchema );
  2223. }
  2224. pBt.pSchema = null;// sqlite3DbFree(0, pBt->pSchema);
  2225. //freeTempSpace(pBt);
  2226. pBt = null; //sqlite3_free(ref pBt);
  2227. }
  2228. #if !SQLITE_OMIT_SHARED_CACHE
  2229. Debug.Assert( p.wantToLock==null );
  2230. Debug.Assert( p.locked==null );
  2231. if( p.pPrev ) p.pPrev.pNext = p.pNext;
  2232. if( p.pNext ) p.pNext.pPrev = p.pPrev;
  2233. #endif
  2234. //sqlite3_free(ref p);
  2235. return SQLITE_OK;
  2236. }
  2237. /*
  2238. ** Change the limit on the number of pages allowed in the cache.
  2239. **
  2240. ** The maximum number of cache pages is set to the absolute
  2241. ** value of mxPage. If mxPage is negative, the pager will
  2242. ** operate asynchronously - it will not stop to do fsync()s
  2243. ** to insure data is written to the disk surface before
  2244. ** continuing. Transactions still work if synchronous is off,
  2245. ** and the database cannot be corrupted if this program
  2246. ** crashes. But if the operating system crashes or there is
  2247. ** an abrupt power failure when synchronous is off, the database
  2248. ** could be left in an inconsistent and unrecoverable state.
  2249. ** Synchronous is on by default so database corruption is not
  2250. ** normally a worry.
  2251. */
  2252. static int sqlite3BtreeSetCacheSize( Btree p, int mxPage )
  2253. {
  2254. BtShared pBt = p.pBt;
  2255. Debug.Assert( sqlite3_mutex_held( p.db.mutex ) );
  2256. sqlite3BtreeEnter( p );
  2257. sqlite3PagerSetCachesize( pBt.pPager, mxPage );
  2258. sqlite3BtreeLeave( p );
  2259. return SQLITE_OK;
  2260. }
  2261. /*
  2262. ** Change the way data is synced to disk in order to increase or decrease
  2263. ** how well the database resists damage due to OS crashes and power
  2264. ** failures. Level 1 is the same as asynchronous (no syncs() occur and
  2265. ** there is a high probability of damage) Level 2 is the default. There
  2266. ** is a very low but non-zero probability of damage. Level 3 reduces the
  2267. ** probability of damage to near zero but with a write performance reduction.
  2268. */
  2269. #if !SQLITE_OMIT_PAGER_PRAGMAS
  2270. static int sqlite3BtreeSetSafetyLevel(
  2271. Btree p, /* The btree to set the safety level on */
  2272. int level, /* PRAGMA synchronous. 1=OFF, 2=NORMAL, 3=FULL */
  2273. int fullSync, /* PRAGMA fullfsync. */
  2274. int ckptFullSync /* PRAGMA checkpoint_fullfync */
  2275. )
  2276. {
  2277. BtShared pBt = p.pBt;
  2278. Debug.Assert( sqlite3_mutex_held( p.db.mutex ) );
  2279. Debug.Assert( level >= 1 && level <= 3 );
  2280. sqlite3BtreeEnter( p );
  2281. sqlite3PagerSetSafetyLevel( pBt.pPager, level, fullSync, ckptFullSync );
  2282. sqlite3BtreeLeave( p );
  2283. return SQLITE_OK;
  2284. }
  2285. #endif
  2286. /*
  2287. ** Return TRUE if the given btree is set to safety level 1. In other
  2288. ** words, return TRUE if no sync() occurs on the disk files.
  2289. */
  2290. static int sqlite3BtreeSyncDisabled( Btree p )
  2291. {
  2292. BtShared pBt = p.pBt;
  2293. int rc;
  2294. Debug.Assert( sqlite3_mutex_held( p.db.mutex ) );
  2295. sqlite3BtreeEnter( p );
  2296. Debug.Assert( pBt != null && pBt.pPager != null );
  2297. rc = sqlite3PagerNosync( pBt.pPager ) ? 1 : 0;
  2298. sqlite3BtreeLeave( p );
  2299. return rc;
  2300. }
  2301. #if !(SQLITE_OMIT_PAGER_PRAGMAS) || !(SQLITE_OMIT_VACUUM)
  2302. /*
  2303. ** Change the default pages size and the number of reserved bytes per page.
  2304. ** Or, if the page size has already been fixed, return SQLITE_READONLY
  2305. ** without changing anything.
  2306. **
  2307. ** The page size must be a power of 2 between 512 and 65536. If the page
  2308. ** size supplied does not meet this constraint then the page size is not
  2309. ** changed.
  2310. **
  2311. ** Page sizes are constrained to be a power of two so that the region
  2312. ** of the database file used for locking (beginning at PENDING_BYTE,
  2313. ** the first byte past the 1GB boundary, 0x40000000) needs to occur
  2314. ** at the beginning of a page.
  2315. **
  2316. ** If parameter nReserve is less than zero, then the number of reserved
  2317. ** bytes per page is left unchanged.
  2318. **
  2319. ** If iFix!=0 then the pageSizeFixed flag is set so that the page size
  2320. ** and autovacuum mode can no longer be changed.
  2321. */
  2322. static int sqlite3BtreeSetPageSize( Btree p, int pageSize, int nReserve, int iFix )
  2323. {
  2324. int rc = SQLITE_OK;
  2325. BtShared pBt = p.pBt;
  2326. Debug.Assert( nReserve >= -1 && nReserve <= 255 );
  2327. sqlite3BtreeEnter( p );
  2328. if ( pBt.pageSizeFixed )
  2329. {
  2330. sqlite3BtreeLeave( p );
  2331. return SQLITE_READONLY;
  2332. }
  2333. if ( nReserve < 0 )
  2334. {
  2335. nReserve = (int)( pBt.pageSize - pBt.usableSize );
  2336. }
  2337. Debug.Assert( nReserve >= 0 && nReserve <= 255 );
  2338. if ( pageSize >= 512 && pageSize <= SQLITE_MAX_PAGE_SIZE &&
  2339. ( ( pageSize - 1 ) & pageSize ) == 0 )
  2340. {
  2341. Debug.Assert( ( pageSize & 7 ) == 0 );
  2342. Debug.Assert( null == pBt.pPage1 && null == pBt.pCursor );
  2343. pBt.pageSize = (u32)pageSize;
  2344. // freeTempSpace(pBt);
  2345. }
  2346. rc = sqlite3PagerSetPagesize( pBt.pPager, ref pBt.pageSize, nReserve );
  2347. pBt.usableSize = (u16)( pBt.pageSize - nReserve );
  2348. if ( iFix != 0 )
  2349. pBt.pageSizeFixed = true;
  2350. sqlite3BtreeLeave( p );
  2351. return rc;
  2352. }
  2353. /*
  2354. ** Return the currently defined page size
  2355. */
  2356. static int sqlite3BtreeGetPageSize( Btree p )
  2357. {
  2358. return (int)p.pBt.pageSize;
  2359. }
  2360. /*
  2361. ** Return the number of bytes of space at the end of every page that
  2362. ** are intentually left unused. This is the "reserved" space that is
  2363. ** sometimes used by extensions.
  2364. */
  2365. static int sqlite3BtreeGetReserve( Btree p )
  2366. {
  2367. int n;
  2368. sqlite3BtreeEnter( p );
  2369. n = (int)( p.pBt.pageSize - p.pBt.usableSize );
  2370. sqlite3BtreeLeave( p );
  2371. return n;
  2372. }
  2373. /*
  2374. ** Set the maximum page count for a database if mxPage is positive.
  2375. ** No changes are made if mxPage is 0 or negative.
  2376. ** Regardless of the value of mxPage, return the maximum page count.
  2377. */
  2378. static Pgno sqlite3BtreeMaxPageCount( Btree p, int mxPage )
  2379. {
  2380. Pgno n;
  2381. sqlite3BtreeEnter( p );
  2382. n = sqlite3PagerMaxPageCount( p.pBt.pPager, mxPage );
  2383. sqlite3BtreeLeave( p );
  2384. return n;
  2385. }
  2386. /*
  2387. ** Set the secureDelete flag if newFlag is 0 or 1. If newFlag is -1,
  2388. ** then make no changes. Always return the value of the secureDelete
  2389. ** setting after the change.
  2390. */
  2391. static int sqlite3BtreeSecureDelete( Btree p, int newFlag )
  2392. {
  2393. int b;
  2394. if ( p == null )
  2395. return 0;
  2396. sqlite3BtreeEnter( p );
  2397. if ( newFlag >= 0 )
  2398. {
  2399. p.pBt.secureDelete = ( newFlag != 0 );
  2400. }
  2401. b = p.pBt.secureDelete ? 1 : 0;
  2402. sqlite3BtreeLeave( p );
  2403. return b;
  2404. }
  2405. #endif //* !(SQLITE_OMIT_PAGER_PRAGMAS) || !(SQLITE_OMIT_VACUUM) */
  2406. /*
  2407. ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
  2408. ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
  2409. ** is disabled. The default value for the auto-vacuum property is
  2410. ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
  2411. */
  2412. static int sqlite3BtreeSetAutoVacuum( Btree p, int autoVacuum )
  2413. {
  2414. #if SQLITE_OMIT_AUTOVACUUM
  2415. return SQLITE_READONLY;
  2416. #else
  2417. BtShared pBt = p.pBt;
  2418. int rc = SQLITE_OK;
  2419. u8 av = (u8)autoVacuum;
  2420. sqlite3BtreeEnter( p );
  2421. if ( pBt.pageSizeFixed && ( av != 0 ) != pBt.autoVacuum )
  2422. {
  2423. rc = SQLITE_READONLY;
  2424. }
  2425. else
  2426. {
  2427. pBt.autoVacuum = av != 0;
  2428. pBt.incrVacuum = av == 2;
  2429. }
  2430. sqlite3BtreeLeave( p );
  2431. return rc;
  2432. #endif
  2433. }
  2434. /*
  2435. ** Return the value of the 'auto-vacuum' property. If auto-vacuum is
  2436. ** enabled 1 is returned. Otherwise 0.
  2437. */
  2438. static int sqlite3BtreeGetAutoVacuum( Btree p )
  2439. {
  2440. #if SQLITE_OMIT_AUTOVACUUM
  2441. return BTREE_AUTOVACUUM_NONE;
  2442. #else
  2443. int rc;
  2444. sqlite3BtreeEnter( p );
  2445. rc = (
  2446. ( !p.pBt.autoVacuum ) ? BTREE_AUTOVACUUM_NONE :
  2447. ( !p.pBt.incrVacuum ) ? BTREE_AUTOVACUUM_FULL :
  2448. BTREE_AUTOVACUUM_INCR
  2449. );
  2450. sqlite3BtreeLeave( p );
  2451. return rc;
  2452. #endif
  2453. }
  2454. /*
  2455. ** Get a reference to pPage1 of the database file. This will
  2456. ** also acquire a readlock on that file.
  2457. **
  2458. ** SQLITE_OK is returned on success. If the file is not a
  2459. ** well-formed database file, then SQLITE_CORRUPT is returned.
  2460. ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM
  2461. ** is returned if we run out of memory.
  2462. */
  2463. static int lockBtree( BtShared pBt )
  2464. {
  2465. int rc; /* Result code from subfunctions */
  2466. MemPage pPage1 = null; /* Page 1 of the database file */
  2467. Pgno nPage; /* Number of pages in the database */
  2468. Pgno nPageFile = 0; /* Number of pages in the database file */
  2469. Pgno nPageHeader; /* Number of pages in the database according to hdr */
  2470. Debug.Assert( sqlite3_mutex_held( pBt.mutex ) );
  2471. Debug.Assert( pBt.pPage1 == null );
  2472. rc = sqlite3PagerSharedLock( pBt.pPager );
  2473. if ( rc != SQLITE_OK )
  2474. return rc;
  2475. rc = btreeGetPage( pBt, 1, ref pPage1, 0 );
  2476. if ( rc != SQLITE_OK )
  2477. return rc;
  2478. /* Do some checking to help insure the file we opened really is
  2479. ** a valid database file.
  2480. */
  2481. nPage = nPageHeader = sqlite3Get4byte( pPage1.aData, 28 );//get4byte(28+(u8*)pPage1->aData);
  2482. sqlite3PagerPagecount( pBt.pPager, ref nPageFile );
  2483. if ( nPage == 0 || memcmp( pPage1.aData, 24, pPage1.aData, 92, 4 ) != 0 )//memcmp(24 + (u8*)pPage1.aData, 92 + (u8*)pPage1.aData, 4) != 0)
  2484. {
  2485. nPage = nPageFile;
  2486. }
  2487. if ( nPage > 0 )
  2488. {
  2489. u32 pageSize;
  2490. u32 usableSize;
  2491. u8[] page1 = pPage1.aData;
  2492. rc = SQLITE_NOTADB;
  2493. if ( memcmp( page1, zMagicHeader, 16 ) != 0 )
  2494. {
  2495. goto page1_init_failed;
  2496. }
  2497. #if SQLITE_OMIT_WAL
  2498. if ( page1[18] > 1 )
  2499. {
  2500. pBt.readOnly = true;
  2501. }
  2502. if ( page1[19] > 1 )
  2503. {
  2504. goto page1_init_failed;
  2505. }
  2506. #else
  2507. if( page1[18]>2 ){
  2508. pBt.readOnly = true;
  2509. }
  2510. if( page1[19]>2 ){
  2511. goto page1_init_failed;
  2512. }
  2513. /* If the write version is set to 2, this database should be accessed
  2514. ** in WAL mode. If the log is not already open, open it now. Then
  2515. ** return SQLITE_OK and return without populating BtShared.pPage1.
  2516. ** The caller detects this and calls this function again. This is
  2517. ** required as the version of page 1 currently in the page1 buffer
  2518. ** may not be the latest version - there may be a newer one in the log
  2519. ** file.
  2520. */
  2521. if( page1[19]==2 && pBt.doNotUseWAL==false ){
  2522. int isOpen = 0;
  2523. rc = sqlite3PagerOpenWal(pBt.pPager, ref isOpen);
  2524. if( rc!=SQLITE_OK ){
  2525. goto page1_init_failed;
  2526. }else if( isOpen==0 ){
  2527. releasePage(pPage1);
  2528. return SQLITE_OK;
  2529. }
  2530. rc = SQLITE_NOTADB;
  2531. }
  2532. #endif
  2533. /* The maximum embedded fraction must be exactly 25%. And the minimum
  2534. ** embedded fraction must be 12.5% for both leaf-data and non-leaf-data.
  2535. ** The original design allowed these amounts to vary, but as of
  2536. ** version 3.6.0, we require them to be fixed.
  2537. */
  2538. if ( memcmp( page1, 21, "\x0040\x0020\x0020", 3 ) != 0 )// "\100\040\040"
  2539. {
  2540. goto page1_init_failed;
  2541. }
  2542. pageSize = (u32)( ( page1[16] << 8 ) | ( page1[17] << 16 ) );
  2543. if ( ( ( pageSize - 1 ) & pageSize ) != 0
  2544. || pageSize > SQLITE_MAX_PAGE_SIZE
  2545. || pageSize <= 256
  2546. )
  2547. {
  2548. goto page1_init_failed;
  2549. }
  2550. Debug.Assert( ( pageSize & 7 ) == 0 );
  2551. usableSize = pageSize - page1[20];
  2552. if ( pageSize != pBt.pageSize )
  2553. {
  2554. /* After reading the first page of the database assuming a page size
  2555. ** of BtShared.pageSize, we have discovered that the page-size is
  2556. ** actually pageSize. Unlock the database, leave pBt.pPage1 at
  2557. ** zero and return SQLITE_OK. The caller will call this function
  2558. ** again with the correct page-size.
  2559. */
  2560. releasePage( pPage1 );
  2561. pBt.usableSize = usableSize;
  2562. pBt.pageSize = pageSize;
  2563. // freeTempSpace(pBt);
  2564. rc = sqlite3PagerSetPagesize( pBt.pPager, ref pBt.pageSize,
  2565. (int)( pageSize - usableSize ) );
  2566. return rc;
  2567. }
  2568. if ( ( pBt.db.flags & SQLITE_RecoveryMode ) == 0 && nPage > nPageFile )
  2569. {
  2570. rc = SQLITE_CORRUPT_BKPT();
  2571. goto page1_init_failed;
  2572. }
  2573. if ( usableSize < 480 )
  2574. {
  2575. goto page1_init_failed;
  2576. }
  2577. pBt.pageSize = pageSize;
  2578. pBt.usableSize = usableSize;
  2579. #if !SQLITE_OMIT_AUTOVACUUM
  2580. pBt.autoVacuum = ( sqlite3Get4byte( page1, 36 + 4 * 4 ) != 0 );
  2581. pBt.incrVacuum = ( sqlite3Get4byte( page1, 36 + 7 * 4 ) != 0 );
  2582. #endif
  2583. }
  2584. /* maxLocal is the maximum amount of payload to store locally for
  2585. ** a cell. Make sure it is small enough so that at least minFanout
  2586. ** cells can will fit on one page. We assume a 10-byte page header.
  2587. ** Besides the payload, the cell must store:
  2588. ** 2-byte pointer to the cell
  2589. ** 4-byte child pointer
  2590. ** 9-byte nKey value
  2591. ** 4-byte nData value
  2592. ** 4-byte overflow page pointer
  2593. ** So a cell consists of a 2-byte pointer, a header which is as much as
  2594. ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
  2595. ** page pointer.
  2596. */
  2597. pBt.maxLocal = (u16)( ( pBt.usableSize - 12 ) * 64 / 255 - 23 );
  2598. pBt.minLocal = (u16)( ( pBt.usableSize - 12 ) * 32 / 255 - 23 );
  2599. pBt.maxLeaf = (u16)( pBt.usableSize - 35 );
  2600. pBt.minLeaf = (u16)( ( pBt.usableSize - 12 ) * 32 / 255 - 23 );
  2601. Debug.Assert( pBt.maxLeaf + 23 <= MX_CELL_SIZE( pBt ) );
  2602. pBt.pPage1 = pPage1;
  2603. pBt.nPage = nPage;
  2604. return SQLITE_OK;
  2605. page1_init_failed:
  2606. releasePage( pPage1 );
  2607. pBt.pPage1 = null;
  2608. return rc;
  2609. }
  2610. /*
  2611. ** If there are no outstanding cursors and we are not in the middle
  2612. ** of a transaction but there is a read lock on the database, then
  2613. ** this routine unrefs the first page of the database file which
  2614. ** has the effect of releasing the read lock.
  2615. **
  2616. ** If there is a transaction in progress, this routine is a no-op.
  2617. */
  2618. static void unlockBtreeIfUnused( BtShared pBt )
  2619. {
  2620. Debug.Assert( sqlite3_mutex_held( pBt.mutex ) );
  2621. Debug.Assert( pBt.pCursor == null || pBt.inTransaction > TRANS_NONE );
  2622. if ( pBt.inTransaction == TRANS_NONE && pBt.pPage1 != null )
  2623. {
  2624. Debug.Assert( pBt.pPage1.aData != null );
  2625. Debug.Assert( sqlite3PagerRefcount( pBt.pPager ) == 1 );
  2626. Debug.Assert( pBt.pPage1.aData != null );
  2627. releasePage( pBt.pPage1 );
  2628. pBt.pPage1 = null;
  2629. }
  2630. }
  2631. /*
  2632. ** If pBt points to an empty file then convert that empty file
  2633. ** into a new empty database by initializing the first page of
  2634. ** the database.
  2635. */
  2636. static int newDatabase( BtShared pBt )
  2637. {
  2638. MemPage pP1;
  2639. byte[] data;
  2640. int rc;
  2641. Debug.Assert( sqlite3_mutex_held( pBt.mutex ) );
  2642. if ( pBt.nPage > 0 )
  2643. {
  2644. return SQLITE_OK;
  2645. }
  2646. pP1 = pBt.pPage1;
  2647. Debug.Assert( pP1 != null );
  2648. data = pP1.aData;
  2649. rc = sqlite3PagerWrite( pP1.pDbPage );
  2650. if ( rc != 0 )
  2651. return rc;
  2652. Buffer.BlockCopy( zMagicHeader, 0, data, 0, 16 );// memcpy(data, zMagicHeader, sizeof(zMagicHeader));
  2653. Debug.Assert( zMagicHeader.Length == 16 );
  2654. data[16] = (u8)( ( pBt.pageSize >> 8 ) & 0xff );
  2655. data[17] = (u8)( ( pBt.pageSize >> 16 ) & 0xff );
  2656. data[18] = 1;
  2657. data[19] = 1;
  2658. Debug.Assert( pBt.usableSize <= pBt.pageSize && pBt.usableSize + 255 >= pBt.pageSize );
  2659. data[20] = (u8)( pBt.pageSize - pBt.usableSize );
  2660. data[21] = 64;
  2661. data[22] = 32;
  2662. data[23] = 32;
  2663. //memset(&data[24], 0, 100-24);
  2664. zeroPage( pP1, PTF_INTKEY | PTF_LEAF | PTF_LEAFDATA );
  2665. pBt.pageSizeFixed = true;
  2666. #if !SQLITE_OMIT_AUTOVACUUM
  2667. Debug.Assert( pBt.autoVacuum == true || pBt.autoVacuum == false );
  2668. Debug.Assert( pBt.incrVacuum == true || pBt.incrVacuum == false );
  2669. sqlite3Put4byte( data, 36 + 4 * 4, pBt.autoVacuum ? 1 : 0 );
  2670. sqlite3Put4byte( data, 36 + 7 * 4, pBt.incrVacuum ? 1 : 0 );
  2671. #endif
  2672. pBt.nPage = 1;
  2673. data[31] = 1;
  2674. return SQLITE_OK;
  2675. }
  2676. /*
  2677. ** Attempt to start a new transaction. A write-transaction
  2678. ** is started if the second argument is nonzero, otherwise a read-
  2679. ** transaction. If the second argument is 2 or more and exclusive
  2680. ** transaction is started, meaning that no other process is allowed
  2681. ** to access the database. A preexisting transaction may not be
  2682. ** upgraded to exclusive by calling this routine a second time - the
  2683. ** exclusivity flag only works for a new transaction.
  2684. **
  2685. ** A write-transaction must be started before attempting any
  2686. ** changes to the database. None of the following routines
  2687. ** will work unless a transaction is started first:
  2688. **
  2689. ** sqlite3BtreeCreateTable()
  2690. ** sqlite3BtreeCreateIndex()
  2691. ** sqlite3BtreeClearTable()
  2692. ** sqlite3BtreeDropTable()
  2693. ** sqlite3BtreeInsert()
  2694. ** sqlite3BtreeDelete()
  2695. ** sqlite3BtreeUpdateMeta()
  2696. **
  2697. ** If an initial attempt to acquire the lock fails because of lock contention
  2698. ** and the database was previously unlocked, then invoke the busy handler
  2699. ** if there is one. But if there was previously a read-lock, do not
  2700. ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
  2701. ** returned when there is already a read-lock in order to avoid a deadlock.
  2702. **
  2703. ** Suppose there are two processes A and B. A has a read lock and B has
  2704. ** a reserved lock. B tries to promote to exclusive but is blocked because
  2705. ** of A's read lock. A tries to promote to reserved but is blocked by B.
  2706. ** One or the other of the two processes must give way or there can be
  2707. ** no progress. By returning SQLITE_BUSY and not invoking the busy callback
  2708. ** when A already has a read lock, we encourage A to give up and let B
  2709. ** proceed.
  2710. */
  2711. static int sqlite3BtreeBeginTrans( Btree p, int wrflag )
  2712. {
  2713. BtShared pBt = p.pBt;
  2714. int rc = SQLITE_OK;
  2715. sqlite3BtreeEnter( p );
  2716. btreeIntegrity( p );
  2717. /* If the btree is already in a write-transaction, or it
  2718. ** is already in a read-transaction and a read-transaction
  2719. ** is requested, this is a no-op.
  2720. */
  2721. if ( p.inTrans == TRANS_WRITE || ( p.inTrans == TRANS_READ && 0 == wrflag ) )
  2722. {
  2723. goto trans_begun;
  2724. }
  2725. /* Write transactions are not possible on a read-only database */
  2726. if ( pBt.readOnly && wrflag != 0 )
  2727. {
  2728. rc = SQLITE_READONLY;
  2729. goto trans_begun;
  2730. }
  2731. #if !SQLITE_OMIT_SHARED_CACHE
  2732. /* If another database handle has already opened a write transaction
  2733. ** on this shared-btree structure and a second write transaction is
  2734. ** requested, return SQLITE_LOCKED.
  2735. */
  2736. if( (wrflag && pBt.inTransaction==TRANS_WRITE) || pBt.isPending ){
  2737. sqlite3 pBlock = pBt.pWriter.db;
  2738. }else if( wrflag>1 ){
  2739. BtLock pIter;
  2740. for(pIter=pBt.pLock; pIter; pIter=pIter.pNext){
  2741. if( pIter.pBtree!=p ){
  2742. pBlock = pIter.pBtree.db;
  2743. break;
  2744. }
  2745. }
  2746. }
  2747. if( pBlock ){
  2748. sqlite3ConnectionBlocked(p.db, pBlock);
  2749. rc = SQLITE_LOCKED_SHAREDCACHE;
  2750. goto trans_begun;
  2751. }
  2752. #endif
  2753. /* Any read-only or read-write transaction implies a read-lock on
  2754. ** page 1. So if some other shared-cache client already has a write-lock
  2755. ** on page 1, the transaction cannot be opened. */
  2756. rc = querySharedCacheTableLock( p, MASTER_ROOT, READ_LOCK );
  2757. if ( SQLITE_OK != rc )
  2758. goto trans_begun;
  2759. pBt.initiallyEmpty = pBt.nPage == 0;
  2760. do
  2761. {
  2762. /* Call lockBtree() until either pBt.pPage1 is populated or
  2763. ** lockBtree() returns something other than SQLITE_OK. lockBtree()
  2764. ** may return SQLITE_OK but leave pBt.pPage1 set to 0 if after
  2765. ** reading page 1 it discovers that the page-size of the database
  2766. ** file is not pBt.pageSize. In this case lockBtree() will update
  2767. ** pBt.pageSize to the page-size of the file on disk.
  2768. */
  2769. while ( pBt.pPage1 == null && SQLITE_OK == ( rc = lockBtree( pBt ) ) )
  2770. ;
  2771. if ( rc == SQLITE_OK && wrflag != 0 )
  2772. {
  2773. if ( pBt.readOnly )
  2774. {
  2775. rc = SQLITE_READONLY;
  2776. }
  2777. else
  2778. {
  2779. rc = sqlite3PagerBegin( pBt.pPager, wrflag > 1, sqlite3TempInMemory( p.db ) ? 1 : 0 );
  2780. if ( rc == SQLITE_OK )
  2781. {
  2782. rc = newDatabase( pBt );
  2783. }
  2784. }
  2785. }
  2786. if ( rc != SQLITE_OK )
  2787. {
  2788. unlockBtreeIfUnused( pBt );
  2789. }
  2790. } while ( ( rc & 0xFF ) == SQLITE_BUSY && pBt.inTransaction == TRANS_NONE &&
  2791. btreeInvokeBusyHandler( pBt ) != 0 );
  2792. if ( rc == SQLITE_OK )
  2793. {
  2794. if ( p.inTrans == TRANS_NONE )
  2795. {
  2796. pBt.nTransaction++;
  2797. #if !SQLITE_OMIT_SHARED_CACHE
  2798. if( p.sharable ){
  2799. Debug.Assert( p.lock.pBtree==p && p.lock.iTable==1 );
  2800. p.lock.eLock = READ_LOCK;
  2801. p.lock.pNext = pBt.pLock;
  2802. pBt.pLock = &p.lock;
  2803. }
  2804. #endif
  2805. }
  2806. p.inTrans = ( wrflag != 0 ? TRANS_WRITE : TRANS_READ );
  2807. if ( p.inTrans > pBt.inTransaction )
  2808. {
  2809. pBt.inTransaction = p.inTrans;
  2810. }
  2811. if ( wrflag != 0 )
  2812. {
  2813. MemPage pPage1 = pBt.pPage1;
  2814. #if !SQLITE_OMIT_SHARED_CACHE
  2815. Debug.Assert( !pBt.pWriter );
  2816. pBt.pWriter = p;
  2817. pBt.isExclusive = (u8)(wrflag>1);
  2818. #endif
  2819. /* If the db-size header field is incorrect (as it may be if an old
  2820. ** client has been writing the database file), update it now. Doing
  2821. ** this sooner rather than later means the database size can safely
  2822. ** re-read the database size from page 1 if a savepoint or transaction
  2823. ** rollback occurs within the transaction.
  2824. */
  2825. if ( pBt.nPage != sqlite3Get4byte( pPage1.aData, 28 ) )
  2826. {
  2827. rc = sqlite3PagerWrite( pPage1.pDbPage );
  2828. if ( rc == SQLITE_OK )
  2829. {
  2830. sqlite3Put4byte( pPage1.aData, (u32)28, pBt.nPage );
  2831. }
  2832. }
  2833. }
  2834. }
  2835. trans_begun:
  2836. if ( rc == SQLITE_OK && wrflag != 0 )
  2837. {
  2838. /* This call makes sure that the pager has the correct number of
  2839. ** open savepoints. If the second parameter is greater than 0 and
  2840. ** the sub-journal is not already open, then it will be opened here.
  2841. */
  2842. rc = sqlite3PagerOpenSavepoint( pBt.pPager, p.db.nSavepoint );
  2843. }
  2844. btreeIntegrity( p );
  2845. sqlite3BtreeLeave( p );
  2846. return rc;
  2847. }
  2848. #if !SQLITE_OMIT_AUTOVACUUM
  2849. /*
  2850. ** Set the pointer-map entries for all children of page pPage. Also, if
  2851. ** pPage contains cells that point to overflow pages, set the pointer
  2852. ** map entries for the overflow pages as well.
  2853. */
  2854. static int setChildPtrmaps( MemPage pPage )
  2855. {
  2856. int i; /* Counter variable */
  2857. int nCell; /* Number of cells in page pPage */
  2858. int rc; /* Return code */
  2859. BtShared pBt = pPage.pBt;
  2860. u8 isInitOrig = pPage.isInit;
  2861. Pgno pgno = pPage.pgno;
  2862. Debug.Assert( sqlite3_mutex_held( pPage.pBt.mutex ) );
  2863. rc = btreeInitPage( pPage );
  2864. if ( rc != SQLITE_OK )
  2865. {
  2866. goto set_child_ptrmaps_out;
  2867. }
  2868. nCell = pPage.nCell;
  2869. for ( i = 0; i < nCell; i++ )
  2870. {
  2871. int pCell = findCell( pPage, i );
  2872. ptrmapPutOvflPtr( pPage, pCell, ref rc );
  2873. if ( 0 == pPage.leaf )
  2874. {
  2875. Pgno childPgno = sqlite3Get4byte( pPage.aData, pCell );
  2876. ptrmapPut( pBt, childPgno, PTRMAP_BTREE, pgno, ref rc );
  2877. }
  2878. }
  2879. if ( 0 == pPage.leaf )
  2880. {
  2881. Pgno childPgno = sqlite3Get4byte( pPage.aData, pPage.hdrOffset + 8 );
  2882. ptrmapPut( pBt, childPgno, PTRMAP_BTREE, pgno, ref rc );
  2883. }
  2884. set_child_ptrmaps_out:
  2885. pPage.isInit = isInitOrig;
  2886. return rc;
  2887. }
  2888. /*
  2889. ** Somewhere on pPage is a pointer to page iFrom. Modify this pointer so
  2890. ** that it points to iTo. Parameter eType describes the type of pointer to
  2891. ** be modified, as follows:
  2892. **
  2893. ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
  2894. ** page of pPage.
  2895. **
  2896. ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
  2897. ** page pointed to by one of the cells on pPage.
  2898. **
  2899. ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
  2900. ** overflow page in the list.
  2901. */
  2902. static int modifyPagePointer( MemPage pPage, Pgno iFrom, Pgno iTo, u8 eType )
  2903. {
  2904. Debug.Assert( sqlite3_mutex_held( pPage.pBt.mutex ) );
  2905. Debug.Assert( sqlite3PagerIswriteable( pPage.pDbPage ) );
  2906. if ( eType == PTRMAP_OVERFLOW2 )
  2907. {
  2908. /* The pointer is always the first 4 bytes of the page in this case. */
  2909. if ( sqlite3Get4byte( pPage.aData ) != iFrom )
  2910. {
  2911. return SQLITE_CORRUPT_BKPT();
  2912. }
  2913. sqlite3Put4byte( pPage.aData, iTo );
  2914. }
  2915. else
  2916. {
  2917. u8 isInitOrig = pPage.isInit;
  2918. int i;
  2919. int nCell;
  2920. btreeInitPage( pPage );
  2921. nCell = pPage.nCell;
  2922. for ( i = 0; i < nCell; i++ )
  2923. {
  2924. int pCell = findCell( pPage, i );
  2925. if ( eType == PTRMAP_OVERFLOW1 )
  2926. {
  2927. CellInfo info = new CellInfo();
  2928. btreeParseCellPtr( pPage, pCell, ref info );
  2929. if ( info.iOverflow != 0 )
  2930. {
  2931. if ( iFrom == sqlite3Get4byte( pPage.aData, pCell, info.iOverflow ) )
  2932. {
  2933. sqlite3Put4byte( pPage.aData, pCell + info.iOverflow, (int)iTo );
  2934. break;
  2935. }
  2936. }
  2937. }
  2938. else
  2939. {
  2940. if ( sqlite3Get4byte( pPage.aData, pCell ) == iFrom )
  2941. {
  2942. sqlite3Put4byte( pPage.aData, pCell, (int)iTo );
  2943. break;
  2944. }
  2945. }
  2946. }
  2947. if ( i == nCell )
  2948. {
  2949. if ( eType != PTRMAP_BTREE ||
  2950. sqlite3Get4byte( pPage.aData, pPage.hdrOffset + 8 ) != iFrom )
  2951. {
  2952. return SQLITE_CORRUPT_BKPT();
  2953. }
  2954. sqlite3Put4byte( pPage.aData, pPage.hdrOffset + 8, iTo );
  2955. }
  2956. pPage.isInit = isInitOrig;
  2957. }
  2958. return SQLITE_OK;
  2959. }
  2960. /*
  2961. ** Move the open database page pDbPage to location iFreePage in the
  2962. ** database. The pDbPage reference remains valid.
  2963. **
  2964. ** The isCommit flag indicates that there is no need to remember that
  2965. ** the journal needs to be sync()ed before database page pDbPage.pgno
  2966. ** can be written to. The caller has already promised not to write to that
  2967. ** page.
  2968. */
  2969. static int relocatePage(
  2970. BtShared pBt, /* Btree */
  2971. MemPage pDbPage, /* Open page to move */
  2972. u8 eType, /* Pointer map 'type' entry for pDbPage */
  2973. Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
  2974. Pgno iFreePage, /* The location to move pDbPage to */
  2975. int isCommit /* isCommit flag passed to sqlite3PagerMovepage */
  2976. )
  2977. {
  2978. MemPage pPtrPage = new MemPage(); /* The page that contains a pointer to pDbPage */
  2979. Pgno iDbPage = pDbPage.pgno;
  2980. Pager pPager = pBt.pPager;
  2981. int rc;
  2982. Debug.Assert( eType == PTRMAP_OVERFLOW2 || eType == PTRMAP_OVERFLOW1 ||
  2983. eType == PTRMAP_BTREE || eType == PTRMAP_ROOTPAGE );
  2984. Debug.Assert( sqlite3_mutex_held( pBt.mutex ) );
  2985. Debug.Assert( pDbPage.pBt == pBt );
  2986. /* Move page iDbPage from its current location to page number iFreePage */
  2987. TRACE( "AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n",
  2988. iDbPage, iFreePage, iPtrPage, eType );
  2989. rc = sqlite3PagerMovepage( pPager, pDbPage.pDbPage, iFreePage, isCommit );
  2990. if ( rc != SQLITE_OK )
  2991. {
  2992. return rc;
  2993. }
  2994. pDbPage.pgno = iFreePage;
  2995. /* If pDbPage was a btree-page, then it may have child pages and/or cells
  2996. ** that point to overflow pages. The pointer map entries for all these
  2997. ** pages need to be changed.
  2998. **
  2999. ** If pDbPage is an overflow page, then the first 4 bytes may store a
  3000. ** pointer to a subsequent overflow page. If this is the case, then
  3001. ** the pointer map needs to be updated for the subsequent overflow page.
  3002. */
  3003. if ( eType == PTRMAP_BTREE || eType == PTRMAP_ROOTPAGE )
  3004. {
  3005. rc = setChildPtrmaps( pDbPage );
  3006. if ( rc != SQLITE_OK )
  3007. {
  3008. return rc;
  3009. }
  3010. }
  3011. else
  3012. {
  3013. Pgno nextOvfl = sqlite3Get4byte( pDbPage.aData );
  3014. if ( nextOvfl != 0 )
  3015. {
  3016. ptrmapPut( pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage, ref rc );
  3017. if ( rc != SQLITE_OK )
  3018. {
  3019. return rc;
  3020. }
  3021. }
  3022. }
  3023. /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
  3024. ** that it points at iFreePage. Also fix the pointer map entry for
  3025. ** iPtrPage.
  3026. */
  3027. if ( eType != PTRMAP_ROOTPAGE )
  3028. {
  3029. rc = btreeGetPage( pBt, iPtrPage, ref pPtrPage, 0 );
  3030. if ( rc != SQLITE_OK )
  3031. {
  3032. return rc;
  3033. }
  3034. rc = sqlite3PagerWrite( pPtrPage.pDbPage );
  3035. if ( rc != SQLITE_OK )
  3036. {
  3037. releasePage( pPtrPage );
  3038. return rc;
  3039. }
  3040. rc = modifyPagePointer( pPtrPage, iDbPage, iFreePage, eType );
  3041. releasePage( pPtrPage );
  3042. if ( rc == SQLITE_OK )
  3043. {
  3044. ptrmapPut( pBt, iFreePage, eType, iPtrPage, ref rc );
  3045. }
  3046. }
  3047. return rc;
  3048. }
  3049. /* Forward declaration required by incrVacuumStep(). */
  3050. //static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
  3051. /*
  3052. ** Perform a single step of an incremental-vacuum. If successful,
  3053. ** return SQLITE_OK. If there is no work to do (and therefore no
  3054. ** point in calling this function again), return SQLITE_DONE.
  3055. **
  3056. ** More specificly, this function attempts to re-organize the
  3057. ** database so that the last page of the file currently in use
  3058. ** is no longer in use.
  3059. **
  3060. ** If the nFin parameter is non-zero, this function assumes
  3061. ** that the caller will keep calling incrVacuumStep() until
  3062. ** it returns SQLITE_DONE or an error, and that nFin is the
  3063. ** number of pages the database file will contain after this
  3064. ** process is complete. If nFin is zero, it is assumed that
  3065. ** incrVacuumStep() will be called a finite amount of times
  3066. ** which may or may not empty the freelist. A full autovacuum
  3067. ** has nFin>0. A "PRAGMA incremental_vacuum" has nFin==null.
  3068. */
  3069. static int incrVacuumStep( BtShared pBt, Pgno nFin, Pgno iLastPg )
  3070. {
  3071. Pgno nFreeList; /* Number of pages still on the free-list */
  3072. int rc;
  3073. Debug.Assert( sqlite3_mutex_held( pBt.mutex ) );
  3074. Debug.Assert( iLastPg > nFin );
  3075. if ( !PTRMAP_ISPAGE( pBt, iLastPg ) && iLastPg != PENDING_BYTE_PAGE( pBt ) )
  3076. {
  3077. u8 eType = 0;
  3078. Pgno iPtrPage = 0;
  3079. nFreeList = sqlite3Get4byte( pBt.pPage1.aData, 36 );
  3080. if ( nFreeList == 0 )
  3081. {
  3082. return SQLITE_DONE;
  3083. }
  3084. rc = ptrmapGet( pBt, iLastPg, ref eType, ref iPtrPage );
  3085. if ( rc != SQLITE_OK )
  3086. {
  3087. return rc;
  3088. }
  3089. if ( eType == PTRMAP_ROOTPAGE )
  3090. {
  3091. return SQLITE_CORRUPT_BKPT();
  3092. }
  3093. if ( eType == PTRMAP_FREEPAGE )
  3094. {
  3095. if ( nFin == 0 )
  3096. {
  3097. /* Remove the page from the files free-list. This is not required
  3098. ** if nFin is non-zero. In that case, the free-list will be
  3099. ** truncated to zero after this function returns, so it doesn't
  3100. ** matter if it still contains some garbage entries.
  3101. */
  3102. Pgno iFreePg = 0;
  3103. MemPage pFreePg = new MemPage();
  3104. rc = allocateBtreePage( pBt, ref pFreePg, ref iFreePg, iLastPg, 1 );
  3105. if ( rc != SQLITE_OK )
  3106. {
  3107. return rc;
  3108. }
  3109. Debug.Assert( iFreePg == iLastPg );
  3110. releasePage( pFreePg );
  3111. }
  3112. }
  3113. else
  3114. {
  3115. Pgno iFreePg = 0; /* Index of free page to move pLastPg to */
  3116. MemPage pLastPg = new MemPage();
  3117. rc = btreeGetPage( pBt, iLastPg, ref pLastPg, 0 );
  3118. if ( rc != SQLITE_OK )
  3119. {
  3120. return rc;
  3121. }
  3122. /* If nFin is zero, this loop runs exactly once and page pLastPg
  3123. ** is swapped with the first free page pulled off the free list.
  3124. **
  3125. ** On the other hand, if nFin is greater than zero, then keep
  3126. ** looping until a free-page located within the first nFin pages
  3127. ** of the file is found.
  3128. */
  3129. do
  3130. {
  3131. MemPage pFreePg = new MemPage();
  3132. rc = allocateBtreePage( pBt, ref pFreePg, ref iFreePg, 0, 0 );
  3133. if ( rc != SQLITE_OK )
  3134. {
  3135. releasePage( pLastPg );
  3136. return rc;
  3137. }
  3138. releasePage( pFreePg );
  3139. } while ( nFin != 0 && iFreePg > nFin );
  3140. Debug.Assert( iFreePg < iLastPg );
  3141. rc = sqlite3PagerWrite( pLastPg.pDbPage );
  3142. if ( rc == SQLITE_OK )
  3143. {
  3144. rc = relocatePage( pBt, pLastPg, eType, iPtrPage, iFreePg, ( nFin != 0 ) ? 1 : 0 );
  3145. }
  3146. releasePage( pLastPg );
  3147. if ( rc != SQLITE_OK )
  3148. {
  3149. return rc;
  3150. }
  3151. }
  3152. }
  3153. if ( nFin == 0 )
  3154. {
  3155. iLastPg--;
  3156. while ( iLastPg == PENDING_BYTE_PAGE( pBt ) || PTRMAP_ISPAGE( pBt, iLastPg ) )
  3157. {
  3158. if ( PTRMAP_ISPAGE( pBt, iLastPg ) )
  3159. {
  3160. MemPage pPg = new MemPage();
  3161. rc = btreeGetPage( pBt, iLastPg, ref pPg, 0 );
  3162. if ( rc != SQLITE_OK )
  3163. {
  3164. return rc;
  3165. }
  3166. rc = sqlite3PagerWrite( pPg.pDbPage );
  3167. releasePage( pPg );
  3168. if ( rc != SQLITE_OK )
  3169. {
  3170. return rc;
  3171. }
  3172. }
  3173. iLastPg--;
  3174. }
  3175. sqlite3PagerTruncateImage( pBt.pPager, iLastPg );
  3176. pBt.nPage = iLastPg;
  3177. }
  3178. return SQLITE_OK;
  3179. }
  3180. /*
  3181. ** A write-transaction must be opened before calling this function.
  3182. ** It performs a single unit of work towards an incremental vacuum.
  3183. **
  3184. ** If the incremental vacuum is finished after this function has run,
  3185. ** SQLITE_DONE is returned. If it is not finished, but no error occurred,
  3186. ** SQLITE_OK is returned. Otherwise an SQLite error code.
  3187. */
  3188. static int sqlite3BtreeIncrVacuum( Btree p )
  3189. {
  3190. int rc;
  3191. BtShared pBt = p.pBt;
  3192. sqlite3BtreeEnter( p );
  3193. Debug.Assert( pBt.inTransaction == TRANS_WRITE && p.inTrans == TRANS_WRITE );
  3194. if ( !pBt.autoVacuum )
  3195. {
  3196. rc = SQLITE_DONE;
  3197. }
  3198. else
  3199. {
  3200. invalidateAllOverflowCache( pBt );
  3201. rc = incrVacuumStep( pBt, 0, btreePagecount( pBt ) );
  3202. if ( rc == SQLITE_OK )
  3203. {
  3204. rc = sqlite3PagerWrite( pBt.pPage1.pDbPage );
  3205. sqlite3Put4byte( pBt.pPage1.aData, (u32)28, pBt.nPage );//put4byte(&pBt->pPage1->aData[28], pBt->nPage);
  3206. }
  3207. }
  3208. sqlite3BtreeLeave( p );
  3209. return rc;
  3210. }
  3211. /*
  3212. ** This routine is called prior to sqlite3PagerCommit when a transaction
  3213. ** is commited for an auto-vacuum database.
  3214. **
  3215. ** If SQLITE_OK is returned, then pnTrunc is set to the number of pages
  3216. ** the database file should be truncated to during the commit process.
  3217. ** i.e. the database has been reorganized so that only the first pnTrunc
  3218. ** pages are in use.
  3219. */
  3220. static int autoVacuumCommit( BtShared pBt )
  3221. {
  3222. int rc = SQLITE_OK;
  3223. Pager pPager = pBt.pPager;
  3224. // VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager) );
  3225. #if !NDEBUG || DEBUG
  3226. int nRef = sqlite3PagerRefcount( pPager );
  3227. #else
  3228. int nRef=0;
  3229. #endif
  3230. Debug.Assert( sqlite3_mutex_held( pBt.mutex ) );
  3231. invalidateAllOverflowCache( pBt );
  3232. Debug.Assert( pBt.autoVacuum );
  3233. if ( !pBt.incrVacuum )
  3234. {
  3235. Pgno nFin; /* Number of pages in database after autovacuuming */
  3236. Pgno nFree; /* Number of pages on the freelist initially */
  3237. Pgno nPtrmap; /* Number of PtrMap pages to be freed */
  3238. Pgno iFree; /* The next page to be freed */
  3239. int nEntry; /* Number of entries on one ptrmap page */
  3240. Pgno nOrig; /* Database size before freeing */
  3241. nOrig = btreePagecount( pBt );
  3242. if ( PTRMAP_ISPAGE( pBt, nOrig ) || nOrig == PENDING_BYTE_PAGE( pBt ) )
  3243. {
  3244. /* It is not possible to create a database for which the final page
  3245. ** is either a pointer-map page or the pending-byte page. If one
  3246. ** is encountered, this indicates corruption.
  3247. */
  3248. return SQLITE_CORRUPT_BKPT();
  3249. }
  3250. nFree = sqlite3Get4byte( pBt.pPage1.aData, 36 );
  3251. nEntry = (int)pBt.usableSize / 5;
  3252. nPtrmap = (Pgno)( ( nFree - nOrig + PTRMAP_PAGENO( pBt, nOrig ) + (Pgno)nEntry ) / nEntry );
  3253. nFin = nOrig - nFree - nPtrmap;
  3254. if ( nOrig > PENDING_BYTE_PAGE( pBt ) && nFin < PENDING_BYTE_PAGE( pBt ) )
  3255. {
  3256. nFin--;
  3257. }
  3258. while ( PTRMAP_ISPAGE( pBt, nFin ) || nFin == PENDING_BYTE_PAGE( pBt ) )
  3259. {
  3260. nFin--;
  3261. }
  3262. if ( nFin > nOrig )
  3263. return SQLITE_CORRUPT_BKPT();
  3264. for ( iFree = nOrig; iFree > nFin && rc == SQLITE_OK; iFree-- )
  3265. {
  3266. rc = incrVacuumStep( pBt, nFin, iFree );
  3267. }
  3268. if ( ( rc == SQLITE_DONE || rc == SQLITE_OK ) && nFree > 0 )
  3269. {
  3270. rc = sqlite3PagerWrite( pBt.pPage1.pDbPage );
  3271. sqlite3Put4byte( pBt.pPage1.aData, 32, 0 );
  3272. sqlite3Put4byte( pBt.pPage1.aData, 36, 0 );
  3273. sqlite3Put4byte( pBt.pPage1.aData, (u32)28, nFin );
  3274. sqlite3PagerTruncateImage( pBt.pPager, nFin );
  3275. pBt.nPage = nFin;
  3276. }
  3277. if ( rc != SQLITE_OK )
  3278. {
  3279. sqlite3PagerRollback( pPager );
  3280. }
  3281. }
  3282. Debug.Assert( nRef == sqlite3PagerRefcount( pPager ) );
  3283. return rc;
  3284. }
  3285. #else //* ifndef SQLITE_OMIT_AUTOVACUUM */
  3286. //# define setChildPtrmaps(x) SQLITE_OK
  3287. #endif
  3288. /*
  3289. ** This routine does the first phase of a two-phase commit. This routine
  3290. ** causes a rollback journal to be created (if it does not already exist)
  3291. ** and populated with enough information so that if a power loss occurs
  3292. ** the database can be restored to its original state by playing back
  3293. ** the journal. Then the contents of the journal are flushed out to
  3294. ** the disk. After the journal is safely on oxide, the changes to the
  3295. ** database are written into the database file and flushed to oxide.
  3296. ** At the end of this call, the rollback journal still exists on the
  3297. ** disk and we are still holding all locks, so the transaction has not
  3298. ** committed. See sqlite3BtreeCommitPhaseTwo() for the second phase of the
  3299. ** commit process.
  3300. **
  3301. ** This call is a no-op if no write-transaction is currently active on pBt.
  3302. **
  3303. ** Otherwise, sync the database file for the btree pBt. zMaster points to
  3304. ** the name of a master journal file that should be written into the
  3305. ** individual journal file, or is NULL, indicating no master journal file
  3306. ** (single database transaction).
  3307. **
  3308. ** When this is called, the master journal should already have been
  3309. ** created, populated with this journal pointer and synced to disk.
  3310. **
  3311. ** Once this is routine has returned, the only thing required to commit
  3312. ** the write-transaction for this database file is to delete the journal.
  3313. */
  3314. static int sqlite3BtreeCommitPhaseOne( Btree p, string zMaster )
  3315. {
  3316. int rc = SQLITE_OK;
  3317. if ( p.inTrans == TRANS_WRITE )
  3318. {
  3319. BtShared pBt = p.pBt;
  3320. sqlite3BtreeEnter( p );
  3321. #if !SQLITE_OMIT_AUTOVACUUM
  3322. if ( pBt.autoVacuum )
  3323. {
  3324. rc = autoVacuumCommit( pBt );
  3325. if ( rc != SQLITE_OK )
  3326. {
  3327. sqlite3BtreeLeave( p );
  3328. return rc;
  3329. }
  3330. }
  3331. #endif
  3332. rc = sqlite3PagerCommitPhaseOne( pBt.pPager, zMaster, false );
  3333. sqlite3BtreeLeave( p );
  3334. }
  3335. return rc;
  3336. }
  3337. /*
  3338. ** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback()
  3339. ** at the conclusion of a transaction.
  3340. */
  3341. static void btreeEndTransaction( Btree p )
  3342. {
  3343. BtShared pBt = p.pBt;
  3344. Debug.Assert( sqlite3BtreeHoldsMutex( p ) );
  3345. btreeClearHasContent( pBt );
  3346. if ( p.inTrans > TRANS_NONE && p.db.activeVdbeCnt > 1 )
  3347. {
  3348. /* If there are other active statements that belong to this database
  3349. ** handle, downgrade to a read-only transaction. The other statements
  3350. ** may still be reading from the database. */
  3351. downgradeAllSharedCacheTableLocks( p );
  3352. p.inTrans = TRANS_READ;
  3353. }
  3354. else
  3355. {
  3356. /* If the handle had any kind of transaction open, decrement the
  3357. ** transaction count of the shared btree. If the transaction count
  3358. ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused()
  3359. ** call below will unlock the pager. */
  3360. if ( p.inTrans != TRANS_NONE )
  3361. {
  3362. clearAllSharedCacheTableLocks( p );
  3363. pBt.nTransaction--;
  3364. if ( 0 == pBt.nTransaction )
  3365. {
  3366. pBt.inTransaction = TRANS_NONE;
  3367. }
  3368. }
  3369. /* Set the current transaction state to TRANS_NONE and unlock the
  3370. ** pager if this call closed the only read or write transaction. */
  3371. p.inTrans = TRANS_NONE;
  3372. unlockBtreeIfUnused( pBt );
  3373. }
  3374. btreeIntegrity( p );
  3375. }
  3376. /*
  3377. ** Commit the transaction currently in progress.
  3378. **
  3379. ** This routine implements the second phase of a 2-phase commit. The
  3380. ** sqlite3BtreeCommitPhaseOne() routine does the first phase and should
  3381. ** be invoked prior to calling this routine. The sqlite3BtreeCommitPhaseOne()
  3382. ** routine did all the work of writing information out to disk and flushing the
  3383. ** contents so that they are written onto the disk platter. All this
  3384. ** routine has to do is delete or truncate or zero the header in the
  3385. ** the rollback journal (which causes the transaction to commit) and
  3386. ** drop locks.
  3387. **
  3388. ** This will release the write lock on the database file. If there
  3389. ** are no active cursors, it also releases the read lock.
  3390. */
  3391. static int sqlite3BtreeCommitPhaseTwo( Btree p )
  3392. {
  3393. if ( p.inTrans == TRANS_NONE )
  3394. return SQLITE_OK;
  3395. sqlite3BtreeEnter( p );
  3396. btreeIntegrity( p );
  3397. /* If the handle has a write-transaction open, commit the shared-btrees
  3398. ** transaction and set the shared state to TRANS_READ.
  3399. */
  3400. if ( p.inTrans == TRANS_WRITE )
  3401. {
  3402. int rc;
  3403. BtShared pBt = p.pBt;
  3404. Debug.Assert( pBt.inTransaction == TRANS_WRITE );
  3405. Debug.Assert( pBt.nTransaction > 0 );
  3406. rc = sqlite3PagerCommitPhaseTwo( pBt.pPager );
  3407. if ( rc != SQLITE_OK )
  3408. {
  3409. sqlite3BtreeLeave( p );
  3410. return rc;
  3411. }
  3412. pBt.inTransaction = TRANS_READ;
  3413. }
  3414. btreeEndTransaction( p );
  3415. sqlite3BtreeLeave( p );
  3416. return SQLITE_OK;
  3417. }
  3418. /*
  3419. ** Do both phases of a commit.
  3420. */
  3421. static int sqlite3BtreeCommit( Btree p )
  3422. {
  3423. int rc;
  3424. sqlite3BtreeEnter( p );
  3425. rc = sqlite3BtreeCommitPhaseOne( p, null );
  3426. if ( rc == SQLITE_OK )
  3427. {
  3428. rc = sqlite3BtreeCommitPhaseTwo( p );
  3429. }
  3430. sqlite3BtreeLeave( p );
  3431. return rc;
  3432. }
  3433. #if !NDEBUG || DEBUG
  3434. /*
  3435. ** Return the number of write-cursors open on this handle. This is for use
  3436. ** in Debug.Assert() expressions, so it is only compiled if NDEBUG is not
  3437. ** defined.
  3438. **
  3439. ** For the purposes of this routine, a write-cursor is any cursor that
  3440. ** is capable of writing to the databse. That means the cursor was
  3441. ** originally opened for writing and the cursor has not be disabled
  3442. ** by having its state changed to CURSOR_FAULT.
  3443. */
  3444. static int countWriteCursors( BtShared pBt )
  3445. {
  3446. BtCursor pCur;
  3447. int r = 0;
  3448. for ( pCur = pBt.pCursor; pCur != null; pCur = pCur.pNext )
  3449. {
  3450. if ( pCur.wrFlag != 0 && pCur.eState != CURSOR_FAULT )
  3451. r++;
  3452. }
  3453. return r;
  3454. }
  3455. #else
  3456. static int countWriteCursors(BtShared pBt) { return -1; }
  3457. #endif
  3458. /*
  3459. ** This routine sets the state to CURSOR_FAULT and the error
  3460. ** code to errCode for every cursor on BtShared that pBtree
  3461. ** references.
  3462. **
  3463. ** Every cursor is tripped, including cursors that belong
  3464. ** to other database connections that happen to be sharing
  3465. ** the cache with pBtree.
  3466. **
  3467. ** This routine gets called when a rollback occurs.
  3468. ** All cursors using the same cache must be tripped
  3469. ** to prevent them from trying to use the btree after
  3470. ** the rollback. The rollback may have deleted tables
  3471. ** or moved root pages, so it is not sufficient to
  3472. ** save the state of the cursor. The cursor must be
  3473. ** invalidated.
  3474. */
  3475. static void sqlite3BtreeTripAllCursors( Btree pBtree, int errCode )
  3476. {
  3477. BtCursor p;
  3478. sqlite3BtreeEnter( pBtree );
  3479. for ( p = pBtree.pBt.pCursor; p != null; p = p.pNext )
  3480. {
  3481. int i;
  3482. sqlite3BtreeClearCursor( p );
  3483. p.eState = CURSOR_FAULT;
  3484. p.skipNext = errCode;
  3485. for ( i = 0; i <= p.iPage; i++ )
  3486. {
  3487. releasePage( p.apPage[i] );
  3488. p.apPage[i] = null;
  3489. }
  3490. }
  3491. sqlite3BtreeLeave( pBtree );
  3492. }
  3493. /*
  3494. ** Rollback the transaction in progress. All cursors will be
  3495. ** invalided by this operation. Any attempt to use a cursor
  3496. ** that was open at the beginning of this operation will result
  3497. ** in an error.
  3498. **
  3499. ** This will release the write lock on the database file. If there
  3500. ** are no active cursors, it also releases the read lock.
  3501. */
  3502. static int sqlite3BtreeRollback( Btree p )
  3503. {
  3504. int rc;
  3505. BtShared pBt = p.pBt;
  3506. MemPage pPage1 = new MemPage();
  3507. sqlite3BtreeEnter( p );
  3508. rc = saveAllCursors( pBt, 0, null );
  3509. #if !SQLITE_OMIT_SHARED_CACHE
  3510. if( rc!=SQLITE_OK ){
  3511. /* This is a horrible situation. An IO or malloc() error occurred whilst
  3512. ** trying to save cursor positions. If this is an automatic rollback (as
  3513. ** the result of a constraint, malloc() failure or IO error) then
  3514. ** the cache may be internally inconsistent (not contain valid trees) so
  3515. ** we cannot simply return the error to the caller. Instead, abort
  3516. ** all queries that may be using any of the cursors that failed to save.
  3517. */
  3518. sqlite3BtreeTripAllCursors(p, rc);
  3519. }
  3520. #endif
  3521. btreeIntegrity( p );
  3522. if ( p.inTrans == TRANS_WRITE )
  3523. {
  3524. int rc2;
  3525. Debug.Assert( TRANS_WRITE == pBt.inTransaction );
  3526. rc2 = sqlite3PagerRollback( pBt.pPager );
  3527. if ( rc2 != SQLITE_OK )
  3528. {
  3529. rc = rc2;
  3530. }
  3531. /* The rollback may have destroyed the pPage1.aData value. So
  3532. ** call btreeGetPage() on page 1 again to make
  3533. ** sure pPage1.aData is set correctly. */
  3534. if ( btreeGetPage( pBt, 1, ref pPage1, 0 ) == SQLITE_OK )
  3535. {
  3536. Pgno nPage = sqlite3Get4byte( pPage1.aData, 28 );
  3537. testcase( nPage == 0 );
  3538. if ( nPage == 0 )
  3539. sqlite3PagerPagecount( pBt.pPager, ref nPage );
  3540. testcase( pBt.nPage != nPage );
  3541. pBt.nPage = nPage;
  3542. releasePage( pPage1 );
  3543. }
  3544. Debug.Assert( countWriteCursors( pBt ) == 0 );
  3545. pBt.inTransaction = TRANS_READ;
  3546. }
  3547. btreeEndTransaction( p );
  3548. sqlite3BtreeLeave( p );
  3549. return rc;
  3550. }
  3551. /*
  3552. ** Start a statement subtransaction. The subtransaction can can be rolled
  3553. ** back independently of the main transaction. You must start a transaction
  3554. ** before starting a subtransaction. The subtransaction is ended automatically
  3555. ** if the main transaction commits or rolls back.
  3556. **
  3557. ** Statement subtransactions are used around individual SQL statements
  3558. ** that are contained within a BEGIN...COMMIT block. If a constraint
  3559. ** error occurs within the statement, the effect of that one statement
  3560. ** can be rolled back without having to rollback the entire transaction.
  3561. **
  3562. ** A statement sub-transaction is implemented as an anonymous savepoint. The
  3563. ** value passed as the second parameter is the total number of savepoints,
  3564. ** including the new anonymous savepoint, open on the B-Tree. i.e. if there
  3565. ** are no active savepoints and no other statement-transactions open,
  3566. ** iStatement is 1. This anonymous savepoint can be released or rolled back
  3567. ** using the sqlite3BtreeSavepoint() function.
  3568. */
  3569. static int sqlite3BtreeBeginStmt( Btree p, int iStatement )
  3570. {
  3571. int rc;
  3572. BtShared pBt = p.pBt;
  3573. sqlite3BtreeEnter( p );
  3574. Debug.Assert( p.inTrans == TRANS_WRITE );
  3575. Debug.Assert( !pBt.readOnly );
  3576. Debug.Assert( iStatement > 0 );
  3577. Debug.Assert( iStatement > p.db.nSavepoint );
  3578. Debug.Assert( pBt.inTransaction == TRANS_WRITE );
  3579. /* At the pager level, a statement transaction is a savepoint with
  3580. ** an index greater than all savepoints created explicitly using
  3581. ** SQL statements. It is illegal to open, release or rollback any
  3582. ** such savepoints while the statement transaction savepoint is active.
  3583. */
  3584. rc = sqlite3PagerOpenSavepoint( pBt.pPager, iStatement );
  3585. sqlite3BtreeLeave( p );
  3586. return rc;
  3587. }
  3588. /*
  3589. ** The second argument to this function, op, is always SAVEPOINT_ROLLBACK
  3590. ** or SAVEPOINT_RELEASE. This function either releases or rolls back the
  3591. ** savepoint identified by parameter iSavepoint, depending on the value
  3592. ** of op.
  3593. **
  3594. ** Normally, iSavepoint is greater than or equal to zero. However, if op is
  3595. ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
  3596. ** contents of the entire transaction are rolled back. This is different
  3597. ** from a normal transaction rollback, as no locks are released and the
  3598. ** transaction remains open.
  3599. */
  3600. static int sqlite3BtreeSavepoint( Btree p, int op, int iSavepoint )
  3601. {
  3602. int rc = SQLITE_OK;
  3603. if ( p != null && p.inTrans == TRANS_WRITE )
  3604. {
  3605. BtShared pBt = p.pBt;
  3606. Debug.Assert( op == SAVEPOINT_RELEASE || op == SAVEPOINT_ROLLBACK );
  3607. Debug.Assert( iSavepoint >= 0 || ( iSavepoint == -1 && op == SAVEPOINT_ROLLBACK ) );
  3608. sqlite3BtreeEnter( p );
  3609. rc = sqlite3PagerSavepoint( pBt.pPager, op, iSavepoint );
  3610. if ( rc == SQLITE_OK )
  3611. {
  3612. if ( iSavepoint < 0 && pBt.initiallyEmpty )
  3613. pBt.nPage = 0;
  3614. rc = newDatabase( pBt );
  3615. pBt.nPage = sqlite3Get4byte( pBt.pPage1.aData, 28 );
  3616. /* The database size was written into the offset 28 of the header
  3617. ** when the transaction started, so we know that the value at offset
  3618. ** 28 is nonzero. */
  3619. Debug.Assert( pBt.nPage > 0 );
  3620. }
  3621. sqlite3BtreeLeave( p );
  3622. }
  3623. return rc;
  3624. }
  3625. /*
  3626. ** Create a new cursor for the BTree whose root is on the page
  3627. ** iTable. If a read-only cursor is requested, it is assumed that
  3628. ** the caller already has at least a read-only transaction open
  3629. ** on the database already. If a write-cursor is requested, then
  3630. ** the caller is assumed to have an open write transaction.
  3631. **
  3632. ** If wrFlag==null, then the cursor can only be used for reading.
  3633. ** If wrFlag==1, then the cursor can be used for reading or for
  3634. ** writing if other conditions for writing are also met. These
  3635. ** are the conditions that must be met in order for writing to
  3636. ** be allowed:
  3637. **
  3638. ** 1: The cursor must have been opened with wrFlag==1
  3639. **
  3640. ** 2: Other database connections that share the same pager cache
  3641. ** but which are not in the READ_UNCOMMITTED state may not have
  3642. ** cursors open with wrFlag==null on the same table. Otherwise
  3643. ** the changes made by this write cursor would be visible to
  3644. ** the read cursors in the other database connection.
  3645. **
  3646. ** 3: The database must be writable (not on read-only media)
  3647. **
  3648. ** 4: There must be an active transaction.
  3649. **
  3650. ** No checking is done to make sure that page iTable really is the
  3651. ** root page of a b-tree. If it is not, then the cursor acquired
  3652. ** will not work correctly.
  3653. **
  3654. ** It is assumed that the sqlite3BtreeCursorZero() has been called
  3655. ** on pCur to initialize the memory space prior to invoking this routine.
  3656. */
  3657. static int btreeCursor(
  3658. Btree p, /* The btree */
  3659. int iTable, /* Root page of table to open */
  3660. int wrFlag, /* 1 to write. 0 read-only */
  3661. KeyInfo pKeyInfo, /* First arg to comparison function */
  3662. BtCursor pCur /* Space for new cursor */
  3663. )
  3664. {
  3665. BtShared pBt = p.pBt; /* Shared b-tree handle */
  3666. Debug.Assert( sqlite3BtreeHoldsMutex( p ) );
  3667. Debug.Assert( wrFlag == 0 || wrFlag == 1 );
  3668. /* The following Debug.Assert statements verify that if this is a sharable
  3669. ** b-tree database, the connection is holding the required table locks,
  3670. ** and that no other connection has any open cursor that conflicts with
  3671. ** this lock. */
  3672. Debug.Assert( hasSharedCacheTableLock( p, (u32)iTable, pKeyInfo != null ? 1 : 0, wrFlag + 1 ) );
  3673. Debug.Assert( wrFlag == 0 || !hasReadConflicts( p, (u32)iTable ) );
  3674. /* Assert that the caller has opened the required transaction. */
  3675. Debug.Assert( p.inTrans > TRANS_NONE );
  3676. Debug.Assert( wrFlag == 0 || p.inTrans == TRANS_WRITE );
  3677. Debug.Assert( pBt.pPage1 != null && pBt.pPage1.aData != null );
  3678. if ( NEVER( wrFlag != 0 && pBt.readOnly ) )
  3679. {
  3680. return SQLITE_READONLY;
  3681. }
  3682. if ( iTable == 1 && btreePagecount( pBt ) == 0 )
  3683. {
  3684. return SQLITE_EMPTY;
  3685. }
  3686. /* Now that no other errors can occur, finish filling in the BtCursor
  3687. ** variables and link the cursor into the BtShared list. */
  3688. pCur.pgnoRoot = (Pgno)iTable;
  3689. pCur.iPage = -1;
  3690. pCur.pKeyInfo = pKeyInfo;
  3691. pCur.pBtree = p;
  3692. pCur.pBt = pBt;
  3693. pCur.wrFlag = (u8)wrFlag;
  3694. pCur.pNext = pBt.pCursor;
  3695. if ( pCur.pNext != null )
  3696. {
  3697. pCur.pNext.pPrev = pCur;
  3698. }
  3699. pBt.pCursor = pCur;
  3700. pCur.eState = CURSOR_INVALID;
  3701. pCur.cachedRowid = 0;
  3702. return SQLITE_OK;
  3703. }
  3704. static int sqlite3BtreeCursor(
  3705. Btree p, /* The btree */
  3706. int iTable, /* Root page of table to open */
  3707. int wrFlag, /* 1 to write. 0 read-only */
  3708. KeyInfo pKeyInfo, /* First arg to xCompare() */
  3709. BtCursor pCur /* Write new cursor here */
  3710. )
  3711. {
  3712. int rc;
  3713. sqlite3BtreeEnter( p );
  3714. rc = btreeCursor( p, iTable, wrFlag, pKeyInfo, pCur );
  3715. sqlite3BtreeLeave( p );
  3716. return rc;
  3717. }
  3718. /*
  3719. ** Return the size of a BtCursor object in bytes.
  3720. **
  3721. ** This interfaces is needed so that users of cursors can preallocate
  3722. ** sufficient storage to hold a cursor. The BtCursor object is opaque
  3723. ** to users so they cannot do the sizeof() themselves - they must call
  3724. ** this routine.
  3725. */
  3726. static int sqlite3BtreeCursorSize()
  3727. {
  3728. return -1; // Not Used -- return ROUND8(sizeof(BtCursor));
  3729. }
  3730. /*
  3731. ** Initialize memory that will be converted into a BtCursor object.
  3732. **
  3733. ** The simple approach here would be to memset() the entire object
  3734. ** to zero. But it turns out that the apPage[] and aiIdx[] arrays
  3735. ** do not need to be zeroed and they are large, so we can save a lot
  3736. ** of run-time by skipping the initialization of those elements.
  3737. */
  3738. static void sqlite3BtreeCursorZero( BtCursor p )
  3739. {
  3740. p.Clear(); // memset( p, 0, offsetof( BtCursor, iPage ) );
  3741. }
  3742. /*
  3743. ** Set the cached rowid value of every cursor in the same database file
  3744. ** as pCur and having the same root page number as pCur. The value is
  3745. ** set to iRowid.
  3746. **
  3747. ** Only positive rowid values are considered valid for this cache.
  3748. ** The cache is initialized to zero, indicating an invalid cache.
  3749. ** A btree will work fine with zero or negative rowids. We just cannot
  3750. ** cache zero or negative rowids, which means tables that use zero or
  3751. ** negative rowids might run a little slower. But in practice, zero
  3752. ** or negative rowids are very uncommon so this should not be a problem.
  3753. */
  3754. static void sqlite3BtreeSetCachedRowid( BtCursor pCur, sqlite3_int64 iRowid )
  3755. {
  3756. BtCursor p;
  3757. for ( p = pCur.pBt.pCursor; p != null; p = p.pNext )
  3758. {
  3759. if ( p.pgnoRoot == pCur.pgnoRoot )
  3760. p.cachedRowid = iRowid;
  3761. }
  3762. Debug.Assert( pCur.cachedRowid == iRowid );
  3763. }
  3764. /*
  3765. ** Return the cached rowid for the given cursor. A negative or zero
  3766. ** return value indicates that the rowid cache is invalid and should be
  3767. ** ignored. If the rowid cache has never before been set, then a
  3768. ** zero is returned.
  3769. */
  3770. static sqlite3_int64 sqlite3BtreeGetCachedRowid( BtCursor pCur )
  3771. {
  3772. return pCur.cachedRowid;
  3773. }
  3774. /*
  3775. ** Close a cursor. The read lock on the database file is released
  3776. ** when the last cursor is closed.
  3777. */
  3778. static int sqlite3BtreeCloseCursor( BtCursor pCur )
  3779. {
  3780. Btree pBtree = pCur.pBtree;
  3781. if ( pBtree != null )
  3782. {
  3783. int i;
  3784. BtShared pBt = pCur.pBt;
  3785. sqlite3BtreeEnter( pBtree );
  3786. sqlite3BtreeClearCursor( pCur );
  3787. if ( pCur.pPrev != null )
  3788. {
  3789. pCur.pPrev.pNext = pCur.pNext;
  3790. }
  3791. else
  3792. {
  3793. pBt.pCursor = pCur.pNext;
  3794. }
  3795. if ( pCur.pNext != null )
  3796. {
  3797. pCur.pNext.pPrev = pCur.pPrev;
  3798. }
  3799. for ( i = 0; i <= pCur.iPage; i++ )
  3800. {
  3801. releasePage( pCur.apPage[i] );
  3802. }
  3803. unlockBtreeIfUnused( pBt );
  3804. invalidateOverflowCache( pCur );
  3805. /* sqlite3_free(ref pCur); */
  3806. sqlite3BtreeLeave( pBtree );
  3807. }
  3808. return SQLITE_OK;
  3809. }
  3810. /*
  3811. ** Make sure the BtCursor* given in the argument has a valid
  3812. ** BtCursor.info structure. If it is not already valid, call
  3813. ** btreeParseCell() to fill it in.
  3814. **
  3815. ** BtCursor.info is a cache of the information in the current cell.
  3816. ** Using this cache reduces the number of calls to btreeParseCell().
  3817. **
  3818. ** 2007-06-25: There is a bug in some versions of MSVC that cause the
  3819. ** compiler to crash when getCellInfo() is implemented as a macro.
  3820. ** But there is a measureable speed advantage to using the macro on gcc
  3821. ** (when less compiler optimizations like -Os or -O0 are used and the
  3822. ** compiler is not doing agressive inlining.) So we use a real function
  3823. ** for MSVC and a macro for everything else. Ticket #2457.
  3824. */
  3825. #if !NDEBUG
  3826. static void assertCellInfo( BtCursor pCur )
  3827. {
  3828. CellInfo info;
  3829. int iPage = pCur.iPage;
  3830. info = new CellInfo();//memset(info, 0, sizeof(info));
  3831. btreeParseCell( pCur.apPage[iPage], pCur.aiIdx[iPage], ref info );
  3832. Debug.Assert( info.GetHashCode() == pCur.info.GetHashCode() || info.Equals( pCur.info ) );//memcmp(info, pCur.info, sizeof(info))==0 );
  3833. }
  3834. #else
  3835. // #define assertCellInfo(x)
  3836. static void assertCellInfo(BtCursor pCur) { }
  3837. #endif
  3838. #if _MSC_VER
  3839. /* Use a real function in MSVC to work around bugs in that compiler. */
  3840. static void getCellInfo( BtCursor pCur )
  3841. {
  3842. if ( pCur.info.nSize == 0 )
  3843. {
  3844. int iPage = pCur.iPage;
  3845. btreeParseCell( pCur.apPage[iPage], pCur.aiIdx[iPage], ref pCur.info );
  3846. pCur.validNKey = true;
  3847. }
  3848. else
  3849. {
  3850. assertCellInfo( pCur );
  3851. }
  3852. }
  3853. #else //* if not _MSC_VER */
  3854. /* Use a macro in all other compilers so that the function is inlined */
  3855. //#define getCellInfo(pCur) \
  3856. // if( pCur.info.nSize==null ){ \
  3857. // int iPage = pCur.iPage; \
  3858. // btreeParseCell(pCur.apPage[iPage],pCur.aiIdx[iPage],&pCur.info); \
  3859. // pCur.validNKey = true; \
  3860. // }else{ \
  3861. // assertCellInfo(pCur); \
  3862. // }
  3863. #endif //* _MSC_VER */
  3864. #if !NDEBUG //* The next routine used only within Debug.Assert() statements */
  3865. /*
  3866. ** Return true if the given BtCursor is valid. A valid cursor is one
  3867. ** that is currently pointing to a row in a (non-empty) table.
  3868. ** This is a verification routine is used only within Debug.Assert() statements.
  3869. */
  3870. static bool sqlite3BtreeCursorIsValid( BtCursor pCur )
  3871. {
  3872. return pCur != null && pCur.eState == CURSOR_VALID;
  3873. }
  3874. #else
  3875. static bool sqlite3BtreeCursorIsValid(BtCursor pCur) { return true; }
  3876. #endif //* NDEBUG */
  3877. /*
  3878. ** Set pSize to the size of the buffer needed to hold the value of
  3879. ** the key for the current entry. If the cursor is not pointing
  3880. ** to a valid entry, pSize is set to 0.
  3881. **
  3882. ** For a table with the INTKEY flag set, this routine returns the key
  3883. ** itself, not the number of bytes in the key.
  3884. **
  3885. ** The caller must position the cursor prior to invoking this routine.
  3886. **
  3887. ** This routine cannot fail. It always returns SQLITE_OK.
  3888. */
  3889. static int sqlite3BtreeKeySize( BtCursor pCur, ref i64 pSize )
  3890. {
  3891. Debug.Assert( cursorHoldsMutex( pCur ) );
  3892. Debug.Assert( pCur.eState == CURSOR_INVALID || pCur.eState == CURSOR_VALID );
  3893. if ( pCur.eState != CURSOR_VALID )
  3894. {
  3895. pSize = 0;
  3896. }
  3897. else
  3898. {
  3899. getCellInfo( pCur );
  3900. pSize = pCur.info.nKey;
  3901. }
  3902. return SQLITE_OK;
  3903. }
  3904. /*
  3905. ** Set pSize to the number of bytes of data in the entry the
  3906. ** cursor currently points to.
  3907. **
  3908. ** The caller must guarantee that the cursor is pointing to a non-NULL
  3909. ** valid entry. In other words, the calling procedure must guarantee
  3910. ** that the cursor has Cursor.eState==CURSOR_VALID.
  3911. **
  3912. ** Failure is not possible. This function always returns SQLITE_OK.
  3913. ** It might just as well be a procedure (returning void) but we continue
  3914. ** to return an integer result code for historical reasons.
  3915. */
  3916. static int sqlite3BtreeDataSize( BtCursor pCur, ref u32 pSize )
  3917. {
  3918. Debug.Assert( cursorHoldsMutex( pCur ) );
  3919. Debug.Assert( pCur.eState == CURSOR_VALID );
  3920. getCellInfo( pCur );
  3921. pSize = pCur.info.nData;
  3922. return SQLITE_OK;
  3923. }
  3924. /*
  3925. ** Given the page number of an overflow page in the database (parameter
  3926. ** ovfl), this function finds the page number of the next page in the
  3927. ** linked list of overflow pages. If possible, it uses the auto-vacuum
  3928. ** pointer-map data instead of reading the content of page ovfl to do so.
  3929. **
  3930. ** If an error occurs an SQLite error code is returned. Otherwise:
  3931. **
  3932. ** The page number of the next overflow page in the linked list is
  3933. ** written to pPgnoNext. If page ovfl is the last page in its linked
  3934. ** list, pPgnoNext is set to zero.
  3935. **
  3936. ** If ppPage is not NULL, and a reference to the MemPage object corresponding
  3937. ** to page number pOvfl was obtained, then ppPage is set to point to that
  3938. ** reference. It is the responsibility of the caller to call releasePage()
  3939. ** on ppPage to free the reference. In no reference was obtained (because
  3940. ** the pointer-map was used to obtain the value for pPgnoNext), then
  3941. ** ppPage is set to zero.
  3942. */
  3943. static int getOverflowPage(
  3944. BtShared pBt, /* The database file */
  3945. Pgno ovfl, /* Current overflow page number */
  3946. ref MemPage ppPage, /* OUT: MemPage handle (may be NULL) */
  3947. ref Pgno pPgnoNext /* OUT: Next overflow page number */
  3948. )
  3949. {
  3950. Pgno next = 0;
  3951. MemPage pPage = null;
  3952. int rc = SQLITE_OK;
  3953. Debug.Assert( sqlite3_mutex_held( pBt.mutex ) );
  3954. // Debug.Assert( pPgnoNext);
  3955. #if !SQLITE_OMIT_AUTOVACUUM
  3956. /* Try to find the next page in the overflow list using the
  3957. ** autovacuum pointer-map pages. Guess that the next page in
  3958. ** the overflow list is page number (ovfl+1). If that guess turns
  3959. ** out to be wrong, fall back to loading the data of page
  3960. ** number ovfl to determine the next page number.
  3961. */
  3962. if ( pBt.autoVacuum )
  3963. {
  3964. Pgno pgno = 0;
  3965. Pgno iGuess = ovfl + 1;
  3966. u8 eType = 0;
  3967. while ( PTRMAP_ISPAGE( pBt, iGuess ) || iGuess == PENDING_BYTE_PAGE( pBt ) )
  3968. {
  3969. iGuess++;
  3970. }
  3971. if ( iGuess <= btreePagecount( pBt ) )
  3972. {
  3973. rc = ptrmapGet( pBt, iGuess, ref eType, ref pgno );
  3974. if ( rc == SQLITE_OK && eType == PTRMAP_OVERFLOW2 && pgno == ovfl )
  3975. {
  3976. next = iGuess;
  3977. rc = SQLITE_DONE;
  3978. }
  3979. }
  3980. }
  3981. #endif
  3982. Debug.Assert( next == 0 || rc == SQLITE_DONE );
  3983. if ( rc == SQLITE_OK )
  3984. {
  3985. rc = btreeGetPage( pBt, ovfl, ref pPage, 0 );
  3986. Debug.Assert( rc == SQLITE_OK || pPage == null );
  3987. if ( rc == SQLITE_OK )
  3988. {
  3989. next = sqlite3Get4byte( pPage.aData );
  3990. }
  3991. }
  3992. pPgnoNext = next;
  3993. if ( ppPage != null )
  3994. {
  3995. ppPage = pPage;
  3996. }
  3997. else
  3998. {
  3999. releasePage( pPage );
  4000. }
  4001. return ( rc == SQLITE_DONE ? SQLITE_OK : rc );
  4002. }
  4003. /*
  4004. ** Copy data from a buffer to a page, or from a page to a buffer.
  4005. **
  4006. ** pPayload is a pointer to data stored on database page pDbPage.
  4007. ** If argument eOp is false, then nByte bytes of data are copied
  4008. ** from pPayload to the buffer pointed at by pBuf. If eOp is true,
  4009. ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
  4010. ** of data are copied from the buffer pBuf to pPayload.
  4011. **
  4012. ** SQLITE_OK is returned on success, otherwise an error code.
  4013. */
  4014. static int copyPayload(
  4015. byte[] pPayload, /* Pointer to page data */
  4016. u32 payloadOffset, /* Offset into page data */
  4017. byte[] pBuf, /* Pointer to buffer */
  4018. u32 pBufOffset, /* Offset into buffer */
  4019. u32 nByte, /* Number of bytes to copy */
  4020. int eOp, /* 0 . copy from page, 1 . copy to page */
  4021. DbPage pDbPage /* Page containing pPayload */
  4022. )
  4023. {
  4024. if ( eOp != 0 )
  4025. {
  4026. /* Copy data from buffer to page (a write operation) */
  4027. int rc = sqlite3PagerWrite( pDbPage );
  4028. if ( rc != SQLITE_OK )
  4029. {
  4030. return rc;
  4031. }
  4032. Buffer.BlockCopy( pBuf, (int)pBufOffset, pPayload, (int)payloadOffset, (int)nByte );// memcpy( pPayload, pBuf, nByte );
  4033. }
  4034. else
  4035. {
  4036. /* Copy data from page to buffer (a read operation) */
  4037. Buffer.BlockCopy( pPayload, (int)payloadOffset, pBuf, (int)pBufOffset, (int)nByte );//memcpy(pBuf, pPayload, nByte);
  4038. }
  4039. return SQLITE_OK;
  4040. }
  4041. //static int copyPayload(
  4042. // byte[] pPayload, /* Pointer to page data */
  4043. // byte[] pBuf, /* Pointer to buffer */
  4044. // int nByte, /* Number of bytes to copy */
  4045. // int eOp, /* 0 -> copy from page, 1 -> copy to page */
  4046. // DbPage pDbPage /* Page containing pPayload */
  4047. //){
  4048. // if( eOp!=0 ){
  4049. // /* Copy data from buffer to page (a write operation) */
  4050. // int rc = sqlite3PagerWrite(pDbPage);
  4051. // if( rc!=SQLITE_OK ){
  4052. // return rc;
  4053. // }
  4054. // memcpy(pPayload, pBuf, nByte);
  4055. // }else{
  4056. // /* Copy data from page to buffer (a read operation) */
  4057. // memcpy(pBuf, pPayload, nByte);
  4058. // }
  4059. // return SQLITE_OK;
  4060. //}
  4061. /*
  4062. ** This function is used to read or overwrite payload information
  4063. ** for the entry that the pCur cursor is pointing to. If the eOp
  4064. ** parameter is 0, this is a read operation (data copied into
  4065. ** buffer pBuf). If it is non-zero, a write (data copied from
  4066. ** buffer pBuf).
  4067. **
  4068. ** A total of "amt" bytes are read or written beginning at "offset".
  4069. ** Data is read to or from the buffer pBuf.
  4070. **
  4071. ** The content being read or written might appear on the main page
  4072. ** or be scattered out on multiple overflow pages.
  4073. **
  4074. ** If the BtCursor.isIncrblobHandle flag is set, and the current
  4075. ** cursor entry uses one or more overflow pages, this function
  4076. ** allocates space for and lazily popluates the overflow page-list
  4077. ** cache array (BtCursor.aOverflow). Subsequent calls use this
  4078. ** cache to make seeking to the supplied offset more efficient.
  4079. **
  4080. ** Once an overflow page-list cache has been allocated, it may be
  4081. ** invalidated if some other cursor writes to the same table, or if
  4082. ** the cursor is moved to a different row. Additionally, in auto-vacuum
  4083. ** mode, the following events may invalidate an overflow page-list cache.
  4084. **
  4085. ** * An incremental vacuum,
  4086. ** * A commit in auto_vacuum="full" mode,
  4087. ** * Creating a table (may require moving an overflow page).
  4088. */
  4089. static int accessPayload(
  4090. BtCursor pCur, /* Cursor pointing to entry to read from */
  4091. u32 offset, /* Begin reading this far into payload */
  4092. u32 amt, /* Read this many bytes */
  4093. byte[] pBuf, /* Write the bytes into this buffer */
  4094. int eOp /* zero to read. non-zero to write. */
  4095. )
  4096. {
  4097. u32 pBufOffset = 0;
  4098. byte[] aPayload;
  4099. int rc = SQLITE_OK;
  4100. u32 nKey;
  4101. int iIdx = 0;
  4102. MemPage pPage = pCur.apPage[pCur.iPage]; /* Btree page of current entry */
  4103. BtShared pBt = pCur.pBt; /* Btree this cursor belongs to */
  4104. Debug.Assert( pPage != null );
  4105. Debug.Assert( pCur.eState == CURSOR_VALID );
  4106. Debug.Assert( pCur.aiIdx[pCur.iPage] < pPage.nCell );
  4107. Debug.Assert( cursorHoldsMutex( pCur ) );
  4108. getCellInfo( pCur );
  4109. aPayload = pCur.info.pCell; //pCur.info.pCell + pCur.info.nHeader;
  4110. nKey = (u32)( pPage.intKey != 0 ? 0 : (int)pCur.info.nKey );
  4111. if ( NEVER( offset + amt > nKey + pCur.info.nData )
  4112. || pCur.info.nLocal > pBt.usableSize//&aPayload[pCur.info.nLocal] > &pPage.aData[pBt.usableSize]
  4113. )
  4114. {
  4115. /* Trying to read or write past the end of the data is an error */
  4116. return SQLITE_CORRUPT_BKPT();
  4117. }
  4118. /* Check if data must be read/written to/from the btree page itself. */
  4119. if ( offset < pCur.info.nLocal )
  4120. {
  4121. int a = (int)amt;
  4122. if ( a + offset > pCur.info.nLocal )
  4123. {
  4124. a = (int)( pCur.info.nLocal - offset );
  4125. }
  4126. rc = copyPayload( aPayload, (u32)( offset + pCur.info.iCell + pCur.info.nHeader ), pBuf, pBufOffset, (u32)a, eOp, pPage.pDbPage );
  4127. offset = 0;
  4128. pBufOffset += (u32)a; //pBuf += a;
  4129. amt -= (u32)a;
  4130. }
  4131. else
  4132. {
  4133. offset -= pCur.info.nLocal;
  4134. }
  4135. if ( rc == SQLITE_OK && amt > 0 )
  4136. {
  4137. u32 ovflSize = (u32)( pBt.usableSize - 4 ); /* Bytes content per ovfl page */
  4138. Pgno nextPage;
  4139. nextPage = sqlite3Get4byte( aPayload, pCur.info.nLocal + pCur.info.iCell + pCur.info.nHeader );
  4140. #if !SQLITE_OMIT_INCRBLOB
  4141. /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[]
  4142. ** has not been allocated, allocate it now. The array is sized at
  4143. ** one entry for each overflow page in the overflow chain. The
  4144. ** page number of the first overflow page is stored in aOverflow[0],
  4145. ** etc. A value of 0 in the aOverflow[] array means "not yet known"
  4146. ** (the cache is lazily populated).
  4147. */
  4148. if( pCur.isIncrblobHandle && !pCur.aOverflow ){
  4149. int nOvfl = (pCur.info.nPayload-pCur.info.nLocal+ovflSize-1)/ovflSize;
  4150. pCur.aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl);
  4151. /* nOvfl is always positive. If it were zero, fetchPayload would have
  4152. ** been used instead of this routine. */
  4153. if( ALWAYS(nOvfl) && !pCur.aOverflow ){
  4154. rc = SQLITE_NOMEM;
  4155. }
  4156. }
  4157. /* If the overflow page-list cache has been allocated and the
  4158. ** entry for the first required overflow page is valid, skip
  4159. ** directly to it.
  4160. */
  4161. if( pCur.aOverflow && pCur.aOverflow[offset/ovflSize] ){
  4162. iIdx = (offset/ovflSize);
  4163. nextPage = pCur.aOverflow[iIdx];
  4164. offset = (offset%ovflSize);
  4165. }
  4166. #endif
  4167. for ( ; rc == SQLITE_OK && amt > 0 && nextPage != 0; iIdx++ )
  4168. {
  4169. #if !SQLITE_OMIT_INCRBLOB
  4170. /* If required, populate the overflow page-list cache. */
  4171. if( pCur.aOverflow ){
  4172. Debug.Assert(!pCur.aOverflow[iIdx] || pCur.aOverflow[iIdx]==nextPage);
  4173. pCur.aOverflow[iIdx] = nextPage;
  4174. }
  4175. #endif
  4176. MemPage MemPageDummy = null;
  4177. if ( offset >= ovflSize )
  4178. {
  4179. /* The only reason to read this page is to obtain the page
  4180. ** number for the next page in the overflow chain. The page
  4181. ** data is not required. So first try to lookup the overflow
  4182. ** page-list cache, if any, then fall back to the getOverflowPage()
  4183. ** function.
  4184. */
  4185. #if !SQLITE_OMIT_INCRBLOB
  4186. if( pCur.aOverflow && pCur.aOverflow[iIdx+1] ){
  4187. nextPage = pCur.aOverflow[iIdx+1];
  4188. } else
  4189. #endif
  4190. rc = getOverflowPage( pBt, nextPage, ref MemPageDummy, ref nextPage );
  4191. offset -= ovflSize;
  4192. }
  4193. else
  4194. {
  4195. /* Need to read this page properly. It contains some of the
  4196. ** range of data that is being read (eOp==null) or written (eOp!=null).
  4197. */
  4198. PgHdr pDbPage = new PgHdr();
  4199. int a = (int)amt;
  4200. rc = sqlite3PagerGet( pBt.pPager, nextPage, ref pDbPage );
  4201. if ( rc == SQLITE_OK )
  4202. {
  4203. aPayload = sqlite3PagerGetData( pDbPage );
  4204. nextPage = sqlite3Get4byte( aPayload );
  4205. if ( a + offset > ovflSize )
  4206. {
  4207. a = (int)( ovflSize - offset );
  4208. }
  4209. rc = copyPayload( aPayload, offset + 4, pBuf, pBufOffset, (u32)a, eOp, pDbPage );
  4210. sqlite3PagerUnref( pDbPage );
  4211. offset = 0;
  4212. amt -= (u32)a;
  4213. pBufOffset += (u32)a;//pBuf += a;
  4214. }
  4215. }
  4216. }
  4217. }
  4218. if ( rc == SQLITE_OK && amt > 0 )
  4219. {
  4220. return SQLITE_CORRUPT_BKPT();
  4221. }
  4222. return rc;
  4223. }
  4224. /*
  4225. ** Read part of the key associated with cursor pCur. Exactly
  4226. ** "amt" bytes will be transfered into pBuf[]. The transfer
  4227. ** begins at "offset".
  4228. **
  4229. ** The caller must ensure that pCur is pointing to a valid row
  4230. ** in the table.
  4231. **
  4232. ** Return SQLITE_OK on success or an error code if anything goes
  4233. ** wrong. An error is returned if "offset+amt" is larger than
  4234. ** the available payload.
  4235. */
  4236. static int sqlite3BtreeKey( BtCursor pCur, u32 offset, u32 amt, byte[] pBuf )
  4237. {
  4238. Debug.Assert( cursorHoldsMutex( pCur ) );
  4239. Debug.Assert( pCur.eState == CURSOR_VALID );
  4240. Debug.Assert( pCur.iPage >= 0 && pCur.apPage[pCur.iPage] != null );
  4241. Debug.Assert( pCur.aiIdx[pCur.iPage] < pCur.apPage[pCur.iPage].nCell );
  4242. return accessPayload( pCur, offset, amt, pBuf, 0 );
  4243. }
  4244. /*
  4245. ** Read part of the data associated with cursor pCur. Exactly
  4246. ** "amt" bytes will be transfered into pBuf[]. The transfer
  4247. ** begins at "offset".
  4248. **
  4249. ** Return SQLITE_OK on success or an error code if anything goes
  4250. ** wrong. An error is returned if "offset+amt" is larger than
  4251. ** the available payload.
  4252. */
  4253. static int sqlite3BtreeData( BtCursor pCur, u32 offset, u32 amt, byte[] pBuf )
  4254. {
  4255. int rc;
  4256. #if !SQLITE_OMIT_INCRBLOB
  4257. if ( pCur.eState==CURSOR_INVALID ){
  4258. return SQLITE_ABORT;
  4259. }
  4260. #endif
  4261. Debug.Assert( cursorHoldsMutex( pCur ) );
  4262. rc = restoreCursorPosition( pCur );
  4263. if ( rc == SQLITE_OK )
  4264. {
  4265. Debug.Assert( pCur.eState == CURSOR_VALID );
  4266. Debug.Assert( pCur.iPage >= 0 && pCur.apPage[pCur.iPage] != null );
  4267. Debug.Assert( pCur.aiIdx[pCur.iPage] < pCur.apPage[pCur.iPage].nCell );
  4268. rc = accessPayload( pCur, offset, amt, pBuf, 0 );
  4269. }
  4270. return rc;
  4271. }
  4272. /*
  4273. ** Return a pointer to payload information from the entry that the
  4274. ** pCur cursor is pointing to. The pointer is to the beginning of
  4275. ** the key if skipKey==null and it points to the beginning of data if
  4276. ** skipKey==1. The number of bytes of available key/data is written
  4277. ** into pAmt. If pAmt==null, then the value returned will not be
  4278. ** a valid pointer.
  4279. **
  4280. ** This routine is an optimization. It is common for the entire key
  4281. ** and data to fit on the local page and for there to be no overflow
  4282. ** pages. When that is so, this routine can be used to access the
  4283. ** key and data without making a copy. If the key and/or data spills
  4284. ** onto overflow pages, then accessPayload() must be used to reassemble
  4285. ** the key/data and copy it into a preallocated buffer.
  4286. **
  4287. ** The pointer returned by this routine looks directly into the cached
  4288. ** page of the database. The data might change or move the next time
  4289. ** any btree routine is called.
  4290. */
  4291. static byte[] fetchPayload(
  4292. BtCursor pCur, /* Cursor pointing to entry to read from */
  4293. ref int pAmt, /* Write the number of available bytes here */
  4294. ref int outOffset, /* Offset into Buffer */
  4295. bool skipKey /* read beginning at data if this is true */
  4296. )
  4297. {
  4298. byte[] aPayload;
  4299. MemPage pPage;
  4300. u32 nKey;
  4301. u32 nLocal;
  4302. Debug.Assert( pCur != null && pCur.iPage >= 0 && pCur.apPage[pCur.iPage] != null );
  4303. Debug.Assert( pCur.eState == CURSOR_VALID );
  4304. Debug.Assert( cursorHoldsMutex( pCur ) );
  4305. outOffset = -1;
  4306. pPage = pCur.apPage[pCur.iPage];
  4307. Debug.Assert( pCur.aiIdx[pCur.iPage] < pPage.nCell );
  4308. if ( NEVER( pCur.info.nSize == 0 ) )
  4309. {
  4310. btreeParseCell( pCur.apPage[pCur.iPage], pCur.aiIdx[pCur.iPage],
  4311. ref pCur.info );
  4312. }
  4313. //aPayload = pCur.info.pCell;
  4314. //aPayload += pCur.info.nHeader;
  4315. aPayload = sqlite3Malloc( pCur.info.nSize - pCur.info.nHeader );
  4316. if ( pPage.intKey != 0 )
  4317. {
  4318. nKey = 0;
  4319. }
  4320. else
  4321. {
  4322. nKey = (u32)pCur.info.nKey;
  4323. }
  4324. if ( skipKey )
  4325. {
  4326. //aPayload += nKey;
  4327. outOffset = (int)( pCur.info.iCell + pCur.info.nHeader + nKey );
  4328. Buffer.BlockCopy( pCur.info.pCell, outOffset, aPayload, 0, (int)( pCur.info.nSize - pCur.info.nHeader - nKey ) );
  4329. nLocal = pCur.info.nLocal - nKey;
  4330. }
  4331. else
  4332. {
  4333. outOffset = (int)( pCur.info.iCell + pCur.info.nHeader );
  4334. Buffer.BlockCopy( pCur.info.pCell, outOffset, aPayload, 0, pCur.info.nSize - pCur.info.nHeader );
  4335. nLocal = pCur.info.nLocal;
  4336. Debug.Assert( nLocal <= nKey );
  4337. }
  4338. pAmt = (int)nLocal;
  4339. return aPayload;
  4340. }
  4341. /*
  4342. ** For the entry that cursor pCur is point to, return as
  4343. ** many bytes of the key or data as are available on the local
  4344. ** b-tree page. Write the number of available bytes into pAmt.
  4345. **
  4346. ** The pointer returned is ephemeral. The key/data may move
  4347. ** or be destroyed on the next call to any Btree routine,
  4348. ** including calls from other threads against the same cache.
  4349. ** Hence, a mutex on the BtShared should be held prior to calling
  4350. ** this routine.
  4351. **
  4352. ** These routines is used to get quick access to key and data
  4353. ** in the common case where no overflow pages are used.
  4354. */
  4355. static byte[] sqlite3BtreeKeyFetch( BtCursor pCur, ref int pAmt, ref int outOffset )
  4356. {
  4357. byte[] p = null;
  4358. Debug.Assert( sqlite3_mutex_held( pCur.pBtree.db.mutex ) );
  4359. Debug.Assert( cursorHoldsMutex( pCur ) );
  4360. if ( ALWAYS( pCur.eState == CURSOR_VALID ) )
  4361. {
  4362. p = fetchPayload( pCur, ref pAmt, ref outOffset, false );
  4363. }
  4364. return p;
  4365. }
  4366. static byte[] sqlite3BtreeDataFetch( BtCursor pCur, ref int pAmt, ref int outOffset )
  4367. {
  4368. byte[] p = null;
  4369. Debug.Assert( sqlite3_mutex_held( pCur.pBtree.db.mutex ) );
  4370. Debug.Assert( cursorHoldsMutex( pCur ) );
  4371. if ( ALWAYS( pCur.eState == CURSOR_VALID ) )
  4372. {
  4373. p = fetchPayload( pCur, ref pAmt, ref outOffset, true );
  4374. }
  4375. return p;
  4376. }
  4377. /*
  4378. ** Move the cursor down to a new child page. The newPgno argument is the
  4379. ** page number of the child page to move to.
  4380. **
  4381. ** This function returns SQLITE_CORRUPT if the page-header flags field of
  4382. ** the new child page does not match the flags field of the parent (i.e.
  4383. ** if an intkey page appears to be the parent of a non-intkey page, or
  4384. ** vice-versa).
  4385. */
  4386. static int moveToChild( BtCursor pCur, u32 newPgno )
  4387. {
  4388. int rc;
  4389. int i = pCur.iPage;
  4390. MemPage pNewPage = new MemPage();
  4391. BtShared pBt = pCur.pBt;
  4392. Debug.Assert( cursorHoldsMutex( pCur ) );
  4393. Debug.Assert( pCur.eState == CURSOR_VALID );
  4394. Debug.Assert( pCur.iPage < BTCURSOR_MAX_DEPTH );
  4395. if ( pCur.iPage >= ( BTCURSOR_MAX_DEPTH - 1 ) )
  4396. {
  4397. return SQLITE_CORRUPT_BKPT();
  4398. }
  4399. rc = getAndInitPage( pBt, newPgno, ref pNewPage );
  4400. if ( rc != 0 )
  4401. return rc;
  4402. pCur.apPage[i + 1] = pNewPage;
  4403. pCur.aiIdx[i + 1] = 0;
  4404. pCur.iPage++;
  4405. pCur.info.nSize = 0;
  4406. pCur.validNKey = false;
  4407. if ( pNewPage.nCell < 1 || pNewPage.intKey != pCur.apPage[i].intKey )
  4408. {
  4409. return SQLITE_CORRUPT_BKPT();
  4410. }
  4411. return SQLITE_OK;
  4412. }
  4413. #if !NDEBUG
  4414. /*
  4415. ** Page pParent is an internal (non-leaf) tree page. This function
  4416. ** asserts that page number iChild is the left-child if the iIdx'th
  4417. ** cell in page pParent. Or, if iIdx is equal to the total number of
  4418. ** cells in pParent, that page number iChild is the right-child of
  4419. ** the page.
  4420. */
  4421. static void assertParentIndex( MemPage pParent, int iIdx, Pgno iChild )
  4422. {
  4423. Debug.Assert( iIdx <= pParent.nCell );
  4424. if ( iIdx == pParent.nCell )
  4425. {
  4426. Debug.Assert( sqlite3Get4byte( pParent.aData, pParent.hdrOffset + 8 ) == iChild );
  4427. }
  4428. else
  4429. {
  4430. Debug.Assert( sqlite3Get4byte( pParent.aData, findCell( pParent, iIdx ) ) == iChild );
  4431. }
  4432. }
  4433. #else
  4434. //# define assertParentIndex(x,y,z)
  4435. static void assertParentIndex(MemPage pParent, int iIdx, Pgno iChild) { }
  4436. #endif
  4437. /*
  4438. ** Move the cursor up to the parent page.
  4439. **
  4440. ** pCur.idx is set to the cell index that contains the pointer
  4441. ** to the page we are coming from. If we are coming from the
  4442. ** right-most child page then pCur.idx is set to one more than
  4443. ** the largest cell index.
  4444. */
  4445. static void moveToParent( BtCursor pCur )
  4446. {
  4447. Debug.Assert( cursorHoldsMutex( pCur ) );
  4448. Debug.Assert( pCur.eState == CURSOR_VALID );
  4449. Debug.Assert( pCur.iPage > 0 );
  4450. Debug.Assert( pCur.apPage[pCur.iPage] != null );
  4451. assertParentIndex(
  4452. pCur.apPage[pCur.iPage - 1],
  4453. pCur.aiIdx[pCur.iPage - 1],
  4454. pCur.apPage[pCur.iPage].pgno
  4455. );
  4456. releasePage( pCur.apPage[pCur.iPage] );
  4457. pCur.iPage--;
  4458. pCur.info.nSize = 0;
  4459. pCur.validNKey = false;
  4460. }
  4461. /*
  4462. ** Move the cursor to point to the root page of its b-tree structure.
  4463. **
  4464. ** If the table has a virtual root page, then the cursor is moved to point
  4465. ** to the virtual root page instead of the actual root page. A table has a
  4466. ** virtual root page when the actual root page contains no cells and a
  4467. ** single child page. This can only happen with the table rooted at page 1.
  4468. **
  4469. ** If the b-tree structure is empty, the cursor state is set to
  4470. ** CURSOR_INVALID. Otherwise, the cursor is set to point to the first
  4471. ** cell located on the root (or virtual root) page and the cursor state
  4472. ** is set to CURSOR_VALID.
  4473. **
  4474. ** If this function returns successfully, it may be assumed that the
  4475. ** page-header flags indicate that the [virtual] root-page is the expected
  4476. ** kind of b-tree page (i.e. if when opening the cursor the caller did not
  4477. ** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D,
  4478. ** indicating a table b-tree, or if the caller did specify a KeyInfo
  4479. ** structure the flags byte is set to 0x02 or 0x0A, indicating an index
  4480. ** b-tree).
  4481. */
  4482. static int moveToRoot( BtCursor pCur )
  4483. {
  4484. MemPage pRoot;
  4485. int rc = SQLITE_OK;
  4486. Btree p = pCur.pBtree;
  4487. BtShared pBt = p.pBt;
  4488. Debug.Assert( cursorHoldsMutex( pCur ) );
  4489. Debug.Assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
  4490. Debug.Assert( CURSOR_VALID < CURSOR_REQUIRESEEK );
  4491. Debug.Assert( CURSOR_FAULT > CURSOR_REQUIRESEEK );
  4492. if ( pCur.eState >= CURSOR_REQUIRESEEK )
  4493. {
  4494. if ( pCur.eState == CURSOR_FAULT )
  4495. {
  4496. Debug.Assert( pCur.skipNext != SQLITE_OK );
  4497. return pCur.skipNext;
  4498. }
  4499. sqlite3BtreeClearCursor( pCur );
  4500. }
  4501. if ( pCur.iPage >= 0 )
  4502. {
  4503. int i;
  4504. for ( i = 1; i <= pCur.iPage; i++ )
  4505. {
  4506. releasePage( pCur.apPage[i] );
  4507. }
  4508. pCur.iPage = 0;
  4509. }
  4510. else
  4511. {
  4512. rc = getAndInitPage( pBt, pCur.pgnoRoot, ref pCur.apPage[0] );
  4513. if ( rc != SQLITE_OK )
  4514. {
  4515. pCur.eState = CURSOR_INVALID;
  4516. return rc;
  4517. }
  4518. pCur.iPage = 0;
  4519. /* If pCur.pKeyInfo is not NULL, then the caller that opened this cursor
  4520. ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
  4521. ** NULL, the caller expects a table b-tree. If this is not the case,
  4522. ** return an SQLITE_CORRUPT error. */
  4523. Debug.Assert( pCur.apPage[0].intKey == 1 || pCur.apPage[0].intKey == 0 );
  4524. if ( ( pCur.pKeyInfo == null ) != ( pCur.apPage[0].intKey != 0 ) )
  4525. {
  4526. return SQLITE_CORRUPT_BKPT();
  4527. }
  4528. }
  4529. /* Assert that the root page is of the correct type. This must be the
  4530. ** case as the call to this function that loaded the root-page (either
  4531. ** this call or a previous invocation) would have detected corruption
  4532. ** if the assumption were not true, and it is not possible for the flags
  4533. ** byte to have been modified while this cursor is holding a reference
  4534. ** to the page. */
  4535. pRoot = pCur.apPage[0];
  4536. Debug.Assert( pRoot.pgno == pCur.pgnoRoot );
  4537. Debug.Assert( pRoot.isInit != 0 && ( pCur.pKeyInfo == null ) == ( pRoot.intKey != 0 ) );
  4538. pCur.aiIdx[0] = 0;
  4539. pCur.info.nSize = 0;
  4540. pCur.atLast = 0;
  4541. pCur.validNKey = false;
  4542. if ( pRoot.nCell == 0 && 0 == pRoot.leaf )
  4543. {
  4544. Pgno subpage;
  4545. if ( pRoot.pgno != 1 )
  4546. return SQLITE_CORRUPT_BKPT();
  4547. subpage = sqlite3Get4byte( pRoot.aData, pRoot.hdrOffset + 8 );
  4548. pCur.eState = CURSOR_VALID;
  4549. rc = moveToChild( pCur, subpage );
  4550. }
  4551. else
  4552. {
  4553. pCur.eState = ( ( pRoot.nCell > 0 ) ? CURSOR_VALID : CURSOR_INVALID );
  4554. }
  4555. return rc;
  4556. }
  4557. /*
  4558. ** Move the cursor down to the left-most leaf entry beneath the
  4559. ** entry to which it is currently pointing.
  4560. **
  4561. ** The left-most leaf is the one with the smallest key - the first
  4562. ** in ascending order.
  4563. */
  4564. static int moveToLeftmost( BtCursor pCur )
  4565. {
  4566. Pgno pgno;
  4567. int rc = SQLITE_OK;
  4568. MemPage pPage;
  4569. Debug.Assert( cursorHoldsMutex( pCur ) );
  4570. Debug.Assert( pCur.eState == CURSOR_VALID );
  4571. while ( rc == SQLITE_OK && 0 == ( pPage = pCur.apPage[pCur.iPage] ).leaf )
  4572. {
  4573. Debug.Assert( pCur.aiIdx[pCur.iPage] < pPage.nCell );
  4574. pgno = sqlite3Get4byte( pPage.aData, findCell( pPage, pCur.aiIdx[pCur.iPage] ) );
  4575. rc = moveToChild( pCur, pgno );
  4576. }
  4577. return rc;
  4578. }
  4579. /*
  4580. ** Move the cursor down to the right-most leaf entry beneath the
  4581. ** page to which it is currently pointing. Notice the difference
  4582. ** between moveToLeftmost() and moveToRightmost(). moveToLeftmost()
  4583. ** finds the left-most entry beneath the *entry* whereas moveToRightmost()
  4584. ** finds the right-most entry beneath the page*.
  4585. **
  4586. ** The right-most entry is the one with the largest key - the last
  4587. ** key in ascending order.
  4588. */
  4589. static int moveToRightmost( BtCursor pCur )
  4590. {
  4591. Pgno pgno;
  4592. int rc = SQLITE_OK;
  4593. MemPage pPage = null;
  4594. Debug.Assert( cursorHoldsMutex( pCur ) );
  4595. Debug.Assert( pCur.eState == CURSOR_VALID );
  4596. while ( rc == SQLITE_OK && 0 == ( pPage = pCur.apPage[pCur.iPage] ).leaf )
  4597. {
  4598. pgno = sqlite3Get4byte( pPage.aData, pPage.hdrOffset + 8 );
  4599. pCur.aiIdx[pCur.iPage] = pPage.nCell;
  4600. rc = moveToChild( pCur, pgno );
  4601. }
  4602. if ( rc == SQLITE_OK )
  4603. {
  4604. pCur.aiIdx[pCur.iPage] = (u16)( pPage.nCell - 1 );
  4605. pCur.info.nSize = 0;
  4606. pCur.validNKey = false;
  4607. }
  4608. return rc;
  4609. }
  4610. /* Move the cursor to the first entry in the table. Return SQLITE_OK
  4611. ** on success. Set pRes to 0 if the cursor actually points to something
  4612. ** or set pRes to 1 if the table is empty.
  4613. */
  4614. static int sqlite3BtreeFirst( BtCursor pCur, ref int pRes )
  4615. {
  4616. int rc;
  4617. Debug.Assert( cursorHoldsMutex( pCur ) );
  4618. Debug.Assert( sqlite3_mutex_held( pCur.pBtree.db.mutex ) );
  4619. rc = moveToRoot( pCur );
  4620. if ( rc == SQLITE_OK )
  4621. {
  4622. if ( pCur.eState == CURSOR_INVALID )
  4623. {
  4624. Debug.Assert( pCur.apPage[pCur.iPage].nCell == 0 );
  4625. pRes = 1;
  4626. }
  4627. else
  4628. {
  4629. Debug.Assert( pCur.apPage[pCur.iPage].nCell > 0 );
  4630. pRes = 0;
  4631. rc = moveToLeftmost( pCur );
  4632. }
  4633. }
  4634. return rc;
  4635. }
  4636. /* Move the cursor to the last entry in the table. Return SQLITE_OK
  4637. ** on success. Set pRes to 0 if the cursor actually points to something
  4638. ** or set pRes to 1 if the table is empty.
  4639. */
  4640. static int sqlite3BtreeLast( BtCursor pCur, ref int pRes )
  4641. {
  4642. int rc;
  4643. Debug.Assert( cursorHoldsMutex( pCur ) );
  4644. Debug.Assert( sqlite3_mutex_held( pCur.pBtree.db.mutex ) );
  4645. /* If the cursor already points to the last entry, this is a no-op. */
  4646. if ( CURSOR_VALID == pCur.eState && pCur.atLast != 0 )
  4647. {
  4648. #if SQLITE_DEBUG
  4649. /* This block serves to Debug.Assert() that the cursor really does point
  4650. ** to the last entry in the b-tree. */
  4651. int ii;
  4652. for ( ii = 0; ii < pCur.iPage; ii++ )
  4653. {
  4654. Debug.Assert( pCur.aiIdx[ii] == pCur.apPage[ii].nCell );
  4655. }
  4656. Debug.Assert( pCur.aiIdx[pCur.iPage] == pCur.apPage[pCur.iPage].nCell - 1 );
  4657. Debug.Assert( pCur.apPage[pCur.iPage].leaf != 0 );
  4658. #endif
  4659. return SQLITE_OK;
  4660. }
  4661. rc = moveToRoot( pCur );
  4662. if ( rc == SQLITE_OK )
  4663. {
  4664. if ( CURSOR_INVALID == pCur.eState )
  4665. {
  4666. Debug.Assert( pCur.apPage[pCur.iPage].nCell == 0 );
  4667. pRes = 1;
  4668. }
  4669. else
  4670. {
  4671. Debug.Assert( pCur.eState == CURSOR_VALID );
  4672. pRes = 0;
  4673. rc = moveToRightmost( pCur );
  4674. pCur.atLast = (u8)( rc == SQLITE_OK ? 1 : 0 );
  4675. }
  4676. }
  4677. return rc;
  4678. }
  4679. /* Move the cursor so that it points to an entry near the key
  4680. ** specified by pIdxKey or intKey. Return a success code.
  4681. **
  4682. ** For INTKEY tables, the intKey parameter is used. pIdxKey
  4683. ** must be NULL. For index tables, pIdxKey is used and intKey
  4684. ** is ignored.
  4685. **
  4686. ** If an exact match is not found, then the cursor is always
  4687. ** left pointing at a leaf page which would hold the entry if it
  4688. ** were present. The cursor might point to an entry that comes
  4689. ** before or after the key.
  4690. **
  4691. ** An integer is written into pRes which is the result of
  4692. ** comparing the key with the entry to which the cursor is
  4693. ** pointing. The meaning of the integer written into
  4694. ** pRes is as follows:
  4695. **
  4696. ** pRes<0 The cursor is left pointing at an entry that
  4697. ** is smaller than intKey/pIdxKey or if the table is empty
  4698. ** and the cursor is therefore left point to nothing.
  4699. **
  4700. ** pRes==null The cursor is left pointing at an entry that
  4701. ** exactly matches intKey/pIdxKey.
  4702. **
  4703. ** pRes>0 The cursor is left pointing at an entry that
  4704. ** is larger than intKey/pIdxKey.
  4705. **
  4706. */
  4707. static int sqlite3BtreeMovetoUnpacked(
  4708. BtCursor pCur, /* The cursor to be moved */
  4709. UnpackedRecord pIdxKey, /* Unpacked index key */
  4710. i64 intKey, /* The table key */
  4711. int biasRight, /* If true, bias the search to the high end */
  4712. ref int pRes /* Write search results here */
  4713. )
  4714. {
  4715. int rc;
  4716. Debug.Assert( cursorHoldsMutex( pCur ) );
  4717. Debug.Assert( sqlite3_mutex_held( pCur.pBtree.db.mutex ) );
  4718. // Not needed in C# // Debug.Assert( pRes != 0 );
  4719. Debug.Assert( ( pIdxKey == null ) == ( pCur.pKeyInfo == null ) );
  4720. /* If the cursor is already positioned at the point we are trying
  4721. ** to move to, then just return without doing any work */
  4722. if ( pCur.eState == CURSOR_VALID && pCur.validNKey
  4723. && pCur.apPage[0].intKey != 0
  4724. )
  4725. {
  4726. if ( pCur.info.nKey == intKey )
  4727. {
  4728. pRes = 0;
  4729. return SQLITE_OK;
  4730. }
  4731. if ( pCur.atLast != 0 && pCur.info.nKey < intKey )
  4732. {
  4733. pRes = -1;
  4734. return SQLITE_OK;
  4735. }
  4736. }
  4737. rc = moveToRoot( pCur );
  4738. if ( rc != 0 )
  4739. {
  4740. return rc;
  4741. }
  4742. Debug.Assert( pCur.apPage[pCur.iPage] != null );
  4743. Debug.Assert( pCur.apPage[pCur.iPage].isInit != 0 );
  4744. Debug.Assert( pCur.apPage[pCur.iPage].nCell > 0 || pCur.eState == CURSOR_INVALID );
  4745. if ( pCur.eState == CURSOR_INVALID )
  4746. {
  4747. pRes = -1;
  4748. Debug.Assert( pCur.apPage[pCur.iPage].nCell == 0 );
  4749. return SQLITE_OK;
  4750. }
  4751. Debug.Assert( pCur.apPage[0].intKey != 0 || pIdxKey != null );
  4752. for ( ; ; )
  4753. {
  4754. int lwr, upr;
  4755. Pgno chldPg;
  4756. MemPage pPage = pCur.apPage[pCur.iPage];
  4757. int c;
  4758. /* pPage.nCell must be greater than zero. If this is the root-page
  4759. ** the cursor would have been INVALID above and this for(;;) loop
  4760. ** not run. If this is not the root-page, then the moveToChild() routine
  4761. ** would have already detected db corruption. Similarly, pPage must
  4762. ** be the right kind (index or table) of b-tree page. Otherwise
  4763. ** a moveToChild() or moveToRoot() call would have detected corruption. */
  4764. Debug.Assert( pPage.nCell > 0 );
  4765. Debug.Assert( pPage.intKey == ( ( pIdxKey == null ) ? 1 : 0 ) );
  4766. lwr = 0;
  4767. upr = pPage.nCell - 1;
  4768. if ( biasRight != 0 )
  4769. {
  4770. pCur.aiIdx[pCur.iPage] = (u16)upr;
  4771. }
  4772. else
  4773. {
  4774. pCur.aiIdx[pCur.iPage] = (u16)( ( upr + lwr ) / 2 );
  4775. }
  4776. for ( ; ; )
  4777. {
  4778. int idx = pCur.aiIdx[pCur.iPage]; /* Index of current cell in pPage */
  4779. int pCell; /* Pointer to current cell in pPage */
  4780. pCur.info.nSize = 0;
  4781. pCell = findCell( pPage, idx ) + pPage.childPtrSize;
  4782. if ( pPage.intKey != 0 )
  4783. {
  4784. i64 nCellKey = 0;
  4785. if ( pPage.hasData != 0 )
  4786. {
  4787. u32 Dummy0 = 0;
  4788. pCell += getVarint32( pPage.aData, pCell, ref Dummy0 );
  4789. }
  4790. getVarint( pPage.aData, pCell, ref nCellKey );
  4791. if ( nCellKey == intKey )
  4792. {
  4793. c = 0;
  4794. }
  4795. else if ( nCellKey < intKey )
  4796. {
  4797. c = -1;
  4798. }
  4799. else
  4800. {
  4801. Debug.Assert( nCellKey > intKey );
  4802. c = +1;
  4803. }
  4804. pCur.validNKey = true;
  4805. pCur.info.nKey = nCellKey;
  4806. }
  4807. else
  4808. {
  4809. /* The maximum supported page-size is 65536 bytes. This means that
  4810. ** the maximum number of record bytes stored on an index B-Tree
  4811. ** page is less than 16384 bytes and may be stored as a 2-byte
  4812. ** varint. This information is used to attempt to avoid parsing
  4813. ** the entire cell by checking for the cases where the record is
  4814. ** stored entirely within the b-tree page by inspecting the first
  4815. ** 2 bytes of the cell.
  4816. */
  4817. int nCell = pPage.aData[pCell + 0]; //pCell[0];
  4818. if ( 0 == ( nCell & 0x80 ) && nCell <= pPage.maxLocal )
  4819. {
  4820. /* This branch runs if the record-size field of the cell is a
  4821. ** single byte varint and the record fits entirely on the main
  4822. ** b-tree page. */
  4823. c = sqlite3VdbeRecordCompare( nCell, pPage.aData, pCell + 1, pIdxKey ); //c = sqlite3VdbeRecordCompare( nCell, (void*)&pCell[1], pIdxKey );
  4824. }
  4825. else if ( 0 == ( pPage.aData[pCell + 1] & 0x80 )//!(pCell[1] & 0x80)
  4826. && ( nCell = ( ( nCell & 0x7f ) << 7 ) + pPage.aData[pCell + 1] ) <= pPage.maxLocal//pCell[1])<=pPage.maxLocal
  4827. )
  4828. {
  4829. /* The record-size field is a 2 byte varint and the record
  4830. ** fits entirely on the main b-tree page. */
  4831. c = sqlite3VdbeRecordCompare( nCell, pPage.aData, pCell + 2, pIdxKey ); //c = sqlite3VdbeRecordCompare( nCell, (void*)&pCell[2], pIdxKey );
  4832. }
  4833. else
  4834. {
  4835. /* The record flows over onto one or more overflow pages. In
  4836. ** this case the whole cell needs to be parsed, a buffer allocated
  4837. ** and accessPayload() used to retrieve the record into the
  4838. ** buffer before VdbeRecordCompare() can be called. */
  4839. u8[] pCellKey;
  4840. u8[] pCellBody = new u8[pPage.aData.Length - pCell + pPage.childPtrSize];
  4841. Buffer.BlockCopy( pPage.aData, pCell - pPage.childPtrSize, pCellBody, 0, pCellBody.Length );// u8 * const pCellBody = pCell - pPage->childPtrSize;
  4842. btreeParseCellPtr( pPage, pCellBody, ref pCur.info );
  4843. nCell = (int)pCur.info.nKey;
  4844. pCellKey = sqlite3Malloc( nCell );
  4845. //if ( pCellKey == null )
  4846. //{
  4847. // rc = SQLITE_NOMEM;
  4848. // goto moveto_finish;
  4849. //}
  4850. rc = accessPayload( pCur, 0, (u32)nCell, pCellKey, 0 );
  4851. if ( rc != 0 )
  4852. {
  4853. pCellKey = null;// sqlite3_free(ref pCellKey );
  4854. goto moveto_finish;
  4855. }
  4856. c = sqlite3VdbeRecordCompare( nCell, pCellKey, pIdxKey );
  4857. pCellKey = null;// sqlite3_free(ref pCellKey );
  4858. }
  4859. }
  4860. if ( c == 0 )
  4861. {
  4862. if ( pPage.intKey != 0 && 0 == pPage.leaf )
  4863. {
  4864. lwr = idx;
  4865. upr = lwr - 1;
  4866. break;
  4867. }
  4868. else
  4869. {
  4870. pRes = 0;
  4871. rc = SQLITE_OK;
  4872. goto moveto_finish;
  4873. }
  4874. }
  4875. if ( c < 0 )
  4876. {
  4877. lwr = idx + 1;
  4878. }
  4879. else
  4880. {
  4881. upr = idx - 1;
  4882. }
  4883. if ( lwr > upr )
  4884. {
  4885. break;
  4886. }
  4887. pCur.aiIdx[pCur.iPage] = (u16)( ( lwr + upr ) / 2 );
  4888. }
  4889. Debug.Assert( lwr == upr + 1 );
  4890. Debug.Assert( pPage.isInit != 0 );
  4891. if ( pPage.leaf != 0 )
  4892. {
  4893. chldPg = 0;
  4894. }
  4895. else if ( lwr >= pPage.nCell )
  4896. {
  4897. chldPg = sqlite3Get4byte( pPage.aData, pPage.hdrOffset + 8 );
  4898. }
  4899. else
  4900. {
  4901. chldPg = sqlite3Get4byte( pPage.aData, findCell( pPage, lwr ) );
  4902. }
  4903. if ( chldPg == 0 )
  4904. {
  4905. Debug.Assert( pCur.aiIdx[pCur.iPage] < pCur.apPage[pCur.iPage].nCell );
  4906. pRes = c;
  4907. rc = SQLITE_OK;
  4908. goto moveto_finish;
  4909. }
  4910. pCur.aiIdx[pCur.iPage] = (u16)lwr;
  4911. pCur.info.nSize = 0;
  4912. pCur.validNKey = false;
  4913. rc = moveToChild( pCur, chldPg );
  4914. if ( rc != 0 )
  4915. goto moveto_finish;
  4916. }
  4917. moveto_finish:
  4918. return rc;
  4919. }
  4920. /*
  4921. ** Return TRUE if the cursor is not pointing at an entry of the table.
  4922. **
  4923. ** TRUE will be returned after a call to sqlite3BtreeNext() moves
  4924. ** past the last entry in the table or sqlite3BtreePrev() moves past
  4925. ** the first entry. TRUE is also returned if the table is empty.
  4926. */
  4927. static bool sqlite3BtreeEof( BtCursor pCur )
  4928. {
  4929. /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
  4930. ** have been deleted? This API will need to change to return an error code
  4931. ** as well as the boolean result value.
  4932. */
  4933. return ( CURSOR_VALID != pCur.eState );
  4934. }
  4935. /*
  4936. ** Advance the cursor to the next entry in the database. If
  4937. ** successful then set pRes=0. If the cursor
  4938. ** was already pointing to the last entry in the database before
  4939. ** this routine was called, then set pRes=1.
  4940. */
  4941. static int sqlite3BtreeNext( BtCursor pCur, ref int pRes )
  4942. {
  4943. int rc;
  4944. int idx;
  4945. MemPage pPage;
  4946. Debug.Assert( cursorHoldsMutex( pCur ) );
  4947. rc = restoreCursorPosition( pCur );
  4948. if ( rc != SQLITE_OK )
  4949. {
  4950. return rc;
  4951. }
  4952. // Not needed in C# // Debug.Assert( pRes != 0 );
  4953. if ( CURSOR_INVALID == pCur.eState )
  4954. {
  4955. pRes = 1;
  4956. return SQLITE_OK;
  4957. }
  4958. if ( pCur.skipNext > 0 )
  4959. {
  4960. pCur.skipNext = 0;
  4961. pRes = 0;
  4962. return SQLITE_OK;
  4963. }
  4964. pCur.skipNext = 0;
  4965. pPage = pCur.apPage[pCur.iPage];
  4966. idx = ++pCur.aiIdx[pCur.iPage];
  4967. Debug.Assert( pPage.isInit != 0 );
  4968. Debug.Assert( idx <= pPage.nCell );
  4969. pCur.info.nSize = 0;
  4970. pCur.validNKey = false;
  4971. if ( idx >= pPage.nCell )
  4972. {
  4973. if ( 0 == pPage.leaf )
  4974. {
  4975. rc = moveToChild( pCur, sqlite3Get4byte( pPage.aData, pPage.hdrOffset + 8 ) );
  4976. if ( rc != 0 )
  4977. return rc;
  4978. rc = moveToLeftmost( pCur );
  4979. pRes = 0;
  4980. return rc;
  4981. }
  4982. do
  4983. {
  4984. if ( pCur.iPage == 0 )
  4985. {
  4986. pRes = 1;
  4987. pCur.eState = CURSOR_INVALID;
  4988. return SQLITE_OK;
  4989. }
  4990. moveToParent( pCur );
  4991. pPage = pCur.apPage[pCur.iPage];
  4992. } while ( pCur.aiIdx[pCur.iPage] >= pPage.nCell );
  4993. pRes = 0;
  4994. if ( pPage.intKey != 0 )
  4995. {
  4996. rc = sqlite3BtreeNext( pCur, ref pRes );
  4997. }
  4998. else
  4999. {
  5000. rc = SQLITE_OK;
  5001. }
  5002. return rc;
  5003. }
  5004. pRes = 0;
  5005. if ( pPage.leaf != 0 )
  5006. {
  5007. return SQLITE_OK;
  5008. }
  5009. rc = moveToLeftmost( pCur );
  5010. return rc;
  5011. }
  5012. /*
  5013. ** Step the cursor to the back to the previous entry in the database. If
  5014. ** successful then set pRes=0. If the cursor
  5015. ** was already pointing to the first entry in the database before
  5016. ** this routine was called, then set pRes=1.
  5017. */
  5018. static int sqlite3BtreePrevious( BtCursor pCur, ref int pRes )
  5019. {
  5020. int rc;
  5021. MemPage pPage;
  5022. Debug.Assert( cursorHoldsMutex( pCur ) );
  5023. rc = restoreCursorPosition( pCur );
  5024. if ( rc != SQLITE_OK )
  5025. {
  5026. return rc;
  5027. }
  5028. pCur.atLast = 0;
  5029. if ( CURSOR_INVALID == pCur.eState )
  5030. {
  5031. pRes = 1;
  5032. return SQLITE_OK;
  5033. }
  5034. if ( pCur.skipNext < 0 )
  5035. {
  5036. pCur.skipNext = 0;
  5037. pRes = 0;
  5038. return SQLITE_OK;
  5039. }
  5040. pCur.skipNext = 0;
  5041. pPage = pCur.apPage[pCur.iPage];
  5042. Debug.Assert( pPage.isInit != 0 );
  5043. if ( 0 == pPage.leaf )
  5044. {
  5045. int idx = pCur.aiIdx[pCur.iPage];
  5046. rc = moveToChild( pCur, sqlite3Get4byte( pPage.aData, findCell( pPage, idx ) ) );
  5047. if ( rc != 0 )
  5048. {
  5049. return rc;
  5050. }
  5051. rc = moveToRightmost( pCur );
  5052. }
  5053. else
  5054. {
  5055. while ( pCur.aiIdx[pCur.iPage] == 0 )
  5056. {
  5057. if ( pCur.iPage == 0 )
  5058. {
  5059. pCur.eState = CURSOR_INVALID;
  5060. pRes = 1;
  5061. return SQLITE_OK;
  5062. }
  5063. moveToParent( pCur );
  5064. }
  5065. pCur.info.nSize = 0;
  5066. pCur.validNKey = false;
  5067. pCur.aiIdx[pCur.iPage]--;
  5068. pPage = pCur.apPage[pCur.iPage];
  5069. if ( pPage.intKey != 0 && 0 == pPage.leaf )
  5070. {
  5071. rc = sqlite3BtreePrevious( pCur, ref pRes );
  5072. }
  5073. else
  5074. {
  5075. rc = SQLITE_OK;
  5076. }
  5077. }
  5078. pRes = 0;
  5079. return rc;
  5080. }
  5081. /*
  5082. ** Allocate a new page from the database file.
  5083. **
  5084. ** The new page is marked as dirty. (In other words, sqlite3PagerWrite()
  5085. ** has already been called on the new page.) The new page has also
  5086. ** been referenced and the calling routine is responsible for calling
  5087. ** sqlite3PagerUnref() on the new page when it is done.
  5088. **
  5089. ** SQLITE_OK is returned on success. Any other return value indicates
  5090. ** an error. ppPage and pPgno are undefined in the event of an error.
  5091. ** Do not invoke sqlite3PagerUnref() on ppPage if an error is returned.
  5092. **
  5093. ** If the "nearby" parameter is not 0, then a (feeble) effort is made to
  5094. ** locate a page close to the page number "nearby". This can be used in an
  5095. ** attempt to keep related pages close to each other in the database file,
  5096. ** which in turn can make database access faster.
  5097. **
  5098. ** If the "exact" parameter is not 0, and the page-number nearby exists
  5099. ** anywhere on the free-list, then it is guarenteed to be returned. This
  5100. ** is only used by auto-vacuum databases when allocating a new table.
  5101. */
  5102. static int allocateBtreePage(
  5103. BtShared pBt,
  5104. ref MemPage ppPage,
  5105. ref Pgno pPgno,
  5106. Pgno nearby,
  5107. u8 exact
  5108. )
  5109. {
  5110. MemPage pPage1;
  5111. int rc;
  5112. u32 n; /* Number of pages on the freelist */
  5113. u32 k; /* Number of leaves on the trunk of the freelist */
  5114. MemPage pTrunk = null;
  5115. MemPage pPrevTrunk = null;
  5116. Pgno mxPage; /* Total size of the database file */
  5117. Debug.Assert( sqlite3_mutex_held( pBt.mutex ) );
  5118. pPage1 = pBt.pPage1;
  5119. mxPage = btreePagecount( pBt );
  5120. n = sqlite3Get4byte( pPage1.aData, 36 );
  5121. testcase( n == mxPage - 1 );
  5122. if ( n >= mxPage )
  5123. {
  5124. return SQLITE_CORRUPT_BKPT();
  5125. }
  5126. if ( n > 0 )
  5127. {
  5128. /* There are pages on the freelist. Reuse one of those pages. */
  5129. Pgno iTrunk;
  5130. u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
  5131. /* If the 'exact' parameter was true and a query of the pointer-map
  5132. ** shows that the page 'nearby' is somewhere on the free-list, then
  5133. ** the entire-list will be searched for that page.
  5134. */
  5135. #if !SQLITE_OMIT_AUTOVACUUM
  5136. if ( exact != 0 && nearby <= mxPage )
  5137. {
  5138. u8 eType = 0;
  5139. Debug.Assert( nearby > 0 );
  5140. Debug.Assert( pBt.autoVacuum );
  5141. u32 Dummy0 = 0;
  5142. rc = ptrmapGet( pBt, nearby, ref eType, ref Dummy0 );
  5143. if ( rc != 0 )
  5144. return rc;
  5145. if ( eType == PTRMAP_FREEPAGE )
  5146. {
  5147. searchList = 1;
  5148. }
  5149. pPgno = nearby;
  5150. }
  5151. #endif
  5152. /* Decrement the free-list count by 1. Set iTrunk to the index of the
  5153. ** first free-list trunk page. iPrevTrunk is initially 1.
  5154. */
  5155. rc = sqlite3PagerWrite( pPage1.pDbPage );
  5156. if ( rc != 0 )
  5157. return rc;
  5158. sqlite3Put4byte( pPage1.aData, (u32)36, n - 1 );
  5159. /* The code within this loop is run only once if the 'searchList' variable
  5160. ** is not true. Otherwise, it runs once for each trunk-page on the
  5161. ** free-list until the page 'nearby' is located.
  5162. */
  5163. do
  5164. {
  5165. pPrevTrunk = pTrunk;
  5166. if ( pPrevTrunk != null )
  5167. {
  5168. iTrunk = sqlite3Get4byte( pPrevTrunk.aData, 0 );
  5169. }
  5170. else
  5171. {
  5172. iTrunk = sqlite3Get4byte( pPage1.aData, 32 );
  5173. }
  5174. testcase( iTrunk == mxPage );
  5175. if ( iTrunk > mxPage )
  5176. {
  5177. rc = SQLITE_CORRUPT_BKPT();
  5178. }
  5179. else
  5180. {
  5181. rc = btreeGetPage( pBt, iTrunk, ref pTrunk, 0 );
  5182. }
  5183. if ( rc != 0 )
  5184. {
  5185. pTrunk = null;
  5186. goto end_allocate_page;
  5187. }
  5188. k = sqlite3Get4byte( pTrunk.aData, 4 );
  5189. if ( k == 0 && 0 == searchList )
  5190. {
  5191. /* The trunk has no leaves and the list is not being searched.
  5192. ** So extract the trunk page itself and use it as the newly
  5193. ** allocated page */
  5194. Debug.Assert( pPrevTrunk == null );
  5195. rc = sqlite3PagerWrite( pTrunk.pDbPage );
  5196. if ( rc != 0 )
  5197. {
  5198. goto end_allocate_page;
  5199. }
  5200. pPgno = iTrunk;
  5201. Buffer.BlockCopy( pTrunk.aData, 0, pPage1.aData, 32, 4 );//memcpy( pPage1.aData[32], ref pTrunk.aData[0], 4 );
  5202. ppPage = pTrunk;
  5203. pTrunk = null;
  5204. TRACE( "ALLOCATE: %d trunk - %d free pages left\n", pPgno, n - 1 );
  5205. }
  5206. else if ( k > (u32)( pBt.usableSize / 4 - 2 ) )
  5207. {
  5208. /* Value of k is out of range. Database corruption */
  5209. rc = SQLITE_CORRUPT_BKPT();
  5210. goto end_allocate_page;
  5211. #if !SQLITE_OMIT_AUTOVACUUM
  5212. }
  5213. else if ( searchList != 0 && nearby == iTrunk )
  5214. {
  5215. /* The list is being searched and this trunk page is the page
  5216. ** to allocate, regardless of whether it has leaves.
  5217. */
  5218. Debug.Assert( pPgno == iTrunk );
  5219. ppPage = pTrunk;
  5220. searchList = 0;
  5221. rc = sqlite3PagerWrite( pTrunk.pDbPage );
  5222. if ( rc != 0 )
  5223. {
  5224. goto end_allocate_page;
  5225. }
  5226. if ( k == 0 )
  5227. {
  5228. if ( null == pPrevTrunk )
  5229. {
  5230. //memcpy(pPage1.aData[32], pTrunk.aData[0], 4);
  5231. pPage1.aData[32 + 0] = pTrunk.aData[0 + 0];
  5232. pPage1.aData[32 + 1] = pTrunk.aData[0 + 1];
  5233. pPage1.aData[32 + 2] = pTrunk.aData[0 + 2];
  5234. pPage1.aData[32 + 3] = pTrunk.aData[0 + 3];
  5235. }
  5236. else
  5237. {
  5238. rc = sqlite3PagerWrite( pPrevTrunk.pDbPage );
  5239. if ( rc != SQLITE_OK )
  5240. {
  5241. goto end_allocate_page;
  5242. }
  5243. //memcpy(pPrevTrunk.aData[0], pTrunk.aData[0], 4);
  5244. pPrevTrunk.aData[0 + 0] = pTrunk.aData[0 + 0];
  5245. pPrevTrunk.aData[0 + 1] = pTrunk.aData[0 + 1];
  5246. pPrevTrunk.aData[0 + 2] = pTrunk.aData[0 + 2];
  5247. pPrevTrunk.aData[0 + 3] = pTrunk.aData[0 + 3];
  5248. }
  5249. }
  5250. else
  5251. {
  5252. /* The trunk page is required by the caller but it contains
  5253. ** pointers to free-list leaves. The first leaf becomes a trunk
  5254. ** page in this case.
  5255. */
  5256. MemPage pNewTrunk = new MemPage();
  5257. Pgno iNewTrunk = sqlite3Get4byte( pTrunk.aData, 8 );
  5258. if ( iNewTrunk > mxPage )
  5259. {
  5260. rc = SQLITE_CORRUPT_BKPT();
  5261. goto end_allocate_page;
  5262. }
  5263. testcase( iNewTrunk == mxPage );
  5264. rc = btreeGetPage( pBt, iNewTrunk, ref pNewTrunk, 0 );
  5265. if ( rc != SQLITE_OK )
  5266. {
  5267. goto end_allocate_page;
  5268. }
  5269. rc = sqlite3PagerWrite( pNewTrunk.pDbPage );
  5270. if ( rc != SQLITE_OK )
  5271. {
  5272. releasePage( pNewTrunk );
  5273. goto end_allocate_page;
  5274. }
  5275. //memcpy(pNewTrunk.aData[0], pTrunk.aData[0], 4);
  5276. pNewTrunk.aData[0 + 0] = pTrunk.aData[0 + 0];
  5277. pNewTrunk.aData[0 + 1] = pTrunk.aData[0 + 1];
  5278. pNewTrunk.aData[0 + 2] = pTrunk.aData[0 + 2];
  5279. pNewTrunk.aData[0 + 3] = pTrunk.aData[0 + 3];
  5280. sqlite3Put4byte( pNewTrunk.aData, (u32)4, (u32)( k - 1 ) );
  5281. Buffer.BlockCopy( pTrunk.aData, 12, pNewTrunk.aData, 8, (int)( k - 1 ) * 4 );//memcpy( pNewTrunk.aData[8], ref pTrunk.aData[12], ( k - 1 ) * 4 );
  5282. releasePage( pNewTrunk );
  5283. if ( null == pPrevTrunk )
  5284. {
  5285. Debug.Assert( sqlite3PagerIswriteable( pPage1.pDbPage ) );
  5286. sqlite3Put4byte( pPage1.aData, (u32)32, iNewTrunk );
  5287. }
  5288. else
  5289. {
  5290. rc = sqlite3PagerWrite( pPrevTrunk.pDbPage );
  5291. if ( rc != 0 )
  5292. {
  5293. goto end_allocate_page;
  5294. }
  5295. sqlite3Put4byte( pPrevTrunk.aData, (u32)0, iNewTrunk );
  5296. }
  5297. }
  5298. pTrunk = null;
  5299. TRACE( "ALLOCATE: %d trunk - %d free pages left\n", pPgno, n - 1 );
  5300. #endif
  5301. }
  5302. else if ( k > 0 )
  5303. {
  5304. /* Extract a leaf from the trunk */
  5305. u32 closest;
  5306. Pgno iPage;
  5307. byte[] aData = pTrunk.aData;
  5308. rc = sqlite3PagerWrite( pTrunk.pDbPage );
  5309. if ( rc != 0 )
  5310. {
  5311. goto end_allocate_page;
  5312. }
  5313. if ( nearby > 0 )
  5314. {
  5315. u32 i;
  5316. int dist;
  5317. closest = 0;
  5318. dist = (int)( sqlite3Get4byte( aData, 8 ) - nearby );
  5319. if ( dist < 0 )
  5320. dist = -dist;
  5321. for ( i = 1; i < k; i++ )
  5322. {
  5323. int d2 = (int)( sqlite3Get4byte( aData, 8 + i * 4 ) - nearby );
  5324. if ( d2 < 0 )
  5325. d2 = -d2;
  5326. if ( d2 < dist )
  5327. {
  5328. closest = i;
  5329. dist = d2;
  5330. }
  5331. }
  5332. }
  5333. else
  5334. {
  5335. closest = 0;
  5336. }
  5337. iPage = sqlite3Get4byte( aData, 8 + closest * 4 );
  5338. testcase( iPage == mxPage );
  5339. if ( iPage > mxPage )
  5340. {
  5341. rc = SQLITE_CORRUPT_BKPT();
  5342. goto end_allocate_page;
  5343. }
  5344. testcase( iPage == mxPage );
  5345. if ( 0 == searchList || iPage == nearby )
  5346. {
  5347. int noContent;
  5348. pPgno = iPage;
  5349. TRACE( "ALLOCATE: %d was leaf %d of %d on trunk %d" +
  5350. ": %d more free pages\n",
  5351. pPgno, closest + 1, k, pTrunk.pgno, n - 1 );
  5352. if ( closest < k - 1 )
  5353. {
  5354. Buffer.BlockCopy( aData, (int)( 4 + k * 4 ), aData, 8 + (int)closest * 4, 4 );//memcpy( aData[8 + closest * 4], ref aData[4 + k * 4], 4 );
  5355. }
  5356. sqlite3Put4byte( aData, (u32)4, ( k - 1 ) );// sqlite3Put4byte( aData, 4, k - 1 );
  5357. Debug.Assert( sqlite3PagerIswriteable( pTrunk.pDbPage ) );
  5358. noContent = !btreeGetHasContent( pBt, pPgno ) ? 1 : 0;
  5359. rc = btreeGetPage( pBt, pPgno, ref ppPage, noContent );
  5360. if ( rc == SQLITE_OK )
  5361. {
  5362. rc = sqlite3PagerWrite( ( ppPage ).pDbPage );
  5363. if ( rc != SQLITE_OK )
  5364. {
  5365. releasePage( ppPage );
  5366. }
  5367. }
  5368. searchList = 0;
  5369. }
  5370. }
  5371. releasePage( pPrevTrunk );
  5372. pPrevTrunk = null;
  5373. } while ( searchList != 0 );
  5374. }
  5375. else
  5376. {
  5377. /* There are no pages on the freelist, so create a new page at the
  5378. ** end of the file */
  5379. rc = sqlite3PagerWrite( pBt.pPage1.pDbPage );
  5380. if ( rc != 0 )
  5381. return rc;
  5382. pBt.nPage++;
  5383. if ( pBt.nPage == PENDING_BYTE_PAGE( pBt ) )
  5384. pBt.nPage++;
  5385. #if !SQLITE_OMIT_AUTOVACUUM
  5386. if ( pBt.autoVacuum && PTRMAP_ISPAGE( pBt, pBt.nPage ) )
  5387. {
  5388. /* If pPgno refers to a pointer-map page, allocate two new pages
  5389. ** at the end of the file instead of one. The first allocated page
  5390. ** becomes a new pointer-map page, the second is used by the caller.
  5391. */
  5392. MemPage pPg = null;
  5393. TRACE( "ALLOCATE: %d from end of file (pointer-map page)\n", pPgno );
  5394. Debug.Assert( pBt.nPage != PENDING_BYTE_PAGE( pBt ) );
  5395. rc = btreeGetPage( pBt, pBt.nPage, ref pPg, 1 );
  5396. if ( rc == SQLITE_OK )
  5397. {
  5398. rc = sqlite3PagerWrite( pPg.pDbPage );
  5399. releasePage( pPg );
  5400. }
  5401. if ( rc != 0 )
  5402. return rc;
  5403. pBt.nPage++;
  5404. if ( pBt.nPage == PENDING_BYTE_PAGE( pBt ) )
  5405. {
  5406. pBt.nPage++;
  5407. }
  5408. }
  5409. #endif
  5410. sqlite3Put4byte( pBt.pPage1.aData, (u32)28, pBt.nPage );
  5411. pPgno = pBt.nPage;
  5412. Debug.Assert( pPgno != PENDING_BYTE_PAGE( pBt ) );
  5413. rc = btreeGetPage( pBt, pPgno, ref ppPage, 1 );
  5414. if ( rc != 0 )
  5415. return rc;
  5416. rc = sqlite3PagerWrite( ( ppPage ).pDbPage );
  5417. if ( rc != SQLITE_OK )
  5418. {
  5419. releasePage( ppPage );
  5420. }
  5421. TRACE( "ALLOCATE: %d from end of file\n", pPgno );
  5422. }
  5423. Debug.Assert( pPgno != PENDING_BYTE_PAGE( pBt ) );
  5424. end_allocate_page:
  5425. releasePage( pTrunk );
  5426. releasePage( pPrevTrunk );
  5427. if ( rc == SQLITE_OK )
  5428. {
  5429. if ( sqlite3PagerPageRefcount( ( ppPage ).pDbPage ) > 1 )
  5430. {
  5431. releasePage( ppPage );
  5432. return SQLITE_CORRUPT_BKPT();
  5433. }
  5434. ( ppPage ).isInit = 0;
  5435. }
  5436. else
  5437. {
  5438. ppPage = null;
  5439. }
  5440. return rc;
  5441. }
  5442. /*
  5443. ** This function is used to add page iPage to the database file free-list.
  5444. ** It is assumed that the page is not already a part of the free-list.
  5445. **
  5446. ** The value passed as the second argument to this function is optional.
  5447. ** If the caller happens to have a pointer to the MemPage object
  5448. ** corresponding to page iPage handy, it may pass it as the second value.
  5449. ** Otherwise, it may pass NULL.
  5450. **
  5451. ** If a pointer to a MemPage object is passed as the second argument,
  5452. ** its reference count is not altered by this function.
  5453. */
  5454. static int freePage2( BtShared pBt, MemPage pMemPage, Pgno iPage )
  5455. {
  5456. MemPage pTrunk = null; /* Free-list trunk page */
  5457. Pgno iTrunk = 0; /* Page number of free-list trunk page */
  5458. MemPage pPage1 = pBt.pPage1; /* Local reference to page 1 */
  5459. MemPage pPage; /* Page being freed. May be NULL. */
  5460. int rc; /* Return Code */
  5461. int nFree; /* Initial number of pages on free-list */
  5462. Debug.Assert( sqlite3_mutex_held( pBt.mutex ) );
  5463. Debug.Assert( iPage > 1 );
  5464. Debug.Assert( null == pMemPage || pMemPage.pgno == iPage );
  5465. if ( pMemPage != null )
  5466. {
  5467. pPage = pMemPage;
  5468. sqlite3PagerRef( pPage.pDbPage );
  5469. }
  5470. else
  5471. {
  5472. pPage = btreePageLookup( pBt, iPage );
  5473. }
  5474. /* Increment the free page count on pPage1 */
  5475. rc = sqlite3PagerWrite( pPage1.pDbPage );
  5476. if ( rc != 0 )
  5477. goto freepage_out;
  5478. nFree = (int)sqlite3Get4byte( pPage1.aData, 36 );
  5479. sqlite3Put4byte( pPage1.aData, 36, nFree + 1 );
  5480. if ( pBt.secureDelete )
  5481. {
  5482. /* If the secure_delete option is enabled, then
  5483. ** always fully overwrite deleted information with zeros.
  5484. */
  5485. if ( ( null == pPage && ( ( rc = btreeGetPage( pBt, iPage, ref pPage, 0 ) ) != 0 ) )
  5486. || ( ( rc = sqlite3PagerWrite( pPage.pDbPage ) ) != 0 )
  5487. )
  5488. {
  5489. goto freepage_out;
  5490. }
  5491. Array.Clear( pPage.aData, 0, (int)pPage.pBt.pageSize );//memset(pPage->aData, 0, pPage->pBt->pageSize);
  5492. }
  5493. /* If the database supports auto-vacuum, write an entry in the pointer-map
  5494. ** to indicate that the page is free.
  5495. */
  5496. #if !SQLITE_OMIT_AUTOVACUUM // if ( ISAUTOVACUUM )
  5497. if ( pBt.autoVacuum )
  5498. #else
  5499. if (false)
  5500. #endif
  5501. {
  5502. ptrmapPut( pBt, iPage, PTRMAP_FREEPAGE, 0, ref rc );
  5503. if ( rc != 0 )
  5504. goto freepage_out;
  5505. }
  5506. /* Now manipulate the actual database free-list structure. There are two
  5507. ** possibilities. If the free-list is currently empty, or if the first
  5508. ** trunk page in the free-list is full, then this page will become a
  5509. ** new free-list trunk page. Otherwise, it will become a leaf of the
  5510. ** first trunk page in the current free-list. This block tests if it
  5511. ** is possible to add the page as a new free-list leaf.
  5512. */
  5513. if ( nFree != 0 )
  5514. {
  5515. u32 nLeaf; /* Initial number of leaf cells on trunk page */
  5516. iTrunk = sqlite3Get4byte( pPage1.aData, 32 );
  5517. rc = btreeGetPage( pBt, iTrunk, ref pTrunk, 0 );
  5518. if ( rc != SQLITE_OK )
  5519. {
  5520. goto freepage_out;
  5521. }
  5522. nLeaf = sqlite3Get4byte( pTrunk.aData, 4 );
  5523. Debug.Assert( pBt.usableSize > 32 );
  5524. if ( nLeaf > (u32)pBt.usableSize / 4 - 2 )
  5525. {
  5526. rc = SQLITE_CORRUPT_BKPT();
  5527. goto freepage_out;
  5528. }
  5529. if ( nLeaf < (u32)pBt.usableSize / 4 - 8 )
  5530. {
  5531. /* In this case there is room on the trunk page to insert the page
  5532. ** being freed as a new leaf.
  5533. **
  5534. ** Note that the trunk page is not really full until it contains
  5535. ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
  5536. ** coded. But due to a coding error in versions of SQLite prior to
  5537. ** 3.6.0, databases with freelist trunk pages holding more than
  5538. ** usableSize/4 - 8 entries will be reported as corrupt. In order
  5539. ** to maintain backwards compatibility with older versions of SQLite,
  5540. ** we will continue to restrict the number of entries to usableSize/4 - 8
  5541. ** for now. At some point in the future (once everyone has upgraded
  5542. ** to 3.6.0 or later) we should consider fixing the conditional above
  5543. ** to read "usableSize/4-2" instead of "usableSize/4-8".
  5544. */
  5545. rc = sqlite3PagerWrite( pTrunk.pDbPage );
  5546. if ( rc == SQLITE_OK )
  5547. {
  5548. sqlite3Put4byte( pTrunk.aData, (u32)4, nLeaf + 1 );
  5549. sqlite3Put4byte( pTrunk.aData, (u32)8 + nLeaf * 4, iPage );
  5550. if ( pPage != null && !pBt.secureDelete )
  5551. {
  5552. sqlite3PagerDontWrite( pPage.pDbPage );
  5553. }
  5554. rc = btreeSetHasContent( pBt, iPage );
  5555. }
  5556. TRACE( "FREE-PAGE: %d leaf on trunk page %d\n", iPage, pTrunk.pgno );
  5557. goto freepage_out;
  5558. }
  5559. }
  5560. /* If control flows to this point, then it was not possible to add the
  5561. ** the page being freed as a leaf page of the first trunk in the free-list.
  5562. ** Possibly because the free-list is empty, or possibly because the
  5563. ** first trunk in the free-list is full. Either way, the page being freed
  5564. ** will become the new first trunk page in the free-list.
  5565. */
  5566. if ( pPage == null && SQLITE_OK != ( rc = btreeGetPage( pBt, iPage, ref pPage, 0 ) ) )
  5567. {
  5568. goto freepage_out;
  5569. }
  5570. rc = sqlite3PagerWrite( pPage.pDbPage );
  5571. if ( rc != SQLITE_OK )
  5572. {
  5573. goto freepage_out;
  5574. }
  5575. sqlite3Put4byte( pPage.aData, iTrunk );
  5576. sqlite3Put4byte( pPage.aData, 4, 0 );
  5577. sqlite3Put4byte( pPage1.aData, (u32)32, iPage );
  5578. TRACE( "FREE-PAGE: %d new trunk page replacing %d\n", pPage.pgno, iTrunk );
  5579. freepage_out:
  5580. if ( pPage != null )
  5581. {
  5582. pPage.isInit = 0;
  5583. }
  5584. releasePage( pPage );
  5585. releasePage( pTrunk );
  5586. return rc;
  5587. }
  5588. static void freePage( MemPage pPage, ref int pRC )
  5589. {
  5590. if ( ( pRC ) == SQLITE_OK )
  5591. {
  5592. pRC = freePage2( pPage.pBt, pPage, pPage.pgno );
  5593. }
  5594. }
  5595. /*
  5596. ** Free any overflow pages associated with the given Cell.
  5597. */
  5598. static int clearCell( MemPage pPage, int pCell )
  5599. {
  5600. BtShared pBt = pPage.pBt;
  5601. CellInfo info = new CellInfo();
  5602. Pgno ovflPgno;
  5603. int rc;
  5604. int nOvfl;
  5605. u32 ovflPageSize;
  5606. Debug.Assert( sqlite3_mutex_held( pPage.pBt.mutex ) );
  5607. btreeParseCellPtr( pPage, pCell, ref info );
  5608. if ( info.iOverflow == 0 )
  5609. {
  5610. return SQLITE_OK; /* No overflow pages. Return without doing anything */
  5611. }
  5612. ovflPgno = sqlite3Get4byte( pPage.aData, pCell, info.iOverflow );
  5613. Debug.Assert( pBt.usableSize > 4 );
  5614. ovflPageSize = (u16)( pBt.usableSize - 4 );
  5615. nOvfl = (int)( ( info.nPayload - info.nLocal + ovflPageSize - 1 ) / ovflPageSize );
  5616. Debug.Assert( ovflPgno == 0 || nOvfl > 0 );
  5617. while ( nOvfl-- != 0 )
  5618. {
  5619. Pgno iNext = 0;
  5620. MemPage pOvfl = null;
  5621. if ( ovflPgno < 2 || ovflPgno > btreePagecount( pBt ) )
  5622. {
  5623. /* 0 is not a legal page number and page 1 cannot be an
  5624. ** overflow page. Therefore if ovflPgno<2 or past the end of the
  5625. ** file the database must be corrupt. */
  5626. return SQLITE_CORRUPT_BKPT();
  5627. }
  5628. if ( nOvfl != 0 )
  5629. {
  5630. rc = getOverflowPage( pBt, ovflPgno, ref pOvfl, ref iNext );
  5631. if ( rc != 0 )
  5632. return rc;
  5633. }
  5634. if ( ( pOvfl != null || ( ( pOvfl = btreePageLookup( pBt, ovflPgno ) ) != null ) )
  5635. && sqlite3PagerPageRefcount( pOvfl.pDbPage ) != 1
  5636. )
  5637. {
  5638. /* There is no reason any cursor should have an outstanding reference
  5639. ** to an overflow page belonging to a cell that is being deleted/updated.
  5640. ** So if there exists more than one reference to this page, then it
  5641. ** must not really be an overflow page and the database must be corrupt.
  5642. ** It is helpful to detect this before calling freePage2(), as
  5643. ** freePage2() may zero the page contents if secure-delete mode is
  5644. ** enabled. If this 'overflow' page happens to be a page that the
  5645. ** caller is iterating through or using in some other way, this
  5646. ** can be problematic.
  5647. */
  5648. rc = SQLITE_CORRUPT_BKPT();
  5649. }
  5650. else
  5651. {
  5652. rc = freePage2( pBt, pOvfl, ovflPgno );
  5653. }
  5654. if ( pOvfl != null )
  5655. {
  5656. sqlite3PagerUnref( pOvfl.pDbPage );
  5657. }
  5658. if ( rc != 0 )
  5659. return rc;
  5660. ovflPgno = iNext;
  5661. }
  5662. return SQLITE_OK;
  5663. }
  5664. /*
  5665. ** Create the byte sequence used to represent a cell on page pPage
  5666. ** and write that byte sequence into pCell[]. Overflow pages are
  5667. ** allocated and filled in as necessary. The calling procedure
  5668. ** is responsible for making sure sufficient space has been allocated
  5669. ** for pCell[].
  5670. **
  5671. ** Note that pCell does not necessary need to point to the pPage.aData
  5672. ** area. pCell might point to some temporary storage. The cell will
  5673. ** be constructed in this temporary area then copied into pPage.aData
  5674. ** later.
  5675. */
  5676. static int fillInCell(
  5677. MemPage pPage, /* The page that contains the cell */
  5678. byte[] pCell, /* Complete text of the cell */
  5679. byte[] pKey, i64 nKey, /* The key */
  5680. byte[] pData, int nData, /* The data */
  5681. int nZero, /* Extra zero bytes to append to pData */
  5682. ref int pnSize /* Write cell size here */
  5683. )
  5684. {
  5685. int nPayload;
  5686. u8[] pSrc;
  5687. int pSrcIndex = 0;
  5688. int nSrc, n, rc;
  5689. int spaceLeft;
  5690. MemPage pOvfl = null;
  5691. MemPage pToRelease = null;
  5692. byte[] pPrior;
  5693. int pPriorIndex = 0;
  5694. byte[] pPayload;
  5695. int pPayloadIndex = 0;
  5696. BtShared pBt = pPage.pBt;
  5697. Pgno pgnoOvfl = 0;
  5698. int nHeader;
  5699. CellInfo info = new CellInfo();
  5700. Debug.Assert( sqlite3_mutex_held( pPage.pBt.mutex ) );
  5701. /* pPage is not necessarily writeable since pCell might be auxiliary
  5702. ** buffer space that is separate from the pPage buffer area */
  5703. // TODO -- Determine if the following Assert is needed under c#
  5704. //Debug.Assert( pCell < pPage.aData || pCell >= &pPage.aData[pBt.pageSize]
  5705. // || sqlite3PagerIswriteable(pPage.pDbPage) );
  5706. /* Fill in the header. */
  5707. nHeader = 0;
  5708. if ( 0 == pPage.leaf )
  5709. {
  5710. nHeader += 4;
  5711. }
  5712. if ( pPage.hasData != 0 )
  5713. {
  5714. nHeader += (int)putVarint( pCell, nHeader, (int)( nData + nZero ) ); //putVarint( pCell[nHeader], nData + nZero );
  5715. }
  5716. else
  5717. {
  5718. nData = nZero = 0;
  5719. }
  5720. nHeader += putVarint( pCell, nHeader, (u64)nKey ); //putVarint( pCell[nHeader], *(u64*)&nKey );
  5721. btreeParseCellPtr( pPage, pCell, ref info );
  5722. Debug.Assert( info.nHeader == nHeader );
  5723. Debug.Assert( info.nKey == nKey );
  5724. Debug.Assert( info.nData == (u32)( nData + nZero ) );
  5725. /* Fill in the payload */
  5726. nPayload = nData + nZero;
  5727. if ( pPage.intKey != 0 )
  5728. {
  5729. pSrc = pData;
  5730. nSrc = nData;
  5731. nData = 0;
  5732. }
  5733. else
  5734. {
  5735. if ( NEVER( nKey > 0x7fffffff || pKey == null ) )
  5736. {
  5737. return SQLITE_CORRUPT_BKPT();
  5738. }
  5739. nPayload += (int)nKey;
  5740. pSrc = pKey;
  5741. nSrc = (int)nKey;
  5742. }
  5743. pnSize = info.nSize;
  5744. spaceLeft = info.nLocal;
  5745. // pPayload = &pCell[nHeader];
  5746. pPayload = pCell;
  5747. pPayloadIndex = nHeader;
  5748. // pPrior = &pCell[info.iOverflow];
  5749. pPrior = pCell;
  5750. pPriorIndex = info.iOverflow;
  5751. while ( nPayload > 0 )
  5752. {
  5753. if ( spaceLeft == 0 )
  5754. {
  5755. #if !SQLITE_OMIT_AUTOVACUUM
  5756. Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
  5757. if ( pBt.autoVacuum )
  5758. {
  5759. do
  5760. {
  5761. pgnoOvfl++;
  5762. } while (
  5763. PTRMAP_ISPAGE( pBt, pgnoOvfl ) || pgnoOvfl == PENDING_BYTE_PAGE( pBt )
  5764. );
  5765. }
  5766. #endif
  5767. rc = allocateBtreePage( pBt, ref pOvfl, ref pgnoOvfl, pgnoOvfl, 0 );
  5768. #if !SQLITE_OMIT_AUTOVACUUM
  5769. /* If the database supports auto-vacuum, and the second or subsequent
  5770. ** overflow page is being allocated, add an entry to the pointer-map
  5771. ** for that page now.
  5772. **
  5773. ** If this is the first overflow page, then write a partial entry
  5774. ** to the pointer-map. If we write nothing to this pointer-map slot,
  5775. ** then the optimistic overflow chain processing in clearCell()
  5776. ** may misinterpret the uninitialised values and delete the
  5777. ** wrong pages from the database.
  5778. */
  5779. if ( pBt.autoVacuum && rc == SQLITE_OK )
  5780. {
  5781. u8 eType = (u8)( pgnoPtrmap != 0 ? PTRMAP_OVERFLOW2 : PTRMAP_OVERFLOW1 );
  5782. ptrmapPut( pBt, pgnoOvfl, eType, pgnoPtrmap, ref rc );
  5783. if ( rc != 0 )
  5784. {
  5785. releasePage( pOvfl );
  5786. }
  5787. }
  5788. #endif
  5789. if ( rc != 0 )
  5790. {
  5791. releasePage( pToRelease );
  5792. return rc;
  5793. }
  5794. /* If pToRelease is not zero than pPrior points into the data area
  5795. ** of pToRelease. Make sure pToRelease is still writeable. */
  5796. Debug.Assert( pToRelease == null || sqlite3PagerIswriteable( pToRelease.pDbPage ) );
  5797. /* If pPrior is part of the data area of pPage, then make sure pPage
  5798. ** is still writeable */
  5799. // TODO -- Determine if the following Assert is needed under c#
  5800. //Debug.Assert( pPrior < pPage.aData || pPrior >= &pPage.aData[pBt.pageSize]
  5801. // || sqlite3PagerIswriteable(pPage.pDbPage) );
  5802. sqlite3Put4byte( pPrior, pPriorIndex, pgnoOvfl );
  5803. releasePage( pToRelease );
  5804. pToRelease = pOvfl;
  5805. pPrior = pOvfl.aData;
  5806. pPriorIndex = 0;
  5807. sqlite3Put4byte( pPrior, 0 );
  5808. pPayload = pOvfl.aData;
  5809. pPayloadIndex = 4; //&pOvfl.aData[4];
  5810. spaceLeft = (int)pBt.usableSize - 4;
  5811. }
  5812. n = nPayload;
  5813. if ( n > spaceLeft )
  5814. n = spaceLeft;
  5815. /* If pToRelease is not zero than pPayload points into the data area
  5816. ** of pToRelease. Make sure pToRelease is still writeable. */
  5817. Debug.Assert( pToRelease == null || sqlite3PagerIswriteable( pToRelease.pDbPage ) );
  5818. /* If pPayload is part of the data area of pPage, then make sure pPage
  5819. ** is still writeable */
  5820. // TODO -- Determine if the following Assert is needed under c#
  5821. //Debug.Assert( pPayload < pPage.aData || pPayload >= &pPage.aData[pBt.pageSize]
  5822. // || sqlite3PagerIswriteable(pPage.pDbPage) );
  5823. if ( nSrc > 0 )
  5824. {
  5825. if ( n > nSrc )
  5826. n = nSrc;
  5827. Debug.Assert( pSrc != null );
  5828. Buffer.BlockCopy( pSrc, pSrcIndex, pPayload, pPayloadIndex, n );//memcpy(pPayload, pSrc, n);
  5829. }
  5830. else
  5831. {
  5832. byte[] pZeroBlob = sqlite3Malloc( n ); // memset(pPayload, 0, n);
  5833. Buffer.BlockCopy( pZeroBlob, 0, pPayload, pPayloadIndex, n );
  5834. }
  5835. nPayload -= n;
  5836. pPayloadIndex += n;// pPayload += n;
  5837. pSrcIndex += n;// pSrc += n;
  5838. nSrc -= n;
  5839. spaceLeft -= n;
  5840. if ( nSrc == 0 )
  5841. {
  5842. nSrc = nData;
  5843. pSrc = pData;
  5844. }
  5845. }
  5846. releasePage( pToRelease );
  5847. return SQLITE_OK;
  5848. }
  5849. /*
  5850. ** Remove the i-th cell from pPage. This routine effects pPage only.
  5851. ** The cell content is not freed or deallocated. It is assumed that
  5852. ** the cell content has been copied someplace else. This routine just
  5853. ** removes the reference to the cell from pPage.
  5854. **
  5855. ** "sz" must be the number of bytes in the cell.
  5856. */
  5857. static void dropCell( MemPage pPage, int idx, int sz, ref int pRC )
  5858. {
  5859. int i; /* Loop counter */
  5860. u32 pc; /* Offset to cell content of cell being deleted */
  5861. u8[] data; /* pPage.aData */
  5862. int ptr; /* Used to move bytes around within data[] */
  5863. int rc; /* The return code */
  5864. int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */
  5865. if ( pRC != 0 )
  5866. return;
  5867. Debug.Assert( idx >= 0 && idx < pPage.nCell );
  5868. #if SQLITE_DEBUG
  5869. Debug.Assert( sz == cellSize( pPage, idx ) );
  5870. #endif
  5871. Debug.Assert( sqlite3PagerIswriteable( pPage.pDbPage ) );
  5872. Debug.Assert( sqlite3_mutex_held( pPage.pBt.mutex ) );
  5873. data = pPage.aData;
  5874. ptr = pPage.cellOffset + 2 * idx; //ptr = &data[pPage.cellOffset + 2 * idx];
  5875. pc = (u32)get2byte( data, ptr );
  5876. hdr = pPage.hdrOffset;
  5877. testcase( pc == get2byte( data, hdr + 5 ) );
  5878. testcase( pc + sz == pPage.pBt.usableSize );
  5879. if ( pc < (u32)get2byte( data, hdr + 5 ) || pc + sz > pPage.pBt.usableSize )
  5880. {
  5881. pRC = SQLITE_CORRUPT_BKPT();
  5882. return;
  5883. }
  5884. rc = freeSpace( pPage, pc, sz );
  5885. if ( rc != 0 )
  5886. {
  5887. pRC = rc;
  5888. return;
  5889. }
  5890. //for ( i = idx + 1 ; i < pPage.nCell ; i++, ptr += 2 )
  5891. //{
  5892. // ptr[0] = ptr[2];
  5893. // ptr[1] = ptr[3];
  5894. //}
  5895. Buffer.BlockCopy( data, ptr + 2, data, ptr, ( pPage.nCell - 1 - idx ) * 2 );
  5896. pPage.nCell--;
  5897. data[pPage.hdrOffset + 3] = (byte)( pPage.nCell >> 8 );
  5898. data[pPage.hdrOffset + 4] = (byte)( pPage.nCell ); //put2byte( data, hdr + 3, pPage.nCell );
  5899. pPage.nFree += 2;
  5900. }
  5901. /*
  5902. ** Insert a new cell on pPage at cell index "i". pCell points to the
  5903. ** content of the cell.
  5904. **
  5905. ** If the cell content will fit on the page, then put it there. If it
  5906. ** will not fit, then make a copy of the cell content into pTemp if
  5907. ** pTemp is not null. Regardless of pTemp, allocate a new entry
  5908. ** in pPage.aOvfl[] and make it point to the cell content (either
  5909. ** in pTemp or the original pCell) and also record its index.
  5910. ** Allocating a new entry in pPage.aCell[] implies that
  5911. ** pPage.nOverflow is incremented.
  5912. **
  5913. ** If nSkip is non-zero, then do not copy the first nSkip bytes of the
  5914. ** cell. The caller will overwrite them after this function returns. If
  5915. ** nSkip is non-zero, then pCell may not point to an invalid memory location
  5916. ** (but pCell+nSkip is always valid).
  5917. */
  5918. static void insertCell(
  5919. MemPage pPage, /* Page into which we are copying */
  5920. int i, /* New cell becomes the i-th cell of the page */
  5921. u8[] pCell, /* Content of the new cell */
  5922. int sz, /* Bytes of content in pCell */
  5923. u8[] pTemp, /* Temp storage space for pCell, if needed */
  5924. Pgno iChild, /* If non-zero, replace first 4 bytes with this value */
  5925. ref int pRC /* Read and write return code from here */
  5926. )
  5927. {
  5928. int idx = 0; /* Where to write new cell content in data[] */
  5929. int j; /* Loop counter */
  5930. int end; /* First byte past the last cell pointer in data[] */
  5931. int ins; /* Index in data[] where new cell pointer is inserted */
  5932. int cellOffset; /* Address of first cell pointer in data[] */
  5933. u8[] data; /* The content of the whole page */
  5934. u8 ptr; /* Used for moving information around in data[] */
  5935. int nSkip = ( iChild != 0 ? 4 : 0 );
  5936. if ( pRC != 0 )
  5937. return;
  5938. Debug.Assert( i >= 0 && i <= pPage.nCell + pPage.nOverflow );
  5939. Debug.Assert( pPage.nCell <= MX_CELL( pPage.pBt ) && MX_CELL( pPage.pBt ) <= 10921 );
  5940. Debug.Assert( pPage.nOverflow <= ArraySize( pPage.aOvfl ) );
  5941. Debug.Assert( sqlite3_mutex_held( pPage.pBt.mutex ) );
  5942. /* The cell should normally be sized correctly. However, when moving a
  5943. ** malformed cell from a leaf page to an interior page, if the cell size
  5944. ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
  5945. ** might be less than 8 (leaf-size + pointer) on the interior node. Hence
  5946. ** the term after the || in the following assert(). */
  5947. Debug.Assert( sz == cellSizePtr( pPage, pCell ) || ( sz == 8 && iChild > 0 ) );
  5948. if ( pPage.nOverflow != 0 || sz + 2 > pPage.nFree )
  5949. {
  5950. if ( pTemp != null )
  5951. {
  5952. Buffer.BlockCopy( pCell, nSkip, pTemp, nSkip, sz - nSkip );//memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip);
  5953. pCell = pTemp;
  5954. }
  5955. if ( iChild != 0 )
  5956. {
  5957. sqlite3Put4byte( pCell, iChild );
  5958. }
  5959. j = pPage.nOverflow++;
  5960. Debug.Assert( j < pPage.aOvfl.Length );//(int)(sizeof(pPage.aOvfl)/sizeof(pPage.aOvfl[0])) );
  5961. pPage.aOvfl[j].pCell = pCell;
  5962. pPage.aOvfl[j].idx = (u16)i;
  5963. }
  5964. else
  5965. {
  5966. int rc = sqlite3PagerWrite( pPage.pDbPage );
  5967. if ( rc != SQLITE_OK )
  5968. {
  5969. pRC = rc;
  5970. return;
  5971. }
  5972. Debug.Assert( sqlite3PagerIswriteable( pPage.pDbPage ) );
  5973. data = pPage.aData;
  5974. cellOffset = pPage.cellOffset;
  5975. end = cellOffset + 2 * pPage.nCell;
  5976. ins = cellOffset + 2 * i;
  5977. rc = allocateSpace( pPage, sz, ref idx );
  5978. if ( rc != 0 )
  5979. {
  5980. pRC = rc;
  5981. return;
  5982. }
  5983. /* The allocateSpace() routine guarantees the following two properties
  5984. ** if it returns success */
  5985. Debug.Assert( idx >= end + 2 );
  5986. Debug.Assert( idx + sz <= pPage.pBt.usableSize );
  5987. pPage.nCell++;
  5988. pPage.nFree -= (u16)( 2 + sz );
  5989. Buffer.BlockCopy( pCell, nSkip, data, idx + nSkip, sz - nSkip ); //memcpy( data[idx + nSkip], pCell + nSkip, sz - nSkip );
  5990. if ( iChild != 0 )
  5991. {
  5992. sqlite3Put4byte( data, idx, iChild );
  5993. }
  5994. //for(j=end, ptr=&data[j]; j>ins; j-=2, ptr-=2){
  5995. // ptr[0] = ptr[-2];
  5996. // ptr[1] = ptr[-1];
  5997. //}
  5998. for ( j = end; j > ins; j -= 2 )
  5999. {
  6000. data[j + 0] = data[j - 2];
  6001. data[j + 1] = data[j - 1];
  6002. }
  6003. put2byte( data, ins, idx );
  6004. put2byte( data, pPage.hdrOffset + 3, pPage.nCell );
  6005. #if !SQLITE_OMIT_AUTOVACUUM
  6006. if ( pPage.pBt.autoVacuum )
  6007. {
  6008. /* The cell may contain a pointer to an overflow page. If so, write
  6009. ** the entry for the overflow page into the pointer map.
  6010. */
  6011. ptrmapPutOvflPtr( pPage, pCell, ref pRC );
  6012. }
  6013. #endif
  6014. }
  6015. }
  6016. /*
  6017. ** Add a list of cells to a page. The page should be initially empty.
  6018. ** The cells are guaranteed to fit on the page.
  6019. */
  6020. static void assemblePage(
  6021. MemPage pPage, /* The page to be assemblied */
  6022. int nCell, /* The number of cells to add to this page */
  6023. u8[] apCell, /* Pointer to a single the cell bodies */
  6024. int[] aSize /* Sizes of the cells bodie*/
  6025. )
  6026. {
  6027. int i; /* Loop counter */
  6028. int pCellptr; /* Address of next cell pointer */
  6029. int cellbody; /* Address of next cell body */
  6030. byte[] data = pPage.aData; /* Pointer to data for pPage */
  6031. int hdr = pPage.hdrOffset; /* Offset of header on pPage */
  6032. int nUsable = (int)pPage.pBt.usableSize; /* Usable size of page */
  6033. Debug.Assert( pPage.nOverflow == 0 );
  6034. Debug.Assert( sqlite3_mutex_held( pPage.pBt.mutex ) );
  6035. Debug.Assert( nCell >= 0 && nCell <= MX_CELL( pPage.pBt ) && MX_CELL( pPage.pBt ) <= 10921 );
  6036. Debug.Assert( sqlite3PagerIswriteable( pPage.pDbPage ) );
  6037. /* Check that the page has just been zeroed by zeroPage() */
  6038. Debug.Assert( pPage.nCell == 0 );
  6039. Debug.Assert( get2byteNotZero( data, hdr + 5 ) == nUsable );
  6040. pCellptr = pPage.cellOffset + nCell * 2; //data[pPage.cellOffset + nCell * 2];
  6041. cellbody = nUsable;
  6042. for ( i = nCell - 1; i >= 0; i-- )
  6043. {
  6044. pCellptr -= 2;
  6045. cellbody -= aSize[i];
  6046. put2byte( data, pCellptr, cellbody );
  6047. Buffer.BlockCopy( apCell, 0, data, cellbody, aSize[i] );// memcpy(data[cellbody], apCell[i], aSize[i]);
  6048. }
  6049. put2byte( data, hdr + 3, nCell );
  6050. put2byte( data, hdr + 5, cellbody );
  6051. pPage.nFree -= (u16)( nCell * 2 + nUsable - cellbody );
  6052. pPage.nCell = (u16)nCell;
  6053. }
  6054. static void assemblePage(
  6055. MemPage pPage, /* The page to be assemblied */
  6056. int nCell, /* The number of cells to add to this page */
  6057. u8[][] apCell, /* Pointers to cell bodies */
  6058. u16[] aSize, /* Sizes of the cells */
  6059. int offset /* Offset into the cell bodies, for c# */
  6060. )
  6061. {
  6062. int i; /* Loop counter */
  6063. int pCellptr; /* Address of next cell pointer */
  6064. int cellbody; /* Address of next cell body */
  6065. byte[] data = pPage.aData; /* Pointer to data for pPage */
  6066. int hdr = pPage.hdrOffset; /* Offset of header on pPage */
  6067. int nUsable = (int)pPage.pBt.usableSize; /* Usable size of page */
  6068. Debug.Assert( pPage.nOverflow == 0 );
  6069. Debug.Assert( sqlite3_mutex_held( pPage.pBt.mutex ) );
  6070. Debug.Assert( nCell >= 0 && nCell <= MX_CELL( pPage.pBt ) && MX_CELL( pPage.pBt ) <= 5460 );
  6071. Debug.Assert( sqlite3PagerIswriteable( pPage.pDbPage ) );
  6072. /* Check that the page has just been zeroed by zeroPage() */
  6073. Debug.Assert( pPage.nCell == 0 );
  6074. Debug.Assert( get2byte( data, hdr + 5 ) == nUsable );
  6075. pCellptr = pPage.cellOffset + nCell * 2; //data[pPage.cellOffset + nCell * 2];
  6076. cellbody = nUsable;
  6077. for ( i = nCell - 1; i >= 0; i-- )
  6078. {
  6079. pCellptr -= 2;
  6080. cellbody -= aSize[i + offset];
  6081. put2byte( data, pCellptr, cellbody );
  6082. Buffer.BlockCopy( apCell[offset + i], 0, data, cellbody, aSize[i + offset] );// memcpy(&data[cellbody], apCell[i], aSize[i]);
  6083. }
  6084. put2byte( data, hdr + 3, nCell );
  6085. put2byte( data, hdr + 5, cellbody );
  6086. pPage.nFree -= (u16)( nCell * 2 + nUsable - cellbody );
  6087. pPage.nCell = (u16)nCell;
  6088. }
  6089. static void assemblePage(
  6090. MemPage pPage, /* The page to be assemblied */
  6091. int nCell, /* The number of cells to add to this page */
  6092. u8[] apCell, /* Pointers to cell bodies */
  6093. u16[] aSize /* Sizes of the cells */
  6094. )
  6095. {
  6096. int i; /* Loop counter */
  6097. int pCellptr; /* Address of next cell pointer */
  6098. int cellbody; /* Address of next cell body */
  6099. u8[] data = pPage.aData; /* Pointer to data for pPage */
  6100. int hdr = pPage.hdrOffset; /* Offset of header on pPage */
  6101. int nUsable = (int)pPage.pBt.usableSize; /* Usable size of page */
  6102. Debug.Assert( pPage.nOverflow == 0 );
  6103. Debug.Assert( sqlite3_mutex_held( pPage.pBt.mutex ) );
  6104. Debug.Assert( nCell >= 0 && nCell <= MX_CELL( pPage.pBt ) && MX_CELL( pPage.pBt ) <= 5460 );
  6105. Debug.Assert( sqlite3PagerIswriteable( pPage.pDbPage ) );
  6106. /* Check that the page has just been zeroed by zeroPage() */
  6107. Debug.Assert( pPage.nCell == 0 );
  6108. Debug.Assert( get2byte( data, hdr + 5 ) == nUsable );
  6109. pCellptr = pPage.cellOffset + nCell * 2; //&data[pPage.cellOffset + nCell * 2];
  6110. cellbody = nUsable;
  6111. for ( i = nCell - 1; i >= 0; i-- )
  6112. {
  6113. pCellptr -= 2;
  6114. cellbody -= aSize[i];
  6115. put2byte( data, pCellptr, cellbody );
  6116. Buffer.BlockCopy( apCell, 0, data, cellbody, aSize[i] );//memcpy( data[cellbody], apCell[i], aSize[i] );
  6117. }
  6118. put2byte( data, hdr + 3, nCell );
  6119. put2byte( data, hdr + 5, cellbody );
  6120. pPage.nFree -= (u16)( nCell * 2 + nUsable - cellbody );
  6121. pPage.nCell = (u16)nCell;
  6122. }
  6123. /*
  6124. ** The following parameters determine how many adjacent pages get involved
  6125. ** in a balancing operation. NN is the number of neighbors on either side
  6126. ** of the page that participate in the balancing operation. NB is the
  6127. ** total number of pages that participate, including the target page and
  6128. ** NN neighbors on either side.
  6129. **
  6130. ** The minimum value of NN is 1 (of course). Increasing NN above 1
  6131. ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
  6132. ** in exchange for a larger degradation in INSERT and UPDATE performance.
  6133. ** The value of NN appears to give the best results overall.
  6134. */
  6135. static int NN = 1; /* Number of neighbors on either side of pPage */
  6136. static int NB = ( NN * 2 + 1 ); /* Total pages involved in the balance */
  6137. #if !SQLITE_OMIT_QUICKBALANCE
  6138. /*
  6139. ** This version of balance() handles the common special case where
  6140. ** a new entry is being inserted on the extreme right-end of the
  6141. ** tree, in other words, when the new entry will become the largest
  6142. ** entry in the tree.
  6143. **
  6144. ** Instead of trying to balance the 3 right-most leaf pages, just add
  6145. ** a new page to the right-hand side and put the one new entry in
  6146. ** that page. This leaves the right side of the tree somewhat
  6147. ** unbalanced. But odds are that we will be inserting new entries
  6148. ** at the end soon afterwards so the nearly empty page will quickly
  6149. ** fill up. On average.
  6150. **
  6151. ** pPage is the leaf page which is the right-most page in the tree.
  6152. ** pParent is its parent. pPage must have a single overflow entry
  6153. ** which is also the right-most entry on the page.
  6154. **
  6155. ** The pSpace buffer is used to store a temporary copy of the divider
  6156. ** cell that will be inserted into pParent. Such a cell consists of a 4
  6157. ** byte page number followed by a variable length integer. In other
  6158. ** words, at most 13 bytes. Hence the pSpace buffer must be at
  6159. ** least 13 bytes in size.
  6160. */
  6161. static int balance_quick( MemPage pParent, MemPage pPage, u8[] pSpace )
  6162. {
  6163. BtShared pBt = pPage.pBt; /* B-Tree Database */
  6164. MemPage pNew = new MemPage();/* Newly allocated page */
  6165. int rc; /* Return Code */
  6166. Pgno pgnoNew = 0; /* Page number of pNew */
  6167. Debug.Assert( sqlite3_mutex_held( pPage.pBt.mutex ) );
  6168. Debug.Assert( sqlite3PagerIswriteable( pParent.pDbPage ) );
  6169. Debug.Assert( pPage.nOverflow == 1 );
  6170. /* This error condition is now caught prior to reaching this function */
  6171. if ( pPage.nCell <= 0 )
  6172. return SQLITE_CORRUPT_BKPT();
  6173. /* Allocate a new page. This page will become the right-sibling of
  6174. ** pPage. Make the parent page writable, so that the new divider cell
  6175. ** may be inserted. If both these operations are successful, proceed.
  6176. */
  6177. rc = allocateBtreePage( pBt, ref pNew, ref pgnoNew, 0, 0 );
  6178. if ( rc == SQLITE_OK )
  6179. {
  6180. int pOut = 4;//u8 pOut = &pSpace[4];
  6181. u8[] pCell = pPage.aOvfl[0].pCell;
  6182. int[] szCell = new int[1];
  6183. szCell[0] = cellSizePtr( pPage, pCell );
  6184. int pStop;
  6185. Debug.Assert( sqlite3PagerIswriteable( pNew.pDbPage ) );
  6186. Debug.Assert( pPage.aData[0] == ( PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF ) );
  6187. zeroPage( pNew, PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF );
  6188. assemblePage( pNew, 1, pCell, szCell );
  6189. /* If this is an auto-vacuum database, update the pointer map
  6190. ** with entries for the new page, and any pointer from the
  6191. ** cell on the page to an overflow page. If either of these
  6192. ** operations fails, the return code is set, but the contents
  6193. ** of the parent page are still manipulated by thh code below.
  6194. ** That is Ok, at this point the parent page is guaranteed to
  6195. ** be marked as dirty. Returning an error code will cause a
  6196. ** rollback, undoing any changes made to the parent page.
  6197. */
  6198. #if !SQLITE_OMIT_AUTOVACUUM // if ( ISAUTOVACUUM )
  6199. if ( pBt.autoVacuum )
  6200. #else
  6201. if (false)
  6202. #endif
  6203. {
  6204. ptrmapPut( pBt, pgnoNew, PTRMAP_BTREE, pParent.pgno, ref rc );
  6205. if ( szCell[0] > pNew.minLocal )
  6206. {
  6207. ptrmapPutOvflPtr( pNew, pCell, ref rc );
  6208. }
  6209. }
  6210. /* Create a divider cell to insert into pParent. The divider cell
  6211. ** consists of a 4-byte page number (the page number of pPage) and
  6212. ** a variable length key value (which must be the same value as the
  6213. ** largest key on pPage).
  6214. **
  6215. ** To find the largest key value on pPage, first find the right-most
  6216. ** cell on pPage. The first two fields of this cell are the
  6217. ** record-length (a variable length integer at most 32-bits in size)
  6218. ** and the key value (a variable length integer, may have any value).
  6219. ** The first of the while(...) loops below skips over the record-length
  6220. ** field. The second while(...) loop copies the key value from the
  6221. ** cell on pPage into the pSpace buffer.
  6222. */
  6223. int iCell = findCell( pPage, pPage.nCell - 1 ); //pCell = findCell( pPage, pPage.nCell - 1 );
  6224. pCell = pPage.aData;
  6225. int _pCell = iCell;
  6226. pStop = _pCell + 9; //pStop = &pCell[9];
  6227. while ( ( ( pCell[_pCell++] ) & 0x80 ) != 0 && _pCell < pStop )
  6228. ; //while ( ( *( pCell++ ) & 0x80 ) && pCell < pStop ) ;
  6229. pStop = _pCell + 9;//pStop = &pCell[9];
  6230. while ( ( ( pSpace[pOut++] = pCell[_pCell++] ) & 0x80 ) != 0 && _pCell < pStop )
  6231. ; //while ( ( ( *( pOut++ ) = *( pCell++ ) ) & 0x80 ) && pCell < pStop ) ;
  6232. /* Insert the new divider cell into pParent. */
  6233. insertCell( pParent, pParent.nCell, pSpace, pOut, //(int)(pOut-pSpace),
  6234. null, pPage.pgno, ref rc );
  6235. /* Set the right-child pointer of pParent to point to the new page. */
  6236. sqlite3Put4byte( pParent.aData, pParent.hdrOffset + 8, pgnoNew );
  6237. /* Release the reference to the new page. */
  6238. releasePage( pNew );
  6239. }
  6240. return rc;
  6241. }
  6242. #endif //* SQLITE_OMIT_QUICKBALANCE */
  6243. #if FALSE
  6244. /*
  6245. ** This function does not contribute anything to the operation of SQLite.
  6246. ** it is sometimes activated temporarily while debugging code responsible
  6247. ** for setting pointer-map entries.
  6248. */
  6249. static int ptrmapCheckPages(MemPage **apPage, int nPage){
  6250. int i, j;
  6251. for(i=0; i<nPage; i++){
  6252. Pgno n;
  6253. u8 e;
  6254. MemPage pPage = apPage[i];
  6255. BtShared pBt = pPage.pBt;
  6256. Debug.Assert( pPage.isInit!=0 );
  6257. for(j=0; j<pPage.nCell; j++){
  6258. CellInfo info;
  6259. u8 *z;
  6260. z = findCell(pPage, j);
  6261. btreeParseCellPtr(pPage, z, info);
  6262. if( info.iOverflow ){
  6263. Pgno ovfl = sqlite3Get4byte(z[info.iOverflow]);
  6264. ptrmapGet(pBt, ovfl, ref e, ref n);
  6265. Debug.Assert( n==pPage.pgno && e==PTRMAP_OVERFLOW1 );
  6266. }
  6267. if( 0==pPage.leaf ){
  6268. Pgno child = sqlite3Get4byte(z);
  6269. ptrmapGet(pBt, child, ref e, ref n);
  6270. Debug.Assert( n==pPage.pgno && e==PTRMAP_BTREE );
  6271. }
  6272. }
  6273. if( 0==pPage.leaf ){
  6274. Pgno child = sqlite3Get4byte(pPage.aData,pPage.hdrOffset+8]);
  6275. ptrmapGet(pBt, child, ref e, ref n);
  6276. Debug.Assert( n==pPage.pgno && e==PTRMAP_BTREE );
  6277. }
  6278. }
  6279. return 1;
  6280. }
  6281. #endif
  6282. /*
  6283. ** This function is used to copy the contents of the b-tree node stored
  6284. ** on page pFrom to page pTo. If page pFrom was not a leaf page, then
  6285. ** the pointer-map entries for each child page are updated so that the
  6286. ** parent page stored in the pointer map is page pTo. If pFrom contained
  6287. ** any cells with overflow page pointers, then the corresponding pointer
  6288. ** map entries are also updated so that the parent page is page pTo.
  6289. **
  6290. ** If pFrom is currently carrying any overflow cells (entries in the
  6291. ** MemPage.aOvfl[] array), they are not copied to pTo.
  6292. **
  6293. ** Before returning, page pTo is reinitialized using btreeInitPage().
  6294. **
  6295. ** The performance of this function is not critical. It is only used by
  6296. ** the balance_shallower() and balance_deeper() procedures, neither of
  6297. ** which are called often under normal circumstances.
  6298. */
  6299. static void copyNodeContent( MemPage pFrom, MemPage pTo, ref int pRC )
  6300. {
  6301. if ( ( pRC ) == SQLITE_OK )
  6302. {
  6303. BtShared pBt = pFrom.pBt;
  6304. u8[] aFrom = pFrom.aData;
  6305. u8[] aTo = pTo.aData;
  6306. int iFromHdr = pFrom.hdrOffset;
  6307. int iToHdr = ( ( pTo.pgno == 1 ) ? 100 : 0 );
  6308. int rc;
  6309. int iData;
  6310. Debug.Assert( pFrom.isInit != 0 );
  6311. Debug.Assert( pFrom.nFree >= iToHdr );
  6312. Debug.Assert( get2byte( aFrom, iFromHdr + 5 ) <= pBt.usableSize );
  6313. /* Copy the b-tree node content from page pFrom to page pTo. */
  6314. iData = get2byte( aFrom, iFromHdr + 5 );
  6315. Buffer.BlockCopy( aFrom, iData, aTo, iData, (int)pBt.usableSize - iData );//memcpy(aTo[iData], ref aFrom[iData], pBt.usableSize-iData);
  6316. Buffer.BlockCopy( aFrom, iFromHdr, aTo, iToHdr, pFrom.cellOffset + 2 * pFrom.nCell );//memcpy(aTo[iToHdr], ref aFrom[iFromHdr], pFrom.cellOffset + 2*pFrom.nCell);
  6317. /* Reinitialize page pTo so that the contents of the MemPage structure
  6318. ** match the new data. The initialization of pTo can actually fail under
  6319. ** fairly obscure circumstances, even though it is a copy of initialized
  6320. ** page pFrom.
  6321. */
  6322. pTo.isInit = 0;
  6323. rc = btreeInitPage( pTo );
  6324. if ( rc != SQLITE_OK )
  6325. {
  6326. pRC = rc;
  6327. return;
  6328. }
  6329. /* If this is an auto-vacuum database, update the pointer-map entries
  6330. ** for any b-tree or overflow pages that pTo now contains the pointers to.
  6331. */
  6332. #if !SQLITE_OMIT_AUTOVACUUM // if ( ISAUTOVACUUM )
  6333. if ( pBt.autoVacuum )
  6334. #else
  6335. if (false)
  6336. #endif
  6337. {
  6338. pRC = setChildPtrmaps( pTo );
  6339. }
  6340. }
  6341. }
  6342. /*
  6343. ** This routine redistributes cells on the iParentIdx'th child of pParent
  6344. ** (hereafter "the page") and up to 2 siblings so that all pages have about the
  6345. ** same amount of free space. Usually a single sibling on either side of the
  6346. ** page are used in the balancing, though both siblings might come from one
  6347. ** side if the page is the first or last child of its parent. If the page
  6348. ** has fewer than 2 siblings (something which can only happen if the page
  6349. ** is a root page or a child of a root page) then all available siblings
  6350. ** participate in the balancing.
  6351. **
  6352. ** The number of siblings of the page might be increased or decreased by
  6353. ** one or two in an effort to keep pages nearly full but not over full.
  6354. **
  6355. ** Note that when this routine is called, some of the cells on the page
  6356. ** might not actually be stored in MemPage.aData[]. This can happen
  6357. ** if the page is overfull. This routine ensures that all cells allocated
  6358. ** to the page and its siblings fit into MemPage.aData[] before returning.
  6359. **
  6360. ** In the course of balancing the page and its siblings, cells may be
  6361. ** inserted into or removed from the parent page (pParent). Doing so
  6362. ** may cause the parent page to become overfull or underfull. If this
  6363. ** happens, it is the responsibility of the caller to invoke the correct
  6364. ** balancing routine to fix this problem (see the balance() routine).
  6365. **
  6366. ** If this routine fails for any reason, it might leave the database
  6367. ** in a corrupted state. So if this routine fails, the database should
  6368. ** be rolled back.
  6369. **
  6370. ** The third argument to this function, aOvflSpace, is a pointer to a
  6371. ** buffer big enough to hold one page. If while inserting cells into the parent
  6372. ** page (pParent) the parent page becomes overfull, this buffer is
  6373. ** used to store the parent's overflow cells. Because this function inserts
  6374. ** a maximum of four divider cells into the parent page, and the maximum
  6375. ** size of a cell stored within an internal node is always less than 1/4
  6376. ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
  6377. ** enough for all overflow cells.
  6378. **
  6379. ** If aOvflSpace is set to a null pointer, this function returns
  6380. ** SQLITE_NOMEM.
  6381. */
  6382. // under C#; Try to reuse Memory
  6383. static int balance_nonroot(
  6384. MemPage pParent, /* Parent page of siblings being balanced */
  6385. int iParentIdx, /* Index of "the page" in pParent */
  6386. u8[] aOvflSpace, /* page-size bytes of space for parent ovfl */
  6387. int isRoot /* True if pParent is a root-page */
  6388. )
  6389. {
  6390. MemPage[] apOld = new MemPage[NB]; /* pPage and up to two siblings */
  6391. MemPage[] apCopy = new MemPage[NB]; /* Private copies of apOld[] pages */
  6392. MemPage[] apNew = new MemPage[NB + 2];/* pPage and up to NB siblings after balancing */
  6393. int[] apDiv = new int[NB - 1]; /* Divider cells in pParent */
  6394. int[] cntNew = new int[NB + 2]; /* Index in aCell[] of cell after i-th page */
  6395. int[] szNew = new int[NB + 2]; /* Combined size of cells place on i-th page */
  6396. u16[] szCell = new u16[1]; /* Local size of all cells in apCell[] */
  6397. BtShared pBt; /* The whole database */
  6398. int nCell = 0; /* Number of cells in apCell[] */
  6399. int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
  6400. int nNew = 0; /* Number of pages in apNew[] */
  6401. int nOld; /* Number of pages in apOld[] */
  6402. int i, j, k; /* Loop counters */
  6403. int nxDiv; /* Next divider slot in pParent.aCell[] */
  6404. int rc = SQLITE_OK; /* The return code */
  6405. u16 leafCorrection; /* 4 if pPage is a leaf. 0 if not */
  6406. int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
  6407. int usableSpace; /* Bytes in pPage beyond the header */
  6408. int pageFlags; /* Value of pPage.aData[0] */
  6409. int subtotal; /* Subtotal of bytes in cells on one page */
  6410. //int iSpace1 = 0; /* First unused byte of aSpace1[] */
  6411. int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */
  6412. int szScratch; /* Size of scratch memory requested */
  6413. int pRight; /* Location in parent of right-sibling pointer */
  6414. u8[][] apCell = null; /* All cells begin balanced */
  6415. //u16[] szCell; /* Local size of all cells in apCell[] */
  6416. //u8[] aSpace1; /* Space for copies of dividers cells */
  6417. Pgno pgno; /* Temp var to store a page number in */
  6418. pBt = pParent.pBt;
  6419. Debug.Assert( sqlite3_mutex_held( pBt.mutex ) );
  6420. Debug.Assert( sqlite3PagerIswriteable( pParent.pDbPage ) );
  6421. #if FALSE
  6422. TRACE("BALANCE: begin page %d child of %d\n", pPage.pgno, pParent.pgno);
  6423. #endif
  6424. /* At this point pParent may have at most one overflow cell. And if
  6425. ** this overflow cell is present, it must be the cell with
  6426. ** index iParentIdx. This scenario comes about when this function
  6427. ** is called (indirectly) from sqlite3BtreeDelete().
  6428. */
  6429. Debug.Assert( pParent.nOverflow == 0 || pParent.nOverflow == 1 );
  6430. Debug.Assert( pParent.nOverflow == 0 || pParent.aOvfl[0].idx == iParentIdx );
  6431. //if( !aOvflSpace ){
  6432. // return SQLITE_NOMEM;
  6433. //}
  6434. /* Find the sibling pages to balance. Also locate the cells in pParent
  6435. ** that divide the siblings. An attempt is made to find NN siblings on
  6436. ** either side of pPage. More siblings are taken from one side, however,
  6437. ** if there are fewer than NN siblings on the other side. If pParent
  6438. ** has NB or fewer children then all children of pParent are taken.
  6439. **
  6440. ** This loop also drops the divider cells from the parent page. This
  6441. ** way, the remainder of the function does not have to deal with any
  6442. ** overflow cells in the parent page, since if any existed they will
  6443. ** have already been removed.
  6444. */
  6445. i = pParent.nOverflow + pParent.nCell;
  6446. if ( i < 2 )
  6447. {
  6448. nxDiv = 0;
  6449. nOld = i + 1;
  6450. }
  6451. else
  6452. {
  6453. nOld = 3;
  6454. if ( iParentIdx == 0 )
  6455. {
  6456. nxDiv = 0;
  6457. }
  6458. else if ( iParentIdx == i )
  6459. {
  6460. nxDiv = i - 2;
  6461. }
  6462. else
  6463. {
  6464. nxDiv = iParentIdx - 1;
  6465. }
  6466. i = 2;
  6467. }
  6468. if ( ( i + nxDiv - pParent.nOverflow ) == pParent.nCell )
  6469. {
  6470. pRight = pParent.hdrOffset + 8; //&pParent.aData[pParent.hdrOffset + 8];
  6471. }
  6472. else
  6473. {
  6474. pRight = findCell( pParent, i + nxDiv - pParent.nOverflow );
  6475. }
  6476. pgno = sqlite3Get4byte( pParent.aData, pRight );
  6477. while ( true )
  6478. {
  6479. rc = getAndInitPage( pBt, pgno, ref apOld[i] );
  6480. if ( rc != 0 )
  6481. {
  6482. apOld = new MemPage[i + 1];//memset(apOld, 0, (i+1)*sizeof(MemPage*));
  6483. goto balance_cleanup;
  6484. }
  6485. nMaxCells += 1 + apOld[i].nCell + apOld[i].nOverflow;
  6486. if ( ( i-- ) == 0 )
  6487. break;
  6488. if ( i + nxDiv == pParent.aOvfl[0].idx && pParent.nOverflow != 0 )
  6489. {
  6490. apDiv[i] = 0;// = pParent.aOvfl[0].pCell;
  6491. pgno = sqlite3Get4byte( pParent.aOvfl[0].pCell, apDiv[i] );
  6492. szNew[i] = cellSizePtr( pParent, apDiv[i] );
  6493. pParent.nOverflow = 0;
  6494. }
  6495. else
  6496. {
  6497. apDiv[i] = findCell( pParent, i + nxDiv - pParent.nOverflow );
  6498. pgno = sqlite3Get4byte( pParent.aData, apDiv[i] );
  6499. szNew[i] = cellSizePtr( pParent, apDiv[i] );
  6500. /* Drop the cell from the parent page. apDiv[i] still points to
  6501. ** the cell within the parent, even though it has been dropped.
  6502. ** This is safe because dropping a cell only overwrites the first
  6503. ** four bytes of it, and this function does not need the first
  6504. ** four bytes of the divider cell. So the pointer is safe to use
  6505. ** later on.
  6506. **
  6507. ** Unless SQLite is compiled in secure-delete mode. In this case,
  6508. ** the dropCell() routine will overwrite the entire cell with zeroes.
  6509. ** In this case, temporarily copy the cell into the aOvflSpace[]
  6510. ** buffer. It will be copied out again as soon as the aSpace[] buffer
  6511. ** is allocated. */
  6512. //if (pBt.secureDelete)
  6513. //{
  6514. // int iOff = (int)(apDiv[i]) - (int)(pParent.aData); //SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent.aData);
  6515. // if( (iOff+szNew[i])>(int)pBt->usableSize )
  6516. // {
  6517. // rc = SQLITE_CORRUPT_BKPT();
  6518. // Array.Clear(apOld[0].aData,0,apOld[0].aData.Length); //memset(apOld, 0, (i + 1) * sizeof(MemPage*));
  6519. // goto balance_cleanup;
  6520. // }
  6521. // else
  6522. // {
  6523. // memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
  6524. // apDiv[i] = &aOvflSpace[apDiv[i] - pParent.aData];
  6525. // }
  6526. //}
  6527. dropCell( pParent, i + nxDiv - pParent.nOverflow, szNew[i], ref rc );
  6528. }
  6529. }
  6530. /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
  6531. ** alignment */
  6532. nMaxCells = ( nMaxCells + 3 ) & ~3;
  6533. /*
  6534. ** Allocate space for memory structures
  6535. */
  6536. //k = pBt.pageSize + ROUND8(sizeof(MemPage));
  6537. //szScratch =
  6538. // nMaxCells*sizeof(u8*) /* apCell */
  6539. // + nMaxCells*sizeof(u16) /* szCell */
  6540. // + pBt.pageSize /* aSpace1 */
  6541. // + k*nOld; /* Page copies (apCopy) */
  6542. apCell = sqlite3ScratchMalloc( apCell, nMaxCells );
  6543. //if( apCell==null ){
  6544. // rc = SQLITE_NOMEM;
  6545. // goto balance_cleanup;
  6546. //}
  6547. if ( szCell.Length < nMaxCells )
  6548. Array.Resize( ref szCell, nMaxCells ); //(u16*)&apCell[nMaxCells];
  6549. //aSpace1 = new byte[pBt.pageSize * (nMaxCells)];// aSpace1 = (u8*)&szCell[nMaxCells];
  6550. //Debug.Assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
  6551. /*
  6552. ** Load pointers to all cells on sibling pages and the divider cells
  6553. ** into the local apCell[] array. Make copies of the divider cells
  6554. ** into space obtained from aSpace1[] and remove the the divider Cells
  6555. ** from pParent.
  6556. **
  6557. ** If the siblings are on leaf pages, then the child pointers of the
  6558. ** divider cells are stripped from the cells before they are copied
  6559. ** into aSpace1[]. In this way, all cells in apCell[] are without
  6560. ** child pointers. If siblings are not leaves, then all cell in
  6561. ** apCell[] include child pointers. Either way, all cells in apCell[]
  6562. ** are alike.
  6563. **
  6564. ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
  6565. ** leafData: 1 if pPage holds key+data and pParent holds only keys.
  6566. */
  6567. leafCorrection = (u16)( apOld[0].leaf * 4 );
  6568. leafData = apOld[0].hasData;
  6569. for ( i = 0; i < nOld; i++ )
  6570. {
  6571. int limit;
  6572. /* Before doing anything else, take a copy of the i'th original sibling
  6573. ** The rest of this function will use data from the copies rather
  6574. ** that the original pages since the original pages will be in the
  6575. ** process of being overwritten. */
  6576. //MemPage pOld = apCopy[i] = (MemPage*)&aSpace1[pBt.pageSize + k*i];
  6577. //memcpy(pOld, apOld[i], sizeof(MemPage));
  6578. //pOld.aData = (void*)&pOld[1];
  6579. //memcpy(pOld.aData, apOld[i].aData, pBt.pageSize);
  6580. MemPage pOld = apCopy[i] = apOld[i].Copy();
  6581. limit = pOld.nCell + pOld.nOverflow;
  6582. for ( j = 0; j < limit; j++ )
  6583. {
  6584. Debug.Assert( nCell < nMaxCells );
  6585. //apCell[nCell] = findOverflowCell( pOld, j );
  6586. //szCell[nCell] = cellSizePtr( pOld, apCell, nCell );
  6587. int iFOFC = findOverflowCell( pOld, j );
  6588. szCell[nCell] = cellSizePtr( pOld, iFOFC );
  6589. // Copy the Data Locally
  6590. if ( apCell[nCell] == null )
  6591. apCell[nCell] = new u8[szCell[nCell]];
  6592. else if ( apCell[nCell].Length < szCell[nCell] )
  6593. Array.Resize( ref apCell[nCell], szCell[nCell] );
  6594. if ( iFOFC < 0 ) // Overflow Cell
  6595. Buffer.BlockCopy( pOld.aOvfl[-( iFOFC + 1 )].pCell, 0, apCell[nCell], 0, szCell[nCell] );
  6596. else
  6597. Buffer.BlockCopy( pOld.aData, iFOFC, apCell[nCell], 0, szCell[nCell] );
  6598. nCell++;
  6599. }
  6600. if ( i < nOld - 1 && 0 == leafData )
  6601. {
  6602. u16 sz = (u16)szNew[i];
  6603. byte[] pTemp = sqlite3Malloc( sz + leafCorrection );
  6604. Debug.Assert( nCell < nMaxCells );
  6605. szCell[nCell] = sz;
  6606. //pTemp = &aSpace1[iSpace1];
  6607. //iSpace1 += sz;
  6608. Debug.Assert( sz <= pBt.maxLocal + 23 );
  6609. //Debug.Assert(iSpace1 <= pBt.pageSize);
  6610. Buffer.BlockCopy( pParent.aData, apDiv[i], pTemp, 0, sz );//memcpy( pTemp, apDiv[i], sz );
  6611. if ( apCell[nCell] == null || apCell[nCell].Length < sz )
  6612. Array.Resize( ref apCell[nCell], sz );
  6613. Buffer.BlockCopy( pTemp, leafCorrection, apCell[nCell], 0, sz );//apCell[nCell] = pTemp + leafCorrection;
  6614. Debug.Assert( leafCorrection == 0 || leafCorrection == 4 );
  6615. szCell[nCell] = (u16)( szCell[nCell] - leafCorrection );
  6616. if ( 0 == pOld.leaf )
  6617. {
  6618. Debug.Assert( leafCorrection == 0 );
  6619. Debug.Assert( pOld.hdrOffset == 0 );
  6620. /* The right pointer of the child page pOld becomes the left
  6621. ** pointer of the divider cell */
  6622. Buffer.BlockCopy( pOld.aData, 8, apCell[nCell], 0, 4 );//memcpy( apCell[nCell], ref pOld.aData[8], 4 );
  6623. }
  6624. else
  6625. {
  6626. Debug.Assert( leafCorrection == 4 );
  6627. if ( szCell[nCell] < 4 )
  6628. {
  6629. /* Do not allow any cells smaller than 4 bytes. */
  6630. szCell[nCell] = 4;
  6631. }
  6632. }
  6633. nCell++;
  6634. }
  6635. }
  6636. /*
  6637. ** Figure out the number of pages needed to hold all nCell cells.
  6638. ** Store this number in "k". Also compute szNew[] which is the total
  6639. ** size of all cells on the i-th page and cntNew[] which is the index
  6640. ** in apCell[] of the cell that divides page i from page i+1.
  6641. ** cntNew[k] should equal nCell.
  6642. **
  6643. ** Values computed by this block:
  6644. **
  6645. ** k: The total number of sibling pages
  6646. ** szNew[i]: Spaced used on the i-th sibling page.
  6647. ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to
  6648. ** the right of the i-th sibling page.
  6649. ** usableSpace: Number of bytes of space available on each sibling.
  6650. **
  6651. */
  6652. usableSpace = (int)pBt.usableSize - 12 + leafCorrection;
  6653. for ( subtotal = k = i = 0; i < nCell; i++ )
  6654. {
  6655. Debug.Assert( i < nMaxCells );
  6656. subtotal += szCell[i] + 2;
  6657. if ( subtotal > usableSpace )
  6658. {
  6659. szNew[k] = subtotal - szCell[i];
  6660. cntNew[k] = i;
  6661. if ( leafData != 0 )
  6662. {
  6663. i--;
  6664. }
  6665. subtotal = 0;
  6666. k++;
  6667. if ( k > NB + 1 )
  6668. {
  6669. rc = SQLITE_CORRUPT_BKPT();
  6670. goto balance_cleanup;
  6671. }
  6672. }
  6673. }
  6674. szNew[k] = subtotal;
  6675. cntNew[k] = nCell;
  6676. k++;
  6677. /*
  6678. ** The packing computed by the previous block is biased toward the siblings
  6679. ** on the left side. The left siblings are always nearly full, while the
  6680. ** right-most sibling might be nearly empty. This block of code attempts
  6681. ** to adjust the packing of siblings to get a better balance.
  6682. **
  6683. ** This adjustment is more than an optimization. The packing above might
  6684. ** be so out of balance as to be illegal. For example, the right-most
  6685. ** sibling might be completely empty. This adjustment is not optional.
  6686. */
  6687. for ( i = k - 1; i > 0; i-- )
  6688. {
  6689. int szRight = szNew[i]; /* Size of sibling on the right */
  6690. int szLeft = szNew[i - 1]; /* Size of sibling on the left */
  6691. int r; /* Index of right-most cell in left sibling */
  6692. int d; /* Index of first cell to the left of right sibling */
  6693. r = cntNew[i - 1] - 1;
  6694. d = r + 1 - leafData;
  6695. Debug.Assert( d < nMaxCells );
  6696. Debug.Assert( r < nMaxCells );
  6697. while ( szRight == 0 || szRight + szCell[d] + 2 <= szLeft - ( szCell[r] + 2 ) )
  6698. {
  6699. szRight += szCell[d] + 2;
  6700. szLeft -= szCell[r] + 2;
  6701. cntNew[i - 1]--;
  6702. r = cntNew[i - 1] - 1;
  6703. d = r + 1 - leafData;
  6704. }
  6705. szNew[i] = szRight;
  6706. szNew[i - 1] = szLeft;
  6707. }
  6708. /* Either we found one or more cells (cntnew[0])>0) or pPage is
  6709. ** a virtual root page. A virtual root page is when the real root
  6710. ** page is page 1 and we are the only child of that page.
  6711. */
  6712. Debug.Assert( cntNew[0] > 0 || ( pParent.pgno == 1 && pParent.nCell == 0 ) );
  6713. TRACE( "BALANCE: old: %d %d %d ",
  6714. apOld[0].pgno,
  6715. nOld >= 2 ? apOld[1].pgno : 0,
  6716. nOld >= 3 ? apOld[2].pgno : 0
  6717. );
  6718. /*
  6719. ** Allocate k new pages. Reuse old pages where possible.
  6720. */
  6721. if ( apOld[0].pgno <= 1 )
  6722. {
  6723. rc = SQLITE_CORRUPT_BKPT();
  6724. goto balance_cleanup;
  6725. }
  6726. pageFlags = apOld[0].aData[0];
  6727. for ( i = 0; i < k; i++ )
  6728. {
  6729. MemPage pNew = new MemPage();
  6730. if ( i < nOld )
  6731. {
  6732. pNew = apNew[i] = apOld[i];
  6733. apOld[i] = null;
  6734. rc = sqlite3PagerWrite( pNew.pDbPage );
  6735. nNew++;
  6736. if ( rc != 0 )
  6737. goto balance_cleanup;
  6738. }
  6739. else
  6740. {
  6741. Debug.Assert( i > 0 );
  6742. rc = allocateBtreePage( pBt, ref pNew, ref pgno, pgno, 0 );
  6743. if ( rc != 0 )
  6744. goto balance_cleanup;
  6745. apNew[i] = pNew;
  6746. nNew++;
  6747. /* Set the pointer-map entry for the new sibling page. */
  6748. #if !SQLITE_OMIT_AUTOVACUUM // if ( ISAUTOVACUUM )
  6749. if ( pBt.autoVacuum )
  6750. #else
  6751. if (false)
  6752. #endif
  6753. {
  6754. ptrmapPut( pBt, pNew.pgno, PTRMAP_BTREE, pParent.pgno, ref rc );
  6755. if ( rc != SQLITE_OK )
  6756. {
  6757. goto balance_cleanup;
  6758. }
  6759. }
  6760. }
  6761. }
  6762. /* Free any old pages that were not reused as new pages.
  6763. */
  6764. while ( i < nOld )
  6765. {
  6766. freePage( apOld[i], ref rc );
  6767. if ( rc != 0 )
  6768. goto balance_cleanup;
  6769. releasePage( apOld[i] );
  6770. apOld[i] = null;
  6771. i++;
  6772. }
  6773. /*
  6774. ** Put the new pages in accending order. This helps to
  6775. ** keep entries in the disk file in order so that a scan
  6776. ** of the table is a linear scan through the file. That
  6777. ** in turn helps the operating system to deliver pages
  6778. ** from the disk more rapidly.
  6779. **
  6780. ** An O(n^2) insertion sort algorithm is used, but since
  6781. ** n is never more than NB (a small constant), that should
  6782. ** not be a problem.
  6783. **
  6784. ** When NB==3, this one optimization makes the database
  6785. ** about 25% faster for large insertions and deletions.
  6786. */
  6787. for ( i = 0; i < k - 1; i++ )
  6788. {
  6789. int minV = (int)apNew[i].pgno;
  6790. int minI = i;
  6791. for ( j = i + 1; j < k; j++ )
  6792. {
  6793. if ( apNew[j].pgno < (u32)minV )
  6794. {
  6795. minI = j;
  6796. minV = (int)apNew[j].pgno;
  6797. }
  6798. }
  6799. if ( minI > i )
  6800. {
  6801. int t;
  6802. MemPage pT;
  6803. t = (int)apNew[i].pgno;
  6804. pT = apNew[i];
  6805. apNew[i] = apNew[minI];
  6806. apNew[minI] = pT;
  6807. }
  6808. }
  6809. TRACE( "new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n",
  6810. apNew[0].pgno, szNew[0],
  6811. nNew >= 2 ? apNew[1].pgno : 0, nNew >= 2 ? szNew[1] : 0,
  6812. nNew >= 3 ? apNew[2].pgno : 0, nNew >= 3 ? szNew[2] : 0,
  6813. nNew >= 4 ? apNew[3].pgno : 0, nNew >= 4 ? szNew[3] : 0,
  6814. nNew >= 5 ? apNew[4].pgno : 0, nNew >= 5 ? szNew[4] : 0 );
  6815. Debug.Assert( sqlite3PagerIswriteable( pParent.pDbPage ) );
  6816. sqlite3Put4byte( pParent.aData, pRight, apNew[nNew - 1].pgno );
  6817. /*
  6818. ** Evenly distribute the data in apCell[] across the new pages.
  6819. ** Insert divider cells into pParent as necessary.
  6820. */
  6821. j = 0;
  6822. for ( i = 0; i < nNew; i++ )
  6823. {
  6824. /* Assemble the new sibling page. */
  6825. MemPage pNew = apNew[i];
  6826. Debug.Assert( j < nMaxCells );
  6827. zeroPage( pNew, pageFlags );
  6828. assemblePage( pNew, cntNew[i] - j, apCell, szCell, j );
  6829. Debug.Assert( pNew.nCell > 0 || ( nNew == 1 && cntNew[0] == 0 ) );
  6830. Debug.Assert( pNew.nOverflow == 0 );
  6831. j = cntNew[i];
  6832. /* If the sibling page assembled above was not the right-most sibling,
  6833. ** insert a divider cell into the parent page.
  6834. */
  6835. Debug.Assert( i < nNew - 1 || j == nCell );
  6836. if ( j < nCell )
  6837. {
  6838. u8[] pCell;
  6839. u8[] pTemp;
  6840. int sz;
  6841. Debug.Assert( j < nMaxCells );
  6842. pCell = apCell[j];
  6843. sz = szCell[j] + leafCorrection;
  6844. pTemp = sqlite3Malloc( sz );//&aOvflSpace[iOvflSpace];
  6845. if ( 0 == pNew.leaf )
  6846. {
  6847. Buffer.BlockCopy( pCell, 0, pNew.aData, 8, 4 );//memcpy( pNew.aData[8], pCell, 4 );
  6848. }
  6849. else if ( leafData != 0 )
  6850. {
  6851. /* If the tree is a leaf-data tree, and the siblings are leaves,
  6852. ** then there is no divider cell in apCell[]. Instead, the divider
  6853. ** cell consists of the integer key for the right-most cell of
  6854. ** the sibling-page assembled above only.
  6855. */
  6856. CellInfo info = new CellInfo();
  6857. j--;
  6858. btreeParseCellPtr( pNew, apCell[j], ref info );
  6859. pCell = pTemp;
  6860. sz = 4 + putVarint( pCell, 4, (u64)info.nKey );
  6861. pTemp = null;
  6862. }
  6863. else
  6864. {
  6865. //------------ pCell -= 4;
  6866. byte[] _pCell_4 = sqlite3Malloc( pCell.Length + 4 );
  6867. Buffer.BlockCopy( pCell, 0, _pCell_4, 4, pCell.Length );
  6868. pCell = _pCell_4;
  6869. //
  6870. /* Obscure case for non-leaf-data trees: If the cell at pCell was
  6871. ** previously stored on a leaf node, and its reported size was 4
  6872. ** bytes, then it may actually be smaller than this
  6873. ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
  6874. ** any cell). But it is important to pass the correct size to
  6875. ** insertCell(), so reparse the cell now.
  6876. **
  6877. ** Note that this can never happen in an SQLite data file, as all
  6878. ** cells are at least 4 bytes. It only happens in b-trees used
  6879. ** to evaluate "IN (SELECT ...)" and similar clauses.
  6880. */
  6881. if ( szCell[j] == 4 )
  6882. {
  6883. Debug.Assert( leafCorrection == 4 );
  6884. sz = cellSizePtr( pParent, pCell );
  6885. }
  6886. }
  6887. iOvflSpace += sz;
  6888. Debug.Assert( sz <= pBt.maxLocal + 23 );
  6889. Debug.Assert( iOvflSpace <= pBt.pageSize );
  6890. insertCell( pParent, nxDiv, pCell, sz, pTemp, pNew.pgno, ref rc );
  6891. if ( rc != SQLITE_OK )
  6892. goto balance_cleanup;
  6893. Debug.Assert( sqlite3PagerIswriteable( pParent.pDbPage ) );
  6894. j++;
  6895. nxDiv++;
  6896. }
  6897. }
  6898. Debug.Assert( j == nCell );
  6899. Debug.Assert( nOld > 0 );
  6900. Debug.Assert( nNew > 0 );
  6901. if ( ( pageFlags & PTF_LEAF ) == 0 )
  6902. {
  6903. Buffer.BlockCopy( apCopy[nOld - 1].aData, 8, apNew[nNew - 1].aData, 8, 4 ); //u8* zChild = &apCopy[nOld - 1].aData[8];
  6904. //memcpy( apNew[nNew - 1].aData[8], zChild, 4 );
  6905. }
  6906. if ( isRoot != 0 && pParent.nCell == 0 && pParent.hdrOffset <= apNew[0].nFree )
  6907. {
  6908. /* The root page of the b-tree now contains no cells. The only sibling
  6909. ** page is the right-child of the parent. Copy the contents of the
  6910. ** child page into the parent, decreasing the overall height of the
  6911. ** b-tree structure by one. This is described as the "balance-shallower"
  6912. ** sub-algorithm in some documentation.
  6913. **
  6914. ** If this is an auto-vacuum database, the call to copyNodeContent()
  6915. ** sets all pointer-map entries corresponding to database image pages
  6916. ** for which the pointer is stored within the content being copied.
  6917. **
  6918. ** The second Debug.Assert below verifies that the child page is defragmented
  6919. ** (it must be, as it was just reconstructed using assemblePage()). This
  6920. ** is important if the parent page happens to be page 1 of the database
  6921. ** image. */
  6922. Debug.Assert( nNew == 1 );
  6923. Debug.Assert( apNew[0].nFree ==
  6924. ( get2byte( apNew[0].aData, 5 ) - apNew[0].cellOffset - apNew[0].nCell * 2 )
  6925. );
  6926. copyNodeContent( apNew[0], pParent, ref rc );
  6927. freePage( apNew[0], ref rc );
  6928. }
  6929. else
  6930. #if !SQLITE_OMIT_AUTOVACUUM // if ( ISAUTOVACUUM )
  6931. if ( pBt.autoVacuum )
  6932. #else
  6933. if (false)
  6934. #endif
  6935. {
  6936. /* Fix the pointer-map entries for all the cells that were shifted around.
  6937. ** There are several different types of pointer-map entries that need to
  6938. ** be dealt with by this routine. Some of these have been set already, but
  6939. ** many have not. The following is a summary:
  6940. **
  6941. ** 1) The entries associated with new sibling pages that were not
  6942. ** siblings when this function was called. These have already
  6943. ** been set. We don't need to worry about old siblings that were
  6944. ** moved to the free-list - the freePage() code has taken care
  6945. ** of those.
  6946. **
  6947. ** 2) The pointer-map entries associated with the first overflow
  6948. ** page in any overflow chains used by new divider cells. These
  6949. ** have also already been taken care of by the insertCell() code.
  6950. **
  6951. ** 3) If the sibling pages are not leaves, then the child pages of
  6952. ** cells stored on the sibling pages may need to be updated.
  6953. **
  6954. ** 4) If the sibling pages are not internal intkey nodes, then any
  6955. ** overflow pages used by these cells may need to be updated
  6956. ** (internal intkey nodes never contain pointers to overflow pages).
  6957. **
  6958. ** 5) If the sibling pages are not leaves, then the pointer-map
  6959. ** entries for the right-child pages of each sibling may need
  6960. ** to be updated.
  6961. **
  6962. ** Cases 1 and 2 are dealt with above by other code. The next
  6963. ** block deals with cases 3 and 4 and the one after that, case 5. Since
  6964. ** setting a pointer map entry is a relatively expensive operation, this
  6965. ** code only sets pointer map entries for child or overflow pages that have
  6966. ** actually moved between pages. */
  6967. MemPage pNew = apNew[0];
  6968. MemPage pOld = apCopy[0];
  6969. int nOverflow = pOld.nOverflow;
  6970. int iNextOld = pOld.nCell + nOverflow;
  6971. int iOverflow = ( nOverflow != 0 ? pOld.aOvfl[0].idx : -1 );
  6972. j = 0; /* Current 'old' sibling page */
  6973. k = 0; /* Current 'new' sibling page */
  6974. for ( i = 0; i < nCell; i++ )
  6975. {
  6976. int isDivider = 0;
  6977. while ( i == iNextOld )
  6978. {
  6979. /* Cell i is the cell immediately following the last cell on old
  6980. ** sibling page j. If the siblings are not leaf pages of an
  6981. ** intkey b-tree, then cell i was a divider cell. */
  6982. pOld = apCopy[++j];
  6983. iNextOld = i + ( 0 == leafData ? 1 : 0 ) + pOld.nCell + pOld.nOverflow;
  6984. if ( pOld.nOverflow != 0 )
  6985. {
  6986. nOverflow = pOld.nOverflow;
  6987. iOverflow = i + ( 0 == leafData ? 1 : 0 ) + pOld.aOvfl[0].idx;
  6988. }
  6989. isDivider = 0 == leafData ? 1 : 0;
  6990. }
  6991. Debug.Assert( nOverflow > 0 || iOverflow < i );
  6992. Debug.Assert( nOverflow < 2 || pOld.aOvfl[0].idx == pOld.aOvfl[1].idx - 1 );
  6993. Debug.Assert( nOverflow < 3 || pOld.aOvfl[1].idx == pOld.aOvfl[2].idx - 1 );
  6994. if ( i == iOverflow )
  6995. {
  6996. isDivider = 1;
  6997. if ( ( --nOverflow ) > 0 )
  6998. {
  6999. iOverflow++;
  7000. }
  7001. }
  7002. if ( i == cntNew[k] )
  7003. {
  7004. /* Cell i is the cell immediately following the last cell on new
  7005. ** sibling page k. If the siblings are not leaf pages of an
  7006. ** intkey b-tree, then cell i is a divider cell. */
  7007. pNew = apNew[++k];
  7008. if ( 0 == leafData )
  7009. continue;
  7010. }
  7011. Debug.Assert( j < nOld );
  7012. Debug.Assert( k < nNew );
  7013. /* If the cell was originally divider cell (and is not now) or
  7014. ** an overflow cell, or if the cell was located on a different sibling
  7015. ** page before the balancing, then the pointer map entries associated
  7016. ** with any child or overflow pages need to be updated. */
  7017. if ( isDivider != 0 || pOld.pgno != pNew.pgno )
  7018. {
  7019. if ( 0 == leafCorrection )
  7020. {
  7021. ptrmapPut( pBt, sqlite3Get4byte( apCell[i] ), PTRMAP_BTREE, pNew.pgno, ref rc );
  7022. }
  7023. if ( szCell[i] > pNew.minLocal )
  7024. {
  7025. ptrmapPutOvflPtr( pNew, apCell[i], ref rc );
  7026. }
  7027. }
  7028. }
  7029. if ( 0 == leafCorrection )
  7030. {
  7031. for ( i = 0; i < nNew; i++ )
  7032. {
  7033. u32 key = sqlite3Get4byte( apNew[i].aData, 8 );
  7034. ptrmapPut( pBt, key, PTRMAP_BTREE, apNew[i].pgno, ref rc );
  7035. }
  7036. }
  7037. #if FALSE
  7038. /* The ptrmapCheckPages() contains Debug.Assert() statements that verify that
  7039. ** all pointer map pages are set correctly. This is helpful while
  7040. ** debugging. This is usually disabled because a corrupt database may
  7041. ** cause an Debug.Assert() statement to fail. */
  7042. ptrmapCheckPages(apNew, nNew);
  7043. ptrmapCheckPages(pParent, 1);
  7044. #endif
  7045. }
  7046. Debug.Assert( pParent.isInit != 0 );
  7047. TRACE( "BALANCE: finished: old=%d new=%d cells=%d\n",
  7048. nOld, nNew, nCell );
  7049. /*
  7050. ** Cleanup before returning.
  7051. */
  7052. balance_cleanup:
  7053. sqlite3ScratchFree( apCell );
  7054. for ( i = 0; i < nOld; i++ )
  7055. {
  7056. releasePage( apOld[i] );
  7057. }
  7058. for ( i = 0; i < nNew; i++ )
  7059. {
  7060. releasePage( apNew[i] );
  7061. }
  7062. return rc;
  7063. }
  7064. /*
  7065. ** This function is called when the root page of a b-tree structure is
  7066. ** overfull (has one or more overflow pages).
  7067. **
  7068. ** A new child page is allocated and the contents of the current root
  7069. ** page, including overflow cells, are copied into the child. The root
  7070. ** page is then overwritten to make it an empty page with the right-child
  7071. ** pointer pointing to the new page.
  7072. **
  7073. ** Before returning, all pointer-map entries corresponding to pages
  7074. ** that the new child-page now contains pointers to are updated. The
  7075. ** entry corresponding to the new right-child pointer of the root
  7076. ** page is also updated.
  7077. **
  7078. ** If successful, ppChild is set to contain a reference to the child
  7079. ** page and SQLITE_OK is returned. In this case the caller is required
  7080. ** to call releasePage() on ppChild exactly once. If an error occurs,
  7081. ** an error code is returned and ppChild is set to 0.
  7082. */
  7083. static int balance_deeper( MemPage pRoot, ref MemPage ppChild )
  7084. {
  7085. int rc; /* Return value from subprocedures */
  7086. MemPage pChild = null; /* Pointer to a new child page */
  7087. Pgno pgnoChild = 0; /* Page number of the new child page */
  7088. BtShared pBt = pRoot.pBt; /* The BTree */
  7089. Debug.Assert( pRoot.nOverflow > 0 );
  7090. Debug.Assert( sqlite3_mutex_held( pBt.mutex ) );
  7091. /* Make pRoot, the root page of the b-tree, writable. Allocate a new
  7092. ** page that will become the new right-child of pPage. Copy the contents
  7093. ** of the node stored on pRoot into the new child page.
  7094. */
  7095. rc = sqlite3PagerWrite( pRoot.pDbPage );
  7096. if ( rc == SQLITE_OK )
  7097. {
  7098. rc = allocateBtreePage( pBt, ref pChild, ref pgnoChild, pRoot.pgno, 0 );
  7099. copyNodeContent( pRoot, pChild, ref rc );
  7100. #if !SQLITE_OMIT_AUTOVACUUM // if ( ISAUTOVACUUM )
  7101. if ( pBt.autoVacuum )
  7102. #else
  7103. if (false)
  7104. #endif
  7105. {
  7106. ptrmapPut( pBt, pgnoChild, PTRMAP_BTREE, pRoot.pgno, ref rc );
  7107. }
  7108. }
  7109. if ( rc != 0 )
  7110. {
  7111. ppChild = null;
  7112. releasePage( pChild );
  7113. return rc;
  7114. }
  7115. Debug.Assert( sqlite3PagerIswriteable( pChild.pDbPage ) );
  7116. Debug.Assert( sqlite3PagerIswriteable( pRoot.pDbPage ) );
  7117. Debug.Assert( pChild.nCell == pRoot.nCell );
  7118. TRACE( "BALANCE: copy root %d into %d\n", pRoot.pgno, pChild.pgno );
  7119. /* Copy the overflow cells from pRoot to pChild */
  7120. Array.Copy( pRoot.aOvfl, pChild.aOvfl, pRoot.nOverflow );//memcpy(pChild.aOvfl, pRoot.aOvfl, pRoot.nOverflow*sizeof(pRoot.aOvfl[0]));
  7121. pChild.nOverflow = pRoot.nOverflow;
  7122. /* Zero the contents of pRoot. Then install pChild as the right-child. */
  7123. zeroPage( pRoot, pChild.aData[0] & ~PTF_LEAF );
  7124. sqlite3Put4byte( pRoot.aData, pRoot.hdrOffset + 8, pgnoChild );
  7125. ppChild = pChild;
  7126. return SQLITE_OK;
  7127. }
  7128. /*
  7129. ** The page that pCur currently points to has just been modified in
  7130. ** some way. This function figures out if this modification means the
  7131. ** tree needs to be balanced, and if so calls the appropriate balancing
  7132. ** routine. Balancing routines are:
  7133. **
  7134. ** balance_quick()
  7135. ** balance_deeper()
  7136. ** balance_nonroot()
  7137. */
  7138. static u8[] aBalanceQuickSpace = new u8[13];
  7139. static int balance( BtCursor pCur )
  7140. {
  7141. int rc = SQLITE_OK;
  7142. int nMin = (int)pCur.pBt.usableSize * 2 / 3;
  7143. //u8[] pFree = null;
  7144. #if !NDEBUG || SQLITE_COVERAGE_TEST || DEBUG
  7145. int balance_quick_called = 0;//TESTONLY( int balance_quick_called = 0 );
  7146. int balance_deeper_called = 0;//TESTONLY( int balance_deeper_called = 0 );
  7147. #else
  7148. int balance_quick_called = 0;
  7149. int balance_deeper_called = 0;
  7150. #endif
  7151. do
  7152. {
  7153. int iPage = pCur.iPage;
  7154. MemPage pPage = pCur.apPage[iPage];
  7155. if ( iPage == 0 )
  7156. {
  7157. if ( pPage.nOverflow != 0 )
  7158. {
  7159. /* The root page of the b-tree is overfull. In this case call the
  7160. ** balance_deeper() function to create a new child for the root-page
  7161. ** and copy the current contents of the root-page to it. The
  7162. ** next iteration of the do-loop will balance the child page.
  7163. */
  7164. Debug.Assert( ( balance_deeper_called++ ) == 0 );
  7165. rc = balance_deeper( pPage, ref pCur.apPage[1] );
  7166. if ( rc == SQLITE_OK )
  7167. {
  7168. pCur.iPage = 1;
  7169. pCur.aiIdx[0] = 0;
  7170. pCur.aiIdx[1] = 0;
  7171. Debug.Assert( pCur.apPage[1].nOverflow != 0 );
  7172. }
  7173. }
  7174. else
  7175. {
  7176. break;
  7177. }
  7178. }
  7179. else if ( pPage.nOverflow == 0 && pPage.nFree <= nMin )
  7180. {
  7181. break;
  7182. }
  7183. else
  7184. {
  7185. MemPage pParent = pCur.apPage[iPage - 1];
  7186. int iIdx = pCur.aiIdx[iPage - 1];
  7187. rc = sqlite3PagerWrite( pParent.pDbPage );
  7188. if ( rc == SQLITE_OK )
  7189. {
  7190. #if !SQLITE_OMIT_QUICKBALANCE
  7191. if ( pPage.hasData != 0
  7192. && pPage.nOverflow == 1
  7193. && pPage.aOvfl[0].idx == pPage.nCell
  7194. && pParent.pgno != 1
  7195. && pParent.nCell == iIdx
  7196. )
  7197. {
  7198. /* Call balance_quick() to create a new sibling of pPage on which
  7199. ** to store the overflow cell. balance_quick() inserts a new cell
  7200. ** into pParent, which may cause pParent overflow. If this
  7201. ** happens, the next interation of the do-loop will balance pParent
  7202. ** use either balance_nonroot() or balance_deeper(). Until this
  7203. ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
  7204. ** buffer.
  7205. **
  7206. ** The purpose of the following Debug.Assert() is to check that only a
  7207. ** single call to balance_quick() is made for each call to this
  7208. ** function. If this were not verified, a subtle bug involving reuse
  7209. ** of the aBalanceQuickSpace[] might sneak in.
  7210. */
  7211. Debug.Assert( ( balance_quick_called++ ) == 0 );
  7212. rc = balance_quick( pParent, pPage, aBalanceQuickSpace );
  7213. }
  7214. else
  7215. #endif
  7216. {
  7217. /* In this case, call balance_nonroot() to redistribute cells
  7218. ** between pPage and up to 2 of its sibling pages. This involves
  7219. ** modifying the contents of pParent, which may cause pParent to
  7220. ** become overfull or underfull. The next iteration of the do-loop
  7221. ** will balance the parent page to correct this.
  7222. **
  7223. ** If the parent page becomes overfull, the overflow cell or cells
  7224. ** are stored in the pSpace buffer allocated immediately below.
  7225. ** A subsequent iteration of the do-loop will deal with this by
  7226. ** calling balance_nonroot() (balance_deeper() may be called first,
  7227. ** but it doesn't deal with overflow cells - just moves them to a
  7228. ** different page). Once this subsequent call to balance_nonroot()
  7229. ** has completed, it is safe to release the pSpace buffer used by
  7230. ** the previous call, as the overflow cell data will have been
  7231. ** copied either into the body of a database page or into the new
  7232. ** pSpace buffer passed to the latter call to balance_nonroot().
  7233. */
  7234. u8[] pSpace = new u8[pCur.pBt.pageSize];// u8 pSpace = sqlite3PageMalloc( pCur.pBt.pageSize );
  7235. rc = balance_nonroot( pParent, iIdx, null, iPage == 1 ? 1 : 0 );
  7236. //if (pFree != null)
  7237. //{
  7238. // /* If pFree is not NULL, it points to the pSpace buffer used
  7239. // ** by a previous call to balance_nonroot(). Its contents are
  7240. // ** now stored either on real database pages or within the
  7241. // ** new pSpace buffer, so it may be safely freed here. */
  7242. // sqlite3PageFree(ref pFree);
  7243. //}
  7244. /* The pSpace buffer will be freed after the next call to
  7245. ** balance_nonroot(), or just before this function returns, whichever
  7246. ** comes first. */
  7247. //pFree = pSpace;
  7248. }
  7249. }
  7250. pPage.nOverflow = 0;
  7251. /* The next iteration of the do-loop balances the parent page. */
  7252. releasePage( pPage );
  7253. pCur.iPage--;
  7254. }
  7255. } while ( rc == SQLITE_OK );
  7256. //if (pFree != null)
  7257. //{
  7258. // sqlite3PageFree(ref pFree);
  7259. //}
  7260. return rc;
  7261. }
  7262. /*
  7263. ** Insert a new record into the BTree. The key is given by (pKey,nKey)
  7264. ** and the data is given by (pData,nData). The cursor is used only to
  7265. ** define what table the record should be inserted into. The cursor
  7266. ** is left pointing at a random location.
  7267. **
  7268. ** For an INTKEY table, only the nKey value of the key is used. pKey is
  7269. ** ignored. For a ZERODATA table, the pData and nData are both ignored.
  7270. **
  7271. ** If the seekResult parameter is non-zero, then a successful call to
  7272. ** MovetoUnpacked() to seek cursor pCur to (pKey, nKey) has already
  7273. ** been performed. seekResult is the search result returned (a negative
  7274. ** number if pCur points at an entry that is smaller than (pKey, nKey), or
  7275. ** a positive value if pCur points at an etry that is larger than
  7276. ** (pKey, nKey)).
  7277. **
  7278. ** If the seekResult parameter is non-zero, then the caller guarantees that
  7279. ** cursor pCur is pointing at the existing copy of a row that is to be
  7280. ** overwritten. If the seekResult parameter is 0, then cursor pCur may
  7281. ** point to any entry or to no entry at all and so this function has to seek
  7282. ** the cursor before the new key can be inserted.
  7283. */
  7284. static int sqlite3BtreeInsert(
  7285. BtCursor pCur, /* Insert data into the table of this cursor */
  7286. byte[] pKey, i64 nKey, /* The key of the new record */
  7287. byte[] pData, int nData, /* The data of the new record */
  7288. int nZero, /* Number of extra 0 bytes to append to data */
  7289. int appendBias, /* True if this is likely an append */
  7290. int seekResult /* Result of prior MovetoUnpacked() call */
  7291. )
  7292. {
  7293. int rc;
  7294. int loc = seekResult; /* -1: before desired location +1: after */
  7295. int szNew = 0;
  7296. int idx;
  7297. MemPage pPage;
  7298. Btree p = pCur.pBtree;
  7299. BtShared pBt = p.pBt;
  7300. int oldCell;
  7301. byte[] newCell = null;
  7302. if ( pCur.eState == CURSOR_FAULT )
  7303. {
  7304. Debug.Assert( pCur.skipNext != SQLITE_OK );
  7305. return pCur.skipNext;
  7306. }
  7307. Debug.Assert( cursorHoldsMutex( pCur ) );
  7308. Debug.Assert( pCur.wrFlag != 0 && pBt.inTransaction == TRANS_WRITE && !pBt.readOnly );
  7309. Debug.Assert( hasSharedCacheTableLock( p, pCur.pgnoRoot, pCur.pKeyInfo != null ? 1 : 0, 2 ) );
  7310. /* Assert that the caller has been consistent. If this cursor was opened
  7311. ** expecting an index b-tree, then the caller should be inserting blob
  7312. ** keys with no associated data. If the cursor was opened expecting an
  7313. ** intkey table, the caller should be inserting integer keys with a
  7314. ** blob of associated data. */
  7315. Debug.Assert( ( pKey == null ) == ( pCur.pKeyInfo == null ) );
  7316. /* If this is an insert into a table b-tree, invalidate any incrblob
  7317. ** cursors open on the row being replaced (assuming this is a replace
  7318. ** operation - if it is not, the following is a no-op). */
  7319. if ( pCur.pKeyInfo == null )
  7320. {
  7321. invalidateIncrblobCursors( p, nKey, 0 );
  7322. }
  7323. /* Save the positions of any other cursors open on this table.
  7324. **
  7325. ** In some cases, the call to btreeMoveto() below is a no-op. For
  7326. ** example, when inserting data into a table with auto-generated integer
  7327. ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the
  7328. ** integer key to use. It then calls this function to actually insert the
  7329. ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
  7330. ** that the cursor is already where it needs to be and returns without
  7331. ** doing any work. To avoid thwarting these optimizations, it is important
  7332. ** not to clear the cursor here.
  7333. */
  7334. rc = saveAllCursors( pBt, pCur.pgnoRoot, pCur );
  7335. if ( rc != 0 )
  7336. return rc;
  7337. if ( 0 == loc )
  7338. {
  7339. rc = btreeMoveto( pCur, pKey, nKey, appendBias, ref loc );
  7340. if ( rc != 0 )
  7341. return rc;
  7342. }
  7343. Debug.Assert( pCur.eState == CURSOR_VALID || ( pCur.eState == CURSOR_INVALID && loc != 0 ) );
  7344. pPage = pCur.apPage[pCur.iPage];
  7345. Debug.Assert( pPage.intKey != 0 || nKey >= 0 );
  7346. Debug.Assert( pPage.leaf != 0 || 0 == pPage.intKey );
  7347. TRACE( "INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
  7348. pCur.pgnoRoot, nKey, nData, pPage.pgno,
  7349. loc == 0 ? "overwrite" : "new entry" );
  7350. Debug.Assert( pPage.isInit != 0 );
  7351. allocateTempSpace( pBt );
  7352. newCell = pBt.pTmpSpace;
  7353. //if (newCell == null) return SQLITE_NOMEM;
  7354. rc = fillInCell( pPage, newCell, pKey, nKey, pData, nData, nZero, ref szNew );
  7355. if ( rc != 0 )
  7356. goto end_insert;
  7357. Debug.Assert( szNew == cellSizePtr( pPage, newCell ) );
  7358. Debug.Assert( szNew <= MX_CELL_SIZE( pBt ) );
  7359. idx = pCur.aiIdx[pCur.iPage];
  7360. if ( loc == 0 )
  7361. {
  7362. u16 szOld;
  7363. Debug.Assert( idx < pPage.nCell );
  7364. rc = sqlite3PagerWrite( pPage.pDbPage );
  7365. if ( rc != 0 )
  7366. {
  7367. goto end_insert;
  7368. }
  7369. oldCell = findCell( pPage, idx );
  7370. if ( 0 == pPage.leaf )
  7371. {
  7372. //memcpy(newCell, oldCell, 4);
  7373. newCell[0] = pPage.aData[oldCell + 0];
  7374. newCell[1] = pPage.aData[oldCell + 1];
  7375. newCell[2] = pPage.aData[oldCell + 2];
  7376. newCell[3] = pPage.aData[oldCell + 3];
  7377. }
  7378. szOld = cellSizePtr( pPage, oldCell );
  7379. rc = clearCell( pPage, oldCell );
  7380. dropCell( pPage, idx, szOld, ref rc );
  7381. if ( rc != 0 )
  7382. goto end_insert;
  7383. }
  7384. else if ( loc < 0 && pPage.nCell > 0 )
  7385. {
  7386. Debug.Assert( pPage.leaf != 0 );
  7387. idx = ++pCur.aiIdx[pCur.iPage];
  7388. }
  7389. else
  7390. {
  7391. Debug.Assert( pPage.leaf != 0 );
  7392. }
  7393. insertCell( pPage, idx, newCell, szNew, null, 0, ref rc );
  7394. Debug.Assert( rc != SQLITE_OK || pPage.nCell > 0 || pPage.nOverflow > 0 );
  7395. /* If no error has occured and pPage has an overflow cell, call balance()
  7396. ** to redistribute the cells within the tree. Since balance() may move
  7397. ** the cursor, zero the BtCursor.info.nSize and BtCursor.validNKey
  7398. ** variables.
  7399. **
  7400. ** Previous versions of SQLite called moveToRoot() to move the cursor
  7401. ** back to the root page as balance() used to invalidate the contents
  7402. ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
  7403. ** set the cursor state to "invalid". This makes common insert operations
  7404. ** slightly faster.
  7405. **
  7406. ** There is a subtle but important optimization here too. When inserting
  7407. ** multiple records into an intkey b-tree using a single cursor (as can
  7408. ** happen while processing an "INSERT INTO ... SELECT" statement), it
  7409. ** is advantageous to leave the cursor pointing to the last entry in
  7410. ** the b-tree if possible. If the cursor is left pointing to the last
  7411. ** entry in the table, and the next row inserted has an integer key
  7412. ** larger than the largest existing key, it is possible to insert the
  7413. ** row without seeking the cursor. This can be a big performance boost.
  7414. */
  7415. pCur.info.nSize = 0;
  7416. pCur.validNKey = false;
  7417. if ( rc == SQLITE_OK && pPage.nOverflow != 0 )
  7418. {
  7419. rc = balance( pCur );
  7420. /* Must make sure nOverflow is reset to zero even if the balance()
  7421. ** fails. Internal data structure corruption will result otherwise.
  7422. ** Also, set the cursor state to invalid. This stops saveCursorPosition()
  7423. ** from trying to save the current position of the cursor. */
  7424. pCur.apPage[pCur.iPage].nOverflow = 0;
  7425. pCur.eState = CURSOR_INVALID;
  7426. }
  7427. Debug.Assert( pCur.apPage[pCur.iPage].nOverflow == 0 );
  7428. end_insert:
  7429. return rc;
  7430. }
  7431. /*
  7432. ** Delete the entry that the cursor is pointing to. The cursor
  7433. ** is left pointing at a arbitrary location.
  7434. */
  7435. static int sqlite3BtreeDelete( BtCursor pCur )
  7436. {
  7437. Btree p = pCur.pBtree;
  7438. BtShared pBt = p.pBt;
  7439. int rc; /* Return code */
  7440. MemPage pPage; /* Page to delete cell from */
  7441. int pCell; /* Pointer to cell to delete */
  7442. int iCellIdx; /* Index of cell to delete */
  7443. int iCellDepth; /* Depth of node containing pCell */
  7444. Debug.Assert( cursorHoldsMutex( pCur ) );
  7445. Debug.Assert( pBt.inTransaction == TRANS_WRITE );
  7446. Debug.Assert( !pBt.readOnly );
  7447. Debug.Assert( pCur.wrFlag != 0 );
  7448. Debug.Assert( hasSharedCacheTableLock( p, pCur.pgnoRoot, pCur.pKeyInfo != null ? 1 : 0, 2 ) );
  7449. Debug.Assert( !hasReadConflicts( p, pCur.pgnoRoot ) );
  7450. if ( NEVER( pCur.aiIdx[pCur.iPage] >= pCur.apPage[pCur.iPage].nCell )
  7451. || NEVER( pCur.eState != CURSOR_VALID )
  7452. )
  7453. {
  7454. return SQLITE_ERROR; /* Something has gone awry. */
  7455. }
  7456. /* If this is a delete operation to remove a row from a table b-tree,
  7457. ** invalidate any incrblob cursors open on the row being deleted. */
  7458. if ( pCur.pKeyInfo == null )
  7459. {
  7460. invalidateIncrblobCursors( p, pCur.info.nKey, 0 );
  7461. }
  7462. iCellDepth = pCur.iPage;
  7463. iCellIdx = pCur.aiIdx[iCellDepth];
  7464. pPage = pCur.apPage[iCellDepth];
  7465. pCell = findCell( pPage, iCellIdx );
  7466. /* If the page containing the entry to delete is not a leaf page, move
  7467. ** the cursor to the largest entry in the tree that is smaller than
  7468. ** the entry being deleted. This cell will replace the cell being deleted
  7469. ** from the internal node. The 'previous' entry is used for this instead
  7470. ** of the 'next' entry, as the previous entry is always a part of the
  7471. ** sub-tree headed by the child page of the cell being deleted. This makes
  7472. ** balancing the tree following the delete operation easier. */
  7473. if ( 0 == pPage.leaf )
  7474. {
  7475. int notUsed = 0;
  7476. rc = sqlite3BtreePrevious( pCur, ref notUsed );
  7477. if ( rc != 0 )
  7478. return rc;
  7479. }
  7480. /* Save the positions of any other cursors open on this table before
  7481. ** making any modifications. Make the page containing the entry to be
  7482. ** deleted writable. Then free any overflow pages associated with the
  7483. ** entry and finally remove the cell itself from within the page.
  7484. */
  7485. rc = saveAllCursors( pBt, pCur.pgnoRoot, pCur );
  7486. if ( rc != 0 )
  7487. return rc;
  7488. rc = sqlite3PagerWrite( pPage.pDbPage );
  7489. if ( rc != 0 )
  7490. return rc;
  7491. rc = clearCell( pPage, pCell );
  7492. dropCell( pPage, iCellIdx, cellSizePtr( pPage, pCell ), ref rc );
  7493. if ( rc != 0 )
  7494. return rc;
  7495. /* If the cell deleted was not located on a leaf page, then the cursor
  7496. ** is currently pointing to the largest entry in the sub-tree headed
  7497. ** by the child-page of the cell that was just deleted from an internal
  7498. ** node. The cell from the leaf node needs to be moved to the internal
  7499. ** node to replace the deleted cell. */
  7500. if ( 0 == pPage.leaf )
  7501. {
  7502. MemPage pLeaf = pCur.apPage[pCur.iPage];
  7503. int nCell;
  7504. Pgno n = pCur.apPage[iCellDepth + 1].pgno;
  7505. //byte[] pTmp;
  7506. pCell = findCell( pLeaf, pLeaf.nCell - 1 );
  7507. nCell = cellSizePtr( pLeaf, pCell );
  7508. Debug.Assert( MX_CELL_SIZE( pBt ) >= nCell );
  7509. //allocateTempSpace(pBt);
  7510. //pTmp = pBt.pTmpSpace;
  7511. rc = sqlite3PagerWrite( pLeaf.pDbPage );
  7512. byte[] pNext_4 = sqlite3Malloc( nCell + 4 );
  7513. Buffer.BlockCopy( pLeaf.aData, pCell - 4, pNext_4, 0, nCell + 4 );
  7514. insertCell( pPage, iCellIdx, pNext_4, nCell + 4, null, n, ref rc ); //insertCell( pPage, iCellIdx, pCell - 4, nCell + 4, pTmp, n, ref rc );
  7515. dropCell( pLeaf, pLeaf.nCell - 1, nCell, ref rc );
  7516. if ( rc != 0 )
  7517. return rc;
  7518. }
  7519. /* Balance the tree. If the entry deleted was located on a leaf page,
  7520. ** then the cursor still points to that page. In this case the first
  7521. ** call to balance() repairs the tree, and the if(...) condition is
  7522. ** never true.
  7523. **
  7524. ** Otherwise, if the entry deleted was on an internal node page, then
  7525. ** pCur is pointing to the leaf page from which a cell was removed to
  7526. ** replace the cell deleted from the internal node. This is slightly
  7527. ** tricky as the leaf node may be underfull, and the internal node may
  7528. ** be either under or overfull. In this case run the balancing algorithm
  7529. ** on the leaf node first. If the balance proceeds far enough up the
  7530. ** tree that we can be sure that any problem in the internal node has
  7531. ** been corrected, so be it. Otherwise, after balancing the leaf node,
  7532. ** walk the cursor up the tree to the internal node and balance it as
  7533. ** well. */
  7534. rc = balance( pCur );
  7535. if ( rc == SQLITE_OK && pCur.iPage > iCellDepth )
  7536. {
  7537. while ( pCur.iPage > iCellDepth )
  7538. {
  7539. releasePage( pCur.apPage[pCur.iPage--] );
  7540. }
  7541. rc = balance( pCur );
  7542. }
  7543. if ( rc == SQLITE_OK )
  7544. {
  7545. moveToRoot( pCur );
  7546. }
  7547. return rc;
  7548. }
  7549. /*
  7550. ** Create a new BTree table. Write into piTable the page
  7551. ** number for the root page of the new table.
  7552. **
  7553. ** The type of type is determined by the flags parameter. Only the
  7554. ** following values of flags are currently in use. Other values for
  7555. ** flags might not work:
  7556. **
  7557. ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys
  7558. ** BTREE_ZERODATA Used for SQL indices
  7559. */
  7560. static int btreeCreateTable( Btree p, ref int piTable, int createTabFlags )
  7561. {
  7562. BtShared pBt = p.pBt;
  7563. MemPage pRoot = new MemPage();
  7564. Pgno pgnoRoot = 0;
  7565. int rc;
  7566. int ptfFlags; /* Page-type flage for the root page of new table */
  7567. Debug.Assert( sqlite3BtreeHoldsMutex( p ) );
  7568. Debug.Assert( pBt.inTransaction == TRANS_WRITE );
  7569. Debug.Assert( !pBt.readOnly );
  7570. #if SQLITE_OMIT_AUTOVACUUM
  7571. rc = allocateBtreePage(pBt, ref pRoot, ref pgnoRoot, 1, 0);
  7572. if( rc !=0){
  7573. return rc;
  7574. }
  7575. #else
  7576. if ( pBt.autoVacuum )
  7577. {
  7578. Pgno pgnoMove = 0; /* Move a page here to make room for the root-page */
  7579. MemPage pPageMove = new MemPage(); /* The page to move to. */
  7580. /* Creating a new table may probably require moving an existing database
  7581. ** to make room for the new tables root page. In case this page turns
  7582. ** out to be an overflow page, delete all overflow page-map caches
  7583. ** held by open cursors.
  7584. */
  7585. invalidateAllOverflowCache( pBt );
  7586. /* Read the value of meta[3] from the database to determine where the
  7587. ** root page of the new table should go. meta[3] is the largest root-page
  7588. ** created so far, so the new root-page is (meta[3]+1).
  7589. */
  7590. sqlite3BtreeGetMeta( p, BTREE_LARGEST_ROOT_PAGE, ref pgnoRoot );
  7591. pgnoRoot++;
  7592. /* The new root-page may not be allocated on a pointer-map page, or the
  7593. ** PENDING_BYTE page.
  7594. */
  7595. while ( pgnoRoot == PTRMAP_PAGENO( pBt, pgnoRoot ) ||
  7596. pgnoRoot == PENDING_BYTE_PAGE( pBt ) )
  7597. {
  7598. pgnoRoot++;
  7599. }
  7600. Debug.Assert( pgnoRoot >= 3 );
  7601. /* Allocate a page. The page that currently resides at pgnoRoot will
  7602. ** be moved to the allocated page (unless the allocated page happens
  7603. ** to reside at pgnoRoot).
  7604. */
  7605. rc = allocateBtreePage( pBt, ref pPageMove, ref pgnoMove, pgnoRoot, 1 );
  7606. if ( rc != SQLITE_OK )
  7607. {
  7608. return rc;
  7609. }
  7610. if ( pgnoMove != pgnoRoot )
  7611. {
  7612. /* pgnoRoot is the page that will be used for the root-page of
  7613. ** the new table (assuming an error did not occur). But we were
  7614. ** allocated pgnoMove. If required (i.e. if it was not allocated
  7615. ** by extending the file), the current page at position pgnoMove
  7616. ** is already journaled.
  7617. */
  7618. u8 eType = 0;
  7619. Pgno iPtrPage = 0;
  7620. releasePage( pPageMove );
  7621. /* Move the page currently at pgnoRoot to pgnoMove. */
  7622. rc = btreeGetPage( pBt, pgnoRoot, ref pRoot, 0 );
  7623. if ( rc != SQLITE_OK )
  7624. {
  7625. return rc;
  7626. }
  7627. rc = ptrmapGet( pBt, pgnoRoot, ref eType, ref iPtrPage );
  7628. if ( eType == PTRMAP_ROOTPAGE || eType == PTRMAP_FREEPAGE )
  7629. {
  7630. rc = SQLITE_CORRUPT_BKPT();
  7631. }
  7632. if ( rc != SQLITE_OK )
  7633. {
  7634. releasePage( pRoot );
  7635. return rc;
  7636. }
  7637. Debug.Assert( eType != PTRMAP_ROOTPAGE );
  7638. Debug.Assert( eType != PTRMAP_FREEPAGE );
  7639. rc = relocatePage( pBt, pRoot, eType, iPtrPage, pgnoMove, 0 );
  7640. releasePage( pRoot );
  7641. /* Obtain the page at pgnoRoot */
  7642. if ( rc != SQLITE_OK )
  7643. {
  7644. return rc;
  7645. }
  7646. rc = btreeGetPage( pBt, pgnoRoot, ref pRoot, 0 );
  7647. if ( rc != SQLITE_OK )
  7648. {
  7649. return rc;
  7650. }
  7651. rc = sqlite3PagerWrite( pRoot.pDbPage );
  7652. if ( rc != SQLITE_OK )
  7653. {
  7654. releasePage( pRoot );
  7655. return rc;
  7656. }
  7657. }
  7658. else
  7659. {
  7660. pRoot = pPageMove;
  7661. }
  7662. /* Update the pointer-map and meta-data with the new root-page number. */
  7663. ptrmapPut( pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0, ref rc );
  7664. if ( rc != 0 )
  7665. {
  7666. releasePage( pRoot );
  7667. return rc;
  7668. }
  7669. /* When the new root page was allocated, page 1 was made writable in
  7670. ** order either to increase the database filesize, or to decrement the
  7671. ** freelist count. Hence, the sqlite3BtreeUpdateMeta() call cannot fail.
  7672. */
  7673. Debug.Assert( sqlite3PagerIswriteable( pBt.pPage1.pDbPage ) );
  7674. rc = sqlite3BtreeUpdateMeta( p, 4, pgnoRoot );
  7675. if ( NEVER( rc != 0 ) )
  7676. {
  7677. releasePage( pRoot );
  7678. return rc;
  7679. }
  7680. }
  7681. else
  7682. {
  7683. rc = allocateBtreePage( pBt, ref pRoot, ref pgnoRoot, 1, 0 );
  7684. if ( rc != 0 )
  7685. return rc;
  7686. }
  7687. #endif
  7688. Debug.Assert( sqlite3PagerIswriteable( pRoot.pDbPage ) );
  7689. if ( ( createTabFlags & BTREE_INTKEY ) != 0 )
  7690. {
  7691. ptfFlags = PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF;
  7692. }
  7693. else
  7694. {
  7695. ptfFlags = PTF_ZERODATA | PTF_LEAF;
  7696. }
  7697. zeroPage( pRoot, ptfFlags );
  7698. sqlite3PagerUnref( pRoot.pDbPage );
  7699. Debug.Assert( ( pBt.openFlags & BTREE_SINGLE ) == 0 || pgnoRoot == 2 );
  7700. piTable = (int)pgnoRoot;
  7701. return SQLITE_OK;
  7702. }
  7703. static int sqlite3BtreeCreateTable( Btree p, ref int piTable, int flags )
  7704. {
  7705. int rc;
  7706. sqlite3BtreeEnter( p );
  7707. rc = btreeCreateTable( p, ref piTable, flags );
  7708. sqlite3BtreeLeave( p );
  7709. return rc;
  7710. }
  7711. /*
  7712. ** Erase the given database page and all its children. Return
  7713. ** the page to the freelist.
  7714. */
  7715. static int clearDatabasePage(
  7716. BtShared pBt, /* The BTree that contains the table */
  7717. Pgno pgno, /* Page number to clear */
  7718. int freePageFlag, /* Deallocate page if true */
  7719. ref int pnChange /* Add number of Cells freed to this counter */
  7720. )
  7721. {
  7722. MemPage pPage = new MemPage();
  7723. int rc;
  7724. byte[] pCell;
  7725. int i;
  7726. Debug.Assert( sqlite3_mutex_held( pBt.mutex ) );
  7727. if ( pgno > btreePagecount( pBt ) )
  7728. {
  7729. return SQLITE_CORRUPT_BKPT();
  7730. }
  7731. rc = getAndInitPage( pBt, pgno, ref pPage );
  7732. if ( rc != 0 )
  7733. return rc;
  7734. for ( i = 0; i < pPage.nCell; i++ )
  7735. {
  7736. int iCell = findCell( pPage, i );
  7737. pCell = pPage.aData; // pCell = findCell( pPage, i );
  7738. if ( 0 == pPage.leaf )
  7739. {
  7740. rc = clearDatabasePage( pBt, sqlite3Get4byte( pCell, iCell ), 1, ref pnChange );
  7741. if ( rc != 0 )
  7742. goto cleardatabasepage_out;
  7743. }
  7744. rc = clearCell( pPage, iCell );
  7745. if ( rc != 0 )
  7746. goto cleardatabasepage_out;
  7747. }
  7748. if ( 0 == pPage.leaf )
  7749. {
  7750. rc = clearDatabasePage( pBt, sqlite3Get4byte( pPage.aData, 8 ), 1, ref pnChange );
  7751. if ( rc != 0 )
  7752. goto cleardatabasepage_out;
  7753. }
  7754. else //if (pnChange != 0)
  7755. {
  7756. //Debug.Assert(pPage.intKey != 0);
  7757. pnChange += pPage.nCell;
  7758. }
  7759. if ( freePageFlag != 0 )
  7760. {
  7761. freePage( pPage, ref rc );
  7762. }
  7763. else if ( ( rc = sqlite3PagerWrite( pPage.pDbPage ) ) == 0 )
  7764. {
  7765. zeroPage( pPage, pPage.aData[0] | PTF_LEAF );
  7766. }
  7767. cleardatabasepage_out:
  7768. releasePage( pPage );
  7769. return rc;
  7770. }
  7771. /*
  7772. ** Delete all information from a single table in the database. iTable is
  7773. ** the page number of the root of the table. After this routine returns,
  7774. ** the root page is empty, but still exists.
  7775. **
  7776. ** This routine will fail with SQLITE_LOCKED if there are any open
  7777. ** read cursors on the table. Open write cursors are moved to the
  7778. ** root of the table.
  7779. **
  7780. ** If pnChange is not NULL, then table iTable must be an intkey table. The
  7781. ** integer value pointed to by pnChange is incremented by the number of
  7782. ** entries in the table.
  7783. */
  7784. static int sqlite3BtreeClearTable( Btree p, int iTable, ref int pnChange )
  7785. {
  7786. int rc;
  7787. BtShared pBt = p.pBt;
  7788. sqlite3BtreeEnter( p );
  7789. Debug.Assert( p.inTrans == TRANS_WRITE );
  7790. /* Invalidate all incrblob cursors open on table iTable (assuming iTable
  7791. ** is the root of a table b-tree - if it is not, the following call is
  7792. ** a no-op). */
  7793. invalidateIncrblobCursors( p, 0, 1 );
  7794. rc = saveAllCursors( pBt, (Pgno)iTable, null );
  7795. if ( SQLITE_OK == rc )
  7796. {
  7797. rc = clearDatabasePage( pBt, (Pgno)iTable, 0, ref pnChange );
  7798. }
  7799. sqlite3BtreeLeave( p );
  7800. return rc;
  7801. }
  7802. /*
  7803. ** Erase all information in a table and add the root of the table to
  7804. ** the freelist. Except, the root of the principle table (the one on
  7805. ** page 1) is never added to the freelist.
  7806. **
  7807. ** This routine will fail with SQLITE_LOCKED if there are any open
  7808. ** cursors on the table.
  7809. **
  7810. ** If AUTOVACUUM is enabled and the page at iTable is not the last
  7811. ** root page in the database file, then the last root page
  7812. ** in the database file is moved into the slot formerly occupied by
  7813. ** iTable and that last slot formerly occupied by the last root page
  7814. ** is added to the freelist instead of iTable. In this say, all
  7815. ** root pages are kept at the beginning of the database file, which
  7816. ** is necessary for AUTOVACUUM to work right. piMoved is set to the
  7817. ** page number that used to be the last root page in the file before
  7818. ** the move. If no page gets moved, piMoved is set to 0.
  7819. ** The last root page is recorded in meta[3] and the value of
  7820. ** meta[3] is updated by this procedure.
  7821. */
  7822. static int btreeDropTable( Btree p, Pgno iTable, ref int piMoved )
  7823. {
  7824. int rc;
  7825. MemPage pPage = null;
  7826. BtShared pBt = p.pBt;
  7827. Debug.Assert( sqlite3BtreeHoldsMutex( p ) );
  7828. Debug.Assert( p.inTrans == TRANS_WRITE );
  7829. /* It is illegal to drop a table if any cursors are open on the
  7830. ** database. This is because in auto-vacuum mode the backend may
  7831. ** need to move another root-page to fill a gap left by the deleted
  7832. ** root page. If an open cursor was using this page a problem would
  7833. ** occur.
  7834. **
  7835. ** This error is caught long before control reaches this point.
  7836. */
  7837. if ( NEVER( pBt.pCursor ) )
  7838. {
  7839. sqlite3ConnectionBlocked( p.db, pBt.pCursor.pBtree.db );
  7840. return SQLITE_LOCKED_SHAREDCACHE;
  7841. }
  7842. rc = btreeGetPage( pBt, (Pgno)iTable, ref pPage, 0 );
  7843. if ( rc != 0 )
  7844. return rc;
  7845. int Dummy0 = 0;
  7846. rc = sqlite3BtreeClearTable( p, (int)iTable, ref Dummy0 );
  7847. if ( rc != 0 )
  7848. {
  7849. releasePage( pPage );
  7850. return rc;
  7851. }
  7852. piMoved = 0;
  7853. if ( iTable > 1 )
  7854. {
  7855. #if SQLITE_OMIT_AUTOVACUUM
  7856. freePage(pPage, ref rc);
  7857. releasePage(pPage);
  7858. #else
  7859. if ( pBt.autoVacuum )
  7860. {
  7861. Pgno maxRootPgno = 0;
  7862. sqlite3BtreeGetMeta( p, BTREE_LARGEST_ROOT_PAGE, ref maxRootPgno );
  7863. if ( iTable == maxRootPgno )
  7864. {
  7865. /* If the table being dropped is the table with the largest root-page
  7866. ** number in the database, put the root page on the free list.
  7867. */
  7868. freePage( pPage, ref rc );
  7869. releasePage( pPage );
  7870. if ( rc != SQLITE_OK )
  7871. {
  7872. return rc;
  7873. }
  7874. }
  7875. else
  7876. {
  7877. /* The table being dropped does not have the largest root-page
  7878. ** number in the database. So move the page that does into the
  7879. ** gap left by the deleted root-page.
  7880. */
  7881. MemPage pMove = new MemPage();
  7882. releasePage( pPage );
  7883. rc = btreeGetPage( pBt, maxRootPgno, ref pMove, 0 );
  7884. if ( rc != SQLITE_OK )
  7885. {
  7886. return rc;
  7887. }
  7888. rc = relocatePage( pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0 );
  7889. releasePage( pMove );
  7890. if ( rc != SQLITE_OK )
  7891. {
  7892. return rc;
  7893. }
  7894. pMove = null;
  7895. rc = btreeGetPage( pBt, maxRootPgno, ref pMove, 0 );
  7896. freePage( pMove, ref rc );
  7897. releasePage( pMove );
  7898. if ( rc != SQLITE_OK )
  7899. {
  7900. return rc;
  7901. }
  7902. piMoved = (int)maxRootPgno;
  7903. }
  7904. /* Set the new 'max-root-page' value in the database header. This
  7905. ** is the old value less one, less one more if that happens to
  7906. ** be a root-page number, less one again if that is the
  7907. ** PENDING_BYTE_PAGE.
  7908. */
  7909. maxRootPgno--;
  7910. while ( maxRootPgno == PENDING_BYTE_PAGE( pBt )
  7911. || PTRMAP_ISPAGE( pBt, maxRootPgno ) )
  7912. {
  7913. maxRootPgno--;
  7914. }
  7915. Debug.Assert( maxRootPgno != PENDING_BYTE_PAGE( pBt ) );
  7916. rc = sqlite3BtreeUpdateMeta( p, 4, maxRootPgno );
  7917. }
  7918. else
  7919. {
  7920. freePage( pPage, ref rc );
  7921. releasePage( pPage );
  7922. }
  7923. #endif
  7924. }
  7925. else
  7926. {
  7927. /* If sqlite3BtreeDropTable was called on page 1.
  7928. ** This really never should happen except in a corrupt
  7929. ** database.
  7930. */
  7931. zeroPage( pPage, PTF_INTKEY | PTF_LEAF );
  7932. releasePage( pPage );
  7933. }
  7934. return rc;
  7935. }
  7936. static int sqlite3BtreeDropTable( Btree p, int iTable, ref int piMoved )
  7937. {
  7938. int rc;
  7939. sqlite3BtreeEnter( p );
  7940. rc = btreeDropTable( p, (u32)iTable, ref piMoved );
  7941. sqlite3BtreeLeave( p );
  7942. return rc;
  7943. }
  7944. /*
  7945. ** This function may only be called if the b-tree connection already
  7946. ** has a read or write transaction open on the database.
  7947. **
  7948. ** Read the meta-information out of a database file. Meta[0]
  7949. ** is the number of free pages currently in the database. Meta[1]
  7950. ** through meta[15] are available for use by higher layers. Meta[0]
  7951. ** is read-only, the others are read/write.
  7952. **
  7953. ** The schema layer numbers meta values differently. At the schema
  7954. ** layer (and the SetCookie and ReadCookie opcodes) the number of
  7955. ** free pages is not visible. So Cookie[0] is the same as Meta[1].
  7956. */
  7957. static void sqlite3BtreeGetMeta( Btree p, int idx, ref u32 pMeta )
  7958. {
  7959. BtShared pBt = p.pBt;
  7960. sqlite3BtreeEnter( p );
  7961. Debug.Assert( p.inTrans > TRANS_NONE );
  7962. Debug.Assert( SQLITE_OK == querySharedCacheTableLock( p, MASTER_ROOT, READ_LOCK ) );
  7963. Debug.Assert( pBt.pPage1 != null );
  7964. Debug.Assert( idx >= 0 && idx <= 15 );
  7965. pMeta = sqlite3Get4byte( pBt.pPage1.aData, 36 + idx * 4 );
  7966. /* If auto-vacuum is disabled in this build and this is an auto-vacuum
  7967. ** database, mark the database as read-only. */
  7968. #if SQLITE_OMIT_AUTOVACUUM
  7969. if( idx==BTREE_LARGEST_ROOT_PAGE && pMeta>0 ) pBt.readOnly = 1;
  7970. #endif
  7971. sqlite3BtreeLeave( p );
  7972. }
  7973. /*
  7974. ** Write meta-information back into the database. Meta[0] is
  7975. ** read-only and may not be written.
  7976. */
  7977. static int sqlite3BtreeUpdateMeta( Btree p, int idx, u32 iMeta )
  7978. {
  7979. BtShared pBt = p.pBt;
  7980. byte[] pP1;
  7981. int rc;
  7982. Debug.Assert( idx >= 1 && idx <= 15 );
  7983. sqlite3BtreeEnter( p );
  7984. Debug.Assert( p.inTrans == TRANS_WRITE );
  7985. Debug.Assert( pBt.pPage1 != null );
  7986. pP1 = pBt.pPage1.aData;
  7987. rc = sqlite3PagerWrite( pBt.pPage1.pDbPage );
  7988. if ( rc == SQLITE_OK )
  7989. {
  7990. sqlite3Put4byte( pP1, 36 + idx * 4, iMeta );
  7991. #if !SQLITE_OMIT_AUTOVACUUM
  7992. if ( idx == BTREE_INCR_VACUUM )
  7993. {
  7994. Debug.Assert( pBt.autoVacuum || iMeta == 0 );
  7995. Debug.Assert( iMeta == 0 || iMeta == 1 );
  7996. pBt.incrVacuum = iMeta != 0;
  7997. }
  7998. #endif
  7999. }
  8000. sqlite3BtreeLeave( p );
  8001. return rc;
  8002. }
  8003. #if !SQLITE_OMIT_BTREECOUNT
  8004. /*
  8005. ** The first argument, pCur, is a cursor opened on some b-tree. Count the
  8006. ** number of entries in the b-tree and write the result to pnEntry.
  8007. **
  8008. ** SQLITE_OK is returned if the operation is successfully executed.
  8009. ** Otherwise, if an error is encountered (i.e. an IO error or database
  8010. ** corruption) an SQLite error code is returned.
  8011. */
  8012. static int sqlite3BtreeCount( BtCursor pCur, ref i64 pnEntry )
  8013. {
  8014. i64 nEntry = 0; /* Value to return in pnEntry */
  8015. int rc; /* Return code */
  8016. rc = moveToRoot( pCur );
  8017. /* Unless an error occurs, the following loop runs one iteration for each
  8018. ** page in the B-Tree structure (not including overflow pages).
  8019. */
  8020. while ( rc == SQLITE_OK )
  8021. {
  8022. int iIdx; /* Index of child node in parent */
  8023. MemPage pPage; /* Current page of the b-tree */
  8024. /* If this is a leaf page or the tree is not an int-key tree, then
  8025. ** this page contains countable entries. Increment the entry counter
  8026. ** accordingly.
  8027. */
  8028. pPage = pCur.apPage[pCur.iPage];
  8029. if ( pPage.leaf != 0 || 0 == pPage.intKey )
  8030. {
  8031. nEntry += pPage.nCell;
  8032. }
  8033. /* pPage is a leaf node. This loop navigates the cursor so that it
  8034. ** points to the first interior cell that it points to the parent of
  8035. ** the next page in the tree that has not yet been visited. The
  8036. ** pCur.aiIdx[pCur.iPage] value is set to the index of the parent cell
  8037. ** of the page, or to the number of cells in the page if the next page
  8038. ** to visit is the right-child of its parent.
  8039. **
  8040. ** If all pages in the tree have been visited, return SQLITE_OK to the
  8041. ** caller.
  8042. */
  8043. if ( pPage.leaf != 0 )
  8044. {
  8045. do
  8046. {
  8047. if ( pCur.iPage == 0 )
  8048. {
  8049. /* All pages of the b-tree have been visited. Return successfully. */
  8050. pnEntry = nEntry;
  8051. return SQLITE_OK;
  8052. }
  8053. moveToParent( pCur );
  8054. } while ( pCur.aiIdx[pCur.iPage] >= pCur.apPage[pCur.iPage].nCell );
  8055. pCur.aiIdx[pCur.iPage]++;
  8056. pPage = pCur.apPage[pCur.iPage];
  8057. }
  8058. /* Descend to the child node of the cell that the cursor currently
  8059. ** points at. This is the right-child if (iIdx==pPage.nCell).
  8060. */
  8061. iIdx = pCur.aiIdx[pCur.iPage];
  8062. if ( iIdx == pPage.nCell )
  8063. {
  8064. rc = moveToChild( pCur, sqlite3Get4byte( pPage.aData, pPage.hdrOffset + 8 ) );
  8065. }
  8066. else
  8067. {
  8068. rc = moveToChild( pCur, sqlite3Get4byte( pPage.aData, findCell( pPage, iIdx ) ) );
  8069. }
  8070. }
  8071. /* An error has occurred. Return an error code. */
  8072. return rc;
  8073. }
  8074. #endif
  8075. /*
  8076. ** Return the pager associated with a BTree. This routine is used for
  8077. ** testing and debugging only.
  8078. */
  8079. static Pager sqlite3BtreePager( Btree p )
  8080. {
  8081. return p.pBt.pPager;
  8082. }
  8083. #if !SQLITE_OMIT_INTEGRITY_CHECK
  8084. /*
  8085. ** Append a message to the error message string.
  8086. */
  8087. static void checkAppendMsg(
  8088. IntegrityCk pCheck,
  8089. string zMsg1,
  8090. string zFormat,
  8091. params object[] ap
  8092. )
  8093. {
  8094. if ( 0 == pCheck.mxErr )
  8095. return;
  8096. //va_list ap;
  8097. lock ( lock_va_list )
  8098. {
  8099. pCheck.mxErr--;
  8100. pCheck.nErr++;
  8101. va_start( ap, zFormat );
  8102. if ( pCheck.errMsg.zText.Length != 0 )
  8103. {
  8104. sqlite3StrAccumAppend( pCheck.errMsg, "\n", 1 );
  8105. }
  8106. if ( zMsg1.Length > 0 )
  8107. {
  8108. sqlite3StrAccumAppend( pCheck.errMsg, zMsg1.ToString(), -1 );
  8109. }
  8110. sqlite3VXPrintf( pCheck.errMsg, 1, zFormat, ap );
  8111. va_end( ref ap );
  8112. }
  8113. }
  8114. static void checkAppendMsg(
  8115. IntegrityCk pCheck,
  8116. StringBuilder zMsg1,
  8117. string zFormat,
  8118. params object[] ap
  8119. )
  8120. {
  8121. if ( 0 == pCheck.mxErr )
  8122. return;
  8123. //va_list ap;
  8124. lock ( lock_va_list )
  8125. {
  8126. pCheck.mxErr--;
  8127. pCheck.nErr++;
  8128. va_start( ap, zFormat );
  8129. if ( pCheck.errMsg.zText.Length != 0 )
  8130. {
  8131. sqlite3StrAccumAppend( pCheck.errMsg, "\n", 1 );
  8132. }
  8133. if ( zMsg1.Length > 0 )
  8134. {
  8135. sqlite3StrAccumAppend( pCheck.errMsg, zMsg1.ToString(), -1 );
  8136. }
  8137. sqlite3VXPrintf( pCheck.errMsg, 1, zFormat, ap );
  8138. va_end( ref ap );
  8139. }
  8140. //if( pCheck.errMsg.mallocFailed ){
  8141. // pCheck.mallocFailed = 1;
  8142. //}
  8143. }
  8144. #endif //* SQLITE_OMIT_INTEGRITY_CHECK */
  8145. #if !SQLITE_OMIT_INTEGRITY_CHECK
  8146. /*
  8147. ** Add 1 to the reference count for page iPage. If this is the second
  8148. ** reference to the page, add an error message to pCheck.zErrMsg.
  8149. ** Return 1 if there are 2 ore more references to the page and 0 if
  8150. ** if this is the first reference to the page.
  8151. **
  8152. ** Also check that the page number is in bounds.
  8153. */
  8154. static int checkRef( IntegrityCk pCheck, Pgno iPage, string zContext )
  8155. {
  8156. if ( iPage == 0 )
  8157. return 1;
  8158. if ( iPage > pCheck.nPage )
  8159. {
  8160. checkAppendMsg( pCheck, zContext, "invalid page number %d", iPage );
  8161. return 1;
  8162. }
  8163. if ( pCheck.anRef[iPage] == 1 )
  8164. {
  8165. checkAppendMsg( pCheck, zContext, "2nd reference to page %d", iPage );
  8166. return 1;
  8167. }
  8168. return ( ( pCheck.anRef[iPage]++ ) > 1 ) ? 1 : 0;
  8169. }
  8170. #if !SQLITE_OMIT_AUTOVACUUM
  8171. /*
  8172. ** Check that the entry in the pointer-map for page iChild maps to
  8173. ** page iParent, pointer type ptrType. If not, append an error message
  8174. ** to pCheck.
  8175. */
  8176. static void checkPtrmap(
  8177. IntegrityCk pCheck, /* Integrity check context */
  8178. Pgno iChild, /* Child page number */
  8179. u8 eType, /* Expected pointer map type */
  8180. Pgno iParent, /* Expected pointer map parent page number */
  8181. string zContext /* Context description (used for error msg) */
  8182. )
  8183. {
  8184. int rc;
  8185. u8 ePtrmapType = 0;
  8186. Pgno iPtrmapParent = 0;
  8187. rc = ptrmapGet( pCheck.pBt, iChild, ref ePtrmapType, ref iPtrmapParent );
  8188. if ( rc != SQLITE_OK )
  8189. {
  8190. //if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck.mallocFailed = 1;
  8191. checkAppendMsg( pCheck, zContext, "Failed to read ptrmap key=%d", iChild );
  8192. return;
  8193. }
  8194. if ( ePtrmapType != eType || iPtrmapParent != iParent )
  8195. {
  8196. checkAppendMsg( pCheck, zContext,
  8197. "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)",
  8198. iChild, eType, iParent, ePtrmapType, iPtrmapParent );
  8199. }
  8200. }
  8201. #endif
  8202. /*
  8203. ** Check the integrity of the freelist or of an overflow page list.
  8204. ** Verify that the number of pages on the list is N.
  8205. */
  8206. static void checkList(
  8207. IntegrityCk pCheck, /* Integrity checking context */
  8208. int isFreeList, /* True for a freelist. False for overflow page list */
  8209. int iPage, /* Page number for first page in the list */
  8210. int N, /* Expected number of pages in the list */
  8211. string zContext /* Context for error messages */
  8212. )
  8213. {
  8214. int i;
  8215. int expected = N;
  8216. int iFirst = iPage;
  8217. while ( N-- > 0 && pCheck.mxErr != 0 )
  8218. {
  8219. PgHdr pOvflPage = new PgHdr();
  8220. byte[] pOvflData;
  8221. if ( iPage < 1 )
  8222. {
  8223. checkAppendMsg( pCheck, zContext,
  8224. "%d of %d pages missing from overflow list starting at %d",
  8225. N + 1, expected, iFirst );
  8226. break;
  8227. }
  8228. if ( checkRef( pCheck, (u32)iPage, zContext ) != 0 )
  8229. break;
  8230. if ( sqlite3PagerGet( pCheck.pPager, (Pgno)iPage, ref pOvflPage ) != 0 )
  8231. {
  8232. checkAppendMsg( pCheck, zContext, "failed to get page %d", iPage );
  8233. break;
  8234. }
  8235. pOvflData = sqlite3PagerGetData( pOvflPage );
  8236. if ( isFreeList != 0 )
  8237. {
  8238. int n = (int)sqlite3Get4byte( pOvflData, 4 );
  8239. #if !SQLITE_OMIT_AUTOVACUUM
  8240. if ( pCheck.pBt.autoVacuum )
  8241. {
  8242. checkPtrmap( pCheck, (u32)iPage, PTRMAP_FREEPAGE, 0, zContext );
  8243. }
  8244. #endif
  8245. if ( n > (int)pCheck.pBt.usableSize / 4 - 2 )
  8246. {
  8247. checkAppendMsg( pCheck, zContext,
  8248. "freelist leaf count too big on page %d", iPage );
  8249. N--;
  8250. }
  8251. else
  8252. {
  8253. for ( i = 0; i < n; i++ )
  8254. {
  8255. Pgno iFreePage = sqlite3Get4byte( pOvflData, 8 + i * 4 );
  8256. #if !SQLITE_OMIT_AUTOVACUUM
  8257. if ( pCheck.pBt.autoVacuum )
  8258. {
  8259. checkPtrmap( pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext );
  8260. }
  8261. #endif
  8262. checkRef( pCheck, iFreePage, zContext );
  8263. }
  8264. N -= n;
  8265. }
  8266. }
  8267. #if !SQLITE_OMIT_AUTOVACUUM
  8268. else
  8269. {
  8270. /* If this database supports auto-vacuum and iPage is not the last
  8271. ** page in this overflow list, check that the pointer-map entry for
  8272. ** the following page matches iPage.
  8273. */
  8274. if ( pCheck.pBt.autoVacuum && N > 0 )
  8275. {
  8276. i = (int)sqlite3Get4byte( pOvflData );
  8277. checkPtrmap( pCheck, (u32)i, PTRMAP_OVERFLOW2, (u32)iPage, zContext );
  8278. }
  8279. }
  8280. #endif
  8281. iPage = (int)sqlite3Get4byte( pOvflData );
  8282. sqlite3PagerUnref( pOvflPage );
  8283. }
  8284. }
  8285. #endif //* SQLITE_OMIT_INTEGRITY_CHECK */
  8286. #if !SQLITE_OMIT_INTEGRITY_CHECK
  8287. /*
  8288. ** Do various sanity checks on a single page of a tree. Return
  8289. ** the tree depth. Root pages return 0. Parents of root pages
  8290. ** return 1, and so forth.
  8291. **
  8292. ** These checks are done:
  8293. **
  8294. ** 1. Make sure that cells and freeblocks do not overlap
  8295. ** but combine to completely cover the page.
  8296. ** NO 2. Make sure cell keys are in order.
  8297. ** NO 3. Make sure no key is less than or equal to zLowerBound.
  8298. ** NO 4. Make sure no key is greater than or equal to zUpperBound.
  8299. ** 5. Check the integrity of overflow pages.
  8300. ** 6. Recursively call checkTreePage on all children.
  8301. ** 7. Verify that the depth of all children is the same.
  8302. ** 8. Make sure this page is at least 33% full or else it is
  8303. ** the root of the tree.
  8304. */
  8305. static i64 refNULL = 0; //Dummy for C# ref NULL
  8306. static int checkTreePage(
  8307. IntegrityCk pCheck, /* Context for the sanity check */
  8308. int iPage, /* Page number of the page to check */
  8309. string zParentContext, /* Parent context */
  8310. ref i64 pnParentMinKey,
  8311. ref i64 pnParentMaxKey,
  8312. object _pnParentMinKey, /* C# Needed to determine if content passed*/
  8313. object _pnParentMaxKey /* C# Needed to determine if content passed*/
  8314. )
  8315. {
  8316. MemPage pPage = new MemPage();
  8317. int i, rc, depth, d2, pgno, cnt;
  8318. int hdr, cellStart;
  8319. int nCell;
  8320. u8[] data;
  8321. BtShared pBt;
  8322. int usableSize;
  8323. StringBuilder zContext = new StringBuilder( 100 );
  8324. byte[] hit = null;
  8325. i64 nMinKey = 0;
  8326. i64 nMaxKey = 0;
  8327. sqlite3_snprintf( 200, zContext, "Page %d: ", iPage );
  8328. /* Check that the page exists
  8329. */
  8330. pBt = pCheck.pBt;
  8331. usableSize = (int)pBt.usableSize;
  8332. if ( iPage == 0 )
  8333. return 0;
  8334. if ( checkRef( pCheck, (u32)iPage, zParentContext ) != 0 )
  8335. return 0;
  8336. if ( ( rc = btreeGetPage( pBt, (Pgno)iPage, ref pPage, 0 ) ) != 0 )
  8337. {
  8338. checkAppendMsg( pCheck, zContext.ToString(),
  8339. "unable to get the page. error code=%d", rc );
  8340. return 0;
  8341. }
  8342. /* Clear MemPage.isInit to make sure the corruption detection code in
  8343. ** btreeInitPage() is executed. */
  8344. pPage.isInit = 0;
  8345. if ( ( rc = btreeInitPage( pPage ) ) != 0 )
  8346. {
  8347. Debug.Assert( rc == SQLITE_CORRUPT ); /* The only possible error from InitPage */
  8348. checkAppendMsg( pCheck, zContext.ToString(),
  8349. "btreeInitPage() returns error code %d", rc );
  8350. releasePage( pPage );
  8351. return 0;
  8352. }
  8353. /* Check out all the cells.
  8354. */
  8355. depth = 0;
  8356. for ( i = 0; i < pPage.nCell && pCheck.mxErr != 0; i++ )
  8357. {
  8358. u8[] pCell;
  8359. u32 sz;
  8360. CellInfo info = new CellInfo();
  8361. /* Check payload overflow pages
  8362. */
  8363. sqlite3_snprintf( 200, zContext,
  8364. "On tree page %d cell %d: ", iPage, i );
  8365. int iCell = findCell( pPage, i ); //pCell = findCell( pPage, i );
  8366. pCell = pPage.aData;
  8367. btreeParseCellPtr( pPage, iCell, ref info ); //btreeParseCellPtr( pPage, pCell, info );
  8368. sz = info.nData;
  8369. if ( 0 == pPage.intKey )
  8370. sz += (u32)info.nKey;
  8371. /* For intKey pages, check that the keys are in order.
  8372. */
  8373. else if ( i == 0 )
  8374. nMinKey = nMaxKey = info.nKey;
  8375. else
  8376. {
  8377. if ( info.nKey <= nMaxKey )
  8378. {
  8379. checkAppendMsg( pCheck, zContext.ToString(),
  8380. "Rowid %lld out of order (previous was %lld)", info.nKey, nMaxKey );
  8381. }
  8382. nMaxKey = info.nKey;
  8383. }
  8384. Debug.Assert( sz == info.nPayload );
  8385. if ( ( sz > info.nLocal )
  8386. //&& (pCell[info.iOverflow]<=&pPage.aData[pBt.usableSize])
  8387. )
  8388. {
  8389. int nPage = (int)( sz - info.nLocal + usableSize - 5 ) / ( usableSize - 4 );
  8390. Pgno pgnoOvfl = sqlite3Get4byte( pCell, iCell, info.iOverflow );
  8391. #if !SQLITE_OMIT_AUTOVACUUM
  8392. if ( pBt.autoVacuum )
  8393. {
  8394. checkPtrmap( pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, (u32)iPage, zContext.ToString() );
  8395. }
  8396. #endif
  8397. checkList( pCheck, 0, (int)pgnoOvfl, nPage, zContext.ToString() );
  8398. }
  8399. /* Check sanity of left child page.
  8400. */
  8401. if ( 0 == pPage.leaf )
  8402. {
  8403. pgno = (int)sqlite3Get4byte( pCell, iCell ); //sqlite3Get4byte( pCell );
  8404. #if !SQLITE_OMIT_AUTOVACUUM
  8405. if ( pBt.autoVacuum )
  8406. {
  8407. checkPtrmap( pCheck, (u32)pgno, PTRMAP_BTREE, (u32)iPage, zContext.ToString() );
  8408. }
  8409. #endif
  8410. if ( i == 0 )
  8411. d2 = checkTreePage( pCheck, pgno, zContext.ToString(), ref nMinKey, ref refNULL, pCheck, null );
  8412. else
  8413. d2 = checkTreePage( pCheck, pgno, zContext.ToString(), ref nMinKey, ref nMaxKey, pCheck, pCheck );
  8414. if ( i > 0 && d2 != depth )
  8415. {
  8416. checkAppendMsg( pCheck, zContext, "Child page depth differs" );
  8417. }
  8418. depth = d2;
  8419. }
  8420. }
  8421. if ( 0 == pPage.leaf )
  8422. {
  8423. pgno = (int)sqlite3Get4byte( pPage.aData, pPage.hdrOffset + 8 );
  8424. sqlite3_snprintf( 200, zContext,
  8425. "On page %d at right child: ", iPage );
  8426. #if !SQLITE_OMIT_AUTOVACUUM
  8427. if ( pBt.autoVacuum )
  8428. {
  8429. checkPtrmap( pCheck, (u32)pgno, PTRMAP_BTREE, (u32)iPage, zContext.ToString() );
  8430. }
  8431. #endif
  8432. // checkTreePage(pCheck, pgno, zContext, NULL, !pPage->nCell ? NULL : &nMaxKey);
  8433. if ( 0 == pPage.nCell )
  8434. checkTreePage( pCheck, pgno, zContext.ToString(), ref refNULL, ref refNULL, null, null );
  8435. else
  8436. checkTreePage( pCheck, pgno, zContext.ToString(), ref refNULL, ref nMaxKey, null, pCheck );
  8437. }
  8438. /* For intKey leaf pages, check that the min/max keys are in order
  8439. ** with any left/parent/right pages.
  8440. */
  8441. if ( pPage.leaf != 0 && pPage.intKey != 0 )
  8442. {
  8443. /* if we are a left child page */
  8444. if ( _pnParentMinKey != null )
  8445. {
  8446. /* if we are the left most child page */
  8447. if ( _pnParentMaxKey == null )
  8448. {
  8449. if ( nMaxKey > pnParentMinKey )
  8450. {
  8451. checkAppendMsg( pCheck, zContext,
  8452. "Rowid %lld out of order (max larger than parent min of %lld)",
  8453. nMaxKey, pnParentMinKey );
  8454. }
  8455. }
  8456. else
  8457. {
  8458. if ( nMinKey <= pnParentMinKey )
  8459. {
  8460. checkAppendMsg( pCheck, zContext,
  8461. "Rowid %lld out of order (min less than parent min of %lld)",
  8462. nMinKey, pnParentMinKey );
  8463. }
  8464. if ( nMaxKey > pnParentMaxKey )
  8465. {
  8466. checkAppendMsg( pCheck, zContext,
  8467. "Rowid %lld out of order (max larger than parent max of %lld)",
  8468. nMaxKey, pnParentMaxKey );
  8469. }
  8470. pnParentMinKey = nMaxKey;
  8471. }
  8472. /* else if we're a right child page */
  8473. }
  8474. else if ( _pnParentMaxKey != null )
  8475. {
  8476. if ( nMinKey <= pnParentMaxKey )
  8477. {
  8478. checkAppendMsg( pCheck, zContext,
  8479. "Rowid %lld out of order (min less than parent max of %lld)",
  8480. nMinKey, pnParentMaxKey );
  8481. }
  8482. }
  8483. }
  8484. /* Check for complete coverage of the page
  8485. */
  8486. data = pPage.aData;
  8487. hdr = pPage.hdrOffset;
  8488. hit = sqlite3Malloc( pBt.pageSize );
  8489. //if( hit==null ){
  8490. // pCheck.mallocFailed = 1;
  8491. //}else
  8492. {
  8493. int contentOffset = get2byteNotZero( data, hdr + 5 );
  8494. Debug.Assert( contentOffset <= usableSize ); /* Enforced by btreeInitPage() */
  8495. Array.Clear( hit, contentOffset, usableSize - contentOffset );//memset(hit+contentOffset, 0, usableSize-contentOffset);
  8496. for ( int iLoop = contentOffset - 1; iLoop >= 0; iLoop-- )
  8497. hit[iLoop] = 1;//memset(hit, 1, contentOffset);
  8498. nCell = get2byte( data, hdr + 3 );
  8499. cellStart = hdr + 12 - 4 * pPage.leaf;
  8500. for ( i = 0; i < nCell; i++ )
  8501. {
  8502. int pc = get2byte( data, cellStart + i * 2 );
  8503. u32 size = 65536;
  8504. int j;
  8505. if ( pc <= usableSize - 4 )
  8506. {
  8507. size = cellSizePtr( pPage, data, pc );
  8508. }
  8509. if ( (int)( pc + size - 1 ) >= usableSize )
  8510. {
  8511. checkAppendMsg( pCheck, "",
  8512. "Corruption detected in cell %d on page %d", i, iPage );
  8513. }
  8514. else
  8515. {
  8516. for ( j = (int)( pc + size - 1 ); j >= pc; j-- )
  8517. hit[j]++;
  8518. }
  8519. }
  8520. i = get2byte( data, hdr + 1 );
  8521. while ( i > 0 )
  8522. {
  8523. int size, j;
  8524. Debug.Assert( i <= usableSize - 4 ); /* Enforced by btreeInitPage() */
  8525. size = get2byte( data, i + 2 );
  8526. Debug.Assert( i + size <= usableSize ); /* Enforced by btreeInitPage() */
  8527. for ( j = i + size - 1; j >= i; j-- )
  8528. hit[j]++;
  8529. j = get2byte( data, i );
  8530. Debug.Assert( j == 0 || j > i + size ); /* Enforced by btreeInitPage() */
  8531. Debug.Assert( j <= usableSize - 4 ); /* Enforced by btreeInitPage() */
  8532. i = j;
  8533. }
  8534. for ( i = cnt = 0; i < usableSize; i++ )
  8535. {
  8536. if ( hit[i] == 0 )
  8537. {
  8538. cnt++;
  8539. }
  8540. else if ( hit[i] > 1 )
  8541. {
  8542. checkAppendMsg( pCheck, "",
  8543. "Multiple uses for byte %d of page %d", i, iPage );
  8544. break;
  8545. }
  8546. }
  8547. if ( cnt != data[hdr + 7] )
  8548. {
  8549. checkAppendMsg( pCheck, "",
  8550. "Fragmentation of %d bytes reported as %d on page %d",
  8551. cnt, data[hdr + 7], iPage );
  8552. }
  8553. }
  8554. sqlite3PageFree( ref hit );
  8555. releasePage( pPage );
  8556. return depth + 1;
  8557. }
  8558. #endif //* SQLITE_OMIT_INTEGRITY_CHECK */
  8559. #if !SQLITE_OMIT_INTEGRITY_CHECK
  8560. /*
  8561. ** This routine does a complete check of the given BTree file. aRoot[] is
  8562. ** an array of pages numbers were each page number is the root page of
  8563. ** a table. nRoot is the number of entries in aRoot.
  8564. **
  8565. ** A read-only or read-write transaction must be opened before calling
  8566. ** this function.
  8567. **
  8568. ** Write the number of error seen in pnErr. Except for some memory
  8569. ** allocation errors, an error message held in memory obtained from
  8570. ** malloc is returned if pnErr is non-zero. If pnErr==null then NULL is
  8571. ** returned. If a memory allocation error occurs, NULL is returned.
  8572. */
  8573. static string sqlite3BtreeIntegrityCheck(
  8574. Btree p, /* The btree to be checked */
  8575. int[] aRoot, /* An array of root pages numbers for individual trees */
  8576. int nRoot, /* Number of entries in aRoot[] */
  8577. int mxErr, /* Stop reporting errors after this many */
  8578. ref int pnErr /* Write number of errors seen to this variable */
  8579. )
  8580. {
  8581. Pgno i;
  8582. int nRef;
  8583. IntegrityCk sCheck = new IntegrityCk();
  8584. BtShared pBt = p.pBt;
  8585. StringBuilder zErr = new StringBuilder( 100 );//char zErr[100];
  8586. sqlite3BtreeEnter( p );
  8587. Debug.Assert( p.inTrans > TRANS_NONE && pBt.inTransaction > TRANS_NONE );
  8588. nRef = sqlite3PagerRefcount( pBt.pPager );
  8589. sCheck.pBt = pBt;
  8590. sCheck.pPager = pBt.pPager;
  8591. sCheck.nPage = btreePagecount( sCheck.pBt );
  8592. sCheck.mxErr = mxErr;
  8593. sCheck.nErr = 0;
  8594. //sCheck.mallocFailed = 0;
  8595. pnErr = 0;
  8596. if ( sCheck.nPage == 0 )
  8597. {
  8598. sqlite3BtreeLeave( p );
  8599. return "";
  8600. }
  8601. sCheck.anRef = sqlite3Malloc( sCheck.anRef, (int)sCheck.nPage + 1 );
  8602. //if( !sCheck.anRef ){
  8603. // pnErr = 1;
  8604. // sqlite3BtreeLeave(p);
  8605. // return 0;
  8606. //}
  8607. // for (i = 0; i <= sCheck.nPage; i++) { sCheck.anRef[i] = 0; }
  8608. i = PENDING_BYTE_PAGE( pBt );
  8609. if ( i <= sCheck.nPage )
  8610. {
  8611. sCheck.anRef[i] = 1;
  8612. }
  8613. sqlite3StrAccumInit( sCheck.errMsg, null, 1000, 20000 );
  8614. //sCheck.errMsg.useMalloc = 2;
  8615. /* Check the integrity of the freelist
  8616. */
  8617. checkList( sCheck, 1, (int)sqlite3Get4byte( pBt.pPage1.aData, 32 ),
  8618. (int)sqlite3Get4byte( pBt.pPage1.aData, 36 ), "Main freelist: " );
  8619. /* Check all the tables.
  8620. */
  8621. for ( i = 0; (int)i < nRoot && sCheck.mxErr != 0; i++ )
  8622. {
  8623. if ( aRoot[i] == 0 )
  8624. continue;
  8625. #if !SQLITE_OMIT_AUTOVACUUM
  8626. if ( pBt.autoVacuum && aRoot[i] > 1 )
  8627. {
  8628. checkPtrmap( sCheck, (u32)aRoot[i], PTRMAP_ROOTPAGE, 0, "" );
  8629. }
  8630. #endif
  8631. checkTreePage( sCheck, aRoot[i], "List of tree roots: ", ref refNULL, ref refNULL, null, null );
  8632. }
  8633. /* Make sure every page in the file is referenced
  8634. */
  8635. for ( i = 1; i <= sCheck.nPage && sCheck.mxErr != 0; i++ )
  8636. {
  8637. #if SQLITE_OMIT_AUTOVACUUM
  8638. if( sCheck.anRef[i]==null ){
  8639. checkAppendMsg(sCheck, 0, "Page %d is never used", i);
  8640. }
  8641. #else
  8642. /* If the database supports auto-vacuum, make sure no tables contain
  8643. ** references to pointer-map pages.
  8644. */
  8645. if ( sCheck.anRef[i] == 0 &&
  8646. ( PTRMAP_PAGENO( pBt, i ) != i || !pBt.autoVacuum ) )
  8647. {
  8648. checkAppendMsg( sCheck, "", "Page %d is never used", i );
  8649. }
  8650. if ( sCheck.anRef[i] != 0 &&
  8651. ( PTRMAP_PAGENO( pBt, i ) == i && pBt.autoVacuum ) )
  8652. {
  8653. checkAppendMsg( sCheck, "", "Pointer map page %d is referenced", i );
  8654. }
  8655. #endif
  8656. }
  8657. /* Make sure this analysis did not leave any unref() pages.
  8658. ** This is an internal consistency check; an integrity check
  8659. ** of the integrity check.
  8660. */
  8661. if ( NEVER( nRef != sqlite3PagerRefcount( pBt.pPager ) ) )
  8662. {
  8663. checkAppendMsg( sCheck, "",
  8664. "Outstanding page count goes from %d to %d during this analysis",
  8665. nRef, sqlite3PagerRefcount( pBt.pPager )
  8666. );
  8667. }
  8668. /* Clean up and report errors.
  8669. */
  8670. sqlite3BtreeLeave( p );
  8671. sCheck.anRef = null;// sqlite3_free( ref sCheck.anRef );
  8672. //if( sCheck.mallocFailed ){
  8673. // sqlite3StrAccumReset(sCheck.errMsg);
  8674. // pnErr = sCheck.nErr+1;
  8675. // return 0;
  8676. //}
  8677. pnErr = sCheck.nErr;
  8678. if ( sCheck.nErr == 0 )
  8679. sqlite3StrAccumReset( sCheck.errMsg );
  8680. return sqlite3StrAccumFinish( sCheck.errMsg );
  8681. }
  8682. #endif //* SQLITE_OMIT_INTEGRITY_CHECK */
  8683. /*
  8684. ** Return the full pathname of the underlying database file.
  8685. **
  8686. ** The pager filename is invariant as long as the pager is
  8687. ** open so it is safe to access without the BtShared mutex.
  8688. */
  8689. static string sqlite3BtreeGetFilename( Btree p )
  8690. {
  8691. Debug.Assert( p.pBt.pPager != null );
  8692. return sqlite3PagerFilename( p.pBt.pPager );
  8693. }
  8694. /*
  8695. ** Return the pathname of the journal file for this database. The return
  8696. ** value of this routine is the same regardless of whether the journal file
  8697. ** has been created or not.
  8698. **
  8699. ** The pager journal filename is invariant as long as the pager is
  8700. ** open so it is safe to access without the BtShared mutex.
  8701. */
  8702. static string sqlite3BtreeGetJournalname( Btree p )
  8703. {
  8704. Debug.Assert( p.pBt.pPager != null );
  8705. return sqlite3PagerJournalname( p.pBt.pPager );
  8706. }
  8707. /*
  8708. ** Return non-zero if a transaction is active.
  8709. */
  8710. static bool sqlite3BtreeIsInTrans( Btree p )
  8711. {
  8712. Debug.Assert( p == null || sqlite3_mutex_held( p.db.mutex ) );
  8713. return ( p != null && ( p.inTrans == TRANS_WRITE ) );
  8714. }
  8715. #if !SQLITE_OMIT_WAL
  8716. /*
  8717. ** Run a checkpoint on the Btree passed as the first argument.
  8718. **
  8719. ** Return SQLITE_LOCKED if this or any other connection has an open
  8720. ** transaction on the shared-cache the argument Btree is connected to.
  8721. */
  8722. static int sqlite3BtreeCheckpoint(Btree p){
  8723. int rc = SQLITE_OK;
  8724. if( p != null){
  8725. BtShared pBt = p.pBt;
  8726. sqlite3BtreeEnter(p);
  8727. if( pBt.inTransaction!=TRANS_NONE ){
  8728. rc = SQLITE_LOCKED;
  8729. }else{
  8730. rc = sqlite3PagerCheckpoint(pBt.pPager);
  8731. }
  8732. sqlite3BtreeLeave(p);
  8733. }
  8734. return rc;
  8735. }
  8736. #endif
  8737. /*
  8738. ** Return non-zero if a read (or write) transaction is active.
  8739. */
  8740. static bool sqlite3BtreeIsInReadTrans( Btree p )
  8741. {
  8742. Debug.Assert( p != null );
  8743. Debug.Assert( sqlite3_mutex_held( p.db.mutex ) );
  8744. return p.inTrans != TRANS_NONE;
  8745. }
  8746. static bool sqlite3BtreeIsInBackup( Btree p )
  8747. {
  8748. Debug.Assert( p != null );
  8749. Debug.Assert( sqlite3_mutex_held( p.db.mutex ) );
  8750. return p.nBackup != 0;
  8751. }
  8752. /*
  8753. ** This function returns a pointer to a blob of memory associated with
  8754. ** a single shared-btree. The memory is used by client code for its own
  8755. ** purposes (for example, to store a high-level schema associated with
  8756. ** the shared-btree). The btree layer manages reference counting issues.
  8757. **
  8758. ** The first time this is called on a shared-btree, nBytes bytes of memory
  8759. ** are allocated, zeroed, and returned to the caller. For each subsequent
  8760. ** call the nBytes parameter is ignored and a pointer to the same blob
  8761. ** of memory returned.
  8762. **
  8763. ** If the nBytes parameter is 0 and the blob of memory has not yet been
  8764. ** allocated, a null pointer is returned. If the blob has already been
  8765. ** allocated, it is returned as normal.
  8766. **
  8767. ** Just before the shared-btree is closed, the function passed as the
  8768. ** xFree argument when the memory allocation was made is invoked on the
  8769. ** blob of allocated memory. This function should not call sqlite3_free(ref )
  8770. ** on the memory, the btree layer does that.
  8771. */
  8772. static Schema sqlite3BtreeSchema( Btree p, int nBytes, dxFreeSchema xFree )
  8773. {
  8774. BtShared pBt = p.pBt;
  8775. sqlite3BtreeEnter( p );
  8776. if ( null == pBt.pSchema && nBytes != 0 )
  8777. {
  8778. pBt.pSchema = new Schema();//sqlite3DbMallocZero(0, nBytes);
  8779. pBt.xFreeSchema = xFree;
  8780. }
  8781. sqlite3BtreeLeave( p );
  8782. return pBt.pSchema;
  8783. }
  8784. /*
  8785. ** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared
  8786. ** btree as the argument handle holds an exclusive lock on the
  8787. ** sqlite_master table. Otherwise SQLITE_OK.
  8788. */
  8789. static int sqlite3BtreeSchemaLocked( Btree p )
  8790. {
  8791. int rc;
  8792. Debug.Assert( sqlite3_mutex_held( p.db.mutex ) );
  8793. sqlite3BtreeEnter( p );
  8794. rc = querySharedCacheTableLock( p, MASTER_ROOT, READ_LOCK );
  8795. Debug.Assert( rc == SQLITE_OK || rc == SQLITE_LOCKED_SHAREDCACHE );
  8796. sqlite3BtreeLeave( p );
  8797. return rc;
  8798. }
  8799. #if !SQLITE_OMIT_SHARED_CACHE
  8800. /*
  8801. ** Obtain a lock on the table whose root page is iTab. The
  8802. ** lock is a write lock if isWritelock is true or a read lock
  8803. ** if it is false.
  8804. */
  8805. int sqlite3BtreeLockTable(Btree p, int iTab, u8 isWriteLock){
  8806. int rc = SQLITE_OK;
  8807. Debug.Assert( p.inTrans!=TRANS_NONE );
  8808. if( p.sharable ){
  8809. u8 lockType = READ_LOCK + isWriteLock;
  8810. Debug.Assert( READ_LOCK+1==WRITE_LOCK );
  8811. Debug.Assert( isWriteLock==null || isWriteLock==1 );
  8812. sqlite3BtreeEnter(p);
  8813. rc = querySharedCacheTableLock(p, iTab, lockType);
  8814. if( rc==SQLITE_OK ){
  8815. rc = setSharedCacheTableLock(p, iTab, lockType);
  8816. }
  8817. sqlite3BtreeLeave(p);
  8818. }
  8819. return rc;
  8820. }
  8821. #endif
  8822. #if !SQLITE_OMIT_INCRBLOB
  8823. /*
  8824. ** Argument pCsr must be a cursor opened for writing on an
  8825. ** INTKEY table currently pointing at a valid table entry.
  8826. ** This function modifies the data stored as part of that entry.
  8827. **
  8828. ** Only the data content may only be modified, it is not possible to
  8829. ** change the length of the data stored. If this function is called with
  8830. ** parameters that attempt to write past the end of the existing data,
  8831. ** no modifications are made and SQLITE_CORRUPT is returned.
  8832. */
  8833. int sqlite3BtreePutData(BtCursor pCsr, u32 offset, u32 amt, void *z){
  8834. int rc;
  8835. Debug.Assert( cursorHoldsMutex(pCsr) );
  8836. Debug.Assert( sqlite3_mutex_held(pCsr.pBtree.db.mutex) );
  8837. Debug.Assert( pCsr.isIncrblobHandle );
  8838. rc = restoreCursorPosition(pCsr);
  8839. if( rc!=SQLITE_OK ){
  8840. return rc;
  8841. }
  8842. Debug.Assert( pCsr.eState!=CURSOR_REQUIRESEEK );
  8843. if( pCsr.eState!=CURSOR_VALID ){
  8844. return SQLITE_ABORT;
  8845. }
  8846. /* Check some assumptions:
  8847. ** (a) the cursor is open for writing,
  8848. ** (b) there is a read/write transaction open,
  8849. ** (c) the connection holds a write-lock on the table (if required),
  8850. ** (d) there are no conflicting read-locks, and
  8851. ** (e) the cursor points at a valid row of an intKey table.
  8852. */
  8853. if( !pCsr.wrFlag ){
  8854. return SQLITE_READONLY;
  8855. }
  8856. Debug.Assert( !pCsr.pBt.readOnly && pCsr.pBt.inTransaction==TRANS_WRITE );
  8857. Debug.Assert( hasSharedCacheTableLock(pCsr.pBtree, pCsr.pgnoRoot, 0, 2) );
  8858. Debug.Assert( !hasReadConflicts(pCsr.pBtree, pCsr.pgnoRoot) );
  8859. Debug.Assert( pCsr.apPage[pCsr.iPage].intKey );
  8860. return accessPayload(pCsr, offset, amt, (byte[] *)z, 1);
  8861. }
  8862. /*
  8863. ** Set a flag on this cursor to cache the locations of pages from the
  8864. ** overflow list for the current row. This is used by cursors opened
  8865. ** for incremental blob IO only.
  8866. **
  8867. ** This function sets a flag only. The actual page location cache
  8868. ** (stored in BtCursor.aOverflow[]) is allocated and used by function
  8869. ** accessPayload() (the worker function for sqlite3BtreeData() and
  8870. ** sqlite3BtreePutData()).
  8871. */
  8872. static void sqlite3BtreeCacheOverflow(BtCursor pCur){
  8873. Debug.Assert( cursorHoldsMutex(pCur) );
  8874. Debug.Assert( sqlite3_mutex_held(pCur.pBtree.db.mutex) );
  8875. invalidateOverflowCache(pCur)
  8876. pCur.isIncrblobHandle = 1;
  8877. }
  8878. #endif
  8879. /*
  8880. ** Set both the "read version" (single byte at byte offset 18) and
  8881. ** "write version" (single byte at byte offset 19) fields in the database
  8882. ** header to iVersion.
  8883. */
  8884. static int sqlite3BtreeSetVersion( Btree pBtree, int iVersion )
  8885. {
  8886. BtShared pBt = pBtree.pBt;
  8887. int rc; /* Return code */
  8888. Debug.Assert( pBtree.inTrans == TRANS_NONE );
  8889. Debug.Assert( iVersion == 1 || iVersion == 2 );
  8890. /* If setting the version fields to 1, do not automatically open the
  8891. ** WAL connection, even if the version fields are currently set to 2.
  8892. */
  8893. pBt.doNotUseWAL = iVersion == 1;
  8894. rc = sqlite3BtreeBeginTrans( pBtree, 0 );
  8895. if ( rc == SQLITE_OK )
  8896. {
  8897. u8[] aData = pBt.pPage1.aData;
  8898. if ( aData[18] != (u8)iVersion || aData[19] != (u8)iVersion )
  8899. {
  8900. rc = sqlite3BtreeBeginTrans( pBtree, 2 );
  8901. if ( rc == SQLITE_OK )
  8902. {
  8903. rc = sqlite3PagerWrite( pBt.pPage1.pDbPage );
  8904. if ( rc == SQLITE_OK )
  8905. {
  8906. aData[18] = (u8)iVersion;
  8907. aData[19] = (u8)iVersion;
  8908. }
  8909. }
  8910. }
  8911. }
  8912. pBt.doNotUseWAL = false;
  8913. return rc;
  8914. }
  8915. }
  8916. }