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.

bypass.go 4.9 kB

11 months ago
11 months ago
11 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package ops2
  2. import (
  3. "fmt"
  4. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/dag"
  5. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/exec"
  6. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  7. "gitlink.org.cn/cloudream/storage/common/pkgs/storage/agtpool"
  8. "gitlink.org.cn/cloudream/storage/common/pkgs/storage/types"
  9. )
  10. func init() {
  11. exec.UseOp[*BypassToShardStore]()
  12. exec.UseVarValue[*BypassFileInfoValue]()
  13. exec.UseVarValue[*BypassHandleResultValue]()
  14. exec.UseOp[*BypassFromShardStore]()
  15. exec.UseVarValue[*BypassFilePathValue]()
  16. }
  17. type BypassFileInfoValue struct {
  18. types.BypassFileInfo
  19. }
  20. func (v *BypassFileInfoValue) Clone() exec.VarValue {
  21. return &BypassFileInfoValue{
  22. BypassFileInfo: v.BypassFileInfo,
  23. }
  24. }
  25. type BypassHandleResultValue struct {
  26. Commited bool
  27. }
  28. func (r *BypassHandleResultValue) Clone() exec.VarValue {
  29. return &BypassHandleResultValue{
  30. Commited: r.Commited,
  31. }
  32. }
  33. type BypassToShardStore struct {
  34. StorageID cdssdk.StorageID
  35. BypassFileInfo exec.VarID
  36. BypassCallback exec.VarID
  37. FileHash exec.VarID
  38. }
  39. func (o *BypassToShardStore) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
  40. stgAgts, err := exec.GetValueByType[*agtpool.AgentPool](ctx)
  41. if err != nil {
  42. return err
  43. }
  44. shardStore, err := stgAgts.GetShardStore(o.StorageID)
  45. if err != nil {
  46. return err
  47. }
  48. br, ok := shardStore.(types.BypassWrite)
  49. if !ok {
  50. return fmt.Errorf("shard store %v not support bypass write", o.StorageID)
  51. }
  52. fileInfo, err := exec.BindVar[*BypassFileInfoValue](e, ctx.Context, o.BypassFileInfo)
  53. if err != nil {
  54. return err
  55. }
  56. err = br.BypassUploaded(fileInfo.BypassFileInfo)
  57. if err != nil {
  58. return err
  59. }
  60. e.PutVar(o.BypassCallback, &BypassHandleResultValue{Commited: true})
  61. e.PutVar(o.FileHash, &FileHashValue{Hash: fileInfo.FileHash})
  62. return nil
  63. }
  64. func (o *BypassToShardStore) String() string {
  65. return fmt.Sprintf("BypassToShardStore[StorageID:%v] Info: %v, Callback: %v", o.StorageID, o.BypassFileInfo, o.BypassCallback)
  66. }
  67. type BypassFilePathValue struct {
  68. types.BypassFilePath
  69. }
  70. func (v *BypassFilePathValue) Clone() exec.VarValue {
  71. return &BypassFilePathValue{
  72. BypassFilePath: v.BypassFilePath,
  73. }
  74. }
  75. type BypassFromShardStore struct {
  76. StorageID cdssdk.StorageID
  77. FileHash cdssdk.FileHash
  78. Output exec.VarID
  79. }
  80. func (o *BypassFromShardStore) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
  81. stgAgts, err := exec.GetValueByType[*agtpool.AgentPool](ctx)
  82. if err != nil {
  83. return err
  84. }
  85. shardStore, err := stgAgts.GetShardStore(o.StorageID)
  86. if err != nil {
  87. return err
  88. }
  89. br, ok := shardStore.(types.BypassRead)
  90. if !ok {
  91. return fmt.Errorf("shard store %v not support bypass read", o.StorageID)
  92. }
  93. path, err := br.BypassRead(o.FileHash)
  94. if err != nil {
  95. return err
  96. }
  97. e.PutVar(o.Output, &BypassFilePathValue{BypassFilePath: path})
  98. return nil
  99. }
  100. func (o *BypassFromShardStore) String() string {
  101. return fmt.Sprintf("BypassFromShardStore[StorageID:%v] FileHash: %v, Output: %v", o.StorageID, o.FileHash, o.Output)
  102. }
  103. // 旁路写入
  104. type BypassToShardStoreNode struct {
  105. dag.NodeBase
  106. StorageID cdssdk.StorageID
  107. FileHashStoreKey string
  108. }
  109. func (b *GraphNodeBuilder) NewBypassToShardStore(storageID cdssdk.StorageID, fileHashStoreKey string) *BypassToShardStoreNode {
  110. node := &BypassToShardStoreNode{
  111. StorageID: storageID,
  112. FileHashStoreKey: fileHashStoreKey,
  113. }
  114. b.AddNode(node)
  115. node.InputValues().Init(1)
  116. node.OutputValues().Init(node, 2)
  117. return node
  118. }
  119. func (n *BypassToShardStoreNode) BypassFileInfoSlot() dag.ValueInputSlot {
  120. return dag.ValueInputSlot{
  121. Node: n,
  122. Index: 0,
  123. }
  124. }
  125. func (n *BypassToShardStoreNode) BypassCallbackVar() dag.ValueOutputSlot {
  126. return dag.ValueOutputSlot{
  127. Node: n,
  128. Index: 0,
  129. }
  130. }
  131. func (n *BypassToShardStoreNode) FileHashVar() dag.ValueOutputSlot {
  132. return dag.ValueOutputSlot{
  133. Node: n,
  134. Index: 1,
  135. }
  136. }
  137. func (t *BypassToShardStoreNode) GenerateOp() (exec.Op, error) {
  138. return &BypassToShardStore{
  139. StorageID: t.StorageID,
  140. BypassFileInfo: t.BypassFileInfoSlot().Var().VarID,
  141. BypassCallback: t.BypassCallbackVar().Var().VarID,
  142. FileHash: t.FileHashVar().Var().VarID,
  143. }, nil
  144. }
  145. // 旁路读取
  146. type BypassFromShardStoreNode struct {
  147. dag.NodeBase
  148. StorageID cdssdk.StorageID
  149. FileHash cdssdk.FileHash
  150. }
  151. func (b *GraphNodeBuilder) NewBypassFromShardStore(storageID cdssdk.StorageID, fileHash cdssdk.FileHash) *BypassFromShardStoreNode {
  152. node := &BypassFromShardStoreNode{
  153. StorageID: storageID,
  154. FileHash: fileHash,
  155. }
  156. b.AddNode(node)
  157. node.OutputValues().Init(node, 1)
  158. return node
  159. }
  160. func (n *BypassFromShardStoreNode) FilePathVar() dag.ValueOutputSlot {
  161. return dag.ValueOutputSlot{
  162. Node: n,
  163. Index: 0,
  164. }
  165. }
  166. func (n *BypassFromShardStoreNode) GenerateOp() (exec.Op, error) {
  167. return &BypassFromShardStore{
  168. StorageID: n.StorageID,
  169. FileHash: n.FileHash,
  170. Output: n.FilePathVar().Var().VarID,
  171. }, nil
  172. }

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