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.

chunked.go 1.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package ops
  2. import (
  3. "context"
  4. "io"
  5. "gitlink.org.cn/cloudream/common/pkgs/future"
  6. "gitlink.org.cn/cloudream/common/utils/io2"
  7. "gitlink.org.cn/cloudream/storage/common/pkgs/ioswitch"
  8. "golang.org/x/sync/semaphore"
  9. )
  10. type ChunkedSplit struct {
  11. Input *ioswitch.StreamVar `json:"input"`
  12. Outputs []*ioswitch.StreamVar `json:"outputs"`
  13. ChunkSize int `json:"chunkSize"`
  14. PaddingZeros bool `json:"paddingZeros"`
  15. }
  16. func (o *ChunkedSplit) Execute(ctx context.Context, sw *ioswitch.Switch) error {
  17. err := sw.BindVars(ctx, o.Input)
  18. if err != nil {
  19. return err
  20. }
  21. defer o.Input.Stream.Close()
  22. outputs := io2.ChunkedSplit(o.Input.Stream, o.ChunkSize, len(o.Outputs), io2.ChunkedSplitOption{
  23. PaddingZeros: o.PaddingZeros,
  24. })
  25. sem := semaphore.NewWeighted(int64(len(outputs)))
  26. for i := range outputs {
  27. sem.Acquire(ctx, 1)
  28. o.Outputs[i].Stream = io2.AfterReadClosedOnce(outputs[i], func(closer io.ReadCloser) {
  29. sem.Release(1)
  30. })
  31. }
  32. ioswitch.PutArrayVars(sw, o.Outputs)
  33. return sem.Acquire(ctx, int64(len(outputs)))
  34. }
  35. type ChunkedJoin struct {
  36. Inputs []*ioswitch.StreamVar `json:"inputs"`
  37. Output *ioswitch.StreamVar `json:"output"`
  38. ChunkSize int `json:"chunkSize"`
  39. }
  40. func (o *ChunkedJoin) Execute(ctx context.Context, sw *ioswitch.Switch) error {
  41. err := ioswitch.BindArrayVars(sw, ctx, o.Inputs)
  42. if err != nil {
  43. return err
  44. }
  45. var strReaders []io.Reader
  46. for _, s := range o.Inputs {
  47. strReaders = append(strReaders, s.Stream)
  48. }
  49. defer func() {
  50. for _, str := range o.Inputs {
  51. str.Stream.Close()
  52. }
  53. }()
  54. fut := future.NewSetVoid()
  55. o.Output.Stream = io2.AfterReadClosedOnce(io2.BufferedChunkedJoin(strReaders, o.ChunkSize), func(closer io.ReadCloser) {
  56. fut.SetVoid()
  57. })
  58. sw.PutVars(o.Output)
  59. return fut.Wait(ctx)
  60. }
  61. func init() {
  62. OpUnion.AddT((*ChunkedSplit)(nil))
  63. OpUnion.AddT((*ChunkedJoin)(nil))
  64. }

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