Merge pull request #956 from Bond-009/db

Simplify db code
pull/1397/head
dkanada 5 years ago committed by GitHub
commit e722801f80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -15,14 +15,14 @@ namespace Emby.Server.Implementations.Activity
{
public class ActivityRepository : BaseSqliteRepository, IActivityRepository
{
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
protected IFileSystem FileSystem { get; private set; }
private static readonly CultureInfo _usCulture = CultureInfo.ReadOnly(new CultureInfo("en-US"));
private readonly IFileSystem _fileSystem;
public ActivityRepository(ILoggerFactory loggerFactory, IServerApplicationPaths appPaths, IFileSystem fileSystem)
: base(loggerFactory.CreateLogger(nameof(ActivityRepository)))
{
DbFilePath = Path.Combine(appPaths.DataPath, "activitylog.db");
FileSystem = fileSystem;
_fileSystem = fileSystem;
}
public void Initialize()
@ -35,7 +35,7 @@ namespace Emby.Server.Implementations.Activity
{
Logger.LogError(ex, "Error loading database file. Will reset and retry.");
FileSystem.DeleteFile(DbFilePath);
_fileSystem.DeleteFile(DbFilePath);
InitializeInternal();
}
@ -43,10 +43,8 @@ namespace Emby.Server.Implementations.Activity
private void InitializeInternal()
{
using (var connection = CreateConnection())
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)",
@ -85,8 +83,7 @@ namespace Emby.Server.Implementations.Activity
throw new ArgumentNullException(nameof(entry));
}
using (WriteLock.Write())
using (var connection = CreateConnection())
using (var connection = GetConnection())
{
connection.RunInTransaction(db =>
{
@ -124,8 +121,7 @@ namespace Emby.Server.Implementations.Activity
throw new ArgumentNullException(nameof(entry));
}
using (WriteLock.Write())
using (var connection = CreateConnection())
using (var connection = GetConnection())
{
connection.RunInTransaction(db =>
{
@ -159,8 +155,7 @@ namespace Emby.Server.Implementations.Activity
public QueryResult<ActivityLogEntry> GetActivityLogEntries(DateTime? minDate, bool? hasUserId, int? startIndex, int? limit)
{
using (WriteLock.Read())
using (var connection = CreateConnection(true))
using (var connection = GetConnection(true))
{
var commandText = BaseActivitySelectText;
var whereClauses = new List<string>();
@ -218,7 +213,7 @@ namespace Emby.Server.Implementations.Activity
var list = new List<ActivityLogEntry>();
var result = new QueryResult<ActivityLogEntry>();
var statements = PrepareAllSafe(db, statementTexts).ToList();
var statements = PrepareAll(db, statementTexts).ToList();
using (var statement = statements[0])
{

@ -754,10 +754,6 @@ namespace Emby.Server.Implementations
UserDataManager = new UserDataManager(LoggerFactory, ServerConfigurationManager, () => UserManager);
serviceCollection.AddSingleton(UserDataManager);
UserRepository = GetUserRepository();
// This is only needed for disposal purposes. If removing this, make sure to have the manager handle disposing it
serviceCollection.AddSingleton(UserRepository);
var displayPreferencesRepo = new SqliteDisplayPreferencesRepository(LoggerFactory, JsonSerializer, ApplicationPaths, FileSystemManager);
serviceCollection.AddSingleton<IDisplayPreferencesRepository>(displayPreferencesRepo);
@ -767,6 +763,8 @@ namespace Emby.Server.Implementations
AuthenticationRepository = GetAuthenticationRepository();
serviceCollection.AddSingleton(AuthenticationRepository);
UserRepository = GetUserRepository();
UserManager = new UserManager(LoggerFactory, ServerConfigurationManager, UserRepository, XmlSerializer, NetworkManager, () => ImageProcessor, () => DtoService, this, JsonSerializer, FileSystemManager);
serviceCollection.AddSingleton(UserManager);
@ -807,7 +805,6 @@ namespace Emby.Server.Implementations
serviceCollection.AddSingleton(TVSeriesManager);
DeviceManager = new DeviceManager(AuthenticationRepository, JsonSerializer, LibraryManager, LocalizationManager, UserManager, FileSystemManager, LibraryMonitor, ServerConfigurationManager);
serviceCollection.AddSingleton(DeviceManager);
MediaSourceManager = new MediaSourceManager(ItemRepository, ApplicationPaths, LocalizationManager, UserManager, LibraryManager, LoggerFactory, JsonSerializer, FileSystemManager, UserDataManager, () => MediaEncoder);
@ -1893,8 +1890,12 @@ namespace Emby.Server.Implementations
Logger.LogError(ex, "Error disposing {Type}", part.GetType().Name);
}
}
UserRepository.Dispose();
}
UserRepository = null;
_disposed = true;
}
}

@ -1,183 +1,141 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using Microsoft.Extensions.Logging;
using SQLitePCL;
using SQLitePCL.pretty;
namespace Emby.Server.Implementations.Data
{
public abstract class BaseSqliteRepository : IDisposable
{
protected string DbFilePath { get; set; }
protected ReaderWriterLockSlim WriteLock;
protected ILogger Logger { get; private set; }
private bool _disposed = false;
protected BaseSqliteRepository(ILogger logger)
{
Logger = logger;
WriteLock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
}
protected TransactionMode TransactionMode => TransactionMode.Deferred;
protected TransactionMode ReadTransactionMode => 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);
//rc = raw.sqlite3_config(raw.SQLITE_CONFIG_SINGLETHREAD, 1);
//rc = raw.sqlite3_config(raw.SQLITE_CONFIG_SERIALIZED, 1);
//CheckOk(rc);
rc = raw.sqlite3_enable_shared_cache(1);
/// <summary>
/// Gets or sets the path to the DB file.
/// </summary>
/// <value>Path to the DB file.</value>
protected string DbFilePath { get; set; }
ThreadSafeMode = raw.sqlite3_threadsafe();
}
/// <summary>
/// Gets the logger.
/// </summary>
/// <value>The logger.</value>
protected ILogger Logger { get; }
private static bool _versionLogged;
/// <summary>
/// Gets the default connection flags.
/// </summary>
/// <value>The default connection flags.</value>
protected virtual ConnectionFlags DefaultConnectionFlags => ConnectionFlags.NoMutex;
private string _defaultWal;
protected ManagedConnection _connection;
/// <summary>
/// Gets the transaction mode.
/// </summary>
/// <value>The transaction mode.</value>>
protected TransactionMode TransactionMode => TransactionMode.Deferred;
protected virtual bool EnableSingleConnection => true;
/// <summary>
/// Gets the transaction mode for read-only operations.
/// </summary>
/// <value>The transaction mode.</value>
protected TransactionMode ReadTransactionMode => TransactionMode.Deferred;
protected ManagedConnection CreateConnection(bool isReadOnly = false)
{
if (_connection != null)
{
return _connection;
}
/// <summary>
/// Gets the cache size.
/// </summary>
/// <value>The cache size or null.</value>
protected virtual int? CacheSize => null;
lock (WriteLock)
{
if (!_versionLogged)
{
_versionLogged = true;
Logger.LogInformation("Sqlite version: " + SQLite3.Version);
Logger.LogInformation("Sqlite compiler options: " + string.Join(",", SQLite3.CompilerOptions.ToArray()));
}
/// <summary>
/// Gets the journal mode.
/// </summary>
/// <value>The journal mode.</value>
protected virtual string JournalMode => "WAL";
ConnectionFlags connectionFlags;
/// <summary>
/// Gets the page size.
/// </summary>
/// <value>The page size or null.</value>
protected virtual int? PageSize => null;
if (isReadOnly)
{
//Logger.LogInformation("Opening read connection");
//connectionFlags = ConnectionFlags.ReadOnly;
connectionFlags = ConnectionFlags.Create;
connectionFlags |= ConnectionFlags.ReadWrite;
}
else
{
//Logger.LogInformation("Opening write connection");
connectionFlags = ConnectionFlags.Create;
connectionFlags |= ConnectionFlags.ReadWrite;
}
/// <summary>
/// Gets the temp store mode.
/// </summary>
/// <value>The temp store mode.</value>
/// <see cref="TempStoreMode"/>
protected virtual TempStoreMode TempStore => TempStoreMode.Default;
if (EnableSingleConnection)
{
connectionFlags |= ConnectionFlags.PrivateCache;
}
else
{
connectionFlags |= ConnectionFlags.SharedCached;
}
/// <summary>
/// Gets the synchronous mode.
/// </summary>
/// <value>The synchronous mode or null.</value>
/// <see cref="SynchronousMode"/>
protected virtual SynchronousMode? Synchronous => null;
connectionFlags |= ConnectionFlags.NoMutex;
/// <summary>
/// Gets or sets the write lock.
/// </summary>
/// <value>The write lock.</value>
protected SemaphoreSlim WriteLock { get; set; } = new SemaphoreSlim(1, 1);
var db = SQLite3.Open(DbFilePath, connectionFlags, null);
/// <summary>
/// Gets or sets the write connection.
/// </summary>
/// <value>The write connection.</value>
protected SQLiteDatabaseConnection WriteConnection { get; set; }
try
protected ManagedConnection GetConnection(bool _ = false)
{
if (string.IsNullOrWhiteSpace(_defaultWal))
WriteLock.Wait();
if (WriteConnection != null)
{
_defaultWal = db.Query("PRAGMA journal_mode").SelectScalarString().First();
Logger.LogInformation("Default journal_mode for {0} is {1}", DbFilePath, _defaultWal);
return new ManagedConnection(WriteConnection, WriteLock);
}
var queries = new List<string>
{
//"PRAGMA cache size=-10000"
//"PRAGMA read_uncommitted = true",
"PRAGMA synchronous=Normal"
};
WriteConnection = SQLite3.Open(
DbFilePath,
DefaultConnectionFlags | ConnectionFlags.Create | ConnectionFlags.ReadWrite,
null);
if (CacheSize.HasValue)
{
queries.Add("PRAGMA cache_size=" + CacheSize.Value.ToString(CultureInfo.InvariantCulture));
WriteConnection.Execute("PRAGMA cache_size=" + CacheSize.Value);
}
if (EnableTempStoreMemory)
{
queries.Add("PRAGMA temp_store = memory");
}
else
if (!string.IsNullOrWhiteSpace(JournalMode))
{
queries.Add("PRAGMA temp_store = file");
WriteConnection.Execute("PRAGMA journal_mode=" + JournalMode);
}
foreach (var query in queries)
if (Synchronous.HasValue)
{
db.Execute(query);
}
}
catch
{
using (db)
{
WriteConnection.Execute("PRAGMA synchronous=" + (int)Synchronous.Value);
}
throw;
if (PageSize.HasValue)
{
WriteConnection.Execute("PRAGMA page_size=" + PageSize.Value);
}
_connection = new ManagedConnection(db, false);
WriteConnection.Execute("PRAGMA temp_store=" + (int)TempStore);
return _connection;
}
return new ManagedConnection(WriteConnection, WriteLock);
}
public IStatement PrepareStatement(ManagedConnection connection, string sql)
{
return connection.PrepareStatement(sql);
}
public IStatement PrepareStatementSafe(ManagedConnection connection, string sql)
{
return connection.PrepareStatement(sql);
}
=> connection.PrepareStatement(sql);
public IStatement PrepareStatement(IDatabaseConnection connection, string sql)
{
return connection.PrepareStatement(sql);
}
public IStatement PrepareStatementSafe(IDatabaseConnection connection, string sql)
{
return connection.PrepareStatement(sql);
}
public List<IStatement> PrepareAll(IDatabaseConnection connection, IEnumerable<string> sql)
{
return PrepareAllSafe(connection, sql);
}
=> connection.PrepareStatement(sql);
public List<IStatement> PrepareAllSafe(IDatabaseConnection connection, IEnumerable<string> sql)
{
return sql.Select(connection.PrepareStatement).ToList();
}
public IEnumerable<IStatement> PrepareAll(IDatabaseConnection connection, IEnumerable<string> sql)
=> sql.Select(connection.PrepareStatement);
protected bool TableExists(ManagedConnection connection, string name)
{
@ -199,40 +157,33 @@ namespace Emby.Server.Implementations.Data
}, ReadTransactionMode);
}
protected void RunDefaultInitialization(ManagedConnection db)
{
var queries = new List<string>
protected List<string> GetColumnNames(IDatabaseConnection connection, string table)
{
"PRAGMA journal_mode=WAL",
"PRAGMA page_size=4096",
"PRAGMA synchronous=Normal"
};
var columnNames = new List<string>();
if (EnableTempStoreMemory)
foreach (var row in connection.Query("PRAGMA table_info(" + table + ")"))
{
queries.AddRange(new List<string>
if (row[1].SQLiteType != SQLiteType.Null)
{
"pragma default_temp_store = memory",
"pragma temp_store = memory"
});
var name = row[1].ToString();
columnNames.Add(name);
}
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());
return columnNames;
}
protected virtual bool EnableTempStoreMemory => false;
protected void AddColumn(IDatabaseConnection connection, string table, string columnName, string type, List<string> existingColumnNames)
{
if (existingColumnNames.Contains(columnName, StringComparer.OrdinalIgnoreCase))
{
return;
}
protected virtual int? CacheSize => null;
connection.Execute("alter table " + table + " add column " + columnName + " " + type + " NULL");
}
private bool _disposed;
protected void CheckDisposed()
{
if (_disposed)
@ -241,139 +192,94 @@ namespace Emby.Server.Implementations.Data
}
}
/// <inheritdoc />
public void Dispose()
{
_disposed = true;
Dispose(true);
GC.SuppressFinalize(this);
}
private readonly object _disposeLock = new object();
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool dispose)
{
if (dispose)
if (_disposed)
{
DisposeConnection();
}
return;
}
private void DisposeConnection()
if (dispose)
{
WriteLock.Wait();
try
{
lock (_disposeLock)
{
using (WriteLock.Write())
{
if (_connection != null)
{
using (_connection)
{
_connection.Close();
WriteConnection.Dispose();
}
_connection = null;
}
CloseConnection();
}
}
}
catch (Exception ex)
finally
{
Logger.LogError(ex, "Error disposing database");
WriteLock.Release();
}
}
protected virtual void CloseConnection()
{
WriteLock.Dispose();
}
protected List<string> GetColumnNames(IDatabaseConnection connection, string table)
{
var list = new List<string>();
foreach (var row in connection.Query("PRAGMA table_info(" + table + ")"))
{
if (row[1].SQLiteType != SQLiteType.Null)
{
var name = row[1].ToString();
WriteConnection = null;
WriteLock = null;
list.Add(name);
}
_disposed = true;
}
return list;
}
protected void AddColumn(IDatabaseConnection connection, string table, string columnName, string type, List<string> existingColumnNames)
{
if (existingColumnNames.Contains(columnName, StringComparer.OrdinalIgnoreCase))
/// <summary>
/// The disk synchronization mode, controls how aggressively SQLite will write data
/// all the way out to physical storage.
/// </summary>
public enum SynchronousMode
{
return;
}
/// <summary>
/// SQLite continues without syncing as soon as it has handed data off to the operating system
/// </summary>
Off = 0,
connection.Execute("alter table " + table + " add column " + columnName + " " + type + " NULL");
}
}
/// <summary>
/// SQLite database engine will still sync at the most critical moments
/// </summary>
Normal = 1,
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;
}
}
}
/// <summary>
/// SQLite database engine will use the xSync method of the VFS
/// to ensure that all content is safely written to the disk surface prior to continuing.
/// </summary>
Full = 2,
public static IDisposable Read(this ReaderWriterLockSlim obj)
{
//if (BaseSqliteRepository.ThreadSafeMode > 0)
//{
// return new DummyToken();
//}
return new WriteLockToken(obj);
/// <summary>
/// EXTRA synchronous is like FULL with the addition that the directory containing a rollback journal
/// is synced after that journal is unlinked to commit a transaction in DELETE mode.
/// </summary>
Extra = 3
}
public static IDisposable Write(this ReaderWriterLockSlim obj)
/// <summary>
/// Storage mode used by temporary database files.
/// </summary>
public enum TempStoreMode
{
//if (BaseSqliteRepository.ThreadSafeMode > 0)
//{
// return new DummyToken();
//}
return new WriteLockToken(obj);
}
/// <summary>
/// The compile-time C preprocessor macro SQLITE_TEMP_STORE
/// is used to determine where temporary tables and indices are stored.
/// </summary>
Default = 0,
/// <summary>
/// Temporary tables and indices are stored in a file.
/// </summary>
File = 1,
/// <summary>
/// Temporary tables and indices are kept in as if they were pure in-memory databases memory.
/// </summary>
Memory = 2
}
}

