2013-06-18 09:43:07 +00:00
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Persistence;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
using MediaBrowser.Model.Serialization;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Data;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.Persistence
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Class SQLiteItemRepository
|
|
|
|
/// </summary>
|
2013-06-18 19:16:27 +00:00
|
|
|
public class SqliteItemRepository : IItemRepository
|
2013-06-18 09:43:07 +00:00
|
|
|
{
|
2013-09-29 22:00:08 +00:00
|
|
|
private IDbConnection _connection;
|
2013-06-18 09:43:07 +00:00
|
|
|
|
2013-06-18 19:16:27 +00:00
|
|
|
private readonly ILogger _logger;
|
2013-06-26 16:08:16 +00:00
|
|
|
|
2013-09-17 15:14:22 +00:00
|
|
|
private readonly TypeMapper _typeMapper = new TypeMapper();
|
2013-09-29 22:00:08 +00:00
|
|
|
|
2013-06-18 09:43:07 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the name of the repository
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The name.</value>
|
|
|
|
public string Name
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2013-06-18 19:16:27 +00:00
|
|
|
return "SQLite";
|
2013-06-18 09:43:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the json serializer.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The json serializer.</value>
|
|
|
|
private readonly IJsonSerializer _jsonSerializer;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The _app paths
|
|
|
|
/// </summary>
|
|
|
|
private readonly IApplicationPaths _appPaths;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The _save item command
|
|
|
|
/// </summary>
|
2013-09-29 22:00:08 +00:00
|
|
|
private IDbCommand _saveItemCommand;
|
2013-06-18 09:43:07 +00:00
|
|
|
|
|
|
|
private readonly string _criticReviewsPath;
|
|
|
|
|
2013-06-18 19:16:27 +00:00
|
|
|
private SqliteChapterRepository _chapterRepository;
|
2013-12-06 03:39:44 +00:00
|
|
|
private SqliteMediaStreamsRepository _mediaStreamsRepository;
|
2013-06-18 19:16:27 +00:00
|
|
|
|
2013-09-29 22:00:08 +00:00
|
|
|
private IDbCommand _deleteChildrenCommand;
|
|
|
|
private IDbCommand _saveChildrenCommand;
|
2014-02-19 18:50:37 +00:00
|
|
|
private IDbCommand _deleteItemCommand;
|
2013-09-29 22:00:08 +00:00
|
|
|
|
2013-06-18 09:43:07 +00:00
|
|
|
/// <summary>
|
|
|
|
/// 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
|
|
|
|
/// or
|
|
|
|
/// jsonSerializer
|
|
|
|
/// </exception>
|
|
|
|
public SqliteItemRepository(IApplicationPaths appPaths, IJsonSerializer jsonSerializer, ILogManager logManager)
|
|
|
|
{
|
|
|
|
if (appPaths == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException("appPaths");
|
|
|
|
}
|
|
|
|
if (jsonSerializer == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException("jsonSerializer");
|
|
|
|
}
|
|
|
|
|
|
|
|
_appPaths = appPaths;
|
|
|
|
_jsonSerializer = jsonSerializer;
|
|
|
|
|
|
|
|
_criticReviewsPath = Path.Combine(_appPaths.DataPath, "critic-reviews");
|
2013-06-18 19:16:27 +00:00
|
|
|
|
|
|
|
_logger = logManager.GetLogger(GetType().Name);
|
|
|
|
|
2013-07-06 21:23:32 +00:00
|
|
|
var chapterDbFile = Path.Combine(_appPaths.DataPath, "chapters.db");
|
2013-11-02 19:30:29 +00:00
|
|
|
var chapterConnection = SqliteExtensions.ConnectToDb(chapterDbFile, _logger).Result;
|
2013-07-06 21:23:32 +00:00
|
|
|
_chapterRepository = new SqliteChapterRepository(chapterConnection, logManager);
|
2013-12-06 03:39:44 +00:00
|
|
|
|
|
|
|
var mediaStreamsDbFile = Path.Combine(_appPaths.DataPath, "mediainfo.db");
|
|
|
|
var mediaStreamsConnection = SqliteExtensions.ConnectToDb(mediaStreamsDbFile, _logger).Result;
|
|
|
|
_mediaStreamsRepository = new SqliteMediaStreamsRepository(mediaStreamsConnection, logManager);
|
2013-06-18 09:43:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Opens the connection to the database
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
public async Task Initialize()
|
|
|
|
{
|
|
|
|
var dbFile = Path.Combine(_appPaths.DataPath, "library.db");
|
2013-09-29 22:00:08 +00:00
|
|
|
|
2013-11-02 19:30:29 +00:00
|
|
|
_connection = await SqliteExtensions.ConnectToDb(dbFile, _logger).ConfigureAwait(false);
|
2013-06-18 09:43:07 +00:00
|
|
|
|
|
|
|
string[] queries = {
|
|
|
|
|
2013-06-26 16:08:16 +00:00
|
|
|
"create table if not exists TypedBaseItems (guid GUID primary key, type TEXT, data BLOB)",
|
2013-06-27 13:31:57 +00:00
|
|
|
"create index if not exists idx_TypedBaseItems on TypedBaseItems(guid)",
|
2013-06-18 19:16:27 +00:00
|
|
|
|
2013-06-26 16:08:16 +00:00
|
|
|
"create table if not exists ChildrenIds (ParentId GUID, ItemId GUID, PRIMARY KEY (ParentId, ItemId))",
|
|
|
|
"create index if not exists idx_ChildrenIds on ChildrenIds(ParentId,ItemId)",
|
2013-06-20 16:44:24 +00:00
|
|
|
|
2013-06-18 09:43:07 +00:00
|
|
|
//pragmas
|
2013-12-08 01:42:15 +00:00
|
|
|
"pragma temp_store = memory",
|
|
|
|
|
|
|
|
"pragma shrink_memory"
|
2013-06-18 09:43:07 +00:00
|
|
|
};
|
|
|
|
|
2013-06-18 19:16:27 +00:00
|
|
|
_connection.RunQueries(queries, _logger);
|
2013-06-18 09:43:07 +00:00
|
|
|
|
|
|
|
PrepareStatements();
|
2013-12-08 01:42:15 +00:00
|
|
|
|
2013-12-06 03:39:44 +00:00
|
|
|
_mediaStreamsRepository.Initialize();
|
2013-07-06 21:23:32 +00:00
|
|
|
_chapterRepository.Initialize();
|
2013-12-08 01:42:15 +00:00
|
|
|
|
|
|
|
_shrinkMemoryTimer = new SqliteShrinkMemoryTimer(_connection, _writeLock, _logger);
|
2013-06-18 09:43:07 +00:00
|
|
|
}
|
|
|
|
|
2013-12-08 01:42:15 +00:00
|
|
|
private SqliteShrinkMemoryTimer _shrinkMemoryTimer;
|
|
|
|
|
2013-06-18 09:43:07 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The _write lock
|
|
|
|
/// </summary>
|
|
|
|
private readonly SemaphoreSlim _writeLock = new SemaphoreSlim(1, 1);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Prepares the statements.
|
|
|
|
/// </summary>
|
|
|
|
private void PrepareStatements()
|
|
|
|
{
|
2013-09-29 22:00:08 +00:00
|
|
|
_saveItemCommand = _connection.CreateCommand();
|
|
|
|
_saveItemCommand.CommandText = "replace into TypedBaseItems (guid, type, data) values (@1, @2, @3)";
|
|
|
|
_saveItemCommand.Parameters.Add(_saveItemCommand, "@1");
|
|
|
|
_saveItemCommand.Parameters.Add(_saveItemCommand, "@2");
|
|
|
|
_saveItemCommand.Parameters.Add(_saveItemCommand, "@3");
|
|
|
|
|
|
|
|
_deleteChildrenCommand = _connection.CreateCommand();
|
|
|
|
_deleteChildrenCommand.CommandText = "delete from ChildrenIds where ParentId=@ParentId";
|
|
|
|
_deleteChildrenCommand.Parameters.Add(_deleteChildrenCommand, "@ParentId");
|
|
|
|
|
2014-02-19 18:50:37 +00:00
|
|
|
_deleteItemCommand = _connection.CreateCommand();
|
|
|
|
_deleteItemCommand.CommandText = "delete from TypedBaseItems where guid=@Id";
|
|
|
|
_deleteItemCommand.Parameters.Add(_deleteItemCommand, "@Id");
|
|
|
|
|
2013-09-29 22:00:08 +00:00
|
|
|
_saveChildrenCommand = _connection.CreateCommand();
|
|
|
|
_saveChildrenCommand.CommandText = "replace into ChildrenIds (ParentId, ItemId) values (@ParentId, @ItemId)";
|
|
|
|
_saveChildrenCommand.Parameters.Add(_saveChildrenCommand, "@ParentId");
|
|
|
|
_saveChildrenCommand.Parameters.Add(_saveChildrenCommand, "@ItemId");
|
2013-06-18 09:43:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Save a standard item in the repo
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
/// <exception cref="System.ArgumentNullException">item</exception>
|
|
|
|
public Task SaveItem(BaseItem item, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
if (item == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException("item");
|
|
|
|
}
|
|
|
|
|
|
|
|
return SaveItems(new[] { item }, cancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Saves the items.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="items">The items.</param>
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
/// <exception cref="System.ArgumentNullException">
|
|
|
|
/// items
|
|
|
|
/// or
|
|
|
|
/// cancellationToken
|
|
|
|
/// </exception>
|
|
|
|
public async Task SaveItems(IEnumerable<BaseItem> items, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
if (items == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException("items");
|
|
|
|
}
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
2015-02-07 03:25:23 +00:00
|
|
|
CheckDisposed();
|
|
|
|
|
2013-06-18 09:43:07 +00:00
|
|
|
await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
2013-09-29 22:00:08 +00:00
|
|
|
IDbTransaction transaction = null;
|
2013-06-18 09:43:07 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2013-06-18 19:16:27 +00:00
|
|
|
transaction = _connection.BeginTransaction();
|
2013-06-18 09:43:07 +00:00
|
|
|
|
|
|
|
foreach (var item in items)
|
|
|
|
{
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
2013-09-29 22:00:08 +00:00
|
|
|
_saveItemCommand.GetParameter(0).Value = item.Id;
|
|
|
|
_saveItemCommand.GetParameter(1).Value = item.GetType().FullName;
|
|
|
|
_saveItemCommand.GetParameter(2).Value = _jsonSerializer.SerializeToBytes(item);
|
2013-06-18 09:43:07 +00:00
|
|
|
|
|
|
|
_saveItemCommand.Transaction = transaction;
|
|
|
|
|
2013-09-29 22:00:08 +00:00
|
|
|
_saveItemCommand.ExecuteNonQuery();
|
2013-06-18 09:43:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
transaction.Commit();
|
|
|
|
}
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
{
|
|
|
|
if (transaction != null)
|
|
|
|
{
|
|
|
|
transaction.Rollback();
|
|
|
|
}
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2013-06-18 19:16:27 +00:00
|
|
|
_logger.ErrorException("Failed to save items:", e);
|
2013-06-18 09:43:07 +00:00
|
|
|
|
|
|
|
if (transaction != null)
|
|
|
|
{
|
|
|
|
transaction.Rollback();
|
|
|
|
}
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
if (transaction != null)
|
|
|
|
{
|
|
|
|
transaction.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
_writeLock.Release();
|
|
|
|
}
|
|
|
|
}
|
2014-10-23 04:26:01 +00:00
|
|
|
|
2013-06-18 09:43:07 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Internal retrieve from items or users table
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="id">The id.</param>
|
|
|
|
/// <returns>BaseItem.</returns>
|
|
|
|
/// <exception cref="System.ArgumentNullException">id</exception>
|
|
|
|
/// <exception cref="System.ArgumentException"></exception>
|
2013-06-26 16:08:16 +00:00
|
|
|
public BaseItem RetrieveItem(Guid id)
|
2013-06-18 09:43:07 +00:00
|
|
|
{
|
|
|
|
if (id == Guid.Empty)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException("id");
|
|
|
|
}
|
|
|
|
|
2015-02-07 03:25:23 +00:00
|
|
|
CheckDisposed();
|
|
|
|
|
2013-06-18 19:16:27 +00:00
|
|
|
using (var cmd = _connection.CreateCommand())
|
2013-06-18 09:43:07 +00:00
|
|
|
{
|
2013-06-26 16:08:16 +00:00
|
|
|
cmd.CommandText = "select type,data from TypedBaseItems where guid = @guid";
|
2013-09-29 22:00:08 +00:00
|
|
|
cmd.Parameters.Add(cmd, "@guid", DbType.Guid).Value = id;
|
2013-06-18 09:43:07 +00:00
|
|
|
|
|
|
|
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult | CommandBehavior.SingleRow))
|
|
|
|
{
|
|
|
|
if (reader.Read())
|
|
|
|
{
|
2014-02-20 04:53:15 +00:00
|
|
|
return GetItem(reader);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2013-06-26 16:08:16 +00:00
|
|
|
|
2014-02-20 04:53:15 +00:00
|
|
|
private BaseItem GetItem(IDataReader reader)
|
|
|
|
{
|
|
|
|
var typeString = reader.GetString(0);
|
2013-06-26 16:08:16 +00:00
|
|
|
|
2014-02-20 04:53:15 +00:00
|
|
|
var type = _typeMapper.GetType(typeString);
|
2013-06-26 16:08:16 +00:00
|
|
|
|
2014-02-20 04:53:15 +00:00
|
|
|
if (type == null)
|
|
|
|
{
|
|
|
|
_logger.Debug("Unknown type {0}", typeString);
|
2013-06-26 16:08:16 +00:00
|
|
|
|
2013-06-18 09:43:07 +00:00
|
|
|
return null;
|
|
|
|
}
|
2014-02-20 04:53:15 +00:00
|
|
|
|
|
|
|
using (var stream = reader.GetMemoryStream(1))
|
|
|
|
{
|
|
|
|
return _jsonSerializer.DeserializeFromStream(stream, type) as BaseItem;
|
|
|
|
}
|
2013-06-18 09:43:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the critic reviews.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="itemId">The item id.</param>
|
|
|
|
/// <returns>Task{IEnumerable{ItemReview}}.</returns>
|
2013-09-17 15:14:22 +00:00
|
|
|
public IEnumerable<ItemReview> GetCriticReviews(Guid itemId)
|
2013-06-18 09:43:07 +00:00
|
|
|
{
|
2013-09-17 15:14:22 +00:00
|
|
|
try
|
2013-06-18 09:43:07 +00:00
|
|
|
{
|
2013-09-17 15:14:22 +00:00
|
|
|
var path = Path.Combine(_criticReviewsPath, itemId + ".json");
|
2013-06-18 09:43:07 +00:00
|
|
|
|
2013-09-17 15:14:22 +00:00
|
|
|
return _jsonSerializer.DeserializeFromFile<List<ItemReview>>(path);
|
|
|
|
}
|
|
|
|
catch (DirectoryNotFoundException)
|
|
|
|
{
|
|
|
|
return new List<ItemReview>();
|
|
|
|
}
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
{
|
|
|
|
return new List<ItemReview>();
|
|
|
|
}
|
2013-06-18 09:43:07 +00:00
|
|
|
}
|
|
|
|
|
2014-02-03 17:44:13 +00:00
|
|
|
private readonly Task _cachedTask = Task.FromResult(true);
|
2013-06-18 09:43:07 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Saves the critic reviews.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="itemId">The item id.</param>
|
|
|
|
/// <param name="criticReviews">The critic reviews.</param>
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
public Task SaveCriticReviews(Guid itemId, IEnumerable<ItemReview> criticReviews)
|
|
|
|
{
|
2013-10-01 18:24:27 +00:00
|
|
|
Directory.CreateDirectory(_criticReviewsPath);
|
2013-06-18 09:43:07 +00:00
|
|
|
|
2013-09-24 15:08:51 +00:00
|
|
|
var path = Path.Combine(_criticReviewsPath, itemId + ".json");
|
|
|
|
|
|
|
|
_jsonSerializer.SerializeToFile(criticReviews.ToList(), path);
|
2013-06-18 09:43:07 +00:00
|
|
|
|
2014-02-03 17:44:13 +00:00
|
|
|
return _cachedTask;
|
2013-06-18 09:43:07 +00:00
|
|
|
}
|
2013-06-18 19:16:27 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets chapters for an item
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="id">The id.</param>
|
|
|
|
/// <returns>IEnumerable{ChapterInfo}.</returns>
|
|
|
|
/// <exception cref="System.ArgumentNullException">id</exception>
|
|
|
|
public IEnumerable<ChapterInfo> GetChapters(Guid id)
|
|
|
|
{
|
2015-02-07 03:25:23 +00:00
|
|
|
CheckDisposed();
|
2013-06-18 19:16:27 +00:00
|
|
|
return _chapterRepository.GetChapters(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a single chapter for an item
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="id">The id.</param>
|
|
|
|
/// <param name="index">The index.</param>
|
|
|
|
/// <returns>ChapterInfo.</returns>
|
|
|
|
/// <exception cref="System.ArgumentNullException">id</exception>
|
|
|
|
public ChapterInfo GetChapter(Guid id, int index)
|
|
|
|
{
|
2015-02-07 03:25:23 +00:00
|
|
|
CheckDisposed();
|
2013-06-18 19:16:27 +00:00
|
|
|
return _chapterRepository.GetChapter(id, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Saves the chapters.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="id">The id.</param>
|
|
|
|
/// <param name="chapters">The chapters.</param>
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
/// <exception cref="System.ArgumentNullException">
|
|
|
|
/// id
|
|
|
|
/// or
|
|
|
|
/// chapters
|
|
|
|
/// or
|
|
|
|
/// cancellationToken
|
|
|
|
/// </exception>
|
|
|
|
public Task SaveChapters(Guid id, IEnumerable<ChapterInfo> chapters, CancellationToken cancellationToken)
|
|
|
|
{
|
2015-02-07 03:25:23 +00:00
|
|
|
CheckDisposed();
|
2013-06-18 19:16:27 +00:00
|
|
|
return _chapterRepository.SaveChapters(id, chapters, cancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
|
|
|
/// </summary>
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private readonly object _disposeLock = new object();
|
|
|
|
|
2015-02-07 03:25:23 +00:00
|
|
|
private bool _disposed;
|
|
|
|
private void CheckDisposed()
|
|
|
|
{
|
|
|
|
if (_disposed)
|
|
|
|
{
|
|
|
|
throw new ObjectDisposedException(GetType().Name + " has been disposed and cannot be accessed.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-18 19:16:27 +00:00
|
|
|
/// <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)
|
|
|
|
{
|
2015-02-07 03:25:23 +00:00
|
|
|
_disposed = true;
|
|
|
|
|
2013-06-18 19:16:27 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
lock (_disposeLock)
|
|
|
|
{
|
2013-12-08 01:42:15 +00:00
|
|
|
if (_shrinkMemoryTimer != null)
|
|
|
|
{
|
|
|
|
_shrinkMemoryTimer.Dispose();
|
|
|
|
_shrinkMemoryTimer = null;
|
|
|
|
}
|
|
|
|
|
2013-06-18 19:16:27 +00:00
|
|
|
if (_connection != null)
|
|
|
|
{
|
|
|
|
if (_connection.IsOpen())
|
|
|
|
{
|
|
|
|
_connection.Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
_connection.Dispose();
|
|
|
|
_connection = null;
|
|
|
|
}
|
2013-12-08 01:42:15 +00:00
|
|
|
|
|
|
|
if (_chapterRepository != null)
|
|
|
|
{
|
|
|
|
_chapterRepository.Dispose();
|
|
|
|
_chapterRepository = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_mediaStreamsRepository != null)
|
|
|
|
{
|
|
|
|
_mediaStreamsRepository.Dispose();
|
|
|
|
_mediaStreamsRepository = null;
|
|
|
|
}
|
2013-06-18 19:16:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
_logger.ErrorException("Error disposing database", ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-20 16:44:24 +00:00
|
|
|
|
2013-06-26 16:08:16 +00:00
|
|
|
public IEnumerable<Guid> GetChildren(Guid parentId)
|
2013-06-20 16:44:24 +00:00
|
|
|
{
|
|
|
|
if (parentId == Guid.Empty)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException("parentId");
|
|
|
|
}
|
|
|
|
|
2015-02-07 03:25:23 +00:00
|
|
|
CheckDisposed();
|
|
|
|
|
2013-06-20 16:44:24 +00:00
|
|
|
using (var cmd = _connection.CreateCommand())
|
|
|
|
{
|
2013-06-26 16:08:16 +00:00
|
|
|
cmd.CommandText = "select ItemId from ChildrenIds where ParentId = @ParentId";
|
2013-06-20 16:44:24 +00:00
|
|
|
|
2013-09-29 22:00:08 +00:00
|
|
|
cmd.Parameters.Add(cmd, "@ParentId", DbType.Guid).Value = parentId;
|
2013-06-20 16:44:24 +00:00
|
|
|
|
|
|
|
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
|
|
|
|
{
|
|
|
|
while (reader.Read())
|
|
|
|
{
|
2013-06-26 16:08:16 +00:00
|
|
|
yield return reader.GetGuid(0);
|
2013-06-20 16:44:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-23 04:26:01 +00:00
|
|
|
public IEnumerable<BaseItem> GetChildrenItems(Guid parentId)
|
|
|
|
{
|
|
|
|
if (parentId == Guid.Empty)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException("parentId");
|
|
|
|
}
|
|
|
|
|
2015-02-07 03:25:23 +00:00
|
|
|
CheckDisposed();
|
|
|
|
|
2014-10-23 04:26:01 +00:00
|
|
|
using (var cmd = _connection.CreateCommand())
|
|
|
|
{
|
|
|
|
cmd.CommandText = "select type,data from TypedBaseItems where guid in (select ItemId from ChildrenIds where ParentId = @ParentId)";
|
|
|
|
|
|
|
|
cmd.Parameters.Add(cmd, "@ParentId", DbType.Guid).Value = parentId;
|
|
|
|
|
|
|
|
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
|
|
|
|
{
|
|
|
|
while (reader.Read())
|
|
|
|
{
|
|
|
|
var item = GetItem(reader);
|
|
|
|
|
|
|
|
if (item != null)
|
|
|
|
{
|
|
|
|
yield return item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-21 16:10:02 +00:00
|
|
|
public IEnumerable<string> GetItemsOfType(Type type)
|
2014-02-20 04:53:15 +00:00
|
|
|
{
|
|
|
|
if (type == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException("type");
|
|
|
|
}
|
|
|
|
|
2015-02-07 03:25:23 +00:00
|
|
|
CheckDisposed();
|
|
|
|
|
2014-02-20 04:53:15 +00:00
|
|
|
using (var cmd = _connection.CreateCommand())
|
|
|
|
{
|
2015-03-21 16:10:02 +00:00
|
|
|
cmd.CommandText = "select guid from TypedBaseItems where type = @type";
|
2014-02-20 04:53:15 +00:00
|
|
|
|
|
|
|
cmd.Parameters.Add(cmd, "@type", DbType.String).Value = type.FullName;
|
|
|
|
|
|
|
|
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
|
|
|
|
{
|
|
|
|
while (reader.Read())
|
|
|
|
{
|
2015-03-21 16:10:02 +00:00
|
|
|
yield return reader.GetString(0);
|
2014-02-20 04:53:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-19 18:50:37 +00:00
|
|
|
public async Task DeleteItem(Guid id, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
if (id == Guid.Empty)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException("id");
|
|
|
|
}
|
|
|
|
|
2015-02-07 03:25:23 +00:00
|
|
|
CheckDisposed();
|
|
|
|
|
2014-02-19 18:50:37 +00:00
|
|
|
await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
IDbTransaction transaction = null;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
transaction = _connection.BeginTransaction();
|
|
|
|
|
|
|
|
// First delete children
|
|
|
|
_deleteChildrenCommand.GetParameter(0).Value = id;
|
|
|
|
_deleteChildrenCommand.Transaction = transaction;
|
|
|
|
_deleteChildrenCommand.ExecuteNonQuery();
|
|
|
|
|
|
|
|
// Delete the item
|
|
|
|
_deleteItemCommand.GetParameter(0).Value = id;
|
|
|
|
_deleteItemCommand.Transaction = transaction;
|
|
|
|
_deleteItemCommand.ExecuteNonQuery();
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-26 16:08:16 +00:00
|
|
|
public async Task SaveChildren(Guid parentId, IEnumerable<Guid> children, CancellationToken cancellationToken)
|
2013-06-20 16:44:24 +00:00
|
|
|
{
|
|
|
|
if (parentId == Guid.Empty)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException("parentId");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (children == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException("children");
|
|
|
|
}
|
|
|
|
|
2015-02-07 03:25:23 +00:00
|
|
|
CheckDisposed();
|
|
|
|
|
2013-06-20 16:44:24 +00:00
|
|
|
await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
2013-09-29 22:00:08 +00:00
|
|
|
IDbTransaction transaction = null;
|
2013-06-20 16:44:24 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
transaction = _connection.BeginTransaction();
|
|
|
|
|
|
|
|
// First delete
|
2013-09-29 22:00:08 +00:00
|
|
|
_deleteChildrenCommand.GetParameter(0).Value = parentId;
|
2013-06-20 16:44:24 +00:00
|
|
|
_deleteChildrenCommand.Transaction = transaction;
|
2013-09-29 22:00:08 +00:00
|
|
|
|
|
|
|
_deleteChildrenCommand.ExecuteNonQuery();
|
2013-06-20 16:44:24 +00:00
|
|
|
|
2013-06-26 16:08:16 +00:00
|
|
|
foreach (var id in children)
|
2013-06-20 16:44:24 +00:00
|
|
|
{
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
2013-09-29 22:00:08 +00:00
|
|
|
_saveChildrenCommand.GetParameter(0).Value = parentId;
|
|
|
|
_saveChildrenCommand.GetParameter(1).Value = id;
|
2013-06-20 16:44:24 +00:00
|
|
|
|
|
|
|
_saveChildrenCommand.Transaction = transaction;
|
|
|
|
|
2013-09-29 22:00:08 +00:00
|
|
|
_saveChildrenCommand.ExecuteNonQuery();
|
2013-06-20 16:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
2013-12-06 03:39:44 +00:00
|
|
|
|
|
|
|
public IEnumerable<MediaStream> GetMediaStreams(MediaStreamQuery query)
|
|
|
|
{
|
2015-02-07 03:25:23 +00:00
|
|
|
CheckDisposed();
|
2013-12-06 03:39:44 +00:00
|
|
|
return _mediaStreamsRepository.GetMediaStreams(query);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Task SaveMediaStreams(Guid id, IEnumerable<MediaStream> streams, CancellationToken cancellationToken)
|
|
|
|
{
|
2015-02-07 03:25:23 +00:00
|
|
|
CheckDisposed();
|
2013-12-06 03:39:44 +00:00
|
|
|
return _mediaStreamsRepository.SaveMediaStreams(id, streams, cancellationToken);
|
|
|
|
}
|
2013-06-18 09:43:07 +00:00
|
|
|
}
|
|
|
|
}
|