-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchunk.go
90 lines (74 loc) · 2.24 KB
/
chunk.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
package wal
import (
"bytes"
"encoding/base64"
"encoding/binary"
"strconv"
"github.com/pkg/errors"
)
var (
chunkOffsetSize = 8
chunkSeparator = byte(':')
)
type chunk []byte
func newChunk(data []byte) *chunk {
return newChunkOffset(data, NewOffset())
}
func newChunkOffset(data []byte, o Offset) *chunk {
// Create a chunk large enough to hold its offset + len(data).
c := make(chunk, chunkOffsetSize+len(data))
binary.LittleEndian.PutUint64(c[:chunkOffsetSize], uint64(o))
copy(c[chunkOffsetSize:], data)
return &c
}
// MarshalText implements the encoding.TextMarshaler interface, and is
// primarily used for encoding a data chunk before it is written to
// persistent storage.
func (c chunk) MarshalText() ([]byte, error) {
// Convert the chunk's offset to a string, then write it out as-is,
// followed by a separator ":".
offset := []byte(strconv.FormatInt(int64(c.Offset()), 10))
offset = append(offset, chunkSeparator)
// Encode the data.
enc := base64.RawStdEncoding
data := make([]byte, enc.EncodedLen(len(c)-chunkOffsetSize))
enc.Encode(data, c[chunkOffsetSize:])
return append(offset, data...), nil
}
// UnmarshalText implements the encoding.TextUnmarshaler interface, and is
// primarily used for decoding a data chunk that has been read in from
// persistent storage.
func (c *chunk) UnmarshalText(p []byte) error {
sep := bytes.Index(p, []byte{chunkSeparator})
if sep == -1 {
return errors.New("no chunk separator")
}
enc := base64.RawStdEncoding
size := chunkOffsetSize + enc.DecodedLen(len(p[sep+1:]))
*c = append([]byte{}, make([]byte, size)...)
// Unmarshal the offset.
off, err := strconv.ParseInt(string(p[:sep]), 10, 64)
if err != nil {
return errors.Wrap(err, "parse offset")
}
binary.LittleEndian.PutUint64((*c)[:chunkOffsetSize], uint64(off))
// Decode the rest of the data.
if _, err = enc.Decode((*c)[chunkOffsetSize:], p[sep+1:]); err != nil {
return errors.Wrap(err, "unmarshal text")
}
return nil
}
func (c chunk) String() string {
p, err := c.MarshalText()
if err != nil {
return ""
}
return string(p)
}
// Offset returns the chunk's offset.
func (c chunk) Offset() Offset {
return Offset(binary.LittleEndian.Uint64(c[:chunkOffsetSize]))
}
func (c chunk) Data() []byte {
return c[chunkOffsetSize:]
}