-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment.go
276 lines (244 loc) · 6.53 KB
/
segment.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package archivedb
import (
"bytes"
"encoding/binary"
"io"
"os"
"strconv"
"github.com/millken/archivedb/internal/mmap"
"github.com/pkg/errors"
)
const (
SegmentVersion = 1
SegmentMagic = "ArSeG"
SegmentSize uint32 = 1 << 30 // 1GB
SegmentHeaderSize = 6 // magic + version
)
var (
ErrInvalidSegment = errors.New("invalid segment")
ErrInvalidSegmentVersion = errors.New("invalid segment version")
ErrSegmentNotWritable = errors.New("segment not writable")
)
type segmentHeader struct {
Version uint8
}
func newSegmentHeader() segmentHeader {
return segmentHeader{Version: SegmentVersion}
}
// WriteTo writes the header to w.
func (hdr *segmentHeader) WriteTo(w io.Writer) (n int64, err error) {
var buf bytes.Buffer
buf.WriteString(SegmentMagic)
binary.Write(&buf, binary.BigEndian, hdr.Version)
return buf.WriteTo(w)
}
func decodeSegmentHeader(b []byte) (hdr segmentHeader, err error) {
if len(b) < SegmentHeaderSize {
return hdr, errors.Wrap(ErrInvalidSegment, "invalid segment header")
}
magic := b[0:len(SegmentMagic)]
if !bytes.Equal(magic, []byte(SegmentMagic)) {
return hdr, errors.Wrap(ErrInvalidSegment, "invalid magic")
}
hdr.Version = b[len(SegmentMagic)]
return hdr, nil
}
type segment struct {
mmap *mmap.File
path string
size uint32
id uint16
}
// newSegment returns a new instance of segment.
func newSegment(id uint16, path string) *segment {
return &segment{
id: id,
path: path,
}
}
// createSegment generates an empty segment at path.
func createSegment(id uint16, path string) (*segment, error) {
// Generate segment in temp location.
f, err := os.Create(path + ".initializing")
if err != nil {
return nil, err
}
defer f.Close()
// Write header to file and close.
hdr := newSegmentHeader()
if _, err := hdr.WriteTo(f); err != nil {
return nil, err
} else if err := f.Sync(); err != nil {
return nil, err
} else if err := f.Truncate(int64(SegmentSize)); err != nil {
return nil, err
} else if err := f.Close(); err != nil {
return nil, err
}
// Swap with target path.
if err := os.Rename(f.Name(), path); err != nil {
return nil, err
}
// Open segment at new location.
segment := newSegment(id, path)
if err := segment.Open(); err != nil {
return nil, err
}
return segment, nil
}
// ID returns the id the segment was initialized with.
func (s *segment) ID() uint16 { return s.id }
// Size returns the size of the data in the segment.
// This is only populated once InitForWrite() is called.
func (s *segment) Size() uint32 { return s.size }
func (s *segment) Open() error {
if err := func() (err error) {
if s.mmap, err = mmap.OpenFile(s.path, mmap.Read|mmap.Write); err != nil {
return err
}
// Read header.
buf, err := s.mmap.ReadOff(0, SegmentHeaderSize)
if err != nil {
return err
}
hdr, err := decodeSegmentHeader(buf)
if err != nil {
return err
} else if hdr.Version != SegmentVersion {
return ErrInvalidSegmentVersion
}
for s.size = uint32(SegmentHeaderSize); s.size < uint32(s.mmap.Len()); {
buf, err := s.mmap.ReadOff(int(s.size), EntryHeaderSize)
if err != nil {
return err
}
hdr, err := readEntryHeader(buf)
if err != nil {
return err
}
if !isValidEntryFlag(hdr.Flag) {
break
}
s.size += hdr.EntrySize()
}
if n, err := s.mmap.Seek(int64(s.size), io.SeekStart); err != nil {
return err
} else if n != int64(s.size) {
return ErrInvalidSegment
}
return nil
}(); err != nil {
s.Close()
return err
}
return nil
}
func (s *segment) WriteEntry(e entry) error {
if !s.CanWrite(e) {
return ErrSegmentNotWritable
}
// Write entry header.
n, err := s.mmap.Write(e.hdr.Encode())
if err != nil {
return err
} else if n != EntryHeaderSize {
return errors.Wrapf(ErrInvalidEntryHeader, "write entry header length %d", n)
}
s.size += uint32(n)
n, err = s.mmap.Write(e.key)
if err != nil {
return err
} else if n != int(e.hdr.KeySize) {
return errors.Wrapf(ErrInvalidEntryHeader, "write key length %d", n)
}
s.size += uint32(n)
n, err = s.mmap.Write(e.value)
if err != nil {
return err
} else if n != int(e.hdr.ValueSize) {
return errors.Wrapf(ErrInvalidEntryHeader, "write value length %d", n)
}
s.size += uint32(n)
return nil
}
func (s *segment) ReadEntry(off uint32) (e entry, err error) {
if off >= s.size {
return e, errors.Wrap(ErrInvalidOffset, "request offset exceeds segment size")
}
buf, err := s.mmap.ReadOff(int(off), EntryHeaderSize)
if err != nil {
return e, err
}
e.hdr, err = readEntryHeader(buf)
if err != nil {
return e, err
}
if !isValidEntryFlag(e.hdr.Flag) {
return e, errors.Wrap(ErrInvalidOffset, "invalid entry flag")
}
start := off + EntryHeaderSize
e.key, err = s.mmap.ReadOff(int(start), int(e.hdr.KeySize))
if err != nil {
return e, err
}
start += uint32(e.hdr.KeySize)
e.value, err = s.mmap.ReadOff(int(start), int(e.hdr.ValueSize))
if err != nil {
return e, err
}
return
}
func (s *segment) ForEachEntry(fn func(e entry) error) error {
hbuf := make([]byte, EntryHeaderSize)
for i := uint32(SegmentHeaderSize); i < s.size; {
if n, err := s.mmap.ReadAt(hbuf, int64(i)); err != nil {
return err
} else if n != int(EntryHeaderSize) {
return errors.Wrapf(ErrInvalidEntryHeader, "read entry header length %d", n)
}
hdr, err := readEntryHeader(hbuf)
if err != nil {
return err
}
if !isValidEntryFlag(hdr.Flag) {
break
}
start := i + EntryHeaderSize
key := make([]byte, hdr.KeySize)
if n, err := s.mmap.ReadAt(key, int64(start)); err != nil {
return err
} else if n != int(hdr.KeySize) {
return errors.Wrapf(ErrInvalidEntryHeader, "read key length %d", n)
}
start += uint32(hdr.KeySize)
value := make([]byte, hdr.ValueSize)
if n, err := s.mmap.ReadAt(value, int64(start)); err != nil {
return err
} else if n != int(hdr.ValueSize) {
return errors.Wrapf(ErrInvalidEntryHeader, "read value length %d", n)
}
e := createEntry(hdr.Flag, key, value)
if err := fn(e); err != nil {
return err
}
i += hdr.EntrySize()
}
return nil
}
// Close unmaps the segment.
func (s *segment) Close() (err error) {
return s.mmap.Close()
}
// CanWrite returns true if segment has space to write entry data.
func (s *segment) CanWrite(e entry) bool {
return s.size+e.Size() <= SegmentSize
}
// Flush flushes the buffer to disk.
func (s *segment) Flush() error {
return s.mmap.Sync()
}
// parseSegmentFilename returns the id represented by the hexadecimal filename.
func parseSegmentFilename(filename string) (uint16, error) {
i, err := strconv.ParseUint(filename, 16, 32)
return uint16(i), err
}