-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpull.go
More file actions
238 lines (192 loc) · 5.6 KB
/
pull.go
File metadata and controls
238 lines (192 loc) · 5.6 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
* Copyright 2016 Frank Wessels <fwessels@xs4all.nl>
*
* 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 s3git
import (
"encoding/hex"
"github.com/bmatsuo/lmdb-go/lmdb"
"github.com/s3git/s3git-go/internal/backend"
"github.com/s3git/s3git-go/internal/cas"
"github.com/s3git/s3git-go/internal/core"
"github.com/s3git/s3git-go/internal/kv"
"io/ioutil"
"os"
)
// Pull updates for the repository
func (repo Repository) Pull(progress func(maxTicks int64)) error {
return pull(progress)
}
func pull(progress func(maxTicks int64)) error {
client, err := backend.GetDefaultClient()
if err != nil {
return err
}
// Get map of prefixes already in store
prefixesInBackend, err := listPrefixes(client)
if err != nil {
return err
}
prefixesToFetch := []string{}
// Iterate over prefixes and find out if they are already available locally
for prefix, _ := range prefixesInBackend {
key, _ := hex.DecodeString(prefix)
_, _, err := kv.GetLevel1(key)
if err != nil && !lmdb.IsNotFound(err) {
return err
}
if lmdb.IsNotFound(err) {
// Prefix is not available locally --> add to get fetched
prefixesToFetch = append(prefixesToFetch, prefix)
}
}
if len(prefixesToFetch) == 0 {
return nil
}
progress(int64(len(prefixesToFetch)))
// TODO: [perf] Speed up by running parallel in multiple go routines
for _, prefix := range prefixesToFetch {
// Fetch Prefix object and all objects directly and indirectly referenced by it
err = fetchPrefix(prefix, client)
if err != nil {
return err
}
progress(int64(len(prefixesToFetch)))
}
return nil
}
// Fetch Prefix object and all objects directly and indirectly referenced by it
func fetchPrefix(prefix string, client backend.Backend) error {
// Make sure that we do the storage of the object in the cas in
// reverse order, that is store prefix object in the cas as the
// *last* action, just before that the commit object, etc.
// This ensures that, if the pull action is prematurely terminated
// (either by user/application, or some sort of error/crash) that
// a subsequent pull will still miss the prefix object and pull it
// again
// First fetch blob for prefix object
prefixName, prefixBytes, err := fetchBlobTempFileAndContents(prefix, client)
if err != nil {
return err
}
defer os.Remove(prefixName)
po, err := core.GetPrefixObjectFromString(string(prefixBytes))
if err != nil {
return err
}
{
// Now pull down commit object
commitName, commitBytes, err := fetchBlobTempFileAndContents(po.S3gitFollowMe, client)
if err != nil {
return err
}
defer os.Remove(commitName)
co, err := core.GetCommitObjectFromString(string(commitBytes))
if err != nil {
return err
}
if co.S3gitTree != "" {
// Now pull down tree object
treeName, treeBytes, err := fetchBlobTempFileAndContents(co.S3gitTree, client)
if err != nil {
return err
}
defer os.Remove(treeName)
to, err := core.GetTreeObjectFromString(string(treeBytes))
if err != nil {
return err
}
// Cache root keys for all added blobs in this commit ...
err = cacheKeysInKV(to.S3gitAdded, kv.BLOB)
if err != nil {
return err
}
// Add tree object to cas
_, err = cas.StoreBlobInCache(treeName, kv.TREE)
if err != nil {
return err
}
// Delete the chunks for the tree object since we are unlikely to need it again
err = cas.DeleteLeavesForBlob(co.S3gitTree)
if err != nil {
return err
}
}
if co.S3gitSnapshot != "" {
err = cacheKeysInKV([]string{co.S3gitSnapshot}, kv.SNAPSHOT)
if err != nil {
return err
}
}
// Add commit object to cas
_, err = cas.StoreBlobInCache(commitName, kv.COMMIT)
if err != nil {
return err
}
// Mark warm and cold parents as parents
err = co.MarkWarmAndColdParents()
if err != nil {
return err
}
}
// Add prefix object to cas
_, err = cas.StoreBlobInCache(prefixName, kv.PREFIX)
if err != nil {
return err
}
return nil
}
// Fetch blob down to temp file along with contents
func fetchBlobTempFileAndContents(prefix string, client backend.Backend) (tempFile string, contents []byte, err error) {
name, err := cas.FetchBlobToTempFile(prefix, client)
if err != nil {
return "", nil, err
}
contents, err = ioutil.ReadFile(name)
if err != nil {
return "", nil, err
}
return name, contents, nil
}
const treeBatchSize = 0x4000
// Just cache the key for BLOBs, content will be pulled down later when needed
// For performance do not write per key to the KV but write in larger batches
func cacheKeysInKV(added []string, objType string) error {
// Create arrays to be able to batch the operation
keys := make([][]byte, 0, treeBatchSize)
values := make([][]byte, 0, treeBatchSize)
count := 0
for _, add := range added {
key, _ := hex.DecodeString(add)
keys = append(keys, key)
values = append(values, nil)
if len(keys) == cap(keys) {
err := kv.AddMultiToLevel1(keys, values, objType)
if err != nil {
return err
}
keys = keys[:0]
values = values[:0]
}
count++
}
if len(keys) > 0 {
err := kv.AddMultiToLevel1(keys, values, objType)
if err != nil {
return err
}
count++
}
return nil
}