2
2
mirror of https://github.com/octoleo/restic.git synced 2025-01-15 11:26:57 +00:00

Allow multiple entries in the index

This commit is contained in:
Alexander Neumann 2016-08-03 20:03:52 +02:00
parent 8b4d4ec25f
commit 7e732dbd2d

View File

@ -18,7 +18,7 @@ import (
// Index holds a lookup table for id -> pack. // Index holds a lookup table for id -> pack.
type Index struct { type Index struct {
m sync.Mutex m sync.Mutex
pack map[backend.ID]indexEntry pack map[backend.ID][]indexEntry
final bool // set to true for all indexes read from the backend ("finalized") final bool // set to true for all indexes read from the backend ("finalized")
id backend.ID // set to the ID of the index when it's finalized id backend.ID // set to the ID of the index when it's finalized
@ -36,18 +36,19 @@ type indexEntry struct {
// NewIndex returns a new index. // NewIndex returns a new index.
func NewIndex() *Index { func NewIndex() *Index {
return &Index{ return &Index{
pack: make(map[backend.ID]indexEntry), pack: make(map[backend.ID][]indexEntry),
created: time.Now(), created: time.Now(),
} }
} }
func (idx *Index) store(blob PackedBlob) { func (idx *Index) store(blob PackedBlob) {
idx.pack[blob.ID] = indexEntry{ list := idx.pack[blob.ID]
idx.pack[blob.ID] = append(list, indexEntry{
tpe: blob.Type, tpe: blob.Type,
packID: blob.PackID, packID: blob.PackID,
offset: blob.Offset, offset: blob.Offset,
length: blob.Length, length: blob.Length,
} })
} }
// Final returns true iff the index is already written to the repository, it is // Final returns true iff the index is already written to the repository, it is
@ -131,7 +132,8 @@ func (idx *Index) Lookup(id backend.ID) (pb PackedBlob, err error) {
idx.m.Lock() idx.m.Lock()
defer idx.m.Unlock() defer idx.m.Unlock()
if p, ok := idx.pack[id]; ok { if packs, ok := idx.pack[id]; ok {
p := packs[0]
debug.Log("Index.Lookup", "id %v found in pack %v at %d, length %d", debug.Log("Index.Lookup", "id %v found in pack %v at %d, length %d",
id.Str(), p.packID.Str(), p.offset, p.length) id.Str(), p.packID.Str(), p.offset, p.length)
@ -154,7 +156,8 @@ func (idx *Index) ListPack(id backend.ID) (list []PackedBlob) {
idx.m.Lock() idx.m.Lock()
defer idx.m.Unlock() defer idx.m.Unlock()
for blobID, entry := range idx.pack { for blobID, packList := range idx.pack {
for _, entry := range packList {
if entry.packID == id { if entry.packID == id {
list = append(list, PackedBlob{ list = append(list, PackedBlob{
ID: blobID, ID: blobID,
@ -165,6 +168,7 @@ func (idx *Index) ListPack(id backend.ID) (list []PackedBlob) {
}) })
} }
} }
}
return list return list
} }
@ -257,7 +261,8 @@ func (idx *Index) Each(done chan struct{}) <-chan PackedBlob {
close(ch) close(ch)
}() }()
for id, blob := range idx.pack { for id, packs := range idx.pack {
for _, blob := range packs {
select { select {
case <-done: case <-done:
return return
@ -270,6 +275,7 @@ func (idx *Index) Each(done chan struct{}) <-chan PackedBlob {
}: }:
} }
} }
}
}() }()
return ch return ch
@ -281,9 +287,11 @@ func (idx *Index) Packs() backend.IDSet {
defer idx.m.Unlock() defer idx.m.Unlock()
packs := backend.NewIDSet() packs := backend.NewIDSet()
for _, entry := range idx.pack { for _, list := range idx.pack {
for _, entry := range list {
packs.Insert(entry.packID) packs.Insert(entry.packID)
} }
}
return packs return packs
} }
@ -294,12 +302,14 @@ func (idx *Index) Count(t pack.BlobType) (n uint) {
idx.m.Lock() idx.m.Lock()
defer idx.m.Unlock() defer idx.m.Unlock()
for id, blob := range idx.pack { for id, list := range idx.pack {
for _, blob := range list {
if blob.tpe == t { if blob.tpe == t {
n++ n++
debug.Log("Index.Count", " blob %v counted: %v", id.Str(), blob) debug.Log("Index.Count", " blob %v counted: %v", id.Str(), blob)
} }
} }
}
return return
} }
@ -330,7 +340,8 @@ func (idx *Index) generatePackList() ([]*packJSON, error) {
list := []*packJSON{} list := []*packJSON{}
packs := make(map[backend.ID]*packJSON) packs := make(map[backend.ID]*packJSON)
for id, blob := range idx.pack { for id, packedBlobs := range idx.pack {
for _, blob := range packedBlobs {
if blob.packID.IsNull() { if blob.packID.IsNull() {
panic("null pack id") panic("null pack id")
} }
@ -362,6 +373,7 @@ func (idx *Index) generatePackList() ([]*packJSON, error) {
Length: blob.length, Length: blob.length,
}) })
} }
}
debug.Log("Index.generatePackList", "done") debug.Log("Index.generatePackList", "done")