undo that change, remove magic numbers

This commit is contained in:
Kendall Garner 2025-07-08 11:26:07 -07:00
parent 0becfd4b59
commit 58bb8e716e
No known key found for this signature in database
GPG key ID: 9355F387FE765C94

View file

@ -46,7 +46,9 @@ const ALBUM_LIST_SORT_MAPPING: Record<AlbumListSort, AlbumListSortType | undefin
[AlbumListSort.YEAR]: AlbumListSortType.BY_YEAR, [AlbumListSort.YEAR]: AlbumListSortType.BY_YEAR,
}; };
const BATCH_SIZE = 500; const MAX_SUBSONIC_ITEMS = 500;
// A trick to skip ahead 10x
const SUBSONIC_FAST_BATCH_SIZE = MAX_SUBSONIC_ITEMS * 10;
export const SubsonicController: ControllerEndpoint = { export const SubsonicController: ControllerEndpoint = {
addToPlaylist: async ({ apiClientProps, body, query }) => { addToPlaylist: async ({ apiClientProps, body, query }) => {
@ -420,7 +422,7 @@ export const SubsonicController: ControllerEndpoint = {
while (fetchNextPage) { while (fetchNextPage) {
const res = await ssApiClient(apiClientProps).search3({ const res = await ssApiClient(apiClientProps).search3({
query: { query: {
albumCount: 500, albumCount: MAX_SUBSONIC_ITEMS,
albumOffset: startIndex, albumOffset: startIndex,
artistCount: 0, artistCount: 0,
artistOffset: 0, artistOffset: 0,
@ -439,8 +441,7 @@ export const SubsonicController: ControllerEndpoint = {
totalRecordCount += albumCount; totalRecordCount += albumCount;
startIndex += albumCount; startIndex += albumCount;
// The max limit size for Subsonic is 500 fetchNextPage = albumCount === MAX_SUBSONIC_ITEMS;
fetchNextPage = albumCount === 500;
} }
return totalRecordCount; return totalRecordCount;
@ -524,7 +525,7 @@ export const SubsonicController: ControllerEndpoint = {
genre: query.genres?.length ? query.genres[0] : undefined, genre: query.genres?.length ? query.genres[0] : undefined,
musicFolderId: query.musicFolderId, musicFolderId: query.musicFolderId,
offset: startIndex, offset: startIndex,
size: 500, size: MAX_SUBSONIC_ITEMS,
toYear, toYear,
type, type,
}, },
@ -548,8 +549,7 @@ export const SubsonicController: ControllerEndpoint = {
totalRecordCount += albumCount; totalRecordCount += albumCount;
startIndex += albumCount; startIndex += albumCount;
// The max limit size for Subsonic is 500 fetchNextPage = albumCount === MAX_SUBSONIC_ITEMS;
fetchNextPage = albumCount === 500;
} }
return totalRecordCount; return totalRecordCount;
@ -1089,7 +1089,7 @@ export const SubsonicController: ControllerEndpoint = {
artistCount: 0, artistCount: 0,
artistOffset: 0, artistOffset: 0,
query: query.searchTerm || '""', query: query.searchTerm || '""',
songCount: 500, songCount: MAX_SUBSONIC_ITEMS,
songOffset: startIndex, songOffset: startIndex,
}, },
}); });
@ -1103,8 +1103,7 @@ export const SubsonicController: ControllerEndpoint = {
totalRecordCount += songCount; totalRecordCount += songCount;
startIndex += songCount; startIndex += songCount;
// The max limit size for Subsonic is 500 fetchNextPage = songCount === MAX_SUBSONIC_ITEMS;
fetchNextPage = songCount === 500;
} }
return totalRecordCount; return totalRecordCount;
@ -1112,6 +1111,10 @@ export const SubsonicController: ControllerEndpoint = {
if (query.genreIds) { if (query.genreIds) {
let totalRecordCount = 0; let totalRecordCount = 0;
// Rather than just do `getSongsByGenre` by groups of 500, instead
// jump the offset 10x, and then backtrack on the last chunk. This improves
// performance for extremely large libraries
while (fetchNextSection) { while (fetchNextSection) {
const res = await ssApiClient(apiClientProps).getSongsByGenre({ const res = await ssApiClient(apiClientProps).getSongsByGenre({
query: { query: {
@ -1130,17 +1133,17 @@ export const SubsonicController: ControllerEndpoint = {
if (numberOfResults !== 1) { if (numberOfResults !== 1) {
fetchNextSection = false; fetchNextSection = false;
startIndex = sectionIndex === 0 ? 0 : sectionIndex - BATCH_SIZE; startIndex = sectionIndex === 0 ? 0 : sectionIndex - SUBSONIC_FAST_BATCH_SIZE;
break; break;
} else { } else {
sectionIndex += BATCH_SIZE; sectionIndex += SUBSONIC_FAST_BATCH_SIZE;
} }
} }
while (fetchNextPage) { while (fetchNextPage) {
const res = await ssApiClient(apiClientProps).getSongsByGenre({ const res = await ssApiClient(apiClientProps).getSongsByGenre({
query: { query: {
count: BATCH_SIZE, count: MAX_SUBSONIC_ITEMS,
genre: query.genreIds[0], genre: query.genreIds[0],
musicFolderId: query.musicFolderId, musicFolderId: query.musicFolderId,
offset: startIndex, offset: startIndex,
@ -1156,7 +1159,7 @@ export const SubsonicController: ControllerEndpoint = {
totalRecordCount = startIndex + numberOfResults; totalRecordCount = startIndex + numberOfResults;
startIndex += numberOfResults; startIndex += numberOfResults;
fetchNextPage = numberOfResults === 500; fetchNextPage = numberOfResults === MAX_SUBSONIC_ITEMS;
} }
return totalRecordCount; return totalRecordCount;
@ -1178,6 +1181,9 @@ export const SubsonicController: ControllerEndpoint = {
let totalRecordCount = 0; let totalRecordCount = 0;
// Rather than just do `search3` by groups of 500, instead
// jump the offset 10x, and then backtrack on the last chunk. This improves
// performance for extremely large libraries
while (fetchNextSection) { while (fetchNextSection) {
const res = await ssApiClient(apiClientProps).search3({ const res = await ssApiClient(apiClientProps).search3({
query: { query: {
@ -1197,13 +1203,12 @@ export const SubsonicController: ControllerEndpoint = {
const numberOfResults = (res.body.searchResult3?.song || []).length || 0; const numberOfResults = (res.body.searchResult3?.song || []).length || 0;
// Check each batch of 500 songs to check for data if (numberOfResults !== 1) {
sectionIndex += BATCH_SIZE; fetchNextSection = false;
fetchNextSection = numberOfResults === 1; startIndex = sectionIndex === 0 ? 0 : sectionIndex - SUBSONIC_FAST_BATCH_SIZE;
break;
if (!fetchNextSection) { } else {
// fetchNextBlock will be false on the next loop so we need to subtract 2 * BATCH_SIZE sectionIndex += SUBSONIC_FAST_BATCH_SIZE;
startIndex = sectionIndex - 2 * BATCH_SIZE;
} }
} }
@ -1215,7 +1220,7 @@ export const SubsonicController: ControllerEndpoint = {
artistCount: 0, artistCount: 0,
artistOffset: 0, artistOffset: 0,
query: query.searchTerm || '""', query: query.searchTerm || '""',
songCount: 500, songCount: MAX_SUBSONIC_ITEMS,
songOffset: startIndex, songOffset: startIndex,
}, },
}); });
@ -1229,8 +1234,7 @@ export const SubsonicController: ControllerEndpoint = {
totalRecordCount = startIndex + numberOfResults; totalRecordCount = startIndex + numberOfResults;
startIndex += numberOfResults; startIndex += numberOfResults;
// The max limit size for Subsonic is 500 fetchNextPage = numberOfResults === MAX_SUBSONIC_ITEMS;
fetchNextPage = numberOfResults === 500;
} }
return totalRecordCount; return totalRecordCount;