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
This commit is contained in:
parent
b1df857819
commit
791a2dda4d
13
index.go
13
index.go
|
@ -35,6 +35,8 @@ const (
|
||||||
MagicIndex = 0xBAAAD700
|
MagicIndex = 0xBAAAD700
|
||||||
|
|
||||||
indexFormatV1 = 1
|
indexFormatV1 = 1
|
||||||
|
|
||||||
|
size_unit = 4
|
||||||
)
|
)
|
||||||
|
|
||||||
const indexFilename = "index"
|
const indexFilename = "index"
|
||||||
|
@ -203,12 +205,13 @@ func (w *indexWriter) write(bufs ...[]byte) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// addPadding adds zero byte padding until the file size is a multiple of n.
|
// addPadding adds zero byte padding until the file size is a multiple size_unit.
|
||||||
func (w *indexWriter) addPadding(n int) error {
|
func (w *indexWriter) addPadding() error {
|
||||||
p := n - (int(w.pos) % n)
|
p := w.pos % size_unit
|
||||||
if p == 0 {
|
if p == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
p = size_unit - p
|
||||||
return errors.Wrap(w.write(make([]byte, p)), "add padding")
|
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)
|
sort.Sort(valt)
|
||||||
|
|
||||||
// Align beginning to 4 bytes for more efficient index list scans.
|
// 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
|
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.
|
// 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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue