Enhance the command-line completion extension to return the names of
[sqlite.git] / ext / lsm1 / lsm_sorted.c
blobde962b88085ea995cd5616997e50ab670e692f30
1 /*
2 ** 2011-08-14
3 **
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
6 **
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 *************************************************************************
13 ** PAGE FORMAT:
15 ** The maximum page size is 65536 bytes.
17 ** Since all records are equal to or larger than 2 bytes in size, and
18 ** some space within the page is consumed by the page footer, there must
19 ** be less than 2^15 records on each page.
21 ** Each page ends with a footer that describes the pages contents. This
22 ** footer serves as similar purpose to the page header in an SQLite database.
23 ** A footer is used instead of a header because it makes it easier to
24 ** populate a new page based on a sorted list of key/value pairs.
26 ** The footer consists of the following values (starting at the end of
27 ** the page and continuing backwards towards the start). All values are
28 ** stored as unsigned big-endian integers.
30 ** * Number of records on page (2 bytes).
31 ** * Flags field (2 bytes).
32 ** * Left-hand pointer value (8 bytes).
33 ** * The starting offset of each record (2 bytes per record).
35 ** Records may span pages. Unless it happens to be an exact fit, the part
36 ** of the final record that starts on page X that does not fit on page X
37 ** is stored at the start of page (X+1). This means there may be pages where
38 ** (N==0). And on most pages the first record that starts on the page will
39 ** not start at byte offset 0. For example:
41 ** aaaaa bbbbb ccc <footer> cc eeeee fffff g <footer> gggg....
43 ** RECORD FORMAT:
44 **
45 ** The first byte of the record is a flags byte. It is a combination
46 ** of the following flags (defined in lsmInt.h):
48 ** LSM_START_DELETE
49 ** LSM_END_DELETE
50 ** LSM_POINT_DELETE
51 ** LSM_INSERT
52 ** LSM_SEPARATOR
53 ** LSM_SYSTEMKEY
55 ** Immediately following the type byte is a pointer to the smallest key
56 ** in the next file that is larger than the key in the current record. The
57 ** pointer is encoded as a varint. When added to the 32-bit page number
58 ** stored in the footer, it is the page number of the page that contains the
59 ** smallest key in the next sorted file that is larger than this key.
61 ** Next is the number of bytes in the key, encoded as a varint.
63 ** If the LSM_INSERT flag is set, the number of bytes in the value, as
64 ** a varint, is next.
66 ** Finally, the blob of data containing the key, and for LSM_INSERT
67 ** records, the value as well.
70 #ifndef _LSM_INT_H
71 # include "lsmInt.h"
72 #endif
74 #define LSM_LOG_STRUCTURE 0
75 #define LSM_LOG_DATA 0
78 ** Macros to help decode record types.
80 #define rtTopic(eType) ((eType) & LSM_SYSTEMKEY)
81 #define rtIsDelete(eType) (((eType) & 0x0F)==LSM_POINT_DELETE)
83 #define rtIsSeparator(eType) (((eType) & LSM_SEPARATOR)!=0)
84 #define rtIsWrite(eType) (((eType) & LSM_INSERT)!=0)
85 #define rtIsSystem(eType) (((eType) & LSM_SYSTEMKEY)!=0)
88 ** The following macros are used to access a page footer.
90 #define SEGMENT_NRECORD_OFFSET(pgsz) ((pgsz) - 2)
91 #define SEGMENT_FLAGS_OFFSET(pgsz) ((pgsz) - 2 - 2)
92 #define SEGMENT_POINTER_OFFSET(pgsz) ((pgsz) - 2 - 2 - 8)
93 #define SEGMENT_CELLPTR_OFFSET(pgsz, iCell) ((pgsz) - 2 - 2 - 8 - 2 - (iCell)*2)
95 #define SEGMENT_EOF(pgsz, nEntry) SEGMENT_CELLPTR_OFFSET(pgsz, nEntry-1)
97 #define SEGMENT_BTREE_FLAG 0x0001
98 #define PGFTR_SKIP_NEXT_FLAG 0x0002
99 #define PGFTR_SKIP_THIS_FLAG 0x0004
102 #ifndef LSM_SEGMENTPTR_FREE_THRESHOLD
103 # define LSM_SEGMENTPTR_FREE_THRESHOLD 1024
104 #endif
106 typedef struct SegmentPtr SegmentPtr;
107 typedef struct LsmBlob LsmBlob;
109 struct LsmBlob {
110 lsm_env *pEnv;
111 void *pData;
112 int nData;
113 int nAlloc;
117 ** A SegmentPtr object may be used for one of two purposes:
119 ** * To iterate and/or seek within a single Segment (the combination of a
120 ** main run and an optional sorted run).
122 ** * To iterate through the separators array of a segment.
124 struct SegmentPtr {
125 Level *pLevel; /* Level object segment is part of */
126 Segment *pSeg; /* Segment to access */
128 /* Current page. See segmentPtrLoadPage(). */
129 Page *pPg; /* Current page */
130 u16 flags; /* Copy of page flags field */
131 int nCell; /* Number of cells on pPg */
132 LsmPgno iPtr; /* Base cascade pointer */
134 /* Current cell. See segmentPtrLoadCell() */
135 int iCell; /* Current record within page pPg */
136 int eType; /* Type of current record */
137 LsmPgno iPgPtr; /* Cascade pointer offset */
138 void *pKey; int nKey; /* Key associated with current record */
139 void *pVal; int nVal; /* Current record value (eType==WRITE only) */
141 /* Blobs used to allocate buffers for pKey and pVal as required */
142 LsmBlob blob1;
143 LsmBlob blob2;
147 ** Used to iterate through the keys stored in a b-tree hierarchy from start
148 ** to finish. Only First() and Next() operations are required.
150 ** btreeCursorNew()
151 ** btreeCursorFirst()
152 ** btreeCursorNext()
153 ** btreeCursorFree()
154 ** btreeCursorPosition()
155 ** btreeCursorRestore()
157 typedef struct BtreePg BtreePg;
158 typedef struct BtreeCursor BtreeCursor;
159 struct BtreePg {
160 Page *pPage;
161 int iCell;
163 struct BtreeCursor {
164 Segment *pSeg; /* Iterate through this segments btree */
165 FileSystem *pFS; /* File system to read pages from */
166 int nDepth; /* Allocated size of aPg[] */
167 int iPg; /* Current entry in aPg[]. -1 -> EOF. */
168 BtreePg *aPg; /* Pages from root to current location */
170 /* Cache of current entry. pKey==0 for EOF. */
171 void *pKey;
172 int nKey;
173 int eType;
174 LsmPgno iPtr;
176 /* Storage for key, if not local */
177 LsmBlob blob;
182 ** A cursor used for merged searches or iterations through up to one
183 ** Tree structure and any number of sorted files.
185 ** lsmMCursorNew()
186 ** lsmMCursorSeek()
187 ** lsmMCursorNext()
188 ** lsmMCursorPrev()
189 ** lsmMCursorFirst()
190 ** lsmMCursorLast()
191 ** lsmMCursorKey()
192 ** lsmMCursorValue()
193 ** lsmMCursorValid()
195 ** iFree:
196 ** This variable is only used by cursors providing input data for a
197 ** new top-level segment. Such cursors only ever iterate forwards, not
198 ** backwards.
200 struct MultiCursor {
201 lsm_db *pDb; /* Connection that owns this cursor */
202 MultiCursor *pNext; /* Next cursor owned by connection pDb */
203 int flags; /* Mask of CURSOR_XXX flags */
205 int eType; /* Cache of current key type */
206 LsmBlob key; /* Cache of current key (or NULL) */
207 LsmBlob val; /* Cache of current value */
209 /* All the component cursors: */
210 TreeCursor *apTreeCsr[2]; /* Up to two tree cursors */
211 int iFree; /* Next element of free-list (-ve for eof) */
212 SegmentPtr *aPtr; /* Array of segment pointers */
213 int nPtr; /* Size of array aPtr[] */
214 BtreeCursor *pBtCsr; /* b-tree cursor (db writes only) */
216 /* Comparison results */
217 int nTree; /* Size of aTree[] array */
218 int *aTree; /* Array of comparison results */
220 /* Used by cursors flushing the in-memory tree only */
221 void *pSystemVal; /* Pointer to buffer to free */
223 /* Used by worker cursors only */
224 LsmPgno *pPrevMergePtr;
228 ** The following constants are used to assign integers to each component
229 ** cursor of a multi-cursor.
231 #define CURSOR_DATA_TREE0 0 /* Current tree cursor (apTreeCsr[0]) */
232 #define CURSOR_DATA_TREE1 1 /* The "old" tree, if any (apTreeCsr[1]) */
233 #define CURSOR_DATA_SYSTEM 2 /* Free-list entries (new-toplevel only) */
234 #define CURSOR_DATA_SEGMENT 3 /* First segment pointer (aPtr[0]) */
237 ** CURSOR_IGNORE_DELETE
238 ** If set, this cursor will not visit SORTED_DELETE keys.
240 ** CURSOR_FLUSH_FREELIST
241 ** This cursor is being used to create a new toplevel. It should also
242 ** iterate through the contents of the in-memory free block list.
244 ** CURSOR_IGNORE_SYSTEM
245 ** If set, this cursor ignores system keys.
247 ** CURSOR_NEXT_OK
248 ** Set if it is Ok to call lsm_csr_next().
250 ** CURSOR_PREV_OK
251 ** Set if it is Ok to call lsm_csr_prev().
253 ** CURSOR_READ_SEPARATORS
254 ** Set if this cursor should visit the separator keys in segment
255 ** aPtr[nPtr-1].
257 ** CURSOR_SEEK_EQ
258 ** Cursor has undergone a successful lsm_csr_seek(LSM_SEEK_EQ) operation.
259 ** The key and value are stored in MultiCursor.key and MultiCursor.val
260 ** respectively.
262 #define CURSOR_IGNORE_DELETE 0x00000001
263 #define CURSOR_FLUSH_FREELIST 0x00000002
264 #define CURSOR_IGNORE_SYSTEM 0x00000010
265 #define CURSOR_NEXT_OK 0x00000020
266 #define CURSOR_PREV_OK 0x00000040
267 #define CURSOR_READ_SEPARATORS 0x00000080
268 #define CURSOR_SEEK_EQ 0x00000100
270 typedef struct MergeWorker MergeWorker;
271 typedef struct Hierarchy Hierarchy;
273 struct Hierarchy {
274 Page **apHier;
275 int nHier;
279 ** aSave:
280 ** When mergeWorkerNextPage() is called to advance to the next page in
281 ** the output segment, if the bStore flag for an element of aSave[] is
282 ** true, it is cleared and the corresponding iPgno value is set to the
283 ** page number of the page just completed.
285 ** aSave[0] is used to record the pointer value to be pushed into the
286 ** b-tree hierarchy. aSave[1] is used to save the page number of the
287 ** page containing the indirect key most recently written to the b-tree.
288 ** see mergeWorkerPushHierarchy() for details.
290 struct MergeWorker {
291 lsm_db *pDb; /* Database handle */
292 Level *pLevel; /* Worker snapshot Level being merged */
293 MultiCursor *pCsr; /* Cursor to read new segment contents from */
294 int bFlush; /* True if this is an in-memory tree flush */
295 Hierarchy hier; /* B-tree hierarchy under construction */
296 Page *pPage; /* Current output page */
297 int nWork; /* Number of calls to mergeWorkerNextPage() */
298 LsmPgno *aGobble; /* Gobble point for each input segment */
300 LsmPgno iIndirect;
301 struct SavedPgno {
302 LsmPgno iPgno;
303 int bStore;
304 } aSave[2];
307 #ifdef LSM_DEBUG_EXPENSIVE
308 static int assertPointersOk(lsm_db *, Segment *, Segment *, int);
309 static int assertBtreeOk(lsm_db *, Segment *);
310 static void assertRunInOrder(lsm_db *pDb, Segment *pSeg);
311 #else
312 #define assertRunInOrder(x,y)
313 #define assertBtreeOk(x,y)
314 #endif
317 struct FilePage { u8 *aData; int nData; };
318 static u8 *fsPageData(Page *pPg, int *pnData){
319 *pnData = ((struct FilePage *)(pPg))->nData;
320 return ((struct FilePage *)(pPg))->aData;
322 /*UNUSED static u8 *fsPageDataPtr(Page *pPg){
323 return ((struct FilePage *)(pPg))->aData;
327 ** Write nVal as a 16-bit unsigned big-endian integer into buffer aOut.
329 void lsmPutU16(u8 *aOut, u16 nVal){
330 aOut[0] = (u8)((nVal>>8) & 0xFF);
331 aOut[1] = (u8)(nVal & 0xFF);
334 void lsmPutU32(u8 *aOut, u32 nVal){
335 aOut[0] = (u8)((nVal>>24) & 0xFF);
336 aOut[1] = (u8)((nVal>>16) & 0xFF);
337 aOut[2] = (u8)((nVal>> 8) & 0xFF);
338 aOut[3] = (u8)((nVal ) & 0xFF);
341 int lsmGetU16(u8 *aOut){
342 return (aOut[0] << 8) + aOut[1];
345 u32 lsmGetU32(u8 *aOut){
346 return ((u32)aOut[0] << 24)
347 + ((u32)aOut[1] << 16)
348 + ((u32)aOut[2] << 8)
349 + ((u32)aOut[3]);
352 u64 lsmGetU64(u8 *aOut){
353 return ((u64)aOut[0] << 56)
354 + ((u64)aOut[1] << 48)
355 + ((u64)aOut[2] << 40)
356 + ((u64)aOut[3] << 32)
357 + ((u64)aOut[4] << 24)
358 + ((u32)aOut[5] << 16)
359 + ((u32)aOut[6] << 8)
360 + ((u32)aOut[7]);
363 void lsmPutU64(u8 *aOut, u64 nVal){
364 aOut[0] = (u8)((nVal>>56) & 0xFF);
365 aOut[1] = (u8)((nVal>>48) & 0xFF);
366 aOut[2] = (u8)((nVal>>40) & 0xFF);
367 aOut[3] = (u8)((nVal>>32) & 0xFF);
368 aOut[4] = (u8)((nVal>>24) & 0xFF);
369 aOut[5] = (u8)((nVal>>16) & 0xFF);
370 aOut[6] = (u8)((nVal>> 8) & 0xFF);
371 aOut[7] = (u8)((nVal ) & 0xFF);
374 static int sortedBlobGrow(lsm_env *pEnv, LsmBlob *pBlob, int nData){
375 assert( pBlob->pEnv==pEnv || (pBlob->pEnv==0 && pBlob->pData==0) );
376 if( pBlob->nAlloc<nData ){
377 pBlob->pData = lsmReallocOrFree(pEnv, pBlob->pData, nData);
378 if( !pBlob->pData ) return LSM_NOMEM_BKPT;
379 pBlob->nAlloc = nData;
380 pBlob->pEnv = pEnv;
382 return LSM_OK;
385 static int sortedBlobSet(lsm_env *pEnv, LsmBlob *pBlob, void *pData, int nData){
386 if( sortedBlobGrow(pEnv, pBlob, nData) ) return LSM_NOMEM;
387 memcpy(pBlob->pData, pData, nData);
388 pBlob->nData = nData;
389 return LSM_OK;
392 #if 0
393 static int sortedBlobCopy(LsmBlob *pDest, LsmBlob *pSrc){
394 return sortedBlobSet(pDest, pSrc->pData, pSrc->nData);
396 #endif
398 static void sortedBlobFree(LsmBlob *pBlob){
399 assert( pBlob->pEnv || pBlob->pData==0 );
400 if( pBlob->pData ) lsmFree(pBlob->pEnv, pBlob->pData);
401 memset(pBlob, 0, sizeof(LsmBlob));
404 static int sortedReadData(
405 Segment *pSeg,
406 Page *pPg,
407 int iOff,
408 int nByte,
409 void **ppData,
410 LsmBlob *pBlob
412 int rc = LSM_OK;
413 int iEnd;
414 int nData;
415 int nCell;
416 u8 *aData;
418 aData = fsPageData(pPg, &nData);
419 nCell = lsmGetU16(&aData[SEGMENT_NRECORD_OFFSET(nData)]);
420 iEnd = SEGMENT_EOF(nData, nCell);
421 assert( iEnd>0 && iEnd<nData );
423 if( iOff+nByte<=iEnd ){
424 *ppData = (void *)&aData[iOff];
425 }else{
426 int nRem = nByte;
427 int i = iOff;
428 u8 *aDest;
430 /* Make sure the blob is big enough to store the value being loaded. */
431 rc = sortedBlobGrow(lsmPageEnv(pPg), pBlob, nByte);
432 if( rc!=LSM_OK ) return rc;
433 pBlob->nData = nByte;
434 aDest = (u8 *)pBlob->pData;
435 *ppData = pBlob->pData;
437 /* Increment the pointer pages ref-count. */
438 lsmFsPageRef(pPg);
440 while( rc==LSM_OK ){
441 Page *pNext;
442 int flags;
444 /* Copy data from pPg into the output buffer. */
445 int nCopy = LSM_MIN(nRem, iEnd-i);
446 if( nCopy>0 ){
447 memcpy(&aDest[nByte-nRem], &aData[i], nCopy);
448 nRem -= nCopy;
449 i += nCopy;
450 assert( nRem==0 || i==iEnd );
452 assert( nRem>=0 );
453 if( nRem==0 ) break;
454 i -= iEnd;
456 /* Grab the next page in the segment */
458 do {
459 rc = lsmFsDbPageNext(pSeg, pPg, 1, &pNext);
460 if( rc==LSM_OK && pNext==0 ){
461 rc = LSM_CORRUPT_BKPT;
463 if( rc ) break;
464 lsmFsPageRelease(pPg);
465 pPg = pNext;
466 aData = fsPageData(pPg, &nData);
467 flags = lsmGetU16(&aData[SEGMENT_FLAGS_OFFSET(nData)]);
468 }while( flags&SEGMENT_BTREE_FLAG );
470 iEnd = SEGMENT_EOF(nData, lsmGetU16(&aData[nData-2]));
471 assert( iEnd>0 && iEnd<nData );
474 lsmFsPageRelease(pPg);
477 return rc;
480 static int pageGetNRec(u8 *aData, int nData){
481 return (int)lsmGetU16(&aData[SEGMENT_NRECORD_OFFSET(nData)]);
484 static LsmPgno pageGetPtr(u8 *aData, int nData){
485 return (LsmPgno)lsmGetU64(&aData[SEGMENT_POINTER_OFFSET(nData)]);
488 static int pageGetFlags(u8 *aData, int nData){
489 return (int)lsmGetU16(&aData[SEGMENT_FLAGS_OFFSET(nData)]);
492 static u8 *pageGetCell(u8 *aData, int nData, int iCell){
493 return &aData[lsmGetU16(&aData[SEGMENT_CELLPTR_OFFSET(nData, iCell)])];
497 ** Return the number of cells on page pPg.
499 static int pageObjGetNRec(Page *pPg){
500 int nData;
501 u8 *aData = lsmFsPageData(pPg, &nData);
502 return pageGetNRec(aData, nData);
506 ** Return the decoded (possibly relative) pointer value stored in cell
507 ** iCell from page aData/nData.
509 static LsmPgno pageGetRecordPtr(u8 *aData, int nData, int iCell){
510 LsmPgno iRet; /* Return value */
511 u8 *aCell; /* Pointer to cell iCell */
513 assert( iCell<pageGetNRec(aData, nData) && iCell>=0 );
514 aCell = pageGetCell(aData, nData, iCell);
515 lsmVarintGet64(&aCell[1], &iRet);
516 return iRet;
519 static u8 *pageGetKey(
520 Segment *pSeg, /* Segment pPg belongs to */
521 Page *pPg, /* Page to read from */
522 int iCell, /* Index of cell on page to read */
523 int *piTopic, /* OUT: Topic associated with this key */
524 int *pnKey, /* OUT: Size of key in bytes */
525 LsmBlob *pBlob /* If required, use this for dynamic memory */
527 u8 *pKey;
528 int nDummy;
529 int eType;
530 u8 *aData;
531 int nData;
533 aData = fsPageData(pPg, &nData);
535 assert( !(pageGetFlags(aData, nData) & SEGMENT_BTREE_FLAG) );
536 assert( iCell<pageGetNRec(aData, nData) );
538 pKey = pageGetCell(aData, nData, iCell);
539 eType = *pKey++;
540 pKey += lsmVarintGet32(pKey, &nDummy);
541 pKey += lsmVarintGet32(pKey, pnKey);
542 if( rtIsWrite(eType) ){
543 pKey += lsmVarintGet32(pKey, &nDummy);
545 *piTopic = rtTopic(eType);
547 sortedReadData(pSeg, pPg, pKey-aData, *pnKey, (void **)&pKey, pBlob);
548 return pKey;
551 static int pageGetKeyCopy(
552 lsm_env *pEnv, /* Environment handle */
553 Segment *pSeg, /* Segment pPg belongs to */
554 Page *pPg, /* Page to read from */
555 int iCell, /* Index of cell on page to read */
556 int *piTopic, /* OUT: Topic associated with this key */
557 LsmBlob *pBlob /* If required, use this for dynamic memory */
559 int rc = LSM_OK;
560 int nKey;
561 u8 *aKey;
563 aKey = pageGetKey(pSeg, pPg, iCell, piTopic, &nKey, pBlob);
564 assert( (void *)aKey!=pBlob->pData || nKey==pBlob->nData );
565 if( (void *)aKey!=pBlob->pData ){
566 rc = sortedBlobSet(pEnv, pBlob, aKey, nKey);
569 return rc;
572 static LsmPgno pageGetBtreeRef(Page *pPg, int iKey){
573 LsmPgno iRef;
574 u8 *aData;
575 int nData;
576 u8 *aCell;
578 aData = fsPageData(pPg, &nData);
579 aCell = pageGetCell(aData, nData, iKey);
580 assert( aCell[0]==0 );
581 aCell++;
582 aCell += lsmVarintGet64(aCell, &iRef);
583 lsmVarintGet64(aCell, &iRef);
584 assert( iRef>0 );
585 return iRef;
588 #define GETVARINT64(a, i) (((i)=((u8*)(a))[0])<=240?1:lsmVarintGet64((a), &(i)))
589 #define GETVARINT32(a, i) (((i)=((u8*)(a))[0])<=240?1:lsmVarintGet32((a), &(i)))
591 static int pageGetBtreeKey(
592 Segment *pSeg, /* Segment page pPg belongs to */
593 Page *pPg,
594 int iKey,
595 LsmPgno *piPtr,
596 int *piTopic,
597 void **ppKey,
598 int *pnKey,
599 LsmBlob *pBlob
601 u8 *aData;
602 int nData;
603 u8 *aCell;
604 int eType;
606 aData = fsPageData(pPg, &nData);
607 assert( SEGMENT_BTREE_FLAG & pageGetFlags(aData, nData) );
608 assert( iKey>=0 && iKey<pageGetNRec(aData, nData) );
610 aCell = pageGetCell(aData, nData, iKey);
611 eType = *aCell++;
612 aCell += GETVARINT64(aCell, *piPtr);
614 if( eType==0 ){
615 int rc;
616 LsmPgno iRef; /* Page number of referenced page */
617 Page *pRef;
618 aCell += GETVARINT64(aCell, iRef);
619 rc = lsmFsDbPageGet(lsmPageFS(pPg), pSeg, iRef, &pRef);
620 if( rc!=LSM_OK ) return rc;
621 pageGetKeyCopy(lsmPageEnv(pPg), pSeg, pRef, 0, &eType, pBlob);
622 lsmFsPageRelease(pRef);
623 *ppKey = pBlob->pData;
624 *pnKey = pBlob->nData;
625 }else{
626 aCell += GETVARINT32(aCell, *pnKey);
627 *ppKey = aCell;
629 if( piTopic ) *piTopic = rtTopic(eType);
631 return LSM_OK;
634 static int btreeCursorLoadKey(BtreeCursor *pCsr){
635 int rc = LSM_OK;
636 if( pCsr->iPg<0 ){
637 pCsr->pKey = 0;
638 pCsr->nKey = 0;
639 pCsr->eType = 0;
640 }else{
641 LsmPgno dummy;
642 int iPg = pCsr->iPg;
643 int iCell = pCsr->aPg[iPg].iCell;
644 while( iCell<0 && (--iPg)>=0 ){
645 iCell = pCsr->aPg[iPg].iCell-1;
647 if( iPg<0 || iCell<0 ) return LSM_CORRUPT_BKPT;
649 rc = pageGetBtreeKey(
650 pCsr->pSeg,
651 pCsr->aPg[iPg].pPage, iCell,
652 &dummy, &pCsr->eType, &pCsr->pKey, &pCsr->nKey, &pCsr->blob
654 pCsr->eType |= LSM_SEPARATOR;
657 return rc;
660 static int btreeCursorPtr(u8 *aData, int nData, int iCell){
661 int nCell;
663 nCell = pageGetNRec(aData, nData);
664 if( iCell>=nCell ){
665 return (int)pageGetPtr(aData, nData);
667 return (int)pageGetRecordPtr(aData, nData, iCell);
670 static int btreeCursorNext(BtreeCursor *pCsr){
671 int rc = LSM_OK;
673 BtreePg *pPg = &pCsr->aPg[pCsr->iPg];
674 int nCell;
675 u8 *aData;
676 int nData;
678 assert( pCsr->iPg>=0 );
679 assert( pCsr->iPg==pCsr->nDepth-1 );
681 aData = fsPageData(pPg->pPage, &nData);
682 nCell = pageGetNRec(aData, nData);
683 assert( pPg->iCell<=nCell );
684 pPg->iCell++;
685 if( pPg->iCell==nCell ){
686 LsmPgno iLoad;
688 /* Up to parent. */
689 lsmFsPageRelease(pPg->pPage);
690 pPg->pPage = 0;
691 pCsr->iPg--;
692 while( pCsr->iPg>=0 ){
693 pPg = &pCsr->aPg[pCsr->iPg];
694 aData = fsPageData(pPg->pPage, &nData);
695 if( pPg->iCell<pageGetNRec(aData, nData) ) break;
696 lsmFsPageRelease(pPg->pPage);
697 pCsr->iPg--;
700 /* Read the key */
701 rc = btreeCursorLoadKey(pCsr);
703 /* Unless the cursor is at EOF, descend to cell -1 (yes, negative one) of
704 ** the left-most most descendent. */
705 if( pCsr->iPg>=0 ){
706 pCsr->aPg[pCsr->iPg].iCell++;
708 iLoad = btreeCursorPtr(aData, nData, pPg->iCell);
709 do {
710 Page *pLoad;
711 pCsr->iPg++;
712 rc = lsmFsDbPageGet(pCsr->pFS, pCsr->pSeg, iLoad, &pLoad);
713 pCsr->aPg[pCsr->iPg].pPage = pLoad;
714 pCsr->aPg[pCsr->iPg].iCell = 0;
715 if( rc==LSM_OK ){
716 if( pCsr->iPg==(pCsr->nDepth-1) ) break;
717 aData = fsPageData(pLoad, &nData);
718 iLoad = btreeCursorPtr(aData, nData, 0);
720 }while( rc==LSM_OK && pCsr->iPg<(pCsr->nDepth-1) );
721 pCsr->aPg[pCsr->iPg].iCell = -1;
724 }else{
725 rc = btreeCursorLoadKey(pCsr);
728 if( rc==LSM_OK && pCsr->iPg>=0 ){
729 aData = fsPageData(pCsr->aPg[pCsr->iPg].pPage, &nData);
730 pCsr->iPtr = btreeCursorPtr(aData, nData, pCsr->aPg[pCsr->iPg].iCell+1);
733 return rc;
736 static void btreeCursorFree(BtreeCursor *pCsr){
737 if( pCsr ){
738 int i;
739 lsm_env *pEnv = lsmFsEnv(pCsr->pFS);
740 for(i=0; i<=pCsr->iPg; i++){
741 lsmFsPageRelease(pCsr->aPg[i].pPage);
743 sortedBlobFree(&pCsr->blob);
744 lsmFree(pEnv, pCsr->aPg);
745 lsmFree(pEnv, pCsr);
749 static int btreeCursorFirst(BtreeCursor *pCsr){
750 int rc;
752 Page *pPg = 0;
753 FileSystem *pFS = pCsr->pFS;
754 int iPg = (int)pCsr->pSeg->iRoot;
756 do {
757 rc = lsmFsDbPageGet(pFS, pCsr->pSeg, iPg, &pPg);
758 assert( (rc==LSM_OK)==(pPg!=0) );
759 if( rc==LSM_OK ){
760 u8 *aData;
761 int nData;
762 int flags;
764 aData = fsPageData(pPg, &nData);
765 flags = pageGetFlags(aData, nData);
766 if( (flags & SEGMENT_BTREE_FLAG)==0 ) break;
768 if( (pCsr->nDepth % 8)==0 ){
769 int nNew = pCsr->nDepth + 8;
770 pCsr->aPg = (BtreePg *)lsmReallocOrFreeRc(
771 lsmFsEnv(pFS), pCsr->aPg, sizeof(BtreePg) * nNew, &rc
773 if( rc==LSM_OK ){
774 memset(&pCsr->aPg[pCsr->nDepth], 0, sizeof(BtreePg) * 8);
778 if( rc==LSM_OK ){
779 assert( pCsr->aPg[pCsr->nDepth].iCell==0 );
780 pCsr->aPg[pCsr->nDepth].pPage = pPg;
781 pCsr->nDepth++;
782 iPg = (int)pageGetRecordPtr(aData, nData, 0);
785 }while( rc==LSM_OK );
786 lsmFsPageRelease(pPg);
787 pCsr->iPg = pCsr->nDepth-1;
789 if( rc==LSM_OK && pCsr->nDepth ){
790 pCsr->aPg[pCsr->iPg].iCell = -1;
791 rc = btreeCursorNext(pCsr);
794 return rc;
797 static void btreeCursorPosition(BtreeCursor *pCsr, MergeInput *p){
798 if( pCsr->iPg>=0 ){
799 p->iPg = lsmFsPageNumber(pCsr->aPg[pCsr->iPg].pPage);
800 p->iCell = ((pCsr->aPg[pCsr->iPg].iCell + 1) << 8) + pCsr->nDepth;
801 }else{
802 p->iPg = 0;
803 p->iCell = 0;
807 static void btreeCursorSplitkey(BtreeCursor *pCsr, MergeInput *p){
808 int iCell = pCsr->aPg[pCsr->iPg].iCell;
809 if( iCell>=0 ){
810 p->iCell = iCell;
811 p->iPg = lsmFsPageNumber(pCsr->aPg[pCsr->iPg].pPage);
812 }else{
813 int i;
814 for(i=pCsr->iPg-1; i>=0; i--){
815 if( pCsr->aPg[i].iCell>0 ) break;
817 assert( i>=0 );
818 p->iCell = pCsr->aPg[i].iCell-1;
819 p->iPg = lsmFsPageNumber(pCsr->aPg[i].pPage);
823 static int sortedKeyCompare(
824 int (*xCmp)(void *, int, void *, int),
825 int iLhsTopic, void *pLhsKey, int nLhsKey,
826 int iRhsTopic, void *pRhsKey, int nRhsKey
828 int res = iLhsTopic - iRhsTopic;
829 if( res==0 ){
830 res = xCmp(pLhsKey, nLhsKey, pRhsKey, nRhsKey);
832 return res;
835 static int btreeCursorRestore(
836 BtreeCursor *pCsr,
837 int (*xCmp)(void *, int, void *, int),
838 MergeInput *p
840 int rc = LSM_OK;
842 if( p->iPg ){
843 lsm_env *pEnv = lsmFsEnv(pCsr->pFS);
844 int iCell; /* Current cell number on leaf page */
845 LsmPgno iLeaf; /* Page number of current leaf page */
846 int nDepth; /* Depth of b-tree structure */
847 Segment *pSeg = pCsr->pSeg;
849 /* Decode the MergeInput structure */
850 iLeaf = p->iPg;
851 nDepth = (p->iCell & 0x00FF);
852 iCell = (p->iCell >> 8) - 1;
854 /* Allocate the BtreeCursor.aPg[] array */
855 assert( pCsr->aPg==0 );
856 pCsr->aPg = (BtreePg *)lsmMallocZeroRc(pEnv, sizeof(BtreePg) * nDepth, &rc);
858 /* Populate the last entry of the aPg[] array */
859 if( rc==LSM_OK ){
860 Page **pp = &pCsr->aPg[nDepth-1].pPage;
861 pCsr->iPg = nDepth-1;
862 pCsr->nDepth = nDepth;
863 pCsr->aPg[pCsr->iPg].iCell = iCell;
864 rc = lsmFsDbPageGet(pCsr->pFS, pSeg, iLeaf, pp);
867 /* Populate any other aPg[] array entries */
868 if( rc==LSM_OK && nDepth>1 ){
869 LsmBlob blob = {0,0,0};
870 void *pSeek;
871 int nSeek;
872 int iTopicSeek;
873 int iPg = 0;
874 int iLoad = (int)pSeg->iRoot;
875 Page *pPg = pCsr->aPg[nDepth-1].pPage;
877 if( pageObjGetNRec(pPg)==0 ){
878 /* This can happen when pPg is the right-most leaf in the b-tree.
879 ** In this case, set the iTopicSeek/pSeek/nSeek key to a value
880 ** greater than any real key. */
881 assert( iCell==-1 );
882 iTopicSeek = 1000;
883 pSeek = 0;
884 nSeek = 0;
885 }else{
886 LsmPgno dummy;
887 rc = pageGetBtreeKey(pSeg, pPg,
888 0, &dummy, &iTopicSeek, &pSeek, &nSeek, &pCsr->blob
892 do {
893 Page *pPg2;
894 rc = lsmFsDbPageGet(pCsr->pFS, pSeg, iLoad, &pPg2);
895 assert( rc==LSM_OK || pPg2==0 );
896 if( rc==LSM_OK ){
897 u8 *aData; /* Buffer containing page data */
898 int nData; /* Size of aData[] in bytes */
899 int iMin;
900 int iMax;
901 int iCell2;
903 aData = fsPageData(pPg2, &nData);
904 assert( (pageGetFlags(aData, nData) & SEGMENT_BTREE_FLAG) );
906 iLoad = (int)pageGetPtr(aData, nData);
907 iCell2 = pageGetNRec(aData, nData);
908 iMax = iCell2-1;
909 iMin = 0;
911 while( iMax>=iMin ){
912 int iTry = (iMin+iMax)/2;
913 void *pKey; int nKey; /* Key for cell iTry */
914 int iTopic; /* Topic for key pKeyT/nKeyT */
915 LsmPgno iPtr; /* Pointer for cell iTry */
916 int res; /* (pSeek - pKeyT) */
918 rc = pageGetBtreeKey(
919 pSeg, pPg2, iTry, &iPtr, &iTopic, &pKey, &nKey, &blob
921 if( rc!=LSM_OK ) break;
923 res = sortedKeyCompare(
924 xCmp, iTopicSeek, pSeek, nSeek, iTopic, pKey, nKey
926 assert( res!=0 );
928 if( res<0 ){
929 iLoad = (int)iPtr;
930 iCell2 = iTry;
931 iMax = iTry-1;
932 }else{
933 iMin = iTry+1;
937 pCsr->aPg[iPg].pPage = pPg2;
938 pCsr->aPg[iPg].iCell = iCell2;
939 iPg++;
940 assert( iPg!=nDepth-1
941 || lsmFsRedirectPage(pCsr->pFS, pSeg->pRedirect, iLoad)==iLeaf
944 }while( rc==LSM_OK && iPg<(nDepth-1) );
945 sortedBlobFree(&blob);
948 /* Load the current key and pointer */
949 if( rc==LSM_OK ){
950 BtreePg *pBtreePg;
951 u8 *aData;
952 int nData;
954 pBtreePg = &pCsr->aPg[pCsr->iPg];
955 aData = fsPageData(pBtreePg->pPage, &nData);
956 pCsr->iPtr = btreeCursorPtr(aData, nData, pBtreePg->iCell+1);
957 if( pBtreePg->iCell<0 ){
958 LsmPgno dummy;
959 int i;
960 for(i=pCsr->iPg-1; i>=0; i--){
961 if( pCsr->aPg[i].iCell>0 ) break;
963 assert( i>=0 );
964 rc = pageGetBtreeKey(pSeg,
965 pCsr->aPg[i].pPage, pCsr->aPg[i].iCell-1,
966 &dummy, &pCsr->eType, &pCsr->pKey, &pCsr->nKey, &pCsr->blob
968 pCsr->eType |= LSM_SEPARATOR;
970 }else{
971 rc = btreeCursorLoadKey(pCsr);
975 return rc;
978 static int btreeCursorNew(
979 lsm_db *pDb,
980 Segment *pSeg,
981 BtreeCursor **ppCsr
983 int rc = LSM_OK;
984 BtreeCursor *pCsr;
986 assert( pSeg->iRoot );
987 pCsr = lsmMallocZeroRc(pDb->pEnv, sizeof(BtreeCursor), &rc);
988 if( pCsr ){
989 pCsr->pFS = pDb->pFS;
990 pCsr->pSeg = pSeg;
991 pCsr->iPg = -1;
994 *ppCsr = pCsr;
995 return rc;
998 static void segmentPtrSetPage(SegmentPtr *pPtr, Page *pNext){
999 lsmFsPageRelease(pPtr->pPg);
1000 if( pNext ){
1001 int nData;
1002 u8 *aData = fsPageData(pNext, &nData);
1003 pPtr->nCell = pageGetNRec(aData, nData);
1004 pPtr->flags = (u16)pageGetFlags(aData, nData);
1005 pPtr->iPtr = pageGetPtr(aData, nData);
1007 pPtr->pPg = pNext;
1011 ** Load a new page into the SegmentPtr object pPtr.
1013 static int segmentPtrLoadPage(
1014 FileSystem *pFS,
1015 SegmentPtr *pPtr, /* Load page into this SegmentPtr object */
1016 int iNew /* Page number of new page */
1018 Page *pPg = 0; /* The new page */
1019 int rc; /* Return Code */
1021 rc = lsmFsDbPageGet(pFS, pPtr->pSeg, iNew, &pPg);
1022 assert( rc==LSM_OK || pPg==0 );
1023 segmentPtrSetPage(pPtr, pPg);
1025 return rc;
1028 static int segmentPtrReadData(
1029 SegmentPtr *pPtr,
1030 int iOff,
1031 int nByte,
1032 void **ppData,
1033 LsmBlob *pBlob
1035 return sortedReadData(pPtr->pSeg, pPtr->pPg, iOff, nByte, ppData, pBlob);
1038 static int segmentPtrNextPage(
1039 SegmentPtr *pPtr, /* Load page into this SegmentPtr object */
1040 int eDir /* +1 for next(), -1 for prev() */
1042 Page *pNext; /* New page to load */
1043 int rc; /* Return code */
1045 assert( eDir==1 || eDir==-1 );
1046 assert( pPtr->pPg );
1047 assert( pPtr->pSeg || eDir>0 );
1049 rc = lsmFsDbPageNext(pPtr->pSeg, pPtr->pPg, eDir, &pNext);
1050 assert( rc==LSM_OK || pNext==0 );
1051 segmentPtrSetPage(pPtr, pNext);
1052 return rc;
1055 static int segmentPtrLoadCell(
1056 SegmentPtr *pPtr, /* Load page into this SegmentPtr object */
1057 int iNew /* Cell number of new cell */
1059 int rc = LSM_OK;
1060 if( pPtr->pPg ){
1061 u8 *aData; /* Pointer to page data buffer */
1062 int iOff; /* Offset in aData[] to read from */
1063 int nPgsz; /* Size of page (aData[]) in bytes */
1065 assert( iNew<pPtr->nCell );
1066 pPtr->iCell = iNew;
1067 aData = fsPageData(pPtr->pPg, &nPgsz);
1068 iOff = lsmGetU16(&aData[SEGMENT_CELLPTR_OFFSET(nPgsz, pPtr->iCell)]);
1069 pPtr->eType = aData[iOff];
1070 iOff++;
1071 iOff += GETVARINT64(&aData[iOff], pPtr->iPgPtr);
1072 iOff += GETVARINT32(&aData[iOff], pPtr->nKey);
1073 if( rtIsWrite(pPtr->eType) ){
1074 iOff += GETVARINT32(&aData[iOff], pPtr->nVal);
1076 assert( pPtr->nKey>=0 );
1078 rc = segmentPtrReadData(
1079 pPtr, iOff, pPtr->nKey, &pPtr->pKey, &pPtr->blob1
1081 if( rc==LSM_OK && rtIsWrite(pPtr->eType) ){
1082 rc = segmentPtrReadData(
1083 pPtr, iOff+pPtr->nKey, pPtr->nVal, &pPtr->pVal, &pPtr->blob2
1085 }else{
1086 pPtr->nVal = 0;
1087 pPtr->pVal = 0;
1091 return rc;
1095 static Segment *sortedSplitkeySegment(Level *pLevel){
1096 Merge *pMerge = pLevel->pMerge;
1097 MergeInput *p = &pMerge->splitkey;
1098 Segment *pSeg;
1099 int i;
1101 for(i=0; i<pMerge->nInput; i++){
1102 if( p->iPg==pMerge->aInput[i].iPg ) break;
1104 if( pMerge->nInput==(pLevel->nRight+1) && i>=(pMerge->nInput-1) ){
1105 pSeg = &pLevel->pNext->lhs;
1106 }else{
1107 pSeg = &pLevel->aRhs[i];
1110 return pSeg;
1113 static void sortedSplitkey(lsm_db *pDb, Level *pLevel, int *pRc){
1114 Segment *pSeg;
1115 Page *pPg = 0;
1116 lsm_env *pEnv = pDb->pEnv; /* Environment handle */
1117 int rc = *pRc;
1118 Merge *pMerge = pLevel->pMerge;
1120 pSeg = sortedSplitkeySegment(pLevel);
1121 if( rc==LSM_OK ){
1122 rc = lsmFsDbPageGet(pDb->pFS, pSeg, pMerge->splitkey.iPg, &pPg);
1124 if( rc==LSM_OK ){
1125 int iTopic;
1126 LsmBlob blob = {0, 0, 0, 0};
1127 u8 *aData;
1128 int nData;
1130 aData = lsmFsPageData(pPg, &nData);
1131 if( pageGetFlags(aData, nData) & SEGMENT_BTREE_FLAG ){
1132 void *pKey;
1133 int nKey;
1134 LsmPgno dummy;
1135 rc = pageGetBtreeKey(pSeg,
1136 pPg, pMerge->splitkey.iCell, &dummy, &iTopic, &pKey, &nKey, &blob
1138 if( rc==LSM_OK && blob.pData!=pKey ){
1139 rc = sortedBlobSet(pEnv, &blob, pKey, nKey);
1141 }else{
1142 rc = pageGetKeyCopy(
1143 pEnv, pSeg, pPg, pMerge->splitkey.iCell, &iTopic, &blob
1147 pLevel->iSplitTopic = iTopic;
1148 pLevel->pSplitKey = blob.pData;
1149 pLevel->nSplitKey = blob.nData;
1150 lsmFsPageRelease(pPg);
1153 *pRc = rc;
1157 ** Reset a segment cursor. Also free its buffers if they are nThreshold
1158 ** bytes or larger in size.
1160 static void segmentPtrReset(SegmentPtr *pPtr, int nThreshold){
1161 lsmFsPageRelease(pPtr->pPg);
1162 pPtr->pPg = 0;
1163 pPtr->nCell = 0;
1164 pPtr->pKey = 0;
1165 pPtr->nKey = 0;
1166 pPtr->pVal = 0;
1167 pPtr->nVal = 0;
1168 pPtr->eType = 0;
1169 pPtr->iCell = 0;
1170 if( pPtr->blob1.nAlloc>=nThreshold ) sortedBlobFree(&pPtr->blob1);
1171 if( pPtr->blob2.nAlloc>=nThreshold ) sortedBlobFree(&pPtr->blob2);
1174 static int segmentPtrIgnoreSeparators(MultiCursor *pCsr, SegmentPtr *pPtr){
1175 return (pCsr->flags & CURSOR_READ_SEPARATORS)==0
1176 || (pPtr!=&pCsr->aPtr[pCsr->nPtr-1]);
1179 static int segmentPtrAdvance(
1180 MultiCursor *pCsr,
1181 SegmentPtr *pPtr,
1182 int bReverse
1184 int eDir = (bReverse ? -1 : 1);
1185 Level *pLvl = pPtr->pLevel;
1186 do {
1187 int rc;
1188 int iCell; /* Number of new cell in page */
1189 int svFlags = 0; /* SegmentPtr.eType before advance */
1191 iCell = pPtr->iCell + eDir;
1192 assert( pPtr->pPg );
1193 assert( iCell<=pPtr->nCell && iCell>=-1 );
1195 if( bReverse && pPtr->pSeg!=&pPtr->pLevel->lhs ){
1196 svFlags = pPtr->eType;
1197 assert( svFlags );
1200 if( iCell>=pPtr->nCell || iCell<0 ){
1201 do {
1202 rc = segmentPtrNextPage(pPtr, eDir);
1203 }while( rc==LSM_OK
1204 && pPtr->pPg
1205 && (pPtr->nCell==0 || (pPtr->flags & SEGMENT_BTREE_FLAG) )
1207 if( rc!=LSM_OK ) return rc;
1208 iCell = bReverse ? (pPtr->nCell-1) : 0;
1210 rc = segmentPtrLoadCell(pPtr, iCell);
1211 if( rc!=LSM_OK ) return rc;
1213 if( svFlags && pPtr->pPg ){
1214 int res = sortedKeyCompare(pCsr->pDb->xCmp,
1215 rtTopic(pPtr->eType), pPtr->pKey, pPtr->nKey,
1216 pLvl->iSplitTopic, pLvl->pSplitKey, pLvl->nSplitKey
1218 if( res<0 ) segmentPtrReset(pPtr, LSM_SEGMENTPTR_FREE_THRESHOLD);
1221 if( pPtr->pPg==0 && (svFlags & LSM_END_DELETE) ){
1222 Segment *pSeg = pPtr->pSeg;
1223 rc = lsmFsDbPageGet(pCsr->pDb->pFS, pSeg, pSeg->iFirst, &pPtr->pPg);
1224 if( rc!=LSM_OK ) return rc;
1225 pPtr->eType = LSM_START_DELETE | LSM_POINT_DELETE;
1226 pPtr->eType |= (pLvl->iSplitTopic ? LSM_SYSTEMKEY : 0);
1227 pPtr->pKey = pLvl->pSplitKey;
1228 pPtr->nKey = pLvl->nSplitKey;
1231 }while( pCsr
1232 && pPtr->pPg
1233 && segmentPtrIgnoreSeparators(pCsr, pPtr)
1234 && rtIsSeparator(pPtr->eType)
1237 return LSM_OK;
1240 static void segmentPtrEndPage(
1241 FileSystem *pFS,
1242 SegmentPtr *pPtr,
1243 int bLast,
1244 int *pRc
1246 if( *pRc==LSM_OK ){
1247 Segment *pSeg = pPtr->pSeg;
1248 Page *pNew = 0;
1249 if( bLast ){
1250 *pRc = lsmFsDbPageLast(pFS, pSeg, &pNew);
1251 }else{
1252 *pRc = lsmFsDbPageGet(pFS, pSeg, pSeg->iFirst, &pNew);
1254 segmentPtrSetPage(pPtr, pNew);
1260 ** Try to move the segment pointer passed as the second argument so that it
1261 ** points at either the first (bLast==0) or last (bLast==1) cell in the valid
1262 ** region of the segment defined by pPtr->iFirst and pPtr->iLast.
1264 ** Return LSM_OK if successful or an lsm error code if something goes
1265 ** wrong (IO error, OOM etc.).
1267 static int segmentPtrEnd(MultiCursor *pCsr, SegmentPtr *pPtr, int bLast){
1268 Level *pLvl = pPtr->pLevel;
1269 int rc = LSM_OK;
1270 FileSystem *pFS = pCsr->pDb->pFS;
1271 int bIgnore;
1273 segmentPtrEndPage(pFS, pPtr, bLast, &rc);
1274 while( rc==LSM_OK && pPtr->pPg
1275 && (pPtr->nCell==0 || (pPtr->flags & SEGMENT_BTREE_FLAG))
1277 rc = segmentPtrNextPage(pPtr, (bLast ? -1 : 1));
1280 if( rc==LSM_OK && pPtr->pPg ){
1281 rc = segmentPtrLoadCell(pPtr, bLast ? (pPtr->nCell-1) : 0);
1282 if( rc==LSM_OK && bLast && pPtr->pSeg!=&pLvl->lhs ){
1283 int res = sortedKeyCompare(pCsr->pDb->xCmp,
1284 rtTopic(pPtr->eType), pPtr->pKey, pPtr->nKey,
1285 pLvl->iSplitTopic, pLvl->pSplitKey, pLvl->nSplitKey
1287 if( res<0 ) segmentPtrReset(pPtr, LSM_SEGMENTPTR_FREE_THRESHOLD);
1291 bIgnore = segmentPtrIgnoreSeparators(pCsr, pPtr);
1292 if( rc==LSM_OK && pPtr->pPg && bIgnore && rtIsSeparator(pPtr->eType) ){
1293 rc = segmentPtrAdvance(pCsr, pPtr, bLast);
1296 #if 0
1297 if( bLast && rc==LSM_OK && pPtr->pPg
1298 && pPtr->pSeg==&pLvl->lhs
1299 && pLvl->nRight && (pPtr->eType & LSM_START_DELETE)
1301 pPtr->iCell++;
1302 pPtr->eType = LSM_END_DELETE | (pLvl->iSplitTopic);
1303 pPtr->pKey = pLvl->pSplitKey;
1304 pPtr->nKey = pLvl->nSplitKey;
1305 pPtr->pVal = 0;
1306 pPtr->nVal = 0;
1308 #endif
1310 return rc;
1313 static void segmentPtrKey(SegmentPtr *pPtr, void **ppKey, int *pnKey){
1314 assert( pPtr->pPg );
1315 *ppKey = pPtr->pKey;
1316 *pnKey = pPtr->nKey;
1319 #if 0 /* NOT USED */
1320 static char *keyToString(lsm_env *pEnv, void *pKey, int nKey){
1321 int i;
1322 u8 *aKey = (u8 *)pKey;
1323 char *zRet = (char *)lsmMalloc(pEnv, nKey+1);
1325 for(i=0; i<nKey; i++){
1326 zRet[i] = (char)(isalnum(aKey[i]) ? aKey[i] : '.');
1328 zRet[nKey] = '\0';
1329 return zRet;
1331 #endif
1333 #if 0 /* NOT USED */
1335 ** Check that the page that pPtr currently has loaded is the correct page
1336 ** to search for key (pKey/nKey). If it is, return 1. Otherwise, an assert
1337 ** fails and this function does not return.
1339 static int assertKeyLocation(
1340 MultiCursor *pCsr,
1341 SegmentPtr *pPtr,
1342 void *pKey, int nKey
1344 lsm_env *pEnv = lsmFsEnv(pCsr->pDb->pFS);
1345 LsmBlob blob = {0, 0, 0};
1346 int eDir;
1347 int iTopic = 0; /* TODO: Fix me */
1349 for(eDir=-1; eDir<=1; eDir+=2){
1350 Page *pTest = pPtr->pPg;
1352 lsmFsPageRef(pTest);
1353 while( pTest ){
1354 Segment *pSeg = pPtr->pSeg;
1355 Page *pNext;
1357 int rc = lsmFsDbPageNext(pSeg, pTest, eDir, &pNext);
1358 lsmFsPageRelease(pTest);
1359 if( rc ) return 1;
1360 pTest = pNext;
1362 if( pTest ){
1363 int nData;
1364 u8 *aData = fsPageData(pTest, &nData);
1365 int nCell = pageGetNRec(aData, nData);
1366 int flags = pageGetFlags(aData, nData);
1367 if( nCell && 0==(flags&SEGMENT_BTREE_FLAG) ){
1368 int nPgKey;
1369 int iPgTopic;
1370 u8 *pPgKey;
1371 int res;
1372 int iCell;
1374 iCell = ((eDir < 0) ? (nCell-1) : 0);
1375 pPgKey = pageGetKey(pSeg, pTest, iCell, &iPgTopic, &nPgKey, &blob);
1376 res = iTopic - iPgTopic;
1377 if( res==0 ) res = pCsr->pDb->xCmp(pKey, nKey, pPgKey, nPgKey);
1378 if( (eDir==1 && res>0) || (eDir==-1 && res<0) ){
1379 /* Taking this branch means something has gone wrong. */
1380 char *zMsg = lsmMallocPrintf(pEnv, "Key \"%s\" is not on page %d",
1381 keyToString(pEnv, pKey, nKey), lsmFsPageNumber(pPtr->pPg)
1383 fprintf(stderr, "%s\n", zMsg);
1384 assert( !"assertKeyLocation() failed" );
1386 lsmFsPageRelease(pTest);
1387 pTest = 0;
1393 sortedBlobFree(&blob);
1394 return 1;
1396 #endif
1398 #ifndef NDEBUG
1399 static int assertSeekResult(
1400 MultiCursor *pCsr,
1401 SegmentPtr *pPtr,
1402 int iTopic,
1403 void *pKey,
1404 int nKey,
1405 int eSeek
1407 if( pPtr->pPg ){
1408 int res;
1409 res = sortedKeyCompare(pCsr->pDb->xCmp, iTopic, pKey, nKey,
1410 rtTopic(pPtr->eType), pPtr->pKey, pPtr->nKey
1413 if( eSeek==LSM_SEEK_EQ ) return (res==0);
1414 if( eSeek==LSM_SEEK_LE ) return (res>=0);
1415 if( eSeek==LSM_SEEK_GE ) return (res<=0);
1418 return 1;
1420 #endif
1422 static int segmentPtrSearchOversized(
1423 MultiCursor *pCsr, /* Cursor context */
1424 SegmentPtr *pPtr, /* Pointer to seek */
1425 int iTopic, /* Topic of key to search for */
1426 void *pKey, int nKey /* Key to seek to */
1428 int (*xCmp)(void *, int, void *, int) = pCsr->pDb->xCmp;
1429 int rc = LSM_OK;
1431 /* If the OVERSIZED flag is set, then there is no pointer in the
1432 ** upper level to the next page in the segment that contains at least
1433 ** one key. So compare the largest key on the current page with the
1434 ** key being sought (pKey/nKey). If (pKey/nKey) is larger, advance
1435 ** to the next page in the segment that contains at least one key.
1437 while( rc==LSM_OK && (pPtr->flags & PGFTR_SKIP_NEXT_FLAG) ){
1438 u8 *pLastKey;
1439 int nLastKey;
1440 int iLastTopic;
1441 int res; /* Result of comparison */
1442 Page *pNext;
1444 /* Load the last key on the current page. */
1445 pLastKey = pageGetKey(pPtr->pSeg,
1446 pPtr->pPg, pPtr->nCell-1, &iLastTopic, &nLastKey, &pPtr->blob1
1449 /* If the loaded key is >= than (pKey/nKey), break out of the loop.
1450 ** If (pKey/nKey) is present in this array, it must be on the current
1451 ** page. */
1452 res = sortedKeyCompare(
1453 xCmp, iLastTopic, pLastKey, nLastKey, iTopic, pKey, nKey
1455 if( res>=0 ) break;
1457 /* Advance to the next page that contains at least one key. */
1458 pNext = pPtr->pPg;
1459 lsmFsPageRef(pNext);
1460 while( 1 ){
1461 Page *pLoad;
1462 u8 *aData; int nData;
1464 rc = lsmFsDbPageNext(pPtr->pSeg, pNext, 1, &pLoad);
1465 lsmFsPageRelease(pNext);
1466 pNext = pLoad;
1467 if( pNext==0 ) break;
1469 assert( rc==LSM_OK );
1470 aData = lsmFsPageData(pNext, &nData);
1471 if( (pageGetFlags(aData, nData) & SEGMENT_BTREE_FLAG)==0
1472 && pageGetNRec(aData, nData)>0
1474 break;
1477 if( pNext==0 ) break;
1478 segmentPtrSetPage(pPtr, pNext);
1480 /* This should probably be an LSM_CORRUPT error. */
1481 assert( rc!=LSM_OK || (pPtr->flags & PGFTR_SKIP_THIS_FLAG) );
1484 return rc;
1487 static int ptrFwdPointer(
1488 Page *pPage,
1489 int iCell,
1490 Segment *pSeg,
1491 LsmPgno *piPtr,
1492 int *pbFound
1494 Page *pPg = pPage;
1495 int iFirst = iCell;
1496 int rc = LSM_OK;
1498 do {
1499 Page *pNext = 0;
1500 u8 *aData;
1501 int nData;
1503 aData = lsmFsPageData(pPg, &nData);
1504 if( (pageGetFlags(aData, nData) & SEGMENT_BTREE_FLAG)==0 ){
1505 int i;
1506 int nCell = pageGetNRec(aData, nData);
1507 for(i=iFirst; i<nCell; i++){
1508 u8 eType = *pageGetCell(aData, nData, i);
1509 if( (eType & LSM_START_DELETE)==0 ){
1510 *pbFound = 1;
1511 *piPtr = pageGetRecordPtr(aData, nData, i) + pageGetPtr(aData, nData);
1512 lsmFsPageRelease(pPg);
1513 return LSM_OK;
1518 rc = lsmFsDbPageNext(pSeg, pPg, 1, &pNext);
1519 lsmFsPageRelease(pPg);
1520 pPg = pNext;
1521 iFirst = 0;
1522 }while( pPg && rc==LSM_OK );
1523 lsmFsPageRelease(pPg);
1525 *pbFound = 0;
1526 return rc;
1529 static int sortedRhsFirst(MultiCursor *pCsr, Level *pLvl, SegmentPtr *pPtr){
1530 int rc;
1531 rc = segmentPtrEnd(pCsr, pPtr, 0);
1532 while( pPtr->pPg && rc==LSM_OK ){
1533 int res = sortedKeyCompare(pCsr->pDb->xCmp,
1534 pLvl->iSplitTopic, pLvl->pSplitKey, pLvl->nSplitKey,
1535 rtTopic(pPtr->eType), pPtr->pKey, pPtr->nKey
1537 if( res<=0 ) break;
1538 rc = segmentPtrAdvance(pCsr, pPtr, 0);
1540 return rc;
1545 ** This function is called as part of a SEEK_GE op on a multi-cursor if the
1546 ** FC pointer read from segment *pPtr comes from an entry with the
1547 ** LSM_START_DELETE flag set. In this case the pointer value cannot be
1548 ** trusted. Instead, the pointer that should be followed is that associated
1549 ** with the next entry in *pPtr that does not have LSM_START_DELETE set.
1551 ** Why the pointers can't be trusted:
1555 ** TODO: This is a stop-gap solution:
1557 ** At the moment, this function is called from within segmentPtrSeek(),
1558 ** as part of the initial lsmMCursorSeek() call. However, consider a
1559 ** database where the following has occurred:
1561 ** 1. A range delete removes keys 1..9999 using a range delete.
1562 ** 2. Keys 1 through 9999 are reinserted.
1563 ** 3. The levels containing the ops in 1. and 2. above are merged. Call
1564 ** this level N. Level N contains FC pointers to level N+1.
1566 ** Then, if the user attempts to query for (key>=2 LIMIT 10), the
1567 ** lsmMCursorSeek() call will iterate through 9998 entries searching for a
1568 ** pointer down to the level N+1 that is never actually used. It would be
1569 ** much better if the multi-cursor could do this lazily - only seek to the
1570 ** level (N+1) page after the user has moved the cursor on level N passed
1571 ** the big range-delete.
1573 static int segmentPtrFwdPointer(
1574 MultiCursor *pCsr, /* Multi-cursor pPtr belongs to */
1575 SegmentPtr *pPtr, /* Segment-pointer to extract FC ptr from */
1576 LsmPgno *piPtr /* OUT: FC pointer value */
1578 Level *pLvl = pPtr->pLevel;
1579 Level *pNext = pLvl->pNext;
1580 Page *pPg = pPtr->pPg;
1581 int rc;
1582 int bFound;
1583 LsmPgno iOut = 0;
1585 if( pPtr->pSeg==&pLvl->lhs || pPtr->pSeg==&pLvl->aRhs[pLvl->nRight-1] ){
1586 if( pNext==0
1587 || (pNext->nRight==0 && pNext->lhs.iRoot)
1588 || (pNext->nRight!=0 && pNext->aRhs[0].iRoot)
1590 /* Do nothing. The pointer will not be used anyway. */
1591 return LSM_OK;
1593 }else{
1594 if( pPtr[1].pSeg->iRoot ){
1595 return LSM_OK;
1599 /* Search for a pointer within the current segment. */
1600 lsmFsPageRef(pPg);
1601 rc = ptrFwdPointer(pPg, pPtr->iCell, pPtr->pSeg, &iOut, &bFound);
1603 if( rc==LSM_OK && bFound==0 ){
1604 /* This case happens when pPtr points to the left-hand-side of a segment
1605 ** currently undergoing an incremental merge. In this case, jump to the
1606 ** oldest segment in the right-hand-side of the same level and continue
1607 ** searching. But - do not consider any keys smaller than the levels
1608 ** split-key. */
1609 SegmentPtr ptr;
1611 if( pPtr->pLevel->nRight==0 || pPtr->pSeg!=&pPtr->pLevel->lhs ){
1612 return LSM_CORRUPT_BKPT;
1615 memset(&ptr, 0, sizeof(SegmentPtr));
1616 ptr.pLevel = pPtr->pLevel;
1617 ptr.pSeg = &ptr.pLevel->aRhs[ptr.pLevel->nRight-1];
1618 rc = sortedRhsFirst(pCsr, ptr.pLevel, &ptr);
1619 if( rc==LSM_OK ){
1620 rc = ptrFwdPointer(ptr.pPg, ptr.iCell, ptr.pSeg, &iOut, &bFound);
1621 ptr.pPg = 0;
1623 segmentPtrReset(&ptr, 0);
1626 *piPtr = iOut;
1627 return rc;
1630 static int segmentPtrSeek(
1631 MultiCursor *pCsr, /* Cursor context */
1632 SegmentPtr *pPtr, /* Pointer to seek */
1633 int iTopic, /* Key topic to seek to */
1634 void *pKey, int nKey, /* Key to seek to */
1635 int eSeek, /* Search bias - see above */
1636 int *piPtr, /* OUT: FC pointer */
1637 int *pbStop
1639 int (*xCmp)(void *, int, void *, int) = pCsr->pDb->xCmp;
1640 int res = 0; /* Result of comparison operation */
1641 int rc = LSM_OK;
1642 int iMin;
1643 int iMax;
1644 LsmPgno iPtrOut = 0;
1646 /* If the current page contains an oversized entry, then there are no
1647 ** pointers to one or more of the subsequent pages in the sorted run.
1648 ** The following call ensures that the segment-ptr points to the correct
1649 ** page in this case. */
1650 rc = segmentPtrSearchOversized(pCsr, pPtr, iTopic, pKey, nKey);
1651 iPtrOut = pPtr->iPtr;
1653 /* Assert that this page is the right page of this segment for the key
1654 ** that we are searching for. Do this by loading page (iPg-1) and testing
1655 ** that pKey/nKey is greater than all keys on that page, and then by
1656 ** loading (iPg+1) and testing that pKey/nKey is smaller than all
1657 ** the keys it houses.
1659 ** TODO: With range-deletes in the tree, the test described above may fail.
1661 #if 0
1662 assert( assertKeyLocation(pCsr, pPtr, pKey, nKey) );
1663 #endif
1665 assert( pPtr->nCell>0
1666 || pPtr->pSeg->nSize==1
1667 || lsmFsDbPageIsLast(pPtr->pSeg, pPtr->pPg)
1669 if( pPtr->nCell==0 ){
1670 segmentPtrReset(pPtr, LSM_SEGMENTPTR_FREE_THRESHOLD);
1671 }else{
1672 iMin = 0;
1673 iMax = pPtr->nCell-1;
1675 while( 1 ){
1676 int iTry = (iMin+iMax)/2;
1677 void *pKeyT; int nKeyT; /* Key for cell iTry */
1678 int iTopicT;
1680 assert( iTry<iMax || iMin==iMax );
1682 rc = segmentPtrLoadCell(pPtr, iTry);
1683 if( rc!=LSM_OK ) break;
1685 segmentPtrKey(pPtr, &pKeyT, &nKeyT);
1686 iTopicT = rtTopic(pPtr->eType);
1688 res = sortedKeyCompare(xCmp, iTopicT, pKeyT, nKeyT, iTopic, pKey, nKey);
1689 if( res<=0 ){
1690 iPtrOut = pPtr->iPtr + pPtr->iPgPtr;
1693 if( res==0 || iMin==iMax ){
1694 break;
1695 }else if( res>0 ){
1696 iMax = LSM_MAX(iTry-1, iMin);
1697 }else{
1698 iMin = iTry+1;
1702 if( rc==LSM_OK ){
1703 assert( res==0 || (iMin==iMax && iMin>=0 && iMin<pPtr->nCell) );
1704 if( res ){
1705 rc = segmentPtrLoadCell(pPtr, iMin);
1707 assert( rc!=LSM_OK || res>0 || iPtrOut==(pPtr->iPtr + pPtr->iPgPtr) );
1709 if( rc==LSM_OK ){
1710 switch( eSeek ){
1711 case LSM_SEEK_EQ: {
1712 int eType = pPtr->eType;
1713 if( (res<0 && (eType & LSM_START_DELETE))
1714 || (res>0 && (eType & LSM_END_DELETE))
1715 || (res==0 && (eType & LSM_POINT_DELETE))
1717 *pbStop = 1;
1718 }else if( res==0 && (eType & LSM_INSERT) ){
1719 lsm_env *pEnv = pCsr->pDb->pEnv;
1720 *pbStop = 1;
1721 pCsr->eType = pPtr->eType;
1722 rc = sortedBlobSet(pEnv, &pCsr->key, pPtr->pKey, pPtr->nKey);
1723 if( rc==LSM_OK ){
1724 rc = sortedBlobSet(pEnv, &pCsr->val, pPtr->pVal, pPtr->nVal);
1726 pCsr->flags |= CURSOR_SEEK_EQ;
1728 segmentPtrReset(pPtr, LSM_SEGMENTPTR_FREE_THRESHOLD);
1729 break;
1731 case LSM_SEEK_LE:
1732 if( res>0 ) rc = segmentPtrAdvance(pCsr, pPtr, 1);
1733 break;
1734 case LSM_SEEK_GE: {
1735 /* Figure out if we need to 'skip' the pointer forward or not */
1736 if( (res<=0 && (pPtr->eType & LSM_START_DELETE))
1737 || (res>0 && (pPtr->eType & LSM_END_DELETE))
1739 rc = segmentPtrFwdPointer(pCsr, pPtr, &iPtrOut);
1741 if( res<0 && rc==LSM_OK ){
1742 rc = segmentPtrAdvance(pCsr, pPtr, 0);
1744 break;
1750 /* If the cursor seek has found a separator key, and this cursor is
1751 ** supposed to ignore separators keys, advance to the next entry. */
1752 if( rc==LSM_OK && pPtr->pPg
1753 && segmentPtrIgnoreSeparators(pCsr, pPtr)
1754 && rtIsSeparator(pPtr->eType)
1756 assert( eSeek!=LSM_SEEK_EQ );
1757 rc = segmentPtrAdvance(pCsr, pPtr, eSeek==LSM_SEEK_LE);
1761 assert( rc!=LSM_OK || assertSeekResult(pCsr,pPtr,iTopic,pKey,nKey,eSeek) );
1762 *piPtr = (int)iPtrOut;
1763 return rc;
1766 static int seekInBtree(
1767 MultiCursor *pCsr, /* Multi-cursor object */
1768 Segment *pSeg, /* Seek within this segment */
1769 int iTopic,
1770 void *pKey, int nKey, /* Key to seek to */
1771 LsmPgno *aPg, /* OUT: Page numbers */
1772 Page **ppPg /* OUT: Leaf (sorted-run) page reference */
1774 int i = 0;
1775 int rc;
1776 int iPg;
1777 Page *pPg = 0;
1778 LsmBlob blob = {0, 0, 0};
1780 iPg = (int)pSeg->iRoot;
1781 do {
1782 LsmPgno *piFirst = 0;
1783 if( aPg ){
1784 aPg[i++] = iPg;
1785 piFirst = &aPg[i];
1788 rc = lsmFsDbPageGet(pCsr->pDb->pFS, pSeg, iPg, &pPg);
1789 assert( rc==LSM_OK || pPg==0 );
1790 if( rc==LSM_OK ){
1791 u8 *aData; /* Buffer containing page data */
1792 int nData; /* Size of aData[] in bytes */
1793 int iMin;
1794 int iMax;
1795 int nRec;
1796 int flags;
1798 aData = fsPageData(pPg, &nData);
1799 flags = pageGetFlags(aData, nData);
1800 if( (flags & SEGMENT_BTREE_FLAG)==0 ) break;
1802 iPg = (int)pageGetPtr(aData, nData);
1803 nRec = pageGetNRec(aData, nData);
1805 iMin = 0;
1806 iMax = nRec-1;
1807 while( iMax>=iMin ){
1808 int iTry = (iMin+iMax)/2;
1809 void *pKeyT; int nKeyT; /* Key for cell iTry */
1810 int iTopicT; /* Topic for key pKeyT/nKeyT */
1811 LsmPgno iPtr; /* Pointer associated with cell iTry */
1812 int res; /* (pKey - pKeyT) */
1814 rc = pageGetBtreeKey(
1815 pSeg, pPg, iTry, &iPtr, &iTopicT, &pKeyT, &nKeyT, &blob
1817 if( rc!=LSM_OK ) break;
1818 if( piFirst && pKeyT==blob.pData ){
1819 *piFirst = pageGetBtreeRef(pPg, iTry);
1820 piFirst = 0;
1821 i++;
1824 res = sortedKeyCompare(
1825 pCsr->pDb->xCmp, iTopic, pKey, nKey, iTopicT, pKeyT, nKeyT
1827 if( res<0 ){
1828 iPg = (int)iPtr;
1829 iMax = iTry-1;
1830 }else{
1831 iMin = iTry+1;
1834 lsmFsPageRelease(pPg);
1835 pPg = 0;
1837 }while( rc==LSM_OK );
1839 sortedBlobFree(&blob);
1840 assert( (rc==LSM_OK)==(pPg!=0) );
1841 if( ppPg ){
1842 *ppPg = pPg;
1843 }else{
1844 lsmFsPageRelease(pPg);
1846 return rc;
1849 static int seekInSegment(
1850 MultiCursor *pCsr,
1851 SegmentPtr *pPtr,
1852 int iTopic,
1853 void *pKey, int nKey,
1854 int iPg, /* Page to search */
1855 int eSeek, /* Search bias - see above */
1856 int *piPtr, /* OUT: FC pointer */
1857 int *pbStop /* OUT: Stop search flag */
1859 int iPtr = iPg;
1860 int rc = LSM_OK;
1862 if( pPtr->pSeg->iRoot ){
1863 Page *pPg;
1864 assert( pPtr->pSeg->iRoot!=0 );
1865 rc = seekInBtree(pCsr, pPtr->pSeg, iTopic, pKey, nKey, 0, &pPg);
1866 if( rc==LSM_OK ) segmentPtrSetPage(pPtr, pPg);
1867 }else{
1868 if( iPtr==0 ){
1869 iPtr = (int)pPtr->pSeg->iFirst;
1871 if( rc==LSM_OK ){
1872 rc = segmentPtrLoadPage(pCsr->pDb->pFS, pPtr, iPtr);
1876 if( rc==LSM_OK ){
1877 rc = segmentPtrSeek(pCsr, pPtr, iTopic, pKey, nKey, eSeek, piPtr, pbStop);
1879 return rc;
1883 ** Seek each segment pointer in the array of (pLvl->nRight+1) at aPtr[].
1885 ** pbStop:
1886 ** This parameter is only significant if parameter eSeek is set to
1887 ** LSM_SEEK_EQ. In this case, it is set to true before returning if
1888 ** the seek operation is finished. This can happen in two ways:
1890 ** a) A key matching (pKey/nKey) is found, or
1891 ** b) A point-delete or range-delete deleting the key is found.
1893 ** In case (a), the multi-cursor CURSOR_SEEK_EQ flag is set and the pCsr->key
1894 ** and pCsr->val blobs populated before returning.
1896 static int seekInLevel(
1897 MultiCursor *pCsr, /* Sorted cursor object to seek */
1898 SegmentPtr *aPtr, /* Pointer to array of (nRhs+1) SPs */
1899 int eSeek, /* Search bias - see above */
1900 int iTopic, /* Key topic to search for */
1901 void *pKey, int nKey, /* Key to search for */
1902 LsmPgno *piPgno, /* IN/OUT: fraction cascade pointer (or 0) */
1903 int *pbStop /* OUT: See above */
1905 Level *pLvl = aPtr[0].pLevel; /* Level to seek within */
1906 int rc = LSM_OK; /* Return code */
1907 int iOut = 0; /* Pointer to return to caller */
1908 int res = -1; /* Result of xCmp(pKey, split) */
1909 int nRhs = pLvl->nRight; /* Number of right-hand-side segments */
1910 int bStop = 0;
1912 /* If this is a composite level (one currently undergoing an incremental
1913 ** merge), figure out if the search key is larger or smaller than the
1914 ** levels split-key. */
1915 if( nRhs ){
1916 res = sortedKeyCompare(pCsr->pDb->xCmp, iTopic, pKey, nKey,
1917 pLvl->iSplitTopic, pLvl->pSplitKey, pLvl->nSplitKey
1921 /* If (res<0), then key pKey/nKey is smaller than the split-key (or this
1922 ** is not a composite level and there is no split-key). Search the
1923 ** left-hand-side of the level in this case. */
1924 if( res<0 ){
1925 int iPtr = 0;
1926 if( nRhs==0 ) iPtr = (int)*piPgno;
1928 rc = seekInSegment(
1929 pCsr, &aPtr[0], iTopic, pKey, nKey, iPtr, eSeek, &iOut, &bStop
1931 if( rc==LSM_OK && nRhs>0 && eSeek==LSM_SEEK_GE && aPtr[0].pPg==0 ){
1932 res = 0;
1936 if( res>=0 ){
1937 int bHit = 0; /* True if at least one rhs is not EOF */
1938 int iPtr = (int)*piPgno;
1939 int i;
1940 for(i=1; rc==LSM_OK && i<=nRhs && bStop==0; i++){
1941 SegmentPtr *pPtr = &aPtr[i];
1942 iOut = 0;
1943 rc = seekInSegment(
1944 pCsr, pPtr, iTopic, pKey, nKey, iPtr, eSeek, &iOut, &bStop
1946 iPtr = iOut;
1948 /* If the segment-pointer has settled on a key that is smaller than
1949 ** the splitkey, invalidate the segment-pointer. */
1950 if( pPtr->pPg ){
1951 res = sortedKeyCompare(pCsr->pDb->xCmp,
1952 rtTopic(pPtr->eType), pPtr->pKey, pPtr->nKey,
1953 pLvl->iSplitTopic, pLvl->pSplitKey, pLvl->nSplitKey
1955 if( res<0 ){
1956 if( pPtr->eType & LSM_START_DELETE ){
1957 pPtr->eType &= ~LSM_INSERT;
1958 pPtr->pKey = pLvl->pSplitKey;
1959 pPtr->nKey = pLvl->nSplitKey;
1960 pPtr->pVal = 0;
1961 pPtr->nVal = 0;
1962 }else{
1963 segmentPtrReset(pPtr, LSM_SEGMENTPTR_FREE_THRESHOLD);
1968 if( aPtr[i].pKey ) bHit = 1;
1971 if( rc==LSM_OK && eSeek==LSM_SEEK_LE && bHit==0 ){
1972 rc = segmentPtrEnd(pCsr, &aPtr[0], 1);
1976 assert( eSeek==LSM_SEEK_EQ || bStop==0 );
1977 *piPgno = iOut;
1978 *pbStop = bStop;
1979 return rc;
1982 static void multiCursorGetKey(
1983 MultiCursor *pCsr,
1984 int iKey,
1985 int *peType, /* OUT: Key type (SORTED_WRITE etc.) */
1986 void **ppKey, /* OUT: Pointer to buffer containing key */
1987 int *pnKey /* OUT: Size of *ppKey in bytes */
1989 int nKey = 0;
1990 void *pKey = 0;
1991 int eType = 0;
1993 switch( iKey ){
1994 case CURSOR_DATA_TREE0:
1995 case CURSOR_DATA_TREE1: {
1996 TreeCursor *pTreeCsr = pCsr->apTreeCsr[iKey-CURSOR_DATA_TREE0];
1997 if( lsmTreeCursorValid(pTreeCsr) ){
1998 lsmTreeCursorKey(pTreeCsr, &eType, &pKey, &nKey);
2000 break;
2003 case CURSOR_DATA_SYSTEM: {
2004 Snapshot *pWorker = pCsr->pDb->pWorker;
2005 if( pWorker && (pCsr->flags & CURSOR_FLUSH_FREELIST) ){
2006 int nEntry = pWorker->freelist.nEntry;
2007 if( pCsr->iFree < (nEntry*2) ){
2008 FreelistEntry *aEntry = pWorker->freelist.aEntry;
2009 int i = nEntry - 1 - (pCsr->iFree / 2);
2010 u32 iKey2 = 0;
2012 if( (pCsr->iFree % 2) ){
2013 eType = LSM_END_DELETE|LSM_SYSTEMKEY;
2014 iKey2 = aEntry[i].iBlk-1;
2015 }else if( aEntry[i].iId>=0 ){
2016 eType = LSM_INSERT|LSM_SYSTEMKEY;
2017 iKey2 = aEntry[i].iBlk;
2019 /* If the in-memory entry immediately before this one was a
2020 ** DELETE, and the block number is one greater than the current
2021 ** block number, mark this entry as an "end-delete-range". */
2022 if( i<(nEntry-1) && aEntry[i+1].iBlk==iKey2+1 && aEntry[i+1].iId<0 ){
2023 eType |= LSM_END_DELETE;
2026 }else{
2027 eType = LSM_START_DELETE|LSM_SYSTEMKEY;
2028 iKey2 = aEntry[i].iBlk + 1;
2031 /* If the in-memory entry immediately after this one is a
2032 ** DELETE, and the block number is one less than the current
2033 ** key, mark this entry as an "start-delete-range". */
2034 if( i>0 && aEntry[i-1].iBlk==iKey2-1 && aEntry[i-1].iId<0 ){
2035 eType |= LSM_START_DELETE;
2038 pKey = pCsr->pSystemVal;
2039 nKey = 4;
2040 lsmPutU32(pKey, ~iKey2);
2043 break;
2046 default: {
2047 int iPtr = iKey - CURSOR_DATA_SEGMENT;
2048 assert( iPtr>=0 );
2049 if( iPtr==pCsr->nPtr ){
2050 if( pCsr->pBtCsr ){
2051 pKey = pCsr->pBtCsr->pKey;
2052 nKey = pCsr->pBtCsr->nKey;
2053 eType = pCsr->pBtCsr->eType;
2055 }else if( iPtr<pCsr->nPtr ){
2056 SegmentPtr *pPtr = &pCsr->aPtr[iPtr];
2057 if( pPtr->pPg ){
2058 pKey = pPtr->pKey;
2059 nKey = pPtr->nKey;
2060 eType = pPtr->eType;
2063 break;
2067 if( peType ) *peType = eType;
2068 if( pnKey ) *pnKey = nKey;
2069 if( ppKey ) *ppKey = pKey;
2072 static int sortedDbKeyCompare(
2073 MultiCursor *pCsr,
2074 int iLhsFlags, void *pLhsKey, int nLhsKey,
2075 int iRhsFlags, void *pRhsKey, int nRhsKey
2077 int (*xCmp)(void *, int, void *, int) = pCsr->pDb->xCmp;
2078 int res;
2080 /* Compare the keys, including the system flag. */
2081 res = sortedKeyCompare(xCmp,
2082 rtTopic(iLhsFlags), pLhsKey, nLhsKey,
2083 rtTopic(iRhsFlags), pRhsKey, nRhsKey
2086 /* If a key has the LSM_START_DELETE flag set, but not the LSM_INSERT or
2087 ** LSM_POINT_DELETE flags, it is considered a delta larger. This prevents
2088 ** the beginning of an open-ended set from masking a database entry or
2089 ** delete at a lower level. */
2090 if( res==0 && (pCsr->flags & CURSOR_IGNORE_DELETE) ){
2091 const int m = LSM_POINT_DELETE|LSM_INSERT|LSM_END_DELETE |LSM_START_DELETE;
2092 int iDel1 = 0;
2093 int iDel2 = 0;
2095 if( LSM_START_DELETE==(iLhsFlags & m) ) iDel1 = +1;
2096 if( LSM_END_DELETE ==(iLhsFlags & m) ) iDel1 = -1;
2097 if( LSM_START_DELETE==(iRhsFlags & m) ) iDel2 = +1;
2098 if( LSM_END_DELETE ==(iRhsFlags & m) ) iDel2 = -1;
2100 res = (iDel1 - iDel2);
2103 return res;
2106 static void multiCursorDoCompare(MultiCursor *pCsr, int iOut, int bReverse){
2107 int i1;
2108 int i2;
2109 int iRes;
2110 void *pKey1; int nKey1; int eType1;
2111 void *pKey2; int nKey2; int eType2;
2112 const int mul = (bReverse ? -1 : 1);
2114 assert( pCsr->aTree && iOut<pCsr->nTree );
2115 if( iOut>=(pCsr->nTree/2) ){
2116 i1 = (iOut - pCsr->nTree/2) * 2;
2117 i2 = i1 + 1;
2118 }else{
2119 i1 = pCsr->aTree[iOut*2];
2120 i2 = pCsr->aTree[iOut*2+1];
2123 multiCursorGetKey(pCsr, i1, &eType1, &pKey1, &nKey1);
2124 multiCursorGetKey(pCsr, i2, &eType2, &pKey2, &nKey2);
2126 if( pKey1==0 ){
2127 iRes = i2;
2128 }else if( pKey2==0 ){
2129 iRes = i1;
2130 }else{
2131 int res;
2133 /* Compare the keys */
2134 res = sortedDbKeyCompare(pCsr,
2135 eType1, pKey1, nKey1, eType2, pKey2, nKey2
2138 res = res * mul;
2139 if( res==0 ){
2140 /* The two keys are identical. Normally, this means that the key from
2141 ** the newer run clobbers the old. However, if the newer key is a
2142 ** separator key, or a range-delete-boundary only, do not allow it
2143 ** to clobber an older entry. */
2144 int nc1 = (eType1 & (LSM_INSERT|LSM_POINT_DELETE))==0;
2145 int nc2 = (eType2 & (LSM_INSERT|LSM_POINT_DELETE))==0;
2146 iRes = (nc1 > nc2) ? i2 : i1;
2147 }else if( res<0 ){
2148 iRes = i1;
2149 }else{
2150 iRes = i2;
2154 pCsr->aTree[iOut] = iRes;
2158 ** This function advances segment pointer iPtr belonging to multi-cursor
2159 ** pCsr forward (bReverse==0) or backward (bReverse!=0).
2161 ** If the segment pointer points to a segment that is part of a composite
2162 ** level, then the following special case is handled.
2164 ** * If iPtr is the lhs of a composite level, and the cursor is being
2165 ** advanced forwards, and segment iPtr is at EOF, move all pointers
2166 ** that correspond to rhs segments of the same level to the first
2167 ** key in their respective data.
2169 static int segmentCursorAdvance(
2170 MultiCursor *pCsr,
2171 int iPtr,
2172 int bReverse
2174 int rc;
2175 SegmentPtr *pPtr = &pCsr->aPtr[iPtr];
2176 Level *pLvl = pPtr->pLevel;
2177 int bComposite; /* True if pPtr is part of composite level */
2179 /* Advance the segment-pointer object. */
2180 rc = segmentPtrAdvance(pCsr, pPtr, bReverse);
2181 if( rc!=LSM_OK ) return rc;
2183 bComposite = (pLvl->nRight>0 && pCsr->nPtr>pLvl->nRight);
2184 if( bComposite && pPtr->pPg==0 ){
2185 int bFix = 0;
2186 if( (bReverse==0)==(pPtr->pSeg==&pLvl->lhs) ){
2187 int i;
2188 if( bReverse ){
2189 SegmentPtr *pLhs = &pCsr->aPtr[iPtr - 1 - (pPtr->pSeg - pLvl->aRhs)];
2190 for(i=0; i<pLvl->nRight; i++){
2191 if( pLhs[i+1].pPg ) break;
2193 if( i==pLvl->nRight ){
2194 bFix = 1;
2195 rc = segmentPtrEnd(pCsr, pLhs, 1);
2197 }else{
2198 bFix = 1;
2199 for(i=0; rc==LSM_OK && i<pLvl->nRight; i++){
2200 rc = sortedRhsFirst(pCsr, pLvl, &pCsr->aPtr[iPtr+1+i]);
2205 if( bFix ){
2206 int i;
2207 for(i=pCsr->nTree-1; i>0; i--){
2208 multiCursorDoCompare(pCsr, i, bReverse);
2213 #if 0
2214 if( bComposite && pPtr->pSeg==&pLvl->lhs /* lhs of composite level */
2215 && bReverse==0 /* csr advanced forwards */
2216 && pPtr->pPg==0 /* segment at EOF */
2218 int i;
2219 for(i=0; rc==LSM_OK && i<pLvl->nRight; i++){
2220 rc = sortedRhsFirst(pCsr, pLvl, &pCsr->aPtr[iPtr+1+i]);
2222 for(i=pCsr->nTree-1; i>0; i--){
2223 multiCursorDoCompare(pCsr, i, 0);
2226 #endif
2228 return rc;
2231 static void mcursorFreeComponents(MultiCursor *pCsr){
2232 int i;
2233 lsm_env *pEnv = pCsr->pDb->pEnv;
2235 /* Close the tree cursor, if any. */
2236 lsmTreeCursorDestroy(pCsr->apTreeCsr[0]);
2237 lsmTreeCursorDestroy(pCsr->apTreeCsr[1]);
2239 /* Reset the segment pointers */
2240 for(i=0; i<pCsr->nPtr; i++){
2241 segmentPtrReset(&pCsr->aPtr[i], 0);
2244 /* And the b-tree cursor, if any */
2245 btreeCursorFree(pCsr->pBtCsr);
2247 /* Free allocations */
2248 lsmFree(pEnv, pCsr->aPtr);
2249 lsmFree(pEnv, pCsr->aTree);
2250 lsmFree(pEnv, pCsr->pSystemVal);
2252 /* Zero fields */
2253 pCsr->nPtr = 0;
2254 pCsr->aPtr = 0;
2255 pCsr->nTree = 0;
2256 pCsr->aTree = 0;
2257 pCsr->pSystemVal = 0;
2258 pCsr->apTreeCsr[0] = 0;
2259 pCsr->apTreeCsr[1] = 0;
2260 pCsr->pBtCsr = 0;
2263 void lsmMCursorFreeCache(lsm_db *pDb){
2264 MultiCursor *p;
2265 MultiCursor *pNext;
2266 for(p=pDb->pCsrCache; p; p=pNext){
2267 pNext = p->pNext;
2268 lsmMCursorClose(p, 0);
2270 pDb->pCsrCache = 0;
2274 ** Close the cursor passed as the first argument.
2276 ** If the bCache parameter is true, then shift the cursor to the pCsrCache
2277 ** list for possible reuse instead of actually deleting it.
2279 void lsmMCursorClose(MultiCursor *pCsr, int bCache){
2280 if( pCsr ){
2281 lsm_db *pDb = pCsr->pDb;
2282 MultiCursor **pp; /* Iterator variable */
2284 /* The cursor may or may not be currently part of the linked list
2285 ** starting at lsm_db.pCsr. If it is, extract it. */
2286 for(pp=&pDb->pCsr; *pp; pp=&((*pp)->pNext)){
2287 if( *pp==pCsr ){
2288 *pp = pCsr->pNext;
2289 break;
2293 if( bCache ){
2294 int i; /* Used to iterate through segment-pointers */
2296 /* Release any page references held by this cursor. */
2297 assert( !pCsr->pBtCsr );
2298 for(i=0; i<pCsr->nPtr; i++){
2299 SegmentPtr *pPtr = &pCsr->aPtr[i];
2300 lsmFsPageRelease(pPtr->pPg);
2301 pPtr->pPg = 0;
2304 /* Reset the tree cursors */
2305 lsmTreeCursorReset(pCsr->apTreeCsr[0]);
2306 lsmTreeCursorReset(pCsr->apTreeCsr[1]);
2308 /* Add the cursor to the pCsrCache list */
2309 pCsr->pNext = pDb->pCsrCache;
2310 pDb->pCsrCache = pCsr;
2311 }else{
2312 /* Free the allocation used to cache the current key, if any. */
2313 sortedBlobFree(&pCsr->key);
2314 sortedBlobFree(&pCsr->val);
2316 /* Free the component cursors */
2317 mcursorFreeComponents(pCsr);
2319 /* Free the cursor structure itself */
2320 lsmFree(pDb->pEnv, pCsr);
2325 #define TREE_NONE 0
2326 #define TREE_OLD 1
2327 #define TREE_BOTH 2
2330 ** Parameter eTree is one of TREE_OLD or TREE_BOTH.
2332 static int multiCursorAddTree(MultiCursor *pCsr, Snapshot *pSnap, int eTree){
2333 int rc = LSM_OK;
2334 lsm_db *db = pCsr->pDb;
2336 /* Add a tree cursor on the 'old' tree, if it exists. */
2337 if( eTree!=TREE_NONE
2338 && lsmTreeHasOld(db)
2339 && db->treehdr.iOldLog!=pSnap->iLogOff
2341 rc = lsmTreeCursorNew(db, 1, &pCsr->apTreeCsr[1]);
2344 /* Add a tree cursor on the 'current' tree, if required. */
2345 if( rc==LSM_OK && eTree==TREE_BOTH ){
2346 rc = lsmTreeCursorNew(db, 0, &pCsr->apTreeCsr[0]);
2349 return rc;
2352 static int multiCursorAddRhs(MultiCursor *pCsr, Level *pLvl){
2353 int i;
2354 int nRhs = pLvl->nRight;
2356 assert( pLvl->nRight>0 );
2357 assert( pCsr->aPtr==0 );
2358 pCsr->aPtr = lsmMallocZero(pCsr->pDb->pEnv, sizeof(SegmentPtr) * nRhs);
2359 if( !pCsr->aPtr ) return LSM_NOMEM_BKPT;
2360 pCsr->nPtr = nRhs;
2362 for(i=0; i<nRhs; i++){
2363 pCsr->aPtr[i].pSeg = &pLvl->aRhs[i];
2364 pCsr->aPtr[i].pLevel = pLvl;
2367 return LSM_OK;
2370 static void multiCursorAddOne(MultiCursor *pCsr, Level *pLvl, int *pRc){
2371 if( *pRc==LSM_OK ){
2372 int iPtr = pCsr->nPtr;
2373 int i;
2374 pCsr->aPtr[iPtr].pLevel = pLvl;
2375 pCsr->aPtr[iPtr].pSeg = &pLvl->lhs;
2376 iPtr++;
2377 for(i=0; i<pLvl->nRight; i++){
2378 pCsr->aPtr[iPtr].pLevel = pLvl;
2379 pCsr->aPtr[iPtr].pSeg = &pLvl->aRhs[i];
2380 iPtr++;
2383 if( pLvl->nRight && pLvl->pSplitKey==0 ){
2384 sortedSplitkey(pCsr->pDb, pLvl, pRc);
2386 pCsr->nPtr = iPtr;
2390 static int multiCursorAddAll(MultiCursor *pCsr, Snapshot *pSnap){
2391 Level *pLvl;
2392 int nPtr = 0;
2393 int rc = LSM_OK;
2395 for(pLvl=pSnap->pLevel; pLvl; pLvl=pLvl->pNext){
2396 /* If the LEVEL_INCOMPLETE flag is set, then this function is being
2397 ** called (indirectly) from within a sortedNewToplevel() call to
2398 ** construct pLvl. In this case ignore pLvl - this cursor is going to
2399 ** be used to retrieve a freelist entry from the LSM, and the partially
2400 ** complete level may confuse it. */
2401 if( pLvl->flags & LEVEL_INCOMPLETE ) continue;
2402 nPtr += (1 + pLvl->nRight);
2405 assert( pCsr->aPtr==0 );
2406 pCsr->aPtr = lsmMallocZeroRc(pCsr->pDb->pEnv, sizeof(SegmentPtr) * nPtr, &rc);
2408 for(pLvl=pSnap->pLevel; pLvl; pLvl=pLvl->pNext){
2409 if( (pLvl->flags & LEVEL_INCOMPLETE)==0 ){
2410 multiCursorAddOne(pCsr, pLvl, &rc);
2414 return rc;
2417 static int multiCursorInit(MultiCursor *pCsr, Snapshot *pSnap){
2418 int rc;
2419 rc = multiCursorAddAll(pCsr, pSnap);
2420 if( rc==LSM_OK ){
2421 rc = multiCursorAddTree(pCsr, pSnap, TREE_BOTH);
2423 pCsr->flags |= (CURSOR_IGNORE_SYSTEM | CURSOR_IGNORE_DELETE);
2424 return rc;
2427 static MultiCursor *multiCursorNew(lsm_db *db, int *pRc){
2428 MultiCursor *pCsr;
2429 pCsr = (MultiCursor *)lsmMallocZeroRc(db->pEnv, sizeof(MultiCursor), pRc);
2430 if( pCsr ){
2431 pCsr->pNext = db->pCsr;
2432 db->pCsr = pCsr;
2433 pCsr->pDb = db;
2435 return pCsr;
2439 void lsmSortedRemap(lsm_db *pDb){
2440 MultiCursor *pCsr;
2441 for(pCsr=pDb->pCsr; pCsr; pCsr=pCsr->pNext){
2442 int iPtr;
2443 if( pCsr->pBtCsr ){
2444 btreeCursorLoadKey(pCsr->pBtCsr);
2446 for(iPtr=0; iPtr<pCsr->nPtr; iPtr++){
2447 segmentPtrLoadCell(&pCsr->aPtr[iPtr], pCsr->aPtr[iPtr].iCell);
2452 static void multiCursorReadSeparators(MultiCursor *pCsr){
2453 if( pCsr->nPtr>0 ){
2454 pCsr->flags |= CURSOR_READ_SEPARATORS;
2459 ** Have this cursor skip over SORTED_DELETE entries.
2461 static void multiCursorIgnoreDelete(MultiCursor *pCsr){
2462 if( pCsr ) pCsr->flags |= CURSOR_IGNORE_DELETE;
2466 ** If the free-block list is not empty, then have this cursor visit a key
2467 ** with (a) the system bit set, and (b) the key "FREELIST" and (c) a value
2468 ** blob containing the serialized free-block list.
2470 static int multiCursorVisitFreelist(MultiCursor *pCsr){
2471 int rc = LSM_OK;
2472 pCsr->flags |= CURSOR_FLUSH_FREELIST;
2473 pCsr->pSystemVal = lsmMallocRc(pCsr->pDb->pEnv, 4 + 8, &rc);
2474 return rc;
2478 ** Allocate and return a new database cursor.
2480 ** This method should only be called to allocate user cursors. As it may
2481 ** recycle a cursor from lsm_db.pCsrCache.
2483 int lsmMCursorNew(
2484 lsm_db *pDb, /* Database handle */
2485 MultiCursor **ppCsr /* OUT: Allocated cursor */
2487 MultiCursor *pCsr = 0;
2488 int rc = LSM_OK;
2490 if( pDb->pCsrCache ){
2491 int bOld; /* True if there is an old in-memory tree */
2493 /* Remove a cursor from the pCsrCache list and add it to the open list. */
2494 pCsr = pDb->pCsrCache;
2495 pDb->pCsrCache = pCsr->pNext;
2496 pCsr->pNext = pDb->pCsr;
2497 pDb->pCsr = pCsr;
2499 /* The cursor can almost be used as is, except that the old in-memory
2500 ** tree cursor may be present and not required, or required and not
2501 ** present. Fix this if required. */
2502 bOld = (lsmTreeHasOld(pDb) && pDb->treehdr.iOldLog!=pDb->pClient->iLogOff);
2503 if( !bOld && pCsr->apTreeCsr[1] ){
2504 lsmTreeCursorDestroy(pCsr->apTreeCsr[1]);
2505 pCsr->apTreeCsr[1] = 0;
2506 }else if( bOld && !pCsr->apTreeCsr[1] ){
2507 rc = lsmTreeCursorNew(pDb, 1, &pCsr->apTreeCsr[1]);
2510 pCsr->flags = (CURSOR_IGNORE_SYSTEM | CURSOR_IGNORE_DELETE);
2512 }else{
2513 pCsr = multiCursorNew(pDb, &rc);
2514 if( rc==LSM_OK ) rc = multiCursorInit(pCsr, pDb->pClient);
2517 if( rc!=LSM_OK ){
2518 lsmMCursorClose(pCsr, 0);
2519 pCsr = 0;
2521 assert( (rc==LSM_OK)==(pCsr!=0) );
2522 *ppCsr = pCsr;
2523 return rc;
2526 static int multiCursorGetVal(
2527 MultiCursor *pCsr,
2528 int iVal,
2529 void **ppVal,
2530 int *pnVal
2532 int rc = LSM_OK;
2534 *ppVal = 0;
2535 *pnVal = 0;
2537 switch( iVal ){
2538 case CURSOR_DATA_TREE0:
2539 case CURSOR_DATA_TREE1: {
2540 TreeCursor *pTreeCsr = pCsr->apTreeCsr[iVal-CURSOR_DATA_TREE0];
2541 if( lsmTreeCursorValid(pTreeCsr) ){
2542 lsmTreeCursorValue(pTreeCsr, ppVal, pnVal);
2543 }else{
2544 *ppVal = 0;
2545 *pnVal = 0;
2547 break;
2550 case CURSOR_DATA_SYSTEM: {
2551 Snapshot *pWorker = pCsr->pDb->pWorker;
2552 if( pWorker
2553 && (pCsr->iFree % 2)==0
2554 && pCsr->iFree < (pWorker->freelist.nEntry*2)
2556 int iEntry = pWorker->freelist.nEntry - 1 - (pCsr->iFree / 2);
2557 u8 *aVal = &((u8 *)(pCsr->pSystemVal))[4];
2558 lsmPutU64(aVal, pWorker->freelist.aEntry[iEntry].iId);
2559 *ppVal = aVal;
2560 *pnVal = 8;
2562 break;
2565 default: {
2566 int iPtr = iVal-CURSOR_DATA_SEGMENT;
2567 if( iPtr<pCsr->nPtr ){
2568 SegmentPtr *pPtr = &pCsr->aPtr[iPtr];
2569 if( pPtr->pPg ){
2570 *ppVal = pPtr->pVal;
2571 *pnVal = pPtr->nVal;
2577 assert( rc==LSM_OK || (*ppVal==0 && *pnVal==0) );
2578 return rc;
2581 static int multiCursorAdvance(MultiCursor *pCsr, int bReverse);
2584 ** This function is called by worker connections to walk the part of the
2585 ** free-list stored within the LSM data structure.
2587 int lsmSortedWalkFreelist(
2588 lsm_db *pDb, /* Database handle */
2589 int bReverse, /* True to iterate from largest to smallest */
2590 int (*x)(void *, int, i64), /* Callback function */
2591 void *pCtx /* First argument to pass to callback */
2593 MultiCursor *pCsr; /* Cursor used to read db */
2594 int rc = LSM_OK; /* Return Code */
2595 Snapshot *pSnap = 0;
2597 assert( pDb->pWorker );
2598 if( pDb->bIncrMerge ){
2599 rc = lsmCheckpointDeserialize(pDb, 0, pDb->pShmhdr->aSnap1, &pSnap);
2600 if( rc!=LSM_OK ) return rc;
2601 }else{
2602 pSnap = pDb->pWorker;
2605 pCsr = multiCursorNew(pDb, &rc);
2606 if( pCsr ){
2607 rc = multiCursorAddAll(pCsr, pSnap);
2608 pCsr->flags |= CURSOR_IGNORE_DELETE;
2611 if( rc==LSM_OK ){
2612 if( bReverse==0 ){
2613 rc = lsmMCursorLast(pCsr);
2614 }else{
2615 rc = lsmMCursorSeek(pCsr, 1, "", 0, LSM_SEEK_GE);
2618 while( rc==LSM_OK && lsmMCursorValid(pCsr) && rtIsSystem(pCsr->eType) ){
2619 void *pKey; int nKey;
2620 void *pVal = 0; int nVal = 0;
2622 rc = lsmMCursorKey(pCsr, &pKey, &nKey);
2623 if( rc==LSM_OK ) rc = lsmMCursorValue(pCsr, &pVal, &nVal);
2624 if( rc==LSM_OK && (nKey!=4 || nVal!=8) ) rc = LSM_CORRUPT_BKPT;
2626 if( rc==LSM_OK ){
2627 int iBlk;
2628 i64 iSnap;
2629 iBlk = (int)(~(lsmGetU32((u8 *)pKey)));
2630 iSnap = (i64)lsmGetU64((u8 *)pVal);
2631 if( x(pCtx, iBlk, iSnap) ) break;
2632 rc = multiCursorAdvance(pCsr, !bReverse);
2637 lsmMCursorClose(pCsr, 0);
2638 if( pSnap!=pDb->pWorker ){
2639 lsmFreeSnapshot(pDb->pEnv, pSnap);
2642 return rc;
2645 int lsmSortedLoadFreelist(
2646 lsm_db *pDb, /* Database handle (must be worker) */
2647 void **ppVal, /* OUT: Blob containing LSM free-list */
2648 int *pnVal /* OUT: Size of *ppVal blob in bytes */
2650 MultiCursor *pCsr; /* Cursor used to retreive free-list */
2651 int rc = LSM_OK; /* Return Code */
2653 assert( pDb->pWorker );
2654 assert( *ppVal==0 && *pnVal==0 );
2656 pCsr = multiCursorNew(pDb, &rc);
2657 if( pCsr ){
2658 rc = multiCursorAddAll(pCsr, pDb->pWorker);
2659 pCsr->flags |= CURSOR_IGNORE_DELETE;
2662 if( rc==LSM_OK ){
2663 rc = lsmMCursorLast(pCsr);
2664 if( rc==LSM_OK
2665 && rtIsWrite(pCsr->eType) && rtIsSystem(pCsr->eType)
2666 && pCsr->key.nData==8
2667 && 0==memcmp(pCsr->key.pData, "FREELIST", 8)
2669 void *pVal; int nVal; /* Value read from database */
2670 rc = lsmMCursorValue(pCsr, &pVal, &nVal);
2671 if( rc==LSM_OK ){
2672 *ppVal = lsmMallocRc(pDb->pEnv, nVal, &rc);
2673 if( *ppVal ){
2674 memcpy(*ppVal, pVal, nVal);
2675 *pnVal = nVal;
2680 lsmMCursorClose(pCsr, 0);
2683 return rc;
2686 static int multiCursorAllocTree(MultiCursor *pCsr){
2687 int rc = LSM_OK;
2688 if( pCsr->aTree==0 ){
2689 int nByte; /* Bytes of space to allocate */
2690 int nMin; /* Total number of cursors being merged */
2692 nMin = CURSOR_DATA_SEGMENT + pCsr->nPtr + (pCsr->pBtCsr!=0);
2693 pCsr->nTree = 2;
2694 while( pCsr->nTree<nMin ){
2695 pCsr->nTree = pCsr->nTree*2;
2698 nByte = sizeof(int)*pCsr->nTree*2;
2699 pCsr->aTree = (int *)lsmMallocZeroRc(pCsr->pDb->pEnv, nByte, &rc);
2701 return rc;
2704 static void multiCursorCacheKey(MultiCursor *pCsr, int *pRc){
2705 if( *pRc==LSM_OK ){
2706 void *pKey;
2707 int nKey;
2708 multiCursorGetKey(pCsr, pCsr->aTree[1], &pCsr->eType, &pKey, &nKey);
2709 *pRc = sortedBlobSet(pCsr->pDb->pEnv, &pCsr->key, pKey, nKey);
2713 #ifdef LSM_DEBUG_EXPENSIVE
2714 static void assertCursorTree(MultiCursor *pCsr){
2715 int bRev = !!(pCsr->flags & CURSOR_PREV_OK);
2716 int *aSave = pCsr->aTree;
2717 int nSave = pCsr->nTree;
2718 int rc;
2720 pCsr->aTree = 0;
2721 pCsr->nTree = 0;
2722 rc = multiCursorAllocTree(pCsr);
2723 if( rc==LSM_OK ){
2724 int i;
2725 for(i=pCsr->nTree-1; i>0; i--){
2726 multiCursorDoCompare(pCsr, i, bRev);
2729 assert( nSave==pCsr->nTree
2730 && 0==memcmp(aSave, pCsr->aTree, sizeof(int)*nSave)
2733 lsmFree(pCsr->pDb->pEnv, pCsr->aTree);
2736 pCsr->aTree = aSave;
2737 pCsr->nTree = nSave;
2739 #else
2740 # define assertCursorTree(x)
2741 #endif
2743 static int mcursorLocationOk(MultiCursor *pCsr, int bDeleteOk){
2744 int eType = pCsr->eType;
2745 int iKey;
2746 int i;
2747 int rdmask;
2749 assert( pCsr->flags & (CURSOR_NEXT_OK|CURSOR_PREV_OK) );
2750 assertCursorTree(pCsr);
2752 rdmask = (pCsr->flags & CURSOR_NEXT_OK) ? LSM_END_DELETE : LSM_START_DELETE;
2754 /* If the cursor does not currently point to an actual database key (i.e.
2755 ** it points to a delete key, or the start or end of a range-delete), and
2756 ** the CURSOR_IGNORE_DELETE flag is set, skip past this entry. */
2757 if( (pCsr->flags & CURSOR_IGNORE_DELETE) && bDeleteOk==0 ){
2758 if( (eType & LSM_INSERT)==0 ) return 0;
2761 /* If the cursor points to a system key (free-list entry), and the
2762 ** CURSOR_IGNORE_SYSTEM flag is set, skip thie entry. */
2763 if( (pCsr->flags & CURSOR_IGNORE_SYSTEM) && rtTopic(eType)!=0 ){
2764 return 0;
2767 #ifndef NDEBUG
2768 /* This block fires assert() statements to check one of the assumptions
2769 ** in the comment below - that if the lhs sub-cursor of a level undergoing
2770 ** a merge is valid, then all the rhs sub-cursors must be at EOF.
2772 ** Also assert that all rhs sub-cursors are either at EOF or point to
2773 ** a key that is not less than the level split-key. */
2774 for(i=0; i<pCsr->nPtr; i++){
2775 SegmentPtr *pPtr = &pCsr->aPtr[i];
2776 Level *pLvl = pPtr->pLevel;
2777 if( pLvl->nRight && pPtr->pPg ){
2778 if( pPtr->pSeg==&pLvl->lhs ){
2779 int j;
2780 for(j=0; j<pLvl->nRight; j++) assert( pPtr[j+1].pPg==0 );
2781 }else{
2782 int res = sortedKeyCompare(pCsr->pDb->xCmp,
2783 rtTopic(pPtr->eType), pPtr->pKey, pPtr->nKey,
2784 pLvl->iSplitTopic, pLvl->pSplitKey, pLvl->nSplitKey
2786 assert( res>=0 );
2790 #endif
2792 /* Now check if this key has already been deleted by a range-delete. If
2793 ** so, skip past it.
2795 ** Assume, for the moment, that the tree contains no levels currently
2796 ** undergoing incremental merge, and that this cursor is iterating forwards
2797 ** through the database keys. The cursor currently points to a key in
2798 ** level L. This key has already been deleted if any of the sub-cursors
2799 ** that point to levels newer than L (or to the in-memory tree) point to
2800 ** a key greater than the current key with the LSM_END_DELETE flag set.
2802 ** Or, if the cursor is iterating backwards through data keys, if any
2803 ** such sub-cursor points to a key smaller than the current key with the
2804 ** LSM_START_DELETE flag set.
2806 ** Why it works with levels undergoing a merge too:
2808 ** When a cursor iterates forwards, the sub-cursors for the rhs of a
2809 ** level are only activated once the lhs reaches EOF. So when iterating
2810 ** forwards, the keys visited are the same as if the level was completely
2811 ** merged.
2813 ** If the cursor is iterating backwards, then the lhs sub-cursor is not
2814 ** initialized until the last of the rhs sub-cursors has reached EOF.
2815 ** Additionally, if the START_DELETE flag is set on the last entry (in
2816 ** reverse order - so the entry with the smallest key) of a rhs sub-cursor,
2817 ** then a pseudo-key equal to the levels split-key with the END_DELETE
2818 ** flag set is visited by the sub-cursor.
2820 iKey = pCsr->aTree[1];
2821 for(i=0; i<iKey; i++){
2822 int csrflags;
2823 multiCursorGetKey(pCsr, i, &csrflags, 0, 0);
2824 if( (rdmask & csrflags) ){
2825 const int SD_ED = (LSM_START_DELETE|LSM_END_DELETE);
2826 if( (csrflags & SD_ED)==SD_ED
2827 || (pCsr->flags & CURSOR_IGNORE_DELETE)==0
2829 void *pKey; int nKey;
2830 multiCursorGetKey(pCsr, i, 0, &pKey, &nKey);
2831 if( 0==sortedKeyCompare(pCsr->pDb->xCmp,
2832 rtTopic(eType), pCsr->key.pData, pCsr->key.nData,
2833 rtTopic(csrflags), pKey, nKey
2835 continue;
2838 return 0;
2842 /* The current cursor position is one this cursor should visit. Return 1. */
2843 return 1;
2846 static int multiCursorSetupTree(MultiCursor *pCsr, int bRev){
2847 int rc;
2849 rc = multiCursorAllocTree(pCsr);
2850 if( rc==LSM_OK ){
2851 int i;
2852 for(i=pCsr->nTree-1; i>0; i--){
2853 multiCursorDoCompare(pCsr, i, bRev);
2857 assertCursorTree(pCsr);
2858 multiCursorCacheKey(pCsr, &rc);
2860 if( rc==LSM_OK && mcursorLocationOk(pCsr, 0)==0 ){
2861 rc = multiCursorAdvance(pCsr, bRev);
2863 return rc;
2867 static int multiCursorEnd(MultiCursor *pCsr, int bLast){
2868 int rc = LSM_OK;
2869 int i;
2871 pCsr->flags &= ~(CURSOR_NEXT_OK | CURSOR_PREV_OK);
2872 pCsr->flags |= (bLast ? CURSOR_PREV_OK : CURSOR_NEXT_OK);
2873 pCsr->iFree = 0;
2875 /* Position the two in-memory tree cursors */
2876 for(i=0; rc==LSM_OK && i<2; i++){
2877 if( pCsr->apTreeCsr[i] ){
2878 rc = lsmTreeCursorEnd(pCsr->apTreeCsr[i], bLast);
2882 for(i=0; rc==LSM_OK && i<pCsr->nPtr; i++){
2883 SegmentPtr *pPtr = &pCsr->aPtr[i];
2884 Level *pLvl = pPtr->pLevel;
2885 int iRhs;
2886 int bHit = 0;
2888 if( bLast ){
2889 for(iRhs=0; iRhs<pLvl->nRight && rc==LSM_OK; iRhs++){
2890 rc = segmentPtrEnd(pCsr, &pPtr[iRhs+1], 1);
2891 if( pPtr[iRhs+1].pPg ) bHit = 1;
2893 if( bHit==0 && rc==LSM_OK ){
2894 rc = segmentPtrEnd(pCsr, pPtr, 1);
2895 }else{
2896 segmentPtrReset(pPtr, LSM_SEGMENTPTR_FREE_THRESHOLD);
2898 }else{
2899 int bLhs = (pPtr->pSeg==&pLvl->lhs);
2900 assert( pPtr->pSeg==&pLvl->lhs || pPtr->pSeg==&pLvl->aRhs[0] );
2902 if( bLhs ){
2903 rc = segmentPtrEnd(pCsr, pPtr, 0);
2904 if( pPtr->pKey ) bHit = 1;
2906 for(iRhs=0; iRhs<pLvl->nRight && rc==LSM_OK; iRhs++){
2907 if( bHit ){
2908 segmentPtrReset(&pPtr[iRhs+1], LSM_SEGMENTPTR_FREE_THRESHOLD);
2909 }else{
2910 rc = sortedRhsFirst(pCsr, pLvl, &pPtr[iRhs+bLhs]);
2914 i += pLvl->nRight;
2917 /* And the b-tree cursor, if applicable */
2918 if( rc==LSM_OK && pCsr->pBtCsr ){
2919 assert( bLast==0 );
2920 rc = btreeCursorFirst(pCsr->pBtCsr);
2923 if( rc==LSM_OK ){
2924 rc = multiCursorSetupTree(pCsr, bLast);
2927 return rc;
2931 int mcursorSave(MultiCursor *pCsr){
2932 int rc = LSM_OK;
2933 if( pCsr->aTree ){
2934 int iTree = pCsr->aTree[1];
2935 if( iTree==CURSOR_DATA_TREE0 || iTree==CURSOR_DATA_TREE1 ){
2936 multiCursorCacheKey(pCsr, &rc);
2939 mcursorFreeComponents(pCsr);
2940 return rc;
2943 int mcursorRestore(lsm_db *pDb, MultiCursor *pCsr){
2944 int rc;
2945 rc = multiCursorInit(pCsr, pDb->pClient);
2946 if( rc==LSM_OK && pCsr->key.pData ){
2947 rc = lsmMCursorSeek(pCsr,
2948 rtTopic(pCsr->eType), pCsr->key.pData, pCsr->key.nData, +1
2951 return rc;
2954 int lsmSaveCursors(lsm_db *pDb){
2955 int rc = LSM_OK;
2956 MultiCursor *pCsr;
2958 for(pCsr=pDb->pCsr; rc==LSM_OK && pCsr; pCsr=pCsr->pNext){
2959 rc = mcursorSave(pCsr);
2961 return rc;
2964 int lsmRestoreCursors(lsm_db *pDb){
2965 int rc = LSM_OK;
2966 MultiCursor *pCsr;
2968 for(pCsr=pDb->pCsr; rc==LSM_OK && pCsr; pCsr=pCsr->pNext){
2969 rc = mcursorRestore(pDb, pCsr);
2971 return rc;
2974 int lsmMCursorFirst(MultiCursor *pCsr){
2975 return multiCursorEnd(pCsr, 0);
2978 int lsmMCursorLast(MultiCursor *pCsr){
2979 return multiCursorEnd(pCsr, 1);
2982 lsm_db *lsmMCursorDb(MultiCursor *pCsr){
2983 return pCsr->pDb;
2986 void lsmMCursorReset(MultiCursor *pCsr){
2987 int i;
2988 lsmTreeCursorReset(pCsr->apTreeCsr[0]);
2989 lsmTreeCursorReset(pCsr->apTreeCsr[1]);
2990 for(i=0; i<pCsr->nPtr; i++){
2991 segmentPtrReset(&pCsr->aPtr[i], LSM_SEGMENTPTR_FREE_THRESHOLD);
2993 pCsr->key.nData = 0;
2996 static int treeCursorSeek(
2997 MultiCursor *pCsr,
2998 TreeCursor *pTreeCsr,
2999 void *pKey, int nKey,
3000 int eSeek,
3001 int *pbStop
3003 int rc = LSM_OK;
3004 if( pTreeCsr ){
3005 int res = 0;
3006 lsmTreeCursorSeek(pTreeCsr, pKey, nKey, &res);
3007 switch( eSeek ){
3008 case LSM_SEEK_EQ: {
3009 int eType = lsmTreeCursorFlags(pTreeCsr);
3010 if( (res<0 && (eType & LSM_START_DELETE))
3011 || (res>0 && (eType & LSM_END_DELETE))
3012 || (res==0 && (eType & LSM_POINT_DELETE))
3014 *pbStop = 1;
3015 }else if( res==0 && (eType & LSM_INSERT) ){
3016 lsm_env *pEnv = pCsr->pDb->pEnv;
3017 void *p; int n; /* Key/value from tree-cursor */
3018 *pbStop = 1;
3019 pCsr->flags |= CURSOR_SEEK_EQ;
3020 rc = lsmTreeCursorKey(pTreeCsr, &pCsr->eType, &p, &n);
3021 if( rc==LSM_OK ) rc = sortedBlobSet(pEnv, &pCsr->key, p, n);
3022 if( rc==LSM_OK ) rc = lsmTreeCursorValue(pTreeCsr, &p, &n);
3023 if( rc==LSM_OK ) rc = sortedBlobSet(pEnv, &pCsr->val, p, n);
3025 lsmTreeCursorReset(pTreeCsr);
3026 break;
3028 case LSM_SEEK_GE:
3029 if( res<0 && lsmTreeCursorValid(pTreeCsr) ){
3030 lsmTreeCursorNext(pTreeCsr);
3032 break;
3033 default:
3034 if( res>0 ){
3035 assert( lsmTreeCursorValid(pTreeCsr) );
3036 lsmTreeCursorPrev(pTreeCsr);
3038 break;
3041 return rc;
3046 ** Seek the cursor.
3048 int lsmMCursorSeek(
3049 MultiCursor *pCsr,
3050 int iTopic,
3051 void *pKey, int nKey,
3052 int eSeek
3054 int eESeek = eSeek; /* Effective eSeek parameter */
3055 int bStop = 0; /* Set to true to halt search operation */
3056 int rc = LSM_OK; /* Return code */
3057 int iPtr = 0; /* Used to iterate through pCsr->aPtr[] */
3058 LsmPgno iPgno = 0; /* FC pointer value */
3060 assert( pCsr->apTreeCsr[0]==0 || iTopic==0 );
3061 assert( pCsr->apTreeCsr[1]==0 || iTopic==0 );
3063 if( eESeek==LSM_SEEK_LEFAST ) eESeek = LSM_SEEK_LE;
3065 assert( eESeek==LSM_SEEK_EQ || eESeek==LSM_SEEK_LE || eESeek==LSM_SEEK_GE );
3066 assert( (pCsr->flags & CURSOR_FLUSH_FREELIST)==0 );
3067 assert( pCsr->nPtr==0 || pCsr->aPtr[0].pLevel );
3069 pCsr->flags &= ~(CURSOR_NEXT_OK | CURSOR_PREV_OK | CURSOR_SEEK_EQ);
3070 rc = treeCursorSeek(pCsr, pCsr->apTreeCsr[0], pKey, nKey, eESeek, &bStop);
3071 if( rc==LSM_OK && bStop==0 ){
3072 rc = treeCursorSeek(pCsr, pCsr->apTreeCsr[1], pKey, nKey, eESeek, &bStop);
3075 /* Seek all segment pointers. */
3076 for(iPtr=0; iPtr<pCsr->nPtr && rc==LSM_OK && bStop==0; iPtr++){
3077 SegmentPtr *pPtr = &pCsr->aPtr[iPtr];
3078 assert( pPtr->pSeg==&pPtr->pLevel->lhs );
3079 rc = seekInLevel(pCsr, pPtr, eESeek, iTopic, pKey, nKey, &iPgno, &bStop);
3080 iPtr += pPtr->pLevel->nRight;
3083 if( eSeek!=LSM_SEEK_EQ ){
3084 if( rc==LSM_OK ){
3085 rc = multiCursorAllocTree(pCsr);
3087 if( rc==LSM_OK ){
3088 int i;
3089 for(i=pCsr->nTree-1; i>0; i--){
3090 multiCursorDoCompare(pCsr, i, eESeek==LSM_SEEK_LE);
3092 if( eSeek==LSM_SEEK_GE ) pCsr->flags |= CURSOR_NEXT_OK;
3093 if( eSeek==LSM_SEEK_LE ) pCsr->flags |= CURSOR_PREV_OK;
3096 multiCursorCacheKey(pCsr, &rc);
3097 if( rc==LSM_OK && eSeek!=LSM_SEEK_LEFAST && 0==mcursorLocationOk(pCsr, 0) ){
3098 switch( eESeek ){
3099 case LSM_SEEK_EQ:
3100 lsmMCursorReset(pCsr);
3101 break;
3102 case LSM_SEEK_GE:
3103 rc = lsmMCursorNext(pCsr);
3104 break;
3105 default:
3106 rc = lsmMCursorPrev(pCsr);
3107 break;
3112 return rc;
3115 int lsmMCursorValid(MultiCursor *pCsr){
3116 int res = 0;
3117 if( pCsr->flags & CURSOR_SEEK_EQ ){
3118 res = 1;
3119 }else if( pCsr->aTree ){
3120 int iKey = pCsr->aTree[1];
3121 if( iKey==CURSOR_DATA_TREE0 || iKey==CURSOR_DATA_TREE1 ){
3122 res = lsmTreeCursorValid(pCsr->apTreeCsr[iKey-CURSOR_DATA_TREE0]);
3123 }else{
3124 void *pKey;
3125 multiCursorGetKey(pCsr, iKey, 0, &pKey, 0);
3126 res = pKey!=0;
3129 return res;
3132 static int mcursorAdvanceOk(
3133 MultiCursor *pCsr,
3134 int bReverse,
3135 int *pRc
3137 void *pNew; /* Pointer to buffer containing new key */
3138 int nNew; /* Size of buffer pNew in bytes */
3139 int eNewType; /* Type of new record */
3141 if( *pRc ) return 1;
3143 /* Check the current key value. If it is not greater than (if bReverse==0)
3144 ** or less than (if bReverse!=0) the key currently cached in pCsr->key,
3145 ** then the cursor has not yet been successfully advanced.
3147 multiCursorGetKey(pCsr, pCsr->aTree[1], &eNewType, &pNew, &nNew);
3148 if( pNew ){
3149 int typemask = (pCsr->flags & CURSOR_IGNORE_DELETE) ? ~(0) : LSM_SYSTEMKEY;
3150 int res = sortedDbKeyCompare(pCsr,
3151 eNewType & typemask, pNew, nNew,
3152 pCsr->eType & typemask, pCsr->key.pData, pCsr->key.nData
3155 if( (bReverse==0 && res<=0) || (bReverse!=0 && res>=0) ){
3156 return 0;
3159 multiCursorCacheKey(pCsr, pRc);
3160 assert( pCsr->eType==eNewType );
3162 /* If this cursor is configured to skip deleted keys, and the current
3163 ** cursor points to a SORTED_DELETE entry, then the cursor has not been
3164 ** successfully advanced.
3166 ** Similarly, if the cursor is configured to skip system keys and the
3167 ** current cursor points to a system key, it has not yet been advanced.
3169 if( *pRc==LSM_OK && 0==mcursorLocationOk(pCsr, 0) ) return 0;
3171 return 1;
3174 static void flCsrAdvance(MultiCursor *pCsr){
3175 assert( pCsr->flags & CURSOR_FLUSH_FREELIST );
3176 if( pCsr->iFree % 2 ){
3177 pCsr->iFree++;
3178 }else{
3179 int nEntry = pCsr->pDb->pWorker->freelist.nEntry;
3180 FreelistEntry *aEntry = pCsr->pDb->pWorker->freelist.aEntry;
3182 int i = nEntry - 1 - (pCsr->iFree / 2);
3184 /* If the current entry is a delete and the "end-delete" key will not
3185 ** be attached to the next entry, increment iFree by 1 only. */
3186 if( aEntry[i].iId<0 ){
3187 while( 1 ){
3188 if( i==0 || aEntry[i-1].iBlk!=aEntry[i].iBlk-1 ){
3189 pCsr->iFree--;
3190 break;
3192 if( aEntry[i-1].iId>=0 ) break;
3193 pCsr->iFree += 2;
3194 i--;
3197 pCsr->iFree += 2;
3201 static int multiCursorAdvance(MultiCursor *pCsr, int bReverse){
3202 int rc = LSM_OK; /* Return Code */
3203 if( lsmMCursorValid(pCsr) ){
3204 do {
3205 int iKey = pCsr->aTree[1];
3207 assertCursorTree(pCsr);
3209 /* If this multi-cursor is advancing forwards, and the sub-cursor
3210 ** being advanced is the one that separator keys may be being read
3211 ** from, record the current absolute pointer value. */
3212 if( pCsr->pPrevMergePtr ){
3213 if( iKey==(CURSOR_DATA_SEGMENT+pCsr->nPtr) ){
3214 assert( pCsr->pBtCsr );
3215 *pCsr->pPrevMergePtr = pCsr->pBtCsr->iPtr;
3216 }else if( pCsr->pBtCsr==0 && pCsr->nPtr>0
3217 && iKey==(CURSOR_DATA_SEGMENT+pCsr->nPtr-1)
3219 SegmentPtr *pPtr = &pCsr->aPtr[iKey-CURSOR_DATA_SEGMENT];
3220 *pCsr->pPrevMergePtr = pPtr->iPtr+pPtr->iPgPtr;
3224 if( iKey==CURSOR_DATA_TREE0 || iKey==CURSOR_DATA_TREE1 ){
3225 TreeCursor *pTreeCsr = pCsr->apTreeCsr[iKey-CURSOR_DATA_TREE0];
3226 if( bReverse ){
3227 rc = lsmTreeCursorPrev(pTreeCsr);
3228 }else{
3229 rc = lsmTreeCursorNext(pTreeCsr);
3231 }else if( iKey==CURSOR_DATA_SYSTEM ){
3232 assert( pCsr->flags & CURSOR_FLUSH_FREELIST );
3233 assert( bReverse==0 );
3234 flCsrAdvance(pCsr);
3235 }else if( iKey==(CURSOR_DATA_SEGMENT+pCsr->nPtr) ){
3236 assert( bReverse==0 && pCsr->pBtCsr );
3237 rc = btreeCursorNext(pCsr->pBtCsr);
3238 }else{
3239 rc = segmentCursorAdvance(pCsr, iKey-CURSOR_DATA_SEGMENT, bReverse);
3241 if( rc==LSM_OK ){
3242 int i;
3243 for(i=(iKey+pCsr->nTree)/2; i>0; i=i/2){
3244 multiCursorDoCompare(pCsr, i, bReverse);
3246 assertCursorTree(pCsr);
3248 }while( mcursorAdvanceOk(pCsr, bReverse, &rc)==0 );
3250 return rc;
3253 int lsmMCursorNext(MultiCursor *pCsr){
3254 if( (pCsr->flags & CURSOR_NEXT_OK)==0 ) return LSM_MISUSE_BKPT;
3255 return multiCursorAdvance(pCsr, 0);
3258 int lsmMCursorPrev(MultiCursor *pCsr){
3259 if( (pCsr->flags & CURSOR_PREV_OK)==0 ) return LSM_MISUSE_BKPT;
3260 return multiCursorAdvance(pCsr, 1);
3263 int lsmMCursorKey(MultiCursor *pCsr, void **ppKey, int *pnKey){
3264 if( (pCsr->flags & CURSOR_SEEK_EQ) || pCsr->aTree==0 ){
3265 *pnKey = pCsr->key.nData;
3266 *ppKey = pCsr->key.pData;
3267 }else{
3268 int iKey = pCsr->aTree[1];
3270 if( iKey==CURSOR_DATA_TREE0 || iKey==CURSOR_DATA_TREE1 ){
3271 TreeCursor *pTreeCsr = pCsr->apTreeCsr[iKey-CURSOR_DATA_TREE0];
3272 lsmTreeCursorKey(pTreeCsr, 0, ppKey, pnKey);
3273 }else{
3274 int nKey;
3276 #ifndef NDEBUG
3277 void *pKey;
3278 int eType;
3279 multiCursorGetKey(pCsr, iKey, &eType, &pKey, &nKey);
3280 assert( eType==pCsr->eType );
3281 assert( nKey==pCsr->key.nData );
3282 assert( memcmp(pKey, pCsr->key.pData, nKey)==0 );
3283 #endif
3285 nKey = pCsr->key.nData;
3286 if( nKey==0 ){
3287 *ppKey = 0;
3288 }else{
3289 *ppKey = pCsr->key.pData;
3291 *pnKey = nKey;
3294 return LSM_OK;
3298 ** Compare the current key that cursor csr points to with pKey/nKey. Set
3299 ** *piRes to the result and return LSM_OK.
3301 int lsm_csr_cmp(lsm_cursor *csr, const void *pKey, int nKey, int *piRes){
3302 MultiCursor *pCsr = (MultiCursor *)csr;
3303 void *pCsrkey; int nCsrkey;
3304 int rc;
3305 rc = lsmMCursorKey(pCsr, &pCsrkey, &nCsrkey);
3306 if( rc==LSM_OK ){
3307 int (*xCmp)(void *, int, void *, int) = pCsr->pDb->xCmp;
3308 *piRes = sortedKeyCompare(xCmp, 0, pCsrkey, nCsrkey, 0, (void *)pKey, nKey);
3310 return rc;
3313 int lsmMCursorValue(MultiCursor *pCsr, void **ppVal, int *pnVal){
3314 void *pVal;
3315 int nVal;
3316 int rc;
3317 if( (pCsr->flags & CURSOR_SEEK_EQ) || pCsr->aTree==0 ){
3318 rc = LSM_OK;
3319 nVal = pCsr->val.nData;
3320 pVal = pCsr->val.pData;
3321 }else{
3323 assert( pCsr->aTree );
3324 assert( mcursorLocationOk(pCsr, (pCsr->flags & CURSOR_IGNORE_DELETE)) );
3326 rc = multiCursorGetVal(pCsr, pCsr->aTree[1], &pVal, &nVal);
3327 if( pVal && rc==LSM_OK ){
3328 rc = sortedBlobSet(pCsr->pDb->pEnv, &pCsr->val, pVal, nVal);
3329 pVal = pCsr->val.pData;
3332 if( rc!=LSM_OK ){
3333 pVal = 0;
3334 nVal = 0;
3337 *ppVal = pVal;
3338 *pnVal = nVal;
3339 return rc;
3342 int lsmMCursorType(MultiCursor *pCsr, int *peType){
3343 assert( pCsr->aTree );
3344 multiCursorGetKey(pCsr, pCsr->aTree[1], peType, 0, 0);
3345 return LSM_OK;
3349 ** Buffer aData[], size nData, is assumed to contain a valid b-tree
3350 ** hierarchy page image. Return the offset in aData[] of the next free
3351 ** byte in the data area (where a new cell may be written if there is
3352 ** space).
3354 static int mergeWorkerPageOffset(u8 *aData, int nData){
3355 int nRec;
3356 int iOff;
3357 int nKey;
3358 int eType;
3360 nRec = lsmGetU16(&aData[SEGMENT_NRECORD_OFFSET(nData)]);
3361 iOff = lsmGetU16(&aData[SEGMENT_CELLPTR_OFFSET(nData, nRec-1)]);
3362 eType = aData[iOff++];
3363 assert( eType==0
3364 || eType==(LSM_SYSTEMKEY|LSM_SEPARATOR)
3365 || eType==(LSM_SEPARATOR)
3368 iOff += lsmVarintGet32(&aData[iOff], &nKey);
3369 iOff += lsmVarintGet32(&aData[iOff], &nKey);
3371 return iOff + (eType ? nKey : 0);
3375 ** Following a checkpoint operation, database pages that are part of the
3376 ** checkpointed state of the LSM are deemed read-only. This includes the
3377 ** right-most page of the b-tree hierarchy of any separators array under
3378 ** construction, and all pages between it and the b-tree root, inclusive.
3379 ** This is a problem, as when further pages are appended to the separators
3380 ** array, entries must be added to the indicated b-tree hierarchy pages.
3382 ** This function copies all such b-tree pages to new locations, so that
3383 ** they can be modified as required.
3385 ** The complication is that not all database pages are the same size - due
3386 ** to the way the file.c module works some (the first and last in each block)
3387 ** are 4 bytes smaller than the others.
3389 static int mergeWorkerMoveHierarchy(
3390 MergeWorker *pMW, /* Merge worker */
3391 int bSep /* True for separators run */
3393 lsm_db *pDb = pMW->pDb; /* Database handle */
3394 int rc = LSM_OK; /* Return code */
3395 int i;
3396 Page **apHier = pMW->hier.apHier;
3397 int nHier = pMW->hier.nHier;
3399 for(i=0; rc==LSM_OK && i<nHier; i++){
3400 Page *pNew = 0;
3401 rc = lsmFsSortedAppend(pDb->pFS, pDb->pWorker, pMW->pLevel, 1, &pNew);
3402 assert( rc==LSM_OK );
3404 if( rc==LSM_OK ){
3405 u8 *a1; int n1;
3406 u8 *a2; int n2;
3408 a1 = fsPageData(pNew, &n1);
3409 a2 = fsPageData(apHier[i], &n2);
3411 assert( n1==n2 || n1+4==n2 );
3413 if( n1==n2 ){
3414 memcpy(a1, a2, n2);
3415 }else{
3416 int nEntry = pageGetNRec(a2, n2);
3417 int iEof1 = SEGMENT_EOF(n1, nEntry);
3418 int iEof2 = SEGMENT_EOF(n2, nEntry);
3420 memcpy(a1, a2, iEof2 - 4);
3421 memcpy(&a1[iEof1], &a2[iEof2], n2 - iEof2);
3424 lsmFsPageRelease(apHier[i]);
3425 apHier[i] = pNew;
3427 #if 0
3428 assert( n1==n2 || n1+4==n2 || n2+4==n1 );
3429 if( n1>=n2 ){
3430 /* If n1 (size of the new page) is equal to or greater than n2 (the
3431 ** size of the old page), then copy the data into the new page. If
3432 ** n1==n2, this could be done with a single memcpy(). However,
3433 ** since sometimes n1>n2, the page content and footer must be copied
3434 ** separately. */
3435 int nEntry = pageGetNRec(a2, n2);
3436 int iEof1 = SEGMENT_EOF(n1, nEntry);
3437 int iEof2 = SEGMENT_EOF(n2, nEntry);
3438 memcpy(a1, a2, iEof2);
3439 memcpy(&a1[iEof1], &a2[iEof2], n2 - iEof2);
3440 lsmFsPageRelease(apHier[i]);
3441 apHier[i] = pNew;
3442 }else{
3443 lsmPutU16(&a1[SEGMENT_FLAGS_OFFSET(n1)], SEGMENT_BTREE_FLAG);
3444 lsmPutU16(&a1[SEGMENT_NRECORD_OFFSET(n1)], 0);
3445 lsmPutU64(&a1[SEGMENT_POINTER_OFFSET(n1)], 0);
3446 i = i - 1;
3447 lsmFsPageRelease(pNew);
3449 #endif
3453 #ifdef LSM_DEBUG
3454 if( rc==LSM_OK ){
3455 for(i=0; i<nHier; i++) assert( lsmFsPageWritable(apHier[i]) );
3457 #endif
3459 return rc;
3463 ** Allocate and populate the MergeWorker.apHier[] array.
3465 static int mergeWorkerLoadHierarchy(MergeWorker *pMW){
3466 int rc = LSM_OK;
3467 Segment *pSeg;
3468 Hierarchy *p;
3470 pSeg = &pMW->pLevel->lhs;
3471 p = &pMW->hier;
3473 if( p->apHier==0 && pSeg->iRoot!=0 ){
3474 FileSystem *pFS = pMW->pDb->pFS;
3475 lsm_env *pEnv = pMW->pDb->pEnv;
3476 Page **apHier = 0;
3477 int nHier = 0;
3478 int iPg = (int)pSeg->iRoot;
3480 do {
3481 Page *pPg = 0;
3482 u8 *aData;
3483 int nData;
3484 int flags;
3486 rc = lsmFsDbPageGet(pFS, pSeg, iPg, &pPg);
3487 if( rc!=LSM_OK ) break;
3489 aData = fsPageData(pPg, &nData);
3490 flags = pageGetFlags(aData, nData);
3491 if( flags&SEGMENT_BTREE_FLAG ){
3492 Page **apNew = (Page **)lsmRealloc(
3493 pEnv, apHier, sizeof(Page *)*(nHier+1)
3495 if( apNew==0 ){
3496 rc = LSM_NOMEM_BKPT;
3497 break;
3499 apHier = apNew;
3500 memmove(&apHier[1], &apHier[0], sizeof(Page *) * nHier);
3501 nHier++;
3503 apHier[0] = pPg;
3504 iPg = (int)pageGetPtr(aData, nData);
3505 }else{
3506 lsmFsPageRelease(pPg);
3507 break;
3509 }while( 1 );
3511 if( rc==LSM_OK ){
3512 u8 *aData;
3513 int nData;
3514 aData = fsPageData(apHier[0], &nData);
3515 pMW->aSave[0].iPgno = pageGetPtr(aData, nData);
3516 p->nHier = nHier;
3517 p->apHier = apHier;
3518 rc = mergeWorkerMoveHierarchy(pMW, 0);
3519 }else{
3520 int i;
3521 for(i=0; i<nHier; i++){
3522 lsmFsPageRelease(apHier[i]);
3524 lsmFree(pEnv, apHier);
3528 return rc;
3532 ** B-tree pages use almost the same format as regular pages. The
3533 ** differences are:
3535 ** 1. The record format is (usually, see below) as follows:
3537 ** + Type byte (always SORTED_SEPARATOR or SORTED_SYSTEM_SEPARATOR),
3538 ** + Absolute pointer value (varint),
3539 ** + Number of bytes in key (varint),
3540 ** + LsmBlob containing key data.
3542 ** 2. All pointer values are stored as absolute values (not offsets
3543 ** relative to the footer pointer value).
3545 ** 3. Each pointer that is part of a record points to a page that
3546 ** contains keys smaller than the records key (note: not "equal to or
3547 ** smaller than - smaller than").
3549 ** 4. The pointer in the page footer of a b-tree page points to a page
3550 ** that contains keys equal to or larger than the largest key on the
3551 ** b-tree page.
3553 ** The reason for having the page footer pointer point to the right-child
3554 ** (instead of the left) is that doing things this way makes the
3555 ** mergeWorkerMoveHierarchy() operation less complicated (since the pointers
3556 ** that need to be updated are all stored as fixed-size integers within the
3557 ** page footer, not varints in page records).
3559 ** Records may not span b-tree pages. If this function is called to add a
3560 ** record larger than (page-size / 4) bytes, then a pointer to the indexed
3561 ** array page that contains the main record is added to the b-tree instead.
3562 ** In this case the record format is:
3564 ** + 0x00 byte (1 byte)
3565 ** + Absolute pointer value (varint),
3566 ** + Absolute page number of page containing key (varint).
3568 ** See function seekInBtree() for the code that traverses b-tree pages.
3571 static int mergeWorkerBtreeWrite(
3572 MergeWorker *pMW,
3573 u8 eType,
3574 LsmPgno iPtr,
3575 LsmPgno iKeyPg,
3576 void *pKey,
3577 int nKey
3579 Hierarchy *p = &pMW->hier;
3580 lsm_db *pDb = pMW->pDb; /* Database handle */
3581 int rc = LSM_OK; /* Return Code */
3582 int iLevel; /* Level of b-tree hierachy to write to */
3583 int nData; /* Size of aData[] in bytes */
3584 u8 *aData; /* Page data for level iLevel */
3585 int iOff; /* Offset on b-tree page to write record to */
3586 int nRec; /* Initial number of records on b-tree page */
3588 /* iKeyPg should be zero for an ordinary b-tree key, or non-zero for an
3589 ** indirect key. The flags byte for an indirect key is 0x00. */
3590 assert( (eType==0)==(iKeyPg!=0) );
3592 /* The MergeWorker.apHier[] array contains the right-most leaf of the b-tree
3593 ** hierarchy, the root node, and all nodes that lie on the path between.
3594 ** apHier[0] is the right-most leaf and apHier[pMW->nHier-1] is the current
3595 ** root page.
3597 ** This loop searches for a node with enough space to store the key on,
3598 ** starting with the leaf and iterating up towards the root. When the loop
3599 ** exits, the key may be written to apHier[iLevel]. */
3600 for(iLevel=0; iLevel<=p->nHier; iLevel++){
3601 int nByte; /* Number of free bytes required */
3603 if( iLevel==p->nHier ){
3604 /* Extend the array and allocate a new root page. */
3605 Page **aNew;
3606 aNew = (Page **)lsmRealloc(
3607 pMW->pDb->pEnv, p->apHier, sizeof(Page *)*(p->nHier+1)
3609 if( !aNew ){
3610 return LSM_NOMEM_BKPT;
3612 p->apHier = aNew;
3613 }else{
3614 Page *pOld;
3615 int nFree;
3617 /* If the key will fit on this page, break out of the loop here.
3618 ** The new entry will be written to page apHier[iLevel]. */
3619 pOld = p->apHier[iLevel];
3620 assert( lsmFsPageWritable(pOld) );
3621 aData = fsPageData(pOld, &nData);
3622 if( eType==0 ){
3623 nByte = 2 + 1 + lsmVarintLen32((int)iPtr) + lsmVarintLen32((int)iKeyPg);
3624 }else{
3625 nByte = 2 + 1 + lsmVarintLen32((int)iPtr) + lsmVarintLen32(nKey) + nKey;
3627 nRec = pageGetNRec(aData, nData);
3628 nFree = SEGMENT_EOF(nData, nRec) - mergeWorkerPageOffset(aData, nData);
3629 if( nByte<=nFree ) break;
3631 /* Otherwise, this page is full. Set the right-hand-child pointer
3632 ** to iPtr and release it. */
3633 lsmPutU64(&aData[SEGMENT_POINTER_OFFSET(nData)], iPtr);
3634 assert( lsmFsPageNumber(pOld)==0 );
3635 rc = lsmFsPagePersist(pOld);
3636 if( rc==LSM_OK ){
3637 iPtr = lsmFsPageNumber(pOld);
3638 lsmFsPageRelease(pOld);
3642 /* Allocate a new page for apHier[iLevel]. */
3643 p->apHier[iLevel] = 0;
3644 if( rc==LSM_OK ){
3645 rc = lsmFsSortedAppend(
3646 pDb->pFS, pDb->pWorker, pMW->pLevel, 1, &p->apHier[iLevel]
3649 if( rc!=LSM_OK ) return rc;
3651 aData = fsPageData(p->apHier[iLevel], &nData);
3652 memset(aData, 0, nData);
3653 lsmPutU16(&aData[SEGMENT_FLAGS_OFFSET(nData)], SEGMENT_BTREE_FLAG);
3654 lsmPutU16(&aData[SEGMENT_NRECORD_OFFSET(nData)], 0);
3656 if( iLevel==p->nHier ){
3657 p->nHier++;
3658 break;
3662 /* Write the key into page apHier[iLevel]. */
3663 aData = fsPageData(p->apHier[iLevel], &nData);
3664 iOff = mergeWorkerPageOffset(aData, nData);
3665 nRec = pageGetNRec(aData, nData);
3666 lsmPutU16(&aData[SEGMENT_CELLPTR_OFFSET(nData, nRec)], (u16)iOff);
3667 lsmPutU16(&aData[SEGMENT_NRECORD_OFFSET(nData)], (u16)(nRec+1));
3668 if( eType==0 ){
3669 aData[iOff++] = 0x00;
3670 iOff += lsmVarintPut32(&aData[iOff], (int)iPtr);
3671 iOff += lsmVarintPut32(&aData[iOff], (int)iKeyPg);
3672 }else{
3673 aData[iOff++] = eType;
3674 iOff += lsmVarintPut32(&aData[iOff], (int)iPtr);
3675 iOff += lsmVarintPut32(&aData[iOff], nKey);
3676 memcpy(&aData[iOff], pKey, nKey);
3679 return rc;
3682 static int mergeWorkerBtreeIndirect(MergeWorker *pMW){
3683 int rc = LSM_OK;
3684 if( pMW->iIndirect ){
3685 LsmPgno iKeyPg = pMW->aSave[1].iPgno;
3686 rc = mergeWorkerBtreeWrite(pMW, 0, pMW->iIndirect, iKeyPg, 0, 0);
3687 pMW->iIndirect = 0;
3689 return rc;
3693 ** Append the database key (iTopic/pKey/nKey) to the b-tree under
3694 ** construction. This key has not yet been written to a segment page.
3695 ** The pointer that will accompany the new key in the b-tree - that
3696 ** points to the completed segment page that contains keys smaller than
3697 ** (pKey/nKey) is currently stored in pMW->aSave[0].iPgno.
3699 static int mergeWorkerPushHierarchy(
3700 MergeWorker *pMW, /* Merge worker object */
3701 int iTopic, /* Topic value for this key */
3702 void *pKey, /* Pointer to key buffer */
3703 int nKey /* Size of pKey buffer in bytes */
3705 int rc = LSM_OK; /* Return Code */
3706 LsmPgno iPtr; /* Pointer value to accompany pKey/nKey */
3708 assert( pMW->aSave[0].bStore==0 );
3709 assert( pMW->aSave[1].bStore==0 );
3710 rc = mergeWorkerBtreeIndirect(pMW);
3712 /* Obtain the absolute pointer value to store along with the key in the
3713 ** page body. This pointer points to a page that contains keys that are
3714 ** smaller than pKey/nKey. */
3715 iPtr = pMW->aSave[0].iPgno;
3716 assert( iPtr!=0 );
3718 /* Determine if the indirect format should be used. */
3719 if( (nKey*4 > lsmFsPageSize(pMW->pDb->pFS)) ){
3720 pMW->iIndirect = iPtr;
3721 pMW->aSave[1].bStore = 1;
3722 }else{
3723 rc = mergeWorkerBtreeWrite(
3724 pMW, (u8)(iTopic | LSM_SEPARATOR), iPtr, 0, pKey, nKey
3728 /* Ensure that the SortedRun.iRoot field is correct. */
3729 return rc;
3732 static int mergeWorkerFinishHierarchy(
3733 MergeWorker *pMW /* Merge worker object */
3735 int i; /* Used to loop through apHier[] */
3736 int rc = LSM_OK; /* Return code */
3737 LsmPgno iPtr; /* New right-hand-child pointer value */
3739 iPtr = pMW->aSave[0].iPgno;
3740 for(i=0; i<pMW->hier.nHier && rc==LSM_OK; i++){
3741 Page *pPg = pMW->hier.apHier[i];
3742 int nData; /* Size of aData[] in bytes */
3743 u8 *aData; /* Page data for pPg */
3745 aData = fsPageData(pPg, &nData);
3746 lsmPutU64(&aData[SEGMENT_POINTER_OFFSET(nData)], iPtr);
3748 rc = lsmFsPagePersist(pPg);
3749 iPtr = lsmFsPageNumber(pPg);
3750 lsmFsPageRelease(pPg);
3753 if( pMW->hier.nHier ){
3754 pMW->pLevel->lhs.iRoot = iPtr;
3755 lsmFree(pMW->pDb->pEnv, pMW->hier.apHier);
3756 pMW->hier.apHier = 0;
3757 pMW->hier.nHier = 0;
3760 return rc;
3763 static int mergeWorkerAddPadding(
3764 MergeWorker *pMW /* Merge worker object */
3766 FileSystem *pFS = pMW->pDb->pFS;
3767 return lsmFsSortedPadding(pFS, pMW->pDb->pWorker, &pMW->pLevel->lhs);
3771 ** Release all page references currently held by the merge-worker passed
3772 ** as the only argument. Unless an error has occurred, all pages have
3773 ** already been released.
3775 static void mergeWorkerReleaseAll(MergeWorker *pMW){
3776 int i;
3777 lsmFsPageRelease(pMW->pPage);
3778 pMW->pPage = 0;
3780 for(i=0; i<pMW->hier.nHier; i++){
3781 lsmFsPageRelease(pMW->hier.apHier[i]);
3782 pMW->hier.apHier[i] = 0;
3784 lsmFree(pMW->pDb->pEnv, pMW->hier.apHier);
3785 pMW->hier.apHier = 0;
3786 pMW->hier.nHier = 0;
3789 static int keyszToSkip(FileSystem *pFS, int nKey){
3790 int nPgsz; /* Nominal database page size */
3791 nPgsz = lsmFsPageSize(pFS);
3792 return LSM_MIN(((nKey * 4) / nPgsz), 3);
3796 ** Release the reference to the current output page of merge-worker *pMW
3797 ** (reference pMW->pPage). Set the page number values in aSave[] as
3798 ** required (see comments above struct MergeWorker for details).
3800 static int mergeWorkerPersistAndRelease(MergeWorker *pMW){
3801 int rc;
3802 int i;
3804 assert( pMW->pPage || (pMW->aSave[0].bStore==0 && pMW->aSave[1].bStore==0) );
3806 /* Persist the page */
3807 rc = lsmFsPagePersist(pMW->pPage);
3809 /* If required, save the page number. */
3810 for(i=0; i<2; i++){
3811 if( pMW->aSave[i].bStore ){
3812 pMW->aSave[i].iPgno = lsmFsPageNumber(pMW->pPage);
3813 pMW->aSave[i].bStore = 0;
3817 /* Release the completed output page. */
3818 lsmFsPageRelease(pMW->pPage);
3819 pMW->pPage = 0;
3820 return rc;
3824 ** Advance to the next page of an output run being populated by merge-worker
3825 ** pMW. The footer of the new page is initialized to indicate that it contains
3826 ** zero records. The flags field is cleared. The page footer pointer field
3827 ** is set to iFPtr.
3829 ** If successful, LSM_OK is returned. Otherwise, an error code.
3831 static int mergeWorkerNextPage(
3832 MergeWorker *pMW, /* Merge worker object to append page to */
3833 LsmPgno iFPtr /* Pointer value for footer of new page */
3835 int rc = LSM_OK; /* Return code */
3836 Page *pNext = 0; /* New page appended to run */
3837 lsm_db *pDb = pMW->pDb; /* Database handle */
3839 rc = lsmFsSortedAppend(pDb->pFS, pDb->pWorker, pMW->pLevel, 0, &pNext);
3840 assert( rc || pMW->pLevel->lhs.iFirst>0 || pMW->pDb->compress.xCompress );
3842 if( rc==LSM_OK ){
3843 u8 *aData; /* Data buffer belonging to page pNext */
3844 int nData; /* Size of aData[] in bytes */
3846 rc = mergeWorkerPersistAndRelease(pMW);
3848 pMW->pPage = pNext;
3849 pMW->pLevel->pMerge->iOutputOff = 0;
3850 aData = fsPageData(pNext, &nData);
3851 lsmPutU16(&aData[SEGMENT_NRECORD_OFFSET(nData)], 0);
3852 lsmPutU16(&aData[SEGMENT_FLAGS_OFFSET(nData)], 0);
3853 lsmPutU64(&aData[SEGMENT_POINTER_OFFSET(nData)], iFPtr);
3854 pMW->nWork++;
3857 return rc;
3861 ** Write a blob of data into an output segment being populated by a
3862 ** merge-worker object. If argument bSep is true, write into the separators
3863 ** array. Otherwise, the main array.
3865 ** This function is used to write the blobs of data for keys and values.
3867 static int mergeWorkerData(
3868 MergeWorker *pMW, /* Merge worker object */
3869 int bSep, /* True to write to separators run */
3870 int iFPtr, /* Footer ptr for new pages */
3871 u8 *aWrite, /* Write data from this buffer */
3872 int nWrite /* Size of aWrite[] in bytes */
3874 int rc = LSM_OK; /* Return code */
3875 int nRem = nWrite; /* Number of bytes still to write */
3877 while( rc==LSM_OK && nRem>0 ){
3878 Merge *pMerge = pMW->pLevel->pMerge;
3879 int nCopy; /* Number of bytes to copy */
3880 u8 *aData; /* Pointer to buffer of current output page */
3881 int nData; /* Size of aData[] in bytes */
3882 int nRec; /* Number of records on current output page */
3883 int iOff; /* Offset in aData[] to write to */
3885 assert( lsmFsPageWritable(pMW->pPage) );
3887 aData = fsPageData(pMW->pPage, &nData);
3888 nRec = pageGetNRec(aData, nData);
3889 iOff = pMerge->iOutputOff;
3890 nCopy = LSM_MIN(nRem, SEGMENT_EOF(nData, nRec) - iOff);
3892 memcpy(&aData[iOff], &aWrite[nWrite-nRem], nCopy);
3893 nRem -= nCopy;
3895 if( nRem>0 ){
3896 rc = mergeWorkerNextPage(pMW, iFPtr);
3897 }else{
3898 pMerge->iOutputOff = iOff + nCopy;
3902 return rc;
3907 ** The MergeWorker passed as the only argument is working to merge two or
3908 ** more existing segments together (not to flush an in-memory tree). It
3909 ** has not yet written the first key to the first page of the output.
3911 static int mergeWorkerFirstPage(MergeWorker *pMW){
3912 int rc = LSM_OK; /* Return code */
3913 Page *pPg = 0; /* First page of run pSeg */
3914 int iFPtr = 0; /* Pointer value read from footer of pPg */
3915 MultiCursor *pCsr = pMW->pCsr;
3917 assert( pMW->pPage==0 );
3919 if( pCsr->pBtCsr ){
3920 rc = LSM_OK;
3921 iFPtr = (int)pMW->pLevel->pNext->lhs.iFirst;
3922 }else if( pCsr->nPtr>0 ){
3923 Segment *pSeg;
3924 pSeg = pCsr->aPtr[pCsr->nPtr-1].pSeg;
3925 rc = lsmFsDbPageGet(pMW->pDb->pFS, pSeg, pSeg->iFirst, &pPg);
3926 if( rc==LSM_OK ){
3927 u8 *aData; /* Buffer for page pPg */
3928 int nData; /* Size of aData[] in bytes */
3929 aData = fsPageData(pPg, &nData);
3930 iFPtr = (int)pageGetPtr(aData, nData);
3931 lsmFsPageRelease(pPg);
3935 if( rc==LSM_OK ){
3936 rc = mergeWorkerNextPage(pMW, iFPtr);
3937 if( pCsr->pPrevMergePtr ) *pCsr->pPrevMergePtr = iFPtr;
3938 pMW->aSave[0].bStore = 1;
3941 return rc;
3944 static int mergeWorkerWrite(
3945 MergeWorker *pMW, /* Merge worker object to write into */
3946 int eType, /* One of SORTED_SEPARATOR, WRITE or DELETE */
3947 void *pKey, int nKey, /* Key value */
3948 void *pVal, int nVal, /* Value value */
3949 int iPtr /* Absolute value of page pointer, or 0 */
3951 int rc = LSM_OK; /* Return code */
3952 Merge *pMerge; /* Persistent part of level merge state */
3953 int nHdr; /* Space required for this record header */
3954 Page *pPg; /* Page to write to */
3955 u8 *aData; /* Data buffer for page pWriter->pPage */
3956 int nData = 0; /* Size of buffer aData[] in bytes */
3957 int nRec = 0; /* Number of records on page pPg */
3958 int iFPtr = 0; /* Value of pointer in footer of pPg */
3959 int iRPtr = 0; /* Value of pointer written into record */
3960 int iOff = 0; /* Current write offset within page pPg */
3961 Segment *pSeg; /* Segment being written */
3962 int flags = 0; /* If != 0, flags value for page footer */
3963 int bFirst = 0; /* True for first key of output run */
3965 pMerge = pMW->pLevel->pMerge;
3966 pSeg = &pMW->pLevel->lhs;
3968 if( pSeg->iFirst==0 && pMW->pPage==0 ){
3969 rc = mergeWorkerFirstPage(pMW);
3970 bFirst = 1;
3972 pPg = pMW->pPage;
3973 if( pPg ){
3974 aData = fsPageData(pPg, &nData);
3975 nRec = pageGetNRec(aData, nData);
3976 iFPtr = (int)pageGetPtr(aData, nData);
3977 iRPtr = iPtr - iFPtr;
3980 /* Figure out how much space is required by the new record. The space
3981 ** required is divided into two sections: the header and the body. The
3982 ** header consists of the intial varint fields. The body are the blobs
3983 ** of data that correspond to the key and value data. The entire header
3984 ** must be stored on the page. The body may overflow onto the next and
3985 ** subsequent pages.
3987 ** The header space is:
3989 ** 1) record type - 1 byte.
3990 ** 2) Page-pointer-offset - 1 varint
3991 ** 3) Key size - 1 varint
3992 ** 4) Value size - 1 varint (only if LSM_INSERT flag is set)
3994 if( rc==LSM_OK ){
3995 nHdr = 1 + lsmVarintLen32(iRPtr) + lsmVarintLen32(nKey);
3996 if( rtIsWrite(eType) ) nHdr += lsmVarintLen32(nVal);
3998 /* If the entire header will not fit on page pPg, or if page pPg is
3999 ** marked read-only, advance to the next page of the output run. */
4000 iOff = pMerge->iOutputOff;
4001 if( iOff<0 || pPg==0 || iOff+nHdr > SEGMENT_EOF(nData, nRec+1) ){
4002 if( iOff>=0 && pPg ){
4003 /* Zero any free space on the page */
4004 assert( aData );
4005 memset(&aData[iOff], 0, SEGMENT_EOF(nData, nRec)-iOff);
4007 iFPtr = (int)*pMW->pCsr->pPrevMergePtr;
4008 iRPtr = iPtr - iFPtr;
4009 iOff = 0;
4010 nRec = 0;
4011 rc = mergeWorkerNextPage(pMW, iFPtr);
4012 pPg = pMW->pPage;
4016 /* If this record header will be the first on the page, and the page is
4017 ** not the very first in the entire run, add a copy of the key to the
4018 ** b-tree hierarchy.
4020 if( rc==LSM_OK && nRec==0 && bFirst==0 ){
4021 assert( pMerge->nSkip>=0 );
4023 if( pMerge->nSkip==0 ){
4024 rc = mergeWorkerPushHierarchy(pMW, rtTopic(eType), pKey, nKey);
4025 assert( pMW->aSave[0].bStore==0 );
4026 pMW->aSave[0].bStore = 1;
4027 pMerge->nSkip = keyszToSkip(pMW->pDb->pFS, nKey);
4028 }else{
4029 pMerge->nSkip--;
4030 flags = PGFTR_SKIP_THIS_FLAG;
4033 if( pMerge->nSkip ) flags |= PGFTR_SKIP_NEXT_FLAG;
4036 /* Update the output segment */
4037 if( rc==LSM_OK ){
4038 aData = fsPageData(pPg, &nData);
4040 /* Update the page footer. */
4041 lsmPutU16(&aData[SEGMENT_NRECORD_OFFSET(nData)], (u16)(nRec+1));
4042 lsmPutU16(&aData[SEGMENT_CELLPTR_OFFSET(nData, nRec)], (u16)iOff);
4043 if( flags ) lsmPutU16(&aData[SEGMENT_FLAGS_OFFSET(nData)], (u16)flags);
4045 /* Write the entry header into the current page. */
4046 aData[iOff++] = (u8)eType; /* 1 */
4047 iOff += lsmVarintPut32(&aData[iOff], iRPtr); /* 2 */
4048 iOff += lsmVarintPut32(&aData[iOff], nKey); /* 3 */
4049 if( rtIsWrite(eType) ) iOff += lsmVarintPut32(&aData[iOff], nVal); /* 4 */
4050 pMerge->iOutputOff = iOff;
4052 /* Write the key and data into the segment. */
4053 assert( iFPtr==pageGetPtr(aData, nData) );
4054 rc = mergeWorkerData(pMW, 0, iFPtr+iRPtr, pKey, nKey);
4055 if( rc==LSM_OK && rtIsWrite(eType) ){
4056 if( rc==LSM_OK ){
4057 rc = mergeWorkerData(pMW, 0, iFPtr+iRPtr, pVal, nVal);
4062 return rc;
4067 ** Free all resources allocated by mergeWorkerInit().
4069 static void mergeWorkerShutdown(MergeWorker *pMW, int *pRc){
4070 int i; /* Iterator variable */
4071 int rc = *pRc;
4072 MultiCursor *pCsr = pMW->pCsr;
4074 /* Unless the merge has finished, save the cursor position in the
4075 ** Merge.aInput[] array. See function mergeWorkerInit() for the
4076 ** code to restore a cursor position based on aInput[]. */
4077 if( rc==LSM_OK && pCsr ){
4078 Merge *pMerge = pMW->pLevel->pMerge;
4079 if( lsmMCursorValid(pCsr) ){
4080 int bBtree = (pCsr->pBtCsr!=0);
4081 int iPtr;
4083 /* pMerge->nInput==0 indicates that this is a FlushTree() operation. */
4084 assert( pMerge->nInput==0 || pMW->pLevel->nRight>0 );
4085 assert( pMerge->nInput==0 || pMerge->nInput==(pCsr->nPtr+bBtree) );
4087 for(i=0; i<(pMerge->nInput-bBtree); i++){
4088 SegmentPtr *pPtr = &pCsr->aPtr[i];
4089 if( pPtr->pPg ){
4090 pMerge->aInput[i].iPg = lsmFsPageNumber(pPtr->pPg);
4091 pMerge->aInput[i].iCell = pPtr->iCell;
4092 }else{
4093 pMerge->aInput[i].iPg = 0;
4094 pMerge->aInput[i].iCell = 0;
4097 if( bBtree && pMerge->nInput ){
4098 assert( i==pCsr->nPtr );
4099 btreeCursorPosition(pCsr->pBtCsr, &pMerge->aInput[i]);
4102 /* Store the location of the split-key */
4103 iPtr = pCsr->aTree[1] - CURSOR_DATA_SEGMENT;
4104 if( iPtr<pCsr->nPtr ){
4105 pMerge->splitkey = pMerge->aInput[iPtr];
4106 }else{
4107 btreeCursorSplitkey(pCsr->pBtCsr, &pMerge->splitkey);
4111 /* Zero any free space left on the final page. This helps with
4112 ** compression if using a compression hook. And prevents valgrind
4113 ** from complaining about uninitialized byte passed to write(). */
4114 if( pMW->pPage ){
4115 int nData;
4116 u8 *aData = fsPageData(pMW->pPage, &nData);
4117 int iOff = pMerge->iOutputOff;
4118 int iEof = SEGMENT_EOF(nData, pageGetNRec(aData, nData));
4119 memset(&aData[iOff], 0, iEof - iOff);
4122 pMerge->iOutputOff = -1;
4125 lsmMCursorClose(pCsr, 0);
4127 /* Persist and release the output page. */
4128 if( rc==LSM_OK ) rc = mergeWorkerPersistAndRelease(pMW);
4129 if( rc==LSM_OK ) rc = mergeWorkerBtreeIndirect(pMW);
4130 if( rc==LSM_OK ) rc = mergeWorkerFinishHierarchy(pMW);
4131 if( rc==LSM_OK ) rc = mergeWorkerAddPadding(pMW);
4132 lsmFsFlushWaiting(pMW->pDb->pFS, &rc);
4133 mergeWorkerReleaseAll(pMW);
4135 lsmFree(pMW->pDb->pEnv, pMW->aGobble);
4136 pMW->aGobble = 0;
4137 pMW->pCsr = 0;
4139 *pRc = rc;
4143 ** The cursor passed as the first argument is being used as the input for
4144 ** a merge operation. When this function is called, *piFlags contains the
4145 ** database entry flags for the current entry. The entry about to be written
4146 ** to the output.
4148 ** Note that this function only has to work for cursors configured to
4149 ** iterate forwards (not backwards).
4151 static void mergeRangeDeletes(MultiCursor *pCsr, int *piVal, int *piFlags){
4152 int f = *piFlags;
4153 int iKey = pCsr->aTree[1];
4154 int i;
4156 assert( pCsr->flags & CURSOR_NEXT_OK );
4157 if( pCsr->flags & CURSOR_IGNORE_DELETE ){
4158 /* The ignore-delete flag is set when the output of the merge will form
4159 ** the oldest level in the database. In this case there is no point in
4160 ** retaining any range-delete flags. */
4161 assert( (f & LSM_POINT_DELETE)==0 );
4162 f &= ~(LSM_START_DELETE|LSM_END_DELETE);
4163 }else{
4164 for(i=0; i<(CURSOR_DATA_SEGMENT + pCsr->nPtr); i++){
4165 if( i!=iKey ){
4166 int eType;
4167 void *pKey;
4168 int nKey;
4169 int res;
4170 multiCursorGetKey(pCsr, i, &eType, &pKey, &nKey);
4172 if( pKey ){
4173 res = sortedKeyCompare(pCsr->pDb->xCmp,
4174 rtTopic(pCsr->eType), pCsr->key.pData, pCsr->key.nData,
4175 rtTopic(eType), pKey, nKey
4177 assert( res<=0 );
4178 if( res==0 ){
4179 if( (f & (LSM_INSERT|LSM_POINT_DELETE))==0 ){
4180 if( eType & LSM_INSERT ){
4181 f |= LSM_INSERT;
4182 *piVal = i;
4184 else if( eType & LSM_POINT_DELETE ){
4185 f |= LSM_POINT_DELETE;
4188 f |= (eType & (LSM_END_DELETE|LSM_START_DELETE));
4191 if( i>iKey && (eType & LSM_END_DELETE) && res<0 ){
4192 if( f & (LSM_INSERT|LSM_POINT_DELETE) ){
4193 f |= (LSM_END_DELETE|LSM_START_DELETE);
4194 }else{
4195 f = 0;
4197 break;
4203 assert( (f & LSM_INSERT)==0 || (f & LSM_POINT_DELETE)==0 );
4204 if( (f & LSM_START_DELETE)
4205 && (f & LSM_END_DELETE)
4206 && (f & LSM_POINT_DELETE )
4208 f = 0;
4212 *piFlags = f;
4215 static int mergeWorkerStep(MergeWorker *pMW){
4216 lsm_db *pDb = pMW->pDb; /* Database handle */
4217 MultiCursor *pCsr; /* Cursor to read input data from */
4218 int rc = LSM_OK; /* Return code */
4219 int eType; /* SORTED_SEPARATOR, WRITE or DELETE */
4220 void *pKey; int nKey; /* Key */
4221 LsmPgno iPtr;
4222 int iVal;
4224 pCsr = pMW->pCsr;
4226 /* Pull the next record out of the source cursor. */
4227 lsmMCursorKey(pCsr, &pKey, &nKey);
4228 eType = pCsr->eType;
4230 /* Figure out if the output record may have a different pointer value
4231 ** than the previous. This is the case if the current key is identical to
4232 ** a key that appears in the lowest level run being merged. If so, set
4233 ** iPtr to the absolute pointer value. If not, leave iPtr set to zero,
4234 ** indicating that the output pointer value should be a copy of the pointer
4235 ** value written with the previous key. */
4236 iPtr = (pCsr->pPrevMergePtr ? *pCsr->pPrevMergePtr : 0);
4237 if( pCsr->pBtCsr ){
4238 BtreeCursor *pBtCsr = pCsr->pBtCsr;
4239 if( pBtCsr->pKey ){
4240 int res = rtTopic(pBtCsr->eType) - rtTopic(eType);
4241 if( res==0 ) res = pDb->xCmp(pBtCsr->pKey, pBtCsr->nKey, pKey, nKey);
4242 if( 0==res ) iPtr = pBtCsr->iPtr;
4243 assert( res>=0 );
4245 }else if( pCsr->nPtr ){
4246 SegmentPtr *pPtr = &pCsr->aPtr[pCsr->nPtr-1];
4247 if( pPtr->pPg
4248 && 0==pDb->xCmp(pPtr->pKey, pPtr->nKey, pKey, nKey)
4250 iPtr = pPtr->iPtr+pPtr->iPgPtr;
4254 iVal = pCsr->aTree[1];
4255 mergeRangeDeletes(pCsr, &iVal, &eType);
4257 if( eType!=0 ){
4258 if( pMW->aGobble ){
4259 int iGobble = pCsr->aTree[1] - CURSOR_DATA_SEGMENT;
4260 if( iGobble<pCsr->nPtr && iGobble>=0 ){
4261 SegmentPtr *pGobble = &pCsr->aPtr[iGobble];
4262 if( (pGobble->flags & PGFTR_SKIP_THIS_FLAG)==0 ){
4263 pMW->aGobble[iGobble] = lsmFsPageNumber(pGobble->pPg);
4268 /* If this is a separator key and we know that the output pointer has not
4269 ** changed, there is no point in writing an output record. Otherwise,
4270 ** proceed. */
4271 if( rc==LSM_OK && (rtIsSeparator(eType)==0 || iPtr!=0) ){
4272 /* Write the record into the main run. */
4273 void *pVal; int nVal;
4274 rc = multiCursorGetVal(pCsr, iVal, &pVal, &nVal);
4275 if( pVal && rc==LSM_OK ){
4276 assert( nVal>=0 );
4277 rc = sortedBlobSet(pDb->pEnv, &pCsr->val, pVal, nVal);
4278 pVal = pCsr->val.pData;
4280 if( rc==LSM_OK ){
4281 rc = mergeWorkerWrite(pMW, eType, pKey, nKey, pVal, nVal, (int)iPtr);
4286 /* Advance the cursor to the next input record (assuming one exists). */
4287 assert( lsmMCursorValid(pMW->pCsr) );
4288 if( rc==LSM_OK ) rc = lsmMCursorNext(pMW->pCsr);
4290 return rc;
4293 static int mergeWorkerDone(MergeWorker *pMW){
4294 return pMW->pCsr==0 || !lsmMCursorValid(pMW->pCsr);
4297 static void sortedFreeLevel(lsm_env *pEnv, Level *p){
4298 if( p ){
4299 lsmFree(pEnv, p->pSplitKey);
4300 lsmFree(pEnv, p->pMerge);
4301 lsmFree(pEnv, p->aRhs);
4302 lsmFree(pEnv, p);
4306 static void sortedInvokeWorkHook(lsm_db *pDb){
4307 if( pDb->xWork ){
4308 pDb->xWork(pDb, pDb->pWorkCtx);
4312 static int sortedNewToplevel(
4313 lsm_db *pDb, /* Connection handle */
4314 int eTree, /* One of the TREE_XXX constants */
4315 int *pnWrite /* OUT: Number of database pages written */
4317 int rc = LSM_OK; /* Return Code */
4318 MultiCursor *pCsr = 0;
4319 Level *pNext = 0; /* The current top level */
4320 Level *pNew; /* The new level itself */
4321 Segment *pLinked = 0; /* Delete separators from this segment */
4322 Level *pDel = 0; /* Delete this entire level */
4323 int nWrite = 0; /* Number of database pages written */
4324 Freelist freelist;
4326 if( eTree!=TREE_NONE ){
4327 rc = lsmShmCacheChunks(pDb, pDb->treehdr.nChunk);
4330 assert( pDb->bUseFreelist==0 );
4331 pDb->pFreelist = &freelist;
4332 pDb->bUseFreelist = 1;
4333 memset(&freelist, 0, sizeof(freelist));
4335 /* Allocate the new level structure to write to. */
4336 pNext = lsmDbSnapshotLevel(pDb->pWorker);
4337 pNew = (Level *)lsmMallocZeroRc(pDb->pEnv, sizeof(Level), &rc);
4338 if( pNew ){
4339 pNew->pNext = pNext;
4340 lsmDbSnapshotSetLevel(pDb->pWorker, pNew);
4343 /* Create a cursor to gather the data required by the new segment. The new
4344 ** segment contains everything in the tree and pointers to the next segment
4345 ** in the database (if any). */
4346 pCsr = multiCursorNew(pDb, &rc);
4347 if( pCsr ){
4348 pCsr->pDb = pDb;
4349 rc = multiCursorVisitFreelist(pCsr);
4350 if( rc==LSM_OK ){
4351 rc = multiCursorAddTree(pCsr, pDb->pWorker, eTree);
4353 if( rc==LSM_OK && pNext && pNext->pMerge==0 ){
4354 if( (pNext->flags & LEVEL_FREELIST_ONLY) ){
4355 pDel = pNext;
4356 pCsr->aPtr = lsmMallocZeroRc(pDb->pEnv, sizeof(SegmentPtr), &rc);
4357 multiCursorAddOne(pCsr, pNext, &rc);
4358 }else if( eTree!=TREE_NONE && pNext->lhs.iRoot ){
4359 pLinked = &pNext->lhs;
4360 rc = btreeCursorNew(pDb, pLinked, &pCsr->pBtCsr);
4364 /* If this will be the only segment in the database, discard any delete
4365 ** markers present in the in-memory tree. */
4366 if( pNext==0 ){
4367 multiCursorIgnoreDelete(pCsr);
4371 if( rc!=LSM_OK ){
4372 lsmMCursorClose(pCsr, 0);
4373 }else{
4374 LsmPgno iLeftPtr = 0;
4375 Merge merge; /* Merge object used to create new level */
4376 MergeWorker mergeworker; /* MergeWorker object for the same purpose */
4378 memset(&merge, 0, sizeof(Merge));
4379 memset(&mergeworker, 0, sizeof(MergeWorker));
4381 pNew->pMerge = &merge;
4382 pNew->flags |= LEVEL_INCOMPLETE;
4383 mergeworker.pDb = pDb;
4384 mergeworker.pLevel = pNew;
4385 mergeworker.pCsr = pCsr;
4386 pCsr->pPrevMergePtr = &iLeftPtr;
4388 /* Mark the separators array for the new level as a "phantom". */
4389 mergeworker.bFlush = 1;
4391 /* Do the work to create the new merged segment on disk */
4392 if( rc==LSM_OK ) rc = lsmMCursorFirst(pCsr);
4393 while( rc==LSM_OK && mergeWorkerDone(&mergeworker)==0 ){
4394 rc = mergeWorkerStep(&mergeworker);
4396 mergeWorkerShutdown(&mergeworker, &rc);
4397 assert( rc!=LSM_OK || mergeworker.nWork==0 || pNew->lhs.iFirst );
4398 if( rc==LSM_OK && pNew->lhs.iFirst ){
4399 rc = lsmFsSortedFinish(pDb->pFS, &pNew->lhs);
4401 nWrite = mergeworker.nWork;
4402 pNew->flags &= ~LEVEL_INCOMPLETE;
4403 if( eTree==TREE_NONE ){
4404 pNew->flags |= LEVEL_FREELIST_ONLY;
4406 pNew->pMerge = 0;
4409 if( rc!=LSM_OK || pNew->lhs.iFirst==0 ){
4410 assert( rc!=LSM_OK || pDb->pWorker->freelist.nEntry==0 );
4411 lsmDbSnapshotSetLevel(pDb->pWorker, pNext);
4412 sortedFreeLevel(pDb->pEnv, pNew);
4413 }else{
4414 if( pLinked ){
4415 pLinked->iRoot = 0;
4416 }else if( pDel ){
4417 assert( pNew->pNext==pDel );
4418 pNew->pNext = pDel->pNext;
4419 lsmFsSortedDelete(pDb->pFS, pDb->pWorker, 1, &pDel->lhs);
4420 sortedFreeLevel(pDb->pEnv, pDel);
4423 #if LSM_LOG_STRUCTURE
4424 lsmSortedDumpStructure(pDb, pDb->pWorker, LSM_LOG_DATA, 0, "new-toplevel");
4425 #endif
4427 if( freelist.nEntry ){
4428 Freelist *p = &pDb->pWorker->freelist;
4429 lsmFree(pDb->pEnv, p->aEntry);
4430 memcpy(p, &freelist, sizeof(freelist));
4431 freelist.aEntry = 0;
4432 }else{
4433 pDb->pWorker->freelist.nEntry = 0;
4436 assertBtreeOk(pDb, &pNew->lhs);
4437 sortedInvokeWorkHook(pDb);
4440 if( pnWrite ) *pnWrite = nWrite;
4441 pDb->pWorker->nWrite += nWrite;
4442 pDb->pFreelist = 0;
4443 pDb->bUseFreelist = 0;
4444 lsmFree(pDb->pEnv, freelist.aEntry);
4445 return rc;
4449 ** The nMerge levels in the LSM beginning with pLevel consist of a
4450 ** left-hand-side segment only. Replace these levels with a single new
4451 ** level consisting of a new empty segment on the left-hand-side and the
4452 ** nMerge segments from the replaced levels on the right-hand-side.
4454 ** Also, allocate and populate a Merge object and set Level.pMerge to
4455 ** point to it.
4457 static int sortedMergeSetup(
4458 lsm_db *pDb, /* Database handle */
4459 Level *pLevel, /* First level to merge */
4460 int nMerge, /* Merge this many levels together */
4461 Level **ppNew /* New, merged, level */
4463 int rc = LSM_OK; /* Return Code */
4464 Level *pNew; /* New Level object */
4465 int bUseNext = 0; /* True to link in next separators */
4466 Merge *pMerge; /* New Merge object */
4467 int nByte; /* Bytes of space allocated at pMerge */
4469 #ifdef LSM_DEBUG
4470 int iLevel;
4471 Level *pX = pLevel;
4472 for(iLevel=0; iLevel<nMerge; iLevel++){
4473 assert( pX->nRight==0 );
4474 pX = pX->pNext;
4476 #endif
4478 /* Allocate the new Level object */
4479 pNew = (Level *)lsmMallocZeroRc(pDb->pEnv, sizeof(Level), &rc);
4480 if( pNew ){
4481 pNew->aRhs = (Segment *)lsmMallocZeroRc(pDb->pEnv,
4482 nMerge * sizeof(Segment), &rc);
4485 /* Populate the new Level object */
4486 if( rc==LSM_OK ){
4487 Level *pNext = 0; /* Level following pNew */
4488 int i;
4489 int bFreeOnly = 1;
4490 Level *pTopLevel;
4491 Level *p = pLevel;
4492 Level **pp;
4493 pNew->nRight = nMerge;
4494 pNew->iAge = pLevel->iAge+1;
4495 for(i=0; i<nMerge; i++){
4496 assert( p->nRight==0 );
4497 pNext = p->pNext;
4498 pNew->aRhs[i] = p->lhs;
4499 if( (p->flags & LEVEL_FREELIST_ONLY)==0 ) bFreeOnly = 0;
4500 sortedFreeLevel(pDb->pEnv, p);
4501 p = pNext;
4504 if( bFreeOnly ) pNew->flags |= LEVEL_FREELIST_ONLY;
4506 /* Replace the old levels with the new. */
4507 pTopLevel = lsmDbSnapshotLevel(pDb->pWorker);
4508 pNew->pNext = p;
4509 for(pp=&pTopLevel; *pp!=pLevel; pp=&((*pp)->pNext));
4510 *pp = pNew;
4511 lsmDbSnapshotSetLevel(pDb->pWorker, pTopLevel);
4513 /* Determine whether or not the next separators will be linked in */
4514 if( pNext && pNext->pMerge==0 && pNext->lhs.iRoot && pNext
4515 && (bFreeOnly==0 || (pNext->flags & LEVEL_FREELIST_ONLY))
4517 bUseNext = 1;
4521 /* Allocate the merge object */
4522 nByte = sizeof(Merge) + sizeof(MergeInput) * (nMerge + bUseNext);
4523 pMerge = (Merge *)lsmMallocZeroRc(pDb->pEnv, nByte, &rc);
4524 if( pMerge ){
4525 pMerge->aInput = (MergeInput *)&pMerge[1];
4526 pMerge->nInput = nMerge + bUseNext;
4527 pNew->pMerge = pMerge;
4530 *ppNew = pNew;
4531 return rc;
4534 static int mergeWorkerInit(
4535 lsm_db *pDb, /* Db connection to do merge work */
4536 Level *pLevel, /* Level to work on merging */
4537 MergeWorker *pMW /* Object to initialize */
4539 int rc = LSM_OK; /* Return code */
4540 Merge *pMerge = pLevel->pMerge; /* Persistent part of merge state */
4541 MultiCursor *pCsr = 0; /* Cursor opened for pMW */
4542 Level *pNext = pLevel->pNext; /* Next level in LSM */
4544 assert( pDb->pWorker );
4545 assert( pLevel->pMerge );
4546 assert( pLevel->nRight>0 );
4548 memset(pMW, 0, sizeof(MergeWorker));
4549 pMW->pDb = pDb;
4550 pMW->pLevel = pLevel;
4551 pMW->aGobble = lsmMallocZeroRc(pDb->pEnv, sizeof(LsmPgno)*pLevel->nRight,&rc);
4553 /* Create a multi-cursor to read the data to write to the new
4554 ** segment. The new segment contains:
4556 ** 1. Records from LHS of each of the nMerge levels being merged.
4557 ** 2. Separators from either the last level being merged, or the
4558 ** separators attached to the LHS of the following level, or neither.
4560 ** If the new level is the lowest (oldest) in the db, discard any
4561 ** delete keys. Key annihilation.
4563 pCsr = multiCursorNew(pDb, &rc);
4564 if( pCsr ){
4565 pCsr->flags |= CURSOR_NEXT_OK;
4566 rc = multiCursorAddRhs(pCsr, pLevel);
4568 if( rc==LSM_OK && pMerge->nInput > pLevel->nRight ){
4569 rc = btreeCursorNew(pDb, &pNext->lhs, &pCsr->pBtCsr);
4570 }else if( pNext ){
4571 multiCursorReadSeparators(pCsr);
4572 }else{
4573 multiCursorIgnoreDelete(pCsr);
4576 assert( rc!=LSM_OK || pMerge->nInput==(pCsr->nPtr+(pCsr->pBtCsr!=0)) );
4577 pMW->pCsr = pCsr;
4579 /* Load the b-tree hierarchy into memory. */
4580 if( rc==LSM_OK ) rc = mergeWorkerLoadHierarchy(pMW);
4581 if( rc==LSM_OK && pMW->hier.nHier==0 ){
4582 pMW->aSave[0].iPgno = pLevel->lhs.iFirst;
4585 /* Position the cursor. */
4586 if( rc==LSM_OK ){
4587 pCsr->pPrevMergePtr = &pMerge->iCurrentPtr;
4588 if( pLevel->lhs.iFirst==0 ){
4589 /* The output array is still empty. So position the cursor at the very
4590 ** start of the input. */
4591 rc = multiCursorEnd(pCsr, 0);
4592 }else{
4593 /* The output array is non-empty. Position the cursor based on the
4594 ** page/cell data saved in the Merge.aInput[] array. */
4595 int i;
4596 for(i=0; rc==LSM_OK && i<pCsr->nPtr; i++){
4597 MergeInput *pInput = &pMerge->aInput[i];
4598 if( pInput->iPg ){
4599 SegmentPtr *pPtr;
4600 assert( pCsr->aPtr[i].pPg==0 );
4601 pPtr = &pCsr->aPtr[i];
4602 rc = segmentPtrLoadPage(pDb->pFS, pPtr, (int)pInput->iPg);
4603 if( rc==LSM_OK && pPtr->nCell>0 ){
4604 rc = segmentPtrLoadCell(pPtr, pInput->iCell);
4609 if( rc==LSM_OK && pCsr->pBtCsr ){
4610 int (*xCmp)(void *, int, void *, int) = pCsr->pDb->xCmp;
4611 assert( i==pCsr->nPtr );
4612 rc = btreeCursorRestore(pCsr->pBtCsr, xCmp, &pMerge->aInput[i]);
4615 if( rc==LSM_OK ){
4616 rc = multiCursorSetupTree(pCsr, 0);
4619 pCsr->flags |= CURSOR_NEXT_OK;
4622 return rc;
4625 static int sortedBtreeGobble(
4626 lsm_db *pDb, /* Worker connection */
4627 MultiCursor *pCsr, /* Multi-cursor being used for a merge */
4628 int iGobble /* pCsr->aPtr[] entry to operate on */
4630 int rc = LSM_OK;
4631 if( rtTopic(pCsr->eType)==0 ){
4632 Segment *pSeg = pCsr->aPtr[iGobble].pSeg;
4633 LsmPgno *aPg;
4634 int nPg;
4636 /* Seek from the root of the b-tree to the segment leaf that may contain
4637 ** a key equal to the one multi-cursor currently points to. Record the
4638 ** page number of each b-tree page and the leaf. The segment may be
4639 ** gobbled up to (but not including) the first of these page numbers.
4641 assert( pSeg->iRoot>0 );
4642 aPg = lsmMallocZeroRc(pDb->pEnv, sizeof(LsmPgno)*32, &rc);
4643 if( rc==LSM_OK ){
4644 rc = seekInBtree(pCsr, pSeg,
4645 rtTopic(pCsr->eType), pCsr->key.pData, pCsr->key.nData, aPg, 0
4649 if( rc==LSM_OK ){
4650 for(nPg=0; aPg[nPg]; nPg++);
4651 lsmFsGobble(pDb, pSeg, aPg, nPg);
4654 lsmFree(pDb->pEnv, aPg);
4656 return rc;
4660 ** Argument p points to a level of age N. Return the number of levels in
4661 ** the linked list starting at p that have age=N (always at least 1).
4663 static int sortedCountLevels(Level *p){
4664 int iAge = p->iAge;
4665 int nRet = 0;
4666 do {
4667 nRet++;
4668 p = p->pNext;
4669 }while( p && p->iAge==iAge );
4670 return nRet;
4673 static int sortedSelectLevel(lsm_db *pDb, int nMerge, Level **ppOut){
4674 Level *pTopLevel = lsmDbSnapshotLevel(pDb->pWorker);
4675 int rc = LSM_OK;
4676 Level *pLevel = 0; /* Output value */
4677 Level *pBest = 0; /* Best level to work on found so far */
4678 int nBest; /* Number of segments merged at pBest */
4679 Level *pThis = 0; /* First in run of levels with age=iAge */
4680 int nThis = 0; /* Number of levels starting at pThis */
4682 assert( nMerge>=1 );
4683 nBest = LSM_MAX(1, nMerge-1);
4685 /* Find the longest contiguous run of levels not currently undergoing a
4686 ** merge with the same age in the structure. Or the level being merged
4687 ** with the largest number of right-hand segments. Work on it. */
4688 for(pLevel=pTopLevel; pLevel; pLevel=pLevel->pNext){
4689 if( pLevel->nRight==0 && pThis && pLevel->iAge==pThis->iAge ){
4690 nThis++;
4691 }else{
4692 if( nThis>nBest ){
4693 if( (pLevel->iAge!=pThis->iAge+1)
4694 || (pLevel->nRight==0 && sortedCountLevels(pLevel)<=pDb->nMerge)
4696 pBest = pThis;
4697 nBest = nThis;
4700 if( pLevel->nRight ){
4701 if( pLevel->nRight>nBest ){
4702 nBest = pLevel->nRight;
4703 pBest = pLevel;
4705 nThis = 0;
4706 pThis = 0;
4707 }else{
4708 pThis = pLevel;
4709 nThis = 1;
4713 if( nThis>nBest ){
4714 assert( pThis );
4715 pBest = pThis;
4716 nBest = nThis;
4719 if( pBest==0 && nMerge==1 ){
4720 int nFree = 0;
4721 int nUsr = 0;
4722 for(pLevel=pTopLevel; pLevel; pLevel=pLevel->pNext){
4723 assert( !pLevel->nRight );
4724 if( pLevel->flags & LEVEL_FREELIST_ONLY ){
4725 nFree++;
4726 }else{
4727 nUsr++;
4730 if( nUsr>1 ){
4731 pBest = pTopLevel;
4732 nBest = nFree + nUsr;
4736 if( pBest ){
4737 if( pBest->nRight==0 ){
4738 rc = sortedMergeSetup(pDb, pBest, nBest, ppOut);
4739 }else{
4740 *ppOut = pBest;
4744 return rc;
4747 static int sortedDbIsFull(lsm_db *pDb){
4748 Level *pTop = lsmDbSnapshotLevel(pDb->pWorker);
4750 if( lsmDatabaseFull(pDb) ) return 1;
4751 if( pTop && pTop->iAge==0
4752 && (pTop->nRight || sortedCountLevels(pTop)>=pDb->nMerge)
4754 return 1;
4756 return 0;
4759 typedef struct MoveBlockCtx MoveBlockCtx;
4760 struct MoveBlockCtx {
4761 int iSeen; /* Previous free block on list */
4762 int iFrom; /* Total number of blocks in file */
4765 static int moveBlockCb(void *pCtx, int iBlk, i64 iSnapshot){
4766 MoveBlockCtx *p = (MoveBlockCtx *)pCtx;
4767 assert( p->iFrom==0 );
4768 if( iBlk==(p->iSeen-1) ){
4769 p->iSeen = iBlk;
4770 return 0;
4772 p->iFrom = p->iSeen-1;
4773 return 1;
4777 ** This function is called to further compact a database for which all
4778 ** of the content has already been merged into a single segment. If
4779 ** possible, it moves the contents of a single block from the end of the
4780 ** file to a free-block that lies closer to the start of the file (allowing
4781 ** the file to be eventually truncated).
4783 static int sortedMoveBlock(lsm_db *pDb, int *pnWrite){
4784 Snapshot *p = pDb->pWorker;
4785 Level *pLvl = lsmDbSnapshotLevel(p);
4786 int iFrom; /* Block to move */
4787 int iTo; /* Destination to move block to */
4788 int rc; /* Return code */
4790 MoveBlockCtx sCtx;
4792 assert( pLvl->pNext==0 && pLvl->nRight==0 );
4793 assert( p->redirect.n<=LSM_MAX_BLOCK_REDIRECTS );
4795 *pnWrite = 0;
4797 /* Check that the redirect array is not already full. If it is, return
4798 ** without moving any database content. */
4799 if( p->redirect.n>=LSM_MAX_BLOCK_REDIRECTS ) return LSM_OK;
4801 /* Find the last block of content in the database file. Do this by
4802 ** traversing the free-list in reverse (descending block number) order.
4803 ** The first block not on the free list is the one that will be moved.
4804 ** Since the db consists of a single segment, there is no ambiguity as
4805 ** to which segment the block belongs to. */
4806 sCtx.iSeen = p->nBlock+1;
4807 sCtx.iFrom = 0;
4808 rc = lsmWalkFreelist(pDb, 1, moveBlockCb, &sCtx);
4809 if( rc!=LSM_OK || sCtx.iFrom==0 ) return rc;
4810 iFrom = sCtx.iFrom;
4812 /* Find the first free block in the database, ignoring block 1. Block
4813 ** 1 is tricky as it is smaller than the other blocks. */
4814 rc = lsmBlockAllocate(pDb, iFrom, &iTo);
4815 if( rc!=LSM_OK || iTo==0 ) return rc;
4816 assert( iTo!=1 && iTo<iFrom );
4818 rc = lsmFsMoveBlock(pDb->pFS, &pLvl->lhs, iTo, iFrom);
4819 if( rc==LSM_OK ){
4820 if( p->redirect.a==0 ){
4821 int nByte = sizeof(struct RedirectEntry) * LSM_MAX_BLOCK_REDIRECTS;
4822 p->redirect.a = lsmMallocZeroRc(pDb->pEnv, nByte, &rc);
4824 if( rc==LSM_OK ){
4826 /* Check if the block just moved was already redirected. */
4827 int i;
4828 for(i=0; i<p->redirect.n; i++){
4829 if( p->redirect.a[i].iTo==iFrom ) break;
4832 if( i==p->redirect.n ){
4833 /* Block iFrom was not already redirected. Add a new array entry. */
4834 memmove(&p->redirect.a[1], &p->redirect.a[0],
4835 sizeof(struct RedirectEntry) * p->redirect.n
4837 p->redirect.a[0].iFrom = iFrom;
4838 p->redirect.a[0].iTo = iTo;
4839 p->redirect.n++;
4840 }else{
4841 /* Block iFrom was already redirected. Overwrite existing entry. */
4842 p->redirect.a[i].iTo = iTo;
4845 rc = lsmBlockFree(pDb, iFrom);
4847 *pnWrite = lsmFsBlockSize(pDb->pFS) / lsmFsPageSize(pDb->pFS);
4848 pLvl->lhs.pRedirect = &p->redirect;
4852 #if LSM_LOG_STRUCTURE
4853 if( rc==LSM_OK ){
4854 char aBuf[64];
4855 sprintf(aBuf, "move-block %d/%d", p->redirect.n-1, LSM_MAX_BLOCK_REDIRECTS);
4856 lsmSortedDumpStructure(pDb, pDb->pWorker, LSM_LOG_DATA, 0, aBuf);
4858 #endif
4859 return rc;
4864 static int mergeInsertFreelistSegments(
4865 lsm_db *pDb,
4866 int nFree,
4867 MergeWorker *pMW
4869 int rc = LSM_OK;
4870 if( nFree>0 ){
4871 MultiCursor *pCsr = pMW->pCsr;
4872 Level *pLvl = pMW->pLevel;
4873 SegmentPtr *aNew1;
4874 Segment *aNew2;
4876 Level *pIter;
4877 Level *pNext;
4878 int i = 0;
4880 aNew1 = (SegmentPtr *)lsmMallocZeroRc(
4881 pDb->pEnv, sizeof(SegmentPtr) * (pCsr->nPtr+nFree), &rc
4883 if( rc ) return rc;
4884 memcpy(&aNew1[nFree], pCsr->aPtr, sizeof(SegmentPtr)*pCsr->nPtr);
4885 pCsr->nPtr += nFree;
4886 lsmFree(pDb->pEnv, pCsr->aTree);
4887 lsmFree(pDb->pEnv, pCsr->aPtr);
4888 pCsr->aTree = 0;
4889 pCsr->aPtr = aNew1;
4891 aNew2 = (Segment *)lsmMallocZeroRc(
4892 pDb->pEnv, sizeof(Segment) * (pLvl->nRight+nFree), &rc
4894 if( rc ) return rc;
4895 memcpy(&aNew2[nFree], pLvl->aRhs, sizeof(Segment)*pLvl->nRight);
4896 pLvl->nRight += nFree;
4897 lsmFree(pDb->pEnv, pLvl->aRhs);
4898 pLvl->aRhs = aNew2;
4900 for(pIter=pDb->pWorker->pLevel; rc==LSM_OK && pIter!=pLvl; pIter=pNext){
4901 Segment *pSeg = &pLvl->aRhs[i];
4902 memcpy(pSeg, &pIter->lhs, sizeof(Segment));
4904 pCsr->aPtr[i].pSeg = pSeg;
4905 pCsr->aPtr[i].pLevel = pLvl;
4906 rc = segmentPtrEnd(pCsr, &pCsr->aPtr[i], 0);
4908 pDb->pWorker->pLevel = pNext = pIter->pNext;
4909 sortedFreeLevel(pDb->pEnv, pIter);
4910 i++;
4912 assert( i==nFree );
4913 assert( rc!=LSM_OK || pDb->pWorker->pLevel==pLvl );
4915 for(i=nFree; i<pCsr->nPtr; i++){
4916 pCsr->aPtr[i].pSeg = &pLvl->aRhs[i];
4919 lsmFree(pDb->pEnv, pMW->aGobble);
4920 pMW->aGobble = 0;
4922 return rc;
4925 static int sortedWork(
4926 lsm_db *pDb, /* Database handle. Must be worker. */
4927 int nWork, /* Number of pages of work to do */
4928 int nMerge, /* Try to merge this many levels at once */
4929 int bFlush, /* Set if call is to make room for a flush */
4930 int *pnWrite /* OUT: Actual number of pages written */
4932 int rc = LSM_OK; /* Return Code */
4933 int nRemaining = nWork; /* Units of work to do before returning */
4934 Snapshot *pWorker = pDb->pWorker;
4936 assert( pWorker );
4937 if( lsmDbSnapshotLevel(pWorker)==0 ) return LSM_OK;
4939 while( nRemaining>0 ){
4940 Level *pLevel = 0;
4942 /* Find a level to work on. */
4943 rc = sortedSelectLevel(pDb, nMerge, &pLevel);
4944 assert( rc==LSM_OK || pLevel==0 );
4946 if( pLevel==0 ){
4947 int nDone = 0;
4948 Level *pTopLevel = lsmDbSnapshotLevel(pDb->pWorker);
4949 if( bFlush==0 && nMerge==1 && pTopLevel && pTopLevel->pNext==0 ){
4950 rc = sortedMoveBlock(pDb, &nDone);
4952 nRemaining -= nDone;
4954 /* Could not find any work to do. Finished. */
4955 if( nDone==0 ) break;
4956 }else{
4957 int bSave = 0;
4958 Freelist freelist = {0, 0, 0};
4959 MergeWorker mergeworker; /* State used to work on the level merge */
4961 assert( pDb->bIncrMerge==0 );
4962 assert( pDb->pFreelist==0 && pDb->bUseFreelist==0 );
4964 pDb->bIncrMerge = 1;
4965 rc = mergeWorkerInit(pDb, pLevel, &mergeworker);
4966 assert( mergeworker.nWork==0 );
4968 while( rc==LSM_OK
4969 && 0==mergeWorkerDone(&mergeworker)
4970 && (mergeworker.nWork<nRemaining || pDb->bUseFreelist)
4972 int eType = rtTopic(mergeworker.pCsr->eType);
4973 rc = mergeWorkerStep(&mergeworker);
4975 /* If the cursor now points at the first entry past the end of the
4976 ** user data (i.e. either to EOF or to the first free-list entry
4977 ** that will be added to the run), then check if it is possible to
4978 ** merge in any free-list entries that are either in-memory or in
4979 ** free-list-only blocks. */
4980 if( rc==LSM_OK && nMerge==1 && eType==0
4981 && (rtTopic(mergeworker.pCsr->eType) || mergeWorkerDone(&mergeworker))
4983 int nFree = 0; /* Number of free-list-only levels to merge */
4984 Level *pLvl;
4985 assert( pDb->pFreelist==0 && pDb->bUseFreelist==0 );
4987 /* Now check if all levels containing data newer than this one
4988 ** are single-segment free-list only levels. If so, they will be
4989 ** merged in now. */
4990 for(pLvl=pDb->pWorker->pLevel;
4991 pLvl!=mergeworker.pLevel && (pLvl->flags & LEVEL_FREELIST_ONLY);
4992 pLvl=pLvl->pNext
4994 assert( pLvl->nRight==0 );
4995 nFree++;
4997 if( pLvl==mergeworker.pLevel ){
4999 rc = mergeInsertFreelistSegments(pDb, nFree, &mergeworker);
5000 if( rc==LSM_OK ){
5001 rc = multiCursorVisitFreelist(mergeworker.pCsr);
5003 if( rc==LSM_OK ){
5004 rc = multiCursorSetupTree(mergeworker.pCsr, 0);
5005 pDb->pFreelist = &freelist;
5006 pDb->bUseFreelist = 1;
5011 nRemaining -= LSM_MAX(mergeworker.nWork, 1);
5013 if( rc==LSM_OK ){
5014 /* Check if the merge operation is completely finished. If not,
5015 ** gobble up (declare eligible for recycling) any pages from rhs
5016 ** segments for which the content has been completely merged into
5017 ** the lhs of the level. */
5018 if( mergeWorkerDone(&mergeworker)==0 ){
5019 int i;
5020 for(i=0; i<pLevel->nRight; i++){
5021 SegmentPtr *pGobble = &mergeworker.pCsr->aPtr[i];
5022 if( pGobble->pSeg->iRoot ){
5023 rc = sortedBtreeGobble(pDb, mergeworker.pCsr, i);
5024 }else if( mergeworker.aGobble[i] ){
5025 lsmFsGobble(pDb, pGobble->pSeg, &mergeworker.aGobble[i], 1);
5028 }else{
5029 int i;
5030 int bEmpty;
5031 mergeWorkerShutdown(&mergeworker, &rc);
5032 bEmpty = (pLevel->lhs.iFirst==0);
5034 if( bEmpty==0 && rc==LSM_OK ){
5035 rc = lsmFsSortedFinish(pDb->pFS, &pLevel->lhs);
5038 if( pDb->bUseFreelist ){
5039 Freelist *p = &pDb->pWorker->freelist;
5040 lsmFree(pDb->pEnv, p->aEntry);
5041 memcpy(p, &freelist, sizeof(freelist));
5042 pDb->bUseFreelist = 0;
5043 pDb->pFreelist = 0;
5044 bSave = 1;
5047 for(i=0; i<pLevel->nRight; i++){
5048 lsmFsSortedDelete(pDb->pFS, pWorker, 1, &pLevel->aRhs[i]);
5051 if( bEmpty ){
5052 /* If the new level is completely empty, remove it from the
5053 ** database snapshot. This can only happen if all input keys were
5054 ** annihilated. Since keys are only annihilated if the new level
5055 ** is the last in the linked list (contains the most ancient of
5056 ** database content), this guarantees that pLevel->pNext==0. */
5057 Level *pTop; /* Top level of worker snapshot */
5058 Level **pp; /* Read/write iterator for Level.pNext list */
5060 assert( pLevel->pNext==0 );
5062 /* Remove the level from the worker snapshot. */
5063 pTop = lsmDbSnapshotLevel(pWorker);
5064 for(pp=&pTop; *pp!=pLevel; pp=&((*pp)->pNext));
5065 *pp = pLevel->pNext;
5066 lsmDbSnapshotSetLevel(pWorker, pTop);
5068 /* Free the Level structure. */
5069 sortedFreeLevel(pDb->pEnv, pLevel);
5070 }else{
5072 /* Free the separators of the next level, if required. */
5073 if( pLevel->pMerge->nInput > pLevel->nRight ){
5074 assert( pLevel->pNext->lhs.iRoot );
5075 pLevel->pNext->lhs.iRoot = 0;
5078 /* Zero the right-hand-side of pLevel */
5079 lsmFree(pDb->pEnv, pLevel->aRhs);
5080 pLevel->nRight = 0;
5081 pLevel->aRhs = 0;
5083 /* Free the Merge object */
5084 lsmFree(pDb->pEnv, pLevel->pMerge);
5085 pLevel->pMerge = 0;
5088 if( bSave && rc==LSM_OK ){
5089 pDb->bIncrMerge = 0;
5090 rc = lsmSaveWorker(pDb, 0);
5095 /* Clean up the MergeWorker object initialized above. If no error
5096 ** has occurred, invoke the work-hook to inform the application that
5097 ** the database structure has changed. */
5098 mergeWorkerShutdown(&mergeworker, &rc);
5099 pDb->bIncrMerge = 0;
5100 if( rc==LSM_OK ) sortedInvokeWorkHook(pDb);
5102 #if LSM_LOG_STRUCTURE
5103 lsmSortedDumpStructure(pDb, pDb->pWorker, LSM_LOG_DATA, 0, "work");
5104 #endif
5105 assertBtreeOk(pDb, &pLevel->lhs);
5106 assertRunInOrder(pDb, &pLevel->lhs);
5108 /* If bFlush is true and the database is no longer considered "full",
5109 ** break out of the loop even if nRemaining is still greater than
5110 ** zero. The caller has an in-memory tree to flush to disk. */
5111 if( bFlush && sortedDbIsFull(pDb)==0 ) break;
5115 if( pnWrite ) *pnWrite = (nWork - nRemaining);
5116 pWorker->nWrite += (nWork - nRemaining);
5118 #ifdef LSM_LOG_WORK
5119 lsmLogMessage(pDb, rc, "sortedWork(): %d pages", (nWork-nRemaining));
5120 #endif
5121 return rc;
5125 ** The database connection passed as the first argument must be a worker
5126 ** connection. This function checks if there exists an "old" in-memory tree
5127 ** ready to be flushed to disk. If so, true is returned. Otherwise false.
5129 ** If an error occurs, *pRc is set to an LSM error code before returning.
5130 ** It is assumed that *pRc is set to LSM_OK when this function is called.
5132 static int sortedTreeHasOld(lsm_db *pDb, int *pRc){
5133 int rc = LSM_OK;
5134 int bRet = 0;
5136 assert( pDb->pWorker );
5137 if( *pRc==LSM_OK ){
5138 if( rc==LSM_OK
5139 && pDb->treehdr.iOldShmid
5140 && pDb->treehdr.iOldLog!=pDb->pWorker->iLogOff
5142 bRet = 1;
5143 }else{
5144 bRet = 0;
5146 *pRc = rc;
5148 assert( *pRc==LSM_OK || bRet==0 );
5149 return bRet;
5153 ** Create a new free-list only top-level segment. Return LSM_OK if successful
5154 ** or an LSM error code if some error occurs.
5156 static int sortedNewFreelistOnly(lsm_db *pDb){
5157 return sortedNewToplevel(pDb, TREE_NONE, 0);
5160 int lsmSaveWorker(lsm_db *pDb, int bFlush){
5161 Snapshot *p = pDb->pWorker;
5162 if( p->freelist.nEntry>pDb->nMaxFreelist ){
5163 int rc = sortedNewFreelistOnly(pDb);
5164 if( rc!=LSM_OK ) return rc;
5166 return lsmCheckpointSaveWorker(pDb, bFlush);
5169 static int doLsmSingleWork(
5170 lsm_db *pDb,
5171 int bShutdown,
5172 int nMerge, /* Minimum segments to merge together */
5173 int nPage, /* Number of pages to write to disk */
5174 int *pnWrite, /* OUT: Pages actually written to disk */
5175 int *pbCkpt /* OUT: True if an auto-checkpoint is req. */
5177 Snapshot *pWorker; /* Worker snapshot */
5178 int rc = LSM_OK; /* Return code */
5179 int bDirty = 0;
5180 int nMax = nPage; /* Maximum pages to write to disk */
5181 int nRem = nPage;
5182 int bCkpt = 0;
5184 assert( nPage>0 );
5186 /* Open the worker 'transaction'. It will be closed before this function
5187 ** returns. */
5188 assert( pDb->pWorker==0 );
5189 rc = lsmBeginWork(pDb);
5190 if( rc!=LSM_OK ) return rc;
5191 pWorker = pDb->pWorker;
5193 /* If this connection is doing auto-checkpoints, set nMax (and nRem) so
5194 ** that this call stops writing when the auto-checkpoint is due. The
5195 ** caller will do the checkpoint, then possibly call this function again. */
5196 if( bShutdown==0 && pDb->nAutockpt ){
5197 u32 nSync;
5198 u32 nUnsync;
5199 int nPgsz;
5201 lsmCheckpointSynced(pDb, 0, 0, &nSync);
5202 nUnsync = lsmCheckpointNWrite(pDb->pShmhdr->aSnap1, 0);
5203 nPgsz = lsmCheckpointPgsz(pDb->pShmhdr->aSnap1);
5205 nMax = (int)LSM_MIN(nMax, (pDb->nAutockpt/nPgsz) - (int)(nUnsync-nSync));
5206 if( nMax<nRem ){
5207 bCkpt = 1;
5208 nRem = LSM_MAX(nMax, 0);
5212 /* If there exists in-memory data ready to be flushed to disk, attempt
5213 ** to flush it now. */
5214 if( pDb->nTransOpen==0 ){
5215 rc = lsmTreeLoadHeader(pDb, 0);
5217 if( sortedTreeHasOld(pDb, &rc) ){
5218 /* sortedDbIsFull() returns non-zero if either (a) there are too many
5219 ** levels in total in the db, or (b) there are too many levels with the
5220 ** the same age in the db. Either way, call sortedWork() to merge
5221 ** existing segments together until this condition is cleared. */
5222 if( sortedDbIsFull(pDb) ){
5223 int nPg = 0;
5224 rc = sortedWork(pDb, nRem, nMerge, 1, &nPg);
5225 nRem -= nPg;
5226 assert( rc!=LSM_OK || nRem<=0 || !sortedDbIsFull(pDb) );
5227 bDirty = 1;
5230 if( rc==LSM_OK && nRem>0 ){
5231 int nPg = 0;
5232 rc = sortedNewToplevel(pDb, TREE_OLD, &nPg);
5233 nRem -= nPg;
5234 if( rc==LSM_OK ){
5235 if( pDb->nTransOpen>0 ){
5236 lsmTreeDiscardOld(pDb);
5238 rc = lsmSaveWorker(pDb, 1);
5239 bDirty = 0;
5244 /* If nPage is still greater than zero, do some merging. */
5245 if( rc==LSM_OK && nRem>0 && bShutdown==0 ){
5246 int nPg = 0;
5247 rc = sortedWork(pDb, nRem, nMerge, 0, &nPg);
5248 nRem -= nPg;
5249 if( nPg ) bDirty = 1;
5252 /* If the in-memory part of the free-list is too large, write a new
5253 ** top-level containing just the in-memory free-list entries to disk. */
5254 if( rc==LSM_OK && pDb->pWorker->freelist.nEntry > pDb->nMaxFreelist ){
5255 while( rc==LSM_OK && lsmDatabaseFull(pDb) ){
5256 int nPg = 0;
5257 rc = sortedWork(pDb, 16, nMerge, 1, &nPg);
5258 nRem -= nPg;
5260 if( rc==LSM_OK ){
5261 rc = sortedNewFreelistOnly(pDb);
5263 bDirty = 1;
5266 if( rc==LSM_OK ){
5267 *pnWrite = (nMax - nRem);
5268 *pbCkpt = (bCkpt && nRem<=0);
5269 if( nMerge==1 && pDb->nAutockpt>0 && *pnWrite>0
5270 && pWorker->pLevel
5271 && pWorker->pLevel->nRight==0
5272 && pWorker->pLevel->pNext==0
5274 *pbCkpt = 1;
5278 if( rc==LSM_OK && bDirty ){
5279 lsmFinishWork(pDb, 0, &rc);
5280 }else{
5281 int rcdummy = LSM_BUSY;
5282 lsmFinishWork(pDb, 0, &rcdummy);
5283 *pnWrite = 0;
5285 assert( pDb->pWorker==0 );
5286 return rc;
5289 static int doLsmWork(lsm_db *pDb, int nMerge, int nPage, int *pnWrite){
5290 int rc = LSM_OK; /* Return code */
5291 int nWrite = 0; /* Number of pages written */
5293 assert( nMerge>=1 );
5295 if( nPage!=0 ){
5296 int bCkpt = 0;
5297 do {
5298 int nThis = 0;
5299 int nReq = (nPage>=0) ? (nPage-nWrite) : ((int)0x7FFFFFFF);
5301 bCkpt = 0;
5302 rc = doLsmSingleWork(pDb, 0, nMerge, nReq, &nThis, &bCkpt);
5303 nWrite += nThis;
5304 if( rc==LSM_OK && bCkpt ){
5305 rc = lsm_checkpoint(pDb, 0);
5307 }while( rc==LSM_OK && bCkpt && (nWrite<nPage || nPage<0) );
5310 if( pnWrite ){
5311 if( rc==LSM_OK ){
5312 *pnWrite = nWrite;
5313 }else{
5314 *pnWrite = 0;
5317 return rc;
5321 ** Perform work to merge database segments together.
5323 int lsm_work(lsm_db *pDb, int nMerge, int nKB, int *pnWrite){
5324 int rc; /* Return code */
5325 int nPgsz; /* Nominal page size in bytes */
5326 int nPage; /* Equivalent of nKB in pages */
5327 int nWrite = 0; /* Number of pages written */
5329 /* This function may not be called if pDb has an open read or write
5330 ** transaction. Return LSM_MISUSE if an application attempts this. */
5331 if( pDb->nTransOpen || pDb->pCsr ) return LSM_MISUSE_BKPT;
5332 if( nMerge<=0 ) nMerge = pDb->nMerge;
5334 lsmFsPurgeCache(pDb->pFS);
5336 /* Convert from KB to pages */
5337 nPgsz = lsmFsPageSize(pDb->pFS);
5338 if( nKB>=0 ){
5339 nPage = ((i64)nKB * 1024 + nPgsz - 1) / nPgsz;
5340 }else{
5341 nPage = -1;
5344 rc = doLsmWork(pDb, nMerge, nPage, &nWrite);
5346 if( pnWrite ){
5347 /* Convert back from pages to KB */
5348 *pnWrite = (int)(((i64)nWrite * 1024 + nPgsz - 1) / nPgsz);
5350 return rc;
5353 int lsm_flush(lsm_db *db){
5354 int rc;
5356 if( db->nTransOpen>0 || db->pCsr ){
5357 rc = LSM_MISUSE_BKPT;
5358 }else{
5359 rc = lsmBeginWriteTrans(db);
5360 if( rc==LSM_OK ){
5361 lsmFlushTreeToDisk(db);
5362 lsmTreeDiscardOld(db);
5363 lsmTreeMakeOld(db);
5364 lsmTreeDiscardOld(db);
5367 if( rc==LSM_OK ){
5368 rc = lsmFinishWriteTrans(db, 1);
5369 }else{
5370 lsmFinishWriteTrans(db, 0);
5372 lsmFinishReadTrans(db);
5375 return rc;
5379 ** This function is called in auto-work mode to perform merging work on
5380 ** the data structure. It performs enough merging work to prevent the
5381 ** height of the tree from growing indefinitely assuming that roughly
5382 ** nUnit database pages worth of data have been written to the database
5383 ** (i.e. the in-memory tree) since the last call.
5385 int lsmSortedAutoWork(
5386 lsm_db *pDb, /* Database handle */
5387 int nUnit /* Pages of data written to in-memory tree */
5389 int rc = LSM_OK; /* Return code */
5390 int nDepth = 0; /* Current height of tree (longest path) */
5391 Level *pLevel; /* Used to iterate through levels */
5392 int bRestore = 0;
5394 assert( pDb->pWorker==0 );
5395 assert( pDb->nTransOpen>0 );
5397 /* Determine how many units of work to do before returning. One unit of
5398 ** work is achieved by writing one page (~4KB) of merged data. */
5399 for(pLevel=lsmDbSnapshotLevel(pDb->pClient); pLevel; pLevel=pLevel->pNext){
5400 /* nDepth += LSM_MAX(1, pLevel->nRight); */
5401 nDepth += 1;
5403 if( lsmTreeHasOld(pDb) ){
5404 nDepth += 1;
5405 bRestore = 1;
5406 rc = lsmSaveCursors(pDb);
5407 if( rc!=LSM_OK ) return rc;
5410 if( nDepth>0 ){
5411 int nRemaining; /* Units of work to do before returning */
5413 nRemaining = nUnit * nDepth;
5414 #ifdef LSM_LOG_WORK
5415 lsmLogMessage(pDb, rc, "lsmSortedAutoWork(): %d*%d = %d pages",
5416 nUnit, nDepth, nRemaining);
5417 #endif
5418 assert( nRemaining>=0 );
5419 rc = doLsmWork(pDb, pDb->nMerge, nRemaining, 0);
5420 if( rc==LSM_BUSY ) rc = LSM_OK;
5422 if( bRestore && pDb->pCsr ){
5423 lsmMCursorFreeCache(pDb);
5424 lsmFreeSnapshot(pDb->pEnv, pDb->pClient);
5425 pDb->pClient = 0;
5426 if( rc==LSM_OK ){
5427 rc = lsmCheckpointLoad(pDb, 0);
5429 if( rc==LSM_OK ){
5430 rc = lsmCheckpointDeserialize(pDb, 0, pDb->aSnapshot, &pDb->pClient);
5432 if( rc==LSM_OK ){
5433 rc = lsmRestoreCursors(pDb);
5438 return rc;
5442 ** This function is only called during system shutdown. The contents of
5443 ** any in-memory trees present (old or current) are written out to disk.
5445 int lsmFlushTreeToDisk(lsm_db *pDb){
5446 int rc;
5448 rc = lsmBeginWork(pDb);
5449 while( rc==LSM_OK && sortedDbIsFull(pDb) ){
5450 rc = sortedWork(pDb, 256, pDb->nMerge, 1, 0);
5453 if( rc==LSM_OK ){
5454 rc = sortedNewToplevel(pDb, TREE_BOTH, 0);
5457 lsmFinishWork(pDb, 1, &rc);
5458 return rc;
5462 ** Return a string representation of the segment passed as the only argument.
5463 ** Space for the returned string is allocated using lsmMalloc(), and should
5464 ** be freed by the caller using lsmFree().
5466 static char *segToString(lsm_env *pEnv, Segment *pSeg, int nMin){
5467 int nSize = pSeg->nSize;
5468 LsmPgno iRoot = pSeg->iRoot;
5469 LsmPgno iFirst = pSeg->iFirst;
5470 LsmPgno iLast = pSeg->iLastPg;
5471 char *z;
5473 char *z1;
5474 char *z2;
5475 int nPad;
5477 z1 = lsmMallocPrintf(pEnv, "%d.%d", iFirst, iLast);
5478 if( iRoot ){
5479 z2 = lsmMallocPrintf(pEnv, "root=%d", iRoot);
5480 }else{
5481 z2 = lsmMallocPrintf(pEnv, "size=%d", nSize);
5484 nPad = nMin - 2 - strlen(z1) - 1 - strlen(z2);
5485 nPad = LSM_MAX(0, nPad);
5487 if( iRoot ){
5488 z = lsmMallocPrintf(pEnv, "/%s %*s%s\\", z1, nPad, "", z2);
5489 }else{
5490 z = lsmMallocPrintf(pEnv, "|%s %*s%s|", z1, nPad, "", z2);
5492 lsmFree(pEnv, z1);
5493 lsmFree(pEnv, z2);
5495 return z;
5498 static int fileToString(
5499 lsm_db *pDb, /* For xMalloc() */
5500 char *aBuf,
5501 int nBuf,
5502 int nMin,
5503 Segment *pSeg
5505 int i = 0;
5506 if( pSeg ){
5507 char *zSeg;
5509 zSeg = segToString(pDb->pEnv, pSeg, nMin);
5510 snprintf(&aBuf[i], nBuf-i, "%s", zSeg);
5511 i += strlen(&aBuf[i]);
5512 lsmFree(pDb->pEnv, zSeg);
5514 #ifdef LSM_LOG_FREELIST
5515 lsmInfoArrayStructure(pDb, 1, pSeg->iFirst, &zSeg);
5516 snprintf(&aBuf[i], nBuf-1, " (%s)", zSeg);
5517 i += strlen(&aBuf[i]);
5518 lsmFree(pDb->pEnv, zSeg);
5519 #endif
5520 aBuf[nBuf] = 0;
5521 }else{
5522 aBuf[0] = '\0';
5525 return i;
5528 void sortedDumpPage(lsm_db *pDb, Segment *pRun, Page *pPg, int bVals){
5529 LsmBlob blob = {0, 0, 0}; /* LsmBlob used for keys */
5530 LsmString s;
5531 int i;
5533 int nRec;
5534 int iPtr;
5535 int flags;
5536 u8 *aData;
5537 int nData;
5539 aData = fsPageData(pPg, &nData);
5541 nRec = pageGetNRec(aData, nData);
5542 iPtr = (int)pageGetPtr(aData, nData);
5543 flags = pageGetFlags(aData, nData);
5545 lsmStringInit(&s, pDb->pEnv);
5546 lsmStringAppendf(&s,"nCell=%d iPtr=%d flags=%d {", nRec, iPtr, flags);
5547 if( flags&SEGMENT_BTREE_FLAG ) iPtr = 0;
5549 for(i=0; i<nRec; i++){
5550 Page *pRef = 0; /* Pointer to page iRef */
5551 int iChar;
5552 u8 *aKey; int nKey = 0; /* Key */
5553 u8 *aVal = 0; int nVal = 0; /* Value */
5554 int iTopic;
5555 u8 *aCell;
5556 int iPgPtr;
5557 int eType;
5559 aCell = pageGetCell(aData, nData, i);
5560 eType = *aCell++;
5561 assert( (flags & SEGMENT_BTREE_FLAG) || eType!=0 );
5562 aCell += lsmVarintGet32(aCell, &iPgPtr);
5564 if( eType==0 ){
5565 LsmPgno iRef; /* Page number of referenced page */
5566 aCell += lsmVarintGet64(aCell, &iRef);
5567 lsmFsDbPageGet(pDb->pFS, pRun, iRef, &pRef);
5568 aKey = pageGetKey(pRun, pRef, 0, &iTopic, &nKey, &blob);
5569 }else{
5570 aCell += lsmVarintGet32(aCell, &nKey);
5571 if( rtIsWrite(eType) ) aCell += lsmVarintGet32(aCell, &nVal);
5572 sortedReadData(0, pPg, (aCell-aData), nKey+nVal, (void **)&aKey, &blob);
5573 aVal = &aKey[nKey];
5574 iTopic = eType;
5577 lsmStringAppendf(&s, "%s%2X:", (i==0?"":" "), iTopic);
5578 for(iChar=0; iChar<nKey; iChar++){
5579 lsmStringAppendf(&s, "%c", isalnum(aKey[iChar]) ? aKey[iChar] : '.');
5581 if( nVal>0 && bVals ){
5582 lsmStringAppendf(&s, "##");
5583 for(iChar=0; iChar<nVal; iChar++){
5584 lsmStringAppendf(&s, "%c", isalnum(aVal[iChar]) ? aVal[iChar] : '.');
5588 lsmStringAppendf(&s, " %d", iPgPtr+iPtr);
5589 lsmFsPageRelease(pRef);
5591 lsmStringAppend(&s, "}", 1);
5593 lsmLogMessage(pDb, LSM_OK, " Page %d: %s", lsmFsPageNumber(pPg), s.z);
5594 lsmStringClear(&s);
5596 sortedBlobFree(&blob);
5599 static void infoCellDump(
5600 lsm_db *pDb, /* Database handle */
5601 Segment *pSeg, /* Segment page belongs to */
5602 int bIndirect, /* True to follow indirect refs */
5603 Page *pPg,
5604 int iCell,
5605 int *peType,
5606 int *piPgPtr,
5607 u8 **paKey, int *pnKey,
5608 u8 **paVal, int *pnVal,
5609 LsmBlob *pBlob
5611 u8 *aData; int nData; /* Page data */
5612 u8 *aKey; int nKey = 0; /* Key */
5613 u8 *aVal = 0; int nVal = 0; /* Value */
5614 int eType;
5615 int iPgPtr;
5616 Page *pRef = 0; /* Pointer to page iRef */
5617 u8 *aCell;
5619 aData = fsPageData(pPg, &nData);
5621 aCell = pageGetCell(aData, nData, iCell);
5622 eType = *aCell++;
5623 aCell += lsmVarintGet32(aCell, &iPgPtr);
5625 if( eType==0 ){
5626 int dummy;
5627 LsmPgno iRef; /* Page number of referenced page */
5628 aCell += lsmVarintGet64(aCell, &iRef);
5629 if( bIndirect ){
5630 lsmFsDbPageGet(pDb->pFS, pSeg, iRef, &pRef);
5631 pageGetKeyCopy(pDb->pEnv, pSeg, pRef, 0, &dummy, pBlob);
5632 aKey = (u8 *)pBlob->pData;
5633 nKey = pBlob->nData;
5634 lsmFsPageRelease(pRef);
5635 }else{
5636 aKey = (u8 *)"<indirect>";
5637 nKey = 11;
5639 }else{
5640 aCell += lsmVarintGet32(aCell, &nKey);
5641 if( rtIsWrite(eType) ) aCell += lsmVarintGet32(aCell, &nVal);
5642 sortedReadData(pSeg, pPg, (aCell-aData), nKey+nVal, (void **)&aKey, pBlob);
5643 aVal = &aKey[nKey];
5646 if( peType ) *peType = eType;
5647 if( piPgPtr ) *piPgPtr = iPgPtr;
5648 if( paKey ) *paKey = aKey;
5649 if( paVal ) *paVal = aVal;
5650 if( pnKey ) *pnKey = nKey;
5651 if( pnVal ) *pnVal = nVal;
5654 static int infoAppendBlob(LsmString *pStr, int bHex, u8 *z, int n){
5655 int iChar;
5656 for(iChar=0; iChar<n; iChar++){
5657 if( bHex ){
5658 lsmStringAppendf(pStr, "%02X", z[iChar]);
5659 }else{
5660 lsmStringAppendf(pStr, "%c", isalnum(z[iChar]) ?z[iChar] : '.');
5663 return LSM_OK;
5666 #define INFO_PAGE_DUMP_DATA 0x01
5667 #define INFO_PAGE_DUMP_VALUES 0x02
5668 #define INFO_PAGE_DUMP_HEX 0x04
5669 #define INFO_PAGE_DUMP_INDIRECT 0x08
5671 static int infoPageDump(
5672 lsm_db *pDb, /* Database handle */
5673 LsmPgno iPg, /* Page number of page to dump */
5674 int flags,
5675 char **pzOut /* OUT: lsmMalloc'd string */
5677 int rc = LSM_OK; /* Return code */
5678 Page *pPg = 0; /* Handle for page iPg */
5679 int i, j; /* Loop counters */
5680 const int perLine = 16; /* Bytes per line in the raw hex dump */
5681 Segment *pSeg = 0;
5682 Snapshot *pSnap;
5684 int bValues = (flags & INFO_PAGE_DUMP_VALUES);
5685 int bHex = (flags & INFO_PAGE_DUMP_HEX);
5686 int bData = (flags & INFO_PAGE_DUMP_DATA);
5687 int bIndirect = (flags & INFO_PAGE_DUMP_INDIRECT);
5689 *pzOut = 0;
5690 if( iPg==0 ) return LSM_ERROR;
5692 assert( pDb->pClient || pDb->pWorker );
5693 pSnap = pDb->pClient;
5694 if( pSnap==0 ) pSnap = pDb->pWorker;
5695 if( pSnap->redirect.n>0 ){
5696 Level *pLvl;
5697 int bUse = 0;
5698 for(pLvl=pSnap->pLevel; pLvl->pNext; pLvl=pLvl->pNext);
5699 pSeg = (pLvl->nRight==0 ? &pLvl->lhs : &pLvl->aRhs[pLvl->nRight-1]);
5700 rc = lsmFsSegmentContainsPg(pDb->pFS, pSeg, iPg, &bUse);
5701 if( bUse==0 ){
5702 pSeg = 0;
5706 /* iPg is a real page number (not subject to redirection). So it is safe
5707 ** to pass a NULL in place of the segment pointer as the second argument
5708 ** to lsmFsDbPageGet() here. */
5709 if( rc==LSM_OK ){
5710 rc = lsmFsDbPageGet(pDb->pFS, 0, iPg, &pPg);
5713 if( rc==LSM_OK ){
5714 LsmBlob blob = {0, 0, 0, 0};
5715 int nKeyWidth = 0;
5716 LsmString str;
5717 int nRec;
5718 int iPtr;
5719 int flags2;
5720 int iCell;
5721 u8 *aData; int nData; /* Page data and size thereof */
5723 aData = fsPageData(pPg, &nData);
5724 nRec = pageGetNRec(aData, nData);
5725 iPtr = (int)pageGetPtr(aData, nData);
5726 flags2 = pageGetFlags(aData, nData);
5728 lsmStringInit(&str, pDb->pEnv);
5729 lsmStringAppendf(&str, "Page : %lld (%d bytes)\n", iPg, nData);
5730 lsmStringAppendf(&str, "nRec : %d\n", nRec);
5731 lsmStringAppendf(&str, "iPtr : %d\n", iPtr);
5732 lsmStringAppendf(&str, "flags: %04x\n", flags2);
5733 lsmStringAppendf(&str, "\n");
5735 for(iCell=0; iCell<nRec; iCell++){
5736 int nKey;
5737 infoCellDump(
5738 pDb, pSeg, bIndirect, pPg, iCell, 0, 0, 0, &nKey, 0, 0, &blob
5740 if( nKey>nKeyWidth ) nKeyWidth = nKey;
5742 if( bHex ) nKeyWidth = nKeyWidth * 2;
5744 for(iCell=0; iCell<nRec; iCell++){
5745 u8 *aKey; int nKey = 0; /* Key */
5746 u8 *aVal; int nVal = 0; /* Value */
5747 int iPgPtr;
5748 int eType;
5749 LsmPgno iAbsPtr;
5750 char zFlags[8];
5752 infoCellDump(pDb, pSeg, bIndirect, pPg, iCell, &eType, &iPgPtr,
5753 &aKey, &nKey, &aVal, &nVal, &blob
5755 iAbsPtr = iPgPtr + ((flags2 & SEGMENT_BTREE_FLAG) ? 0 : iPtr);
5757 lsmFlagsToString(eType, zFlags);
5758 lsmStringAppendf(&str, "%s %d (%s) ",
5759 zFlags, iAbsPtr, (rtTopic(eType) ? "sys" : "usr")
5761 infoAppendBlob(&str, bHex, aKey, nKey);
5762 if( nVal>0 && bValues ){
5763 lsmStringAppendf(&str, "%*s", nKeyWidth - (nKey*(1+bHex)), "");
5764 lsmStringAppendf(&str, " ");
5765 infoAppendBlob(&str, bHex, aVal, nVal);
5767 if( rtTopic(eType) ){
5768 int iBlk = (int)~lsmGetU32(aKey);
5769 lsmStringAppendf(&str, " (block=%d", iBlk);
5770 if( nVal>0 ){
5771 i64 iSnap = lsmGetU64(aVal);
5772 lsmStringAppendf(&str, " snapshot=%lld", iSnap);
5774 lsmStringAppendf(&str, ")");
5776 lsmStringAppendf(&str, "\n");
5779 if( bData ){
5780 lsmStringAppendf(&str, "\n-------------------"
5781 "-------------------------------------------------------------\n");
5782 lsmStringAppendf(&str, "Page %d\n",
5783 iPg, (iPg-1)*nData, iPg*nData - 1);
5784 for(i=0; i<nData; i += perLine){
5785 lsmStringAppendf(&str, "%04x: ", i);
5786 for(j=0; j<perLine; j++){
5787 if( i+j>nData ){
5788 lsmStringAppendf(&str, " ");
5789 }else{
5790 lsmStringAppendf(&str, "%02x ", aData[i+j]);
5793 lsmStringAppendf(&str, " ");
5794 for(j=0; j<perLine; j++){
5795 if( i+j>nData ){
5796 lsmStringAppendf(&str, " ");
5797 }else{
5798 lsmStringAppendf(&str,"%c", isprint(aData[i+j]) ? aData[i+j] : '.');
5801 lsmStringAppendf(&str,"\n");
5805 *pzOut = str.z;
5806 sortedBlobFree(&blob);
5807 lsmFsPageRelease(pPg);
5810 return rc;
5813 int lsmInfoPageDump(
5814 lsm_db *pDb, /* Database handle */
5815 LsmPgno iPg, /* Page number of page to dump */
5816 int bHex, /* True to output key/value in hex form */
5817 char **pzOut /* OUT: lsmMalloc'd string */
5819 int flags = INFO_PAGE_DUMP_DATA | INFO_PAGE_DUMP_VALUES;
5820 if( bHex ) flags |= INFO_PAGE_DUMP_HEX;
5821 return infoPageDump(pDb, iPg, flags, pzOut);
5824 void sortedDumpSegment(lsm_db *pDb, Segment *pRun, int bVals){
5825 assert( pDb->xLog );
5826 if( pRun && pRun->iFirst ){
5827 int flags = (bVals ? INFO_PAGE_DUMP_VALUES : 0);
5828 char *zSeg;
5829 Page *pPg;
5831 zSeg = segToString(pDb->pEnv, pRun, 0);
5832 lsmLogMessage(pDb, LSM_OK, "Segment: %s", zSeg);
5833 lsmFree(pDb->pEnv, zSeg);
5835 lsmFsDbPageGet(pDb->pFS, pRun, pRun->iFirst, &pPg);
5836 while( pPg ){
5837 Page *pNext;
5838 char *z = 0;
5839 infoPageDump(pDb, lsmFsPageNumber(pPg), flags, &z);
5840 lsmLogMessage(pDb, LSM_OK, "%s", z);
5841 lsmFree(pDb->pEnv, z);
5842 #if 0
5843 sortedDumpPage(pDb, pRun, pPg, bVals);
5844 #endif
5845 lsmFsDbPageNext(pRun, pPg, 1, &pNext);
5846 lsmFsPageRelease(pPg);
5847 pPg = pNext;
5853 ** Invoke the log callback zero or more times with messages that describe
5854 ** the current database structure.
5856 void lsmSortedDumpStructure(
5857 lsm_db *pDb, /* Database handle (used for xLog callback) */
5858 Snapshot *pSnap, /* Snapshot to dump */
5859 int bKeys, /* Output the keys from each segment */
5860 int bVals, /* Output the values from each segment */
5861 const char *zWhy /* Caption to print near top of dump */
5863 Snapshot *pDump = pSnap;
5864 Level *pTopLevel;
5865 char *zFree = 0;
5867 assert( pSnap );
5868 pTopLevel = lsmDbSnapshotLevel(pDump);
5869 if( pDb->xLog && pTopLevel ){
5870 static int nCall = 0;
5871 Level *pLevel;
5872 int iLevel = 0;
5874 nCall++;
5875 lsmLogMessage(pDb, LSM_OK, "Database structure %d (%s)", nCall, zWhy);
5877 #if 0
5878 if( nCall==1031 || nCall==1032 ) bKeys=1;
5879 #endif
5881 for(pLevel=pTopLevel; pLevel; pLevel=pLevel->pNext){
5882 char zLeft[1024];
5883 char zRight[1024];
5884 int i = 0;
5886 Segment *aLeft[24];
5887 Segment *aRight[24];
5889 int nLeft = 0;
5890 int nRight = 0;
5892 Segment *pSeg = &pLevel->lhs;
5893 aLeft[nLeft++] = pSeg;
5895 for(i=0; i<pLevel->nRight; i++){
5896 aRight[nRight++] = &pLevel->aRhs[i];
5899 #ifdef LSM_LOG_FREELIST
5900 if( nRight ){
5901 memmove(&aRight[1], aRight, sizeof(aRight[0])*nRight);
5902 aRight[0] = 0;
5903 nRight++;
5905 #endif
5907 for(i=0; i<nLeft || i<nRight; i++){
5908 int iPad = 0;
5909 char zLevel[32];
5910 zLeft[0] = '\0';
5911 zRight[0] = '\0';
5913 if( i<nLeft ){
5914 fileToString(pDb, zLeft, sizeof(zLeft), 24, aLeft[i]);
5916 if( i<nRight ){
5917 fileToString(pDb, zRight, sizeof(zRight), 24, aRight[i]);
5920 if( i==0 ){
5921 snprintf(zLevel, sizeof(zLevel), "L%d: (age=%d) (flags=%.4x)",
5922 iLevel, (int)pLevel->iAge, (int)pLevel->flags
5924 }else{
5925 zLevel[0] = '\0';
5928 if( nRight==0 ){
5929 iPad = 10;
5932 lsmLogMessage(pDb, LSM_OK, "% 25s % *s% -35s %s",
5933 zLevel, iPad, "", zLeft, zRight
5937 iLevel++;
5940 if( bKeys ){
5941 for(pLevel=pTopLevel; pLevel; pLevel=pLevel->pNext){
5942 int i;
5943 sortedDumpSegment(pDb, &pLevel->lhs, bVals);
5944 for(i=0; i<pLevel->nRight; i++){
5945 sortedDumpSegment(pDb, &pLevel->aRhs[i], bVals);
5951 lsmInfoFreelist(pDb, &zFree);
5952 lsmLogMessage(pDb, LSM_OK, "Freelist: %s", zFree);
5953 lsmFree(pDb->pEnv, zFree);
5955 assert( lsmFsIntegrityCheck(pDb) );
5958 void lsmSortedFreeLevel(lsm_env *pEnv, Level *pLevel){
5959 Level *pNext;
5960 Level *p;
5962 for(p=pLevel; p; p=pNext){
5963 pNext = p->pNext;
5964 sortedFreeLevel(pEnv, p);
5968 void lsmSortedSaveTreeCursors(lsm_db *pDb){
5969 MultiCursor *pCsr;
5970 for(pCsr=pDb->pCsr; pCsr; pCsr=pCsr->pNext){
5971 lsmTreeCursorSave(pCsr->apTreeCsr[0]);
5972 lsmTreeCursorSave(pCsr->apTreeCsr[1]);
5976 void lsmSortedExpandBtreePage(Page *pPg, int nOrig){
5977 u8 *aData;
5978 int nData;
5979 int nEntry;
5980 int iHdr;
5982 aData = lsmFsPageData(pPg, &nData);
5983 nEntry = pageGetNRec(aData, nOrig);
5984 iHdr = SEGMENT_EOF(nOrig, nEntry);
5985 memmove(&aData[iHdr + (nData-nOrig)], &aData[iHdr], nOrig-iHdr);
5988 #ifdef LSM_DEBUG_EXPENSIVE
5989 static void assertRunInOrder(lsm_db *pDb, Segment *pSeg){
5990 Page *pPg = 0;
5991 LsmBlob blob1 = {0, 0, 0, 0};
5992 LsmBlob blob2 = {0, 0, 0, 0};
5994 lsmFsDbPageGet(pDb->pFS, pSeg, pSeg->iFirst, &pPg);
5995 while( pPg ){
5996 u8 *aData; int nData;
5997 Page *pNext;
5999 aData = lsmFsPageData(pPg, &nData);
6000 if( 0==(pageGetFlags(aData, nData) & SEGMENT_BTREE_FLAG) ){
6001 int i;
6002 int nRec = pageGetNRec(aData, nData);
6003 for(i=0; i<nRec; i++){
6004 int iTopic1, iTopic2;
6005 pageGetKeyCopy(pDb->pEnv, pSeg, pPg, i, &iTopic1, &blob1);
6007 if( i==0 && blob2.nData ){
6008 assert( sortedKeyCompare(
6009 pDb->xCmp, iTopic2, blob2.pData, blob2.nData,
6010 iTopic1, blob1.pData, blob1.nData
6011 )<0 );
6014 if( i<(nRec-1) ){
6015 pageGetKeyCopy(pDb->pEnv, pSeg, pPg, i+1, &iTopic2, &blob2);
6016 assert( sortedKeyCompare(
6017 pDb->xCmp, iTopic1, blob1.pData, blob1.nData,
6018 iTopic2, blob2.pData, blob2.nData
6019 )<0 );
6024 lsmFsDbPageNext(pSeg, pPg, 1, &pNext);
6025 lsmFsPageRelease(pPg);
6026 pPg = pNext;
6029 sortedBlobFree(&blob1);
6030 sortedBlobFree(&blob2);
6032 #endif
6034 #ifdef LSM_DEBUG_EXPENSIVE
6036 ** This function is only included in the build if LSM_DEBUG_EXPENSIVE is
6037 ** defined. Its only purpose is to evaluate various assert() statements to
6038 ** verify that the database is well formed in certain respects.
6040 ** More specifically, it checks that the array pOne contains the required
6041 ** pointers to pTwo. Array pTwo must be a main array. pOne may be either a
6042 ** separators array or another main array. If pOne does not contain the
6043 ** correct set of pointers, an assert() statement fails.
6045 static int assertPointersOk(
6046 lsm_db *pDb, /* Database handle */
6047 Segment *pOne, /* Segment containing pointers */
6048 Segment *pTwo, /* Segment containing pointer targets */
6049 int bRhs /* True if pTwo may have been Gobble()d */
6051 int rc = LSM_OK; /* Error code */
6052 SegmentPtr ptr1; /* Iterates through pOne */
6053 SegmentPtr ptr2; /* Iterates through pTwo */
6054 LsmPgno iPrev;
6056 assert( pOne && pTwo );
6058 memset(&ptr1, 0, sizeof(ptr1));
6059 memset(&ptr2, 0, sizeof(ptr1));
6060 ptr1.pSeg = pOne;
6061 ptr2.pSeg = pTwo;
6062 segmentPtrEndPage(pDb->pFS, &ptr1, 0, &rc);
6063 segmentPtrEndPage(pDb->pFS, &ptr2, 0, &rc);
6065 /* Check that the footer pointer of the first page of pOne points to
6066 ** the first page of pTwo. */
6067 iPrev = pTwo->iFirst;
6068 if( ptr1.iPtr!=iPrev && !bRhs ){
6069 assert( 0 );
6072 if( rc==LSM_OK && ptr1.nCell>0 ){
6073 rc = segmentPtrLoadCell(&ptr1, 0);
6076 while( rc==LSM_OK && ptr2.pPg ){
6077 LsmPgno iThis;
6079 /* Advance to the next page of segment pTwo that contains at least
6080 ** one cell. Break out of the loop if the iterator reaches EOF. */
6082 rc = segmentPtrNextPage(&ptr2, 1);
6083 assert( rc==LSM_OK );
6084 }while( rc==LSM_OK && ptr2.pPg && ptr2.nCell==0 );
6085 if( rc!=LSM_OK || ptr2.pPg==0 ) break;
6086 iThis = lsmFsPageNumber(ptr2.pPg);
6088 if( (ptr2.flags & (PGFTR_SKIP_THIS_FLAG|SEGMENT_BTREE_FLAG))==0 ){
6090 /* Load the first cell in the array pTwo page. */
6091 rc = segmentPtrLoadCell(&ptr2, 0);
6093 /* Iterate forwards through pOne, searching for a key that matches the
6094 ** key ptr2.pKey/nKey. This key should have a pointer to the page that
6095 ** ptr2 currently points to. */
6096 while( rc==LSM_OK ){
6097 int res = rtTopic(ptr1.eType) - rtTopic(ptr2.eType);
6098 if( res==0 ){
6099 res = pDb->xCmp(ptr1.pKey, ptr1.nKey, ptr2.pKey, ptr2.nKey);
6102 if( res<0 ){
6103 assert( bRhs || ptr1.iPtr+ptr1.iPgPtr==iPrev );
6104 }else if( res>0 ){
6105 assert( 0 );
6106 }else{
6107 assert( ptr1.iPtr+ptr1.iPgPtr==iThis );
6108 iPrev = iThis;
6109 break;
6112 rc = segmentPtrAdvance(0, &ptr1, 0);
6113 if( ptr1.pPg==0 ){
6114 assert( 0 );
6120 segmentPtrReset(&ptr1, 0);
6121 segmentPtrReset(&ptr2, 0);
6122 return LSM_OK;
6126 ** This function is only included in the build if LSM_DEBUG_EXPENSIVE is
6127 ** defined. Its only purpose is to evaluate various assert() statements to
6128 ** verify that the database is well formed in certain respects.
6130 ** More specifically, it checks that the b-tree embedded in array pRun
6131 ** contains the correct keys. If not, an assert() fails.
6133 static int assertBtreeOk(
6134 lsm_db *pDb,
6135 Segment *pSeg
6137 int rc = LSM_OK; /* Return code */
6138 if( pSeg->iRoot ){
6139 LsmBlob blob = {0, 0, 0}; /* Buffer used to cache overflow keys */
6140 FileSystem *pFS = pDb->pFS; /* File system to read from */
6141 Page *pPg = 0; /* Main run page */
6142 BtreeCursor *pCsr = 0; /* Btree cursor */
6144 rc = btreeCursorNew(pDb, pSeg, &pCsr);
6145 if( rc==LSM_OK ){
6146 rc = btreeCursorFirst(pCsr);
6148 if( rc==LSM_OK ){
6149 rc = lsmFsDbPageGet(pFS, pSeg, pSeg->iFirst, &pPg);
6152 while( rc==LSM_OK ){
6153 Page *pNext;
6154 u8 *aData;
6155 int nData;
6156 int flags;
6158 rc = lsmFsDbPageNext(pSeg, pPg, 1, &pNext);
6159 lsmFsPageRelease(pPg);
6160 pPg = pNext;
6161 if( pPg==0 ) break;
6162 aData = fsPageData(pPg, &nData);
6163 flags = pageGetFlags(aData, nData);
6164 if( rc==LSM_OK
6165 && 0==((SEGMENT_BTREE_FLAG|PGFTR_SKIP_THIS_FLAG) & flags)
6166 && 0!=pageGetNRec(aData, nData)
6168 u8 *pKey;
6169 int nKey;
6170 int iTopic;
6171 pKey = pageGetKey(pSeg, pPg, 0, &iTopic, &nKey, &blob);
6172 assert( nKey==pCsr->nKey && 0==memcmp(pKey, pCsr->pKey, nKey) );
6173 assert( lsmFsPageNumber(pPg)==pCsr->iPtr );
6174 rc = btreeCursorNext(pCsr);
6177 assert( rc!=LSM_OK || pCsr->pKey==0 );
6179 if( pPg ) lsmFsPageRelease(pPg);
6181 btreeCursorFree(pCsr);
6182 sortedBlobFree(&blob);
6185 return rc;
6187 #endif /* ifdef LSM_DEBUG_EXPENSIVE */