|
- package ops2
-
- import (
- "fmt"
- "io"
-
- "gitlink.org.cn/cloudream/common/pkgs/future"
- "gitlink.org.cn/cloudream/common/pkgs/ioswitch/dag"
- "gitlink.org.cn/cloudream/common/pkgs/ioswitch/exec"
- "gitlink.org.cn/cloudream/common/pkgs/logger"
- "gitlink.org.cn/cloudream/common/utils/io2"
- clitypes "gitlink.org.cn/cloudream/jcs-pub/client/types"
- "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/ioswitch2"
- "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/storage/pool"
- )
-
- func init() {
- exec.UseOp[*BaseWrite]()
- exec.UseOp[*BaseRead]()
- exec.UseVarValue[*FileInfoValue]()
- }
-
- type FileInfoValue struct {
- Hash clitypes.FileHash `json:"hash"`
- Size int64 `json:"size"`
- }
-
- func (v *FileInfoValue) Clone() exec.VarValue {
- return &FileInfoValue{Hash: v.Hash, Size: v.Size}
- }
-
- type BaseRead struct {
- Output exec.VarID
- UserSpace clitypes.UserSpaceDetail
- Path string
- }
-
- func (o *BaseRead) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
- logger.
- WithField("Output", o.Output).
- WithField("UserSpace", o.UserSpace).
- WithField("Path", o.Path).
- Debug("base read")
- defer logger.Debug("base read end")
-
- stgPool, err := exec.GetValueByType[*pool.Pool](ctx)
- if err != nil {
- return fmt.Errorf("getting storage pool: %w", err)
- }
-
- store, err := stgPool.GetBaseStore(&o.UserSpace)
- if err != nil {
- return fmt.Errorf("getting base store of storage %v: %w", o.UserSpace, err)
- }
-
- stream, err := store.Read(o.Path)
- if err != nil {
- return fmt.Errorf("reading object %v: %w", o.Path, err)
- }
-
- fut := future.NewSetVoid()
- output := &exec.StreamValue{
- Stream: io2.AfterReadClosed(stream, func(closer io.ReadCloser) {
- fut.SetVoid()
- }),
- }
-
- e.PutVar(o.Output, output)
- return fut.Wait(ctx.Context)
- }
-
- func (o *BaseRead) String() string {
- return fmt.Sprintf("PublicRead %v:%v -> %v", o.UserSpace, o.Path, o.Output)
- }
-
- type BaseWrite struct {
- Input exec.VarID
- UserSpace clitypes.UserSpaceDetail
- Path string
- FileInfo exec.VarID
- }
-
- func (o *BaseWrite) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
- logger.
- WithField("Input", o.Input).
- Debugf("write file to base store")
- defer logger.Debugf("write file to base store finished")
-
- stgPool, err := exec.GetValueByType[*pool.Pool](ctx)
- if err != nil {
- return fmt.Errorf("getting storage pool: %w", err)
- }
-
- store, err := stgPool.GetBaseStore(&o.UserSpace)
- if err != nil {
- return fmt.Errorf("getting base store of storage %v: %w", o.UserSpace, err)
- }
-
- input, err := exec.BindVar[*exec.StreamValue](e, ctx.Context, o.Input)
- if err != nil {
- return err
- }
- defer input.Stream.Close()
-
- info, err := store.Write(o.Path, input.Stream)
- if err != nil {
- return err
- }
-
- e.PutVar(o.FileInfo, &FileInfoValue{
- Hash: info.Hash,
- Size: info.Size,
- })
- return nil
- }
-
- func (o *BaseWrite) String() string {
- return fmt.Sprintf("PublicWrite %v -> %v:%v", o.Input, o.UserSpace, o.Path)
- }
-
- type BaseReadNode struct {
- dag.NodeBase
- From ioswitch2.From
- UserSpace clitypes.UserSpaceDetail
- Path string
- }
-
- func (b *GraphNodeBuilder) NewPublicRead(from ioswitch2.From, userSpace clitypes.UserSpaceDetail, path string) *BaseReadNode {
- node := &BaseReadNode{
- From: from,
- UserSpace: userSpace,
- Path: path,
- }
- b.AddNode(node)
-
- node.OutputStreams().Init(node, 1)
- return node
- }
-
- func (t *BaseReadNode) GetFrom() ioswitch2.From {
- return t.From
- }
-
- func (t *BaseReadNode) Output() dag.StreamOutputSlot {
- return dag.StreamOutputSlot{
- Node: t,
- Index: 0,
- }
- }
-
- func (t *BaseReadNode) GenerateOp() (exec.Op, error) {
- return &BaseRead{
- Output: t.Output().Var().VarID,
- UserSpace: t.UserSpace,
- Path: t.Path,
- }, nil
- }
-
- type BaseWriteNode struct {
- dag.NodeBase
- To ioswitch2.To
- UserSpace clitypes.UserSpaceDetail
- Path string
- FileInfoStoreKey string
- }
-
- func (b *GraphNodeBuilder) NewPublicWrite(to ioswitch2.To, userSpace clitypes.UserSpaceDetail, path string) *BaseWriteNode {
- node := &BaseWriteNode{
- To: to,
- UserSpace: userSpace,
- Path: path,
- }
- b.AddNode(node)
-
- node.InputStreams().Init(1)
- return node
- }
-
- func (t *BaseWriteNode) GetTo() ioswitch2.To {
- return t.To
- }
-
- func (t *BaseWriteNode) SetInput(input *dag.StreamVar) {
- input.To(t, 0)
- }
-
- func (t *BaseWriteNode) Input() dag.StreamInputSlot {
- return dag.StreamInputSlot{
- Node: t,
- Index: 0,
- }
- }
-
- func (t *BaseWriteNode) FileInfoVar() *dag.ValueVar {
- return t.OutputValues().Get(0)
- }
-
- func (t *BaseWriteNode) GenerateOp() (exec.Op, error) {
- return &BaseWrite{
- Input: t.InputStreams().Get(0).VarID,
- UserSpace: t.UserSpace,
- Path: t.Path,
- FileInfo: t.FileInfoVar().VarID,
- }, nil
- }
|