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.

segment.go 4.3 kB

11 months ago
11 months ago
11 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package ops2
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "gitlink.org.cn/cloudream/common/pkgs/future"
  7. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/dag"
  8. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/exec"
  9. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/utils"
  10. "gitlink.org.cn/cloudream/common/utils/io2"
  11. )
  12. func init() {
  13. exec.UseOp[*SegmentSplit]()
  14. exec.UseOp[*SegmentJoin]()
  15. }
  16. type SegmentSplit struct {
  17. Input exec.VarID
  18. Segments []int64
  19. Outputs []exec.VarID
  20. }
  21. func (o *SegmentSplit) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
  22. input, err := exec.BindVar[*exec.StreamValue](e, ctx.Context, o.Input)
  23. if err != nil {
  24. return err
  25. }
  26. defer input.Stream.Close()
  27. for i, outID := range o.Outputs {
  28. fut := future.NewSetVoid()
  29. segStr := io.LimitReader(input.Stream, o.Segments[i])
  30. segStr2 := io2.DelegateReadCloser(segStr, func() error {
  31. fut.SetError(context.Canceled)
  32. return nil
  33. })
  34. segStr2 = io2.AfterEOF(segStr2, func(str io.ReadCloser, err error) {
  35. fut.SetVoid()
  36. })
  37. e.PutVar(outID, &exec.StreamValue{Stream: segStr2})
  38. err = fut.Wait(ctx.Context)
  39. if err != nil {
  40. return err
  41. }
  42. }
  43. return nil
  44. }
  45. func (o *SegmentSplit) String() string {
  46. return fmt.Sprintf("SegmentSplit(%v, %v) -> %v", o.Input, o.Segments, o.Outputs)
  47. }
  48. type SegmentJoin struct {
  49. Inputs []exec.VarID
  50. Output exec.VarID
  51. // 这些字段只在执行时使用
  52. ctx *exec.ExecContext
  53. e *exec.Executor
  54. nextStreamIdx int
  55. nextStream io.ReadCloser
  56. fut *future.SetVoidFuture
  57. }
  58. func (o *SegmentJoin) Read(buf []byte) (int, error) {
  59. for {
  60. if o.nextStream == nil {
  61. if o.nextStreamIdx >= len(o.Inputs) {
  62. o.fut.SetVoid()
  63. return 0, io.EOF
  64. }
  65. input, err := exec.BindVar[*exec.StreamValue](o.e, o.ctx.Context, o.Inputs[o.nextStreamIdx])
  66. if err != nil {
  67. return 0, err
  68. }
  69. o.nextStream = input.Stream
  70. o.nextStreamIdx++
  71. }
  72. n, err := o.nextStream.Read(buf)
  73. if err == io.EOF {
  74. o.nextStream.Close()
  75. o.nextStream = nil
  76. continue
  77. }
  78. return n, err
  79. }
  80. }
  81. func (o *SegmentJoin) Close() error {
  82. if o.nextStream != nil {
  83. o.nextStream.Close()
  84. o.nextStream = nil
  85. o.fut.SetVoid()
  86. }
  87. return nil
  88. }
  89. func (o *SegmentJoin) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
  90. o.ctx = ctx
  91. o.e = e
  92. o.nextStreamIdx = 0
  93. o.nextStream = nil
  94. o.fut = future.NewSetVoid()
  95. e.PutVar(o.Output, &exec.StreamValue{Stream: o})
  96. return o.fut.Wait(ctx.Context)
  97. }
  98. func (o *SegmentJoin) String() string {
  99. return fmt.Sprintf("SegmentJoin %v -> %v", utils.FormatVarIDs(o.Inputs), o.Output)
  100. }
  101. type SegmentSplitNode struct {
  102. dag.NodeBase
  103. Segments []int64
  104. }
  105. func (b *GraphNodeBuilder) NewSegmentSplit(segments []int64) *SegmentSplitNode {
  106. node := &SegmentSplitNode{
  107. Segments: segments,
  108. }
  109. b.AddNode(node)
  110. node.InputStreams().Init(1)
  111. node.OutputStreams().Init(node, len(segments))
  112. return node
  113. }
  114. func (n *SegmentSplitNode) SetInput(input *dag.StreamVar) {
  115. input.To(n, 0)
  116. }
  117. func (t *SegmentSplitNode) RemoveAllStream() {
  118. t.InputStreams().ClearAllInput(t)
  119. t.OutputStreams().ClearAllOutput(t)
  120. }
  121. func (n *SegmentSplitNode) Segment(index int) *dag.StreamVar {
  122. return n.OutputStreams().Get(index)
  123. }
  124. func (n *SegmentSplitNode) GenerateOp() (exec.Op, error) {
  125. var realSegs []int64
  126. var realSegVarIDs []exec.VarID
  127. for i := 0; i < len(n.Segments); i++ {
  128. if n.Segments[i] > 0 {
  129. realSegs = append(realSegs, n.Segments[i])
  130. realSegVarIDs = append(realSegVarIDs, n.Segment(i).VarID)
  131. }
  132. }
  133. return &SegmentSplit{
  134. Input: n.InputStreams().Get(0).VarID,
  135. Segments: realSegs,
  136. Outputs: realSegVarIDs,
  137. }, nil
  138. }
  139. type SegmentJoinNode struct {
  140. dag.NodeBase
  141. }
  142. func (b *GraphNodeBuilder) NewSegmentJoin(segmentSizes []int64) *SegmentJoinNode {
  143. node := &SegmentJoinNode{}
  144. b.AddNode(node)
  145. node.InputStreams().Init(len(segmentSizes))
  146. node.OutputStreams().Init(node, 1)
  147. return node
  148. }
  149. func (n *SegmentJoinNode) SetInput(index int, input *dag.StreamVar) {
  150. input.To(n, index)
  151. }
  152. func (n *SegmentJoinNode) RemoveAllInputs() {
  153. n.InputStreams().ClearAllInput(n)
  154. }
  155. func (n *SegmentJoinNode) Joined() *dag.StreamVar {
  156. return n.OutputStreams().Get(0)
  157. }
  158. func (t *SegmentJoinNode) GenerateOp() (exec.Op, error) {
  159. return &SegmentJoin{
  160. Inputs: t.InputStreams().GetVarIDs(),
  161. Output: t.OutputStreams().Get(0).VarID,
  162. }, nil
  163. }

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