Skip to content

Commit

Permalink
Merge pull request #2 from SenseNet/feature/upgrade-services
Browse files Browse the repository at this point in the history
Upgrade sensenet references and add support for the async api.
  • Loading branch information
tusmester authored Oct 16, 2019
2 parents 3bb8857 + 7dc8bfc commit 9b9265c
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 64 deletions.
33 changes: 18 additions & 15 deletions src/AzureBlobStorage.Tests/AzureBlobProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class AzureBlobProviderTests
private static int _fileId;

[Fact]
public void WriteTest()
public async Task WriteTest()
{
const int chunkSize = 4096;
const int contentSize = 64 * 1024;
Expand All @@ -32,7 +32,7 @@ public void WriteTest()
while(offset < content.Length)
{
var chunk = content.Skip(offset).Take(chunkSize).ToArray();
provider.Write(context, offset, chunk);
await provider.WriteAsync(context, offset, chunk, CancellationToken.None);
offset += chunk.Length;
}

Expand All @@ -59,15 +59,15 @@ public async Task WriteAsyncTest()
var chunk = content.Skip(offset).Take(chunkSize).ToArray();

// write chunk asynchronously
await provider.WriteAsync(context, offset, chunk);
await provider.WriteAsync(context, offset, chunk, CancellationToken.None);
offset += chunk.Length;
}

TestNewBlobAndCleanup(provider, blobId, context, contentSize);
}

