Refactor cache and 'cache' command

This commit is contained in:
Alexander Neumann
2015-03-14 12:30:47 +01:00
parent e1fc17aeb1
commit b17840c6ee
2 changed files with 110 additions and 78 deletions

View File

@@ -2,11 +2,8 @@ package main
import (
"fmt"
"io"
"sync"
"github.com/restic/restic"
"github.com/restic/restic/backend"
)
type CmdCache struct{}
@@ -35,87 +32,17 @@ func (cmd CmdCache) Execute(args []string) error {
return err
}
fmt.Printf("update cache, load trees\n")
list, err := s.List(backend.Tree)
if err != nil {
return err
}
cache, err := restic.NewCache(s)
if err != nil {
return err
}
treeCh := make(chan backend.ID)
worker := func(wg *sync.WaitGroup, ch chan backend.ID) {
for treeID := range ch {
cached, err := cache.Has(backend.Tree, "", treeID)
if err != nil {
fmt.Printf("tree %v cache error: %v\n", treeID.Str(), err)
continue
}
if cached {
fmt.Printf("tree %v already cached\n", treeID.Str())
continue
}
rd, err := s.GetReader(backend.Tree, treeID)
if err != nil {
fmt.Printf(" load error: %v\n", err)
continue
}
decRd, err := s.Key().DecryptFrom(rd)
if err != nil {
fmt.Printf(" store error: %v\n", err)
continue
}
wr, err := cache.Store(backend.Tree, "", treeID)
if err != nil {
fmt.Printf(" store error: %v\n", err)
continue
}
_, err = io.Copy(wr, decRd)
if err != nil {
fmt.Printf(" Copy error: %v\n", err)
continue
}
err = decRd.Close()
if err != nil {
fmt.Printf(" close error: %v\n", err)
continue
}
err = rd.Close()
if err != nil {
fmt.Printf(" close error: %v\n", err)
continue
}
fmt.Printf("tree %v stored\n", treeID.Str())
}
wg.Done()
fmt.Printf("clear cache for old snapshots\n")
err = cache.Clear(s)
if err != nil {
return err
}
var wg sync.WaitGroup
// start workers
for i := 0; i < 500; i++ {
wg.Add(1)
go worker(&wg, treeCh)
}
for _, treeID := range list {
treeCh <- treeID
}
close(treeCh)
wg.Wait()
fmt.Printf("done\n")
return nil
}