forked from pachyderm/pachyderm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datum_set.go
208 lines (187 loc) · 5.17 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
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
package worker
import (
"fmt"
"io"
"sort"
"github.com/pachyderm/pachyderm/src/client"
"github.com/pachyderm/pachyderm/src/client/pfs"
"github.com/pachyderm/pachyderm/src/client/pps"
)
// 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
}
func newAtomDatumFactory(pachClient *client.APIClient, input *pps.AtomInput) (DatumFactory, error) {
result := &atomDatumFactory{}
if input.Commit == "" {
// this can happen if a pipeline with multiple inputs has been triggered
// before all commits have inputs
return result, nil
}
fs, err := pachClient.GlobFileStream(pachClient.Ctx(), &pfs.GlobFileRequest{
Commit: client.NewCommit(input.Repo, input.Commit),
Pattern: input.Glob,
})
if err != nil {
return nil, err
}
for {
fileInfo, err := fs.Recv()
if err == io.EOF {
break
} else if err != nil {
return nil, err
}
result.inputs = append(result.inputs, &Input{
FileInfo: fileInfo,
Name: input.Name,
Lazy: input.Lazy,
Branch: input.Branch,
EmptyFiles: input.EmptyFiles,
})
}
// 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(pachClient *client.APIClient, union []*pps.Input) (DatumFactory, error) {
result := &unionDatumFactory{}
for _, input := range union {
datumFactory, err := NewDatumFactory(pachClient, 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
}
type gitDatumFactory struct {
inputs []*Input
}
func newGitDatumFactory(pachClient *client.APIClient, input *pps.GitInput) (DatumFactory, error) {
result := &gitDatumFactory{}
if input.Commit == "" {
// this can happen if a pipeline with multiple inputs has been triggered
// before all commits have inputs
return result, nil
}
fileInfo, err := pachClient.InspectFile(input.Name, input.Commit, "/commit.json")
if err != nil {
return nil, err
}
result.inputs = append(
result.inputs,
&Input{
FileInfo: fileInfo,
Name: input.Name,
Branch: input.Branch,
GitURL: input.URL,
},
)
return result, nil
}
func (d *gitDatumFactory) Len() int {
return len(d.inputs)
}
func (d *gitDatumFactory) Datum(i int) []*Input {
return []*Input{d.inputs[i]}
}
func newCrossDatumFactory(pachClient *client.APIClient, cross []*pps.Input) (DatumFactory, error) {
result := &crossDatumFactory{}
for _, input := range cross {
datumFactory, err := NewDatumFactory(pachClient, input)
if err != nil {
return nil, err
}
result.inputs = append(result.inputs, datumFactory)
}
return result, nil
}
func newCronDatumFactory(pachClient *client.APIClient, input *pps.CronInput) (DatumFactory, error) {
return newAtomDatumFactory(pachClient, &pps.AtomInput{
Name: input.Name,
Repo: input.Repo,
Branch: "master",
Commit: input.Commit,
Glob: "time",
})
}
// NewDatumFactory creates a datumFactory for an input.
func NewDatumFactory(pachClient *client.APIClient, input *pps.Input) (DatumFactory, error) {
switch {
case input.Atom != nil:
return newAtomDatumFactory(pachClient, input.Atom)
case input.Union != nil:
return newUnionDatumFactory(pachClient, input.Union)
case input.Cross != nil:
return newCrossDatumFactory(pachClient, input.Cross)
case input.Cron != nil:
return newCronDatumFactory(pachClient, input.Cron)
case input.Git != nil:
return newGitDatumFactory(pachClient, input.Git)
}
return nil, fmt.Errorf("unrecognized input type")
}