From 791a2dda4d843dbf74a6c10a3f9afb61acc741b7 Mon Sep 17 00:00:00 2001 From: Nipun Talukdar Date: Sat, 4 Nov 2017 00:46:19 +0530 Subject: [PATCH] Fixed a problem of adding padding of 4 zero bytes in some cases (#194) * Fixed a problem of adding padding of 4 zero bytes in some cases * Incorporated review comments --- index.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/index.go b/index.go index f976de695..258db74d2 100644 --- a/index.go +++ b/index.go @@ -35,6 +35,8 @@ const ( MagicIndex = 0xBAAAD700 indexFormatV1 = 1 + + size_unit = 4 ) const indexFilename = "index" @@ -203,12 +205,13 @@ func (w *indexWriter) write(bufs ...[]byte) error { return nil } -// addPadding adds zero byte padding until the file size is a multiple of n. -func (w *indexWriter) addPadding(n int) error { - p := n - (int(w.pos) % n) +// addPadding adds zero byte padding until the file size is a multiple size_unit. +func (w *indexWriter) addPadding() error { + p := w.pos % size_unit if p == 0 { return nil } + p = size_unit - p return errors.Wrap(w.write(make([]byte, p)), "add padding") } @@ -373,7 +376,7 @@ func (w *indexWriter) WriteLabelIndex(names []string, values []string) error { sort.Sort(valt) // Align beginning to 4 bytes for more efficient index list scans. - if err := w.addPadding(4); err != nil { + if err := w.addPadding(); err != nil { return err } @@ -446,7 +449,7 @@ func (w *indexWriter) WritePostings(name, value string, it Postings) error { } // Align beginning to 4 bytes for more efficient postings list scans. - if err := w.addPadding(4); err != nil { + if err := w.addPadding(); err != nil { return err }