Optimizations

This commit is contained in:
Ben Sarmiento
2024-05-27 10:18:51 +02:00
parent 5c5a73e6e4
commit 4ec1c4496d
6 changed files with 76 additions and 75 deletions

View File

@@ -32,24 +32,6 @@ func (t *TorrentManager) refreshTorrents() []string {
freshIDs := mapset.NewSet[string]()
freshAccessKeys := mapset.NewSet[string]()
cachedAccessKeys := mapset.NewSet[string]()
t.getTorrentFiles("data").Each(func(filePath string) bool {
torrent := t.readTorrentFromFile(filePath)
if torrent != nil {
accessKey := t.GetKey(torrent)
allTorrents.Set(accessKey, torrent)
t.assignDirectory(torrent, func(directory string) {
listing, _ := t.DirectoryMap.Get(directory)
listing.Set(accessKey, torrent)
updatedPaths.Add(fmt.Sprintf("%s/%s", directory, accessKey))
})
filename := filepath.Base(filePath)
cachedAccessKeys.Add(strings.TrimSuffix(filename, ".zurgtorrent"))
}
return false
})
for i := range instances {
wg.Add(1)
idx := i
@@ -80,15 +62,12 @@ func (t *TorrentManager) refreshTorrents() []string {
updatedPaths.Add(fmt.Sprintf("%s/%s", directory, accessKey))
})
t.writeTorrentToFile(torrent)
} else if !mainTorrent.DownloadedIDs.Contains(tInfo.ID) {
forMerging = torrent
}
// write to file if it is a new torrent
if forMerging == nil && !cachedAccessKeys.Contains(accessKey) {
t.writeTorrentToFile(torrent)
}
mergeChan <- forMerging
})
}
@@ -137,20 +116,15 @@ func (t *TorrentManager) refreshTorrents() []string {
t.log.Infof("Compiled into %d torrents, %d were missing info", allTorrents.Count(), noInfoCount)
existingIDs := mapset.NewSet[string]()
// delete info files that are no longer present
t.getInfoFiles().Each(func(path string) bool {
path = filepath.Base(path)
torrentID := strings.TrimSuffix(path, ".zurginfo")
if !t.binOnceDone(torrentID) {
existingIDs.Add(torrentID)
if !t.binOnceDone(torrentID) && !freshIDs.Contains(torrentID) {
t.deleteInfoFile(torrentID)
}
return false
})
existingIDs.Difference(freshIDs).Each(func(id string) bool {
// t.log.Infof("Deleting stale info file %s", id)
t.deleteInfoFile(id)
return false
})
return updatedPaths.ToSlice()
}