2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-23 11:28:54 +00:00
restic/internal/repository/master_index.go

267 lines
5.7 KiB
Go
Raw Normal View History

2015-10-12 20:34:12 +00:00
package repository
import (
2017-06-18 12:45:02 +00:00
"context"
2015-10-12 20:34:12 +00:00
"sync"
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/errors"
2017-07-24 15:42:25 +00:00
"github.com/restic/restic/internal/restic"
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/debug"
2015-10-12 20:34:12 +00:00
)
// MasterIndex is a collection of indexes and IDs of chunks that are in the process of being saved.
type MasterIndex struct {
idx []*Index
idxMutex sync.RWMutex
}
// NewMasterIndex creates a new master index.
func NewMasterIndex() *MasterIndex {
return &MasterIndex{}
2015-10-12 20:34:12 +00:00
}
// Lookup queries all known Indexes for the ID and returns the first match.
2016-08-31 20:39:36 +00:00
func (mi *MasterIndex) Lookup(id restic.ID, tpe restic.BlobType) (blobs []restic.PackedBlob, err error) {
2015-10-12 20:34:12 +00:00
mi.idxMutex.RLock()
defer mi.idxMutex.RUnlock()
2016-09-27 20:35:08 +00:00
debug.Log("looking up id %v, tpe %v", id.Str(), tpe)
2015-10-12 20:34:12 +00:00
for _, idx := range mi.idx {
blobs, err = idx.Lookup(id, tpe)
2015-10-12 20:34:12 +00:00
if err == nil {
2017-01-13 11:22:42 +00:00
debug.Log("found id %v: %v", id.Str(), blobs)
2015-10-12 20:34:12 +00:00
return
}
}
2016-09-27 20:35:08 +00:00
debug.Log("id %v not found in any index", id.Str())
return nil, errors.Errorf("id %v not found in any index", id)
2015-10-12 20:34:12 +00:00
}
// LookupSize queries all known Indexes for the ID and returns the first match.
2016-08-31 18:58:57 +00:00
func (mi *MasterIndex) LookupSize(id restic.ID, tpe restic.BlobType) (uint, error) {
2015-10-12 20:34:12 +00:00
mi.idxMutex.RLock()
defer mi.idxMutex.RUnlock()
for _, idx := range mi.idx {
2017-01-12 21:14:31 +00:00
if idx.Has(id, tpe) {
return idx.LookupSize(id, tpe)
2015-10-12 20:34:12 +00:00
}
}
return 0, errors.Errorf("id %v not found in any index", id)
2015-10-12 20:34:12 +00:00
}
2015-11-01 21:32:28 +00:00
// ListPack returns the list of blobs in a pack. The first matching index is
// returned, or nil if no index contains information about the pack id.
2016-08-31 20:39:36 +00:00
func (mi *MasterIndex) ListPack(id restic.ID) (list []restic.PackedBlob) {
2015-11-01 21:32:28 +00:00
mi.idxMutex.RLock()
defer mi.idxMutex.RUnlock()
for _, idx := range mi.idx {
list := idx.ListPack(id)
if len(list) > 0 {
return list
}
}
return nil
}
2015-10-12 20:34:12 +00:00
// Has queries all known Indexes for the ID and returns the first match.
2016-08-31 18:58:57 +00:00
func (mi *MasterIndex) Has(id restic.ID, tpe restic.BlobType) bool {
2015-10-12 20:34:12 +00:00
mi.idxMutex.RLock()
defer mi.idxMutex.RUnlock()
for _, idx := range mi.idx {
if idx.Has(id, tpe) {
2015-10-12 20:34:12 +00:00
return true
}
}
return false
}
// Count returns the number of blobs of type t in the index.
2016-08-31 18:58:57 +00:00
func (mi *MasterIndex) Count(t restic.BlobType) (n uint) {
2015-10-12 20:34:12 +00:00
mi.idxMutex.RLock()
defer mi.idxMutex.RUnlock()
var sum uint
for _, idx := range mi.idx {
sum += idx.Count(t)
}
return sum
}
// Insert adds a new index to the MasterIndex.
func (mi *MasterIndex) Insert(idx *Index) {
mi.idxMutex.Lock()
defer mi.idxMutex.Unlock()
mi.idx = append(mi.idx, idx)
}
// Remove deletes an index from the MasterIndex.
func (mi *MasterIndex) Remove(index *Index) {
mi.idxMutex.Lock()
defer mi.idxMutex.Unlock()
for i, idx := range mi.idx {
if idx == index {
mi.idx = append(mi.idx[:i], mi.idx[i+1:]...)
return
}
}
}
// Store remembers the id and pack in the index.
func (mi *MasterIndex) Store(pb restic.PackedBlob) {
2015-10-12 20:34:12 +00:00
mi.idxMutex.RLock()
for _, idx := range mi.idx {
if !idx.Final() {
mi.idxMutex.RUnlock()
idx.Store(pb)
return
2015-10-12 20:34:12 +00:00
}
}
mi.idxMutex.RUnlock()
mi.idxMutex.Lock()
defer mi.idxMutex.Unlock()
newIdx := NewIndex()
newIdx.Store(pb)
2015-10-12 20:34:12 +00:00
mi.idx = append(mi.idx, newIdx)
}
// NotFinalIndexes returns all indexes that have not yet been saved.
func (mi *MasterIndex) NotFinalIndexes() []*Index {
mi.idxMutex.Lock()
defer mi.idxMutex.Unlock()
var list []*Index
for _, idx := range mi.idx {
if !idx.Final() {
list = append(list, idx)
}
}
2016-09-27 20:35:08 +00:00
debug.Log("return %d indexes", len(list))
2015-10-12 21:59:17 +00:00
return list
}
// FullIndexes returns all indexes that are full.
func (mi *MasterIndex) FullIndexes() []*Index {
mi.idxMutex.Lock()
defer mi.idxMutex.Unlock()
var list []*Index
2016-09-27 20:35:08 +00:00
debug.Log("checking %d indexes", len(mi.idx))
2015-10-12 21:59:17 +00:00
for _, idx := range mi.idx {
if idx.Final() {
2016-09-27 20:35:08 +00:00
debug.Log("index %p is final", idx)
2015-10-12 21:59:17 +00:00
continue
}
if IndexFull(idx) {
2016-09-27 20:35:08 +00:00
debug.Log("index %p is full", idx)
2015-10-12 21:59:17 +00:00
list = append(list, idx)
} else {
2016-09-27 20:35:08 +00:00
debug.Log("index %p not full", idx)
2015-10-12 21:59:17 +00:00
}
}
2016-09-27 20:35:08 +00:00
debug.Log("return %d indexes", len(list))
2015-10-12 20:34:12 +00:00
return list
}
// All returns all indexes.
func (mi *MasterIndex) All() []*Index {
mi.idxMutex.Lock()
defer mi.idxMutex.Unlock()
return mi.idx
}
2015-11-02 18:28:30 +00:00
2017-06-18 12:45:02 +00:00
// Each returns a channel that yields all blobs known to the index. When the
// context is cancelled, the background goroutine terminates. This blocks any
// modification of the index.
func (mi *MasterIndex) Each(ctx context.Context) <-chan restic.PackedBlob {
mi.idxMutex.RLock()
ch := make(chan restic.PackedBlob)
go func() {
defer mi.idxMutex.RUnlock()
defer func() {
close(ch)
}()
for _, idx := range mi.idx {
idxCh := idx.Each(ctx)
for pb := range idxCh {
select {
case <-ctx.Done():
return
case ch <- pb:
}
}
}
}()
return ch
}
2015-11-02 18:28:30 +00:00
// RebuildIndex combines all known indexes to a new index, leaving out any
// packs whose ID is contained in packBlacklist. The new index contains the IDs
// of all known indexes in the "supersedes" field.
2016-08-31 18:29:54 +00:00
func (mi *MasterIndex) RebuildIndex(packBlacklist restic.IDSet) (*Index, error) {
2015-11-02 18:28:30 +00:00
mi.idxMutex.Lock()
defer mi.idxMutex.Unlock()
2016-09-27 20:35:08 +00:00
debug.Log("start rebuilding index of %d indexes, pack blacklist: %v", len(mi.idx), packBlacklist)
2015-11-02 18:28:30 +00:00
newIndex := NewIndex()
2017-06-18 12:45:02 +00:00
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
2015-11-02 18:28:30 +00:00
for i, idx := range mi.idx {
2016-09-27 20:35:08 +00:00
debug.Log("adding index %d", i)
2015-11-02 18:28:30 +00:00
2017-06-18 12:45:02 +00:00
for pb := range idx.Each(ctx) {
2015-11-02 18:28:30 +00:00
if packBlacklist.Has(pb.PackID) {
continue
}
newIndex.Store(pb)
}
if !idx.Final() {
2016-09-27 20:35:08 +00:00
debug.Log("index %d isn't final, don't add to supersedes field", i)
2015-11-02 18:28:30 +00:00
continue
}
id, err := idx.ID()
if err != nil {
2016-09-27 20:35:08 +00:00
debug.Log("index %d does not have an ID: %v", err)
2015-11-02 18:28:30 +00:00
return nil, err
}
2016-09-27 20:35:08 +00:00
debug.Log("adding index id %v to supersedes field", id.Str())
2015-11-02 18:28:30 +00:00
err = newIndex.AddToSupersedes(id)
if err != nil {
return nil, err
}
}
return newIndex, nil
}