@@ -1484,31 +1484,35 @@ func (r *Reader) SortedLabelValues(name string, matchers ...*labels.Matcher) ([]
14841484// LabelValues returns value tuples that exist for the given label name.
14851485// It is not safe to use the return value beyond the lifetime of the byte slice
14861486// passed into the Reader.
1487- // TODO(replay): Support filtering by matchers
14881487func (r * Reader ) LabelValues (name string , matchers ... * labels.Matcher ) ([]string , error ) {
1489- if len (matchers ) > 0 {
1490- return nil , errors .Errorf ("matchers parameter is not implemented: %+v" , matchers )
1491- }
1492-
14931488 if r .version == FormatV1 {
14941489 e , ok := r .postingsV1 [name ]
14951490 if ! ok {
14961491 return nil , nil
14971492 }
14981493 values := make ([]string , 0 , len (e ))
14991494 for k := range e {
1500- values = append (values , k )
1495+ isMatch := true
1496+ for _ , m := range matchers {
1497+ if m .Name == name && ! m .Matches (k ) {
1498+ isMatch = false
1499+ break
1500+ }
1501+ }
1502+
1503+ if isMatch {
1504+ values = append (values , k )
1505+ }
15011506 }
15021507 return values , nil
15031508
15041509 }
1505- e , ok := r .postings [name ]
1506- if ! ok {
1507- return nil , nil
1508- }
1510+
1511+ e := r .postings [name ]
15091512 if len (e ) == 0 {
15101513 return nil , nil
15111514 }
1515+
15121516 values := make ([]string , 0 , len (e )* symbolFactor )
15131517
15141518 d := encoding .NewDecbufAt (r .b , int (r .toc .PostingsTable ), nil )
@@ -1528,7 +1532,19 @@ func (r *Reader) LabelValues(name string, matchers ...*labels.Matcher) ([]string
15281532 d .Skip (skip )
15291533 }
15301534 s := yoloString (d .UvarintBytes ()) // Label value.
1531- values = append (values , s )
1535+
1536+ isMatch := true
1537+ // Try to exclude via matchers for the label name
1538+ for _ , m := range matchers {
1539+ if m .Name == name && ! m .Matches (s ) {
1540+ isMatch = false
1541+ break
1542+ }
1543+ }
1544+
1545+ if isMatch {
1546+ values = append (values , s )
1547+ }
15321548 if s == lastVal {
15331549 break
15341550 }
@@ -1647,8 +1663,8 @@ func (r *Reader) Postings(name string, values ...string) (Postings, error) {
16471663 return Merge (res ... ), nil
16481664 }
16491665
1650- e , ok := r .postings [name ]
1651- if ! ok {
1666+ e := r .postings [name ]
1667+ if len ( e ) == 0 {
16521668 return EmptyPostings (), nil
16531669 }
16541670
@@ -1725,6 +1741,71 @@ func (r *Reader) Postings(name string, values ...string) (Postings, error) {
17251741 return Merge (res ... ), nil
17261742}
17271743
1744+ func (r * Reader ) PostingsWithLabel (name string ) (Postings , error ) {
1745+ if r .version == FormatV1 {
1746+ e := r .postingsV1 [name ]
1747+ if len (e ) == 0 {
1748+ return EmptyPostings (), nil
1749+ }
1750+
1751+ var res []Postings
1752+ for _ , off := range e {
1753+ // Read from the postings table.
1754+ d := encoding .NewDecbufAt (r .b , int (off ), castagnoliTable )
1755+ _ , p , err := r .dec .Postings (d .Get ())
1756+ if err != nil {
1757+ return nil , errors .Wrap (err , "decode postings" )
1758+ }
1759+ res = append (res , p )
1760+ }
1761+ return Merge (res ... ), nil
1762+ }
1763+
1764+ e := r .postings [name ]
1765+ if len (e ) == 0 {
1766+ return EmptyPostings (), nil
1767+ }
1768+
1769+ d := encoding .NewDecbufAt (r .b , int (r .toc .PostingsTable ), nil )
1770+ // Skip to start
1771+ d .Skip (e [0 ].off )
1772+ lastVal := e [len (e )- 1 ].value
1773+
1774+ skip := 0
1775+ var res []Postings
1776+ for d .Err () == nil {
1777+ if skip == 0 {
1778+ // These are always the same number of bytes,
1779+ // and it's faster to skip than to parse.
1780+ skip = d .Len ()
1781+ d .Uvarint () // Keycount.
1782+ d .UvarintBytes () // Label name.
1783+ skip -= d .Len ()
1784+ } else {
1785+ d .Skip (skip )
1786+ }
1787+ v := yoloString (d .UvarintBytes ()) // Label value.
1788+
1789+ postingsOff := d .Uvarint64 ()
1790+ // Read from the postings table
1791+ d2 := encoding .NewDecbufAt (r .b , int (postingsOff ), castagnoliTable )
1792+ _ , p , err := r .dec .Postings (d2 .Get ())
1793+ if err != nil {
1794+ return nil , errors .Wrap (err , "decode postings" )
1795+ }
1796+ res = append (res , p )
1797+
1798+ if v == lastVal {
1799+ break
1800+ }
1801+ }
1802+ if d .Err () != nil {
1803+ return nil , errors .Wrap (d .Err (), "get postings offset entry" )
1804+ }
1805+
1806+ return Merge (res ... ), nil
1807+ }
1808+
17281809// SortedPostings returns the given postings list reordered so that the backing series
17291810// are sorted.
17301811func (r * Reader ) SortedPostings (p Postings ) Postings {
0 commit comments