mirror of https://github.com/restic/restic.git
Add interfaces to ListAllPacks
This commit is contained in:
parent
47950b82a0
commit
3fa7304e94
|
@ -5,49 +5,9 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"restic/backend"
|
"restic/backend"
|
||||||
"restic/debug"
|
"restic/debug"
|
||||||
"restic/pack"
|
|
||||||
"restic/worker"
|
"restic/worker"
|
||||||
)
|
)
|
||||||
|
|
||||||
const rebuildIndexWorkers = 10
|
|
||||||
|
|
||||||
// ListAllPacksResult is returned in the channel from LoadBlobsFromAllPacks.
|
|
||||||
type ListAllPacksResult struct {
|
|
||||||
PackID backend.ID
|
|
||||||
Size int64
|
|
||||||
Entries []pack.Blob
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListAllPacks sends the contents of all packs to ch.
|
|
||||||
func ListAllPacks(repo *Repository, ch chan<- worker.Job, done <-chan struct{}) {
|
|
||||||
f := func(job worker.Job, done <-chan struct{}) (interface{}, error) {
|
|
||||||
packID := job.Data.(backend.ID)
|
|
||||||
entries, size, err := repo.ListPack(packID)
|
|
||||||
|
|
||||||
return ListAllPacksResult{
|
|
||||||
PackID: packID,
|
|
||||||
Size: size,
|
|
||||||
Entries: entries,
|
|
||||||
}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
jobCh := make(chan worker.Job)
|
|
||||||
wp := worker.New(rebuildIndexWorkers, f, jobCh, ch)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer close(jobCh)
|
|
||||||
for id := range repo.List(backend.Data, done) {
|
|
||||||
select {
|
|
||||||
case jobCh <- worker.Job{Data: id}:
|
|
||||||
case <-done:
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
wp.Wait()
|
|
||||||
}
|
|
||||||
|
|
||||||
// RebuildIndex lists all packs in the repo, writes a new index and removes all
|
// RebuildIndex lists all packs in the repo, writes a new index and removes all
|
||||||
// old indexes. This operation should only be done with an exclusive lock in
|
// old indexes. This operation should only be done with an exclusive lock in
|
||||||
// place.
|
// place.
|
||||||
|
|
|
@ -0,0 +1,52 @@
|
||||||
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"restic/backend"
|
||||||
|
"restic/pack"
|
||||||
|
"restic/worker"
|
||||||
|
)
|
||||||
|
|
||||||
|
const listPackWorkers = 10
|
||||||
|
|
||||||
|
// Lister combines lists packs in a repo and blobs in a pack.
|
||||||
|
type Lister interface {
|
||||||
|
List(backend.Type, <-chan struct{}) <-chan backend.ID
|
||||||
|
ListPack(backend.ID) ([]pack.Blob, int64, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListAllPacksResult is returned in the channel from LoadBlobsFromAllPacks.
|
||||||
|
type ListAllPacksResult struct {
|
||||||
|
PackID backend.ID
|
||||||
|
Size int64
|
||||||
|
Entries []pack.Blob
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListAllPacks sends the contents of all packs to ch.
|
||||||
|
func ListAllPacks(repo Lister, ch chan<- worker.Job, done <-chan struct{}) {
|
||||||
|
f := func(job worker.Job, done <-chan struct{}) (interface{}, error) {
|
||||||
|
packID := job.Data.(backend.ID)
|
||||||
|
entries, size, err := repo.ListPack(packID)
|
||||||
|
|
||||||
|
return ListAllPacksResult{
|
||||||
|
PackID: packID,
|
||||||
|
Size: size,
|
||||||
|
Entries: entries,
|
||||||
|
}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
jobCh := make(chan worker.Job)
|
||||||
|
wp := worker.New(listPackWorkers, f, jobCh, ch)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer close(jobCh)
|
||||||
|
for id := range repo.List(backend.Data, done) {
|
||||||
|
select {
|
||||||
|
case jobCh <- worker.Job{Data: id}:
|
||||||
|
case <-done:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
wp.Wait()
|
||||||
|
}
|
Loading…
Reference in New Issue