diff --git a/go.mod b/go.mod index d503abb..3de43d0 100644 --- a/go.mod +++ b/go.mod @@ -10,10 +10,10 @@ require ( require github.com/orcaman/concurrent-map/v2 v2.0.1 require ( + github.com/deckarep/golang-set/v2 v2.6.0 github.com/json-iterator/go v1.1.12 github.com/julienschmidt/httprouter v1.3.0 github.com/panjf2000/ants/v2 v2.8.2 - github.com/scylladb/go-set v1.0.2 ) require ( diff --git a/go.sum b/go.sum index 86a4869..30eaa40 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/fatih/set v0.2.1 h1:nn2CaJyknWE/6txyUDGwysr3G5QC6xWB/PtVjPBbeaA= -github.com/fatih/set v0.2.1/go.mod h1:+RKtMCH+favT2+3YecHGxcc0b4KyVWA1QWWJUs4E0CI= +github.com/deckarep/golang-set/v2 v2.6.0 h1:XfcQbWM1LlMB8BsJ8N9vW5ehnnPVIw0je80NsVHagjM= +github.com/deckarep/golang-set/v2 v2.6.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= @@ -22,8 +22,6 @@ github.com/panjf2000/ants/v2 v2.8.2/go.mod h1:7ZxyxsqE4vvW0M7LSD8aI3cKwgFhBHbxnl github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/scylladb/go-set v1.0.2 h1:SkvlMCKhP0wyyct6j+0IHJkBkSZL+TDzZ4E7f7BCcRE= -github.com/scylladb/go-set v1.0.2/go.mod h1:DkpGd78rljTxKAnTDPFqXSGxvETQnJyuSOQwsHycqfs= github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= diff --git a/internal/torrent/manager.go b/internal/torrent/manager.go index ed59577..70e8095 100644 --- a/internal/torrent/manager.go +++ b/internal/torrent/manager.go @@ -9,10 +9,9 @@ import ( "github.com/debridmediamanager/zurg/internal/config" "github.com/debridmediamanager/zurg/pkg/logutil" "github.com/debridmediamanager/zurg/pkg/realdebrid" + mapset "github.com/deckarep/golang-set/v2" cmap "github.com/orcaman/concurrent-map/v2" "github.com/panjf2000/ants/v2" - "github.com/scylladb/go-set" - "github.com/scylladb/go-set/strset" ) const ( @@ -25,7 +24,7 @@ type TorrentManager struct { Api *realdebrid.RealDebrid DirectoryMap cmap.ConcurrentMap[string, cmap.ConcurrentMap[string, *Torrent]] // directory -> accessKey -> Torrent DownloadCache cmap.ConcurrentMap[string, *realdebrid.Download] - allAccessKeys *strset.Set + allAccessKeys mapset.Set[string] latestState *LibraryState requiredVersion string workerPool *ants.Pool @@ -42,7 +41,7 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, p t := &TorrentManager{ Config: cfg, Api: api, - allAccessKeys: set.NewStringSet(), + allAccessKeys: mapset.NewSet[string](), latestState: &initialSate, requiredVersion: "06.12.2023", workerPool: p, @@ -120,7 +119,7 @@ func (t *TorrentManager) TriggerHookOnLibraryUpdate(updatedPaths []string) { } func (t *TorrentManager) assignedDirectoryCb(tor *Torrent, cb func(string)) { - torrentIDs := strset.Union(tor.DownloadedIDs, tor.InProgressIDs).List() + torrentIDs := tor.DownloadedIDs.Union(tor.InProgressIDs).ToSlice() // get filenames needed for directory conditions var filenames []string var fileSizes []int64 @@ -200,7 +199,7 @@ func (t *TorrentManager) readTorrentFromFile(torrentID string) *Torrent { if err := json.Unmarshal(jsonData, &torrent); err != nil { return nil } - if strset.Union(torrent.DownloadedIDs, torrent.InProgressIDs).IsEmpty() { + if torrent.DownloadedIDs.Union(torrent.InProgressIDs).IsEmpty() { t.log.Fatal("Torrent has no downloaded or in progress ids") } if torrent.Version != t.requiredVersion { diff --git a/internal/torrent/refresh.go b/internal/torrent/refresh.go index f6f976c..7fb73e2 100644 --- a/internal/torrent/refresh.go +++ b/internal/torrent/refresh.go @@ -8,9 +8,8 @@ import ( "time" "github.com/debridmediamanager/zurg/pkg/realdebrid" + mapset "github.com/deckarep/golang-set/v2" cmap "github.com/orcaman/concurrent-map/v2" - "github.com/scylladb/go-set" - "github.com/scylladb/go-set/strset" ) func (t *TorrentManager) RefreshTorrents() []string { @@ -35,7 +34,7 @@ func (t *TorrentManager) RefreshTorrents() []string { close(infoChan) t.log.Debugf("Fetched info for %d torrents", len(instances)) - freshKeys := set.NewStringSet() + freshKeys := mapset.NewSet[string]() allTorrents, _ := t.DirectoryMap.Get(INT_ALL) noInfoCount := 0 for info := range infoChan { @@ -48,7 +47,7 @@ func (t *TorrentManager) RefreshTorrents() []string { } if torrent, exists := allTorrents.Get(info.AccessKey); !exists { allTorrents.Set(info.AccessKey, info) - } else if !strset.Difference(info.DownloadedIDs, torrent.DownloadedIDs).IsEmpty() { + } else if !info.DownloadedIDs.Difference(torrent.DownloadedIDs).IsEmpty() { mainTorrent := t.mergeToMain(torrent, info) allTorrents.Set(info.AccessKey, &mainTorrent) } @@ -56,7 +55,7 @@ func (t *TorrentManager) RefreshTorrents() []string { t.log.Infof("Compiled into %d torrents, %d were missing info", allTorrents.Count(), noInfoCount) var updatedPaths []string // torrents yet to be assigned in a directory - strset.Difference(freshKeys, t.allAccessKeys).Each(func(accessKey string) bool { + freshKeys.Difference(t.allAccessKeys).Each(func(accessKey string) bool { // assign to directories tor, ok := allTorrents.Get(accessKey) if !ok { @@ -79,8 +78,7 @@ func (t *TorrentManager) RefreshTorrents() []string { return true }) // removed torrents - // items which are in in t.allAccessKeys but not in freshKeys - strset.Difference(t.allAccessKeys, freshKeys).Each(func(accessKey string) bool { + t.allAccessKeys.Difference(freshKeys).Each(func(accessKey string) bool { t.Delete(accessKey, false) return true }) @@ -188,8 +186,8 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent { torrent.SelectedFiles.Set(filepath.Base(file.Path), file) } } - torrent.DownloadedIDs = strset.New() - torrent.InProgressIDs = strset.New() + torrent.DownloadedIDs = mapset.NewSet[string]() + torrent.InProgressIDs = mapset.NewSet[string]() if info.Progress == 100 { torrent.DownloadedIDs.Add(info.ID) } else { @@ -207,11 +205,11 @@ func (t *TorrentManager) mergeToMain(existing, toMerge *Torrent) Torrent { mainTorrent.AccessKey = existing.AccessKey mainTorrent.Hash = existing.Hash - mainTorrent.DownloadedIDs = strset.New() - mainTorrent.InProgressIDs = strset.New() + mainTorrent.DownloadedIDs = mapset.NewSet[string]() + mainTorrent.InProgressIDs = mapset.NewSet[string]() // this function triggers only when we have a new DownloadedID - strset.Difference(toMerge.DownloadedIDs, existing.DownloadedIDs).Each(func(id string) bool { + toMerge.DownloadedIDs.Difference(existing.DownloadedIDs).Each(func(id string) bool { mainTorrent.DownloadedIDs.Add(id) mainTorrent.InProgressIDs.Remove(id) return true diff --git a/internal/torrent/repair.go b/internal/torrent/repair.go index 0996888..e6990ca 100644 --- a/internal/torrent/repair.go +++ b/internal/torrent/repair.go @@ -117,7 +117,7 @@ func (t *TorrentManager) reinsertTorrent(torrent *Torrent, missingFiles string) // if missingFiles is not provided if missingFiles == "" { // only replace the torrent if we are reinserting all files - oldTorrentIDs = torrent.DownloadedIDs.List() + oldTorrentIDs = torrent.DownloadedIDs.ToSlice() tmpSelection := "" torrent.SelectedFiles.IterCb(func(_ string, file *File) { tmpSelection += fmt.Sprintf("%d,", file.ID) // select all files diff --git a/internal/torrent/types.go b/internal/torrent/types.go index 3909769..a3ba6e3 100644 --- a/internal/torrent/types.go +++ b/internal/torrent/types.go @@ -6,9 +6,9 @@ import ( "time" "github.com/debridmediamanager/zurg/pkg/realdebrid" + mapset "github.com/deckarep/golang-set/v2" jsoniter "github.com/json-iterator/go" cmap "github.com/orcaman/concurrent-map/v2" - "github.com/scylladb/go-set/strset" ) var json = jsoniter.ConfigCompatibleWithStandardLibrary @@ -20,8 +20,8 @@ type Torrent struct { LatestAdded string `json:"LatestAdded"` ForRepair bool `json:"ForRepair"` Unfixable bool `json:"Unfixable"` - DownloadedIDs *strset.Set `json:"DownloadedIDs"` - InProgressIDs *strset.Set `json:"InProgressIDs"` + DownloadedIDs mapset.Set[string] `json:"DownloadedIDs"` + InProgressIDs mapset.Set[string] `json:"InProgressIDs"` Version string `json:"Version"` // only used for files } @@ -46,14 +46,14 @@ func (t *Torrent) MarshalJSON() ([]byte, error) { if t.DownloadedIDs.IsEmpty() { temp.DownloadedIDsJson = []byte(`""`) } else { - downloadedIDsStr := `"` + strings.Join(t.DownloadedIDs.List(), ",") + `"` + downloadedIDsStr := `"` + strings.Join(t.DownloadedIDs.ToSlice(), ",") + `"` temp.DownloadedIDsJson = []byte(downloadedIDsStr) } if t.InProgressIDs.IsEmpty() { temp.InProgressIDsJson = []byte(`""`) } else { - inProgressIDsStr := `"` + strings.Join(t.InProgressIDs.List(), ",") + `"` + inProgressIDsStr := `"` + strings.Join(t.InProgressIDs.ToSlice(), ",") + `"` temp.InProgressIDsJson = []byte(inProgressIDsStr) } @@ -83,16 +83,16 @@ func (t *Torrent) UnmarshalJSON(data []byte) error { if len(temp.DownloadedIDsJson) > 2 { downloadedIDs := strings.Split(strings.ReplaceAll(string(temp.DownloadedIDsJson), `"`, ""), ",") - t.DownloadedIDs = strset.New(downloadedIDs...) + t.DownloadedIDs = mapset.NewSet[string](downloadedIDs...) } else { - t.DownloadedIDs = strset.New() + t.DownloadedIDs = mapset.NewSet[string]() } if len(temp.InProgressIDsJson) > 2 { inProgressIDs := strings.Split(strings.ReplaceAll(string(temp.InProgressIDsJson), `"`, ""), ",") - t.InProgressIDs = strset.New(inProgressIDs...) + t.InProgressIDs = mapset.NewSet[string](inProgressIDs...) } else { - t.InProgressIDs = strset.New() + t.InProgressIDs = mapset.NewSet[string]() } return nil