From 740a10a4e3f85ffcfd26ec18263d4c78d4b14ecc Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 10 Sep 2013 14:56:00 -0400 Subject: de-normalize item by name data. create counts during library scan for fast access. --- .../Library/Validators/CountHelpers.cs | 155 +++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 MediaBrowser.Server.Implementations/Library/Validators/CountHelpers.cs (limited to 'MediaBrowser.Server.Implementations/Library/Validators/CountHelpers.cs') diff --git a/MediaBrowser.Server.Implementations/Library/Validators/CountHelpers.cs b/MediaBrowser.Server.Implementations/Library/Validators/CountHelpers.cs new file mode 100644 index 000000000..ea4d887ea --- /dev/null +++ b/MediaBrowser.Server.Implementations/Library/Validators/CountHelpers.cs @@ -0,0 +1,155 @@ +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Entities.Audio; +using MediaBrowser.Controller.Entities.Movies; +using MediaBrowser.Controller.Entities.TV; +using MediaBrowser.Model.Dto; +using System; +using System.Collections.Generic; + +namespace MediaBrowser.Server.Implementations.Library.Validators +{ + /// + /// Class CountHelpers + /// + internal static class CountHelpers + { + /// + /// Adds to dictionary. + /// + /// The item. + /// The counts. + internal static void AddToDictionary(BaseItem item, Dictionary counts) + { + if (item is Movie) + { + IncrementCount(counts, "Movie"); + } + else if (item is Trailer) + { + IncrementCount(counts, "Trailer"); + } + else if (item is Series) + { + IncrementCount(counts, "Series"); + } + else if (item is Game) + { + IncrementCount(counts, "Game"); + } + else if (item is Audio) + { + IncrementCount(counts, "Audio"); + } + else if (item is MusicAlbum) + { + IncrementCount(counts, "MusicAlbum"); + } + else if (item is Episode) + { + IncrementCount(counts, "Episode"); + } + else if (item is MusicVideo) + { + IncrementCount(counts, "MusicVideo"); + } + else if (item is AdultVideo) + { + IncrementCount(counts, "AdultVideo"); + } + + IncrementCount(counts, "Total"); + } + + /// + /// Increments the count. + /// + /// The counts. + /// The key. + internal static void IncrementCount(Dictionary counts, string key) + { + int count; + + if (counts.TryGetValue(key, out count)) + { + count++; + counts[key] = count; + } + else + { + counts.Add(key, 1); + } + } + + /// + /// Gets the counts. + /// + /// The counts. + /// ItemByNameCounts. + internal static ItemByNameCounts GetCounts(Dictionary counts) + { + return new ItemByNameCounts + { + AdultVideoCount = GetCount(counts, "AdultVideo"), + AlbumCount = GetCount(counts, "MusicAlbum"), + EpisodeCount = GetCount(counts, "Episode"), + GameCount = GetCount(counts, "Game"), + MovieCount = GetCount(counts, "Movie"), + MusicVideoCount = GetCount(counts, "MusicVideo"), + SeriesCount = GetCount(counts, "Series"), + SongCount = GetCount(counts, "Audio"), + TrailerCount = GetCount(counts, "Trailer"), + TotalCount = GetCount(counts, "Total") + }; + } + + /// + /// Gets the count. + /// + /// The counts. + /// The key. + /// System.Int32. + internal static int GetCount(Dictionary counts, string key) + { + int count; + + if (counts.TryGetValue(key, out count)) + { + return count; + } + + return 0; + } + + /// + /// Sets the item counts. + /// + /// The user id. + /// The media. + /// The names. + /// The master dictionary. + internal static void SetItemCounts(Guid? userId, BaseItem media, List names, Dictionary>> masterDictionary) + { + foreach (var name in names) + { + Dictionary> libraryCounts; + + if (!masterDictionary.TryGetValue(name, out libraryCounts)) + { + libraryCounts = new Dictionary>(); + masterDictionary.Add(name, libraryCounts); + } + + var userLibId = userId ?? Guid.Empty; + Dictionary userDictionary; + + if (!libraryCounts.TryGetValue(userLibId, out userDictionary)) + { + userDictionary = new Dictionary(); + libraryCounts.Add(userLibId, userDictionary); + } + + AddToDictionary(media, userDictionary); + } + } + } +} -- cgit v1.2.3