-
Notifications
You must be signed in to change notification settings - Fork 0
/
record.go
111 lines (90 loc) · 2.12 KB
/
record.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package wal
import (
"bytes"
"fmt"
)
const (
recordSizeLength = 8
recordChecksumLength = 8
recordMetadataLength = recordSizeLength + recordChecksumLength
)
var (
errInvalidChecksum = fmt.Errorf("invalid checksum")
errInvalidMetadata = fmt.Errorf("invalid metadata")
)
type record struct {
// size is the total length of the payload
size uint64
// checksum is a checksum of the payload
checksum uint64
// payload is the payload of the block
payload []byte
}
func (r *record) blockC(_blockSize int64) int64 {
c := (int64(len(r.payload)) + recordMetadataLength) / _blockSize
if int64(len(r.payload))+recordMetadataLength%_blockSize > 0 {
c++
}
return c
}
func (r *record) isMetaValid() bool {
if r.checksum == 0 || r.size == 0 {
return false
}
return true
}
func (r *record) isValid() bool {
if !r.isMetaValid() {
return false
}
if uint64(len(r.payload)) != r.size {
return false
}
return true
}
func (r *record) marshal() (data []byte, err error) {
buff := bytes.Buffer{}
// size
_, err = buff.Write(encodeUint64(uint64(len(r.payload))))
if err != nil {
return nil, fmt.Errorf("can't write payload size to buffer: %w", err)
}
// checksum
_, err = buff.Write(encodeUint64(sum(r.payload)))
if err != nil {
return nil, fmt.Errorf("can't write payload checksum to buffer: %w", err)
}
// payload
_, err = buff.Write(r.payload)
if err != nil {
return nil, fmt.Errorf("can't write payload to buffer: %w", err)
}
return buff.Bytes(), nil
}
func (r *record) unmarshalMetadata(_data []byte) error {
if len(_data) < recordMetadataLength {
return fmt.Errorf("not enough bytes")
}
// size
r.size = decodeUint64(_data[:recordSizeLength])
_data = _data[recordSizeLength:]
// checksum
r.checksum = decodeUint64(_data[:recordChecksumLength])
if !r.isMetaValid() {
return errInvalidMetadata
}
return nil
}
func (r *record) unmarshalPayload(_data []byte) error {
if !r.isMetaValid() {
return errInvalidMetadata
}
if uint64(len(_data)) < r.size {
return fmt.Errorf("not enough bytes")
}
r.payload = _data[:r.size]
if !check(r.payload, r.checksum) {
return errInvalidChecksum
}
return nil
}