speed up db upgrade

This commit is contained in:
Luke Pulverenti 2016-07-06 01:13:26 -04:00
parent e50fbdfafc
commit 8629d509e4
3 changed files with 61 additions and 36 deletions

View File

@ -117,7 +117,7 @@ namespace MediaBrowser.Api
config.EnableStandaloneMusicKeys = true; config.EnableStandaloneMusicKeys = true;
config.EnableCaseSensitiveItemIds = true; config.EnableCaseSensitiveItemIds = true;
//config.EnableFolderView = true; //config.EnableFolderView = true;
config.SchemaVersion = 101; config.SchemaVersion = 107;
} }
public void Post(UpdateStartupConfiguration request) public void Post(UpdateStartupConfiguration request)

View File

@ -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);
{ }
IsCurrentSchema = false,
ExcludeItemTypes = new[] { typeof(LiveTvProgram).Name }
});
var numComplete = 0; private async Task UpdateToLatestSchema(int queryStartIndex, int progressStartIndex, int? totalRecordCount, CancellationToken cancellationToken, IProgress<double> progress)
var numItems = itemIds.Count; {
IEnumerable<BaseItem> items;
int numItemsToSave;
var pageSize = 2000;
if (totalRecordCount.HasValue)
{
var list = _libraryManager.GetItemList(new InternalItemsQuery
{
IsCurrentSchema = false,
ExcludeItemTypes = new[] { typeof(LiveTvProgram).Name },
StartIndex = queryStartIndex,
Limit = pageSize
}).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); _logger.Debug("Upgrading schema for {0} items", numItems);
foreach (var itemId in itemIds) if (numItemsToSave > 0)
{ {
cancellationToken.ThrowIfCancellationRequested(); try
if (itemId != Guid.Empty)
{ {
// Somehow some invalid data got into the db. It probably predates the boundary checking await _itemRepo.SaveItems(items, cancellationToken).ConfigureAwait(false);
var item = _libraryManager.GetItemById(itemId); }
catch (OperationCanceledException)
if (item != null) {
{ throw;
try }
{ catch (Exception ex)
await _itemRepo.SaveItem(item, cancellationToken).ConfigureAwait(false); {
} _logger.ErrorException("Error saving item", ex);
catch (OperationCanceledException)
{
throw;
}
catch (Exception ex)
{
_logger.ErrorException("Error saving item", ex);
}
}
} }
numComplete++; progressStartIndex += pageSize;
double percent = numComplete; double percent = progressStartIndex;
percent /= numItems; percent /= numItems;
progress.Report(percent * 100); 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) private async Task CleanDeadItems(CancellationToken cancellationToken, IProgress<double> progress)

View File

@ -95,7 +95,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
private IDbCommand _updateInheritedRatingCommand; private IDbCommand _updateInheritedRatingCommand;
private IDbCommand _updateInheritedTagsCommand; private IDbCommand _updateInheritedTagsCommand;
public const int LatestSchemaVersion = 101; public const int LatestSchemaVersion = 107;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="SqliteItemRepository"/> class. /// Initializes a new instance of the <see cref="SqliteItemRepository"/> class.