-
Notifications
You must be signed in to change notification settings - Fork 607
/
Copy pathhelpers.go
142 lines (115 loc) · 3.65 KB
/
helpers.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
/*
Copyright 2021 Cortex Labs, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package enqueuer
import (
"encoding/json"
"github.com/aws/aws-sdk-go/service/s3"
awslib "github.com/cortexlabs/cortex/pkg/lib/aws"
"github.com/cortexlabs/cortex/pkg/lib/errors"
"github.com/cortexlabs/cortex/pkg/lib/pointer"
"github.com/gobwas/glob"
)
type jsonBuffer struct {
BatchSize int
messageList []json.RawMessage
}
func newJSONBuffer(batchSize int) *jsonBuffer {
return &jsonBuffer{
BatchSize: batchSize,
messageList: make([]json.RawMessage, 0, batchSize),
}
}
func (j *jsonBuffer) Add(jsonMessage json.RawMessage) {
j.messageList = append(j.messageList, jsonMessage)
}
func (j *jsonBuffer) Clear() {
j.messageList = make([]json.RawMessage, 0, j.BatchSize)
}
func (j *jsonBuffer) Length() int {
return len(j.messageList)
}
func addJSONObjectsToQueue(uploader *sqsBatchUploader, jsonMessageList *jsonBuffer) error {
jsonBytes, err := json.Marshal(jsonMessageList.messageList)
if err != nil {
return err
}
err = uploader.AddToBatch(randomMessageID(), pointer.String(string(jsonBytes)))
if err != nil {
return err
}
return nil
}
func s3IteratorFromLister(awsClient *awslib.Client, s3Lister S3Lister, fn func(string, *s3.Object) (bool, error)) (int64, error) {
includeGlobPatterns := make([]glob.Glob, 0, len(s3Lister.Includes))
for _, includePattern := range s3Lister.Includes {
globExpression, err := glob.Compile(includePattern, '/')
if err != nil {
return 0, errors.Wrap(err, "failed to interpret glob pattern", includePattern)
}
includeGlobPatterns = append(includeGlobPatterns, globExpression)
}
excludeGlobPatterns := make([]glob.Glob, 0, len(s3Lister.Excludes))
for _, excludePattern := range s3Lister.Excludes {
globExpression, err := glob.Compile(excludePattern, '/')
if err != nil {
return 0, errors.Wrap(err, "failed to interpret glob pattern", excludePattern)
}
excludeGlobPatterns = append(excludeGlobPatterns, globExpression)
}
var numResults int64
for _, s3Path := range s3Lister.S3Paths {
bucket, key, err := awslib.SplitS3Path(s3Path)
if err != nil {
return 0, err
}
awsClientForBucket, err := awslib.NewFromClientS3Path(s3Path, awsClient)
if err != nil {
return 0, err
}
err = awsClientForBucket.S3Iterator(bucket, key, false, nil, nil, func(s3Obj *s3.Object) (bool, error) {
s3FilePath := awslib.S3Path(bucket, *s3Obj.Key)
shouldSkip := false
if len(includeGlobPatterns) > 0 {
shouldSkip = true
for _, includeGlobPattern := range includeGlobPatterns {
if includeGlobPattern.Match(s3FilePath) {
shouldSkip = false
break
}
}
}
for _, excludeGlobPattern := range excludeGlobPatterns {
if excludeGlobPattern.Match(s3FilePath) {
shouldSkip = true
break
}
}
if !shouldSkip {
shouldContinue, err := fn(bucket, s3Obj)
numResults++
if s3Lister.MaxResults != nil && numResults >= *s3Lister.MaxResults {
shouldContinue = false
}
return shouldContinue, err
}
return true, nil
})
if err != nil {
return 0, err
}
if s3Lister.MaxResults != nil && numResults >= *s3Lister.MaxResults {
return numResults, nil
}
}
return numResults, nil
}