You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

clone.go 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.StreamVar `json:"raw"`
  18. Cloneds []*exec.StreamVar `json:"cloneds"`
  19. }
  20. func (o *CloneStream) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
  21. err := e.BindVars(ctx.Context, o.Raw)
  22. if err != nil {
  23. return err
  24. }
  25. defer o.Raw.Stream.Close()
  26. cloned := io2.Clone(o.Raw.Stream, len(o.Cloneds))
  27. sem := semaphore.NewWeighted(int64(len(o.Cloneds)))
  28. for i, s := range cloned {
  29. sem.Acquire(ctx.Context, 1)
  30. o.Cloneds[i].Stream = io2.AfterReadClosedOnce(s, func(closer io.ReadCloser) {
  31. sem.Release(1)
  32. })
  33. }
  34. exec.PutArrayVars(e, o.Cloneds)
  35. return sem.Acquire(ctx.Context, int64(len(o.Cloneds)))
  36. }
  37. func (o *CloneStream) String() string {
  38. return fmt.Sprintf("CloneStream %v -> (%v)", o.Raw.ID, utils.FormatVarIDs(o.Cloneds))
  39. }
  40. type CloneVar struct {
  41. Raw exec.Var `json:"raw"`
  42. Cloneds []exec.Var `json:"cloneds"`
  43. }
  44. func (o *CloneVar) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
  45. err := e.BindVars(ctx.Context, o.Raw)
  46. if err != nil {
  47. return err
  48. }
  49. for _, v := range o.Cloneds {
  50. if err := exec.AssignVar(o.Raw, v); err != nil {
  51. return fmt.Errorf("clone var: %w", err)
  52. }
  53. }
  54. e.PutVars(o.Cloneds...)
  55. return nil
  56. }
  57. func (o *CloneVar) String() string {
  58. return fmt.Sprintf("CloneStream %v -> (%v)", o.Raw.GetID(), utils.FormatVarIDs(o.Cloneds))
  59. }
  60. type CloneStreamType struct {
  61. dag.NodeBase
  62. }
  63. func (b *GraphNodeBuilder) NewCloneStream() *CloneStreamType {
  64. node := &CloneStreamType{}
  65. b.AddNode(node)
  66. return node
  67. }
  68. func (t *CloneStreamType) SetInput(raw *dag.StreamVar) {
  69. t.InputStreams().EnsureSize(1)
  70. raw.Connect(t, 0)
  71. }
  72. func (t *CloneStreamType) NewOutput() *dag.StreamVar {
  73. output := t.Graph().NewStreamVar()
  74. t.OutputStreams().SetupNew(t, output)
  75. return output
  76. }
  77. func (t *CloneStreamType) GenerateOp() (exec.Op, error) {
  78. return &CloneStream{
  79. Raw: t.InputStreams().Get(0).Var,
  80. Cloneds: lo.Map(t.OutputStreams().RawArray(), func(v *dag.StreamVar, idx int) *exec.StreamVar {
  81. return v.Var
  82. }),
  83. }, nil
  84. }
  85. // func (t *CloneStreamType) String() string {
  86. // return fmt.Sprintf("CloneStream[]%v%v", formatStreamIO(node), formatValueIO(node))
  87. // }
  88. type CloneVarType struct {
  89. dag.NodeBase
  90. }
  91. func (b *GraphNodeBuilder) NewCloneValue() *CloneVarType {
  92. node := &CloneVarType{}
  93. b.AddNode(node)
  94. return node
  95. }
  96. func (t *CloneVarType) SetInput(raw *dag.ValueVar) {
  97. t.InputValues().EnsureSize(1)
  98. raw.Connect(t, 0)
  99. }
  100. func (t *CloneVarType) NewOutput() *dag.ValueVar {
  101. output := t.Graph().NewValueVar(t.InputValues().Get(0).Type)
  102. t.OutputValues().SetupNew(t, output)
  103. return output
  104. }
  105. func (t *CloneVarType) GenerateOp() (exec.Op, error) {
  106. return &CloneVar{
  107. Raw: t.InputValues().Get(0).Var,
  108. Cloneds: lo.Map(t.OutputValues().RawArray(), func(v *dag.ValueVar, idx int) exec.Var {
  109. return v.Var
  110. }),
  111. }, nil
  112. }
  113. // func (t *CloneVarType) String() string {
  114. // return fmt.Sprintf("CloneVar[]%v%v", formatStreamIO(node), formatValueIO(node))
  115. // }

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