[#28] Client: add method to calculate ObjectId #38

Merged
PavelGrossSpb merged 3 commits from PavelGrossSpb/frostfs-sdk-csharp:misc/CalculateObjectId into master 2024-12-12 12:28:46 +00:00
2 changed files with 23 additions and 9 deletions
Showing only changes of commit 7fd60805e7 - Show all commits

View file

@ -268,7 +268,7 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl
internal async Task<FrostFsObjectId> PutSingleObjectAsync(PrmSingleObjectPut args, CallContext ctx)
{
var grpcObject = ObjectTools.CreateObject(args.FrostFsObject, ClientContext);
var grpcObject = ObjectTools.CreateSingleObject(args.FrostFsObject, ClientContext);
var request = new PutSingleRequest
{

View file

@ -14,6 +14,7 @@ public static class ObjectTools
{
public static FrostFsObjectId CalculateObjectId(
FrostFsObjectHeader header,
ReadOnlyMemory<byte> payloadHash,
FrostFsOwner owner,
FrostFsVersion version,
ClientKey key)
@ -38,7 +39,7 @@ public static class ObjectTools
throw new ArgumentNullException(nameof(key));
}
var grpcHeader = CreateHeader(header, [], owner, version);
var grpcHeader = CreateHeader(header, payloadHash, owner, version);
if (header.Split != null)
SetSplitValues(grpcHeader, header.Split, owner, version, key);
@ -46,7 +47,7 @@ public static class ObjectTools
return new ObjectID { Value = grpcHeader.Sha256() }.ToModel();
}
internal static Object.Object CreateObject(FrostFsObject @object, ClientContext ctx)
internal static Object.Object CreateSingleObject(FrostFsObject @object, ClientContext ctx)
{
@object.Header.OwnerId ??= ctx.Owner;
@object.Header.Version ??= ctx.Version;
@ -81,7 +82,8 @@ public static class ObjectTools
internal static void SetSplitValues(
Header grpcHeader,
FrostFsSplit split,
FrostFsOwner owner, FrostFsVersion version,
FrostFsOwner owner,
FrostFsVersion version,
ClientKey key)
{
if (split == null)
@ -100,7 +102,7 @@ public static class ObjectTools
if (split.ParentHeader is not null)
{
var grpcParentHeader = CreateHeader(split.ParentHeader, [], owner, version);
var grpcParentHeader = CreateHeader(split.ParentHeader, Array.Empty<byte>().Sha256(), owner, version);
grpcHeader.Split.Parent = new ObjectID { Value = grpcParentHeader.Sha256() };
grpcHeader.Split.ParentHeader = grpcParentHeader;
@ -114,15 +116,18 @@ public static class ObjectTools
grpcHeader.Split.Previous = split.Previous?.ToMessage();
}
internal static Header CreateHeader(FrostFsObjectHeader header, byte[]? payload, FrostFsOwner owner, FrostFsVersion version)
internal static Header CreateHeader(
FrostFsObjectHeader header,
ReadOnlyMemory<byte> payloadChecksum,
FrostFsOwner owner,
FrostFsVersion version)
{
header.OwnerId ??= owner;
header.Version ??= version;
var grpcHeader = header.GetHeader();
if (payload != null) // && payload.Length > 0
grpcHeader.PayloadHash = Sha256Checksum(payload);
grpcHeader.PayloadHash = ChecksumFromSha256(payloadChecksum);
return grpcHeader;
}
@ -135,4 +140,13 @@ public static class ObjectTools
Sum = ByteString.CopyFrom(data.Sha256())
};
}
internal static Checksum ChecksumFromSha256(ReadOnlyMemory<byte> dataHash)
{
return new Checksum
{
Type = ChecksumType.Sha256,
Sum = UnsafeByteOperations.UnsafeWrap(dataHash)
};
}
}