@@ -2135,14 +2135,12 @@ func buildChunkSamples(d encoding.Decbuf, numChunks int, info *chunkSamples) err
21352135 return d .Err ()
21362136}
21372137
2138- func (dec * Decoder ) prepSeries (b []byte , lbls * labels.Labels , chks * []ChunkMeta ) (* encoding.Decbuf , uint64 , error ) {
2138+ // prepSeries returns series labels for a series, only returning selected `by` label names.
2139+ // If `by` is nil, it returns all labels for the series.
2140+ func (dec * Decoder ) prepSeries (b []byte , lbls * labels.Labels , by map [string ]struct {}) (* encoding.Decbuf , uint64 , error ) {
21392141 builder := labelpool .Get ()
21402142 defer labelpool .Put (builder )
21412143
2142- if chks != nil {
2143- * chks = (* chks )[:0 ]
2144- }
2145-
21462144 d := encoding .DecWrap (tsdb_enc.Decbuf {B : b })
21472145
21482146 fprint := d .Be64 ()
@@ -2160,6 +2158,13 @@ func (dec *Decoder) prepSeries(b []byte, lbls *labels.Labels, chks *[]ChunkMeta)
21602158 if err != nil {
21612159 return nil , 0 , errors .Wrap (err , "lookup label name" )
21622160 }
2161+
2162+ if by != nil {
2163+ if _ , ok := by [ln ]; ! ok {
2164+ continue
2165+ }
2166+ }
2167+
21632168 lv , err := dec .LookupSymbol (lvo )
21642169 if err != nil {
21652170 return nil , 0 , errors .Wrap (err , "lookup label value" )
@@ -2171,62 +2176,30 @@ func (dec *Decoder) prepSeries(b []byte, lbls *labels.Labels, chks *[]ChunkMeta)
21712176 // Commit built labels.
21722177 builder .Sort ()
21732178 * lbls = builder .Labels ()
2174-
21752179 return & d , fprint , nil
21762180}
21772181
2178- // prepSeriesBy returns series labels and chunks for a series and only returning selected `by` label names.
2179- // If `by` is empty, it returns all labels for the series.
2180- func (dec * Decoder ) prepSeriesBy (b []byte , lbls * labels.Labels , chks * []ChunkMeta , by map [string ]struct {}) (* encoding.Decbuf , uint64 , error ) {
2181- if by == nil {
2182- return dec .prepSeries (b , lbls , chks )
2183- }
2184-
2185- builder := labelpool .Get ()
2186- defer labelpool .Put (builder )
2187-
2188- if chks != nil {
2189- * chks = (* chks )[:0 ]
2190- }
2191-
2182+ // skipSeriesLabels reads past the label section in buffer b, ready to read chunks after that.
2183+ func (dec * Decoder ) skipSeriesLabels (b []byte ) (* encoding.Decbuf , uint64 , error ) {
21922184 d := encoding .DecWrap (tsdb_enc.Decbuf {B : b })
21932185
21942186 fprint := d .Be64 ()
21952187 k := d .Uvarint ()
21962188
2197- for i := 0 ; i < k ; i ++ {
2198- lno := uint32 ( d .Uvarint () )
2199- lvo := uint32 ( d .Uvarint () )
2189+ for range k {
2190+ _ = d .Uvarint ()
2191+ _ = d .Uvarint ()
22002192
22012193 if d .Err () != nil {
22022194 return nil , 0 , errors .Wrap (d .Err (), "read series label offsets" )
22032195 }
2204- // todo(cyriltovena): we could cache this by user requests spanning multiple prepSeries calls.
2205- ln , err := dec .LookupSymbol (lno )
2206- if err != nil {
2207- return nil , 0 , errors .Wrap (err , "lookup label name" )
2208- }
2209- if _ , ok := by [ln ]; ! ok {
2210- continue
2211- }
2212-
2213- lv , err := dec .LookupSymbol (lvo )
2214- if err != nil {
2215- return nil , 0 , errors .Wrap (err , "lookup label value" )
2216- }
2217-
2218- builder .Add (ln , lv )
22192196 }
22202197
2221- // Commit built labels.
2222- builder .Sort ()
2223- * lbls = builder .Labels ()
2224-
22252198 return & d , fprint , nil
22262199}
22272200
22282201func (dec * Decoder ) ChunkStats (version int , b []byte , seriesRef storage.SeriesRef , from , through int64 , lbls * labels.Labels , by map [string ]struct {}) (uint64 , ChunkStats , error ) {
2229- d , fp , err := dec .prepSeriesBy (b , lbls , nil , by )
2202+ d , fp , err := dec .prepSeries (b , lbls , by )
22302203 if err != nil {
22312204 return 0 , ChunkStats {}, err
22322205 }
@@ -2368,15 +2341,25 @@ func (dec *Decoder) readChunkStatsPriorV3(d *encoding.Decbuf, seriesRef storage.
23682341 return res , nil
23692342}
23702343
2371- // Series decodes a series entry from the given byte slice into lset and chks.
2372- func (dec * Decoder ) Series (version int , b []byte , seriesRef storage.SeriesRef , from int64 , through int64 , lbls * labels.Labels , chks * []ChunkMeta ) (uint64 , error ) {
2373- d , fprint , err := dec .prepSeries (b , lbls , chks )
2344+ // Series decodes a series entry from the given byte slice into lbls and chks.
2345+ // lbls can be nil, indicating the caller only wants the chunks.
2346+ func (dec * Decoder ) Series (version int , b []byte , seriesRef storage.SeriesRef , from int64 , through int64 , lbls * labels.Labels , chks * []ChunkMeta ) (fprint uint64 , err error ) {
2347+ var d * encoding.Decbuf
2348+ if lbls == nil {
2349+ d , fprint , err = dec .skipSeriesLabels (b )
2350+ } else {
2351+ d , fprint , err = dec .prepSeries (b , lbls , nil )
2352+ }
23742353 if err != nil {
23752354 return 0 , err
23762355 }
23772356
2357+ * chks = (* chks )[:0 ]
23782358 // read chunks based on fmt
23792359 if err := dec .readChunks (version , d , seriesRef , from , through , chks ); err != nil {
2360+ if lbls == nil {
2361+ return 0 , errors .Wrapf (err , "series footprint %x" , fprint )
2362+ }
23802363 return 0 , errors .Wrapf (err , "series %s" , lbls .String ())
23812364 }
23822365 return fprint , nil
0 commit comments