-
Notifications
You must be signed in to change notification settings - Fork 0
/
xlog_record_block_image_header.go
83 lines (68 loc) · 2.77 KB
/
xlog_record_block_image_header.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package walparser
import (
"fmt"
"github.com/pkg/errors"
"github.com/tinsane/tracelog"
)
const (
BkpImageHasHole uint8 = 0x01
BkpImageIsCompressed uint8 = 0x02
BkpImageApply uint8 = 0x04
)
type InconsistentBlockImageHoleStateError struct {
error
}
func NewInconsistentBlockImageHoleStateError(holeOffset uint16, holeLength uint16, imageLength uint16, hasHole bool) InconsistentBlockImageHoleStateError {
return InconsistentBlockImageHoleStateError{errors.Errorf(
"block image hole state is inconsistent: holeOffset is: %v, holeLength is: %v, imageLength is: %v, while hasHole is: %v",
holeOffset, holeLength, imageLength, hasHole)}
}
func (err InconsistentBlockImageHoleStateError) Error() string {
return fmt.Sprintf(tracelog.GetErrorFormatter(), err.error)
}
type InconsistentBlockImageLengthError struct {
error
}
func NewInconsistentBlockImageLengthError(hasHole bool, isCompressed bool, length uint16) InconsistentBlockImageLengthError {
return InconsistentBlockImageLengthError{errors.Errorf("block image has invalid state: hasHole: %v, isCompressed: %v, imageLength: %v", hasHole, isCompressed, length)}
}
func (err InconsistentBlockImageLengthError) Error() string {
return fmt.Sprintf(tracelog.GetErrorFormatter(), err.error)
}
type XLogRecordBlockImageHeader struct {
ImageLength uint16
HoleOffset uint16
HoleLength uint16
Info uint8
}
func (imageHeader *XLogRecordBlockImageHeader) HasHole() bool {
return (imageHeader.Info & BkpImageHasHole) != 0
}
func (imageHeader *XLogRecordBlockImageHeader) IsCompressed() bool {
return (imageHeader.Info & BkpImageIsCompressed) != 0
}
func (imageHeader *XLogRecordBlockImageHeader) ApplyImage() bool {
return (imageHeader.Info & BkpImageApply) != 0
}
func (imageHeader *XLogRecordBlockImageHeader) checkHoleStateConsistency() error {
if (imageHeader.HasHole() && (imageHeader.HoleOffset == 0 || imageHeader.HoleLength == 0 || imageHeader.ImageLength == BlockSize)) ||
(!imageHeader.HasHole() && (imageHeader.HoleOffset != 0 || imageHeader.HoleLength != 0)) {
return NewInconsistentBlockImageHoleStateError(imageHeader.HoleOffset, imageHeader.HoleLength,
imageHeader.ImageLength, imageHeader.HasHole())
}
return nil
}
func (imageHeader *XLogRecordBlockImageHeader) checkLengthConsistency() error {
if (imageHeader.IsCompressed() && imageHeader.ImageLength == BlockSize) ||
(!imageHeader.HasHole() && !imageHeader.IsCompressed() && imageHeader.ImageLength != BlockSize) {
return NewInconsistentBlockImageLengthError(imageHeader.HasHole(), imageHeader.IsCompressed(), imageHeader.ImageLength)
}
return nil
}
func (imageHeader *XLogRecordBlockImageHeader) checkConsistency() error {
err := imageHeader.checkHoleStateConsistency()
if err != nil {
return err
}
return imageHeader.checkLengthConsistency()
}