From 1f3768486212efe055cb1f6e099ad8ac9cb7abe6 Mon Sep 17 00:00:00 2001 From: Timmy Welch Date: Sun, 23 Feb 2025 12:31:09 -0800 Subject: [PATCH] Fix loading saved hashes --- BasicMap.go | 14 +++++++------- savedHashes.go | 13 +++---------- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/BasicMap.go b/BasicMap.go index be1a815..bbc460a 100644 --- a/BasicMap.go +++ b/BasicMap.go @@ -20,6 +20,8 @@ type basicMapStorage struct { pHashes []SavedHash } +var ErrIDNotFound = errors.New("ID not found on this server") + // atleast must have a read lock before using func (b *basicMapStorage) atleast(kind goimagehash.Kind, maxDistance int, searchHash uint64) []Result { matchingHashes := make([]Result, 0, 20) // hope that we don't need more @@ -210,18 +212,17 @@ func (b *basicMapStorage) EncodeHashes() (SavedHashes, error) { savedHashes := SavedHashes{ 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 { if len(*ids) > 1 { 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 } @@ -231,8 +232,7 @@ func (b *basicMapStorage) AssociateIDs(newids []NewIDs) error { ids, found := b.ids[newid.OldID] b.hashMutex.RUnlock() if !found { - msg := "ID not found on this server" - return errors.New(msg) + return ErrIDNotFound } b.hashMutex.Lock() *ids = InsertID(*ids, newid.NewID) diff --git a/savedHashes.go b/savedHashes.go index 386c04f..271c4d5 100644 --- a/savedHashes.go +++ b/savedHashes.go @@ -75,12 +75,8 @@ func (f *Format) Set(s string) error { return nil } -func (s *SavedHashes) InsertHash(hash Hash, id ID) { - h := SavedHash{ - hash, - id, - } - index, itemFound := slices.BinarySearchFunc(s.Hashes, h, func(existing SavedHash, target SavedHash) int { +func (s *SavedHashes) InsertHash(hash SavedHash) { + index, itemFound := slices.BinarySearchFunc(s.Hashes, hash, func(existing SavedHash, target SavedHash) int { return cmp.Or( cmp.Compare(existing.Hash.Hash, target.Hash.Hash), cmp.Compare(existing.Hash.Kind, target.Hash.Kind), @@ -89,7 +85,7 @@ func (s *SavedHashes) InsertHash(hash Hash, id ID) { ) }) 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 { return loadedHashes, nil } - if !errors.Is(err, NoHashes) { - return nil, err - } } return nil, NoHashes