diff --git a/NzbDrone.Core.Test/Framework/MockLib.cs b/NzbDrone.Core.Test/Framework/MockLib.cs index 08e9aba04..0030f9682 100644 --- a/NzbDrone.Core.Test/Framework/MockLib.cs +++ b/NzbDrone.Core.Test/Framework/MockLib.cs @@ -5,6 +5,7 @@ using System.IO; using System.Linq; using FizzWare.NBuilder; using Moq; +using NzbDrone.Core.Datastore; using NzbDrone.Core.Instrumentation; using NzbDrone.Core.Providers.Core; using NzbDrone.Core.Repository; @@ -34,25 +35,30 @@ namespace NzbDrone.Core.Test.Framework } } - public static IRepository GetEmptyRepository() - { - return GetEmptyRepository(false); - } - public static IRepository GetEmptyRepository(bool enableLogging) + public static IRepository GetEmptyRepository(bool enableLogging = false, string fileName = "") { Console.WriteLine("Creating an empty SQLite database"); - var provider = ProviderFactory.GetProvider("Data Source=" + Guid.NewGuid() + ".db;Version=3;New=True", - "System.Data.SQLite"); - var repo = new SimpleRepository(provider, SimpleRepositoryOptions.RunMigrations); + if (String.IsNullOrWhiteSpace(fileName)) + { + fileName = Guid.NewGuid() + ".db"; + } + + + var provider = Connection.GetDataProvider(Connection.GetConnectionString(fileName)); + var repo = Connection.CreateSimpleRepository(provider); ForceMigration(repo); + Migrations.Run(Connection.GetConnectionString(fileName), false); + if (enableLogging) { provider.Log = new NlogWriter(); } - + Console.WriteLine("**********************************************************************************"); + Console.WriteLine("*****************************REPO IS READY****************************************"); + Console.WriteLine("**********************************************************************************"); return repo; } diff --git a/NzbDrone.Core.Test/dbBenchmark.cs b/NzbDrone.Core.Test/dbBenchmark.cs index 0adf347bb..0691068b8 100644 --- a/NzbDrone.Core.Test/dbBenchmark.cs +++ b/NzbDrone.Core.Test/dbBenchmark.cs @@ -17,8 +17,8 @@ namespace NzbDrone.Core.Test public class DbBenchmark : TestBase { const int Episodes_Per_Season = 20; - private readonly List seasonsNumbers = new List { 1, 2, 3, 4, 5, 6, 7, 8 }; - private readonly List seriesIds = new List { 1, 2, 3, 4, 5, 6, 7, 8 }; + private readonly List seasonsNumbers = new List { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + private readonly List seriesIds = new List { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }; private readonly List episodes = new List(); private readonly List files = new List(); private readonly IRepository repo = MockLib.GetEmptyRepository(); @@ -68,15 +68,40 @@ namespace NzbDrone.Core.Test } } - repo.AddMany(episodes); - repo.AddMany(files); + } + + repo.AddMany(episodes); + repo.AddMany(files); } - } [Test] - public void get_episode_by_series_seasons_episode_x1000() + public void get_episode_by_series_seasons_episode_x5000() + { + var epProvider = new EpisodeProvider(repo, null); + + + Thread.Sleep(1000); + + + var random = new Random(); + Console.WriteLine("Starting Test"); + + var sw = Stopwatch.StartNew(); + for (int i = 0; i < 5000; i++) + { + epProvider.GetEpisode(6, random.Next(2, 5), random.Next(2, Episodes_Per_Season - 10)).Should().NotBeNull(); + } + + + sw.Stop(); + + Console.WriteLine("Took " + sw.Elapsed); + } + + [Test] + public void get_episode_by_series_seasons_x1000() { var epProvider = new EpisodeProvider(repo, null); @@ -89,30 +114,6 @@ namespace NzbDrone.Core.Test var sw = Stopwatch.StartNew(); for (int i = 0; i < 1000; i++) - { - epProvider.GetEpisode(6, random.Next(2, 5), random.Next(2, Episodes_Per_Season - 10)).Should().NotBeNull(); - } - - - sw.Stop(); - - Console.WriteLine("Took " + sw.Elapsed); - } - - [Test] - public void get_episode_by_series_seasons_x500() - { - var epProvider = new EpisodeProvider(repo, null); - - - Thread.Sleep(1000); - - - var random = new Random(); - Console.WriteLine("Starting Test"); - - var sw = Stopwatch.StartNew(); - for (int i = 0; i < 500; i++) { epProvider.GetEpisodesBySeason(6, random.Next(2, 5)).Should().NotBeNull(); } @@ -124,7 +125,7 @@ namespace NzbDrone.Core.Test } [Test] - public void get_episode_file_count_x50() + public void get_episode_file_count_x100() { var mocker = new AutoMoq.AutoMoqer(); mocker.SetConstant(repo); @@ -139,12 +140,39 @@ namespace NzbDrone.Core.Test Console.WriteLine("Starting Test"); var sw = Stopwatch.StartNew(); - for (int i = 0; i < 50; i++) + for (int i = 0; i < 100; i++) { mediaProvider.GetEpisodeFilesCount(random.Next(1, 5)).Should().NotBeNull(); } + sw.Stop(); + + Console.WriteLine("Took " + sw.Elapsed); + } + + + [Test] + public void get_season_count_x5000() + { + var mocker = new AutoMoq.AutoMoqer(); + mocker.SetConstant(repo); + var provider = mocker.Resolve(); + + + Thread.Sleep(1000); + + + var random = new Random(); + Console.WriteLine("Starting Test"); + + var sw = Stopwatch.StartNew(); + for (int i = 0; i < 5000; i++) + { + provider.GetSeasons(random.Next(1, 10)).Should().HaveSameCount(seasonsNumbers); + } + + sw.Stop(); Console.WriteLine("Took " + sw.Elapsed); diff --git a/NzbDrone.Core/CentralDispatch.cs b/NzbDrone.Core/CentralDispatch.cs index c5a4f887a..621256685 100644 --- a/NzbDrone.Core/CentralDispatch.cs +++ b/NzbDrone.Core/CentralDispatch.cs @@ -56,7 +56,7 @@ namespace NzbDrone.Core LogConfiguration.Setup(); - Migrations.Run(Connection.MainConnectionString); + Migrations.Run(Connection.MainConnectionString, true); ForceMigration(_kernel.Get()); SetupDefaultQualityProfiles(_kernel.Get()); //Setup the default QualityProfiles on start-up diff --git a/NzbDrone.Core/Datastore/Connection.cs b/NzbDrone.Core/Datastore/Connection.cs index 40a5f56cc..b717abd0f 100644 --- a/NzbDrone.Core/Datastore/Connection.cs +++ b/NzbDrone.Core/Datastore/Connection.cs @@ -44,6 +44,11 @@ namespace NzbDrone.Core.Datastore return ProviderFactory.GetProvider(connectionString, "System.Data.SQLite"); } + public static IRepository CreateSimpleRepository(IDataProvider dataProvider) + { + return new SimpleRepository(dataProvider, SimpleRepositoryOptions.RunMigrations); + } + public static IRepository CreateSimpleRepository(string connectionString) { return new SimpleRepository(GetDataProvider(connectionString), SimpleRepositoryOptions.RunMigrations); diff --git a/NzbDrone.Core/Datastore/Migrations.cs b/NzbDrone.Core/Datastore/Migrations.cs index ae677b957..91330a07c 100644 --- a/NzbDrone.Core/Datastore/Migrations.cs +++ b/NzbDrone.Core/Datastore/Migrations.cs @@ -16,14 +16,23 @@ namespace NzbDrone.Core.Datastore { private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); - public static void Run(string connetionString) + public static void Run(string connetionString, bool trace) { Logger.Info("Preparing run database migration"); try { - var migrator = new Migrator.Migrator("Sqlite", connetionString, - Assembly.GetAssembly(typeof(Migrations)), true, new MigrationLogger()); + Migrator.Migrator migrator; + if (trace) + { + migrator = new Migrator.Migrator("Sqlite", connetionString, Assembly.GetAssembly(typeof(Migrations)), true, new MigrationLogger()); + } + else + { + migrator = new Migrator.Migrator("Sqlite", connetionString, Assembly.GetAssembly(typeof(Migrations))); + } + + migrator.MigrateToLastVersion(); @@ -93,11 +102,7 @@ namespace NzbDrone.Core.Datastore { public override void Up() { - //Remove jobs table forcing it to repopulate - var repoProvider = new RepositoryProvider(); - var jobTable = repoProvider.GetSchemaFromType(typeof(JobSetting)); - - Database.RemoveTable(jobTable.Name); + Database.RemoveTable(RepositoryProvider.JobsSchema.Name); } public override void Down() @@ -122,4 +127,32 @@ namespace NzbDrone.Core.Datastore throw new NotImplementedException(); } } + + [Migration(20110604)] + public class Migration20110604 : Migration + { + public override void Up() + { + var episodesTable = RepositoryProvider.EpisodesSchema; + //Database.AddIndex("idx_episodes_series_season_episode", episodesTable.Name, true, + // episodesTable.GetColumnByPropertyName("SeriesId").Name, + // episodesTable.GetColumnByPropertyName("SeasonNumber").Name, + // episodesTable.GetColumnByPropertyName("EpisodeNumber").Name); + + Database.AddIndex("idx_episodes_series_season", episodesTable.Name, false, + episodesTable.GetColumnByPropertyName("SeriesId").Name, + episodesTable.GetColumnByPropertyName("SeasonNumber").Name); + + Database.AddIndex("idx_episodes_series", episodesTable.Name, false, + episodesTable.GetColumnByPropertyName("SeriesId").Name); + + Migrations.RemoveDeletedColumns(Database); + Migrations.AddNewColumns(Database); + } + + public override void Down() + { + throw new NotImplementedException(); + } + } } \ No newline at end of file diff --git a/NzbDrone.Core/Datastore/RepositoryProvider.cs b/NzbDrone.Core/Datastore/RepositoryProvider.cs index 11006811d..040fa9ac0 100644 --- a/NzbDrone.Core/Datastore/RepositoryProvider.cs +++ b/NzbDrone.Core/Datastore/RepositoryProvider.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Reflection; using Migrator.Providers; using Migrator.Providers.SQLite; +using NzbDrone.Core.Repository; using SubSonic.DataProviders; using SubSonic.Extensions; using SubSonic.Schema; @@ -15,6 +16,12 @@ namespace NzbDrone.Core.Datastore { public class RepositoryProvider { + public static readonly ITable EpisodesSchema = new RepositoryProvider().GetSchemaFromType(typeof(Episode)); + public static readonly ITable SeriesSchema = new RepositoryProvider().GetSchemaFromType(typeof(Series)); + public static readonly ITable EpisodeFilesSchema = new RepositoryProvider().GetSchemaFromType(typeof(EpisodeFile)); + public static readonly ITable JobsSchema = new RepositoryProvider().GetSchemaFromType(typeof(JobSetting)); + + public virtual IList GetRepositoryTypes() { var coreAssembly = Assembly.GetExecutingAssembly(); @@ -85,5 +92,7 @@ namespace NzbDrone.Core.Datastore return migColumn; } + + } }