From 5d69ba630e0098024fe179156b53a9c1895946f5 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Mon, 12 Feb 2024 18:06:46 +0200 Subject: [PATCH] lib/mergeset: properly record the firstItem in metaindexRow at blockStreamWriter.WriteBlock The 3c246cdf00ea453dd5d42ef8d679ca96df54b61c added an optimization where the previous metaindexRow could be saved to disk when the current block header couldn't be added indexBlock because the resulting indexBlock size became too big. This could result in an empty metaindexRow.firstItem for the next metaindexRow. --- lib/mergeset/block_stream_writer.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/mergeset/block_stream_writer.go b/lib/mergeset/block_stream_writer.go index a184d7a4b..242ec8615 100644 --- a/lib/mergeset/block_stream_writer.go +++ b/lib/mergeset/block_stream_writer.go @@ -130,11 +130,6 @@ func (bsw *blockStreamWriter) MustClose() { func (bsw *blockStreamWriter) WriteBlock(ib *inmemoryBlock) { bsw.bh.firstItem, bsw.bh.commonPrefix, bsw.bh.itemsCount, bsw.bh.marshalType = ib.MarshalSortedData(&bsw.sb, bsw.bh.firstItem[:0], bsw.bh.commonPrefix[:0], bsw.compressLevel) - if !bsw.mrFirstItemCaught { - bsw.mr.firstItem = append(bsw.mr.firstItem[:0], bsw.bh.firstItem...) - bsw.mrFirstItemCaught = true - } - // Write itemsData fs.MustWriteData(bsw.itemsWriter, bsw.sb.itemsData) bsw.bh.itemsBlockSize = uint32(len(bsw.sb.itemsData)) @@ -155,6 +150,11 @@ func (bsw *blockStreamWriter) WriteBlock(ib *inmemoryBlock) { bsw.flushIndexData() bsw.unpackedIndexBlockBuf = bsw.bh.Marshal(bsw.unpackedIndexBlockBuf) } + + if !bsw.mrFirstItemCaught { + bsw.mr.firstItem = append(bsw.mr.firstItem[:0], bsw.bh.firstItem...) + bsw.mrFirstItemCaught = true + } bsw.bh.Reset() bsw.mr.blockHeadersCount++ }