@cryptotaxi247 / kubo / commits / 4fdfbc7d3

compute add size in background to not stall add operation

License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>

Jeromy committed Dec 4, 2015 at 13:01 UTC 4fdfbc7d32f3b8140771830ce2c1c4e3c1622f02
1 file changed +61 -43
core/commands/add.go
+61 -43
@@ -65,14 +65,19 @@ remains to be implemented.
65 return nil
66 }
67
68 - size, err := sizeFile.Size()
69 - if err != nil {
70 - // see comment above
71 - return nil
72 - }
68 + sizeCh := make(chan int64, 1)
69 + req.Values()["size"] = sizeCh
70
74 - log.Debugf("Total size of file being added: %v\n", size)
75 - req.Values()["size"] = size
71 + go func() {
72 + size, err := sizeFile.Size()
73 + if err != nil {
74 + // see comment above
75 + return
76 + }
77 +
78 + log.Debugf("Total size of file being added: %v\n", size)
79 + sizeCh <- size
80 + }()
81
82 return nil
83 },
@@ -189,17 +194,12 @@ remains to be implemented.
194 return
195 }
196
192 - size := int64(0)
193 - s, found := req.Values()["size"]
194 - if found {
195 - size = s.(int64)
196 - }
197 - showProgressBar := !quiet && size >= progressBarMinSize
197 + showProgressBar := !quiet
198
199 var bar *pb.ProgressBar
200 var terminalWidth int
201 if showProgressBar {
202 - bar = pb.New64(size).SetUnits(pb.U_BYTES)
202 + bar = pb.New64(0).SetUnits(pb.U_BYTES)
203 bar.ManualUpdate = true
204 bar.Start()
205
@@ -215,43 +215,61 @@ remains to be implemented.
215 bar.Update()
216 }
217
218 + var sizeChan chan int64
219 + s, found := req.Values()["size"]
220 + if found {
221 + sizeChan = s.(chan int64)
222 + }
223 +
224 lastFile := ""
225 var totalProgress, prevFiles, lastBytes int64
226
221 - for out := range outChan {
222 - output := out.(*coreunix.AddedObject)
223 - if len(output.Hash) > 0 {
224 - if showProgressBar {
225 - // clear progress bar line before we print "added x" output
226 - fmt.Fprintf(res.Stderr(), "\033[2K\r")
227 - }
228 - if quiet {
229 - fmt.Fprintf(res.Stdout(), "%s\n", output.Hash)
230 - } else {
231 - fmt.Fprintf(res.Stdout(), "added %s %s\n", output.Hash, output.Name)
227 + LOOP:
228 + for {
229 + select {
230 + case out, ok := <-outChan:
231 + if !ok {
232 + break LOOP
233 }
234 + output := out.(*coreunix.AddedObject)
235 + if len(output.Hash) > 0 {
236 + if showProgressBar {
237 + // clear progress bar line before we print "added x" output
238 + fmt.Fprintf(res.Stderr(), "\033[2K\r")
239 + }
240 + if quiet {
241 + fmt.Fprintf(res.Stdout(), "%s\n", output.Hash)
242 + } else {
243 + fmt.Fprintf(res.Stdout(), "added %s %s\n", output.Hash, output.Name)
244 + }
245
234 - } else {
235 - log.Debugf("add progress: %v %v\n", output.Name, output.Bytes)
236 -
237 - if !showProgressBar {
238 - continue
246 + } else {
247 + log.Debugf("add progress: %v %v\n", output.Name, output.Bytes)
248 +
249 + if !showProgressBar {
250 + continue
251 + }
252 +
253 + if len(lastFile) == 0 {
254 + lastFile = output.Name
255 + }
256 + if output.Name != lastFile || output.Bytes < lastBytes {
257 + prevFiles += lastBytes
258 + lastFile = output.Name
259 + }
260 + lastBytes = output.Bytes
261 + delta := prevFiles + lastBytes - totalProgress
262 + totalProgress = bar.Add64(delta)
263 }
264
241 - if len(lastFile) == 0 {
242 - lastFile = output.Name
243 - }
244 - if output.Name != lastFile || output.Bytes < lastBytes {
245 - prevFiles += lastBytes
246 - lastFile = output.Name
265 + if showProgressBar {
266 + bar.Update()
267 }
248 - lastBytes = output.Bytes
249 - delta := prevFiles + lastBytes - totalProgress
250 - totalProgress = bar.Add64(delta)
251 - }
252 -
253 - if showProgressBar {
254 - bar.Update()
268 + case size := <-sizeChan:
269 + bar.Total = size
270 + bar.ShowPercent = true
271 + bar.ShowBar = true
272 + bar.ShowTimeLeft = true
273 }
274 }
275 },