@@ -739,18 +739,19 @@ func (c *LeveledCompactor) populateBlock(blocks []BlockReader, meta *BlockMeta,
739739 }
740740
741741 ref , lset , chks , dranges := set .At () // The chunks here are not fully deleted.
742+ // Skip the series with all deleted chunks.
743+ if len (chks ) == 0 {
744+ set .RemoveLastRef ()
745+ continue
746+ }
747+
742748 if overlapping {
743749 // If blocks are overlapping, it is possible to have unsorted chunks.
744750 sort .Slice (chks , func (i , j int ) bool {
745751 return chks [i ].MinTime < chks [j ].MinTime
746752 })
747753 }
748754
749- // Skip the series with all deleted chunks.
750- if len (chks ) == 0 {
751- continue
752- }
753-
754755 for i , chk := range chks {
755756 // Re-encode head chunks that are still open (being appended to) or
756757 // outside the compacted MaxTime range.
@@ -814,7 +815,6 @@ func (c *LeveledCompactor) populateBlock(blocks []BlockReader, meta *BlockMeta,
814815 if err := chunkw .WriteChunks (mergedChks ... ); err != nil {
815816 return errors .Wrap (err , "write chunks" )
816817 }
817-
818818 if err := indexw .AddSeries (ref , lset , mergedChks ... ); err != nil {
819819 return errors .Wrap (err , "add series" )
820820 }
@@ -1100,6 +1100,8 @@ func (c *compactionMerger) Next() bool {
11001100 ref , lset , chks , intervals := c .sets [0 ].At ()
11011101 idx := 0
11021102
1103+ // Find the labels with the lowest index when
1104+ // sorted in ascending order.
11031105 for i , s := range c .sets [1 :] {
11041106 if ! c .oks [1 + i ] {
11051107 continue
@@ -1111,6 +1113,9 @@ func (c *compactionMerger) Next() bool {
11111113 }
11121114 }
11131115
1116+ // Building the seriesMap and gathering the chunks
1117+ // from other index readers which have same labels as
1118+ // 'lset' described above.
11141119 c .l = append (c .l [:0 ], lset ... )
11151120 c .c = append (c .c [:0 ], chks ... )
11161121 c.seriesMap [idx ][ref ] = c .ref
@@ -1135,6 +1140,22 @@ func (c *compactionMerger) Next() bool {
11351140 return true
11361141}
11371142
1143+ // After calling RemoveLastRef(), At() is only valid after calling
1144+ // another Next().
1145+ func (c * compactionMerger ) RemoveLastRef () {
1146+ if c .ref == 0 || c .Err () != nil {
1147+ return
1148+ }
1149+ toRemoveRef := c .ref - 1
1150+ for key1 := range c .seriesMap {
1151+ for key2 , rf := range c .seriesMap [key1 ] {
1152+ if rf == toRemoveRef {
1153+ delete (c .seriesMap [key1 ], key2 )
1154+ }
1155+ }
1156+ }
1157+ }
1158+
11381159func (c * compactionMerger ) Err () error {
11391160 for _ , s := range c .sets {
11401161 if s .Err () != nil {
0 commit comments