Skip to content

Commit

Permalink
Merge branch 'next-beta' into feature/blob-open-read
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas-pajurek authored Aug 16, 2024
2 parents 83fba11 + 479b2f9 commit 6170dff
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 29 deletions.
28 changes: 14 additions & 14 deletions docs/storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,19 +237,19 @@ Clients are thread-safe.
| `Name` | |
| `Uri` | |

| Method group | Note |
| ---------------------------- | ---- |
| `Create` | |
| `CreateIfNotExists` | |
| `DeleteBlob` | |
| `DeleteBlobIfExists` | |
| `Exists` | |
| `GetBlobs` | |
| `GetBlobClient` | |
| `GetBlockBlobClient` | |
| `GetParentBlobServiceClient` | |
| `GetProperties` | |
| `UploadBlob` | |
| Method group | Note |
| ---------------------------- | ---------------------------------------------------------------------------------------------- |
| `Create` | |
| `CreateIfNotExists` | |
| `DeleteBlob` | |
| `DeleteBlobIfExists` | |
| `Exists` | |
| `GetBlobClient` | |
| `GetBlockBlobClient` | |
| `GetBlobs` | The `BlobTraits` and `BlobStates` parameters are ignored except `BlobStates.Uncommitted` flag. |
| `GetParentBlobServiceClient` | |
| `GetProperties` | |
| `UploadBlob` | |

| Constructors & factory methods | Note |
| ----------------------------------------------------------------------- | ----------------------------- |
Expand Down Expand Up @@ -457,7 +457,7 @@ Following hooks are supported in both `Before` and `After` variants:
- `Upload`
- `OpenRead`
- All `Container` operations
- `Create`
- `Create` / `CreateIfNotExists`
- All `Table Service` operations
- All `Entity` operations
- `Add`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public override AsyncPageable<BlobItem> GetBlobsAsync(
string? prefix = null,
CancellationToken cancellationToken = default)
{
var blobs = GetBlobsCore(prefix);
var blobs = GetBlobsCore(prefix, states);
return new InMemoryPageable.YieldingAsync<BlobItem>(blobs, _defaultMaxPageSize);
}

Expand All @@ -217,16 +217,16 @@ public override Pageable<BlobItem> GetBlobs(
string? prefix = null,
CancellationToken cancellationToken = default)
{
var blobs = GetBlobsCore(prefix);
var blobs = GetBlobsCore(prefix, states);
return new InMemoryPageable.Sync<BlobItem>(blobs, _defaultMaxPageSize);
}


private IReadOnlyList<BlobItem> GetBlobsCore(string? prefix)
private IReadOnlyList<BlobItem> GetBlobsCore(string? prefix, BlobStates? states)
{
var container = GetContainer();

return container.GetBlobs(prefix);
return container.GetBlobs(prefix, states);
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,27 @@ public BlobContainerProperties GetProperties()

public override string? ToString() => $"{Service} / {Name}";

public IReadOnlyList<BlobItem> GetBlobs(string? prefix)
public IReadOnlyList<BlobItem> GetBlobs(string? prefix, BlobStates? states)
{
lock (_lock)
{
return _blobEntries
.Values
.Where(entry => entry.Blob.Exists)
.Where(entry => prefix is null ? true : entry.Blob.Name.StartsWith(prefix))
.Where(entry => filter(entry.Blob))
.Select(entry => BlobsModelFactory.BlobItem(entry.Blob.Name))
.ToList();
}

bool filter(InMemoryBlockBlob blob)
{
var result = true;

result &= blob.Exists || (states?.HasFlag(BlobStates.Uncommitted) is true && blob.HasUncommittedBlocks);
result &= prefix is null || blob.Name.StartsWith(prefix);

return result;
}

}

public AcquiredBlob AcquireBlob(string blobName, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ internal class InMemoryBlockBlob(string blobName, InMemoryBlobContainer containe

public bool Exists => _properties is not null;

public bool HasUncommittedBlocks => _uncommittedBlocks is not null;

public bool TryGetProperties(
BlobRequestConditions? conditions,
[NotNullWhen(true)] out BlobProperties? properties,
Expand Down
2 changes: 0 additions & 2 deletions src/Spotflow.InMemory.Azure/Internals/TaskExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Runtime.CompilerServices;

namespace Spotflow.InMemory.Azure.Internals;

internal static class TaskExtensions
Expand Down
21 changes: 15 additions & 6 deletions tests/Tests/Storage/Blobs/BlobContainerClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Azure;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Azure.Storage.Blobs.Specialized;

using Spotflow.InMemory.Azure.Storage;
using Spotflow.InMemory.Azure.Storage.Blobs;
Expand Down Expand Up @@ -142,23 +143,31 @@ public void Exists_For_Existing_Container_Should_Be_True()

[TestMethod]
[TestCategory(TestCategory.AzureInfra)]
public void GetBlobs_Should_Return_Existing_Blobs()
[DataRow(10, 1, BlobStates.None, 10)]
[DataRow(10, 1, BlobStates.Uncommitted, 11)]
public void GetBlobs_Should_Return_Existing_Relevant_Blobs(int commitedCount, int uncommitedCount, BlobStates states, int expectedTotalCount)
{
var containerClient = ImplementationProvider.GetBlobContainerClient();

containerClient.CreateIfNotExists();

var blobNamePrefix = Guid.NewGuid().ToString();

var count = ImplementationProvider.IsAzureConfigAvailable ? 10 : 100_000;

for (var i = 0; i < count; i++)
for (var i = 0; i < commitedCount; i++)
{
var blobClient = containerClient.GetBlobClient($"{blobNamePrefix}_test-blob-{i:D10}");
var blobClient = containerClient.GetBlobClient($"{blobNamePrefix}_test-blob-commited-{i:D10}");
blobClient.Upload(BinaryData.FromString("test"));
}

containerClient.GetBlobs(prefix: blobNamePrefix).Should().HaveCount(count);
for (var i = 0; i < uncommitedCount; i++)
{
var blockBlobClient = containerClient.GetBlockBlobClient($"{blobNamePrefix}_test-blob-uncommited-{i:D10}");
blockBlobClient.StageBlock(Convert.ToBase64String([1]), BinaryData.FromString("test").ToStream());
}

containerClient.GetBlobs(prefix: blobNamePrefix, states: states)
.Should()
.HaveCount(expectedTotalCount);
}

[TestMethod]
Expand Down

0 comments on commit 6170dff

Please sign in to comment.