forked from convox/rack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.go
138 lines (106 loc) · 2.58 KB
/
index.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
package aws
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
"github.com/convox/rack/api/cache"
"github.com/convox/rack/api/structs"
)
var (
IndexOperationConcurrency = 128
)
func (p *AWSProvider) IndexDiff(index *structs.Index) ([]string, error) {
missing := []string{}
inch := make(chan string)
outch := make(chan string)
errch := make(chan error)
for i := 1; i < IndexOperationConcurrency; i++ {
go p.missingHashes(p.SettingsBucket, inch, outch, errch)
}
go func() {
for hash := range *index {
inch <- hash
}
}()
for range *index {
select {
case hash := <-outch:
if hash != "" {
missing = append(missing, hash)
}
case err := <-errch:
return nil, err
}
}
close(inch)
return missing, nil
}
func (p *AWSProvider) IndexDownload(index *structs.Index, dir string) error {
inch := make(chan string)
errch := make(chan error)
for i := 1; i < IndexOperationConcurrency; i++ {
go p.downloadItems(p.SettingsBucket, *index, dir, inch, errch)
}
go func() {
for hash := range *index {
inch <- hash
}
}()
for range *index {
if err := <-errch; err != nil {
return err
}
}
return nil
}
func (p *AWSProvider) IndexUpload(hash string, data []byte) error {
return p.s3Put(p.SettingsBucket, fmt.Sprintf("index/%s", hash), data, false)
}
func (p *AWSProvider) downloadItems(bucket string, index structs.Index, dir string, inch chan string, errch chan error) {
for hash := range inch {
errch <- p.downloadItem(bucket, hash, index[hash], dir)
}
}
func (p *AWSProvider) downloadItem(bucket, hash string, item structs.IndexItem, dir string) error {
data, err := p.s3Get(bucket, fmt.Sprintf("index/%s", hash))
if err != nil {
return err
}
file := filepath.Join(dir, item.Name)
err = os.MkdirAll(filepath.Dir(file), 0755)
if err != nil {
return err
}
err = ioutil.WriteFile(file, data, item.Mode)
if err != nil {
return err
}
return os.Chtimes(file, item.ModTime, item.ModTime)
}
func (p *AWSProvider) missingHashes(bucket string, inch, outch chan string, errch chan error) {
for hash := range inch {
exists, err := p.hashExists(bucket, hash)
if err != nil {
errch <- err
} else if !exists {
outch <- hash
} else {
outch <- ""
}
}
}
func (p *AWSProvider) hashExists(bucket, hash string) (bool, error) {
if exists, ok := cache.Get("index.missingHash", hash).(bool); ok && exists {
return true, nil
}
exists, err := p.s3Exists(bucket, fmt.Sprintf("index/%s", hash))
if err != nil {
return false, err
}
if exists {
cache.Set("index.missingHash", hash, true, 30*24*time.Hour)
}
return exists, nil
}