2014-09-11 01:57:11 +00:00
|
|
|
|
using System.Text;
|
|
|
|
|
using MediaBrowser.Common.Configuration;
|
2014-01-29 05:17:58 +00:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2013-12-06 20:07:34 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Data;
|
2014-02-08 20:02:35 +00:00
|
|
|
|
using System.IO;
|
2013-12-06 20:07:34 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.Persistence
|
|
|
|
|
{
|
2014-01-29 05:17:58 +00:00
|
|
|
|
public class SqliteProviderInfoRepository : IProviderRepository
|
2013-12-06 20:07:34 +00:00
|
|
|
|
{
|
|
|
|
|
private IDbConnection _connection;
|
|
|
|
|
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
2014-01-29 05:17:58 +00:00
|
|
|
|
private IDbCommand _saveStatusCommand;
|
|
|
|
|
private readonly IApplicationPaths _appPaths;
|
2013-12-06 20:07:34 +00:00
|
|
|
|
|
2014-01-29 05:17:58 +00:00
|
|
|
|
public SqliteProviderInfoRepository(IApplicationPaths appPaths, ILogManager logManager)
|
2013-12-06 20:07:34 +00:00
|
|
|
|
{
|
2014-01-29 05:17:58 +00:00
|
|
|
|
_appPaths = appPaths;
|
2013-12-06 20:07:34 +00:00
|
|
|
|
_logger = logManager.GetLogger(GetType().Name);
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-08 19:39:39 +00:00
|
|
|
|
private SqliteShrinkMemoryTimer _shrinkMemoryTimer;
|
2014-01-29 05:17:58 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the name of the repository
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The name.</value>
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return "SQLite";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-06 20:07:34 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Opens the connection to the database
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Task.</returns>
|
2014-01-29 05:17:58 +00:00
|
|
|
|
public async Task Initialize()
|
2013-12-06 20:07:34 +00:00
|
|
|
|
{
|
2014-02-08 20:02:35 +00:00
|
|
|
|
var dbFile = Path.Combine(_appPaths.DataPath, "refreshinfo.db");
|
2013-12-06 20:07:34 +00:00
|
|
|
|
|
2014-01-29 05:17:58 +00:00
|
|
|
|
_connection = await SqliteExtensions.ConnectToDb(dbFile, _logger).ConfigureAwait(false);
|
2013-12-06 20:07:34 +00:00
|
|
|
|
|
|
|
|
|
string[] queries = {
|
|
|
|
|
|
2014-09-11 01:57:11 +00:00
|
|
|
|
"create table if not exists MetadataStatus (ItemId GUID PRIMARY KEY, ItemName TEXT, ItemType TEXT, SeriesName TEXT, DateLastMetadataRefresh datetime, DateLastImagesRefresh datetime, LastStatus TEXT, LastErrorMessage TEXT, MetadataProvidersRefreshed TEXT, ImageProvidersRefreshed TEXT, ItemDateModified DateTimeNull)",
|
2014-01-29 05:17:58 +00:00
|
|
|
|
"create index if not exists idx_MetadataStatus on MetadataStatus(ItemId)",
|
|
|
|
|
|
2013-12-06 20:07:34 +00:00
|
|
|
|
//pragmas
|
2013-12-08 01:42:15 +00:00
|
|
|
|
"pragma temp_store = memory",
|
|
|
|
|
|
|
|
|
|
"pragma shrink_memory"
|
2013-12-06 20:07:34 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_connection.RunQueries(queries, _logger);
|
|
|
|
|
|
2014-09-11 01:57:11 +00:00
|
|
|
|
AddItemDateModifiedCommand();
|
|
|
|
|
|
2013-12-06 20:07:34 +00:00
|
|
|
|
PrepareStatements();
|
2013-12-08 19:39:39 +00:00
|
|
|
|
|
|
|
|
|
_shrinkMemoryTimer = new SqliteShrinkMemoryTimer(_connection, _writeLock, _logger);
|
2013-12-06 20:07:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-29 05:17:58 +00:00
|
|
|
|
private static readonly string[] StatusColumns =
|
|
|
|
|
{
|
|
|
|
|
"ItemId",
|
2014-02-08 20:02:35 +00:00
|
|
|
|
"ItemName",
|
2014-02-08 22:38:02 +00:00
|
|
|
|
"ItemType",
|
2014-02-08 20:02:35 +00:00
|
|
|
|
"SeriesName",
|
2014-01-29 05:17:58 +00:00
|
|
|
|
"DateLastMetadataRefresh",
|
|
|
|
|
"DateLastImagesRefresh",
|
|
|
|
|
"LastStatus",
|
|
|
|
|
"LastErrorMessage",
|
|
|
|
|
"MetadataProvidersRefreshed",
|
2014-09-11 01:57:11 +00:00
|
|
|
|
"ImageProvidersRefreshed",
|
|
|
|
|
"ItemDateModified"
|
2014-01-29 05:17:58 +00:00
|
|
|
|
};
|
2013-12-06 20:07:34 +00:00
|
|
|
|
|
2014-09-11 01:57:11 +00:00
|
|
|
|
private void AddItemDateModifiedCommand()
|
|
|
|
|
{
|
|
|
|
|
using (var cmd = _connection.CreateCommand())
|
|
|
|
|
{
|
|
|
|
|
cmd.CommandText = "PRAGMA table_info(MetadataStatus)";
|
|
|
|
|
|
|
|
|
|
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
|
|
|
|
|
{
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
if (!reader.IsDBNull(1))
|
|
|
|
|
{
|
|
|
|
|
var name = reader.GetString(1);
|
|
|
|
|
|
|
|
|
|
if (string.Equals(name, "ItemDateModified", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var builder = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
builder.AppendLine("alter table MetadataStatus");
|
|
|
|
|
builder.AppendLine("add column ItemDateModified DateTime NULL");
|
|
|
|
|
|
|
|
|
|
_connection.RunQueries(new[] { builder.ToString() }, _logger);
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-06 20:07:34 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _write lock
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly SemaphoreSlim _writeLock = new SemaphoreSlim(1, 1);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Prepares the statements.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void PrepareStatements()
|
|
|
|
|
{
|
2014-01-29 05:17:58 +00:00
|
|
|
|
_saveStatusCommand = _connection.CreateCommand();
|
|
|
|
|
|
|
|
|
|
_saveStatusCommand.CommandText = string.Format("replace into MetadataStatus ({0}) values ({1})",
|
|
|
|
|
string.Join(",", StatusColumns),
|
|
|
|
|
string.Join(",", StatusColumns.Select(i => "@" + i).ToArray()));
|
|
|
|
|
|
|
|
|
|
foreach (var col in StatusColumns)
|
|
|
|
|
{
|
|
|
|
|
_saveStatusCommand.Parameters.Add(_saveStatusCommand, "@" + col);
|
|
|
|
|
}
|
2013-12-06 20:07:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-29 05:17:58 +00:00
|
|
|
|
public MetadataStatus GetMetadataStatus(Guid itemId)
|
|
|
|
|
{
|
|
|
|
|
if (itemId == Guid.Empty)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("itemId");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (var cmd = _connection.CreateCommand())
|
|
|
|
|
{
|
|
|
|
|
var cmdText = "select " + string.Join(",", StatusColumns) + " from MetadataStatus where";
|
|
|
|
|
|
|
|
|
|
cmdText += " ItemId=@ItemId";
|
|
|
|
|
cmd.Parameters.Add(cmd, "@ItemId", DbType.Guid).Value = itemId;
|
|
|
|
|
|
|
|
|
|
cmd.CommandText = cmdText;
|
|
|
|
|
|
|
|
|
|
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult | CommandBehavior.SingleRow))
|
|
|
|
|
{
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
return GetStatus(reader);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private MetadataStatus GetStatus(IDataReader reader)
|
|
|
|
|
{
|
|
|
|
|
var result = new MetadataStatus
|
|
|
|
|
{
|
|
|
|
|
ItemId = reader.GetGuid(0)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!reader.IsDBNull(1))
|
|
|
|
|
{
|
2014-02-08 20:02:35 +00:00
|
|
|
|
result.ItemName = reader.GetString(1);
|
2014-01-29 05:17:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!reader.IsDBNull(2))
|
|
|
|
|
{
|
2014-02-08 22:38:02 +00:00
|
|
|
|
result.ItemName = reader.GetString(2);
|
2014-01-29 05:17:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!reader.IsDBNull(3))
|
|
|
|
|
{
|
2014-02-08 22:38:02 +00:00
|
|
|
|
result.SeriesName = reader.GetString(3);
|
2014-01-29 05:17:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!reader.IsDBNull(4))
|
|
|
|
|
{
|
2014-02-08 22:38:02 +00:00
|
|
|
|
result.DateLastMetadataRefresh = reader.GetDateTime(4).ToUniversalTime();
|
2014-01-29 05:17:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!reader.IsDBNull(5))
|
|
|
|
|
{
|
2014-02-08 22:38:02 +00:00
|
|
|
|
result.DateLastImagesRefresh = reader.GetDateTime(5).ToUniversalTime();
|
2014-01-29 05:17:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!reader.IsDBNull(6))
|
|
|
|
|
{
|
2014-02-08 22:38:02 +00:00
|
|
|
|
result.LastStatus = (ProviderRefreshStatus)Enum.Parse(typeof(ProviderRefreshStatus), reader.GetString(6), true);
|
2014-02-08 20:02:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!reader.IsDBNull(7))
|
|
|
|
|
{
|
2014-02-08 22:38:02 +00:00
|
|
|
|
result.LastErrorMessage = reader.GetString(7);
|
2014-02-08 20:02:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-17 03:31:19 +00:00
|
|
|
|
// Skip metadata and image providers
|
2014-01-29 05:17:58 +00:00
|
|
|
|
|
2014-09-11 01:57:11 +00:00
|
|
|
|
if (!reader.IsDBNull(10))
|
|
|
|
|
{
|
|
|
|
|
result.ItemDateModified = reader.GetDateTime(10).ToUniversalTime();
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-29 05:17:58 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SaveMetadataStatus(MetadataStatus status, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
if (status == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("status");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
IDbTransaction transaction = null;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
transaction = _connection.BeginTransaction();
|
|
|
|
|
|
|
|
|
|
_saveStatusCommand.GetParameter(0).Value = status.ItemId;
|
2014-02-08 20:02:35 +00:00
|
|
|
|
_saveStatusCommand.GetParameter(1).Value = status.ItemName;
|
2014-02-08 22:38:02 +00:00
|
|
|
|
_saveStatusCommand.GetParameter(2).Value = status.ItemType;
|
|
|
|
|
_saveStatusCommand.GetParameter(3).Value = status.SeriesName;
|
|
|
|
|
_saveStatusCommand.GetParameter(4).Value = status.DateLastMetadataRefresh;
|
|
|
|
|
_saveStatusCommand.GetParameter(5).Value = status.DateLastImagesRefresh;
|
|
|
|
|
_saveStatusCommand.GetParameter(6).Value = status.LastStatus.ToString();
|
|
|
|
|
_saveStatusCommand.GetParameter(7).Value = status.LastErrorMessage;
|
2015-04-17 03:31:19 +00:00
|
|
|
|
_saveStatusCommand.GetParameter(8).Value = string.Empty;
|
|
|
|
|
_saveStatusCommand.GetParameter(9).Value = string.Empty;
|
2014-09-11 01:57:11 +00:00
|
|
|
|
_saveStatusCommand.GetParameter(10).Value = status.ItemDateModified;
|
2014-01-29 05:17:58 +00:00
|
|
|
|
|
|
|
|
|
_saveStatusCommand.Transaction = transaction;
|
|
|
|
|
|
|
|
|
|
_saveStatusCommand.ExecuteNonQuery();
|
|
|
|
|
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
if (transaction != null)
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Failed to save provider info:", e);
|
2013-12-06 20:07:34 +00:00
|
|
|
|
|
|
|
|
|
if (transaction != null)
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (transaction != null)
|
|
|
|
|
{
|
|
|
|
|
transaction.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_writeLock.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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();
|
|
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
lock (_disposeLock)
|
|
|
|
|
{
|
2013-12-08 19:39:39 +00:00
|
|
|
|
if (_shrinkMemoryTimer != null)
|
|
|
|
|
{
|
|
|
|
|
_shrinkMemoryTimer.Dispose();
|
|
|
|
|
_shrinkMemoryTimer = null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-06 20:07:34 +00:00
|
|
|
|
if (_connection != null)
|
|
|
|
|
{
|
|
|
|
|
if (_connection.IsOpen())
|
|
|
|
|
{
|
|
|
|
|
_connection.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_connection.Dispose();
|
|
|
|
|
_connection = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error disposing database", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|