From 858dadcdd1caadb5fa8cc13a02eb227098f39c3c Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Fri, 14 Apr 2023 13:43:56 +0200 Subject: POC sql connection pool --- Emby.Server.Implementations/Data/SqliteItemRepository.cs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'Emby.Server.Implementations/Data/SqliteItemRepository.cs') diff --git a/Emby.Server.Implementations/Data/SqliteItemRepository.cs b/Emby.Server.Implementations/Data/SqliteItemRepository.cs index e59f2a198..33466c34b 100644 --- a/Emby.Server.Implementations/Data/SqliteItemRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteItemRepository.cs @@ -336,6 +336,7 @@ namespace Emby.Server.Implementations.Data _jsonOptions = JsonDefaults.Options; DbFilePath = Path.Combine(_config.ApplicationPaths.DataPath, "library.db"); + ReadConnectionsCount = 5; } /// @@ -347,10 +348,10 @@ namespace Emby.Server.Implementations.Data /// /// Opens the connection to the database. /// - /// The user data repository. - /// The user manager. - 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))"; @@ -551,8 +552,6 @@ namespace Emby.Server.Implementations.Data connection.RunQueries(postQueries); } - - userDataRepo.Initialize(userManager, WriteLock, WriteConnection); } public void SaveImages(BaseItem item) -- cgit v1.2.3 From 13152bf09dcadf8807e1240be7ca84662d2ed397 Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Fri, 21 Apr 2023 14:05:27 +0200 Subject: Change number of read connections based on # of threads and add comments --- Emby.Server.Implementations/Data/ConnectionPool.cs | 19 +++++++++++++++++-- .../Data/SqliteItemRepository.cs | 2 +- 2 files changed, 18 insertions(+), 3 deletions(-) (limited to 'Emby.Server.Implementations/Data/SqliteItemRepository.cs') diff --git a/Emby.Server.Implementations/Data/ConnectionPool.cs b/Emby.Server.Implementations/Data/ConnectionPool.cs index 091a1b74f..6d28a5e43 100644 --- a/Emby.Server.Implementations/Data/ConnectionPool.cs +++ b/Emby.Server.Implementations/Data/ConnectionPool.cs @@ -1,16 +1,22 @@ -#pragma warning disable CS1591 - using System; using System.Collections.Concurrent; using SQLitePCL.pretty; namespace Emby.Server.Implementations.Data; +/// +/// A pool of SQLite Database connections. +/// public sealed class ConnectionPool : IDisposable { private readonly BlockingCollection _connections = new(); private bool _disposed; + /// + /// Initializes a new instance of the class. + /// + /// The number of database connection to create. + /// Factory function to create the database connections. public ConnectionPool(int count, Func factory) { for (int i = 0; i < count; i++) @@ -19,6 +25,10 @@ public sealed class ConnectionPool : IDisposable } } + /// + /// Gets a database connection from the pool if one is available, otherwise blocks. + /// + /// A database connection. public ManagedConnection GetConnection() { if (_disposed) @@ -34,6 +44,10 @@ public sealed class ConnectionPool : IDisposable } } + /// + /// Return a database connection to the pool. + /// + /// The database connection to return. public void Return(SQLiteDatabaseConnection connection) { if (_disposed) @@ -45,6 +59,7 @@ public sealed class ConnectionPool : IDisposable _connections.Add(connection); } + /// public void Dispose() { if (_disposed) diff --git a/Emby.Server.Implementations/Data/SqliteItemRepository.cs b/Emby.Server.Implementations/Data/SqliteItemRepository.cs index 33466c34b..22d485d33 100644 --- a/Emby.Server.Implementations/Data/SqliteItemRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteItemRepository.cs @@ -336,7 +336,7 @@ namespace Emby.Server.Implementations.Data _jsonOptions = JsonDefaults.Options; DbFilePath = Path.Combine(_config.ApplicationPaths.DataPath, "library.db"); - ReadConnectionsCount = 5; + ReadConnectionsCount = Environment.ProcessorCount * 2; } /// -- cgit v1.2.3