Reworked PRAGMA statements use

This commit is contained in:
Bond_009 2019-04-03 17:34:54 +02:00 committed by Bond_009
parent edfd2d0cd9
commit 7898af4ceb
7 changed files with 46 additions and 60 deletions

View File

@ -45,8 +45,6 @@ namespace Emby.Server.Implementations.Activity
{
using (var connection = GetConnection())
{
RunDefaultInitialization(connection);
connection.RunQueries(new[]
{
"create table if not exists ActivityLog (Id INTEGER PRIMARY KEY, Name TEXT NOT NULL, Overview TEXT, ShortOverview TEXT, Type TEXT NOT NULL, ItemId TEXT, UserId TEXT, DateCreated DATETIME NOT NULL, LogSeverity TEXT NOT NULL)",

View File

@ -9,27 +9,37 @@ namespace Emby.Server.Implementations.Data
{
public abstract class BaseSqliteRepository : IDisposable
{
protected string DbFilePath { get; set; }
protected ILogger Logger { get; }
private bool _disposed = false;
protected BaseSqliteRepository(ILogger logger)
{
Logger = logger;
}
protected string DbFilePath { get; set; }
protected ILogger Logger { get; }
protected virtual ConnectionFlags DefaultConnectionFlags => ConnectionFlags.NoMutex;
protected TransactionMode TransactionMode => TransactionMode.Deferred;
protected TransactionMode ReadTransactionMode => TransactionMode.Deferred;
protected virtual ConnectionFlags DefaultConnectionFlags => ConnectionFlags.NoMutex;
protected virtual int? CacheSize => null;
protected virtual string JournalMode => "WAL";
protected virtual int? PageSize => null;
protected virtual TempStoreMode TempStore => TempStoreMode.Default;
protected virtual SynchronousMode? Synchronous => null;
protected SemaphoreSlim WriteLock = new SemaphoreSlim(1, 1);
protected SQLiteDatabaseConnection WriteConnection;
private string _defaultWal;
protected ManagedConnection GetConnection(bool _ = false)
{
WriteLock.Wait();
@ -43,23 +53,28 @@ namespace Emby.Server.Implementations.Data
DefaultConnectionFlags | ConnectionFlags.Create | ConnectionFlags.ReadWrite,
null);
if (string.IsNullOrWhiteSpace(_defaultWal))
if (CacheSize.HasValue)
{
_defaultWal = WriteConnection.Query("PRAGMA journal_mode").SelectScalarString().First();
Logger.LogInformation("Default journal_mode for {0} is {1}", DbFilePath, _defaultWal);
WriteConnection.Execute("PRAGMA cache_size=" + (int)CacheSize.Value);
}
if (EnableTempStoreMemory)
if (!string.IsNullOrWhiteSpace(JournalMode))
{
WriteConnection.Execute("PRAGMA temp_store = memory");
WriteConnection.Execute("PRAGMA journal_mode=" + JournalMode);
}
else
if (Synchronous.HasValue)
{
WriteConnection.Execute("PRAGMA temp_store = file");
WriteConnection.Execute("PRAGMA synchronous=" + (int)Synchronous.Value);
}
if (PageSize.HasValue)
{
WriteConnection.Execute("PRAGMA page_size=" + (int)PageSize.Value);
}
WriteConnection.Execute("PRAGMA temp_store=" + (int)TempStore);
return new ManagedConnection(WriteConnection, WriteLock);
}
@ -92,38 +107,6 @@ namespace Emby.Server.Implementations.Data
}, ReadTransactionMode);
}
protected void RunDefaultInitialization(ManagedConnection db)
{
var queries = new List<string>
{
"PRAGMA journal_mode=WAL",
"PRAGMA page_size=4096",
"PRAGMA synchronous=Normal"
};
if (EnableTempStoreMemory)
{
queries.AddRange(new List<string>
{
"pragma default_temp_store = memory",
"pragma temp_store = memory"
});
}
else
{
queries.AddRange(new List<string>
{
"pragma temp_store = file"
});
}
db.ExecuteAll(string.Join(";", queries));
Logger.LogInformation("PRAGMA synchronous=" + db.Query("PRAGMA synchronous").SelectScalarString().First());
}
protected virtual bool EnableTempStoreMemory => true;
private bool _disposed;
protected void CheckDisposed()
{
if (_disposed)
@ -199,4 +182,19 @@ namespace Emby.Server.Implementations.Data
connection.Execute("alter table " + table + " add column " + columnName + " " + type + " NULL");
}
}
public enum SynchronousMode
{
Off = 0,
Normal = 1,
Full = 2,
Extra = 3
}
public enum TempStoreMode
{
Default = 0,
File = 1,
Memory = 2
}
}

View File

@ -63,8 +63,6 @@ namespace Emby.Server.Implementations.Data
{
using (var connection = GetConnection())
{
RunDefaultInitialization(connection);
string[] queries = {
"create table if not exists userdisplaypreferences (id GUID NOT NULL, userId GUID NOT NULL, client text NOT NULL, data BLOB NOT NULL)",

View File

@ -92,7 +92,7 @@ namespace Emby.Server.Implementations.Data
private const string ChaptersTableName = "Chapters2";
protected override bool EnableTempStoreMemory => true;
protected override TempStoreMode TempStore => TempStoreMode.Memory;
/// <summary>
/// Opens the connection to the database
@ -101,8 +101,6 @@ namespace Emby.Server.Implementations.Data
{
using (var connection = GetConnection())
{
RunDefaultInitialization(connection);
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, PRIMARY KEY (ItemId, StreamIndex))";

View File

@ -128,8 +128,6 @@ namespace Emby.Server.Implementations.Data
return list;
}
protected override bool EnableTempStoreMemory => true;
/// <summary>
/// Saves the user data.
/// </summary>

View File

@ -42,8 +42,6 @@ namespace Emby.Server.Implementations.Data
{
using (var connection = GetConnection())
{
RunDefaultInitialization(connection);
var localUsersTableExists = TableExists(connection, "LocalUsersv2");
connection.RunQueries(new[] {

View File

@ -25,8 +25,6 @@ namespace Emby.Server.Implementations.Security
{
using (var connection = GetConnection())
{
RunDefaultInitialization(connection);
var tableNewlyCreated = !TableExists(connection, "Tokens");
string[] queries = {