From fa714425dd91fed2ca691cd45f73f7ea5a579dff Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 18 Nov 2016 03:39:20 -0500 Subject: begin to rework repositories --- .../Activity/ActivityRepository.cs | 197 +++++++++++++++++++++ 1 file changed, 197 insertions(+) create mode 100644 Emby.Server.Implementations/Activity/ActivityRepository.cs (limited to 'Emby.Server.Implementations/Activity/ActivityRepository.cs') diff --git a/Emby.Server.Implementations/Activity/ActivityRepository.cs b/Emby.Server.Implementations/Activity/ActivityRepository.cs new file mode 100644 index 000000000..ea9e537c9 --- /dev/null +++ b/Emby.Server.Implementations/Activity/ActivityRepository.cs @@ -0,0 +1,197 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Emby.Server.Implementations.Data; +using MediaBrowser.Controller; +using MediaBrowser.Model.Activity; +using MediaBrowser.Model.Logging; +using MediaBrowser.Model.Querying; +using SQLitePCL.pretty; + +namespace Emby.Server.Implementations.Activity +{ + public class ActivityRepository : BaseSqliteRepository, IActivityRepository + { + private readonly CultureInfo _usCulture = new CultureInfo("en-US"); + + public ActivityRepository(ILogger logger, IServerApplicationPaths appPaths) + : base(logger) + { + DbFilePath = Path.Combine(appPaths.DataPath, "activitylog.db"); + } + + public void Initialize() + { + using (var connection = CreateConnection()) + { + string[] queries = { + + "create table if not exists ActivityLogEntries (Id GUID PRIMARY KEY, Name TEXT, Overview TEXT, ShortOverview TEXT, Type TEXT, ItemId TEXT, UserId TEXT, DateCreated DATETIME, LogSeverity TEXT)", + "create index if not exists idx_ActivityLogEntries on ActivityLogEntries(Id)" + }; + + connection.RunQueries(queries); + } + } + + private const string BaseActivitySelectText = "select Id, Name, Overview, ShortOverview, Type, ItemId, UserId, DateCreated, LogSeverity from ActivityLogEntries"; + + public Task Create(ActivityLogEntry entry) + { + return Update(entry); + } + + public async Task Update(ActivityLogEntry entry) + { + if (entry == null) + { + throw new ArgumentNullException("entry"); + } + + lock (WriteLock) + { + using (var connection = CreateConnection()) + { + connection.RunInTransaction(db => + { + var commandText = "replace into ActivityLogEntries (Id, Name, Overview, ShortOverview, Type, ItemId, UserId, DateCreated, LogSeverity) values (?, ?, ?, ?, ?, ?, ?, ?, ?)"; + + db.Execute(commandText, + entry.Id.ToGuidParamValue(), + entry.Name, + entry.Overview, + entry.ShortOverview, + entry.Type, + entry.ItemId, + entry.UserId, + entry.Date.ToDateTimeParamValue(), + entry.Severity.ToString()); + }); + } + } + } + + public QueryResult GetActivityLogEntries(DateTime? minDate, int? startIndex, int? limit) + { + lock (WriteLock) + { + using (var connection = CreateConnection(true)) + { + var commandText = BaseActivitySelectText; + + var whereClauses = new List(); + var paramList = new List(); + + if (minDate.HasValue) + { + whereClauses.Add("DateCreated>=@DateCreated"); + paramList.Add(minDate.Value.ToDateTimeParamValue()); + } + + var whereTextWithoutPaging = whereClauses.Count == 0 ? + string.Empty : + " where " + string.Join(" AND ", whereClauses.ToArray()); + + if (startIndex.HasValue && startIndex.Value > 0) + { + var pagingWhereText = whereClauses.Count == 0 ? + string.Empty : + " where " + string.Join(" AND ", whereClauses.ToArray()); + + whereClauses.Add(string.Format("Id NOT IN (SELECT Id FROM ActivityLogEntries {0} ORDER BY DateCreated DESC LIMIT {1})", + pagingWhereText, + startIndex.Value.ToString(_usCulture))); + } + + var whereText = whereClauses.Count == 0 ? + string.Empty : + " where " + string.Join(" AND ", whereClauses.ToArray()); + + commandText += whereText; + + commandText += " ORDER BY DateCreated DESC"; + + if (limit.HasValue) + { + commandText += " LIMIT " + limit.Value.ToString(_usCulture); + } + + var totalRecordCount = connection.Query("select count (Id) from ActivityLogEntries" + whereTextWithoutPaging, paramList.ToArray()).SelectScalarInt().First(); + + var list = new List(); + + foreach (var row in connection.Query(commandText, paramList.ToArray())) + { + list.Add(GetEntry(row)); + } + + return new QueryResult() + { + Items = list.ToArray(), + TotalRecordCount = totalRecordCount + }; + } + } + } + + private ActivityLogEntry GetEntry(IReadOnlyList reader) + { + var index = 0; + + var info = new ActivityLogEntry + { + Id = reader[index].ReadGuid().ToString("N") + }; + + index++; + if (reader[index].SQLiteType != SQLiteType.Null) + { + info.Name = reader[index].ToString(); + } + + index++; + if (reader[index].SQLiteType != SQLiteType.Null) + { + info.Overview = reader[index].ToString(); + } + + index++; + if (reader[index].SQLiteType != SQLiteType.Null) + { + info.ShortOverview = reader[index].ToString(); + } + + index++; + if (reader[index].SQLiteType != SQLiteType.Null) + { + info.Type = reader[index].ToString(); + } + + index++; + if (reader[index].SQLiteType != SQLiteType.Null) + { + info.ItemId = reader[index].ToString(); + } + + index++; + if (reader[index].SQLiteType != SQLiteType.Null) + { + info.UserId = reader[index].ToString(); + } + + index++; + info.Date = reader[index].ReadDateTime(); + + index++; + if (reader[index].SQLiteType != SQLiteType.Null) + { + info.Severity = (LogSeverity)Enum.Parse(typeof(LogSeverity), reader[index].ToString(), true); + } + + return info; + } + } +} -- cgit v1.2.3 From 65a1ef020b205b1676bd7dd70e7261a1fa29b7a2 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sat, 19 Nov 2016 00:52:49 -0500 Subject: move sync repository to portable project --- Emby.Common.Implementations/BaseApplicationHost.cs | 1 - .../ScheduledTasks/TaskManager.cs | 26 +- .../Updates/GithubUpdater.cs | 269 ------ Emby.Server.Core/ApplicationHost.cs | 24 +- Emby.Server.Core/Browser/BrowserLauncher.cs | 75 -- Emby.Server.Core/Data/SqliteItemRepository.cs | 29 +- .../EntryPoints/ExternalPortForwarding.cs | 3 +- Emby.Server.Core/EntryPoints/StartupWizard.cs | 59 -- Emby.Server.Core/HttpServerFactory.cs | 6 +- Emby.Server.Core/Migrations/DbMigration.cs | 64 -- .../Migrations/UpdateLevelMigration.cs | 131 --- Emby.Server.Core/Sync/SyncRepository.cs | 976 --------------------- .../Activity/ActivityRepository.cs | 2 +- .../Browser/BrowserLauncher.cs | 73 ++ .../Data/BaseSqliteRepository.cs | 20 +- .../Data/CleanDatabaseScheduledTask.cs | 148 +--- .../Emby.Server.Implementations.csproj | 4 + .../EntryPoints/StartupWizard.cs | 59 ++ .../FileOrganization/OrganizerScheduledTask.cs | 2 +- .../HttpServer/HttpListenerHost.cs | 8 +- .../Migrations/UpdateLevelMigration.cs | 130 +++ .../Security/AuthenticationRepository.cs | 4 +- Emby.Server.Implementations/Sync/SyncRepository.cs | 770 ++++++++++++++++ Emby.Server.Implementations/TV/TVSeriesManager.cs | 4 - MediaBrowser.Api/StartupWizardService.cs | 1 - MediaBrowser.Common/MediaBrowser.Common.csproj | 1 + MediaBrowser.Common/Updates/GithubUpdater.cs | 269 ++++++ .../Entities/InternalItemsQuery.cs | 1 - MediaBrowser.Controller/Entities/TV/Series.cs | 4 - .../Configuration/ServerConfiguration.cs | 4 - MediaBrowser.Model/Tasks/ITaskManager.cs | 2 - MediaBrowser.Server.Mono/MonoAppHost.cs | 26 + MediaBrowser.Server.Mono/Native/MonoFileSystem.cs | 3 +- MediaBrowser.ServerApplication/MainStartup.cs | 2 +- MediaBrowser.ServerApplication/ServerNotifyIcon.cs | 2 +- MediaBrowser.ServerApplication/WindowsAppHost.cs | 9 + 36 files changed, 1392 insertions(+), 1819 deletions(-) delete mode 100644 Emby.Common.Implementations/Updates/GithubUpdater.cs delete mode 100644 Emby.Server.Core/Browser/BrowserLauncher.cs delete mode 100644 Emby.Server.Core/EntryPoints/StartupWizard.cs delete mode 100644 Emby.Server.Core/Migrations/DbMigration.cs delete mode 100644 Emby.Server.Core/Migrations/UpdateLevelMigration.cs delete mode 100644 Emby.Server.Core/Sync/SyncRepository.cs create mode 100644 Emby.Server.Implementations/Browser/BrowserLauncher.cs create mode 100644 Emby.Server.Implementations/EntryPoints/StartupWizard.cs create mode 100644 Emby.Server.Implementations/Migrations/UpdateLevelMigration.cs create mode 100644 Emby.Server.Implementations/Sync/SyncRepository.cs create mode 100644 MediaBrowser.Common/Updates/GithubUpdater.cs (limited to 'Emby.Server.Implementations/Activity/ActivityRepository.cs') diff --git a/Emby.Common.Implementations/BaseApplicationHost.cs b/Emby.Common.Implementations/BaseApplicationHost.cs index 1f194968c..02d7cb31f 100644 --- a/Emby.Common.Implementations/BaseApplicationHost.cs +++ b/Emby.Common.Implementations/BaseApplicationHost.cs @@ -4,7 +4,6 @@ using Emby.Common.Implementations.Devices; using Emby.Common.Implementations.IO; using Emby.Common.Implementations.ScheduledTasks; using Emby.Common.Implementations.Serialization; -using Emby.Common.Implementations.Updates; using MediaBrowser.Common.Net; using MediaBrowser.Common.Plugins; using MediaBrowser.Common.Progress; diff --git a/Emby.Common.Implementations/ScheduledTasks/TaskManager.cs b/Emby.Common.Implementations/ScheduledTasks/TaskManager.cs index 218af7ed5..b0153c588 100644 --- a/Emby.Common.Implementations/ScheduledTasks/TaskManager.cs +++ b/Emby.Common.Implementations/ScheduledTasks/TaskManager.cs @@ -55,25 +55,6 @@ namespace Emby.Common.Implementations.ScheduledTasks private ILogger Logger { get; set; } private readonly IFileSystem _fileSystem; - private bool _suspendTriggers; - - public bool SuspendTriggers - { - get { return _suspendTriggers; } - set - { - Logger.Info("Setting SuspendTriggers to {0}", value); - var executeQueued = _suspendTriggers && !value; - - _suspendTriggers = value; - - if (executeQueued) - { - ExecuteQueuedTasks(); - } - } - } - /// /// Initializes a new instance of the class. /// @@ -230,7 +211,7 @@ namespace Emby.Common.Implementations.ScheduledTasks lock (_taskQueue) { - if (task.State == TaskState.Idle && !SuspendTriggers) + if (task.State == TaskState.Idle) { Execute(task, options); return; @@ -322,11 +303,6 @@ namespace Emby.Common.Implementations.ScheduledTasks /// private void ExecuteQueuedTasks() { - if (SuspendTriggers) - { - return; - } - Logger.Info("ExecuteQueuedTasks"); // Execute queued tasks diff --git a/Emby.Common.Implementations/Updates/GithubUpdater.cs b/Emby.Common.Implementations/Updates/GithubUpdater.cs deleted file mode 100644 index 42bc29ed5..000000000 --- a/Emby.Common.Implementations/Updates/GithubUpdater.cs +++ /dev/null @@ -1,269 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using MediaBrowser.Common.Net; -using MediaBrowser.Model.Serialization; -using MediaBrowser.Model.Updates; - -namespace Emby.Common.Implementations.Updates -{ - public class GithubUpdater - { - private readonly IHttpClient _httpClient; - private readonly IJsonSerializer _jsonSerializer; - - public GithubUpdater(IHttpClient httpClient, IJsonSerializer jsonSerializer) - { - _httpClient = httpClient; - _jsonSerializer = jsonSerializer; - } - - public async Task CheckForUpdateResult(string organzation, string repository, Version minVersion, PackageVersionClass updateLevel, string assetFilename, string packageName, string targetFilename, TimeSpan cacheLength, CancellationToken cancellationToken) - { - var url = string.Format("https://api.github.com/repos/{0}/{1}/releases", organzation, repository); - - var options = new HttpRequestOptions - { - Url = url, - EnableKeepAlive = false, - CancellationToken = cancellationToken, - UserAgent = "Emby/3.0", - BufferContent = false - }; - - if (cacheLength.Ticks > 0) - { - options.CacheMode = CacheMode.Unconditional; - options.CacheLength = cacheLength; - } - - using (var stream = await _httpClient.Get(options).ConfigureAwait(false)) - { - var obj = _jsonSerializer.DeserializeFromStream(stream); - - return CheckForUpdateResult(obj, minVersion, updateLevel, assetFilename, packageName, targetFilename); - } - } - - private CheckForUpdateResult CheckForUpdateResult(RootObject[] obj, Version minVersion, PackageVersionClass updateLevel, string assetFilename, string packageName, string targetFilename) - { - if (updateLevel == PackageVersionClass.Release) - { - // Technically all we need to do is check that it's not pre-release - // But let's addititional checks for -beta and -dev to handle builds that might be temporarily tagged incorrectly. - obj = obj.Where(i => !i.prerelease && !i.name.EndsWith("-beta", StringComparison.OrdinalIgnoreCase) && !i.name.EndsWith("-dev", StringComparison.OrdinalIgnoreCase)).ToArray(); - } - else if (updateLevel == PackageVersionClass.Beta) - { - obj = obj.Where(i => !i.prerelease || i.name.EndsWith("-beta", StringComparison.OrdinalIgnoreCase)).ToArray(); - } - else if (updateLevel == PackageVersionClass.Dev) - { - obj = obj.Where(i => !i.prerelease || i.name.EndsWith("-beta", StringComparison.OrdinalIgnoreCase) || i.name.EndsWith("-dev", StringComparison.OrdinalIgnoreCase)).ToArray(); - } - - var availableUpdate = obj - .Select(i => CheckForUpdateResult(i, minVersion, assetFilename, packageName, targetFilename)) - .Where(i => i != null) - .OrderByDescending(i => Version.Parse(i.AvailableVersion)) - .FirstOrDefault(); - - return availableUpdate ?? new CheckForUpdateResult - { - IsUpdateAvailable = false - }; - } - - private bool MatchesUpdateLevel(RootObject i, PackageVersionClass updateLevel) - { - if (updateLevel == PackageVersionClass.Beta) - { - return !i.prerelease || i.name.EndsWith("-beta", StringComparison.OrdinalIgnoreCase); - } - if (updateLevel == PackageVersionClass.Dev) - { - return !i.prerelease || i.name.EndsWith("-beta", StringComparison.OrdinalIgnoreCase) || - i.name.EndsWith("-dev", StringComparison.OrdinalIgnoreCase); - } - - // Technically all we need to do is check that it's not pre-release - // But let's addititional checks for -beta and -dev to handle builds that might be temporarily tagged incorrectly. - return !i.prerelease && !i.name.EndsWith("-beta", StringComparison.OrdinalIgnoreCase) && - !i.name.EndsWith("-dev", StringComparison.OrdinalIgnoreCase); - } - - public async Task> GetLatestReleases(string organzation, string repository, string assetFilename, CancellationToken cancellationToken) - { - var list = new List(); - - var url = string.Format("https://api.github.com/repos/{0}/{1}/releases", organzation, repository); - - var options = new HttpRequestOptions - { - Url = url, - EnableKeepAlive = false, - CancellationToken = cancellationToken, - UserAgent = "Emby/3.0", - BufferContent = false - }; - - using (var stream = await _httpClient.Get(options).ConfigureAwait(false)) - { - var obj = _jsonSerializer.DeserializeFromStream(stream); - - obj = obj.Where(i => (i.assets ?? new List()).Any(a => IsAsset(a, assetFilename))).ToArray(); - - list.AddRange(obj.Where(i => MatchesUpdateLevel(i, PackageVersionClass.Release)).OrderByDescending(GetVersion).Take(1)); - list.AddRange(obj.Where(i => MatchesUpdateLevel(i, PackageVersionClass.Beta)).OrderByDescending(GetVersion).Take(1)); - list.AddRange(obj.Where(i => MatchesUpdateLevel(i, PackageVersionClass.Dev)).OrderByDescending(GetVersion).Take(1)); - - return list; - } - } - - public Version GetVersion(RootObject obj) - { - Version version; - if (!Version.TryParse(obj.tag_name, out version)) - { - return new Version(1, 0); - } - - return version; - } - - private CheckForUpdateResult CheckForUpdateResult(RootObject obj, Version minVersion, string assetFilename, string packageName, string targetFilename) - { - Version version; - if (!Version.TryParse(obj.tag_name, out version)) - { - return null; - } - - if (version < minVersion) - { - return null; - } - - var asset = (obj.assets ?? new List()).FirstOrDefault(i => IsAsset(i, assetFilename)); - - if (asset == null) - { - return null; - } - - return new CheckForUpdateResult - { - AvailableVersion = version.ToString(), - IsUpdateAvailable = version > minVersion, - Package = new PackageVersionInfo - { - classification = obj.prerelease ? - (obj.name.EndsWith("-dev", StringComparison.OrdinalIgnoreCase) ? PackageVersionClass.Dev : PackageVersionClass.Beta) : - PackageVersionClass.Release, - name = packageName, - sourceUrl = asset.browser_download_url, - targetFilename = targetFilename, - versionStr = version.ToString(), - requiredVersionStr = "1.0.0", - description = obj.body, - infoUrl = obj.html_url - } - }; - } - - private bool IsAsset(Asset asset, string assetFilename) - { - var downloadFilename = Path.GetFileName(asset.browser_download_url) ?? string.Empty; - - if (downloadFilename.IndexOf(assetFilename, StringComparison.OrdinalIgnoreCase) != -1) - { - return true; - } - - return string.Equals(assetFilename, downloadFilename, StringComparison.OrdinalIgnoreCase); - } - - public class Uploader - { - public string login { get; set; } - public int id { get; set; } - public string avatar_url { get; set; } - public string gravatar_id { get; set; } - public string url { get; set; } - public string html_url { get; set; } - public string followers_url { get; set; } - public string following_url { get; set; } - public string gists_url { get; set; } - public string starred_url { get; set; } - public string subscriptions_url { get; set; } - public string organizations_url { get; set; } - public string repos_url { get; set; } - public string events_url { get; set; } - public string received_events_url { get; set; } - public string type { get; set; } - public bool site_admin { get; set; } - } - - public class Asset - { - public string url { get; set; } - public int id { get; set; } - public string name { get; set; } - public object label { get; set; } - public Uploader uploader { get; set; } - public string content_type { get; set; } - public string state { get; set; } - public int size { get; set; } - public int download_count { get; set; } - public string created_at { get; set; } - public string updated_at { get; set; } - public string browser_download_url { get; set; } - } - - public class Author - { - public string login { get; set; } - public int id { get; set; } - public string avatar_url { get; set; } - public string gravatar_id { get; set; } - public string url { get; set; } - public string html_url { get; set; } - public string followers_url { get; set; } - public string following_url { get; set; } - public string gists_url { get; set; } - public string starred_url { get; set; } - public string subscriptions_url { get; set; } - public string organizations_url { get; set; } - public string repos_url { get; set; } - public string events_url { get; set; } - public string received_events_url { get; set; } - public string type { get; set; } - public bool site_admin { get; set; } - } - - public class RootObject - { - public string url { get; set; } - public string assets_url { get; set; } - public string upload_url { get; set; } - public string html_url { get; set; } - public int id { get; set; } - public string tag_name { get; set; } - public string target_commitish { get; set; } - public string name { get; set; } - public bool draft { get; set; } - public Author author { get; set; } - public bool prerelease { get; set; } - public string created_at { get; set; } - public string published_at { get; set; } - public List assets { get; set; } - public string tarball_url { get; set; } - public string zipball_url { get; set; } - public string body { get; set; } - } - } -} diff --git a/Emby.Server.Core/ApplicationHost.cs b/Emby.Server.Core/ApplicationHost.cs index 6073991b1..00b9b99e6 100644 --- a/Emby.Server.Core/ApplicationHost.cs +++ b/Emby.Server.Core/ApplicationHost.cs @@ -65,7 +65,6 @@ using Emby.Common.Implementations.Networking; using Emby.Common.Implementations.Reflection; using Emby.Common.Implementations.Serialization; using Emby.Common.Implementations.TextEncoding; -using Emby.Common.Implementations.Updates; using Emby.Common.Implementations.Xml; using Emby.Photos; using MediaBrowser.Model.IO; @@ -90,10 +89,10 @@ using Emby.Server.Implementations.Devices; using Emby.Server.Implementations.FFMpeg; using Emby.Server.Core.IO; using Emby.Server.Core.Localization; -using Emby.Server.Core.Migrations; +using Emby.Server.Implementations.Migrations; using Emby.Server.Implementations.Security; using Emby.Server.Implementations.Social; -using Emby.Server.Core.Sync; +using Emby.Server.Implementations.Sync; using Emby.Server.Implementations.Channels; using Emby.Server.Implementations.Collections; using Emby.Server.Implementations.Connect; @@ -357,12 +356,6 @@ namespace Emby.Server.Core { await PerformPreInitMigrations().ConfigureAwait(false); - if (ServerConfigurationManager.Configuration.MigrationVersion < CleanDatabaseScheduledTask.MigrationVersion && - ServerConfigurationManager.Configuration.IsStartupWizardCompleted) - { - TaskManager.SuspendTriggers = true; - } - await base.RunStartupTasks().ConfigureAwait(false); await MediaEncoder.Init().ConfigureAwait(false); @@ -494,7 +487,6 @@ namespace Emby.Server.Core { var migrations = new List { - new DbMigration(ServerConfigurationManager, TaskManager) }; foreach (var task in migrations) @@ -568,7 +560,7 @@ namespace Emby.Server.Core AuthenticationRepository = await GetAuthenticationRepository().ConfigureAwait(false); RegisterSingleInstance(AuthenticationRepository); - SyncRepository = await GetSyncRepository().ConfigureAwait(false); + SyncRepository = GetSyncRepository(); RegisterSingleInstance(SyncRepository); UserManager = new UserManager(LogManager.GetLogger("UserManager"), ServerConfigurationManager, UserRepository, XmlSerializer, NetworkManager, () => ImageProcessor, () => DtoService, () => ConnectManager, this, JsonSerializer, FileSystemManager, CryptographyProvider, _defaultUserNameFactory()); @@ -591,7 +583,7 @@ namespace Emby.Server.Core CertificatePath = GetCertificatePath(true); Certificate = GetCertificate(CertificatePath); - HttpServer = HttpServerFactory.CreateServer(this, LogManager, ServerConfigurationManager, NetworkManager, MemoryStreamFactory, "Emby", "web/index.html", textEncoding, SocketFactory, CryptographyProvider, JsonSerializer, XmlSerializer, EnvironmentInfo, Certificate); + HttpServer = HttpServerFactory.CreateServer(this, LogManager, ServerConfigurationManager, NetworkManager, MemoryStreamFactory, "Emby", "web/index.html", textEncoding, SocketFactory, CryptographyProvider, JsonSerializer, XmlSerializer, EnvironmentInfo, Certificate, SupportsDualModeSockets); HttpServer.GlobalResponse = LocalizationManager.GetLocalizedString("StartupEmbyServerIsLoading"); RegisterSingleInstance(HttpServer, false); progress.Report(10); @@ -714,6 +706,8 @@ namespace Emby.Server.Core await ((UserManager)UserManager).Initialize().ConfigureAwait(false); } + protected abstract bool SupportsDualModeSockets { get; } + private ICertificate GetCertificate(string certificateLocation) { if (string.IsNullOrWhiteSpace(certificateLocation)) @@ -844,11 +838,11 @@ namespace Emby.Server.Core return repo; } - private async Task GetSyncRepository() + private ISyncRepository GetSyncRepository() { - var repo = new SyncRepository(LogManager, JsonSerializer, ServerConfigurationManager.ApplicationPaths, GetDbConnector()); + var repo = new SyncRepository(LogManager.GetLogger("SyncRepository"), JsonSerializer, ServerConfigurationManager.ApplicationPaths); - await repo.Initialize().ConfigureAwait(false); + repo.Initialize(); return repo; } diff --git a/Emby.Server.Core/Browser/BrowserLauncher.cs b/Emby.Server.Core/Browser/BrowserLauncher.cs deleted file mode 100644 index 4ccc41c30..000000000 --- a/Emby.Server.Core/Browser/BrowserLauncher.cs +++ /dev/null @@ -1,75 +0,0 @@ -using MediaBrowser.Controller; -using System; - -namespace Emby.Server.Core.Browser -{ - /// - /// Class BrowserLauncher - /// - public static class BrowserLauncher - { - /// - /// Opens the dashboard page. - /// - /// The page. - /// The app host. - public static void OpenDashboardPage(string page, IServerApplicationHost appHost) - { - var url = appHost.GetLocalApiUrl("localhost") + "/web/" + page; - - OpenUrl(appHost, url); - } - - /// - /// Opens the community. - /// - public static void OpenCommunity(IServerApplicationHost appHost) - { - OpenUrl(appHost, "http://emby.media/community"); - } - - public static void OpenEmbyPremiere(IServerApplicationHost appHost) - { - OpenDashboardPage("supporterkey.html", appHost); - } - - /// - /// Opens the web client. - /// - /// The app host. - public static void OpenWebClient(IServerApplicationHost appHost) - { - OpenDashboardPage("index.html", appHost); - } - - /// - /// Opens the dashboard. - /// - /// The app host. - public static void OpenDashboard(IServerApplicationHost appHost) - { - OpenDashboardPage("dashboard.html", appHost); - } - - /// - /// Opens the URL. - /// - /// The URL. - private static void OpenUrl(IServerApplicationHost appHost, string url) - { - try - { - appHost.LaunchUrl(url); - } - catch (NotImplementedException) - { - - } - catch (Exception ex) - { - Console.WriteLine("Error launching url: " + url); - Console.WriteLine(ex.Message); - } - } - } -} diff --git a/Emby.Server.Core/Data/SqliteItemRepository.cs b/Emby.Server.Core/Data/SqliteItemRepository.cs index ed03c0f67..2f08081f6 100644 --- a/Emby.Server.Core/Data/SqliteItemRepository.cs +++ b/Emby.Server.Core/Data/SqliteItemRepository.cs @@ -3060,18 +3060,6 @@ namespace Emby.Server.Core.Data { //whereClauses.Add("(UserId is null or UserId=@UserId)"); } - if (query.IsCurrentSchema.HasValue) - { - if (query.IsCurrentSchema.Value) - { - whereClauses.Add("(SchemaVersion not null AND SchemaVersion=@SchemaVersion)"); - } - else - { - whereClauses.Add("(SchemaVersion is null or SchemaVersion<>@SchemaVersion)"); - } - cmd.Parameters.Add(cmd, "@SchemaVersion", DbType.Int32).Value = LatestSchemaVersion; - } if (query.IsHD.HasValue) { whereClauses.Add("IsHD=@IsHD"); @@ -3454,7 +3442,7 @@ namespace Emby.Server.Core.Data cmd.Parameters.Add(cmd, "@NameLessThan", DbType.String).Value = query.NameLessThan.ToLower(); } - if (query.ImageTypes.Length > 0 && _config.Configuration.SchemaVersion >= 87) + if (query.ImageTypes.Length > 0) { foreach (var requiredImage in query.ImageTypes) { @@ -3738,15 +3726,8 @@ namespace Emby.Server.Core.Data } if (query.IsVirtualItem.HasValue) { - if (_config.Configuration.SchemaVersion >= 90) - { - whereClauses.Add("IsVirtualItem=@IsVirtualItem"); - cmd.Parameters.Add(cmd, "@IsVirtualItem", DbType.Boolean).Value = query.IsVirtualItem.Value; - } - else if (!query.IsVirtualItem.Value) - { - whereClauses.Add("LocationType<>'Virtual'"); - } + whereClauses.Add("IsVirtualItem=@IsVirtualItem"); + cmd.Parameters.Add(cmd, "@IsVirtualItem", DbType.Boolean).Value = query.IsVirtualItem.Value; } if (query.IsSpecialSeason.HasValue) { @@ -3770,7 +3751,7 @@ namespace Emby.Server.Core.Data whereClauses.Add("PremiereDate < DATETIME('now')"); } } - if (query.IsMissing.HasValue && _config.Configuration.SchemaVersion >= 90) + if (query.IsMissing.HasValue) { if (query.IsMissing.Value) { @@ -3781,7 +3762,7 @@ namespace Emby.Server.Core.Data whereClauses.Add("(IsVirtualItem=0 OR PremiereDate >= DATETIME('now'))"); } } - if (query.IsVirtualUnaired.HasValue && _config.Configuration.SchemaVersion >= 90) + if (query.IsVirtualUnaired.HasValue) { if (query.IsVirtualUnaired.Value) { diff --git a/Emby.Server.Core/EntryPoints/ExternalPortForwarding.cs b/Emby.Server.Core/EntryPoints/ExternalPortForwarding.cs index c1a19a71f..11e275940 100644 --- a/Emby.Server.Core/EntryPoints/ExternalPortForwarding.cs +++ b/Emby.Server.Core/EntryPoints/ExternalPortForwarding.cs @@ -95,7 +95,7 @@ namespace Emby.Server.Core.EntryPoints NatUtility.StartDiscovery(); - _timer = _timerFactory.Create(ClearCreatedRules, null, TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(5)); + _timer = _timerFactory.Create(ClearCreatedRules, null, TimeSpan.FromMinutes(10), TimeSpan.FromMinutes(10)); _deviceDiscovery.DeviceDiscovered += _deviceDiscovery_DeviceDiscovered; @@ -233,6 +233,7 @@ namespace Emby.Server.Core.EntryPoints await device.CreatePortMap(new Mapping(Protocol.Tcp, privatePort, publicPort) { Description = _appHost.Name + }).ConfigureAwait(false); } catch (Exception ex) diff --git a/Emby.Server.Core/EntryPoints/StartupWizard.cs b/Emby.Server.Core/EntryPoints/StartupWizard.cs deleted file mode 100644 index 30ceca073..000000000 --- a/Emby.Server.Core/EntryPoints/StartupWizard.cs +++ /dev/null @@ -1,59 +0,0 @@ -using MediaBrowser.Controller; -using MediaBrowser.Controller.Plugins; -using MediaBrowser.Model.Logging; -using Emby.Server.Core.Browser; - -namespace Emby.Server.Core.EntryPoints -{ - /// - /// Class StartupWizard - /// - public class StartupWizard : IServerEntryPoint - { - /// - /// The _app host - /// - private readonly IServerApplicationHost _appHost; - /// - /// The _user manager - /// - private readonly ILogger _logger; - - /// - /// Initializes a new instance of the class. - /// - /// The app host. - /// The logger. - public StartupWizard(IServerApplicationHost appHost, ILogger logger) - { - _appHost = appHost; - _logger = logger; - } - - /// - /// Runs this instance. - /// - public void Run() - { - if (_appHost.IsFirstRun) - { - LaunchStartupWizard(); - } - } - - /// - /// Launches the startup wizard. - /// - private void LaunchStartupWizard() - { - BrowserLauncher.OpenDashboardPage("wizardstart.html", _appHost); - } - - /// - /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. - /// - public void Dispose() - { - } - } -} \ No newline at end of file diff --git a/Emby.Server.Core/HttpServerFactory.cs b/Emby.Server.Core/HttpServerFactory.cs index d6c07e258..8ec5fb026 100644 --- a/Emby.Server.Core/HttpServerFactory.cs +++ b/Emby.Server.Core/HttpServerFactory.cs @@ -44,7 +44,8 @@ namespace Emby.Server.Core IJsonSerializer json, IXmlSerializer xml, IEnvironmentInfo environment, - ICertificate certificate) + ICertificate certificate, + bool enableDualModeSockets) { var logger = logManager.GetLogger("HttpServer"); @@ -63,7 +64,8 @@ namespace Emby.Server.Core environment, certificate, new StreamFactory(), - GetParseFn); + GetParseFn, + enableDualModeSockets); } private static Func GetParseFn(Type propertyType) diff --git a/Emby.Server.Core/Migrations/DbMigration.cs b/Emby.Server.Core/Migrations/DbMigration.cs deleted file mode 100644 index 5d652770f..000000000 --- a/Emby.Server.Core/Migrations/DbMigration.cs +++ /dev/null @@ -1,64 +0,0 @@ -using System.Threading.Tasks; -using Emby.Server.Implementations.Data; -using MediaBrowser.Controller.Configuration; -using MediaBrowser.Model.Tasks; -using Emby.Server.Core.Data; -using Emby.Server.Implementations.Migrations; - -namespace Emby.Server.Core.Migrations -{ - public class DbMigration : IVersionMigration - { - private readonly IServerConfigurationManager _config; - private readonly ITaskManager _taskManager; - - public DbMigration(IServerConfigurationManager config, ITaskManager taskManager) - { - _config = config; - _taskManager = taskManager; - } - - public async Task Run() - { - // If a forced migration is required, do that now - if (_config.Configuration.MigrationVersion < CleanDatabaseScheduledTask.MigrationVersion) - { - if (!_config.Configuration.IsStartupWizardCompleted) - { - _config.Configuration.MigrationVersion = CleanDatabaseScheduledTask.MigrationVersion; - _config.SaveConfiguration(); - return; - } - - _taskManager.SuspendTriggers = true; - CleanDatabaseScheduledTask.EnableUnavailableMessage = true; - - Task.Run(async () => - { - await Task.Delay(1000).ConfigureAwait(false); - - _taskManager.Execute(); - }); - - return; - } - - if (_config.Configuration.SchemaVersion < SqliteItemRepository.LatestSchemaVersion) - { - if (!_config.Configuration.IsStartupWizardCompleted) - { - _config.Configuration.SchemaVersion = SqliteItemRepository.LatestSchemaVersion; - _config.SaveConfiguration(); - return; - } - - Task.Run(async () => - { - await Task.Delay(1000).ConfigureAwait(false); - - _taskManager.Execute(); - }); - } - } - } -} diff --git a/Emby.Server.Core/Migrations/UpdateLevelMigration.cs b/Emby.Server.Core/Migrations/UpdateLevelMigration.cs deleted file mode 100644 index c79dbabea..000000000 --- a/Emby.Server.Core/Migrations/UpdateLevelMigration.cs +++ /dev/null @@ -1,131 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Threading; -using System.Threading.Tasks; -using Emby.Common.Implementations.Updates; -using MediaBrowser.Common.Net; -using MediaBrowser.Controller; -using MediaBrowser.Controller.Configuration; -using MediaBrowser.Model.Logging; -using MediaBrowser.Model.Serialization; -using MediaBrowser.Model.Updates; -using Emby.Server.Implementations.Migrations; - -namespace Emby.Server.Core.Migrations -{ - public class UpdateLevelMigration : IVersionMigration - { - private readonly IServerConfigurationManager _config; - private readonly IServerApplicationHost _appHost; - private readonly IHttpClient _httpClient; - private readonly IJsonSerializer _jsonSerializer; - private readonly string _releaseAssetFilename; - private readonly ILogger _logger; - - public UpdateLevelMigration(IServerConfigurationManager config, IServerApplicationHost appHost, IHttpClient httpClient, IJsonSerializer jsonSerializer, string releaseAssetFilename, ILogger logger) - { - _config = config; - _appHost = appHost; - _httpClient = httpClient; - _jsonSerializer = jsonSerializer; - _releaseAssetFilename = releaseAssetFilename; - _logger = logger; - } - - public async Task Run() - { - var lastVersion = _config.Configuration.LastVersion; - var currentVersion = _appHost.ApplicationVersion; - - if (string.Equals(lastVersion, currentVersion.ToString(), StringComparison.OrdinalIgnoreCase)) - { - return; - } - - try - { - var updateLevel = _config.Configuration.SystemUpdateLevel; - - await CheckVersion(currentVersion, updateLevel, CancellationToken.None).ConfigureAwait(false); - } - catch (Exception ex) - { - _logger.ErrorException("Error in update migration", ex); - } - } - - private async Task CheckVersion(Version currentVersion, PackageVersionClass currentUpdateLevel, CancellationToken cancellationToken) - { - var releases = await new GithubUpdater(_httpClient, _jsonSerializer) - .GetLatestReleases("MediaBrowser", "Emby", _releaseAssetFilename, cancellationToken).ConfigureAwait(false); - - var newUpdateLevel = GetNewUpdateLevel(currentVersion, currentUpdateLevel, releases); - - if (newUpdateLevel != currentUpdateLevel) - { - _config.Configuration.SystemUpdateLevel = newUpdateLevel; - _config.SaveConfiguration(); - } - } - - private PackageVersionClass GetNewUpdateLevel(Version currentVersion, PackageVersionClass currentUpdateLevel, List releases) - { - var newUpdateLevel = currentUpdateLevel; - - // If the current version is later than current stable, set the update level to beta - if (releases.Count >= 1) - { - var release = releases[0]; - var version = ParseVersion(release.tag_name); - if (version != null) - { - if (currentVersion > version) - { - newUpdateLevel = PackageVersionClass.Beta; - } - else - { - return PackageVersionClass.Release; - } - } - } - - // If the current version is later than current beta, set the update level to dev - if (releases.Count >= 2) - { - var release = releases[1]; - var version = ParseVersion(release.tag_name); - if (version != null) - { - if (currentVersion > version) - { - newUpdateLevel = PackageVersionClass.Dev; - } - else - { - return PackageVersionClass.Beta; - } - } - } - - return newUpdateLevel; - } - - private Version ParseVersion(string versionString) - { - if (!string.IsNullOrWhiteSpace(versionString)) - { - var parts = versionString.Split('.'); - if (parts.Length == 3) - { - versionString += ".0"; - } - } - - Version version; - Version.TryParse(versionString, out version); - - return version; - } - } -} diff --git a/Emby.Server.Core/Sync/SyncRepository.cs b/Emby.Server.Core/Sync/SyncRepository.cs deleted file mode 100644 index bfcf76767..000000000 --- a/Emby.Server.Core/Sync/SyncRepository.cs +++ /dev/null @@ -1,976 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Data; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Threading.Tasks; -using Emby.Server.Core.Data; -using MediaBrowser.Controller; -using MediaBrowser.Controller.Sync; -using MediaBrowser.Model.Dto; -using MediaBrowser.Model.Logging; -using MediaBrowser.Model.Querying; -using MediaBrowser.Model.Serialization; -using MediaBrowser.Model.Sync; - -namespace Emby.Server.Core.Sync -{ - public class SyncRepository : BaseSqliteRepository, ISyncRepository - { - private readonly CultureInfo _usCulture = new CultureInfo("en-US"); - - private readonly IJsonSerializer _json; - - public SyncRepository(ILogManager logManager, IJsonSerializer json, IServerApplicationPaths appPaths, IDbConnector connector) - : base(logManager, connector) - { - _json = json; - DbFilePath = Path.Combine(appPaths.DataPath, "sync14.db"); - } - - private class SyncSummary - { - public Dictionary Items { get; set; } - - public SyncSummary() - { - Items = new Dictionary(); - } - } - - - public async Task Initialize() - { - using (var connection = await CreateConnection().ConfigureAwait(false)) - { - string[] queries = { - - "create table if not exists SyncJobs (Id GUID PRIMARY KEY, TargetId TEXT NOT NULL, Name TEXT NOT NULL, Profile TEXT, Quality TEXT, Bitrate INT, Status TEXT NOT NULL, Progress FLOAT, UserId TEXT NOT NULL, ItemIds TEXT NOT NULL, Category TEXT, ParentId TEXT, UnwatchedOnly BIT, ItemLimit INT, SyncNewContent BIT, DateCreated DateTime, DateLastModified DateTime, ItemCount int)", - - "create table if not exists SyncJobItems (Id GUID PRIMARY KEY, ItemId TEXT, ItemName TEXT, MediaSourceId TEXT, JobId TEXT, TemporaryPath TEXT, OutputPath TEXT, Status TEXT, TargetId TEXT, DateCreated DateTime, Progress FLOAT, AdditionalFiles TEXT, MediaSource TEXT, IsMarkedForRemoval BIT, JobItemIndex INT, ItemDateModifiedTicks BIGINT)", - - "drop index if exists idx_SyncJobItems2", - "drop index if exists idx_SyncJobItems3", - "drop index if exists idx_SyncJobs1", - "drop index if exists idx_SyncJobs", - "drop index if exists idx_SyncJobItems1", - "create index if not exists idx_SyncJobItems4 on SyncJobItems(TargetId,ItemId,Status,Progress,DateCreated)", - "create index if not exists idx_SyncJobItems5 on SyncJobItems(TargetId,Status,ItemId,Progress)", - - "create index if not exists idx_SyncJobs2 on SyncJobs(TargetId,Status,ItemIds,Progress)", - - "pragma shrink_memory" - }; - - connection.RunQueries(queries, Logger); - - connection.AddColumn(Logger, "SyncJobs", "Profile", "TEXT"); - connection.AddColumn(Logger, "SyncJobs", "Bitrate", "INT"); - connection.AddColumn(Logger, "SyncJobItems", "ItemDateModifiedTicks", "BIGINT"); - } - } - - private const string BaseJobSelectText = "select Id, TargetId, Name, Profile, Quality, Bitrate, Status, Progress, UserId, ItemIds, Category, ParentId, UnwatchedOnly, ItemLimit, SyncNewContent, DateCreated, DateLastModified, ItemCount from SyncJobs"; - private const string BaseJobItemSelectText = "select Id, ItemId, ItemName, MediaSourceId, JobId, TemporaryPath, OutputPath, Status, TargetId, DateCreated, Progress, AdditionalFiles, MediaSource, IsMarkedForRemoval, JobItemIndex, ItemDateModifiedTicks from SyncJobItems"; - - public SyncJob GetJob(string id) - { - if (string.IsNullOrEmpty(id)) - { - throw new ArgumentNullException("id"); - } - - CheckDisposed(); - - var guid = new Guid(id); - - if (guid == Guid.Empty) - { - throw new ArgumentNullException("id"); - } - - using (var connection = CreateConnection(true).Result) - { - using (var cmd = connection.CreateCommand()) - { - cmd.CommandText = BaseJobSelectText + " where Id=@Id"; - - cmd.Parameters.Add(cmd, "@Id", DbType.Guid).Value = guid; - - using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult | CommandBehavior.SingleRow)) - { - if (reader.Read()) - { - return GetJob(reader); - } - } - } - - return null; - } - } - - private SyncJob GetJob(IDataReader reader) - { - var info = new SyncJob - { - Id = reader.GetGuid(0).ToString("N"), - TargetId = reader.GetString(1), - Name = reader.GetString(2) - }; - - if (!reader.IsDBNull(3)) - { - info.Profile = reader.GetString(3); - } - - if (!reader.IsDBNull(4)) - { - info.Quality = reader.GetString(4); - } - - if (!reader.IsDBNull(5)) - { - info.Bitrate = reader.GetInt32(5); - } - - if (!reader.IsDBNull(6)) - { - info.Status = (SyncJobStatus)Enum.Parse(typeof(SyncJobStatus), reader.GetString(6), true); - } - - if (!reader.IsDBNull(7)) - { - info.Progress = reader.GetDouble(7); - } - - if (!reader.IsDBNull(8)) - { - info.UserId = reader.GetString(8); - } - - if (!reader.IsDBNull(9)) - { - info.RequestedItemIds = reader.GetString(9).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList(); - } - - if (!reader.IsDBNull(10)) - { - info.Category = (SyncCategory)Enum.Parse(typeof(SyncCategory), reader.GetString(10), true); - } - - if (!reader.IsDBNull(11)) - { - info.ParentId = reader.GetString(11); - } - - if (!reader.IsDBNull(12)) - { - info.UnwatchedOnly = reader.GetBoolean(12); - } - - if (!reader.IsDBNull(13)) - { - info.ItemLimit = reader.GetInt32(13); - } - - info.SyncNewContent = reader.GetBoolean(14); - - info.DateCreated = reader.GetDateTime(15).ToUniversalTime(); - info.DateLastModified = reader.GetDateTime(16).ToUniversalTime(); - info.ItemCount = reader.GetInt32(17); - - return info; - } - - public Task Create(SyncJob job) - { - return InsertOrUpdate(job, true); - } - - public Task Update(SyncJob job) - { - return InsertOrUpdate(job, false); - } - - private async Task InsertOrUpdate(SyncJob job, bool insert) - { - if (job == null) - { - throw new ArgumentNullException("job"); - } - - CheckDisposed(); - - using (var connection = await CreateConnection().ConfigureAwait(false)) - { - using (var cmd = connection.CreateCommand()) - { - if (insert) - { - cmd.CommandText = "insert into SyncJobs (Id, TargetId, Name, Profile, Quality, Bitrate, Status, Progress, UserId, ItemIds, Category, ParentId, UnwatchedOnly, ItemLimit, SyncNewContent, DateCreated, DateLastModified, ItemCount) values (@Id, @TargetId, @Name, @Profile, @Quality, @Bitrate, @Status, @Progress, @UserId, @ItemIds, @Category, @ParentId, @UnwatchedOnly, @ItemLimit, @SyncNewContent, @DateCreated, @DateLastModified, @ItemCount)"; - - cmd.Parameters.Add(cmd, "@Id"); - cmd.Parameters.Add(cmd, "@TargetId"); - cmd.Parameters.Add(cmd, "@Name"); - cmd.Parameters.Add(cmd, "@Profile"); - cmd.Parameters.Add(cmd, "@Quality"); - cmd.Parameters.Add(cmd, "@Bitrate"); - cmd.Parameters.Add(cmd, "@Status"); - cmd.Parameters.Add(cmd, "@Progress"); - cmd.Parameters.Add(cmd, "@UserId"); - cmd.Parameters.Add(cmd, "@ItemIds"); - cmd.Parameters.Add(cmd, "@Category"); - cmd.Parameters.Add(cmd, "@ParentId"); - cmd.Parameters.Add(cmd, "@UnwatchedOnly"); - cmd.Parameters.Add(cmd, "@ItemLimit"); - cmd.Parameters.Add(cmd, "@SyncNewContent"); - cmd.Parameters.Add(cmd, "@DateCreated"); - cmd.Parameters.Add(cmd, "@DateLastModified"); - cmd.Parameters.Add(cmd, "@ItemCount"); - } - else - { - cmd.CommandText = "update SyncJobs set TargetId=@TargetId,Name=@Name,Profile=@Profile,Quality=@Quality,Bitrate=@Bitrate,Status=@Status,Progress=@Progress,UserId=@UserId,ItemIds=@ItemIds,Category=@Category,ParentId=@ParentId,UnwatchedOnly=@UnwatchedOnly,ItemLimit=@ItemLimit,SyncNewContent=@SyncNewContent,DateCreated=@DateCreated,DateLastModified=@DateLastModified,ItemCount=@ItemCount where Id=@Id"; - - cmd.Parameters.Add(cmd, "@Id"); - cmd.Parameters.Add(cmd, "@TargetId"); - cmd.Parameters.Add(cmd, "@Name"); - cmd.Parameters.Add(cmd, "@Profile"); - cmd.Parameters.Add(cmd, "@Quality"); - cmd.Parameters.Add(cmd, "@Bitrate"); - cmd.Parameters.Add(cmd, "@Status"); - cmd.Parameters.Add(cmd, "@Progress"); - cmd.Parameters.Add(cmd, "@UserId"); - cmd.Parameters.Add(cmd, "@ItemIds"); - cmd.Parameters.Add(cmd, "@Category"); - cmd.Parameters.Add(cmd, "@ParentId"); - cmd.Parameters.Add(cmd, "@UnwatchedOnly"); - cmd.Parameters.Add(cmd, "@ItemLimit"); - cmd.Parameters.Add(cmd, "@SyncNewContent"); - cmd.Parameters.Add(cmd, "@DateCreated"); - cmd.Parameters.Add(cmd, "@DateLastModified"); - cmd.Parameters.Add(cmd, "@ItemCount"); - } - - IDbTransaction transaction = null; - - try - { - transaction = connection.BeginTransaction(); - - var index = 0; - - cmd.GetParameter(index++).Value = new Guid(job.Id); - cmd.GetParameter(index++).Value = job.TargetId; - cmd.GetParameter(index++).Value = job.Name; - cmd.GetParameter(index++).Value = job.Profile; - cmd.GetParameter(index++).Value = job.Quality; - cmd.GetParameter(index++).Value = job.Bitrate; - cmd.GetParameter(index++).Value = job.Status.ToString(); - cmd.GetParameter(index++).Value = job.Progress; - cmd.GetParameter(index++).Value = job.UserId; - cmd.GetParameter(index++).Value = string.Join(",", job.RequestedItemIds.ToArray()); - cmd.GetParameter(index++).Value = job.Category; - cmd.GetParameter(index++).Value = job.ParentId; - cmd.GetParameter(index++).Value = job.UnwatchedOnly; - cmd.GetParameter(index++).Value = job.ItemLimit; - cmd.GetParameter(index++).Value = job.SyncNewContent; - cmd.GetParameter(index++).Value = job.DateCreated; - cmd.GetParameter(index++).Value = job.DateLastModified; - cmd.GetParameter(index++).Value = job.ItemCount; - - cmd.Transaction = transaction; - - cmd.ExecuteNonQuery(); - - transaction.Commit(); - } - catch (OperationCanceledException) - { - if (transaction != null) - { - transaction.Rollback(); - } - - throw; - } - catch (Exception e) - { - Logger.ErrorException("Failed to save record:", e); - - if (transaction != null) - { - transaction.Rollback(); - } - - throw; - } - finally - { - if (transaction != null) - { - transaction.Dispose(); - } - } - } - } - } - - public async Task DeleteJob(string id) - { - if (string.IsNullOrWhiteSpace(id)) - { - throw new ArgumentNullException("id"); - } - - CheckDisposed(); - - using (var connection = await CreateConnection().ConfigureAwait(false)) - { - using (var deleteJobCommand = connection.CreateCommand()) - { - using (var deleteJobItemsCommand = connection.CreateCommand()) - { - IDbTransaction transaction = null; - - try - { - // _deleteJobCommand - deleteJobCommand.CommandText = "delete from SyncJobs where Id=@Id"; - deleteJobCommand.Parameters.Add(deleteJobCommand, "@Id"); - - transaction = connection.BeginTransaction(); - - deleteJobCommand.GetParameter(0).Value = new Guid(id); - deleteJobCommand.Transaction = transaction; - deleteJobCommand.ExecuteNonQuery(); - - // _deleteJobItemsCommand - deleteJobItemsCommand.CommandText = "delete from SyncJobItems where JobId=@JobId"; - deleteJobItemsCommand.Parameters.Add(deleteJobItemsCommand, "@JobId"); - - deleteJobItemsCommand.GetParameter(0).Value = id; - deleteJobItemsCommand.Transaction = transaction; - deleteJobItemsCommand.ExecuteNonQuery(); - - transaction.Commit(); - } - catch (OperationCanceledException) - { - if (transaction != null) - { - transaction.Rollback(); - } - - throw; - } - catch (Exception e) - { - Logger.ErrorException("Failed to save record:", e); - - if (transaction != null) - { - transaction.Rollback(); - } - - throw; - } - finally - { - if (transaction != null) - { - transaction.Dispose(); - } - } - } - } - } - } - - public QueryResult GetJobs(SyncJobQuery query) - { - if (query == null) - { - throw new ArgumentNullException("query"); - } - - CheckDisposed(); - - using (var connection = CreateConnection(true).Result) - { - using (var cmd = connection.CreateCommand()) - { - cmd.CommandText = BaseJobSelectText; - - var whereClauses = new List(); - - if (query.Statuses.Length > 0) - { - var statuses = string.Join(",", query.Statuses.Select(i => "'" + i.ToString() + "'").ToArray()); - - whereClauses.Add(string.Format("Status in ({0})", statuses)); - } - if (!string.IsNullOrWhiteSpace(query.TargetId)) - { - whereClauses.Add("TargetId=@TargetId"); - cmd.Parameters.Add(cmd, "@TargetId", DbType.String).Value = query.TargetId; - } - if (!string.IsNullOrWhiteSpace(query.ExcludeTargetIds)) - { - var excludeIds = (query.ExcludeTargetIds ?? string.Empty).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); - if (excludeIds.Length == 1) - { - whereClauses.Add("TargetId<>@ExcludeTargetId"); - cmd.Parameters.Add(cmd, "@ExcludeTargetId", DbType.String).Value = excludeIds[0]; - } - else if (excludeIds.Length > 1) - { - whereClauses.Add("TargetId<>@ExcludeTargetId"); - cmd.Parameters.Add(cmd, "@ExcludeTargetId", DbType.String).Value = excludeIds[0]; - } - } - if (!string.IsNullOrWhiteSpace(query.UserId)) - { - whereClauses.Add("UserId=@UserId"); - cmd.Parameters.Add(cmd, "@UserId", DbType.String).Value = query.UserId; - } - if (query.SyncNewContent.HasValue) - { - whereClauses.Add("SyncNewContent=@SyncNewContent"); - cmd.Parameters.Add(cmd, "@SyncNewContent", DbType.Boolean).Value = query.SyncNewContent.Value; - } - - cmd.CommandText += " mainTable"; - - var whereTextWithoutPaging = whereClauses.Count == 0 ? - string.Empty : - " where " + string.Join(" AND ", whereClauses.ToArray()); - - var startIndex = query.StartIndex ?? 0; - if (startIndex > 0) - { - whereClauses.Add(string.Format("Id NOT IN (SELECT Id FROM SyncJobs ORDER BY (Select Max(DateLastModified) from SyncJobs where TargetId=mainTable.TargetId) DESC, DateLastModified DESC LIMIT {0})", - startIndex.ToString(_usCulture))); - } - - if (whereClauses.Count > 0) - { - cmd.CommandText += " where " + string.Join(" AND ", whereClauses.ToArray()); - } - - cmd.CommandText += " ORDER BY (Select Max(DateLastModified) from SyncJobs where TargetId=mainTable.TargetId) DESC, DateLastModified DESC"; - - if (query.Limit.HasValue) - { - cmd.CommandText += " LIMIT " + query.Limit.Value.ToString(_usCulture); - } - - cmd.CommandText += "; select count (Id) from SyncJobs" + whereTextWithoutPaging; - - var list = new List(); - var count = 0; - - using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)) - { - while (reader.Read()) - { - list.Add(GetJob(reader)); - } - - if (reader.NextResult() && reader.Read()) - { - count = reader.GetInt32(0); - } - } - - return new QueryResult() - { - Items = list.ToArray(), - TotalRecordCount = count - }; - } - } - } - - public SyncJobItem GetJobItem(string id) - { - if (string.IsNullOrEmpty(id)) - { - throw new ArgumentNullException("id"); - } - - CheckDisposed(); - - var guid = new Guid(id); - - using (var connection = CreateConnection(true).Result) - { - using (var cmd = connection.CreateCommand()) - { - cmd.CommandText = BaseJobItemSelectText + " where Id=@Id"; - - cmd.Parameters.Add(cmd, "@Id", DbType.Guid).Value = guid; - - using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult | CommandBehavior.SingleRow)) - { - if (reader.Read()) - { - return GetJobItem(reader); - } - } - } - - return null; - } - } - - private QueryResult GetJobItemReader(SyncJobItemQuery query, string baseSelectText, Func itemFactory) - { - if (query == null) - { - throw new ArgumentNullException("query"); - } - - using (var connection = CreateConnection(true).Result) - { - using (var cmd = connection.CreateCommand()) - { - cmd.CommandText = baseSelectText; - - var whereClauses = new List(); - - if (!string.IsNullOrWhiteSpace(query.JobId)) - { - whereClauses.Add("JobId=@JobId"); - cmd.Parameters.Add(cmd, "@JobId", DbType.String).Value = query.JobId; - } - if (!string.IsNullOrWhiteSpace(query.ItemId)) - { - whereClauses.Add("ItemId=@ItemId"); - cmd.Parameters.Add(cmd, "@ItemId", DbType.String).Value = query.ItemId; - } - if (!string.IsNullOrWhiteSpace(query.TargetId)) - { - whereClauses.Add("TargetId=@TargetId"); - cmd.Parameters.Add(cmd, "@TargetId", DbType.String).Value = query.TargetId; - } - - if (query.Statuses.Length > 0) - { - var statuses = string.Join(",", query.Statuses.Select(i => "'" + i.ToString() + "'").ToArray()); - - whereClauses.Add(string.Format("Status in ({0})", statuses)); - } - - var whereTextWithoutPaging = whereClauses.Count == 0 ? - string.Empty : - " where " + string.Join(" AND ", whereClauses.ToArray()); - - var startIndex = query.StartIndex ?? 0; - if (startIndex > 0) - { - whereClauses.Add(string.Format("Id NOT IN (SELECT Id FROM SyncJobItems ORDER BY JobItemIndex, DateCreated LIMIT {0})", - startIndex.ToString(_usCulture))); - } - - if (whereClauses.Count > 0) - { - cmd.CommandText += " where " + string.Join(" AND ", whereClauses.ToArray()); - } - - cmd.CommandText += " ORDER BY JobItemIndex, DateCreated"; - - if (query.Limit.HasValue) - { - cmd.CommandText += " LIMIT " + query.Limit.Value.ToString(_usCulture); - } - - cmd.CommandText += "; select count (Id) from SyncJobItems" + whereTextWithoutPaging; - - var list = new List(); - var count = 0; - - using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)) - { - while (reader.Read()) - { - list.Add(itemFactory(reader)); - } - - if (reader.NextResult() && reader.Read()) - { - count = reader.GetInt32(0); - } - } - - return new QueryResult() - { - Items = list.ToArray(), - TotalRecordCount = count - }; - } - } - } - - public Dictionary GetSyncedItemProgresses(SyncJobItemQuery query) - { - var result = new Dictionary(); - - var now = DateTime.UtcNow; - - using (var connection = CreateConnection(true).Result) - { - using (var cmd = connection.CreateCommand()) - { - cmd.CommandText = "select ItemId,Status,Progress from SyncJobItems"; - - var whereClauses = new List(); - - if (!string.IsNullOrWhiteSpace(query.TargetId)) - { - whereClauses.Add("TargetId=@TargetId"); - cmd.Parameters.Add(cmd, "@TargetId", DbType.String).Value = query.TargetId; - } - - if (query.Statuses.Length > 0) - { - var statuses = string.Join(",", query.Statuses.Select(i => "'" + i.ToString() + "'").ToArray()); - - whereClauses.Add(string.Format("Status in ({0})", statuses)); - } - - if (whereClauses.Count > 0) - { - cmd.CommandText += " where " + string.Join(" AND ", whereClauses.ToArray()); - } - - cmd.CommandText += ";" + cmd.CommandText - .Replace("select ItemId,Status,Progress from SyncJobItems", "select ItemIds,Status,Progress from SyncJobs") - .Replace("'Synced'", "'Completed','CompletedWithError'"); - - //Logger.Debug(cmd.CommandText); - - using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)) - { - LogQueryTime("GetSyncedItemProgresses", cmd, now); - - while (reader.Read()) - { - AddStatusResult(reader, result, false); - } - - if (reader.NextResult()) - { - while (reader.Read()) - { - AddStatusResult(reader, result, true); - } - } - } - } - } - - return result; - } - - private void LogQueryTime(string methodName, IDbCommand cmd, DateTime startDate) - { - var elapsed = (DateTime.UtcNow - startDate).TotalMilliseconds; - - var slowThreshold = 1000; - -#if DEBUG - slowThreshold = 50; -#endif - - if (elapsed >= slowThreshold) - { - Logger.Debug("{2} query time (slow): {0}ms. Query: {1}", - Convert.ToInt32(elapsed), - cmd.CommandText, - methodName); - } - else - { - //Logger.Debug("{2} query time: {0}ms. Query: {1}", - // Convert.ToInt32(elapsed), - // cmd.CommandText, - // methodName); - } - } - - private void AddStatusResult(IDataReader reader, Dictionary result, bool multipleIds) - { - if (reader.IsDBNull(0)) - { - return; - } - - var itemIds = new List(); - - var ids = reader.GetString(0); - - if (multipleIds) - { - itemIds = ids.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList(); - } - else - { - itemIds.Add(ids); - } - - if (!reader.IsDBNull(1)) - { - SyncJobItemStatus status; - var statusString = reader.GetString(1); - if (string.Equals(statusString, "Completed", StringComparison.OrdinalIgnoreCase) || - string.Equals(statusString, "CompletedWithError", StringComparison.OrdinalIgnoreCase)) - { - status = SyncJobItemStatus.Synced; - } - else - { - status = (SyncJobItemStatus)Enum.Parse(typeof(SyncJobItemStatus), statusString, true); - } - - if (status == SyncJobItemStatus.Synced) - { - foreach (var itemId in itemIds) - { - result[itemId] = new SyncedItemProgress - { - Status = SyncJobItemStatus.Synced - }; - } - } - else - { - double progress = reader.IsDBNull(2) ? 0.0 : reader.GetDouble(2); - - foreach (var itemId in itemIds) - { - SyncedItemProgress currentStatus; - if (!result.TryGetValue(itemId, out currentStatus) || (currentStatus.Status != SyncJobItemStatus.Synced && progress >= currentStatus.Progress)) - { - result[itemId] = new SyncedItemProgress - { - Status = status, - Progress = progress - }; - } - } - } - } - } - - public QueryResult GetJobItems(SyncJobItemQuery query) - { - return GetJobItemReader(query, BaseJobItemSelectText, GetJobItem); - } - - public Task Create(SyncJobItem jobItem) - { - return InsertOrUpdate(jobItem, true); - } - - public Task Update(SyncJobItem jobItem) - { - return InsertOrUpdate(jobItem, false); - } - - private async Task InsertOrUpdate(SyncJobItem jobItem, bool insert) - { - if (jobItem == null) - { - throw new ArgumentNullException("jobItem"); - } - - CheckDisposed(); - - using (var connection = await CreateConnection().ConfigureAwait(false)) - { - using (var cmd = connection.CreateCommand()) - { - if (insert) - { - cmd.CommandText = "insert into SyncJobItems (Id, ItemId, ItemName, MediaSourceId, JobId, TemporaryPath, OutputPath, Status, TargetId, DateCreated, Progress, AdditionalFiles, MediaSource, IsMarkedForRemoval, JobItemIndex, ItemDateModifiedTicks) values (@Id, @ItemId, @ItemName, @MediaSourceId, @JobId, @TemporaryPath, @OutputPath, @Status, @TargetId, @DateCreated, @Progress, @AdditionalFiles, @MediaSource, @IsMarkedForRemoval, @JobItemIndex, @ItemDateModifiedTicks)"; - - cmd.Parameters.Add(cmd, "@Id"); - cmd.Parameters.Add(cmd, "@ItemId"); - cmd.Parameters.Add(cmd, "@ItemName"); - cmd.Parameters.Add(cmd, "@MediaSourceId"); - cmd.Parameters.Add(cmd, "@JobId"); - cmd.Parameters.Add(cmd, "@TemporaryPath"); - cmd.Parameters.Add(cmd, "@OutputPath"); - cmd.Parameters.Add(cmd, "@Status"); - cmd.Parameters.Add(cmd, "@TargetId"); - cmd.Parameters.Add(cmd, "@DateCreated"); - cmd.Parameters.Add(cmd, "@Progress"); - cmd.Parameters.Add(cmd, "@AdditionalFiles"); - cmd.Parameters.Add(cmd, "@MediaSource"); - cmd.Parameters.Add(cmd, "@IsMarkedForRemoval"); - cmd.Parameters.Add(cmd, "@JobItemIndex"); - cmd.Parameters.Add(cmd, "@ItemDateModifiedTicks"); - } - else - { - // cmd - cmd.CommandText = "update SyncJobItems set ItemId=@ItemId,ItemName=@ItemName,MediaSourceId=@MediaSourceId,JobId=@JobId,TemporaryPath=@TemporaryPath,OutputPath=@OutputPath,Status=@Status,TargetId=@TargetId,DateCreated=@DateCreated,Progress=@Progress,AdditionalFiles=@AdditionalFiles,MediaSource=@MediaSource,IsMarkedForRemoval=@IsMarkedForRemoval,JobItemIndex=@JobItemIndex,ItemDateModifiedTicks=@ItemDateModifiedTicks where Id=@Id"; - - cmd.Parameters.Add(cmd, "@Id"); - cmd.Parameters.Add(cmd, "@ItemId"); - cmd.Parameters.Add(cmd, "@ItemName"); - cmd.Parameters.Add(cmd, "@MediaSourceId"); - cmd.Parameters.Add(cmd, "@JobId"); - cmd.Parameters.Add(cmd, "@TemporaryPath"); - cmd.Parameters.Add(cmd, "@OutputPath"); - cmd.Parameters.Add(cmd, "@Status"); - cmd.Parameters.Add(cmd, "@TargetId"); - cmd.Parameters.Add(cmd, "@DateCreated"); - cmd.Parameters.Add(cmd, "@Progress"); - cmd.Parameters.Add(cmd, "@AdditionalFiles"); - cmd.Parameters.Add(cmd, "@MediaSource"); - cmd.Parameters.Add(cmd, "@IsMarkedForRemoval"); - cmd.Parameters.Add(cmd, "@JobItemIndex"); - cmd.Parameters.Add(cmd, "@ItemDateModifiedTicks"); - } - - IDbTransaction transaction = null; - - try - { - transaction = connection.BeginTransaction(); - - var index = 0; - - cmd.GetParameter(index++).Value = new Guid(jobItem.Id); - cmd.GetParameter(index++).Value = jobItem.ItemId; - cmd.GetParameter(index++).Value = jobItem.ItemName; - cmd.GetParameter(index++).Value = jobItem.MediaSourceId; - cmd.GetParameter(index++).Value = jobItem.JobId; - cmd.GetParameter(index++).Value = jobItem.TemporaryPath; - cmd.GetParameter(index++).Value = jobItem.OutputPath; - cmd.GetParameter(index++).Value = jobItem.Status.ToString(); - cmd.GetParameter(index++).Value = jobItem.TargetId; - cmd.GetParameter(index++).Value = jobItem.DateCreated; - cmd.GetParameter(index++).Value = jobItem.Progress; - cmd.GetParameter(index++).Value = _json.SerializeToString(jobItem.AdditionalFiles); - cmd.GetParameter(index++).Value = jobItem.MediaSource == null ? null : _json.SerializeToString(jobItem.MediaSource); - cmd.GetParameter(index++).Value = jobItem.IsMarkedForRemoval; - cmd.GetParameter(index++).Value = jobItem.JobItemIndex; - cmd.GetParameter(index++).Value = jobItem.ItemDateModifiedTicks; - - cmd.Transaction = transaction; - - cmd.ExecuteNonQuery(); - - transaction.Commit(); - } - catch (OperationCanceledException) - { - if (transaction != null) - { - transaction.Rollback(); - } - - throw; - } - catch (Exception e) - { - Logger.ErrorException("Failed to save record:", e); - - if (transaction != null) - { - transaction.Rollback(); - } - - throw; - } - finally - { - if (transaction != null) - { - transaction.Dispose(); - } - } - } - } - } - - private SyncJobItem GetJobItem(IDataReader reader) - { - var info = new SyncJobItem - { - Id = reader.GetGuid(0).ToString("N"), - ItemId = reader.GetString(1) - }; - - if (!reader.IsDBNull(2)) - { - info.ItemName = reader.GetString(2); - } - - if (!reader.IsDBNull(3)) - { - info.MediaSourceId = reader.GetString(3); - } - - info.JobId = reader.GetString(4); - - if (!reader.IsDBNull(5)) - { - info.TemporaryPath = reader.GetString(5); - } - if (!reader.IsDBNull(6)) - { - info.OutputPath = reader.GetString(6); - } - - if (!reader.IsDBNull(7)) - { - info.Status = (SyncJobItemStatus)Enum.Parse(typeof(SyncJobItemStatus), reader.GetString(7), true); - } - - info.TargetId = reader.GetString(8); - - info.DateCreated = reader.GetDateTime(9).ToUniversalTime(); - - if (!reader.IsDBNull(10)) - { - info.Progress = reader.GetDouble(10); - } - - if (!reader.IsDBNull(11)) - { - var json = reader.GetString(11); - - if (!string.IsNullOrWhiteSpace(json)) - { - info.AdditionalFiles = _json.DeserializeFromString>(json); - } - } - - if (!reader.IsDBNull(12)) - { - var json = reader.GetString(12); - - if (!string.IsNullOrWhiteSpace(json)) - { - info.MediaSource = _json.DeserializeFromString(json); - } - } - - info.IsMarkedForRemoval = reader.GetBoolean(13); - info.JobItemIndex = reader.GetInt32(14); - - if (!reader.IsDBNull(15)) - { - info.ItemDateModifiedTicks = reader.GetInt64(15); - } - - return info; - } - } -} diff --git a/Emby.Server.Implementations/Activity/ActivityRepository.cs b/Emby.Server.Implementations/Activity/ActivityRepository.cs index ea9e537c9..7bc77402e 100644 --- a/Emby.Server.Implementations/Activity/ActivityRepository.cs +++ b/Emby.Server.Implementations/Activity/ActivityRepository.cs @@ -87,7 +87,7 @@ namespace Emby.Server.Implementations.Activity if (minDate.HasValue) { - whereClauses.Add("DateCreated>=@DateCreated"); + whereClauses.Add("DateCreated>=?"); paramList.Add(minDate.Value.ToDateTimeParamValue()); } diff --git a/Emby.Server.Implementations/Browser/BrowserLauncher.cs b/Emby.Server.Implementations/Browser/BrowserLauncher.cs new file mode 100644 index 000000000..05cde91e2 --- /dev/null +++ b/Emby.Server.Implementations/Browser/BrowserLauncher.cs @@ -0,0 +1,73 @@ +using MediaBrowser.Controller; +using System; + +namespace Emby.Server.Implementations.Browser +{ + /// + /// Class BrowserLauncher + /// + public static class BrowserLauncher + { + /// + /// Opens the dashboard page. + /// + /// The page. + /// The app host. + public static void OpenDashboardPage(string page, IServerApplicationHost appHost) + { + var url = appHost.GetLocalApiUrl("localhost") + "/web/" + page; + + OpenUrl(appHost, url); + } + + /// + /// Opens the community. + /// + public static void OpenCommunity(IServerApplicationHost appHost) + { + OpenUrl(appHost, "http://emby.media/community"); + } + + public static void OpenEmbyPremiere(IServerApplicationHost appHost) + { + OpenDashboardPage("supporterkey.html", appHost); + } + + /// + /// Opens the web client. + /// + /// The app host. + public static void OpenWebClient(IServerApplicationHost appHost) + { + OpenDashboardPage("index.html", appHost); + } + + /// + /// Opens the dashboard. + /// + /// The app host. + public static void OpenDashboard(IServerApplicationHost appHost) + { + OpenDashboardPage("dashboard.html", appHost); + } + + /// + /// Opens the URL. + /// + /// The URL. + private static void OpenUrl(IServerApplicationHost appHost, string url) + { + try + { + appHost.LaunchUrl(url); + } + catch (NotImplementedException) + { + + } + catch (Exception) + { + } + } + } +} diff --git a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs index 8febe83b2..c47a534d1 100644 --- a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs +++ b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs @@ -4,6 +4,7 @@ using System.Threading; using System.Threading.Tasks; using MediaBrowser.Model.Logging; using SQLitePCL.pretty; +using System.Linq; namespace Emby.Server.Implementations.Data { @@ -120,21 +121,30 @@ namespace Emby.Server.Implementations.Data } - protected void AddColumn(IDatabaseConnection connection, string table, string columnName, string type) + protected List GetColumnNames(IDatabaseConnection connection, string table) { + var list = new List(); + foreach (var row in connection.Query("PRAGMA table_info(" + table + ")")) { if (row[1].SQLiteType != SQLiteType.Null) { var name = row[1].ToString(); - if (string.Equals(name, columnName, StringComparison.OrdinalIgnoreCase)) - { - return; - } + list.Add(name); } } + return list; + } + + protected void AddColumn(IDatabaseConnection connection, string table, string columnName, string type, List existingColumnNames) + { + if (existingColumnNames.Contains(columnName, StringComparer.OrdinalIgnoreCase)) + { + return; + } + connection.ExecuteAll(string.Join(";", new string[] { "alter table " + table, diff --git a/Emby.Server.Implementations/Data/CleanDatabaseScheduledTask.cs b/Emby.Server.Implementations/Data/CleanDatabaseScheduledTask.cs index dd32e2cbd..3f11b6eb0 100644 --- a/Emby.Server.Implementations/Data/CleanDatabaseScheduledTask.cs +++ b/Emby.Server.Implementations/Data/CleanDatabaseScheduledTask.cs @@ -1,5 +1,4 @@ using MediaBrowser.Common.Progress; -using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Persistence; @@ -7,20 +6,13 @@ using MediaBrowser.Model.Entities; using MediaBrowser.Model.Logging; using System; using System.Collections.Generic; -using System.Globalization; using System.Linq; using System.Threading; using System.Threading.Tasks; -using MediaBrowser.Common.IO; using MediaBrowser.Model.IO; using MediaBrowser.Controller.Channels; using MediaBrowser.Controller.Entities.Audio; -using MediaBrowser.Controller.IO; -using MediaBrowser.Controller.LiveTv; -using MediaBrowser.Controller.Net; -using MediaBrowser.Model.Globalization; using MediaBrowser.Model.Tasks; -using Emby.Server.Implementations.ScheduledTasks; namespace Emby.Server.Implementations.Data { @@ -29,26 +21,14 @@ namespace Emby.Server.Implementations.Data private readonly ILibraryManager _libraryManager; private readonly IItemRepository _itemRepo; private readonly ILogger _logger; - private readonly IServerConfigurationManager _config; private readonly IFileSystem _fileSystem; - private readonly IHttpServer _httpServer; - private readonly ILocalizationManager _localization; - private readonly ITaskManager _taskManager; - public const int MigrationVersion = 23; - public static bool EnableUnavailableMessage = false; - const int LatestSchemaVersion = 109; - - public CleanDatabaseScheduledTask(ILibraryManager libraryManager, IItemRepository itemRepo, ILogger logger, IServerConfigurationManager config, IFileSystem fileSystem, IHttpServer httpServer, ILocalizationManager localization, ITaskManager taskManager) + public CleanDatabaseScheduledTask(ILibraryManager libraryManager, IItemRepository itemRepo, ILogger logger, IFileSystem fileSystem) { _libraryManager = libraryManager; _itemRepo = itemRepo; _logger = logger; - _config = config; _fileSystem = fileSystem; - _httpServer = httpServer; - _localization = localization; - _taskManager = taskManager; } public string Name @@ -68,8 +48,6 @@ namespace Emby.Server.Implementations.Data public async Task Execute(CancellationToken cancellationToken, IProgress progress) { - OnProgress(0); - // Ensure these objects are lazy loaded. // Without this there is a deadlock that will need to be investigated var rootChildren = _libraryManager.RootFolder.Children.ToList(); @@ -78,19 +56,7 @@ namespace Emby.Server.Implementations.Data var innerProgress = new ActionableProgress(); innerProgress.RegisterAction(p => { - double newPercentCommplete = .4 * p; - OnProgress(newPercentCommplete); - - progress.Report(newPercentCommplete); - }); - - await UpdateToLatestSchema(cancellationToken, innerProgress).ConfigureAwait(false); - - innerProgress = new ActionableProgress(); - innerProgress.RegisterAction(p => - { - double newPercentCommplete = 40 + .05 * p; - OnProgress(newPercentCommplete); + double newPercentCommplete = .45 * p; progress.Report(newPercentCommplete); }); await CleanDeadItems(cancellationToken, innerProgress).ConfigureAwait(false); @@ -100,122 +66,12 @@ namespace Emby.Server.Implementations.Data innerProgress.RegisterAction(p => { double newPercentCommplete = 45 + .55 * p; - OnProgress(newPercentCommplete); progress.Report(newPercentCommplete); }); await CleanDeletedItems(cancellationToken, innerProgress).ConfigureAwait(false); progress.Report(100); await _itemRepo.UpdateInheritedValues(cancellationToken).ConfigureAwait(false); - - if (_config.Configuration.MigrationVersion < MigrationVersion) - { - _config.Configuration.MigrationVersion = MigrationVersion; - _config.SaveConfiguration(); - } - - if (_config.Configuration.SchemaVersion < LatestSchemaVersion) - { - _config.Configuration.SchemaVersion = LatestSchemaVersion; - _config.SaveConfiguration(); - } - - if (EnableUnavailableMessage) - { - EnableUnavailableMessage = false; - _httpServer.GlobalResponse = null; - _taskManager.QueueScheduledTask(); - } - - _taskManager.SuspendTriggers = false; - } - - private void OnProgress(double newPercentCommplete) - { - if (EnableUnavailableMessage) - { - var html = "Emby"; - var text = _localization.GetLocalizedString("DbUpgradeMessage"); - html += string.Format(text, newPercentCommplete.ToString("N2", CultureInfo.InvariantCulture)); - - html += ""; - html += ""; - - _httpServer.GlobalResponse = html; - } - } - - private async Task UpdateToLatestSchema(CancellationToken cancellationToken, IProgress progress) - { - var itemIds = _libraryManager.GetItemIds(new InternalItemsQuery - { - IsCurrentSchema = false, - ExcludeItemTypes = new[] { typeof(LiveTvProgram).Name } - }); - - var numComplete = 0; - var numItems = itemIds.Count; - - _logger.Debug("Upgrading schema for {0} items", numItems); - - var list = new List(); - - foreach (var itemId in itemIds) - { - cancellationToken.ThrowIfCancellationRequested(); - - if (itemId != Guid.Empty) - { - // Somehow some invalid data got into the db. It probably predates the boundary checking - var item = _libraryManager.GetItemById(itemId); - - if (item != null) - { - list.Add(item); - } - } - - if (list.Count >= 1000) - { - try - { - await _itemRepo.SaveItems(list, cancellationToken).ConfigureAwait(false); - } - catch (OperationCanceledException) - { - throw; - } - catch (Exception ex) - { - _logger.ErrorException("Error saving item", ex); - } - - list.Clear(); - } - - numComplete++; - double percent = numComplete; - percent /= numItems; - progress.Report(percent * 100); - } - - if (list.Count > 0) - { - try - { - await _itemRepo.SaveItems(list, cancellationToken).ConfigureAwait(false); - } - catch (OperationCanceledException) - { - throw; - } - catch (Exception ex) - { - _logger.ErrorException("Error saving item", ex); - } - } - - progress.Report(100); } private async Task CleanDeadItems(CancellationToken cancellationToken, IProgress progress) diff --git a/Emby.Server.Implementations/Emby.Server.Implementations.csproj b/Emby.Server.Implementations/Emby.Server.Implementations.csproj index 653a6a9c1..973576a0d 100644 --- a/Emby.Server.Implementations/Emby.Server.Implementations.csproj +++ b/Emby.Server.Implementations/Emby.Server.Implementations.csproj @@ -36,6 +36,7 @@ + @@ -64,6 +65,7 @@ + @@ -174,6 +176,7 @@ + @@ -258,6 +261,7 @@ + diff --git a/Emby.Server.Implementations/EntryPoints/StartupWizard.cs b/Emby.Server.Implementations/EntryPoints/StartupWizard.cs new file mode 100644 index 000000000..424153f22 --- /dev/null +++ b/Emby.Server.Implementations/EntryPoints/StartupWizard.cs @@ -0,0 +1,59 @@ +using Emby.Server.Implementations.Browser; +using MediaBrowser.Controller; +using MediaBrowser.Controller.Plugins; +using MediaBrowser.Model.Logging; + +namespace Emby.Server.Implementations.EntryPoints +{ + /// + /// Class StartupWizard + /// + public class StartupWizard : IServerEntryPoint + { + /// + /// The _app host + /// + private readonly IServerApplicationHost _appHost; + /// + /// The _user manager + /// + private readonly ILogger _logger; + + /// + /// Initializes a new instance of the class. + /// + /// The app host. + /// The logger. + public StartupWizard(IServerApplicationHost appHost, ILogger logger) + { + _appHost = appHost; + _logger = logger; + } + + /// + /// Runs this instance. + /// + public void Run() + { + if (_appHost.IsFirstRun) + { + LaunchStartupWizard(); + } + } + + /// + /// Launches the startup wizard. + /// + private void LaunchStartupWizard() + { + BrowserLauncher.OpenDashboardPage("wizardstart.html", _appHost); + } + + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// + public void Dispose() + { + } + } +} \ No newline at end of file diff --git a/Emby.Server.Implementations/FileOrganization/OrganizerScheduledTask.cs b/Emby.Server.Implementations/FileOrganization/OrganizerScheduledTask.cs index 5be7ba7ad..48a85e0e0 100644 --- a/Emby.Server.Implementations/FileOrganization/OrganizerScheduledTask.cs +++ b/Emby.Server.Implementations/FileOrganization/OrganizerScheduledTask.cs @@ -74,7 +74,7 @@ namespace Emby.Server.Implementations.FileOrganization return new[] { // Every so often - new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerInterval, IntervalTicks = TimeSpan.FromMinutes(5).Ticks} + new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerInterval, IntervalTicks = TimeSpan.FromMinutes(15).Ticks} }; } diff --git a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs index 876d140ec..c1758127a 100644 --- a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs +++ b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs @@ -59,12 +59,13 @@ namespace Emby.Server.Implementations.HttpServer private readonly IEnvironmentInfo _environment; private readonly IStreamFactory _streamFactory; private readonly Func> _funcParseFn; + private readonly bool _enableDualModeSockets; public HttpListenerHost(IServerApplicationHost applicationHost, ILogger logger, IServerConfigurationManager config, string serviceName, - string defaultRedirectPath, INetworkManager networkManager, IMemoryStreamFactory memoryStreamProvider, ITextEncoding textEncoding, ISocketFactory socketFactory, ICryptoProvider cryptoProvider, IJsonSerializer jsonSerializer, IXmlSerializer xmlSerializer, IEnvironmentInfo environment, ICertificate certificate, IStreamFactory streamFactory, Func> funcParseFn) + string defaultRedirectPath, INetworkManager networkManager, IMemoryStreamFactory memoryStreamProvider, ITextEncoding textEncoding, ISocketFactory socketFactory, ICryptoProvider cryptoProvider, IJsonSerializer jsonSerializer, IXmlSerializer xmlSerializer, IEnvironmentInfo environment, ICertificate certificate, IStreamFactory streamFactory, Func> funcParseFn, bool enableDualModeSockets) : base(serviceName) { _appHost = applicationHost; @@ -80,6 +81,7 @@ namespace Emby.Server.Implementations.HttpServer _certificate = certificate; _streamFactory = streamFactory; _funcParseFn = funcParseFn; + _enableDualModeSockets = enableDualModeSockets; _config = config; _logger = logger; @@ -179,8 +181,6 @@ namespace Emby.Server.Implementations.HttpServer private IHttpListener GetListener() { - var enableDualMode = _environment.OperatingSystem == OperatingSystem.Windows; - return new WebSocketSharpListener(_logger, _certificate, _memoryStreamProvider, @@ -189,7 +189,7 @@ namespace Emby.Server.Implementations.HttpServer _socketFactory, _cryptoProvider, _streamFactory, - enableDualMode, + _enableDualModeSockets, GetRequest); } diff --git a/Emby.Server.Implementations/Migrations/UpdateLevelMigration.cs b/Emby.Server.Implementations/Migrations/UpdateLevelMigration.cs new file mode 100644 index 000000000..c532ea08d --- /dev/null +++ b/Emby.Server.Implementations/Migrations/UpdateLevelMigration.cs @@ -0,0 +1,130 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using MediaBrowser.Common.Net; +using MediaBrowser.Common.Updates; +using MediaBrowser.Controller; +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Model.Logging; +using MediaBrowser.Model.Serialization; +using MediaBrowser.Model.Updates; + +namespace Emby.Server.Implementations.Migrations +{ + public class UpdateLevelMigration : IVersionMigration + { + private readonly IServerConfigurationManager _config; + private readonly IServerApplicationHost _appHost; + private readonly IHttpClient _httpClient; + private readonly IJsonSerializer _jsonSerializer; + private readonly string _releaseAssetFilename; + private readonly ILogger _logger; + + public UpdateLevelMigration(IServerConfigurationManager config, IServerApplicationHost appHost, IHttpClient httpClient, IJsonSerializer jsonSerializer, string releaseAssetFilename, ILogger logger) + { + _config = config; + _appHost = appHost; + _httpClient = httpClient; + _jsonSerializer = jsonSerializer; + _releaseAssetFilename = releaseAssetFilename; + _logger = logger; + } + + public async Task Run() + { + var lastVersion = _config.Configuration.LastVersion; + var currentVersion = _appHost.ApplicationVersion; + + if (string.Equals(lastVersion, currentVersion.ToString(), StringComparison.OrdinalIgnoreCase)) + { + return; + } + + try + { + var updateLevel = _config.Configuration.SystemUpdateLevel; + + await CheckVersion(currentVersion, updateLevel, CancellationToken.None).ConfigureAwait(false); + } + catch (Exception ex) + { + _logger.ErrorException("Error in update migration", ex); + } + } + + private async Task CheckVersion(Version currentVersion, PackageVersionClass currentUpdateLevel, CancellationToken cancellationToken) + { + var releases = await new GithubUpdater(_httpClient, _jsonSerializer) + .GetLatestReleases("MediaBrowser", "Emby", _releaseAssetFilename, cancellationToken).ConfigureAwait(false); + + var newUpdateLevel = GetNewUpdateLevel(currentVersion, currentUpdateLevel, releases); + + if (newUpdateLevel != currentUpdateLevel) + { + _config.Configuration.SystemUpdateLevel = newUpdateLevel; + _config.SaveConfiguration(); + } + } + + private PackageVersionClass GetNewUpdateLevel(Version currentVersion, PackageVersionClass currentUpdateLevel, List releases) + { + var newUpdateLevel = currentUpdateLevel; + + // If the current version is later than current stable, set the update level to beta + if (releases.Count >= 1) + { + var release = releases[0]; + var version = ParseVersion(release.tag_name); + if (version != null) + { + if (currentVersion > version) + { + newUpdateLevel = PackageVersionClass.Beta; + } + else + { + return PackageVersionClass.Release; + } + } + } + + // If the current version is later than current beta, set the update level to dev + if (releases.Count >= 2) + { + var release = releases[1]; + var version = ParseVersion(release.tag_name); + if (version != null) + { + if (currentVersion > version) + { + newUpdateLevel = PackageVersionClass.Dev; + } + else + { + return PackageVersionClass.Beta; + } + } + } + + return newUpdateLevel; + } + + private Version ParseVersion(string versionString) + { + if (!string.IsNullOrWhiteSpace(versionString)) + { + var parts = versionString.Split('.'); + if (parts.Length == 3) + { + versionString += ".0"; + } + } + + Version version; + Version.TryParse(versionString, out version); + + return version; + } + } +} diff --git a/Emby.Server.Implementations/Security/AuthenticationRepository.cs b/Emby.Server.Implementations/Security/AuthenticationRepository.cs index 5179bd258..f4cb42d29 100644 --- a/Emby.Server.Implementations/Security/AuthenticationRepository.cs +++ b/Emby.Server.Implementations/Security/AuthenticationRepository.cs @@ -40,7 +40,9 @@ namespace Emby.Server.Implementations.Security connection.RunInTransaction(db => { - AddColumn(db, "AccessTokens", "AppVersion", "TEXT"); + var existingColumnNames = GetColumnNames(db, "AccessTokens"); + + AddColumn(db, "AccessTokens", "AppVersion", "TEXT", existingColumnNames); }); } } diff --git a/Emby.Server.Implementations/Sync/SyncRepository.cs b/Emby.Server.Implementations/Sync/SyncRepository.cs new file mode 100644 index 000000000..fbc5772f3 --- /dev/null +++ b/Emby.Server.Implementations/Sync/SyncRepository.cs @@ -0,0 +1,770 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Emby.Server.Implementations.Data; +using MediaBrowser.Controller; +using MediaBrowser.Controller.Sync; +using MediaBrowser.Model.Dto; +using MediaBrowser.Model.Logging; +using MediaBrowser.Model.Querying; +using MediaBrowser.Model.Serialization; +using MediaBrowser.Model.Sync; +using SQLitePCL.pretty; + +namespace Emby.Server.Implementations.Sync +{ + public class SyncRepository : BaseSqliteRepository, ISyncRepository + { + private readonly CultureInfo _usCulture = new CultureInfo("en-US"); + + private readonly IJsonSerializer _json; + + public SyncRepository(ILogger logger, IJsonSerializer json, IServerApplicationPaths appPaths) + : base(logger) + { + _json = json; + DbFilePath = Path.Combine(appPaths.DataPath, "sync14.db"); + } + + private class SyncSummary + { + public Dictionary Items { get; set; } + + public SyncSummary() + { + Items = new Dictionary(); + } + } + + public void Initialize() + { + using (var connection = CreateConnection()) + { + string[] queries = { + + "create table if not exists SyncJobs (Id GUID PRIMARY KEY, TargetId TEXT NOT NULL, Name TEXT NOT NULL, Profile TEXT, Quality TEXT, Bitrate INT, Status TEXT NOT NULL, Progress FLOAT, UserId TEXT NOT NULL, ItemIds TEXT NOT NULL, Category TEXT, ParentId TEXT, UnwatchedOnly BIT, ItemLimit INT, SyncNewContent BIT, DateCreated DateTime, DateLastModified DateTime, ItemCount int)", + + "create table if not exists SyncJobItems (Id GUID PRIMARY KEY, ItemId TEXT, ItemName TEXT, MediaSourceId TEXT, JobId TEXT, TemporaryPath TEXT, OutputPath TEXT, Status TEXT, TargetId TEXT, DateCreated DateTime, Progress FLOAT, AdditionalFiles TEXT, MediaSource TEXT, IsMarkedForRemoval BIT, JobItemIndex INT, ItemDateModifiedTicks BIGINT)", + + "drop index if exists idx_SyncJobItems2", + "drop index if exists idx_SyncJobItems3", + "drop index if exists idx_SyncJobs1", + "drop index if exists idx_SyncJobs", + "drop index if exists idx_SyncJobItems1", + "create index if not exists idx_SyncJobItems4 on SyncJobItems(TargetId,ItemId,Status,Progress,DateCreated)", + "create index if not exists idx_SyncJobItems5 on SyncJobItems(TargetId,Status,ItemId,Progress)", + + "create index if not exists idx_SyncJobs2 on SyncJobs(TargetId,Status,ItemIds,Progress)", + + "pragma shrink_memory" + }; + + connection.RunQueries(queries); + + connection.RunInTransaction(db => + { + var existingColumnNames = GetColumnNames(db, "SyncJobs"); + AddColumn(db, "SyncJobs", "Profile", "TEXT", existingColumnNames); + AddColumn(db, "SyncJobs", "Bitrate", "INT", existingColumnNames); + + existingColumnNames = GetColumnNames(db, "SyncJobItems"); + AddColumn(db, "SyncJobItems", "ItemDateModifiedTicks", "BIGINT", existingColumnNames); + }); + } + } + + private const string BaseJobSelectText = "select Id, TargetId, Name, Profile, Quality, Bitrate, Status, Progress, UserId, ItemIds, Category, ParentId, UnwatchedOnly, ItemLimit, SyncNewContent, DateCreated, DateLastModified, ItemCount from SyncJobs"; + private const string BaseJobItemSelectText = "select Id, ItemId, ItemName, MediaSourceId, JobId, TemporaryPath, OutputPath, Status, TargetId, DateCreated, Progress, AdditionalFiles, MediaSource, IsMarkedForRemoval, JobItemIndex, ItemDateModifiedTicks from SyncJobItems"; + + public SyncJob GetJob(string id) + { + if (string.IsNullOrEmpty(id)) + { + throw new ArgumentNullException("id"); + } + + CheckDisposed(); + + var guid = new Guid(id); + + if (guid == Guid.Empty) + { + throw new ArgumentNullException("id"); + } + + lock (WriteLock) + { + using (var connection = CreateConnection(true)) + { + var commandText = BaseJobSelectText + " where Id=?"; + var paramList = new List(); + + paramList.Add(guid.ToGuidParamValue()); + + foreach (var row in connection.Query(commandText, paramList.ToArray())) + { + return GetJob(row); + } + + return null; + } + } + } + + private SyncJob GetJob(IReadOnlyList reader) + { + var info = new SyncJob + { + Id = reader[0].ReadGuid().ToString("N"), + TargetId = reader[1].ToString(), + Name = reader[2].ToString() + }; + + if (reader[3].SQLiteType != SQLiteType.Null) + { + info.Profile = reader[3].ToString(); + } + + if (reader[4].SQLiteType != SQLiteType.Null) + { + info.Quality = reader[4].ToString(); + } + + if (reader[5].SQLiteType != SQLiteType.Null) + { + info.Bitrate = reader[5].ToInt(); + } + + if (reader[6].SQLiteType != SQLiteType.Null) + { + info.Status = (SyncJobStatus)Enum.Parse(typeof(SyncJobStatus), reader[6].ToString(), true); + } + + if (reader[7].SQLiteType != SQLiteType.Null) + { + info.Progress = reader[7].ToDouble(); + } + + if (reader[8].SQLiteType != SQLiteType.Null) + { + info.UserId = reader[8].ToString(); + } + + if (reader[9].SQLiteType != SQLiteType.Null) + { + info.RequestedItemIds = reader[9].ToString().Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList(); + } + + if (reader[10].SQLiteType != SQLiteType.Null) + { + info.Category = (SyncCategory)Enum.Parse(typeof(SyncCategory), reader[10].ToString(), true); + } + + if (reader[11].SQLiteType != SQLiteType.Null) + { + info.ParentId = reader[11].ToString(); + } + + if (reader[12].SQLiteType != SQLiteType.Null) + { + info.UnwatchedOnly = reader[12].ToBool(); + } + + if (reader[13].SQLiteType != SQLiteType.Null) + { + info.ItemLimit = reader[13].ToInt(); + } + + info.SyncNewContent = reader[14].ToBool(); + + info.DateCreated = reader[15].ReadDateTime(); + info.DateLastModified = reader[16].ReadDateTime(); + info.ItemCount = reader[17].ToInt(); + + return info; + } + + public Task Create(SyncJob job) + { + return InsertOrUpdate(job, true); + } + + public Task Update(SyncJob job) + { + return InsertOrUpdate(job, false); + } + + private async Task InsertOrUpdate(SyncJob job, bool insert) + { + if (job == null) + { + throw new ArgumentNullException("job"); + } + + CheckDisposed(); + + lock (WriteLock) + { + using (var connection = CreateConnection()) + { + string commandText; + var paramList = new List(); + + if (insert) + { + commandText = "insert into SyncJobs (Id, TargetId, Name, Profile, Quality, Bitrate, Status, Progress, UserId, ItemIds, Category, ParentId, UnwatchedOnly, ItemLimit, SyncNewContent, DateCreated, DateLastModified, ItemCount) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + } + else + { + commandText = "update SyncJobs set TargetId=?,Name=?,Profile=?,Quality=?,Bitrate=?,Status=?,Progress=?,UserId=?,ItemIds=?,Category=?,ParentId=?,UnwatchedOnly=?,ItemLimit=?,SyncNewContent=?,DateCreated=?,DateLastModified=?,ItemCount=? where Id=?"; + } + + paramList.Add(job.Id.ToGuidParamValue()); + paramList.Add(job.TargetId); + paramList.Add(job.Name); + paramList.Add(job.Profile); + paramList.Add(job.Quality); + paramList.Add(job.Bitrate); + paramList.Add(job.Status.ToString()); + paramList.Add(job.Progress); + paramList.Add(job.UserId); + + paramList.Add(string.Join(",", job.RequestedItemIds.ToArray())); + paramList.Add(job.Category); + paramList.Add(job.ParentId); + paramList.Add(job.UnwatchedOnly); + paramList.Add(job.ItemLimit); + paramList.Add(job.SyncNewContent); + paramList.Add(job.DateCreated.ToDateTimeParamValue()); + paramList.Add(job.DateLastModified.ToDateTimeParamValue()); + paramList.Add(job.ItemCount); + + connection.RunInTransaction(conn => + { + conn.Execute(commandText, paramList.ToArray()); + }); + } + } + } + + public async Task DeleteJob(string id) + { + if (string.IsNullOrWhiteSpace(id)) + { + throw new ArgumentNullException("id"); + } + + CheckDisposed(); + + lock (WriteLock) + { + using (var connection = CreateConnection()) + { + connection.RunInTransaction(conn => + { + conn.Execute("delete from SyncJobs where Id=?", id.ToGuidParamValue()); + conn.Execute("delete from SyncJobItems where JobId=?", id); + }); + } + } + } + + public QueryResult GetJobs(SyncJobQuery query) + { + if (query == null) + { + throw new ArgumentNullException("query"); + } + + CheckDisposed(); + + lock (WriteLock) + { + using (var connection = CreateConnection(true)) + { + var commandText = BaseJobSelectText; + var paramList = new List(); + + var whereClauses = new List(); + + if (query.Statuses.Length > 0) + { + var statuses = string.Join(",", query.Statuses.Select(i => "'" + i.ToString() + "'").ToArray()); + + whereClauses.Add(string.Format("Status in ({0})", statuses)); + } + if (!string.IsNullOrWhiteSpace(query.TargetId)) + { + whereClauses.Add("TargetId=?"); + paramList.Add(query.TargetId); + } + if (!string.IsNullOrWhiteSpace(query.ExcludeTargetIds)) + { + var excludeIds = (query.ExcludeTargetIds ?? string.Empty).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); + if (excludeIds.Length == 1) + { + whereClauses.Add("TargetId<>?"); + paramList.Add(excludeIds[0]); + } + else if (excludeIds.Length > 1) + { + whereClauses.Add("TargetId<>?"); + paramList.Add(excludeIds[0]); + } + } + if (!string.IsNullOrWhiteSpace(query.UserId)) + { + whereClauses.Add("UserId=?"); + paramList.Add(query.UserId); + } + if (query.SyncNewContent.HasValue) + { + whereClauses.Add("SyncNewContent=?"); + paramList.Add(query.SyncNewContent.Value); + } + + commandText += " mainTable"; + + var whereTextWithoutPaging = whereClauses.Count == 0 ? + string.Empty : + " where " + string.Join(" AND ", whereClauses.ToArray()); + + var startIndex = query.StartIndex ?? 0; + if (startIndex > 0) + { + whereClauses.Add(string.Format("Id NOT IN (SELECT Id FROM SyncJobs ORDER BY (Select Max(DateLastModified) from SyncJobs where TargetId=mainTable.TargetId) DESC, DateLastModified DESC LIMIT {0})", + startIndex.ToString(_usCulture))); + } + + if (whereClauses.Count > 0) + { + commandText += " where " + string.Join(" AND ", whereClauses.ToArray()); + } + + commandText += " ORDER BY (Select Max(DateLastModified) from SyncJobs where TargetId=mainTable.TargetId) DESC, DateLastModified DESC"; + + if (query.Limit.HasValue) + { + commandText += " LIMIT " + query.Limit.Value.ToString(_usCulture); + } + + var list = new List(); + var count = connection.Query("select count (Id) from SyncJobs" + whereTextWithoutPaging, paramList.ToArray()) + .SelectScalarInt() + .First(); + + foreach (var row in connection.Query(commandText, paramList.ToArray())) + { + list.Add(GetJob(row)); + } + + return new QueryResult() + { + Items = list.ToArray(), + TotalRecordCount = count + }; + } + } + } + + public SyncJobItem GetJobItem(string id) + { + if (string.IsNullOrEmpty(id)) + { + throw new ArgumentNullException("id"); + } + + CheckDisposed(); + + lock (WriteLock) + { + var guid = new Guid(id); + + using (var connection = CreateConnection(true)) + { + var commandText = BaseJobItemSelectText + " where Id=?"; + var paramList = new List(); + + paramList.Add(guid.ToGuidParamValue()); + + foreach (var row in connection.Query(commandText, paramList.ToArray())) + { + return GetJobItem(row); + } + + return null; + } + } + } + + private QueryResult GetJobItemReader(SyncJobItemQuery query, string baseSelectText, Func, T> itemFactory) + { + if (query == null) + { + throw new ArgumentNullException("query"); + } + + lock (WriteLock) + { + using (var connection = CreateConnection(true)) + { + var commandText = baseSelectText; + var paramList = new List(); + + var whereClauses = new List(); + + if (!string.IsNullOrWhiteSpace(query.JobId)) + { + whereClauses.Add("JobId=?"); + paramList.Add(query.JobId); + } + if (!string.IsNullOrWhiteSpace(query.ItemId)) + { + whereClauses.Add("ItemId=?"); + paramList.Add(query.ItemId); + } + if (!string.IsNullOrWhiteSpace(query.TargetId)) + { + whereClauses.Add("TargetId=?"); + paramList.Add(query.TargetId); + } + + if (query.Statuses.Length > 0) + { + var statuses = string.Join(",", query.Statuses.Select(i => "'" + i.ToString() + "'").ToArray()); + + whereClauses.Add(string.Format("Status in ({0})", statuses)); + } + + var whereTextWithoutPaging = whereClauses.Count == 0 ? + string.Empty : + " where " + string.Join(" AND ", whereClauses.ToArray()); + + var startIndex = query.StartIndex ?? 0; + if (startIndex > 0) + { + whereClauses.Add(string.Format("Id NOT IN (SELECT Id FROM SyncJobItems ORDER BY JobItemIndex, DateCreated LIMIT {0})", + startIndex.ToString(_usCulture))); + } + + if (whereClauses.Count > 0) + { + commandText += " where " + string.Join(" AND ", whereClauses.ToArray()); + } + + commandText += " ORDER BY JobItemIndex, DateCreated"; + + if (query.Limit.HasValue) + { + commandText += " LIMIT " + query.Limit.Value.ToString(_usCulture); + } + + var list = new List(); + var count = connection.Query("select count (Id) from SyncJobItems" + whereTextWithoutPaging, paramList.ToArray()) + .SelectScalarInt() + .First(); + + foreach (var row in connection.Query(commandText, paramList.ToArray())) + { + list.Add(itemFactory(row)); + } + + return new QueryResult() + { + Items = list.ToArray(), + TotalRecordCount = count + }; + } + } + } + + public Dictionary GetSyncedItemProgresses(SyncJobItemQuery query) + { + var result = new Dictionary(); + + var now = DateTime.UtcNow; + + lock (WriteLock) + { + using (var connection = CreateConnection(true)) + { + var commandText = "select ItemId,Status,Progress from SyncJobItems"; + + var whereClauses = new List(); + var paramList = new List(); + + if (!string.IsNullOrWhiteSpace(query.TargetId)) + { + whereClauses.Add("TargetId=?"); + paramList.Add(query.TargetId); + } + + if (query.Statuses.Length > 0) + { + var statuses = string.Join(",", query.Statuses.Select(i => "'" + i.ToString() + "'").ToArray()); + + whereClauses.Add(string.Format("Status in ({0})", statuses)); + } + + if (whereClauses.Count > 0) + { + commandText += " where " + string.Join(" AND ", whereClauses.ToArray()); + } + + foreach (var row in connection.Query(commandText, paramList.ToArray())) + { + AddStatusResult(row, result, false); + } + LogQueryTime("GetSyncedItemProgresses", commandText, now); + + commandText = commandText + .Replace("select ItemId,Status,Progress from SyncJobItems", "select ItemIds,Status,Progress from SyncJobs") + .Replace("'Synced'", "'Completed','CompletedWithError'"); + + now = DateTime.UtcNow; + foreach (var row in connection.Query(commandText, paramList.ToArray())) + { + AddStatusResult(row, result, true); + } + LogQueryTime("GetSyncedItemProgresses", commandText, now); + } + } + + return result; + } + + private void LogQueryTime(string methodName, string commandText, DateTime startDate) + { + var elapsed = (DateTime.UtcNow - startDate).TotalMilliseconds; + + var slowThreshold = 1000; + +#if DEBUG + slowThreshold = 50; +#endif + + if (elapsed >= slowThreshold) + { + Logger.Debug("{2} query time (slow): {0}ms. Query: {1}", + Convert.ToInt32(elapsed), + commandText, + methodName); + } + else + { + //Logger.Debug("{2} query time: {0}ms. Query: {1}", + // Convert.ToInt32(elapsed), + // cmd.CommandText, + // methodName); + } + } + + private void AddStatusResult(IReadOnlyList reader, Dictionary result, bool multipleIds) + { + if (reader[0].SQLiteType == SQLiteType.Null) + { + return; + } + + var itemIds = new List(); + + var ids = reader[0].ToString(); + + if (multipleIds) + { + itemIds = ids.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList(); + } + else + { + itemIds.Add(ids); + } + + if (reader[1].SQLiteType != SQLiteType.Null) + { + SyncJobItemStatus status; + var statusString = reader[1].ToString(); + if (string.Equals(statusString, "Completed", StringComparison.OrdinalIgnoreCase) || + string.Equals(statusString, "CompletedWithError", StringComparison.OrdinalIgnoreCase)) + { + status = SyncJobItemStatus.Synced; + } + else + { + status = (SyncJobItemStatus)Enum.Parse(typeof(SyncJobItemStatus), statusString, true); + } + + if (status == SyncJobItemStatus.Synced) + { + foreach (var itemId in itemIds) + { + result[itemId] = new SyncedItemProgress + { + Status = SyncJobItemStatus.Synced + }; + } + } + else + { + double progress = reader[2].SQLiteType == SQLiteType.Null ? 0.0 : reader[2].ToDouble(); + + foreach (var itemId in itemIds) + { + SyncedItemProgress currentStatus; + if (!result.TryGetValue(itemId, out currentStatus) || (currentStatus.Status != SyncJobItemStatus.Synced && progress >= currentStatus.Progress)) + { + result[itemId] = new SyncedItemProgress + { + Status = status, + Progress = progress + }; + } + } + } + } + } + + public QueryResult GetJobItems(SyncJobItemQuery query) + { + return GetJobItemReader(query, BaseJobItemSelectText, GetJobItem); + } + + public Task Create(SyncJobItem jobItem) + { + return InsertOrUpdate(jobItem, true); + } + + public Task Update(SyncJobItem jobItem) + { + return InsertOrUpdate(jobItem, false); + } + + private async Task InsertOrUpdate(SyncJobItem jobItem, bool insert) + { + if (jobItem == null) + { + throw new ArgumentNullException("jobItem"); + } + + CheckDisposed(); + + lock (WriteLock) + { + using (var connection = CreateConnection()) + { + string commandText; + + if (insert) + { + commandText = "insert into SyncJobItems (Id, ItemId, ItemName, MediaSourceId, JobId, TemporaryPath, OutputPath, Status, TargetId, DateCreated, Progress, AdditionalFiles, MediaSource, IsMarkedForRemoval, JobItemIndex, ItemDateModifiedTicks) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + } + else + { + // cmd + commandText = "update SyncJobItems set ItemId=?,ItemName=?,MediaSourceId=?,JobId=?,TemporaryPath=?,OutputPath=?,Status=?,TargetId=?,DateCreated=?,Progress=?,AdditionalFiles=?,MediaSource=?,IsMarkedForRemoval=?,JobItemIndex=?,ItemDateModifiedTicks=? where Id=?"; + } + + var paramList = new List(); + paramList.Add(jobItem.Id.ToGuidParamValue()); + paramList.Add(jobItem.ItemId); + paramList.Add(jobItem.ItemName); + paramList.Add(jobItem.MediaSourceId); + paramList.Add(jobItem.JobId); + paramList.Add(jobItem.TemporaryPath); + paramList.Add(jobItem.OutputPath); + paramList.Add(jobItem.Status.ToString()); + + paramList.Add(jobItem.TargetId); + paramList.Add(jobItem.DateCreated.ToDateTimeParamValue()); + paramList.Add(jobItem.Progress); + paramList.Add(_json.SerializeToString(jobItem.AdditionalFiles)); + paramList.Add(jobItem.MediaSource == null ? null : _json.SerializeToString(jobItem.MediaSource)); + paramList.Add(jobItem.IsMarkedForRemoval); + paramList.Add(jobItem.JobItemIndex); + paramList.Add(jobItem.ItemDateModifiedTicks); + + connection.RunInTransaction(conn => + { + conn.Execute(commandText, paramList.ToArray()); + }); + } + } + } + + private SyncJobItem GetJobItem(IReadOnlyList reader) + { + var info = new SyncJobItem + { + Id = reader[0].ReadGuid().ToString("N"), + ItemId = reader[1].ToString() + }; + + if (reader[2].SQLiteType != SQLiteType.Null) + { + info.ItemName = reader[2].ToString(); + } + + if (reader[3].SQLiteType != SQLiteType.Null) + { + info.MediaSourceId = reader[3].ToString(); + } + + info.JobId = reader[4].ToString(); + + if (reader[5].SQLiteType != SQLiteType.Null) + { + info.TemporaryPath = reader[5].ToString(); + } + if (reader[6].SQLiteType != SQLiteType.Null) + { + info.OutputPath = reader[6].ToString(); + } + + if (reader[7].SQLiteType != SQLiteType.Null) + { + info.Status = (SyncJobItemStatus)Enum.Parse(typeof(SyncJobItemStatus), reader[7].ToString(), true); + } + + info.TargetId = reader[8].ToString(); + + info.DateCreated = reader[9].ReadDateTime(); + + if (reader[10].SQLiteType != SQLiteType.Null) + { + info.Progress = reader[10].ToDouble(); + } + + if (reader[11].SQLiteType != SQLiteType.Null) + { + var json = reader[11].ToString(); + + if (!string.IsNullOrWhiteSpace(json)) + { + info.AdditionalFiles = _json.DeserializeFromString>(json); + } + } + + if (reader[12].SQLiteType != SQLiteType.Null) + { + var json = reader[12].ToString(); + + if (!string.IsNullOrWhiteSpace(json)) + { + info.MediaSource = _json.DeserializeFromString(json); + } + } + + info.IsMarkedForRemoval = reader[13].ToBool(); + info.JobItemIndex = reader[14].ToInt(); + + if (reader[15].SQLiteType != SQLiteType.Null) + { + info.ItemDateModifiedTicks = reader[15].ToInt64(); + } + + return info; + } + } +} diff --git a/Emby.Server.Implementations/TV/TVSeriesManager.cs b/Emby.Server.Implementations/TV/TVSeriesManager.cs index f3bab7883..a47aaa305 100644 --- a/Emby.Server.Implementations/TV/TVSeriesManager.cs +++ b/Emby.Server.Implementations/TV/TVSeriesManager.cs @@ -148,10 +148,6 @@ namespace Emby.Server.Implementations.TV private string GetUniqueSeriesKey(BaseItem series) { - if (_config.Configuration.SchemaVersion < 97) - { - return series.Id.ToString("N"); - } return series.GetPresentationUniqueKey(); } diff --git a/MediaBrowser.Api/StartupWizardService.cs b/MediaBrowser.Api/StartupWizardService.cs index 49fdcece1..4e5047f78 100644 --- a/MediaBrowser.Api/StartupWizardService.cs +++ b/MediaBrowser.Api/StartupWizardService.cs @@ -115,7 +115,6 @@ namespace MediaBrowser.Api config.EnableStandaloneMusicKeys = true; config.EnableCaseSensitiveItemIds = true; config.EnableFolderView = true; - config.SchemaVersion = 109; config.EnableSimpleArtistDetection = true; config.SkipDeserializationForBasicTypes = true; config.SkipDeserializationForPrograms = true; diff --git a/MediaBrowser.Common/MediaBrowser.Common.csproj b/MediaBrowser.Common/MediaBrowser.Common.csproj index 9e212219d..eb082f707 100644 --- a/MediaBrowser.Common/MediaBrowser.Common.csproj +++ b/MediaBrowser.Common/MediaBrowser.Common.csproj @@ -70,6 +70,7 @@ + diff --git a/MediaBrowser.Common/Updates/GithubUpdater.cs b/MediaBrowser.Common/Updates/GithubUpdater.cs new file mode 100644 index 000000000..c5000391d --- /dev/null +++ b/MediaBrowser.Common/Updates/GithubUpdater.cs @@ -0,0 +1,269 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using MediaBrowser.Common.Net; +using MediaBrowser.Model.Serialization; +using MediaBrowser.Model.Updates; + +namespace MediaBrowser.Common.Updates +{ + public class GithubUpdater + { + private readonly IHttpClient _httpClient; + private readonly IJsonSerializer _jsonSerializer; + + public GithubUpdater(IHttpClient httpClient, IJsonSerializer jsonSerializer) + { + _httpClient = httpClient; + _jsonSerializer = jsonSerializer; + } + + public async Task CheckForUpdateResult(string organzation, string repository, Version minVersion, PackageVersionClass updateLevel, string assetFilename, string packageName, string targetFilename, TimeSpan cacheLength, CancellationToken cancellationToken) + { + var url = string.Format("https://api.github.com/repos/{0}/{1}/releases", organzation, repository); + + var options = new HttpRequestOptions + { + Url = url, + EnableKeepAlive = false, + CancellationToken = cancellationToken, + UserAgent = "Emby/3.0", + BufferContent = false + }; + + if (cacheLength.Ticks > 0) + { + options.CacheMode = CacheMode.Unconditional; + options.CacheLength = cacheLength; + } + + using (var stream = await _httpClient.Get(options).ConfigureAwait(false)) + { + var obj = _jsonSerializer.DeserializeFromStream(stream); + + return CheckForUpdateResult(obj, minVersion, updateLevel, assetFilename, packageName, targetFilename); + } + } + + private CheckForUpdateResult CheckForUpdateResult(RootObject[] obj, Version minVersion, PackageVersionClass updateLevel, string assetFilename, string packageName, string targetFilename) + { + if (updateLevel == PackageVersionClass.Release) + { + // Technically all we need to do is check that it's not pre-release + // But let's addititional checks for -beta and -dev to handle builds that might be temporarily tagged incorrectly. + obj = obj.Where(i => !i.prerelease && !i.name.EndsWith("-beta", StringComparison.OrdinalIgnoreCase) && !i.name.EndsWith("-dev", StringComparison.OrdinalIgnoreCase)).ToArray(); + } + else if (updateLevel == PackageVersionClass.Beta) + { + obj = obj.Where(i => !i.prerelease || i.name.EndsWith("-beta", StringComparison.OrdinalIgnoreCase)).ToArray(); + } + else if (updateLevel == PackageVersionClass.Dev) + { + obj = obj.Where(i => !i.prerelease || i.name.EndsWith("-beta", StringComparison.OrdinalIgnoreCase) || i.name.EndsWith("-dev", StringComparison.OrdinalIgnoreCase)).ToArray(); + } + + var availableUpdate = obj + .Select(i => CheckForUpdateResult(i, minVersion, assetFilename, packageName, targetFilename)) + .Where(i => i != null) + .OrderByDescending(i => Version.Parse(i.AvailableVersion)) + .FirstOrDefault(); + + return availableUpdate ?? new CheckForUpdateResult + { + IsUpdateAvailable = false + }; + } + + private bool MatchesUpdateLevel(RootObject i, PackageVersionClass updateLevel) + { + if (updateLevel == PackageVersionClass.Beta) + { + return !i.prerelease || i.name.EndsWith("-beta", StringComparison.OrdinalIgnoreCase); + } + if (updateLevel == PackageVersionClass.Dev) + { + return !i.prerelease || i.name.EndsWith("-beta", StringComparison.OrdinalIgnoreCase) || + i.name.EndsWith("-dev", StringComparison.OrdinalIgnoreCase); + } + + // Technically all we need to do is check that it's not pre-release + // But let's addititional checks for -beta and -dev to handle builds that might be temporarily tagged incorrectly. + return !i.prerelease && !i.name.EndsWith("-beta", StringComparison.OrdinalIgnoreCase) && + !i.name.EndsWith("-dev", StringComparison.OrdinalIgnoreCase); + } + + public async Task> GetLatestReleases(string organzation, string repository, string assetFilename, CancellationToken cancellationToken) + { + var list = new List(); + + var url = string.Format("https://api.github.com/repos/{0}/{1}/releases", organzation, repository); + + var options = new HttpRequestOptions + { + Url = url, + EnableKeepAlive = false, + CancellationToken = cancellationToken, + UserAgent = "Emby/3.0", + BufferContent = false + }; + + using (var stream = await _httpClient.Get(options).ConfigureAwait(false)) + { + var obj = _jsonSerializer.DeserializeFromStream(stream); + + obj = obj.Where(i => (i.assets ?? new List()).Any(a => IsAsset(a, assetFilename))).ToArray(); + + list.AddRange(obj.Where(i => MatchesUpdateLevel(i, PackageVersionClass.Release)).OrderByDescending(GetVersion).Take(1)); + list.AddRange(obj.Where(i => MatchesUpdateLevel(i, PackageVersionClass.Beta)).OrderByDescending(GetVersion).Take(1)); + list.AddRange(obj.Where(i => MatchesUpdateLevel(i, PackageVersionClass.Dev)).OrderByDescending(GetVersion).Take(1)); + + return list; + } + } + + public Version GetVersion(RootObject obj) + { + Version version; + if (!Version.TryParse(obj.tag_name, out version)) + { + return new Version(1, 0); + } + + return version; + } + + private CheckForUpdateResult CheckForUpdateResult(RootObject obj, Version minVersion, string assetFilename, string packageName, string targetFilename) + { + Version version; + if (!Version.TryParse(obj.tag_name, out version)) + { + return null; + } + + if (version < minVersion) + { + return null; + } + + var asset = (obj.assets ?? new List()).FirstOrDefault(i => IsAsset(i, assetFilename)); + + if (asset == null) + { + return null; + } + + return new CheckForUpdateResult + { + AvailableVersion = version.ToString(), + IsUpdateAvailable = version > minVersion, + Package = new PackageVersionInfo + { + classification = obj.prerelease ? + (obj.name.EndsWith("-dev", StringComparison.OrdinalIgnoreCase) ? PackageVersionClass.Dev : PackageVersionClass.Beta) : + PackageVersionClass.Release, + name = packageName, + sourceUrl = asset.browser_download_url, + targetFilename = targetFilename, + versionStr = version.ToString(), + requiredVersionStr = "1.0.0", + description = obj.body, + infoUrl = obj.html_url + } + }; + } + + private bool IsAsset(Asset asset, string assetFilename) + { + var downloadFilename = Path.GetFileName(asset.browser_download_url) ?? string.Empty; + + if (downloadFilename.IndexOf(assetFilename, StringComparison.OrdinalIgnoreCase) != -1) + { + return true; + } + + return string.Equals(assetFilename, downloadFilename, StringComparison.OrdinalIgnoreCase); + } + + public class Uploader + { + public string login { get; set; } + public int id { get; set; } + public string avatar_url { get; set; } + public string gravatar_id { get; set; } + public string url { get; set; } + public string html_url { get; set; } + public string followers_url { get; set; } + public string following_url { get; set; } + public string gists_url { get; set; } + public string starred_url { get; set; } + public string subscriptions_url { get; set; } + public string organizations_url { get; set; } + public string repos_url { get; set; } + public string events_url { get; set; } + public string received_events_url { get; set; } + public string type { get; set; } + public bool site_admin { get; set; } + } + + public class Asset + { + public string url { get; set; } + public int id { get; set; } + public string name { get; set; } + public object label { get; set; } + public Uploader uploader { get; set; } + public string content_type { get; set; } + public string state { get; set; } + public int size { get; set; } + public int download_count { get; set; } + public string created_at { get; set; } + public string updated_at { get; set; } + public string browser_download_url { get; set; } + } + + public class Author + { + public string login { get; set; } + public int id { get; set; } + public string avatar_url { get; set; } + public string gravatar_id { get; set; } + public string url { get; set; } + public string html_url { get; set; } + public string followers_url { get; set; } + public string following_url { get; set; } + public string gists_url { get; set; } + public string starred_url { get; set; } + public string subscriptions_url { get; set; } + public string organizations_url { get; set; } + public string repos_url { get; set; } + public string events_url { get; set; } + public string received_events_url { get; set; } + public string type { get; set; } + public bool site_admin { get; set; } + } + + public class RootObject + { + public string url { get; set; } + public string assets_url { get; set; } + public string upload_url { get; set; } + public string html_url { get; set; } + public int id { get; set; } + public string tag_name { get; set; } + public string target_commitish { get; set; } + public string name { get; set; } + public bool draft { get; set; } + public Author author { get; set; } + public bool prerelease { get; set; } + public string created_at { get; set; } + public string published_at { get; set; } + public List assets { get; set; } + public string tarball_url { get; set; } + public string zipball_url { get; set; } + public string body { get; set; } + } + } +} diff --git a/MediaBrowser.Controller/Entities/InternalItemsQuery.cs b/MediaBrowser.Controller/Entities/InternalItemsQuery.cs index 3fb118a9c..94baacf13 100644 --- a/MediaBrowser.Controller/Entities/InternalItemsQuery.cs +++ b/MediaBrowser.Controller/Entities/InternalItemsQuery.cs @@ -123,7 +123,6 @@ namespace MediaBrowser.Controller.Entities public int? MinParentalRating { get; set; } public int? MaxParentalRating { get; set; } - public bool? IsCurrentSchema { get; set; } public bool? HasDeadParentId { get; set; } public bool? IsOffline { get; set; } public bool? IsVirtualItem { get; set; } diff --git a/MediaBrowser.Controller/Entities/TV/Series.cs b/MediaBrowser.Controller/Entities/TV/Series.cs index cca8e3c19..a997d3476 100644 --- a/MediaBrowser.Controller/Entities/TV/Series.cs +++ b/MediaBrowser.Controller/Entities/TV/Series.cs @@ -126,10 +126,6 @@ namespace MediaBrowser.Controller.Entities.TV private static string GetUniqueSeriesKey(BaseItem series) { - if (ConfigurationManager.Configuration.SchemaVersion < 97) - { - return series.Id.ToString("N"); - } return series.GetPresentationUniqueKey(); } diff --git a/MediaBrowser.Model/Configuration/ServerConfiguration.cs b/MediaBrowser.Model/Configuration/ServerConfiguration.cs index 9715a624f..cdda858b7 100644 --- a/MediaBrowser.Model/Configuration/ServerConfiguration.cs +++ b/MediaBrowser.Model/Configuration/ServerConfiguration.cs @@ -192,9 +192,6 @@ namespace MediaBrowser.Model.Configuration public int SharingExpirationDays { get; set; } - public string[] Migrations { get; set; } - - public int MigrationVersion { get; set; } public int SchemaVersion { get; set; } public int SqliteCacheSize { get; set; } @@ -218,7 +215,6 @@ namespace MediaBrowser.Model.Configuration public ServerConfiguration() { LocalNetworkAddresses = new string[] { }; - Migrations = new string[] { }; CodecsUsed = new string[] { }; SqliteCacheSize = 0; ImageExtractionTimeoutMs = 0; diff --git a/MediaBrowser.Model/Tasks/ITaskManager.cs b/MediaBrowser.Model/Tasks/ITaskManager.cs index b6f847feb..fa3da97b3 100644 --- a/MediaBrowser.Model/Tasks/ITaskManager.cs +++ b/MediaBrowser.Model/Tasks/ITaskManager.cs @@ -74,7 +74,5 @@ namespace MediaBrowser.Model.Tasks event EventHandler> TaskExecuting; event EventHandler TaskCompleted; - - bool SuspendTriggers { get; set; } } } \ No newline at end of file diff --git a/MediaBrowser.Server.Mono/MonoAppHost.cs b/MediaBrowser.Server.Mono/MonoAppHost.cs index bb7db6a7c..d864c47d6 100644 --- a/MediaBrowser.Server.Mono/MonoAppHost.cs +++ b/MediaBrowser.Server.Mono/MonoAppHost.cs @@ -92,6 +92,32 @@ namespace MediaBrowser.Server.Mono MainClass.Shutdown(); } + protected override bool SupportsDualModeSockets + { + get + { + return GetMonoVersion() >= new Version(4, 6); + } + } + + private static Version GetMonoVersion() + { + Type type = Type.GetType("Mono.Runtime"); + if (type != null) + { + MethodInfo displayName = type.GetTypeInfo().GetMethod("GetDisplayName", BindingFlags.NonPublic | BindingFlags.Static); + var displayNameValue = displayName.Invoke(null, null).ToString().Trim().Split(' ')[0]; + + Version version; + if (Version.TryParse(displayNameValue, out version)) + { + return version; + } + } + + return new Version(1, 0); + } + protected override void AuthorizeServer() { throw new NotImplementedException(); diff --git a/MediaBrowser.Server.Mono/Native/MonoFileSystem.cs b/MediaBrowser.Server.Mono/Native/MonoFileSystem.cs index 748b94604..3d6a3e35d 100644 --- a/MediaBrowser.Server.Mono/Native/MonoFileSystem.cs +++ b/MediaBrowser.Server.Mono/Native/MonoFileSystem.cs @@ -6,7 +6,8 @@ namespace MediaBrowser.Server.Mono.Native { public class MonoFileSystem : ManagedFileSystem { - public MonoFileSystem(ILogger logger, bool supportsAsyncFileStreams, bool enableManagedInvalidFileNameChars) : base(logger, supportsAsyncFileStreams, enableManagedInvalidFileNameChars, false) + public MonoFileSystem(ILogger logger, bool supportsAsyncFileStreams, bool enableManagedInvalidFileNameChars) + : base(logger, supportsAsyncFileStreams, enableManagedInvalidFileNameChars, true) { } diff --git a/MediaBrowser.ServerApplication/MainStartup.cs b/MediaBrowser.ServerApplication/MainStartup.cs index 9b634d12b..7eafc1721 100644 --- a/MediaBrowser.ServerApplication/MainStartup.cs +++ b/MediaBrowser.ServerApplication/MainStartup.cs @@ -23,8 +23,8 @@ using Emby.Common.Implementations.Logging; using Emby.Common.Implementations.Networking; using Emby.Common.Implementations.Security; using Emby.Server.Core; -using Emby.Server.Core.Browser; using Emby.Server.Implementations; +using Emby.Server.Implementations.Browser; using Emby.Server.Implementations.IO; using ImageMagickSharp; using MediaBrowser.Common.Net; diff --git a/MediaBrowser.ServerApplication/ServerNotifyIcon.cs b/MediaBrowser.ServerApplication/ServerNotifyIcon.cs index 139961e6f..c421dd9eb 100644 --- a/MediaBrowser.ServerApplication/ServerNotifyIcon.cs +++ b/MediaBrowser.ServerApplication/ServerNotifyIcon.cs @@ -4,7 +4,7 @@ using MediaBrowser.Model.Logging; using System; using System.ComponentModel; using System.Windows.Forms; -using Emby.Server.Core.Browser; +using Emby.Server.Implementations.Browser; using MediaBrowser.Model.Globalization; namespace MediaBrowser.ServerApplication diff --git a/MediaBrowser.ServerApplication/WindowsAppHost.cs b/MediaBrowser.ServerApplication/WindowsAppHost.cs index b950de118..937762ed0 100644 --- a/MediaBrowser.ServerApplication/WindowsAppHost.cs +++ b/MediaBrowser.ServerApplication/WindowsAppHost.cs @@ -117,6 +117,14 @@ namespace MediaBrowser.ServerApplication } } + protected override bool SupportsDualModeSockets + { + get + { + return true; + } + } + public override void LaunchUrl(string url) { var process = new Process @@ -137,6 +145,7 @@ namespace MediaBrowser.ServerApplication } catch (Exception ex) { + Console.WriteLine("Error launching url: {0}", url); Logger.ErrorException("Error launching url: {0}", ex, url); throw; -- cgit v1.2.3 From 52227ce00d9602e4356c0b1f91a42ab8c61b19a6 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sat, 19 Nov 2016 03:40:13 -0500 Subject: update save methods --- .../Activity/ActivityRepository.cs | 4 +- .../Data/BaseSqliteRepository.cs | 56 ++++++++++++++++++++-- .../Data/SqliteDisplayPreferencesRepository.cs | 52 +++++++++++--------- .../Data/SqliteUserRepository.cs | 25 +++++----- .../Notifications/SqliteNotificationsRepository.cs | 10 ++-- .../Security/AuthenticationRepository.cs | 4 +- .../Social/SharingRepository.cs | 4 +- Emby.Server.Implementations/Sync/SyncRepository.cs | 16 +++---- .../Probing/ProbeResultNormalizer.cs | 2 +- 9 files changed, 115 insertions(+), 58 deletions(-) (limited to 'Emby.Server.Implementations/Activity/ActivityRepository.cs') diff --git a/Emby.Server.Implementations/Activity/ActivityRepository.cs b/Emby.Server.Implementations/Activity/ActivityRepository.cs index 7bc77402e..8f64f04db 100644 --- a/Emby.Server.Implementations/Activity/ActivityRepository.cs +++ b/Emby.Server.Implementations/Activity/ActivityRepository.cs @@ -51,7 +51,7 @@ namespace Emby.Server.Implementations.Activity throw new ArgumentNullException("entry"); } - lock (WriteLock) + using (WriteLock.Write()) { using (var connection = CreateConnection()) { @@ -76,7 +76,7 @@ namespace Emby.Server.Implementations.Activity public QueryResult GetActivityLogEntries(DateTime? minDate, int? startIndex, int? limit) { - lock (WriteLock) + using (WriteLock.Read()) { using (var connection = CreateConnection(true)) { diff --git a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs index 5a4846ccf..dc5d00985 100644 --- a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs +++ b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs @@ -12,7 +12,7 @@ namespace Emby.Server.Implementations.Data public abstract class BaseSqliteRepository : IDisposable { protected string DbFilePath { get; set; } - protected SemaphoreSlim WriteLock = new SemaphoreSlim(1, 1); + protected ReaderWriterLockSlim WriteLock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion); protected ILogger Logger { get; private set; } protected BaseSqliteRepository(ILogger logger) @@ -130,9 +130,10 @@ namespace Emby.Server.Implementations.Data { lock (_disposeLock) { - WriteLock.Wait(); - - CloseConnection(); + using (WriteLock.Write()) + { + CloseConnection(); + } } } catch (Exception ex) @@ -178,4 +179,51 @@ namespace Emby.Server.Implementations.Data })); } } + + public static class ReaderWriterLockSlimExtensions + { + private sealed class ReadLockToken : IDisposable + { + private ReaderWriterLockSlim _sync; + public ReadLockToken(ReaderWriterLockSlim sync) + { + _sync = sync; + sync.EnterReadLock(); + } + public void Dispose() + { + if (_sync != null) + { + _sync.ExitReadLock(); + _sync = null; + } + } + } + private sealed class WriteLockToken : IDisposable + { + private ReaderWriterLockSlim _sync; + public WriteLockToken(ReaderWriterLockSlim sync) + { + _sync = sync; + sync.EnterWriteLock(); + } + public void Dispose() + { + if (_sync != null) + { + _sync.ExitWriteLock(); + _sync = null; + } + } + } + + public static IDisposable Read(this ReaderWriterLockSlim obj) + { + return new ReadLockToken(obj); + } + public static IDisposable Write(this ReaderWriterLockSlim obj) + { + return new WriteLockToken(obj); + } + } } diff --git a/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs b/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs index 79fc893f4..1fbf9b0a9 100644 --- a/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs @@ -86,7 +86,7 @@ namespace Emby.Server.Implementations.Data cancellationToken.ThrowIfCancellationRequested(); - lock (WriteLock) + using (WriteLock.Write()) { using (var connection = CreateConnection()) { @@ -127,7 +127,7 @@ namespace Emby.Server.Implementations.Data cancellationToken.ThrowIfCancellationRequested(); - lock (WriteLock) + using (WriteLock.Write()) { using (var connection = CreateConnection()) { @@ -159,24 +159,27 @@ namespace Emby.Server.Implementations.Data var guidId = displayPreferencesId.GetMD5(); - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { - var commandText = "select data from userdisplaypreferences where id = ? and userId=? and client=?"; + using (var connection = CreateConnection(true)) + { + var commandText = "select data from userdisplaypreferences where id = ? and userId=? and client=?"; - var paramList = new List(); - paramList.Add(guidId.ToGuidParamValue()); - paramList.Add(userId.ToGuidParamValue()); - paramList.Add(client); + var paramList = new List(); + paramList.Add(guidId.ToGuidParamValue()); + paramList.Add(userId.ToGuidParamValue()); + paramList.Add(client); - foreach (var row in connection.Query(commandText, paramList.ToArray())) - { - return Get(row); - } + foreach (var row in connection.Query(commandText, paramList.ToArray())) + { + return Get(row); + } - return new DisplayPreferences - { - Id = guidId.ToString("N") - }; + return new DisplayPreferences + { + Id = guidId.ToString("N") + }; + } } } @@ -190,16 +193,19 @@ namespace Emby.Server.Implementations.Data { var list = new List(); - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { - var commandText = "select data from userdisplaypreferences where userId=?"; + using (var connection = CreateConnection(true)) + { + var commandText = "select data from userdisplaypreferences where userId=?"; - var paramList = new List(); - paramList.Add(userId.ToGuidParamValue()); + var paramList = new List(); + paramList.Add(userId.ToGuidParamValue()); - foreach (var row in connection.Query(commandText, paramList.ToArray())) - { - list.Add(Get(row)); + foreach (var row in connection.Query(commandText, paramList.ToArray())) + { + list.Add(Get(row)); + } } } diff --git a/Emby.Server.Implementations/Data/SqliteUserRepository.cs b/Emby.Server.Implementations/Data/SqliteUserRepository.cs index 5e4b1c7b8..f0e38f8c0 100644 --- a/Emby.Server.Implementations/Data/SqliteUserRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteUserRepository.cs @@ -83,7 +83,7 @@ namespace Emby.Server.Implementations.Data cancellationToken.ThrowIfCancellationRequested(); - lock (WriteLock) + using (WriteLock.Write()) { using (var connection = CreateConnection()) { @@ -107,18 +107,21 @@ namespace Emby.Server.Implementations.Data { var list = new List(); - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { - foreach (var row in connection.Query("select guid,data from users")) + using (var connection = CreateConnection(true)) { - var id = row[0].ReadGuid(); - - using (var stream = _memoryStreamProvider.CreateNew(row[1].ToBlob())) + foreach (var row in connection.Query("select guid,data from users")) { - stream.Position = 0; - var user = _jsonSerializer.DeserializeFromStream(stream); - user.Id = id; - list.Add(user); + var id = row[0].ReadGuid(); + + using (var stream = _memoryStreamProvider.CreateNew(row[1].ToBlob())) + { + stream.Position = 0; + var user = _jsonSerializer.DeserializeFromStream(stream); + user.Id = id; + list.Add(user); + } } } } @@ -142,7 +145,7 @@ namespace Emby.Server.Implementations.Data cancellationToken.ThrowIfCancellationRequested(); - lock (WriteLock) + using (WriteLock.Write()) { using (var connection = CreateConnection()) { diff --git a/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs b/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs index fcf45b0ad..ee900ef33 100644 --- a/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs +++ b/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs @@ -48,7 +48,7 @@ namespace Emby.Server.Implementations.Notifications { var result = new NotificationResult(); - lock (WriteLock) + using (WriteLock.Read()) { using (var connection = CreateConnection(true)) { @@ -103,7 +103,7 @@ namespace Emby.Server.Implementations.Notifications { var result = new NotificationsSummary(); - lock (WriteLock) + using (WriteLock.Read()) { using (var connection = CreateConnection(true)) { @@ -214,7 +214,7 @@ namespace Emby.Server.Implementations.Notifications cancellationToken.ThrowIfCancellationRequested(); - lock (WriteLock) + using (WriteLock.Write()) { using (var connection = CreateConnection()) { @@ -273,7 +273,7 @@ namespace Emby.Server.Implementations.Notifications { cancellationToken.ThrowIfCancellationRequested(); - lock (WriteLock) + using (WriteLock.Write()) { using (var connection = CreateConnection()) { @@ -289,7 +289,7 @@ namespace Emby.Server.Implementations.Notifications { cancellationToken.ThrowIfCancellationRequested(); - lock (WriteLock) + using (WriteLock.Write()) { using (var connection = CreateConnection()) { diff --git a/Emby.Server.Implementations/Security/AuthenticationRepository.cs b/Emby.Server.Implementations/Security/AuthenticationRepository.cs index f4cb42d29..f6163b80a 100644 --- a/Emby.Server.Implementations/Security/AuthenticationRepository.cs +++ b/Emby.Server.Implementations/Security/AuthenticationRepository.cs @@ -63,7 +63,7 @@ namespace Emby.Server.Implementations.Security cancellationToken.ThrowIfCancellationRequested(); - lock (WriteLock) + using (WriteLock.Write()) { using (var connection = CreateConnection()) { @@ -195,7 +195,7 @@ namespace Emby.Server.Implementations.Security throw new ArgumentNullException("id"); } - lock (WriteLock) + using (WriteLock.Read()) { using (var connection = CreateConnection(true)) { diff --git a/Emby.Server.Implementations/Social/SharingRepository.cs b/Emby.Server.Implementations/Social/SharingRepository.cs index e09b7f5b9..9065ee108 100644 --- a/Emby.Server.Implementations/Social/SharingRepository.cs +++ b/Emby.Server.Implementations/Social/SharingRepository.cs @@ -50,7 +50,7 @@ namespace Emby.Server.Implementations.Social throw new ArgumentNullException("info.Id"); } - lock (WriteLock) + using (WriteLock.Write()) { using (var connection = CreateConnection()) { @@ -75,7 +75,7 @@ namespace Emby.Server.Implementations.Social throw new ArgumentNullException("id"); } - lock (WriteLock) + using (WriteLock.Read()) { using (var connection = CreateConnection(true)) { diff --git a/Emby.Server.Implementations/Sync/SyncRepository.cs b/Emby.Server.Implementations/Sync/SyncRepository.cs index fbc5772f3..2877a8ffd 100644 --- a/Emby.Server.Implementations/Sync/SyncRepository.cs +++ b/Emby.Server.Implementations/Sync/SyncRepository.cs @@ -95,7 +95,7 @@ namespace Emby.Server.Implementations.Sync throw new ArgumentNullException("id"); } - lock (WriteLock) + using (WriteLock.Read()) { using (var connection = CreateConnection(true)) { @@ -206,7 +206,7 @@ namespace Emby.Server.Implementations.Sync CheckDisposed(); - lock (WriteLock) + using (WriteLock.Write()) { using (var connection = CreateConnection()) { @@ -259,7 +259,7 @@ namespace Emby.Server.Implementations.Sync CheckDisposed(); - lock (WriteLock) + using (WriteLock.Write()) { using (var connection = CreateConnection()) { @@ -281,7 +281,7 @@ namespace Emby.Server.Implementations.Sync CheckDisposed(); - lock (WriteLock) + using (WriteLock.Read()) { using (var connection = CreateConnection(true)) { @@ -379,7 +379,7 @@ namespace Emby.Server.Implementations.Sync CheckDisposed(); - lock (WriteLock) + using (WriteLock.Read()) { var guid = new Guid(id); @@ -407,7 +407,7 @@ namespace Emby.Server.Implementations.Sync throw new ArgumentNullException("query"); } - lock (WriteLock) + using (WriteLock.Read()) { using (var connection = CreateConnection(true)) { @@ -487,7 +487,7 @@ namespace Emby.Server.Implementations.Sync var now = DateTime.UtcNow; - lock (WriteLock) + using (WriteLock.Read()) { using (var connection = CreateConnection(true)) { @@ -650,7 +650,7 @@ namespace Emby.Server.Implementations.Sync CheckDisposed(); - lock (WriteLock) + using (WriteLock.Write()) { using (var connection = CreateConnection()) { diff --git a/MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs b/MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs index 6a1583094..09996e1d3 100644 --- a/MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs +++ b/MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs @@ -996,7 +996,7 @@ namespace MediaBrowser.MediaEncoding.Probing { _splitWhiteList = new List { - "AC/DV" + "AC/DC" }; } -- cgit v1.2.3 From 64d15be8390c6174eb7ded067715c226038b38fc Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sun, 20 Nov 2016 00:59:36 -0500 Subject: update queries --- Emby.Server.Core/Data/SqliteItemRepository.cs | 4 +- .../Activity/ActivityRepository.cs | 27 +++-- .../Data/SqliteDisplayPreferencesRepository.cs | 51 +++++---- .../Data/SqliteExtensions.cs | 48 +++++++- .../Data/SqliteUserRepository.cs | 20 ++-- .../Notifications/SqliteNotificationsRepository.cs | 71 ++++++++---- .../Security/AuthenticationRepository.cs | 121 ++++++++++++++------- Emby.Server.Implementations/Sync/SyncRepository.cs | 34 ++++-- .../Persistence/IItemRepository.cs | 2 +- 9 files changed, 253 insertions(+), 125 deletions(-) (limited to 'Emby.Server.Implementations/Activity/ActivityRepository.cs') diff --git a/Emby.Server.Core/Data/SqliteItemRepository.cs b/Emby.Server.Core/Data/SqliteItemRepository.cs index 5b8f18088..97d703602 100644 --- a/Emby.Server.Core/Data/SqliteItemRepository.cs +++ b/Emby.Server.Core/Data/SqliteItemRepository.cs @@ -679,7 +679,7 @@ namespace Emby.Server.Core.Data throw new ArgumentNullException("item"); } - return SaveItems(new[] { item }, cancellationToken); + return SaveItems(new List { item }, cancellationToken); } /// @@ -693,7 +693,7 @@ namespace Emby.Server.Core.Data /// or /// cancellationToken /// - public async Task SaveItems(IEnumerable items, CancellationToken cancellationToken) + public async Task SaveItems(List items, CancellationToken cancellationToken) { if (items == null) { diff --git a/Emby.Server.Implementations/Activity/ActivityRepository.cs b/Emby.Server.Implementations/Activity/ActivityRepository.cs index 8f64f04db..aaa0b2f5d 100644 --- a/Emby.Server.Implementations/Activity/ActivityRepository.cs +++ b/Emby.Server.Implementations/Activity/ActivityRepository.cs @@ -57,18 +57,21 @@ namespace Emby.Server.Implementations.Activity { connection.RunInTransaction(db => { - var commandText = "replace into ActivityLogEntries (Id, Name, Overview, ShortOverview, Type, ItemId, UserId, DateCreated, LogSeverity) values (?, ?, ?, ?, ?, ?, ?, ?, ?)"; - - db.Execute(commandText, - entry.Id.ToGuidParamValue(), - entry.Name, - entry.Overview, - entry.ShortOverview, - entry.Type, - entry.ItemId, - entry.UserId, - entry.Date.ToDateTimeParamValue(), - entry.Severity.ToString()); + using (var statement = db.PrepareStatement("replace into ActivityLogEntries (Id, Name, Overview, ShortOverview, Type, ItemId, UserId, DateCreated, LogSeverity) values (@Id, @Name, @Overview, @ShortOverview, @Type, @ItemId, @UserId, @DateCreated, @LogSeverity)")) + { + statement.BindParameters.TryBind("@Id", entry.Id.ToGuidParamValue()); + statement.BindParameters.TryBind("@Name", entry.Name); + + statement.BindParameters.TryBind("@Overview", entry.Overview); + statement.BindParameters.TryBind("@ShortOverview", entry.ShortOverview); + statement.BindParameters.TryBind("@Type", entry.Type); + statement.BindParameters.TryBind("@ItemId", entry.ItemId); + statement.BindParameters.TryBind("@UserId", entry.UserId); + statement.BindParameters.TryBind("@DateCreated", entry.Date.ToDateTimeParamValue()); + statement.BindParameters.TryBind("@LogSeverity", entry.Severity.ToString()); + + statement.MoveNext(); + } }); } } diff --git a/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs b/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs index 1fbf9b0a9..1c592048e 100644 --- a/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs @@ -100,14 +100,17 @@ namespace Emby.Server.Implementations.Data private void SaveDisplayPreferences(DisplayPreferences displayPreferences, Guid userId, string client, IDatabaseConnection connection) { - var commandText = "replace into userdisplaypreferences (id, userid, client, data) values (?, ?, ?, ?)"; - var serialized = _jsonSerializer.SerializeToBytes(displayPreferences, _memoryStreamProvider); - - connection.Execute(commandText, - displayPreferences.Id.ToGuidParamValue(), - userId.ToGuidParamValue(), - client, - serialized); + using (var statement = connection.PrepareStatement("replace into userdisplaypreferences (id, userid, client, data) values (@id, @userid, @client, @data)")) + { + var serialized = _jsonSerializer.SerializeToBytes(displayPreferences, _memoryStreamProvider); + + statement.BindParameters.TryBind("@id", displayPreferences.Id.ToGuidParamValue()); + statement.BindParameters.TryBind("@userId", userId.ToGuidParamValue()); + statement.BindParameters.TryBind("@client", client); + statement.BindParameters.TryBind("@data", serialized); + + statement.MoveNext(); + } } /// @@ -163,16 +166,16 @@ namespace Emby.Server.Implementations.Data { using (var connection = CreateConnection(true)) { - var commandText = "select data from userdisplaypreferences where id = ? and userId=? and client=?"; - - var paramList = new List(); - paramList.Add(guidId.ToGuidParamValue()); - paramList.Add(userId.ToGuidParamValue()); - paramList.Add(client); - - foreach (var row in connection.Query(commandText, paramList.ToArray())) + using (var statement = connection.PrepareStatement("select data from userdisplaypreferences where id = @id and userId=@userId and client=@client")) { - return Get(row); + statement.BindParameters.TryBind("@id", guidId.ToGuidParamValue()); + statement.BindParameters.TryBind("@userId", userId.ToGuidParamValue()); + statement.BindParameters.TryBind("@client", client); + + foreach (var row in statement.ExecuteQuery()) + { + return Get(row); + } } return new DisplayPreferences @@ -197,14 +200,14 @@ namespace Emby.Server.Implementations.Data { using (var connection = CreateConnection(true)) { - var commandText = "select data from userdisplaypreferences where userId=?"; - - var paramList = new List(); - paramList.Add(userId.ToGuidParamValue()); - - foreach (var row in connection.Query(commandText, paramList.ToArray())) + using (var statement = connection.PrepareStatement("select data from userdisplaypreferences where userId=@userId")) { - list.Add(Get(row)); + statement.BindParameters.TryBind("@userId", userId.ToGuidParamValue()); + + foreach (var row in statement.ExecuteQuery()) + { + list.Add(Get(row)); + } } } } diff --git a/Emby.Server.Implementations/Data/SqliteExtensions.cs b/Emby.Server.Implementations/Data/SqliteExtensions.cs index 014211924..1cc8a8a93 100644 --- a/Emby.Server.Implementations/Data/SqliteExtensions.cs +++ b/Emby.Server.Implementations/Data/SqliteExtensions.cs @@ -168,14 +168,54 @@ namespace Emby.Server.Implementations.Data return result[index].ToFloat(); } - public static DateTime GetDateTime(this IReadOnlyList result, int index) + public static Guid GetGuid(this IReadOnlyList result, int index) { - return result[index].ReadDateTime(); + return result[index].ReadGuid(); } - public static Guid GetGuid(this IReadOnlyList result, int index) + public static void TryBind(this IReadOnlyDictionary bindParameters, string name, string value) { - return result[index].ReadGuid(); + IBindParameter bindParam; + if (bindParameters.TryGetValue(name, out bindParam)) + { + bindParam.Bind(value); + } + } + + public static void TryBind(this IReadOnlyDictionary bindParameters, string name, bool value) + { + IBindParameter bindParam; + if (bindParameters.TryGetValue(name, out bindParam)) + { + bindParam.Bind(value); + } + } + + public static void TryBind(this IReadOnlyDictionary bindParameters, string name, byte[] value) + { + IBindParameter bindParam; + if (bindParameters.TryGetValue(name, out bindParam)) + { + bindParam.Bind(value); + } + } + + public static void TryBindNull(this IReadOnlyDictionary bindParameters, string name) + { + IBindParameter bindParam; + if (bindParameters.TryGetValue(name, out bindParam)) + { + bindParam.BindNull(); + } + } + + public static IEnumerable> ExecuteQuery( + this IStatement This) + { + while (This.MoveNext()) + { + yield return This.Current; + } } } } diff --git a/Emby.Server.Implementations/Data/SqliteUserRepository.cs b/Emby.Server.Implementations/Data/SqliteUserRepository.cs index f0e38f8c0..ee496b669 100644 --- a/Emby.Server.Implementations/Data/SqliteUserRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteUserRepository.cs @@ -89,11 +89,12 @@ namespace Emby.Server.Implementations.Data { connection.RunInTransaction(db => { - var commandText = "replace into users (guid, data) values (?, ?)"; - - db.Execute(commandText, - user.Id.ToGuidParamValue(), - serialized); + using (var statement = db.PrepareStatement("replace into users (guid, data) values (@guid, @data)")) + { + statement.BindParameters.TryBind("@guid", user.Id.ToGuidParamValue()); + statement.BindParameters.TryBind("@data", serialized); + statement.MoveNext(); + } }); } } @@ -151,10 +152,11 @@ namespace Emby.Server.Implementations.Data { connection.RunInTransaction(db => { - var commandText = "delete from users where guid=?"; - - db.Execute(commandText, - user.Id.ToGuidParamValue()); + using (var statement = db.PrepareStatement("delete from users where guid=@id")) + { + statement.BindParameters.TryBind("@id", user.Id.ToGuidParamValue()); + statement.MoveNext(); + } }); } } diff --git a/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs b/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs index 2f3bee788..15322dcd3 100644 --- a/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs +++ b/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs @@ -107,17 +107,23 @@ namespace Emby.Server.Implementations.Notifications { using (var connection = CreateConnection(true)) { - foreach (var row in connection.Query("select Level from Notifications where UserId=? and IsRead=?", userId.ToGuidParamValue(), false)) + using (var statement = connection.PrepareStatement("select Level from Notifications where UserId=@UserId and IsRead=@IsRead")) { - var levels = new List(); + statement.BindParameters.TryBind("@IsRead", false); + statement.BindParameters.TryBind("@UserId", userId.ToGuidParamValue()); - levels.Add(GetLevel(row, 0)); + foreach (var row in statement.ExecuteQuery()) + { + var levels = new List(); - result.UnreadCount = levels.Count; + levels.Add(GetLevel(row, 0)); - if (levels.Count > 0) - { - result.MaxUnreadNotificationLevel = levels.Max(); + result.UnreadCount = levels.Count; + + if (levels.Count > 0) + { + result.MaxUnreadNotificationLevel = levels.Max(); + } } } @@ -220,17 +226,21 @@ namespace Emby.Server.Implementations.Notifications { connection.RunInTransaction(conn => { - conn.Execute("replace into Notifications (Id, UserId, Date, Name, Description, Url, Level, IsRead, Category, RelatedId) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", - notification.Id.ToGuidParamValue(), - notification.UserId.ToGuidParamValue(), - notification.Date.ToDateTimeParamValue(), - notification.Name, - notification.Description, - notification.Url, - notification.Level.ToString(), - notification.IsRead, - string.Empty, - string.Empty); + using (var statement = conn.PrepareStatement("replace into Notifications (Id, UserId, Date, Name, Description, Url, Level, IsRead, Category, RelatedId) values (@Id, @UserId, @Date, @Name, @Description, @Url, @Level, @IsRead, @Category, @RelatedId)")) + { + statement.BindParameters.TryBind("@Id", notification.Id.ToGuidParamValue()); + statement.BindParameters.TryBind("@UserId", notification.UserId.ToGuidParamValue()); + statement.BindParameters.TryBind("@Date", notification.Date.ToDateTimeParamValue()); + statement.BindParameters.TryBind("@Name", notification.Name); + statement.BindParameters.TryBind("@Description", notification.Description); + statement.BindParameters.TryBind("@Url", notification.Url); + statement.BindParameters.TryBind("@Level", notification.Level.ToString()); + statement.BindParameters.TryBind("@IsRead", notification.IsRead); + statement.BindParameters.TryBind("@Category", string.Empty); + statement.BindParameters.TryBind("@RelatedId", string.Empty); + + statement.MoveNext(); + } }); } } @@ -279,7 +289,13 @@ namespace Emby.Server.Implementations.Notifications { connection.RunInTransaction(conn => { - conn.Execute("update Notifications set IsRead=? where UserId=?", isRead, userId.ToGuidParamValue()); + using (var statement = conn.PrepareStatement("update Notifications set IsRead=@IsRead where UserId=@UserId")) + { + statement.BindParameters.TryBind("@IsRead", isRead); + statement.BindParameters.TryBind("@UserId", userId.ToGuidParamValue()); + + statement.MoveNext(); + } }); } } @@ -295,12 +311,21 @@ namespace Emby.Server.Implementations.Notifications { connection.RunInTransaction(conn => { - var userIdParam = userId.ToGuidParamValue(); - - foreach (var id in notificationIdList) + using (var statement = conn.PrepareStatement("update Notifications set IsRead=@IsRead where UserId=@UserId and Id=@Id")) { - conn.Execute("update Notifications set IsRead=? where UserId=? and Id=?", isRead, userIdParam, id); + statement.BindParameters.TryBind("@IsRead", isRead); + statement.BindParameters.TryBind("@UserId", userId.ToGuidParamValue()); + + foreach (var id in notificationIdList) + { + statement.Reset(); + + statement.BindParameters.TryBind("@Id", id.ToGuidParamValue()); + + statement.MoveNext(); + } } + }); } } diff --git a/Emby.Server.Implementations/Security/AuthenticationRepository.cs b/Emby.Server.Implementations/Security/AuthenticationRepository.cs index f6163b80a..160e0f5d2 100644 --- a/Emby.Server.Implementations/Security/AuthenticationRepository.cs +++ b/Emby.Server.Implementations/Security/AuthenticationRepository.cs @@ -69,19 +69,30 @@ namespace Emby.Server.Implementations.Security { connection.RunInTransaction(db => { - var commandText = "replace into AccessTokens (Id, AccessToken, DeviceId, AppName, AppVersion, DeviceName, UserId, IsActive, DateCreated, DateRevoked) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; - - db.Execute(commandText, - info.Id.ToGuidParamValue(), - info.AccessToken, - info.DeviceId, - info.AppName, - info.AppVersion, - info.DeviceName, - info.UserId, - info.IsActive, - info.DateCreated.ToDateTimeParamValue(), - info.DateRevoked.HasValue ? info.DateRevoked.Value.ToDateTimeParamValue() : null); + using (var statement = db.PrepareStatement("replace into AccessTokens (Id, AccessToken, DeviceId, AppName, AppVersion, DeviceName, UserId, IsActive, DateCreated, DateRevoked) values (@Id, @AccessToken, @DeviceId, @AppName, @AppVersion, @DeviceName, @UserId, @IsActive, @DateCreated, @DateRevoked)")) + { + statement.BindParameters.TryBind("@Id", info.Id.ToGuidParamValue()); + statement.BindParameters.TryBind("@AccessToken", info.AccessToken); + + statement.BindParameters.TryBind("@DeviceId", info.DeviceId); + statement.BindParameters.TryBind("@AppName", info.AppName); + statement.BindParameters.TryBind("@AppVersion", info.AppVersion); + statement.BindParameters.TryBind("@DeviceName", info.DeviceName); + statement.BindParameters.TryBind("@UserId", info.UserId); + statement.BindParameters.TryBind("@IsActive", info.IsActive); + statement.BindParameters.TryBind("@DateCreated", info.DateCreated.ToDateTimeParamValue()); + + if (info.DateRevoked.HasValue) + { + statement.BindParameters.TryBind("@DateRevoked", info.DateRevoked.Value.ToDateTimeParamValue()); + } + else + { + statement.BindParameters.TryBindNull("@DateRevoked"); + } + + statement.MoveNext(); + } }); } } @@ -89,6 +100,29 @@ namespace Emby.Server.Implementations.Security private const string BaseSelectText = "select Id, AccessToken, DeviceId, AppName, AppVersion, DeviceName, UserId, IsActive, DateCreated, DateRevoked from AccessTokens"; + private void BindAuthenticationQueryParams(AuthenticationInfoQuery query, IStatement statement) + { + if (!string.IsNullOrWhiteSpace(query.AccessToken)) + { + statement.BindParameters.TryBind("@AccessToken", query.AccessToken); + } + + if (!string.IsNullOrWhiteSpace(query.UserId)) + { + statement.BindParameters.TryBind("@UserId", query.UserId); + } + + if (!string.IsNullOrWhiteSpace(query.DeviceId)) + { + statement.BindParameters.TryBind("@DeviceId", query.DeviceId); + } + + if (query.IsActive.HasValue) + { + statement.BindParameters.TryBind("@IsActive", query.IsActive.Value); + } + } + public QueryResult Get(AuthenticationInfoQuery query) { if (query == null) @@ -99,7 +133,6 @@ namespace Emby.Server.Implementations.Security using (var connection = CreateConnection(true)) { var commandText = BaseSelectText; - var paramList = new List(); var whereClauses = new List(); @@ -107,26 +140,22 @@ namespace Emby.Server.Implementations.Security if (!string.IsNullOrWhiteSpace(query.AccessToken)) { - whereClauses.Add("AccessToken=?"); - paramList.Add(query.AccessToken); + whereClauses.Add("AccessToken=@AccessToken"); } if (!string.IsNullOrWhiteSpace(query.UserId)) { - whereClauses.Add("UserId=?"); - paramList.Add(query.UserId); + whereClauses.Add("UserId=@UserId"); } if (!string.IsNullOrWhiteSpace(query.DeviceId)) { - whereClauses.Add("DeviceId=?"); - paramList.Add(query.DeviceId); + whereClauses.Add("DeviceId=@DeviceId"); } if (query.IsActive.HasValue) { - whereClauses.Add("IsActive=?"); - paramList.Add(query.IsActive.Value); + whereClauses.Add("IsActive=@IsActive"); } if (query.HasUser.HasValue) @@ -171,20 +200,30 @@ namespace Emby.Server.Implementations.Security var list = new List(); - foreach (var row in connection.Query(commandText, paramList.ToArray())) + using (var statement = connection.PrepareStatement(commandText)) { - list.Add(Get(row)); - } + BindAuthenticationQueryParams(query, statement); - var count = connection.Query("select count (Id) from AccessTokens" + whereTextWithoutPaging, paramList.ToArray()) - .SelectScalarInt() - .First(); + foreach (var row in statement.ExecuteQuery()) + { + list.Add(Get(row)); + } - return new QueryResult() - { - Items = list.ToArray(), - TotalRecordCount = count - }; + using (var totalCountStatement = connection.PrepareStatement("select count (Id) from AccessTokens" + whereTextWithoutPaging)) + { + BindAuthenticationQueryParams(query, totalCountStatement); + + var count = totalCountStatement.ExecuteQuery() + .SelectScalarInt() + .First(); + + return new QueryResult() + { + Items = list.ToArray(), + TotalRecordCount = count + }; + } + } } } @@ -199,16 +238,18 @@ namespace Emby.Server.Implementations.Security { using (var connection = CreateConnection(true)) { - var commandText = BaseSelectText + " where Id=?"; - var paramList = new List(); - - paramList.Add(id.ToGuidParamValue()); + var commandText = BaseSelectText + " where Id=@Id"; - foreach (var row in connection.Query(commandText, paramList.ToArray())) + using (var statement = connection.PrepareStatement(commandText)) { - return Get(row); + statement.BindParameters["@Id"].Bind(id.ToGuidParamValue()); + + foreach (var row in statement.ExecuteQuery()) + { + return Get(row); + } + return null; } - return null; } } } diff --git a/Emby.Server.Implementations/Sync/SyncRepository.cs b/Emby.Server.Implementations/Sync/SyncRepository.cs index 2877a8ffd..bbd23831c 100644 --- a/Emby.Server.Implementations/Sync/SyncRepository.cs +++ b/Emby.Server.Implementations/Sync/SyncRepository.cs @@ -492,14 +492,11 @@ namespace Emby.Server.Implementations.Sync using (var connection = CreateConnection(true)) { var commandText = "select ItemId,Status,Progress from SyncJobItems"; - var whereClauses = new List(); - var paramList = new List(); if (!string.IsNullOrWhiteSpace(query.TargetId)) { - whereClauses.Add("TargetId=?"); - paramList.Add(query.TargetId); + whereClauses.Add("TargetId=@TargetId"); } if (query.Statuses.Length > 0) @@ -514,22 +511,39 @@ namespace Emby.Server.Implementations.Sync commandText += " where " + string.Join(" AND ", whereClauses.ToArray()); } - foreach (var row in connection.Query(commandText, paramList.ToArray())) + using (var statement = connection.PrepareStatement(commandText)) { - AddStatusResult(row, result, false); + if (!string.IsNullOrWhiteSpace(query.TargetId)) + { + statement.BindParameters.TryBind("@TargetId", query.TargetId); + } + + foreach (var row in statement.ExecuteQuery()) + { + AddStatusResult(row, result, false); + } + LogQueryTime("GetSyncedItemProgresses", commandText, now); } - LogQueryTime("GetSyncedItemProgresses", commandText, now); commandText = commandText .Replace("select ItemId,Status,Progress from SyncJobItems", "select ItemIds,Status,Progress from SyncJobs") .Replace("'Synced'", "'Completed','CompletedWithError'"); now = DateTime.UtcNow; - foreach (var row in connection.Query(commandText, paramList.ToArray())) + + using (var statement = connection.PrepareStatement(commandText)) { - AddStatusResult(row, result, true); + if (!string.IsNullOrWhiteSpace(query.TargetId)) + { + statement.BindParameters.TryBind("@TargetId", query.TargetId); + } + + foreach (var row in statement.ExecuteQuery()) + { + AddStatusResult(row, result, true); + } + LogQueryTime("GetSyncedItemProgresses", commandText, now); } - LogQueryTime("GetSyncedItemProgresses", commandText, now); } } diff --git a/MediaBrowser.Controller/Persistence/IItemRepository.cs b/MediaBrowser.Controller/Persistence/IItemRepository.cs index 87937869d..0de048865 100644 --- a/MediaBrowser.Controller/Persistence/IItemRepository.cs +++ b/MediaBrowser.Controller/Persistence/IItemRepository.cs @@ -51,7 +51,7 @@ namespace MediaBrowser.Controller.Persistence /// The items. /// The cancellation token. /// Task. - Task SaveItems(IEnumerable items, CancellationToken cancellationToken); + Task SaveItems(List items, CancellationToken cancellationToken); /// /// Retrieves the item. -- cgit v1.2.3 From 7f62a99ab508551b83568051a874703ddd50b563 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sun, 20 Nov 2016 02:10:07 -0500 Subject: update extensions --- .../Activity/ActivityRepository.cs | 20 ++-- .../Data/SqliteDisplayPreferencesRepository.cs | 16 +-- .../Data/SqliteExtensions.cs | 115 ++++++++++++++++++--- .../Data/SqliteUserDataRepository.cs | 34 +++--- .../Data/SqliteUserRepository.cs | 6 +- .../Notifications/SqliteNotificationsRepository.cs | 34 +++--- .../Security/AuthenticationRepository.cs | 30 +++--- Emby.Server.Implementations/Sync/SyncRepository.cs | 4 +- 8 files changed, 173 insertions(+), 86 deletions(-) (limited to 'Emby.Server.Implementations/Activity/ActivityRepository.cs') diff --git a/Emby.Server.Implementations/Activity/ActivityRepository.cs b/Emby.Server.Implementations/Activity/ActivityRepository.cs index aaa0b2f5d..ebf93fa33 100644 --- a/Emby.Server.Implementations/Activity/ActivityRepository.cs +++ b/Emby.Server.Implementations/Activity/ActivityRepository.cs @@ -59,16 +59,16 @@ namespace Emby.Server.Implementations.Activity { using (var statement = db.PrepareStatement("replace into ActivityLogEntries (Id, Name, Overview, ShortOverview, Type, ItemId, UserId, DateCreated, LogSeverity) values (@Id, @Name, @Overview, @ShortOverview, @Type, @ItemId, @UserId, @DateCreated, @LogSeverity)")) { - statement.BindParameters.TryBind("@Id", entry.Id.ToGuidParamValue()); - statement.BindParameters.TryBind("@Name", entry.Name); - - statement.BindParameters.TryBind("@Overview", entry.Overview); - statement.BindParameters.TryBind("@ShortOverview", entry.ShortOverview); - statement.BindParameters.TryBind("@Type", entry.Type); - statement.BindParameters.TryBind("@ItemId", entry.ItemId); - statement.BindParameters.TryBind("@UserId", entry.UserId); - statement.BindParameters.TryBind("@DateCreated", entry.Date.ToDateTimeParamValue()); - statement.BindParameters.TryBind("@LogSeverity", entry.Severity.ToString()); + statement.TryBind("@Id", entry.Id.ToGuidParamValue()); + statement.TryBind("@Name", entry.Name); + + statement.TryBind("@Overview", entry.Overview); + statement.TryBind("@ShortOverview", entry.ShortOverview); + statement.TryBind("@Type", entry.Type); + statement.TryBind("@ItemId", entry.ItemId); + statement.TryBind("@UserId", entry.UserId); + statement.TryBind("@DateCreated", entry.Date.ToDateTimeParamValue()); + statement.TryBind("@LogSeverity", entry.Severity.ToString()); statement.MoveNext(); } diff --git a/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs b/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs index 1c592048e..184caa4d4 100644 --- a/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs @@ -104,10 +104,10 @@ namespace Emby.Server.Implementations.Data { var serialized = _jsonSerializer.SerializeToBytes(displayPreferences, _memoryStreamProvider); - statement.BindParameters.TryBind("@id", displayPreferences.Id.ToGuidParamValue()); - statement.BindParameters.TryBind("@userId", userId.ToGuidParamValue()); - statement.BindParameters.TryBind("@client", client); - statement.BindParameters.TryBind("@data", serialized); + statement.TryBind("@id", displayPreferences.Id.ToGuidParamValue()); + statement.TryBind("@userId", userId.ToGuidParamValue()); + statement.TryBind("@client", client); + statement.TryBind("@data", serialized); statement.MoveNext(); } @@ -168,9 +168,9 @@ namespace Emby.Server.Implementations.Data { using (var statement = connection.PrepareStatement("select data from userdisplaypreferences where id = @id and userId=@userId and client=@client")) { - statement.BindParameters.TryBind("@id", guidId.ToGuidParamValue()); - statement.BindParameters.TryBind("@userId", userId.ToGuidParamValue()); - statement.BindParameters.TryBind("@client", client); + statement.TryBind("@id", guidId.ToGuidParamValue()); + statement.TryBind("@userId", userId.ToGuidParamValue()); + statement.TryBind("@client", client); foreach (var row in statement.ExecuteQuery()) { @@ -202,7 +202,7 @@ namespace Emby.Server.Implementations.Data { using (var statement = connection.PrepareStatement("select data from userdisplaypreferences where userId=@userId")) { - statement.BindParameters.TryBind("@userId", userId.ToGuidParamValue()); + statement.TryBind("@userId", userId.ToGuidParamValue()); foreach (var row in statement.ExecuteQuery()) { diff --git a/Emby.Server.Implementations/Data/SqliteExtensions.cs b/Emby.Server.Implementations/Data/SqliteExtensions.cs index 0f4f703a0..657dadfe5 100644 --- a/Emby.Server.Implementations/Data/SqliteExtensions.cs +++ b/Emby.Server.Implementations/Data/SqliteExtensions.cs @@ -173,69 +173,156 @@ namespace Emby.Server.Implementations.Data return result[index].ReadGuid(); } - public static void TryBind(this IReadOnlyDictionary bindParameters, string name, double value) + public static void TryBind(this IStatement statement, string name, double value) { IBindParameter bindParam; - if (bindParameters.TryGetValue(name, out bindParam)) + if (statement.BindParameters.TryGetValue(name, out bindParam)) { bindParam.Bind(value); } } - public static void TryBind(this IReadOnlyDictionary bindParameters, string name, string value) + public static void TryBind(this IStatement statement, string name, string value) { IBindParameter bindParam; - if (bindParameters.TryGetValue(name, out bindParam)) + if (statement.BindParameters.TryGetValue(name, out bindParam)) { bindParam.Bind(value); } } - public static void TryBind(this IReadOnlyDictionary bindParameters, string name, bool value) + public static void TryBind(this IStatement statement, string name, bool value) { IBindParameter bindParam; - if (bindParameters.TryGetValue(name, out bindParam)) + if (statement.BindParameters.TryGetValue(name, out bindParam)) { bindParam.Bind(value); } } - public static void TryBind(this IReadOnlyDictionary bindParameters, string name, int value) + public static void TryBind(this IStatement statement, string name, float value) { IBindParameter bindParam; - if (bindParameters.TryGetValue(name, out bindParam)) + if (statement.BindParameters.TryGetValue(name, out bindParam)) { bindParam.Bind(value); } } - public static void TryBind(this IReadOnlyDictionary bindParameters, string name, long value) + public static void TryBind(this IStatement statement, string name, int value) { IBindParameter bindParam; - if (bindParameters.TryGetValue(name, out bindParam)) + if (statement.BindParameters.TryGetValue(name, out bindParam)) { bindParam.Bind(value); } } - public static void TryBind(this IReadOnlyDictionary bindParameters, string name, byte[] value) + public static void TryBind(this IStatement statement, string name, Guid value) { IBindParameter bindParam; - if (bindParameters.TryGetValue(name, out bindParam)) + if (statement.BindParameters.TryGetValue(name, out bindParam)) + { + bindParam.Bind(value.ToGuidParamValue()); + } + } + + public static void TryBind(this IStatement statement, string name, DateTime value) + { + IBindParameter bindParam; + if (statement.BindParameters.TryGetValue(name, out bindParam)) + { + bindParam.Bind(value.ToDateTimeParamValue()); + } + } + + public static void TryBind(this IStatement statement, string name, long value) + { + IBindParameter bindParam; + if (statement.BindParameters.TryGetValue(name, out bindParam)) { bindParam.Bind(value); } } - public static void TryBindNull(this IReadOnlyDictionary bindParameters, string name) + public static void TryBind(this IStatement statement, string name, byte[] value) { IBindParameter bindParam; - if (bindParameters.TryGetValue(name, out bindParam)) + if (statement.BindParameters.TryGetValue(name, out bindParam)) + { + bindParam.Bind(value); + } + } + + public static void TryBindNull(this IStatement statement, string name) + { + IBindParameter bindParam; + if (statement.BindParameters.TryGetValue(name, out bindParam)) { bindParam.BindNull(); } } + public static void TryBind(this IStatement statement, string name, DateTime? value) + { + if (value.HasValue) + { + TryBind(statement, name, value.Value); + } + else + { + TryBindNull(statement, name); + } + } + + public static void TryBind(this IStatement statement, string name, Guid? value) + { + if (value.HasValue) + { + TryBind(statement, name, value.Value); + } + else + { + TryBindNull(statement, name); + } + } + + public static void TryBind(this IStatement statement, string name, int? value) + { + if (value.HasValue) + { + TryBind(statement, name, value.Value); + } + else + { + TryBindNull(statement, name); + } + } + + public static void TryBind(this IStatement statement, string name, float? value) + { + if (value.HasValue) + { + TryBind(statement, name, value.Value); + } + else + { + TryBindNull(statement, name); + } + } + + public static void TryBind(this IStatement statement, string name, bool? value) + { + if (value.HasValue) + { + TryBind(statement, name, value.Value); + } + else + { + TryBindNull(statement, name); + } + } + public static IEnumerable> ExecuteQuery( this IStatement This) { diff --git a/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs b/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs index 8aa3698b8..c6b08c4b3 100644 --- a/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs @@ -152,48 +152,48 @@ // { // using (var statement = _connection.PrepareStatement("replace into userdata (key, userId, rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex) values (@key, @userId, @rating,@played,@playCount,@isFavorite,@playbackPositionTicks,@lastPlayedDate,@AudioStreamIndex,@SubtitleStreamIndex)")) // { -// statement.BindParameters.TryBind("@UserId", userId.ToGuidParamValue()); -// statement.BindParameters.TryBind("@Key", key); +// statement.TryBind("@UserId", userId.ToGuidParamValue()); +// statement.TryBind("@Key", key); // if (userData.Rating.HasValue) // { -// statement.BindParameters.TryBind("@rating", userData.Rating.Value); +// statement.TryBind("@rating", userData.Rating.Value); // } // else // { -// statement.BindParameters.TryBindNull("@rating"); +// statement.TryBindNull("@rating"); // } -// statement.BindParameters.TryBind("@played", userData.Played); -// statement.BindParameters.TryBind("@playCount", userData.PlayCount); -// statement.BindParameters.TryBind("@isFavorite", userData.IsFavorite); -// statement.BindParameters.TryBind("@playbackPositionTicks", userData.PlaybackPositionTicks); +// statement.TryBind("@played", userData.Played); +// statement.TryBind("@playCount", userData.PlayCount); +// statement.TryBind("@isFavorite", userData.IsFavorite); +// statement.TryBind("@playbackPositionTicks", userData.PlaybackPositionTicks); // if (userData.LastPlayedDate.HasValue) // { -// statement.BindParameters.TryBind("@lastPlayedDate", userData.LastPlayedDate.Value.ToDateTimeParamValue()); +// statement.TryBind("@lastPlayedDate", userData.LastPlayedDate.Value.ToDateTimeParamValue()); // } // else // { -// statement.BindParameters.TryBindNull("@lastPlayedDate"); +// statement.TryBindNull("@lastPlayedDate"); // } // if (userData.AudioStreamIndex.HasValue) // { -// statement.BindParameters.TryBind("@AudioStreamIndex", userData.AudioStreamIndex.Value); +// statement.TryBind("@AudioStreamIndex", userData.AudioStreamIndex.Value); // } // else // { -// statement.BindParameters.TryBindNull("@AudioStreamIndex"); +// statement.TryBindNull("@AudioStreamIndex"); // } // if (userData.SubtitleStreamIndex.HasValue) // { -// statement.BindParameters.TryBind("@SubtitleStreamIndex", userData.SubtitleStreamIndex.Value); +// statement.TryBind("@SubtitleStreamIndex", userData.SubtitleStreamIndex.Value); // } // else // { -// statement.BindParameters.TryBindNull("@SubtitleStreamIndex"); +// statement.TryBindNull("@SubtitleStreamIndex"); // } // statement.MoveNext(); @@ -243,8 +243,8 @@ // using (var statement = _connection.PrepareStatement("select key,userid,rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex from userdata where key =@Key and userId=@UserId")) // { -// statement.BindParameters.TryBind("@UserId", userId.ToGuidParamValue()); -// statement.BindParameters.TryBind("@Key", key); +// statement.TryBind("@UserId", userId.ToGuidParamValue()); +// statement.TryBind("@Key", key); // foreach (var row in statement.ExecuteQuery()) // { @@ -292,7 +292,7 @@ // { // using (var statement = _connection.PrepareStatement("select key,userid,rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex from userdata where userId=@UserId")) // { -// statement.BindParameters.TryBind("@UserId", userId.ToGuidParamValue()); +// statement.TryBind("@UserId", userId.ToGuidParamValue()); // foreach (var row in statement.ExecuteQuery()) // { diff --git a/Emby.Server.Implementations/Data/SqliteUserRepository.cs b/Emby.Server.Implementations/Data/SqliteUserRepository.cs index ee496b669..5e148d95a 100644 --- a/Emby.Server.Implementations/Data/SqliteUserRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteUserRepository.cs @@ -91,8 +91,8 @@ namespace Emby.Server.Implementations.Data { using (var statement = db.PrepareStatement("replace into users (guid, data) values (@guid, @data)")) { - statement.BindParameters.TryBind("@guid", user.Id.ToGuidParamValue()); - statement.BindParameters.TryBind("@data", serialized); + statement.TryBind("@guid", user.Id.ToGuidParamValue()); + statement.TryBind("@data", serialized); statement.MoveNext(); } }); @@ -154,7 +154,7 @@ namespace Emby.Server.Implementations.Data { using (var statement = db.PrepareStatement("delete from users where guid=@id")) { - statement.BindParameters.TryBind("@id", user.Id.ToGuidParamValue()); + statement.TryBind("@id", user.Id.ToGuidParamValue()); statement.MoveNext(); } }); diff --git a/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs b/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs index 15322dcd3..5a3b64dbb 100644 --- a/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs +++ b/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs @@ -109,8 +109,8 @@ namespace Emby.Server.Implementations.Notifications { using (var statement = connection.PrepareStatement("select Level from Notifications where UserId=@UserId and IsRead=@IsRead")) { - statement.BindParameters.TryBind("@IsRead", false); - statement.BindParameters.TryBind("@UserId", userId.ToGuidParamValue()); + statement.TryBind("@IsRead", false); + statement.TryBind("@UserId", userId.ToGuidParamValue()); foreach (var row in statement.ExecuteQuery()) { @@ -228,16 +228,16 @@ namespace Emby.Server.Implementations.Notifications { using (var statement = conn.PrepareStatement("replace into Notifications (Id, UserId, Date, Name, Description, Url, Level, IsRead, Category, RelatedId) values (@Id, @UserId, @Date, @Name, @Description, @Url, @Level, @IsRead, @Category, @RelatedId)")) { - statement.BindParameters.TryBind("@Id", notification.Id.ToGuidParamValue()); - statement.BindParameters.TryBind("@UserId", notification.UserId.ToGuidParamValue()); - statement.BindParameters.TryBind("@Date", notification.Date.ToDateTimeParamValue()); - statement.BindParameters.TryBind("@Name", notification.Name); - statement.BindParameters.TryBind("@Description", notification.Description); - statement.BindParameters.TryBind("@Url", notification.Url); - statement.BindParameters.TryBind("@Level", notification.Level.ToString()); - statement.BindParameters.TryBind("@IsRead", notification.IsRead); - statement.BindParameters.TryBind("@Category", string.Empty); - statement.BindParameters.TryBind("@RelatedId", string.Empty); + statement.TryBind("@Id", notification.Id.ToGuidParamValue()); + statement.TryBind("@UserId", notification.UserId.ToGuidParamValue()); + statement.TryBind("@Date", notification.Date.ToDateTimeParamValue()); + statement.TryBind("@Name", notification.Name); + statement.TryBind("@Description", notification.Description); + statement.TryBind("@Url", notification.Url); + statement.TryBind("@Level", notification.Level.ToString()); + statement.TryBind("@IsRead", notification.IsRead); + statement.TryBind("@Category", string.Empty); + statement.TryBind("@RelatedId", string.Empty); statement.MoveNext(); } @@ -291,8 +291,8 @@ namespace Emby.Server.Implementations.Notifications { using (var statement = conn.PrepareStatement("update Notifications set IsRead=@IsRead where UserId=@UserId")) { - statement.BindParameters.TryBind("@IsRead", isRead); - statement.BindParameters.TryBind("@UserId", userId.ToGuidParamValue()); + statement.TryBind("@IsRead", isRead); + statement.TryBind("@UserId", userId.ToGuidParamValue()); statement.MoveNext(); } @@ -313,14 +313,14 @@ namespace Emby.Server.Implementations.Notifications { using (var statement = conn.PrepareStatement("update Notifications set IsRead=@IsRead where UserId=@UserId and Id=@Id")) { - statement.BindParameters.TryBind("@IsRead", isRead); - statement.BindParameters.TryBind("@UserId", userId.ToGuidParamValue()); + statement.TryBind("@IsRead", isRead); + statement.TryBind("@UserId", userId.ToGuidParamValue()); foreach (var id in notificationIdList) { statement.Reset(); - statement.BindParameters.TryBind("@Id", id.ToGuidParamValue()); + statement.TryBind("@Id", id.ToGuidParamValue()); statement.MoveNext(); } diff --git a/Emby.Server.Implementations/Security/AuthenticationRepository.cs b/Emby.Server.Implementations/Security/AuthenticationRepository.cs index 160e0f5d2..a9141f153 100644 --- a/Emby.Server.Implementations/Security/AuthenticationRepository.cs +++ b/Emby.Server.Implementations/Security/AuthenticationRepository.cs @@ -71,24 +71,24 @@ namespace Emby.Server.Implementations.Security { using (var statement = db.PrepareStatement("replace into AccessTokens (Id, AccessToken, DeviceId, AppName, AppVersion, DeviceName, UserId, IsActive, DateCreated, DateRevoked) values (@Id, @AccessToken, @DeviceId, @AppName, @AppVersion, @DeviceName, @UserId, @IsActive, @DateCreated, @DateRevoked)")) { - statement.BindParameters.TryBind("@Id", info.Id.ToGuidParamValue()); - statement.BindParameters.TryBind("@AccessToken", info.AccessToken); + statement.TryBind("@Id", info.Id.ToGuidParamValue()); + statement.TryBind("@AccessToken", info.AccessToken); - statement.BindParameters.TryBind("@DeviceId", info.DeviceId); - statement.BindParameters.TryBind("@AppName", info.AppName); - statement.BindParameters.TryBind("@AppVersion", info.AppVersion); - statement.BindParameters.TryBind("@DeviceName", info.DeviceName); - statement.BindParameters.TryBind("@UserId", info.UserId); - statement.BindParameters.TryBind("@IsActive", info.IsActive); - statement.BindParameters.TryBind("@DateCreated", info.DateCreated.ToDateTimeParamValue()); + statement.TryBind("@DeviceId", info.DeviceId); + statement.TryBind("@AppName", info.AppName); + statement.TryBind("@AppVersion", info.AppVersion); + statement.TryBind("@DeviceName", info.DeviceName); + statement.TryBind("@UserId", info.UserId); + statement.TryBind("@IsActive", info.IsActive); + statement.TryBind("@DateCreated", info.DateCreated.ToDateTimeParamValue()); if (info.DateRevoked.HasValue) { - statement.BindParameters.TryBind("@DateRevoked", info.DateRevoked.Value.ToDateTimeParamValue()); + statement.TryBind("@DateRevoked", info.DateRevoked.Value.ToDateTimeParamValue()); } else { - statement.BindParameters.TryBindNull("@DateRevoked"); + statement.TryBindNull("@DateRevoked"); } statement.MoveNext(); @@ -104,22 +104,22 @@ namespace Emby.Server.Implementations.Security { if (!string.IsNullOrWhiteSpace(query.AccessToken)) { - statement.BindParameters.TryBind("@AccessToken", query.AccessToken); + statement.TryBind("@AccessToken", query.AccessToken); } if (!string.IsNullOrWhiteSpace(query.UserId)) { - statement.BindParameters.TryBind("@UserId", query.UserId); + statement.TryBind("@UserId", query.UserId); } if (!string.IsNullOrWhiteSpace(query.DeviceId)) { - statement.BindParameters.TryBind("@DeviceId", query.DeviceId); + statement.TryBind("@DeviceId", query.DeviceId); } if (query.IsActive.HasValue) { - statement.BindParameters.TryBind("@IsActive", query.IsActive.Value); + statement.TryBind("@IsActive", query.IsActive.Value); } } diff --git a/Emby.Server.Implementations/Sync/SyncRepository.cs b/Emby.Server.Implementations/Sync/SyncRepository.cs index bbd23831c..6fb918f15 100644 --- a/Emby.Server.Implementations/Sync/SyncRepository.cs +++ b/Emby.Server.Implementations/Sync/SyncRepository.cs @@ -515,7 +515,7 @@ namespace Emby.Server.Implementations.Sync { if (!string.IsNullOrWhiteSpace(query.TargetId)) { - statement.BindParameters.TryBind("@TargetId", query.TargetId); + statement.TryBind("@TargetId", query.TargetId); } foreach (var row in statement.ExecuteQuery()) @@ -535,7 +535,7 @@ namespace Emby.Server.Implementations.Sync { if (!string.IsNullOrWhiteSpace(query.TargetId)) { - statement.BindParameters.TryBind("@TargetId", query.TargetId); + statement.TryBind("@TargetId", query.TargetId); } foreach (var row in statement.ExecuteQuery()) -- cgit v1.2.3 From 24dc91160d33e9b1ea8edd1f4262b8ecb14db930 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sun, 20 Nov 2016 15:31:55 -0500 Subject: update activity log --- .../Activity/ActivityRepository.cs | 35 +++++++++++++++------- .../Data/SqliteItemRepository.cs | 4 +-- 2 files changed, 27 insertions(+), 12 deletions(-) (limited to 'Emby.Server.Implementations/Activity/ActivityRepository.cs') diff --git a/Emby.Server.Implementations/Activity/ActivityRepository.cs b/Emby.Server.Implementations/Activity/ActivityRepository.cs index ebf93fa33..d730e420a 100644 --- a/Emby.Server.Implementations/Activity/ActivityRepository.cs +++ b/Emby.Server.Implementations/Activity/ActivityRepository.cs @@ -84,19 +84,16 @@ namespace Emby.Server.Implementations.Activity using (var connection = CreateConnection(true)) { var commandText = BaseActivitySelectText; - var whereClauses = new List(); - var paramList = new List(); if (minDate.HasValue) { - whereClauses.Add("DateCreated>=?"); - paramList.Add(minDate.Value.ToDateTimeParamValue()); + whereClauses.Add("DateCreated>=@DateCreated"); } var whereTextWithoutPaging = whereClauses.Count == 0 ? - string.Empty : - " where " + string.Join(" AND ", whereClauses.ToArray()); + string.Empty : + " where " + string.Join(" AND ", whereClauses.ToArray()); if (startIndex.HasValue && startIndex.Value > 0) { @@ -122,13 +119,31 @@ namespace Emby.Server.Implementations.Activity commandText += " LIMIT " + limit.Value.ToString(_usCulture); } - var totalRecordCount = connection.Query("select count (Id) from ActivityLogEntries" + whereTextWithoutPaging, paramList.ToArray()).SelectScalarInt().First(); - var list = new List(); - foreach (var row in connection.Query(commandText, paramList.ToArray())) + using (var statement = connection.PrepareStatement(commandText)) + { + if (minDate.HasValue) + { + statement.TryBind("@DateCreated", minDate.Value.ToDateTimeParamValue()); + } + + foreach (var row in statement.ExecuteQuery()) + { + list.Add(GetEntry(row)); + } + } + + int totalRecordCount; + + using (var statement = connection.PrepareStatement("select count (Id) from ActivityLogEntries" + whereTextWithoutPaging)) { - list.Add(GetEntry(row)); + if (minDate.HasValue) + { + statement.TryBind("@DateCreated", minDate.Value.ToDateTimeParamValue()); + } + + totalRecordCount = statement.ExecuteQuery().SelectScalarInt().First(); } return new QueryResult() diff --git a/Emby.Server.Implementations/Data/SqliteItemRepository.cs b/Emby.Server.Implementations/Data/SqliteItemRepository.cs index e4ceca267..255235cc7 100644 --- a/Emby.Server.Implementations/Data/SqliteItemRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteItemRepository.cs @@ -3860,7 +3860,7 @@ namespace Emby.Server.Implementations.Data whereClauses.Add("LocationType=@LocationType"); if (statement != null) { - statement.TryBind("LocationType", query.LocationTypes[0].ToString()); + statement.TryBind("@LocationType", query.LocationTypes[0].ToString()); } } } @@ -3881,7 +3881,7 @@ namespace Emby.Server.Implementations.Data whereClauses.Add("LocationType<>@ExcludeLocationTypes"); if (statement != null) { - statement.TryBind("ExcludeLocationTypes", query.ExcludeLocationTypes[0].ToString()); + statement.TryBind("@ExcludeLocationTypes", query.ExcludeLocationTypes[0].ToString()); } } } -- cgit v1.2.3 From 1dc080df8ba5b9af9245788634d56cb155afd2ba Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sun, 20 Nov 2016 22:52:58 -0500 Subject: update connections --- .../Activity/ActivityRepository.cs | 16 +- .../Data/BaseSqliteRepository.cs | 58 +- .../Data/SqliteDisplayPreferencesRepository.cs | 26 +- .../Data/SqliteExtensions.cs | 10 +- .../Data/SqliteFileOrganizationRepository.cs | 8 + .../Data/SqliteItemRepository.cs | 799 +++++++++++---------- .../Data/SqliteUserDataRepository.cs | 99 +-- .../Data/SqliteUserRepository.cs | 20 +- .../Notifications/SqliteNotificationsRepository.cs | 25 +- .../Security/AuthenticationRepository.cs | 55 +- .../Social/SharingRepository.cs | 16 +- Emby.Server.Implementations/Sync/SyncRepository.cs | 76 +- 12 files changed, 672 insertions(+), 536 deletions(-) (limited to 'Emby.Server.Implementations/Activity/ActivityRepository.cs') diff --git a/Emby.Server.Implementations/Activity/ActivityRepository.cs b/Emby.Server.Implementations/Activity/ActivityRepository.cs index d730e420a..8a7573d66 100644 --- a/Emby.Server.Implementations/Activity/ActivityRepository.cs +++ b/Emby.Server.Implementations/Activity/ActivityRepository.cs @@ -27,6 +27,14 @@ namespace Emby.Server.Implementations.Activity { using (var connection = CreateConnection()) { + connection.ExecuteAll(string.Join(";", new[] + { + "pragma default_temp_store = memory", + "pragma default_synchronous=Normal", + "pragma temp_store = memory", + "pragma synchronous=Normal", + })); + string[] queries = { "create table if not exists ActivityLogEntries (Id GUID PRIMARY KEY, Name TEXT, Overview TEXT, ShortOverview TEXT, Type TEXT, ItemId TEXT, UserId TEXT, DateCreated DATETIME, LogSeverity TEXT)", @@ -51,9 +59,9 @@ namespace Emby.Server.Implementations.Activity throw new ArgumentNullException("entry"); } - using (WriteLock.Write()) + using (var connection = CreateConnection()) { - using (var connection = CreateConnection()) + using (WriteLock.Write()) { connection.RunInTransaction(db => { @@ -79,9 +87,9 @@ namespace Emby.Server.Implementations.Activity public QueryResult GetActivityLogEntries(DateTime? minDate, int? startIndex, int? limit) { - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { var commandText = BaseActivitySelectText; var whereClauses = new List(); diff --git a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs index c506411d4..382c7f245 100644 --- a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs +++ b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs @@ -30,11 +30,6 @@ namespace Emby.Server.Implementations.Data get { return false; } } - protected virtual bool EnableConnectionPooling - { - get { return true; } - } - static BaseSqliteRepository() { SQLite3.EnableSharedCache = false; @@ -45,7 +40,7 @@ namespace Emby.Server.Implementations.Data private static bool _versionLogged; - protected virtual SQLiteDatabaseConnection CreateConnection(bool isReadOnly = false) + protected SQLiteDatabaseConnection CreateConnection(bool isReadOnly = false, Action onConnect = null) { if (!_versionLogged) { @@ -56,7 +51,7 @@ namespace Emby.Server.Implementations.Data ConnectionFlags connectionFlags; - //isReadOnly = false; + isReadOnly = false; if (isReadOnly) { @@ -70,46 +65,40 @@ namespace Emby.Server.Implementations.Data connectionFlags |= ConnectionFlags.ReadWrite; } - if (EnableConnectionPooling) - { - connectionFlags |= ConnectionFlags.SharedCached; - } - else - { - connectionFlags |= ConnectionFlags.PrivateCache; - } - + connectionFlags |= ConnectionFlags.SharedCached; connectionFlags |= ConnectionFlags.NoMutex; var db = SQLite3.Open(DbFilePath, connectionFlags, null); var queries = new List { - "pragma default_temp_store = memory", + "pragma temp_store = memory", "PRAGMA page_size=4096", "PRAGMA journal_mode=WAL", - "PRAGMA temp_store=memory", - "PRAGMA synchronous=Normal", + "pragma synchronous=Normal", //"PRAGMA cache size=-10000" }; - var cacheSize = CacheSize; - if (cacheSize.HasValue) - { + //var cacheSize = CacheSize; + //if (cacheSize.HasValue) + //{ - } + //} - if (EnableExclusiveMode) - { - queries.Add("PRAGMA locking_mode=EXCLUSIVE"); - } + ////foreach (var query in queries) + ////{ + //// db.Execute(query); + ////} - //foreach (var query in queries) - //{ - // db.Execute(query); - //} + using (WriteLock.Write()) + { + db.ExecuteAll(string.Join(";", queries.ToArray())); - db.ExecuteAll(string.Join(";", queries.ToArray())); + if (onConnect != null) + { + onConnect(db); + } + } return db; } @@ -122,11 +111,6 @@ namespace Emby.Server.Implementations.Data } } - protected virtual bool EnableExclusiveMode - { - get { return false; } - } - internal static void CheckOk(int rc) { string msg = ""; diff --git a/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs b/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs index 184caa4d4..ab927ce86 100644 --- a/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs @@ -54,9 +54,17 @@ namespace Emby.Server.Implementations.Data { using (var connection = CreateConnection()) { + connection.ExecuteAll(string.Join(";", new[] + { + "pragma default_temp_store = memory", + "pragma default_synchronous=Normal", + "pragma temp_store = memory", + "pragma synchronous=Normal", + })); + string[] queries = { - "create table if not exists userdisplaypreferences (id GUID, userId GUID, client text, data BLOB)", + "create table if not exists userdisplaypreferences (id GUID, userId GUID, client text, data BLOB)", "create unique index if not exists userdisplaypreferencesindex on userdisplaypreferences (id, userId, client)" }; @@ -86,9 +94,9 @@ namespace Emby.Server.Implementations.Data cancellationToken.ThrowIfCancellationRequested(); - using (WriteLock.Write()) + using (var connection = CreateConnection()) { - using (var connection = CreateConnection()) + using (WriteLock.Write()) { connection.RunInTransaction(db => { @@ -130,9 +138,9 @@ namespace Emby.Server.Implementations.Data cancellationToken.ThrowIfCancellationRequested(); - using (WriteLock.Write()) + using (var connection = CreateConnection()) { - using (var connection = CreateConnection()) + using (WriteLock.Write()) { connection.RunInTransaction(db => { @@ -162,9 +170,9 @@ namespace Emby.Server.Implementations.Data var guidId = displayPreferencesId.GetMD5(); - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { using (var statement = connection.PrepareStatement("select data from userdisplaypreferences where id = @id and userId=@userId and client=@client")) { @@ -196,9 +204,9 @@ namespace Emby.Server.Implementations.Data { var list = new List(); - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { using (var statement = connection.PrepareStatement("select data from userdisplaypreferences where userId=@userId")) { diff --git a/Emby.Server.Implementations/Data/SqliteExtensions.cs b/Emby.Server.Implementations/Data/SqliteExtensions.cs index e4e91626d..5b2549087 100644 --- a/Emby.Server.Implementations/Data/SqliteExtensions.cs +++ b/Emby.Server.Implementations/Data/SqliteExtensions.cs @@ -131,11 +131,13 @@ namespace Emby.Server.Implementations.Data public static void Attach(IDatabaseConnection db, string path, string alias) { - var commandText = string.Format("attach ? as {0};", alias); - var paramList = new List(); - paramList.Add(path); + var commandText = string.Format("attach @path as {0};", alias); - db.Execute(commandText, paramList.ToArray()); + using (var statement = db.PrepareStatement(commandText)) + { + statement.TryBind("@path", path); + statement.MoveNext(); + } } public static bool IsDBNull(this IReadOnlyList result, int index) diff --git a/Emby.Server.Implementations/Data/SqliteFileOrganizationRepository.cs b/Emby.Server.Implementations/Data/SqliteFileOrganizationRepository.cs index 96edc5d0d..a71682329 100644 --- a/Emby.Server.Implementations/Data/SqliteFileOrganizationRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteFileOrganizationRepository.cs @@ -31,6 +31,14 @@ namespace Emby.Server.Implementations.Data { using (var connection = CreateConnection()) { + connection.ExecuteAll(string.Join(";", new[] + { + "pragma default_temp_store = memory", + "pragma default_synchronous=Normal", + "pragma temp_store = memory", + "pragma synchronous=Normal", + })); + string[] queries = { "create table if not exists FileOrganizerResults (ResultId GUID PRIMARY KEY, OriginalPath TEXT, TargetPath TEXT, FileLength INT, OrganizationDate datetime, Status TEXT, OrganizationType TEXT, StatusMessage TEXT, ExtractedName TEXT, ExtractedYear int null, ExtractedSeasonNumber int null, ExtractedEpisodeNumber int null, ExtractedEndingEpisodeNumber, DuplicatePaths TEXT int null)", diff --git a/Emby.Server.Implementations/Data/SqliteItemRepository.cs b/Emby.Server.Implementations/Data/SqliteItemRepository.cs index c720a8d89..754af9640 100644 --- a/Emby.Server.Implementations/Data/SqliteItemRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteItemRepository.cs @@ -127,10 +127,19 @@ namespace Emby.Server.Implementations.Data { _connection = CreateConnection(false); + _connection.ExecuteAll(string.Join(";", new[] + { + "pragma default_temp_store = memory", + "pragma default_synchronous=Normal", + "pragma temp_store = memory", + "pragma synchronous=Normal", + })); + var createMediaStreamsTableCommand = "create table if not exists mediastreams (ItemId GUID, StreamIndex INT, StreamType TEXT, Codec TEXT, Language TEXT, ChannelLayout TEXT, Profile TEXT, AspectRatio TEXT, Path TEXT, IsInterlaced BIT, BitRate INT NULL, Channels INT NULL, SampleRate INT NULL, IsDefault BIT, IsForced BIT, IsExternal BIT, Height INT NULL, Width INT NULL, AverageFrameRate FLOAT NULL, RealFrameRate FLOAT NULL, Level FLOAT NULL, PixelFormat TEXT, BitDepth INT NULL, IsAnamorphic BIT NULL, RefFrames INT NULL, CodecTag TEXT NULL, Comment TEXT NULL, NalLengthSize TEXT NULL, IsAvc BIT NULL, Title TEXT NULL, TimeBase TEXT NULL, CodecTimeBase TEXT NULL, PRIMARY KEY (ItemId, StreamIndex))"; string[] queries = { + "PRAGMA locking_mode=NORMAL", "create table if not exists TypedBaseItems (guid GUID primary key NOT NULL, type TEXT NOT NULL, data BLOB NULL, ParentId GUID NULL, Path TEXT NULL)", @@ -344,25 +353,26 @@ namespace Emby.Server.Implementations.Data _connection.RunQueries(postQueries); - SqliteExtensions.Attach(_connection, Path.Combine(_config.ApplicationPaths.DataPath, "userdata_v2.db"), "UserDataDb"); - userDataRepo.Initialize(_connection, WriteLock); + //SqliteExtensions.Attach(_connection, Path.Combine(_config.ApplicationPaths.DataPath, "userdata_v2.db"), "UserDataDb"); + userDataRepo.Initialize(WriteLock); //await Vacuum(_connection).ConfigureAwait(false); } - protected override bool EnableConnectionPooling - { - get - { - return false; - } - } - protected override bool EnableExclusiveMode + private SQLiteDatabaseConnection CreateConnection(bool readOnly, bool attachUserdata) { - get + Action onConnect = null; + + if (attachUserdata) { - return true; + onConnect = + c => SqliteExtensions.Attach(c, Path.Combine(_config.ApplicationPaths.DataPath, "userdata_v2.db"), + "UserDataDb"); } + + var conn = CreateConnection(readOnly, onConnect); + + return conn; } private readonly string[] _retriveItemColumns = @@ -635,12 +645,15 @@ namespace Emby.Server.Implementations.Data CheckDisposed(); - using (WriteLock.Write()) + using (var connection = CreateConnection()) { - _connection.RunInTransaction(db => + using (WriteLock.Write()) { - SaveItemsInTranscation(db, items); - }); + connection.RunInTransaction(db => + { + SaveItemsInTranscation(db, items); + }); + } } } @@ -1170,15 +1183,18 @@ namespace Emby.Server.Implementations.Data CheckDisposed(); - using (WriteLock.Write()) + using (var connection = CreateConnection(true)) { - using (var statement = _connection.PrepareStatement("select " + string.Join(",", _retriveItemColumns) + " from TypedBaseItems where guid = @guid")) + using (WriteLock.Read()) { - statement.TryBind("@guid", id); - - foreach (var row in statement.ExecuteQuery()) + using (var statement = connection.PrepareStatement("select " + string.Join(",", _retriveItemColumns) + " from TypedBaseItems where guid = @guid")) { - return GetItem(row); + statement.TryBind("@guid", id); + + foreach (var row in statement.ExecuteQuery()) + { + return GetItem(row); + } } } } @@ -1976,15 +1992,18 @@ namespace Emby.Server.Implementations.Data var list = new List(); - using (WriteLock.Write()) + using (var connection = CreateConnection(true)) { - using (var statement = _connection.PrepareStatement("select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId order by ChapterIndex asc")) + using (WriteLock.Read()) { - statement.TryBind("@ItemId", id); - - foreach (var row in statement.ExecuteQuery()) + using (var statement = connection.PrepareStatement("select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId order by ChapterIndex asc")) { - list.Add(GetChapter(row)); + statement.TryBind("@ItemId", id); + + foreach (var row in statement.ExecuteQuery()) + { + list.Add(GetChapter(row)); + } } } } @@ -2007,16 +2026,19 @@ namespace Emby.Server.Implementations.Data throw new ArgumentNullException("id"); } - using (WriteLock.Write()) + using (var connection = CreateConnection(true)) { - using (var statement = _connection.PrepareStatement("select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId and ChapterIndex=@ChapterIndex")) + using (WriteLock.Read()) { - statement.TryBind("@ItemId", id); - statement.TryBind("@ChapterIndex", index); - - foreach (var row in statement.ExecuteQuery()) + using (var statement = connection.PrepareStatement("select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId and ChapterIndex=@ChapterIndex")) { - return GetChapter(row); + statement.TryBind("@ItemId", id); + statement.TryBind("@ChapterIndex", index); + + foreach (var row in statement.ExecuteQuery()) + { + return GetChapter(row); + } } } } @@ -2085,35 +2107,38 @@ namespace Emby.Server.Implementations.Data var index = 0; - using (WriteLock.Write()) + using (var connection = CreateConnection()) { - _connection.RunInTransaction(db => + using (WriteLock.Write()) { - // First delete chapters - _connection.Execute("delete from " + ChaptersTableName + " where ItemId=@ItemId", id.ToGuidParamValue()); - - using (var saveChapterStatement = db.PrepareStatement("replace into " + ChaptersTableName + " (ItemId, ChapterIndex, StartPositionTicks, Name, ImagePath, ImageDateModified) values (@ItemId, @ChapterIndex, @StartPositionTicks, @Name, @ImagePath, @ImageDateModified)")) + connection.RunInTransaction(db => { - foreach (var chapter in chapters) + // First delete chapters + db.Execute("delete from " + ChaptersTableName + " where ItemId=@ItemId", id.ToGuidParamValue()); + + using (var saveChapterStatement = db.PrepareStatement("replace into " + ChaptersTableName + " (ItemId, ChapterIndex, StartPositionTicks, Name, ImagePath, ImageDateModified) values (@ItemId, @ChapterIndex, @StartPositionTicks, @Name, @ImagePath, @ImageDateModified)")) { - if (index > 0) + foreach (var chapter in chapters) { - saveChapterStatement.Reset(); - } + if (index > 0) + { + saveChapterStatement.Reset(); + } - saveChapterStatement.TryBind("@ItemId", id.ToGuidParamValue()); - saveChapterStatement.TryBind("@ChapterIndex", index); - saveChapterStatement.TryBind("@StartPositionTicks", chapter.StartPositionTicks); - saveChapterStatement.TryBind("@Name", chapter.Name); - saveChapterStatement.TryBind("@ImagePath", chapter.ImagePath); - saveChapterStatement.TryBind("@ImageDateModified", chapter.ImageDateModified); + saveChapterStatement.TryBind("@ItemId", id.ToGuidParamValue()); + saveChapterStatement.TryBind("@ChapterIndex", index); + saveChapterStatement.TryBind("@StartPositionTicks", chapter.StartPositionTicks); + saveChapterStatement.TryBind("@Name", chapter.Name); + saveChapterStatement.TryBind("@ImagePath", chapter.ImagePath); + saveChapterStatement.TryBind("@ImageDateModified", chapter.ImageDateModified); - saveChapterStatement.MoveNext(); + saveChapterStatement.MoveNext(); - index++; + index++; + } } - } - }); + }); + } } } @@ -2383,31 +2408,34 @@ namespace Emby.Server.Implementations.Data } } - using (WriteLock.Write()) + using (var connection = CreateConnection(true, EnableJoinUserData(query))) { - using (var statement = _connection.PrepareStatement(commandText)) + using (WriteLock.Read()) { - if (EnableJoinUserData(query)) + using (var statement = connection.PrepareStatement(commandText)) { - statement.TryBind("@UserId", query.User.Id); - } + if (EnableJoinUserData(query)) + { + statement.TryBind("@UserId", query.User.Id); + } - BindSimilarParams(query, statement); + BindSimilarParams(query, statement); - // Running this again will bind the params - GetWhereClauses(query, statement); + // Running this again will bind the params + GetWhereClauses(query, statement); - foreach (var row in statement.ExecuteQuery()) - { - var item = GetItem(row, query); - if (item != null) + foreach (var row in statement.ExecuteQuery()) { - list.Add(item); + var item = GetItem(row, query); + if (item != null) + { + list.Add(item); + } } } - } - LogQueryTime("GetItemList", commandText, now); + LogQueryTime("GetItemList", commandText, now); + } } // Hack for right now since we currently don't support filtering out these duplicates within a query @@ -2548,72 +2576,75 @@ namespace Emby.Server.Implementations.Data } } - using (WriteLock.Write()) + using (var connection = CreateConnection(true, EnableJoinUserData(query))) { - var totalRecordCount = 0; - var isReturningZeroItems = query.Limit.HasValue && query.Limit <= 0; - - if (!isReturningZeroItems) + using (WriteLock.Read()) { - using (var statement = _connection.PrepareStatement(commandText)) + var totalRecordCount = 0; + var isReturningZeroItems = query.Limit.HasValue && query.Limit <= 0; + + if (!isReturningZeroItems) { - if (EnableJoinUserData(query)) + using (var statement = connection.PrepareStatement(commandText)) { - statement.TryBind("@UserId", query.User.Id); - } + if (EnableJoinUserData(query)) + { + statement.TryBind("@UserId", query.User.Id); + } - BindSimilarParams(query, statement); + BindSimilarParams(query, statement); - // Running this again will bind the params - GetWhereClauses(query, statement); + // Running this again will bind the params + GetWhereClauses(query, statement); - foreach (var row in statement.ExecuteQuery()) - { - var item = GetItem(row, query); - if (item != null) + foreach (var row in statement.ExecuteQuery()) { - list.Add(item); + var item = GetItem(row, query); + if (item != null) + { + list.Add(item); + } } } } - } - commandText = string.Empty; + commandText = string.Empty; - if (EnableGroupByPresentationUniqueKey(query)) - { - commandText += " select count (distinct PresentationUniqueKey)" + GetFromText(); - } - else - { - commandText += " select count (guid)" + GetFromText(); - } + if (EnableGroupByPresentationUniqueKey(query)) + { + commandText += " select count (distinct PresentationUniqueKey)" + GetFromText(); + } + else + { + commandText += " select count (guid)" + GetFromText(); + } - commandText += GetJoinUserDataText(query); - commandText += whereTextWithoutPaging; + commandText += GetJoinUserDataText(query); + commandText += whereTextWithoutPaging; - using (var statement = _connection.PrepareStatement(commandText)) - { - if (EnableJoinUserData(query)) + using (var statement = connection.PrepareStatement(commandText)) { - statement.TryBind("@UserId", query.User.Id); - } + if (EnableJoinUserData(query)) + { + statement.TryBind("@UserId", query.User.Id); + } - BindSimilarParams(query, statement); + BindSimilarParams(query, statement); - // Running this again will bind the params - GetWhereClauses(query, statement); + // Running this again will bind the params + GetWhereClauses(query, statement); - totalRecordCount = statement.ExecuteQuery().SelectScalarInt().First(); - } + totalRecordCount = statement.ExecuteQuery().SelectScalarInt().First(); + } - LogQueryTime("GetItems", commandText, now); + LogQueryTime("GetItems", commandText, now); - return new QueryResult() - { - Items = list.ToArray(), - TotalRecordCount = totalRecordCount - }; + return new QueryResult() + { + Items = list.ToArray(), + TotalRecordCount = totalRecordCount + }; + } } } @@ -2774,29 +2805,32 @@ namespace Emby.Server.Implementations.Data var list = new List(); - using (WriteLock.Write()) + using (var connection = CreateConnection(true, EnableJoinUserData(query))) { - using (var statement = _connection.PrepareStatement(commandText)) + using (WriteLock.Read()) { - if (EnableJoinUserData(query)) + using (var statement = connection.PrepareStatement(commandText)) { - statement.TryBind("@UserId", query.User.Id); - } + if (EnableJoinUserData(query)) + { + statement.TryBind("@UserId", query.User.Id); + } - BindSimilarParams(query, statement); + BindSimilarParams(query, statement); - // Running this again will bind the params - GetWhereClauses(query, statement); + // Running this again will bind the params + GetWhereClauses(query, statement); - foreach (var row in statement.ExecuteQuery()) - { - list.Add(row[0].ReadGuid()); + foreach (var row in statement.ExecuteQuery()) + { + list.Add(row[0].ReadGuid()); + } } - } - LogQueryTime("GetItemList", commandText, now); + LogQueryTime("GetItemList", commandText, now); - return list; + return list; + } } } @@ -2842,34 +2876,37 @@ namespace Emby.Server.Implementations.Data var list = new List>(); - using (WriteLock.Write()) + using (var connection = CreateConnection(true, EnableJoinUserData(query))) { - using (var statement = _connection.PrepareStatement(commandText)) + using (WriteLock.Read()) { - if (EnableJoinUserData(query)) + using (var statement = connection.PrepareStatement(commandText)) { - statement.TryBind("@UserId", query.User.Id); - } - - // Running this again will bind the params - GetWhereClauses(query, statement); + if (EnableJoinUserData(query)) + { + statement.TryBind("@UserId", query.User.Id); + } - foreach (var row in statement.ExecuteQuery()) - { - var id = row.GetGuid(0); - string path = null; + // Running this again will bind the params + GetWhereClauses(query, statement); - if (!row.IsDBNull(1)) + foreach (var row in statement.ExecuteQuery()) { - path = row.GetString(1); + var id = row.GetGuid(0); + string path = null; + + if (!row.IsDBNull(1)) + { + path = row.GetString(1); + } + list.Add(new Tuple(id, path)); } - list.Add(new Tuple(id, path)); } - } - LogQueryTime("GetItemIdsWithPath", commandText, now); + LogQueryTime("GetItemIdsWithPath", commandText, now); - return list; + return list; + } } } @@ -2928,64 +2965,67 @@ namespace Emby.Server.Implementations.Data var list = new List(); - using (WriteLock.Write()) + using (var connection = CreateConnection(true, EnableJoinUserData(query))) { - var totalRecordCount = 0; - - using (var statement = _connection.PrepareStatement(commandText)) + using (WriteLock.Read()) { - if (EnableJoinUserData(query)) + var totalRecordCount = 0; + + using (var statement = connection.PrepareStatement(commandText)) { - statement.TryBind("@UserId", query.User.Id); - } + if (EnableJoinUserData(query)) + { + statement.TryBind("@UserId", query.User.Id); + } - BindSimilarParams(query, statement); + BindSimilarParams(query, statement); - // Running this again will bind the params - GetWhereClauses(query, statement); + // Running this again will bind the params + GetWhereClauses(query, statement); - foreach (var row in statement.ExecuteQuery()) - { - list.Add(row[0].ReadGuid()); + foreach (var row in statement.ExecuteQuery()) + { + list.Add(row[0].ReadGuid()); + } } - } - commandText = string.Empty; + commandText = string.Empty; - if (EnableGroupByPresentationUniqueKey(query)) - { - commandText += " select count (distinct PresentationUniqueKey)" + GetFromText(); - } - else - { - commandText += " select count (guid)" + GetFromText(); - } + if (EnableGroupByPresentationUniqueKey(query)) + { + commandText += " select count (distinct PresentationUniqueKey)" + GetFromText(); + } + else + { + commandText += " select count (guid)" + GetFromText(); + } - commandText += GetJoinUserDataText(query); - commandText += whereTextWithoutPaging; + commandText += GetJoinUserDataText(query); + commandText += whereTextWithoutPaging; - using (var statement = _connection.PrepareStatement(commandText)) - { - if (EnableJoinUserData(query)) + using (var statement = connection.PrepareStatement(commandText)) { - statement.TryBind("@UserId", query.User.Id); - } + if (EnableJoinUserData(query)) + { + statement.TryBind("@UserId", query.User.Id); + } - BindSimilarParams(query, statement); + BindSimilarParams(query, statement); - // Running this again will bind the params - GetWhereClauses(query, statement); + // Running this again will bind the params + GetWhereClauses(query, statement); - totalRecordCount = statement.ExecuteQuery().SelectScalarInt().First(); - } + totalRecordCount = statement.ExecuteQuery().SelectScalarInt().First(); + } - LogQueryTime("GetItemIds", commandText, now); + LogQueryTime("GetItemIds", commandText, now); - return new QueryResult() - { - Items = list.ToArray(), - TotalRecordCount = totalRecordCount - }; + return new QueryResult() + { + Items = list.ToArray(), + TotalRecordCount = totalRecordCount + }; + } } } @@ -4289,33 +4329,36 @@ namespace Emby.Server.Implementations.Data var commandText = "select Guid,InheritedTags,(select group_concat(Tags, '|') from TypedBaseItems where (guid=outer.guid) OR (guid in (Select AncestorId from AncestorIds where ItemId=Outer.guid))) as NewInheritedTags from typedbaseitems as Outer where NewInheritedTags <> InheritedTags"; - using (WriteLock.Write()) + using (var connection = CreateConnection()) { - foreach (var row in _connection.Query(commandText)) + using (WriteLock.Write()) { - var id = row.GetGuid(0); - string value = row.IsDBNull(2) ? null : row.GetString(2); + foreach (var row in connection.Query(commandText)) + { + var id = row.GetGuid(0); + string value = row.IsDBNull(2) ? null : row.GetString(2); - newValues.Add(new Tuple(id, value)); - } + newValues.Add(new Tuple(id, value)); + } - Logger.Debug("UpdateInheritedTags - {0} rows", newValues.Count); - if (newValues.Count == 0) - { - return; - } + Logger.Debug("UpdateInheritedTags - {0} rows", newValues.Count); + if (newValues.Count == 0) + { + return; + } - // write lock here - using (var statement = _connection.PrepareStatement("Update TypedBaseItems set InheritedTags=@InheritedTags where Guid=@Guid")) - { - foreach (var item in newValues) + // write lock here + using (var statement = connection.PrepareStatement("Update TypedBaseItems set InheritedTags=@InheritedTags where Guid=@Guid")) { - var paramList = new List(); + foreach (var item in newValues) + { + var paramList = new List(); - paramList.Add(item.Item1); - paramList.Add(item.Item2); + paramList.Add(item.Item1); + paramList.Add(item.Item2); - statement.Execute(paramList.ToArray()); + statement.Execute(paramList.ToArray()); + } } } } @@ -4360,28 +4403,31 @@ namespace Emby.Server.Implementations.Data CheckDisposed(); - using (WriteLock.Write()) + using (var connection = CreateConnection()) { - _connection.RunInTransaction(db => + using (WriteLock.Write()) { - // Delete people - ExecuteWithSingleParam(db, "delete from People where ItemId=@Id", id.ToGuidParamValue()); + connection.RunInTransaction(db => + { + // Delete people + ExecuteWithSingleParam(db, "delete from People where ItemId=@Id", id.ToGuidParamValue()); - // Delete chapters - ExecuteWithSingleParam(db, "delete from " + ChaptersTableName + " where ItemId=@Id", id.ToGuidParamValue()); + // Delete chapters + ExecuteWithSingleParam(db, "delete from " + ChaptersTableName + " where ItemId=@Id", id.ToGuidParamValue()); - // Delete media streams - ExecuteWithSingleParam(db, "delete from mediastreams where ItemId=@Id", id.ToGuidParamValue()); + // Delete media streams + ExecuteWithSingleParam(db, "delete from mediastreams where ItemId=@Id", id.ToGuidParamValue()); - // Delete ancestors - ExecuteWithSingleParam(db, "delete from AncestorIds where ItemId=@Id", id.ToGuidParamValue()); + // Delete ancestors + ExecuteWithSingleParam(db, "delete from AncestorIds where ItemId=@Id", id.ToGuidParamValue()); - // Delete item values - ExecuteWithSingleParam(db, "delete from ItemValues where ItemId=@Id", id.ToGuidParamValue()); + // Delete item values + ExecuteWithSingleParam(db, "delete from ItemValues where ItemId=@Id", id.ToGuidParamValue()); - // Delete the item - ExecuteWithSingleParam(db, "delete from TypedBaseItems where guid=@Id", id.ToGuidParamValue()); - }); + // Delete the item + ExecuteWithSingleParam(db, "delete from TypedBaseItems where guid=@Id", id.ToGuidParamValue()); + }); + } } } @@ -4417,19 +4463,22 @@ namespace Emby.Server.Implementations.Data var list = new List(); - using (WriteLock.Write()) + using (var connection = CreateConnection(true)) { - using (var statement = _connection.PrepareStatement(commandText)) + using (WriteLock.Read()) { - // Run this again to bind the params - GetPeopleWhereClauses(query, statement); - - foreach (var row in statement.ExecuteQuery()) + using (var statement = connection.PrepareStatement(commandText)) { - list.Add(row.GetString(0)); + // Run this again to bind the params + GetPeopleWhereClauses(query, statement); + + foreach (var row in statement.ExecuteQuery()) + { + list.Add(row.GetString(0)); + } } + return list; } - return list; } } @@ -4455,16 +4504,19 @@ namespace Emby.Server.Implementations.Data var list = new List(); - using (WriteLock.Write()) + using (var connection = CreateConnection(true)) { - using (var statement = _connection.PrepareStatement(commandText)) + using (WriteLock.Read()) { - // Run this again to bind the params - GetPeopleWhereClauses(query, statement); - - foreach (var row in statement.ExecuteQuery()) + using (var statement = connection.PrepareStatement(commandText)) { - list.Add(GetPerson(row)); + // Run this again to bind the params + GetPeopleWhereClauses(query, statement); + + foreach (var row in statement.ExecuteQuery()) + { + list.Add(GetPerson(row)); + } } } } @@ -4667,13 +4719,16 @@ namespace Emby.Server.Implementations.Data commandText += " Group By CleanValue"; - using (WriteLock.Write()) + using (var connection = CreateConnection(true)) { - foreach (var row in _connection.Query(commandText)) + using (WriteLock.Write()) { - if (!row.IsDBNull(0)) + foreach (var row in connection.Query(commandText)) { - list.Add(row.GetString(0)); + if (!row.IsDBNull(0)) + { + list.Add(row.GetString(0)); + } } } } @@ -4825,67 +4880,70 @@ namespace Emby.Server.Implementations.Data var list = new List>(); var count = 0; - using (WriteLock.Write()) + using (var connection = CreateConnection(true, EnableJoinUserData(query))) { - if (!isReturningZeroItems) + using (WriteLock.Read()) { - using (var statement = _connection.PrepareStatement(commandText)) + if (!isReturningZeroItems) { - statement.TryBind("@SelectType", returnType); - if (EnableJoinUserData(query)) + using (var statement = connection.PrepareStatement(commandText)) { - statement.TryBind("@UserId", query.User.Id); - } + statement.TryBind("@SelectType", returnType); + if (EnableJoinUserData(query)) + { + statement.TryBind("@UserId", query.User.Id); + } - if (typeSubQuery != null) - { - GetWhereClauses(typeSubQuery, null, "itemTypes"); - } - BindSimilarParams(query, statement); - GetWhereClauses(innerQuery, statement); - GetWhereClauses(outerQuery, statement); + if (typeSubQuery != null) + { + GetWhereClauses(typeSubQuery, null, "itemTypes"); + } + BindSimilarParams(query, statement); + GetWhereClauses(innerQuery, statement); + GetWhereClauses(outerQuery, statement); - foreach (var row in statement.ExecuteQuery()) - { - var item = GetItem(row); - if (item != null) + foreach (var row in statement.ExecuteQuery()) { - var countStartColumn = columns.Count - 1; + var item = GetItem(row); + if (item != null) + { + var countStartColumn = columns.Count - 1; - list.Add(new Tuple(item, GetItemCounts(row, countStartColumn, typesToCount))); + list.Add(new Tuple(item, GetItemCounts(row, countStartColumn, typesToCount))); + } } - } - LogQueryTime("GetItemValues", commandText, now); + LogQueryTime("GetItemValues", commandText, now); + } } - } - if (query.EnableTotalRecordCount) - { - commandText = "select count (distinct PresentationUniqueKey)" + GetFromText(); + if (query.EnableTotalRecordCount) + { + commandText = "select count (distinct PresentationUniqueKey)" + GetFromText(); - commandText += GetJoinUserDataText(query); - commandText += whereText; + commandText += GetJoinUserDataText(query); + commandText += whereText; - using (var statement = _connection.PrepareStatement(commandText)) - { - statement.TryBind("@SelectType", returnType); - if (EnableJoinUserData(query)) + using (var statement = connection.PrepareStatement(commandText)) { - statement.TryBind("@UserId", query.User.Id); - } + statement.TryBind("@SelectType", returnType); + if (EnableJoinUserData(query)) + { + statement.TryBind("@UserId", query.User.Id); + } - if (typeSubQuery != null) - { - GetWhereClauses(typeSubQuery, null, "itemTypes"); - } - BindSimilarParams(query, statement); - GetWhereClauses(innerQuery, statement); - GetWhereClauses(outerQuery, statement); + if (typeSubQuery != null) + { + GetWhereClauses(typeSubQuery, null, "itemTypes"); + } + BindSimilarParams(query, statement); + GetWhereClauses(innerQuery, statement); + GetWhereClauses(outerQuery, statement); - count = statement.ExecuteQuery().SelectScalarInt().First(); + count = statement.ExecuteQuery().SelectScalarInt().First(); - LogQueryTime("GetItemValues", commandText, now); + LogQueryTime("GetItemValues", commandText, now); + } } } } @@ -5043,33 +5101,35 @@ namespace Emby.Server.Implementations.Data CheckDisposed(); - using (WriteLock.Write()) + using (var connection = CreateConnection()) { - // First delete - // "delete from People where ItemId=?" - _connection.Execute("delete from People where ItemId=?", itemId.ToGuidParamValue()); + using (WriteLock.Write()) + { // First delete + // "delete from People where ItemId=?" + connection.Execute("delete from People where ItemId=?", itemId.ToGuidParamValue()); - var listIndex = 0; + var listIndex = 0; - using (var statement = _connection.PrepareStatement( - "insert into People (ItemId, Name, Role, PersonType, SortOrder, ListOrder) values (@ItemId, @Name, @Role, @PersonType, @SortOrder, @ListOrder)")) - { - foreach (var person in people) + using (var statement = connection.PrepareStatement( + "insert into People (ItemId, Name, Role, PersonType, SortOrder, ListOrder) values (@ItemId, @Name, @Role, @PersonType, @SortOrder, @ListOrder)")) { - if (listIndex > 0) + foreach (var person in people) { - statement.Reset(); - } + if (listIndex > 0) + { + statement.Reset(); + } - statement.TryBind("@ItemId", itemId.ToGuidParamValue()); - statement.TryBind("@Name", person.Name); - statement.TryBind("@Role", person.Role); - statement.TryBind("@PersonType", person.Type); - statement.TryBind("@SortOrder", person.SortOrder); - statement.TryBind("@ListOrder", listIndex); + statement.TryBind("@ItemId", itemId.ToGuidParamValue()); + statement.TryBind("@Name", person.Name); + statement.TryBind("@Role", person.Role); + statement.TryBind("@PersonType", person.Type); + statement.TryBind("@SortOrder", person.SortOrder); + statement.TryBind("@ListOrder", listIndex); - statement.MoveNext(); - listIndex++; + statement.MoveNext(); + listIndex++; + } } } } @@ -5127,25 +5187,28 @@ namespace Emby.Server.Implementations.Data cmdText += " order by StreamIndex ASC"; - using (WriteLock.Write()) + using (var connection = CreateConnection(true)) { - using (var statement = _connection.PrepareStatement(cmdText)) + using (WriteLock.Read()) { - statement.TryBind("@ItemId", query.ItemId.ToGuidParamValue()); - - if (query.Type.HasValue) + using (var statement = connection.PrepareStatement(cmdText)) { - statement.TryBind("@StreamType", query.Type.Value.ToString()); - } + statement.TryBind("@ItemId", query.ItemId.ToGuidParamValue()); - if (query.Index.HasValue) - { - statement.TryBind("@StreamIndex", query.Index.Value); - } + if (query.Type.HasValue) + { + statement.TryBind("@StreamType", query.Type.Value.ToString()); + } - foreach (var row in statement.ExecuteQuery()) - { - list.Add(GetMediaStream(row)); + if (query.Index.HasValue) + { + statement.TryBind("@StreamIndex", query.Index.Value); + } + + foreach (var row in statement.ExecuteQuery()) + { + list.Add(GetMediaStream(row)); + } } } } @@ -5169,58 +5232,60 @@ namespace Emby.Server.Implementations.Data cancellationToken.ThrowIfCancellationRequested(); - using (WriteLock.Write()) + using (var connection = CreateConnection()) { - // First delete chapters - _connection.Execute("delete from mediastreams where ItemId=@ItemId", id.ToGuidParamValue()); + using (WriteLock.Write()) + { // First delete chapters + connection.Execute("delete from mediastreams where ItemId=@ItemId", id.ToGuidParamValue()); - using (var statement = _connection.PrepareStatement(string.Format("replace into mediastreams ({0}) values ({1})", - string.Join(",", _mediaStreamSaveColumns), - string.Join(",", _mediaStreamSaveColumns.Select(i => "@" + i).ToArray())))) - { - foreach (var stream in streams) + using (var statement = connection.PrepareStatement(string.Format("replace into mediastreams ({0}) values ({1})", + string.Join(",", _mediaStreamSaveColumns), + string.Join(",", _mediaStreamSaveColumns.Select(i => "@" + i).ToArray())))) { - var paramList = new List(); - - paramList.Add(id.ToGuidParamValue()); - paramList.Add(stream.Index); - paramList.Add(stream.Type.ToString()); - paramList.Add(stream.Codec); - paramList.Add(stream.Language); - paramList.Add(stream.ChannelLayout); - paramList.Add(stream.Profile); - paramList.Add(stream.AspectRatio); - paramList.Add(stream.Path); - - paramList.Add(stream.IsInterlaced); - paramList.Add(stream.BitRate); - paramList.Add(stream.Channels); - paramList.Add(stream.SampleRate); - - paramList.Add(stream.IsDefault); - paramList.Add(stream.IsForced); - paramList.Add(stream.IsExternal); - - paramList.Add(stream.Width); - paramList.Add(stream.Height); - paramList.Add(stream.AverageFrameRate); - paramList.Add(stream.RealFrameRate); - paramList.Add(stream.Level); - paramList.Add(stream.PixelFormat); - paramList.Add(stream.BitDepth); - paramList.Add(stream.IsExternal); - paramList.Add(stream.RefFrames); - - paramList.Add(stream.CodecTag); - paramList.Add(stream.Comment); - paramList.Add(stream.NalLengthSize); - paramList.Add(stream.IsAVC); - paramList.Add(stream.Title); - - paramList.Add(stream.TimeBase); - paramList.Add(stream.CodecTimeBase); - - statement.Execute(paramList.ToArray()); + foreach (var stream in streams) + { + var paramList = new List(); + + paramList.Add(id.ToGuidParamValue()); + paramList.Add(stream.Index); + paramList.Add(stream.Type.ToString()); + paramList.Add(stream.Codec); + paramList.Add(stream.Language); + paramList.Add(stream.ChannelLayout); + paramList.Add(stream.Profile); + paramList.Add(stream.AspectRatio); + paramList.Add(stream.Path); + + paramList.Add(stream.IsInterlaced); + paramList.Add(stream.BitRate); + paramList.Add(stream.Channels); + paramList.Add(stream.SampleRate); + + paramList.Add(stream.IsDefault); + paramList.Add(stream.IsForced); + paramList.Add(stream.IsExternal); + + paramList.Add(stream.Width); + paramList.Add(stream.Height); + paramList.Add(stream.AverageFrameRate); + paramList.Add(stream.RealFrameRate); + paramList.Add(stream.Level); + paramList.Add(stream.PixelFormat); + paramList.Add(stream.BitDepth); + paramList.Add(stream.IsExternal); + paramList.Add(stream.RefFrames); + + paramList.Add(stream.CodecTag); + paramList.Add(stream.Comment); + paramList.Add(stream.NalLengthSize); + paramList.Add(stream.IsAVC); + paramList.Add(stream.Title); + + paramList.Add(stream.TimeBase); + paramList.Add(stream.CodecTimeBase); + + statement.Execute(paramList.ToArray()); + } } } } diff --git a/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs b/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs index a66536179..0b4768c14 100644 --- a/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs @@ -14,19 +14,12 @@ namespace Emby.Server.Implementations.Data { public class SqliteUserDataRepository : BaseSqliteRepository, IUserDataRepository { - private SQLiteDatabaseConnection _connection; - public SqliteUserDataRepository(ILogger logger, IApplicationPaths appPaths) : base(logger) { DbFilePath = Path.Combine(appPaths.DataPath, "userdata_v2.db"); } - protected override bool EnableConnectionPooling - { - get { return false; } - } - /// /// Gets the name of the repository /// @@ -43,13 +36,24 @@ namespace Emby.Server.Implementations.Data /// Opens the connection to the database /// /// Task. - public void Initialize(SQLiteDatabaseConnection connection, ReaderWriterLockSlim writeLock) + public void Initialize(ReaderWriterLockSlim writeLock) { WriteLock.Dispose(); WriteLock = writeLock; - _connection = connection; - string[] queries = { + using (var connection = CreateConnection()) + { + connection.ExecuteAll(string.Join(";", new[] + { + "pragma default_temp_store = memory", + "pragma default_synchronous=Normal", + "pragma temp_store = memory", + "pragma synchronous=Normal", + })); + + string[] queries = { + + "PRAGMA locking_mode=NORMAL", "create table if not exists UserDataDb.userdata (key nvarchar, userId GUID, rating float null, played bit, playCount int, isFavorite bit, playbackPositionTicks bigint, lastPlayedDate datetime null)", @@ -69,15 +73,16 @@ namespace Emby.Server.Implementations.Data "pragma shrink_memory" }; - _connection.RunQueries(queries); + connection.RunQueries(queries); - connection.RunInTransaction(db => - { - var existingColumnNames = GetColumnNames(db, "userdata"); + connection.RunInTransaction(db => + { + var existingColumnNames = GetColumnNames(db, "userdata"); - AddColumn(db, "userdata", "AudioStreamIndex", "int", existingColumnNames); - AddColumn(db, "userdata", "SubtitleStreamIndex", "int", existingColumnNames); - }); + AddColumn(db, "userdata", "AudioStreamIndex", "int", existingColumnNames); + AddColumn(db, "userdata", "SubtitleStreamIndex", "int", existingColumnNames); + }); + } } /// @@ -139,18 +144,21 @@ namespace Emby.Server.Implementations.Data { cancellationToken.ThrowIfCancellationRequested(); - using (WriteLock.Write()) + using (var connection = CreateConnection()) { - _connection.RunInTransaction(db => + using (WriteLock.Write()) { - SaveUserData(db, userId, key, userData); - }); + connection.RunInTransaction(db => + { + SaveUserData(db, userId, key, userData); + }); + } } } private void SaveUserData(IDatabaseConnection db, Guid userId, string key, UserItemData userData) { - using (var statement = _connection.PrepareStatement("replace into userdata (key, userId, rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex) values (@key, @userId, @rating,@played,@playCount,@isFavorite,@playbackPositionTicks,@lastPlayedDate,@AudioStreamIndex,@SubtitleStreamIndex)")) + using (var statement = db.PrepareStatement("replace into userdata (key, userId, rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex) values (@key, @userId, @rating,@played,@playCount,@isFavorite,@playbackPositionTicks,@lastPlayedDate,@AudioStreamIndex,@SubtitleStreamIndex)")) { statement.TryBind("@UserId", userId.ToGuidParamValue()); statement.TryBind("@Key", key); @@ -207,15 +215,18 @@ namespace Emby.Server.Implementations.Data { cancellationToken.ThrowIfCancellationRequested(); - using (WriteLock.Write()) + using (var connection = CreateConnection()) { - _connection.RunInTransaction(db => + using (WriteLock.Write()) { - foreach (var userItemData in userDataList) + connection.RunInTransaction(db => { - SaveUserData(db, userId, userItemData.Key, userItemData); - } - }); + foreach (var userItemData in userDataList) + { + SaveUserData(db, userId, userItemData.Key, userItemData); + } + }); + } } } @@ -241,16 +252,19 @@ namespace Emby.Server.Implementations.Data throw new ArgumentNullException("key"); } - using (WriteLock.Write()) + using (var connection = CreateConnection(true)) { - using (var statement = _connection.PrepareStatement("select key,userid,rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex from userdata where key =@Key and userId=@UserId")) + using (WriteLock.Read()) { - statement.TryBind("@UserId", userId.ToGuidParamValue()); - statement.TryBind("@Key", key); - - foreach (var row in statement.ExecuteQuery()) + using (var statement = connection.PrepareStatement("select key,userid,rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex from userdata where key =@Key and userId=@UserId")) { - return ReadRow(row); + statement.TryBind("@UserId", userId.ToGuidParamValue()); + statement.TryBind("@Key", key); + + foreach (var row in statement.ExecuteQuery()) + { + return ReadRow(row); + } } } } @@ -291,15 +305,18 @@ namespace Emby.Server.Implementations.Data var list = new List(); - using (WriteLock.Write()) + using (var connection = CreateConnection()) { - using (var statement = _connection.PrepareStatement("select key,userid,rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex from userdata where userId=@UserId")) + using (WriteLock.Read()) { - statement.TryBind("@UserId", userId.ToGuidParamValue()); - - foreach (var row in statement.ExecuteQuery()) + using (var statement = connection.PrepareStatement("select key,userid,rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex from userdata where userId=@UserId")) { - list.Add(ReadRow(row)); + statement.TryBind("@UserId", userId.ToGuidParamValue()); + + foreach (var row in statement.ExecuteQuery()) + { + list.Add(ReadRow(row)); + } } } } diff --git a/Emby.Server.Implementations/Data/SqliteUserRepository.cs b/Emby.Server.Implementations/Data/SqliteUserRepository.cs index 5e148d95a..0a2f9e7cd 100644 --- a/Emby.Server.Implementations/Data/SqliteUserRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteUserRepository.cs @@ -50,6 +50,14 @@ namespace Emby.Server.Implementations.Data { using (var connection = CreateConnection()) { + connection.ExecuteAll(string.Join(";", new[] + { + "pragma default_temp_store = memory", + "pragma default_synchronous=Normal", + "pragma temp_store = memory", + "pragma synchronous=Normal", + })); + string[] queries = { "create table if not exists users (guid GUID primary key, data BLOB)", @@ -83,9 +91,9 @@ namespace Emby.Server.Implementations.Data cancellationToken.ThrowIfCancellationRequested(); - using (WriteLock.Write()) + using (var connection = CreateConnection()) { - using (var connection = CreateConnection()) + using (WriteLock.Write()) { connection.RunInTransaction(db => { @@ -108,9 +116,9 @@ namespace Emby.Server.Implementations.Data { var list = new List(); - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { foreach (var row in connection.Query("select guid,data from users")) { @@ -146,9 +154,9 @@ namespace Emby.Server.Implementations.Data cancellationToken.ThrowIfCancellationRequested(); - using (WriteLock.Write()) + using (var connection = CreateConnection()) { - using (var connection = CreateConnection()) + using (WriteLock.Write()) { connection.RunInTransaction(db => { diff --git a/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs b/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs index 5a3b64dbb..5fc691527 100644 --- a/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs +++ b/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs @@ -29,7 +29,16 @@ namespace Emby.Server.Implementations.Notifications { using (var connection = CreateConnection()) { + connection.ExecuteAll(string.Join(";", new[] + { + "pragma default_temp_store = memory", + "pragma default_synchronous=Normal", + "pragma temp_store = memory", + "pragma synchronous=Normal", + })); + string[] queries = { + "create table if not exists Notifications (Id GUID NOT NULL, UserId GUID NOT NULL, Date DATETIME NOT NULL, Name TEXT NOT NULL, Description TEXT NULL, Url TEXT NULL, Level TEXT NOT NULL, IsRead BOOLEAN NOT NULL, Category TEXT NOT NULL, RelatedId TEXT NULL, PRIMARY KEY (Id, UserId))", "create index if not exists idx_Notifications1 on Notifications(Id)", "create index if not exists idx_Notifications2 on Notifications(UserId)" @@ -103,9 +112,9 @@ namespace Emby.Server.Implementations.Notifications { var result = new NotificationsSummary(); - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { using (var statement = connection.PrepareStatement("select Level from Notifications where UserId=@UserId and IsRead=@IsRead")) { @@ -220,9 +229,9 @@ namespace Emby.Server.Implementations.Notifications cancellationToken.ThrowIfCancellationRequested(); - using (WriteLock.Write()) + using (var connection = CreateConnection()) { - using (var connection = CreateConnection()) + using (WriteLock.Write()) { connection.RunInTransaction(conn => { @@ -283,9 +292,9 @@ namespace Emby.Server.Implementations.Notifications { cancellationToken.ThrowIfCancellationRequested(); - using (WriteLock.Write()) + using (var connection = CreateConnection()) { - using (var connection = CreateConnection()) + using (WriteLock.Write()) { connection.RunInTransaction(conn => { @@ -305,9 +314,9 @@ namespace Emby.Server.Implementations.Notifications { cancellationToken.ThrowIfCancellationRequested(); - using (WriteLock.Write()) + using (var connection = CreateConnection()) { - using (var connection = CreateConnection()) + using (WriteLock.Write()) { connection.RunInTransaction(conn => { diff --git a/Emby.Server.Implementations/Security/AuthenticationRepository.cs b/Emby.Server.Implementations/Security/AuthenticationRepository.cs index a9141f153..0a598e9a7 100644 --- a/Emby.Server.Implementations/Security/AuthenticationRepository.cs +++ b/Emby.Server.Implementations/Security/AuthenticationRepository.cs @@ -30,9 +30,17 @@ namespace Emby.Server.Implementations.Security { using (var connection = CreateConnection()) { + connection.ExecuteAll(string.Join(";", new[] + { + "pragma default_temp_store = memory", + "pragma default_synchronous=Normal", + "pragma temp_store = memory", + "pragma synchronous=Normal", + })); + string[] queries = { - "create table if not exists AccessTokens (Id GUID PRIMARY KEY, AccessToken TEXT NOT NULL, DeviceId TEXT, AppName TEXT, AppVersion TEXT, DeviceName TEXT, UserId TEXT, IsActive BIT, DateCreated DATETIME NOT NULL, DateRevoked DATETIME)", + "create table if not exists AccessTokens (Id GUID PRIMARY KEY, AccessToken TEXT NOT NULL, DeviceId TEXT, AppName TEXT, AppVersion TEXT, DeviceName TEXT, UserId TEXT, IsActive BIT, DateCreated DATETIME NOT NULL, DateRevoked DATETIME)", "create index if not exists idx_AccessTokens on AccessTokens(Id)" }; @@ -63,9 +71,9 @@ namespace Emby.Server.Implementations.Security cancellationToken.ThrowIfCancellationRequested(); - using (WriteLock.Write()) + using (var connection = CreateConnection()) { - using (var connection = CreateConnection()) + using (WriteLock.Write()) { connection.RunInTransaction(db => { @@ -200,28 +208,31 @@ namespace Emby.Server.Implementations.Security var list = new List(); - using (var statement = connection.PrepareStatement(commandText)) + using (WriteLock.Read()) { - BindAuthenticationQueryParams(query, statement); - - foreach (var row in statement.ExecuteQuery()) - { - list.Add(Get(row)); - } - - using (var totalCountStatement = connection.PrepareStatement("select count (Id) from AccessTokens" + whereTextWithoutPaging)) + using (var statement = connection.PrepareStatement(commandText)) { - BindAuthenticationQueryParams(query, totalCountStatement); + BindAuthenticationQueryParams(query, statement); - var count = totalCountStatement.ExecuteQuery() - .SelectScalarInt() - .First(); + foreach (var row in statement.ExecuteQuery()) + { + list.Add(Get(row)); + } - return new QueryResult() + using (var totalCountStatement = connection.PrepareStatement("select count (Id) from AccessTokens" + whereTextWithoutPaging)) { - Items = list.ToArray(), - TotalRecordCount = count - }; + BindAuthenticationQueryParams(query, totalCountStatement); + + var count = totalCountStatement.ExecuteQuery() + .SelectScalarInt() + .First(); + + return new QueryResult() + { + Items = list.ToArray(), + TotalRecordCount = count + }; + } } } } @@ -234,9 +245,9 @@ namespace Emby.Server.Implementations.Security throw new ArgumentNullException("id"); } - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { var commandText = BaseSelectText + " where Id=@Id"; diff --git a/Emby.Server.Implementations/Social/SharingRepository.cs b/Emby.Server.Implementations/Social/SharingRepository.cs index 9065ee108..e0ee62780 100644 --- a/Emby.Server.Implementations/Social/SharingRepository.cs +++ b/Emby.Server.Implementations/Social/SharingRepository.cs @@ -27,6 +27,14 @@ namespace Emby.Server.Implementations.Social { using (var connection = CreateConnection()) { + connection.ExecuteAll(string.Join(";", new[] + { + "pragma default_temp_store = memory", + "pragma default_synchronous=Normal", + "pragma temp_store = memory", + "pragma synchronous=Normal", + })); + string[] queries = { "create table if not exists Shares (Id GUID, ItemId TEXT, UserId TEXT, ExpirationDate DateTime, PRIMARY KEY (Id))", @@ -50,9 +58,9 @@ namespace Emby.Server.Implementations.Social throw new ArgumentNullException("info.Id"); } - using (WriteLock.Write()) + using (var connection = CreateConnection()) { - using (var connection = CreateConnection()) + using (WriteLock.Write()) { connection.RunInTransaction(db => { @@ -75,9 +83,9 @@ namespace Emby.Server.Implementations.Social throw new ArgumentNullException("id"); } - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { var commandText = "select Id, ItemId, UserId, ExpirationDate from Shares where id = ?"; diff --git a/Emby.Server.Implementations/Sync/SyncRepository.cs b/Emby.Server.Implementations/Sync/SyncRepository.cs index 6fb918f15..36558142b 100644 --- a/Emby.Server.Implementations/Sync/SyncRepository.cs +++ b/Emby.Server.Implementations/Sync/SyncRepository.cs @@ -43,6 +43,14 @@ namespace Emby.Server.Implementations.Sync { using (var connection = CreateConnection()) { + connection.ExecuteAll(string.Join(";", new[] + { + "pragma default_temp_store = memory", + "pragma default_synchronous=Normal", + "pragma temp_store = memory", + "pragma synchronous=Normal", + })); + string[] queries = { "create table if not exists SyncJobs (Id GUID PRIMARY KEY, TargetId TEXT NOT NULL, Name TEXT NOT NULL, Profile TEXT, Quality TEXT, Bitrate INT, Status TEXT NOT NULL, Progress FLOAT, UserId TEXT NOT NULL, ItemIds TEXT NOT NULL, Category TEXT, ParentId TEXT, UnwatchedOnly BIT, ItemLimit INT, SyncNewContent BIT, DateCreated DateTime, DateLastModified DateTime, ItemCount int)", @@ -95,9 +103,9 @@ namespace Emby.Server.Implementations.Sync throw new ArgumentNullException("id"); } - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { var commandText = BaseJobSelectText + " where Id=?"; var paramList = new List(); @@ -206,9 +214,9 @@ namespace Emby.Server.Implementations.Sync CheckDisposed(); - using (WriteLock.Write()) + using (var connection = CreateConnection()) { - using (var connection = CreateConnection()) + using (WriteLock.Write()) { string commandText; var paramList = new List(); @@ -259,9 +267,9 @@ namespace Emby.Server.Implementations.Sync CheckDisposed(); - using (WriteLock.Write()) + using (var connection = CreateConnection()) { - using (var connection = CreateConnection()) + using (WriteLock.Write()) { connection.RunInTransaction(conn => { @@ -281,9 +289,9 @@ namespace Emby.Server.Implementations.Sync CheckDisposed(); - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { var commandText = BaseJobSelectText; var paramList = new List(); @@ -379,11 +387,11 @@ namespace Emby.Server.Implementations.Sync CheckDisposed(); - using (WriteLock.Read()) - { - var guid = new Guid(id); + var guid = new Guid(id); - using (var connection = CreateConnection(true)) + using (var connection = CreateConnection(true)) + { + using (WriteLock.Read()) { var commandText = BaseJobItemSelectText + " where Id=?"; var paramList = new List(); @@ -407,9 +415,9 @@ namespace Emby.Server.Implementations.Sync throw new ArgumentNullException("query"); } - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { var commandText = baseSelectText; var paramList = new List(); @@ -487,30 +495,30 @@ namespace Emby.Server.Implementations.Sync var now = DateTime.UtcNow; - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { - using (var connection = CreateConnection(true)) - { - var commandText = "select ItemId,Status,Progress from SyncJobItems"; - var whereClauses = new List(); + var commandText = "select ItemId,Status,Progress from SyncJobItems"; + var whereClauses = new List(); - if (!string.IsNullOrWhiteSpace(query.TargetId)) - { - whereClauses.Add("TargetId=@TargetId"); - } + if (!string.IsNullOrWhiteSpace(query.TargetId)) + { + whereClauses.Add("TargetId=@TargetId"); + } - if (query.Statuses.Length > 0) - { - var statuses = string.Join(",", query.Statuses.Select(i => "'" + i.ToString() + "'").ToArray()); + if (query.Statuses.Length > 0) + { + var statuses = string.Join(",", query.Statuses.Select(i => "'" + i.ToString() + "'").ToArray()); - whereClauses.Add(string.Format("Status in ({0})", statuses)); - } + whereClauses.Add(string.Format("Status in ({0})", statuses)); + } - if (whereClauses.Count > 0) - { - commandText += " where " + string.Join(" AND ", whereClauses.ToArray()); - } + if (whereClauses.Count > 0) + { + commandText += " where " + string.Join(" AND ", whereClauses.ToArray()); + } + using (WriteLock.Read()) + { using (var statement = connection.PrepareStatement(commandText)) { if (!string.IsNullOrWhiteSpace(query.TargetId)) @@ -664,9 +672,9 @@ namespace Emby.Server.Implementations.Sync CheckDisposed(); - using (WriteLock.Write()) + using (var connection = CreateConnection()) { - using (var connection = CreateConnection()) + using (WriteLock.Write()) { string commandText; -- cgit v1.2.3 From f275d7f3d2f40f5e4cbe2f97df6dbd9be8ec37fe Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Mon, 21 Nov 2016 03:54:53 -0500 Subject: reduce library queries --- .../EnvironmentInfo/EnvironmentInfo.cs | 5 ++ Emby.Server.Core/ApplicationHost.cs | 2 +- .../Activity/ActivityRepository.cs | 5 +- .../Data/BaseSqliteRepository.cs | 37 +++++++-- .../Data/SqliteDisplayPreferencesRepository.cs | 5 +- .../Data/SqliteFileOrganizationRepository.cs | 5 +- .../Data/SqliteItemRepository.cs | 96 +++++++++++----------- .../Data/SqliteUserDataRepository.cs | 5 +- .../Data/SqliteUserRepository.cs | 5 +- Emby.Server.Implementations/Dto/DtoService.cs | 16 +++- .../Library/LibraryManager.cs | 31 +++---- .../Library/UserViewManager.cs | 30 ++++--- .../LiveTv/LiveTvManager.cs | 2 +- .../Notifications/SqliteNotificationsRepository.cs | 11 ++- .../Security/AuthenticationRepository.cs | 5 +- .../Social/SharingRepository.cs | 5 +- Emby.Server.Implementations/Sync/SyncRepository.cs | 5 +- Emby.Server.Implementations/TV/TVSeriesManager.cs | 4 +- MediaBrowser.Api/Library/LibraryService.cs | 2 +- MediaBrowser.Api/Movies/MoviesService.cs | 17 +--- MediaBrowser.Controller/Entities/Audio/Audio.cs | 6 ++ MediaBrowser.Controller/Entities/BaseItem.cs | 22 ++++- MediaBrowser.Controller/Entities/Folder.cs | 39 +++++---- .../Entities/InternalItemsQuery.cs | 1 - MediaBrowser.Controller/Entities/TV/Episode.cs | 6 ++ MediaBrowser.Controller/Entities/TV/Season.cs | 6 ++ MediaBrowser.Controller/Entities/TV/Series.cs | 1 - .../Entities/UserViewBuilder.cs | 10 +-- MediaBrowser.Controller/Library/ILibraryManager.cs | 5 +- MediaBrowser.Controller/TV/ITVSeriesManager.cs | 2 +- MediaBrowser.Model/System/IEnvironmentInfo.cs | 1 + MediaBrowser.Providers/Manager/MetadataService.cs | 14 ++-- 32 files changed, 230 insertions(+), 176 deletions(-) (limited to 'Emby.Server.Implementations/Activity/ActivityRepository.cs') diff --git a/Emby.Common.Implementations/EnvironmentInfo/EnvironmentInfo.cs b/Emby.Common.Implementations/EnvironmentInfo/EnvironmentInfo.cs index c040e3931..ebd596dc4 100644 --- a/Emby.Common.Implementations/EnvironmentInfo/EnvironmentInfo.cs +++ b/Emby.Common.Implementations/EnvironmentInfo/EnvironmentInfo.cs @@ -105,5 +105,10 @@ namespace Emby.Common.Implementations.EnvironmentInfo { return null; } + + public string StackTrace + { + get { return Environment.StackTrace; } + } } } diff --git a/Emby.Server.Core/ApplicationHost.cs b/Emby.Server.Core/ApplicationHost.cs index 55a851033..d48138080 100644 --- a/Emby.Server.Core/ApplicationHost.cs +++ b/Emby.Server.Core/ApplicationHost.cs @@ -551,7 +551,7 @@ namespace Emby.Server.Core DisplayPreferencesRepository = displayPreferencesRepo; RegisterSingleInstance(DisplayPreferencesRepository); - var itemRepo = new SqliteItemRepository(ServerConfigurationManager, JsonSerializer, LogManager.GetLogger("SqliteItemRepository"), MemoryStreamFactory, assemblyInfo, FileSystemManager); + var itemRepo = new SqliteItemRepository(ServerConfigurationManager, JsonSerializer, LogManager.GetLogger("SqliteItemRepository"), MemoryStreamFactory, assemblyInfo, FileSystemManager, EnvironmentInfo); ItemRepository = itemRepo; RegisterSingleInstance(ItemRepository); diff --git a/Emby.Server.Implementations/Activity/ActivityRepository.cs b/Emby.Server.Implementations/Activity/ActivityRepository.cs index 8a7573d66..eef9792de 100644 --- a/Emby.Server.Implementations/Activity/ActivityRepository.cs +++ b/Emby.Server.Implementations/Activity/ActivityRepository.cs @@ -29,10 +29,9 @@ namespace Emby.Server.Implementations.Activity { connection.ExecuteAll(string.Join(";", new[] { + "PRAGMA page_size=4096", "pragma default_temp_store = memory", - "pragma default_synchronous=Normal", - "pragma temp_store = memory", - "pragma synchronous=Normal", + "pragma temp_store = memory" })); string[] queries = { diff --git a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs index 382c7f245..d4226ec25 100644 --- a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs +++ b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs @@ -40,6 +40,8 @@ namespace Emby.Server.Implementations.Data private static bool _versionLogged; + private string _defaultWal; + protected SQLiteDatabaseConnection CreateConnection(bool isReadOnly = false, Action onConnect = null) { if (!_versionLogged) @@ -51,6 +53,15 @@ namespace Emby.Server.Implementations.Data ConnectionFlags connectionFlags; + if (isReadOnly) + { + //Logger.Info("Opening read connection"); + } + else + { + //Logger.Info("Opening write connection"); + } + isReadOnly = false; if (isReadOnly) @@ -70,12 +81,16 @@ namespace Emby.Server.Implementations.Data var db = SQLite3.Open(DbFilePath, connectionFlags, null); + if (string.IsNullOrWhiteSpace(_defaultWal)) + { + _defaultWal = db.Query("PRAGMA journal_mode").SelectScalarString().First(); + } + var queries = new List { + "PRAGMA default_temp_store=memory", "pragma temp_store = memory", - "PRAGMA page_size=4096", - "PRAGMA journal_mode=WAL", - "pragma synchronous=Normal", + "PRAGMA journal_mode=WAL" //"PRAGMA cache size=-10000" }; @@ -90,13 +105,19 @@ namespace Emby.Server.Implementations.Data //// db.Execute(query); ////} - using (WriteLock.Write()) - { - db.ExecuteAll(string.Join(";", queries.ToArray())); + //Logger.Info("synchronous: " + db.Query("PRAGMA synchronous").SelectScalarString().First()); + //Logger.Info("temp_store: " + db.Query("PRAGMA temp_store").SelectScalarString().First()); - if (onConnect != null) + //if (!string.Equals(_defaultWal, "wal", StringComparison.OrdinalIgnoreCase) || onConnect != null) + { + using (WriteLock.Write()) { - onConnect(db); + db.ExecuteAll(string.Join(";", queries.ToArray())); + + if (onConnect != null) + { + onConnect(db); + } } } diff --git a/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs b/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs index ab927ce86..17afbcfa9 100644 --- a/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs @@ -56,10 +56,9 @@ namespace Emby.Server.Implementations.Data { connection.ExecuteAll(string.Join(";", new[] { + "PRAGMA page_size=4096", "pragma default_temp_store = memory", - "pragma default_synchronous=Normal", - "pragma temp_store = memory", - "pragma synchronous=Normal", + "pragma temp_store = memory" })); string[] queries = { diff --git a/Emby.Server.Implementations/Data/SqliteFileOrganizationRepository.cs b/Emby.Server.Implementations/Data/SqliteFileOrganizationRepository.cs index a71682329..efc0ee2ed 100644 --- a/Emby.Server.Implementations/Data/SqliteFileOrganizationRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteFileOrganizationRepository.cs @@ -33,10 +33,9 @@ namespace Emby.Server.Implementations.Data { connection.ExecuteAll(string.Join(";", new[] { + "PRAGMA page_size=4096", "pragma default_temp_store = memory", - "pragma default_synchronous=Normal", - "pragma temp_store = memory", - "pragma synchronous=Normal", + "pragma temp_store = memory" })); string[] queries = { diff --git a/Emby.Server.Implementations/Data/SqliteItemRepository.cs b/Emby.Server.Implementations/Data/SqliteItemRepository.cs index 754af9640..bc1eca06a 100644 --- a/Emby.Server.Implementations/Data/SqliteItemRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteItemRepository.cs @@ -29,6 +29,7 @@ using MediaBrowser.Server.Implementations.Devices; using MediaBrowser.Server.Implementations.Playlists; using MediaBrowser.Model.Reflection; using SQLitePCL.pretty; +using MediaBrowser.Model.System; namespace Emby.Server.Implementations.Data { @@ -66,14 +67,14 @@ namespace Emby.Server.Implementations.Data private readonly string _criticReviewsPath; - public const int LatestSchemaVersion = 109; private readonly IMemoryStreamFactory _memoryStreamProvider; private readonly IFileSystem _fileSystem; + private readonly IEnvironmentInfo _environmentInfo; /// /// Initializes a new instance of the class. /// - public SqliteItemRepository(IServerConfigurationManager config, IJsonSerializer jsonSerializer, ILogger logger, IMemoryStreamFactory memoryStreamProvider, IAssemblyInfo assemblyInfo, IFileSystem fileSystem) + public SqliteItemRepository(IServerConfigurationManager config, IJsonSerializer jsonSerializer, ILogger logger, IMemoryStreamFactory memoryStreamProvider, IAssemblyInfo assemblyInfo, IFileSystem fileSystem, IEnvironmentInfo environmentInfo) : base(logger) { if (config == null) @@ -89,6 +90,7 @@ namespace Emby.Server.Implementations.Data _jsonSerializer = jsonSerializer; _memoryStreamProvider = memoryStreamProvider; _fileSystem = fileSystem; + _environmentInfo = environmentInfo; _typeMapper = new TypeMapper(assemblyInfo); _criticReviewsPath = Path.Combine(_config.ApplicationPaths.DataPath, "critic-reviews"); @@ -129,10 +131,9 @@ namespace Emby.Server.Implementations.Data _connection.ExecuteAll(string.Join(";", new[] { - "pragma default_temp_store = memory", - "pragma default_synchronous=Normal", - "pragma temp_store = memory", - "pragma synchronous=Normal", + "PRAGMA page_size=4096", + "PRAGMA default_temp_store=memory", + "PRAGMA temp_store=memory" })); var createMediaStreamsTableCommand @@ -193,7 +194,6 @@ namespace Emby.Server.Implementations.Data AddColumn(db, "TypedBaseItems", "ProductionYear", "INT", existingColumnNames); AddColumn(db, "TypedBaseItems", "ParentId", "GUID", existingColumnNames); AddColumn(db, "TypedBaseItems", "Genres", "Text", existingColumnNames); - AddColumn(db, "TypedBaseItems", "SchemaVersion", "INT", existingColumnNames); AddColumn(db, "TypedBaseItems", "SortName", "Text", existingColumnNames); AddColumn(db, "TypedBaseItems", "RunTimeTicks", "BIGINT", existingColumnNames); @@ -205,7 +205,6 @@ namespace Emby.Server.Implementations.Data AddColumn(db, "TypedBaseItems", "DateModified", "DATETIME", existingColumnNames); AddColumn(db, "TypedBaseItems", "ForcedSortName", "Text", existingColumnNames); - AddColumn(db, "TypedBaseItems", "IsOffline", "BIT", existingColumnNames); AddColumn(db, "TypedBaseItems", "LocationType", "Text", existingColumnNames); AddColumn(db, "TypedBaseItems", "IsSeries", "BIT", existingColumnNames); @@ -381,7 +380,6 @@ namespace Emby.Server.Implementations.Data "data", "StartDate", "EndDate", - "IsOffline", "ChannelId", "IsMovie", "IsSports", @@ -529,7 +527,6 @@ namespace Emby.Server.Implementations.Data "ParentId", "Genres", "InheritedParentalRatingValue", - "SchemaVersion", "SortName", "RunTimeTicks", "OfficialRatingDescription", @@ -539,7 +536,6 @@ namespace Emby.Server.Implementations.Data "DateCreated", "DateModified", "ForcedSortName", - "IsOffline", "LocationType", "PreferredMetadataLanguage", "PreferredMetadataCountryCode", @@ -790,7 +786,6 @@ namespace Emby.Server.Implementations.Data } saveItemStatement.TryBind("@InheritedParentalRatingValue", item.GetInheritedParentalRatingValue() ?? 0); - saveItemStatement.TryBind("@SchemaVersion", LatestSchemaVersion); saveItemStatement.TryBind("@SortName", item.SortName); saveItemStatement.TryBind("@RunTimeTicks", item.RunTimeTicks); @@ -803,7 +798,6 @@ namespace Emby.Server.Implementations.Data saveItemStatement.TryBind("@DateModified", item.DateModified); saveItemStatement.TryBind("@ForcedSortName", item.ForcedSortName); - saveItemStatement.TryBind("@IsOffline", item.IsOffline); saveItemStatement.TryBind("@LocationType", item.LocationType.ToString()); saveItemStatement.TryBind("@PreferredMetadataLanguage", item.PreferredMetadataLanguage); @@ -1182,7 +1176,7 @@ namespace Emby.Server.Implementations.Data } CheckDisposed(); - + //Logger.Info("Retrieving item {0}", id.ToString("N")); using (var connection = CreateConnection(true)) { using (WriteLock.Read()) @@ -1369,64 +1363,72 @@ namespace Emby.Server.Implementations.Data if (!reader.IsDBNull(4)) { - item.IsOffline = reader.GetBoolean(4); + item.ChannelId = reader.GetString(4); } - if (!reader.IsDBNull(5)) - { - item.ChannelId = reader.GetString(5); - } + var index = 5; var hasProgramAttributes = item as IHasProgramAttributes; if (hasProgramAttributes != null) { - if (!reader.IsDBNull(6)) + if (!reader.IsDBNull(index)) { - hasProgramAttributes.IsMovie = reader.GetBoolean(6); + hasProgramAttributes.IsMovie = reader.GetBoolean(index); } + index++; - if (!reader.IsDBNull(7)) + if (!reader.IsDBNull(index)) { - hasProgramAttributes.IsSports = reader.GetBoolean(7); + hasProgramAttributes.IsSports = reader.GetBoolean(index); } + index++; - if (!reader.IsDBNull(8)) + if (!reader.IsDBNull(index)) { - hasProgramAttributes.IsKids = reader.GetBoolean(8); + hasProgramAttributes.IsKids = reader.GetBoolean(index); } + index++; - if (!reader.IsDBNull(9)) + if (!reader.IsDBNull(index)) { - hasProgramAttributes.IsSeries = reader.GetBoolean(9); + hasProgramAttributes.IsSeries = reader.GetBoolean(index); } + index++; - if (!reader.IsDBNull(10)) + if (!reader.IsDBNull(index)) { - hasProgramAttributes.IsLive = reader.GetBoolean(10); + hasProgramAttributes.IsLive = reader.GetBoolean(index); } + index++; - if (!reader.IsDBNull(11)) + if (!reader.IsDBNull(index)) { - hasProgramAttributes.IsNews = reader.GetBoolean(11); + hasProgramAttributes.IsNews = reader.GetBoolean(index); } + index++; - if (!reader.IsDBNull(12)) + if (!reader.IsDBNull(index)) { - hasProgramAttributes.IsPremiere = reader.GetBoolean(12); + hasProgramAttributes.IsPremiere = reader.GetBoolean(index); } + index++; - if (!reader.IsDBNull(13)) + if (!reader.IsDBNull(index)) { - hasProgramAttributes.EpisodeTitle = reader.GetString(13); + hasProgramAttributes.EpisodeTitle = reader.GetString(index); } + index++; - if (!reader.IsDBNull(14)) + if (!reader.IsDBNull(index)) { - hasProgramAttributes.IsRepeat = reader.GetBoolean(14); + hasProgramAttributes.IsRepeat = reader.GetBoolean(index); } + index++; + } + else + { + index += 9; } - - var index = 15; if (!reader.IsDBNull(index)) { @@ -2368,6 +2370,8 @@ namespace Emby.Server.Implementations.Data CheckDisposed(); + //Logger.Info("GetItemList: " + _environmentInfo.StackTrace); + var now = DateTime.UtcNow; var list = new List(); @@ -2533,6 +2537,7 @@ namespace Emby.Server.Implementations.Data TotalRecordCount = returnList.Count }; } + //Logger.Info("GetItems: " + _environmentInfo.StackTrace); var now = DateTime.UtcNow; @@ -2770,6 +2775,7 @@ namespace Emby.Server.Implementations.Data } CheckDisposed(); + //Logger.Info("GetItemIdsList: " + _environmentInfo.StackTrace); var now = DateTime.UtcNow; @@ -2928,6 +2934,7 @@ namespace Emby.Server.Implementations.Data TotalRecordCount = returnList.Count }; } + //Logger.Info("GetItemIds: " + _environmentInfo.StackTrace); var now = DateTime.UtcNow; @@ -3053,14 +3060,6 @@ namespace Emby.Server.Implementations.Data statement.TryBind("@IsLocked", query.IsLocked); } } - if (query.IsOffline.HasValue) - { - whereClauses.Add("IsOffline=@IsOffline"); - if (statement != null) - { - statement.TryBind("@IsOffline", query.IsOffline); - } - } var exclusiveProgramAttribtues = !(query.IsMovie ?? true) || !(query.IsSports ?? true) || @@ -4721,7 +4720,7 @@ namespace Emby.Server.Implementations.Data using (var connection = CreateConnection(true)) { - using (WriteLock.Write()) + using (WriteLock.Read()) { foreach (var row in connection.Query(commandText)) { @@ -4750,6 +4749,7 @@ namespace Emby.Server.Implementations.Data } CheckDisposed(); + //Logger.Info("GetItemValues: " + _environmentInfo.StackTrace); var now = DateTime.UtcNow; diff --git a/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs b/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs index 0b4768c14..9f7cce13b 100644 --- a/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs @@ -45,10 +45,9 @@ namespace Emby.Server.Implementations.Data { connection.ExecuteAll(string.Join(";", new[] { + "PRAGMA page_size=4096", "pragma default_temp_store = memory", - "pragma default_synchronous=Normal", - "pragma temp_store = memory", - "pragma synchronous=Normal", + "pragma temp_store = memory" })); string[] queries = { diff --git a/Emby.Server.Implementations/Data/SqliteUserRepository.cs b/Emby.Server.Implementations/Data/SqliteUserRepository.cs index 0a2f9e7cd..f902d981f 100644 --- a/Emby.Server.Implementations/Data/SqliteUserRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteUserRepository.cs @@ -52,10 +52,9 @@ namespace Emby.Server.Implementations.Data { connection.ExecuteAll(string.Join(";", new[] { + "PRAGMA page_size=4096", "pragma default_temp_store = memory", - "pragma default_synchronous=Normal", - "pragma temp_store = memory", - "pragma synchronous=Normal", + "pragma temp_store = memory" })); string[] queries = { diff --git a/Emby.Server.Implementations/Dto/DtoService.cs b/Emby.Server.Implementations/Dto/DtoService.cs index 85549439b..438fc55d7 100644 --- a/Emby.Server.Implementations/Dto/DtoService.cs +++ b/Emby.Server.Implementations/Dto/DtoService.cs @@ -482,7 +482,7 @@ namespace Emby.Server.Implementations.Dto { if (dtoOptions.EnableUserData) { - dto.UserData = _userDataRepository.GetUserDataDto(item, user).Result; + dto.UserData = await _userDataRepository.GetUserDataDto(item, user).ConfigureAwait(false); } } @@ -1450,11 +1450,19 @@ namespace Emby.Server.Implementations.Dto private void AddInheritedImages(BaseItemDto dto, BaseItem item, DtoOptions options, BaseItem owner) { + if (!item.SupportsInheritedParentImages) + { + return; + } + var logoLimit = options.GetImageLimit(ImageType.Logo); var artLimit = options.GetImageLimit(ImageType.Art); var thumbLimit = options.GetImageLimit(ImageType.Thumb); var backdropLimit = options.GetImageLimit(ImageType.Backdrop); + // For now. Emby apps are not using this + artLimit = 0; + if (logoLimit == 0 && artLimit == 0 && thumbLimit == 0 && backdropLimit == 0) { return; @@ -1515,6 +1523,12 @@ namespace Emby.Server.Implementations.Dto } isFirst = false; + + if (!parent.SupportsInheritedParentImages) + { + break; + } + parent = parent.GetParent(); } } diff --git a/Emby.Server.Implementations/Library/LibraryManager.cs b/Emby.Server.Implementations/Library/LibraryManager.cs index 13c6485e5..283d193dd 100644 --- a/Emby.Server.Implementations/Library/LibraryManager.cs +++ b/Emby.Server.Implementations/Library/LibraryManager.cs @@ -339,11 +339,6 @@ namespace Emby.Server.Implementations.Library { throw new ArgumentNullException("item"); } - RegisterItem(item.Id, item); - } - - private void RegisterItem(Guid id, BaseItem item) - { if (item is IItemByName) { if (!(item is MusicArtist)) @@ -354,13 +349,13 @@ namespace Emby.Server.Implementations.Library if (item.IsFolder) { - if (!(item is ICollectionFolder) && !(item is UserView) && !(item is Channel) && !(item is AggregateFolder)) - { - if (item.SourceType != SourceType.Library) - { - return; - } - } + //if (!(item is ICollectionFolder) && !(item is UserView) && !(item is Channel) && !(item is AggregateFolder)) + //{ + // if (item.SourceType != SourceType.Library) + // { + // return; + // } + //} } else { @@ -370,7 +365,7 @@ namespace Emby.Server.Implementations.Library } } - LibraryItemsCache.AddOrUpdate(id, item, delegate { return item; }); + LibraryItemsCache.AddOrUpdate(item.Id, item, delegate { return item; }); } public async Task DeleteItem(BaseItem item, DeleteOptions options) @@ -822,7 +817,7 @@ namespace Emby.Server.Implementations.Library return _userRootFolder; } - + public BaseItem FindByPath(string path, bool? isFolder) { // If this returns multiple items it could be tricky figuring out which one is correct. @@ -836,7 +831,7 @@ namespace Emby.Server.Implementations.Library SortOrder = SortOrder.Descending, Limit = 1 }; - + return GetItemList(query) .FirstOrDefault(); } @@ -1273,10 +1268,8 @@ namespace Emby.Server.Implementations.Library return ItemRepository.GetItemList(query); } - public IEnumerable GetItemList(InternalItemsQuery query, IEnumerable parentIds) + public IEnumerable GetItemList(InternalItemsQuery query, List parents) { - var parents = parentIds.Select(i => GetItemById(new Guid(i))).Where(i => i != null).ToList(); - SetTopParentIdsOrAncestors(query, parents); if (query.AncestorIds.Length == 0 && query.TopParentIds.Length == 0) @@ -1536,7 +1529,7 @@ namespace Emby.Server.Implementations.Library } // Handle grouping - if (user != null && !string.IsNullOrWhiteSpace(view.ViewType) && UserView.IsEligibleForGrouping(view.ViewType)) + if (user != null && !string.IsNullOrWhiteSpace(view.ViewType) && UserView.IsEligibleForGrouping(view.ViewType) && user.Configuration.GroupedFolders.Length > 0) { return user.RootFolder .GetChildren(user, true) diff --git a/Emby.Server.Implementations/Library/UserViewManager.cs b/Emby.Server.Implementations/Library/UserViewManager.cs index b93f565a3..f7cc8bb73 100644 --- a/Emby.Server.Implementations/Library/UserViewManager.cs +++ b/Emby.Server.Implementations/Library/UserViewManager.cs @@ -245,20 +245,26 @@ namespace Emby.Server.Implementations.Library var includeItemTypes = request.IncludeItemTypes; var limit = request.Limit ?? 10; - var parentIds = string.IsNullOrEmpty(parentId) - ? new string[] { } - : new[] { parentId }; + var parents = new List(); - if (parentIds.Length == 0) + if (!string.IsNullOrWhiteSpace(parentId)) { - parentIds = user.RootFolder.GetChildren(user, true) - .OfType() - .Select(i => i.Id.ToString("N")) - .Where(i => !user.Configuration.LatestItemsExcludes.Contains(i)) - .ToArray(); + var parent = _libraryManager.GetItemById(parentId) as Folder; + if (parent != null) + { + parents.Add(parent); + } + } + + if (parents.Count == 0) + { + parents = user.RootFolder.GetChildren(user, true) + .Where(i => i is Folder) + .Where(i => !user.Configuration.LatestItemsExcludes.Contains(i.Id.ToString("N"))) + .ToList(); } - if (parentIds.Length == 0) + if (parents.Count == 0) { return new List(); } @@ -283,10 +289,10 @@ namespace Emby.Server.Implementations.Library ExcludeItemTypes = excludeItemTypes, ExcludeLocationTypes = new[] { LocationType.Virtual }, Limit = limit * 5, - SourceTypes = parentIds.Length == 0 ? new[] { SourceType.Library } : new SourceType[] { }, + SourceTypes = parents.Count == 0 ? new[] { SourceType.Library } : new SourceType[] { }, IsPlayed = request.IsPlayed - }, parentIds); + }, parents); } } } diff --git a/Emby.Server.Implementations/LiveTv/LiveTvManager.cs b/Emby.Server.Implementations/LiveTv/LiveTvManager.cs index 3a6f23fe9..faf9687f4 100644 --- a/Emby.Server.Implementations/LiveTv/LiveTvManager.cs +++ b/Emby.Server.Implementations/LiveTv/LiveTvManager.cs @@ -491,7 +491,7 @@ namespace Emby.Server.Implementations.LiveTv var id = _tvDtoService.GetInternalChannelId(serviceName, channelInfo.Id); - var item = _itemRepo.RetrieveItem(id) as LiveTvChannel; + var item = _libraryManager.GetItemById(id) as LiveTvChannel; if (item == null) { diff --git a/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs b/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs index 5fc691527..66ef5d5d1 100644 --- a/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs +++ b/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs @@ -31,10 +31,9 @@ namespace Emby.Server.Implementations.Notifications { connection.ExecuteAll(string.Join(";", new[] { + "PRAGMA page_size=4096", "pragma default_temp_store = memory", - "pragma default_synchronous=Normal", - "pragma temp_store = memory", - "pragma synchronous=Normal", + "pragma temp_store = memory" })); string[] queries = { @@ -57,9 +56,9 @@ namespace Emby.Server.Implementations.Notifications { var result = new NotificationResult(); - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { - using (var connection = CreateConnection(true)) + //using (WriteLock.Read()) { var clauses = new List(); var paramList = new List(); @@ -114,7 +113,7 @@ namespace Emby.Server.Implementations.Notifications using (var connection = CreateConnection(true)) { - using (WriteLock.Read()) + //using (WriteLock.Read()) { using (var statement = connection.PrepareStatement("select Level from Notifications where UserId=@UserId and IsRead=@IsRead")) { diff --git a/Emby.Server.Implementations/Security/AuthenticationRepository.cs b/Emby.Server.Implementations/Security/AuthenticationRepository.cs index 0a598e9a7..dad05718c 100644 --- a/Emby.Server.Implementations/Security/AuthenticationRepository.cs +++ b/Emby.Server.Implementations/Security/AuthenticationRepository.cs @@ -32,10 +32,9 @@ namespace Emby.Server.Implementations.Security { connection.ExecuteAll(string.Join(";", new[] { + "PRAGMA page_size=4096", "pragma default_temp_store = memory", - "pragma default_synchronous=Normal", - "pragma temp_store = memory", - "pragma synchronous=Normal", + "pragma temp_store = memory" })); string[] queries = { diff --git a/Emby.Server.Implementations/Social/SharingRepository.cs b/Emby.Server.Implementations/Social/SharingRepository.cs index e0ee62780..6dab54bc6 100644 --- a/Emby.Server.Implementations/Social/SharingRepository.cs +++ b/Emby.Server.Implementations/Social/SharingRepository.cs @@ -29,10 +29,9 @@ namespace Emby.Server.Implementations.Social { connection.ExecuteAll(string.Join(";", new[] { + "PRAGMA page_size=4096", "pragma default_temp_store = memory", - "pragma default_synchronous=Normal", - "pragma temp_store = memory", - "pragma synchronous=Normal", + "pragma temp_store = memory" })); string[] queries = { diff --git a/Emby.Server.Implementations/Sync/SyncRepository.cs b/Emby.Server.Implementations/Sync/SyncRepository.cs index 36558142b..308c3bd00 100644 --- a/Emby.Server.Implementations/Sync/SyncRepository.cs +++ b/Emby.Server.Implementations/Sync/SyncRepository.cs @@ -45,10 +45,9 @@ namespace Emby.Server.Implementations.Sync { connection.ExecuteAll(string.Join(";", new[] { + "PRAGMA page_size=4096", "pragma default_temp_store = memory", - "pragma default_synchronous=Normal", - "pragma temp_store = memory", - "pragma synchronous=Normal", + "pragma temp_store = memory" })); string[] queries = { diff --git a/Emby.Server.Implementations/TV/TVSeriesManager.cs b/Emby.Server.Implementations/TV/TVSeriesManager.cs index a47aaa305..fd576e081 100644 --- a/Emby.Server.Implementations/TV/TVSeriesManager.cs +++ b/Emby.Server.Implementations/TV/TVSeriesManager.cs @@ -72,7 +72,7 @@ namespace Emby.Server.Implementations.TV return GetResult(episodes, null, request); } - public QueryResult GetNextUp(NextUpQuery request, IEnumerable parentsFolders) + public QueryResult GetNextUp(NextUpQuery request, List parentsFolders) { var user = _userManager.GetUserById(request.UserId); @@ -106,7 +106,7 @@ namespace Emby.Server.Implementations.TV PresentationUniqueKey = presentationUniqueKey, Limit = limit - }, parentsFolders.Select(i => i.Id.ToString("N"))).Cast(); + }, parentsFolders.Cast().ToList()).Cast(); // Avoid implicitly captured closure var episodes = GetNextUpEpisodes(request, user, items); diff --git a/MediaBrowser.Api/Library/LibraryService.cs b/MediaBrowser.Api/Library/LibraryService.cs index 36a58cc20..695718a25 100644 --- a/MediaBrowser.Api/Library/LibraryService.cs +++ b/MediaBrowser.Api/Library/LibraryService.cs @@ -369,7 +369,7 @@ namespace MediaBrowser.Api.Library if (item is Movie || (program != null && program.IsMovie) || item is Trailer) { - return new MoviesService(_userManager, _userDataManager, _libraryManager, _itemRepo, _dtoService, _config, _authContext) + return new MoviesService(_userManager, _libraryManager, _dtoService, _config, _authContext) { Request = Request, diff --git a/MediaBrowser.Api/Movies/MoviesService.cs b/MediaBrowser.Api/Movies/MoviesService.cs index 1b2fa4fff..b5c6f52fc 100644 --- a/MediaBrowser.Api/Movies/MoviesService.cs +++ b/MediaBrowser.Api/Movies/MoviesService.cs @@ -78,16 +78,8 @@ namespace MediaBrowser.Api.Movies /// private readonly IUserManager _userManager; - /// - /// The _user data repository - /// - private readonly IUserDataManager _userDataRepository; - /// - /// The _library manager - /// private readonly ILibraryManager _libraryManager; - private readonly IItemRepository _itemRepo; private readonly IDtoService _dtoService; private readonly IServerConfigurationManager _config; private readonly IAuthorizationContext _authContext; @@ -95,17 +87,10 @@ namespace MediaBrowser.Api.Movies /// /// Initializes a new instance of the class. /// - /// The user manager. - /// The user data repository. - /// The library manager. - /// The item repo. - /// The dto service. - public MoviesService(IUserManager userManager, IUserDataManager userDataRepository, ILibraryManager libraryManager, IItemRepository itemRepo, IDtoService dtoService, IServerConfigurationManager config, IAuthorizationContext authContext) + public MoviesService(IUserManager userManager, ILibraryManager libraryManager, IDtoService dtoService, IServerConfigurationManager config, IAuthorizationContext authContext) { _userManager = userManager; - _userDataRepository = userDataRepository; _libraryManager = libraryManager; - _itemRepo = itemRepo; _dtoService = dtoService; _config = config; _authContext = authContext; diff --git a/MediaBrowser.Controller/Entities/Audio/Audio.cs b/MediaBrowser.Controller/Entities/Audio/Audio.cs index 539cc5f22..e4f638cb6 100644 --- a/MediaBrowser.Controller/Entities/Audio/Audio.cs +++ b/MediaBrowser.Controller/Entities/Audio/Audio.cs @@ -61,6 +61,12 @@ namespace MediaBrowser.Controller.Entities.Audio get { return true; } } + [IgnoreDataMember] + public override bool SupportsInheritedParentImages + { + get { return true; } + } + [IgnoreDataMember] protected override bool SupportsOwnedItems { diff --git a/MediaBrowser.Controller/Entities/BaseItem.cs b/MediaBrowser.Controller/Entities/BaseItem.cs index cd61d2cce..b7ea7a92d 100644 --- a/MediaBrowser.Controller/Entities/BaseItem.cs +++ b/MediaBrowser.Controller/Entities/BaseItem.cs @@ -1569,6 +1569,12 @@ namespace MediaBrowser.Controller.Entities return IsVisibleStandaloneInternal(user, true); } + [IgnoreDataMember] + public virtual bool SupportsInheritedParentImages + { + get { return false; } + } + protected bool IsVisibleStandaloneInternal(User user, bool checkFolders) { if (!IsVisible(user)) @@ -2329,17 +2335,25 @@ namespace MediaBrowser.Controller.Entities { get { - if (GetParent() is AggregateFolder || this is BasePluginFolder || this is Channel) + if (this is BasePluginFolder || this is Channel) { return true; } var view = this as UserView; - if (view != null && string.Equals(view.ViewType, CollectionType.LiveTv, StringComparison.OrdinalIgnoreCase)) + if (view != null) { - return true; + if (string.Equals(view.ViewType, CollectionType.LiveTv, StringComparison.OrdinalIgnoreCase)) + { + return true; + } + if (string.Equals(view.ViewType, CollectionType.Channels, StringComparison.OrdinalIgnoreCase)) + { + return true; + } } - if (view != null && string.Equals(view.ViewType, CollectionType.Channels, StringComparison.OrdinalIgnoreCase)) + + if (GetParent() is AggregateFolder) { return true; } diff --git a/MediaBrowser.Controller/Entities/Folder.cs b/MediaBrowser.Controller/Entities/Folder.cs index 3df77673f..d4ddab7b2 100644 --- a/MediaBrowser.Controller/Entities/Folder.cs +++ b/MediaBrowser.Controller/Entities/Folder.cs @@ -1383,6 +1383,15 @@ namespace MediaBrowser.Controller.Entities { return false; } + var iItemByName = this as IItemByName; + if (iItemByName != null) + { + var hasDualAccess = this as IHasDualAccess; + if (hasDualAccess == null || hasDualAccess.IsAccessedByName) + { + return false; + } + } return true; } @@ -1395,17 +1404,6 @@ namespace MediaBrowser.Controller.Entities return; } - var unplayedQueryResult = await GetItems(new InternalItemsQuery(user) - { - Recursive = true, - IsFolder = false, - IsVirtualItem = false, - EnableTotalRecordCount = true, - Limit = 0, - IsPlayed = false - - }).ConfigureAwait(false); - var allItemsQueryResult = await GetItems(new InternalItemsQuery(user) { Recursive = true, @@ -1415,17 +1413,28 @@ namespace MediaBrowser.Controller.Entities Limit = 0 }).ConfigureAwait(false); + var recursiveItemCount = allItemsQueryResult.TotalRecordCount; if (itemDto != null) { itemDto.RecursiveItemCount = allItemsQueryResult.TotalRecordCount; } - var recursiveItemCount = allItemsQueryResult.TotalRecordCount; - double unplayedCount = unplayedQueryResult.TotalRecordCount; - - if (recursiveItemCount > 0) + if (recursiveItemCount > 0 && SupportsPlayedStatus) { + var unplayedQueryResult = recursiveItemCount > 0 ? await GetItems(new InternalItemsQuery(user) + { + Recursive = true, + IsFolder = false, + IsVirtualItem = false, + EnableTotalRecordCount = true, + Limit = 0, + IsPlayed = false + + }).ConfigureAwait(false) : new QueryResult(); + + double unplayedCount = unplayedQueryResult.TotalRecordCount; + var unplayedPercentage = (unplayedCount / recursiveItemCount) * 100; dto.PlayedPercentage = 100 - unplayedPercentage; dto.Played = dto.PlayedPercentage.Value >= 100; diff --git a/MediaBrowser.Controller/Entities/InternalItemsQuery.cs b/MediaBrowser.Controller/Entities/InternalItemsQuery.cs index 94baacf13..be6e95ddd 100644 --- a/MediaBrowser.Controller/Entities/InternalItemsQuery.cs +++ b/MediaBrowser.Controller/Entities/InternalItemsQuery.cs @@ -124,7 +124,6 @@ namespace MediaBrowser.Controller.Entities public int? MaxParentalRating { get; set; } public bool? HasDeadParentId { get; set; } - public bool? IsOffline { get; set; } public bool? IsVirtualItem { get; set; } public Guid? ParentId { get; set; } diff --git a/MediaBrowser.Controller/Entities/TV/Episode.cs b/MediaBrowser.Controller/Entities/TV/Episode.cs index 99abf5611..29a63f317 100644 --- a/MediaBrowser.Controller/Entities/TV/Episode.cs +++ b/MediaBrowser.Controller/Entities/TV/Episode.cs @@ -71,6 +71,12 @@ namespace MediaBrowser.Controller.Entities.TV { return IsStacked || MediaSourceCount > 1; } + } + + [IgnoreDataMember] + public override bool SupportsInheritedParentImages + { + get { return true; } } [IgnoreDataMember] diff --git a/MediaBrowser.Controller/Entities/TV/Season.cs b/MediaBrowser.Controller/Entities/TV/Season.cs index d8a9a16c1..2663a9dd5 100644 --- a/MediaBrowser.Controller/Entities/TV/Season.cs +++ b/MediaBrowser.Controller/Entities/TV/Season.cs @@ -39,6 +39,12 @@ namespace MediaBrowser.Controller.Entities.TV } } + [IgnoreDataMember] + public override bool SupportsInheritedParentImages + { + get { return true; } + } + [IgnoreDataMember] public override Guid? DisplayParentId { diff --git a/MediaBrowser.Controller/Entities/TV/Series.cs b/MediaBrowser.Controller/Entities/TV/Series.cs index a997d3476..69a3a67c8 100644 --- a/MediaBrowser.Controller/Entities/TV/Series.cs +++ b/MediaBrowser.Controller/Entities/TV/Series.cs @@ -135,7 +135,6 @@ namespace MediaBrowser.Controller.Entities.TV { AncestorWithPresentationUniqueKey = GetUniqueSeriesKey(this), IncludeItemTypes = new[] { typeof(Season).Name }, - SortBy = new[] { ItemSortBy.SortName }, IsVirtualItem = false, Limit = 0 }); diff --git a/MediaBrowser.Controller/Entities/UserViewBuilder.cs b/MediaBrowser.Controller/Entities/UserViewBuilder.cs index 3820de83d..d5781d21e 100644 --- a/MediaBrowser.Controller/Entities/UserViewBuilder.cs +++ b/MediaBrowser.Controller/Entities/UserViewBuilder.cs @@ -1812,7 +1812,7 @@ namespace MediaBrowser.Controller.Entities .Where(i => user.IsFolderGrouped(i.Id) && UserView.IsEligibleForGrouping(i)); } - private IEnumerable GetMediaFolders(User user, IEnumerable viewTypes) + private List GetMediaFolders(User user, IEnumerable viewTypes) { if (user == null) { @@ -1822,7 +1822,7 @@ namespace MediaBrowser.Controller.Entities var folder = i as ICollectionFolder; return folder != null && viewTypes.Contains(folder.CollectionType ?? string.Empty, StringComparer.OrdinalIgnoreCase); - }); + }).ToList(); } return GetMediaFolders(user) .Where(i => @@ -1830,17 +1830,17 @@ namespace MediaBrowser.Controller.Entities var folder = i as ICollectionFolder; return folder != null && viewTypes.Contains(folder.CollectionType ?? string.Empty, StringComparer.OrdinalIgnoreCase); - }); + }).ToList(); } - private IEnumerable GetMediaFolders(Folder parent, User user, IEnumerable viewTypes) + private List GetMediaFolders(Folder parent, User user, IEnumerable viewTypes) { if (parent == null || parent is UserView) { return GetMediaFolders(user, viewTypes); } - return new[] { parent }; + return new List { parent }; } private IEnumerable GetRecursiveChildren(Folder parent, User user, IEnumerable viewTypes) diff --git a/MediaBrowser.Controller/Library/ILibraryManager.cs b/MediaBrowser.Controller/Library/ILibraryManager.cs index acdd9a042..955230b8a 100644 --- a/MediaBrowser.Controller/Library/ILibraryManager.cs +++ b/MediaBrowser.Controller/Library/ILibraryManager.cs @@ -538,10 +538,7 @@ namespace MediaBrowser.Controller.Library /// /// Gets the items. /// - /// The query. - /// The parent ids. - /// List<BaseItem>. - IEnumerable GetItemList(InternalItemsQuery query, IEnumerable parentIds); + IEnumerable GetItemList(InternalItemsQuery query, List parents); /// /// Gets the items result. diff --git a/MediaBrowser.Controller/TV/ITVSeriesManager.cs b/MediaBrowser.Controller/TV/ITVSeriesManager.cs index 3d6e87474..771fa602a 100644 --- a/MediaBrowser.Controller/TV/ITVSeriesManager.cs +++ b/MediaBrowser.Controller/TV/ITVSeriesManager.cs @@ -19,6 +19,6 @@ namespace MediaBrowser.Controller.TV /// The request. /// The parents folders. /// QueryResult<BaseItem>. - QueryResult GetNextUp(NextUpQuery request, IEnumerable parentsFolders); + QueryResult GetNextUp(NextUpQuery request, List parentsFolders); } } diff --git a/MediaBrowser.Model/System/IEnvironmentInfo.cs b/MediaBrowser.Model/System/IEnvironmentInfo.cs index 7e7d81e30..f9795a568 100644 --- a/MediaBrowser.Model/System/IEnvironmentInfo.cs +++ b/MediaBrowser.Model/System/IEnvironmentInfo.cs @@ -14,6 +14,7 @@ namespace MediaBrowser.Model.System Architecture SystemArchitecture { get; } string GetEnvironmentVariable(string name); string GetUserId(); + string StackTrace { get; } } public enum OperatingSystem diff --git a/MediaBrowser.Providers/Manager/MetadataService.cs b/MediaBrowser.Providers/Manager/MetadataService.cs index 3046fc395..0491e800f 100644 --- a/MediaBrowser.Providers/Manager/MetadataService.cs +++ b/MediaBrowser.Providers/Manager/MetadataService.cs @@ -536,7 +536,7 @@ namespace MediaBrowser.Providers.Manager refreshResult.UpdateType = refreshResult.UpdateType | ItemUpdateType.MetadataImport; // Only one local provider allowed per item - if (item.IsLocked || IsFullLocalMetadata(localItem.Item)) + if (item.IsLocked || localItem.Item.IsLocked || IsFullLocalMetadata(localItem.Item)) { hasLocalMetadata = true; } @@ -573,14 +573,16 @@ namespace MediaBrowser.Providers.Manager { if (refreshResult.UpdateType > ItemUpdateType.None) { - // If no local metadata, take data from item itself - if (!hasLocalMetadata) + if (hasLocalMetadata) + { + MergeData(temp, metadata, item.LockedFields, true, true); + } + else { // TODO: If the new metadata from above has some blank data, this can cause old data to get filled into those empty fields - MergeData(metadata, temp, new List(), false, true); + MergeData(metadata, temp, new List(), false, false); + MergeData(temp, metadata, item.LockedFields, true, false); } - - MergeData(temp, metadata, item.LockedFields, true, true); } } -- cgit v1.2.3 From a9645e14298dfeb799b86bf9e3cde097af02cfd3 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 25 Nov 2016 01:58:38 -0500 Subject: update components --- Emby.Server.Implementations/Activity/ActivityRepository.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'Emby.Server.Implementations/Activity/ActivityRepository.cs') diff --git a/Emby.Server.Implementations/Activity/ActivityRepository.cs b/Emby.Server.Implementations/Activity/ActivityRepository.cs index eef9792de..ba81992ac 100644 --- a/Emby.Server.Implementations/Activity/ActivityRepository.cs +++ b/Emby.Server.Implementations/Activity/ActivityRepository.cs @@ -58,9 +58,9 @@ namespace Emby.Server.Implementations.Activity throw new ArgumentNullException("entry"); } - using (var connection = CreateConnection()) + using (WriteLock.Write()) { - using (WriteLock.Write()) + using (var connection = CreateConnection()) { connection.RunInTransaction(db => { @@ -86,9 +86,9 @@ namespace Emby.Server.Implementations.Activity public QueryResult GetActivityLogEntries(DateTime? minDate, int? startIndex, int? limit) { - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { var commandText = BaseActivitySelectText; var whereClauses = new List(); -- cgit v1.2.3 From 1c52e4f51b1a9732264203fb0d987cf9752ef45b Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Mon, 28 Nov 2016 14:26:48 -0500 Subject: update transaction modes --- .../Activity/ActivityRepository.cs | 2 +- .../Data/BaseSqliteRepository.cs | 5 +++++ .../Data/SqliteDisplayPreferencesRepository.cs | 4 ++-- .../Data/SqliteFileOrganizationRepository.cs | 6 +++--- .../Data/SqliteItemRepository.cs | 8 ++++---- .../Data/SqliteUserDataRepository.cs | 10 +++++----- .../Data/SqliteUserRepository.cs | 4 ++-- .../Notifications/SqliteNotificationsRepository.cs | 6 +++--- .../Security/AuthenticationRepository.cs | 18 ++++++++++-------- .../Social/SharingRepository.cs | 2 +- Emby.Server.Implementations/Sync/SyncRepository.cs | 8 ++++---- 11 files changed, 40 insertions(+), 33 deletions(-) (limited to 'Emby.Server.Implementations/Activity/ActivityRepository.cs') diff --git a/Emby.Server.Implementations/Activity/ActivityRepository.cs b/Emby.Server.Implementations/Activity/ActivityRepository.cs index ba81992ac..f7419de80 100644 --- a/Emby.Server.Implementations/Activity/ActivityRepository.cs +++ b/Emby.Server.Implementations/Activity/ActivityRepository.cs @@ -79,7 +79,7 @@ namespace Emby.Server.Implementations.Activity statement.MoveNext(); } - }); + }, TransactionMode); } } } diff --git a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs index 308b8356f..af8de0e84 100644 --- a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs +++ b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs @@ -30,6 +30,11 @@ namespace Emby.Server.Implementations.Data get { return false; } } + protected TransactionMode TransactionMode + { + get { return TransactionMode.Immediate; } + } + static BaseSqliteRepository() { SQLite3.EnableSharedCache = false; diff --git a/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs b/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs index 1bd64b21d..743186db2 100644 --- a/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs @@ -100,7 +100,7 @@ namespace Emby.Server.Implementations.Data connection.RunInTransaction(db => { SaveDisplayPreferences(displayPreferences, userId, client, db); - }); + }, TransactionMode); } } } @@ -147,7 +147,7 @@ namespace Emby.Server.Implementations.Data { SaveDisplayPreferences(displayPreference, userId, displayPreference.Client, db); } - }); + }, TransactionMode); } } } diff --git a/Emby.Server.Implementations/Data/SqliteFileOrganizationRepository.cs b/Emby.Server.Implementations/Data/SqliteFileOrganizationRepository.cs index 23bab883e..448d38c8c 100644 --- a/Emby.Server.Implementations/Data/SqliteFileOrganizationRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteFileOrganizationRepository.cs @@ -85,7 +85,7 @@ namespace Emby.Server.Implementations.Data statement.MoveNext(); } - }); + }, TransactionMode); } } } @@ -108,7 +108,7 @@ namespace Emby.Server.Implementations.Data statement.TryBind("@ResultId", id.ToGuidParamValue()); statement.MoveNext(); } - }); + }, TransactionMode); } } } @@ -124,7 +124,7 @@ namespace Emby.Server.Implementations.Data var commandText = "delete from FileOrganizerResults"; db.Execute(commandText); - }); + }, TransactionMode); } } } diff --git a/Emby.Server.Implementations/Data/SqliteItemRepository.cs b/Emby.Server.Implementations/Data/SqliteItemRepository.cs index 29aacc059..54820ab53 100644 --- a/Emby.Server.Implementations/Data/SqliteItemRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteItemRepository.cs @@ -316,7 +316,7 @@ namespace Emby.Server.Implementations.Data AddColumn(db, "MediaStreams", "RefFrames", "INT", existingColumnNames); AddColumn(db, "MediaStreams", "KeyFrames", "TEXT", existingColumnNames); AddColumn(db, "MediaStreams", "IsAnamorphic", "BIT", existingColumnNames); - }); + }, TransactionMode); string[] postQueries = @@ -697,7 +697,7 @@ namespace Emby.Server.Implementations.Data connection.RunInTransaction(db => { SaveItemsInTranscation(db, tuples); - }); + }, TransactionMode); } } } @@ -2211,7 +2211,7 @@ namespace Emby.Server.Implementations.Data index++; } } - }); + }, TransactionMode); } } } @@ -4531,7 +4531,7 @@ namespace Emby.Server.Implementations.Data // Delete the item ExecuteWithSingleParam(db, "delete from TypedBaseItems where guid=@Id", id.ToGuidParamValue()); - }); + }, TransactionMode); } } } diff --git a/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs b/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs index b01f215e0..f7184540a 100644 --- a/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs @@ -78,7 +78,7 @@ namespace Emby.Server.Implementations.Data AddColumn(db, "userdata", "AudioStreamIndex", "int", existingColumnNames); AddColumn(db, "userdata", "SubtitleStreamIndex", "int", existingColumnNames); - }); + }, TransactionMode); ImportUserDataIfNeeded(connection); } @@ -116,7 +116,7 @@ namespace Emby.Server.Implementations.Data statement.TryBind("@IsUserDataImported", true); statement.MoveNext(); } - }); + }, TransactionMode); } private void ImportUserData(IDatabaseConnection connection, string file) @@ -128,7 +128,7 @@ namespace Emby.Server.Implementations.Data connection.RunInTransaction(db => { db.Execute("REPLACE INTO userdata(" + columns + ") SELECT " + columns + " FROM UserDataBackup.userdata;"); - }); + }, TransactionMode); } /// @@ -197,7 +197,7 @@ namespace Emby.Server.Implementations.Data connection.RunInTransaction(db => { SaveUserData(db, userId, key, userData); - }); + }, TransactionMode); } } } @@ -271,7 +271,7 @@ namespace Emby.Server.Implementations.Data { SaveUserData(db, userId, userItemData.Key, userItemData); } - }); + }, TransactionMode); } } } diff --git a/Emby.Server.Implementations/Data/SqliteUserRepository.cs b/Emby.Server.Implementations/Data/SqliteUserRepository.cs index f902d981f..5fb27d081 100644 --- a/Emby.Server.Implementations/Data/SqliteUserRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteUserRepository.cs @@ -102,7 +102,7 @@ namespace Emby.Server.Implementations.Data statement.TryBind("@data", serialized); statement.MoveNext(); } - }); + }, TransactionMode); } } } @@ -164,7 +164,7 @@ namespace Emby.Server.Implementations.Data statement.TryBind("@id", user.Id.ToGuidParamValue()); statement.MoveNext(); } - }); + }, TransactionMode); } } } diff --git a/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs b/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs index 66ef5d5d1..5f0d4d29a 100644 --- a/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs +++ b/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs @@ -249,7 +249,7 @@ namespace Emby.Server.Implementations.Notifications statement.MoveNext(); } - }); + }, TransactionMode); } } } @@ -304,7 +304,7 @@ namespace Emby.Server.Implementations.Notifications statement.MoveNext(); } - }); + }, TransactionMode); } } } @@ -334,7 +334,7 @@ namespace Emby.Server.Implementations.Notifications } } - }); + }, TransactionMode); } } } diff --git a/Emby.Server.Implementations/Security/AuthenticationRepository.cs b/Emby.Server.Implementations/Security/AuthenticationRepository.cs index 2632b9666..e498253c3 100644 --- a/Emby.Server.Implementations/Security/AuthenticationRepository.cs +++ b/Emby.Server.Implementations/Security/AuthenticationRepository.cs @@ -50,7 +50,8 @@ namespace Emby.Server.Implementations.Security var existingColumnNames = GetColumnNames(db, "AccessTokens"); AddColumn(db, "AccessTokens", "AppVersion", "TEXT", existingColumnNames); - }); + + }, TransactionMode); } } @@ -70,9 +71,9 @@ namespace Emby.Server.Implementations.Security cancellationToken.ThrowIfCancellationRequested(); - using (WriteLock.Write()) + using (var connection = CreateConnection()) { - using (var connection = CreateConnection()) + using (WriteLock.Write()) { connection.RunInTransaction(db => { @@ -100,7 +101,8 @@ namespace Emby.Server.Implementations.Security statement.MoveNext(); } - }); + + }, TransactionMode); } } } @@ -137,9 +139,9 @@ namespace Emby.Server.Implementations.Security throw new ArgumentNullException("query"); } - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { var commandText = BaseSelectText; @@ -244,9 +246,9 @@ namespace Emby.Server.Implementations.Security throw new ArgumentNullException("id"); } - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { var commandText = BaseSelectText + " where Id=@Id"; diff --git a/Emby.Server.Implementations/Social/SharingRepository.cs b/Emby.Server.Implementations/Social/SharingRepository.cs index 6dab54bc6..71f80a575 100644 --- a/Emby.Server.Implementations/Social/SharingRepository.cs +++ b/Emby.Server.Implementations/Social/SharingRepository.cs @@ -70,7 +70,7 @@ namespace Emby.Server.Implementations.Social info.ItemId, info.UserId, info.ExpirationDate.ToDateTimeParamValue()); - }); + }, TransactionMode); } } } diff --git a/Emby.Server.Implementations/Sync/SyncRepository.cs b/Emby.Server.Implementations/Sync/SyncRepository.cs index b2d9fbcc9..caf98e666 100644 --- a/Emby.Server.Implementations/Sync/SyncRepository.cs +++ b/Emby.Server.Implementations/Sync/SyncRepository.cs @@ -79,7 +79,7 @@ namespace Emby.Server.Implementations.Sync existingColumnNames = GetColumnNames(db, "SyncJobItems"); AddColumn(db, "SyncJobItems", "ItemDateModifiedTicks", "BIGINT", existingColumnNames); - }); + }, TransactionMode); } } @@ -268,7 +268,7 @@ namespace Emby.Server.Implementations.Sync connection.RunInTransaction(conn => { conn.Execute(commandText, paramList.ToArray()); - }); + }, TransactionMode); } } } @@ -290,7 +290,7 @@ namespace Emby.Server.Implementations.Sync { conn.Execute("delete from SyncJobs where Id=?", id.ToGuidParamValue()); conn.Execute("delete from SyncJobItems where JobId=?", id); - }); + }, TransactionMode); } } } @@ -743,7 +743,7 @@ namespace Emby.Server.Implementations.Sync connection.RunInTransaction(conn => { conn.Execute(commandText, paramList.ToArray()); - }); + }, TransactionMode); } } } -- cgit v1.2.3 From 61e195c096dfc2c5713bbbcbc0be73ddff99cb55 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 29 Nov 2016 14:12:37 -0500 Subject: update repositories --- .../Activity/ActivityRepository.cs | 20 ++-- .../Data/BaseSqliteRepository.cs | 42 +++++--- .../Data/SqliteDisplayPreferencesRepository.cs | 7 +- .../Data/SqliteFileOrganizationRepository.cs | 7 +- .../Data/SqliteItemRepository.cs | 11 +- .../Data/SqliteUserDataRepository.cs | 2 - .../Data/SqliteUserRepository.cs | 7 +- .../Notifications/SqliteNotificationsRepository.cs | 11 +- .../Security/AuthenticationRepository.cs | 119 ++++++++++----------- .../Social/SharingRepository.cs | 7 +- Emby.Server.Implementations/Sync/SyncRepository.cs | 7 +- 11 files changed, 101 insertions(+), 139 deletions(-) (limited to 'Emby.Server.Implementations/Activity/ActivityRepository.cs') diff --git a/Emby.Server.Implementations/Activity/ActivityRepository.cs b/Emby.Server.Implementations/Activity/ActivityRepository.cs index f7419de80..9513c224f 100644 --- a/Emby.Server.Implementations/Activity/ActivityRepository.cs +++ b/Emby.Server.Implementations/Activity/ActivityRepository.cs @@ -27,17 +27,11 @@ namespace Emby.Server.Implementations.Activity { using (var connection = CreateConnection()) { - connection.ExecuteAll(string.Join(";", new[] - { - "PRAGMA page_size=4096", - "pragma default_temp_store = memory", - "pragma temp_store = memory" - })); + RunDefaultInitialization(connection); string[] queries = { - - "create table if not exists ActivityLogEntries (Id GUID PRIMARY KEY, Name TEXT, Overview TEXT, ShortOverview TEXT, Type TEXT, ItemId TEXT, UserId TEXT, DateCreated DATETIME, LogSeverity TEXT)", - "create index if not exists idx_ActivityLogEntries on ActivityLogEntries(Id)" + "create table if not exists ActivityLogEntries (Id GUID PRIMARY KEY, Name TEXT, Overview TEXT, ShortOverview TEXT, Type TEXT, ItemId TEXT, UserId TEXT, DateCreated DATETIME, LogSeverity TEXT)", + "create index if not exists idx_ActivityLogEntries on ActivityLogEntries(Id)" }; connection.RunQueries(queries); @@ -58,9 +52,9 @@ namespace Emby.Server.Implementations.Activity throw new ArgumentNullException("entry"); } - using (WriteLock.Write()) + using (var connection = CreateConnection()) { - using (var connection = CreateConnection()) + using (WriteLock.Write()) { connection.RunInTransaction(db => { @@ -86,9 +80,9 @@ namespace Emby.Server.Implementations.Activity public QueryResult GetActivityLogEntries(DateTime? minDate, int? startIndex, int? limit) { - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { var commandText = BaseActivitySelectText; var whereClauses = new List(); diff --git a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs index af8de0e84..e066d02d3 100644 --- a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs +++ b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs @@ -67,20 +67,8 @@ namespace Emby.Server.Implementations.Data //Logger.Info("Opening write connection"); } - isReadOnly = false; - - if (isReadOnly) - { - connectionFlags = ConnectionFlags.ReadOnly; - //connectionFlags = ConnectionFlags.Create; - //connectionFlags |= ConnectionFlags.ReadWrite; - } - else - { - connectionFlags = ConnectionFlags.Create; - connectionFlags |= ConnectionFlags.ReadWrite; - } - + connectionFlags = ConnectionFlags.Create; + connectionFlags |= ConnectionFlags.ReadWrite; connectionFlags |= ConnectionFlags.SharedCached; connectionFlags |= ConnectionFlags.NoMutex; @@ -89,6 +77,8 @@ namespace Emby.Server.Implementations.Data if (string.IsNullOrWhiteSpace(_defaultWal)) { _defaultWal = db.Query("PRAGMA journal_mode").SelectScalarString().First(); + + Logger.Info("Default journal_mode for {0} is {1}", DbFilePath, _defaultWal); } var queries = new List @@ -115,7 +105,7 @@ namespace Emby.Server.Implementations.Data //Logger.Info("synchronous: " + db.Query("PRAGMA synchronous").SelectScalarString().First()); //Logger.Info("temp_store: " + db.Query("PRAGMA temp_store").SelectScalarString().First()); - if (!string.Equals(_defaultWal, "wal", StringComparison.OrdinalIgnoreCase)) + /*if (!string.Equals(_defaultWal, "wal", StringComparison.OrdinalIgnoreCase)) { queries.Add("PRAGMA journal_mode=WAL"); @@ -124,7 +114,7 @@ namespace Emby.Server.Implementations.Data db.ExecuteAll(string.Join(";", queries.ToArray())); } } - else if (queries.Count > 0) + else*/ if (queries.Count > 0) { db.ExecuteAll(string.Join(";", queries.ToArray())); } @@ -132,6 +122,26 @@ namespace Emby.Server.Implementations.Data return db; } + protected void RunDefaultInitialization(IDatabaseConnection db) + { + var queries = new List + { + "PRAGMA journal_mode=WAL", + "PRAGMA page_size=4096", + }; + + if (EnableTempStoreMemory) + { + queries.AddRange(new List + { + "pragma default_temp_store = memory", + "pragma temp_store = memory" + }); + } + + db.ExecuteAll(string.Join(";", queries.ToArray())); + } + protected virtual bool EnableTempStoreMemory { get diff --git a/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs b/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs index 743186db2..0197efb51 100644 --- a/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs @@ -54,12 +54,7 @@ namespace Emby.Server.Implementations.Data { using (var connection = CreateConnection()) { - connection.ExecuteAll(string.Join(";", new[] - { - "PRAGMA page_size=4096", - "pragma default_temp_store = memory", - "pragma temp_store = memory" - })); + RunDefaultInitialization(connection); string[] queries = { diff --git a/Emby.Server.Implementations/Data/SqliteFileOrganizationRepository.cs b/Emby.Server.Implementations/Data/SqliteFileOrganizationRepository.cs index 448d38c8c..603437120 100644 --- a/Emby.Server.Implementations/Data/SqliteFileOrganizationRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteFileOrganizationRepository.cs @@ -31,12 +31,7 @@ namespace Emby.Server.Implementations.Data { using (var connection = CreateConnection()) { - connection.ExecuteAll(string.Join(";", new[] - { - "PRAGMA page_size=4096", - "pragma default_temp_store = memory", - "pragma temp_store = memory" - })); + RunDefaultInitialization(connection); string[] queries = { diff --git a/Emby.Server.Implementations/Data/SqliteItemRepository.cs b/Emby.Server.Implementations/Data/SqliteItemRepository.cs index 54820ab53..9c096916f 100644 --- a/Emby.Server.Implementations/Data/SqliteItemRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteItemRepository.cs @@ -157,12 +157,7 @@ namespace Emby.Server.Implementations.Data { using (var connection = CreateConnection()) { - connection.ExecuteAll(string.Join(";", new[] - { - "PRAGMA page_size=4096", - "PRAGMA default_temp_store=memory", - "PRAGMA temp_store=memory" - })); + RunDefaultInitialization(connection); var createMediaStreamsTableCommand = "create table if not exists mediastreams (ItemId GUID, StreamIndex INT, StreamType TEXT, Codec TEXT, Language TEXT, ChannelLayout TEXT, Profile TEXT, AspectRatio TEXT, Path TEXT, IsInterlaced BIT, BitRate INT NULL, Channels INT NULL, SampleRate INT NULL, IsDefault BIT, IsForced BIT, IsExternal BIT, Height INT NULL, Width INT NULL, AverageFrameRate FLOAT NULL, RealFrameRate FLOAT NULL, Level FLOAT NULL, PixelFormat TEXT, BitDepth INT NULL, IsAnamorphic BIT NULL, RefFrames INT NULL, CodecTag TEXT NULL, Comment TEXT NULL, NalLengthSize TEXT NULL, IsAvc BIT NULL, Title TEXT NULL, TimeBase TEXT NULL, CodecTimeBase TEXT NULL, PRIMARY KEY (ItemId, StreamIndex))"; @@ -396,9 +391,9 @@ namespace Emby.Server.Implementations.Data { try { - using (WriteLock.Write()) + using (var connection = CreateConnection()) { - using (var connection = CreateConnection()) + using (WriteLock.Write()) { connection.RunQueries(new string[] { diff --git a/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs b/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs index f7184540a..b65e5d1b3 100644 --- a/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs @@ -51,8 +51,6 @@ namespace Emby.Server.Implementations.Data { string[] queries = { - "pragma temp_store = memory", - "create table if not exists userdata (key nvarchar, userId GUID, rating float null, played bit, playCount int, isFavorite bit, playbackPositionTicks bigint, lastPlayedDate datetime null)", "create table if not exists DataSettings (IsUserDataImported bit)", diff --git a/Emby.Server.Implementations/Data/SqliteUserRepository.cs b/Emby.Server.Implementations/Data/SqliteUserRepository.cs index 5fb27d081..99d7563c7 100644 --- a/Emby.Server.Implementations/Data/SqliteUserRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteUserRepository.cs @@ -50,12 +50,7 @@ namespace Emby.Server.Implementations.Data { using (var connection = CreateConnection()) { - connection.ExecuteAll(string.Join(";", new[] - { - "PRAGMA page_size=4096", - "pragma default_temp_store = memory", - "pragma temp_store = memory" - })); + RunDefaultInitialization(connection); string[] queries = { diff --git a/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs b/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs index 5f0d4d29a..767ba5504 100644 --- a/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs +++ b/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs @@ -29,12 +29,7 @@ namespace Emby.Server.Implementations.Notifications { using (var connection = CreateConnection()) { - connection.ExecuteAll(string.Join(";", new[] - { - "PRAGMA page_size=4096", - "pragma default_temp_store = memory", - "pragma temp_store = memory" - })); + RunDefaultInitialization(connection); string[] queries = { @@ -58,7 +53,7 @@ namespace Emby.Server.Implementations.Notifications using (var connection = CreateConnection(true)) { - //using (WriteLock.Read()) + using (WriteLock.Read()) { var clauses = new List(); var paramList = new List(); @@ -113,7 +108,7 @@ namespace Emby.Server.Implementations.Notifications using (var connection = CreateConnection(true)) { - //using (WriteLock.Read()) + using (WriteLock.Read()) { using (var statement = connection.PrepareStatement("select Level from Notifications where UserId=@UserId and IsRead=@IsRead")) { diff --git a/Emby.Server.Implementations/Security/AuthenticationRepository.cs b/Emby.Server.Implementations/Security/AuthenticationRepository.cs index e498253c3..dbda4a460 100644 --- a/Emby.Server.Implementations/Security/AuthenticationRepository.cs +++ b/Emby.Server.Implementations/Security/AuthenticationRepository.cs @@ -30,12 +30,7 @@ namespace Emby.Server.Implementations.Security { using (var connection = CreateConnection()) { - connection.ExecuteAll(string.Join(";", new[] - { - "PRAGMA page_size=4096", - "pragma default_temp_store = memory", - "pragma temp_store = memory" - })); + RunDefaultInitialization(connection); string[] queries = { @@ -139,78 +134,78 @@ namespace Emby.Server.Implementations.Security throw new ArgumentNullException("query"); } - using (var connection = CreateConnection(true)) - { - using (WriteLock.Read()) - { - var commandText = BaseSelectText; + var commandText = BaseSelectText; - var whereClauses = new List(); + var whereClauses = new List(); - var startIndex = query.StartIndex ?? 0; + var startIndex = query.StartIndex ?? 0; - if (!string.IsNullOrWhiteSpace(query.AccessToken)) - { - whereClauses.Add("AccessToken=@AccessToken"); - } + if (!string.IsNullOrWhiteSpace(query.AccessToken)) + { + whereClauses.Add("AccessToken=@AccessToken"); + } - if (!string.IsNullOrWhiteSpace(query.UserId)) - { - whereClauses.Add("UserId=@UserId"); - } + if (!string.IsNullOrWhiteSpace(query.UserId)) + { + whereClauses.Add("UserId=@UserId"); + } - if (!string.IsNullOrWhiteSpace(query.DeviceId)) - { - whereClauses.Add("DeviceId=@DeviceId"); - } + if (!string.IsNullOrWhiteSpace(query.DeviceId)) + { + whereClauses.Add("DeviceId=@DeviceId"); + } - if (query.IsActive.HasValue) - { - whereClauses.Add("IsActive=@IsActive"); - } + if (query.IsActive.HasValue) + { + whereClauses.Add("IsActive=@IsActive"); + } - if (query.HasUser.HasValue) - { - if (query.HasUser.Value) - { - whereClauses.Add("UserId not null"); - } - else - { - whereClauses.Add("UserId is null"); - } - } + if (query.HasUser.HasValue) + { + if (query.HasUser.Value) + { + whereClauses.Add("UserId not null"); + } + else + { + whereClauses.Add("UserId is null"); + } + } - var whereTextWithoutPaging = whereClauses.Count == 0 ? - string.Empty : - " where " + string.Join(" AND ", whereClauses.ToArray()); + var whereTextWithoutPaging = whereClauses.Count == 0 ? + string.Empty : + " where " + string.Join(" AND ", whereClauses.ToArray()); - if (startIndex > 0) - { - var pagingWhereText = whereClauses.Count == 0 ? - string.Empty : - " where " + string.Join(" AND ", whereClauses.ToArray()); + if (startIndex > 0) + { + var pagingWhereText = whereClauses.Count == 0 ? + string.Empty : + " where " + string.Join(" AND ", whereClauses.ToArray()); - whereClauses.Add(string.Format("Id NOT IN (SELECT Id FROM AccessTokens {0} ORDER BY DateCreated LIMIT {1})", - pagingWhereText, - startIndex.ToString(_usCulture))); - } + whereClauses.Add(string.Format("Id NOT IN (SELECT Id FROM AccessTokens {0} ORDER BY DateCreated LIMIT {1})", + pagingWhereText, + startIndex.ToString(_usCulture))); + } - var whereText = whereClauses.Count == 0 ? - string.Empty : - " where " + string.Join(" AND ", whereClauses.ToArray()); + var whereText = whereClauses.Count == 0 ? + string.Empty : + " where " + string.Join(" AND ", whereClauses.ToArray()); - commandText += whereText; + commandText += whereText; - commandText += " ORDER BY DateCreated"; + commandText += " ORDER BY DateCreated"; - if (query.Limit.HasValue) - { - commandText += " LIMIT " + query.Limit.Value.ToString(_usCulture); - } + if (query.Limit.HasValue) + { + commandText += " LIMIT " + query.Limit.Value.ToString(_usCulture); + } - var list = new List(); + var list = new List(); + using (var connection = CreateConnection(true)) + { + using (WriteLock.Read()) + { using (var statement = connection.PrepareStatement(commandText)) { BindAuthenticationQueryParams(query, statement); diff --git a/Emby.Server.Implementations/Social/SharingRepository.cs b/Emby.Server.Implementations/Social/SharingRepository.cs index 71f80a575..12d846e81 100644 --- a/Emby.Server.Implementations/Social/SharingRepository.cs +++ b/Emby.Server.Implementations/Social/SharingRepository.cs @@ -27,12 +27,7 @@ namespace Emby.Server.Implementations.Social { using (var connection = CreateConnection()) { - connection.ExecuteAll(string.Join(";", new[] - { - "PRAGMA page_size=4096", - "pragma default_temp_store = memory", - "pragma temp_store = memory" - })); + RunDefaultInitialization(connection); string[] queries = { diff --git a/Emby.Server.Implementations/Sync/SyncRepository.cs b/Emby.Server.Implementations/Sync/SyncRepository.cs index caf98e666..037507872 100644 --- a/Emby.Server.Implementations/Sync/SyncRepository.cs +++ b/Emby.Server.Implementations/Sync/SyncRepository.cs @@ -43,12 +43,7 @@ namespace Emby.Server.Implementations.Sync { using (var connection = CreateConnection()) { - connection.ExecuteAll(string.Join(";", new[] - { - "PRAGMA page_size=4096", - "pragma default_temp_store = memory", - "pragma temp_store = memory" - })); + RunDefaultInitialization(connection); string[] queries = { -- cgit v1.2.3 From a9a808a9c407a14af9e041cff20e7fe6af3e5061 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sun, 11 Dec 2016 00:12:00 -0500 Subject: fix db locking errors --- .../Activity/ActivityRepository.cs | 8 +- .../Data/BaseSqliteRepository.cs | 114 ++++++++++-- .../Data/SqliteDisplayPreferencesRepository.cs | 16 +- .../Data/SqliteFileOrganizationRepository.cs | 20 +-- .../Data/SqliteItemRepository.cs | 197 +++++++++++---------- .../Data/SqliteUserDataRepository.cs | 16 +- .../Data/SqliteUserRepository.cs | 12 +- .../Notifications/SqliteNotificationsRepository.cs | 36 ++-- .../Security/AuthenticationRepository.cs | 12 +- .../Social/SharingRepository.cs | 8 +- Emby.Server.Implementations/Sync/SyncRepository.cs | 74 ++++---- MediaBrowser.Controller/Entities/TV/Season.cs | 6 +- 12 files changed, 306 insertions(+), 213 deletions(-) (limited to 'Emby.Server.Implementations/Activity/ActivityRepository.cs') diff --git a/Emby.Server.Implementations/Activity/ActivityRepository.cs b/Emby.Server.Implementations/Activity/ActivityRepository.cs index 9513c224f..17aef7268 100644 --- a/Emby.Server.Implementations/Activity/ActivityRepository.cs +++ b/Emby.Server.Implementations/Activity/ActivityRepository.cs @@ -52,9 +52,9 @@ namespace Emby.Server.Implementations.Activity throw new ArgumentNullException("entry"); } - using (var connection = CreateConnection()) + using (WriteLock.Write()) { - using (WriteLock.Write()) + using (var connection = CreateConnection()) { connection.RunInTransaction(db => { @@ -80,9 +80,9 @@ namespace Emby.Server.Implementations.Activity public QueryResult GetActivityLogEntries(DateTime? minDate, int? startIndex, int? limit) { - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { var commandText = BaseActivitySelectText; var whereClauses = new List(); diff --git a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs index f132c9765..bef2ce149 100644 --- a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs +++ b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs @@ -20,27 +20,34 @@ namespace Emby.Server.Implementations.Data { Logger = logger; - WriteLock = AllowLockRecursion ? - new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion) : - new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion); + WriteLock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion); } - protected virtual bool AllowLockRecursion + protected TransactionMode TransactionMode { - get { return false; } + get { return TransactionMode.Immediate; } } - protected TransactionMode TransactionMode + protected TransactionMode ReadTransactionMode { - get { return TransactionMode.Immediate; } + get { return TransactionMode.Deferred; } } + internal static int ThreadSafeMode { get; set; } + static BaseSqliteRepository() { SQLite3.EnableSharedCache = false; int rc = raw.sqlite3_config(raw.SQLITE_CONFIG_MEMSTATUS, 0); //CheckOk(rc); + + rc = raw.sqlite3_config(raw.SQLITE_CONFIG_MULTITHREAD, 1); + //CheckOk(rc); + + rc = raw.sqlite3_enable_shared_cache(1); + + ThreadSafeMode = raw.sqlite3_threadsafe(); } private static bool _versionLogged; @@ -61,16 +68,19 @@ namespace Emby.Server.Implementations.Data if (isReadOnly) { //Logger.Info("Opening read connection"); + //connectionFlags = ConnectionFlags.ReadOnly; + connectionFlags = ConnectionFlags.Create; + connectionFlags |= ConnectionFlags.ReadWrite; } else { //Logger.Info("Opening write connection"); + connectionFlags = ConnectionFlags.Create; + connectionFlags |= ConnectionFlags.ReadWrite; } - connectionFlags = ConnectionFlags.Create; - connectionFlags |= ConnectionFlags.ReadWrite; - connectionFlags |= ConnectionFlags.SharedCached; - //connectionFlags |= ConnectionFlags.NoMutex; + //connectionFlags |= ConnectionFlags.SharedCached; + connectionFlags |= ConnectionFlags.NoMutex; var db = SQLite3.Open(DbFilePath, connectionFlags, null); @@ -114,7 +124,8 @@ namespace Emby.Server.Implementations.Data db.ExecuteAll(string.Join(";", queries.ToArray())); } } - else*/ if (queries.Count > 0) + else*/ + if (queries.Count > 0) { db.ExecuteAll(string.Join(";", queries.ToArray())); } @@ -122,6 +133,26 @@ namespace Emby.Server.Implementations.Data return db; } + public IStatement PrepareStatement(IDatabaseConnection connection, string sql) + { + return connection.PrepareStatement(sql); + } + + public IStatement PrepareStatementSafe(IDatabaseConnection connection, string sql) + { + return connection.PrepareStatement(sql); + } + + public List PrepareAll(IDatabaseConnection connection, string sql) + { + return connection.PrepareAll(sql).ToList(); + } + + public List PrepareAllSafe(IDatabaseConnection connection, string sql) + { + return connection.PrepareAll(sql).ToList(); + } + protected void RunDefaultInitialization(IDatabaseConnection db) { var queries = new List @@ -288,12 +319,69 @@ namespace Emby.Server.Implementations.Data } } + public class DummyToken : IDisposable + { + public void Dispose() + { + } + } + public static IDisposable Read(this ReaderWriterLockSlim obj) { - return new ReadLockToken(obj); + //if (BaseSqliteRepository.ThreadSafeMode > 0) + //{ + // return new DummyToken(); + //} + return new WriteLockToken(obj); } public static IDisposable Write(this ReaderWriterLockSlim obj) { + //if (BaseSqliteRepository.ThreadSafeMode > 0) + //{ + // return new DummyToken(); + //} + return new WriteLockToken(obj); + } + } + + public static class SemaphpreSlimExtensions + { + private sealed class WriteLockToken : IDisposable + { + private SemaphoreSlim _sync; + public WriteLockToken(SemaphoreSlim sync) + { + _sync = sync; + var task = sync.WaitAsync(); + Task.WaitAll(task); + } + public void Dispose() + { + if (_sync != null) + { + _sync.Release(); + _sync = null; + } + } + } + + public class DummyToken : IDisposable + { + public void Dispose() + { + } + } + + public static IDisposable Read(this SemaphoreSlim obj) + { + return Write(obj); + } + public static IDisposable Write(this SemaphoreSlim obj) + { + //if (BaseSqliteRepository.ThreadSafeMode > 0) + //{ + // return new DummyToken(); + //} return new WriteLockToken(obj); } } diff --git a/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs b/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs index 0197efb51..f3d84315e 100644 --- a/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteDisplayPreferencesRepository.cs @@ -88,9 +88,9 @@ namespace Emby.Server.Implementations.Data cancellationToken.ThrowIfCancellationRequested(); - using (var connection = CreateConnection()) + using (WriteLock.Write()) { - using (WriteLock.Write()) + using (var connection = CreateConnection()) { connection.RunInTransaction(db => { @@ -132,9 +132,9 @@ namespace Emby.Server.Implementations.Data cancellationToken.ThrowIfCancellationRequested(); - using (var connection = CreateConnection()) + using (WriteLock.Write()) { - using (WriteLock.Write()) + using (var connection = CreateConnection()) { connection.RunInTransaction(db => { @@ -164,9 +164,9 @@ namespace Emby.Server.Implementations.Data var guidId = displayPreferencesId.GetMD5(); - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { using (var statement = connection.PrepareStatement("select data from userdisplaypreferences where id = @id and userId=@userId and client=@client")) { @@ -198,9 +198,9 @@ namespace Emby.Server.Implementations.Data { var list = new List(); - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { using (var statement = connection.PrepareStatement("select data from userdisplaypreferences where userId=@userId")) { diff --git a/Emby.Server.Implementations/Data/SqliteFileOrganizationRepository.cs b/Emby.Server.Implementations/Data/SqliteFileOrganizationRepository.cs index 603437120..9fbe8669d 100644 --- a/Emby.Server.Implementations/Data/SqliteFileOrganizationRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteFileOrganizationRepository.cs @@ -52,9 +52,9 @@ namespace Emby.Server.Implementations.Data cancellationToken.ThrowIfCancellationRequested(); - using (var connection = CreateConnection()) + using (WriteLock.Write()) { - using (WriteLock.Write()) + using (var connection = CreateConnection()) { connection.RunInTransaction(db => { @@ -92,9 +92,9 @@ namespace Emby.Server.Implementations.Data throw new ArgumentNullException("id"); } - using (var connection = CreateConnection()) + using (WriteLock.Write()) { - using (WriteLock.Write()) + using (var connection = CreateConnection()) { connection.RunInTransaction(db => { @@ -110,9 +110,9 @@ namespace Emby.Server.Implementations.Data public async Task DeleteAll() { - using (var connection = CreateConnection()) + using (WriteLock.Write()) { - using (WriteLock.Write()) + using (var connection = CreateConnection()) { connection.RunInTransaction(db => { @@ -131,9 +131,9 @@ namespace Emby.Server.Implementations.Data throw new ArgumentNullException("query"); } - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { var commandText = "SELECT ResultId, OriginalPath, TargetPath, FileLength, OrganizationDate, Status, OrganizationType, StatusMessage, ExtractedName, ExtractedYear, ExtractedSeasonNumber, ExtractedEpisodeNumber, ExtractedEndingEpisodeNumber, DuplicatePaths from FileOrganizerResults"; @@ -182,9 +182,9 @@ namespace Emby.Server.Implementations.Data throw new ArgumentNullException("id"); } - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { using (var statement = connection.PrepareStatement("select ResultId, OriginalPath, TargetPath, FileLength, OrganizationDate, Status, OrganizationType, StatusMessage, ExtractedName, ExtractedYear, ExtractedSeasonNumber, ExtractedEpisodeNumber, ExtractedEndingEpisodeNumber, DuplicatePaths from FileOrganizerResults where ResultId=@ResultId")) { diff --git a/Emby.Server.Implementations/Data/SqliteItemRepository.cs b/Emby.Server.Implementations/Data/SqliteItemRepository.cs index 14723c0a7..1d9cec87e 100644 --- a/Emby.Server.Implementations/Data/SqliteItemRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteItemRepository.cs @@ -99,14 +99,6 @@ namespace Emby.Server.Implementations.Data DbFilePath = Path.Combine(_config.ApplicationPaths.DataPath, "library.db"); } - protected override bool AllowLockRecursion - { - get - { - return true; - } - } - private const string ChaptersTableName = "Chapters2"; protected override int? CacheSize @@ -387,7 +379,7 @@ namespace Emby.Server.Implementations.Data userDataRepo.Initialize(WriteLock); - _backgroundConnection = CreateConnection(true); + //_backgroundConnection = CreateConnection(true); _shrinkMemoryTimer = _timerFactory.Create(OnShrinkMemoryTimerCallback, null, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(30)); } @@ -396,9 +388,9 @@ namespace Emby.Server.Implementations.Data { try { - using (var connection = CreateConnection()) + lock (WriteLock) { - using (WriteLock.Write()) + using (var connection = CreateConnection()) { connection.RunQueries(new string[] { @@ -682,19 +674,23 @@ namespace Emby.Server.Implementations.Data CheckDisposed(); - var tuples = new List>>(); + var tuples = new List, BaseItem, string>>(); foreach (var item in items) { var ancestorIds = item.SupportsAncestors ? item.GetAncestorIds().Distinct().ToList() : null; - tuples.Add(new Tuple>(item, ancestorIds)); + var topParent = item.GetTopParent(); + + var userdataKey = item.GetUserDataKeys().FirstOrDefault(); + + tuples.Add(new Tuple, BaseItem, string>(item, ancestorIds, topParent, userdataKey)); } - using (var connection = CreateConnection()) + lock (WriteLock) { - using (WriteLock.Write()) + using (var connection = CreateConnection()) { connection.RunInTransaction(db => { @@ -704,11 +700,11 @@ namespace Emby.Server.Implementations.Data } } - private void SaveItemsInTranscation(IDatabaseConnection db, List>> tuples) + private void SaveItemsInTranscation(IDatabaseConnection db, List, BaseItem, string>> tuples) { var requiresReset = false; - var statements = db.PrepareAll(string.Join(";", new string[] + var statements = PrepareAll(db, string.Join(";", new string[] { GetSaveItemCommandText(), "delete from AncestorIds where ItemId=@ItemId", @@ -729,8 +725,10 @@ namespace Emby.Server.Implementations.Data } var item = tuple.Item1; + var topParent = tuple.Item3; + var userDataKey = tuple.Item4; - SaveItem(item, saveItemStatement); + SaveItem(item, topParent, userDataKey, saveItemStatement); //Logger.Debug(_saveItemCommand.CommandText); if (item.SupportsAncestors) @@ -747,7 +745,7 @@ namespace Emby.Server.Implementations.Data } } - private void SaveItem(BaseItem item, IStatement saveItemStatement) + private void SaveItem(BaseItem item, BaseItem topParent, string userDataKey, IStatement saveItemStatement) { saveItemStatement.TryBind("@guid", item.Id); saveItemStatement.TryBind("@type", item.GetType().FullName); @@ -840,7 +838,7 @@ namespace Emby.Server.Implementations.Data saveItemStatement.TryBindNull("@Genres"); } - saveItemStatement.TryBind("@InheritedParentalRatingValue", item.GetInheritedParentalRatingValue() ?? 0); + saveItemStatement.TryBind("@InheritedParentalRatingValue", item.InheritedParentalRatingValue); saveItemStatement.TryBind("@SortName", item.SortName); saveItemStatement.TryBind("@RunTimeTicks", item.RunTimeTicks); @@ -922,7 +920,6 @@ namespace Emby.Server.Implementations.Data saveItemStatement.TryBind("@UnratedType", item.GetBlockUnratedType().ToString()); - var topParent = item.GetTopParent(); if (topParent != null) { //Logger.Debug("Item {0} has top parent {1}", item.Id, topParent.Id); @@ -957,7 +954,7 @@ namespace Emby.Server.Implementations.Data saveItemStatement.TryBind("@CriticRating", item.CriticRating); saveItemStatement.TryBind("@CriticRatingSummary", item.CriticRatingSummary); - var inheritedTags = item.GetInheritedTags(); + var inheritedTags = item.InheritedTags; if (inheritedTags.Count > 0) { saveItemStatement.TryBind("@InheritedTags", string.Join("|", inheritedTags.ToArray())); @@ -976,7 +973,7 @@ namespace Emby.Server.Implementations.Data saveItemStatement.TryBind("@CleanName", GetCleanValue(item.Name)); } - saveItemStatement.TryBind("@PresentationUniqueKey", item.GetPresentationUniqueKey()); + saveItemStatement.TryBind("@PresentationUniqueKey", item.PresentationUniqueKey); saveItemStatement.TryBind("@SlugName", item.SlugName); saveItemStatement.TryBind("@OriginalTitle", item.OriginalTitle); @@ -1013,7 +1010,14 @@ namespace Emby.Server.Implementations.Data saveItemStatement.TryBindNull("@SeriesName"); } - saveItemStatement.TryBind("@UserDataKey", item.GetUserDataKeys().FirstOrDefault()); + if (string.IsNullOrWhiteSpace(userDataKey)) + { + saveItemStatement.TryBindNull("@UserDataKey"); + } + else + { + saveItemStatement.TryBind("@UserDataKey", userDataKey); + } var episode = item as Episode; if (episode != null) @@ -1253,11 +1257,11 @@ namespace Emby.Server.Implementations.Data CheckDisposed(); //Logger.Info("Retrieving item {0}", id.ToString("N")); - using (var connection = CreateConnection(true)) + lock (WriteLock) { - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { - using (var statement = connection.PrepareStatement("select " + string.Join(",", _retriveItemColumns) + " from TypedBaseItems where guid = @guid")) + using (var statement = PrepareStatementSafe(connection, "select " + string.Join(",", _retriveItemColumns) + " from TypedBaseItems where guid = @guid")) { statement.TryBind("@guid", id); @@ -2079,11 +2083,11 @@ namespace Emby.Server.Implementations.Data var list = new List(); - using (var connection = CreateConnection(true)) + lock (WriteLock) { - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { - using (var statement = connection.PrepareStatement("select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId order by ChapterIndex asc")) + using (var statement = PrepareStatementSafe(connection, "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId order by ChapterIndex asc")) { statement.TryBind("@ItemId", id); @@ -2113,11 +2117,11 @@ namespace Emby.Server.Implementations.Data throw new ArgumentNullException("id"); } - using (var connection = CreateConnection(true)) + lock (WriteLock) { - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { - using (var statement = connection.PrepareStatement("select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId and ChapterIndex=@ChapterIndex")) + using (var statement = PrepareStatementSafe(connection, "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId and ChapterIndex=@ChapterIndex")) { statement.TryBind("@ItemId", id); statement.TryBind("@ChapterIndex", index); @@ -2194,16 +2198,16 @@ namespace Emby.Server.Implementations.Data var index = 0; - using (var connection = CreateConnection()) + lock (WriteLock) { - using (WriteLock.Write()) + using (var connection = CreateConnection()) { connection.RunInTransaction(db => { // First delete chapters db.Execute("delete from " + ChaptersTableName + " where ItemId=@ItemId", id.ToGuidParamValue()); - using (var saveChapterStatement = db.PrepareStatement("replace into " + ChaptersTableName + " (ItemId, ChapterIndex, StartPositionTicks, Name, ImagePath, ImageDateModified) values (@ItemId, @ChapterIndex, @StartPositionTicks, @Name, @ImagePath, @ImageDateModified)")) + using (var saveChapterStatement = PrepareStatement(db, "replace into " + ChaptersTableName + " (ItemId, ChapterIndex, StartPositionTicks, Name, ImagePath, ImageDateModified) values (@ItemId, @ChapterIndex, @StartPositionTicks, @Name, @ImagePath, @ImageDateModified)")) { foreach (var chapter in chapters) { @@ -2488,11 +2492,11 @@ namespace Emby.Server.Implementations.Data } } - using (var connection = CreateConnection(true)) + lock (WriteLock) { - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { - using (var statement = connection.PrepareStatement(commandText)) + using (var statement = PrepareStatementSafe(connection, commandText)) { if (EnableJoinUserData(query)) { @@ -2513,9 +2517,9 @@ namespace Emby.Server.Implementations.Data } } } - - LogQueryTime("GetItemList", commandText, now); } + + LogQueryTime("GetItemList", commandText, now); } // Hack for right now since we currently don't support filtering out these duplicates within a query @@ -2683,11 +2687,11 @@ namespace Emby.Server.Implementations.Data statementTexts.Add(commandText); } - using (var connection = CreateConnection(true)) + lock (WriteLock) { - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { - var statements = connection.PrepareAll(string.Join(";", statementTexts.ToArray())) + var statements = PrepareAllSafe(connection, string.Join(";", statementTexts.ToArray())) .ToList(); if (!isReturningZeroItems) @@ -2902,11 +2906,11 @@ namespace Emby.Server.Implementations.Data var list = new List(); - using (var connection = CreateConnection(true)) + lock (WriteLock) { - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { - using (var statement = connection.PrepareStatement(commandText)) + using (var statement = PrepareStatementSafe(connection, commandText)) { if (EnableJoinUserData(query)) { @@ -2923,11 +2927,11 @@ namespace Emby.Server.Implementations.Data list.Add(row[0].ReadGuid()); } } + } - LogQueryTime("GetItemList", commandText, now); + LogQueryTime("GetItemList", commandText, now); - return list; - } + return list; } } @@ -2973,11 +2977,11 @@ namespace Emby.Server.Implementations.Data var list = new List>(); - using (var connection = CreateConnection(true)) + lock (WriteLock) { - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { - using (var statement = connection.PrepareStatement(commandText)) + using (var statement = PrepareStatementSafe(connection, commandText)) { if (EnableJoinUserData(query)) { @@ -2999,11 +3003,11 @@ namespace Emby.Server.Implementations.Data list.Add(new Tuple(id, path)); } } + } - LogQueryTime("GetItemIdsWithPath", commandText, now); + LogQueryTime("GetItemIdsWithPath", commandText, now); - return list; - } + return list; } } @@ -3087,13 +3091,13 @@ namespace Emby.Server.Implementations.Data statementTexts.Add(commandText); } - using (var connection = CreateConnection(true)) + lock (WriteLock) { - var statements = connection.PrepareAll(string.Join(";", statementTexts.ToArray())) - .ToList(); - - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { + var statements = PrepareAllSafe(connection, string.Join(";", statementTexts.ToArray())) + .ToList(); + var totalRecordCount = 0; if (!isReturningZeroItems) @@ -4457,9 +4461,9 @@ namespace Emby.Server.Implementations.Data var commandText = "select Guid,InheritedTags,(select group_concat(Tags, '|') from TypedBaseItems where (guid=outer.guid) OR (guid in (Select AncestorId from AncestorIds where ItemId=Outer.guid))) as NewInheritedTags from typedbaseitems as Outer where NewInheritedTags <> InheritedTags"; - using (var connection = CreateConnection()) + lock (WriteLock) { - using (WriteLock.Write()) + using (var connection = CreateConnection()) { foreach (var row in connection.Query(commandText)) { @@ -4476,7 +4480,7 @@ namespace Emby.Server.Implementations.Data } // write lock here - using (var statement = connection.PrepareStatement("Update TypedBaseItems set InheritedTags=@InheritedTags where Guid=@Guid")) + using (var statement = PrepareStatement(connection, "Update TypedBaseItems set InheritedTags=@InheritedTags where Guid=@Guid")) { foreach (var item in newValues) { @@ -4531,9 +4535,9 @@ namespace Emby.Server.Implementations.Data CheckDisposed(); - using (var connection = CreateConnection()) + lock (WriteLock) { - using (WriteLock.Write()) + using (var connection = CreateConnection()) { connection.RunInTransaction(db => { @@ -4561,7 +4565,7 @@ namespace Emby.Server.Implementations.Data private void ExecuteWithSingleParam(IDatabaseConnection db, string query, byte[] value) { - using (var statement = db.PrepareStatement(query)) + using (var statement = PrepareStatement(db, query)) { statement.TryBind("@Id", value); @@ -4591,11 +4595,11 @@ namespace Emby.Server.Implementations.Data var list = new List(); - using (var connection = CreateConnection(true)) + lock (WriteLock) { - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { - using (var statement = connection.PrepareStatement(commandText)) + using (var statement = PrepareStatementSafe(connection, commandText)) { // Run this again to bind the params GetPeopleWhereClauses(query, statement); @@ -4605,8 +4609,8 @@ namespace Emby.Server.Implementations.Data list.Add(row.GetString(0)); } } - return list; } + return list; } } @@ -4632,11 +4636,11 @@ namespace Emby.Server.Implementations.Data var list = new List(); - using (var connection = CreateConnection(true)) + lock (WriteLock) { - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { - using (var statement = connection.PrepareStatement(commandText)) + using (var statement = PrepareStatementSafe(connection, commandText)) { // Run this again to bind the params GetPeopleWhereClauses(query, statement); @@ -4847,15 +4851,18 @@ namespace Emby.Server.Implementations.Data commandText += " Group By CleanValue"; - using (var connection = CreateConnection(true)) + lock (WriteLock) { - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { - foreach (var row in connection.Query(commandText)) + using (var statement = PrepareStatementSafe(connection, commandText)) { - if (!row.IsDBNull(0)) + foreach (var row in statement.ExecuteQuery()) { - list.Add(row.GetString(0)); + if (!row.IsDBNull(0)) + { + list.Add(row.GetString(0)); + } } } } @@ -5023,11 +5030,11 @@ namespace Emby.Server.Implementations.Data statementTexts.Add(countText); } - using (var connection = CreateConnection(true)) + lock (WriteLock) { - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { - var statements = connection.PrepareAll(string.Join(";", statementTexts.ToArray())).ToList(); + var statements = PrepareAllSafe(connection, string.Join(";", statementTexts.ToArray())).ToList(); if (!isReturningZeroItems) { @@ -5208,7 +5215,7 @@ namespace Emby.Server.Implementations.Data // First delete db.Execute("delete from ItemValues where ItemId=@Id", itemId.ToGuidParamValue()); - using (var statement = db.PrepareStatement("insert into ItemValues (ItemId, Type, Value, CleanValue) values (@ItemId, @Type, @Value, @CleanValue)")) + using (var statement = PrepareStatement(db, "insert into ItemValues (ItemId, Type, Value, CleanValue) values (@ItemId, @Type, @Value, @CleanValue)")) { foreach (var pair in values) { @@ -5246,16 +5253,17 @@ namespace Emby.Server.Implementations.Data CheckDisposed(); - using (var connection = CreateConnection()) + lock (WriteLock) { - using (WriteLock.Write()) - { // First delete + using (var connection = CreateConnection()) + { + // First delete // "delete from People where ItemId=?" connection.Execute("delete from People where ItemId=?", itemId.ToGuidParamValue()); var listIndex = 0; - using (var statement = connection.PrepareStatement( + using (var statement = PrepareStatement(connection, "insert into People (ItemId, Name, Role, PersonType, SortOrder, ListOrder) values (@ItemId, @Name, @Role, @PersonType, @SortOrder, @ListOrder)")) { foreach (var person in people) @@ -5332,11 +5340,11 @@ namespace Emby.Server.Implementations.Data cmdText += " order by StreamIndex ASC"; - using (var connection = CreateConnection(true)) + lock (WriteLock) { - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { - using (var statement = connection.PrepareStatement(cmdText)) + using (var statement = PrepareStatementSafe(connection, cmdText)) { statement.TryBind("@ItemId", query.ItemId.ToGuidParamValue()); @@ -5377,13 +5385,14 @@ namespace Emby.Server.Implementations.Data cancellationToken.ThrowIfCancellationRequested(); - using (var connection = CreateConnection()) + lock (WriteLock) { - using (WriteLock.Write()) - { // First delete chapters + using (var connection = CreateConnection()) + { + // First delete chapters connection.Execute("delete from mediastreams where ItemId=@ItemId", id.ToGuidParamValue()); - using (var statement = connection.PrepareStatement(string.Format("replace into mediastreams ({0}) values ({1})", + using (var statement = PrepareStatement(connection, string.Format("replace into mediastreams ({0}) values ({1})", string.Join(",", _mediaStreamSaveColumns), string.Join(",", _mediaStreamSaveColumns.Select(i => "@" + i).ToArray())))) { diff --git a/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs b/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs index b65e5d1b3..8acbd0bb8 100644 --- a/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs @@ -188,9 +188,9 @@ namespace Emby.Server.Implementations.Data { cancellationToken.ThrowIfCancellationRequested(); - using (var connection = CreateConnection()) + lock (WriteLock) { - using (WriteLock.Write()) + using (var connection = CreateConnection()) { connection.RunInTransaction(db => { @@ -259,9 +259,9 @@ namespace Emby.Server.Implementations.Data { cancellationToken.ThrowIfCancellationRequested(); - using (var connection = CreateConnection()) + lock (WriteLock) { - using (WriteLock.Write()) + using (var connection = CreateConnection()) { connection.RunInTransaction(db => { @@ -296,9 +296,9 @@ namespace Emby.Server.Implementations.Data throw new ArgumentNullException("key"); } - using (var connection = CreateConnection(true)) + lock (WriteLock) { - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { using (var statement = connection.PrepareStatement("select key,userid,rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex from userdata where key =@Key and userId=@UserId")) { @@ -349,9 +349,9 @@ namespace Emby.Server.Implementations.Data var list = new List(); - using (var connection = CreateConnection()) + lock (WriteLock) { - using (WriteLock.Read()) + using (var connection = CreateConnection()) { using (var statement = connection.PrepareStatement("select key,userid,rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex from userdata where userId=@UserId")) { diff --git a/Emby.Server.Implementations/Data/SqliteUserRepository.cs b/Emby.Server.Implementations/Data/SqliteUserRepository.cs index 99d7563c7..b2b917e5e 100644 --- a/Emby.Server.Implementations/Data/SqliteUserRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteUserRepository.cs @@ -85,9 +85,9 @@ namespace Emby.Server.Implementations.Data cancellationToken.ThrowIfCancellationRequested(); - using (var connection = CreateConnection()) + using (WriteLock.Write()) { - using (WriteLock.Write()) + using (var connection = CreateConnection()) { connection.RunInTransaction(db => { @@ -110,9 +110,9 @@ namespace Emby.Server.Implementations.Data { var list = new List(); - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { foreach (var row in connection.Query("select guid,data from users")) { @@ -148,9 +148,9 @@ namespace Emby.Server.Implementations.Data cancellationToken.ThrowIfCancellationRequested(); - using (var connection = CreateConnection()) + using (WriteLock.Write()) { - using (WriteLock.Write()) + using (var connection = CreateConnection()) { connection.RunInTransaction(db => { diff --git a/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs b/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs index aae41da19..43e19da65 100644 --- a/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs +++ b/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs @@ -51,24 +51,24 @@ namespace Emby.Server.Implementations.Notifications { var result = new NotificationResult(); - using (var connection = CreateConnection(true)) - { - using (WriteLock.Read()) - { - var clauses = new List(); - var paramList = new List(); + var clauses = new List(); + var paramList = new List(); - if (query.IsRead.HasValue) - { - clauses.Add("IsRead=?"); - paramList.Add(query.IsRead.Value); - } + if (query.IsRead.HasValue) + { + clauses.Add("IsRead=?"); + paramList.Add(query.IsRead.Value); + } - clauses.Add("UserId=?"); - paramList.Add(query.UserId.ToGuidParamValue()); + clauses.Add("UserId=?"); + paramList.Add(query.UserId.ToGuidParamValue()); - var whereClause = " where " + string.Join(" And ", clauses.ToArray()); + var whereClause = " where " + string.Join(" And ", clauses.ToArray()); + using (var connection = CreateConnection(true)) + { + lock (WriteLock) + { result.TotalRecordCount = connection.Query("select count(Id) from Notifications" + whereClause, paramList.ToArray()).SelectScalarInt().First(); var commandText = string.Format("select Id,UserId,Date,Name,Description,Url,Level,IsRead,Category,RelatedId from Notifications{0} order by IsRead asc, Date desc", whereClause); @@ -108,7 +108,7 @@ namespace Emby.Server.Implementations.Notifications using (var connection = CreateConnection(true)) { - using (WriteLock.Read()) + lock (WriteLock) { using (var statement = connection.PrepareStatement("select Level from Notifications where UserId=@UserId and IsRead=@IsRead")) { @@ -225,7 +225,7 @@ namespace Emby.Server.Implementations.Notifications using (var connection = CreateConnection()) { - using (WriteLock.Write()) + lock (WriteLock) { connection.RunInTransaction(conn => { @@ -288,7 +288,7 @@ namespace Emby.Server.Implementations.Notifications using (var connection = CreateConnection()) { - using (WriteLock.Write()) + lock (WriteLock) { connection.RunInTransaction(conn => { @@ -310,7 +310,7 @@ namespace Emby.Server.Implementations.Notifications using (var connection = CreateConnection()) { - using (WriteLock.Write()) + lock (WriteLock) { connection.RunInTransaction(conn => { diff --git a/Emby.Server.Implementations/Security/AuthenticationRepository.cs b/Emby.Server.Implementations/Security/AuthenticationRepository.cs index dbda4a460..dbca4931b 100644 --- a/Emby.Server.Implementations/Security/AuthenticationRepository.cs +++ b/Emby.Server.Implementations/Security/AuthenticationRepository.cs @@ -66,9 +66,9 @@ namespace Emby.Server.Implementations.Security cancellationToken.ThrowIfCancellationRequested(); - using (var connection = CreateConnection()) + using (WriteLock.Write()) { - using (WriteLock.Write()) + using (var connection = CreateConnection()) { connection.RunInTransaction(db => { @@ -202,9 +202,9 @@ namespace Emby.Server.Implementations.Security var list = new List(); - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { using (var statement = connection.PrepareStatement(commandText)) { @@ -241,9 +241,9 @@ namespace Emby.Server.Implementations.Security throw new ArgumentNullException("id"); } - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { var commandText = BaseSelectText + " where Id=@Id"; diff --git a/Emby.Server.Implementations/Social/SharingRepository.cs b/Emby.Server.Implementations/Social/SharingRepository.cs index 12d846e81..e8230947e 100644 --- a/Emby.Server.Implementations/Social/SharingRepository.cs +++ b/Emby.Server.Implementations/Social/SharingRepository.cs @@ -52,9 +52,9 @@ namespace Emby.Server.Implementations.Social throw new ArgumentNullException("info.Id"); } - using (var connection = CreateConnection()) + using (WriteLock.Write()) { - using (WriteLock.Write()) + using (var connection = CreateConnection()) { connection.RunInTransaction(db => { @@ -77,9 +77,9 @@ namespace Emby.Server.Implementations.Social throw new ArgumentNullException("id"); } - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { var commandText = "select Id, ItemId, UserId, ExpirationDate from Shares where id = ?"; diff --git a/Emby.Server.Implementations/Sync/SyncRepository.cs b/Emby.Server.Implementations/Sync/SyncRepository.cs index 037507872..885f8e64a 100644 --- a/Emby.Server.Implementations/Sync/SyncRepository.cs +++ b/Emby.Server.Implementations/Sync/SyncRepository.cs @@ -105,9 +105,9 @@ namespace Emby.Server.Implementations.Sync throw new ArgumentNullException("id"); } - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { var commandText = BaseJobSelectText + " where Id=?"; var paramList = new List(); @@ -216,9 +216,9 @@ namespace Emby.Server.Implementations.Sync CheckDisposed(); - using (var connection = CreateConnection()) + using (WriteLock.Write()) { - using (WriteLock.Write()) + using (var connection = CreateConnection()) { string commandText; var paramList = new List(); @@ -277,9 +277,9 @@ namespace Emby.Server.Implementations.Sync CheckDisposed(); - using (var connection = CreateConnection()) + using (WriteLock.Write()) { - using (WriteLock.Write()) + using (var connection = CreateConnection()) { connection.RunInTransaction(conn => { @@ -299,9 +299,9 @@ namespace Emby.Server.Implementations.Sync CheckDisposed(); - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { var commandText = BaseJobSelectText; var paramList = new List(); @@ -399,9 +399,9 @@ namespace Emby.Server.Implementations.Sync var guid = new Guid(id); - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { var commandText = BaseJobItemSelectText + " where Id=?"; var paramList = new List(); @@ -425,9 +425,9 @@ namespace Emby.Server.Implementations.Sync throw new ArgumentNullException("query"); } - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { - using (WriteLock.Read()) + using (var connection = CreateConnection(true)) { var commandText = baseSelectText; var paramList = new List(); @@ -505,41 +505,41 @@ namespace Emby.Server.Implementations.Sync var now = DateTime.UtcNow; - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { - var commandText = "select ItemId,Status,Progress from SyncJobItems"; - var whereClauses = new List(); - - if (!string.IsNullOrWhiteSpace(query.TargetId)) + using (var connection = CreateConnection(true)) { - whereClauses.Add("TargetId=@TargetId"); - } + var commandText = "select ItemId,Status,Progress from SyncJobItems"; + var whereClauses = new List(); - if (query.Statuses.Length > 0) - { - var statuses = string.Join(",", query.Statuses.Select(i => "'" + i.ToString() + "'").ToArray()); + if (!string.IsNullOrWhiteSpace(query.TargetId)) + { + whereClauses.Add("TargetId=@TargetId"); + } - whereClauses.Add(string.Format("Status in ({0})", statuses)); - } + if (query.Statuses.Length > 0) + { + var statuses = string.Join(",", query.Statuses.Select(i => "'" + i.ToString() + "'").ToArray()); - if (whereClauses.Count > 0) - { - commandText += " where " + string.Join(" AND ", whereClauses.ToArray()); - } + whereClauses.Add(string.Format("Status in ({0})", statuses)); + } + + if (whereClauses.Count > 0) + { + commandText += " where " + string.Join(" AND ", whereClauses.ToArray()); + } - var statementTexts = new List + var statementTexts = new List { commandText }; - commandText = commandText - .Replace("select ItemId,Status,Progress from SyncJobItems", "select ItemIds,Status,Progress from SyncJobs") - .Replace("'Synced'", "'Completed','CompletedWithError'"); + commandText = commandText + .Replace("select ItemId,Status,Progress from SyncJobItems", "select ItemIds,Status,Progress from SyncJobs") + .Replace("'Synced'", "'Completed','CompletedWithError'"); - statementTexts.Add(commandText); + statementTexts.Add(commandText); - using (WriteLock.Read()) - { var statements = connection.PrepareAll(string.Join(";", statementTexts.ToArray())) .ToList(); @@ -692,9 +692,9 @@ namespace Emby.Server.Implementations.Sync CheckDisposed(); - using (var connection = CreateConnection()) + using (WriteLock.Write()) { - using (WriteLock.Write()) + using (var connection = CreateConnection()) { string commandText; diff --git a/MediaBrowser.Controller/Entities/TV/Season.cs b/MediaBrowser.Controller/Entities/TV/Season.cs index e0cc496a1..f2a6586e2 100644 --- a/MediaBrowser.Controller/Entities/TV/Season.cs +++ b/MediaBrowser.Controller/Entities/TV/Season.cs @@ -48,11 +48,7 @@ namespace MediaBrowser.Controller.Entities.TV [IgnoreDataMember] public override Guid? DisplayParentId { - get - { - var series = Series; - return series == null ? ParentId : series.Id; - } + get { return SeriesId; } } [IgnoreDataMember] -- cgit v1.2.3 From 1aff48b93b72fe7d418b4798f504bd0d145f44e8 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Mon, 12 Dec 2016 00:49:19 -0500 Subject: move book support into the core --- Emby.Server.Core/ApplicationHost.cs | 1 + .../Activity/ActivityRepository.cs | 44 +-- .../Data/BaseSqliteRepository.cs | 2 +- .../Data/SqliteItemRepository.cs | 306 +++++++++++---------- .../Data/SqliteUserDataRepository.cs | 24 +- .../Emby.Server.Implementations.csproj | 1 + .../Library/Resolvers/Audio/AudioResolver.cs | 6 + .../Library/Resolvers/Books/BookResolver.cs | 77 ++++++ .../Library/UserDataManager.cs | 2 +- .../Security/AuthenticationRepository.cs | 44 +-- Emby.Server.Implementations/TV/TVSeriesManager.cs | 70 +++-- MediaBrowser.Api/UserLibrary/UserLibraryService.cs | 3 +- .../Entities/Audio/AudioPodcast.cs | 12 +- MediaBrowser.Controller/Entities/AudioBook.cs | 64 +++++ MediaBrowser.Controller/Entities/BaseItem.cs | 9 + .../Entities/CollectionFolder.cs | 1 + MediaBrowser.Controller/Entities/TV/Episode.cs | 3 +- MediaBrowser.Controller/Entities/Video.cs | 9 + .../LiveTv/LiveTvAudioRecording.cs | 9 + MediaBrowser.Controller/LiveTv/LiveTvChannel.cs | 9 + .../MediaBrowser.Controller.csproj | 1 + .../Books/AudioBookMetadataService.cs | 41 +++ .../Books/AudioPodcastMetadataService.cs | 41 +++ .../MediaBrowser.Providers.csproj | 2 + .../MediaBrowser.WebDashboard.csproj | 3 - 25 files changed, 567 insertions(+), 217 deletions(-) create mode 100644 Emby.Server.Implementations/Library/Resolvers/Books/BookResolver.cs create mode 100644 MediaBrowser.Controller/Entities/AudioBook.cs create mode 100644 MediaBrowser.Providers/Books/AudioBookMetadataService.cs create mode 100644 MediaBrowser.Providers/Books/AudioPodcastMetadataService.cs (limited to 'Emby.Server.Implementations/Activity/ActivityRepository.cs') diff --git a/Emby.Server.Core/ApplicationHost.cs b/Emby.Server.Core/ApplicationHost.cs index 2074b5ae4..a6d2d32c0 100644 --- a/Emby.Server.Core/ApplicationHost.cs +++ b/Emby.Server.Core/ApplicationHost.cs @@ -424,6 +424,7 @@ namespace Emby.Server.Core ServiceStack.Text.JsConfig.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "ShortOverview", "Taglines", "Keywords", "ExtraType" }; ServiceStack.Text.JsConfig.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "ShortOverview", "Taglines", "Keywords", "ExtraType" }; ServiceStack.Text.JsConfig.ExcludePropertyNames = new[] { "Artists", "AlbumArtists", "ChannelMediaSources", "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "ShortOverview", "Taglines", "Keywords", "ExtraType" }; + ServiceStack.Text.JsConfig.ExcludePropertyNames = new[] { "Artists", "AlbumArtists", "ChannelMediaSources", "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "ShortOverview", "Taglines", "Keywords", "ExtraType" }; ServiceStack.Text.JsConfig.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "ShortOverview", "Taglines", "Keywords", "ExtraType" }; ServiceStack.Text.JsConfig.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "ShortOverview", "Taglines", "Keywords", "ExtraType" }; ServiceStack.Text.JsConfig.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "ShortOverview", "Taglines", "Keywords", "ExtraType" }; diff --git a/Emby.Server.Implementations/Activity/ActivityRepository.cs b/Emby.Server.Implementations/Activity/ActivityRepository.cs index 17aef7268..fda8b949b 100644 --- a/Emby.Server.Implementations/Activity/ActivityRepository.cs +++ b/Emby.Server.Implementations/Activity/ActivityRepository.cs @@ -84,6 +84,9 @@ namespace Emby.Server.Implementations.Activity { using (var connection = CreateConnection(true)) { + var list = new List(); + int totalRecordCount = 0; + var commandText = BaseActivitySelectText; var whereClauses = new List(); @@ -120,32 +123,37 @@ namespace Emby.Server.Implementations.Activity commandText += " LIMIT " + limit.Value.ToString(_usCulture); } - var list = new List(); + var statementTexts = new List(); + statementTexts.Add(commandText); + statementTexts.Add("select count (Id) from ActivityLogEntries" + whereTextWithoutPaging); - using (var statement = connection.PrepareStatement(commandText)) + connection.RunInTransaction(db => { - if (minDate.HasValue) - { - statement.TryBind("@DateCreated", minDate.Value.ToDateTimeParamValue()); - } + var statements = PrepareAllSafe(db, string.Join(";", statementTexts.ToArray())).ToList(); - foreach (var row in statement.ExecuteQuery()) + using (var statement = statements[0]) { - list.Add(GetEntry(row)); + if (minDate.HasValue) + { + statement.TryBind("@DateCreated", minDate.Value.ToDateTimeParamValue()); + } + + foreach (var row in statement.ExecuteQuery()) + { + list.Add(GetEntry(row)); + } } - } - - int totalRecordCount; - using (var statement = connection.PrepareStatement("select count (Id) from ActivityLogEntries" + whereTextWithoutPaging)) - { - if (minDate.HasValue) + using (var statement = statements[1]) { - statement.TryBind("@DateCreated", minDate.Value.ToDateTimeParamValue()); - } + if (minDate.HasValue) + { + statement.TryBind("@DateCreated", minDate.Value.ToDateTimeParamValue()); + } - totalRecordCount = statement.ExecuteQuery().SelectScalarInt().First(); - } + totalRecordCount = statement.ExecuteQuery().SelectScalarInt().First(); + } + }, ReadTransactionMode); return new QueryResult() { diff --git a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs index 8f2bb2cea..9e60a43aa 100644 --- a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs +++ b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs @@ -79,7 +79,7 @@ namespace Emby.Server.Implementations.Data connectionFlags |= ConnectionFlags.ReadWrite; } - //connectionFlags |= ConnectionFlags.SharedCached; + connectionFlags |= ConnectionFlags.SharedCached; connectionFlags |= ConnectionFlags.NoMutex; var db = SQLite3.Open(DbFilePath, connectionFlags, null); diff --git a/Emby.Server.Implementations/Data/SqliteItemRepository.cs b/Emby.Server.Implementations/Data/SqliteItemRepository.cs index e3dd3f884..768f5b5a0 100644 --- a/Emby.Server.Implementations/Data/SqliteItemRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteItemRepository.cs @@ -123,17 +123,10 @@ namespace Emby.Server.Implementations.Data } } - private SQLiteDatabaseConnection _backgroundConnection; protected override void CloseConnection() { base.CloseConnection(); - if (_backgroundConnection != null) - { - _backgroundConnection.Dispose(); - _backgroundConnection = null; - } - if (_shrinkMemoryTimer != null) { _shrinkMemoryTimer.Dispose(); @@ -379,8 +372,6 @@ namespace Emby.Server.Implementations.Data userDataRepo.Initialize(WriteLock); - //_backgroundConnection = CreateConnection(true); - _shrinkMemoryTimer = _timerFactory.Create(OnShrinkMemoryTimerCallback, null, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(30)); } @@ -1370,6 +1361,10 @@ namespace Emby.Server.Implementations.Data { return false; } + if (type == typeof(AudioBook)) + { + return false; + } if (type == typeof(MusicAlbum)) { return false; @@ -2691,51 +2686,55 @@ namespace Emby.Server.Implementations.Data { using (var connection = CreateConnection(true)) { - var statements = PrepareAllSafe(connection, string.Join(";", statementTexts.ToArray())) - .ToList(); - - if (!isReturningZeroItems) + connection.RunInTransaction(db => { - using (var statement = statements[0]) + var statements = PrepareAllSafe(db, string.Join(";", statementTexts.ToArray())) + .ToList(); + + if (!isReturningZeroItems) { - if (EnableJoinUserData(query)) + using (var statement = statements[0]) { - statement.TryBind("@UserId", query.User.Id); - } + if (EnableJoinUserData(query)) + { + statement.TryBind("@UserId", query.User.Id); + } - BindSimilarParams(query, statement); + BindSimilarParams(query, statement); - // Running this again will bind the params - GetWhereClauses(query, statement); + // Running this again will bind the params + GetWhereClauses(query, statement); - foreach (var row in statement.ExecuteQuery()) - { - var item = GetItem(row, query); - if (item != null) + foreach (var row in statement.ExecuteQuery()) { - list.Add(item); + var item = GetItem(row, query); + if (item != null) + { + list.Add(item); + } } } - } - } - if (query.EnableTotalRecordCount) - { - using (var statement = statements[statements.Count - 1]) - { - if (EnableJoinUserData(query)) + if (query.EnableTotalRecordCount) { - statement.TryBind("@UserId", query.User.Id); - } + using (var statement = statements[statements.Count - 1]) + { + if (EnableJoinUserData(query)) + { + statement.TryBind("@UserId", query.User.Id); + } - BindSimilarParams(query, statement); + BindSimilarParams(query, statement); - // Running this again will bind the params - GetWhereClauses(query, statement); + // Running this again will bind the params + GetWhereClauses(query, statement); - totalRecordCount = statement.ExecuteQuery().SelectScalarInt().First(); + totalRecordCount = statement.ExecuteQuery().SelectScalarInt().First(); + } + } } - } + + }, ReadTransactionMode); LogQueryTime("GetItems", commandText, now); @@ -3095,49 +3094,53 @@ namespace Emby.Server.Implementations.Data { using (var connection = CreateConnection(true)) { - var statements = PrepareAllSafe(connection, string.Join(";", statementTexts.ToArray())) - .ToList(); - var totalRecordCount = 0; - if (!isReturningZeroItems) + connection.RunInTransaction(db => { - using (var statement = statements[0]) + var statements = PrepareAllSafe(db, string.Join(";", statementTexts.ToArray())) + .ToList(); + + if (!isReturningZeroItems) { - if (EnableJoinUserData(query)) + using (var statement = statements[0]) { - statement.TryBind("@UserId", query.User.Id); - } + if (EnableJoinUserData(query)) + { + statement.TryBind("@UserId", query.User.Id); + } - BindSimilarParams(query, statement); + BindSimilarParams(query, statement); - // Running this again will bind the params - GetWhereClauses(query, statement); + // Running this again will bind the params + GetWhereClauses(query, statement); - foreach (var row in statement.ExecuteQuery()) - { - list.Add(row[0].ReadGuid()); + foreach (var row in statement.ExecuteQuery()) + { + list.Add(row[0].ReadGuid()); + } } } - } - if (query.EnableTotalRecordCount) - { - using (var statement = statements[statements.Count - 1]) + if (query.EnableTotalRecordCount) { - if (EnableJoinUserData(query)) + using (var statement = statements[statements.Count - 1]) { - statement.TryBind("@UserId", query.User.Id); - } + if (EnableJoinUserData(query)) + { + statement.TryBind("@UserId", query.User.Id); + } - BindSimilarParams(query, statement); + BindSimilarParams(query, statement); - // Running this again will bind the params - GetWhereClauses(query, statement); + // Running this again will bind the params + GetWhereClauses(query, statement); - totalRecordCount = statement.ExecuteQuery().SelectScalarInt().First(); + totalRecordCount = statement.ExecuteQuery().SelectScalarInt().First(); + } } - } + + }, ReadTransactionMode); LogQueryTime("GetItemIds", commandText, now); @@ -4426,6 +4429,7 @@ namespace Emby.Server.Implementations.Data typeof(Movie), typeof(Playlist), typeof(AudioPodcast), + typeof(AudioBook), typeof(Trailer), typeof(BoxSet), typeof(Episode), @@ -4594,21 +4598,23 @@ namespace Emby.Server.Implementations.Data commandText += " order by ListOrder"; var list = new List(); - using (WriteLock.Read()) { using (var connection = CreateConnection(true)) { - using (var statement = PrepareStatementSafe(connection, commandText)) + connection.RunInTransaction(db => { - // Run this again to bind the params - GetPeopleWhereClauses(query, statement); - - foreach (var row in statement.ExecuteQuery()) + using (var statement = PrepareStatementSafe(db, commandText)) { - list.Add(row.GetString(0)); + // Run this again to bind the params + GetPeopleWhereClauses(query, statement); + + foreach (var row in statement.ExecuteQuery()) + { + list.Add(row.GetString(0)); + } } - } + }, ReadTransactionMode); } return list; } @@ -4640,16 +4646,19 @@ namespace Emby.Server.Implementations.Data { using (var connection = CreateConnection(true)) { - using (var statement = PrepareStatementSafe(connection, commandText)) + connection.RunInTransaction(db => { - // Run this again to bind the params - GetPeopleWhereClauses(query, statement); - - foreach (var row in statement.ExecuteQuery()) + using (var statement = PrepareStatementSafe(db, commandText)) { - list.Add(GetPerson(row)); + // Run this again to bind the params + GetPeopleWhereClauses(query, statement); + + foreach (var row in statement.ExecuteQuery()) + { + list.Add(GetPerson(row)); + } } - } + }, ReadTransactionMode); } } @@ -4855,16 +4864,19 @@ namespace Emby.Server.Implementations.Data { using (var connection = CreateConnection(true)) { - using (var statement = PrepareStatementSafe(connection, commandText)) + connection.RunInTransaction(db => { - foreach (var row in statement.ExecuteQuery()) + using (var statement = PrepareStatementSafe(db, commandText)) { - if (!row.IsDBNull(0)) + foreach (var row in statement.ExecuteQuery()) { - list.Add(row.GetString(0)); + if (!row.IsDBNull(0)) + { + list.Add(row.GetString(0)); + } } } - } + }, ReadTransactionMode); } } LogQueryTime("GetItemValueNames", commandText, now); @@ -5034,69 +5046,72 @@ namespace Emby.Server.Implementations.Data { using (var connection = CreateConnection(true)) { - var statements = PrepareAllSafe(connection, string.Join(";", statementTexts.ToArray())).ToList(); - - if (!isReturningZeroItems) + connection.RunInTransaction(db => { - using (var statement = statements[0]) + var statements = PrepareAllSafe(db, string.Join(";", statementTexts.ToArray())).ToList(); + + if (!isReturningZeroItems) { - statement.TryBind("@SelectType", returnType); - if (EnableJoinUserData(query)) + using (var statement = statements[0]) { - statement.TryBind("@UserId", query.User.Id); - } + statement.TryBind("@SelectType", returnType); + if (EnableJoinUserData(query)) + { + statement.TryBind("@UserId", query.User.Id); + } - if (typeSubQuery != null) - { - GetWhereClauses(typeSubQuery, null, "itemTypes"); - } - BindSimilarParams(query, statement); - GetWhereClauses(innerQuery, statement); - GetWhereClauses(outerQuery, statement); + if (typeSubQuery != null) + { + GetWhereClauses(typeSubQuery, null, "itemTypes"); + } + BindSimilarParams(query, statement); + GetWhereClauses(innerQuery, statement); + GetWhereClauses(outerQuery, statement); - foreach (var row in statement.ExecuteQuery()) - { - var item = GetItem(row); - if (item != null) + foreach (var row in statement.ExecuteQuery()) { - var countStartColumn = columns.Count - 1; + var item = GetItem(row); + if (item != null) + { + var countStartColumn = columns.Count - 1; - list.Add(new Tuple(item, GetItemCounts(row, countStartColumn, typesToCount))); + list.Add(new Tuple(item, GetItemCounts(row, countStartColumn, typesToCount))); + } } - } - LogQueryTime("GetItemValues", commandText, now); + LogQueryTime("GetItemValues", commandText, now); + } } - } - if (query.EnableTotalRecordCount) - { - commandText = "select count (distinct PresentationUniqueKey)" + GetFromText(); + if (query.EnableTotalRecordCount) + { + commandText = "select count (distinct PresentationUniqueKey)" + GetFromText(); - commandText += GetJoinUserDataText(query); - commandText += whereText; + commandText += GetJoinUserDataText(query); + commandText += whereText; - using (var statement = statements[statements.Count - 1]) - { - statement.TryBind("@SelectType", returnType); - if (EnableJoinUserData(query)) + using (var statement = statements[statements.Count - 1]) { - statement.TryBind("@UserId", query.User.Id); - } + statement.TryBind("@SelectType", returnType); + if (EnableJoinUserData(query)) + { + statement.TryBind("@UserId", query.User.Id); + } - if (typeSubQuery != null) - { - GetWhereClauses(typeSubQuery, null, "itemTypes"); - } - BindSimilarParams(query, statement); - GetWhereClauses(innerQuery, statement); - GetWhereClauses(outerQuery, statement); + if (typeSubQuery != null) + { + GetWhereClauses(typeSubQuery, null, "itemTypes"); + } + BindSimilarParams(query, statement); + GetWhereClauses(innerQuery, statement); + GetWhereClauses(outerQuery, statement); - count = statement.ExecuteQuery().SelectScalarInt().First(); + count = statement.ExecuteQuery().SelectScalarInt().First(); - LogQueryTime("GetItemValues", commandText, now); + LogQueryTime("GetItemValues", commandText, now); + } } - } + }, ReadTransactionMode); } } @@ -5344,25 +5359,28 @@ namespace Emby.Server.Implementations.Data { using (var connection = CreateConnection(true)) { - using (var statement = PrepareStatementSafe(connection, cmdText)) + connection.RunInTransaction(db => { - statement.TryBind("@ItemId", query.ItemId.ToGuidParamValue()); - - if (query.Type.HasValue) + using (var statement = PrepareStatementSafe(db, cmdText)) { - statement.TryBind("@StreamType", query.Type.Value.ToString()); - } + statement.TryBind("@ItemId", query.ItemId.ToGuidParamValue()); - if (query.Index.HasValue) - { - statement.TryBind("@StreamIndex", query.Index.Value); - } + if (query.Type.HasValue) + { + statement.TryBind("@StreamType", query.Type.Value.ToString()); + } - foreach (var row in statement.ExecuteQuery()) - { - list.Add(GetMediaStream(row)); + if (query.Index.HasValue) + { + statement.TryBind("@StreamIndex", query.Index.Value); + } + + foreach (var row in statement.ExecuteQuery()) + { + list.Add(GetMediaStream(row)); + } } - } + }, ReadTransactionMode); } } diff --git a/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs b/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs index be59d71b3..7afb5720e 100644 --- a/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs @@ -300,20 +300,26 @@ namespace Emby.Server.Implementations.Data { using (var connection = CreateConnection(true)) { - using (var statement = connection.PrepareStatement("select key,userid,rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex from userdata where key =@Key and userId=@UserId")) - { - statement.TryBind("@UserId", userId.ToGuidParamValue()); - statement.TryBind("@Key", key); + UserItemData result = null; - foreach (var row in statement.ExecuteQuery()) + connection.RunInTransaction(db => + { + using (var statement = db.PrepareStatement("select key,userid,rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex from userdata where key =@Key and userId=@UserId")) { - return ReadRow(row); + statement.TryBind("@UserId", userId.ToGuidParamValue()); + statement.TryBind("@Key", key); + + foreach (var row in statement.ExecuteQuery()) + { + result = ReadRow(row); + break; + } } - } + }, ReadTransactionMode); + + return result; } } - - return null; } public UserItemData GetUserData(Guid userId, List keys) diff --git a/Emby.Server.Implementations/Emby.Server.Implementations.csproj b/Emby.Server.Implementations/Emby.Server.Implementations.csproj index 47f4e7ced..e478b9d81 100644 --- a/Emby.Server.Implementations/Emby.Server.Implementations.csproj +++ b/Emby.Server.Implementations/Emby.Server.Implementations.csproj @@ -119,6 +119,7 @@ + diff --git a/Emby.Server.Implementations/Library/Resolvers/Audio/AudioResolver.cs b/Emby.Server.Implementations/Library/Resolvers/Audio/AudioResolver.cs index d8805355a..2e3d81474 100644 --- a/Emby.Server.Implementations/Library/Resolvers/Audio/AudioResolver.cs +++ b/Emby.Server.Implementations/Library/Resolvers/Audio/AudioResolver.cs @@ -2,6 +2,7 @@ using MediaBrowser.Controller.Resolvers; using MediaBrowser.Model.Entities; using System; +using MediaBrowser.Controller.Entities; namespace Emby.Server.Implementations.Library.Resolvers.Audio { @@ -59,6 +60,11 @@ namespace Emby.Server.Implementations.Library.Resolvers.Audio { return new MediaBrowser.Controller.Entities.Audio.Audio(); } + + if (string.Equals(collectionType, CollectionType.Books, StringComparison.OrdinalIgnoreCase)) + { + return new AudioBook(); + } } } diff --git a/Emby.Server.Implementations/Library/Resolvers/Books/BookResolver.cs b/Emby.Server.Implementations/Library/Resolvers/Books/BookResolver.cs new file mode 100644 index 000000000..4852c3c6a --- /dev/null +++ b/Emby.Server.Implementations/Library/Resolvers/Books/BookResolver.cs @@ -0,0 +1,77 @@ +using System; +using System.IO; +using System.Linq; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Library; +using MediaBrowser.Model.Entities; + +namespace Emby.Server.Implementations.Library.Resolvers.Books +{ + /// + /// + /// + public class BookResolver : MediaBrowser.Controller.Resolvers.ItemResolver + { + private readonly string[] _validExtensions = {".pdf", ".epub", ".mobi", ".cbr", ".cbz"}; + + /// + /// + /// + /// + /// + protected override Book Resolve(ItemResolveArgs args) + { + var collectionType = args.GetCollectionType(); + + // Only process items that are in a collection folder containing books + if (!string.Equals(collectionType, CollectionType.Books, StringComparison.OrdinalIgnoreCase)) + return null; + + if (args.IsDirectory) + { + return GetBook(args); + } + + var extension = Path.GetExtension(args.Path); + + if (extension != null && _validExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase)) + { + // It's a book + return new Book + { + Path = args.Path, + IsInMixedFolder = true + }; + } + + return null; + } + + /// + /// + /// + /// + /// + private Book GetBook(ItemResolveArgs args) + { + var bookFiles = args.FileSystemChildren.Where(f => + { + var fileExtension = Path.GetExtension(f.FullName) ?? + string.Empty; + + return _validExtensions.Contains(fileExtension, + StringComparer + .OrdinalIgnoreCase); + }).ToList(); + + // Don't return a Book if there is more (or less) than one document in the directory + if (bookFiles.Count != 1) + return null; + + return new Book + { + Path = bookFiles[0].FullName + }; + } + } +} diff --git a/Emby.Server.Implementations/Library/UserDataManager.cs b/Emby.Server.Implementations/Library/UserDataManager.cs index c8dde1287..f4a30fc00 100644 --- a/Emby.Server.Implementations/Library/UserDataManager.cs +++ b/Emby.Server.Implementations/Library/UserDataManager.cs @@ -274,7 +274,7 @@ namespace Emby.Server.Implementations.Library positionTicks = 0; data.Played = false; } - if (item is Audio) + if (!item.SupportsPositionTicksResume) { positionTicks = 0; } diff --git a/Emby.Server.Implementations/Security/AuthenticationRepository.cs b/Emby.Server.Implementations/Security/AuthenticationRepository.cs index dbca4931b..a136701da 100644 --- a/Emby.Server.Implementations/Security/AuthenticationRepository.cs +++ b/Emby.Server.Implementations/Security/AuthenticationRepository.cs @@ -201,35 +201,47 @@ namespace Emby.Server.Implementations.Security } var list = new List(); + int totalRecordCount = 0; using (WriteLock.Read()) { using (var connection = CreateConnection(true)) { - using (var statement = connection.PrepareStatement(commandText)) + connection.RunInTransaction(db => { - BindAuthenticationQueryParams(query, statement); + var statementTexts = new List(); + statementTexts.Add(commandText); + statementTexts.Add("select count (Id) from AccessTokens" + whereTextWithoutPaging); - foreach (var row in statement.ExecuteQuery()) - { - list.Add(Get(row)); - } + var statements = PrepareAllSafe(db, string.Join(";", statementTexts.ToArray())) + .ToList(); - using (var totalCountStatement = connection.PrepareStatement("select count (Id) from AccessTokens" + whereTextWithoutPaging)) + using (var statement = statements[0]) { - BindAuthenticationQueryParams(query, totalCountStatement); + BindAuthenticationQueryParams(query, statement); - var count = totalCountStatement.ExecuteQuery() - .SelectScalarInt() - .First(); + foreach (var row in statement.ExecuteQuery()) + { + list.Add(Get(row)); + } - return new QueryResult() + using (var totalCountStatement = statements[1]) { - Items = list.ToArray(), - TotalRecordCount = count - }; + BindAuthenticationQueryParams(query, totalCountStatement); + + totalRecordCount = totalCountStatement.ExecuteQuery() + .SelectScalarInt() + .First(); + } } - } + + }, ReadTransactionMode); + + return new QueryResult() + { + Items = list.ToArray(), + TotalRecordCount = totalRecordCount + }; } } } diff --git a/Emby.Server.Implementations/TV/TVSeriesManager.cs b/Emby.Server.Implementations/TV/TVSeriesManager.cs index 4f876f6a3..88d224525 100644 --- a/Emby.Server.Implementations/TV/TVSeriesManager.cs +++ b/Emby.Server.Implementations/TV/TVSeriesManager.cs @@ -62,7 +62,14 @@ namespace Emby.Server.Implementations.TV PresentationUniqueKey = presentationUniqueKey, Limit = limit, ParentId = parentIdGuid, - Recursive = true + Recursive = true, + DtoOptions = new MediaBrowser.Controller.Dto.DtoOptions + { + Fields = new List + { + + } + } }).Cast(); @@ -104,7 +111,15 @@ namespace Emby.Server.Implementations.TV IncludeItemTypes = new[] { typeof(Series).Name }, SortOrder = SortOrder.Ascending, PresentationUniqueKey = presentationUniqueKey, - Limit = limit + Limit = limit, + DtoOptions = new MediaBrowser.Controller.Dto.DtoOptions + { + Fields = new List + { + + }, + EnableImages = false + } }, parentsFolders.Cast().ToList()).Cast(); @@ -120,26 +135,32 @@ namespace Emby.Server.Implementations.TV var currentUser = user; var allNextUp = series - .Select(i => GetNextUp(i, currentUser)) + .Select(i => GetNextUp(GetUniqueSeriesKey(i), currentUser)) // Include if an episode was found, and either the series is not unwatched or the specific series was requested - .OrderByDescending(i => i.Item1) - .ToList(); + .OrderByDescending(i => i.Item1); // If viewing all next up for all series, remove first episodes - if (string.IsNullOrWhiteSpace(request.SeriesId)) - { - var withoutFirstEpisode = allNextUp - .Where(i => i.Item1 != DateTime.MinValue) - .ToList(); - - // But if that returns empty, keep those first episodes (avoid completely empty view) - if (withoutFirstEpisode.Count > 0) - { - allNextUp = withoutFirstEpisode; - } - } + // But if that returns empty, keep those first episodes (avoid completely empty view) + var alwaysEnableFirstEpisode = string.IsNullOrWhiteSpace(request.SeriesId); + var isFirstItemAFirstEpisode = true; return allNextUp + .Where(i => + { + if (alwaysEnableFirstEpisode || i.Item1 != DateTime.MinValue) + { + isFirstItemAFirstEpisode = false; + return true; + } + + if (isFirstItemAFirstEpisode) + { + return false; + } + + return true; + }) + .Take(request.Limit.HasValue ? (request.Limit.Value * 2) : int.MaxValue) .Select(i => i.Item2()) .Where(i => i != null) .Take(request.Limit ?? int.MaxValue); @@ -153,13 +174,10 @@ namespace Emby.Server.Implementations.TV /// /// Gets the next up. /// - /// The series. - /// The user. /// Task{Episode}. - private Tuple> GetNextUp(Series series, User user) + private Tuple> GetNextUp(string seriesKey, User user) { var enableSeriesPresentationKey = _config.Configuration.EnableSeriesPresentationUniqueKey; - var seriesKey = GetUniqueSeriesKey(series); var lastWatchedEpisode = _libraryManager.GetItemList(new InternalItemsQuery(user) { @@ -170,7 +188,15 @@ namespace Emby.Server.Implementations.TV SortOrder = SortOrder.Descending, IsPlayed = true, Limit = 1, - ParentIndexNumberNotEquals = 0 + ParentIndexNumberNotEquals = 0, + DtoOptions = new MediaBrowser.Controller.Dto.DtoOptions + { + Fields = new List + { + + }, + EnableImages = false + } }).FirstOrDefault(); diff --git a/MediaBrowser.Api/UserLibrary/UserLibraryService.cs b/MediaBrowser.Api/UserLibrary/UserLibraryService.cs index 4121cc295..1ac98d165 100644 --- a/MediaBrowser.Api/UserLibrary/UserLibraryService.cs +++ b/MediaBrowser.Api/UserLibrary/UserLibraryService.cs @@ -12,6 +12,7 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; using MediaBrowser.Common.IO; +using MediaBrowser.Controller.Entities.Audio; using MediaBrowser.Controller.IO; using MediaBrowser.Model.IO; using MediaBrowser.Controller.Providers; @@ -324,7 +325,7 @@ namespace MediaBrowser.Api.UserLibrary var item = i.Item2[0]; var childCount = 0; - if (i.Item1 != null && i.Item2.Count > 1) + if (i.Item1 != null && (i.Item2.Count > 1 || i.Item1 is MusicAlbum)) { item = i.Item1; childCount = i.Item2.Count; diff --git a/MediaBrowser.Controller/Entities/Audio/AudioPodcast.cs b/MediaBrowser.Controller/Entities/Audio/AudioPodcast.cs index 9072e1094..8c820d367 100644 --- a/MediaBrowser.Controller/Entities/Audio/AudioPodcast.cs +++ b/MediaBrowser.Controller/Entities/Audio/AudioPodcast.cs @@ -1,6 +1,16 @@ -namespace MediaBrowser.Controller.Entities.Audio +using MediaBrowser.Model.Serialization; + +namespace MediaBrowser.Controller.Entities.Audio { public class AudioPodcast : Audio { + [IgnoreDataMember] + public override bool SupportsPositionTicksResume + { + get + { + return true; + } + } } } diff --git a/MediaBrowser.Controller/Entities/AudioBook.cs b/MediaBrowser.Controller/Entities/AudioBook.cs new file mode 100644 index 000000000..efeb9b497 --- /dev/null +++ b/MediaBrowser.Controller/Entities/AudioBook.cs @@ -0,0 +1,64 @@ +using System; +using MediaBrowser.Model.Configuration; +using MediaBrowser.Model.Serialization; +using MediaBrowser.Model.Entities; + +namespace MediaBrowser.Controller.Entities +{ + public class AudioBook : Audio.Audio, IHasSeries + { + [IgnoreDataMember] + public override bool SupportsPositionTicksResume + { + get + { + return true; + } + } + + [IgnoreDataMember] + public string SeriesPresentationUniqueKey { get; set; } + [IgnoreDataMember] + public string SeriesName { get; set; } + [IgnoreDataMember] + public Guid? SeriesId { get; set; } + [IgnoreDataMember] + public string SeriesSortName { get; set; } + + public string FindSeriesSortName() + { + return SeriesSortName; + } + public string FindSeriesName() + { + return SeriesName; + } + public string FindSeriesPresentationUniqueKey() + { + return SeriesPresentationUniqueKey; + } + + [IgnoreDataMember] + public override bool EnableRefreshOnDateModifiedChange + { + get { return true; } + } + + public Guid? FindSeriesId() + { + return SeriesId; + } + + public override bool CanDownload() + { + var locationType = LocationType; + return locationType != LocationType.Remote && + locationType != LocationType.Virtual; + } + + public override UnratedItem GetBlockUnratedType() + { + return UnratedItem.Book; + } + } +} diff --git a/MediaBrowser.Controller/Entities/BaseItem.cs b/MediaBrowser.Controller/Entities/BaseItem.cs index b7ea7a92d..9f4503466 100644 --- a/MediaBrowser.Controller/Entities/BaseItem.cs +++ b/MediaBrowser.Controller/Entities/BaseItem.cs @@ -142,6 +142,15 @@ namespace MediaBrowser.Controller.Entities } } + [IgnoreDataMember] + public virtual bool SupportsPositionTicksResume + { + get + { + return false; + } + } + public bool DetectIsInMixedFolder() { if (SupportsIsInMixedFolderDetection) diff --git a/MediaBrowser.Controller/Entities/CollectionFolder.cs b/MediaBrowser.Controller/Entities/CollectionFolder.cs index 68dd055f3..ebc55ca8a 100644 --- a/MediaBrowser.Controller/Entities/CollectionFolder.cs +++ b/MediaBrowser.Controller/Entities/CollectionFolder.cs @@ -264,6 +264,7 @@ namespace MediaBrowser.Controller.Entities /// Our children are actually just references to the ones in the physical root... /// /// The linked children. + [IgnoreDataMember] public override List LinkedChildren { get { return GetLinkedChildrenInternal(); } diff --git a/MediaBrowser.Controller/Entities/TV/Episode.cs b/MediaBrowser.Controller/Entities/TV/Episode.cs index 737257898..e6ebcb7fd 100644 --- a/MediaBrowser.Controller/Entities/TV/Episode.cs +++ b/MediaBrowser.Controller/Entities/TV/Episode.cs @@ -193,9 +193,10 @@ namespace MediaBrowser.Controller.Entities.TV { return "Season " + ParentIndexNumber.Value.ToString(CultureInfo.InvariantCulture); } + return "Season Unknown"; } - return season == null ? SeasonName : season.Name; + return season.Name; } public string FindSeriesName() diff --git a/MediaBrowser.Controller/Entities/Video.cs b/MediaBrowser.Controller/Entities/Video.cs index 2dd134334..7ba59df4f 100644 --- a/MediaBrowser.Controller/Entities/Video.cs +++ b/MediaBrowser.Controller/Entities/Video.cs @@ -44,6 +44,15 @@ namespace MediaBrowser.Controller.Entities } } + [IgnoreDataMember] + public override bool SupportsPositionTicksResume + { + get + { + return true; + } + } + [IgnoreDataMember] protected override bool SupportsIsInMixedFolderDetection { diff --git a/MediaBrowser.Controller/LiveTv/LiveTvAudioRecording.cs b/MediaBrowser.Controller/LiveTv/LiveTvAudioRecording.cs index 88e5b2802..e67fc5759 100644 --- a/MediaBrowser.Controller/LiveTv/LiveTvAudioRecording.cs +++ b/MediaBrowser.Controller/LiveTv/LiveTvAudioRecording.cs @@ -46,6 +46,15 @@ namespace MediaBrowser.Controller.LiveTv set { } } + [IgnoreDataMember] + public override bool SupportsPositionTicksResume + { + get + { + return true; + } + } + /// /// Gets a value indicating whether this instance is owned item. /// diff --git a/MediaBrowser.Controller/LiveTv/LiveTvChannel.cs b/MediaBrowser.Controller/LiveTv/LiveTvChannel.cs index f568ae6ae..d164b5e0d 100644 --- a/MediaBrowser.Controller/LiveTv/LiveTvChannel.cs +++ b/MediaBrowser.Controller/LiveTv/LiveTvChannel.cs @@ -38,6 +38,15 @@ namespace MediaBrowser.Controller.LiveTv } } + [IgnoreDataMember] + public override bool SupportsPositionTicksResume + { + get + { + return false; + } + } + [IgnoreDataMember] public override SourceType SourceType { diff --git a/MediaBrowser.Controller/MediaBrowser.Controller.csproj b/MediaBrowser.Controller/MediaBrowser.Controller.csproj index 2f96088ab..28229f8a7 100644 --- a/MediaBrowser.Controller/MediaBrowser.Controller.csproj +++ b/MediaBrowser.Controller/MediaBrowser.Controller.csproj @@ -96,6 +96,7 @@ + diff --git a/MediaBrowser.Providers/Books/AudioBookMetadataService.cs b/MediaBrowser.Providers/Books/AudioBookMetadataService.cs new file mode 100644 index 000000000..696619a8c --- /dev/null +++ b/MediaBrowser.Providers/Books/AudioBookMetadataService.cs @@ -0,0 +1,41 @@ +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.Providers; +using MediaBrowser.Model.Entities; +using MediaBrowser.Model.Logging; +using MediaBrowser.Providers.Manager; +using System.Collections.Generic; +using System.Linq; +using MediaBrowser.Common.IO; +using MediaBrowser.Controller.Entities.Audio; +using MediaBrowser.Controller.IO; +using MediaBrowser.Model.IO; + +namespace MediaBrowser.Providers.Books +{ + public class AudioBookMetadataService : MetadataService + { + protected override void MergeData(MetadataResult source, MetadataResult target, List lockedFields, bool replaceData, bool mergeMetadataSettings) + { + ProviderUtils.MergeBaseItemData(source, target, lockedFields, replaceData, mergeMetadataSettings); + + var sourceItem = source.Item; + var targetItem = target.Item; + + if (replaceData || targetItem.Artists.Count == 0) + { + targetItem.Artists = sourceItem.Artists.ToList(); + } + + if (replaceData || string.IsNullOrEmpty(targetItem.Album)) + { + targetItem.Album = sourceItem.Album; + } + } + + public AudioBookMetadataService(IServerConfigurationManager serverConfigurationManager, ILogger logger, IProviderManager providerManager, IFileSystem fileSystem, IUserDataManager userDataManager, ILibraryManager libraryManager) : base(serverConfigurationManager, logger, providerManager, fileSystem, userDataManager, libraryManager) + { + } + } +} diff --git a/MediaBrowser.Providers/Books/AudioPodcastMetadataService.cs b/MediaBrowser.Providers/Books/AudioPodcastMetadataService.cs new file mode 100644 index 000000000..86b2cf1b1 --- /dev/null +++ b/MediaBrowser.Providers/Books/AudioPodcastMetadataService.cs @@ -0,0 +1,41 @@ +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.Providers; +using MediaBrowser.Model.Entities; +using MediaBrowser.Model.Logging; +using MediaBrowser.Providers.Manager; +using System.Collections.Generic; +using System.Linq; +using MediaBrowser.Common.IO; +using MediaBrowser.Controller.Entities.Audio; +using MediaBrowser.Controller.IO; +using MediaBrowser.Model.IO; + +namespace MediaBrowser.Providers.Books +{ + public class AudioPodcastMetadataService : MetadataService + { + protected override void MergeData(MetadataResult source, MetadataResult target, List lockedFields, bool replaceData, bool mergeMetadataSettings) + { + ProviderUtils.MergeBaseItemData(source, target, lockedFields, replaceData, mergeMetadataSettings); + + var sourceItem = source.Item; + var targetItem = target.Item; + + if (replaceData || targetItem.Artists.Count == 0) + { + targetItem.Artists = sourceItem.Artists.ToList(); + } + + if (replaceData || string.IsNullOrEmpty(targetItem.Album)) + { + targetItem.Album = sourceItem.Album; + } + } + + public AudioPodcastMetadataService(IServerConfigurationManager serverConfigurationManager, ILogger logger, IProviderManager providerManager, IFileSystem fileSystem, IUserDataManager userDataManager, ILibraryManager libraryManager) : base(serverConfigurationManager, logger, providerManager, fileSystem, userDataManager, libraryManager) + { + } + } +} diff --git a/MediaBrowser.Providers/MediaBrowser.Providers.csproj b/MediaBrowser.Providers/MediaBrowser.Providers.csproj index dcb21612f..fe554545f 100644 --- a/MediaBrowser.Providers/MediaBrowser.Providers.csproj +++ b/MediaBrowser.Providers/MediaBrowser.Providers.csproj @@ -46,6 +46,8 @@ Properties\SharedVersion.cs + + diff --git a/MediaBrowser.WebDashboard/MediaBrowser.WebDashboard.csproj b/MediaBrowser.WebDashboard/MediaBrowser.WebDashboard.csproj index 3cf7e54c0..be5db5a0e 100644 --- a/MediaBrowser.WebDashboard/MediaBrowser.WebDashboard.csproj +++ b/MediaBrowser.WebDashboard/MediaBrowser.WebDashboard.csproj @@ -1400,9 +1400,6 @@ PreserveNewest - - PreserveNewest - PreserveNewest -- cgit v1.2.3 From c2d0fd99852aae31379b89591b538d075743362f Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Mon, 12 Dec 2016 03:53:25 -0500 Subject: update season queries --- .../Activity/ActivityRepository.cs | 11 ++-- .../Data/SqliteItemRepository.cs | 66 +++++++++++----------- .../Security/AuthenticationRepository.cs | 12 ++-- Emby.Server.Implementations/TV/TVSeriesManager.cs | 23 ++++---- MediaBrowser.Controller/Entities/TV/Series.cs | 4 +- src/Emby.Server/project.json | 19 +++++++ 6 files changed, 75 insertions(+), 60 deletions(-) (limited to 'Emby.Server.Implementations/Activity/ActivityRepository.cs') diff --git a/Emby.Server.Implementations/Activity/ActivityRepository.cs b/Emby.Server.Implementations/Activity/ActivityRepository.cs index fda8b949b..7ac0e680c 100644 --- a/Emby.Server.Implementations/Activity/ActivityRepository.cs +++ b/Emby.Server.Implementations/Activity/ActivityRepository.cs @@ -85,7 +85,7 @@ namespace Emby.Server.Implementations.Activity using (var connection = CreateConnection(true)) { var list = new List(); - int totalRecordCount = 0; + var result = new QueryResult(); var commandText = BaseActivitySelectText; var whereClauses = new List(); @@ -151,15 +151,12 @@ namespace Emby.Server.Implementations.Activity statement.TryBind("@DateCreated", minDate.Value.ToDateTimeParamValue()); } - totalRecordCount = statement.ExecuteQuery().SelectScalarInt().First(); + result.TotalRecordCount = statement.ExecuteQuery().SelectScalarInt().First(); } }, ReadTransactionMode); - return new QueryResult() - { - Items = list.ToArray(), - TotalRecordCount = totalRecordCount - }; + result.Items = list.ToArray(); + return result; } } } diff --git a/Emby.Server.Implementations/Data/SqliteItemRepository.cs b/Emby.Server.Implementations/Data/SqliteItemRepository.cs index 768f5b5a0..1f0f5a521 100644 --- a/Emby.Server.Implementations/Data/SqliteItemRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteItemRepository.cs @@ -343,6 +343,9 @@ namespace Emby.Server.Implementations.Data // series "create index if not exists idx_TypeSeriesPresentationUniqueKey1 on TypedBaseItems(Type,SeriesPresentationUniqueKey,PresentationUniqueKey,SortName)", + // series next up + "create index if not exists idx_SeriesPresentationUniqueKey on TypedBaseItems(SeriesPresentationUniqueKey)", + // live tv programs "create index if not exists idx_TypeTopParentIdStartDate on TypedBaseItems(Type,TopParentId,StartDate)", @@ -2263,6 +2266,10 @@ namespace Emby.Server.Implementations.Data { return true; } + if (sortingFields.Contains(ItemSortBy.SeriesDatePlayed, StringComparer.OrdinalIgnoreCase)) + { + return true; + } if (query.IsFavoriteOrLiked.HasValue) { @@ -2656,7 +2663,7 @@ namespace Emby.Server.Implementations.Data } } - var totalRecordCount = 0; + var result = new QueryResult(); var isReturningZeroItems = query.Limit.HasValue && query.Limit <= 0; var statementTexts = new List(); @@ -2714,23 +2721,23 @@ namespace Emby.Server.Implementations.Data } } } + } - if (query.EnableTotalRecordCount) + if (query.EnableTotalRecordCount) + { + using (var statement = statements[statements.Count - 1]) { - using (var statement = statements[statements.Count - 1]) + if (EnableJoinUserData(query)) { - if (EnableJoinUserData(query)) - { - statement.TryBind("@UserId", query.User.Id); - } + statement.TryBind("@UserId", query.User.Id); + } - BindSimilarParams(query, statement); + BindSimilarParams(query, statement); - // Running this again will bind the params - GetWhereClauses(query, statement); + // Running this again will bind the params + GetWhereClauses(query, statement); - totalRecordCount = statement.ExecuteQuery().SelectScalarInt().First(); - } + result.TotalRecordCount = statement.ExecuteQuery().SelectScalarInt().First(); } } @@ -2738,11 +2745,8 @@ namespace Emby.Server.Implementations.Data LogQueryTime("GetItems", commandText, now); - return new QueryResult() - { - Items = list.ToArray(), - TotalRecordCount = totalRecordCount - }; + result.Items = list.ToArray(); + return result; } } } @@ -2855,7 +2859,7 @@ namespace Emby.Server.Implementations.Data } if (string.Equals(name, ItemSortBy.SeriesDatePlayed, StringComparison.OrdinalIgnoreCase)) { - return new Tuple("(Select MAX(LastPlayedDate) from TypedBaseItems B" + GetJoinUserDataText(query) + " where B.Guid in (Select ItemId from AncestorIds where AncestorId in (select guid from typedbaseitems c where C.Type = 'MediaBrowser.Controller.Entities.TV.Series' And C.Guid in (Select AncestorId from AncestorIds where ItemId=A.Guid))))", false); + return new Tuple("(Select MAX(LastPlayedDate) from TypedBaseItems B" + GetJoinUserDataText(query) + " where B.SeriesPresentationUniqueKey=A.PresentationUniqueKey)", false); } return new Tuple(name, false); @@ -3094,7 +3098,7 @@ namespace Emby.Server.Implementations.Data { using (var connection = CreateConnection(true)) { - var totalRecordCount = 0; + var result = new QueryResult(); connection.RunInTransaction(db => { @@ -3136,7 +3140,7 @@ namespace Emby.Server.Implementations.Data // Running this again will bind the params GetWhereClauses(query, statement); - totalRecordCount = statement.ExecuteQuery().SelectScalarInt().First(); + result.TotalRecordCount = statement.ExecuteQuery().SelectScalarInt().First(); } } @@ -3144,11 +3148,8 @@ namespace Emby.Server.Implementations.Data LogQueryTime("GetItemIds", commandText, now); - return new QueryResult() - { - Items = list.ToArray(), - TotalRecordCount = totalRecordCount - }; + result.Items = list.ToArray(); + return result; } } } @@ -5026,7 +5027,7 @@ namespace Emby.Server.Implementations.Data var isReturningZeroItems = query.Limit.HasValue && query.Limit <= 0; var list = new List>(); - var count = 0; + var result = new QueryResult>(); var statementTexts = new List(); if (!isReturningZeroItems) @@ -5106,7 +5107,7 @@ namespace Emby.Server.Implementations.Data GetWhereClauses(innerQuery, statement); GetWhereClauses(outerQuery, statement); - count = statement.ExecuteQuery().SelectScalarInt().First(); + result.TotalRecordCount = statement.ExecuteQuery().SelectScalarInt().First(); LogQueryTime("GetItemValues", commandText, now); } @@ -5115,16 +5116,13 @@ namespace Emby.Server.Implementations.Data } } - if (count == 0) + if (result.TotalRecordCount == 0) { - count = list.Count; + result.TotalRecordCount = list.Count; } + result.Items = list.ToArray(); - return new QueryResult> - { - Items = list.ToArray(), - TotalRecordCount = count - }; + return result; } private ItemCounts GetItemCounts(IReadOnlyList reader, int countStartColumn, List typesToCount) diff --git a/Emby.Server.Implementations/Security/AuthenticationRepository.cs b/Emby.Server.Implementations/Security/AuthenticationRepository.cs index a136701da..392db6935 100644 --- a/Emby.Server.Implementations/Security/AuthenticationRepository.cs +++ b/Emby.Server.Implementations/Security/AuthenticationRepository.cs @@ -201,12 +201,13 @@ namespace Emby.Server.Implementations.Security } var list = new List(); - int totalRecordCount = 0; using (WriteLock.Read()) { using (var connection = CreateConnection(true)) { + var result = new QueryResult(); + connection.RunInTransaction(db => { var statementTexts = new List(); @@ -229,7 +230,7 @@ namespace Emby.Server.Implementations.Security { BindAuthenticationQueryParams(query, totalCountStatement); - totalRecordCount = totalCountStatement.ExecuteQuery() + result.TotalRecordCount = totalCountStatement.ExecuteQuery() .SelectScalarInt() .First(); } @@ -237,11 +238,8 @@ namespace Emby.Server.Implementations.Security }, ReadTransactionMode); - return new QueryResult() - { - Items = list.ToArray(), - TotalRecordCount = totalRecordCount - }; + result.Items = list.ToArray(); + return result; } } } diff --git a/Emby.Server.Implementations/TV/TVSeriesManager.cs b/Emby.Server.Implementations/TV/TVSeriesManager.cs index 88d224525..363fe5593 100644 --- a/Emby.Server.Implementations/TV/TVSeriesManager.cs +++ b/Emby.Server.Implementations/TV/TVSeriesManager.cs @@ -58,7 +58,8 @@ namespace Emby.Server.Implementations.TV var items = _libraryManager.GetItemList(new InternalItemsQuery(user) { IncludeItemTypes = new[] { typeof(Series).Name }, - SortOrder = SortOrder.Ascending, + SortBy = new[] { ItemSortBy.SeriesDatePlayed }, + SortOrder = SortOrder.Descending, PresentationUniqueKey = presentationUniqueKey, Limit = limit, ParentId = parentIdGuid, @@ -71,7 +72,7 @@ namespace Emby.Server.Implementations.TV } } - }).Cast(); + }).Cast().Select(GetUniqueSeriesKey); // Avoid implicitly captured closure var episodes = GetNextUpEpisodes(request, user, items); @@ -109,19 +110,20 @@ namespace Emby.Server.Implementations.TV var items = _libraryManager.GetItemList(new InternalItemsQuery(user) { IncludeItemTypes = new[] { typeof(Series).Name }, - SortOrder = SortOrder.Ascending, + SortBy = new[] { ItemSortBy.SeriesDatePlayed }, + SortOrder = SortOrder.Descending, PresentationUniqueKey = presentationUniqueKey, Limit = limit, DtoOptions = new MediaBrowser.Controller.Dto.DtoOptions { Fields = new List { - + }, EnableImages = false } - }, parentsFolders.Cast().ToList()).Cast(); + }, parentsFolders.Cast().ToList()).Cast().Select(GetUniqueSeriesKey); // Avoid implicitly captured closure var episodes = GetNextUpEpisodes(request, user, items); @@ -129,15 +131,15 @@ namespace Emby.Server.Implementations.TV return GetResult(episodes, null, request); } - public IEnumerable GetNextUpEpisodes(NextUpQuery request, User user, IEnumerable series) + public IEnumerable GetNextUpEpisodes(NextUpQuery request, User user, IEnumerable seriesKeys) { // Avoid implicitly captured closure var currentUser = user; - var allNextUp = series - .Select(i => GetNextUp(GetUniqueSeriesKey(i), currentUser)) - // Include if an episode was found, and either the series is not unwatched or the specific series was requested - .OrderByDescending(i => i.Item1); + var allNextUp = seriesKeys + .Select(i => GetNextUp(i, currentUser)); + + //allNextUp = allNextUp.OrderByDescending(i => i.Item1); // If viewing all next up for all series, remove first episodes // But if that returns empty, keep those first episodes (avoid completely empty view) @@ -160,7 +162,6 @@ namespace Emby.Server.Implementations.TV return true; }) - .Take(request.Limit.HasValue ? (request.Limit.Value * 2) : int.MaxValue) .Select(i => i.Item2()) .Where(i => i != null) .Take(request.Limit ?? int.MaxValue); diff --git a/MediaBrowser.Controller/Entities/TV/Series.cs b/MediaBrowser.Controller/Entities/TV/Series.cs index 92cd20769..caebff16f 100644 --- a/MediaBrowser.Controller/Entities/TV/Series.cs +++ b/MediaBrowser.Controller/Entities/TV/Series.cs @@ -171,7 +171,9 @@ namespace MediaBrowser.Controller.Entities.TV } query.IsVirtualItem = false; query.Limit = 0; - return LibraryManager.GetItemsResult(query).TotalRecordCount; + var totalRecordCount = LibraryManager.GetItemsResult(query).TotalRecordCount; + + return totalRecordCount; } /// diff --git a/src/Emby.Server/project.json b/src/Emby.Server/project.json index 35ebdc213..5b64beae6 100644 --- a/src/Emby.Server/project.json +++ b/src/Emby.Server/project.json @@ -18,6 +18,25 @@ "SQLitePCLRaw.provider.sqlite3.netstandard11": "1.1.1-pre20161109081005" }, + "runtimes": { + "win7-x86": {}, + "win7-x64": {}, + "win8-x86": {}, + "win8-x64": {}, + "win8-arm": {}, + "win81-x86": {}, + "win81-x64": {}, + "win81-arm": {}, + "win10-x86": {}, + "win10-x64": {}, + "win10-arm": {}, + "win10-arm64": {}, + "osx.10.10-x64": {}, + "osx.10.11-x64": {}, + "osx.10.12-x64": {}, + "ubuntu.14.04-x64": {} + }, + "frameworks": { "netcoreapp1.1": { "imports": "dnxcore50", -- cgit v1.2.3 From e1b880a5a072764cabace79cd6d1d65315ec65e4 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 13 Dec 2016 02:36:30 -0500 Subject: update series queries --- .../Activity/ActivityRepository.cs | 15 +- .../Data/BaseSqliteRepository.cs | 2 + .../Data/SqliteItemRepository.cs | 261 +++++++++++---------- .../Data/SqliteUserDataRepository.cs | 12 +- Emby.Server.Implementations/Dto/DtoService.cs | 105 ++++++--- .../FileOrganization/EpisodeFileOrganizer.cs | 6 + .../SocketSharp/WebSocketSharpListener.cs | 3 +- .../Library/LibraryManager.cs | 58 +++-- .../Library/UserDataManager.cs | 7 +- .../Notifications/SqliteNotificationsRepository.cs | 20 +- .../Security/AuthenticationRepository.cs | 12 +- MediaBrowser.Api/BaseApiService.cs | 32 ++- MediaBrowser.Api/ItemLookupService.cs | 15 +- MediaBrowser.Controller/Entities/BaseItem.cs | 3 +- .../Entities/CollectionFolder.cs | 68 ++++-- MediaBrowser.Controller/Entities/Folder.cs | 36 +-- MediaBrowser.Controller/Entities/IHasUserData.cs | 6 +- MediaBrowser.Controller/Library/ILibraryManager.cs | 2 + .../Library/IUserDataManager.cs | 6 +- MediaBrowser.Model/Querying/ItemFields.cs | 2 + RSSDP/SsdpDevicePublisherBase.cs | 8 +- 21 files changed, 418 insertions(+), 261 deletions(-) (limited to 'Emby.Server.Implementations/Activity/ActivityRepository.cs') diff --git a/Emby.Server.Implementations/Activity/ActivityRepository.cs b/Emby.Server.Implementations/Activity/ActivityRepository.cs index 7ac0e680c..5f6518a14 100644 --- a/Emby.Server.Implementations/Activity/ActivityRepository.cs +++ b/Emby.Server.Implementations/Activity/ActivityRepository.cs @@ -84,9 +84,6 @@ namespace Emby.Server.Implementations.Activity { using (var connection = CreateConnection(true)) { - var list = new List(); - var result = new QueryResult(); - var commandText = BaseActivitySelectText; var whereClauses = new List(); @@ -127,8 +124,11 @@ namespace Emby.Server.Implementations.Activity statementTexts.Add(commandText); statementTexts.Add("select count (Id) from ActivityLogEntries" + whereTextWithoutPaging); - connection.RunInTransaction(db => + return connection.RunInTransaction(db => { + var list = new List(); + var result = new QueryResult(); + var statements = PrepareAllSafe(db, string.Join(";", statementTexts.ToArray())).ToList(); using (var statement = statements[0]) @@ -153,10 +153,11 @@ namespace Emby.Server.Implementations.Activity result.TotalRecordCount = statement.ExecuteQuery().SelectScalarInt().First(); } - }, ReadTransactionMode); - result.Items = list.ToArray(); - return result; + result.Items = list.ToArray(); + return result; + + }, ReadTransactionMode); } } } diff --git a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs index 9e60a43aa..2fc721f83 100644 --- a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs +++ b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs @@ -43,6 +43,7 @@ namespace Emby.Server.Implementations.Data //CheckOk(rc); rc = raw.sqlite3_config(raw.SQLITE_CONFIG_MULTITHREAD, 1); + //rc = raw.sqlite3_config(raw.SQLITE_CONFIG_SERIALIZED, 1); //CheckOk(rc); rc = raw.sqlite3_enable_shared_cache(1); @@ -94,6 +95,7 @@ namespace Emby.Server.Implementations.Data var queries = new List { //"PRAGMA cache size=-10000" + //"PRAGMA read_uncommitted = true" }; if (EnableTempStoreMemory) diff --git a/Emby.Server.Implementations/Data/SqliteItemRepository.cs b/Emby.Server.Implementations/Data/SqliteItemRepository.cs index 42cbf1965..803ebeca0 100644 --- a/Emby.Server.Implementations/Data/SqliteItemRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteItemRepository.cs @@ -328,6 +328,8 @@ namespace Emby.Server.Implementations.Data "drop table if exists Images", "drop index if exists idx_Images", "drop index if exists idx_TypeSeriesPresentationUniqueKey", + "drop index if exists idx_SeriesPresentationUniqueKey", + "drop index if exists idx_TypeSeriesPresentationUniqueKey2", "create index if not exists idx_PathTypedBaseItems on TypedBaseItems(Path)", "create index if not exists idx_ParentIdTypedBaseItems on TypedBaseItems(ParentId)", @@ -343,8 +345,9 @@ namespace Emby.Server.Implementations.Data // series "create index if not exists idx_TypeSeriesPresentationUniqueKey1 on TypedBaseItems(Type,SeriesPresentationUniqueKey,PresentationUniqueKey,SortName)", - // series next up - "create index if not exists idx_SeriesPresentationUniqueKey on TypedBaseItems(SeriesPresentationUniqueKey)", + // series counts + // seriesdateplayed sort order + "create index if not exists idx_TypeSeriesPresentationUniqueKey3 on TypedBaseItems(SeriesPresentationUniqueKey,Type,IsFolder,IsVirtualItem)", // live tv programs "create index if not exists idx_TypeTopParentIdStartDate on TypedBaseItems(Type,TopParentId,StartDate)", @@ -2079,25 +2082,29 @@ namespace Emby.Server.Implementations.Data throw new ArgumentNullException("id"); } - var list = new List(); - using (WriteLock.Read()) { using (var connection = CreateConnection(true)) { - using (var statement = PrepareStatementSafe(connection, "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId order by ChapterIndex asc")) + return connection.RunInTransaction(db => { - statement.TryBind("@ItemId", id); + var list = new List(); - foreach (var row in statement.ExecuteQuery()) + using (var statement = PrepareStatementSafe(db, "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId order by ChapterIndex asc")) { - list.Add(GetChapter(row)); + statement.TryBind("@ItemId", id); + + foreach (var row in statement.ExecuteQuery()) + { + list.Add(GetChapter(row)); + } } - } + + return list; + + }, ReadTransactionMode); } } - - return list; } /// @@ -2470,32 +2477,33 @@ namespace Emby.Server.Implementations.Data //commandText += GetGroupBy(query); - int count = 0; - using (WriteLock.Read()) { using (var connection = CreateConnection(true)) { - using (var statement = PrepareStatementSafe(connection, commandText)) + return connection.RunInTransaction(db => { - if (EnableJoinUserData(query)) + using (var statement = PrepareStatementSafe(db, commandText)) { - statement.TryBind("@UserId", query.User.Id); - } + if (EnableJoinUserData(query)) + { + statement.TryBind("@UserId", query.User.Id); + } - BindSimilarParams(query, statement); + BindSimilarParams(query, statement); - // Running this again will bind the params - GetWhereClauses(query, statement); + // Running this again will bind the params + GetWhereClauses(query, statement); - count = statement.ExecuteQuery().SelectScalarInt().First(); - } + var count = statement.ExecuteQuery().SelectScalarInt().First(); + LogQueryTime("GetCount", commandText, now); + return count; + } + + }, ReadTransactionMode); } - LogQueryTime("GetCount", commandText, now); } - - return count; } public List GetItemList(InternalItemsQuery query) @@ -2511,8 +2519,6 @@ namespace Emby.Server.Implementations.Data var now = DateTime.UtcNow; - var list = new List(); - // Hack for right now since we currently don't support filtering out these duplicates within a query if (query.Limit.HasValue && query.EnableGroupByMetadataKey) { @@ -2553,53 +2559,59 @@ namespace Emby.Server.Implementations.Data { using (var connection = CreateConnection(true)) { - using (var statement = PrepareStatementSafe(connection, commandText)) + return connection.RunInTransaction(db => { - if (EnableJoinUserData(query)) + var list = new List(); + + using (var statement = PrepareStatementSafe(db, commandText)) { - statement.TryBind("@UserId", query.User.Id); - } + if (EnableJoinUserData(query)) + { + statement.TryBind("@UserId", query.User.Id); + } - BindSimilarParams(query, statement); + BindSimilarParams(query, statement); - // Running this again will bind the params - GetWhereClauses(query, statement); + // Running this again will bind the params + GetWhereClauses(query, statement); - foreach (var row in statement.ExecuteQuery()) - { - var item = GetItem(row, query); - if (item != null) + foreach (var row in statement.ExecuteQuery()) { - list.Add(item); + var item = GetItem(row, query); + if (item != null) + { + list.Add(item); + } } } - } - } - LogQueryTime("GetItemList", commandText, now); - } + // Hack for right now since we currently don't support filtering out these duplicates within a query + if (query.EnableGroupByMetadataKey) + { + var limit = query.Limit ?? int.MaxValue; + limit -= 4; + var newList = new List(); - // Hack for right now since we currently don't support filtering out these duplicates within a query - if (query.EnableGroupByMetadataKey) - { - var limit = query.Limit ?? int.MaxValue; - limit -= 4; - var newList = new List(); + foreach (var item in list) + { + AddItem(newList, item); - foreach (var item in list) - { - AddItem(newList, item); + if (newList.Count >= limit) + { + break; + } + } - if (newList.Count >= limit) - { - break; - } - } + list = newList; + } - list = newList; - } + LogQueryTime("GetItemList", commandText, now); - return list; + return list; + + }, ReadTransactionMode); + } + } } private void AddItem(List items, BaseItem newItem) @@ -2637,7 +2649,7 @@ namespace Emby.Server.Implementations.Data var slowThreshold = 1000; #if DEBUG - slowThreshold = 50; + slowThreshold = 2; #endif if (elapsed >= slowThreshold) @@ -2718,7 +2730,6 @@ namespace Emby.Server.Implementations.Data } } - var result = new QueryResult(); var isReturningZeroItems = query.Limit.HasValue && query.Limit <= 0; var statementTexts = new List(); @@ -2748,8 +2759,9 @@ namespace Emby.Server.Implementations.Data { using (var connection = CreateConnection(true)) { - connection.RunInTransaction(db => + return connection.RunInTransaction(db => { + var result = new QueryResult(); var statements = PrepareAllSafe(db, string.Join(";", statementTexts.ToArray())) .ToList(); @@ -2796,12 +2808,12 @@ namespace Emby.Server.Implementations.Data } } - }, ReadTransactionMode); + LogQueryTime("GetItems", commandText, now); - LogQueryTime("GetItems", commandText, now); + result.Items = list.ToArray(); + return result; - result.Items = list.ToArray(); - return result; + }, ReadTransactionMode); } } } @@ -2962,34 +2974,38 @@ namespace Emby.Server.Implementations.Data } } - var list = new List(); - using (WriteLock.Read()) { using (var connection = CreateConnection(true)) { - using (var statement = PrepareStatementSafe(connection, commandText)) + return connection.RunInTransaction(db => { - if (EnableJoinUserData(query)) + var list = new List(); + + using (var statement = PrepareStatementSafe(db, commandText)) { - statement.TryBind("@UserId", query.User.Id); - } + if (EnableJoinUserData(query)) + { + statement.TryBind("@UserId", query.User.Id); + } - BindSimilarParams(query, statement); + BindSimilarParams(query, statement); - // Running this again will bind the params - GetWhereClauses(query, statement); + // Running this again will bind the params + GetWhereClauses(query, statement); - foreach (var row in statement.ExecuteQuery()) - { - list.Add(row[0].ReadGuid()); + foreach (var row in statement.ExecuteQuery()) + { + list.Add(row[0].ReadGuid()); + } } - } - } - LogQueryTime("GetItemList", commandText, now); + LogQueryTime("GetItemList", commandText, now); - return list; + return list; + + }, ReadTransactionMode); + } } } @@ -3153,10 +3169,10 @@ namespace Emby.Server.Implementations.Data { using (var connection = CreateConnection(true)) { - var result = new QueryResult(); - - connection.RunInTransaction(db => + return connection.RunInTransaction(db => { + var result = new QueryResult(); + var statements = PrepareAllSafe(db, string.Join(";", statementTexts.ToArray())) .ToList(); @@ -3199,12 +3215,12 @@ namespace Emby.Server.Implementations.Data } } - }, ReadTransactionMode); + LogQueryTime("GetItemIds", commandText, now); - LogQueryTime("GetItemIds", commandText, now); + result.Items = list.ToArray(); + return result; - result.Items = list.ToArray(); - return result; + }, ReadTransactionMode); } } } @@ -4653,13 +4669,13 @@ namespace Emby.Server.Implementations.Data commandText += " order by ListOrder"; - var list = new List(); using (WriteLock.Read()) { using (var connection = CreateConnection(true)) { - connection.RunInTransaction(db => + return connection.RunInTransaction(db => { + var list = new List(); using (var statement = PrepareStatementSafe(db, commandText)) { // Run this again to bind the params @@ -4670,9 +4686,9 @@ namespace Emby.Server.Implementations.Data list.Add(row.GetString(0)); } } + return list; }, ReadTransactionMode); } - return list; } } @@ -4696,14 +4712,14 @@ namespace Emby.Server.Implementations.Data commandText += " order by ListOrder"; - var list = new List(); - using (WriteLock.Read()) { using (var connection = CreateConnection(true)) { - connection.RunInTransaction(db => + return connection.RunInTransaction(db => { + var list = new List(); + using (var statement = PrepareStatementSafe(db, commandText)) { // Run this again to bind the params @@ -4714,11 +4730,11 @@ namespace Emby.Server.Implementations.Data list.Add(GetPerson(row)); } } + + return list; }, ReadTransactionMode); } } - - return list; } private List GetPeopleWhereClauses(InternalPeopleQuery query, IStatement statement) @@ -4899,8 +4915,6 @@ namespace Emby.Server.Implementations.Data ("Type=" + itemValueTypes[0].ToString(CultureInfo.InvariantCulture)) : ("Type in (" + string.Join(",", itemValueTypes.Select(i => i.ToString(CultureInfo.InvariantCulture)).ToArray()) + ")"); - var list = new List(); - var commandText = "Select Value From ItemValues where " + typeClause; if (withItemTypes.Count > 0) @@ -4920,8 +4934,10 @@ namespace Emby.Server.Implementations.Data { using (var connection = CreateConnection(true)) { - connection.RunInTransaction(db => + return connection.RunInTransaction(db => { + var list = new List(); + using (var statement = PrepareStatementSafe(db, commandText)) { foreach (var row in statement.ExecuteQuery()) @@ -4932,12 +4948,13 @@ namespace Emby.Server.Implementations.Data } } } + + LogQueryTime("GetItemValueNames", commandText, now); + + return list; }, ReadTransactionMode); } } - LogQueryTime("GetItemValueNames", commandText, now); - - return list; } private QueryResult> GetItemValues(InternalItemsQuery query, int[] itemValueTypes, string returnType) @@ -5081,9 +5098,6 @@ namespace Emby.Server.Implementations.Data var isReturningZeroItems = query.Limit.HasValue && query.Limit <= 0; - var list = new List>(); - var result = new QueryResult>(); - var statementTexts = new List(); if (!isReturningZeroItems) { @@ -5102,8 +5116,11 @@ namespace Emby.Server.Implementations.Data { using (var connection = CreateConnection(true)) { - connection.RunInTransaction(db => + return connection.RunInTransaction(db => { + var list = new List>(); + var result = new QueryResult>(); + var statements = PrepareAllSafe(db, string.Join(";", statementTexts.ToArray())).ToList(); if (!isReturningZeroItems) @@ -5167,17 +5184,18 @@ namespace Emby.Server.Implementations.Data LogQueryTime("GetItemValues", commandText, now); } } + + if (result.TotalRecordCount == 0) + { + result.TotalRecordCount = list.Count; + } + result.Items = list.ToArray(); + + return result; + }, ReadTransactionMode); } } - - if (result.TotalRecordCount == 0) - { - result.TotalRecordCount = list.Count; - } - result.Items = list.ToArray(); - - return result; } private ItemCounts GetItemCounts(IReadOnlyList reader, int countStartColumn, List typesToCount) @@ -5390,8 +5408,6 @@ namespace Emby.Server.Implementations.Data throw new ArgumentNullException("query"); } - var list = new List(); - var cmdText = "select " + string.Join(",", _mediaStreamSaveColumns) + " from mediastreams where"; cmdText += " ItemId=@ItemId"; @@ -5412,8 +5428,10 @@ namespace Emby.Server.Implementations.Data { using (var connection = CreateConnection(true)) { - connection.RunInTransaction(db => + return connection.RunInTransaction(db => { + var list = new List(); + using (var statement = PrepareStatementSafe(db, cmdText)) { statement.TryBind("@ItemId", query.ItemId.ToGuidParamValue()); @@ -5433,11 +5451,12 @@ namespace Emby.Server.Implementations.Data list.Add(GetMediaStream(row)); } } + + return list; + }, ReadTransactionMode); } } - - return list; } public async Task SaveMediaStreams(Guid id, List streams, CancellationToken cancellationToken) diff --git a/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs b/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs index 7afb5720e..7767ae892 100644 --- a/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs @@ -300,9 +300,7 @@ namespace Emby.Server.Implementations.Data { using (var connection = CreateConnection(true)) { - UserItemData result = null; - - connection.RunInTransaction(db => + return connection.RunInTransaction(db => { using (var statement = db.PrepareStatement("select key,userid,rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex from userdata where key =@Key and userId=@UserId")) { @@ -311,13 +309,13 @@ namespace Emby.Server.Implementations.Data foreach (var row in statement.ExecuteQuery()) { - result = ReadRow(row); - break; + return ReadRow(row); } } - }, ReadTransactionMode); - return result; + return null; + + }, ReadTransactionMode); } } } diff --git a/Emby.Server.Implementations/Dto/DtoService.cs b/Emby.Server.Implementations/Dto/DtoService.cs index 2b2c3e000..d0c473777 100644 --- a/Emby.Server.Implementations/Dto/DtoService.cs +++ b/Emby.Server.Implementations/Dto/DtoService.cs @@ -459,12 +459,21 @@ namespace Emby.Server.Implementations.Dto if (dtoOptions.EnableUserData) { - dto.UserData = await _userDataRepository.GetUserDataDto(item, dto, user).ConfigureAwait(false); + dto.UserData = await _userDataRepository.GetUserDataDto(item, dto, user, dtoOptions.Fields).ConfigureAwait(false); } if (!dto.ChildCount.HasValue && item.SourceType == SourceType.Library) { - dto.ChildCount = GetChildCount(folder, user); + // For these types we can try to optimize and assume these values will be equal + if (item is MusicAlbum || item is Season) + { + dto.ChildCount = dto.RecursiveItemCount; + } + + if (dtoOptions.Fields.Contains(ItemFields.ChildCount)) + { + dto.ChildCount = dto.ChildCount ?? GetChildCount(folder, user); + } } if (fields.Contains(ItemFields.CumulativeRunTimeTicks)) @@ -1151,28 +1160,29 @@ namespace Emby.Server.Implementations.Dto { dto.Artists = hasArtist.Artists; - var artistItems = _libraryManager.GetArtists(new InternalItemsQuery - { - EnableTotalRecordCount = false, - ItemIds = new[] { item.Id.ToString("N") } - }); - - dto.ArtistItems = artistItems.Items - .Select(i => - { - var artist = i.Item1; - return new NameIdPair - { - Name = artist.Name, - Id = artist.Id.ToString("N") - }; - }) - .ToList(); + //var artistItems = _libraryManager.GetArtists(new InternalItemsQuery + //{ + // EnableTotalRecordCount = false, + // ItemIds = new[] { item.Id.ToString("N") } + //}); + + //dto.ArtistItems = artistItems.Items + // .Select(i => + // { + // var artist = i.Item1; + // return new NameIdPair + // { + // Name = artist.Name, + // Id = artist.Id.ToString("N") + // }; + // }) + // .ToList(); // Include artists that are not in the database yet, e.g., just added via metadata editor - var foundArtists = artistItems.Items.Select(i => i.Item1.Name).ToList(); + //var foundArtists = artistItems.Items.Select(i => i.Item1.Name).ToList(); + dto.ArtistItems = new List(); dto.ArtistItems.AddRange(hasArtist.Artists - .Except(foundArtists, new DistinctNameComparer()) + //.Except(foundArtists, new DistinctNameComparer()) .Select(i => { // This should not be necessary but we're seeing some cases of it @@ -1201,23 +1211,48 @@ namespace Emby.Server.Implementations.Dto { dto.AlbumArtist = hasAlbumArtist.AlbumArtists.FirstOrDefault(); - var artistItems = _libraryManager.GetAlbumArtists(new InternalItemsQuery - { - EnableTotalRecordCount = false, - ItemIds = new[] { item.Id.ToString("N") } - }); - - dto.AlbumArtists = artistItems.Items + //var artistItems = _libraryManager.GetAlbumArtists(new InternalItemsQuery + //{ + // EnableTotalRecordCount = false, + // ItemIds = new[] { item.Id.ToString("N") } + //}); + + //dto.AlbumArtists = artistItems.Items + // .Select(i => + // { + // var artist = i.Item1; + // return new NameIdPair + // { + // Name = artist.Name, + // Id = artist.Id.ToString("N") + // }; + // }) + // .ToList(); + + dto.AlbumArtists = new List(); + dto.AlbumArtists.AddRange(hasAlbumArtist.AlbumArtists + //.Except(foundArtists, new DistinctNameComparer()) .Select(i => { - var artist = i.Item1; - return new NameIdPair + // This should not be necessary but we're seeing some cases of it + if (string.IsNullOrWhiteSpace(i)) { - Name = artist.Name, - Id = artist.Id.ToString("N") - }; - }) - .ToList(); + return null; + } + + var artist = _libraryManager.GetArtist(i); + if (artist != null) + { + return new NameIdPair + { + Name = artist.Name, + Id = artist.Id.ToString("N") + }; + } + + return null; + + }).Where(i => i != null)); } // Add video info diff --git a/Emby.Server.Implementations/FileOrganization/EpisodeFileOrganizer.cs b/Emby.Server.Implementations/FileOrganization/EpisodeFileOrganizer.cs index cc2dcb6fd..5bb21d02a 100644 --- a/Emby.Server.Implementations/FileOrganization/EpisodeFileOrganizer.cs +++ b/Emby.Server.Implementations/FileOrganization/EpisodeFileOrganizer.cs @@ -519,6 +519,12 @@ namespace Emby.Server.Implementations.FileOrganization private void PerformFileSorting(TvFileOrganizationOptions options, FileOrganizationResult result) { + // We should probably handle this earlier so that we never even make it this far + if (string.Equals(result.OriginalPath, result.TargetPath, StringComparison.OrdinalIgnoreCase)) + { + return; + } + _libraryMonitor.ReportFileSystemChangeBeginning(result.TargetPath); _fileSystem.CreateDirectory(Path.GetDirectoryName(result.TargetPath)); diff --git a/Emby.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs b/Emby.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs index 94cc383a7..4606d0e31 100644 --- a/Emby.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs +++ b/Emby.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs @@ -76,7 +76,8 @@ namespace Emby.Server.Implementations.HttpServer.SocketSharp private void ProcessContext(HttpListenerContext context) { - Task.Factory.StartNew(() => InitTask(context)); + //Task.Factory.StartNew(() => InitTask(context), TaskCreationOptions.DenyChildAttach | TaskCreationOptions.PreferFairness); + Task.Run(() => InitTask(context)); } private Task InitTask(HttpListenerContext context) diff --git a/Emby.Server.Implementations/Library/LibraryManager.cs b/Emby.Server.Implementations/Library/LibraryManager.cs index ad91988e5..1ff61286f 100644 --- a/Emby.Server.Implementations/Library/LibraryManager.cs +++ b/Emby.Server.Implementations/Library/LibraryManager.cs @@ -817,7 +817,31 @@ namespace Emby.Server.Implementations.Library return _userRootFolder; } - + + public Guid? FindIdByPath(string path, bool? isFolder) + { + // If this returns multiple items it could be tricky figuring out which one is correct. + // In most cases, the newest one will be and the others obsolete but not yet cleaned up + + var query = new InternalItemsQuery + { + Path = path, + IsFolder = isFolder, + SortBy = new[] { ItemSortBy.DateCreated }, + SortOrder = SortOrder.Descending, + Limit = 1 + }; + + var id = GetItemIds(query); + + if (id.Count == 0) + { + return null; + } + + return id[0]; + } + public BaseItem FindByPath(string path, bool? isFolder) { // If this returns multiple items it could be tricky figuring out which one is correct. @@ -1430,7 +1454,7 @@ namespace Emby.Server.Implementations.Library })) { // Optimize by querying against top level views - query.TopParentIds = parents.SelectMany(i => GetTopParentsForQuery(i, query.User)).Select(i => i.Id.ToString("N")).ToArray(); + query.TopParentIds = parents.SelectMany(i => GetTopParentIdsForQuery(i, query.User)).Select(i => i.ToString("N")).ToArray(); query.AncestorIds = new string[] { }; } } @@ -1489,7 +1513,7 @@ namespace Emby.Server.Implementations.Library })) { // Optimize by querying against top level views - query.TopParentIds = parents.SelectMany(i => GetTopParentsForQuery(i, query.User)).Select(i => i.Id.ToString("N")).ToArray(); + query.TopParentIds = parents.SelectMany(i => GetTopParentIdsForQuery(i, query.User)).Select(i => i.ToString("N")).ToArray(); } else { @@ -1515,11 +1539,11 @@ namespace Emby.Server.Implementations.Library }, CancellationToken.None).Result.ToList(); - query.TopParentIds = userViews.SelectMany(i => GetTopParentsForQuery(i, user)).Select(i => i.Id.ToString("N")).ToArray(); + query.TopParentIds = userViews.SelectMany(i => GetTopParentIdsForQuery(i, user)).Select(i => i.ToString("N")).ToArray(); } } - private IEnumerable GetTopParentsForQuery(BaseItem item, User user) + private IEnumerable GetTopParentIdsForQuery(BaseItem item, User user) { var view = item as UserView; @@ -1527,7 +1551,7 @@ namespace Emby.Server.Implementations.Library { if (string.Equals(view.ViewType, CollectionType.LiveTv)) { - return new[] { view }; + return new[] { view.Id }; } if (string.Equals(view.ViewType, CollectionType.Channels)) { @@ -1537,7 +1561,7 @@ namespace Emby.Server.Implementations.Library }, CancellationToken.None).Result; - return channelResult.Items; + return channelResult.Items.Select(i => i.Id); } // Translate view into folders @@ -1546,18 +1570,18 @@ namespace Emby.Server.Implementations.Library var displayParent = GetItemById(view.DisplayParentId); if (displayParent != null) { - return GetTopParentsForQuery(displayParent, user); + return GetTopParentIdsForQuery(displayParent, user); } - return new BaseItem[] { }; + return new Guid[] { }; } if (view.ParentId != Guid.Empty) { var displayParent = GetItemById(view.ParentId); if (displayParent != null) { - return GetTopParentsForQuery(displayParent, user); + return GetTopParentIdsForQuery(displayParent, user); } - return new BaseItem[] { }; + return new Guid[] { }; } // Handle grouping @@ -1568,23 +1592,23 @@ namespace Emby.Server.Implementations.Library .OfType() .Where(i => string.IsNullOrWhiteSpace(i.CollectionType) || string.Equals(i.CollectionType, view.ViewType, StringComparison.OrdinalIgnoreCase)) .Where(i => user.IsFolderGrouped(i.Id)) - .SelectMany(i => GetTopParentsForQuery(i, user)); + .SelectMany(i => GetTopParentIdsForQuery(i, user)); } - return new BaseItem[] { }; + return new Guid[] { }; } var collectionFolder = item as CollectionFolder; if (collectionFolder != null) { - return collectionFolder.GetPhysicalParents(); + return collectionFolder.PhysicalFolderIds; } - + var topParent = item.GetTopParent(); if (topParent != null) { - return new[] { topParent }; + return new[] { topParent.Id }; } - return new BaseItem[] { }; + return new Guid[] { }; } /// diff --git a/Emby.Server.Implementations/Library/UserDataManager.cs b/Emby.Server.Implementations/Library/UserDataManager.cs index f4a30fc00..5a14edf13 100644 --- a/Emby.Server.Implementations/Library/UserDataManager.cs +++ b/Emby.Server.Implementations/Library/UserDataManager.cs @@ -12,6 +12,7 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; +using MediaBrowser.Model.Querying; namespace Emby.Server.Implementations.Library { @@ -186,16 +187,16 @@ namespace Emby.Server.Implementations.Library var userData = GetUserData(user.Id, item); var dto = GetUserItemDataDto(userData); - await item.FillUserDataDtoValues(dto, userData, null, user).ConfigureAwait(false); + await item.FillUserDataDtoValues(dto, userData, null, user, new List()).ConfigureAwait(false); return dto; } - public async Task GetUserDataDto(IHasUserData item, BaseItemDto itemDto, User user) + public async Task GetUserDataDto(IHasUserData item, BaseItemDto itemDto, User user, List fields) { var userData = GetUserData(user.Id, item); var dto = GetUserItemDataDto(userData); - await item.FillUserDataDtoValues(dto, userData, itemDto, user).ConfigureAwait(false); + await item.FillUserDataDtoValues(dto, userData, itemDto, user, fields).ConfigureAwait(false); return dto; } diff --git a/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs b/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs index 43e19da65..f18278cb2 100644 --- a/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs +++ b/Emby.Server.Implementations/Notifications/SqliteNotificationsRepository.cs @@ -65,9 +65,9 @@ namespace Emby.Server.Implementations.Notifications var whereClause = " where " + string.Join(" And ", clauses.ToArray()); - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { - lock (WriteLock) + using (var connection = CreateConnection(true)) { result.TotalRecordCount = connection.Query("select count(Id) from Notifications" + whereClause, paramList.ToArray()).SelectScalarInt().First(); @@ -106,9 +106,9 @@ namespace Emby.Server.Implementations.Notifications { var result = new NotificationsSummary(); - using (var connection = CreateConnection(true)) + using (WriteLock.Read()) { - lock (WriteLock) + using (var connection = CreateConnection(true)) { using (var statement = connection.PrepareStatement("select Level from Notifications where UserId=@UserId and IsRead=@IsRead")) { @@ -223,9 +223,9 @@ namespace Emby.Server.Implementations.Notifications cancellationToken.ThrowIfCancellationRequested(); - using (var connection = CreateConnection()) + lock (WriteLock) { - lock (WriteLock) + using (var connection = CreateConnection()) { connection.RunInTransaction(conn => { @@ -286,9 +286,9 @@ namespace Emby.Server.Implementations.Notifications { cancellationToken.ThrowIfCancellationRequested(); - using (var connection = CreateConnection()) + using (WriteLock.Write()) { - lock (WriteLock) + using (var connection = CreateConnection()) { connection.RunInTransaction(conn => { @@ -308,9 +308,9 @@ namespace Emby.Server.Implementations.Notifications { cancellationToken.ThrowIfCancellationRequested(); - using (var connection = CreateConnection()) + using (WriteLock.Write()) { - lock (WriteLock) + using (var connection = CreateConnection()) { connection.RunInTransaction(conn => { diff --git a/Emby.Server.Implementations/Security/AuthenticationRepository.cs b/Emby.Server.Implementations/Security/AuthenticationRepository.cs index 392db6935..7199f4f4d 100644 --- a/Emby.Server.Implementations/Security/AuthenticationRepository.cs +++ b/Emby.Server.Implementations/Security/AuthenticationRepository.cs @@ -206,10 +206,10 @@ namespace Emby.Server.Implementations.Security { using (var connection = CreateConnection(true)) { - var result = new QueryResult(); - - connection.RunInTransaction(db => + return connection.RunInTransaction(db => { + var result = new QueryResult(); + var statementTexts = new List(); statementTexts.Add(commandText); statementTexts.Add("select count (Id) from AccessTokens" + whereTextWithoutPaging); @@ -236,10 +236,10 @@ namespace Emby.Server.Implementations.Security } } - }, ReadTransactionMode); + result.Items = list.ToArray(); + return result; - result.Items = list.ToArray(); - return result; + }, ReadTransactionMode); } } } diff --git a/MediaBrowser.Api/BaseApiService.cs b/MediaBrowser.Api/BaseApiService.cs index 73a2bedb9..7a8951396 100644 --- a/MediaBrowser.Api/BaseApiService.cs +++ b/MediaBrowser.Api/BaseApiService.cs @@ -121,7 +121,9 @@ namespace MediaBrowser.Api { var options = new DtoOptions(); - options.DeviceId = authContext.GetAuthorizationInfo(Request).DeviceId; + var authInfo = authContext.GetAuthorizationInfo(Request); + + options.DeviceId = authInfo.DeviceId; var hasFields = request as IHasItemFields; if (hasFields != null) @@ -129,6 +131,34 @@ namespace MediaBrowser.Api options.Fields = hasFields.GetItemFields().ToList(); } + var client = authInfo.Client ?? string.Empty; + if (client.IndexOf("kodi", StringComparison.OrdinalIgnoreCase) != -1 || + client.IndexOf("wmc", StringComparison.OrdinalIgnoreCase) != -1 || + client.IndexOf("media center", StringComparison.OrdinalIgnoreCase) != -1 || + client.IndexOf("classic", StringComparison.OrdinalIgnoreCase) != -1) + { + options.Fields.Add(Model.Querying.ItemFields.RecursiveItemCount); + } + + if (client.IndexOf("kodi", StringComparison.OrdinalIgnoreCase) != -1 || + client.IndexOf("wmc", StringComparison.OrdinalIgnoreCase) != -1 || + client.IndexOf("media center", StringComparison.OrdinalIgnoreCase) != -1 || + client.IndexOf("classic", StringComparison.OrdinalIgnoreCase) != -1 || + client.IndexOf("roku", StringComparison.OrdinalIgnoreCase) != -1 || + client.IndexOf("samsung", StringComparison.OrdinalIgnoreCase) != -1) + { + options.Fields.Add(Model.Querying.ItemFields.ChildCount); + } + + if (client.IndexOf("web", StringComparison.OrdinalIgnoreCase) == -1 && + client.IndexOf("mobile", StringComparison.OrdinalIgnoreCase) == -1 && + client.IndexOf("ios", StringComparison.OrdinalIgnoreCase) == -1 && + client.IndexOf("android", StringComparison.OrdinalIgnoreCase) == -1 && + client.IndexOf("theater", StringComparison.OrdinalIgnoreCase) == -1) + { + options.Fields.Add(Model.Querying.ItemFields.ChildCount); + } + var hasDtoOptions = request as IHasDtoOptions; if (hasDtoOptions != null) { diff --git a/MediaBrowser.Api/ItemLookupService.cs b/MediaBrowser.Api/ItemLookupService.cs index 0ae1fbff4..b5c51bfef 100644 --- a/MediaBrowser.Api/ItemLookupService.cs +++ b/MediaBrowser.Api/ItemLookupService.cs @@ -14,8 +14,6 @@ using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; -using MediaBrowser.Common.IO; -using MediaBrowser.Controller.IO; using MediaBrowser.Model.IO; using MediaBrowser.Model.Serialization; using MediaBrowser.Model.Services; @@ -88,6 +86,12 @@ namespace MediaBrowser.Api { } + [Route("/Items/RemoteSearch/Book", "POST")] + [Authenticated] + public class GetBookRemoteSearchResults : RemoteSearchQuery, IReturn> + { + } + [Route("/Items/RemoteSearch/Image", "GET", Summary = "Gets a remote image")] public class GetRemoteSearchImage { @@ -147,6 +151,13 @@ namespace MediaBrowser.Api return ToOptimizedResult(result); } + public async Task Post(GetBookRemoteSearchResults request) + { + var result = await _providerManager.GetRemoteSearchResults(request, CancellationToken.None).ConfigureAwait(false); + + return ToOptimizedResult(result); + } + public async Task Post(GetMovieRemoteSearchResults request) { var result = await _providerManager.GetRemoteSearchResults(request, CancellationToken.None).ConfigureAwait(false); diff --git a/MediaBrowser.Controller/Entities/BaseItem.cs b/MediaBrowser.Controller/Entities/BaseItem.cs index 9f4503466..2aa53d651 100644 --- a/MediaBrowser.Controller/Entities/BaseItem.cs +++ b/MediaBrowser.Controller/Entities/BaseItem.cs @@ -30,6 +30,7 @@ using MediaBrowser.Model.Globalization; using MediaBrowser.Model.IO; using MediaBrowser.Model.LiveTv; using MediaBrowser.Model.Providers; +using MediaBrowser.Model.Querying; using MediaBrowser.Model.Serialization; namespace MediaBrowser.Controller.Entities @@ -2191,7 +2192,7 @@ namespace MediaBrowser.Controller.Entities return path; } - public virtual Task FillUserDataDtoValues(UserItemDataDto dto, UserItemData userData, BaseItemDto itemDto, User user) + public virtual Task FillUserDataDtoValues(UserItemDataDto dto, UserItemData userData, BaseItemDto itemDto, User user, List itemFields) { if (RunTimeTicks.HasValue) { diff --git a/MediaBrowser.Controller/Entities/CollectionFolder.cs b/MediaBrowser.Controller/Entities/CollectionFolder.cs index ebc55ca8a..c505aefb3 100644 --- a/MediaBrowser.Controller/Entities/CollectionFolder.cs +++ b/MediaBrowser.Controller/Entities/CollectionFolder.cs @@ -27,6 +27,7 @@ namespace MediaBrowser.Controller.Entities public CollectionFolder() { PhysicalLocationsList = new List(); + PhysicalFolderIds = new List(); } [IgnoreDataMember] @@ -153,6 +154,7 @@ namespace MediaBrowser.Controller.Entities } public List PhysicalLocationsList { get; set; } + public List PhysicalFolderIds { get; set; } protected override IEnumerable GetFileSystemChildren(IDirectoryService directoryService) { @@ -176,6 +178,18 @@ namespace MediaBrowser.Controller.Entities } } + if (!changed) + { + var folderIds = PhysicalFolderIds.ToList(); + + var newFolderIds = GetPhysicalFolders(false).Select(i => i.Id).ToList(); + + if (!folderIds.SequenceEqual(newFolderIds)) + { + changed = true; + } + } + return changed; } @@ -186,6 +200,31 @@ namespace MediaBrowser.Controller.Entities return changed; } + protected override bool RefreshLinkedChildren(IEnumerable fileSystemChildren) + { + var physicalFolders = GetPhysicalFolders(false) + .ToList(); + + var linkedChildren = physicalFolders + .SelectMany(c => c.LinkedChildren) + .ToList(); + + var changed = !linkedChildren.SequenceEqual(LinkedChildren, new LinkedChildComparer()); + + LinkedChildren = linkedChildren; + + var folderIds = PhysicalFolderIds.ToList(); + var newFolderIds = physicalFolders.Select(i => i.Id).ToList(); + + if (!folderIds.SequenceEqual(newFolderIds)) + { + changed = true; + PhysicalFolderIds = newFolderIds.ToList(); + } + + return changed; + } + internal override bool IsValidFromResolver(BaseItem newItem) { var newCollectionFolder = newItem as CollectionFolder; @@ -260,26 +299,6 @@ namespace MediaBrowser.Controller.Entities return Task.FromResult(true); } - /// - /// Our children are actually just references to the ones in the physical root... - /// - /// The linked children. - [IgnoreDataMember] - public override List LinkedChildren - { - get { return GetLinkedChildrenInternal(); } - set - { - base.LinkedChildren = value; - } - } - private List GetLinkedChildrenInternal() - { - return GetPhysicalParents() - .SelectMany(c => c.LinkedChildren) - .ToList(); - } - /// /// Our children are actually just references to the ones in the physical root... /// @@ -292,11 +311,16 @@ namespace MediaBrowser.Controller.Entities private IEnumerable GetActualChildren() { - return GetPhysicalParents().SelectMany(c => c.Children); + return GetPhysicalFolders(true).SelectMany(c => c.Children); } - public IEnumerable GetPhysicalParents() + private IEnumerable GetPhysicalFolders(bool enableCache) { + if (enableCache) + { + return PhysicalFolderIds.Select(i => LibraryManager.GetItemById(i)).OfType(); + } + var rootChildren = LibraryManager.RootFolder.Children .OfType() .ToList(); diff --git a/MediaBrowser.Controller/Entities/Folder.cs b/MediaBrowser.Controller/Entities/Folder.cs index 4705f03fa..a84e9a5d2 100644 --- a/MediaBrowser.Controller/Entities/Folder.cs +++ b/MediaBrowser.Controller/Entities/Folder.cs @@ -1222,7 +1222,7 @@ namespace MediaBrowser.Controller.Entities /// Refreshes the linked children. /// /// true if XXXX, false otherwise - private bool RefreshLinkedChildren(IEnumerable fileSystemChildren) + protected virtual bool RefreshLinkedChildren(IEnumerable fileSystemChildren) { var currentManualLinks = LinkedChildren.Where(i => i.Type == LinkedChildType.Manual).ToList(); var currentShortcutLinks = LinkedChildren.Where(i => i.Type == LinkedChildType.Shortcut).ToList(); @@ -1410,23 +1410,24 @@ namespace MediaBrowser.Controller.Entities } } - public override async Task FillUserDataDtoValues(UserItemDataDto dto, UserItemData userData, BaseItemDto itemDto, User user) + public override async Task FillUserDataDtoValues(UserItemDataDto dto, UserItemData userData, BaseItemDto itemDto, User user, List itemFields) { if (!SupportsUserDataFromChildren) { return; } - var recursiveItemCount = GetRecursiveChildCount(user); - if (itemDto != null) { - itemDto.RecursiveItemCount = recursiveItemCount; + if (itemFields.Contains(ItemFields.RecursiveItemCount)) + { + itemDto.RecursiveItemCount = GetRecursiveChildCount(user); + } } - if (recursiveItemCount > 0 && SupportsPlayedStatus) + if (SupportsPlayedStatus) { - var unplayedQueryResult = recursiveItemCount > 0 ? await GetItems(new InternalItemsQuery(user) + var unplayedQueryResult = await GetItems(new InternalItemsQuery(user) { Recursive = true, IsFolder = false, @@ -1435,21 +1436,24 @@ namespace MediaBrowser.Controller.Entities Limit = 0, IsPlayed = false - }).ConfigureAwait(false) : new QueryResult(); + }).ConfigureAwait(false); double unplayedCount = unplayedQueryResult.TotalRecordCount; - var unplayedPercentage = (unplayedCount / recursiveItemCount) * 100; - dto.PlayedPercentage = 100 - unplayedPercentage; - dto.Played = dto.PlayedPercentage.Value >= 100; dto.UnplayedItemCount = unplayedQueryResult.TotalRecordCount; - } - if (itemDto != null) - { - if (this is Season || this is MusicAlbum) + if (itemDto != null && itemDto.RecursiveItemCount.HasValue) + { + if (itemDto.RecursiveItemCount.Value > 0) + { + var unplayedPercentage = (unplayedCount/itemDto.RecursiveItemCount.Value)*100; + dto.PlayedPercentage = 100 - unplayedPercentage; + dto.Played = dto.PlayedPercentage.Value >= 100; + } + } + else { - itemDto.ChildCount = recursiveItemCount; + dto.Played = (dto.UnplayedItemCount ?? 0) == 0; } } } diff --git a/MediaBrowser.Controller/Entities/IHasUserData.cs b/MediaBrowser.Controller/Entities/IHasUserData.cs index c21e170ae..0b3b7dc8d 100644 --- a/MediaBrowser.Controller/Entities/IHasUserData.cs +++ b/MediaBrowser.Controller/Entities/IHasUserData.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using MediaBrowser.Model.Dto; +using MediaBrowser.Model.Querying; namespace MediaBrowser.Controller.Entities { @@ -14,10 +15,7 @@ namespace MediaBrowser.Controller.Entities /// /// Fills the user data dto values. /// - /// The dto. - /// The user data. - /// The user. - Task FillUserDataDtoValues(UserItemDataDto dto, UserItemData userData, BaseItemDto itemDto, User user); + Task FillUserDataDtoValues(UserItemDataDto dto, UserItemData userData, BaseItemDto itemDto, User user, List fields); bool EnableRememberingTrackSelections { get; } diff --git a/MediaBrowser.Controller/Library/ILibraryManager.cs b/MediaBrowser.Controller/Library/ILibraryManager.cs index d297fd006..bf9a07626 100644 --- a/MediaBrowser.Controller/Library/ILibraryManager.cs +++ b/MediaBrowser.Controller/Library/ILibraryManager.cs @@ -62,6 +62,8 @@ namespace MediaBrowser.Controller.Library /// BaseItem. BaseItem FindByPath(string path, bool? isFolder); + Guid? FindIdByPath(string path, bool? isFolder); + /// /// Gets the artist. /// diff --git a/MediaBrowser.Controller/Library/IUserDataManager.cs b/MediaBrowser.Controller/Library/IUserDataManager.cs index 86c52c4c3..5940c7e29 100644 --- a/MediaBrowser.Controller/Library/IUserDataManager.cs +++ b/MediaBrowser.Controller/Library/IUserDataManager.cs @@ -5,6 +5,7 @@ using MediaBrowser.Model.Entities; using System; using System.Threading; using System.Threading.Tasks; +using MediaBrowser.Model.Querying; namespace MediaBrowser.Controller.Library { @@ -37,12 +38,9 @@ namespace MediaBrowser.Controller.Library /// /// Gets the user data dto. /// - /// The item. - /// The user. - /// UserItemDataDto. Task GetUserDataDto(IHasUserData item, User user); - Task GetUserDataDto(IHasUserData item, BaseItemDto itemDto, User user); + Task GetUserDataDto(IHasUserData item, BaseItemDto itemDto, User user, List fields); /// /// Get all user data for the given user diff --git a/MediaBrowser.Model/Querying/ItemFields.cs b/MediaBrowser.Model/Querying/ItemFields.cs index bf1d4991c..e36abf16e 100644 --- a/MediaBrowser.Model/Querying/ItemFields.cs +++ b/MediaBrowser.Model/Querying/ItemFields.cs @@ -171,6 +171,8 @@ /// PrimaryImageAspectRatio, + RecursiveItemCount, + /// /// The revenue /// diff --git a/RSSDP/SsdpDevicePublisherBase.cs b/RSSDP/SsdpDevicePublisherBase.cs index 260c00896..8ab35d661 100644 --- a/RSSDP/SsdpDevicePublisherBase.cs +++ b/RSSDP/SsdpDevicePublisherBase.cs @@ -291,7 +291,7 @@ namespace Rssdp.Infrastructure if (devices != null) { var deviceList = devices.ToList(); - WriteTrace(String.Format("Sending {0} search responses", deviceList.Count)); + //WriteTrace(String.Format("Sending {0} search responses", deviceList.Count)); foreach (var device in deviceList) { @@ -300,7 +300,7 @@ namespace Rssdp.Infrastructure } else { - WriteTrace(String.Format("Sending 0 search responses.")); + //WriteTrace(String.Format("Sending 0 search responses.")); } }); } @@ -413,7 +413,7 @@ namespace Rssdp.Infrastructure //DisposeRebroadcastTimer(); - WriteTrace("Begin Sending Alive Notifications For All Devices"); + //WriteTrace("Begin Sending Alive Notifications For All Devices"); _LastNotificationTime = DateTime.Now; @@ -430,7 +430,7 @@ namespace Rssdp.Infrastructure SendAliveNotifications(device, true); } - WriteTrace("Completed Sending Alive Notifications For All Devices"); + //WriteTrace("Completed Sending Alive Notifications For All Devices"); } catch (ObjectDisposedException ex) { -- cgit v1.2.3 From 71854c1a09bb820c181485cf0601959461bca852 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 13 Dec 2016 03:45:04 -0500 Subject: update connection process --- .../Activity/ActivityRepository.cs | 2 +- .../Data/BaseSqliteRepository.cs | 126 +++++++++++---------- .../Data/SqliteItemRepository.cs | 30 +++-- .../Security/AuthenticationRepository.cs | 2 +- 4 files changed, 86 insertions(+), 74 deletions(-) (limited to 'Emby.Server.Implementations/Activity/ActivityRepository.cs') diff --git a/Emby.Server.Implementations/Activity/ActivityRepository.cs b/Emby.Server.Implementations/Activity/ActivityRepository.cs index 5f6518a14..bf8835846 100644 --- a/Emby.Server.Implementations/Activity/ActivityRepository.cs +++ b/Emby.Server.Implementations/Activity/ActivityRepository.cs @@ -129,7 +129,7 @@ namespace Emby.Server.Implementations.Activity var list = new List(); var result = new QueryResult(); - var statements = PrepareAllSafe(db, string.Join(";", statementTexts.ToArray())).ToList(); + var statements = PrepareAllSafe(db, statementTexts).ToList(); using (var statement = statements[0]) { diff --git a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs index 2fc721f83..b29309ef2 100644 --- a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs +++ b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs @@ -25,7 +25,7 @@ namespace Emby.Server.Implementations.Data protected TransactionMode TransactionMode { - get { return TransactionMode.Immediate; } + get { return TransactionMode.Deferred; } } protected TransactionMode ReadTransactionMode @@ -57,82 +57,86 @@ namespace Emby.Server.Implementations.Data protected SQLiteDatabaseConnection CreateConnection(bool isReadOnly = false) { - if (!_versionLogged) + lock (WriteLock) { - _versionLogged = true; - Logger.Info("Sqlite version: " + SQLite3.Version); - Logger.Info("Sqlite compiler options: " + string.Join(",", SQLite3.CompilerOptions.ToArray())); - } + if (!_versionLogged) + { + _versionLogged = true; + Logger.Info("Sqlite version: " + SQLite3.Version); + Logger.Info("Sqlite compiler options: " + string.Join(",", SQLite3.CompilerOptions.ToArray())); + } - ConnectionFlags connectionFlags; + ConnectionFlags connectionFlags; - if (isReadOnly) - { - //Logger.Info("Opening read connection"); - //connectionFlags = ConnectionFlags.ReadOnly; - connectionFlags = ConnectionFlags.Create; - connectionFlags |= ConnectionFlags.ReadWrite; - } - else - { - //Logger.Info("Opening write connection"); - connectionFlags = ConnectionFlags.Create; - connectionFlags |= ConnectionFlags.ReadWrite; - } + if (isReadOnly) + { + //Logger.Info("Opening read connection"); + //connectionFlags = ConnectionFlags.ReadOnly; + connectionFlags = ConnectionFlags.Create; + connectionFlags |= ConnectionFlags.ReadWrite; + } + else + { + //Logger.Info("Opening write connection"); + connectionFlags = ConnectionFlags.Create; + connectionFlags |= ConnectionFlags.ReadWrite; + } - connectionFlags |= ConnectionFlags.SharedCached; - connectionFlags |= ConnectionFlags.NoMutex; + connectionFlags |= ConnectionFlags.SharedCached; + connectionFlags |= ConnectionFlags.NoMutex; - var db = SQLite3.Open(DbFilePath, connectionFlags, null); + var db = SQLite3.Open(DbFilePath, connectionFlags, null); - if (string.IsNullOrWhiteSpace(_defaultWal)) - { - _defaultWal = db.Query("PRAGMA journal_mode").SelectScalarString().First(); + if (string.IsNullOrWhiteSpace(_defaultWal)) + { + _defaultWal = db.Query("PRAGMA journal_mode").SelectScalarString().First(); - Logger.Info("Default journal_mode for {0} is {1}", DbFilePath, _defaultWal); - } + Logger.Info("Default journal_mode for {0} is {1}", DbFilePath, _defaultWal); + } - var queries = new List + var queries = new List { //"PRAGMA cache size=-10000" - //"PRAGMA read_uncommitted = true" + //"PRAGMA read_uncommitted = true", + "PRAGMA synchronous=Normal" }; - if (EnableTempStoreMemory) - { - queries.Add("PRAGMA temp_store = memory"); - } + if (EnableTempStoreMemory) + { + queries.Add("PRAGMA temp_store = memory"); + } - //var cacheSize = CacheSize; - //if (cacheSize.HasValue) - //{ + //var cacheSize = CacheSize; + //if (cacheSize.HasValue) + //{ - //} + //} - ////foreach (var query in queries) - ////{ - //// db.Execute(query); - ////} + ////foreach (var query in queries) + ////{ + //// db.Execute(query); + ////} - //Logger.Info("synchronous: " + db.Query("PRAGMA synchronous").SelectScalarString().First()); - //Logger.Info("temp_store: " + db.Query("PRAGMA temp_store").SelectScalarString().First()); + //Logger.Info("synchronous: " + db.Query("PRAGMA synchronous").SelectScalarString().First()); + //Logger.Info("temp_store: " + db.Query("PRAGMA temp_store").SelectScalarString().First()); - /*if (!string.Equals(_defaultWal, "wal", StringComparison.OrdinalIgnoreCase)) - { - queries.Add("PRAGMA journal_mode=WAL"); + /*if (!string.Equals(_defaultWal, "wal", StringComparison.OrdinalIgnoreCase)) + { + queries.Add("PRAGMA journal_mode=WAL"); - using (WriteLock.Write()) + using (WriteLock.Write()) + { + db.ExecuteAll(string.Join(";", queries.ToArray())); + } + } + else*/ + foreach (var query in queries) { - db.ExecuteAll(string.Join(";", queries.ToArray())); + db.Execute(query); } - } - else*/ - if (queries.Count > 0) - { - db.ExecuteAll(string.Join(";", queries.ToArray())); - } - return db; + return db; + } } public IStatement PrepareStatement(IDatabaseConnection connection, string sql) @@ -145,14 +149,14 @@ namespace Emby.Server.Implementations.Data return connection.PrepareStatement(sql); } - public List PrepareAll(IDatabaseConnection connection, string sql) + public List PrepareAll(IDatabaseConnection connection, IEnumerable sql) { - return connection.PrepareAll(sql).ToList(); + return PrepareAllSafe(connection, sql); } - public List PrepareAllSafe(IDatabaseConnection connection, string sql) + public List PrepareAllSafe(IDatabaseConnection connection, IEnumerable sql) { - return connection.PrepareAll(sql).ToList(); + return sql.Select(connection.PrepareStatement).ToList(); } protected void RunDefaultInitialization(IDatabaseConnection db) @@ -161,6 +165,7 @@ namespace Emby.Server.Implementations.Data { "PRAGMA journal_mode=WAL", "PRAGMA page_size=4096", + "PRAGMA synchronous=Normal" }; if (EnableTempStoreMemory) @@ -173,6 +178,7 @@ namespace Emby.Server.Implementations.Data } db.ExecuteAll(string.Join(";", queries.ToArray())); + Logger.Info("PRAGMA synchronous=" + db.Query("PRAGMA synchronous").SelectScalarString().First()); } protected virtual bool EnableTempStoreMemory diff --git a/Emby.Server.Implementations/Data/SqliteItemRepository.cs b/Emby.Server.Implementations/Data/SqliteItemRepository.cs index 803ebeca0..f93819322 100644 --- a/Emby.Server.Implementations/Data/SqliteItemRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteItemRepository.cs @@ -701,12 +701,12 @@ namespace Emby.Server.Implementations.Data { var requiresReset = false; - var statements = PrepareAll(db, string.Join(";", new string[] + var statements = PrepareAllSafe(db, new string[] { GetSaveItemCommandText(), "delete from AncestorIds where ItemId=@ItemId", "insert into AncestorIds (ItemId, AncestorId, AncestorIdText) values (@ItemId, @AncestorId, @AncestorIdText)" - })).ToList(); + }).ToList(); using (var saveItemStatement = statements[0]) { @@ -1258,18 +1258,23 @@ namespace Emby.Server.Implementations.Data { using (var connection = CreateConnection(true)) { - using (var statement = PrepareStatementSafe(connection, "select " + string.Join(",", _retriveItemColumns) + " from TypedBaseItems where guid = @guid")) + return connection.RunInTransaction(db => { - statement.TryBind("@guid", id); - - foreach (var row in statement.ExecuteQuery()) + using (var statement = PrepareStatementSafe(db, "select " + string.Join(",", _retriveItemColumns) + " from TypedBaseItems where guid = @guid")) { - return GetItem(row); + statement.TryBind("@guid", id); + + foreach (var row in statement.ExecuteQuery()) + { + return GetItem(row); + } } - } + + return null; + + }, ReadTransactionMode); } } - return null; } private BaseItem GetItem(IReadOnlyList reader) @@ -2762,7 +2767,7 @@ namespace Emby.Server.Implementations.Data return connection.RunInTransaction(db => { var result = new QueryResult(); - var statements = PrepareAllSafe(db, string.Join(";", statementTexts.ToArray())) + var statements = PrepareAllSafe(db, statementTexts) .ToList(); if (!isReturningZeroItems) @@ -3173,7 +3178,7 @@ namespace Emby.Server.Implementations.Data { var result = new QueryResult(); - var statements = PrepareAllSafe(db, string.Join(";", statementTexts.ToArray())) + var statements = PrepareAllSafe(db, statementTexts) .ToList(); if (!isReturningZeroItems) @@ -5121,7 +5126,8 @@ namespace Emby.Server.Implementations.Data var list = new List>(); var result = new QueryResult>(); - var statements = PrepareAllSafe(db, string.Join(";", statementTexts.ToArray())).ToList(); + var statements = PrepareAllSafe(db, statementTexts) + .ToList(); if (!isReturningZeroItems) { diff --git a/Emby.Server.Implementations/Security/AuthenticationRepository.cs b/Emby.Server.Implementations/Security/AuthenticationRepository.cs index 7199f4f4d..a2d61873b 100644 --- a/Emby.Server.Implementations/Security/AuthenticationRepository.cs +++ b/Emby.Server.Implementations/Security/AuthenticationRepository.cs @@ -214,7 +214,7 @@ namespace Emby.Server.Implementations.Security statementTexts.Add(commandText); statementTexts.Add("select count (Id) from AccessTokens" + whereTextWithoutPaging); - var statements = PrepareAllSafe(db, string.Join(";", statementTexts.ToArray())) + var statements = PrepareAllSafe(db, statementTexts) .ToList(); using (var statement = statements[0]) -- cgit v1.2.3