[#1] Return iterator from ListContainersAsync and SearchObjectsAsync

Signed-off-by: Ivan Pchelintsev <i.pchelintsev@yadro.com>
This commit is contained in:
Ivan Pchelintsev 2024-05-22 10:32:07 +03:00
parent 70ba87bc60
commit 8cacbcc8e9
3 changed files with 21 additions and 18 deletions

View file

@ -5,7 +5,7 @@ namespace FrostFS.SDK.ClientV2.Interfaces;
public interface IFrostFSClient public interface IFrostFSClient
{ {
Task<ModelsV2.Container> GetContainerAsync(ContainerId containerId); Task<ModelsV2.Container> GetContainerAsync(ContainerId containerId);
Task<ContainerId[]> ListContainersAsync(); IAsyncEnumerable<ContainerId> ListContainersAsync();
Task<ContainerId> CreateContainerAsync(ModelsV2.Container container); Task<ContainerId> CreateContainerAsync(ModelsV2.Container container);
Task DeleteContainerAsync(ContainerId containerId); Task DeleteContainerAsync(ContainerId containerId);
Task<ObjectHeader> GetObjectHeadAsync(ContainerId containerId, ObjectId objectId); Task<ObjectHeader> GetObjectHeadAsync(ContainerId containerId, ObjectId objectId);
@ -13,5 +13,5 @@ public interface IFrostFSClient
Task<ObjectId> PutObjectAsync(ObjectHeader header, Stream payload); Task<ObjectId> PutObjectAsync(ObjectHeader header, Stream payload);
Task<ObjectId> PutObjectAsync(ObjectHeader header, byte[] payload); Task<ObjectId> PutObjectAsync(ObjectHeader header, byte[] payload);
Task DeleteObjectAsync(ContainerId containerId, ObjectId objectId); Task DeleteObjectAsync(ContainerId containerId, ObjectId objectId);
Task<ObjectId[]> SearchObjectAsync(ContainerId cid, params ObjectFilter[] filters); IAsyncEnumerable<ObjectId> SearchObjectsAsync(ContainerId cid, params ObjectFilter[] filters);
} }

View file

@ -21,8 +21,7 @@ public partial class Client
return response.Body.Container.ToModel(); return response.Body.Container.ToModel();
} }
//TODO: Return iterator public async IAsyncEnumerable<ContainerId> ListContainersAsync()
public async Task<ContainerId[]> ListContainersAsync()
{ {
var request = new ListRequest var request = new ListRequest
{ {
@ -34,9 +33,10 @@ public partial class Client
request.AddMetaHeader(); request.AddMetaHeader();
request.Sign(_key); request.Sign(_key);
var response = await _containerServiceClient.ListAsync(request); var response = await _containerServiceClient.ListAsync(request);
return response.Body.ContainerIds.Select( foreach (var cid in response.Body.ContainerIds)
cid => ContainerId.FromHash(cid.Value.ToByteArray()) {
).ToArray(); yield return ContainerId.FromHash(cid.Value.ToByteArray());
}
} }
public async Task<ContainerId> CreateContainerAsync(ModelsV2.Container container) public async Task<ContainerId> CreateContainerAsync(ModelsV2.Container container)
@ -55,6 +55,7 @@ public partial class Client
request.AddMetaHeader(); request.AddMetaHeader();
request.Sign(_key); request.Sign(_key);
var response = await _containerServiceClient.PutAsync(request); var response = await _containerServiceClient.PutAsync(request);
RequestVerifier.ProcessResponse(response);
return ContainerId.FromHash(response.Body.ContainerId.Value.ToByteArray()); return ContainerId.FromHash(response.Body.ContainerId.Value.ToByteArray());
} }

View file

@ -179,8 +179,7 @@ public partial class Client
await _objectServiceClient.DeleteAsync(request); await _objectServiceClient.DeleteAsync(request);
} }
//TODO: Return iterator public async IAsyncEnumerable<ObjectId> SearchObjectsAsync(ContainerId cid, params ObjectFilter[] filters)
public async Task<ObjectId[]> SearchObjectAsync(ContainerId cid, params ObjectFilter[] filters)
{ {
var request = new SearchRequest var request = new SearchRequest
{ {
@ -199,25 +198,28 @@ public partial class Client
; ;
request.AddMetaHeader(); request.AddMetaHeader();
request.Sign(_key); request.Sign(_key);
var ids = await SearchObject(request); var objectsIds = SearchObjects(request);
return ids.Select(oid => ObjectId.FromHash(oid.Value.ToByteArray())).ToArray(); await foreach (var oid in objectsIds)
{
yield return ObjectId.FromHash(oid.Value.ToByteArray());
}
} }
private async Task<List<ObjectID>> SearchObject(SearchRequest request) private async IAsyncEnumerable<ObjectID> SearchObjects(SearchRequest request)
{ {
var objectsIds = new List<ObjectID> { }; using var stream = SearchObjectsInit(request);
using var stream = SearchObjectInit(request);
var ids = await stream.Read(); var ids = await stream.Read();
while (ids is not null) while (ids is not null)
{ {
objectsIds.AddRange(ids); foreach (var oid in ids)
{
yield return oid;
}
ids = await stream.Read(); ids = await stream.Read();
} }
return objectsIds;
} }
private SearchReader SearchObjectInit(SearchRequest initRequest) private SearchReader SearchObjectsInit(SearchRequest initRequest)
{ {
if (initRequest is null) if (initRequest is null)
{ {