Files
zurg/internal/torrent/uncached.go
2024-07-21 02:11:32 +02:00

62 lines
1.6 KiB
Go

package torrent
import (
"fmt"
mapset "github.com/deckarep/golang-set/v2"
)
func (t *TorrentManager) GetUncachedTorrents() ([]*Torrent, error) {
t.log.Debug("Checking if torrents are still cached")
torrents, _ := t.DirectoryMap.Get(INT_ALL)
var hashGroups []mapset.Set[string]
const maxGroupSize = 399
currentGroup := mapset.NewSet[string]()
hashGroups = append(hashGroups, currentGroup)
torrents.IterCb(func(_ string, torrent *Torrent) {
if torrent.UnrepairableReason != "" {
return
}
if currentGroup.Cardinality() >= maxGroupSize {
currentGroup = mapset.NewSet[string]()
hashGroups = append(hashGroups, currentGroup)
}
currentGroup.Add(torrent.Hash)
})
var availabilityChecks = make(map[string]bool)
for i := range hashGroups {
if hashGroups[i].Cardinality() == 0 {
break
}
resp, err := t.rd.AvailabilityCheck(hashGroups[i].ToSlice())
if err != nil {
return nil, fmt.Errorf("availability check is incomplete, skipping uncached check: %v", err)
}
for hash, hosterHash := range resp {
// Check if HosterHash is a map (Variants field is used)
availabilityChecks[hash] = len(hosterHash.Variants) > 0
}
}
var uncachedTorrents []*Torrent
torrents.IterCb(func(_ string, torrent *Torrent) {
if torrent.UnrepairableReason != "" {
return
}
if _, ok := availabilityChecks[torrent.Hash]; !ok || !availabilityChecks[torrent.Hash] {
t.log.Debugf("Torrent %s is no longer cached, adding to repair list", t.GetKey(torrent))
uncachedTorrents = append(uncachedTorrents, torrent)
}
})
t.log.Debugf("Found %d torrents that are no longer cached", len(uncachedTorrents))
return uncachedTorrents, nil
}