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 5.2 kB

11 months ago
11 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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.OutputStreams().Resize(len(segments))
  111. return node
  112. }
  113. func (n *SegmentSplitNode) SetInput(input *dag.Var) {
  114. n.InputStreams().EnsureSize(1)
  115. input.StreamTo(n, 0)
  116. }
  117. func (t *SegmentSplitNode) RemoveAllStream() {
  118. if t.InputStreams().Len() == 0 {
  119. return
  120. }
  121. t.InputStreams().Get(0).StreamNotTo(t, 0)
  122. t.InputStreams().Resize(0)
  123. for _, out := range t.OutputStreams().RawArray() {
  124. out.NoInputAllStream()
  125. }
  126. t.OutputStreams().Resize(0)
  127. }
  128. func (n *SegmentSplitNode) Segment(index int) *dag.Var {
  129. // 必须连续消耗流
  130. for i := 0; i <= index; i++ {
  131. if n.OutputStreams().Get(i) == nil {
  132. n.OutputStreams().Setup(n, n.Graph().NewVar(), i)
  133. }
  134. }
  135. return n.OutputStreams().Get(index)
  136. }
  137. func (t *SegmentSplitNode) GenerateOp() (exec.Op, error) {
  138. lastUsedSeg := 0
  139. for i := t.OutputStreams().Len() - 1; i >= 0; i-- {
  140. if t.OutputStreams().Get(i) != nil {
  141. lastUsedSeg = i
  142. break
  143. }
  144. }
  145. return &SegmentSplit{
  146. Input: t.InputStreams().Get(0).VarID,
  147. Segments: t.segments[:lastUsedSeg+1],
  148. Outputs: t.OutputStreams().GetVarIDs(),
  149. }, nil
  150. }
  151. type SegmentJoinNode struct {
  152. dag.NodeBase
  153. UsedStart int
  154. UsedCount int
  155. }
  156. func (b *GraphNodeBuilder) NewSegmentJoin(segmentSizes []int64) *SegmentJoinNode {
  157. node := &SegmentJoinNode{}
  158. b.AddNode(node)
  159. node.InputStreams().Resize(len(segmentSizes))
  160. node.OutputStreams().SetupNew(node, b.NewVar())
  161. return node
  162. }
  163. func (n *SegmentJoinNode) SetInput(index int, input *dag.Var) {
  164. input.StreamTo(n, index)
  165. }
  166. func (n *SegmentJoinNode) RemoveAllInputs() {
  167. for i, in := range n.InputStreams().RawArray() {
  168. in.StreamNotTo(n, i)
  169. }
  170. n.InputStreams().Resize(0)
  171. }
  172. // 记录本计划中实际要使用的分段的范围,范围外的分段流都会取消输入
  173. func (n *SegmentJoinNode) MarkUsed(start, cnt int) {
  174. n.UsedStart = start
  175. n.UsedCount = cnt
  176. for i := 0; i < start; i++ {
  177. str := n.InputStreams().Get(i)
  178. if str != nil {
  179. str.StreamNotTo(n, i)
  180. }
  181. }
  182. for i := start + cnt; i < n.InputStreams().Len(); i++ {
  183. str := n.InputStreams().Get(i)
  184. if str != nil {
  185. str.StreamNotTo(n, i)
  186. }
  187. }
  188. }
  189. func (n *SegmentJoinNode) Joined() *dag.Var {
  190. return n.OutputStreams().Get(0)
  191. }
  192. func (t *SegmentJoinNode) GenerateOp() (exec.Op, error) {
  193. return &SegmentJoin{
  194. Inputs: t.InputStreams().GetVarIDsRanged(t.UsedStart, t.UsedStart+t.UsedCount),
  195. Output: t.OutputStreams().Get(0).VarID,
  196. }, nil
  197. }

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