Commits

Mathias Panzenböck committed 2b3491f

reduced index size and queries

  • Participants
  • Parent commits 8d69c0f

Comments (0)

Files changed (2)

 var ACTIONS = {
 	'index': function (req, res, done) {
 		async.parallel({
-			albums: db.all.bind(db,'select albumname, artist, launchdate from albums order by albumname, artist'),
 			artists: function (done) {
-				db.all('select artist from artists order by artist',
+				db.all('select artist, albumname, launchdate from albums',
 				function (err, rows) {
 					if (err) return done(err);
-					done(null,collect(rows,'artist'));
+					var artists = {};
+					for (var i = 0; i < rows.length; ++ i) {
+						var album = rows[i];
+						var albums = album.artist in artists ?
+							artists[album.artist] :
+							(artists[album.artist] = {});
+						albums[album.albumname] = album.launchdate;
+					}
+					done(null, artists);
 				});
 			},
+
 			genres: function (done) {
-				db.all('select distinct genre from genres order by genre',
+				db.all('select genre, albumname from genres',
 				function (err, rows) {
 					if (err) return done(err);
-					async.map(rows, function (row, done) {
-						db.all('select albumname from genres where genre = ? order by albumname',[row.genre],
-						function (err, rows) {
-							if (err) return done(err);
-							done(null,{
-								genre: row.genre,
-								albums: collect(rows,'albumname')
-							});
-						})
-					}, done);
+					var genres = {};
+					for (var i = 0; i < rows.length; ++ i) {
+						var album = rows[i];
+						var albums = album.genre in genres ?
+							genres[album.genre] :
+							(genres[album.genre] = []);
+						albums.push(album.albumname);
+					}
+					done(null,genres);
 				});
 			}
 		}, done);

javascripts/magnatune.js

 		build: function (data) {
 			var C = {};
 
-			var sorted_artists = C.SortedArtists = data.artists;
-			var sorted_genres  = C.SortedGenres  = data.genres;
-			var sorted_albums  = C.SortedAlbums  = data.albums;
-					
+			var sorted_artists = C.SortedArtists = [];
+			var sorted_genres  = C.SortedGenres  = [];
+			var sorted_albums  = C.SortedAlbums  = [];
+			
 			var artists = C.Artists = {};
 			var genres  = C.Genres  = {};
 			var albums  = C.Albums  = {};
 
-			// resolve associations
-			for (var i = 0; i < sorted_artists.length; ++ i) {
+			// build artists and albums
+			for (var artistname in data.artists) {
+				var artist_albums = data.artists[artistname];
 				var artist = {
-					artist: sorted_artists[i],
+					artist: artistname,
 					albums: [],
 					genres: {}
 				};
-				sorted_artists[i] = artists[artist.artist] = artist;
-			}
-					
-			for (var i = 0; i < sorted_albums.length; ++ i) {
-				var album = sorted_albums[i];
-				var artist = album.artist = artists[album.artist];
-				album.genres = [];
-				artist.albums.push(album);
-				albums[album.albumname] = album;
-			}
-
-			for (var i = 0; i < sorted_genres.length; ++ i) {
-				var genre = sorted_genres[i];
-				var genre_artists = {};
-				for (var j = 0; j < genre.albums.length; ++ j) {
-					var album = genre.albums[j] = albums[genre.albums[j]];
-					album.genres.push(genre);
-					genre_artists[album.artist.artist] = album.artist;
-					album.artist.genres[genre.genre] = genre;
-				}
-				genre.artists = [];
-				for (var artist_name in genre_artists) {
-					genre.artists.push(genre_artists[artist_name]);
-				}
-				genre.artists.sort(this.ArtistNameSorter);
-				genres[genre.genre] = genre;
-			}
-			
-			for (var i = 0; i < sorted_artists.length; ++ i) {
-				var artist = sorted_artists[i];
-				var artist_genres = artist.genres;
-				artist.genres = [];
-				for (var genre_name in artist_genres) {
-					artist.genres.push(genres[genre_name]);
-				}
-				artist.genres.sort(this.GenreNameSorter);
 				var latestdate = -Infinity;
-				for (var j = 0; j < artist.albums.length; ++ j) {
-					var launchdate = artist.albums[j].launchdate;
-					if (launchdate > latestdate) {
+				// build albums and
+				// resolve artist <-> album associations
+				for (var albumname in artist_albums) {
+					var launchdate = artist_albums[albumname];
+					var album = {
+						albumname: albumname,
+						launchdate: launchdate,
+						artist: artist,
+						genres: []
+					};
+					artist.albums.push(album);
+					albums[albumname] = album;
+					sorted_albums.push(album);
+					if (latestdate < launchdate) {
 						latestdate = launchdate;
 					}
 				}
 				artist.latestdate = latestdate;
+				artists[artistname] = artist;
+				sorted_artists.push(artist);
+
+				artist.albums.sort(this.AlbumNameSorter);
+			}
+
+			// build genres
+			for (var genrename in data.genres) {
+				var genre_albums = data.genres[genrename];
+				var genre = {
+					genre: genrename,
+					albums: [],
+					artists: []
+				};
+				var genre_artists = {};
+				// resolve genre <-> album associations and
+				// genre <-> artist associations
+				for (var i = 0; i < genre_albums.length; ++ i) {
+					var album = albums[genre_albums[i]];
+					var artist = album.artist;
+					album.genres.push(genre);
+					artist.genres[genrename] = true;
+					genre_artists[artist.artist] = true;
+					genre.albums.push(album);
+				}
+				for (var artist in genre_artists) {
+					genre.artists.push(artists[artist]);
+				}
+				genres[genrename] = genre;
+				sorted_genres.push(genre);
+
+				genre.artists.sort(this.ArtistNameSorter);
+				genre.albums.sort(this.AlbumNameSorter);
+			}
+			
+			// sort toplevel lists
+			sorted_artists.sort(this.ArtistNameSorter);
+			sorted_albums.sort(this.AlbumNameSorter);
+			sorted_genres.sort(this.GenreNameSorter);
+
+			// sort genres of artists
+			for (var artistname in artists) {
+				var artist = artists[artistname];
+				var artist_genres = [];
+				for (var genre in artist.genres) {
+					artist_genres.push(genres[genre]);
+				}
+				artist_genres.sort(this.GenreNameSorter);
+				artist.genres = artist_genres;
+			}
+
+			// sort genres of albums
+			for (var albumname in albums) {
+				albums[albumname].genres.sort(this.GenreNameSorter);
 			}
 
 			return C;