[Fact]
public void WriteChunkTest_Error_NotWritten()
public async Task WriteChunkTest_Error_NotWritten()
{
const int uploadChunkSize = 500;
const int wrongBinaryChunkSize = 300;
Expand All @@ -83,12 +83,12 @@ public void WriteChunkTest_Error_NotWritten()

var offset = 0;

Assert.Throws<InvalidOperationException>(() =>
await Assert.ThrowsAsync<InvalidOperationException>(async () =>
{
while (offset < content.Length)
{
var chunk = content.Skip(offset).Take(uploadChunkSize).ToArray();
provider.Write(context, offset, chunk);
await provider.WriteAsync(context, offset, chunk, CancellationToken.None);
offset += chunk.Length;
}
});
Expand All @@ -100,7 +100,7 @@ public void WriteChunkTest_Error_NotWritten()
}

[Fact]
public void WriteChunkTest_Error_WrongBlockList()
public async Task WriteChunkTest_Error_WrongBlockList()
{
const int uploadChunkSize = 400;
const int wrongBinaryChunkSize = 200;
Expand All @@ -116,12 +116,12 @@ public void WriteChunkTest_Error_WrongBlockList()

var offset = 0;

Assert.Throws<InvalidOperationException>(() =>
await Assert.ThrowsAsync<InvalidOperationException>(async () =>
{
while (offset < content.Length)
{
var chunk = content.Skip(offset).Take(uploadChunkSize).ToArray();
provider.Write(context, offset, chunk);
await provider.WriteAsync(context, offset, chunk, CancellationToken.None);
offset += chunk.Length;
}
});
Expand Down Expand Up @@ -267,7 +267,7 @@ public void CloneStreamForWriteTest(string blobId, long size)

[Theory]
[MemberData(nameof(TestData.BlobData), MemberType = typeof(TestData))]
public void DeleteTest(string blobId, long size)
public async Task DeleteTest(string blobId, long size)
{
const int chunkSize = 1024 * 1024;

Expand All @@ -277,9 +277,12 @@ public void DeleteTest(string blobId, long size)
out var _,
out var _, blobId);

// the blob provider generated a new blob id
blobId = ((AzureBlobProviderData) context.BlobProviderData).BlobId;

Assert.True(provider.BlobExists(blobId));

provider.Delete(context);
await provider.DeleteAsync(context, CancellationToken.None);

Assert.False(provider.BlobExists(blobId));

Expand Down Expand Up @@ -320,8 +323,8 @@ private static void Cleanup(AzureBlobProvider provider, string blobId)
{
BlobProviderData = new AzureBlobProviderData { BlobId = blobId }
};
provider.Allocate(context);
provider.Delete(context);

provider.DeleteAsync(context, CancellationToken.None).GetAwaiter().GetResult();
}

private static void PrepareBlob(int fileId, int contentSize, int chunkSize,
Expand All @@ -347,7 +350,7 @@ private static void PrepareBlob(int fileId, int contentSize, int chunkSize,
}
};

provider.Allocate(context);
provider.AllocateAsync(context, CancellationToken.None).GetAwaiter().GetResult();

blobId = ((AzureBlobProviderData)context.BlobProviderData).BlobId;

Expand All @@ -371,7 +374,7 @@ private static void WriteNewBlob(int fileId, int contentSize, int chunkSize,
while (offset < content.Length)
{
var chunk = content.Skip(offset).Take(chunkSize).ToArray();
provider.Write(context, offset, chunk);
provider.WriteAsync(context, offset, chunk, CancellationToken.None).GetAwaiter().GetResult();
offset += chunk.Length;
}
}
Expand Down
34 changes: 28 additions & 6 deletions src/AzureBlobStorage.Tests/AzureBlobStorage.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,31 @@
<Reference Include="Microsoft.Azure.Storage.Common, Version=10.0.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Azure.Storage.Common.10.0.3\lib\net452\Microsoft.Azure.Storage.Common.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Extensions.Configuration, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Extensions.Configuration.2.2.0\lib\netstandard2.0\Microsoft.Extensions.Configuration.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Extensions.Configuration.Abstractions, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Extensions.Configuration.Abstractions.2.2.0\lib\netstandard2.0\Microsoft.Extensions.Configuration.Abstractions.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Extensions.Primitives, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Extensions.Primitives.2.2.0\lib\netstandard2.0\Microsoft.Extensions.Primitives.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="SenseNet.BlobStorage, Version=7.3.4.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\SenseNet.BlobStorage.7.3.4\lib\netstandard2.0\SenseNet.BlobStorage.dll</HintPath>
<Reference Include="SenseNet.BlobStorage, Version=7.4.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\SenseNet.BlobStorage.7.4.0\lib\netstandard2.0\SenseNet.BlobStorage.dll</HintPath>
</Reference>
<Reference Include="SenseNet.Common, Version=7.3.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\SenseNet.Common.7.3.0\lib\netstandard2.0\SenseNet.Common.dll</HintPath>
<Reference Include="SenseNet.Common, Version=7.4.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\SenseNet.Common.7.4.0\lib\netstandard2.0\SenseNet.Common.dll</HintPath>
</Reference>
<Reference Include="SenseNet.Tools, Version=3.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\SenseNet.Tools.3.1.0\lib\netstandard2.0\SenseNet.Tools.dll</HintPath>
<Reference Include="SenseNet.Tools, Version=3.2.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\SenseNet.Tools.3.2.0\lib\netstandard2.0\SenseNet.Tools.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Buffers.4.4.0\lib\netstandard2.0\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="System.Configuration" />
<Reference Include="System.Configuration.ConfigurationManager, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Configuration.ConfigurationManager.4.5.0\lib\net461\System.Configuration.ConfigurationManager.dll</HintPath>
Expand All @@ -76,7 +88,17 @@
<Reference Include="System.Drawing.Common, Version=4.0.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Drawing.Common.4.5.1\lib\net461\System.Drawing.Common.dll</HintPath>
</Reference>
<Reference Include="System.Memory, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Memory.4.5.1\lib\netstandard2.0\System.Memory.dll</HintPath>
</Reference>
<Reference Include="System.Net" />
<Reference Include="System.Numerics" />
<Reference Include="System.Numerics.Vectors, Version=4.1.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Numerics.Vectors.4.4.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.5.1\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Security.AccessControl, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.AccessControl.4.5.0\lib\net461\System.Security.AccessControl.dll</HintPath>
</Reference>
Expand Down
13 changes: 10 additions & 3 deletions src/AzureBlobStorage.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@
<package id="Microsoft.Azure.Storage.Blob" version="10.0.3" targetFramework="net461" />
<package id="Microsoft.Azure.Storage.Common" version="10.0.3" targetFramework="net461" />
<package id="Microsoft.CSharp" version="4.5.0" targetFramework="net461" />
<package id="Microsoft.Extensions.Configuration" version="2.2.0" targetFramework="net461" />
<package id="Microsoft.Extensions.Configuration.Abstractions" version="2.2.0" targetFramework="net461" />
<package id="Microsoft.Extensions.Primitives" version="2.2.0" targetFramework="net461" />
<package id="Newtonsoft.Json" version="11.0.2" targetFramework="net461" />
<package id="SenseNet.BlobStorage" version="7.3.4" targetFramework="net461" />
<package id="SenseNet.Common" version="7.3.0" targetFramework="net461" />
<package id="SenseNet.Tools" version="3.1.0" targetFramework="net461" />
<package id="SenseNet.BlobStorage" version="7.4.0" targetFramework="net461" />
<package id="SenseNet.Common" version="7.4.0" targetFramework="net461" />
<package id="SenseNet.Tools" version="3.2.0" targetFramework="net461" />
<package id="System.Buffers" version="4.4.0" targetFramework="net461" />
<package id="System.Configuration.ConfigurationManager" version="4.5.0" targetFramework="net461" />
<package id="System.Data.SqlClient" version="4.5.1" targetFramework="net461" />
<package id="System.Diagnostics.EventLog" version="4.5.0" targetFramework="net461" />
<package id="System.Drawing.Common" version="4.5.1" targetFramework="net461" />
<package id="System.Memory" version="4.5.1" targetFramework="net461" />
<package id="System.Numerics.Vectors" version="4.4.0" targetFramework="net461" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.1" targetFramework="net461" />
<package id="System.Security.AccessControl" version="4.5.0" targetFramework="net461" />
<package id="System.Security.Permissions" version="4.5.0" targetFramework="net461" />
<package id="System.Security.Principal.Windows" version="4.5.0" targetFramework="net461" />
Expand Down
49 changes: 11 additions & 38 deletions src/AzureBlobStorage/AzureBlobProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Blob;
Expand Down Expand Up @@ -78,7 +79,7 @@ public IEnumerable<string> GetBlobIds()

#region IBlobProvider

public void Allocate(BlobStorageContext context)
public Task AllocateAsync(BlobStorageContext context, CancellationToken cancellationToken)
{
SnTrace.Database.Write("AzureBlobProvider.Allocate: {0}", context.BlobProviderData);

Expand All @@ -87,20 +88,22 @@ public void Allocate(BlobStorageContext context)
BlobId = NewBlobId(),
ChunkSize = ChunkSize
};

return Task.CompletedTask;
}

public Stream CloneStream(BlobStorageContext context, Stream stream)
{
return stream is CloudBlobStream ? GetStreamForWrite(context) : GetStreamForRead(context);
}

public void Delete(BlobStorageContext context)
public async Task DeleteAsync(BlobStorageContext context, CancellationToken cancellationToken)
{
using (var op = SnTrace.Database.StartOperation("AzureBlobProvider.Delete: {0}", context.BlobProviderData))
{
var providerData = (AzureBlobProviderData)context.BlobProviderData;
var blob = GetBlob(providerData.BlobId);
blob.Delete();
await blob.DeleteAsync(cancellationToken).ConfigureAwait(false);
op.Successful = true;
}
}
Expand Down Expand Up @@ -128,8 +131,8 @@ public object ParseData(string providerData)
SnTrace.Database.Write("AzureBlobProvider.ParseData: {0}", providerData);
return BlobStorageContext.DeserializeBlobProviderData<AzureBlobProviderData>(providerData);
}

