Fix directory mapping

This commit is contained in:
Ben Sarmiento
2023-10-25 17:59:32 +02:00
parent 800401729a
commit 9cffb9fc86
6 changed files with 112 additions and 42 deletions

View File

@@ -16,13 +16,15 @@ import (
)
type TorrentManager struct {
requiredVersion string
torrents []Torrent
inProgress []string
checksum string
config config.ConfigInterface
cache *expirable.LRU[string, string]
workerPool chan bool
requiredVersion string
torrents []Torrent
inProgress []string
checksum string
config config.ConfigInterface
cache *expirable.LRU[string, string]
workerPool chan bool
TorrentDirectoriesMap map[string][]string
processedTorrents map[string]bool
}
// NewTorrentManager creates a new torrent manager
@@ -30,10 +32,12 @@ type TorrentManager struct {
// and store them in-memory
func NewTorrentManager(config config.ConfigInterface, cache *expirable.LRU[string, string]) *TorrentManager {
t := &TorrentManager{
requiredVersion: "24.10.2023",
config: config,
cache: cache,
workerPool: make(chan bool, config.GetNumOfWorkers()),
requiredVersion: "24.10.2023",
config: config,
cache: cache,
workerPool: make(chan bool, config.GetNumOfWorkers()),
TorrentDirectoriesMap: make(map[string][]string),
processedTorrents: make(map[string]bool),
}
// Initialize torrents for the first time
@@ -85,7 +89,7 @@ func (t *TorrentManager) repairAll(wg *sync.WaitGroup) {
func (t *TorrentManager) GetByDirectory(directory string) []Torrent {
var torrents []Torrent
for i := range t.torrents {
for _, dir := range t.torrents[i].Directories {
for _, dir := range t.TorrentDirectoriesMap[t.torrents[i].Name] {
if dir == directory {
torrents = append(torrents, t.torrents[i])
}
@@ -567,35 +571,58 @@ func (t *TorrentManager) repair(torrentID string, selectedFiles []File, tryReins
}
func (t *TorrentManager) mapToDirectories() {
// Map to directories
version := t.config.GetVersion()
if version == "v1" {
// Map torrents to directories
switch t.config.GetVersion() {
case "v1":
configV1 := t.config.(*config.ZurgConfigV1)
groupMap := configV1.GetGroupMap()
// for every group, iterate over every torrent
// and then sprinkle/distribute the torrents to the directories of the group
for group, directories := range groupMap {
log.Printf("Processing directory group: %s\n", group)
var directoryMap = make(map[string]int)
log.Printf("Processing directory group '%s', sequence: %s\n", group, strings.Join(directories, " > "))
counter := make(map[string]int)
for i := range t.torrents {
// don't process torrents that are already mapped if it is not the first run
if _, exists := t.processedTorrents[t.torrents[i].ID]; exists {
continue
}
for _, directory := range directories {
if configV1.MeetsConditions(directory, t.torrents[i].ID, t.torrents[i].Name) {
// append to t.torrents[i].Directories if not yet there
var filenames []string
for _, file := range t.torrents[i].Files {
filenames = append(filenames, file.Path)
}
if configV1.MeetsConditions(directory, t.torrents[i].ID, t.torrents[i].Name, filenames) {
found := false
for _, dir := range t.torrents[i].Directories {
for _, dir := range t.TorrentDirectoriesMap[t.torrents[i].Name] {
if dir == directory {
found = true
break
}
}
if !found {
t.torrents[i].Directories = append(t.torrents[i].Directories, directory)
counter[directory]++
t.TorrentDirectoriesMap[t.torrents[i].Name] = append(t.TorrentDirectoriesMap[t.torrents[i].Name], directory)
break
}
directoryMap[directory]++
break
}
}
}
log.Printf("Directory group: %v\n", directoryMap)
sum := 0
for _, count := range counter {
sum += count
}
if sum > 0 {
log.Printf("Directory group processed: %s %v %d\n", group, counter, sum)
} else {
log.Println("No new additions to directory group", group)
}
}
default:
log.Println("Unknown config version")
}
for _, torrent := range t.torrents {
t.processedTorrents[torrent.ID] = true
}
log.Println("Finished mapping to directories")
}