1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-12-23 01:16:18 +00:00

Reduced memory usage on random requests

Closes #2
This commit is contained in:
spl0k 2013-09-23 12:43:56 +02:00
parent 3ecd787895
commit fba936db6c

View File

@ -31,14 +31,19 @@ def rand_songs():
query = query.filter(Track.genre == genre) query = query.filter(Track.genre == genre)
if fid: if fid:
query = query.filter(Track.root_folder_id == fid) query = query.filter(Track.root_folder_id == fid)
tracks = query.all() count = query.count()
if not tracks: if not count:
return request.formatter({ 'randomSongs': {} }) return request.formatter({ 'randomSongs': {} })
tracks = []
for _ in xrange(size):
x = random.choice(xrange(count))
tracks.append(query.offset(x).limit(1).one())
return request.formatter({ return request.formatter({
'randomSongs': { 'randomSongs': {
'song': [ random.choice(tracks).as_subsonic_child(request.user) for x in xrange(size) ] 'song': [ t.as_subsonic_child(request.user) for t in tracks ]
} }
}) })
@ -53,10 +58,15 @@ def album_list():
query = Folder.query.filter(Folder.tracks.any()) query = Folder.query.filter(Folder.tracks.any())
if ltype == 'random': if ltype == 'random':
albums = query.all() albums = []
count = query.count()
for _ in xrange(size):
x = random.choice(xrange(count))
albums.append(query.offset(x).limit(1).one())
return request.formatter({ return request.formatter({
'albumList': { 'albumList': {
'album': [ random.choice(albums).as_subsonic_child(request.user) for x in xrange(size) ] 'album': [ a.as_subsonic_child(request.user) for a in albums ]
} }
}) })
elif ltype == 'newest': elif ltype == 'newest':
@ -94,10 +104,15 @@ def album_list_id3():
query = Album.query query = Album.query
if ltype == 'random': if ltype == 'random':
albums = query.all() albums = []
count = query.count()
for _ in xrange(size):
x = random.choice(xrange(count))
albums.append(query.offset(x).limit(1).one())
return request.formatter({ return request.formatter({
'albumList2': { 'albumList': {
'album': [ random.choice(albums).as_subsonic_album(request.user) for x in xrange(size) ] 'album': [ a.as_subsonic_album(request.user) for a in albums ]
} }
}) })
elif ltype == 'newest': elif ltype == 'newest':