forked from pachyderm/pachyderm
/
datum_set.go
161 lines (143 loc) · 4.19 KB
/
datum_set.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
package worker
import (
"fmt"
"sort"
"github.com/pachyderm/pachyderm/src/client"
"github.com/pachyderm/pachyderm/src/client/pfs"
"github.com/pachyderm/pachyderm/src/client/pps"
"golang.org/x/net/context"
)
// DatumFactory is an interface which allows you to iterate through the datums
// for a job.
type DatumFactory interface {
Len() int
Datum(i int) []*Input
}
type atomDatumFactory struct {
inputs []*Input
index int
}
func newAtomDatumFactory(ctx context.Context, pfsClient pfs.APIClient, input *pps.AtomInput) (DatumFactory, error) {
result := &atomDatumFactory{}
fileInfos, err := pfsClient.GlobFile(ctx, &pfs.GlobFileRequest{
Commit: client.NewCommit(input.Repo, input.Commit),
Pattern: input.Glob,
})
if err != nil {
return nil, err
}
for _, fileInfo := range fileInfos.FileInfo {
result.inputs = append(result.inputs, &Input{
FileInfo: fileInfo,
Name: input.Name,
Lazy: input.Lazy,
Branch: input.Branch,
})
}
// We sort the inputs so that the order is deterministic. Note that it's
// not possible for 2 inputs to have the same path so this is guaranteed to
// produce a deterministic order.
sort.Slice(result.inputs, func(i, j int) bool {
// We sort by descending size first because it can boost performance to
// process the biggest datums first.
if result.inputs[i].FileInfo.SizeBytes != result.inputs[j].FileInfo.SizeBytes {
return result.inputs[i].FileInfo.SizeBytes > result.inputs[j].FileInfo.SizeBytes
}
return result.inputs[i].FileInfo.File.Path < result.inputs[j].FileInfo.File.Path
})
return result, nil
}
func (d *atomDatumFactory) Len() int {
return len(d.inputs)
}
func (d *atomDatumFactory) Datum(i int) []*Input {
return []*Input{d.inputs[i]}
}
type unionDatumFactory struct {
inputs []DatumFactory
}
func newUnionDatumFactory(ctx context.Context, pfsClient pfs.APIClient, union []*pps.Input) (DatumFactory, error) {
result := &unionDatumFactory{}
for _, input := range union {
datumFactory, err := NewDatumFactory(ctx, pfsClient, input)
if err != nil {
return nil, err
}
result.inputs = append(result.inputs, datumFactory)
}
return result, nil
}
func (d *unionDatumFactory) Len() int {
result := 0
for _, datumFactory := range d.inputs {
result += datumFactory.Len()
}
return result
}
func (d *unionDatumFactory) Datum(i int) []*Input {
for _, datumFactory := range d.inputs {
if i < datumFactory.Len() {
return datumFactory.Datum(i)
}
i -= datumFactory.Len()
}
panic("index out of bounds")
}
type crossDatumFactory struct {
inputs []DatumFactory
}
func (d *crossDatumFactory) Len() int {
if len(d.inputs) == 0 {
return 0
}
result := d.inputs[0].Len()
for i := 1; i < len(d.inputs); i++ {
result *= d.inputs[i].Len()
}
return result
}
func (d *crossDatumFactory) Datum(i int) []*Input {
if i >= d.Len() {
panic("index out of bounds")
}
var result []*Input
for _, datumFactory := range d.inputs {
result = append(result, datumFactory.Datum(i%datumFactory.Len())...)
i /= datumFactory.Len()
}
return result
}
func newCrossDatumFactory(ctx context.Context, pfsClient pfs.APIClient, cross []*pps.Input) (DatumFactory, error) {
result := &crossDatumFactory{}
for _, input := range cross {
datumFactory, err := NewDatumFactory(ctx, pfsClient, input)
if err != nil {
return nil, err
}
result.inputs = append(result.inputs, datumFactory)
}
return result, nil
}
func newCronDatumFactory(ctx context.Context, pfsClient pfs.APIClient, input *pps.CronInput) (DatumFactory, error) {
return newAtomDatumFactory(ctx, pfsClient, &pps.AtomInput{
Name: input.Name,
Repo: input.Repo,
Branch: "master",
Commit: input.Commit,
Glob: "time",
})
}
// NewDatumFactory creates a datumFactory for an input.
func NewDatumFactory(ctx context.Context, pfsClient pfs.APIClient, input *pps.Input) (DatumFactory, error) {
switch {
case input.Atom != nil:
return newAtomDatumFactory(ctx, pfsClient, input.Atom)
case input.Union != nil:
return newUnionDatumFactory(ctx, pfsClient, input.Union)
case input.Cross != nil:
return newCrossDatumFactory(ctx, pfsClient, input.Cross)
case input.Cron != nil:
return newCronDatumFactory(ctx, pfsClient, input.Cron)
}
return nil, fmt.Errorf("unrecognized input type")
}