Skip to content

Commit

Permalink
refactor: marshal/unmashal on index
Browse files Browse the repository at this point in the history
  • Loading branch information
ifplusor committed May 25, 2022
1 parent cafef5e commit 57d988c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
8 changes: 2 additions & 6 deletions internal/store/segment/block/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,7 @@ func (b *FileBlock) persistIndex(ctx context.Context) error {
buf := make([]byte, length)
for i, index := range b.indexes {
off := length - (i+1)*v1IndexLength
binary.BigEndian.PutUint64(buf[off:off+8], uint64(index.offset))
binary.BigEndian.PutUint32(buf[off+8:off+12], uint32(index.length))
_, _ = index.MarshalTo(buf[off : off+v1IndexLength])
}

if _, err := b.f.WriteAt(buf, b.cap-int64(length)); err != nil {
Expand Down Expand Up @@ -468,10 +467,7 @@ func (b *FileBlock) loadIndexFromFile() error {
b.indexes = make([]index, num)
for i := range b.indexes {
off := length - (i+1)*v1IndexLength
b.indexes[i] = index{
offset: int64(binary.BigEndian.Uint64(data[off : off+8])),
length: int32(binary.BigEndian.Uint32(data[off+8 : off+12])),
}
b.indexes[i], _ = unmashalIndex(data[off : off+v1IndexLength])
}

return nil
Expand Down
27 changes: 27 additions & 0 deletions internal/store/segment/block/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@

package block

import (
"bytes"
"encoding/binary"
)

const (
v1IndexLength = 8 + 4
)
Expand All @@ -22,3 +27,25 @@ type index struct {
offset int64
length int32
}

func (i index) MarshalTo(data []byte) (int, error) {
if len(data) < v1IndexLength {
// TODO(james.yin): correct error.
return 0, bytes.ErrTooLarge
}
binary.BigEndian.PutUint64(data[0:8], uint64(i.offset))
binary.BigEndian.PutUint32(data[8:12], uint32(i.length))
return v1IndexLength, nil
}

func unmashalIndex(data []byte) (index, error) {
if len(data) < v1IndexLength {
// TODO(james.yin): correct error.
return index{}, bytes.ErrTooLarge
}
i := index{
offset: int64(binary.BigEndian.Uint64(data[0:8])),
length: int32(binary.BigEndian.Uint32(data[8:12])),
}
return i, nil
}

0 comments on commit 57d988c

Please sign in to comment.