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.Data.SQLite; using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace MediaBrowser.Server.Implementations.Persistence { /// /// Class SQLiteItemRepository /// public class SqliteItemRepository : IItemRepository { private SQLiteConnection _connection; private readonly ILogger _logger; private TypeMapper _typeMapper = new TypeMapper(); /// /// Gets the name of the repository /// /// The name. public string Name { get { return "SQLite"; } } /// /// Gets the json serializer. /// /// The json serializer. private readonly IJsonSerializer _jsonSerializer; /// /// The _app paths /// private readonly IApplicationPaths _appPaths; /// /// The _save item command /// private SQLiteCommand _saveItemCommand; private readonly string _criticReviewsPath; private SqliteChapterRepository _chapterRepository; private SQLiteCommand _deleteChildrenCommand; private SQLiteCommand _saveChildrenCommand; /// /// Initializes a new instance of the class. /// /// The app paths. /// The json serializer. /// The log manager. /// /// appPaths /// or /// jsonSerializer /// 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"); _logger = logManager.GetLogger(GetType().Name); _chapterRepository = new SqliteChapterRepository(appPaths, logManager); } /// /// Opens the connection to the database /// /// Task. public async Task Initialize() { var dbFile = Path.Combine(_appPaths.DataPath, "library.db"); _connection = await SqliteExtensions.ConnectToDb(dbFile).ConfigureAwait(false); string[] queries = { "create table if not exists TypedBaseItems (guid GUID primary key, type TEXT, data BLOB)", "create index if not exists idx_TypedBaseItems on baseitems(guid)", "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)", //pragmas "pragma temp_store = memory" }; _connection.RunQueries(queries, _logger); PrepareStatements(); await _chapterRepository.Initialize().ConfigureAwait(false); } /// /// The _write lock /// private readonly SemaphoreSlim _writeLock = new SemaphoreSlim(1, 1); /// /// Prepares the statements. /// private void PrepareStatements() { _saveItemCommand = new SQLiteCommand { CommandText = "replace into TypedBaseItems (guid, type, data) values (@1, @2, @3)" }; _saveItemCommand.Parameters.Add(new SQLiteParameter("@1")); _saveItemCommand.Parameters.Add(new SQLiteParameter("@2")); _saveItemCommand.Parameters.Add(new SQLiteParameter("@3")); _deleteChildrenCommand = new SQLiteCommand { CommandText = "delete from ChildrenIds where ParentId=@ParentId" }; _deleteChildrenCommand.Parameters.Add(new SQLiteParameter("@ParentId")); _saveChildrenCommand = new SQLiteCommand { CommandText = "replace into ChildrenIds (ParentId, ItemId) values (@ParentId, @ItemId)" }; _saveChildrenCommand.Parameters.Add(new SQLiteParameter("@ParentId")); _saveChildrenCommand.Parameters.Add(new SQLiteParameter("@ItemId")); } /// /// Save a standard item in the repo /// /// The item. /// The cancellation token. /// Task. /// item public Task SaveItem(BaseItem item, CancellationToken cancellationToken) { if (item == null) { throw new ArgumentNullException("item"); } return SaveItems(new[] { item }, cancellationToken); } /// /// Saves the items. /// /// The items. /// The cancellation token. /// Task. /// /// items /// or /// cancellationToken /// public async Task SaveItems(IEnumerable items, CancellationToken cancellationToken) { if (items == null) { throw new ArgumentNullException("items"); } if (cancellationToken == null) { throw new ArgumentNullException("cancellationToken"); } cancellationToken.ThrowIfCancellationRequested(); await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false); SQLiteTransaction transaction = null; try { transaction = _connection.BeginTransaction(); foreach (var item in items) { cancellationToken.ThrowIfCancellationRequested(); _saveItemCommand.Parameters[0].Value = item.Id; _saveItemCommand.Parameters[1].Value = item.GetType().FullName; _saveItemCommand.Parameters[2].Value = _jsonSerializer.SerializeToBytes(item); _saveItemCommand.Transaction = transaction; await _saveItemCommand.ExecuteNonQueryAsync(cancellationToken); } transaction.Commit(); } catch (OperationCanceledException) { if (transaction != null) { transaction.Rollback(); } throw; } catch (Exception e) { _logger.ErrorException("Failed to save items:", e); if (transaction != null) { transaction.Rollback(); } throw; } finally { if (transaction != null) { transaction.Dispose(); } _writeLock.Release(); } } /// /// Internal retrieve from items or users table /// /// The id. /// BaseItem. /// id /// public BaseItem RetrieveItem(Guid id) { if (id == Guid.Empty) { throw new ArgumentNullException("id"); } using (var cmd = _connection.CreateCommand()) { cmd.CommandText = "select type,data from TypedBaseItems where guid = @guid"; var guidParam = cmd.Parameters.Add("@guid", DbType.Guid); guidParam.Value = id; using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult | CommandBehavior.SingleRow)) { if (reader.Read()) { var typeString = reader.GetString(0); var type = _typeMapper.GetType(typeString); if (type == null) { _logger.Debug("Unknown type {0}", typeString); return null; } using (var stream = reader.GetMemoryStream(1)) { return _jsonSerializer.DeserializeFromStream(stream, type) as BaseItem; } } } return null; } } /// /// Gets the critic reviews. /// /// The item id. /// Task{IEnumerable{ItemReview}}. public Task> GetCriticReviews(Guid itemId) { return Task.Run>(() => { try { var path = Path.Combine(_criticReviewsPath, itemId + ".json"); return _jsonSerializer.DeserializeFromFile>(path); } catch (DirectoryNotFoundException) { return new List(); } catch (FileNotFoundException) { return new List(); } }); } /// /// Saves the critic reviews. /// /// The item id. /// The critic reviews. /// Task. public Task SaveCriticReviews(Guid itemId, IEnumerable criticReviews) { return Task.Run(() => { if (!Directory.Exists(_criticReviewsPath)) { Directory.CreateDirectory(_criticReviewsPath); } var path = Path.Combine(_criticReviewsPath, itemId + ".json"); _jsonSerializer.SerializeToFile(criticReviews.ToList(), path); }); } /// /// Gets chapters for an item /// /// The id. /// IEnumerable{ChapterInfo}. /// id public IEnumerable GetChapters(Guid id) { return _chapterRepository.GetChapters(id); } /// /// Gets a single chapter for an item /// /// The id. /// The index. /// ChapterInfo. /// id public ChapterInfo GetChapter(Guid id, int index) { return _chapterRepository.GetChapter(id, index); } /// /// Saves the chapters. /// /// The id. /// The chapters. /// The cancellation token. /// Task. /// /// id /// or /// chapters /// or /// cancellationToken /// public Task SaveChapters(Guid id, IEnumerable chapters, CancellationToken cancellationToken) { return _chapterRepository.SaveChapters(id, chapters, cancellationToken); } /// /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } private readonly object _disposeLock = new object(); /// /// Releases unmanaged and - optionally - managed resources. /// /// true to release both managed and unmanaged resources; false to release only unmanaged resources. protected virtual void Dispose(bool dispose) { if (dispose) { try { lock (_disposeLock) { if (_connection != null) { if (_connection.IsOpen()) { _connection.Close(); } _connection.Dispose(); _connection = null; } } } catch (Exception ex) { _logger.ErrorException("Error disposing database", ex); } if (_chapterRepository != null) { _chapterRepository.Dispose(); _chapterRepository = null; } } } public IEnumerable GetChildren(Guid parentId) { if (parentId == Guid.Empty) { throw new ArgumentNullException("parentId"); } using (var cmd = _connection.CreateCommand()) { cmd.CommandText = "select ItemId from ChildrenIds 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 reader.GetGuid(0); } } } } public async Task SaveChildren(Guid parentId, IEnumerable 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 id in children) { cancellationToken.ThrowIfCancellationRequested(); _saveChildrenCommand.Parameters[0].Value = parentId; _saveChildrenCommand.Parameters[1].Value = id; _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(); } } } }