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.

range.go 2.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package ops2
  2. import (
  3. "fmt"
  4. "io"
  5. "gitlink.org.cn/cloudream/common/pkgs/future"
  6. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/dag"
  7. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/exec"
  8. "gitlink.org.cn/cloudream/common/utils/io2"
  9. "gitlink.org.cn/cloudream/common/utils/math2"
  10. )
  11. func init() {
  12. exec.UseOp[*Range]()
  13. }
  14. type Range struct {
  15. Input *exec.StreamVar `json:"input"`
  16. Output *exec.StreamVar `json:"output"`
  17. Offset int64 `json:"offset"`
  18. Length *int64 `json:"length"`
  19. }
  20. func (o *Range) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
  21. err := e.BindVars(ctx.Context, o.Input)
  22. if err != nil {
  23. return err
  24. }
  25. defer o.Input.Stream.Close()
  26. buf := make([]byte, 1024*16)
  27. // 跳过前Offset个字节
  28. for o.Offset > 0 {
  29. rdCnt := math2.Min(o.Offset, int64(len(buf)))
  30. rd, err := o.Input.Stream.Read(buf[:rdCnt])
  31. if err == io.EOF {
  32. // 输入流不够长度也不报错,只是产生一个空的流
  33. break
  34. }
  35. if err != nil {
  36. return err
  37. }
  38. o.Offset -= int64(rd)
  39. }
  40. fut := future.NewSetVoid()
  41. if o.Length == nil {
  42. o.Output.Stream = io2.AfterEOF(o.Input.Stream, func(closer io.ReadCloser, err error) {
  43. fut.SetVoid()
  44. })
  45. e.PutVars(o.Output)
  46. return fut.Wait(ctx.Context)
  47. }
  48. o.Output.Stream = io2.AfterEOF(io2.Length(o.Input.Stream, *o.Length), func(closer io.ReadCloser, err error) {
  49. fut.SetVoid()
  50. })
  51. e.PutVars(o.Output)
  52. err = fut.Wait(ctx.Context)
  53. if err != nil {
  54. return err
  55. }
  56. io2.DropWithBuf(o.Input.Stream, buf)
  57. return nil
  58. }
  59. func (o *Range) String() string {
  60. return fmt.Sprintf("Range(%v+%v) %v -> %v", o.Offset, o.Length, o.Input.ID, o.Output.ID)
  61. }
  62. type RangeNode struct {
  63. dag.NodeBase
  64. Range exec.Range
  65. }
  66. func (b *GraphNodeBuilder) NewRange() *RangeNode {
  67. node := &RangeNode{}
  68. b.AddNode(node)
  69. return node
  70. }
  71. func (t *RangeNode) RangeStream(input *dag.StreamVar, rng exec.Range) *dag.StreamVar {
  72. t.InputStreams().EnsureSize(1)
  73. input.Connect(t, 0)
  74. t.Range = rng
  75. output := t.Graph().NewStreamVar()
  76. t.OutputStreams().Setup(t, output, 0)
  77. return output
  78. }
  79. func (t *RangeNode) GenerateOp() (exec.Op, error) {
  80. return &Range{
  81. Input: t.InputStreams().Get(0).Var,
  82. Output: t.OutputStreams().Get(0).Var,
  83. Offset: t.Range.Offset,
  84. Length: t.Range.Length,
  85. }, nil
  86. }
  87. // func (t *RangeType) String() string {
  88. // return fmt.Sprintf("Range[%v+%v]%v%v", t.Range.Offset, t.Range.Length, formatStreamIO(node), formatValueIO(node))
  89. // }

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