Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Fix some bugs surrounding block redirects. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | block-redirects |
Files: | files | file ages | folders |
SHA1: |
73976dbcbe892e2a76b9c26b9cbbfc1b |
User & Date: | dan 2013-01-21 10:02:17.783 |
Context
2013-01-21
| ||
14:23 | Fix LSM_INFO_PAGE_DUMP so that it works with redirected blocks. check-in: deb0ccacd4 user: dan tags: block-redirects | |
10:02 | Fix some bugs surrounding block redirects. check-in: 73976dbcbe user: dan tags: block-redirects | |
2013-01-19
| ||
20:07 | Change the lsm file-format to allow a small number of blocks belonging to the oldest segment in the database to be moved without modifying their content. This makes it easier to compact a database file to the minimum required size. check-in: 09251cee6a user: dan tags: block-redirects | |
Changes
Changes to src/lsmInt.h.
︙ | ︙ | |||
837 838 839 840 841 842 843 | void lsmSnapshotSetCkptid(Snapshot *, i64); Level *lsmDbSnapshotLevel(Snapshot *); void lsmDbSnapshotSetLevel(Snapshot *, Level *); void lsmDbRecoveryComplete(lsm_db *, int); | | | 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 | void lsmSnapshotSetCkptid(Snapshot *, i64); Level *lsmDbSnapshotLevel(Snapshot *); void lsmDbSnapshotSetLevel(Snapshot *, Level *); void lsmDbRecoveryComplete(lsm_db *, int); int lsmBlockAllocate(lsm_db *, int, int *); int lsmBlockFree(lsm_db *, int); int lsmBlockRefree(lsm_db *, int); void lsmFreelistDeltaBegin(lsm_db *); void lsmFreelistDeltaEnd(lsm_db *); int lsmFreelistDelta(lsm_db *pDb); |
︙ | ︙ |
Changes to src/lsm_ckpt.c.
︙ | ︙ | |||
425 426 427 428 429 430 431 | for(pLevel=lsmDbSnapshotLevel(pSnap); iLevel<nLevel; pLevel=pLevel->pNext){ ckptExportLevel(pLevel, &ckpt, &iOut, &rc); iLevel++; } /* Write the block-redirect list */ ckptSetValue(&ckpt, iOut++, pSnap->redirect.n, &rc); | | | 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 | for(pLevel=lsmDbSnapshotLevel(pSnap); iLevel<nLevel; pLevel=pLevel->pNext){ ckptExportLevel(pLevel, &ckpt, &iOut, &rc); iLevel++; } /* Write the block-redirect list */ ckptSetValue(&ckpt, iOut++, pSnap->redirect.n, &rc); for(i=0; i<pSnap->redirect.n; i++){ ckptSetValue(&ckpt, iOut++, pSnap->redirect.a[i].iFrom, &rc); ckptSetValue(&ckpt, iOut++, pSnap->redirect.a[i].iTo, &rc); } /* Write the freelist */ assert( pSnap->freelist.nEntry<=pDb->nMaxFreelist ); if( rc==LSM_OK ){ |
︙ | ︙ | |||
954 955 956 957 958 959 960 | u32 *a = &aCkpt[CKPT_HDR_SIZE + CKPT_LOGPTR_SIZE + i*2]; pNew->aiAppend[i] = ckptRead64(a); } /* Read the block-redirect list */ pNew->redirect.n = aCkpt[iIn++]; if( pNew->redirect.n ){ | | | | 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 | u32 *a = &aCkpt[CKPT_HDR_SIZE + CKPT_LOGPTR_SIZE + i*2]; pNew->aiAppend[i] = ckptRead64(a); } /* Read the block-redirect list */ pNew->redirect.n = aCkpt[iIn++]; if( pNew->redirect.n ){ pNew->redirect.a = lsmMallocZeroRc(pDb->pEnv, (sizeof(struct RedirectEntry) * LSM_MAX_BLOCK_REDIRECTS), &rc ); if( rc==LSM_OK ){ for(i=0; i<pNew->redirect.n; i++){ pNew->redirect.a[i].iFrom = aCkpt[iIn++]; pNew->redirect.a[i].iTo = aCkpt[iIn++]; } } |
︙ | ︙ |
Changes to src/lsm_file.c.
︙ | ︙ | |||
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 | fsGrowMapping(pFS, nMin, &rc); if( rc!=LSM_OK ) return rc; } return lsmEnvSync(pFS->pEnv, pFS->fdDb); } static int fsPageGet(FileSystem *, Redirect *, Pgno, int, Page **, int *); /* ** Parameter iBlock is a database file block. This function reads the value ** stored in the blocks "next block" pointer and stores it in *piNext. ** LSM_OK is returned if everything is successful, or an LSM error code ** otherwise. */ static int fsBlockNext( FileSystem *pFS, /* File-system object handle */ int iBlock, /* Read field from this block */ int *piNext /* OUT: Next block in linked list */ ){ int rc; assert( pFS->bUseMmap==0 || pFS->pCompress==0 ); if( pFS->pCompress ){ i64 iOff; /* File offset to read data from */ u8 aNext[4]; /* 4-byte pointer read from db file */ | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | > > > > | 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 | fsGrowMapping(pFS, nMin, &rc); if( rc!=LSM_OK ) return rc; } return lsmEnvSync(pFS->pEnv, pFS->fdDb); } static int fsPageGet(FileSystem *, Redirect *, Pgno, int, Page **, int *); static int fsRedirectBlock(Redirect *p, int iBlk){ if( p ){ int i; for(i=0; i<p->n; i++){ if( iBlk==p->a[i].iFrom ) return p->a[i].iTo; } } return iBlk; } static Pgno fsRedirectPage(FileSystem *pFS, Redirect *pRedir, Pgno iPg){ Pgno iReal = iPg; if( pRedir ){ const int nPagePerBlock = (pFS->nBlocksize / pFS->nPagesize); int iBlk = fsPageToBlock(pFS, iPg); int i; for(i=0; i<pRedir->n; i++){ int iFrom = pRedir->a[i].iFrom; if( iFrom>iBlk ) break; if( iFrom==iBlk ){ int iTo = pRedir->a[i].iTo; iReal = iPg - (Pgno)(iFrom - iTo) * nPagePerBlock; if( iTo==1 ){ iReal += (fsFirstPageOnBlock(pFS, 1)-1); } break; } } } return iReal; } /* ** Parameter iBlock is a database file block. This function reads the value ** stored in the blocks "next block" pointer and stores it in *piNext. ** LSM_OK is returned if everything is successful, or an LSM error code ** otherwise. */ static int fsBlockNext( FileSystem *pFS, /* File-system object handle */ Segment *pSeg, /* Use this segment for block redirects */ int iBlock, /* Read field from this block */ int *piNext /* OUT: Next block in linked list */ ){ int rc; int iRead; /* Read block from here */ if( pSeg ){ iRead = fsRedirectBlock(pSeg->pRedirect, iBlock); }else{ iRead = iBlock; } assert( pFS->bUseMmap==0 || pFS->pCompress==0 ); if( pFS->pCompress ){ i64 iOff; /* File offset to read data from */ u8 aNext[4]; /* 4-byte pointer read from db file */ iOff = (i64)iRead * pFS->nBlocksize - sizeof(aNext); rc = lsmEnvRead(pFS->pEnv, pFS->fdDb, iOff, aNext, sizeof(aNext)); if( rc==LSM_OK ){ *piNext = (int)lsmGetU32(aNext); } }else{ const int nPagePerBlock = (pFS->nBlocksize / pFS->nPagesize); Page *pLast; rc = fsPageGet(pFS, 0, iRead*nPagePerBlock, 0, &pLast, 0); if( rc==LSM_OK ){ *piNext = lsmGetU32(&pLast->aData[pFS->nPagesize-4]); lsmFsPageRelease(pLast); } } if( pSeg ){ *piNext = fsRedirectBlock(pSeg->pRedirect, *piNext); } return rc; } /* ** Return the page number of the last page on the same block as page iPg. */ Pgno fsLastPageOnPagesBlock(FileSystem *pFS, Pgno iPg){ |
︙ | ︙ | |||
941 942 943 944 945 946 947 | iEob = fsLastPageOnPagesBlock(pFS, iOff) + 1; nRead = LSM_MIN(iEob - iOff, nData); rc = lsmEnvRead(pFS->pEnv, pFS->fdDb, iOff, aData, nRead); if( rc==LSM_OK && nRead!=nData ){ int iBlk; | > > | | 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 | iEob = fsLastPageOnPagesBlock(pFS, iOff) + 1; nRead = LSM_MIN(iEob - iOff, nData); rc = lsmEnvRead(pFS->pEnv, pFS->fdDb, iOff, aData, nRead); if( rc==LSM_OK && nRead!=nData ){ int iBlk; /* TODO: Fix the pSeg arg to this */ assert( 0 ); rc = fsBlockNext(pFS, 0, fsPageToBlock(pFS, iOff), &iBlk); if( rc==LSM_OK ){ i64 iOff2 = fsFirstPageOnBlock(pFS, iBlk); rc = lsmEnvRead(pFS->pEnv, pFS->fdDb, iOff2, &aData[nRead], nData-nRead); } } return rc; |
︙ | ︙ | |||
1028 1029 1030 1031 1032 1033 1034 | iEob = fsLastPageOnPagesBlock(pFS, iOff); if( (iOff+iAdd)<=iEob ){ *piRes = (iOff+iAdd); return LSM_OK; } | > | | 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 | iEob = fsLastPageOnPagesBlock(pFS, iOff); if( (iOff+iAdd)<=iEob ){ *piRes = (iOff+iAdd); return LSM_OK; } assert( 0 ); rc = fsBlockNext(pFS, 0, fsPageToBlock(pFS, iOff), &iBlk); *piRes = fsFirstPageOnBlock(pFS, iBlk) + iAdd - (iEob - iOff + 1); return rc; } static int fsAllocateBuffer(FileSystem *pFS, int bWrite){ u8 **pp; /* Pointer to either aIBuffer or aOBuffer */ |
︙ | ︙ | |||
1384 1385 1386 1387 1388 1389 1390 | iBlk = fsPageToBlock(pFS, pDel->iFirst); iLastBlk = fsPageToBlock(pFS, pDel->iLastPg); /* Mark all blocks currently used by this sorted run as free */ while( iBlk && rc==LSM_OK ){ int iNext = 0; if( iBlk!=iLastBlk ){ | | | 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 | iBlk = fsPageToBlock(pFS, pDel->iFirst); iLastBlk = fsPageToBlock(pFS, pDel->iLastPg); /* Mark all blocks currently used by this sorted run as free */ while( iBlk && rc==LSM_OK ){ int iNext = 0; if( iBlk!=iLastBlk ){ rc = fsBlockNext(pFS, pDel, iBlk, &iNext); }else if( bZero==0 && pDel->iLastPg!=fsLastPageOnBlock(pFS, iLastBlk) ){ break; } rc = fsFreeBlock(pFS, pSnapshot, pDel, iBlk); iBlk = iNext; } |
︙ | ︙ | |||
1437 1438 1439 1440 1441 1442 1443 | while( rc==LSM_OK ){ int iNext = 0; Pgno iFirst = firstOnBlock(pFS, iBlk, aPgno, nPgno); if( iFirst ){ pRun->iFirst = iFirst; break; } | | | 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 | while( rc==LSM_OK ){ int iNext = 0; Pgno iFirst = firstOnBlock(pFS, iBlk, aPgno, nPgno); if( iFirst ){ pRun->iFirst = iFirst; break; } rc = fsBlockNext(pFS, pRun, iBlk, &iNext); if( rc==LSM_OK ) rc = fsFreeBlock(pFS, pSnapshot, pRun, iBlk); pRun->nSize -= ( 1 + fsLastPageOnBlock(pFS, iBlk) - fsFirstPageOnBlock(pFS, iBlk) ); iBlk = iNext; } |
︙ | ︙ | |||
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 | int rc = LSM_OK; FileSystem *pFS = pPg->pFS; Pgno iPg = pPg->iPg; if( pFS->pCompress ){ int nSpace = pPg->nCompress + 2*3; do { if( eDir>0 ){ rc = fsNextPageOffset(pFS, pRun, iPg, nSpace, &iPg); }else{ if( iPg==pRun->iFirst ){ iPg = 0; }else{ | > > > | 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 | int rc = LSM_OK; FileSystem *pFS = pPg->pFS; Pgno iPg = pPg->iPg; if( pFS->pCompress ){ int nSpace = pPg->nCompress + 2*3; /* TODO: Fix redirects */ assert( 0 ); do { if( eDir>0 ){ rc = fsNextPageOffset(pFS, pRun, iPg, nSpace, &iPg); }else{ if( iPg==pRun->iFirst ){ iPg = 0; }else{ |
︙ | ︙ | |||
1557 1558 1559 1560 1561 1562 1563 | }else if( fsIsFirst(pFS, iPg) ){ assert( pPg->flags & PAGE_HASPREV ); iPg = fsLastPageOnBlock(pFS, lsmGetU32(&pPg->aData[-4])); }else{ iPg--; } }else{ | > | > > > | | 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 | }else if( fsIsFirst(pFS, iPg) ){ assert( pPg->flags & PAGE_HASPREV ); iPg = fsLastPageOnBlock(pFS, lsmGetU32(&pPg->aData[-4])); }else{ iPg--; } }else{ Pgno iLast = fsRedirectPage(pFS, pRun->pRedirect, pRun->iLastPg); if( pRun && iPg==iLast ){ *ppNext = 0; return LSM_OK; }else if( fsIsLast(pFS, iPg) ){ int iBlk = fsRedirectBlock( pRun->pRedirect, lsmGetU32(&pPg->aData[pFS->nPagesize-4]) ); iPg = fsFirstPageOnBlock(pFS, iBlk); }else{ iPg++; } } rc = fsPageGet(pFS, pRun->pRedirect, iPg, 0, ppNext, 0); } |
︙ | ︙ | |||
1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 | int rc = LSM_OK; Page *pPg = 0; *ppOut = 0; int iApp = 0; int iNext = 0; Segment *p = &pLvl->lhs; int iPrev = p->iLastPg; if( pFS->pCompress || bDefer ){ /* In compressed database mode the page is not assigned a page number ** or location in the database file at this point. This will be done ** by the lsmFsPagePersist() call. */ rc = fsPageBuffer(pFS, 1, &pPg); if( rc==LSM_OK ){ | > > | 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 | int rc = LSM_OK; Page *pPg = 0; *ppOut = 0; int iApp = 0; int iNext = 0; Segment *p = &pLvl->lhs; int iPrev = p->iLastPg; assert( p->pRedirect==0 ); if( pFS->pCompress || bDefer ){ /* In compressed database mode the page is not assigned a page number ** or location in the database file at this point. This will be done ** by the lsmFsPagePersist() call. */ rc = fsPageBuffer(pFS, 1, &pPg); if( rc==LSM_OK ){ |
︙ | ︙ | |||
1636 1637 1638 1639 1640 1641 1642 | pFS->nOut++; } }else{ if( iPrev==0 ){ iApp = findAppendPoint(pFS, pLvl); }else if( fsIsLast(pFS, iPrev) ){ int iNext; | | | | 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 | pFS->nOut++; } }else{ if( iPrev==0 ){ iApp = findAppendPoint(pFS, pLvl); }else if( fsIsLast(pFS, iPrev) ){ int iNext; rc = fsBlockNext(pFS, 0, fsPageToBlock(pFS, iPrev), &iNext); if( rc!=LSM_OK ) return rc; iApp = fsFirstPageOnBlock(pFS, iNext); }else{ iApp = iPrev + 1; } /* If this is the first page allocated, or if the page allocated is the ** last in the block, also allocate the next block here. */ if( iApp==0 || fsIsLast(pFS, iApp) ){ int iNew; /* New block number */ rc = lsmBlockAllocate(pFS->pDb, 0, &iNew); if( rc!=LSM_OK ) return rc; if( iApp==0 ){ iApp = fsFirstPageOnBlock(pFS, iNew); }else{ iNext = fsFirstPageOnBlock(pFS, iNew); } } |
︙ | ︙ | |||
1716 1717 1718 1719 1720 1721 1722 | if( rc==LSM_OK ){ int iBlk = (int)lsmGetU32(&pLast->aData[pFS->nPagesize-4]); lsmBlockRefree(pFS->pDb, iBlk); lsmFsPageRelease(pLast); } }else{ int iBlk = 0; | | | 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 | if( rc==LSM_OK ){ int iBlk = (int)lsmGetU32(&pLast->aData[pFS->nPagesize-4]); lsmBlockRefree(pFS->pDb, iBlk); lsmFsPageRelease(pLast); } }else{ int iBlk = 0; rc = fsBlockNext(pFS, p, fsPageToBlock(pFS, p->iLastPg), &iBlk); if( rc==LSM_OK ){ lsmBlockRefree(pFS->pDb, iBlk); } } } return rc; } |
︙ | ︙ | |||
1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 | /* ** Return true if page is currently writable. */ int lsmFsPageWritable(Page *pPg){ return (pPg->flags & PAGE_DIRTY) ? 1 : 0; } /* ** Append raw data to a segment. This function is only used in compressed ** database mode. */ static Pgno fsAppendData( FileSystem *pFS, /* File-system handle */ | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 | /* ** Return true if page is currently writable. */ int lsmFsPageWritable(Page *pPg){ return (pPg->flags & PAGE_DIRTY) ? 1 : 0; } /* ** Copy the contents of block iFrom to block iTo. ** ** It is safe to assume that there are no outstanding references to pages ** on block iTo. And that block iFrom is not currently being written. */ int lsmFsMoveBlock(FileSystem *pFS, int iTo, int iFrom){ Snapshot *p = pFS->pDb->pWorker; int rc = LSM_OK; assert( iTo!=1 ); assert( iFrom>iTo ); if( pFS->bUseMmap ){ fsGrowMapping(pFS, (i64)iFrom * pFS->nBlocksize, &rc); if( rc==LSM_OK ){ u8 *aMap = (u8 *)(pFS->pMap); i64 iFromOff = (i64)(iFrom-1) * pFS->nBlocksize; i64 iToOff = (i64)(iTo-1) * pFS->nBlocksize; memcpy(&aMap[iToOff], &aMap[iFromOff], pFS->nBlocksize); } }else{ assert( 0 ); } /* Update append-point list if necessary */ if( rc==LSM_OK ){ int i; for(i=0; i<LSM_APPLIST_SZ; i++){ if( fsPageToBlock(pFS, p->aiAppend[i])==iFrom ){ const int nPagePerBlock = (pFS->nBlocksize / pFS->nPagesize); p->aiAppend[i] -= (i64)(iFrom-iTo) * nPagePerBlock; } } } return rc; } /* ** Append raw data to a segment. This function is only used in compressed ** database mode. */ static Pgno fsAppendData( FileSystem *pFS, /* File-system handle */ |
︙ | ︙ | |||
1880 1881 1882 1883 1884 1885 1886 | /* If this is the first data written into the segment, find an append-point ** or allocate a new block. */ if( iApp==1 ){ pSeg->iFirst = iApp = findAppendPoint(pFS, 0); if( iApp==0 ){ int iBlk; | | | 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 | /* If this is the first data written into the segment, find an append-point ** or allocate a new block. */ if( iApp==1 ){ pSeg->iFirst = iApp = findAppendPoint(pFS, 0); if( iApp==0 ){ int iBlk; rc = lsmBlockAllocate(pFS->pDb, 0, &iBlk); pSeg->iFirst = iApp = fsFirstPageOnBlock(pFS, iBlk); } } iRet = iApp; /* Write as much data as is possible at iApp (usually all of it). */ iLastOnBlock = fsLastPageOnPagesBlock(pFS, iApp); |
︙ | ︙ | |||
1909 1910 1911 1912 1913 1914 1915 | assert( nRem<=0 || (iApp-1)==iLastOnBlock ); if( rc==LSM_OK && (iApp-1)==iLastOnBlock ){ u8 aPtr[4]; /* Space to serialize a u32 */ int iBlk; /* New block number */ if( nWrite>0 ){ /* Allocate a new block. */ | | > | | 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 | assert( nRem<=0 || (iApp-1)==iLastOnBlock ); if( rc==LSM_OK && (iApp-1)==iLastOnBlock ){ u8 aPtr[4]; /* Space to serialize a u32 */ int iBlk; /* New block number */ if( nWrite>0 ){ /* Allocate a new block. */ rc = lsmBlockAllocate(pFS->pDb, 0, &iBlk); /* Set the "next" pointer on the old block */ if( rc==LSM_OK ){ assert( iApp==(fsPageToBlock(pFS, iApp)*pFS->nBlocksize)-4 ); lsmPutU32(aPtr, iBlk); rc = lsmEnvWrite(pFS->pEnv, pFS->fdDb, iApp, aPtr, sizeof(aPtr)); } /* Set the "prev" pointer on the new block */ if( rc==LSM_OK ){ Pgno iWrite; lsmPutU32(aPtr, fsPageToBlock(pFS, iApp)); iWrite = fsFirstPageOnBlock(pFS, iBlk); rc = lsmEnvWrite(pFS->pEnv, pFS->fdDb, iWrite-4, aPtr, sizeof(aPtr)); if( nRem>0 ) iApp = iWrite; } }else{ /* The next block is already allocated. */ assert( nRem>0 ); assert( pSeg->pRedirect==0 ); rc = fsBlockNext(pFS, 0, fsPageToBlock(pFS, iApp), &iBlk); iRet = iApp = fsFirstPageOnBlock(pFS, iBlk); } /* Write the remaining data into the new block */ if( rc==LSM_OK && nRem>0 ){ rc = lsmEnvWrite(pFS->pEnv, pFS->fdDb, iApp, &aData[nWrite], nRem); iApp += nRem; |
︙ | ︙ | |||
1990 1991 1992 1993 1994 1995 1996 | if( fsIsLast(pFS, iPrev) ){ /* Grab the first page on the next block (which has already be ** allocated). In this case set *piPrev to tell the caller to set ** the "previous block" pointer in the first 4 bytes of the page. */ int iNext; int iBlk = fsPageToBlock(pFS, iPrev); | > | | | 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 | if( fsIsLast(pFS, iPrev) ){ /* Grab the first page on the next block (which has already be ** allocated). In this case set *piPrev to tell the caller to set ** the "previous block" pointer in the first 4 bytes of the page. */ int iNext; int iBlk = fsPageToBlock(pFS, iPrev); assert( pSeg->pRedirect==0 ); rc = fsBlockNext(pFS, 0, iBlk, &iNext); if( rc!=LSM_OK ) return rc; *piNew = fsFirstPageOnBlock(pFS, iNext); *piPrev = iBlk; }else{ *piNew = iPrev+1; if( fsIsLast(pFS, *piNew) ){ /* Allocate the next block here. */ int iBlk; rc = lsmBlockAllocate(pFS->pDb, 0, &iBlk); if( rc!=LSM_OK ) return rc; *piNext = iBlk; } } pSeg->nSize++; pSeg->iLastPg = *piNew; |
︙ | ︙ | |||
2354 2355 2356 2357 2358 2359 2360 | iBlk = fsPageToBlock(pFS, pArray->iFirst); iLastBlk = fsPageToBlock(pFS, pArray->iLastPg); lsmStringInit(&str, pDb->pEnv); if( bBlock ){ lsmStringAppendf(&str, "%d", iBlk); while( iBlk!=iLastBlk ){ | | | | 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 | iBlk = fsPageToBlock(pFS, pArray->iFirst); iLastBlk = fsPageToBlock(pFS, pArray->iLastPg); lsmStringInit(&str, pDb->pEnv); if( bBlock ){ lsmStringAppendf(&str, "%d", iBlk); while( iBlk!=iLastBlk ){ fsBlockNext(pFS, pArray, iBlk, &iBlk); lsmStringAppendf(&str, " %d", iBlk); } }else{ lsmStringAppendf(&str, "%d", pArray->iFirst); while( iBlk!=iLastBlk ){ lsmStringAppendf(&str, " %d", fsLastPageOnBlock(pFS, iBlk)); fsBlockNext(pFS, pArray, iBlk, &iBlk); lsmStringAppendf(&str, " %d", fsFirstPageOnBlock(pFS, iBlk)); } lsmStringAppendf(&str, " %d", pArray->iLastPg); } *pzOut = str.z; } |
︙ | ︙ | |||
2473 2474 2475 2476 2477 2478 2479 2480 | Segment *pSeg, int bExtra, /* If true, count the "next" block if any */ int nUsed, u8 *aUsed ){ if( pSeg ){ if( pSeg && pSeg->nSize>0 ){ int rc; | > | > | | | | | > > > | | 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 | Segment *pSeg, int bExtra, /* If true, count the "next" block if any */ int nUsed, u8 *aUsed ){ if( pSeg ){ if( pSeg && pSeg->nSize>0 ){ Redirect *pRedir = pSeg->pRedirect; int rc; Pgno iLast; /* Last page of segment (post-redirection) */ Pgno iFirst; /* First page of segment (post-redirection) */ int iBlk; /* Current block (during iteration) */ int iLastBlk; /* Last real block of segment */ int bLastIsLastOnBlock; /* True iLast is the last on its block */ iBlk = fsRedirectBlock(pRedir, fsPageToBlock(pFS, pSeg->iFirst)); iLastBlk = fsRedirectBlock(pRedir, fsPageToBlock(pFS, pSeg->iLastPg)); iLast = fsRedirectPage(pFS, pRedir, pSeg->iLastPg); iFirst = fsRedirectPage(pFS, pRedir, pSeg->iFirst); bLastIsLastOnBlock = (fsLastPageOnBlock(pFS, iLastBlk)==iLast); assert( iBlk>0 ); /* If the first page of this run is also the first page of its first ** block, set the flag to indicate that the first page of iBlk is ** in use. */ if( fsFirstPageOnBlock(pFS, iBlk)==iFirst ){ assert( (aUsed[iBlk-1] & INTEGRITY_CHECK_FIRST_PG)==0 ); aUsed[iBlk-1] |= INTEGRITY_CHECK_FIRST_PG; } do { /* iBlk is a part of this sorted run. */ aUsed[iBlk-1] |= INTEGRITY_CHECK_USED; |
︙ | ︙ | |||
2509 2510 2511 2512 2513 2514 2515 | /* Special case. The sorted run being scanned is the output run of ** a level currently undergoing an incremental merge. The sorted ** run ends on the last page of iBlk, but the next block has already ** been allocated. So mark it as in use as well. */ if( iBlk==iLastBlk && bLastIsLastOnBlock && bExtra ){ int iExtra = 0; | | | | 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 | /* Special case. The sorted run being scanned is the output run of ** a level currently undergoing an incremental merge. The sorted ** run ends on the last page of iBlk, but the next block has already ** been allocated. So mark it as in use as well. */ if( iBlk==iLastBlk && bLastIsLastOnBlock && bExtra ){ int iExtra = 0; rc = fsBlockNext(pFS, pSeg, iBlk, &iExtra); assert( rc==LSM_OK ); assert( aUsed[iExtra-1]==0 ); aUsed[iExtra-1] |= INTEGRITY_CHECK_USED; aUsed[iExtra-1] |= INTEGRITY_CHECK_FIRST_PG; aUsed[iExtra-1] |= INTEGRITY_CHECK_LAST_PG; } /* Move on to the next block in the sorted run. Or set iBlk to zero ** in order to break out of the loop if this was the last block in ** the run. */ if( iBlk==iLastBlk ){ iBlk = 0; }else{ rc = fsBlockNext(pFS, pSeg, iBlk, &iBlk); assert( rc==LSM_OK ); } }while( iBlk ); } } } |
︙ | ︙ |
Changes to src/lsm_shared.c.
︙ | ︙ | |||
623 624 625 626 627 628 629 630 631 632 633 | } typedef struct FindFreeblockCtx FindFreeblockCtx; struct FindFreeblockCtx { i64 iInUse; int iRet; }; static int findFreeblockCb(void *pCtx, int iBlk, i64 iSnapshot){ FindFreeblockCtx *p = (FindFreeblockCtx *)pCtx; | > | | < < < < < < < < > < < < | | 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 | } typedef struct FindFreeblockCtx FindFreeblockCtx; struct FindFreeblockCtx { i64 iInUse; int iRet; int bNotOne; }; static int findFreeblockCb(void *pCtx, int iBlk, i64 iSnapshot){ FindFreeblockCtx *p = (FindFreeblockCtx *)pCtx; if( iSnapshot<p->iInUse && (iBlk!=1 || p->bNotOne==0) ){ p->iRet = iBlk; return 1; } return 0; } static int findFreeblock(lsm_db *pDb, i64 iInUse, int bNotOne, int *piRet){ int rc; /* Return code */ FindFreeblockCtx ctx; /* Context object */ ctx.iInUse = iInUse; ctx.iRet = 0; ctx.bNotOne = bNotOne; rc = lsmWalkFreelist(pDb, 0, findFreeblockCb, (void *)&ctx); *piRet = ctx.iRet; return rc; } /* ** Allocate a new database file block to write data to, either by extending ** the database file or by recycling a free-list entry. The worker snapshot ** must be held in order to call this function. ** ** If successful, *piBlk is set to the block number allocated and LSM_OK is ** returned. Otherwise, *piBlk is zeroed and an lsm error code returned. */ int lsmBlockAllocate(lsm_db *pDb, int iBefore, int *piBlk){ Snapshot *p = pDb->pWorker; int iRet = 0; /* Block number of allocated block */ int rc = LSM_OK; i64 iInUse = 0; /* Snapshot id still in use */ i64 iSynced = 0; /* Snapshot id synced to disk */ assert( p ); |
︙ | ︙ | |||
707 708 709 710 711 712 713 | "snapshot-in-use: %lld (iSynced=%lld) (client-id=%lld)", iInUse, iSynced, (pDb->pClient ? pDb->pClient->iId : 0) ); } #endif /* Query the free block list for a suitable block */ | | > > > > | | | < | | 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 | "snapshot-in-use: %lld (iSynced=%lld) (client-id=%lld)", iInUse, iSynced, (pDb->pClient ? pDb->pClient->iId : 0) ); } #endif /* Query the free block list for a suitable block */ if( rc==LSM_OK ) rc = findFreeblock(pDb, iInUse, (iBefore>0), &iRet); if( iBefore>0 && (iRet<=0 || iRet>=iBefore) ){ iRet = 0; }else if( rc==LSM_OK ){ /* If a block was found in the free block list, use it and remove it from ** the list. Otherwise, if no suitable block was found, allocate one from ** the end of the file. */ if( iRet>0 ){ #ifdef LSM_LOG_FREELIST lsmLogMessage(pDb, 0, "reusing block %d (snapshot-in-use=%lld)", iRet, iInUse); #endif rc = freelistAppend(pDb, iRet, -1); if( rc==LSM_OK ){ rc = dbTruncate(pDb, iInUse); } }else{ iRet = ++(p->nBlock); #ifdef LSM_LOG_FREELIST lsmLogMessage(pDb, 0, "extending file to %d blocks", iRet); #endif } } assert( iBefore>0 || iRet>0 || rc!=LSM_OK ); *piBlk = iRet; return rc; } /* ** Free a database block. The worker snapshot must be held in order to call ** this function. |
︙ | ︙ |
Changes to src/lsm_sorted.c.
︙ | ︙ | |||
4528 4529 4530 4531 4532 4533 4534 | && (pTop->nRight || sortedCountLevels(pTop)>=pDb->nMerge) ){ return 1; } return 0; } | > > > > > > > > > > > > > > > > > > > > > > > > | > | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 | && (pTop->nRight || sortedCountLevels(pTop)>=pDb->nMerge) ){ return 1; } return 0; } typedef struct MoveBlockCtx MoveBlockCtx; struct MoveBlockCtx { int iSeen; /* Previous free block on list */ int iFrom; /* Total number of blocks in file */ }; static int moveBlockCb(void *pCtx, int iBlk, i64 iSnapshot){ MoveBlockCtx *p = (MoveBlockCtx *)pCtx; assert( p->iFrom==0 ); if( iBlk==(p->iSeen-1) ){ p->iSeen = iBlk; return 0; } p->iFrom = p->iSeen-1; return 1; } /* ** This function is called to further compact a database for which all ** of the content has already been merged into a single segment. If ** possible, it moves the contents of a single block from the end of the ** file to a free-block that lies closer to the start of the file (allowing ** the file to be eventually truncated). */ static int sortedMoveBlock(lsm_db *pDb, int *pnWrite){ Snapshot *p = pDb->pWorker; Level *pLvl = lsmDbSnapshotLevel(p); int iFrom; /* Block to move */ int iTo; /* Destination to move block to */ int rc; /* Return code */ MoveBlockCtx sCtx; assert( pLvl->pNext==0 && pLvl->nRight==0 ); assert( p->redirect.n<=LSM_MAX_BLOCK_REDIRECTS ); *pnWrite = 0; /* Check that the redirect array is not already full. If it is, return ** without moving any database content. */ if( p->redirect.n>=LSM_MAX_BLOCK_REDIRECTS ) return LSM_OK; /* Find the last block of content in the database file. Do this by ** traversing the free-list in reverse (descending block number) order. ** The first block not on the free list is the one that will be moved. ** Since the db consists of a single segment, there is no ambiguity as ** to which segment the block belongs to. */ sCtx.iSeen = p->nBlock+1; sCtx.iFrom = 0; rc = lsmWalkFreelist(pDb, 1, moveBlockCb, &sCtx); if( rc!=LSM_OK || sCtx.iFrom==0 ) return rc; iFrom = sCtx.iFrom; /* Find the first free block in the database, ignoring block 1. Block ** 1 is tricky as it is smaller than the other blocks. */ rc = lsmBlockAllocate(pDb, iFrom, &iTo); if( rc!=LSM_OK || iTo==0 ) return rc; assert( iTo!=1 && iTo<iFrom ); rc = lsmFsMoveBlock(pDb->pFS, iTo, iFrom); if( rc==LSM_OK ){ if( p->redirect.a==0 ){ int nByte = sizeof(struct RedirectEntry) * LSM_MAX_BLOCK_REDIRECTS; p->redirect.a = lsmMallocZeroRc(pDb->pEnv, nByte, &rc); } if( rc==LSM_OK ){ memmove(&p->redirect.a[1], &p->redirect.a[0], sizeof(struct RedirectEntry) * p->redirect.n ); p->redirect.a[0].iFrom = iFrom; p->redirect.a[0].iTo = iTo; p->redirect.n++; rc = lsmBlockFree(pDb, iFrom); *pnWrite = lsmFsBlockSize(pDb->pFS) / lsmFsPageSize(pDb->pFS); pLvl->lhs.pRedirect = &p->redirect; } } return rc; } static int sortedWork( lsm_db *pDb, /* Database handle. Must be worker. */ int nWork, /* Number of pages of work to do */ int nMerge, /* Try to merge this many levels at once */ int bFlush, /* Set if call is to make room for a flush */ |
︙ | ︙ | |||
4561 4562 4563 4564 4565 4566 4567 | rc = sortedSelectLevel(pDb, nMerge, &pLevel); assert( rc==LSM_OK || pLevel==0 ); if( pLevel==0 ){ int nDone = 0; Level *pTopLevel = lsmDbSnapshotLevel(pDb->pWorker); if( bFlush==0 && nMerge==1 && pTopLevel && pTopLevel->pNext==0 ){ | | | 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 | rc = sortedSelectLevel(pDb, nMerge, &pLevel); assert( rc==LSM_OK || pLevel==0 ); if( pLevel==0 ){ int nDone = 0; Level *pTopLevel = lsmDbSnapshotLevel(pDb->pWorker); if( bFlush==0 && nMerge==1 && pTopLevel && pTopLevel->pNext==0 ){ rc = sortedMoveBlock(pDb, &nDone); } nRemaining -= nDone; /* Could not find any work to do. Finished. */ if( nDone==0 ) break; }else{ MergeWorker mergeworker; /* State used to work on the level merge */ |
︙ | ︙ |