[#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) 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 var request = new PutSingleRequest
{ {

View file

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