Proper cached index computation
This commit is contained in:
@@ -23,21 +23,23 @@ func (rd *RealDebrid) GetTorrents(onlyOne bool) ([]Torrent, int, error) {
|
||||
return nil, 0, result.err
|
||||
}
|
||||
|
||||
totalElements := result.total
|
||||
|
||||
if onlyOne {
|
||||
return result.torrents, result.total, nil
|
||||
return result.torrents, totalElements, nil
|
||||
}
|
||||
|
||||
allTorrents := []Torrent{}
|
||||
page := 1
|
||||
pageSize := 250
|
||||
|
||||
maxPages := (result.total + pageSize - 1) / pageSize
|
||||
rd.log.Debugf("Torrents total count is %d", result.total)
|
||||
maxPages := (totalElements + pageSize - 1) / pageSize
|
||||
rd.log.Debugf("Torrents total count is %d", totalElements)
|
||||
maxParallelThreads := 4
|
||||
if maxPages < maxParallelThreads {
|
||||
maxParallelThreads = maxPages
|
||||
}
|
||||
found := false
|
||||
found := -1
|
||||
for {
|
||||
allResults := make(chan fetchTorrentsResult, maxParallelThreads) // Channel to collect results from goroutines
|
||||
for i := 0; i < maxParallelThreads; i++ { // Launch GET_PARALLEL concurrent fetches
|
||||
@@ -47,7 +49,7 @@ func (rd *RealDebrid) GetTorrents(onlyOne bool) ([]Torrent, int, error) {
|
||||
allResults <- fetchTorrentsResult{
|
||||
torrents: nil,
|
||||
page: page + idx,
|
||||
total: result.total,
|
||||
total: totalElements,
|
||||
err: nil,
|
||||
}
|
||||
return
|
||||
@@ -67,32 +69,37 @@ func (rd *RealDebrid) GetTorrents(onlyOne bool) ([]Torrent, int, error) {
|
||||
batches[bIdx] = append(batches[bIdx], result.torrents...)
|
||||
}
|
||||
for bIdx, batch := range batches { // 4 batches
|
||||
if !found && len(batch) > 0 {
|
||||
if found < 0 && len(batch) > 0 {
|
||||
cachedCount := len(rd.torrentsCache)
|
||||
for cIdx, cached := range rd.torrentsCache { // N cached torrents
|
||||
cIdxEnd := cachedCount - 1 - cIdx
|
||||
for tIdx, torrent := range batch { // 250 torrents in batch
|
||||
tIdxEnd := indexFromEnd(tIdx, page+bIdx, pageSize, result.total)
|
||||
tIdxEnd := indexFromEnd(tIdx, page+bIdx, pageSize, totalElements)
|
||||
if torrent.ID == cached.ID && torrent.Progress == cached.Progress && tIdxEnd == cIdxEnd {
|
||||
found = true
|
||||
found = ((page + bIdx - 1) * pageSize) + tIdx
|
||||
break
|
||||
}
|
||||
}
|
||||
if found {
|
||||
// rd.log.Debugf("From torrent %s (id=%s) onwards, the torrents were untouched", cached.Name, cached.ID)
|
||||
if found >= 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
allTorrents = append(allTorrents, batch...)
|
||||
}
|
||||
if found {
|
||||
allTorrents = append(allTorrents, rd.torrentsCache[len(allTorrents):]...)
|
||||
if found >= 0 {
|
||||
tIdx := found % pageSize
|
||||
pageNum := (found / pageSize) + 1
|
||||
tIdxEnd := indexFromEnd(tIdx, pageNum, pageSize, totalElements)
|
||||
cIdx := len(rd.torrentsCache) - 1 - tIdxEnd
|
||||
last := len(allTorrents) - 1
|
||||
cIdx += last - found + 1
|
||||
allTorrents = append(allTorrents, rd.torrentsCache[cIdx:]...)
|
||||
}
|
||||
|
||||
rd.log.Debugf("Got %d/%d torrents", len(allTorrents), result.total)
|
||||
rd.log.Debugf("Got %d/%d torrents", len(allTorrents), totalElements)
|
||||
|
||||
if len(allTorrents) >= result.total || page >= maxPages {
|
||||
if len(allTorrents) >= totalElements || page >= maxPages {
|
||||
break
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user