Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(487)

Side by Side Diff: third_party/sqlite/src/src/btree.c

Issue 2732553002: [sql] SQLite patch to implement "smart" auto-vacuum. (Closed)
Patch Set: sigh, keep clang happy on windows Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/sqlite/src/src/btree.h ('k') | third_party/sqlite/src/src/btreeInt.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 ** 2004 April 6 2 ** 2004 April 6
3 ** 3 **
4 ** The author disclaims copyright to this source code. In place of 4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing: 5 ** a legal notice, here is a blessing:
6 ** 6 **
7 ** May you do good and not evil. 7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others. 8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give. 9 ** May you share freely, never taking more than you give.
10 ** 10 **
(...skipping 2794 matching lines...) Expand 10 before | Expand all | Expand 10 after
2805 rc = ( 2805 rc = (
2806 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE: 2806 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
2807 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL: 2807 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
2808 BTREE_AUTOVACUUM_INCR 2808 BTREE_AUTOVACUUM_INCR
2809 ); 2809 );
2810 sqlite3BtreeLeave(p); 2810 sqlite3BtreeLeave(p);
2811 return rc; 2811 return rc;
2812 #endif 2812 #endif
2813 } 2813 }
2814 2814
2815 /*
2816 ** Change the 'auto-vacuum-slack-pages' property of the database. If auto vacuum
2817 ** is enabled, this is the number of chunks of slack to allow before
2818 ** automatically running an incremental vacuum.
2819 */
2820 int sqlite3BtreeSetAutoVacuumSlackPages(Btree *p, int autoVacuumSlack){
2821 #ifdef SQLITE_OMIT_AUTOVACUUM
2822 return SQLITE_READONLY;
2823 #else
2824 BtShared *pBt = p->pBt;
2825 int rc = SQLITE_OK;
2826 u8 avs = (u8)autoVacuumSlack;
2827 if( autoVacuumSlack>avs ){
2828 avs = 0xFF;
2829 }
2830
2831 sqlite3BtreeEnter(p);
2832 pBt->autoVacuumSlack = avs;
2833 sqlite3BtreeLeave(p);
2834 return rc;
2835 #endif
2836 }
2837
2838 /*
2839 ** Return the value of the 'auto-vacuum-slack-pages' property.
2840 */
2841 int sqlite3BtreeGetAutoVacuumSlackPages(Btree *p){
2842 #ifdef SQLITE_OMIT_AUTOVACUUM
2843 return 0;
2844 #else
2845 int rc = 0;
2846 sqlite3BtreeEnter(p);
2847 if( p->pBt->autoVacuum!=0 ){
2848 rc = p->pBt->autoVacuumSlack;
2849 }
2850 sqlite3BtreeLeave(p);
2851 return rc;
2852 #endif
2853 }
2854
2815 2855
2816 /* 2856 /*
2817 ** Get a reference to pPage1 of the database file. This will 2857 ** Get a reference to pPage1 of the database file. This will
2818 ** also acquire a readlock on that file. 2858 ** also acquire a readlock on that file.
2819 ** 2859 **
2820 ** SQLITE_OK is returned on success. If the file is not a 2860 ** SQLITE_OK is returned on success. If the file is not a
2821 ** well-formed database file, then SQLITE_CORRUPT is returned. 2861 ** well-formed database file, then SQLITE_CORRUPT is returned.
2822 ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM 2862 ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM
2823 ** is returned if we run out of memory. 2863 ** is returned if we run out of memory.
2824 */ 2864 */
(...skipping 821 matching lines...) Expand 10 before | Expand all | Expand 10 after
3646 ** This routine is called prior to sqlite3PagerCommit when a transaction 3686 ** This routine is called prior to sqlite3PagerCommit when a transaction
3647 ** is committed for an auto-vacuum database. 3687 ** is committed for an auto-vacuum database.
3648 ** 3688 **
3649 ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages 3689 ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages
3650 ** the database file should be truncated to during the commit process. 3690 ** the database file should be truncated to during the commit process.
3651 ** i.e. the database has been reorganized so that only the first *pnTrunc 3691 ** i.e. the database has been reorganized so that only the first *pnTrunc
3652 ** pages are in use. 3692 ** pages are in use.
3653 */ 3693 */
3654 static int autoVacuumCommit(BtShared *pBt){ 3694 static int autoVacuumCommit(BtShared *pBt){
3655 int rc = SQLITE_OK; 3695 int rc = SQLITE_OK;
3696 int bShouldVacuum = pBt->autoVacuum && !pBt->incrVacuum;
3656 Pager *pPager = pBt->pPager; 3697 Pager *pPager = pBt->pPager;
3657 VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager); ) 3698 VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager); )
3658 3699
3659 assert( sqlite3_mutex_held(pBt->mutex) ); 3700 assert( sqlite3_mutex_held(pBt->mutex) );
3660 invalidateAllOverflowCache(pBt); 3701 invalidateAllOverflowCache(pBt);
3661 assert(pBt->autoVacuum); 3702 assert(pBt->autoVacuum);
3662 if( !pBt->incrVacuum ){ 3703 if( bShouldVacuum && pBt->autoVacuumSlack ){
3704 Pgno nOrig; /* Database size before freeing */
3705 Pgno nFree; /* Number of pages on the freelist initially */
3706
3707 nOrig = btreePagecount(pBt);
3708 nFree = get4byte(&pBt->pPage1->aData[36]);
3709 bShouldVacuum =
3710 (nOrig-nFree-1)/pBt->autoVacuumSlack < (nOrig-1)/pBt->autoVacuumSlack;
3711 /* TODO: When integrating this test with the following code, contrive to
3712 ** trim to the integral chunk boundary, rather than trimming the entire free
3713 ** list.
3714 */
3715 }
3716 if( bShouldVacuum ){
3663 Pgno nFin; /* Number of pages in database after autovacuuming */ 3717 Pgno nFin; /* Number of pages in database after autovacuuming */
3664 Pgno nFree; /* Number of pages on the freelist initially */ 3718 Pgno nFree; /* Number of pages on the freelist initially */
3665 Pgno iFree; /* The next page to be freed */ 3719 Pgno iFree; /* The next page to be freed */
3666 Pgno nOrig; /* Database size before freeing */ 3720 Pgno nOrig; /* Database size before freeing */
3667 3721
3668 nOrig = btreePagecount(pBt); 3722 nOrig = btreePagecount(pBt);
3669 if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){ 3723 if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){
3670 /* It is not possible to create a database for which the final page 3724 /* It is not possible to create a database for which the final page
3671 ** is either a pointer-map page or the pending-byte page. If one 3725 ** is either a pointer-map page or the pending-byte page. If one
3672 ** is encountered, this indicates corruption. 3726 ** is encountered, this indicates corruption.
(...skipping 6085 matching lines...) Expand 10 before | Expand all | Expand 10 after
9758 /* 9812 /*
9759 ** Return the number of connections to the BtShared object accessed by 9813 ** Return the number of connections to the BtShared object accessed by
9760 ** the Btree handle passed as the only argument. For private caches 9814 ** the Btree handle passed as the only argument. For private caches
9761 ** this is always 1. For shared caches it may be 1 or greater. 9815 ** this is always 1. For shared caches it may be 1 or greater.
9762 */ 9816 */
9763 int sqlite3BtreeConnectionCount(Btree *p){ 9817 int sqlite3BtreeConnectionCount(Btree *p){
9764 testcase( p->sharable ); 9818 testcase( p->sharable );
9765 return p->pBt->nRef; 9819 return p->pBt->nRef;
9766 } 9820 }
9767 #endif 9821 #endif
OLDNEW
« no previous file with comments | « third_party/sqlite/src/src/btree.h ('k') | third_party/sqlite/src/src/btreeInt.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698