refactor(server): replace RangeByChunks with Go 1.23 iterators (#3292)

* refactor(server): replace RangeByChunks with Go 1.23 iterators

* chore: fix comments re: SQLITE_MAX_VARIABLE_NUMBER

* test: improve playqueue test

* refactor(server): don't create a new iterator when it is not required
This commit is contained in:
Deluan Quintão
2024-09-22 11:47:10 -04:00
committed by GitHub
parent 3910e77a7a
commit 669c8f4c49
9 changed files with 79 additions and 99 deletions
+9 -12
View File
@@ -101,25 +101,22 @@ func (r *playQueueRepository) toModel(pq *playQueue) model.PlayQueue {
return q
}
// loadTracks loads the tracks from the database. It receives a list of track IDs and returns a list of MediaFiles
// in the same order as the input list.
func (r *playQueueRepository) loadTracks(tracks model.MediaFiles) model.MediaFiles {
if len(tracks) == 0 {
return nil
}
// Collect all ids
ids := make([]string, len(tracks))
for i, t := range tracks {
ids[i] = t.ID
}
// Break the list in chunks, up to 500 items, to avoid hitting SQLITE_MAX_FUNCTION_ARG limit
chunks := slice.BreakUp(ids, 500)
// Query each chunk of media_file ids and store results in a map
mfRepo := NewMediaFileRepository(r.ctx, r.db)
trackMap := map[string]model.MediaFile{}
for i := range chunks {
idsFilter := Eq{"media_file.id": chunks[i]}
// Create an iterator to collect all track IDs
ids := slice.SeqFunc(tracks, func(t model.MediaFile) string { return t.ID })
// Break the list in chunks, up to 500 items, to avoid hitting SQLITE_MAX_VARIABLE_NUMBER limit
for chunk := range slice.CollectChunks(ids, 500) {
idsFilter := Eq{"media_file.id": chunk}
tracks, err := mfRepo.GetAll(model.QueryOptions{Filters: idsFilter})
if err != nil {
u := loggedUser(r.ctx)