|
- package ops2
-
- import (
- "fmt"
-
- "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/ioswitch/dag"
- "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/ioswitch/exec"
- "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/storage/pool"
- jcstypes "gitlink.org.cn/cloudream/jcs-pub/common/types"
- )
-
- func init() {
- exec.UseOp[*GetShardInfo]()
- exec.UseOp[*StoreShard]()
- }
-
- type GetShardInfo struct {
- UserSpace jcstypes.UserSpaceDetail
- FileHash jcstypes.FileHash
- ShardInfo exec.VarID
- }
-
- func (o *GetShardInfo) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
- stgPool, err := exec.GetValueByType[*pool.Pool](ctx)
- if err != nil {
- return fmt.Errorf("getting shard store: %w", err)
- }
-
- store, err := stgPool.GetShardStore(&o.UserSpace)
- if err != nil {
- return fmt.Errorf("getting shard store: %w", err)
- }
-
- info, err := store.Info(o.FileHash)
- if err != nil {
- return fmt.Errorf("getting shard info: %w", err)
- }
- e.PutVar(o.ShardInfo, &FileInfoValue{
- FileInfo: info,
- })
- return nil
- }
-
- func (o *GetShardInfo) String() string {
- return fmt.Sprintf("GetShardInfo(%v)", o.FileHash)
- }
-
- type StoreShard struct {
- UserSpace jcstypes.UserSpaceDetail
- FileInfo exec.VarID
- ShardInfo exec.VarID
- }
-
- func (o *StoreShard) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
- stgPool, err := exec.GetValueByType[*pool.Pool](ctx)
- if err != nil {
- return fmt.Errorf("getting storage pool: %w", err)
- }
-
- store, err := stgPool.GetShardStore(&o.UserSpace)
- if err != nil {
- return fmt.Errorf("getting shard store: %w", err)
- }
-
- info, err := exec.BindVar[*FileInfoValue](e, ctx.Context, o.FileInfo)
- if err != nil {
- return err
- }
-
- stored, err := store.Store(info.Path, info.Hash, info.Size)
- if err != nil {
- return fmt.Errorf("adding shard: %w", err)
- }
-
- e.PutVar(o.ShardInfo, &FileInfoValue{
- FileInfo: stored,
- })
- return nil
- }
-
- func (o *StoreShard) String() string {
- return fmt.Sprintf("StoreShard: addInfo=%v, shardInfo=%v", o.FileInfo, o.ShardInfo)
- }
-
- type GetShardInfoNode struct {
- dag.NodeBase
- UserSpace jcstypes.UserSpaceDetail
- FileHash jcstypes.FileHash
- }
-
- func (b *GraphNodeBuilder) NewGetShardInfo(userSpace jcstypes.UserSpaceDetail, fileHash jcstypes.FileHash) *GetShardInfoNode {
- node := &GetShardInfoNode{
- UserSpace: userSpace,
- FileHash: fileHash,
- }
- b.AddNode(node)
-
- node.OutputValues().Init(node, 1)
- return node
- }
-
- func (n *GetShardInfoNode) FileInfoVar() dag.ValueOutputSlot {
- return dag.ValueOutputSlot{
- Node: n,
- Index: 0,
- }
- }
-
- func (n *GetShardInfoNode) GenerateOp() (exec.Op, error) {
- return &GetShardInfo{
- UserSpace: n.UserSpace,
- FileHash: n.FileHash,
- ShardInfo: n.FileInfoVar().Var().VarID,
- }, nil
- }
-
- type StoreShardNode struct {
- dag.NodeBase
- UserSpace jcstypes.UserSpaceDetail
- ShardInfoKey string
- }
-
- func (b *GraphNodeBuilder) NewStoreShard(userSpace jcstypes.UserSpaceDetail, shardInfoKey string) *StoreShardNode {
- node := &StoreShardNode{
- UserSpace: userSpace,
- ShardInfoKey: shardInfoKey,
- }
- b.AddNode(node)
-
- node.InputValues().Init(1)
- node.OutputValues().Init(node, 1)
- return node
- }
-
- func (t *StoreShardNode) FileInfoSlot() dag.ValueInputSlot {
- return dag.ValueInputSlot{
- Node: t,
- Index: 0,
- }
- }
-
- func (t *StoreShardNode) ShardInfoVar() dag.ValueOutputSlot {
- return dag.ValueOutputSlot{
- Node: t,
- Index: 0,
- }
- }
-
- func (t *StoreShardNode) GenerateOp() (exec.Op, error) {
- return &StoreShard{
- UserSpace: t.UserSpace,
- FileInfo: t.FileInfoSlot().Var().VarID,
- ShardInfo: t.ShardInfoVar().Var().VarID,
- }, nil
- }
|