2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-22 21:05:10 +00:00

Revert "index: remove redundant storage of indexmap size"

This reverts commit f1c388c623.

For an uninitialized indexmap the returned size was `-1` which is
unexpected and could cause problems.
This commit is contained in:
Michael Eischer 2023-06-08 18:07:06 +02:00
parent ac1dfc99bb
commit 55c21846b1

View File

@ -2,7 +2,6 @@ package index
import ( import (
"hash/maphash" "hash/maphash"
"math"
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
) )
@ -19,6 +18,7 @@ import (
type indexMap struct { type indexMap struct {
// The number of buckets is always a power of two and never zero. // The number of buckets is always a power of two and never zero.
buckets []uint buckets []uint
numentries uint
mh maphash.Hash mh maphash.Hash
@ -34,9 +34,9 @@ const (
// using id as the key. // using id as the key.
func (m *indexMap) add(id restic.ID, packIdx int, offset, length uint32, uncompressedLength uint32) { func (m *indexMap) add(id restic.ID, packIdx int, offset, length uint32, uncompressedLength uint32) {
switch { switch {
case m.len() == math.MaxUint: // Lazy initialization. case m.numentries == 0: // Lazy initialization.
m.init() m.init()
case m.len() >= maxLoad*uint(len(m.buckets)): case m.numentries >= maxLoad*uint(len(m.buckets)):
m.grow() m.grow()
} }
@ -50,6 +50,7 @@ func (m *indexMap) add(id restic.ID, packIdx int, offset, length uint32, uncompr
e.uncompressedLength = uncompressedLength e.uncompressedLength = uncompressedLength
m.buckets[h] = idx m.buckets[h] = idx
m.numentries++
} }
// foreach calls fn for all entries in the map, until fn returns false. // foreach calls fn for all entries in the map, until fn returns false.
@ -131,7 +132,7 @@ func (m *indexMap) init() {
m.newEntry() m.newEntry()
} }
func (m *indexMap) len() uint { return m.blockList.Size() - 1 } func (m *indexMap) len() uint { return m.numentries }
func (m *indexMap) newEntry() (*indexEntry, uint) { func (m *indexMap) newEntry() (*indexEntry, uint) {
return m.blockList.Alloc() return m.blockList.Alloc()