public void Write(BlobStorageContext context, long offset, byte[] buffer)
public async Task WriteAsync(BlobStorageContext context, long offset, byte[] buffer, CancellationToken cancellationToken)
{
var providerData = (AzureBlobProviderData)context.BlobProviderData;

Expand All @@ -149,15 +152,16 @@ public void Write(BlobStorageContext context, long offset, byte[] buffer)

using (var chunkStream = new MemoryStream(buffer))
{
blob.PutBlock(blockId, chunkStream, null, null, Options);
await blob.PutBlockAsync(blockId, chunkStream, null, null, Options, null, cancellationToken)
.ConfigureAwait(false);
}

if (id != blockCount)
return;

var blockList = Enumerable.Range(1, blockCount).ToList().ConvertAll(ConvertToBlockId);

blob.PutBlockList(blockList, null, Options);
await blob.PutBlockListAsync(blockList, null, Options, null, cancellationToken).ConfigureAwait(false);

SetBlobMetadata(blob, context);

Expand All @@ -168,37 +172,6 @@ string ConvertToBlockId(int blobId)
}
}

public async Task WriteAsync(BlobStorageContext context, long offset, byte[] buffer)
{
var providerData = (AzureBlobProviderData)context.BlobProviderData;

SnTrace.Database.Write("AzureBlobProvider.Write: {0}", providerData);

var blob = GetBlob(providerData.BlobId);
var chunkSize = providerData.ChunkSize;
var id = (int)(offset / chunkSize) + 1;
var blockId =
Convert.ToBase64String(
Encoding.UTF8.GetBytes(string.Format(CultureInfo.InvariantCulture, "{0:D6}", id)));

var blockCount = (int)Math.Ceiling((decimal)context.Length / chunkSize);
using (var chunkStream = new MemoryStream(buffer))
{
await blob.PutBlockAsync(blockId, chunkStream, null, null, Options, null);
}

if (id != blockCount)
return;

var blockList = Enumerable.Range(1, blockCount).ToList().ConvertAll(block =>
Convert.ToBase64String(
Encoding.UTF8.GetBytes(string.Format(CultureInfo.InvariantCulture, "{0:D6}", block))));

await blob.PutBlockListAsync(blockList, null, Options, null);

SetBlobMetadata(blob, context);
}

#endregion

#region IMultiTenantProvider
Expand Down
4 changes: 2 additions & 2 deletions src/AzureBlobStorage/AzureBlobStorage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Storage.Blob" Version="10.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="SenseNet.BlobStorage" Version="7.3.4" />
<PackageReference Include="SenseNet.Tools" Version="3.1.0" />
<PackageReference Include="SenseNet.BlobStorage" Version="7.4.0" />
<PackageReference Include="SenseNet.Tools" Version="3.2.0" />
</ItemGroup>

</Project>

0 comments on commit 9b9265c

Please sign in to comment.