-
Notifications
You must be signed in to change notification settings - Fork 4
/
sort_sorttype.go
309 lines (277 loc) · 7.83 KB
/
sort_sorttype.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Package extsort implements an unstable external sort for all the records in a chan or iterator
package extsort
import (
"bufio"
"context"
"encoding/binary"
"io"
"sort"
"github.com/lanrat/extsort/queue"
"github.com/lanrat/extsort/tempfile"
"golang.org/x/sync/errgroup"
)
type chunk struct {
data []SortType
less CompareLessFunc
}
func newChunk(size int, lessFunc CompareLessFunc) *chunk {
c := new(chunk)
c.less = lessFunc
c.data = make([]SortType, 0, size)
return c
}
func (c *chunk) Len() int {
return len(c.data)
}
func (c *chunk) Swap(i, j int) {
c.data[i], c.data[j] = c.data[j], c.data[i]
}
func (c *chunk) Less(i, j int) bool {
return c.less(c.data[i], c.data[j])
}
// SortTypeSorter stores an input chan and feeds Sort to return a sorted chan
type SortTypeSorter struct {
config Config
buildSortCtx context.Context
saveCtx context.Context
mergeErrChan chan error
tempWriter tempfile.TempWriter
tempReader tempfile.TempReader
input chan SortType
chunkChan chan *chunk
saveChunkChan chan *chunk
mergeChunkChan chan SortType
lessFunc CompareLessFunc
fromBytes FromBytes
}
func newSorter(i chan SortType, fromBytes FromBytes, lessFunc CompareLessFunc, config *Config) *SortTypeSorter {
s := new(SortTypeSorter)
s.input = i
s.lessFunc = lessFunc
s.fromBytes = fromBytes
s.config = *mergeConfig(config)
s.chunkChan = make(chan *chunk, s.config.ChanBuffSize)
s.saveChunkChan = make(chan *chunk, s.config.ChanBuffSize)
s.mergeChunkChan = make(chan SortType, s.config.SortedChanBuffSize)
s.mergeErrChan = make(chan error, 1)
return s
}
// New returns a new Sorter instance that can be used to sort the input chan
// fromBytes is needed to unmarshal SortTypes from []byte on disk
// lessfunc is the comparator used for SortType
// config ca be nil to use the defaults, or only set the non-default values desired
// if errors or interupted, may leave temp files behind in config.TempFilesDir
// the returned chanels contain the data returned from calling Sort()
func New(i chan SortType, fromBytes FromBytes, lessFunc CompareLessFunc, config *Config) (*SortTypeSorter, chan SortType, chan error) {
var err error
s := newSorter(i, fromBytes, lessFunc, config)
s.tempWriter, err = tempfile.New(s.config.TempFilesDir)
if err != nil {
s.mergeErrChan <- err
close(s.mergeErrChan)
close(s.mergeChunkChan)
}
return s, s.mergeChunkChan, s.mergeErrChan
}
// NewMock is the same as New() but is backed by memory instead of a temporary file on disk
// n is the size to initialize the backing bytes buffer too
func NewMock(i chan SortType, fromBytes FromBytes, lessFunc CompareLessFunc, config *Config, n int) (*SortTypeSorter, chan SortType, chan error) {
s := newSorter(i, fromBytes, lessFunc, config)
s.tempWriter = tempfile.Mock(n)
return s, s.mergeChunkChan, s.mergeErrChan
}
// Sort sorts the Sorter's input chan and returns a new sorted chan, and error Chan
// Sort is a chunking operation that runs multiple workers asynchronously
// this blocks while sorting chunks and unblocks when merging
// NOTE: the context passed to Sort must outlive Sort() returning.
// Merge uses the same context and runs in a goroutine after Sort returns().
// for example, if calling sort in an errGroup, you must pass the group's parent context into sort.
func (s *SortTypeSorter) Sort(ctx context.Context) {
var buildSortErrGroup, saveErrGroup *errgroup.Group
buildSortErrGroup, s.buildSortCtx = errgroup.WithContext(ctx)
saveErrGroup, s.saveCtx = errgroup.WithContext(ctx)
//start creating chunks
buildSortErrGroup.Go(s.buildChunks)
// sort chunks
for i := 0; i < s.config.NumWorkers; i++ {
buildSortErrGroup.Go(s.sortChunks)
}
// save chunks
saveErrGroup.Go(s.saveChunks)
err := buildSortErrGroup.Wait()
if err != nil {
s.mergeErrChan <- err
close(s.mergeErrChan)
close(s.mergeChunkChan)
return
}
// need to close saveChunkChan
close(s.saveChunkChan)
err = saveErrGroup.Wait()
if err != nil {
s.mergeErrChan <- err
close(s.mergeErrChan)
close(s.mergeChunkChan)
return
}
// read chunks and merge
// if this errors, it is returned in the errorChan
go s.mergeNChunks(ctx)
}
// buildChunks reads data from the input chan to builds chunks and pushes them to chunkChan
func (s *SortTypeSorter) buildChunks() error {
defer close(s.chunkChan) // if this is not called on error, causes a deadlock
for {
c := newChunk(s.config.ChunkSize, s.lessFunc)
for i := 0; i < s.config.ChunkSize; i++ {
select {
case rec, ok := <-s.input:
if !ok {
break
}
c.data = append(c.data, rec)
case <-s.buildSortCtx.Done():
return s.buildSortCtx.Err()
}
}
if len(c.data) == 0 {
// the chunk is empty
break
}
// chunk is now full
s.chunkChan <- c
}
return nil
}
// sortChunks is a worker for sorting the data stored in a chunk prior to save
func (s *SortTypeSorter) sortChunks() error {
for {
select {
case b, more := <-s.chunkChan:
if more {
// sort
sort.Sort(b)
// save
s.saveChunkChan <- b
} else {
return nil
}
case <-s.buildSortCtx.Done():
return s.buildSortCtx.Err()
}
}
}
// saveChunks is a worker for saving sorted data to disk
func (s *SortTypeSorter) saveChunks() error {
var err error
scratch := make([]byte, binary.MaxVarintLen64)
for {
select {
case b, more := <-s.saveChunkChan:
if more {
for _, d := range b.data {
// binary encoding for size
raw := d.ToBytes()
n := binary.PutUvarint(scratch, uint64(len(raw)))
_, err = s.tempWriter.Write(scratch[:n])
if err != nil {
return err
}
// add data
_, err = s.tempWriter.Write(raw)
if err != nil {
return err
}
}
_, err = s.tempWriter.Next()
if err != nil {
return err
}
} else {
s.tempReader, err = s.tempWriter.Save()
return err
}
case <-s.saveCtx.Done():
// delete the temp file from disk (error unchecked)
s.tempWriter.Close()
return s.saveCtx.Err()
}
}
}
// mergeNChunks runs asynchronously in the background feeding data to getNext
// sends errors to s.mergeErrorChan
func (s *SortTypeSorter) mergeNChunks(ctx context.Context) {
//populate queue with data from mergeFile list
defer close(s.mergeChunkChan)
// close temp file when done
defer func() {
err := s.tempReader.Close()
if err != nil {
s.mergeErrChan <- err
}
}()
defer close(s.mergeErrChan)
pq := queue.NewPriorityQueue(func(a, b interface{}) bool {
return s.lessFunc(a.(*mergeFile).nextRec, b.(*mergeFile).nextRec)
})
for i := 0; i < s.tempReader.Size(); i++ {
merge := new(mergeFile)
merge.fromBytes = s.fromBytes
merge.reader = s.tempReader.Read(i)
_, ok, err := merge.getNext() // start the merge by preloading the values
if err == io.EOF || !ok {
continue
}
if err != nil {
s.mergeErrChan <- err
return
}
pq.Push(merge)
}
for pq.Len() > 0 {
merge := pq.Peek().(*mergeFile)
rec, more, err := merge.getNext()
if err != nil {
s.mergeErrChan <- err
return
}
if more {
pq.PeekUpdate()
} else {
pq.Pop()
}
// check for err in context just in case
select {
case s.mergeChunkChan <- rec:
case <-ctx.Done():
s.mergeErrChan <- err
return
}
}
}
// mergefile represents each sorted chunk on disk and its next value
type mergeFile struct {
nextRec SortType
fromBytes FromBytes
reader *bufio.Reader
}
// getNext returns the next value from the sorted chunk on disk
// the first call will return nil while the struct is initialized
func (m *mergeFile) getNext() (SortType, bool, error) {
var newRecBytes []byte
old := m.nextRec
n, err := binary.ReadUvarint(m.reader)
if err == nil {
newRecBytes = make([]byte, int(n))
_, err = io.ReadFull(m.reader, newRecBytes)
}
if err != nil {
if err == io.EOF {
m.nextRec = nil
return old, false, nil
}
return nil, false, err
}
m.nextRec = m.fromBytes(newRecBytes)
return old, true, nil
}