-
Notifications
You must be signed in to change notification settings - Fork 32
/
reader.go
59 lines (48 loc) · 1.59 KB
/
reader.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
package bigqueue
import (
"strings"
"github.com/grandecola/mmap"
)
// reader knows how to read data from arena.
type reader interface {
// grow grows reader's capacity, if necessary, to guarantee space for
// another n bytes. After grow(n), at least n bytes can be written to reader
// without another allocation. If n is negative, grow panics.
grow(n int)
// readFrom copies data from arena starting at given offset. Because the data
// may be spread over multiple arenas, an index into the data is provided so
// the data is copied starting at given index.
readFrom(aa *mmap.File, offset, index int) int
}
// bytesReader holds a slice of bytes to hold the data.
type bytesReader struct {
b []byte
}
// grow expands the capacity of bytesReader by n bytes.
func (br *bytesReader) grow(n int) {
if n < 0 {
panic("bigqueue.reader.grow: negative count")
}
temp := make([]byte, n+len(br.b))
if br.b != nil {
_ = copy(temp, br.b)
}
br.b = temp
}
// readFrom reads the arena at offset and copies the data at index.
func (br *bytesReader) readFrom(aa *mmap.File, offset, index int) int {
n, _ := aa.ReadAt(br.b[index:], int64(offset))
return n
}
// stringReader holds a string builder to hold the data read from arena(s).
type stringReader struct {
sb strings.Builder
}
// grow expands the capacity of the string builder by n bytes.
func (sr *stringReader) grow(n int) {
sr.sb.Grow(n)
}
// readFrom reads data from arena starting at offset and stores it at provided index.
func (sr *stringReader) readFrom(aa *mmap.File, offset, index int) int {
return aa.ReadStringAt(&sr.sb, int64(offset))
}