[#28] Client: Apply code optimizations

Signed-off-by: Pavel Gross <p.gross@yadro.com>
This commit is contained in:
Pavel Gross 2024-11-18 16:57:20 +03:00
parent 766f61a5f7
commit 749000a090
57 changed files with 845 additions and 1116 deletions

View file

@ -3,6 +3,8 @@ using System.Collections.ObjectModel;
using System.Globalization;
using System.Text;
using Grpc.Core.Interceptors;
namespace FrostFS.SDK;
public class ClientSettings
@ -11,60 +13,25 @@ public class ClientSettings
public string Host { get; set; } = string.Empty;
public virtual void Validate()
{
var errors = CheckFields();
if (errors != null)
ThrowSettingsException(errors);
}
protected Collection<string>? CheckFields()
{
if (string.IsNullOrWhiteSpace(Host))
{
var error = string.Format(CultureInfo.InvariantCulture, errorTemplate, nameof(Host));
return new Collection<string>([error]);
}
return null;
}
protected static void ThrowSettingsException(Collection<string> errors)
{
if (errors is null)
{
throw new ArgumentNullException(nameof(errors));
}
StringBuilder messages = new();
foreach (var error in errors)
{
messages.AppendLine(error);
}
throw new ArgumentException(messages.ToString());
}
}
public class SingleOwnerClientSettings : ClientSettings
{
public string Key { get; set; } = string.Empty;
public override void Validate()
{
var errors = CheckFields();
if (errors != null)
ThrowSettingsException(errors);
}
public Action<CallStatistics>? Callback { get; set; }
protected new Collection<string>? CheckFields()
public Collection<Interceptor> Interceptors { get; } = [];
public void Validate()
{
Collection<string>? errors = base.CheckFields();
StringBuilder? errors = null;
if (string.IsNullOrWhiteSpace(Host))
(errors = new StringBuilder(128)).AppendLine(string.Format(CultureInfo.InvariantCulture, errorTemplate, nameof(Host)));
if (string.IsNullOrWhiteSpace(Key))
(errors ??= []).Add(string.Format(CultureInfo.InvariantCulture, errorTemplate, nameof(Key)));
(errors ??= new StringBuilder(128)).AppendLine(string.Format(CultureInfo.InvariantCulture, errorTemplate, nameof(Key)));
return errors;
if (errors != null)
{
throw new ArgumentException(errors.ToString());
}
}
}

View file

@ -27,7 +27,7 @@ public class FrostFsContainerId
if (containerID != null)
{
this.modelId = Base58.Encode(containerID.Value.ToByteArray());
this.modelId = Base58.Encode(containerID.Value.Span);
return this.modelId;
}

View file

@ -96,7 +96,7 @@ public class FrostFsContainerInfo
PlacementPolicy = PlacementPolicy.Value.GetPolicy(),
Nonce = ByteString.CopyFrom(Nonce.ToBytes()),
OwnerId = Owner?.OwnerID,
Version = Version?.Version
Version = Version?.VersionID
};
var attribs = GetGrpsAttributes();

View file

@ -10,7 +10,7 @@ public class FrostFsVersion(int major, int minor)
public int Major { get; set; } = major;
public int Minor { get; set; } = minor;
internal Version Version
internal Version VersionID
{
get
{

View file

@ -8,13 +8,8 @@ public class FrostFsObjectId(string id)
{
public string Value { get; } = id;
public static FrostFsObjectId FromHash(byte[] hash)
public static FrostFsObjectId FromHash(ReadOnlySpan<byte> hash)
{
if (hash is null)
{
throw new ArgumentNullException(nameof(hash));
}
if (hash.Length != Constants.Sha256HashLength)
throw new FormatException("ObjectID must be a sha256 hash.");

View file

@ -4,6 +4,7 @@ using FrostFS.Refs;
using FrostFS.SDK.Client.Mappers.GRPC;
using FrostFS.SDK.Cryptography;
namespace FrostFS.SDK;
public class FrostFsOwner(string id)

View file

@ -1,9 +1,117 @@
using System;
using FrostFS.Refs;
using FrostFS.SDK.Client;
using FrostFS.SDK.Cryptography;
using FrostFS.Session;
using Google.Protobuf;
namespace FrostFS.SDK;
public class FrostFsSessionToken(byte[] token, Guid id)
public class FrostFsSessionToken
{
public Guid Id { get; private set; } = id;
public byte[] Token { get; private set; } = token;
private Guid _id;
private ReadOnlyMemory<byte> _sessionKey;
private readonly SessionToken.Types.Body _body;
private FrostFsSessionToken()
{
ProtoId = ByteString.Empty;
ProtoSessionKey = ByteString.Empty;
_body = new SessionToken.Types.Body();
}
internal FrostFsSessionToken(SessionToken token)
{
ProtoId = token.Body.Id;
ProtoSessionKey = token.Body.SessionKey;
_body = token.Body;
}
public Guid Id
{
get
{
if (_id == Guid.Empty)
_id = ProtoId.ToUuid();
return _id;
}
}
public ReadOnlyMemory<byte> SessionKey
{
get
{
if (_sessionKey.IsEmpty)
_sessionKey = ProtoSessionKey.Memory;
return _sessionKey;
}
}
internal ByteString ProtoId { get; }
internal ByteString ProtoSessionKey { get; }
public SessionToken CreateContainerToken(ContainerID? containerId, ContainerSessionContext.Types.Verb verb, ClientKey key)
{
if (key is null)
{
throw new ArgumentNullException(nameof(key));
}
SessionToken sessionToken = new() { Body = _body.Clone() };
sessionToken.Body.Container = new() { Verb = verb };
if (containerId != null)
sessionToken.Body.Container.ContainerId = containerId;
else
sessionToken.Body.Container.Wildcard = true;
sessionToken.Body.SessionKey = key.PublicKeyProto;
sessionToken.Signature = key.ECDsaKey.SignMessagePart(sessionToken.Body);
return sessionToken;
}
public SessionToken CreateObjectTokenContext(Address address, ObjectSessionContext.Types.Verb verb, ClientKey key)
{
if (address is null)
{
throw new ArgumentNullException(nameof(address));
}
if (key is null)
{
throw new ArgumentNullException(nameof(key));
}
SessionToken sessionToken = new()
{
Body = _body.Clone()
};
ObjectSessionContext.Types.Target target = new() { Container = address.ContainerId };
if (address.ObjectId != null)
target.Objects.Add(address.ObjectId);
sessionToken.Body.Object = new()
{
Target = target,
Verb = verb
};
sessionToken.Body.SessionKey = key.PublicKeyProto;
sessionToken.Signature = key.ECDsaKey.SignMessagePart(sessionToken.Body);
return sessionToken;
}
}