aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Server.Implementations/Persistence
diff options
context:
space:
mode:
authorLuke <luke.pulverenti@gmail.com>2016-07-06 15:46:35 -0400
committerGitHub <noreply@github.com>2016-07-06 15:46:35 -0400
commitecdfb3ceff7d3c462e39dbcfda8f2a547a5140e5 (patch)
tree3eb53b4177ba8d82f01c8a926bf533ddacda7dc3 /MediaBrowser.Server.Implementations/Persistence
parent41fd919629dc833fcf0018bc0260443d5ca2dedc (diff)
parent80688496e8ad610b288ff8420a8853e91cd59749 (diff)
Merge pull request #1923 from MediaBrowser/dev
Dev
Diffstat (limited to 'MediaBrowser.Server.Implementations/Persistence')
-rw-r--r--MediaBrowser.Server.Implementations/Persistence/CleanDatabaseScheduledTask.cs91
-rw-r--r--MediaBrowser.Server.Implementations/Persistence/SqliteItemRepository.cs82
2 files changed, 134 insertions, 39 deletions
diff --git a/MediaBrowser.Server.Implementations/Persistence/CleanDatabaseScheduledTask.cs b/MediaBrowser.Server.Implementations/Persistence/CleanDatabaseScheduledTask.cs
index b11a3e496..c321e5f01 100644
--- a/MediaBrowser.Server.Implementations/Persistence/CleanDatabaseScheduledTask.cs
+++ b/MediaBrowser.Server.Implementations/Persistence/CleanDatabaseScheduledTask.cs
@@ -142,52 +142,77 @@ namespace MediaBrowser.Server.Implementations.Persistence
}
}
- private async Task UpdateToLatestSchema(CancellationToken cancellationToken, IProgress<double> progress)
+ private Task UpdateToLatestSchema(CancellationToken cancellationToken, IProgress<double> progress)
{
- var itemIds = _libraryManager.GetItemIds(new InternalItemsQuery
+ return UpdateToLatestSchema(0, 0, null, cancellationToken, progress);
+ }
+
+ private async Task UpdateToLatestSchema(int queryStartIndex, int progressStartIndex, int? totalRecordCount, CancellationToken cancellationToken, IProgress<double> progress)
+ {
+ IEnumerable<BaseItem> items;
+ int numItemsToSave;
+ var pageSize = 1000;
+
+ if (totalRecordCount.HasValue)
{
- IsCurrentSchema = false,
- ExcludeItemTypes = new[] { typeof(LiveTvProgram).Name }
- });
+ var list = _libraryManager.GetItemList(new InternalItemsQuery
+ {
+ IsCurrentSchema = false,
+ ExcludeItemTypes = new[] { typeof(LiveTvProgram).Name },
+ StartIndex = queryStartIndex,
+ Limit = pageSize
- var numComplete = 0;
- var numItems = itemIds.Count;
+ }).ToList();
+
+ items = list;
+ numItemsToSave = list.Count;
+ }
+ else
+ {
+ var itemsResult = _libraryManager.GetItemsResult(new InternalItemsQuery
+ {
+ IsCurrentSchema = false,
+ ExcludeItemTypes = new[] { typeof(LiveTvProgram).Name },
+ StartIndex = queryStartIndex,
+ Limit = pageSize
+ });
+
+ totalRecordCount = itemsResult.TotalRecordCount;
+ items = itemsResult.Items;
+ numItemsToSave = itemsResult.Items.Length;
+ }
+
+ var numItems = totalRecordCount.Value;
_logger.Debug("Upgrading schema for {0} items", numItems);
- foreach (var itemId in itemIds)
+ if (numItemsToSave > 0)
{
- cancellationToken.ThrowIfCancellationRequested();
-
- if (itemId != Guid.Empty)
+ try
{
- // Somehow some invalid data got into the db. It probably predates the boundary checking
- var item = _libraryManager.GetItemById(itemId);
-
- if (item != null)
- {
- try
- {
- await _itemRepo.SaveItem(item, cancellationToken).ConfigureAwait(false);
- }
- catch (OperationCanceledException)
- {
- throw;
- }
- catch (Exception ex)
- {
- _logger.ErrorException("Error saving item", ex);
- }
- }
+ await _itemRepo.SaveItems(items, cancellationToken).ConfigureAwait(false);
+ }
+ catch (OperationCanceledException)
+ {
+ throw;
+ }
+ catch (Exception ex)
+ {
+ _logger.ErrorException("Error saving item", ex);
}
- numComplete++;
- double percent = numComplete;
+ progressStartIndex += pageSize;
+ double percent = progressStartIndex;
percent /= numItems;
progress.Report(percent * 100);
- }
- progress.Report(100);
+ var newStartIndex = queryStartIndex + (pageSize - numItemsToSave);
+ await UpdateToLatestSchema(newStartIndex, progressStartIndex, totalRecordCount, cancellationToken, progress).ConfigureAwait(false);
+ }
+ else
+ {
+ progress.Report(100);
+ }
}
private async Task CleanDeadItems(CancellationToken cancellationToken, IProgress<double> progress)
diff --git a/MediaBrowser.Server.Implementations/Persistence/SqliteItemRepository.cs b/MediaBrowser.Server.Implementations/Persistence/SqliteItemRepository.cs
index 5b492c240..93f58b399 100644
--- a/MediaBrowser.Server.Implementations/Persistence/SqliteItemRepository.cs
+++ b/MediaBrowser.Server.Implementations/Persistence/SqliteItemRepository.cs
@@ -95,7 +95,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
private IDbCommand _updateInheritedRatingCommand;
private IDbCommand _updateInheritedTagsCommand;
- public const int LatestSchemaVersion = 97;
+ public const int LatestSchemaVersion = 107;
/// <summary>
/// Initializes a new instance of the <see cref="SqliteItemRepository"/> class.
@@ -271,10 +271,15 @@ namespace MediaBrowser.Server.Implementations.Persistence
_connection.AddColumn(Logger, "TypedBaseItems", "IsVirtualItem", "BIT");
_connection.AddColumn(Logger, "TypedBaseItems", "SeriesName", "Text");
_connection.AddColumn(Logger, "TypedBaseItems", "UserDataKey", "Text");
+ _connection.AddColumn(Logger, "TypedBaseItems", "SeasonName", "Text");
+ _connection.AddColumn(Logger, "TypedBaseItems", "SeasonId", "GUID");
+ _connection.AddColumn(Logger, "TypedBaseItems", "SeriesId", "GUID");
_connection.AddColumn(Logger, "UserDataKeys", "Priority", "INT");
_connection.AddColumn(Logger, "ItemValues", "CleanValue", "Text");
+ _connection.AddColumn(Logger, ChaptersTableName, "ImageDateModified", "DATETIME");
+
string[] postQueries =
{
@@ -402,7 +407,11 @@ namespace MediaBrowser.Server.Implementations.Persistence
"Album",
"CriticRating",
"CriticRatingSummary",
- "IsVirtualItem"
+ "IsVirtualItem",
+ "SeriesName",
+ "SeasonName",
+ "SeasonId",
+ "SeriesId"
};
private readonly string[] _mediaStreamSaveColumns =
@@ -522,7 +531,10 @@ namespace MediaBrowser.Server.Implementations.Persistence
"Album",
"IsVirtualItem",
"SeriesName",
- "UserDataKey"
+ "UserDataKey",
+ "SeasonName",
+ "SeasonId",
+ "SeriesId"
};
_saveItemCommand = _connection.CreateCommand();
_saveItemCommand.CommandText = "replace into TypedBaseItems (" + string.Join(",", saveColumns.ToArray()) + ") values (";
@@ -581,6 +593,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
_saveChapterCommand.Parameters.Add(_saveChapterCommand, "@StartPositionTicks");
_saveChapterCommand.Parameters.Add(_saveChapterCommand, "@Name");
_saveChapterCommand.Parameters.Add(_saveChapterCommand, "@ImagePath");
+ _saveChapterCommand.Parameters.Add(_saveChapterCommand, "@ImageDateModified");
// MediaStreams
_deleteStreamsCommand = _connection.CreateCommand();
@@ -944,7 +957,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
var hasSeries = item as IHasSeries;
if (hasSeries != null)
{
- _saveItemCommand.GetParameter(index++).Value = hasSeries.SeriesName;
+ _saveItemCommand.GetParameter(index++).Value = hasSeries.FindSeriesName();
}
else
{
@@ -953,6 +966,27 @@ namespace MediaBrowser.Server.Implementations.Persistence
_saveItemCommand.GetParameter(index++).Value = item.GetUserDataKeys().FirstOrDefault();
+ var episode = item as Episode;
+ if (episode != null)
+ {
+ _saveItemCommand.GetParameter(index++).Value = episode.FindSeasonName();
+ _saveItemCommand.GetParameter(index++).Value = episode.FindSeasonId();
+ }
+ else
+ {
+ _saveItemCommand.GetParameter(index++).Value = null;
+ _saveItemCommand.GetParameter(index++).Value = null;
+ }
+
+ if (hasSeries != null)
+ {
+ _saveItemCommand.GetParameter(index++).Value = hasSeries.FindSeriesId();
+ }
+ else
+ {
+ _saveItemCommand.GetParameter(index++).Value = null;
+ }
+
_saveItemCommand.Transaction = transaction;
_saveItemCommand.ExecuteNonQuery();
@@ -1375,6 +1409,36 @@ namespace MediaBrowser.Server.Implementations.Persistence
item.IsVirtualItem = reader.GetBoolean(58);
}
+ var hasSeries = item as IHasSeries;
+ if (hasSeries != null)
+ {
+ if (!reader.IsDBNull(59))
+ {
+ hasSeries.SeriesName = reader.GetString(59);
+ }
+ }
+
+ var episode = item as Episode;
+ if (episode != null)
+ {
+ if (!reader.IsDBNull(60))
+ {
+ episode.SeasonName = reader.GetString(60);
+ }
+ if (!reader.IsDBNull(61))
+ {
+ episode.SeasonId = reader.GetGuid(61);
+ }
+ }
+
+ if (hasSeries != null)
+ {
+ if (!reader.IsDBNull(62))
+ {
+ hasSeries.SeriesId = reader.GetGuid(62);
+ }
+ }
+
return item;
}
@@ -1436,7 +1500,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
using (var cmd = _connection.CreateCommand())
{
- cmd.CommandText = "select StartPositionTicks,Name,ImagePath from " + ChaptersTableName + " where ItemId = @ItemId order by ChapterIndex asc";
+ cmd.CommandText = "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId order by ChapterIndex asc";
cmd.Parameters.Add(cmd, "@ItemId", DbType.Guid).Value = id;
@@ -1469,7 +1533,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
using (var cmd = _connection.CreateCommand())
{
- cmd.CommandText = "select StartPositionTicks,Name,ImagePath from " + ChaptersTableName + " where ItemId = @ItemId and ChapterIndex=@ChapterIndex";
+ cmd.CommandText = "select StartPositionTicks,Name,ImagePath,ImageDateModified from " + ChaptersTableName + " where ItemId = @ItemId and ChapterIndex=@ChapterIndex";
cmd.Parameters.Add(cmd, "@ItemId", DbType.Guid).Value = id;
cmd.Parameters.Add(cmd, "@ChapterIndex", DbType.Int32).Value = index;
@@ -1507,6 +1571,11 @@ namespace MediaBrowser.Server.Implementations.Persistence
chapter.ImagePath = reader.GetString(2);
}
+ if (!reader.IsDBNull(3))
+ {
+ chapter.ImageDateModified = reader.GetDateTime(3).ToUniversalTime();
+ }
+
return chapter;
}
@@ -1566,6 +1635,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
_saveChapterCommand.GetParameter(2).Value = chapter.StartPositionTicks;
_saveChapterCommand.GetParameter(3).Value = chapter.Name;
_saveChapterCommand.GetParameter(4).Value = chapter.ImagePath;
+ _saveChapterCommand.GetParameter(5).Value = chapter.ImageDateModified;
_saveChapterCommand.Transaction = transaction;