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.

plan_builder.go 2.0 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package plans
  2. import (
  3. "fmt"
  4. "github.com/google/uuid"
  5. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  6. "gitlink.org.cn/cloudream/storage/common/pkgs/db/model"
  7. "gitlink.org.cn/cloudream/storage/common/pkgs/ioswitch"
  8. )
  9. type StreamInfo struct {
  10. ID ioswitch.StreamID
  11. }
  12. type PlanBuilder struct {
  13. streams []*StreamInfo
  14. agentPlans map[cdssdk.NodeID]*AgentPlanBuilder
  15. }
  16. func (b *PlanBuilder) Build() (*ComposedPlan, error) {
  17. planID := uuid.NewString()
  18. var agentPlans []AgentPlan
  19. for _, b := range b.agentPlans {
  20. plan, err := b.Build(ioswitch.PlanID(planID))
  21. if err != nil {
  22. return nil, err
  23. }
  24. agentPlans = append(agentPlans, plan)
  25. }
  26. return &ComposedPlan{
  27. ID: ioswitch.PlanID(planID),
  28. AgentPlans: agentPlans,
  29. }, nil
  30. }
  31. func (b *PlanBuilder) newStream() *StreamInfo {
  32. str := &StreamInfo{
  33. ID: ioswitch.StreamID(fmt.Sprintf("%d", len(b.streams)+1)),
  34. }
  35. b.streams = append(b.streams, str)
  36. return str
  37. }
  38. func NewPlanBuilder() PlanBuilder {
  39. return PlanBuilder{
  40. agentPlans: make(map[cdssdk.NodeID]*AgentPlanBuilder),
  41. }
  42. }
  43. func (b *PlanBuilder) FromExecutor() *FromExecutorStream {
  44. return &FromExecutorStream{
  45. owner: b,
  46. info: b.newStream(),
  47. }
  48. }
  49. func (b *PlanBuilder) AtAgent(node model.Node) *AgentPlanBuilder {
  50. agtPlan, ok := b.agentPlans[node.NodeID]
  51. if !ok {
  52. agtPlan = &AgentPlanBuilder{
  53. owner: b,
  54. node: node,
  55. }
  56. b.agentPlans[node.NodeID] = agtPlan
  57. }
  58. return agtPlan
  59. }
  60. type FromExecutorStream struct {
  61. owner *PlanBuilder
  62. info *StreamInfo
  63. toNode *model.Node
  64. }
  65. func (s *FromExecutorStream) ToNode(node model.Node) *AgentStream {
  66. s.toNode = &node
  67. return &AgentStream{
  68. owner: s.owner.AtAgent(node),
  69. info: s.info,
  70. }
  71. }
  72. type ToExecutorStream struct {
  73. info *StreamInfo
  74. fromNode *model.Node
  75. }
  76. type MultiStream struct {
  77. Streams []*AgentStream
  78. }
  79. func (m *MultiStream) Count() int {
  80. return len(m.Streams)
  81. }
  82. func (m *MultiStream) Stream(index int) *AgentStream {
  83. return m.Streams[index]
  84. }

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