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.

shard_store.go 5.2 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
1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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/pkgs/logger"
  9. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  10. "gitlink.org.cn/cloudream/common/utils/io2"
  11. "gitlink.org.cn/cloudream/storage/common/pkgs/ioswitch2"
  12. "gitlink.org.cn/cloudream/storage/common/pkgs/storage/mgr"
  13. "gitlink.org.cn/cloudream/storage/common/pkgs/storage/types"
  14. )
  15. func init() {
  16. exec.UseOp[*ShardRead]()
  17. exec.UseOp[*ShardWrite]()
  18. exec.UseVarValue[*FileHashValue]()
  19. }
  20. type FileHashValue struct {
  21. Hash cdssdk.FileHash `json:"hash"`
  22. }
  23. func (v *FileHashValue) Clone() exec.VarValue {
  24. return &FileHashValue{Hash: v.Hash}
  25. }
  26. type ShardRead struct {
  27. Output exec.VarID `json:"output"`
  28. StorageID cdssdk.StorageID `json:"storageID"`
  29. Open types.OpenOption `json:"option"`
  30. }
  31. func (o *ShardRead) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
  32. logger.
  33. WithField("Open", o.Open.String()).
  34. Debugf("reading from shard store")
  35. defer logger.Debugf("reading from shard store finished")
  36. stgMgr, err := exec.GetValueByType[*mgr.Manager](ctx)
  37. if err != nil {
  38. return fmt.Errorf("getting storage manager: %w", err)
  39. }
  40. store, err := stgMgr.GetShardStore(o.StorageID)
  41. if err != nil {
  42. return fmt.Errorf("getting shard store of storage %v: %w", o.StorageID, err)
  43. }
  44. file, err := store.Open(o.Open)
  45. if err != nil {
  46. return fmt.Errorf("opening shard store file: %w", err)
  47. }
  48. fut := future.NewSetVoid()
  49. e.PutVar(o.Output, &exec.StreamValue{
  50. Stream: io2.AfterReadClosedOnce(file, func(closer io.ReadCloser) {
  51. fut.SetVoid()
  52. }),
  53. })
  54. return fut.Wait(ctx.Context)
  55. }
  56. func (o *ShardRead) String() string {
  57. return fmt.Sprintf("ShardRead %v -> %v", o.Open.String(), o.Output)
  58. }
  59. type ShardWrite struct {
  60. Input exec.VarID `json:"input"`
  61. FileHash exec.VarID `json:"fileHash"`
  62. StorageID cdssdk.StorageID `json:"storageID"`
  63. }
  64. func (o *ShardWrite) Execute(ctx *exec.ExecContext, e *exec.Executor) error {
  65. logger.
  66. WithField("Input", o.Input).
  67. WithField("FileHash", o.FileHash).
  68. Debugf("writting file to shard store")
  69. defer logger.Debugf("write to shard store finished")
  70. stgMgr, err := exec.GetValueByType[*mgr.Manager](ctx)
  71. if err != nil {
  72. return fmt.Errorf("getting storage manager: %w", err)
  73. }
  74. store, err := stgMgr.GetShardStore(o.StorageID)
  75. if err != nil {
  76. return fmt.Errorf("getting shard store of storage %v: %w", o.StorageID, err)
  77. }
  78. input, err := exec.BindVar[*exec.StreamValue](e, ctx.Context, o.Input)
  79. if err != nil {
  80. return err
  81. }
  82. defer input.Stream.Close()
  83. fileInfo, err := store.Create(input.Stream)
  84. if err != nil {
  85. return fmt.Errorf("writing file to shard store: %w", err)
  86. }
  87. e.PutVar(o.FileHash, &FileHashValue{
  88. Hash: fileInfo.Hash,
  89. })
  90. return nil
  91. }
  92. func (o *ShardWrite) String() string {
  93. return fmt.Sprintf("ShardWrite %v -> %v", o.Input, o.FileHash)
  94. }
  95. type ShardReadNode struct {
  96. dag.NodeBase
  97. From ioswitch2.From
  98. StorageID cdssdk.StorageID
  99. Open types.OpenOption
  100. }
  101. func (b *GraphNodeBuilder) NewShardRead(fr ioswitch2.From, stgID cdssdk.StorageID, open types.OpenOption) *ShardReadNode {
  102. node := &ShardReadNode{
  103. From: fr,
  104. StorageID: stgID,
  105. Open: open,
  106. }
  107. b.AddNode(node)
  108. node.OutputStreams().Init(node, 1)
  109. return node
  110. }
  111. func (t *ShardReadNode) GetFrom() ioswitch2.From {
  112. return t.From
  113. }
  114. func (t *ShardReadNode) Output() dag.StreamSlot {
  115. return dag.StreamSlot{
  116. Var: t.OutputStreams().Get(0),
  117. Index: 0,
  118. }
  119. }
  120. func (t *ShardReadNode) GenerateOp() (exec.Op, error) {
  121. return &ShardRead{
  122. Output: t.OutputStreams().Get(0).VarID,
  123. StorageID: t.StorageID,
  124. Open: t.Open,
  125. }, nil
  126. }
  127. // func (t *IPFSReadType) String() string {
  128. // return fmt.Sprintf("IPFSRead[%s,%v+%v]%v%v", t.FileHash, t.Option.Offset, t.Option.Length, formatStreamIO(node), formatValueIO(node))
  129. // }
  130. type ShardWriteNode struct {
  131. dag.NodeBase
  132. To ioswitch2.To
  133. StorageID cdssdk.StorageID
  134. FileHashStoreKey string
  135. }
  136. func (b *GraphNodeBuilder) NewShardWrite(to ioswitch2.To, stgID cdssdk.StorageID, fileHashStoreKey string) *ShardWriteNode {
  137. node := &ShardWriteNode{
  138. To: to,
  139. StorageID: stgID,
  140. FileHashStoreKey: fileHashStoreKey,
  141. }
  142. b.AddNode(node)
  143. node.InputStreams().Init(1)
  144. node.OutputValues().Init(node, 1)
  145. return node
  146. }
  147. func (t *ShardWriteNode) GetTo() ioswitch2.To {
  148. return t.To
  149. }
  150. func (t *ShardWriteNode) SetInput(input *dag.StreamVar) {
  151. input.To(t, 0)
  152. }
  153. func (t *ShardWriteNode) Input() dag.StreamSlot {
  154. return dag.StreamSlot{
  155. Var: t.InputStreams().Get(0),
  156. Index: 0,
  157. }
  158. }
  159. func (t *ShardWriteNode) FileHashVar() *dag.ValueVar {
  160. return t.OutputValues().Get(0)
  161. }
  162. func (t *ShardWriteNode) GenerateOp() (exec.Op, error) {
  163. return &ShardWrite{
  164. Input: t.InputStreams().Get(0).VarID,
  165. FileHash: t.OutputValues().Get(0).VarID,
  166. StorageID: t.StorageID,
  167. }, nil
  168. }
  169. // func (t *IPFSWriteType) String() string {
  170. // return fmt.Sprintf("IPFSWrite[%s,%v+%v]%v%v", t.FileHashStoreKey, t.Range.Offset, t.Range.Length, formatStreamIO(node), formatValueIO(node))
  171. // }

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