Sandbox/Program.cs
Pavel Gross 126199d488 [#27] Client: demo project
Signed-off-by: Pavel Gross <p.gross@yadro.com>
2024-12-16 13:42:50 +03:00

325 lines
12 KiB
C#

using System.Collections.ObjectModel;
using FrostFS.SDK;
using FrostFS.SDK.Client;
using Grpc.Core;
using Grpc.Net.Client;
using Microsoft.Extensions.Options;
class Demo
{
private const string Key = "KzPXA6669m2pf18XmUdoR8MnP1pi1PMmefiFujStVFnv7WR5SRmK";
private const string Host = "http://172.23.32.4:8080";
static void Main(string[] args)
{
}
#region Network
public static async Task<FrostFsNetmapSnapshot> GetNetmapSnapshotAsync()
{
var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host });
Func<string, ChannelBase> grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url));
var client = FrostFSClient.GetInstance(clientOptions, grpcChannel);
return await client.GetNetmapSnapshotAsync(default);
}
public static async Task<FrostFsNodeInfo> GetNodeInfoAsync()
{
var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host });
Func<string, ChannelBase> grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url));
var client = FrostFSClient.GetInstance(clientOptions, grpcChannel);
return await client.GetNodeInfoAsync(default);
}
public static async Task<NetworkSettings> GetNetworkSettingsAsync()
{
var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host });
Func<string, ChannelBase> grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url));
var client = FrostFSClient.GetInstance(clientOptions, grpcChannel);
return await client.GetNetworkSettingsAsync(default);
}
#endregion
#region Session
public static async Task<FrostFsSessionToken> GetSessionTokenAsync()
{
var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host });
Func<string, ChannelBase> grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url));
var client = FrostFSClient.GetInstance(clientOptions, grpcChannel);
return await client.CreateSessionAsync(new PrmSessionCreate(int.MaxValue), default);
}
#endregion
#region Chain
public static async Task<ReadOnlyMemory<byte>> AddChainAsync(string targetString, string name, byte[] raw)
{
var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host });
Func<string, ChannelBase> grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url));
var client = FrostFSClient.GetInstance(clientOptions, grpcChannel);
var target = new FrostFsChainTarget(Enum.Parse<FrostFsTargetType>(targetString), name);
var chain = new FrostFsChain(raw);
var args = new PrmApeChainAdd(target, chain);
return await client.AddChainAsync(args, default);
}
public static async Task RemoveChainAsync(string targetString, string name, byte[] raw)
{
var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host });
Func<string, ChannelBase> grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url));
var client = FrostFSClient.GetInstance(clientOptions, grpcChannel);
var target = new FrostFsChainTarget(Enum.Parse<FrostFsTargetType>(targetString), name);
var chain = new FrostFsChain(raw);
var args = new PrmApeChainRemove(target, chain);
await client.RemoveChainAsync(args, default);
}
public static async Task RemoveChainAsync(string targetString, string name)
{
var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host });
Func<string, ChannelBase> grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url));
var client = FrostFSClient.GetInstance(clientOptions, grpcChannel);
var target = new FrostFsChainTarget(Enum.Parse<FrostFsTargetType>(targetString), name);
var args = new PrmApeChainList(target);
await client.ListChainAsync(args, default);
}
#endregion
#region Container
public static async Task<FrostFsContainerId> CreateContainerAsync()
{
var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host });
Func<string, ChannelBase> grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url));
var client = FrostFSClient.GetInstance(clientOptions, grpcChannel);
var createContainerParam = new PrmContainerCreate(
new FrostFsContainerInfo(new FrostFsPlacementPolicy(true, new FrostFsReplica(1))),
PrmWait.DefaultParams);
return await client.CreateContainerAsync(createContainerParam, default);
}
public static async Task<FrostFsContainerId> GetContainerAsync()
{
var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host });
Func<string, ChannelBase> grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url));
var client = FrostFSClient.GetInstance(clientOptions, grpcChannel);
var createContainerParam = new PrmContainerCreate(
new FrostFsContainerInfo(new FrostFsPlacementPolicy(true, new FrostFsReplica(1))),
PrmWait.DefaultParams);
return await client.CreateContainerAsync(createContainerParam, default);
}
public static async Task DeleteContainerAsync(FrostFsContainerId containerId)
{
var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host });
Func<string, ChannelBase> grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url));
var client = FrostFSClient.GetInstance(clientOptions, grpcChannel);
var args = new PrmContainerDelete(containerId, new PrmWait(60, 5));
await client.DeleteContainerAsync(args, default);
}
public static async Task<ICollection<FrostFsContainerId>> ListContainersAsync(FrostFsContainerId containerId)
{
var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host });
Func<string, ChannelBase> grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url));
var client = FrostFSClient.GetInstance(clientOptions, grpcChannel);
var args = new PrmContainerGetAll();
Collection<FrostFsContainerId> containers = [];
await foreach (var cid in client.ListContainersAsync(args, default))
{
containers.Add(cid);
}
return containers;
}
#endregion
#region Object
public static async Task<FrostFsHeaderResult> GetObjectHeadAsync(FrostFsContainerId containerId, FrostFsObjectId objectId)
{
var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host });
Func<string, ChannelBase> grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url));
var client = FrostFSClient.GetInstance(clientOptions, grpcChannel);
var args = new PrmObjectHeadGet(containerId, objectId);
return await client.GetObjectHeadAsync(args, default);
}
public static async Task<FrostFsObject> GetObjectAsync(FrostFsContainerId containerId, FrostFsObjectId objectId)
{
var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host });
Func<string, ChannelBase> grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url));
var client = FrostFSClient.GetInstance(clientOptions, grpcChannel);
var args = new PrmObjectGet(containerId, objectId);
return await client.GetObjectAsync(args, default);
}
public static async Task<byte[]> GetRangeAsync(FrostFsContainerId containerId, FrostFsObjectId objectId)
{
var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host });
Func<string, ChannelBase> grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url));
var client = FrostFSClient.GetInstance(clientOptions, grpcChannel);
var args = new PrmRangeGet(containerId, objectId, new FrostFsRange(64, 128));
var rangeReader = await client.GetRangeAsync(args, default);
var downloadedBytes = new byte[128];
MemoryStream ms = new(downloadedBytes);
ReadOnlyMemory<byte>? chunk;
while ((chunk = await rangeReader!.ReadChunk()) != null)
{
ms.Write(chunk.Value.Span);
}
return downloadedBytes;
}
public static async Task<ReadOnlyMemory<byte>[]> GetRangeHashAsync(FrostFsContainerId containerId, FrostFsObjectId objectId)
{
var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host });
Func<string, ChannelBase> grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url));
var client = FrostFSClient.GetInstance(clientOptions, grpcChannel);
Random rnd = new();
var salt = new byte[32];
rnd.NextBytes(salt);
var args = new PrmRangeHashGet(containerId, objectId, [new FrostFsRange(100, 64)], salt);
return await client.GetRangeHashAsync(args, default);
}
public static async Task<FrostFsObjectId> PutObjectAsync(FrostFsContainerId containerId, byte[] bytes)
{
var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host });
Func<string, ChannelBase> grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url));
var client = FrostFSClient.GetInstance(clientOptions, grpcChannel);
var args = new PrmObjectPut(
new FrostFsObjectHeader(
containerId: containerId,
type: FrostFsObjectType.Regular,
[new FrostFsAttributePair("fileName", "test")]));
var stream = await client.PutObjectAsync(args, default);
await stream.WriteAsync(bytes.AsMemory());
var objectId = await stream.CompleteAsync();
return objectId;
}
public static async Task<FrostFsObjectId> PutSingleObjectAsync(FrostFsObject obj)
{
var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host });
Func<string, ChannelBase> grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url));
var client = FrostFSClient.GetInstance(clientOptions, grpcChannel);
var args = new PrmSingleObjectPut(obj);
return await client.PutSingleObjectAsync(args, default);
}
public static async Task<FrostFsObjectId> PatchObjectAsync(FrostFsContainerId containerId, FrostFsObjectId objectId, byte[] patch)
{
var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host });
Func<string, ChannelBase> grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url));
var client = FrostFSClient.GetInstance(clientOptions, grpcChannel);
var range = new FrostFsRange(64, (ulong)patch.Length);
var args = new PrmObjectPatch(
new FrostFsAddress(containerId, objectId),
payload: new MemoryStream(patch),
maxChunkLength: 256,
range: range);
return await client.PatchObjectAsync(args, default);
}
public static async Task DeleteObjectAsync(FrostFsContainerId containerId, FrostFsObjectId objectId, byte[] patch)
{
var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host });
Func<string, ChannelBase> grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url));
var client = FrostFSClient.GetInstance(clientOptions, grpcChannel);
var args = new PrmObjectDelete(containerId, objectId);
await client.DeleteObjectAsync(args, default);
}
public static async Task<ICollection<FrostFsObjectId>> SearchObjectsAsync(FrostFsContainerId containerId)
{
var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host });
Func<string, ChannelBase> grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url));
var client = FrostFSClient.GetInstance(clientOptions, grpcChannel);
var filter = new FilterByAttributePair(FrostFsMatchType.Equals, "fileName", "test");
var args = new PrmObjectSearch(containerId, null, [], filter);
Collection<FrostFsObjectId> objects = [];
await foreach (var objId in client.SearchObjectsAsync(args, default))
{
objects.Add(objId);
}
return objects;
}
#endregion
}