From 32d58c1f31c1deeb9ca846ba371739ee4939b8f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cenk=20Alt=C4=B1?= Date: Mon, 1 Nov 2021 21:37:55 -0400 Subject: [PATCH] refactor db writes --- .../resumer/boltdbresumer/boltdbresumer.go | 20 +++++++++++++------ torrent/torrent_stop.go | 12 ++--------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/internal/resumer/boltdbresumer/boltdbresumer.go b/internal/resumer/boltdbresumer/boltdbresumer.go index 27698d45..e7c2fdca 100644 --- a/internal/resumer/boltdbresumer/boltdbresumer.go +++ b/internal/resumer/boltdbresumer/boltdbresumer.go @@ -147,25 +147,33 @@ func (r *Resumer) WriteStarted(torrentID string, value bool) error { }) } -// WriteStopAfterDownload writes the start status of a torrent. -func (r *Resumer) WriteStopAfterDownload(torrentID string, value bool) error { +// HandleStopAfterDownload clears the start status and stop_after_download fields. +func (r *Resumer) HandleStopAfterDownload(torrentID string) error { return r.db.Update(func(tx *bbolt.Tx) error { b := tx.Bucket(r.bucket).Bucket([]byte(torrentID)) if b == nil { return nil } - return b.Put(Keys.StopAfterDownload, []byte(strconv.FormatBool(value))) + err := b.Put(Keys.Started, []byte(strconv.FormatBool(false))) + if err != nil { + return err + } + return b.Put(Keys.StopAfterDownload, []byte(strconv.FormatBool(false))) }) } -// WriteStopAfterMetadata writes the start status of a torrent. -func (r *Resumer) WriteStopAfterMetadata(torrentID string, value bool) error { +// HandleStopAfterMetadata clears the start status and stop_after_metadata fields. +func (r *Resumer) HandleStopAfterMetadata(torrentID string) error { return r.db.Update(func(tx *bbolt.Tx) error { b := tx.Bucket(r.bucket).Bucket([]byte(torrentID)) if b == nil { return nil } - return b.Put(Keys.StopAfterMetadata, []byte(strconv.FormatBool(value))) + err := b.Put(Keys.Started, []byte(strconv.FormatBool(false))) + if err != nil { + return err + } + return b.Put(Keys.StopAfterMetadata, []byte(strconv.FormatBool(false))) }) } diff --git a/torrent/torrent_stop.go b/torrent/torrent_stop.go index 4e60a0f9..90d58aaa 100644 --- a/torrent/torrent_stop.go +++ b/torrent/torrent_stop.go @@ -22,11 +22,7 @@ func (t *torrent) handleStopped() { } func (t *torrent) stopAndSetStoppedOnComplete() { - err := t.session.resumer.WriteStarted(t.id, false) - if err != nil { - t.log.Errorf("cannot write status to resume db: %s", err) - } - err = t.session.resumer.WriteStopAfterDownload(t.id, false) + err := t.session.resumer.HandleStopAfterDownload(t.id) if err != nil { t.log.Errorf("cannot write status to resume db: %s", err) } @@ -34,11 +30,7 @@ func (t *torrent) stopAndSetStoppedOnComplete() { } func (t *torrent) stopAndSetStoppedOnMetadata() { - err := t.session.resumer.WriteStarted(t.id, false) - if err != nil { - t.log.Errorf("cannot write status to resume db: %s", err) - } - err = t.session.resumer.WriteStopAfterMetadata(t.id, false) + err := t.session.resumer.HandleStopAfterMetadata(t.id) if err != nil { t.log.Errorf("cannot write status to resume db: %s", err) }