Skip to content

Commit

Permalink
Merge pull request #402 from pmcleish/perf_improvements
Browse files Browse the repository at this point in the history
Batching of MovieDB, TvDB fanart/banners for AniDB_Anime contracts
  • Loading branch information
da3dsoul authored Sep 9, 2016
2 parents fa40d83 + bf3ed8a commit 8774525
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 6 deletions.
59 changes: 56 additions & 3 deletions JMMServer/Entities/AniDB_Anime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1980,7 +1980,21 @@ public override string ToString()
private Contract_AniDBAnime GenerateContract(ISessionWrapper session, List<AniDB_Anime_Title> titles)
{
List<Contract_AniDB_Character> characters = GetCharactersContract();
Contract_AniDBAnime contract = GenerateContract(titles, null, characters);
List<MovieDB_Fanart> movDbFanart = null;
List<TvDB_ImageFanart> tvDbFanart = null;
List<TvDB_ImageWideBanner> tvDbBanners = null;

if (AnimeTypeEnum == enAnimeType.Movie)
{
movDbFanart = GetMovieDBFanarts(session);
}
else
{
tvDbFanart = GetTvDBImageFanarts(session);
tvDbBanners = GetTvDBImageWideBanners(session);
}

Contract_AniDBAnime contract = GenerateContract(titles, null, characters, movDbFanart, tvDbFanart, tvDbBanners);
AniDB_Anime_DefaultImage defFanart = GetDefaultFanart(session);
AniDB_Anime_DefaultImage defPoster = GetDefaultPoster(session);
AniDB_Anime_DefaultImage defBanner = GetDefaultWideBanner(session);
Expand Down Expand Up @@ -2035,7 +2049,8 @@ private Contract_AniDBAnime GenerateContract(ISessionWrapper session, List<AniDB
}

private Contract_AniDBAnime GenerateContract(List<AniDB_Anime_Title> titles, DefaultAnimeImages defaultImages,
List<Contract_AniDB_Character> characters)
List<Contract_AniDB_Character> characters, IEnumerable<MovieDB_Fanart> movDbFanart, IEnumerable<TvDB_ImageFanart> tvDbFanart,
IEnumerable<TvDB_ImageWideBanner> tvDbBanners)
{
Contract_AniDBAnime contract = new Contract_AniDBAnime();
contract.AirDate = this.AirDate;
Expand Down Expand Up @@ -2090,6 +2105,34 @@ private Contract_AniDBAnime GenerateContract(List<AniDB_Anime_Title> titles, Def
contract.DefaultImageWideBanner = defaultImages.WideBanner?.ToContract();
}

if (AnimeTypeEnum == enAnimeType.Movie)
{
contract.Fanarts = movDbFanart?.Select(a => new Contract_AniDB_Anime_DefaultImage
{
ImageType = (int)JMMImageType.MovieDB_FanArt,
MovieFanart = a.ToContract(),
AniDB_Anime_DefaultImageID = a.MovieDB_FanartID
})
.ToList() ?? new List<Contract_AniDB_Anime_DefaultImage>();
}
else // Not a movie
{
contract.Fanarts = tvDbFanart?.Select(a => new Contract_AniDB_Anime_DefaultImage
{
ImageType = (int)JMMImageType.TvDB_FanArt,
TVFanart = a.ToContract(),
AniDB_Anime_DefaultImageID = a.TvDB_ImageFanartID
})
.ToList() ?? new List<Contract_AniDB_Anime_DefaultImage>();
contract.Banners = tvDbBanners?.Select(a => new Contract_AniDB_Anime_DefaultImage
{
ImageType = (int)JMMImageType.TvDB_FanArt,
TVWideBanner = a.ToContract(),
AniDB_Anime_DefaultImageID = a.TvDB_ImageWideBannerID
})
.ToList() ?? new List<Contract_AniDB_Anime_DefaultImage>();
}

return contract;
}

