github.com/advanderveer/restic@v0.8.1-0.20171209104529-42a8c19aaea6/internal/list/list.go (about)

     1  package list
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/restic/restic/internal/restic"
     7  	"github.com/restic/restic/internal/worker"
     8  )
     9  
    10  const listPackWorkers = 10
    11  
    12  // Lister combines lists packs in a repo and blobs in a pack.
    13  type Lister interface {
    14  	List(context.Context, restic.FileType) <-chan restic.ID
    15  	ListPack(context.Context, restic.ID) ([]restic.Blob, int64, error)
    16  }
    17  
    18  // Result is returned in the channel from LoadBlobsFromAllPacks.
    19  type Result struct {
    20  	packID  restic.ID
    21  	size    int64
    22  	entries []restic.Blob
    23  }
    24  
    25  // PackID returns the pack ID of this result.
    26  func (l Result) PackID() restic.ID {
    27  	return l.packID
    28  }
    29  
    30  // Size returns the size of the pack.
    31  func (l Result) Size() int64 {
    32  	return l.size
    33  }
    34  
    35  // Entries returns a list of all blobs saved in the pack.
    36  func (l Result) Entries() []restic.Blob {
    37  	return l.entries
    38  }
    39  
    40  // AllPacks sends the contents of all packs to ch.
    41  func AllPacks(ctx context.Context, repo Lister, ignorePacks restic.IDSet, ch chan<- worker.Job) {
    42  	f := func(ctx context.Context, job worker.Job) (interface{}, error) {
    43  		packID := job.Data.(restic.ID)
    44  		entries, size, err := repo.ListPack(ctx, packID)
    45  
    46  		return Result{
    47  			packID:  packID,
    48  			size:    size,
    49  			entries: entries,
    50  		}, err
    51  	}
    52  
    53  	jobCh := make(chan worker.Job)
    54  	wp := worker.New(ctx, listPackWorkers, f, jobCh, ch)
    55  
    56  	go func() {
    57  		defer close(jobCh)
    58  		for id := range repo.List(ctx, restic.DataFile) {
    59  			if ignorePacks.Has(id) {
    60  				continue
    61  			}
    62  
    63  			select {
    64  			case jobCh <- worker.Job{Data: id}:
    65  			case <-ctx.Done():
    66  				return
    67  			}
    68  		}
    69  	}()
    70  
    71  	wp.Wait()
    72  }