-
Notifications
You must be signed in to change notification settings - Fork 4
/
tempfile.go
166 lines (141 loc) · 3.18 KB
/
tempfile.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
package extsort
import (
"bufio"
"encoding/binary"
"io"
"os"
)
type tempWriter struct {
f *os.File
c compressedWriter
w *bufio.Writer
keepFile bool
scratch []byte
offsets []int64
size int64
}
func newTempWriter(dir string, compress Compression, keepFile bool) (*tempWriter, error) {
f, err := newTempFile(dir, "extsort", keepFile)
if err != nil {
return nil, err
}
c := compress.newWriter(f)
w := bufio.NewWriterSize(c, 1<<16) // 64k
return &tempWriter{f: f, c: c, w: w, scratch: make([]byte, binary.MaxVarintLen64), keepFile: keepFile}, nil
}
func (t *tempWriter) ReaderAt() io.ReaderAt {
return t.f
}
func (t *tempWriter) Encode(ent *entry) error {
if err := t.encodeSize(ent.keyLen); err != nil {
return err
}
if err := t.encodeSize(ent.ValLen()); err != nil {
return err
}
if _, err := t.Write(ent.data); err != nil {
return err
}
return nil
}
func (t *tempWriter) Write(p []byte) (int, error) {
n, err := t.w.Write(p)
t.size += int64(n)
return n, err
}
func (t *tempWriter) Flush() error {
if err := t.w.Flush(); err != nil {
return err
}
if err := t.c.Close(); err != nil {
return err
}
pos, err := t.f.Seek(0, io.SeekCurrent)
if err != nil {
return err
}
t.offsets = append(t.offsets, pos)
t.c.Reset(t.f)
t.w.Reset(t.c)
return nil
}
func (t *tempWriter) Close() (err error) {
if e := t.c.Close(); e != nil {
err = e
}
if e := closeTempFile(t.f, t.keepFile); e != nil {
err = e
}
return
}
func (t *tempWriter) encodeSize(sz int) error {
n := binary.PutUvarint(t.scratch, uint64(sz))
if _, err := t.Write(t.scratch[:n]); err != nil {
return err
}
return nil
}
func (t *tempWriter) Size() int64 {
return t.size
}
// --------------------------------------------------------------------
type tempReader struct {
readers []io.ReadCloser
sections []*bufio.Reader
}
func newTempReader(ra io.ReaderAt, offsets []int64, bufSize int, compress Compression) (*tempReader, error) {
r := &tempReader{
readers: make([]io.ReadCloser, 0, len(offsets)),
sections: make([]*bufio.Reader, 0, len(offsets)),
}
slimit := bufSize / (len(offsets) + 1)
offset := int64(0)
for _, next := range offsets {
crd, err := compress.newReader(io.NewSectionReader(ra, offset, next-offset))
if err != nil {
_ = r.Close()
return nil, err
}
r.sections = append(r.sections, bufio.NewReaderSize(crd, slimit))
r.readers = append(r.readers, crd)
offset = next
}
return r, nil
}
func (t *tempReader) NumSections() int {
return len(t.sections)
}
func (t *tempReader) ReadNext(section int) (*entry, error) {
r := t.sections[section]
if r == nil {
return nil, nil
}
ku, err := binary.ReadUvarint(r)
if err == io.EOF {
t.sections[section] = nil
return nil, nil
} else if err != nil {
return nil, err
}
vu, err := binary.ReadUvarint(r)
if err == io.EOF {
t.sections[section] = nil
return nil, nil
} else if err != nil {
return nil, err
}
ent := fetchEntry(int(ku), int(vu))
if _, err := io.ReadFull(r, ent.data); err != nil {
ent.Release()
return nil, err
}
return ent, nil
}
func (t *tempReader) Close() (err error) {
for _, crd := range t.readers {
if e := crd.Close(); e != nil {
err = e
}
}
return
}