frostfs-sdk-csharp/src/FrostFS.SDK.ModelsV2/Object/FrostFsObject.cs
Pavel Gross fefa2da218
All checks were successful
DCO / DCO (pull_request) Successful in 42s
[#16] Unit tests
Signed-off-by: Pavel Gross <p.gross@yadro.com>
2024-07-09 17:56:44 +03:00

76 lines
No EOL
1.9 KiB
C#

using System;
using System.Security.Cryptography;
using FrostFS.SDK.ModelsV2.Enums;
namespace FrostFS.SDK.ModelsV2;
public class FrostFsObject
{
public FrostFsObject(ObjectId objectId, ObjectHeader header, byte[] payload)
{
ObjectId = objectId;
Payload = payload;
Header = header;
}
public FrostFsObject(ContainerId container, byte[] payload, ObjectType objectType = ObjectType.Regular)
{
Payload = payload;
Header = new ObjectHeader(containerId: container, type: objectType, attributes: []);
}
public ObjectHeader Header { get; set; }
public ObjectId? ObjectId
{
get; internal set;
}
public byte[] Payload { get; set; }
public IObjectReader? ObjectReader { get; set; }
public Signature? Signature { get; set; }
public void SetParent(LargeObject largeObject)
{
if (Header?.Split == null)
throw new Exception("The object is not initialized properly");
Header.Split.ParentHeader = largeObject.Header;
}
}
public class LargeObject(ContainerId container) : FrostFsObject(container, [])
{
private readonly SHA256 payloadHash = SHA256.Create();
public void AppendBlock(byte[] bytes, int count)
{
Header!.PayloadLength += (ulong)count;
this.payloadHash.TransformBlock(bytes, 0, count, bytes, 0);
}
public LargeObject CalculateHash()
{
this.payloadHash.TransformFinalBlock([], 0, 0);
Header!.PayloadCheckSum = this.payloadHash.Hash;
return this;
}
public ulong PayloadLength
{
get { return Header!.PayloadLength; }
}
}
public class LinkObject : FrostFsObject
{
public LinkObject(ContainerId containerId, SplitId splitId, LargeObject largeObject) : base (containerId, [])
{
Header!.Split = new Split(splitId)
{
ParentHeader = largeObject.Header
};
}
}