when writing updated word counts to the junk filter, remove entries where both counts are 0

no point in keeping them around.
also pass on error when getting a word from database returned an error.
This commit is contained in:
Mechiel Lukkien 2025-02-26 15:05:25 +01:00
parent 394bdef39d
commit 062c3ac182
No known key found for this signature in database

View File

@ -288,17 +288,28 @@ func (f *Filter) Save() error {
}
err := f.db.Write(context.Background(), func(tx *bstore.Tx) error {
update := func(w string, ham, spam uint32) error {
zeroword := w != "-" && ham == 0 && spam == 0
if f.isNew {
if zeroword {
return nil
}
return tx.Insert(&Wordscore{w, ham, spam})
}
wc := Wordscore{w, 0, 0}
err := tx.Get(&wc)
if err == bstore.ErrAbsent {
if zeroword {
return nil
}
return tx.Insert(&Wordscore{w, ham, spam})
} else if err != nil {
return err
}
if zeroword {
return tx.Delete(&Wordscore{w, 0, 0})
}
return tx.Update(&Wordscore{w, ham, spam})
}
if err := update("-", f.hams, f.spams); err != nil {
@ -334,6 +345,8 @@ func loadWords(ctx context.Context, db *bstore.DB, l []string, dst map[string]wo
wc := Wordscore{Word: w}
if err := tx.Get(&wc); err == nil {
dst[w] = word{wc.Ham, wc.Spam}
} else if err != bstore.ErrAbsent {
return fmt.Errorf("get word: %v", err)
}
}
return nil