Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Testing finds no cases where separator runs are faster than embedded btrees. So discard the separator runs related code. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | embedded-btree |
Files: | files | file ages | folders |
SHA1: |
afcbe561ffbea8572cf6d1a908430bb7 |
User & Date: | dan 2012-06-27 16:02:23.686 |
Context
2012-06-27
| ||
16:10 | Remove a broken assert from lsm_sorted.c. Leaf check-in: 50e87e23bb user: dan tags: embedded-btree | |
16:02 | Testing finds no cases where separator runs are faster than embedded btrees. So discard the separator runs related code. check-in: afcbe561ff user: dan tags: embedded-btree | |
10:22 | Ensure that "expensive-assert" code only runs when LSM_DEBUG_EXPENSIVE is defined. check-in: 2be9bac408 user: dan tags: embedded-btree | |
Changes
Changes to src/lsmInt.h.
︙ | ︙ | |||
68 69 70 71 72 73 74 | typedef struct Mempool Mempool; typedef struct MetaPage MetaPage; typedef struct MultiCursor MultiCursor; typedef struct Page Page; typedef struct Segment Segment; typedef struct SegmentMerger SegmentMerger; typedef struct Snapshot Snapshot; | < | 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | typedef struct Mempool Mempool; typedef struct MetaPage MetaPage; typedef struct MultiCursor MultiCursor; typedef struct Page Page; typedef struct Segment Segment; typedef struct SegmentMerger SegmentMerger; typedef struct Snapshot Snapshot; typedef struct TransMark TransMark; typedef struct Tree Tree; typedef struct TreeMark TreeMark; typedef struct TreeVersion TreeVersion; typedef struct TreeCursor TreeCursor; typedef struct Merge Merge; typedef struct MergeInput MergeInput; |
︙ | ︙ | |||
200 201 202 203 204 205 206 | void *pLogCtx; /* Work done notification callback */ void (*xWork)(lsm_db *, void *); void *pWorkCtx; }; | | < < < < < | 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | void *pLogCtx; /* Work done notification callback */ void (*xWork)(lsm_db *, void *); void *pWorkCtx; }; struct Segment { int iFirst; /* First page of this run */ int iLast; /* Last page of this run */ Pgno iRoot; /* Root page number (if any) */ int nSize; /* Size of this run in pages */ }; /* ** iSplitTopic/pSplitKey/nSplitKey: ** If nRight>0, this buffer contains a copy of the largest key that has ** already been written to the left-hand-side of the level. */ struct Level { Segment lhs; /* Left-hand (main) segment */ |
︙ | ︙ | |||
240 241 242 243 244 245 246 | ** ** It is assumed that code that uses an instance of this structure has ** access to the associated Level struct. ** ** bHierReadonly: ** True if the b-tree hierarchy is currently read-only. ** | | | < < | | | | 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | ** ** It is assumed that code that uses an instance of this structure has ** access to the associated Level struct. ** ** bHierReadonly: ** True if the b-tree hierarchy is currently read-only. ** ** iOutputOff: ** The byte offset to write to next within the last page of the ** output segment. */ struct Merge { int nInput; /* Number of input runs being merged */ MergeInput *aInput; /* Array nInput entries in size */ int nSkip; /* Number of separators entries to skip */ int iOutputOff; /* Write offset on output page */ int bHierReadonly; /* True if b-tree heirarchies are read-only */ }; struct MergeInput { Pgno iPg; /* Page on which next input is stored */ int iCell; /* Cell containing next input to merge */ }; /* |
︙ | ︙ | |||
369 370 371 372 373 374 375 | int lsmFsPageSize(FileSystem *); void lsmFsSetPageSize(FileSystem *, int); int lsmFsFileid(lsm_db *pDb, void **ppId, int *pnId); /* Creating, populating, gobbling and deleting sorted runs. */ | < < | | | | | | | 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | int lsmFsPageSize(FileSystem *); void lsmFsSetPageSize(FileSystem *, int); int lsmFsFileid(lsm_db *pDb, void **ppId, int *pnId); /* Creating, populating, gobbling and deleting sorted runs. */ void lsmFsGobble(Snapshot *, Segment *, Page *); int lsmFsSortedDelete(FileSystem *, Snapshot *, int, Segment *); int lsmFsSortedFinish(FileSystem *, Segment *); int lsmFsSortedAppend(FileSystem *, Snapshot *, Segment *, Page **); int lsmFsPhantomMaterialize(FileSystem *, Snapshot *, Segment *); /* Functions to retrieve the lsm_env pointer from a FileSystem or Page object */ lsm_env *lsmFsEnv(FileSystem *); lsm_env *lsmPageEnv(Page *); FileSystem *lsmPageFS(Page *); int lsmFsSectorSize(FileSystem *); void lsmSortedSplitkey(lsm_db *, Level *, int *); int lsmFsSetupAppendList(lsm_db *db); /* Reading sorted run content. */ int lsmFsDbPageGet(FileSystem *, Pgno, Page **); int lsmFsDbPageNext(Segment *, Page *, int eDir, Page **); int lsmFsPageWrite(Page *); u8 *lsmFsPageData(Page *, int *); int lsmFsPageRelease(Page *); int lsmFsPagePersist(Page *); void lsmFsPageRef(Page *); Pgno lsmFsPageNumber(Page *); |
︙ | ︙ | |||
471 472 473 474 475 476 477 | int lsmMCursorType(MultiCursor *, int *); lsm_db *lsmMCursorDb(MultiCursor *); int lsmSaveCursors(lsm_db *pDb); int lsmRestoreCursors(lsm_db *pDb); void lsmSortedDumpStructure(lsm_db *pDb, Snapshot *, int, int, const char *); | < | 461 462 463 464 465 466 467 468 469 470 471 472 473 474 | int lsmMCursorType(MultiCursor *, int *); lsm_db *lsmMCursorDb(MultiCursor *); int lsmSaveCursors(lsm_db *pDb); int lsmRestoreCursors(lsm_db *pDb); void lsmSortedDumpStructure(lsm_db *pDb, Snapshot *, int, int, const char *); void lsmFsDumpBlocklists(lsm_db *); void lsmPutU32(u8 *, u32); u32 lsmGetU32(u8 *); /* |
︙ | ︙ |
Changes to src/lsm_ckpt.c.
︙ | ︙ | |||
62 63 64 65 66 67 68 | ** The checksum: ** ** 1. Checksum value 1. ** 2. Checksum value 2. ** ** In the above, a segment record is: ** | | | | | < < | 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | ** The checksum: ** ** 1. Checksum value 1. ** 2. Checksum value 2. ** ** In the above, a segment record is: ** ** 1. First page of array, ** 2. Last page of array, ** 3. Root page of array (or 0), ** 4. Size of array in pages, */ /* ** OVERSIZED CHECKPOINT BLOBS: ** ** There are two slots allocated for checkpoints at the start of each ** database file. Each are 4096 bytes in size, so may accommodate |
︙ | ︙ | |||
219 220 221 222 223 224 225 | Segment *pSeg, CkptBuffer *p, int *piOut, int *pRc ){ int iOut = *piOut; | | | | | < < < < < < < < < < < | 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | Segment *pSeg, CkptBuffer *p, int *piOut, int *pRc ){ int iOut = *piOut; ckptSetValue(p, iOut++, pSeg->iFirst, pRc); ckptSetValue(p, iOut++, pSeg->iLast, pRc); ckptSetValue(p, iOut++, pSeg->iRoot, pRc); ckptSetValue(p, iOut++, pSeg->nSize, pRc); *piOut = iOut; } static void ckptExportLevel( Level *pLevel, CkptBuffer *p, |
︙ | ︙ | |||
386 387 388 389 390 391 392 | static void ckptNewSegment( u32 *aIn, int *piIn, Segment *pSegment /* Populate this structure */ ){ int iIn = *piIn; | | | < < < | | | | < < < < < | 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 | static void ckptNewSegment( u32 *aIn, int *piIn, Segment *pSegment /* Populate this structure */ ){ int iIn = *piIn; assert( pSegment->iFirst==0 && pSegment->iLast==0 ); assert( pSegment->nSize==0 && pSegment->iRoot==0 ); pSegment->iFirst = aIn[iIn++]; pSegment->iLast = aIn[iIn++]; pSegment->iRoot = aIn[iIn++]; pSegment->nSize = aIn[iIn++]; *piIn = iIn; } static int ckptSetupMerge(lsm_db *pDb, u32 *aInt, int *piIn, Level *pLevel){ Merge *pMerge; /* Allocated Merge object */ int nInput; /* Number of input segments in merge */ |
︙ | ︙ | |||
421 422 423 424 425 426 427 | pMerge = (Merge *)lsmMallocZero(pDb->pEnv, nByte); if( !pMerge ) return LSM_NOMEM_BKPT; pLevel->pMerge = pMerge; /* Populate the Merge object. */ pMerge->aInput = (MergeInput *)&pMerge[1]; pMerge->nInput = nInput; | | | < < | 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | pMerge = (Merge *)lsmMallocZero(pDb->pEnv, nByte); if( !pMerge ) return LSM_NOMEM_BKPT; pLevel->pMerge = pMerge; /* Populate the Merge object. */ pMerge->aInput = (MergeInput *)&pMerge[1]; pMerge->nInput = nInput; pMerge->iOutputOff = -1; pMerge->bHierReadonly = 1; pMerge->nSkip = (int)aInt[iIn++]; for(i=0; i<nInput; i++){ pMerge->aInput[i].iPg = (Pgno)aInt[iIn++]; pMerge->aInput[i].iCell = (int)aInt[iIn++]; } /* Set *piIn and return LSM_OK. */ *piIn = iIn; |
︙ | ︙ | |||
500 501 502 503 504 505 506 | return rc; } static int ckptImport(lsm_db *pDb, void *pCkpt, int nInt, int *pRc){ int ret = 0; if( *pRc==LSM_OK ){ Snapshot *pSnap = pDb->pWorker; | < | 477 478 479 480 481 482 483 484 485 486 487 488 489 490 | return rc; } static int ckptImport(lsm_db *pDb, void *pCkpt, int nInt, int *pRc){ int ret = 0; if( *pRc==LSM_OK ){ Snapshot *pSnap = pDb->pWorker; u32 cksum[2] = {0, 0}; u32 *aInt = (u32 *)pCkpt; lsmChecksumBytes((u8 *)aInt, sizeof(u32)*(nInt-2), 0, cksum); if( LSM_LITTLE_ENDIAN ){ int i; for(i=0; i<nInt; i++) aInt[i] = BYTESWAP32(aInt[i]); |
︙ | ︙ | |||
687 688 689 690 691 692 693 | int lsmCheckpointLevels( lsm_db *pDb, /* Database handle */ int *pnHdrLevel, /* OUT: Levels to write to db header */ void **paVal, /* OUT: Pointer to LEVELS blob */ int *pnVal /* OUT: Size of LEVELS blob in bytes */ ){ int rc = LSM_OK; /* Return code */ | | | 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 | int lsmCheckpointLevels( lsm_db *pDb, /* Database handle */ int *pnHdrLevel, /* OUT: Levels to write to db header */ void **paVal, /* OUT: Pointer to LEVELS blob */ int *pnVal /* OUT: Size of LEVELS blob in bytes */ ){ int rc = LSM_OK; /* Return code */ const int SEGMENT_SIZE = 4; /* Size of a checkpoint segment record */ Level *p; /* Used to iterate through levels */ int nFree; /* Free integers remaining in db header */ int nHdr = 0; /* Number of levels stored in db header */ int nLevels = 0; /* Number of levels stored in LEVELS */ /* Number of free integers - 1024 less those used by the checkpoint header, ** less the 4 used for the log-pointer, less the 3 used for the |
︙ | ︙ |
Changes to src/lsm_file.c.
︙ | ︙ | |||
86 87 88 89 90 91 92 | */ #include "lsmInt.h" #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | */ #include "lsmInt.h" #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> /* ** File-system object. Each database connection allocates a single instance ** of the following structure. It is used for all access to the database and ** log files. ** ** pLruFirst, pLruLast: |
︙ | ︙ | |||
148 149 150 151 152 153 154 | struct FileSystem { lsm_db *pDb; /* Database handle that owns this object */ lsm_env *pEnv; /* Environment pointer */ char *zDb; /* Database file name */ int nMetasize; /* Size of meta pages in bytes */ int nPagesize; /* Database page-size in bytes */ int nBlocksize; /* Database block-size in bytes */ | < | 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | struct FileSystem { lsm_db *pDb; /* Database handle that owns this object */ lsm_env *pEnv; /* Environment pointer */ char *zDb; /* Database file name */ int nMetasize; /* Size of meta pages in bytes */ int nPagesize; /* Database page-size in bytes */ int nBlocksize; /* Database block-size in bytes */ /* r/w file descriptors for both files. */ lsm_file *fdDb; /* Database file */ lsm_file *fdLog; /* Log file */ /* mmap() mode things */ int bUseMmap; /* True to use mmap() to access db file */ |
︙ | ︙ | |||
212 213 214 215 216 217 218 | /* ** Number of pgsz byte pages omitted from the start of block 1. The start ** of block 1 contains two 4096 byte meta pages (8192 bytes in total). */ #define BLOCK1_HDR_SIZE(pgsz) LSM_MAX(1, 8192/(pgsz)) | < < < < < | 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | /* ** Number of pgsz byte pages omitted from the start of block 1. The start ** of block 1 contains two 4096 byte meta pages (8192 bytes in total). */ #define BLOCK1_HDR_SIZE(pgsz) LSM_MAX(1, 8192/(pgsz)) /* ** Wrappers around the VFS methods of the lsm_env object: ** ** lsmEnvOpen() ** lsmEnvRead() ** lsmEnvWrite() |
︙ | ︙ | |||
414 415 416 417 418 419 420 | */ void lsmFsClose(FileSystem *pFS){ if( pFS ){ Page *pPg; lsm_env *pEnv = pFS->pEnv; assert( pFS->nOut==0 ); | < | 372 373 374 375 376 377 378 379 380 381 382 383 384 385 | */ void lsmFsClose(FileSystem *pFS){ if( pFS ){ Page *pPg; lsm_env *pEnv = pFS->pEnv; assert( pFS->nOut==0 ); pPg = pFS->pLruFirst; while( pPg ){ Page *pNext = pPg->pLruNext; if( pPg->flags & PAGE_FREE ) lsmFree(pEnv, pPg->aData); lsmFree(pEnv, pPg); pPg = pNext; } |
︙ | ︙ | |||
597 598 599 600 601 602 603 604 605 606 607 608 609 610 | pPg->pLruPrev->pLruNext = pPg; }else{ pFS->pLruFirst = pPg; } pFS->pLruLast = pPg; } static void fsPageRemoveFromHash(FileSystem *pFS, Page *pPg){ int iHash; Page **pp; iHash = fsHashKey(pFS->nHash, pPg->iPg); for(pp=&pFS->apHash[iHash]; *pp!=pPg; pp=&(*pp)->pHashNext); *pp = pPg->pHashNext; | > > > | 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 | pPg->pLruPrev->pLruNext = pPg; }else{ pFS->pLruFirst = pPg; } pFS->pLruLast = pPg; } /* ** Remove page pPg from the hash table. */ static void fsPageRemoveFromHash(FileSystem *pFS, Page *pPg){ int iHash; Page **pp; iHash = fsHashKey(pFS->nHash, pPg->iPg); for(pp=&pFS->apHash[iHash]; *pp!=pPg; pp=&(*pp)->pHashNext); *pp = pPg->pHashNext; |
︙ | ︙ | |||
779 780 781 782 783 784 785 | *piNext = fsPageToBlock(pFS, lsmGetU32(&pLast->aData[pFS->nPagesize-4])); lsmFsPageRelease(pLast); } return rc; } static int fsRunEndsBetween( | | | | | < < | < < | | 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 | *piNext = fsPageToBlock(pFS, lsmGetU32(&pLast->aData[pFS->nPagesize-4])); lsmFsPageRelease(pLast); } return rc; } static int fsRunEndsBetween( Segment *pRun, Segment *pIgnore, int iFirst, int iLast ){ return (pRun!=pIgnore && ( (pRun->iFirst>=iFirst && pRun->iFirst<=iLast) || (pRun->iLast>=iFirst && pRun->iLast<=iLast) )); } static int fsLevelEndsBetween( Level *pLevel, Segment *pIgnore, int iFirst, int iLast ){ int i; if( fsRunEndsBetween(&pLevel->lhs, pIgnore, iFirst, iLast) ){ return 1; } for(i=0; i<pLevel->nRight; i++){ if( fsRunEndsBetween(&pLevel->aRhs[i], pIgnore, iFirst, iLast) ){ return 1; } } return 0; } static int fsFreeBlock( FileSystem *pFS, Snapshot *pSnapshot, Segment *pIgnore, /* Ignore this run when searching */ int iBlk ){ int rc = LSM_OK; /* Return code */ int iFirst; /* First page on block iBlk */ int iLast; /* Last page on block iBlk */ int i; /* Used to iterate through append points */ Level *pLevel; /* Used to iterate through levels */ |
︙ | ︙ | |||
860 861 862 863 864 865 866 | /* ** Delete or otherwise recycle the blocks currently occupied by run pDel. */ int lsmFsSortedDelete( FileSystem *pFS, Snapshot *pSnapshot, | | | | 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 | /* ** Delete or otherwise recycle the blocks currently occupied by run pDel. */ int lsmFsSortedDelete( FileSystem *pFS, Snapshot *pSnapshot, int bZero, /* True to zero the Segment structure */ Segment *pDel ){ if( pDel->iFirst ){ int rc = LSM_OK; int iBlk; int iLastBlk; |
︙ | ︙ | |||
884 885 886 887 888 889 890 | }else if( bZero==0 && pDel->iLast!=fsLastPageOnBlock(pFS, iLastBlk) ){ break; } rc = fsFreeBlock(pFS, pSnapshot, pDel, iBlk); iBlk = iNext; } | | | | 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 | }else if( bZero==0 && pDel->iLast!=fsLastPageOnBlock(pFS, iLastBlk) ){ break; } rc = fsFreeBlock(pFS, pSnapshot, pDel, iBlk); iBlk = iNext; } if( bZero ) memset(pDel, 0, sizeof(Segment)); } return LSM_OK; } /* ** The pager reference passed as the only argument must refer to a sorted ** file page (not a log or meta page). This call indicates that the argument ** page is now the first page in its sorted file - all previous pages may ** be considered free. */ void lsmFsGobble( Snapshot *pSnapshot, Segment *pRun, Page *pPg ){ FileSystem *pFS = pPg->pFS; if( pPg->iPg!=pRun->iFirst ){ int rc = LSM_OK; int iBlk = fsPageToBlock(pFS, pRun->iFirst); |
︙ | ︙ | |||
946 947 948 949 950 951 952 | ** If the previous/next page does exist and is successfully loaded, *ppNext ** is set to point to it and LSM_OK is returned. Otherwise, if an error ** occurs, *ppNext is set to NULL and and lsm error code returned. ** ** Page references returned by this function should be released by the ** caller using lsmFsPageRelease(). */ | | | 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 | ** If the previous/next page does exist and is successfully loaded, *ppNext ** is set to point to it and LSM_OK is returned. Otherwise, if an error ** occurs, *ppNext is set to NULL and and lsm error code returned. ** ** Page references returned by this function should be released by the ** caller using lsmFsPageRelease(). */ int lsmFsDbPageNext(Segment *pRun, Page *pPg, int eDir, Page **ppNext){ FileSystem *pFS = pPg->pFS; int iPg = pPg->iPg; assert( eDir==1 || eDir==-1 ); if( eDir<0 ){ if( pRun && iPg==pRun->iFirst ){ |
︙ | ︙ | |||
1059 1060 1061 1062 1063 1064 1065 | assert( db->pWorker ); for(pLvl=lsmDbSnapshotLevel(db->pWorker); rc==LSM_OK && pLvl; pLvl=pLvl->pNext ){ if( pLvl->nRight==0 ){ | | < | < | < | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | < | < < | 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 | assert( db->pWorker ); for(pLvl=lsmDbSnapshotLevel(db->pWorker); rc==LSM_OK && pLvl; pLvl=pLvl->pNext ){ if( pLvl->nRight==0 ){ addAppendPoint(db, pLvl->lhs.iLast, &rc); }else{ int i; for(i=0; i<pLvl->nRight; i++){ addAppendPoint(db, pLvl->aRhs[i].iLast, &rc); } } } for(pLvl=lsmDbSnapshotLevel(db->pWorker); pLvl; pLvl=pLvl->pNext){ int i; subAppendPoint(db, pLvl->lhs.iFirst); for(i=0; i<pLvl->nRight; i++){ subAppendPoint(db, pLvl->aRhs[i].iFirst); } } return rc; } /* ** Append a page to file iFile. Return a reference to it. lsmFsPageWrite() ** has already been called on the returned reference. */ int lsmFsSortedAppend( FileSystem *pFS, Snapshot *pSnapshot, Segment *p, Page **ppOut ){ int rc = LSM_OK; Page *pPg = 0; *ppOut = 0; int iApp = 0; int iNext = 0; int iPrev = p->iLast; if( iPrev==0 ){ iApp = findAppendPoint(pFS, 0); }else if( fsIsLast(pFS, iPrev) ){ Page *pLast = 0; rc = fsPageGet(pFS, iPrev, 0, &pLast); if( rc!=LSM_OK ) return rc; iApp = lsmGetU32(&pLast->aData[pFS->nPagesize-4]); lsmFsPageRelease(pLast); }else{ iApp = iPrev + 1; } /* If this is the first page allocated, or if the page allocated is the ** last in the block, allocate a new block here. */ if( iApp==0 || fsIsLast(pFS, iApp) ){ int iNew; /* New block number */ lsmBlockAllocate(pFS->pDb, &iNew); if( iApp==0 ){ iApp = fsFirstPageOnBlock(pFS, iNew); }else{ iNext = fsFirstPageOnBlock(pFS, iNew); } } /* Grab the new page. */ pPg = 0; rc = fsPageGet(pFS, iApp, 1, &pPg); assert( rc==LSM_OK || pPg==0 ); /* If this is the first or last page of a block, fill in the pointer ** value at the end of the new page. */ if( rc==LSM_OK ){ p->nSize++; p->iLast = iApp; if( p->iFirst==0 ) p->iFirst = iApp; pPg->flags |= PAGE_DIRTY; if( fsIsLast(pFS, iApp) ){ lsmPutU32(&pPg->aData[pFS->nPagesize-4], iNext); }else if( fsIsFirst(pFS, iApp) ){ lsmPutU32(&pPg->aData[pFS->nPagesize-4], iPrev); } } *ppOut = pPg; return rc; } /* ** Mark the sorted run passed as the second argument as finished. */ int lsmFsSortedFinish(FileSystem *pFS, Segment *p){ int rc = LSM_OK; if( p ){ const int nPagePerBlock = (pFS->nBlocksize / pFS->nPagesize); /* Check if the last page of this run happens to be the last of a block. ** If it is, then an extra block has already been allocated for this run. ** Shift this extra block back to the free-block list. ** ** Otherwise, add the first free page in the last block used by the run ** to the lAppend list. */ |
︙ | ︙ | |||
1525 1526 1527 1528 1529 1530 1531 | *piParam = pFS->bUseMmap; return LSM_OK; } /* ** Helper function for lsmInfoArrayStructure(). */ | | | | < < | < | 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 | *piParam = pFS->bUseMmap; return LSM_OK; } /* ** Helper function for lsmInfoArrayStructure(). */ static Segment *startsWith(Segment *pRun, Pgno iFirst){ return (iFirst==pRun->iFirst) ? pRun : 0; } /* ** This function implements the lsm_info(LSM_INFO_ARRAY_STRUCTURE) request. ** If successful, *pzOut is set to point to a nul-terminated string ** containing the array structure and LSM_OK is returned. The caller should ** eventually free the string using lsmFree(). ** ** If an error occurs, *pzOut is set to NULL and an LSM error code returned. */ int lsmInfoArrayStructure(lsm_db *pDb, Pgno iFirst, char **pzOut){ int rc = LSM_OK; Snapshot *pWorker; /* Worker snapshot */ Snapshot *pRelease = 0; /* Snapshot to release */ Segment *pArray = 0; /* Array to report on */ Level *pLvl; /* Used to iterate through db levels */ *pzOut = 0; if( iFirst==0 ) return LSM_ERROR; /* Obtain the worker snapshot */ pWorker = pDb->pWorker; if( !pWorker ){ pRelease = pWorker = lsmDbSnapshotWorker(pDb); } /* Search for the array that starts on page iFirst */ for(pLvl=lsmDbSnapshotLevel(pWorker); pLvl && pArray==0; pLvl=pLvl->pNext){ if( 0==(pArray = startsWith(&pLvl->lhs, iFirst)) ){ int i; for(i=0; i<pLvl->nRight; i++){ if( (pArray = startsWith(&pLvl->aRhs[i], iFirst)) ) break; } } } if( pArray==0 ){ /* Could not find the requested array. This is an error. */ *pzOut = 0; |
︙ | ︙ | |||
1595 1596 1597 1598 1599 1600 1601 | *pzOut = str.z; } lsmDbSnapshotRelease(pDb->pEnv, pRelease); return rc; } | < < < < < < < < < < < < < < < < < < < < < < < < < < < < | | 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 | *pzOut = str.z; } lsmDbSnapshotRelease(pDb->pEnv, pRelease); return rc; } #ifdef LSM_EXPENSIVE_DEBUG /* ** Helper function for lsmFsIntegrityCheck() */ static void checkBlocks( FileSystem *pFS, Segment *pSeg, int bExtra, u8 *aUsed ){ if( pSeg ){ int i; for(i=0; i<2; i++){ Segment *p = (i ? pSeg->pRun : pSeg->pSep); if( p && p->nSize>0 ){ const int nPagePerBlock = (pFS->nBlocksize / pFS->nPagesize); int iBlk; int iLastBlk; iBlk = fsPageToBlock(pFS, p->iFirst); |
︙ | ︙ |
Changes to src/lsm_main.c.
︙ | ︙ | |||
413 414 415 416 417 418 419 | va_end(ap); return rc; } void lsmAppendSegmentList(LsmString *pStr, char *zPre, Segment *pSeg){ lsmStringAppendf(pStr, "%s{%d %d %d %d %d %d}", zPre, | | | | 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 | va_end(ap); return rc; } void lsmAppendSegmentList(LsmString *pStr, char *zPre, Segment *pSeg){ lsmStringAppendf(pStr, "%s{%d %d %d %d %d %d}", zPre, 0, 0, 0, pSeg->iFirst, pSeg->iLast, pSeg->nSize ); } int lsmStructList( lsm_db *pDb, /* Database handle */ char **pzOut /* OUT: Nul-terminated string (tcl list) */ ){ |
︙ | ︙ |
Changes to src/lsm_sorted.c.
︙ | ︙ | |||
142 143 144 145 146 147 148 | ** ** * To iterate and/or seek within a single Segment (the combination of a ** main run and an optional sorted run). ** ** * To iterate through the separators array of a segment. */ struct SegmentPtr { | | < | 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | ** ** * To iterate and/or seek within a single Segment (the combination of a ** main run and an optional sorted run). ** ** * To iterate through the separators array of a segment. */ struct SegmentPtr { Segment *pSeg; /* Segment to access */ /* Current page. See segmentPtrLoadPage(). */ Page *pPg; /* Current page */ u16 flags; /* Copy of page flags field */ int nCell; /* Number of cells on pPg */ int iPtr; /* Base cascade pointer */ |
︙ | ︙ | |||
210 211 212 213 214 215 216 | typedef struct BtreePg BtreePg; typedef struct BtreeCursor BtreeCursor; struct BtreePg { Page *pPage; int iCell; }; struct BtreeCursor { | | | 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | typedef struct BtreePg BtreePg; typedef struct BtreeCursor BtreeCursor; struct BtreePg { Page *pPage; int iCell; }; struct BtreeCursor { Segment *pSeg; /* Iterate through this segments btree */ FileSystem *pFS; /* File system to read pages from */ int nDepth; /* Allocated size of aPg[] */ int iPg; /* Current entry in aPg[]. -1 -> EOF. */ BtreePg *aPg; /* Pages from root to current location */ /* Cache of current entry. pKey==0 for EOF. */ void *pKey; |
︙ | ︙ | |||
317 318 319 320 321 322 323 | }; struct MergeWorker { lsm_db *pDb; /* Database handle */ Level *pLevel; /* Worker snapshot Level being merged */ MultiCursor *pCsr; /* Cursor to read new segment contents from */ int bFlush; /* True if this is an in-memory tree flush */ | | | | | | 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | }; struct MergeWorker { lsm_db *pDb; /* Database handle */ Level *pLevel; /* Worker snapshot Level being merged */ MultiCursor *pCsr; /* Cursor to read new segment contents from */ int bFlush; /* True if this is an in-memory tree flush */ Hierarchy hier; /* B-tree hierarchy under construction */ Page *pPage; /* Current output page */ int nWork; /* Number of calls to mergeWorkerNextPage() */ }; #ifdef LSM_DEBUG_EXPENSIVE static int assertPointersOk(lsm_db *, Segment *, Segment *, int); static int assertBtreeOk(lsm_db *, Segment *); #endif struct FilePage { u8 *aData; int nData; }; static u8 *fsPageData(Page *pPg, int *pnData){ *pnData = ((struct FilePage *)(pPg))->nData; return ((struct FilePage *)(pPg))->aData; } |
︙ | ︙ | |||
703 704 705 706 707 708 709 | } static int btreeCursorFirst(BtreeCursor *pCsr){ int rc; Page *pPg = 0; FileSystem *pFS = pCsr->pFS; | | | 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 | } static int btreeCursorFirst(BtreeCursor *pCsr){ int rc; Page *pPg = 0; FileSystem *pFS = pCsr->pFS; int iPg = pCsr->pSeg->iRoot; do { rc = lsmFsDbPageGet(pFS, iPg, &pPg); assert( (rc==LSM_OK)==(pPg!=0) ); if( rc==LSM_OK ){ u8 *aData; int nData; |
︙ | ︙ | |||
807 808 809 810 811 812 813 | Blob blob = {0,0,0}; void *pSeek; int nSeek; int iTopicSeek; int dummy; int iPg = 0; | | | 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 | Blob blob = {0,0,0}; void *pSeek; int nSeek; int iTopicSeek; int dummy; int iPg = 0; int iLoad = pCsr->pSeg->iRoot; rc = pageGetBtreeKey(pCsr->aPg[nDepth-1].pPage, 0, &dummy, &iTopicSeek, &pSeek, &nSeek, &pCsr->blob ); do { Page *pPg; |
︙ | ︙ | |||
897 898 899 900 901 902 903 | } } return rc; } static int btreeCursorNew( lsm_db *pDb, | | | | | 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 | } } return rc; } static int btreeCursorNew( lsm_db *pDb, Segment *pSeg, BtreeCursor **ppCsr ){ int rc = LSM_OK; BtreeCursor *pCsr; assert( pSeg->iRoot ); pCsr = lsmMallocZeroRc(pDb->pEnv, sizeof(BtreeCursor), &rc); if( pCsr ){ pCsr->pFS = pDb->pFS; pCsr->pSeg = pSeg; pCsr->iPg = -1; } *ppCsr = pCsr; return rc; } |
︙ | ︙ | |||
938 939 940 941 942 943 944 | FileSystem *pFS, SegmentPtr *pPtr, /* Load page into this SegmentPtr object */ int iNew /* Page number of new page */ ){ Page *pPg = 0; /* The new page */ int rc; /* Return Code */ | < < < < | 937 938 939 940 941 942 943 944 945 946 947 948 949 950 | FileSystem *pFS, SegmentPtr *pPtr, /* Load page into this SegmentPtr object */ int iNew /* Page number of new page */ ){ Page *pPg = 0; /* The new page */ int rc; /* Return Code */ rc = lsmFsDbPageGet(pFS, iNew, &pPg); assert( rc==LSM_OK || pPg==0 ); segmentPtrSetPage(pPtr, pPg); return rc; } |
︙ | ︙ | |||
968 969 970 971 972 973 974 | int eDir /* +1 for next(), -1 for prev() */ ){ Page *pNext; /* New page to load */ int rc; /* Return code */ assert( eDir==1 || eDir==-1 ); assert( pPtr->pPg ); | < | < < | | 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 | int eDir /* +1 for next(), -1 for prev() */ ){ Page *pNext; /* New page to load */ int rc; /* Return code */ assert( eDir==1 || eDir==-1 ); assert( pPtr->pPg ); assert( pPtr->pSeg || eDir>0 ); rc = lsmFsDbPageNext(pPtr->pSeg, pPtr->pPg, eDir, &pNext); assert( rc==LSM_OK || pNext==0 ); segmentPtrSetPage(pPtr, pNext); return rc; } static int segmentPtrLoadCell( SegmentPtr *pPtr, /* Load page into this SegmentPtr object */ |
︙ | ︙ | |||
1027 1028 1029 1030 1031 1032 1033 | int i; Merge *pMerge = pLevel->pMerge; for(i=0; rc==LSM_OK && i<pLevel->nRight; i++){ Page *pPg = 0; int iTopic; Blob blob = {0, 0, 0, 0}; | < | < | 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 | int i; Merge *pMerge = pLevel->pMerge; for(i=0; rc==LSM_OK && i<pLevel->nRight; i++){ Page *pPg = 0; int iTopic; Blob blob = {0, 0, 0, 0}; assert( pLevel->aRhs[i].iFirst!=0 ); rc = lsmFsDbPageGet(pDb->pFS, pMerge->aInput[i].iPg, &pPg); if( rc==LSM_OK ){ rc = pageGetKeyCopy(pEnv, pPg, pMerge->aInput[i].iCell, &iTopic, &blob); } if( rc==LSM_OK ){ int res = -1; if( pLevel->pSplitKey ){ |
︙ | ︙ | |||
1088 1089 1090 1091 1092 1093 1094 | int i; pCsr->aPtr[0].pSeg = &pLevel->lhs; pCsr->nPtr = nPtr; for(i=0; i<pLevel->nRight; i++){ pCsr->aPtr[i+1].pSeg = &pLevel->aRhs[i]; } | < < < | | | 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 | int i; pCsr->aPtr[0].pSeg = &pLevel->lhs; pCsr->nPtr = nPtr; for(i=0; i<pLevel->nRight; i++){ pCsr->aPtr[i+1].pSeg = &pLevel->aRhs[i]; } } return rc; } static int levelCursorInitRun( lsm_db *pDb, Segment *pSeg, int (*xCmp)(void *, int, void *, int), LevelCursor *pCsr /* Cursor structure to initialize */ ){ int rc = LSM_OK; memset(pCsr, 0, sizeof(LevelCursor)); pCsr->pFS = pDb->pFS; pCsr->bIgnoreSeparators = 1; pCsr->xCmp = xCmp; pCsr->nPtr = 1; pCsr->aPtr = (SegmentPtr*)lsmMallocZeroRc(pDb->pEnv, sizeof(SegmentPtr)*pCsr->nPtr, &rc ); if( rc==LSM_OK ){ pCsr->aPtr[0].pSeg = pSeg; } return rc; } static void segmentPtrReset(SegmentPtr *pPtr){ lsmFsPageRelease(pPtr->pPg); |
︙ | ︙ | |||
1195 1196 1197 1198 1199 1200 1201 | FileSystem *pFS, SegmentPtr *pPtr, int bLast, int *pRc ){ if( *pRc==LSM_OK ){ Page *pNew = 0; | | | 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 | FileSystem *pFS, SegmentPtr *pPtr, int bLast, int *pRc ){ if( *pRc==LSM_OK ){ Page *pNew = 0; Pgno iPg = (bLast ? pPtr->pSeg->iLast : pPtr->pSeg->iFirst); *pRc = lsmFsDbPageGet(pFS, iPg, &pNew); segmentPtrSetPage(pPtr, pNew); } } /* ** Try to move the segment pointer passed as the second argument so that it |
︙ | ︙ | |||
1303 1304 1305 1306 1307 1308 1309 | for(eDir=-1; eDir<=1; eDir+=2){ Page *pTest = pPtr->pPg; lsmFsPageRef(pTest); while( pTest ){ Page *pNext; | | | 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 | for(eDir=-1; eDir<=1; eDir+=2){ Page *pTest = pPtr->pPg; lsmFsPageRef(pTest); while( pTest ){ Page *pNext; int rc = lsmFsDbPageNext(pPtr->pSeg, pTest, eDir, &pNext); lsmFsPageRelease(pTest); pTest = pNext; assert( rc==LSM_OK ); if( pTest ){ int nData; u8 *aData = fsPageData(pTest, &nData); |
︙ | ︙ | |||
1386 1387 1388 1389 1390 1391 1392 | ** page. */ res = iLastTopic - iTopic; if( res==0 ) res = pCsr->xCmp(pLastKey, nLastKey, pKey, nKey); if( res>=0 ) break; /* Advance to the next page that contains at least one key. */ do { | | | 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 | ** page. */ res = iLastTopic - iTopic; if( res==0 ) res = pCsr->xCmp(pLastKey, nLastKey, pKey, nKey); if( res>=0 ) break; /* Advance to the next page that contains at least one key. */ do { rc = lsmFsDbPageNext(pPtr->pSeg, pPtr->pPg, 1, &pNext); if( pNext==0 ) break; assert( rc==LSM_OK ); segmentPtrSetPage(pPtr, pNext); }while( (pPtr->nCell==0 || (pPtr->flags & SEGMENT_BTREE_FLAG)) ); if( pNext==0 ) break; /* This should probably be an LSM_CORRUPT error. */ |
︙ | ︙ | |||
1409 1410 1411 1412 1413 1414 1415 | ** loading (iPg+1) and testing that pKey/nKey is smaller than all ** the keys it houses. */ #if 1 assert( assertKeyLocation(pCsr, pPtr, pKey, nKey) ); #endif assert( pPtr->nCell>0 | | | | 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 | ** loading (iPg+1) and testing that pKey/nKey is smaller than all ** the keys it houses. */ #if 1 assert( assertKeyLocation(pCsr, pPtr, pKey, nKey) ); #endif assert( pPtr->nCell>0 || pPtr->pSeg->nSize==1 || lsmFsPageNumber(pPtr->pPg)==pPtr->pSeg->iLast ); if( pPtr->nCell==0 ){ segmentPtrReset(pPtr); }else{ iMin = 0; iMax = pPtr->nCell-1; |
︙ | ︙ | |||
1523 1524 1525 1526 1527 1528 1529 | } pCsr->iCurrentPtr = iBest; } static int seekInBtree( LevelCursor *pCsr, | | | | 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 | } pCsr->iCurrentPtr = iBest; } static int seekInBtree( LevelCursor *pCsr, Segment *pSeg, void *pKey, int nKey, /* Key to seek to */ Page **ppPg /* OUT: Leaf (sorted-run) page reference */ ){ int rc; int iPg; Page *pPg = 0; Blob blob = {0, 0, 0}; int iTopic = 0; /* TODO: Fix me */ iPg = pSeg->iRoot; do { rc = lsmFsDbPageGet(pCsr->pFS, iPg, &pPg); assert( rc==LSM_OK || pPg==0 ); if( rc==LSM_OK ){ u8 *aData; /* Buffer containing page data */ int nData; /* Size of aData[] in bytes */ int iMin; |
︙ | ︙ | |||
1585 1586 1587 1588 1589 1590 1591 | sortedBlobFree(&blob); assert( (rc==LSM_OK)==(pPg!=0) ); *ppPg = pPg; return rc; } | < < < < < < < < < < < < < < < < < < < < < < < < < < | | | | | 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 | sortedBlobFree(&blob); assert( (rc==LSM_OK)==(pPg!=0) ); *ppPg = pPg; return rc; } static int seekInSegment( LevelCursor *pCsr, SegmentPtr *pPtr, void *pKey, int nKey, int iPg, /* Page to search */ int eSeek, /* Search bias - see above */ int *piPtr /* OUT: FC pointer */ ){ int iPtr = iPg; int rc = LSM_OK; if( pPtr->pSeg->iRoot ){ Page *pPg; assert( pPtr->pSeg->iRoot!=0 ); rc = seekInBtree(pCsr, pPtr->pSeg, pKey, nKey, &pPg); if( rc==LSM_OK ) segmentPtrSetPage(pPtr, pPg); }else{ if( iPtr==0 ){ iPtr = pPtr->pSeg->iFirst; } if( rc==LSM_OK ){ rc = segmentPtrLoadPage(pCsr->pFS, pPtr, iPtr); } } if( rc==LSM_OK ){ |
︙ | ︙ | |||
1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 | *pp = pCsr->pNext; break; } } /* Free the allocation used to cache the current key, if any. */ sortedBlobFree(&pCsr->key); /* Free the component cursors */ mcursorFreeComponents(pCsr); /* Free the cursor structure itself */ lsmFree(pDb->pEnv, pCsr); } | > | 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 | *pp = pCsr->pNext; break; } } /* Free the allocation used to cache the current key, if any. */ sortedBlobFree(&pCsr->key); sortedBlobFree(&pCsr->val); /* Free the component cursors */ mcursorFreeComponents(pCsr); /* Free the cursor structure itself */ lsmFree(pDb->pEnv, pCsr); } |
︙ | ︙ | |||
1879 1880 1881 1882 1883 1884 1885 | assert( eMode==MULTICURSOR_ADDLEVEL_ALL || eMode==MULTICURSOR_ADDLEVEL_RHS || eMode==MULTICURSOR_ADDLEVEL_LHS_SEP ); if( eMode==MULTICURSOR_ADDLEVEL_LHS_SEP ){ | | | | 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 | assert( eMode==MULTICURSOR_ADDLEVEL_ALL || eMode==MULTICURSOR_ADDLEVEL_RHS || eMode==MULTICURSOR_ADDLEVEL_LHS_SEP ); if( eMode==MULTICURSOR_ADDLEVEL_LHS_SEP ){ assert( pLevel->lhs.iRoot ); assert( pCsr->pBtCsr==0 ); rc = btreeCursorNew(pCsr->pDb, &pLevel->lhs, &pCsr->pBtCsr); assert( (rc==LSM_OK)==(pCsr->pBtCsr!=0) ); }else{ int i; int nAdd = (eMode==MULTICURSOR_ADDLEVEL_RHS ? pLevel->nRight : 1); for(i=0; i<nAdd; i++){ LevelCursor *pNew; |
︙ | ︙ | |||
1909 1910 1911 1912 1913 1914 1915 | switch( eMode ){ case MULTICURSOR_ADDLEVEL_ALL: rc = levelCursorInit(pDb, pLevel, pCsr->xCmp, pNew); break; case MULTICURSOR_ADDLEVEL_RHS: | | < < < < | 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 | switch( eMode ){ case MULTICURSOR_ADDLEVEL_ALL: rc = levelCursorInit(pDb, pLevel, pCsr->xCmp, pNew); break; case MULTICURSOR_ADDLEVEL_RHS: rc = levelCursorInitRun(pDb, &pLevel->aRhs[i], pCsr->xCmp, pNew); break; } if( pCsr->flags & CURSOR_IGNORE_SYSTEM ){ pNew->bIgnoreSystem = 1; } if( rc==LSM_OK ) pCsr->nSegCsr++; } |
︙ | ︙ | |||
2667 2668 2669 2670 2671 2672 2673 | ** to the way the file.c module works some (the first and last in each block) ** are 4 bytes smaller than the others. */ static int mergeWorkerMoveHierarchy( MergeWorker *pMW, /* Merge worker */ int bSep /* True for separators run */ ){ | | | | | | | | 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 | ** to the way the file.c module works some (the first and last in each block) ** are 4 bytes smaller than the others. */ static int mergeWorkerMoveHierarchy( MergeWorker *pMW, /* Merge worker */ int bSep /* True for separators run */ ){ Segment *pSeg; /* Segment being written */ lsm_db *pDb = pMW->pDb; /* Database handle */ int rc = LSM_OK; /* Return code */ int i; int iRight = 0; Page **apHier = pMW->hier.apHier; int nHier = pMW->hier.nHier; assert( nHier>0 && pMW->pLevel->pMerge->bHierReadonly ); pSeg = &pMW->pLevel->lhs; for(i=0; rc==LSM_OK && i<nHier; i++){ Page *pNew = 0; rc = lsmFsSortedAppend(pDb->pFS, pDb->pWorker, pSeg, &pNew); assert( rc==LSM_OK ); if( rc==LSM_OK ){ u8 *a1; int n1; u8 *a2; int n2; a1 = fsPageData(pNew, &n1); |
︙ | ︙ | |||
2723 2724 2725 2726 2727 2728 2729 | #ifdef LSM_DEBUG if( rc==LSM_OK ){ for(i=0; i<nHier; i++) assert( lsmFsPageWritable(apHier[i]) ); } #endif if( rc==LSM_OK ){ | | | < < < | | | | | | | 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 | #ifdef LSM_DEBUG if( rc==LSM_OK ){ for(i=0; i<nHier; i++) assert( lsmFsPageWritable(apHier[i]) ); } #endif if( rc==LSM_OK ){ pMW->pLevel->pMerge->bHierReadonly = 0; } return rc; } /* ** Allocate and populate the MergeWorker.apHier[] array. */ static int mergeWorkerLoadHierarchy(MergeWorker *pMW){ int rc = LSM_OK; Segment *pSeg; Hierarchy *p; pSeg = &pMW->pLevel->lhs; p = &pMW->hier; if( p->apHier==0 && pSeg->iRoot!=0 ){ int bHierReadonly = pMW->pLevel->pMerge->bHierReadonly; FileSystem *pFS = pMW->pDb->pFS; lsm_env *pEnv = pMW->pDb->pEnv; Page **apHier = 0; int nHier = 0; int iPg = pSeg->iRoot; do { Page *pPg = 0; u8 *aData; int nData; int flags; |
︙ | ︙ | |||
2837 2838 2839 2840 2841 2842 2843 | ** array page that contains the main record is added to the b-tree instead. ** In this case the record format is: ** ** + 0x00 byte (1 byte) ** + Absolute pointer value (varint), ** + Absolute page number of page containing key (varint). ** | | | | | | | | | 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 | ** array page that contains the main record is added to the b-tree instead. ** In this case the record format is: ** ** + 0x00 byte (1 byte) ** + Absolute pointer value (varint), ** + Absolute page number of page containing key (varint). ** ** See function seekInBtree() for the code that traverses b-tree pages. */ static int mergeWorkerPushHierarchy( MergeWorker *pMW, /* Merge worker object */ int bSep, /* True for separators, false otherwise */ Pgno iKeyPg, /* Page that will contain pKey/nKey */ int iTopic, /* Topic value for this key */ void *pKey, /* Pointer to key buffer */ int nKey /* Size of pKey buffer in bytes */ ){ lsm_db *pDb = pMW->pDb; /* Database handle */ int rc; /* Return Code */ int iLevel; /* Level of b-tree hierachy to write to */ int nData; /* Size of aData[] in bytes */ u8 *aData; /* Page data for level iLevel */ int iOff; /* Offset on b-tree page to write record to */ int nRec; /* Initial number of records on b-tree page */ Pgno iPtr; /* Pointer value to accompany pKey/nKey */ int bIndirect; /* True to use an indirect record */ Hierarchy *p; Segment *pSeg; /* If there exists a b-tree hierarchy and it is not loaded into ** memory, load it now. */ pSeg = &pMW->pLevel->lhs; p = &pMW->hier; rc = mergeWorkerLoadHierarchy(pMW); /* Obtain the absolute pointer value to store along with the key in the ** page body. This pointer points to a page that contains keys that are ** smaller than pKey/nKey. */ if( p->nHier ){ aData = fsPageData(p->apHier[0], &nData); iPtr = lsmGetU32(&aData[SEGMENT_POINTER_OFFSET(nData)]); }else{ iPtr = pSeg->iFirst; } if( p->nHier && pMW->pLevel->pMerge->bHierReadonly ){ rc = mergeWorkerMoveHierarchy(pMW, bSep); if( rc!=LSM_OK ) goto push_hierarchy_out; } /* Determine if the indirect format should be used. */ bIndirect = (nKey*4 > lsmFsPageSize(pMW->pDb->pFS)); |
︙ | ︙ | |||
2933 2934 2935 2936 2937 2938 2939 | rc = lsmFsPageRelease(p->apHier[iLevel]); } /* Allocate a new page for apHier[iLevel]. */ p->apHier[iLevel] = 0; if( rc==LSM_OK ){ rc = lsmFsSortedAppend( | | | 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 | rc = lsmFsPageRelease(p->apHier[iLevel]); } /* Allocate a new page for apHier[iLevel]. */ p->apHier[iLevel] = 0; if( rc==LSM_OK ){ rc = lsmFsSortedAppend( pDb->pFS, pDb->pWorker, pSeg, &p->apHier[iLevel] ); } if( rc!=LSM_OK ) goto push_hierarchy_out; aData = fsPageData(p->apHier[iLevel], &nData); memset(aData, 0, nData); lsmPutU16(&aData[SEGMENT_FLAGS_OFFSET(nData)], SEGMENT_BTREE_FLAG); |
︙ | ︙ | |||
2984 2985 2986 2987 2988 2989 2990 | /* Write the right-hand pointer of the right-most leaf page of the ** b-tree heirarchy. */ aData = fsPageData(p->apHier[0], &nData); lsmPutU32(&aData[SEGMENT_POINTER_OFFSET(nData)], iKeyPg); /* Ensure that the SortedRun.iRoot field is correct. */ | | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | < | < < | | | | | | | | 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 | /* Write the right-hand pointer of the right-most leaf page of the ** b-tree heirarchy. */ aData = fsPageData(p->apHier[0], &nData); lsmPutU32(&aData[SEGMENT_POINTER_OFFSET(nData)], iKeyPg); /* Ensure that the SortedRun.iRoot field is correct. */ pSeg->iRoot = lsmFsPageNumber(p->apHier[p->nHier-1]); push_hierarchy_out: return rc; } static int keyszToSkip(FileSystem *pFS, int nKey){ int nPgsz; /* Nominal database page size */ nPgsz = lsmFsPageSize(pFS); return LSM_MIN(((nKey * 4) / nPgsz), 3); } /* ** Advance to the next page of an output run being populated by merge-worker ** pMW. The footer of the new page is initialized to indicate that it contains ** zero records. The flags field is cleared. The page footer pointer field ** is set to iFPtr. ** ** If successful, LSM_OK is returned. Otherwise, an error code. */ static int mergeWorkerNextPage( MergeWorker *pMW, /* Merge worker object to append page to */ int iFPtr /* Pointer value for footer of new page */ ){ int rc = LSM_OK; /* Return code */ Page *pNext = 0; /* New page appended to run */ lsm_db *pDb = pMW->pDb; /* Database handle */ Segment *pSeg; /* Run to append to */ pSeg = &pMW->pLevel->lhs; rc = lsmFsSortedAppend(pDb->pFS, pDb->pWorker, pSeg, &pNext); assert( rc!=LSM_OK || pSeg->iFirst>0 ); if( rc==LSM_OK ){ u8 *aData; /* Data buffer belonging to page pNext */ int nData; /* Size of aData[] in bytes */ lsmFsPageRelease(pMW->pPage); pMW->pPage = pNext; pMW->pLevel->pMerge->iOutputOff = 0; aData = fsPageData(pNext, &nData); lsmPutU16(&aData[SEGMENT_NRECORD_OFFSET(nData)], 0); lsmPutU16(&aData[SEGMENT_FLAGS_OFFSET(nData)], 0); lsmPutU32(&aData[SEGMENT_POINTER_OFFSET(nData)], iFPtr); pMW->nWork++; } return rc; } /* ** Write a blob of data into an output segment being populated by a |
︙ | ︙ | |||
3134 3135 3136 3137 3138 3139 3140 | Merge *pMerge = pMW->pLevel->pMerge; int nCopy; /* Number of bytes to copy */ u8 *aData; /* Pointer to buffer of current output page */ int nData; /* Size of aData[] in bytes */ int nRec; /* Number of records on current output page */ int iOff; /* Offset in aData[] to write to */ | | | | | | < | < < < | | | 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 | Merge *pMerge = pMW->pLevel->pMerge; int nCopy; /* Number of bytes to copy */ u8 *aData; /* Pointer to buffer of current output page */ int nData; /* Size of aData[] in bytes */ int nRec; /* Number of records on current output page */ int iOff; /* Offset in aData[] to write to */ assert( lsmFsPageWritable(pMW->pPage) ); aData = fsPageData(pMW->pPage, &nData); nRec = pageGetNRec(aData, nData); iOff = pMerge->iOutputOff; nCopy = LSM_MIN(nRem, SEGMENT_EOF(nData, nRec) - iOff); memcpy(&aData[iOff], &aWrite[nWrite-nRem], nCopy); nRem -= nCopy; if( nRem>0 ){ rc = mergeWorkerNextPage(pMW, iFPtr); }else{ pMerge->iOutputOff = iOff + nCopy; } } return rc; } static int mergeWorkerWrite( MergeWorker *pMW, /* Merge worker object to write into */ int eType, /* One of SORTED_SEPARATOR, WRITE or DELETE */ void *pKey, int nKey, /* Key value */ MultiCursor *pCsr, /* Read value (if any) from here */ int iPtr, /* Absolute value of page pointer, or 0 */ int *piPtrOut /* OUT: Pointer to write to separators */ ){ int rc = LSM_OK; /* Return code */ Merge *pMerge; /* Persistent part of level merge state */ int nHdr; /* Space required for this record header */ Page *pPg; /* Page to write to */ u8 *aData; /* Data buffer for page pWriter->pPage */ int nData; /* Size of buffer aData[] in bytes */ int nRec; /* Number of records on page pPg */ int iFPtr; /* Value of pointer in footer of pPg */ int iRPtr; /* Value of pointer written into record */ int iOff; /* Current write offset within page pPg */ Segment *pSeg; /* Segment being written */ int flags = 0; /* If != 0, flags value for page footer */ void *pVal; int nVal; pMerge = pMW->pLevel->pMerge; pSeg = &pMW->pLevel->lhs; pPg = pMW->pPage; aData = fsPageData(pPg, &nData); nRec = pageGetNRec(aData, nData); iFPtr = pageGetPtr(aData, nData); /* If iPtr is 0, set it to the same value as the absolute pointer ** stored as part of the previous record. */ if( iPtr==0 ){ |
︙ | ︙ | |||
3222 3223 3224 3225 3226 3227 3228 | rc = lsmMCursorValue(pCsr, &pVal, &nVal); if( rc==LSM_OK ){ nHdr = 1 + lsmVarintLen32(iRPtr) + lsmVarintLen32(nKey); if( rtIsWrite(eType) ) nHdr += lsmVarintLen32(nVal); /* If the entire header will not fit on page pPg, or if page pPg is ** marked read-only, advance to the next page of the output run. */ | | | | | | | < | | | | | | | | < | | | | 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 | rc = lsmMCursorValue(pCsr, &pVal, &nVal); if( rc==LSM_OK ){ nHdr = 1 + lsmVarintLen32(iRPtr) + lsmVarintLen32(nKey); if( rtIsWrite(eType) ) nHdr += lsmVarintLen32(nVal); /* If the entire header will not fit on page pPg, or if page pPg is ** marked read-only, advance to the next page of the output run. */ iOff = pMerge->iOutputOff; if( iOff<0 || iOff+nHdr > SEGMENT_EOF(nData, nRec+1) ){ iFPtr = iFPtr + (nRec ? pageGetRecordPtr(aData, nData, nRec-1) : 0); iRPtr = iPtr - iFPtr; iOff = 0; nRec = 0; rc = mergeWorkerNextPage(pMW, iFPtr); pPg = pMW->pPage; } } /* If this record header will be the first on the page, and the page is ** not the very first in the entire run, special actions may need to be ** taken: ** ** * If currently writing the main run, *piPtrOut should be set to ** the current page number. The caller will add a key to the separators ** array that points to the current page. ** ** * If currently writing the separators array, push a copy of the key ** into the b-tree hierarchy. */ if( rc==LSM_OK && nRec==0 && pSeg->iFirst!=pSeg->iLast ){ assert( pMerge->nSkip>=0 ); if( pMerge->nSkip==0 ){ Pgno iPg = lsmFsPageNumber(pPg); rc = mergeWorkerPushHierarchy(pMW, 0, iPg, rtTopic(eType), pKey, nKey); } if( pMerge->nSkip ){ pMerge->nSkip--; flags = PGFTR_SKIP_THIS_FLAG; }else{ *piPtrOut = lsmFsPageNumber(pPg); pMerge->nSkip = keyszToSkip(pMW->pDb->pFS, nKey); } if( pMerge->nSkip ) flags |= PGFTR_SKIP_NEXT_FLAG; } /* Update the output segment */ if( rc==LSM_OK ){ aData = fsPageData(pPg, &nData); /* Update the page footer. */ lsmPutU16(&aData[SEGMENT_NRECORD_OFFSET(nData)], nRec+1); lsmPutU16(&aData[SEGMENT_CELLPTR_OFFSET(nData, nRec)], iOff); if( flags ) lsmPutU16(&aData[SEGMENT_FLAGS_OFFSET(nData)], flags); /* Write the entry header into the current page. */ aData[iOff++] = eType; /* 1 */ iOff += lsmVarintPut32(&aData[iOff], iRPtr); /* 2 */ iOff += lsmVarintPut32(&aData[iOff], nKey); /* 3 */ if( rtIsWrite(eType) ) iOff += lsmVarintPut32(&aData[iOff], nVal); /* 4 */ pMerge->iOutputOff = iOff; /* Write the key and data into the segment. */ assert( iFPtr==pageGetPtr(aData, nData) ); rc = mergeWorkerData(pMW, 0, iFPtr+iRPtr, pKey, nKey); if( rc==LSM_OK && rtIsWrite(eType) ){ if( rtTopic(eType)==0 ) rc = lsmMCursorValue(pCsr, &pVal, &nVal); if( rc==LSM_OK ){ rc = mergeWorkerData(pMW, 0, iFPtr+iRPtr, pVal, nVal); } } } return rc; } |
︙ | ︙ | |||
3346 3347 3348 3349 3350 3351 3352 | if( bBtree && pMerge->nInput ){ assert( i==pCsr->nSegCsr ); btreeCursorPosition(pCsr->pBtCsr, &pMerge->aInput[i]); } } lsmMCursorClose(pCsr); | | | | | | < | | | > | | | | 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 | if( bBtree && pMerge->nInput ){ assert( i==pCsr->nSegCsr ); btreeCursorPosition(pCsr->pBtCsr, &pMerge->aInput[i]); } } lsmMCursorClose(pCsr); lsmFsPageRelease(pMW->pPage); lsmFsPageRelease(pMW->pPage); for(i=0; i<2; i++){ Hierarchy *p = &pMW->hier; int iPg; for(iPg=0; iPg<p->nHier; iPg++){ lsmFsPageRelease(p->apHier[iPg]); } lsmFree(pMW->pDb->pEnv, p->apHier); p->apHier = 0; p->nHier = 0; } pMW->pCsr = 0; pMW->pPage = 0; pMW->pPage = 0; } static int mergeWorkerFirstPage(MergeWorker *pMW){ int rc; /* Return code */ Page *pPg = 0; /* First page of run pSeg */ int iFPtr; /* Pointer value read from footer of pPg */ MultiCursor *pCsr = pMW->pCsr; assert( pMW->pPage==0 ); if( pCsr->pBtCsr ){ rc = LSM_OK; iFPtr = pMW->pLevel->pNext->lhs.iFirst; }else{ Segment *pSeg; pSeg = pMW->pCsr->aSegCsr[pMW->pCsr->nSegCsr-1].aPtr[0].pSeg; rc = lsmFsDbPageGet(pMW->pDb->pFS, pSeg->iFirst, &pPg); if( rc==LSM_OK ){ u8 *aData; /* Buffer for page pPg */ int nData; /* Size of aData[] in bytes */ aData = fsPageData(pPg, &nData); iFPtr = pageGetPtr(aData, nData); lsmFsPageRelease(pPg); } } if( rc==LSM_OK ){ rc = mergeWorkerNextPage(pMW, iFPtr); } return rc; } static int mergeWorkerStep(MergeWorker *pMW){ lsm_db *pDb = pMW->pDb; /* Database handle */ |
︙ | ︙ | |||
3442 3443 3444 3445 3446 3447 3448 | /* If this is a separator key and we know that the output pointer has not ** changed, there is no point in writing an output record. Otherwise, ** proceed. */ if( rtIsSeparator(eType)==0 || iPtr!=0 ){ int iSPtr = 0; /* Separators require a pointer here */ | | | < < < < < < < < < < < < < < < < < < < < < < < < < < | | | < < < < < < | | | | 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 | /* If this is a separator key and we know that the output pointer has not ** changed, there is no point in writing an output record. Otherwise, ** proceed. */ if( rtIsSeparator(eType)==0 || iPtr!=0 ){ int iSPtr = 0; /* Separators require a pointer here */ if( pMW->pPage==0 ){ rc = mergeWorkerFirstPage(pMW); } /* Write the record into the main run. */ if( rc==LSM_OK ){ rc = mergeWorkerWrite(pMW, eType, pKey, nKey, pCsr, iPtr, &iSPtr); } } /* Advance the cursor to the next input record (assuming one exists). */ assert( lsmMCursorValid(pMW->pCsr) ); if( rc==LSM_OK ) rc = lsmMCursorNext(pMW->pCsr); assert( pMW->pPage==0 ); /* If the cursor is at EOF, the merge is finished. Release all page ** references currently held by the merge worker and inform the ** FileSystem object that no further pages will be appended to either ** the main or separators array. */ if( rc==LSM_OK && !lsmMCursorValid(pMW->pCsr) ){ if( pSeg->iFirst ){ rc = lsmFsSortedFinish(pDb->pFS, pSeg); } #ifdef LSM_DEBUG_EXPENSIVE if( rc==LSM_OK ){ rc = assertBtreeOk(pDb, pSeg); if( pMW->pCsr->pBtCsr ){ Segment *pNext = &pMW->pLevel->pNext->lhs; rc = assertPointersOk(pDb, pSeg, pNext, 0); } } #endif mergeWorkerShutdown(pMW); } return rc; |
︙ | ︙ | |||
3560 3561 3562 3563 3564 3565 3566 | lsmDbSnapshotSetLevel(pDb->pWorker, pNew); rc = multiCursorNew(pDb, pDb->pWorker, (pDb->pTV!=0), 0, &pCsr); if( rc==LSM_OK ){ if( pNext ){ assert( pNext->pMerge==0 || pNext->nRight>0 ); if( pNext->pMerge==0 ){ | | < < < < < < < | | 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 | lsmDbSnapshotSetLevel(pDb->pWorker, pNew); rc = multiCursorNew(pDb, pDb->pWorker, (pDb->pTV!=0), 0, &pCsr); if( rc==LSM_OK ){ if( pNext ){ assert( pNext->pMerge==0 || pNext->nRight>0 ); if( pNext->pMerge==0 ){ if( pNext->lhs.iRoot ){ rc = multiCursorAddLevel(pCsr, pNext, MULTICURSOR_ADDLEVEL_LHS_SEP); if( rc==LSM_OK ){ pDel = &pNext->lhs; } } iLeftPtr = pNext->lhs.iFirst; } }else{ /* The new level will be the only level in the LSM. There is no reason ** to write out delete keys in this case. */ multiCursorIgnoreDelete(pCsr); } } |
︙ | ︙ | |||
3605 3606 3607 3608 3609 3610 3611 | pNew->pMerge = &merge; mergeworker.pDb = pDb; mergeworker.pLevel = pNew; mergeworker.pCsr = pCsr; /* Mark the separators array for the new level as a "phantom". */ mergeworker.bFlush = 1; | < | < | < < < < < | | 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 | pNew->pMerge = &merge; mergeworker.pDb = pDb; mergeworker.pLevel = pNew; mergeworker.pCsr = pCsr; /* Mark the separators array for the new level as a "phantom". */ mergeworker.bFlush = 1; /* Allocate the first page of the output segment. */ rc = mergeWorkerNextPage(&mergeworker, iLeftPtr); /* Do the work to create the new merged segment on disk */ if( rc==LSM_OK ) rc = lsmMCursorFirst(pCsr); while( rc==LSM_OK && mergeWorkerDone(&mergeworker)==0 ){ rc = mergeWorkerStep(&mergeworker); } mergeWorkerShutdown(&mergeworker); pNew->pMerge = 0; } lsmFreelistDeltaEnd(pDb); /* 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 ){ |
︙ | ︙ | |||
3743 3744 3745 3746 3747 3748 3749 | pTopLevel = lsmDbSnapshotLevel(pDb->pWorker); pNew->pNext = p; for(pp=&pTopLevel; *pp!=pLevel; pp=&((*pp)->pNext)); *pp = pNew; lsmDbSnapshotSetLevel(pDb->pWorker, pTopLevel); /* Determine whether or not the next separators will be linked in */ | | | | | | | | | | | 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 | pTopLevel = lsmDbSnapshotLevel(pDb->pWorker); pNew->pNext = p; for(pp=&pTopLevel; *pp!=pLevel; pp=&((*pp)->pNext)); *pp = pNew; lsmDbSnapshotSetLevel(pDb->pWorker, pTopLevel); /* Determine whether or not the next separators will be linked in */ if( pNext && pNext->pMerge==0 && pNext->lhs.iRoot ){ bUseNext = 1; } } /* Allocate the merge object */ nByte = sizeof(Merge) + sizeof(MergeInput) * (nMerge + bUseNext); pMerge = (Merge *)lsmMallocZeroRc(pDb->pEnv, nByte, &rc); if( pMerge ){ pMerge->aInput = (MergeInput *)&pMerge[1]; pMerge->nInput = nMerge + bUseNext; pNew->pMerge = pMerge; } *ppNew = pNew; return rc; } static int mergeWorkerLoadOutputPage(MergeWorker *pMW){ int rc = LSM_OK; /* Return code */ Segment *pSeg; /* Run to load page from */ Level *pLevel; pLevel = pMW->pLevel; pSeg = &pLevel->lhs; if( pSeg->iLast ){ Page *pPg; rc = lsmFsDbPageGet(pMW->pDb->pFS, pSeg->iLast, &pPg); while( rc==LSM_OK ){ Page *pNext; u8 *aData; int nData; aData = fsPageData(pPg, &nData); if( (pageGetFlags(aData, nData) & SEGMENT_BTREE_FLAG)==0 ) break; rc = lsmFsDbPageNext(pSeg, pPg, -1, &pNext); lsmFsPageRelease(pPg); pPg = pNext; } if( rc==LSM_OK ){ pMW->pPage = pPg; if( pLevel->pMerge->iOutputOff>=0 ) rc = lsmFsPageWrite(pPg); } } return rc; } static int mergeWorkerInit( lsm_db *pDb, /* Db connection to do merge work */ |
︙ | ︙ | |||
3834 3835 3836 3837 3838 3839 3840 | multiCursorReadSeparators(pCsr); }else{ multiCursorIgnoreDelete(pCsr); } assert( rc!=LSM_OK || pMerge->nInput==(pCsr->nSegCsr+(pCsr->pBtCsr!=0)) ); pMW->pCsr = pCsr; | | | < | | 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 | multiCursorReadSeparators(pCsr); }else{ multiCursorIgnoreDelete(pCsr); } assert( rc!=LSM_OK || pMerge->nInput==(pCsr->nSegCsr+(pCsr->pBtCsr!=0)) ); pMW->pCsr = pCsr; /* Load the current output page into memory. */ if( rc==LSM_OK ) rc = mergeWorkerLoadOutputPage(pMW); /* Position the cursor. */ if( rc==LSM_OK ){ if( pMW->pPage==0 ){ /* The output array is still empty. So position the cursor at the very ** start of the input. */ rc = multiCursorEnd(pCsr, 0); }else{ /* The output array is non-empty. Position the cursor based on the ** page/cell data saved in the Merge.aInput[] array. */ int i; |
︙ | ︙ | |||
3970 3971 3972 3973 3974 3975 3976 | ** from rhs segments for which the content has been completely merged ** into the lhs of the level. */ if( rc==LSM_OK ){ if( mergeWorkerDone(&mergeworker)==0 ){ int iGobble = mergeworker.pCsr->aTree[1] - CURSOR_DATA_SEGMENT; if( iGobble<pLevel->nRight ){ | < < | | | < | < | < | | < | 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 | ** from rhs segments for which the content has been completely merged ** into the lhs of the level. */ if( rc==LSM_OK ){ if( mergeWorkerDone(&mergeworker)==0 ){ int iGobble = mergeworker.pCsr->aTree[1] - CURSOR_DATA_SEGMENT; if( iGobble<pLevel->nRight ){ SegmentPtr *pGobble = &mergeworker.pCsr->aSegCsr[iGobble].aPtr[0]; if( (pGobble->flags & PGFTR_SKIP_THIS_FLAG)==0 && pGobble->pSeg->iRoot==0 ){ lsmFsGobble(pWorker, pGobble->pSeg, pGobble->pPg); } } }else if( pLevel->lhs.iFirst==0 ){ /* If the new level is completely empty, remove it from the ** database snapshot. This can only happen if all input keys were ** annihilated. Since keys are only annihilated if the new level ** is the last in the linked list (contains the most ancient of ** database content), this guarantees that pLevel->pNext==0. */ Level *pTop; /* Top level of worker snapshot */ Level **pp; /* Read/write iterator for Level.pNext list */ assert( pLevel->pNext==0 ); /* Remove the level from the worker snapshot. */ pTop = lsmDbSnapshotLevel(pWorker); for(pp=&pTop; *pp!=pLevel; pp=&((*pp)->pNext)); *pp = pLevel->pNext; lsmDbSnapshotSetLevel(pWorker, pTop); /* Free the Level structure. */ lsmFsSortedDelete(pDb->pFS, pWorker, 1, &pLevel->lhs); sortedFreeLevel(pDb->pEnv, pLevel); }else{ int i; /* Free the separators of the next level, if required. */ if( pLevel->pMerge->nInput > pLevel->nRight ){ assert( pLevel->pNext->lhs.iRoot ); pLevel->pNext->lhs.iRoot = 0; } /* Free the right-hand-side of pLevel */ for(i=0; i<pLevel->nRight; i++){ lsmFsSortedDelete(pDb->pFS, pWorker, 1, &pLevel->aRhs[i]); } lsmFree(pDb->pEnv, pLevel->aRhs); pLevel->nRight = 0; pLevel->aRhs = 0; /* Free the Merge object */ lsmFree(pDb->pEnv, pLevel->pMerge); |
︙ | ︙ | |||
4176 4177 4178 4179 4180 4181 4182 | } /* ** Return a string representation of the segment passed as the only argument. ** Space for the returned string is allocated using lsmMalloc(), and should ** be freed by the caller using lsmFree(). */ | | | | | | | 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 | } /* ** Return a string representation of the segment passed as the only argument. ** Space for the returned string is allocated using lsmMalloc(), and should ** be freed by the caller using lsmFree(). */ static char *segToString(lsm_env *pEnv, Segment *pSeg, int nMin){ int nSize = pSeg->nSize; Pgno iRoot = pSeg->iRoot; Pgno iFirst = pSeg->iFirst; Pgno iLast = pSeg->iLast; char *z; char *z1; char *z2; int nPad; z1 = lsmMallocPrintf(pEnv, "%d.%d", iFirst, iLast); |
︙ | ︙ | |||
4213 4214 4215 4216 4217 4218 4219 | } static int fileToString( lsm_env *pEnv, /* For xMalloc() */ char *aBuf, int nBuf, int nMin, | | | | | 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 | } static int fileToString( lsm_env *pEnv, /* For xMalloc() */ char *aBuf, int nBuf, int nMin, Segment *pSeg ){ int i = 0; char *zSeg; zSeg = segToString(pEnv, pSeg, nMin); i += sqlite4_snprintf(&aBuf[i], nBuf-i, "%s", zSeg); lsmFree(pEnv, zSeg); return i; } void sortedDumpPage(lsm_db *pDb, Segment *pRun, Page *pPg, int bVals){ Blob blob = {0, 0, 0}; /* Blob used for keys */ LsmString s; int i; int nRec; int iPtr; int flags; |
︙ | ︙ | |||
4439 4440 4441 4442 4443 4444 4445 | lsmFsPageRelease(pPg); } lsmDbSnapshotRelease(pDb->pEnv, pRelease); return rc; } | | | 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 | lsmFsPageRelease(pPg); } lsmDbSnapshotRelease(pDb->pEnv, pRelease); return rc; } void sortedDumpSegment(lsm_db *pDb, Segment *pRun, int bVals){ assert( pDb->xLog ); if( pRun && pRun->iFirst ){ char *zSeg; Page *pPg; zSeg = segToString(pDb->pEnv, pRun, 0); lsmLogMessage(pDb, LSM_OK, "Segment: %s", zSeg); |
︙ | ︙ | |||
4491 4492 4493 4494 4495 4496 4497 | lsmLogMessage(pDb, LSM_OK, "Database structure (%s)", zWhy); for(pLevel=pTopLevel; pLevel; pLevel=pLevel->pNext){ char zLeft[1024]; char zRight[1024]; int i = 0; | | | < | | < < < | < < | 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 | lsmLogMessage(pDb, LSM_OK, "Database structure (%s)", zWhy); for(pLevel=pTopLevel; pLevel; pLevel=pLevel->pNext){ char zLeft[1024]; char zRight[1024]; int i = 0; Segment *aLeft[24]; Segment *aRight[24]; int nLeft = 0; int nRight = 0; Segment *pSeg = &pLevel->lhs; aLeft[nLeft++] = pSeg; for(i=0; i<pLevel->nRight; i++){ aRight[nRight++] = &pLevel->aRhs[i]; } for(i=0; i<nLeft || i<nRight; i++){ int iPad = 0; char zLevel[32]; zLeft[0] = '\0'; zRight[0] = '\0'; |
︙ | ︙ | |||
4541 4542 4543 4544 4545 4546 4547 | zLevel, iPad, "", zLeft, zRight ); } iLevel++; } | < < < < < < < < < < < < < < | < < | < < | 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 | zLevel, iPad, "", zLeft, zRight ); } iLevel++; } if( bKeys ){ for(pLevel=pTopLevel; pLevel; pLevel=pLevel->pNext){ int i; sortedDumpSegment(pDb, &pLevel->lhs, bVals); for(i=0; i<pLevel->nRight; i++){ sortedDumpSegment(pDb, &pLevel->aRhs[i], bVals); } } } } if( pSnap==0 ){ lsmDbSnapshotRelease(pDb->pEnv, pDump); |
︙ | ︙ | |||
4593 4594 4595 4596 4597 4598 4599 | int rc = LSM_OK; Level *p; assert( pDb->pWorker ); for(p=lsmDbSnapshotLevel(pDb->pWorker); p && rc==LSM_OK; p=p->pNext){ Merge *pMerge = p->pMerge; if( pMerge ){ | | < | < | | | | | | 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 | int rc = LSM_OK; Level *p; assert( pDb->pWorker ); for(p=lsmDbSnapshotLevel(pDb->pWorker); p && rc==LSM_OK; p=p->pNext){ Merge *pMerge = p->pMerge; if( pMerge ){ pMerge->iOutputOff = -1; pMerge->bHierReadonly = 1; } } return LSM_OK; } void lsmSortedSaveTreeCursors(lsm_db *pDb){ MultiCursor *pCsr; for(pCsr=pDb->pCsr; pCsr; pCsr=pCsr->pNext){ lsmTreeCursorSave(pCsr->pTreeCsr); } } #ifdef LSM_DEBUG_EXPENSIVE /* ** This function is only included in the build if LSM_DEBUG_EXPENSIVE is ** defined. Its only purpose is to evaluate various assert() statements to ** verify that the database is well formed in certain respects. ** ** More specifically, it checks that the array pOne contains the required ** pointers to pTwo. Array pTwo must be a main array. pOne may be either a ** separators array or another main array. If pOne does not contain the ** correct set of pointers, an assert() statement fails. */ static int assertPointersOk( lsm_db *pDb, /* Database handle */ Segment *pOne, /* Segment containing pointers */ Segment *pTwo, /* Segment containing pointer targets */ int bRhs /* True if pTwo may have been Gobble()d */ ){ int rc = LSM_OK; /* Error code */ SegmentPtr ptr1; /* Iterates through pOne */ SegmentPtr ptr2; /* Iterates through pTwo */ Pgno iPrev; assert( pOne && pTwo ); memset(&ptr1, 0, sizeof(ptr1)); memset(&ptr2, 0, sizeof(ptr1)); ptr1.pSeg = pOne; ptr2.pSeg = pTwo; segmentPtrEndPage(pDb->pFS, &ptr1, 0, &rc); segmentPtrEndPage(pDb->pFS, &ptr2, 0, &rc); /* Check that the footer pointer of the first page of pOne points to ** the first page of pTwo. */ iPrev = pTwo->iFirst; if( ptr1.iPtr!=iPrev && !bRhs ){ |
︙ | ︙ | |||
4711 4712 4713 4714 4715 4716 4717 | ** verify that the database is well formed in certain respects. ** ** More specifically, it checks that the b-tree embedded in array pRun ** contains the correct keys. If not, an assert() fails. */ static int assertBtreeOk( lsm_db *pDb, | | | | | | | 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 | ** verify that the database is well formed in certain respects. ** ** More specifically, it checks that the b-tree embedded in array pRun ** contains the correct keys. If not, an assert() fails. */ static int assertBtreeOk( lsm_db *pDb, Segment *pSeg ){ int rc = LSM_OK; /* Return code */ if( pSeg->iRoot ){ Blob blob = {0, 0, 0}; /* Buffer used to cache overflow keys */ FileSystem *pFS = pDb->pFS; /* File system to read from */ Page *pPg = 0; /* Main run page */ BtreeCursor *pCsr = 0; /* Btree cursor */ rc = btreeCursorNew(pDb, pSeg, &pCsr); if( rc==LSM_OK ){ rc = btreeCursorFirst(pCsr); } if( rc==LSM_OK ){ rc = lsmFsDbPageGet(pFS, pSeg->iFirst, &pPg); } while( rc==LSM_OK ){ Page *pNext; u8 *aData; int nData; int flags; rc = lsmFsDbPageNext(pSeg, pPg, 1, &pNext); lsmFsPageRelease(pPg); pPg = pNext; if( pPg==0 ) break; aData = fsPageData(pPg, &nData); flags = pageGetFlags(aData, nData); if( rc==LSM_OK && 0==((SEGMENT_BTREE_FLAG|PGFTR_SKIP_THIS_FLAG) & flags) |
︙ | ︙ |