-
Notifications
You must be signed in to change notification settings - Fork 3
/
sort_strings.go
298 lines (266 loc) · 7.13 KB
/
sort_strings.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
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 stringChunk struct {
data []string
}
func newStringChunk(size int) *stringChunk {
c := new(stringChunk)
c.data = make([]string, 0, size)
return c
}
func (c *stringChunk) Len() int {
return len(c.data)
}
func (c *stringChunk) Swap(i, j int) {
c.data[i], c.data[j] = c.data[j], c.data[i]
}
func (c *stringChunk) Less(i, j int) bool {
return c.data[i] < c.data[j]
}
// StringSorter stores an input chan and feeds Sort to return a sorted chan
type StringSorter struct {
config Config
buildSortCtx context.Context
saveCtx context.Context
mergeErrChan chan error
tempWriter tempfile.TempWriter
tempReader tempfile.TempReader
input chan string
chunkChan chan *stringChunk
saveChunkChan chan *stringChunk
mergeChunkChan chan string
}
func newStringSorter(i chan string, config *Config) *StringSorter {
s := new(StringSorter)
s.input = i
s.config = *mergeConfig(config)
s.chunkChan = make(chan *stringChunk, s.config.ChanBuffSize)
s.saveChunkChan = make(chan *stringChunk, s.config.ChanBuffSize)
s.mergeChunkChan = make(chan string, s.config.SortedChanBuffSize)
s.mergeErrChan = make(chan error, 1)
return s
}
// StringsMock is the same as Strings() but is backed by memory instead of a temporary file on disk
// n is the size to initialize the backing bytes buffer too
func StringsMock(i chan string, config *Config, n int) (*StringSorter, chan string, chan error) {
s := newStringSorter(i, config)
s.tempWriter = tempfile.Mock(n)
return s, s.mergeChunkChan, s.mergeErrChan
}
// Strings returns a new Sorter instance that can be used to sort the input chan
func Strings(i chan string, config *Config) (*StringSorter, chan string, chan error) {
var err error
s := newStringSorter(i, 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
}
// 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 used 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 *StringSorter) 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 *StringSorter) buildChunks() error {
defer close(s.chunkChan) // if this is not called on error, causes a deadlock
for {
c := newStringChunk(s.config.ChunkSize)
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 *StringSorter) sortChunks() error {
for {
select {
case b, more := <-s.chunkChan:
if more {
// sort
sort.Sort(b)
// save
select {
case s.saveChunkChan <- b:
case <-s.buildSortCtx.Done():
return s.buildSortCtx.Err()
}
} else {
return nil
}
case <-s.buildSortCtx.Done():
return s.buildSortCtx.Err()
}
}
}
// saveChunks is a worker for saving sorted data to disk
func (s *StringSorter) 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
n := binary.PutUvarint(scratch, uint64(len(d)))
_, err = s.tempWriter.Write(scratch[:n])
if err != nil {
return err
}
// add data
_, err = s.tempWriter.WriteString(d)
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 *StringSorter) 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 a.(*mergeStringFile).nextRec < b.(*mergeStringFile).nextRec
})
for i := 0; i < s.tempReader.Size(); i++ {
merge := new(mergeStringFile)
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().(*mergeStringFile)
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 <- ctx.Err()
return
}
}
}
// mergeStringFile represents each sorted chunk on disk and its next value
type mergeStringFile struct {
nextRec string
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 *mergeStringFile) getNext() (string, 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 = ""
return old, false, nil
}
return "", false, err
}
m.nextRec = string(newRecBytes)
return old, true, nil
}