add tombstoneFormatVersionSize & tombstonesCRCSize constants (#9625)

Signed-off-by: chenlujjj <953546398@qq.com>
This commit is contained in:
chenlujjj 2021-11-01 18:35:19 +08:00 committed by GitHub
parent 64d9b41998
commit 660329d5b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 6 deletions

View File

@ -39,8 +39,10 @@ const (
// MagicTombstone is 4 bytes at the head of a tombstone file.
MagicTombstone = 0x0130BA30
tombstoneFormatV1 = 1
tombstonesHeaderSize = 5
tombstoneFormatV1 = 1
tombstoneFormatVersionSize = 1
tombstonesHeaderSize = 5
tombstonesCRCSize = 4
)
// The table gets initialized with sync.Once but may still cause a race
@ -110,7 +112,7 @@ func WriteFile(logger log.Logger, dir string, tr Reader) (int64, error) {
}
// Ignore first byte which is the format type. We do this for compatibility.
if _, err := hash.Write(bytes[1:]); err != nil {
if _, err := hash.Write(bytes[tombstoneFormatVersionSize:]); err != nil {
return 0, errors.Wrap(err, "calculating hash for tombstones")
}
@ -198,7 +200,7 @@ func ReadTombstones(dir string) (Reader, int64, error) {
return nil, 0, errors.Wrap(encoding.ErrInvalidSize, "tombstones header")
}
d := &encoding.Decbuf{B: b[:len(b)-4]} // 4 for the checksum.
d := &encoding.Decbuf{B: b[:len(b)-tombstonesCRCSize]}
if mg := d.Be32(); mg != MagicTombstone {
return nil, 0, fmt.Errorf("invalid magic number %x", mg)
}
@ -206,10 +208,10 @@ func ReadTombstones(dir string) (Reader, int64, error) {
// Verify checksum.
hash := newCRC32()
// Ignore first byte which is the format type.
if _, err := hash.Write(d.Get()[1:]); err != nil {
if _, err := hash.Write(d.Get()[tombstoneFormatVersionSize:]); err != nil {
return nil, 0, errors.Wrap(err, "write to hash")
}
if binary.BigEndian.Uint32(b[len(b)-4:]) != hash.Sum32() {
if binary.BigEndian.Uint32(b[len(b)-tombstonesCRCSize:]) != hash.Sum32() {
return nil, 0, errors.New("checksum did not match")
}