@ -1,79 +1,78 @@
using System;
using System.Collections.Generic;
using System.Threading;
using SQLitePCL.pretty;
namespace Emby.Server.Implementations.Data
{
public class ManagedConnection : IDisposable
{
private SQLiteDatabaseConnection db;
private readonly bool _closeOnDispose;
private SQLiteDatabaseConnection _db;
private readonly SemaphoreSlim _writeLock;
private bool _disposed = false;
public ManagedConnection(SQLiteDatabaseConnection db, bool closeOnDispose)
public ManagedConnection(SQLiteDatabaseConnection db, SemaphoreSlim writeLock)
{
this.db = db;
_closeOnDispose = closeOnDispose;
_db = db;
_writeLock = writeLock;
}
public IStatement PrepareStatement(string sql)
{
return db.PrepareStatement(sql);
return _db.PrepareStatement(sql);
}
public IEnumerable<IStatement> PrepareAll(string sql)
{
return db.PrepareAll(sql);
return _db.PrepareAll(sql);
}
public void ExecuteAll(string sql)
{
db.ExecuteAll(sql);
_db.ExecuteAll(sql);
}
public void Execute(string sql, params object[] values)
{
db.Execute(sql, values);
_db.Execute(sql, values);
}
public void RunQueries(string[] sql)
{
db.RunQueries(sql);
_db.RunQueries(sql);
}
public void RunInTransaction(Action<IDatabaseConnection> action, TransactionMode mode)
{
db.RunInTransaction(action, mode);
_db.RunInTransaction(action, mode);
}
public T RunInTransaction<T>(Func<IDatabaseConnection, T> action, TransactionMode mode)
{
return db.RunInTransaction(action, mode);
return _db.RunInTransaction(action, mode);
}
public IEnumerable<IReadOnlyList<IResultSetValue>> Query(string sql)
{
return db.Query(sql);
return _db.Query(sql);
}
public IEnumerable<IReadOnlyList<IResultSetValue>> Query(string sql, params object[] values)
{
return db.Query(sql, values);
}
public void Close()
{
using (db)
{
}
return _db.Query(sql, values);
}
public void Dispose()
{
if (_closeOnDispose)
if (_disposed)
{
Close();
return;
}
_writeLock.Release();
_db = null; // Don't dispose it
_disposed = true;
}
}
}

