mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-03-20 17:16:42 +00:00
move child definitions to db
This commit is contained in:
@@ -64,6 +64,12 @@
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\packages\ServiceStack.Common.3.9.54\lib\net35\ServiceStack.Interfaces.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="ServiceStack.OrmLite">
|
||||
<HintPath>..\packages\ServiceStack.OrmLite.Sqlite32.3.9.54\lib\net40\ServiceStack.OrmLite.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="ServiceStack.OrmLite.SqliteNET">
|
||||
<HintPath>..\packages\ServiceStack.OrmLite.Sqlite32.3.9.54\lib\net40\ServiceStack.OrmLite.SqliteNET.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="ServiceStack.OrmLite.SqlServer, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\packages\ServiceStack.OrmLite.SqlServer.3.9.43\lib\ServiceStack.OrmLite.SqlServer.dll</HintPath>
|
||||
|
||||
@@ -27,16 +27,13 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
||||
private SQLiteCommand _saveChapterCommand;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SqliteItemRepository"/> class.
|
||||
/// Initializes a new instance of the <see cref="SqliteItemRepository" /> class.
|
||||
/// </summary>
|
||||
/// <param name="appPaths">The app paths.</param>
|
||||
/// <param name="jsonSerializer">The json serializer.</param>
|
||||
/// <param name="logManager">The log manager.</param>
|
||||
/// <exception cref="System.ArgumentNullException">
|
||||
/// appPaths
|
||||
/// <exception cref="System.ArgumentNullException">appPaths
|
||||
/// or
|
||||
/// jsonSerializer
|
||||
/// </exception>
|
||||
/// jsonSerializer</exception>
|
||||
public SqliteChapterRepository(IApplicationPaths appPaths, ILogManager logManager)
|
||||
{
|
||||
if (appPaths == null)
|
||||
|
||||
@@ -56,6 +56,9 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
||||
|
||||
private SqliteChapterRepository _chapterRepository;
|
||||
|
||||
private SQLiteCommand _deleteChildrenCommand;
|
||||
private SQLiteCommand _saveChildrenCommand;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SqliteItemRepository"/> class.
|
||||
/// </summary>
|
||||
@@ -95,7 +98,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
||||
public async Task Initialize()
|
||||
{
|
||||
var dbFile = Path.Combine(_appPaths.DataPath, "library.db");
|
||||
|
||||
|
||||
_connection = await SqliteExtensions.ConnectToDb(dbFile).ConfigureAwait(false);
|
||||
|
||||
string[] queries = {
|
||||
@@ -103,6 +106,9 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
||||
"create table if not exists baseitems (guid GUID primary key, data BLOB)",
|
||||
"create index if not exists idx_baseitems on baseitems(guid)",
|
||||
|
||||
"create table if not exists ChildDefinitions (ParentId GUID, ItemId GUID, Type TEXT, PRIMARY KEY (ParentId, ItemId))",
|
||||
"create index if not exists idx_baseitems on baseitems(ParentId,ItemId)",
|
||||
|
||||
//pragmas
|
||||
"pragma temp_store = memory"
|
||||
};
|
||||
@@ -131,6 +137,22 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
||||
|
||||
_saveItemCommand.Parameters.Add(new SQLiteParameter("@1"));
|
||||
_saveItemCommand.Parameters.Add(new SQLiteParameter("@2"));
|
||||
|
||||
_deleteChildrenCommand = new SQLiteCommand
|
||||
{
|
||||
CommandText = "delete from ChildDefinitions where ParentId=@ParentId"
|
||||
};
|
||||
|
||||
_deleteChildrenCommand.Parameters.Add(new SQLiteParameter("@ParentId"));
|
||||
|
||||
_saveChildrenCommand = new SQLiteCommand
|
||||
{
|
||||
CommandText = "replace into ChildDefinitions (ParentId, ItemId, Type) values (@ParentId, @ItemId, @Type)"
|
||||
};
|
||||
|
||||
_saveChildrenCommand.Parameters.Add(new SQLiteParameter("@ParentId"));
|
||||
_saveChildrenCommand.Parameters.Add(new SQLiteParameter("@ItemId"));
|
||||
_saveChildrenCommand.Parameters.Add(new SQLiteParameter("@Type"));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -401,5 +423,110 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<ChildDefinition> GetChildren(Guid parentId)
|
||||
{
|
||||
if (parentId == Guid.Empty)
|
||||
{
|
||||
throw new ArgumentNullException("parentId");
|
||||
}
|
||||
|
||||
using (var cmd = _connection.CreateCommand())
|
||||
{
|
||||
cmd.CommandText = "select ItemId,Type from ChildDefinitions where ParentId = @ParentId";
|
||||
|
||||
cmd.Parameters.Add("@ParentId", DbType.Guid).Value = parentId;
|
||||
|
||||
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
yield return new ChildDefinition
|
||||
{
|
||||
ItemId = reader.GetGuid(0),
|
||||
Type = reader.GetString(1)
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SaveChildren(Guid parentId, IEnumerable<ChildDefinition> children, CancellationToken cancellationToken)
|
||||
{
|
||||
if (parentId == Guid.Empty)
|
||||
{
|
||||
throw new ArgumentNullException("parentId");
|
||||
}
|
||||
|
||||
if (children == null)
|
||||
{
|
||||
throw new ArgumentNullException("children");
|
||||
}
|
||||
|
||||
if (cancellationToken == null)
|
||||
{
|
||||
throw new ArgumentNullException("cancellationToken");
|
||||
}
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
SQLiteTransaction transaction = null;
|
||||
|
||||
try
|
||||
{
|
||||
transaction = _connection.BeginTransaction();
|
||||
|
||||
// First delete
|
||||
_deleteChildrenCommand.Parameters[0].Value = parentId;
|
||||
_deleteChildrenCommand.Transaction = transaction;
|
||||
await _deleteChildrenCommand.ExecuteNonQueryAsync(cancellationToken);
|
||||
|
||||
foreach (var chapter in children)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
_saveChildrenCommand.Parameters[0].Value = parentId;
|
||||
_saveChildrenCommand.Parameters[1].Value = chapter.ItemId;
|
||||
_saveChildrenCommand.Parameters[2].Value = chapter.Type;
|
||||
|
||||
_saveChildrenCommand.Transaction = transaction;
|
||||
|
||||
await _saveChildrenCommand.ExecuteNonQueryAsync(cancellationToken);
|
||||
}
|
||||
|
||||
transaction.Commit();
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
if (transaction != null)
|
||||
{
|
||||
transaction.Rollback();
|
||||
}
|
||||
|
||||
throw;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.ErrorException("Failed to save children:", e);
|
||||
|
||||
if (transaction != null)
|
||||
{
|
||||
transaction.Rollback();
|
||||
}
|
||||
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (transaction != null)
|
||||
{
|
||||
transaction.Dispose();
|
||||
}
|
||||
|
||||
_writeLock.Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@
|
||||
<package id="ServiceStack" version="3.9.54" targetFramework="net45" />
|
||||
<package id="ServiceStack.Api.Swagger" version="3.9.54" targetFramework="net45" />
|
||||
<package id="ServiceStack.Common" version="3.9.54" targetFramework="net45" />
|
||||
<package id="ServiceStack.OrmLite.Sqlite32" version="3.9.54" targetFramework="net45" />
|
||||
<package id="ServiceStack.OrmLite.SqlServer" version="3.9.43" targetFramework="net45" />
|
||||
<package id="ServiceStack.Redis" version="3.9.43" targetFramework="net45" />
|
||||
<package id="ServiceStack.Text" version="3.9.54" targetFramework="net45" />
|
||||
|
||||
Reference in New Issue
Block a user