2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-17 18:45:15 +00:00

index: use backend.ID instead of string for maps

This commit is contained in:
Alexander Neumann 2015-07-26 00:06:40 +02:00
parent 3063ad1d05
commit 681d7851aa

View File

@ -15,7 +15,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[string]indexEntry pack map[backend.ID]indexEntry
} }
type indexEntry struct { type indexEntry struct {
@ -29,12 +29,12 @@ 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[string]indexEntry), pack: make(map[backend.ID]indexEntry),
} }
} }
func (idx *Index) store(t pack.BlobType, id backend.ID, pack *backend.ID, offset, length uint, old bool) { func (idx *Index) store(t pack.BlobType, id backend.ID, pack *backend.ID, offset, length uint, old bool) {
idx.pack[id.String()] = indexEntry{ idx.pack[id] = indexEntry{
tpe: t, tpe: t,
packID: pack, packID: pack,
offset: offset, offset: offset,
@ -61,9 +61,8 @@ func (idx *Index) Remove(packID backend.ID) {
debug.Log("Index.Remove", "id %v removed", packID.Str()) debug.Log("Index.Remove", "id %v removed", packID.Str())
s := packID.String() if _, ok := idx.pack[packID]; ok {
if _, ok := idx.pack[s]; ok { delete(idx.pack, packID)
delete(idx.pack, s)
} }
} }
@ -72,7 +71,7 @@ func (idx *Index) Lookup(id backend.ID) (packID *backend.ID, tpe pack.BlobType,
idx.m.Lock() idx.m.Lock()
defer idx.m.Unlock() defer idx.m.Unlock()
if p, ok := idx.pack[id.String()]; ok { if p, ok := idx.pack[id]; ok {
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)
return p.packID, p.tpe, p.offset, p.length, nil return p.packID, p.tpe, p.offset, p.length, nil
@ -138,13 +137,7 @@ func (idx *Index) Each(done chan struct{}) <-chan PackedBlob {
close(ch) close(ch)
}() }()
for ids, blob := range idx.pack { for id, blob := range idx.pack {
id, err := backend.ParseID(ids)
if err != nil {
// ignore invalid IDs
continue
}
select { select {
case <-done: case <-done:
return return
@ -181,12 +174,12 @@ func (idx *Index) Count(t pack.BlobType) (n uint) {
} }
type packJSON struct { type packJSON struct {
ID string `json:"id"` ID backend.ID `json:"id"`
Blobs []blobJSON `json:"blobs"` Blobs []blobJSON `json:"blobs"`
} }
type blobJSON struct { type blobJSON struct {
ID string `json:"id"` ID backend.ID `json:"id"`
Type pack.BlobType `json:"type"` Type pack.BlobType `json:"type"`
Offset uint `json:"offset"` Offset uint `json:"offset"`
Length uint `json:"length"` Length uint `json:"length"`
@ -197,7 +190,7 @@ type blobJSON struct {
// blobs in the index. // blobs in the index.
func (idx *Index) generatePackList(selectFn func(indexEntry) bool) ([]*packJSON, error) { func (idx *Index) generatePackList(selectFn func(indexEntry) bool) ([]*packJSON, error) {
list := []*packJSON{} list := []*packJSON{}
packs := make(map[string]*packJSON) packs := make(map[backend.ID]*packJSON)
for id, blob := range idx.pack { for id, blob := range idx.pack {
if selectFn != nil && !selectFn(blob) { if selectFn != nil && !selectFn(blob) {
@ -213,10 +206,10 @@ func (idx *Index) generatePackList(selectFn func(indexEntry) bool) ([]*packJSON,
} }
// see if pack is already in map // see if pack is already in map
p, ok := packs[blob.packID.String()] p, ok := packs[*blob.packID]
if !ok { if !ok {
// else create new pack // else create new pack
p = &packJSON{ID: blob.packID.String()} p = &packJSON{ID: *blob.packID}
// and append it to the list and map // and append it to the list and map
list = append(list, p) list = append(list, p)
@ -302,20 +295,8 @@ func DecodeIndex(rd io.Reader) (*Index, error) {
idx := NewIndex() idx := NewIndex()
for _, pack := range list { for _, pack := range list {
packID, err := backend.ParseID(pack.ID)
if err != nil {
debug.Log("Index.DecodeIndex", "error parsing pack ID %q: %v", pack.ID, err)
return nil, err
}
for _, blob := range pack.Blobs { for _, blob := range pack.Blobs {
blobID, err := backend.ParseID(blob.ID) idx.store(blob.Type, blob.ID, &pack.ID, blob.Offset, blob.Length, true)
if err != nil {
debug.Log("Index.DecodeIndex", "error parsing blob ID %q: %v", blob.ID, err)
return nil, err
}
idx.store(blob.Type, blobID, &packID, blob.Offset, blob.Length, true)
} }
} }