@ -18,13 +18,13 @@ namespace Emby.Server.Implementations.Data
/// </summary>
public class SqliteDisplayPreferencesRepository : BaseSqliteRepository, IDisplayPreferencesRepository
{
protected IFileSystem FileSystem { get; private set; }
private readonly IFileSystem _fileSystem;
public SqliteDisplayPreferencesRepository(ILoggerFactory loggerFactory, IJsonSerializer jsonSerializer, IApplicationPaths appPaths, IFileSystem fileSystem)
: base(loggerFactory.CreateLogger(nameof(SqliteDisplayPreferencesRepository)))
{
_jsonSerializer = jsonSerializer;
FileSystem = fileSystem;
_fileSystem = fileSystem;
DbFilePath = Path.Combine(appPaths.DataPath, "displaypreferences.db");
}
@ -49,7 +49,7 @@ namespace Emby.Server.Implementations.Data
{
Logger.LogError(ex, "Error loading database file. Will reset and retry.");
FileSystem.DeleteFile(DbFilePath);
_fileSystem.DeleteFile(DbFilePath);
InitializeInternal();
}
@ -61,10 +61,8 @@ namespace Emby.Server.Implementations.Data
/// <returns>Task.</returns>
private void InitializeInternal()
{
using (var connection = CreateConnection())
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)",
@ -98,9 +96,7 @@ namespace Emby.Server.Implementations.Data
cancellationToken.ThrowIfCancellationRequested();
using (WriteLock.Write())
{
using (var connection = CreateConnection())
using (var connection = GetConnection())
{
connection.RunInTransaction(db =>
{
@ -108,7 +104,6 @@ namespace Emby.Server.Implementations.Data
}, TransactionMode);
}
}
}
private void SaveDisplayPreferences(DisplayPreferences displayPreferences, Guid userId, string client, IDatabaseConnection connection)
{
@ -142,9 +137,7 @@ namespace Emby.Server.Implementations.Data
cancellationToken.ThrowIfCancellationRequested();
using (WriteLock.Write())
{
using (var connection = CreateConnection())
using (var connection = GetConnection())
{
connection.RunInTransaction(db =>
{
@ -155,7 +148,6 @@ namespace Emby.Server.Implementations.Data
}, TransactionMode);
}
}
}
/// <summary>
/// Gets the display preferences.
@ -174,9 +166,7 @@ namespace Emby.Server.Implementations.Data
var guidId = displayPreferencesId.GetMD5();
using (WriteLock.Read())
{
using (var connection = CreateConnection(true))
using (var connection = GetConnection(true))
{
using (var statement = connection.PrepareStatement("select data from userdisplaypreferences where id = @id and userId=@userId and client=@client"))
{
@ -196,7 +186,6 @@ namespace Emby.Server.Implementations.Data
};
}
}
}
/// <summary>
/// Gets all display preferences for the given user.
@ -208,9 +197,7 @@ namespace Emby.Server.Implementations.Data
{
var list = new List<DisplayPreferences>();
using (WriteLock.Read())
{
using (var connection = CreateConnection(true))
using (var connection = GetConnection(true))
{
using (var statement = connection.PrepareStatement("select data from userdisplaypreferences where userId=@userId"))
{
@ -222,7 +209,6 @@ namespace Emby.Server.Implementations.Data
}
}
}
}
return list;
}

