Factor out section writer

This commit is contained in:
Fabian Reinartz 2016-12-09 22:36:31 +01:00
parent 0b77a3dafc
commit 4eba874b04
1 changed files with 44 additions and 59 deletions

View File

@ -193,6 +193,24 @@ func (w *indexWriter) write(wr io.Writer, b []byte) error {
return err
}
// section writes a CRC32 checksummed section of length l and guarded by flag.
func (w *indexWriter) section(l uint32, flag byte, f func(w io.Writer) error) error {
h := crc32.NewIEEE()
wr := io.MultiWriter(h, w.w)
if err := w.write(wr, ((*[4]byte)(unsafe.Pointer(&l)))[:]); err != nil {
return err
}
if err := w.write(wr, []byte{flagStd}); err != nil {
return err
}
if err := f(wr); err != nil {
return err
}
return w.write(w.w, h.Sum(nil))
}
func (w *indexWriter) writeMeta() error {
meta := &meta{magic: MagicSeries, flag: flagStd}
metab := ((*[metaSize]byte)(unsafe.Pointer(meta)))[:]
@ -245,22 +263,11 @@ func (w *indexWriter) writeSymbols() error {
b = append(b, s...)
}
h := crc32.NewIEEE()
wr := io.MultiWriter(h, w.w)
l := uint32(len(b) + 1)
l := len(b) + 1
if err := w.write(wr, ((*[4]byte)(unsafe.Pointer(&l)))[:]); err != nil {
return err
}
if err := w.write(wr, []byte{flagStd}); err != nil {
return err
}
if err := w.write(wr, b); err != nil {
return err
}
return w.write(w.w, h.Sum(nil))
return w.section(l, flagStd, func(wr io.Writer) error {
return w.write(wr, b)
})
}
func (w *indexWriter) writeSeries() error {
@ -279,18 +286,11 @@ func (w *indexWriter) writeSeries() error {
}
}
h := crc32.NewIEEE()
wr := io.MultiWriter(h, w.w)
l := uint32(len(b) + 1)
l := len(b) + 1
if err := w.write(wr, ((*[4]byte)(unsafe.Pointer(&l)))[:]); err != nil {
return err
}
if err := w.write(wr, b); err != nil {
return err
}
return w.write(w.w, h.Sum(nil))
return w.section(l, flagStd, func(wr io.Writer) error {
return w.write(wr, b)
})
}
func (w *indexWriter) WriteLabelIndex(names []string, values []string) error {
@ -299,23 +299,14 @@ func (w *indexWriter) WriteLabelIndex(names []string, values []string) error {
}
sort.Strings(values)
h := crc32.NewIEEE()
wr := io.MultiWriter(h, w.w)
w.labelIndexes = append(w.labelIndexes, hashEntry{
name: names[0],
offset: uint32(w.n),
})
l := 1 + len(values)*4
if err := w.write(wr, ((*[4]byte)(unsafe.Pointer(&l)))[:]); err != nil {
return err
}
if err := w.write(wr, []byte{flagStd}); err != nil {
return err
}
l := uint32(1 + len(values)*4)
return w.section(l, flagStd, func(wr io.Writer) error {
for _, v := range values {
o := w.symbols[v]
b := ((*[4]byte)(unsafe.Pointer(&o)))[:]
@ -324,8 +315,8 @@ func (w *indexWriter) WriteLabelIndex(names []string, values []string) error {
return err
}
}
return w.write(w.w, h.Sum(nil))
return nil
})
}
func (w *indexWriter) WritePostings(name, value string, it Iterator) error {
@ -346,13 +337,7 @@ const hashEntrySize = uint32(unsafe.Sizeof(hashEntry{}))
func (w *indexWriter) finalize() error {
l := 1 + uint32(len(w.labelIndexes))*hashEntrySize
if err := w.write(w.w, ((*[4]byte)(unsafe.Pointer(&l)))[:]); err != nil {
return err
}
if err := w.write(w.w, []byte{flagStd}); err != nil {
return err
}
return w.section(l, flagStd, func(wr io.Writer) error {
for _, e := range w.labelIndexes {
b := ((*[hashEntrySize]byte)(unsafe.Pointer(&e)))[:]
@ -360,8 +345,8 @@ func (w *indexWriter) finalize() error {
return nil
}
}
return nil
})
}
func (w *indexWriter) Close() error {