Fix encoding hashes

This commit is contained in:
Timmy Welch 2024-10-13 22:14:42 -07:00
parent 095c78f0e7
commit dbf03d258c

View File

@ -88,7 +88,7 @@ func (b *basicMapStorage) InsertHash(hashType int, hash uint64, ids *[]ID) {
if hashFound { if hashFound {
return return
} }
slices.Insert(b.hashes[hashType], index, structHash{hash, ids}) b.hashes[hashType] = slices.Insert(b.hashes[hashType], index, structHash{hash, ids})
} }
func (b *basicMapStorage) MapHashes(hash ImageHash) { func (b *basicMapStorage) MapHashes(hash ImageHash) {
@ -99,6 +99,7 @@ func (b *basicMapStorage) MapHashes(hash ImageHash) {
ids, ok := b.ids[hash.ID] ids, ok := b.ids[hash.ID]
if !ok { if !ok {
ids = &[]ID{hash.ID} ids = &[]ID{hash.ID}
b.ids[hash.ID] = ids
} }
b.InsertHash(hashType, ih.Hash, ids) b.InsertHash(hashType, ih.Hash, ids)
@ -129,15 +130,23 @@ func (b *basicMapStorage) printSizes() {
} }
func (b *basicMapStorage) EncodeHashes() (SavedHashes, error) { func (b *basicMapStorage) EncodeHashes() (SavedHashes, error) {
hashes := SavedHashes{} hashes := SavedHashes{
Hashes: [3]map[uint64]int{
make(map[uint64]int),
make(map[uint64]int),
make(map[uint64]int),
},
}
idmap := map[*[]ID]int{} idmap := map[*[]ID]int{}
for _, ids := range b.ids { for _, ids := range b.ids {
if _, ok := idmap[ids]; ok { if _, ok := idmap[ids]; ok {
continue continue
} }
hashes.IDs = append(hashes.IDs, *ids)
idmap[ids] = len(hashes.IDs) idmap[ids] = len(hashes.IDs)
hashes.IDs = append(hashes.IDs, *ids)
} }
for hashType, hashToID := range b.hashes { for hashType, hashToID := range b.hashes {
for _, hash := range hashToID { for _, hash := range hashToID {
hashes.Hashes[hashType][hash.hash] = idmap[hash.ids] hashes.Hashes[hashType][hash.hash] = idmap[hash.ids]
@ -169,12 +178,8 @@ func (b *basicMapStorage) GetIDs(id ID) IDList {
func NewBasicMapStorage() (HashStorage, error) { func NewBasicMapStorage() (HashStorage, error) {
storage := &basicMapStorage{ storage := &basicMapStorage{
hashMutex: sync.RWMutex{}, hashMutex: sync.RWMutex{},
ids: make(map[ID]*[]ID),
hashes: [3][]structHash{ hashes: [3][]structHash{},
[]structHash{},
[]structHash{},
[]structHash{},
},
} }
return storage, nil return storage, nil
} }