@ -141,7 +141,7 @@ namespace Emby.Server.Implementations.Data
}
}
public static void Attach(ManagedConnection db, string path, string alias)
public static void Attach(SQLiteDatabaseConnection db, string path, string alias)
{
var commandText = string.Format("attach @path as {0};", alias);

@ -36,13 +36,9 @@ namespace Emby.Server.Implementations.Data
/// </summary>
public class SqliteItemRepository : BaseSqliteRepository, IItemRepository
{
private readonly TypeMapper _typeMapper;
private const string ChaptersTableName = "Chapters2";
/// <summary>
/// Gets the name of the repository
/// </summary>
/// <value>The name.</value>
public string Name => "SQLite";
private readonly TypeMapper _typeMapper;
/// <summary>
/// Gets the json serializer.
@ -54,12 +50,9 @@ namespace Emby.Server.Implementations.Data
/// The _app paths
/// </summary>
private readonly IServerConfigurationManager _config;
private IServerApplicationHost _appHost;
private readonly IServerApplicationHost _appHost;
private readonly ILocalizationManager _localization;
public IImageProcessor ImageProcessor { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="SqliteItemRepository"/> class.
/// </summary>
@ -90,21 +83,24 @@ namespace Emby.Server.Implementations.Data
DbFilePath = Path.Combine(_config.ApplicationPaths.DataPath, "library.db");
}
private const string ChaptersTableName = "Chapters2";
/// <inheritdoc />
public string Name => "SQLite";
/// <inheritdoc />
protected override int? CacheSize => 20000;
protected override bool EnableTempStoreMemory => true;
/// <inheritdoc />
protected override TempStoreMode TempStore => TempStoreMode.Memory;
public IImageProcessor ImageProcessor { get; set; }
/// <summary>
/// Opens the connection to the database
/// </summary>
public void Initialize(SqliteUserDataRepository userDataRepo, IUserManager userManager)
{
using (var connection = CreateConnection())
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))";
@ -319,7 +315,7 @@ namespace Emby.Server.Implementations.Data
connection.RunQueries(postQueries);
}
userDataRepo.Initialize(WriteLock, _connection, userManager);
userDataRepo.Initialize(userManager, WriteLock, WriteConnection);
}
private static readonly string[] _retriveItemColumns =
@ -520,10 +516,11 @@ namespace Emby.Server.Implementations.Data
{
saveItemCommandCommandText += ",";
}
saveItemCommandCommandText += "@" + saveColumns[i];
}
saveItemCommandCommandText += ")";
return saveItemCommandCommandText;
return saveItemCommandCommandText + ")";
}
/// <summary>
@ -551,13 +548,11 @@ namespace Emby.Server.Implementations.Data
CheckDisposed();
using (WriteLock.Write())
{
using (var connection = CreateConnection())
using (var connection = GetConnection())
{
connection.RunInTransaction(db =>
{
using (var saveImagesStatement = PrepareStatement(db, "Update TypedBaseItems set Images=@Images where guid=@Id"))
using (var saveImagesStatement = base.PrepareStatement(db, "Update TypedBaseItems set Images=@Images where guid=@Id"))
{
saveImagesStatement.TryBind("@Id", item.Id.ToGuidBlob());
saveImagesStatement.TryBind("@Images", SerializeImages(item));
@ -567,7 +562,6 @@ namespace Emby.Server.Implementations.Data
}, TransactionMode);
}
}
}
/// <summary>
/// Saves the items.
@ -605,26 +599,22 @@ namespace Emby.Server.Implementations.Data
tuples.Add((item, ancestorIds, topParent, userdataKey, inheritedTags));
}
using (WriteLock.Write())
{
using (var connection = CreateConnection())
using (var connection = GetConnection())
{
connection.RunInTransaction(db =>
{
SaveItemsInTranscation(db, tuples);
}, TransactionMode);
}
}
}
private void SaveItemsInTranscation(IDatabaseConnection db, IEnumerable<(BaseItem, List<Guid>, BaseItem, string, List<string>)> tuples)
{
var statements = PrepareAllSafe(db, new string[]
var statements = PrepareAll(db, new string[]
{
GetSaveItemCommandText(),
"delete from AncestorIds where ItemId=@ItemId"
});
}).ToList();
using (var saveItemStatement = statements[0])
using (var deleteAncestorsStatement = statements[1])
@ -999,6 +989,7 @@ namespace Emby.Server.Implementations.Data
{
albumArtists = string.Join("|", hasAlbumArtists.AlbumArtists);
}
saveItemStatement.TryBind("@AlbumArtists", albumArtists);
saveItemStatement.TryBind("@ExternalId", item.ExternalId);
@ -1035,6 +1026,7 @@ namespace Emby.Server.Implementations.Data
{
continue;
}
str.Append($"{i.Key}={i.Value}|");
}
@ -1042,6 +1034,7 @@ namespace Emby.Server.Implementations.Data
{
return null;
}
str.Length -= 1; // Remove last |
return str.ToString();
}
@ -1079,6 +1072,7 @@ namespace Emby.Server.Implementations.Data
{
return null;
}
StringBuilder str = new StringBuilder();
foreach (var i in images)
{
@ -1088,6 +1082,7 @@ namespace Emby.Server.Implementations.Data
}
str.Append(ToValueString(i) + "|");
}
str.Length -= 1; // Remove last |
return str.ToString();
}
@ -1193,11 +1188,9 @@ namespace Emby.Server.Implementations.Data
CheckDisposed();
using (WriteLock.Read())
using (var connection = GetConnection(true))
{
using (var connection = CreateConnection(true))
{
using (var statement = PrepareStatementSafe(connection, "select " + string.Join(",", _retriveItemColumns) + " from TypedBaseItems where guid = @guid"))
using (var statement = PrepareStatement(connection, "select " + string.Join(",", _retriveItemColumns) + " from TypedBaseItems where guid = @guid"))
{
statement.TryBind("@guid", id);
@ -1206,11 +1199,10 @@ namespace Emby.Server.Implementations.Data
return GetItem(row, new InternalItemsQuery());
}
}
}
return null;
}
}
}
private bool TypeRequiresDeserialization(Type type)
{
@ -1909,24 +1901,21 @@ namespace Emby.Server.Implementations.Data
{
CheckDisposed();
using (WriteLock.Read())
{
using (var connection = CreateConnection(true))
using (var connection = GetConnection(true))
{
var list = new List<ChapterInfo>();
var chapters = new List<ChapterInfo>();
using (var statement = PrepareStatementSafe(connection, "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId order by ChapterIndex asc"))
using (var statement = PrepareStatement(connection, "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId order by ChapterIndex asc"))
{
statement.TryBind("@ItemId", item.Id);
foreach (var row in statement.ExecuteQuery())
{
list.Add(GetChapter(row, item));
chapters.Add(GetChapter(row, item));
}
}
return list;
}
return chapters;
}
}
@ -1941,11 +1930,9 @@ namespace Emby.Server.Implementations.Data
{
CheckDisposed();
using (WriteLock.Read())
using (var connection = GetConnection(true))
{
using (var connection = CreateConnection(true))
{
using (var statement = PrepareStatementSafe(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);
@ -1956,7 +1943,7 @@ namespace Emby.Server.Implementations.Data
}
}
}
}
return null;
}
@ -2012,9 +1999,7 @@ namespace Emby.Server.Implementations.Data
throw new ArgumentNullException(nameof(chapters));
}
using (WriteLock.Write())
{
using (var connection = CreateConnection())
using (var connection = GetConnection())
{
connection.RunInTransaction(db =>
{
@ -2028,7 +2013,6 @@ namespace Emby.Server.Implementations.Data
}, TransactionMode);
}
}
}
private void InsertChapters(byte[] idBlob, List<ChapterInfo> chapters, IDatabaseConnection db)
{
@ -2048,7 +2032,7 @@ namespace Emby.Server.Implementations.Data
}
insertText.Length -= 1; // Remove last ,
using (var statement = PrepareStatementSafe(db, insertText.ToString()))
using (var statement = PrepareStatement(db, insertText.ToString()))
{
statement.TryBind("@ItemId", idBlob);
@ -2551,11 +2535,9 @@ namespace Emby.Server.Implementations.Data
commandText += " where " + string.Join(" AND ", whereClauses);
}
using (WriteLock.Read())
{
using (var connection = CreateConnection(true))
using (var connection = GetConnection(true))
{
using (var statement = PrepareStatementSafe(connection, commandText))
using (var statement = PrepareStatement(connection, commandText))
{
if (EnableJoinUserData(query))
{
@ -2573,8 +2555,6 @@ namespace Emby.Server.Implementations.Data
return count;
}
}
}
}
public List<BaseItem> GetItemList(InternalItemsQuery query)
@ -2624,13 +2604,11 @@ namespace Emby.Server.Implementations.Data
}
}
using (WriteLock.Read())
{
using (var connection = CreateConnection(true))
using (var connection = GetConnection(true))
{
var list = new List<BaseItem>();
var items = new List<BaseItem>();
using (var statement = PrepareStatementSafe(connection, commandText))
using (var statement = PrepareStatement(connection, commandText))
{
if (EnableJoinUserData(query))
{
@ -2656,7 +2634,7 @@ namespace Emby.Server.Implementations.Data
var item = GetItem(row, query, hasProgramAttributes, hasEpisodeAttributes, hasServiceName, hasStartDate, hasTrailerTypes, hasArtistFields, hasSeriesFields);
if (item != null)
{
list.Add(item);
items.Add(item);
}
}
}
@ -2668,7 +2646,7 @@ namespace Emby.Server.Implementations.Data
limit -= 4;
var newList = new List<BaseItem>();
foreach (var item in list)
foreach (var item in items)
{
AddItem(newList, item);
@ -2678,13 +2656,12 @@ namespace Emby.Server.Implementations.Data
}
}
list = newList;
items = newList;
}
LogQueryTime("GetItemList", commandText, now);
return list;
}
return items;
}
}
@ -2845,14 +2822,12 @@ namespace Emby.Server.Implementations.Data
statementTexts.Add(commandText);
}
using (WriteLock.Read())
{
using (var connection = CreateConnection(true))
using (var connection = GetConnection(true))
{
return connection.RunInTransaction(db =>
{
var result = new QueryResult<BaseItem>();
var statements = PrepareAllSafe(db, statementTexts);
var statements = PrepareAll(db, statementTexts).ToList();
if (!isReturningZeroItems)
{
@ -2915,7 +2890,6 @@ namespace Emby.Server.Implementations.Data
}, ReadTransactionMode);
}
}
}
private string GetOrderByText(InternalItemsQuery query)
{
@ -3080,13 +3054,11 @@ namespace Emby.Server.Implementations.Data
}
}
using (WriteLock.Read())
{
using (var connection = CreateConnection(true))
using (var connection = GetConnection(true))
{
var list = new List<Guid>();
using (var statement = PrepareStatementSafe(connection, commandText))
using (var statement = PrepareStatement(connection, commandText))
{
if (EnableJoinUserData(query))
{
@ -3110,7 +3082,6 @@ namespace Emby.Server.Implementations.Data
return list;
}
}
}
public List<Tuple<Guid, string>> GetItemIdsWithPath(InternalItemsQuery query)
{
@ -3149,12 +3120,10 @@ namespace Emby.Server.Implementations.Data
}
}
using (WriteLock.Read())
{
var list = new List<Tuple<Guid, string>>();
using (var connection = CreateConnection(true))
using (var connection = GetConnection(true))
{
using (var statement = PrepareStatementSafe(connection, commandText))
using (var statement = PrepareStatement(connection, commandText))
{
if (EnableJoinUserData(query))
{
@ -3182,7 +3151,6 @@ namespace Emby.Server.Implementations.Data
return list;
}
}
public QueryResult<Guid> GetItemIds(InternalItemsQuery query)
{
@ -3265,15 +3233,13 @@ namespace Emby.Server.Implementations.Data
statementTexts.Add(commandText);
}
using (WriteLock.Read())
{
using (var connection = CreateConnection(true))
using (var connection = GetConnection(true))
{
return connection.RunInTransaction(db =>
{
var result = new QueryResult<Guid>();
var statements = PrepareAllSafe(db, statementTexts);
var statements = PrepareAll(db, statementTexts).ToList();
if (!isReturningZeroItems)
{
@ -3324,7 +3290,6 @@ namespace Emby.Server.Implementations.Data
}, ReadTransactionMode);
}
}
}
private bool IsAlphaNumeric(string str)
{
@ -4899,9 +4864,7 @@ namespace Emby.Server.Implementations.Data
private void UpdateInheritedTags(CancellationToken cancellationToken)
{
using (WriteLock.Write())
{
using (var connection = CreateConnection())
using (var connection = GetConnection())
{
connection.RunInTransaction(db =>
{
@ -4921,7 +4884,6 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
}, TransactionMode);
}
}
}
private static Dictionary<string, string[]> GetTypeMapDictionary()
{
@ -4965,9 +4927,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
CheckDisposed();
using (WriteLock.Write())
{
using (var connection = CreateConnection())
using (var connection = GetConnection())
{
connection.RunInTransaction(db =>
{
@ -4993,7 +4953,6 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
}, TransactionMode);
}
}
}
private void ExecuteWithSingleParam(IDatabaseConnection db, string query, byte[] value)
{
@ -5025,12 +4984,10 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
commandText += " order by ListOrder";
using (WriteLock.Read())
{
using (var connection = CreateConnection(true))
using (var connection = GetConnection(true))
{
var list = new List<string>();
using (var statement = PrepareStatementSafe(connection, commandText))
using (var statement = PrepareStatement(connection, commandText))
{
// Run this again to bind the params
GetPeopleWhereClauses(query, statement);
@ -5043,7 +5000,6 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
return list;
}
}
}
public List<PersonInfo> GetPeople(InternalPeopleQuery query)
{
@ -5065,13 +5021,11 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
commandText += " order by ListOrder";
using (WriteLock.Read())
{
using (var connection = CreateConnection(true))
using (var connection = GetConnection(true))
{
var list = new List<PersonInfo>();
using (var statement = PrepareStatementSafe(connection, commandText))
using (var statement = PrepareStatement(connection, commandText))
{
// Run this again to bind the params
GetPeopleWhereClauses(query, statement);
@ -5085,7 +5039,6 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
return list;
}
}
}
private List<string> GetPeopleWhereClauses(InternalPeopleQuery query, IStatement statement)
{
@ -5197,7 +5150,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
insertText.AppendFormat("(@ItemId, @AncestorId{0}, @AncestorIdText{0})", i.ToString(CultureInfo.InvariantCulture));
}
using (var statement = PrepareStatementSafe(db, insertText.ToString()))
using (var statement = PrepareStatement(db, insertText.ToString()))
{
statement.TryBind("@ItemId", itemIdBlob);
@ -5294,13 +5247,11 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
commandText += " Group By CleanValue";
using (WriteLock.Read())
{
using (var connection = CreateConnection(true))
using (var connection = GetConnection(true))
{
var list = new List<string>();
using (var statement = PrepareStatementSafe(connection, commandText))
using (var statement = PrepareStatement(connection, commandText))
{
foreach (var row in statement.ExecuteQuery())
{
@ -5316,7 +5267,6 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
return list;
}
}
}
private QueryResult<(BaseItem, ItemCounts)> GetItemValues(InternalItemsQuery query, int[] itemValueTypes, string returnType)
{
@ -5483,16 +5433,14 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
statementTexts.Add(countText);
}
using (WriteLock.Read())
{
using (var connection = CreateConnection(true))
using (var connection = GetConnection(true))
{
return connection.RunInTransaction(db =>
{
var list = new List<(BaseItem, ItemCounts)>();
var result = new QueryResult<(BaseItem, ItemCounts)>();
var statements = PrepareAllSafe(db, statementTexts);
var statements = PrepareAll(db, statementTexts).ToList();
if (!isReturningZeroItems)
{
@ -5578,7 +5526,6 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
}, ReadTransactionMode);
}
}
}
private ItemCounts GetItemCounts(IReadOnlyList<IResultSetValue> reader, int countStartColumn, string[] typesToCount)
{
@ -5708,7 +5655,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
isSubsequentRow = true;
}
using (var statement = PrepareStatementSafe(db, insertText.ToString()))
using (var statement = PrepareStatement(db, insertText.ToString()))
{
statement.TryBind("@ItemId", idBlob);
@ -5753,9 +5700,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
CheckDisposed();
using (WriteLock.Write())
{
using (var connection = CreateConnection())
using (var connection = GetConnection())
{
connection.RunInTransaction(db =>
{
@ -5767,8 +5712,6 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
InsertPeople(itemIdBlob, people, db);
}, TransactionMode);
}
}
}
@ -5796,7 +5739,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
isSubsequentRow = true;
}
using (var statement = PrepareStatementSafe(db, insertText.ToString()))
using (var statement = PrepareStatement(db, insertText.ToString()))
{
statement.TryBind("@ItemId", idBlob);
@ -5874,13 +5817,11 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
cmdText += " order by StreamIndex ASC";
using (WriteLock.Read())
{
using (var connection = CreateConnection(true))
using (var connection = GetConnection(true))
{
var list = new List<MediaStream>();
using (var statement = PrepareStatementSafe(connection, cmdText))
using (var statement = PrepareStatement(connection, cmdText))
{
statement.TryBind("@ItemId", query.ItemId.ToGuidBlob());
@ -5903,7 +5844,6 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
return list;
}
}
}
public void SaveMediaStreams(Guid id, List<MediaStream> streams, CancellationToken cancellationToken)
{
@ -5921,9 +5861,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
cancellationToken.ThrowIfCancellationRequested();
using (WriteLock.Write())
{
using (var connection = CreateConnection())
using (var connection = GetConnection())
{
connection.RunInTransaction(db =>
{
@ -5937,7 +5875,6 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
}, TransactionMode);
}
}
}
private void InsertMediaStreams(byte[] idBlob, List<MediaStream> streams, IDatabaseConnection db)
{
@ -5969,7 +5906,7 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type
insertText.Append(")");
}
using (var statement = PrepareStatementSafe(db, insertText.ToString()))
using (var statement = PrepareStatement(db, insertText.ToString()))
{
statement.TryBind("@ItemId", idBlob);

@ -7,7 +7,6 @@ using MediaBrowser.Common.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Model.IO;
using Microsoft.Extensions.Logging;
using SQLitePCL.pretty;
@ -33,14 +32,14 @@ namespace Emby.Server.Implementations.Data
/// Opens the connection to the database
/// </summary>
/// <returns>Task.</returns>
public void Initialize(ReaderWriterLockSlim writeLock, ManagedConnection managedConnection, IUserManager userManager)
public void Initialize(IUserManager userManager, SemaphoreSlim dbLock, SQLiteDatabaseConnection dbConnection)
{
_connection = managedConnection;
WriteLock.Dispose();
WriteLock = writeLock;
WriteLock = dbLock;
WriteConnection?.Dispose();
WriteConnection = dbConnection;
using (var connection = CreateConnection())
using (var connection = GetConnection())
{
var userDatasTableExists = TableExists(connection, "UserDatas");
var userDataTableExists = TableExists(connection, "userdata");
@ -129,8 +128,6 @@ namespace Emby.Server.Implementations.Data
return list;
}
protected override bool EnableTempStoreMemory => true;
/// <summary>
/// Saves the user data.
/// </summary>
@ -178,9 +175,7 @@ namespace Emby.Server.Implementations.Data
{
cancellationToken.ThrowIfCancellationRequested();
using (WriteLock.Write())
{
using (var connection = CreateConnection())
using (var connection = GetConnection())
{
connection.RunInTransaction(db =>
{
@ -188,7 +183,6 @@ namespace Emby.Server.Implementations.Data
}, TransactionMode);
}
}
}
private static void SaveUserData(IDatabaseConnection db, long internalUserId, string key, UserItemData userData)
{
@ -249,9 +243,7 @@ namespace Emby.Server.Implementations.Data
{
cancellationToken.ThrowIfCancellationRequested();
using (WriteLock.Write())
{
using (var connection = CreateConnection())
using (var connection = GetConnection())
{
connection.RunInTransaction(db =>
{
@ -262,7 +254,6 @@ namespace Emby.Server.Implementations.Data
}, TransactionMode);
}
}
}
/// <summary>
/// Gets the user data.
@ -281,14 +272,13 @@ namespace Emby.Server.Implementations.Data
{
throw new ArgumentNullException(nameof(internalUserId));
}
if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException(nameof(key));
}
using (WriteLock.Read())
{
using (var connection = CreateConnection(true))
using (var connection = GetConnection(true))
{
using (var statement = connection.PrepareStatement("select key,userid,rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex from UserDatas where key =@Key and userId=@UserId"))
{
@ -304,7 +294,6 @@ namespace Emby.Server.Implementations.Data
return null;
}
}
}
public UserItemData GetUserData(long internalUserId, List<string> keys)
{
@ -335,9 +324,7 @@ namespace Emby.Server.Implementations.Data
var list = new List<UserItemData>();
using (WriteLock.Read())
{
using (var connection = CreateConnection())
using (var connection = GetConnection())
{
using (var statement = connection.PrepareStatement("select key,userid,rating,played,playCount,isFavorite,playbackPositionTicks,lastPlayedDate,AudioStreamIndex,SubtitleStreamIndex from UserDatas where userId=@UserId"))
{
@ -349,7 +336,6 @@ namespace Emby.Server.Implementations.Data
}
}
}
}
return list;
}
@ -392,15 +378,5 @@ namespace Emby.Server.Implementations.Data
return userData;
}
protected override void Dispose(bool dispose)
{
// handled by library database
}
protected override void CloseConnection()
{
// handled by library database
}
}
}

@ -40,10 +40,8 @@ namespace Emby.Server.Implementations.Data
/// <returns>Task.</returns>
public void Initialize()
{
using (var connection = CreateConnection())
using (var connection = GetConnection())
{
RunDefaultInitialization(connection);
var localUsersTableExists = TableExists(connection, "LocalUsersv2");
connection.RunQueries(new[] {
@ -56,7 +54,7 @@ namespace Emby.Server.Implementations.Data
TryMigrateToLocalUsersTable(connection);
}
RemoveEmptyPasswordHashes();
RemoveEmptyPasswordHashes(connection);
}
}
@ -75,9 +73,9 @@ namespace Emby.Server.Implementations.Data
}
}
private void RemoveEmptyPasswordHashes()
private void RemoveEmptyPasswordHashes(ManagedConnection connection)
{
foreach (var user in RetrieveAllUsers())
foreach (var user in RetrieveAllUsers(connection))
{
// If the user password is the sha1 hash of the empty string, remove it
if (!string.Equals(user.Password, "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709", StringComparison.Ordinal)
@ -89,9 +87,6 @@ namespace Emby.Server.Implementations.Data
user.Password = null;
var serialized = _jsonSerializer.SerializeToBytes(user);
using (WriteLock.Write())
using (var connection = CreateConnection())
{
connection.RunInTransaction(db =>
{
using (var statement = db.PrepareStatement("update LocalUsersv2 set data=@data where Id=@InternalId"))
@ -100,13 +95,10 @@ namespace Emby.Server.Implementations.Data
statement.TryBind("@data", serialized);
statement.MoveNext();
}
}, TransactionMode);
}
}
}
/// <summary>
/// Save a user in the repo
/// </summary>
@ -119,9 +111,7 @@ namespace Emby.Server.Implementations.Data
var serialized = _jsonSerializer.SerializeToBytes(user);
using (WriteLock.Write())
{
using (var connection = CreateConnection())
using (var connection = GetConnection())
{
connection.RunInTransaction(db =>
{
@ -133,7 +123,7 @@ namespace Emby.Server.Implementations.Data
statement.MoveNext();
}
var createdUser = GetUser(user.Id, false);
var createdUser = GetUser(user.Id, connection);
if (createdUser == null)
{
@ -145,7 +135,6 @@ namespace Emby.Server.Implementations.Data
}, TransactionMode);
}
}
}
public void UpdateUser(User user)
{
@ -156,9 +145,7 @@ namespace Emby.Server.Implementations.Data
var serialized = _jsonSerializer.SerializeToBytes(user);
using (WriteLock.Write())
{
using (var connection = CreateConnection())
using (var connection = GetConnection())
{
connection.RunInTransaction(db =>
{
@ -172,13 +159,8 @@ namespace Emby.Server.Implementations.Data
}, TransactionMode);
}
}
}
private User GetUser(Guid guid, bool openLock)
{
using (openLock ? WriteLock.Read() : null)
{
using (var connection = CreateConnection(true))
private User GetUser(Guid guid, ManagedConnection connection)
{
using (var statement = connection.PrepareStatement("select id,guid,data from LocalUsersv2 where guid=@guid"))
{
@ -189,8 +171,6 @@ namespace Emby.Server.Implementations.Data
return GetUser(row);
}
}
}
}
return null;
}
@ -216,20 +196,22 @@ namespace Emby.Server.Implementations.Data
/// <returns>IEnumerable{User}.</returns>
public List<User> RetrieveAllUsers()
{
var list = new List<User>();
using (WriteLock.Read())
using (var connection = GetConnection(true))
{
using (var connection = CreateConnection(true))
return new List<User>(RetrieveAllUsers(connection));
}
}
/// <summary>
/// Retrieve all users from the database
/// </summary>
/// <returns>IEnumerable{User}.</returns>
private IEnumerable<User> RetrieveAllUsers(ManagedConnection connection)
{
foreach (var row in connection.Query("select id,guid,data from LocalUsersv2"))
{
list.Add(GetUser(row));
}
}
yield return GetUser(row);
}
return list;
}
/// <summary>
@ -245,9 +227,7 @@ namespace Emby.Server.Implementations.Data
throw new ArgumentNullException(nameof(user));
}
using (WriteLock.Write())
{
using (var connection = CreateConnection())
using (var connection = GetConnection())
{
connection.RunInTransaction(db =>
{
@ -261,4 +241,3 @@ namespace Emby.Server.Implementations.Data
}
}
}
}

