Breakdown generic writeOffsetTable (#643)

* Breakdown generic writeOffsetTable into writeLabelIndexesOffsetTable and writePostingsOffsetTable

Signed-off-by: Ganesh Vernekar <cs15btech11018@iith.ac.in>

* Add CHANGELOG entry

Signed-off-by: Ganesh Vernekar <cs15btech11018@iith.ac.in>

* Remove CHANGELOG entry

Signed-off-by: Ganesh Vernekar <cs15btech11018@iith.ac.in>
This commit is contained in:
Ganesh Vernekar 2019-07-10 12:01:44 +05:30 committed by GitHub
parent b5b8c9200c
commit 0fdd93b0b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -125,8 +125,8 @@ type Writer struct {
symbols map[string]uint32 // symbol offsets symbols map[string]uint32 // symbol offsets
seriesOffsets map[uint64]uint64 // offsets of series seriesOffsets map[uint64]uint64 // offsets of series
labelIndexes []hashEntry // label index offsets labelIndexes []labelIndexHashEntry // label index offsets
postings []hashEntry // postings lists offsets postings []postingsHashEntry // postings lists offsets
// Hold last series to validate that clients insert new series in order. // Hold last series to validate that clients insert new series in order.
lastSeries labels.Labels lastSeries labels.Labels
@ -271,11 +271,11 @@ func (w *Writer) ensureStage(s indexWriterStage) error {
case idxStageDone: case idxStageDone:
w.toc.LabelIndicesTable = w.pos w.toc.LabelIndicesTable = w.pos
if err := w.writeOffsetTable(w.labelIndexes); err != nil { if err := w.writeLabelIndexesOffsetTable(); err != nil {
return err return err
} }
w.toc.PostingsTable = w.pos w.toc.PostingsTable = w.pos
if err := w.writeOffsetTable(w.postings); err != nil { if err := w.writePostingsOffsetTable(); err != nil {
return err return err
} }
if err := w.writeTOC(); err != nil { if err := w.writeTOC(); err != nil {
@ -420,7 +420,7 @@ func (w *Writer) WriteLabelIndex(names []string, values []string) error {
return err return err
} }
w.labelIndexes = append(w.labelIndexes, hashEntry{ w.labelIndexes = append(w.labelIndexes, labelIndexHashEntry{
keys: names, keys: names,
offset: w.pos, offset: w.pos,
}) })
@ -447,12 +447,12 @@ func (w *Writer) WriteLabelIndex(names []string, values []string) error {
return errors.Wrap(err, "write label index") return errors.Wrap(err, "write label index")
} }
// writeOffsetTable writes a sequence of readable hash entries. // writeLabelIndexesOffsetTable writes the label indices offset table.
func (w *Writer) writeOffsetTable(entries []hashEntry) error { func (w *Writer) writeLabelIndexesOffsetTable() error {
w.buf2.Reset() w.buf2.Reset()
w.buf2.PutBE32int(len(entries)) w.buf2.PutBE32int(len(w.labelIndexes))
for _, e := range entries { for _, e := range w.labelIndexes {
w.buf2.PutUvarint(len(e.keys)) w.buf2.PutUvarint(len(e.keys))
for _, k := range e.keys { for _, k := range e.keys {
w.buf2.PutUvarintStr(k) w.buf2.PutUvarintStr(k)
@ -467,6 +467,25 @@ func (w *Writer) writeOffsetTable(entries []hashEntry) error {
return w.write(w.buf1.Get(), w.buf2.Get()) return w.write(w.buf1.Get(), w.buf2.Get())
} }
// writePostingsOffsetTable writes the postings offset table.
func (w *Writer) writePostingsOffsetTable() error {
w.buf2.Reset()
w.buf2.PutBE32int(len(w.postings))
for _, e := range w.postings {
w.buf2.PutUvarint(2)
w.buf2.PutUvarintStr(e.name)
w.buf2.PutUvarintStr(e.value)
w.buf2.PutUvarint64(e.offset)
}
w.buf1.Reset()
w.buf1.PutBE32int(w.buf2.Len())
w.buf2.PutHash(w.crc32)
return w.write(w.buf1.Get(), w.buf2.Get())
}
const indexTOCLen = 6*8 + 4 const indexTOCLen = 6*8 + 4
func (w *Writer) writeTOC() error { func (w *Writer) writeTOC() error {
@ -494,8 +513,9 @@ func (w *Writer) WritePostings(name, value string, it Postings) error {
return err return err
} }
w.postings = append(w.postings, hashEntry{ w.postings = append(w.postings, postingsHashEntry{
keys: []string{name, value}, name: name,
value: value,
offset: w.pos, offset: w.pos,
}) })
@ -542,11 +562,16 @@ func (s uint32slice) Len() int { return len(s) }
func (s uint32slice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } func (s uint32slice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s uint32slice) Less(i, j int) bool { return s[i] < s[j] } func (s uint32slice) Less(i, j int) bool { return s[i] < s[j] }
type hashEntry struct { type labelIndexHashEntry struct {
keys []string keys []string
offset uint64 offset uint64
} }
type postingsHashEntry struct {
name, value string
offset uint64
}
func (w *Writer) Close() error { func (w *Writer) Close() error {
if err := w.ensureStage(idxStageDone); err != nil { if err := w.ensureStage(idxStageDone); err != nil {
return err return err