Sandbox/Program.cs

326 lines
12 KiB
C#
Raw Normal View History

using System.Collections.ObjectModel;
2024-06-17 15:06:25 +00:00
using FrostFS.SDK;
using FrostFS.SDK.Client;
using Grpc.Core;
using Grpc.Net.Client;
using Microsoft.Extensions.Options;
class Demo
2024-05-30 12:10:49 +00:00
{
private const string Key = "KzPXA6669m2pf18XmUdoR8MnP1pi1PMmefiFujStVFnv7WR5SRmK";
private const string Host = "http://172.23.32.4:8080";
static void Main(string[] args)
{
}
2024-06-17 15:06:25 +00:00
#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));
2024-06-17 15:06:25 +00:00
var client = FrostFSClient.GetInstance(clientOptions, grpcChannel);
2024-06-17 15:06:25 +00:00
return await client.GetNetmapSnapshotAsync(default);
}
public static async Task<FrostFsNodeInfo> GetNodeInfoAsync()
2024-06-17 15:06:25 +00:00
{
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);
}
2024-05-30 12:10:49 +00:00
public static async Task<NetworkSettings> GetNetworkSettingsAsync()
2024-06-17 15:06:25 +00:00
{
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);
2024-06-17 15:06:25 +00:00
}
#endregion
2024-06-17 15:06:25 +00:00
#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
2024-06-17 15:06:25 +00:00
#region Chain
public static async Task<ReadOnlyMemory<byte>> AddChainAsync(string targetString, string name, byte[] raw)
2024-06-17 15:06:25 +00:00
{
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);
2024-06-17 15:06:25 +00:00
}
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);
2024-05-30 12:10:49 +00:00
var args = new PrmApeChainRemove(target, chain);
await client.RemoveChainAsync(args, default);
}
public static async Task RemoveChainAsync(string targetString, string name)
2024-05-30 12:10:49 +00:00
{
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);
2024-05-30 12:10:49 +00:00
}
#endregion
2024-05-30 12:10:49 +00:00
#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);
2024-05-30 12:10:49 +00:00
var createContainerParam = new PrmContainerCreate(
new FrostFsContainerInfo(new FrostFsPlacementPolicy(true, new FrostFsReplica(1))),
PrmWait.DefaultParams);
2024-05-30 12:10:49 +00:00
return await client.CreateContainerAsync(createContainerParam, default);
}
public static async Task<FrostFsContainerId> GetContainerAsync()
2024-05-30 12:10:49 +00:00
{
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))
2024-05-30 12:10:49 +00:00
{
containers.Add(cid);
2024-05-30 12:10:49 +00:00
}
return containers;
2024-05-30 12:10:49 +00:00
}
#endregion
2024-05-30 12:10:49 +00:00
#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));
2024-05-30 12:10:49 +00:00
var client = FrostFSClient.GetInstance(clientOptions, grpcChannel);
2024-06-17 15:06:25 +00:00
var args = new PrmObjectHeadGet(containerId, objectId);
2024-05-30 12:10:49 +00:00
return await client.GetObjectHeadAsync(args, default);
}
2024-06-17 15:06:25 +00:00
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));
2024-06-17 15:06:25 +00:00
var client = FrostFSClient.GetInstance(clientOptions, grpcChannel);
2024-06-17 15:06:25 +00:00
var args = new PrmObjectGet(containerId, objectId);
2024-06-17 15:06:25 +00:00
return await client.GetObjectAsync(args, default);
}
2024-06-17 15:06:25 +00:00
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));
2024-05-30 12:10:49 +00:00
var client = FrostFSClient.GetInstance(clientOptions, grpcChannel);
2024-05-30 12:10:49 +00:00
var args = new PrmRangeGet(containerId, objectId, new FrostFsRange(64, 128));
2024-06-17 15:06:25 +00:00
var rangeReader = await client.GetRangeAsync(args, default);
2024-06-17 15:06:25 +00:00
var downloadedBytes = new byte[128];
MemoryStream ms = new(downloadedBytes);
2024-06-17 15:06:25 +00:00
ReadOnlyMemory<byte>? chunk;
while ((chunk = await rangeReader!.ReadChunk()) != null)
{
ms.Write(chunk.Value.Span);
}
return downloadedBytes;
2024-05-30 12:10:49 +00:00
}
public static async Task<ReadOnlyMemory<byte>[]> GetRangeHashAsync(FrostFsContainerId containerId, FrostFsObjectId objectId)
2024-06-17 15:06:25 +00:00
{
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);
2024-06-17 15:06:25 +00:00
}
2024-05-30 12:10:49 +00:00
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);
2024-05-30 12:10:49 +00:00
var args = new PrmObjectPut(
new FrostFsObjectHeader(
containerId: containerId,
type: FrostFsObjectType.Regular,
[new FrostFsAttributePair("fileName", "test")]));
var stream = await client.PutObjectAsync(args, default);
2024-06-17 15:06:25 +00:00
await stream.WriteAsync(bytes.AsMemory());
var objectId = await stream.CompleteAsync();
2024-06-17 15:06:25 +00:00
return objectId;
}
public static async Task<FrostFsObjectId> PutSingleObjectAsync(FrostFsObject obj)
2024-05-30 12:10:49 +00:00
{
var clientOptions = Options.Create(new ClientSettings { Key = Key, Host = Host });
Func<string, ChannelBase> grpcChannel = (url) => GrpcChannel.ForAddress(new Uri(url));
2024-06-17 15:06:25 +00:00
var client = FrostFSClient.GetInstance(clientOptions, grpcChannel);
2024-06-17 15:06:25 +00:00
var args = new PrmSingleObjectPut(obj);
2024-06-17 15:06:25 +00:00
return await client.PutSingleObjectAsync(args, default);
2024-05-30 12:10:49 +00:00
}
public static async Task<FrostFsObjectId> PatchObjectAsync(FrostFsContainerId containerId, FrostFsObjectId objectId, byte[] patch)
2024-05-30 12:10:49 +00:00
{
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);
2024-06-17 15:06:25 +00:00
var range = new FrostFsRange(64, (ulong)patch.Length);
2024-06-17 15:06:25 +00:00
var args = new PrmObjectPatch(
new FrostFsAddress(containerId, objectId),
payload: new MemoryStream(patch),
maxChunkLength: 256,
range: range);
2024-06-17 15:06:25 +00:00
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));
2024-06-17 15:06:25 +00:00
var client = FrostFSClient.GetInstance(clientOptions, grpcChannel);
2024-06-17 15:06:25 +00:00
var args = new PrmObjectDelete(containerId, objectId);
await client.DeleteObjectAsync(args, default);
2024-05-30 12:10:49 +00:00
}
2024-06-17 15:06:25 +00:00
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));
2024-06-17 15:06:25 +00:00
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
2024-05-30 12:10:49 +00:00
}