@ -222,9 +222,8 @@ namespace Emby.Server.Implementations.Library
public void Initialize()
{
_users = LoadUsers();
var users = Users.ToList();
var users = LoadUsers();
_users = users.ToArray();
// If there are no local users with admin rights, make them all admins
if (!users.Any(i => i.Policy.IsAdministrator))
@ -555,18 +554,22 @@ namespace Emby.Server.Implementations.Library
/// Loads the users from the repository
/// </summary>
/// <returns>IEnumerable{User}.</returns>
private User[] LoadUsers()
private List<User> LoadUsers()
{
var users = UserRepository.RetrieveAllUsers();
// There always has to be at least one user.
if (users.Count == 0)
if (users.Count != 0)
{
return users;
}
var defaultName = Environment.UserName;
if (string.IsNullOrWhiteSpace(defaultName))
{
defaultName = "MyJellyfinUser";
}
var name = MakeValidUsername(defaultName);
var user = InstantiateNewUser(name);
@ -575,15 +578,12 @@ namespace Emby.Server.Implementations.Library
UserRepository.CreateUser(user);
users.Add(user);
user.Policy.IsAdministrator = true;
user.Policy.EnableContentDeletion = true;
user.Policy.EnableRemoteControlOfOtherUsers = true;
UpdateUserPolicy(user, user.Policy, false);
}
return users.ToArray();
return new List<User> { user };
}
public UserDto GetUserDto(User user, string remoteEndPoint = null)

