Skip to content

Commit

Permalink
Don't rebuild storage tree after save/delete if tree wasn't built bef…
Browse files Browse the repository at this point in the history
…ore. Don't allow parallel tree build.
  • Loading branch information
SonicGD committed Dec 18, 2020
1 parent 8529b1d commit 1b3d164
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/Sitko.Core.Storage/Sitko.Core.Storage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="MimeMapping" Version="1.0.1.30" />
<PackageReference Include="Nito.AsyncEx" Version="5.1.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1' ">
Expand Down
25 changes: 20 additions & 5 deletions src/Sitko.Core.Storage/Storage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Nito.AsyncEx;
using Sitko.Core.Storage.Cache;

namespace Sitko.Core.Storage
Expand All @@ -16,6 +17,7 @@ public abstract class Storage<T> : IStorage<T>, IAsyncDisposable where T : Stora
private readonly T _options;
private StorageNode? _tree;
private DateTimeOffset? _treeLastBuild;
private AsyncLock _treeLock = new();

protected Storage(T options, ILogger<Storage<T>> logger, IStorageCache? cache)
{
Expand All @@ -39,7 +41,7 @@ public async Task<StorageItem> SaveAsync(Stream file, string fileName, string pa
itemMetadata);

var result = await SaveStorageItemAsync(file, path, destinationPath, storageItem, itemMetadata);
await BuildStorageTreeAsync();
await RebuildStorageTreeAsync();
return result;
}

Expand All @@ -50,7 +52,7 @@ private async Task<StorageItem> SaveStorageItemAsync(Stream file, string path, s
file.Seek(0, SeekOrigin.Begin);
await DoSaveAsync(destinationPath, file, JsonSerializer.Serialize(metadata));
Logger.LogInformation("File saved to {Path}", path);
if (_cache != null && storageItem.FilePath != null)
if (_cache != null && !string.IsNullOrEmpty(storageItem.FilePath))
{
await _cache.RemoveItemAsync(storageItem.FilePath);
}
Expand Down Expand Up @@ -128,7 +130,7 @@ public async Task<bool> DeleteAsync(string filePath)
}

var result = await DoDeleteAsync(filePath);
await BuildStorageTreeAsync();
await RebuildStorageTreeAsync();
return result;
}

Expand Down Expand Up @@ -199,10 +201,23 @@ public async Task<IEnumerable<StorageNode>> GetDirectoryContentsAsync(string pat
return current?.Children ?? new StorageNode[0];
}

private async Task RebuildStorageTreeAsync()
{
if (_tree != null)
{
await BuildStorageTreeAsync();
}
}

private async Task BuildStorageTreeAsync()
{
_tree = await DoBuildStorageTreeAsync();
_treeLastBuild = DateTimeOffset.UtcNow;
using (await _treeLock.LockAsync())
{
Logger.LogInformation("Start building storage tree");
_tree = await DoBuildStorageTreeAsync();
_treeLastBuild = DateTimeOffset.UtcNow;
Logger.LogInformation("Done building storage tree");
}
}

protected abstract Task<StorageNode?> DoBuildStorageTreeAsync();
Expand Down

0 comments on commit 1b3d164

Please sign in to comment.