aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/Data
diff options
context:
space:
mode:
Diffstat (limited to 'Emby.Server.Implementations/Data')
-rw-r--r--Emby.Server.Implementations/Data/BaseSqliteRepository.cs125
-rw-r--r--Emby.Server.Implementations/Data/ConnectionPool.cs79
-rw-r--r--Emby.Server.Implementations/Data/ManagedConnection.cs13
-rw-r--r--Emby.Server.Implementations/Data/SqliteItemRepository.cs182
-rw-r--r--Emby.Server.Implementations/Data/SqliteUserDataRepository.cs38
5 files changed, 268 insertions, 169 deletions
diff --git a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs
index 1d61667f8..d05534ee7 100644
--- a/Emby.Server.Implementations/Data/BaseSqliteRepository.cs
+++ b/Emby.Server.Implementations/Data/BaseSqliteRepository.cs
@@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
-using System.Threading;
using Jellyfin.Extensions;
using Microsoft.Extensions.Logging;
using SQLitePCL.pretty;
@@ -27,10 +26,20 @@ namespace Emby.Server.Implementations.Data
/// <summary>
/// Gets or sets the path to the DB file.
/// </summary>
- /// <value>Path to the DB file.</value>
protected string DbFilePath { get; set; }
/// <summary>
+ /// Gets or sets the number of write connections to create.
+ /// </summary>
+ /// <value>Path to the DB file.</value>
+ protected int WriteConnectionsCount { get; set; } = 1;
+
+ /// <summary>
+ /// Gets or sets the number of read connections to create.
+ /// </summary>
+ protected int ReadConnectionsCount { get; set; } = 1;
+
+ /// <summary>
/// Gets the logger.
/// </summary>
/// <value>The logger.</value>
@@ -63,7 +72,7 @@ namespace Emby.Server.Implementations.Data
/// <summary>
/// Gets the locking mode. <see href="https://www.sqlite.org/pragma.html#pragma_locking_mode" />.
/// </summary>
- protected virtual string LockingMode => "EXCLUSIVE";
+ protected virtual string LockingMode => "NORMAL";
/// <summary>
/// Gets the journal mode. <see href="https://www.sqlite.org/pragma.html#pragma_journal_mode" />.
@@ -73,9 +82,10 @@ namespace Emby.Server.Implementations.Data
/// <summary>
/// Gets the journal size limit. <see href="https://www.sqlite.org/pragma.html#pragma_journal_size_limit" />.
+ /// The default (-1) is overriden to prevent unconstrained WAL size, as reported by users.
/// </summary>
/// <value>The journal size limit.</value>
- protected virtual int? JournalSizeLimit => 0;
+ protected virtual int? JournalSizeLimit => 134_217_728; // 128MiB
/// <summary>
/// Gets the page size.
@@ -88,7 +98,7 @@ namespace Emby.Server.Implementations.Data
/// </summary>
/// <value>The temp store mode.</value>
/// <see cref="TempStoreMode"/>
- protected virtual TempStoreMode TempStore => TempStoreMode.Default;
+ protected virtual TempStoreMode TempStore => TempStoreMode.Memory;
/// <summary>
/// Gets the synchronous mode.
@@ -101,83 +111,114 @@ namespace Emby.Server.Implementations.Data
/// Gets or sets the write lock.
/// </summary>
/// <value>The write lock.</value>
- protected SemaphoreSlim WriteLock { get; set; } = new SemaphoreSlim(1, 1);
+ protected ConnectionPool WriteConnections { get; set; }
/// <summary>
/// Gets or sets the write connection.
/// </summary>
/// <value>The write connection.</value>
- protected SQLiteDatabaseConnection WriteConnection { get; set; }
+ protected ConnectionPool ReadConnections { get; set; }
- protected ManagedConnection GetConnection(bool readOnly = false)
+ public virtual void Initialize()
{
- WriteLock.Wait();
- if (WriteConnection is not null)
+ WriteConnections = new ConnectionPool(WriteConnectionsCount, CreateWriteConnection);
+ ReadConnections = new ConnectionPool(ReadConnectionsCount, CreateReadConnection);
+
+ // Configuration and pragmas can affect VACUUM so it needs to be last.
+ using (var connection = GetConnection())
{
- return new ManagedConnection(WriteConnection, WriteLock);
+ connection.Execute("VACUUM");
}
+ }
+
+ protected ManagedConnection GetConnection(bool readOnly = false)
+ => readOnly ? ReadConnections.GetConnection() : WriteConnections.GetConnection();
- WriteConnection = SQLite3.Open(
+ protected SQLiteDatabaseConnection CreateWriteConnection()
+ {
+ var writeConnection = SQLite3.Open(
DbFilePath,
DefaultConnectionFlags | ConnectionFlags.Create | ConnectionFlags.ReadWrite,
null);
if (CacheSize.HasValue)
{
- WriteConnection.Execute("PRAGMA cache_size=" + CacheSize.Value);
+ writeConnection.Execute("PRAGMA cache_size=" + CacheSize.Value);
}
if (!string.IsNullOrWhiteSpace(LockingMode))
{
- WriteConnection.Execute("PRAGMA locking_mode=" + LockingMode);
+ writeConnection.Execute("PRAGMA locking_mode=" + LockingMode);
}
if (!string.IsNullOrWhiteSpace(JournalMode))
{
- WriteConnection.Execute("PRAGMA journal_mode=" + JournalMode);
+ writeConnection.Execute("PRAGMA journal_mode=" + JournalMode);
}
if (JournalSizeLimit.HasValue)
{
- WriteConnection.Execute("PRAGMA journal_size_limit=" + (int)JournalSizeLimit.Value);
+ writeConnection.Execute("PRAGMA journal_size_limit=" + JournalSizeLimit.Value);
}
if (Synchronous.HasValue)
{
- WriteConnection.Execute("PRAGMA synchronous=" + (int)Synchronous.Value);
+ writeConnection.Execute("PRAGMA synchronous=" + (int)Synchronous.Value);
}
if (PageSize.HasValue)
{
- WriteConnection.Execute("PRAGMA page_size=" + PageSize.Value);
+ writeConnection.Execute("PRAGMA page_size=" + PageSize.Value);
}
- WriteConnection.Execute("PRAGMA temp_store=" + (int)TempStore);
-
- // Configuration and pragmas can affect VACUUM so it needs to be last.
- WriteConnection.Execute("VACUUM");
+ writeConnection.Execute("PRAGMA temp_store=" + (int)TempStore);
- return new ManagedConnection(WriteConnection, WriteLock);
+ return writeConnection;
}
- public IStatement PrepareStatement(ManagedConnection connection, string sql)
- => connection.PrepareStatement(sql);
+ protected SQLiteDatabaseConnection CreateReadConnection()
+ {
+ var connection = SQLite3.Open(
+ DbFilePath,
+ DefaultConnectionFlags | ConnectionFlags.ReadOnly,
+ null);
- public IStatement PrepareStatement(IDatabaseConnection connection, string sql)
- => connection.PrepareStatement(sql);
+ if (CacheSize.HasValue)
+ {
+ connection.Execute("PRAGMA cache_size=" + CacheSize.Value);
+ }
- public IStatement[] PrepareAll(IDatabaseConnection connection, IReadOnlyList<string> sql)
- {
- int len = sql.Count;
- IStatement[] statements = new IStatement[len];
- for (int i = 0; i < len; i++)
+ if (!string.IsNullOrWhiteSpace(LockingMode))
+ {
+ connection.Execute("PRAGMA locking_mode=" + LockingMode);
+ }
+
+ if (!string.IsNullOrWhiteSpace(JournalMode))
+ {
+ connection.Execute("PRAGMA journal_mode=" + JournalMode);
+ }
+
+ if (JournalSizeLimit.HasValue)
+ {
+ connection.Execute("PRAGMA journal_size_limit=" + JournalSizeLimit.Value);
+ }
+
+ if (Synchronous.HasValue)
{
- statements[i] = connection.PrepareStatement(sql[i]);
+ connection.Execute("PRAGMA synchronous=" + (int)Synchronous.Value);
}
- return statements;
+ connection.Execute("PRAGMA temp_store=" + (int)TempStore);
+
+ return connection;
}
+ public IStatement PrepareStatement(ManagedConnection connection, string sql)
+ => connection.PrepareStatement(sql);
+
+ public IStatement PrepareStatement(IDatabaseConnection connection, string sql)
+ => connection.PrepareStatement(sql);
+
protected bool TableExists(ManagedConnection connection, string name)
{
return connection.RunInTransaction(
@@ -252,22 +293,10 @@ namespace Emby.Server.Implementations.Data
if (dispose)
{
- WriteLock.Wait();
- try
- {
- WriteConnection?.Dispose();
- }
- finally
- {
- WriteLock.Release();
- }
-
- WriteLock.Dispose();
+ WriteConnections.Dispose();
+ ReadConnections.Dispose();
}
- WriteConnection = null;
- WriteLock = null;
-
_disposed = true;
}
}
diff --git a/Emby.Server.Implementations/Data/ConnectionPool.cs b/Emby.Server.Implementations/Data/ConnectionPool.cs
new file mode 100644
index 000000000..5ea7e934f
--- /dev/null
+++ b/Emby.Server.Implementations/Data/ConnectionPool.cs
@@ -0,0 +1,79 @@
+using System;
+using System.Collections.Concurrent;
+using SQLitePCL.pretty;
+
+namespace Emby.Server.Implementations.Data;
+
+/// <summary>
+/// A pool of SQLite Database connections.
+/// </summary>
+public sealed class ConnectionPool : IDisposable
+{
+ private readonly BlockingCollection<SQLiteDatabaseConnection> _connections = new();
+ private bool _disposed;
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="ConnectionPool" /> class.
+ /// </summary>
+ /// <param name="count">The number of database connection to create.</param>
+ /// <param name="factory">Factory function to create the database connections.</param>
+ public ConnectionPool(int count, Func<SQLiteDatabaseConnection> factory)
+ {
+ for (int i = 0; i < count; i++)
+ {
+ _connections.Add(factory.Invoke());
+ }
+ }
+
+ /// <summary>
+ /// Gets a database connection from the pool if one is available, otherwise blocks.
+ /// </summary>
+ /// <returns>A database connection.</returns>
+ public ManagedConnection GetConnection()
+ {
+ if (_disposed)
+ {
+ ThrowObjectDisposedException();
+ }
+
+ return new ManagedConnection(_connections.Take(), this);
+
+ static void ThrowObjectDisposedException()
+ {
+ throw new ObjectDisposedException(nameof(ConnectionPool));
+ }
+ }
+
+ /// <summary>
+ /// Return a database connection to the pool.
+ /// </summary>
+ /// <param name="connection">The database connection to return.</param>
+ public void Return(SQLiteDatabaseConnection connection)
+ {
+ if (_disposed)
+ {
+ connection.Dispose();
+ return;
+ }
+
+ _connections.Add(connection);
+ }
+
+ /// <inheritdoc />
+ public void Dispose()
+ {
+ if (_disposed)
+ {
+ return;
+ }
+
+ foreach (var connection in _connections)
+ {
+ connection.Dispose();
+ }
+
+ _connections.Dispose();
+
+ _disposed = true;
+ }
+}
diff --git a/Emby.Server.Implementations/Data/ManagedConnection.cs b/Emby.Server.Implementations/Data/ManagedConnection.cs
index 11e33278d..e84ed8f91 100644
--- a/Emby.Server.Implementations/Data/ManagedConnection.cs
+++ b/Emby.Server.Implementations/Data/ManagedConnection.cs
@@ -2,23 +2,22 @@
using System;
using System.Collections.Generic;
-using System.Threading;
using SQLitePCL.pretty;
namespace Emby.Server.Implementations.Data
{
public sealed class ManagedConnection : IDisposable
{
- private readonly SemaphoreSlim _writeLock;
+ private readonly ConnectionPool _pool;
- private SQLiteDatabaseConnection? _db;
+ private SQLiteDatabaseConnection _db;
private bool _disposed = false;
- public ManagedConnection(SQLiteDatabaseConnection db, SemaphoreSlim writeLock)
+ public ManagedConnection(SQLiteDatabaseConnection db, ConnectionPool pool)
{
_db = db;
- _writeLock = writeLock;
+ _pool = pool;
}
public IStatement PrepareStatement(string sql)
@@ -73,9 +72,9 @@ namespace Emby.Server.Implementations.Data
return;
}
- _writeLock.Release();
+ _pool.Return(_db);
- _db = null; // Don't dispose it
+ _db = null!; // Don't dispose it
_disposed = true;
}
}
diff --git a/Emby.Server.Implementations/Data/SqliteItemRepository.cs b/Emby.Server.Implementations/Data/SqliteItemRepository.cs
index 3bf4d07c5..ca8f605a0 100644
--- a/Emby.Server.Implementations/Data/SqliteItemRepository.cs
+++ b/Emby.Server.Implementations/Data/SqliteItemRepository.cs
@@ -25,6 +25,7 @@ using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
+using MediaBrowser.Controller.Extensions;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Controller.Persistence;
@@ -34,6 +35,7 @@ using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.LiveTv;
using MediaBrowser.Model.Querying;
+using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using SQLitePCL.pretty;
@@ -49,8 +51,8 @@ namespace Emby.Server.Implementations.Data
private const string SaveItemCommandText =
@"replace into TypedBaseItems
- (guid,type,data,Path,StartDate,EndDate,ChannelId,IsMovie,IsSeries,EpisodeTitle,IsRepeat,CommunityRating,CustomRating,IndexNumber,IsLocked,Name,OfficialRating,MediaType,Overview,ParentIndexNumber,PremiereDate,ProductionYear,ParentId,Genres,InheritedParentalRatingValue,SortName,ForcedSortName,RunTimeTicks,Size,DateCreated,DateModified,PreferredMetadataLanguage,PreferredMetadataCountryCode,Width,Height,DateLastRefreshed,DateLastSaved,IsInMixedFolder,LockedFields,Studios,Audio,ExternalServiceId,Tags,IsFolder,UnratedType,TopParentId,TrailerTypes,CriticRating,CleanName,PresentationUniqueKey,OriginalTitle,PrimaryVersionId,DateLastMediaAdded,Album,IsVirtualItem,SeriesName,UserDataKey,SeasonName,SeasonId,SeriesId,ExternalSeriesId,Tagline,ProviderIds,Images,ProductionLocations,ExtraIds,TotalBitrate,ExtraType,Artists,AlbumArtists,ExternalId,SeriesPresentationUniqueKey,ShowId,OwnerId)
- values (@guid,@type,@data,@Path,@StartDate,@EndDate,@ChannelId,@IsMovie,@IsSeries,@EpisodeTitle,@IsRepeat,@CommunityRating,@CustomRating,@IndexNumber,@IsLocked,@Name,@OfficialRating,@MediaType,@Overview,@ParentIndexNumber,@PremiereDate,@ProductionYear,@ParentId,@Genres,@InheritedParentalRatingValue,@SortName,@ForcedSortName,@RunTimeTicks,@Size,@DateCreated,@DateModified,@PreferredMetadataLanguage,@PreferredMetadataCountryCode,@Width,@Height,@DateLastRefreshed,@DateLastSaved,@IsInMixedFolder,@LockedFields,@Studios,@Audio,@ExternalServiceId,@Tags,@IsFolder,@UnratedType,@TopParentId,@TrailerTypes,@CriticRating,@CleanName,@PresentationUniqueKey,@OriginalTitle,@PrimaryVersionId,@DateLastMediaAdded,@Album,@IsVirtualItem,@SeriesName,@UserDataKey,@SeasonName,@SeasonId,@SeriesId,@ExternalSeriesId,@Tagline,@ProviderIds,@Images,@ProductionLocations,@ExtraIds,@TotalBitrate,@ExtraType,@Artists,@AlbumArtists,@ExternalId,@SeriesPresentationUniqueKey,@ShowId,@OwnerId)";
+ (guid,type,data,Path,StartDate,EndDate,ChannelId,IsMovie,IsSeries,EpisodeTitle,IsRepeat,CommunityRating,CustomRating,IndexNumber,IsLocked,Name,OfficialRating,MediaType,Overview,ParentIndexNumber,PremiereDate,ProductionYear,ParentId,Genres,InheritedParentalRatingValue,SortName,ForcedSortName,RunTimeTicks,Size,DateCreated,DateModified,PreferredMetadataLanguage,PreferredMetadataCountryCode,Width,Height,DateLastRefreshed,DateLastSaved,IsInMixedFolder,LockedFields,Studios,Audio,ExternalServiceId,Tags,IsFolder,UnratedType,TopParentId,TrailerTypes,CriticRating,CleanName,PresentationUniqueKey,OriginalTitle,PrimaryVersionId,DateLastMediaAdded,Album,LUFS,IsVirtualItem,SeriesName,UserDataKey,SeasonName,SeasonId,SeriesId,ExternalSeriesId,Tagline,ProviderIds,Images,ProductionLocations,ExtraIds,TotalBitrate,ExtraType,Artists,AlbumArtists,ExternalId,SeriesPresentationUniqueKey,ShowId,OwnerId)
+ values (@guid,@type,@data,@Path,@StartDate,@EndDate,@ChannelId,@IsMovie,@IsSeries,@EpisodeTitle,@IsRepeat,@CommunityRating,@CustomRating,@IndexNumber,@IsLocked,@Name,@OfficialRating,@MediaType,@Overview,@ParentIndexNumber,@PremiereDate,@ProductionYear,@ParentId,@Genres,@InheritedParentalRatingValue,@SortName,@ForcedSortName,@RunTimeTicks,@Size,@DateCreated,@DateModified,@PreferredMetadataLanguage,@PreferredMetadataCountryCode,@Width,@Height,@DateLastRefreshed,@DateLastSaved,@IsInMixedFolder,@LockedFields,@Studios,@Audio,@ExternalServiceId,@Tags,@IsFolder,@UnratedType,@TopParentId,@TrailerTypes,@CriticRating,@CleanName,@PresentationUniqueKey,@OriginalTitle,@PrimaryVersionId,@DateLastMediaAdded,@Album,@LUFS,@IsVirtualItem,@SeriesName,@UserDataKey,@SeasonName,@SeasonId,@SeriesId,@ExternalSeriesId,@Tagline,@ProviderIds,@Images,@ProductionLocations,@ExtraIds,@TotalBitrate,@ExtraType,@Artists,@AlbumArtists,@ExternalId,@SeriesPresentationUniqueKey,@ShowId,@OwnerId)";
private readonly IServerConfigurationManager _config;
private readonly IServerApplicationHost _appHost;
@@ -110,6 +112,7 @@ namespace Emby.Server.Implementations.Data
"PrimaryVersionId",
"DateLastMediaAdded",
"Album",
+ "LUFS",
"CriticRating",
"IsVirtualItem",
"SeriesName",
@@ -318,13 +321,15 @@ namespace Emby.Server.Implementations.Data
/// <param name="logger">Instance of the <see cref="ILogger{SqliteItemRepository}"/> interface.</param>
/// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param>
/// <param name="imageProcessor">Instance of the <see cref="IImageProcessor"/> interface.</param>
+ /// <param name="configuration">Instance of the <see cref="IConfiguration"/> interface.</param>
/// <exception cref="ArgumentNullException">config is null.</exception>
public SqliteItemRepository(
IServerConfigurationManager config,
IServerApplicationHost appHost,
ILogger<SqliteItemRepository> logger,
ILocalizationManager localization,
- IImageProcessor imageProcessor)
+ IImageProcessor imageProcessor,
+ IConfiguration configuration)
: base(logger)
{
_config = config;
@@ -336,10 +341,13 @@ namespace Emby.Server.Implementations.Data
_jsonOptions = JsonDefaults.Options;
DbFilePath = Path.Combine(_config.ApplicationPaths.DataPath, "library.db");
+
+ CacheSize = configuration.GetSqliteCacheSize();
+ ReadConnectionsCount = Environment.ProcessorCount * 2;
}
/// <inheritdoc />
- protected override int? CacheSize => 20000;
+ protected override int? CacheSize { get; }
/// <inheritdoc />
protected override TempStoreMode TempStore => TempStoreMode.Memory;
@@ -347,10 +355,10 @@ namespace Emby.Server.Implementations.Data
/// <summary>
/// Opens the connection to the database.
/// </summary>
- /// <param name="userDataRepo">The user data repository.</param>
- /// <param name="userManager">The user manager.</param>
- public void Initialize(SqliteUserDataRepository userDataRepo, IUserManager userManager)
+ public override void Initialize()
{
+ base.Initialize();
+
const string 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, ColorPrimaries TEXT NULL, ColorSpace TEXT NULL, ColorTransfer TEXT NULL, DvVersionMajor INT NULL, DvVersionMinor INT NULL, DvProfile INT NULL, DvLevel INT NULL, RpuPresentFlag INT NULL, ElPresentFlag INT NULL, BlPresentFlag INT NULL, DvBlSignalCompatibilityId INT NULL, IsHearingImpaired BIT NULL, PRIMARY KEY (ItemId, StreamIndex))";
@@ -488,6 +496,7 @@ namespace Emby.Server.Implementations.Data
AddColumn(db, "TypedBaseItems", "PrimaryVersionId", "Text", existingColumnNames);
AddColumn(db, "TypedBaseItems", "DateLastMediaAdded", "DATETIME", existingColumnNames);
AddColumn(db, "TypedBaseItems", "Album", "Text", existingColumnNames);
+ AddColumn(db, "TypedBaseItems", "LUFS", "Float", existingColumnNames);
AddColumn(db, "TypedBaseItems", "IsVirtualItem", "BIT", existingColumnNames);
AddColumn(db, "TypedBaseItems", "SeriesName", "Text", existingColumnNames);
AddColumn(db, "TypedBaseItems", "UserDataKey", "Text", existingColumnNames);
@@ -551,8 +560,6 @@ namespace Emby.Server.Implementations.Data
connection.RunQueries(postQueries);
}
-
- userDataRepo.Initialize(userManager, WriteLock, WriteConnection);
}
public void SaveImages(BaseItem item)
@@ -624,14 +631,8 @@ namespace Emby.Server.Implementations.Data
private void SaveItemsInTransaction(IDatabaseConnection db, IEnumerable<(BaseItem Item, List<Guid> AncestorIds, BaseItem TopParent, string UserDataKey, List<string> InheritedTags)> tuples)
{
- var statements = PrepareAll(db, new string[]
- {
- SaveItemCommandText,
- "delete from AncestorIds where ItemId=@ItemId"
- });
-
- using (var saveItemStatement = statements[0])
- using (var deleteAncestorsStatement = statements[1])
+ using (var saveItemStatement = PrepareStatement(db, SaveItemCommandText))
+ using (var deleteAncestorsStatement = PrepareStatement(db, "delete from AncestorIds where ItemId=@ItemId"))
{
var requiresReset = false;
foreach (var tuple in tuples)
@@ -913,6 +914,7 @@ namespace Emby.Server.Implementations.Data
}
saveItemStatement.TryBind("@Album", item.Album);
+ saveItemStatement.TryBind("@LUFS", item.LUFS);
saveItemStatement.TryBind("@IsVirtualItem", item.IsVirtualItem);
if (item is IHasSeries hasSeriesName)
@@ -1286,15 +1288,13 @@ namespace Emby.Server.Implementations.Data
CheckDisposed();
using (var connection = GetConnection(true))
+ using (var statement = PrepareStatement(connection, _retrieveItemColumnsSelectQuery))
{
- using (var statement = PrepareStatement(connection, _retrieveItemColumnsSelectQuery))
- {
- statement.TryBind("@guid", id);
+ statement.TryBind("@guid", id);
- foreach (var row in statement.ExecuteQuery())
- {
- return GetItem(row, new InternalItemsQuery());
- }
+ foreach (var row in statement.ExecuteQuery())
+ {
+ return GetItem(row, new InternalItemsQuery());
}
}
@@ -1309,7 +1309,8 @@ namespace Emby.Server.Implementations.Data
{
return false;
}
- else if (type == typeof(UserRootFolder))
+
+ if (type == typeof(UserRootFolder))
{
return false;
}
@@ -1319,55 +1320,68 @@ namespace Emby.Server.Implementations.Data
{
return false;
}
- else if (type == typeof(MusicArtist))
+
+ if (type == typeof(MusicArtist))
{
return false;
}
- else if (type == typeof(Person))
+
+ if (type == typeof(Person))
{
return false;
}
- else if (type == typeof(MusicGenre))
+
+ if (type == typeof(MusicGenre))
{
return false;
}
- else if (type == typeof(Genre))
+
+ if (type == typeof(Genre))
{
return false;
}
- else if (type == typeof(Studio))
+
+ if (type == typeof(Studio))
{
return false;
}
- else if (type == typeof(PlaylistsFolder))
+
+ if (type == typeof(PlaylistsFolder))
{
return false;
}
- else if (type == typeof(PhotoAlbum))
+
+ if (type == typeof(PhotoAlbum))
{
return false;
}
- else if (type == typeof(Year))
+
+ if (type == typeof(Year))
{
return false;
}
- else if (type == typeof(Book))
+
+ if (type == typeof(Book))
{
return false;
}
- else if (type == typeof(LiveTvProgram))
+
+ if (type == typeof(LiveTvProgram))
{
return false;
}
- else if (type == typeof(AudioBook))
+
+ if (type == typeof(AudioBook))
{
return false;
}
- else if (type == typeof(Audio))
+
+ if (type == typeof(Audio))
{
return false;
}
- else if (type == typeof(MusicAlbum))
+
+ if (type == typeof(MusicAlbum))
{
return false;
}
@@ -1751,6 +1765,11 @@ namespace Emby.Server.Implementations.Data
item.Album = album;
}
+ if (reader.TryGetSingle(index++, out var lUFS))
+ {
+ item.LUFS = lUFS;
+ }
+
if (reader.TryGetSingle(index++, out var criticRating))
{
item.CriticRating = criticRating;
@@ -1958,22 +1977,19 @@ namespace Emby.Server.Implementations.Data
{
CheckDisposed();
+ var chapters = new List<ChapterInfo>();
using (var connection = GetConnection(true))
+ using (var statement = PrepareStatement(connection, "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId order by ChapterIndex asc"))
{
- var chapters = new List<ChapterInfo>();
+ statement.TryBind("@ItemId", item.Id);
- using (var statement = PrepareStatement(connection, "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId order by ChapterIndex asc"))
+ foreach (var row in statement.ExecuteQuery())
{
- statement.TryBind("@ItemId", item.Id);
-
- foreach (var row in statement.ExecuteQuery())
- {
- chapters.Add(GetChapter(row, item));
- }
+ chapters.Add(GetChapter(row, item));
}
-
- return chapters;
}
+
+ return chapters;
}
/// <inheritdoc />
@@ -1982,16 +1998,14 @@ namespace Emby.Server.Implementations.Data
CheckDisposed();
using (var connection = GetConnection(true))
+ using (var statement = PrepareStatement(connection, "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId and ChapterIndex=@ChapterIndex"))
{
- using (var statement = PrepareStatement(connection, "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId and ChapterIndex=@ChapterIndex"))
- {
- statement.TryBind("@ItemId", item.Id);
- statement.TryBind("@ChapterIndex", index);
+ statement.TryBind("@ItemId", item.Id);
+ statement.TryBind("@ChapterIndex", index);
- foreach (var row in statement.ExecuteQuery())
- {
- return GetChapter(row, item);
- }
+ foreach (var row in statement.ExecuteQuery())
+ {
+ return GetChapter(row, item);
}
}
@@ -2378,7 +2392,7 @@ namespace Emby.Server.Implementations.Data
else
{
builder.Append(
- @"(SELECT CASE WHEN InheritedParentalRatingValue=0
+ @"(SELECT CASE WHEN COALESCE(InheritedParentalRatingValue, 0)=0
THEN 0
ELSE 10.0 / (1.0 + ABS(InheritedParentalRatingValue - @InheritedParentalRatingValue))
END)");
@@ -2392,6 +2406,7 @@ namespace Emby.Server.Implementations.Data
// genres, tags, studios, person, year?
builder.Append("+ (Select count(1) * 10 from ItemValues where ItemId=Guid and CleanValue in (select CleanValue from ItemValues where ItemId=@SimilarItemId))");
+ builder.Append("+ (Select count(1) * 10 from People where ItemId=Guid and Name in (select Name from People where ItemId=@SimilarItemId))");
if (item is MusicArtist)
{
@@ -2843,13 +2858,10 @@ namespace Emby.Server.Implementations.Data
connection.RunInTransaction(
db =>
{
- var itemQueryStatement = PrepareStatement(db, itemQuery);
- var totalRecordCountQueryStatement = PrepareStatement(db, totalRecordCountQuery);
-
if (!isReturningZeroItems)
{
using (new QueryTimeLogger(Logger, itemQuery, "GetItems.ItemQuery"))
- using (var statement = itemQueryStatement)
+ using (var statement = PrepareStatement(db, itemQuery))
{
if (EnableJoinUserData(query))
{
@@ -2884,7 +2896,7 @@ namespace Emby.Server.Implementations.Data
if (query.EnableTotalRecordCount)
{
using (new QueryTimeLogger(Logger, totalRecordCountQuery, "GetItems.TotalRecordCount"))
- using (var statement = totalRecordCountQueryStatement)
+ using (var statement = PrepareStatement(db, totalRecordCountQuery))
{
if (EnableJoinUserData(query))
{
@@ -4753,22 +4765,20 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
commandText.Append(" LIMIT ").Append(query.Limit);
}
+ var list = new List<string>();
using (var connection = GetConnection(true))
+ using (var statement = PrepareStatement(connection, commandText.ToString()))
{
- var list = new List<string>();
- using (var statement = PrepareStatement(connection, commandText.ToString()))
- {
- // Run this again to bind the params
- GetPeopleWhereClauses(query, statement);
+ // Run this again to bind the params
+ GetPeopleWhereClauses(query, statement);
- foreach (var row in statement.ExecuteQuery())
- {
- list.Add(row.GetString(0));
- }
+ foreach (var row in statement.ExecuteQuery())
+ {
+ list.Add(row.GetString(0));
}
-
- return list;
}
+
+ return list;
}
public List<PersonInfo> GetPeople(InternalPeopleQuery query)
@@ -4793,23 +4803,20 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
commandText += " LIMIT " + query.Limit;
}
+ var list = new List<PersonInfo>();
using (var connection = GetConnection(true))
+ using (var statement = PrepareStatement(connection, commandText))
{
- var list = new List<PersonInfo>();
+ // Run this again to bind the params
+ GetPeopleWhereClauses(query, statement);
- using (var statement = PrepareStatement(connection, commandText))
+ foreach (var row in statement.ExecuteQuery())
{
- // Run this again to bind the params
- GetPeopleWhereClauses(query, statement);
-
- foreach (var row in statement.ExecuteQuery())
- {
- list.Add(GetPerson(row));
- }
+ list.Add(GetPerson(row));
}
-
- return list;
}
+
+ return list;
}
private List<string> GetPeopleWhereClauses(InternalPeopleQuery query, IStatement statement)
@@ -5540,7 +5547,7 @@ AND Type = @InternalPersonType)");
statement.TryBind("@Name" + index, person.Name);
statement.TryBind("@Role" + index, person.Role);
- statement.TryBind("@PersonType" + index, person.Type);
+ statement.TryBind("@PersonType" + index, person.Type.ToString());
statement.TryBind("@SortOrder" + index, person.SortOrder);
statement.TryBind("@ListOrder" + index, listIndex);
@@ -5569,9 +5576,10 @@ AND Type = @InternalPersonType)");
item.Role = role;
}
- if (reader.TryGetString(3, out var type))
+ if (reader.TryGetString(3, out var type)
+ && Enum.TryParse(type, true, out PersonKind personKind))
{
- item.Type = type;
+ item.Type = personKind;
}
if (reader.TryGetInt32(4, out var sortOrder))
diff --git a/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs b/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs
index 5f2c3c9dc..a1e217ad1 100644
--- a/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs
+++ b/Emby.Server.Implementations/Data/SqliteUserDataRepository.cs
@@ -7,7 +7,7 @@ using System.Collections.Generic;
using System.IO;
using System.Threading;
using Jellyfin.Data.Entities;
-using MediaBrowser.Common.Configuration;
+using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Persistence;
@@ -18,33 +18,32 @@ namespace Emby.Server.Implementations.Data
{
public class SqliteUserDataRepository : BaseSqliteRepository, IUserDataRepository
{
+ private readonly IUserManager _userManager;
+
public SqliteUserDataRepository(
ILogger<SqliteUserDataRepository> logger,
- IApplicationPaths appPaths)
+ IServerConfigurationManager config,
+ IUserManager userManager)
: base(logger)
{
- DbFilePath = Path.Combine(appPaths.DataPath, "library.db");
+ _userManager = userManager;
+
+ DbFilePath = Path.Combine(config.ApplicationPaths.DataPath, "library.db");
}
/// <summary>
/// Opens the connection to the database.
/// </summary>
- /// <param name="userManager">The user manager.</param>
- /// <param name="dbLock">The lock to use for database IO.</param>
- /// <param name="dbConnection">The connection to use for database IO.</param>
- public void Initialize(IUserManager userManager, SemaphoreSlim dbLock, SQLiteDatabaseConnection dbConnection)
+ public override void Initialize()
{
- WriteLock.Dispose();
- WriteLock = dbLock;
- WriteConnection?.Dispose();
- WriteConnection = dbConnection;
+ base.Initialize();
using (var connection = GetConnection())
{
var userDatasTableExists = TableExists(connection, "UserDatas");
var userDataTableExists = TableExists(connection, "userdata");
- var users = userDatasTableExists ? null : userManager.Users;
+ var users = userDatasTableExists ? null : _userManager.Users;
connection.RunInTransaction(
db =>
@@ -371,20 +370,5 @@ namespace Emby.Server.Implementations.Data
return userData;
}
-
-#pragma warning disable CA2215
- /// <inheritdoc/>
- /// <remarks>
- /// There is nothing to dispose here since <see cref="BaseSqliteRepository.WriteLock"/> and
- /// <see cref="BaseSqliteRepository.WriteConnection"/> are managed by <see cref="SqliteItemRepository"/>.
- /// See <see cref="Initialize(IUserManager, SemaphoreSlim, SQLiteDatabaseConnection)"/>.
- /// </remarks>
- protected override void Dispose(bool dispose)
- {
- // The write lock and connection for the item repository are shared with the user data repository
- // since they point to the same database. The item repo has responsibility for disposing these two objects,
- // so the user data repo should not attempt to dispose them as well
- }
-#pragma warning restore CA2215
}
}