您最多选择25个标签 标签必须以中文、字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package ops2
  2. import (
  3. "fmt"
  4. "io"
  5. "github.com/samber/lo"
  6. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/dag"
  7. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/exec"
  8. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/utils"
  9. "gitlink.org.cn/cloudream/common/utils/io2"
  10. "golang.org/x/sync/semaphore"
  11. )
  12. func init() {
  13. exec.UseOp[*CloneStream]()
  14. exec.UseOp[*CloneVar]()
  15. }
  16. type CloneStream struct {
  17. Raw exec.VarID `json:"raw"`
  18. Cloneds []exec.VarID `json:"cloneds"`
  19. }
  20. func (o *CloneStream) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
  21. raw, err := exec.BindVar[*exec.StreamValue](e, ctx.Context, o.Raw)
  22. if err != nil {
  23. return err
  24. }
  25. defer raw.Stream.Close()
  26. cloned := io2.Clone(raw.Stream, len(o.Cloneds))
  27. sem := semaphore.NewWeighted(int64(len(o.Cloneds)))
  28. for i, s := range cloned {
  29. err = sem.Acquire(ctx.Context, 1)
  30. if err != nil {
  31. return err
  32. }
  33. e.PutVar(o.Cloneds[i], &exec.StreamValue{
  34. Stream: io2.AfterReadClosedOnce(s, func(closer io.ReadCloser) {
  35. sem.Release(1)
  36. }),
  37. })
  38. }
  39. return sem.Acquire(ctx.Context, int64(len(o.Cloneds)))
  40. }
  41. func (o *CloneStream) String() string {
  42. return fmt.Sprintf("CloneStream %v -> (%v)", o.Raw, utils.FormatVarIDs(o.Cloneds))
  43. }
  44. type CloneVar struct {
  45. Raw exec.VarID `json:"raw"`
  46. Cloneds []exec.VarID `json:"cloneds"`
  47. }
  48. func (o *CloneVar) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
  49. raw, err := e.BindVar(ctx.Context, o.Raw)
  50. if err != nil {
  51. return err
  52. }
  53. for i := range o.Cloneds {
  54. e.PutVar(o.Cloneds[i], raw.Clone())
  55. }
  56. return nil
  57. }
  58. func (o *CloneVar) String() string {
  59. return fmt.Sprintf("CloneStream %v -> (%v)", o.Raw, utils.FormatVarIDs(o.Cloneds))
  60. }
  61. type CloneStreamType struct {
  62. dag.NodeBase
  63. }
  64. func (b *GraphNodeBuilder) NewCloneStream() *CloneStreamType {
  65. node := &CloneStreamType{}
  66. b.AddNode(node)
  67. return node
  68. }
  69. func (t *CloneStreamType) SetInput(raw *dag.Var) {
  70. t.InputStreams().EnsureSize(1)
  71. raw.StreamTo(t, 0)
  72. }
  73. func (t *CloneStreamType) NewOutput() *dag.Var {
  74. output := t.Graph().NewVar()
  75. t.OutputStreams().SetupNew(t, output)
  76. return output
  77. }
  78. func (t *CloneStreamType) GenerateOp() (exec.Op, error) {
  79. return &CloneStream{
  80. Raw: t.InputStreams().Get(0).VarID,
  81. Cloneds: lo.Map(t.OutputStreams().RawArray(), func(v *dag.Var, idx int) exec.VarID {
  82. return v.VarID
  83. }),
  84. }, nil
  85. }
  86. // func (t *CloneStreamType) String() string {
  87. // return fmt.Sprintf("CloneStream[]%v%v", formatStreamIO(node), formatValueIO(node))
  88. // }
  89. type CloneVarType struct {
  90. dag.NodeBase
  91. }
  92. func (b *GraphNodeBuilder) NewCloneValue() *CloneVarType {
  93. node := &CloneVarType{}
  94. b.AddNode(node)
  95. return node
  96. }
  97. func (t *CloneVarType) SetInput(raw *dag.Var) {
  98. t.InputValues().EnsureSize(1)
  99. raw.ValueTo(t, 0)
  100. }
  101. func (t *CloneVarType) NewOutput() *dag.Var {
  102. output := t.Graph().NewVar()
  103. t.OutputValues().SetupNew(t, output)
  104. return output
  105. }
  106. func (t *CloneVarType) GenerateOp() (exec.Op, error) {
  107. return &CloneVar{
  108. Raw: t.InputValues().Get(0).VarID,
  109. Cloneds: lo.Map(t.OutputValues().RawArray(), func(v *dag.Var, idx int) exec.VarID {
  110. return v.VarID
  111. }),
  112. }, nil
  113. }
  114. // func (t *CloneVarType) String() string {
  115. // return fmt.Sprintf("CloneVar[]%v%v", formatStreamIO(node), formatValueIO(node))
  116. // }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。