Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Simplify the way new cursors are created. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
63d8eea50632fc59e36b315164e413bf |
User & Date: | dan 2012-10-02 18:06:14.062 |
Context
2012-10-03
| ||
09:24 | Minor changes to the lsmperf.tcl script. check-in: 45e59053e7 user: dan tags: trunk | |
2012-10-02
| ||
18:06 | Simplify the way new cursors are created. check-in: 63d8eea506 user: dan tags: trunk | |
12:05 | Remove dead code from lsm_sorted.c. check-in: b84772a1aa user: dan tags: trunk | |
Changes
Changes to src/lsm_sorted.c.
︙ | ︙ | |||
105 106 107 108 109 110 111 | ** Macros to help decode record types. */ #define rtTopic(eType) ((eType) & 0xF0) #define rtIsDelete(eType) (((eType) & 0x0F)==SORTED_DELETE) #define rtIsSeparator(eType) (((eType) & 0x0F)==SORTED_SEPARATOR) #define rtIsWrite(eType) (((eType) & 0x0F)==SORTED_WRITE) | < < < | 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | ** Macros to help decode record types. */ #define rtTopic(eType) ((eType) & 0xF0) #define rtIsDelete(eType) (((eType) & 0x0F)==SORTED_DELETE) #define rtIsSeparator(eType) (((eType) & 0x0F)==SORTED_SEPARATOR) #define rtIsWrite(eType) (((eType) & 0x0F)==SORTED_WRITE) /* ** The following macros are used to access a page footer. */ #define SEGMENT_NRECORD_OFFSET(pgsz) ((pgsz) - 2) #define SEGMENT_FLAGS_OFFSET(pgsz) ((pgsz) - 2 - 2) #define SEGMENT_POINTER_OFFSET(pgsz) ((pgsz) - 2 - 2 - 4) #define SEGMENT_CELLPTR_OFFSET(pgsz, iCell) ((pgsz) - 2 - 2 - 4 - 2 - (iCell)*2) |
︙ | ︙ | |||
1852 1853 1854 1855 1856 1857 1858 | mcursorFreeComponents(pCsr); /* Free the cursor structure itself */ lsmFree(pDb->pEnv, pCsr); } } | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | | < < < < < < | < < < < < < < < < < | | < < | < | | | | | > > > > > > > > > > > > > > > > > > | > | > > | > > | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | > > | > | 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 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 | mcursorFreeComponents(pCsr); /* Free the cursor structure itself */ lsmFree(pDb->pEnv, pCsr); } } #define TREE_NONE 0 #define TREE_OLD 1 #define TREE_BOTH 2 /* ** Parameter eTree is one of TREE_OLD or TREE_BOTH. */ static int multiCursorAddTree(MultiCursor *pCsr, Snapshot *pSnap, int eTree){ int rc = LSM_OK; lsm_db *db = pCsr->pDb; /* Add a tree cursor on the 'old' tree, if it exists. */ if( eTree!=TREE_NONE && lsmTreeHasOld(db) && db->treehdr.iOldLog!=pSnap->iLogOff ){ rc = lsmTreeCursorNew(db, 1, &pCsr->apTreeCsr[1]); } /* Add a tree cursor on the 'current' tree, if required. */ if( rc==LSM_OK && eTree==TREE_BOTH ){ rc = lsmTreeCursorNew(db, 0, &pCsr->apTreeCsr[0]); } return rc; } static int multiCursorAddRhs(MultiCursor *pCsr, Level *pLvl){ int i; int nRhs = pLvl->nRight; assert( pLvl->nRight>0 ); assert( pCsr->aPtr==0 ); pCsr->aPtr = lsmMallocZero(pCsr->pDb->pEnv, sizeof(SegmentPtr) * nRhs); if( !pCsr->aPtr ) return LSM_NOMEM_BKPT; pCsr->nPtr = nRhs; for(i=0; i<nRhs; i++){ pCsr->aPtr[i].pSeg = &pLvl->aRhs[i]; pCsr->aPtr[i].pLevel = pLvl; } return LSM_OK; } static int multiCursorAddAll(MultiCursor *pCsr, Snapshot *pSnap){ Level *pLvl; int nPtr = 0; int iPtr = 0; int rc = LSM_OK; for(pLvl=pSnap->pLevel; pLvl; pLvl=pLvl->pNext){ nPtr += (1 + pLvl->nRight); } assert( pCsr->aPtr==0 ); pCsr->aPtr = lsmMallocZeroRc(pCsr->pDb->pEnv, sizeof(SegmentPtr) * nPtr, &rc); if( rc==LSM_OK ) pCsr->nPtr = nPtr; for(pLvl=pSnap->pLevel; pLvl && rc==LSM_OK; pLvl=pLvl->pNext){ int i; pCsr->aPtr[iPtr].pLevel = pLvl; pCsr->aPtr[iPtr].pSeg = &pLvl->lhs; iPtr++; for(i=0; i<pLvl->nRight; i++){ pCsr->aPtr[iPtr].pLevel = pLvl; pCsr->aPtr[iPtr].pSeg = &pLvl->aRhs[i]; iPtr++; } if( pLvl->nRight && pLvl->pSplitKey==0 ){ lsmSortedSplitkey(pCsr->pDb, pLvl, &rc); } } return rc; } static int multiCursorInit(MultiCursor *pCsr, Snapshot *pSnap){ int rc; rc = multiCursorAddAll(pCsr, pSnap); if( rc==LSM_OK ){ rc = multiCursorAddTree(pCsr, pSnap, TREE_BOTH); } pCsr->flags |= (CURSOR_IGNORE_SYSTEM | CURSOR_IGNORE_DELETE); return rc; } static MultiCursor *multiCursorNew(lsm_db *db, int *pRc){ MultiCursor *pCsr; pCsr = (MultiCursor *)lsmMallocZeroRc(db->pEnv, sizeof(MultiCursor), pRc); if( pCsr ){ pCsr->pNext = db->pCsr; db->pCsr = pCsr; pCsr->pDb = db; } return pCsr; } void lsmSortedRemap(lsm_db *pDb){ MultiCursor *pCsr; for(pCsr=pDb->pCsr; pCsr; pCsr=pCsr->pNext){ int iPtr; if( pCsr->pBtCsr ){ btreeCursorLoadKey(pCsr->pBtCsr); |
︙ | ︙ | |||
2005 2006 2007 2008 2009 2010 2011 | */ static void multiCursorIgnoreDelete(MultiCursor *pCsr){ if( pCsr ) pCsr->flags |= CURSOR_IGNORE_DELETE; } /* ** If the free-block list is not empty, then have this cursor visit a key | | | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | | > > > > > | 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 | */ static void multiCursorIgnoreDelete(MultiCursor *pCsr){ if( pCsr ) pCsr->flags |= CURSOR_IGNORE_DELETE; } /* ** If the free-block list is not empty, then have this cursor visit a key ** with (a) the system bit set, and (b) the key "FREELIST" and (c) a value ** blob containing the serialized free-block list. */ static void multiCursorVisitFreelist(MultiCursor *pCsr, int *pnOvfl){ assert( pCsr ); pCsr->pnOvfl = pnOvfl; pCsr->flags |= CURSOR_NEW_SYSTEM; } /* ** Allocate and return a new database cursor. */ int lsmMCursorNew( lsm_db *pDb, /* Database handle */ MultiCursor **ppCsr /* OUT: Allocated cursor */ ){ MultiCursor *pCsr = 0; int rc = LSM_OK; pCsr = multiCursorNew(pDb, &rc); if( rc==LSM_OK ) rc = multiCursorInit(pCsr, pDb->pClient); if( rc!=LSM_OK ){ lsmMCursorClose(pCsr); pCsr = 0; } assert( (rc==LSM_OK)==(pCsr!=0) ); *ppCsr = pCsr; return rc; } static int multiCursorGetVal( MultiCursor *pCsr, |
︙ | ︙ | |||
2127 2128 2129 2130 2131 2132 2133 | } int lsmSortedLoadFreelist( lsm_db *pDb, /* Database handle (must be worker) */ void **ppVal, /* OUT: Blob containing LSM free-list */ int *pnVal /* OUT: Size of *ppVal blob in bytes */ ){ | | | > > | > > > | 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 | } int lsmSortedLoadFreelist( lsm_db *pDb, /* Database handle (must be worker) */ void **ppVal, /* OUT: Blob containing LSM free-list */ int *pnVal /* OUT: Size of *ppVal blob in bytes */ ){ MultiCursor *pCsr; /* Cursor used to retreive free-list */ int rc = LSM_OK; /* Return Code */ assert( pDb->pWorker ); assert( *ppVal==0 && *pnVal==0 ); pCsr = multiCursorNew(pDb, &rc); if( pCsr ){ rc = multiCursorAddAll(pCsr, pDb->pWorker); pCsr->flags |= CURSOR_IGNORE_DELETE; } if( rc==LSM_OK ){ rc = lsmMCursorLast(pCsr); if( rc==LSM_OK && pCsr->eType==SORTED_SYSTEM_WRITE && pCsr->key.nData==8 && 0==memcmp(pCsr->key.pData, "FREELIST", 8) ){ |
︙ | ︙ | |||
2259 2260 2261 2262 2263 2264 2265 | int mcursorSave(MultiCursor *pCsr){ int rc = LSM_OK; if( pCsr->aTree ){ int iTree = pCsr->aTree[1]; if( iTree==CURSOR_DATA_TREE0 || iTree==CURSOR_DATA_TREE1 ){ multiCursorCacheKey(pCsr, &rc); } | < > | | 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 | int mcursorSave(MultiCursor *pCsr){ int rc = LSM_OK; if( pCsr->aTree ){ int iTree = pCsr->aTree[1]; if( iTree==CURSOR_DATA_TREE0 || iTree==CURSOR_DATA_TREE1 ){ multiCursorCacheKey(pCsr, &rc); } } mcursorFreeComponents(pCsr); return rc; } int mcursorRestore(lsm_db *pDb, MultiCursor *pCsr){ int rc; rc = multiCursorInit(pCsr, pDb->pClient); if( rc==LSM_OK && pCsr->key.pData ){ rc = lsmMCursorSeek(pCsr, pCsr->key.pData, pCsr->key.nData, +1); } return rc; } int lsmSaveCursors(lsm_db *pDb){ |
︙ | ︙ | |||
3409 3410 3411 3412 3413 3414 3415 | int nWrite = 0; /* Number of database pages written */ assert( pnOvfl ); /* Allocate the new level structure to write to. */ pNext = lsmDbSnapshotLevel(pDb->pWorker); pNew = (Level *)lsmMallocZeroRc(pDb->pEnv, sizeof(Level), &rc); | > > > | > < | < < < < < | < | < > | | | < < > | < < < < < < < < < < < | 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 | int nWrite = 0; /* Number of database pages written */ assert( pnOvfl ); /* Allocate the new level structure to write to. */ pNext = lsmDbSnapshotLevel(pDb->pWorker); pNew = (Level *)lsmMallocZeroRc(pDb->pEnv, sizeof(Level), &rc); if( pNew ){ pNew->pNext = pNext; lsmDbSnapshotSetLevel(pDb->pWorker, pNew); } /* Create a cursor to gather the data required by the new segment. The new ** segment contains everything in the tree and pointers to the next segment ** in the database (if any). */ pCsr = multiCursorNew(pDb, &rc); if( pCsr ){ pCsr->pDb = pDb; multiCursorVisitFreelist(pCsr, pnOvfl); rc = multiCursorAddTree(pCsr, pDb->pWorker, eTree); if( rc==LSM_OK && pNext && pNext->pMerge==0 && pNext->lhs.iRoot ){ pDel = &pNext->lhs; rc = btreeCursorNew(pDb, pDel, &pCsr->pBtCsr); iLeftPtr = pNext->lhs.iFirst; } } if( rc!=LSM_OK ){ lsmMCursorClose(pCsr); }else{ Merge merge; /* Merge object used to create new level */ |
︙ | ︙ | |||
3477 3478 3479 3480 3481 3482 3483 | nWrite = mergeworker.nWork; mergeWorkerShutdown(&mergeworker, &rc); pNew->pMerge = 0; } /* Link the new level into the top of the tree. */ if( rc==LSM_OK ){ | | < < < < > > > | 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 | nWrite = mergeworker.nWork; mergeWorkerShutdown(&mergeworker, &rc); pNew->pMerge = 0; } /* Link the new level into the top of the tree. */ if( rc==LSM_OK ){ if( pDel ) pDel->iRoot = 0; }else{ lsmDbSnapshotSetLevel(pDb->pWorker, pNext); sortedFreeLevel(pDb->pEnv, pNew); } if( rc==LSM_OK ){ sortedInvokeWorkHook(pDb); } #if 0 lsmSortedDumpStructure(pDb, pDb->pWorker, 0, 0, "new-toplevel"); #endif if( pnWrite ) *pnWrite = nWrite; pDb->pWorker->nWrite += nWrite; return rc; } /* ** The nMerge levels in the LSM beginning with pLevel consist of a ** left-hand-side segment only. Replace these levels with a single new ** level consisting of a new empty segment on the left-hand-side and the |
︙ | ︙ | |||
3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 | lsm_db *pDb, /* Db connection to do merge work */ Level *pLevel, /* Level to work on merging */ MergeWorker *pMW /* Object to initialize */ ){ int rc = LSM_OK; /* Return code */ Merge *pMerge = pLevel->pMerge; /* Persistent part of merge state */ MultiCursor *pCsr = 0; /* Cursor opened for pMW */ assert( pDb->pWorker ); assert( pLevel->pMerge ); assert( pLevel->nRight>0 ); memset(pMW, 0, sizeof(MergeWorker)); pMW->pDb = pDb; pMW->pLevel = pLevel; pMW->aGobble = lsmMallocZeroRc(pDb->pEnv, sizeof(Pgno) * pLevel->nRight, &rc); /* Create a multi-cursor to read the data to write to the new ** segment. The new segment contains: ** ** 1. Records from LHS of each of the nMerge levels being merged. ** 2. Separators from either the last level being merged, or the ** separators attached to the LHS of the following level, or neither. ** ** If the new level is the lowest (oldest) in the db, discard any ** delete keys. Key annihilation. */ | > < | < | | < | > | < < > | 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 | lsm_db *pDb, /* Db connection to do merge work */ Level *pLevel, /* Level to work on merging */ MergeWorker *pMW /* Object to initialize */ ){ int rc = LSM_OK; /* Return code */ Merge *pMerge = pLevel->pMerge; /* Persistent part of merge state */ MultiCursor *pCsr = 0; /* Cursor opened for pMW */ Level *pNext = pLevel->pNext; /* Next level in LSM */ assert( pDb->pWorker ); assert( pLevel->pMerge ); assert( pLevel->nRight>0 ); memset(pMW, 0, sizeof(MergeWorker)); pMW->pDb = pDb; pMW->pLevel = pLevel; pMW->aGobble = lsmMallocZeroRc(pDb->pEnv, sizeof(Pgno) * pLevel->nRight, &rc); /* Create a multi-cursor to read the data to write to the new ** segment. The new segment contains: ** ** 1. Records from LHS of each of the nMerge levels being merged. ** 2. Separators from either the last level being merged, or the ** separators attached to the LHS of the following level, or neither. ** ** If the new level is the lowest (oldest) in the db, discard any ** delete keys. Key annihilation. */ pCsr = multiCursorNew(pDb, &rc); if( pCsr ){ rc = multiCursorAddRhs(pCsr, pLevel); } if( rc==LSM_OK && pMerge->nInput > pLevel->nRight ){ rc = btreeCursorNew(pDb, &pNext->lhs, &pCsr->pBtCsr); }else if( pNext ){ multiCursorReadSeparators(pCsr); }else{ multiCursorIgnoreDelete(pCsr); } assert( rc!=LSM_OK || pMerge->nInput==(pCsr->nPtr+(pCsr->pBtCsr!=0)) ); pMW->pCsr = pCsr; /* Load the current output page into memory. */ if( rc==LSM_OK ) rc = mergeWorkerLoadOutputPage(pMW); /* Position the cursor. */ |
︙ | ︙ |