From 7fd60805e78f60f26d0c970ffa4d2daf6dd538c5 Mon Sep 17 00:00:00 2001 From: Pavel Gross Date: Wed, 11 Dec 2024 12:57:56 +0300 Subject: [PATCH] [#28] Client: Move CalculateObjectId from IFrostFsClient to statis tools Get payload hash as an argument Signed-off-by: Pavel Gross --- .../Services/ObjectServiceProvider.cs | 2 +- src/FrostFS.SDK.Client/Tools/ObjectTools.cs | 30 ++++++++++++++----- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/FrostFS.SDK.Client/Services/ObjectServiceProvider.cs b/src/FrostFS.SDK.Client/Services/ObjectServiceProvider.cs index 133d2e3..246c3eb 100644 --- a/src/FrostFS.SDK.Client/Services/ObjectServiceProvider.cs +++ b/src/FrostFS.SDK.Client/Services/ObjectServiceProvider.cs @@ -268,7 +268,7 @@ internal sealed class ObjectServiceProvider(ObjectService.ObjectServiceClient cl internal async Task PutSingleObjectAsync(PrmSingleObjectPut args, CallContext ctx) { - var grpcObject = ObjectTools.CreateObject(args.FrostFsObject, ClientContext); + var grpcObject = ObjectTools.CreateSingleObject(args.FrostFsObject, ClientContext); var request = new PutSingleRequest { diff --git a/src/FrostFS.SDK.Client/Tools/ObjectTools.cs b/src/FrostFS.SDK.Client/Tools/ObjectTools.cs index 7abdbaf..36a22a1 100644 --- a/src/FrostFS.SDK.Client/Tools/ObjectTools.cs +++ b/src/FrostFS.SDK.Client/Tools/ObjectTools.cs @@ -14,6 +14,7 @@ public static class ObjectTools { public static FrostFsObjectId CalculateObjectId( FrostFsObjectHeader header, + ReadOnlyMemory 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().Sha256(), owner, version); grpcHeader.Split.Parent = new ObjectID { Value = grpcParentHeader.Sha256() }; grpcHeader.Split.ParentHeader = grpcParentHeader; @@ -114,16 +116,19 @@ 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 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 dataHash) + { + return new Checksum + { + Type = ChecksumType.Sha256, + Sum = UnsafeByteOperations.UnsafeWrap(dataHash) + }; + } } \ No newline at end of file