@ -23,10 +23,8 @@ namespace Emby.Server.Implementations.Security
public void Initialize()
{
using (var connection = CreateConnection())
using (var connection = GetConnection())
{
RunDefaultInitialization(connection);
var tableNewlyCreated = !TableExists(connection, "Tokens");
string[] queries = {
@ -87,9 +85,7 @@ namespace Emby.Server.Implementations.Security
throw new ArgumentNullException(nameof(info));
}
using (WriteLock.Write())
{
using (var connection = CreateConnection())
using (var connection = GetConnection())
{
connection.RunInTransaction(db =>
{
@ -113,7 +109,6 @@ namespace Emby.Server.Implementations.Security
}, TransactionMode);
}
}
}
public void Update(AuthenticationInfo info)
{
@ -122,9 +117,7 @@ namespace Emby.Server.Implementations.Security
throw new ArgumentNullException(nameof(info));
}
using (WriteLock.Write())
{
using (var connection = CreateConnection())
using (var connection = GetConnection())
{
connection.RunInTransaction(db =>
{
@ -148,7 +141,6 @@ namespace Emby.Server.Implementations.Security
}, TransactionMode);
}
}
}
public void Delete(AuthenticationInfo info)
{
@ -157,9 +149,7 @@ namespace Emby.Server.Implementations.Security
throw new ArgumentNullException(nameof(info));
}
using (WriteLock.Write())
{
using (var connection = CreateConnection())
using (var connection = GetConnection())
{
connection.RunInTransaction(db =>
{
@ -172,7 +162,6 @@ namespace Emby.Server.Implementations.Security
}, TransactionMode);
}
}
}
private const string BaseSelectText = "select Tokens.Id, AccessToken, DeviceId, AppName, AppVersion, DeviceName, UserId, UserName, DateCreated, DateLastActivity, Devices.CustomName from Tokens left join Devices on Tokens.DeviceId=Devices.Id";
@ -257,9 +246,7 @@ namespace Emby.Server.Implementations.Security
var list = new List<AuthenticationInfo>();
using (WriteLock.Read())
{
using (var connection = CreateConnection(true))
using (var connection = GetConnection(true))
{
return connection.RunInTransaction(db =>
{
@ -269,7 +256,7 @@ namespace Emby.Server.Implementations.Security
statementTexts.Add(commandText);
statementTexts.Add("select count (Id) from Tokens" + whereTextWithoutPaging);
var statements = PrepareAllSafe(db, statementTexts)
var statements = PrepareAll(db, statementTexts)
.ToList();
using (var statement = statements[0])
@ -297,7 +284,6 @@ namespace Emby.Server.Implementations.Security
}, ReadTransactionMode);
}
}
}
private static AuthenticationInfo Get(IReadOnlyList<IResultSetValue> reader)
{
@ -358,13 +344,11 @@ namespace Emby.Server.Implementations.Security
public DeviceOptions GetDeviceOptions(string deviceId)
{
using (WriteLock.Read())
{
using (var connection = CreateConnection(true))
using (var connection = GetConnection(true))
{
return connection.RunInTransaction(db =>
{
using (var statement = PrepareStatementSafe(db, "select CustomName from Devices where Id=@DeviceId"))
using (var statement = base.PrepareStatement(db, "select CustomName from Devices where Id=@DeviceId"))
{
statement.TryBind("@DeviceId", deviceId);
@ -384,7 +368,6 @@ namespace Emby.Server.Implementations.Security
}, ReadTransactionMode);
}
}
}
public void UpdateDeviceOptions(string deviceId, DeviceOptions options)
{
@ -393,9 +376,7 @@ namespace Emby.Server.Implementations.Security
throw new ArgumentNullException(nameof(options));
}
using (WriteLock.Write())
{
using (var connection = CreateConnection())
using (var connection = GetConnection())
{
connection.RunInTransaction(db =>
{
@ -420,4 +401,3 @@ namespace Emby.Server.Implementations.Security
}
}
}
}

@ -24,6 +24,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.AspNetCore;
using SQLitePCL;
using ILogger = Microsoft.Extensions.Logging.ILogger;
namespace Jellyfin.Server
@ -126,7 +127,11 @@ namespace Jellyfin.Server
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
#pragma warning restore CA5359
SQLitePCL.Batteries_V2.Init();
Batteries_V2.Init();
if (raw.sqlite3_enable_shared_cache(1) != raw.SQLITE_OK)
{
Console.WriteLine("WARN: Failed to enable shared cache for SQLite");
}
using (var appHost = new CoreAppHost(
appPaths,

@ -1,6 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller.Net;
using MediaBrowser.Model.Events;

@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Net;

Loading…
Cancel
Save