Expand Down Expand Up @@ -2134,6 +2177,9 @@ public static void UpdateContractDetailedBatch(ISessionWrapper session, IReadOnl
var repVotes = new AniDB_VoteRepository();
var repAnime = new AniDB_AnimeRepository();
var repChars = new AniDB_CharacterRepository();
var repMovDbFanart = new MovieDB_FanartRepository();
var repTvDbBanner = new TvDB_ImageWideBannerRepository();
var repTvDbFanart = new TvDB_ImageFanartRepository();
int[] animeIds = animeColl.Select(a => a.AnimeID).ToArray();

var titlesByAnime = repTitles.GetByAnimeIDs(session, animeIds);
Expand All @@ -2147,6 +2193,9 @@ public static void UpdateContractDetailedBatch(ISessionWrapper session, IReadOnl
var epVidQualByAnime = repAdHoc.GetEpisodeVideoQualityStatsByAnime(session, animeIds);
var defImagesByAnime = repAnime.GetDefaultImagesByAnime(session, animeIds);
var charsByAnime = repChars.GetCharacterAndSeiyuuByAnime(session, animeIds);
var movDbFanartByAnime = repMovDbFanart.GetByAnimeIDs(session, animeIds);
var tvDbBannersByAnime = repTvDbBanner.GetByAnimeIDs(session, animeIds);
var tvDbFanartByAnime = repTvDbFanart.GetByAnimeIDs(session, animeIds);

foreach (AniDB_Anime anime in animeColl)
{
Expand All @@ -2159,8 +2208,12 @@ public static void UpdateContractDetailedBatch(ISessionWrapper session, IReadOnl
var characterContracts = (charsByAnime[anime.AnimeID] ?? Enumerable.Empty<AnimeCharacterAndSeiyuu>())
.Select(ac => ac.ToContract())
.ToList();
var movieDbFanart = movDbFanartByAnime[anime.AnimeID];
var tvDbBanners = tvDbBannersByAnime[anime.AnimeID];
var tvDbFanart = tvDbFanartByAnime[anime.AnimeID];

contract.AniDBAnime = anime.GenerateContract(animeTitles.ToList(), defImages, characterContracts);
contract.AniDBAnime = anime.GenerateContract(animeTitles.ToList(), defImages, characterContracts,
movieDbFanart, tvDbFanart, tvDbBanners);

// Anime titles
contract.AnimeTitles = titlesByAnime[anime.AnimeID]
Expand Down
33 changes: 32 additions & 1 deletion JMMServer/Repositories/MovieDB_FanartRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using JMMServer.Collections;
using JMMServer.Entities;
using JMMServer.Repositories.NHibernate;
using NHibernate;
Expand Down Expand Up @@ -74,6 +77,34 @@ public List<MovieDB_Fanart> GetByMovieID(ISessionWrapper session, int id)
return new List<MovieDB_Fanart>(objs);
}

public ILookup<int, MovieDB_Fanart> GetByAnimeIDs(ISessionWrapper session, int[] animeIds)
{
if (session == null)
throw new ArgumentNullException(nameof(session));
if (animeIds == null)
throw new ArgumentNullException(nameof(animeIds));

if (animeIds.Length == 0)
{
return EmptyLookup<int, MovieDB_Fanart>.Instance;
}

var fanartByAnime = session.CreateSQLQuery(@"
SELECT DISTINCT adbOther.AnimeID, {mdbFanart.*}
FROM CrossRef_AniDB_Other AS adbOther
INNER JOIN MovieDB_Fanart AS mdbFanart
ON mdbFanart.MovieId = adbOther.CrossRefID
WHERE adbOther.CrossRefType = :crossRefType AND adbOther.AnimeID IN (:animeIds)")
.AddScalar("AnimeID", NHibernateUtil.Int32)
.AddEntity("mdbFanart", typeof(MovieDB_Fanart))
.SetInt32("crossRefType", (int)CrossRefType.MovieDB)
.SetParameterList("animeIds", animeIds)
.List<object[]>()
.ToLookup(r => (int)r[0], r => (MovieDB_Fanart)r[1]);

return fanartByAnime;
}

public List<MovieDB_Fanart> GetAllOriginal()
{
using (var session = JMMService.SessionFactory.OpenSession())
Expand Down
32 changes: 31 additions & 1 deletion JMMServer/Repositories/TvDB_ImageFanartRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using JMMServer.Collections;
using JMMServer.Entities;
using JMMServer.Repositories.NHibernate;
using NHibernate;
Expand Down Expand Up @@ -64,6 +67,33 @@ public List<TvDB_ImageFanart> GetBySeriesID(ISessionWrapper session, int seriesI
return new List<TvDB_ImageFanart>(objs);
}

public ILookup<int, TvDB_ImageFanart> GetByAnimeIDs(ISessionWrapper session, int[] animeIds)
{
if (session == null)
throw new ArgumentNullException(nameof(session));
if (animeIds == null)
throw new ArgumentNullException(nameof(animeIds));

if (animeIds.Length == 0)
{
return EmptyLookup<int, TvDB_ImageFanart>.Instance;
}

var fanartByAnime = session.CreateSQLQuery(@"
SELECT DISTINCT crAdbTvTb.AnimeID, {tvdbFanart.*}
FROM CrossRef_AniDB_TvDBV2 AS crAdbTvTb
INNER JOIN TvDB_ImageFanart AS tvdbFanart
ON tvdbFanart.SeriesID = crAdbTvTb.TvDBID
WHERE crAdbTvTb.AnimeID IN (:animeIds)")
.AddScalar("AnimeID", NHibernateUtil.Int32)
.AddEntity("tvdbFanart", typeof(TvDB_ImageFanart))
.SetParameterList("animeIds", animeIds)
.List<object[]>()
.ToLookup(r => (int)r[0], r => (TvDB_ImageFanart)r[1]);

return fanartByAnime;
}

public List<TvDB_ImageFanart> GetAll()
{
using (var session = JMMService.SessionFactory.OpenSession())
Expand Down
32 changes: 31 additions & 1 deletion JMMServer/Repositories/TvDB_ImageWideBannerRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using JMMServer.Collections;
using JMMServer.Entities;
using JMMServer.Repositories.NHibernate;
using NHibernate;
Expand Down Expand Up @@ -64,6 +67,33 @@ public List<TvDB_ImageWideBanner> GetBySeriesID(ISession session, int seriesID)
return new List<TvDB_ImageWideBanner>(objs);
}

public ILookup<int, TvDB_ImageWideBanner> GetByAnimeIDs(ISessionWrapper session, int[] animeIds)
{
if (session == null)
throw new ArgumentNullException(nameof(session));
if (animeIds == null)
throw new ArgumentNullException(nameof(animeIds));

if (animeIds.Length == 0)
{
return EmptyLookup<int, TvDB_ImageWideBanner>.Instance;
}

var bannersByAnime = session.CreateSQLQuery(@"
SELECT DISTINCT crAdbTvTb.AnimeID, {tvdbBanner.*}
FROM CrossRef_AniDB_TvDBV2 AS crAdbTvTb
INNER JOIN TvDB_ImageWideBanner AS tvdbBanner
ON tvdbBanner.SeriesID = crAdbTvTb.TvDBID
WHERE crAdbTvTb.AnimeID IN (:animeIds)")
.AddScalar("AnimeID", NHibernateUtil.Int32)
.AddEntity("tvdbBanner", typeof(TvDB_ImageWideBanner))
.SetParameterList("animeIds", animeIds)
.List<object[]>()
.ToLookup(r => (int)r[0], r => (TvDB_ImageWideBanner)r[1]);

return bannersByAnime;
}

public List<TvDB_ImageWideBanner> GetAll()
{
using (var session = JMMService.SessionFactory.OpenSession())
Expand Down

0 comments on commit 8774525

Please sign in to comment.