From 5d5583b02584e4afe8a2619f8ed3739860418f5f Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 20 Nov 2015 21:04:59 -0500 Subject: [PATCH] update sync --- .../Sync/TargetDataProvider.cs | 80 ++++++++++++------- 1 file changed, 50 insertions(+), 30 deletions(-) diff --git a/MediaBrowser.Server.Implementations/Sync/TargetDataProvider.cs b/MediaBrowser.Server.Implementations/Sync/TargetDataProvider.cs index 41d56d9596..24c62a58f6 100644 --- a/MediaBrowser.Server.Implementations/Sync/TargetDataProvider.cs +++ b/MediaBrowser.Server.Implementations/Sync/TargetDataProvider.cs @@ -60,46 +60,61 @@ namespace MediaBrowser.Server.Implementations.Sync return _fileSystem.GetValidFilename(filename); } - private async Task EnsureData(CancellationToken cancellationToken) + private async Task> RetrieveItems(CancellationToken cancellationToken) { - if (_items == null) - { - _logger.Debug("Getting {0} from {1}", string.Join(MediaSync.PathSeparatorString, GetRemotePath().ToArray()), _provider.Name); + _logger.Debug("Getting {0} from {1}", string.Join(MediaSync.PathSeparatorString, GetRemotePath().ToArray()), _provider.Name); - var fileResult = await _provider.GetFiles(new FileQuery - { - FullPath = GetRemotePath().ToArray() + var fileResult = await _provider.GetFiles(new FileQuery + { + FullPath = GetRemotePath().ToArray() - }, _target, cancellationToken).ConfigureAwait(false); + }, _target, cancellationToken).ConfigureAwait(false); - if (fileResult.Items.Length > 0) - { - using (var stream = await _provider.GetFile(fileResult.Items[0].Id, _target, new Progress(), cancellationToken)) - { - _items = _json.DeserializeFromStream>(stream); - } - } - else + if (fileResult.Items.Length > 0) + { + using (var stream = await _provider.GetFile(fileResult.Items[0].Id, _target, new Progress(), cancellationToken)) { - _items = new List(); + return _json.DeserializeFromStream>(stream); } } + + return new List(); } - private async Task SaveData(CancellationToken cancellationToken) + private async Task EnsureData(CancellationToken cancellationToken) + { + if (_items == null) + { + _items = await RetrieveItems(cancellationToken).ConfigureAwait(false); + } + } + + private async Task SaveData(List items, CancellationToken cancellationToken) { using (var stream = new MemoryStream()) { - _json.SerializeToStream(_items, stream); + _json.SerializeToStream(items, stream); // Save to sync provider stream.Position = 0; - await _provider.SendFile(stream, GetRemotePath(), _target, new Progress(), cancellationToken).ConfigureAwait(false); + var remotePath = GetRemotePath(); + _logger.Debug("Saving data.json to {0}. Remote path: {1}", _provider.Name, string.Join("/", remotePath)); + + await _provider.SendFile(stream, remotePath, _target, new Progress(), cancellationToken).ConfigureAwait(false); } } - private async Task GetData(Func, T> dataFactory) + private async Task GetData(bool enableCache, Func, T> dataFactory) { + if (!enableCache) + { + var items = await RetrieveItems(CancellationToken.None).ConfigureAwait(false); + var newCache = items.ToList(); + var result = dataFactory(items); + await UpdateCache(newCache).ConfigureAwait(false); + return result; + } + await _dataLock.WaitAsync().ConfigureAwait(false); try @@ -115,16 +130,21 @@ namespace MediaBrowser.Server.Implementations.Sync } private async Task UpdateData(Func, List> action) + { + var items = await RetrieveItems(CancellationToken.None).ConfigureAwait(false); + items = action(items); + await SaveData(items.ToList(), CancellationToken.None).ConfigureAwait(false); + + await UpdateCache(null).ConfigureAwait(false); + } + + private async Task UpdateCache(List list) { await _dataLock.WaitAsync().ConfigureAwait(false); try { - await EnsureData(CancellationToken.None).ConfigureAwait(false); - - _items = action(_items); - - await SaveData(CancellationToken.None).ConfigureAwait(false); + _items = list; } finally { @@ -134,7 +154,7 @@ namespace MediaBrowser.Server.Implementations.Sync public Task> GetLocalItems(SyncTarget target, string serverId) { - return GetData(items => items.Where(i => string.Equals(i.ServerId, serverId, StringComparison.OrdinalIgnoreCase)).ToList()); + return GetData(false, items => items.Where(i => string.Equals(i.ServerId, serverId, StringComparison.OrdinalIgnoreCase)).ToList()); } public Task AddOrUpdate(SyncTarget target, LocalItem item) @@ -157,17 +177,17 @@ namespace MediaBrowser.Server.Implementations.Sync public Task Get(SyncTarget target, string id) { - return GetData(items => items.FirstOrDefault(i => string.Equals(i.Id, id, StringComparison.OrdinalIgnoreCase))); + return GetData(true, items => items.FirstOrDefault(i => string.Equals(i.Id, id, StringComparison.OrdinalIgnoreCase))); } public Task> GetItems(SyncTarget target, string serverId, string itemId) { - return GetData(items => items.Where(i => string.Equals(i.ServerId, serverId, StringComparison.OrdinalIgnoreCase) && string.Equals(i.ItemId, itemId, StringComparison.OrdinalIgnoreCase)).ToList()); + return GetData(true, items => items.Where(i => string.Equals(i.ServerId, serverId, StringComparison.OrdinalIgnoreCase) && string.Equals(i.ItemId, itemId, StringComparison.OrdinalIgnoreCase)).ToList()); } public Task> GetItemsBySyncJobItemId(SyncTarget target, string serverId, string syncJobItemId) { - return GetData(items => items.Where(i => string.Equals(i.ServerId, serverId, StringComparison.OrdinalIgnoreCase) && string.Equals(i.SyncJobItemId, syncJobItemId, StringComparison.OrdinalIgnoreCase)).ToList()); + return GetData(false, items => items.Where(i => string.Equals(i.ServerId, serverId, StringComparison.OrdinalIgnoreCase) && string.Equals(i.SyncJobItemId, syncJobItemId, StringComparison.OrdinalIgnoreCase)).ToList()); } } }