From 062c3ac182aa76b49588c452cdade788787b6706 Mon Sep 17 00:00:00 2001 From: Mechiel Lukkien Date: Wed, 26 Feb 2025 15:05:25 +0100 Subject: [PATCH] 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. --- junk/filter.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/junk/filter.go b/junk/filter.go index 494d494..f2bb6ce 100644 --- a/junk/filter.go +++ b/junk/filter.go @@ -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