|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- 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
- }
|