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.5 kB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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.VarID `json:"input"`
  16. Output exec.VarID `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. input, err := exec.BindVar[*exec.StreamValue](e, ctx.Context, o.Input)
  22. if err != nil {
  23. return err
  24. }
  25. defer 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 := 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. e.PutVar(o.Output, &exec.StreamValue{
  43. Stream: io2.AfterEOF(input.Stream, func(closer io.ReadCloser, err error) {
  44. fut.SetVoid()
  45. }),
  46. })
  47. return fut.Wait(ctx.Context)
  48. }
  49. e.PutVar(o.Output, &exec.StreamValue{
  50. Stream: io2.AfterEOF(io2.Length(input.Stream, *o.Length), func(closer io.ReadCloser, err error) {
  51. fut.SetVoid()
  52. }),
  53. })
  54. err = fut.Wait(ctx.Context)
  55. if err != nil {
  56. return err
  57. }
  58. io2.DropWithBuf(input.Stream, buf)
  59. return nil
  60. }
  61. func (o *Range) String() string {
  62. len := ""
  63. if o.Length != nil {
  64. len = fmt.Sprintf("%v", *o.Length)
  65. }
  66. return fmt.Sprintf("Range(%v+%v) %v -> %v", o.Offset, len, o.Input, o.Output)
  67. }
  68. type RangeNode struct {
  69. dag.NodeBase
  70. Range math2.Range
  71. }
  72. func (b *GraphNodeBuilder) NewRange() *RangeNode {
  73. node := &RangeNode{}
  74. b.AddNode(node)
  75. node.InputStreams().Init(1)
  76. node.OutputStreams().Init(node, 1)
  77. return node
  78. }
  79. func (t *RangeNode) RangeStream(input *dag.StreamVar, rng math2.Range) *dag.StreamVar {
  80. input.To(t, 0)
  81. t.Range = rng
  82. return t.OutputStreams().Get(0)
  83. }
  84. func (t *RangeNode) GenerateOp() (exec.Op, error) {
  85. return &Range{
  86. Input: t.InputStreams().Get(0).VarID,
  87. Output: t.OutputStreams().Get(0).VarID,
  88. Offset: t.Range.Offset,
  89. Length: t.Range.Length,
  90. }, nil
  91. }
  92. // func (t *RangeType) String() string {
  93. // return fmt.Sprintf("Range[%v+%v]%v%v", t.Range.Offset, t.Range.Length, formatStreamIO(node), formatValueIO(node))
  94. // }

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