-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoptions.go
72 lines (55 loc) · 1.45 KB
/
options.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
package extsort
import (
"bytes"
"sort"
)
// Compares byte chunks. -1 for a < b and 0 for a == b.
type Compare func(a, b []byte) int
// Equal compares two byte chunks for equality.
type Equal func(a, b []byte) bool
func stdCompare(a, b []byte) int {
return bytes.Compare(a, b)
}
// Options contains sorting options
type Options struct {
// WorkDir specifies the working directory.
// By default os.TempDir() is used.
WorkDir string
// Keep temporary files until Close.
// Default: Immediately remove temporary files.
KeepFiles bool
// Compare defines the compare function.
// Default: bytes.Compare
Compare Compare
// Sort defines the sort function that is used.
// Default: sort.Sort
Sort func(sort.Interface)
// Dedupe defines the compare function for de-duplication.
// Default: nil (= do not de-dupe)
// Keeps the last added item.
Dedupe Equal
// BufferSize limits the memory buffer used for sorting.
// Default: 64MiB (must be at least 64KiB)
BufferSize int
// Compression optionally uses compression for temporary output.
Compression Compression
}
func (o *Options) norm() *Options {
var opt Options
if o != nil {
opt = *o
}
if opt.Compare == nil {
opt.Compare = stdCompare
}
if opt.Sort == nil {
opt.Sort = sort.Sort
}
if std := (1 << 26); opt.BufferSize < 1 {
opt.BufferSize = std
} else if min := (1 << 16); opt.BufferSize < min {
opt.BufferSize = min
}
opt.Compression = opt.Compression.norm()
return &opt
}