2024-02-28 11:51:48 +00:00
|
|
|
package erasurecode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
|
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2024-05-17 12:16:28 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2024-02-28 11:51:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Split splits fully formed object into multiple chunks.
|
|
|
|
func (c *Constructor) Split(obj *objectSDK.Object, key *ecdsa.PrivateKey) ([]*objectSDK.Object, error) {
|
|
|
|
c.clear()
|
|
|
|
|
|
|
|
header, err := obj.CutPayload().Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
headerShards, err := c.encodeRaw(header)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
payloadShards, err := c.encodeRaw(obj.Payload())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := make([]*objectSDK.Object, len(payloadShards))
|
|
|
|
parent, _ := obj.ID()
|
|
|
|
for i := range parts {
|
|
|
|
chunk := objectSDK.New()
|
|
|
|
copyRequiredFields(chunk, obj)
|
|
|
|
chunk.SetPayload(payloadShards[i])
|
|
|
|
chunk.SetPayloadSize(uint64(len(payloadShards[i])))
|
|
|
|
|
2024-05-17 12:16:28 +00:00
|
|
|
var parentSplitParentID *oid.ID
|
|
|
|
if obj.HasParent() {
|
|
|
|
splitParentID, ok := obj.Parent().ID()
|
|
|
|
if ok {
|
|
|
|
parentSplitParentID = &splitParentID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ec := objectSDK.NewECHeader(parent, obj.SplitID(), parentSplitParentID, uint32(i), uint32(len(payloadShards)), headerShards[i], uint32(len(header)))
|
2024-02-28 11:51:48 +00:00
|
|
|
chunk.SetECHeader(ec)
|
|
|
|
if err := setIDWithSignature(chunk, key); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
parts[i] = chunk
|
|
|
|
}
|
|
|
|
return parts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setIDWithSignature(obj *objectSDK.Object, key *ecdsa.PrivateKey) error {
|
2024-03-28 15:53:34 +00:00
|
|
|
objectSDK.CalculateAndSetPayloadChecksum(obj)
|
|
|
|
|
2024-02-28 11:51:48 +00:00
|
|
|
if err := objectSDK.CalculateAndSetID(obj); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if key == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return objectSDK.CalculateAndSetSignature(*key, obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Constructor) encodeRaw(data []byte) ([][]byte, error) {
|
|
|
|
shards, err := c.enc.Split(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := c.enc.Encode(shards); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return shards, nil
|
|
|
|
}
|