Export ListAllPacks

This commit is contained in:
Alexander Neumann 2016-08-04 21:21:32 +02:00
parent acc2fa5816
commit 2b1b6d8c2a
1 changed files with 19 additions and 14 deletions

View File

@ -11,19 +11,20 @@ import (
const rebuildIndexWorkers = 10 const rebuildIndexWorkers = 10
type loadBlobsResult struct { // LoadBlobsResult is returned in the channel from LoadBlobsFromAllPacks.
packID backend.ID type LoadBlobsResult struct {
entries []pack.Blob PackID backend.ID
Entries []pack.Blob
} }
// loadBlobsFromAllPacks sends the contents of all packs to ch. // ListAllPacks sends the contents of all packs to ch.
func loadBlobsFromAllPacks(repo *Repository, ch chan<- worker.Job, done <-chan struct{}) { func ListAllPacks(repo *Repository, ch chan<- worker.Job, done <-chan struct{}) {
f := func(job worker.Job, done <-chan struct{}) (interface{}, error) { f := func(job worker.Job, done <-chan struct{}) (interface{}, error) {
packID := job.Data.(backend.ID) packID := job.Data.(backend.ID)
entries, err := repo.ListPack(packID) entries, err := repo.ListPack(packID)
return loadBlobsResult{ return LoadBlobsResult{
packID: packID, PackID: packID,
entries: entries, Entries: entries,
}, err }, err
} }
@ -31,10 +32,14 @@ func loadBlobsFromAllPacks(repo *Repository, ch chan<- worker.Job, done <-chan s
wp := worker.New(rebuildIndexWorkers, f, jobCh, ch) wp := worker.New(rebuildIndexWorkers, f, jobCh, ch)
go func() { go func() {
defer close(jobCh)
for id := range repo.List(backend.Data, done) { for id := range repo.List(backend.Data, done) {
jobCh <- worker.Job{Data: id} select {
case jobCh <- worker.Job{Data: id}:
case <-done:
return
}
} }
close(jobCh)
}() }()
wp.Wait() wp.Wait()
@ -50,7 +55,7 @@ func RebuildIndex(repo *Repository) error {
defer close(done) defer close(done)
ch := make(chan worker.Job) ch := make(chan worker.Job)
go loadBlobsFromAllPacks(repo, ch, done) go ListAllPacks(repo, ch, done)
idx := NewIndex() idx := NewIndex()
for job := range ch { for job := range ch {
@ -61,15 +66,15 @@ func RebuildIndex(repo *Repository) error {
continue continue
} }
res := job.Result.(loadBlobsResult) res := job.Result.(LoadBlobsResult)
for _, entry := range res.entries { for _, entry := range res.Entries {
pb := PackedBlob{ pb := PackedBlob{
ID: entry.ID, ID: entry.ID,
Type: entry.Type, Type: entry.Type,
Length: entry.Length, Length: entry.Length,
Offset: entry.Offset, Offset: entry.Offset,
PackID: res.packID, PackID: res.PackID,
} }
idx.Store(pb) idx.Store(pb)
} }