Skip to content

Commit

Permalink
Remove 'stale' episodes. When cleaning remove title and description t…
Browse files Browse the repository at this point in the history
…o save space.
  • Loading branch information
dop251 committed Jun 8, 2020
1 parent 0c41b47 commit a21f8d9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
25 changes: 25 additions & 0 deletions cmd/podsync/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,33 @@ func (u *Updater) updateFeed(ctx context.Context, feedConfig *config.Feed) error

log.Debugf("received %d episode(s) for %q", len(result.Episodes), result.Title)

episodeSet := make(map[string]struct{})
if err := u.db.WalkEpisodes(ctx, feedConfig.ID, func(episode *model.Episode) error {
if episode.Status != model.EpisodeDownloaded && episode.Status != model.EpisodeCleaned {
episodeSet[episode.ID] = struct{}{}
}
return nil
}); err != nil {
return err
}

if err := u.db.AddFeed(ctx, feedConfig.ID, result); err != nil {
return err
}

for _, episode := range result.Episodes {
delete(episodeSet, episode.ID)
}

// removing episodes that are no longer available in the feed and not downloaded or cleaned
for id := range episodeSet {
log.Infof("removing episode %q", id)
err := u.db.DeleteEpisode(feedConfig.ID, id)
if err != nil {
return err
}
}

log.Debug("successfully saved updates to storage")
return nil
}
Expand Down Expand Up @@ -372,6 +395,8 @@ func (u *Updater) cleanup(ctx context.Context, feedConfig *config.Feed) error {

if err := u.db.UpdateEpisode(feedID, episode.ID, func(episode *model.Episode) error {
episode.Status = model.EpisodeCleaned
episode.Title = ""
episode.Description = ""
return nil
}); err != nil {
result = multierror.Append(result, errors.Wrapf(err, "failed to set state for cleaned episode: %s", episode.ID))
Expand Down
7 changes: 7 additions & 0 deletions pkg/db/badger.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,13 @@ func (b *Badger) UpdateEpisode(feedID string, episodeID string, cb func(episode
})
}

func (b *Badger) DeleteEpisode(feedID, episodeID string) error {
key := b.getKey(episodePath, feedID, episodeID)
return b.db.Update(func(txn *badger.Txn) error {
return txn.Delete(key)
})
}

func (b *Badger) WalkEpisodes(ctx context.Context, feedID string, cb func(episode *model.Episode) error) error {
return b.db.View(func(txn *badger.Txn) error {
return b.walkEpisodes(txn, feedID, cb)
Expand Down
3 changes: 3 additions & 0 deletions pkg/db/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type Storage interface {
// UpdateEpisode updates episode fields
UpdateEpisode(feedID string, episodeID string, cb func(episode *model.Episode) error) error

// DeleteEpisode deletes an episode
DeleteEpisode(feedID string, episodeID string) error

// WalkEpisodes iterates over episodes that belong to the given feed ID
WalkEpisodes(ctx context.Context, feedID string, cb func(episode *model.Episode) error) error
}

0 comments on commit a21f8d9

Please sign in to comment.