Fix decoding hashes

This commit is contained in:
Timmy Welch 2024-10-14 02:02:26 -07:00
parent f560b7f428
commit da54b3a454
4 changed files with 20 additions and 15 deletions

View File

@ -2,6 +2,7 @@ package ch
import ( import (
"cmp" "cmp"
"errors"
"fmt" "fmt"
"math/bits" "math/bits"
"slices" "slices"
@ -111,6 +112,9 @@ func (b *basicMapStorage) DecodeHashes(hashes SavedHashes) error {
b.hashes[hashType] = make([]structHash, len(sourceHashes)) b.hashes[hashType] = make([]structHash, len(sourceHashes))
for savedHash, idlistLocation := range sourceHashes { for savedHash, idlistLocation := range sourceHashes {
b.hashes[hashType] = append(b.hashes[hashType], structHash{savedHash, &hashes.IDs[idlistLocation]}) b.hashes[hashType] = append(b.hashes[hashType], structHash{savedHash, &hashes.IDs[idlistLocation]})
for _, id := range hashes.IDs[idlistLocation] {
b.ids[id] = &hashes.IDs[idlistLocation]
}
} }
} }
for hashType := range b.hashes { for hashType := range b.hashes {
@ -155,22 +159,22 @@ func (b *basicMapStorage) EncodeHashes() (SavedHashes, error) {
return hashes, nil return hashes, nil
} }
func (b *basicMapStorage) AssociateIDs(newids []NewIDs) { func (b *basicMapStorage) AssociateIDs(newids []NewIDs) error {
for _, newid := range newids { for _, newid := range newids {
ids, found := b.ids[newid.OldID] ids, found := b.ids[newid.OldID]
if !found { if !found {
msg := "No IDs belonging to " + newid.OldID.Domain + "exist on this server" msg := "No IDs belonging to " + string(newid.OldID.Domain) + " exist on this server"
panic(msg) return errors.New(msg)
} }
*ids = InsertID(*ids, newid.NewID) *ids = InsertID(*ids, newid.NewID)
} }
return nil
} }
func (b *basicMapStorage) GetIDs(id ID) IDList { func (b *basicMapStorage) GetIDs(id ID) IDList {
ids, found := b.ids[id] ids, found := b.ids[id]
if !found { if !found {
msg := "No IDs belonging to " + id.Domain + "exist on this server" return nil
panic(msg)
} }
return ToIDList(*ids) return ToIDList(*ids)
} }

View File

@ -163,7 +163,7 @@ type HashStorage interface {
MapHashes(ImageHash) MapHashes(ImageHash)
DecodeHashes(hashes SavedHashes) error DecodeHashes(hashes SavedHashes) error
EncodeHashes() (SavedHashes, error) EncodeHashes() (SavedHashes, error)
AssociateIDs(newIDs []NewIDs) AssociateIDs(newIDs []NewIDs) error
GetIDs(id ID) IDList GetIDs(id ID) IDList
} }

View File

@ -354,26 +354,26 @@ func (s *sqliteStorage) EncodeHashes() (SavedHashes, error) {
return hashes, nil return hashes, nil
} }
func (s *sqliteStorage) AssociateIDs(newIDs []NewIDs) { func (s *sqliteStorage) AssociateIDs(newIDs []NewIDs) error {
for _, ids := range newIDs { for _, ids := range newIDs {
var oldIDID, newIDID int var oldIDID, newIDID int
_, err := s.db.Exec(`INSERT INTO IDs domain,id VALUES (?,?)`, ids.NewID.Domain, ids.NewID.ID) _, err := s.db.Exec(`INSERT INTO IDs domain,id VALUES (?,?)`, ids.NewID.Domain, ids.NewID.ID)
if err != nil { if err != nil {
panic(err) return err
} }
rows, err := s.db.Query(`SELECT idid FROM IDs WHERE domain=? AND id=?`, ids.NewID.Domain, ids.NewID.ID) rows, err := s.db.Query(`SELECT idid FROM IDs WHERE domain=? AND id=?`, ids.NewID.Domain, ids.NewID.ID)
if err != nil && !errors.Is(err, sql.ErrNoRows) { if err != nil && !errors.Is(err, sql.ErrNoRows) {
panic(err) return err
} }
if rows.Next() { if rows.Next() {
rows.Scan(&newIDID) rows.Scan(&newIDID)
} else { } else {
panic("Unable to insert New ID into database") return errors.New("Unable to insert New ID into database")
} }
rows.Close() rows.Close()
rows, err = s.db.Query(`SELECT idid FROM IDs WHERE domain=? AND id=?`, ids.OldID.Domain, ids.OldID.ID) rows, err = s.db.Query(`SELECT idid FROM IDs WHERE domain=? AND id=?`, ids.OldID.Domain, ids.OldID.ID)
if err != nil && !errors.Is(err, sql.ErrNoRows) { if err != nil && !errors.Is(err, sql.ErrNoRows) {
panic(err) return err
} }
if rows.Next() { if rows.Next() {
rows.Scan(&oldIDID) rows.Scan(&oldIDID)
@ -382,16 +382,17 @@ func (s *sqliteStorage) AssociateIDs(newIDs []NewIDs) {
} }
_, err = s.db.Exec(`INSERT INTO id_hash (hashid, id_id) SELECT hashid,? FROM id_hash where id_id=?`, newIDID, oldIDID) _, err = s.db.Exec(`INSERT INTO id_hash (hashid, id_id) SELECT hashid,? FROM id_hash where id_id=?`, newIDID, oldIDID)
if err != nil { if err != nil {
panic(err) return fmt.Errorf("Unable to associate IDs: %w", err)
} }
} }
return nil
} }
func (s *sqliteStorage) GetIDs(id ID) IDList { func (s *sqliteStorage) GetIDs(id ID) IDList {
var idid int var idid int
rows, err := s.db.Query(`SELECT idid FROM IDs WHERE domain=? AND id=?`, id.Domain, id.ID) rows, err := s.db.Query(`SELECT idid FROM IDs WHERE domain=? AND id=?`, id.Domain, id.ID)
if err != nil && !errors.Is(err, sql.ErrNoRows) { if err != nil && !errors.Is(err, sql.ErrNoRows) {
panic(err) return nil
} }
if rows.Next() { if rows.Next() {
rows.Scan(&idid) rows.Scan(&idid)

View File

@ -85,8 +85,8 @@ func (v *VPTree) EncodeHashes() (SavedHashes, error) {
return SavedHashes{}, errors.New("Not Implemented") return SavedHashes{}, errors.New("Not Implemented")
} }
func (v *VPTree) AssociateIDs(newIDs []NewIDs) { func (v *VPTree) AssociateIDs(newIDs []NewIDs) error {
panic("Not Implemented") return errors.New("Not Implemented")
} }
func (v *VPTree) GetIDs(id ID) IDList { func (v *VPTree) GetIDs(id ID) IDList {