Fix loading saved hashes

This commit is contained in:
Timmy Welch 2025-02-23 12:31:09 -08:00
parent 5307b5d8df
commit 1f37684862
2 changed files with 10 additions and 17 deletions

View File

@ -20,6 +20,8 @@ type basicMapStorage struct {
pHashes []SavedHash pHashes []SavedHash
} }
var ErrIDNotFound = errors.New("ID not found on this server")
// atleast must have a read lock before using // atleast must have a read lock before using
func (b *basicMapStorage) atleast(kind goimagehash.Kind, maxDistance int, searchHash uint64) []Result { func (b *basicMapStorage) atleast(kind goimagehash.Kind, maxDistance int, searchHash uint64) []Result {
matchingHashes := make([]Result, 0, 20) // hope that we don't need more matchingHashes := make([]Result, 0, 20) // hope that we don't need more
@ -210,18 +212,17 @@ func (b *basicMapStorage) EncodeHashes() (SavedHashes, error) {
savedHashes := SavedHashes{ savedHashes := SavedHashes{
Hashes: make([]SavedHash, 0, len(b.aHashes)+len(b.dHashes)+len(b.pHashes)), Hashes: make([]SavedHash, 0, len(b.aHashes)+len(b.dHashes)+len(b.pHashes)),
} }
savedHashes.Hashes = append(savedHashes.Hashes, b.aHashes...)
savedHashes.Hashes = append(savedHashes.Hashes, b.dHashes...)
savedHashes.Hashes = append(savedHashes.Hashes, b.pHashes...)
// Only keep groups >1 as they will be mapped in SavedHashes.Hashes // Only keep groups len>1 as they are mapped in SavedHashes.Hashes
for _, ids := range b.ids { for _, ids := range b.ids {
if len(*ids) > 1 { if len(*ids) > 1 {
savedHashes.IDs = append(savedHashes.IDs, *ids) savedHashes.IDs = append(savedHashes.IDs, *ids)
} }
} }
savedHashes.Hashes = append(savedHashes.Hashes, b.aHashes...)
savedHashes.Hashes = append(savedHashes.Hashes, b.dHashes...)
savedHashes.Hashes = append(savedHashes.Hashes, b.pHashes...)
return savedHashes, nil return savedHashes, nil
} }
@ -231,8 +232,7 @@ func (b *basicMapStorage) AssociateIDs(newids []NewIDs) error {
ids, found := b.ids[newid.OldID] ids, found := b.ids[newid.OldID]
b.hashMutex.RUnlock() b.hashMutex.RUnlock()
if !found { if !found {
msg := "ID not found on this server" return ErrIDNotFound
return errors.New(msg)
} }
b.hashMutex.Lock() b.hashMutex.Lock()
*ids = InsertID(*ids, newid.NewID) *ids = InsertID(*ids, newid.NewID)

View File

@ -75,12 +75,8 @@ func (f *Format) Set(s string) error {
return nil return nil
} }
func (s *SavedHashes) InsertHash(hash Hash, id ID) { func (s *SavedHashes) InsertHash(hash SavedHash) {
h := SavedHash{ index, itemFound := slices.BinarySearchFunc(s.Hashes, hash, func(existing SavedHash, target SavedHash) int {
hash,
id,
}
index, itemFound := slices.BinarySearchFunc(s.Hashes, h, func(existing SavedHash, target SavedHash) int {
return cmp.Or( return cmp.Or(
cmp.Compare(existing.Hash.Hash, target.Hash.Hash), cmp.Compare(existing.Hash.Hash, target.Hash.Hash),
cmp.Compare(existing.Hash.Kind, target.Hash.Kind), cmp.Compare(existing.Hash.Kind, target.Hash.Kind),
@ -89,7 +85,7 @@ func (s *SavedHashes) InsertHash(hash Hash, id ID) {
) )
}) })
if !itemFound { if !itemFound {
s.Hashes = slices.Insert(s.Hashes, index, h) s.Hashes = slices.Insert(s.Hashes, index, hash)
} }
} }
@ -230,9 +226,6 @@ func DecodeHashes(format Format, hashes []byte) (*SavedHashes, error) {
if err == nil { if err == nil {
return loadedHashes, nil return loadedHashes, nil
} }
if !errors.Is(err, NoHashes) {
return nil, err
}
} }
return nil, NoHashes return nil, NoHashes