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
1 changed files with 73 additions and 61 deletions

View File

@ -18,7 +18,7 @@ import (
// Index holds a lookup table for id -> pack.
type Index struct {
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")
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.
func NewIndex() *Index {
return &Index{
pack: make(map[backend.ID]indexEntry),
pack: make(map[backend.ID][]indexEntry),
created: time.Now(),
}
}
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,
packID: blob.PackID,
offset: blob.Offset,
length: blob.Length,
}
})
}
// 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()
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",
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()
defer idx.m.Unlock()
for blobID, entry := range idx.pack {
for blobID, packList := range idx.pack {
for _, entry := range packList {
if entry.packID == id {
list = append(list, PackedBlob{
ID: blobID,
@ -165,6 +168,7 @@ func (idx *Index) ListPack(id backend.ID) (list []PackedBlob) {
})
}
}
}
return list
}
@ -257,7 +261,8 @@ func (idx *Index) Each(done chan struct{}) <-chan PackedBlob {
close(ch)
}()
for id, blob := range idx.pack {
for id, packs := range idx.pack {
for _, blob := range packs {
select {
case <-done:
return
@ -270,6 +275,7 @@ func (idx *Index) Each(done chan struct{}) <-chan PackedBlob {
}:
}
}
}
}()
return ch
@ -281,9 +287,11 @@ func (idx *Index) Packs() backend.IDSet {
defer idx.m.Unlock()
packs := backend.NewIDSet()
for _, entry := range idx.pack {
for _, list := range idx.pack {
for _, entry := range list {
packs.Insert(entry.packID)
}
}
return packs
}
@ -294,12 +302,14 @@ func (idx *Index) Count(t pack.BlobType) (n uint) {
idx.m.Lock()
defer idx.m.Unlock()
for id, blob := range idx.pack {
for id, list := range idx.pack {
for _, blob := range list {
if blob.tpe == t {
n++
debug.Log("Index.Count", " blob %v counted: %v", id.Str(), blob)
}
}
}
return
}
@ -330,7 +340,8 @@ func (idx *Index) generatePackList() ([]*packJSON, error) {
list := []*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() {
panic("null pack id")
}
@ -362,6 +373,7 @@ func (idx *Index) generatePackList() ([]*packJSON, error) {
Length: blob.length,
})
}
}
debug.Log("Index.generatePackList", "done")