@@ -42,7 +42,7 @@ type PostingOffsetTable interface {
4242 // LabelNames returns a sorted list of all label names in this table.
4343 LabelNames () ([]string , error )
4444
45- NewSparsePostingOffsetTable () (table * indexheaderpb.PostingOffsetTable )
45+ ToSparsePostingOffsetTable () (table * indexheaderpb.PostingOffsetTable )
4646
4747 // PostingOffsetInMemSampling returns the inverse of the fraction of postings held in memory. A lower value indicates
4848 // postings are sample more frequently.
@@ -58,60 +58,13 @@ type PostingListOffset struct {
5858 Off index.Range
5959}
6060
61- type PostingOffsetTableV1 struct {
62- // For the v1 format, labelname -> labelvalue -> offset.
63- postings map [string ]map [string ]index.Range
64- }
65-
6661func NewPostingOffsetTable (factory * streamencoding.DecbufFactory , tableOffset int , indexVersion int , indexLastPostingListEndBound uint64 , postingOffsetsInMemSampling int , doChecksum bool ) (PostingOffsetTable , error ) {
6762 switch indexVersion {
68- case index .FormatV1 :
69- return newV1PostingOffsetTable (factory , tableOffset , indexLastPostingListEndBound )
7063 case index .FormatV2 :
7164 return newV2PostingOffsetTable (factory , tableOffset , indexLastPostingListEndBound , postingOffsetsInMemSampling , doChecksum )
7265 }
7366
74- return nil , fmt .Errorf ("unknown index version %v" , indexVersion )
75- }
76-
77- func newV1PostingOffsetTable (factory * streamencoding.DecbufFactory , tableOffset int , indexLastPostingListEndBound uint64 ) (* PostingOffsetTableV1 , error ) {
78- t := PostingOffsetTableV1 {
79- postings : map [string ]map [string ]index.Range {},
80- }
81-
82- // Earlier V1 formats don't have a sorted postings offset table, so
83- // load the whole offset table into memory.
84- var lastKey string
85- var lastValue string
86- var prevRng index.Range
87-
88- if err := readOffsetTable (factory , tableOffset , func (key string , value string , off uint64 ) error {
89- if len (t .postings ) > 0 {
90- prevRng .End = int64 (off - crc32 .Size )
91- t.postings [lastKey ][lastValue ] = prevRng
92- }
93-
94- if _ , ok := t .postings [key ]; ! ok {
95- t .postings [key ] = map [string ]index.Range {}
96- }
97-
98- lastKey = key
99- lastValue = value
100- prevRng = index.Range {Start : int64 (off + postingLengthFieldSize )}
101- return nil
102- }); err != nil {
103- return nil , errors .Wrap (err , "read postings table" )
104- }
105-
106- if len (t .postings ) > 0 {
107- // In case lastValOffset is unknown as we don't have next posting anymore. Guess from the index table of contents.
108- // The last posting list ends before the label offset table.
109- // In worst case we will overfetch a few bytes.
110- prevRng .End = int64 (indexLastPostingListEndBound ) - crc32 .Size
111- t.postings [lastKey ][lastValue ] = prevRng
112- }
113-
114- return & t , nil
67+ return nil , fmt .Errorf ("unknown or unsupported index version %v" , indexVersion )
11568}
11669
11770func newV2PostingOffsetTable (factory * streamencoding.DecbufFactory , tableOffset int , indexLastPostingListEndBound uint64 , postingOffsetsInMemSampling int , doChecksum bool ) (table * PostingOffsetTableV2 , err error ) {
@@ -278,95 +231,6 @@ func NewPostingOffsetTableFromSparseHeader(factory *streamencoding.DecbufFactory
278231 return & t , err
279232}
280233
281- // readOffsetTable reads an offset table and at the given position calls f for each
282- // found entry. If f returns an error it stops decoding and returns the received error.
283- func readOffsetTable (factory * streamencoding.DecbufFactory , tableOffset int , f func (string , string , uint64 ) error ) (err error ) {
284- d := factory .NewDecbufAtChecked (tableOffset , castagnoliTable )
285- defer runutil .CloseWithErrCapture (& err , & d , "read offset table" )
286-
287- cnt := d .Be32 ()
288-
289- for d .Err () == nil && d .Len () > 0 && cnt > 0 {
290- keyCount := d .Uvarint ()
291-
292- // The Postings offset table takes only 2 keys per entry (name and value of label).
293- if keyCount != 2 {
294- return errors .Errorf ("unexpected key length for posting table %d" , keyCount )
295- }
296-
297- key := d .UvarintStr ()
298- value := d .UvarintStr ()
299- o := d .Uvarint64 ()
300- if d .Err () != nil {
301- break
302- }
303- if err := f (key , value , o ); err != nil {
304- return err
305- }
306- cnt --
307- }
308- return d .Err ()
309- }
310-
311- func (t * PostingOffsetTableV1 ) PostingsOffset (name string , value string ) (index.Range , bool , error ) {
312- e , ok := t .postings [name ]
313- if ! ok {
314- return index.Range {}, false , nil
315- }
316- rng , ok := e [value ]
317- if ! ok {
318- return index.Range {}, false , nil
319- }
320- return rng , true , nil
321- }
322-
323- func (t * PostingOffsetTableV1 ) LabelValuesOffsets (ctx context.Context , name , prefix string , filter func (string ) bool ) ([]PostingListOffset , error ) {
324- e , ok := t .postings [name ]
325- if ! ok {
326- return nil , nil
327- }
328- values := make ([]PostingListOffset , 0 , len (e ))
329- count := 1
330- for k , r := range e {
331- if count % CheckContextEveryNIterations == 0 && ctx .Err () != nil {
332- return nil , ctx .Err ()
333- }
334- count ++
335- if strings .HasPrefix (k , prefix ) && (filter == nil || filter (k )) {
336- values = append (values , PostingListOffset {LabelValue : k , Off : r })
337- }
338- }
339- slices .SortFunc (values , func (a , b PostingListOffset ) int {
340- return strings .Compare (a .LabelValue , b .LabelValue )
341- })
342- return values , nil
343- }
344-
345- func (t * PostingOffsetTableV1 ) LabelNames () ([]string , error ) {
346- labelNames := make ([]string , 0 , len (t .postings ))
347- allPostingsKeyName , _ := index .AllPostingsKey ()
348-
349- for name := range t .postings {
350- if name == allPostingsKeyName {
351- continue
352- }
353-
354- labelNames = append (labelNames , name )
355- }
356-
357- slices .Sort (labelNames )
358-
359- return labelNames , nil
360- }
361-
362- func (t * PostingOffsetTableV1 ) PostingOffsetInMemSampling () int {
363- return 0
364- }
365-
366- func (t * PostingOffsetTableV1 ) NewSparsePostingOffsetTable () (table * indexheaderpb.PostingOffsetTable ) {
367- return & indexheaderpb.PostingOffsetTable {}
368- }
369-
370234type PostingOffsetTableV2 struct {
371235 // Map of LabelName to a list of some LabelValues's position in the offset table.
372236 // The first and last values for each name are always present, we keep only 1/postingOffsetsInMemSampling of the rest.
@@ -649,7 +513,7 @@ func (t *PostingOffsetTableV2) PostingOffsetInMemSampling() int {
649513}
650514
651515// NewSparsePostingOffsetTable loads all postings offset table data into a sparse index-header to be persisted to disk
652- func (t * PostingOffsetTableV2 ) NewSparsePostingOffsetTable () (table * indexheaderpb.PostingOffsetTable ) {
516+ func (t * PostingOffsetTableV2 ) ToSparsePostingOffsetTable () (table * indexheaderpb.PostingOffsetTable ) {
653517 sparseHeaders := & indexheaderpb.PostingOffsetTable {
654518 Postings : make (map [string ]* indexheaderpb.PostingValueOffsets , len (t .postings )),
655519 PostingOffsetInMemorySampling : int64 (t .postingOffsetsInMemSampling ),
0 commit comments