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.

rs_test.go 6.2 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package ec
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "testing"
  8. "gitlink.org.cn/cloudream/common/pkgs/ipfs"
  9. //"gitlink.org.cn/cloudream/common/pkgs/ipfs"
  10. //"gitlink.org.cn/cloudream/storage/agent/internal/config"
  11. //stgglb "gitlink.org.cn/cloudream/storage/common/globals"
  12. stgglb "gitlink.org.cn/cloudream/storage/common/globals"
  13. )
  14. func test_Encode(t *testing.T) {
  15. enc, _ := NewRs(3, 5, 10)
  16. rc := make([]io.ReadCloser, 3)
  17. rc[0] = ioutil.NopCloser(bytes.NewBufferString("11111111"))
  18. rc[1] = ioutil.NopCloser(bytes.NewBufferString("22222222"))
  19. rc[2] = ioutil.NopCloser(bytes.NewBufferString("33333333"))
  20. /*rc[0].Close()
  21. rc[1].Close()
  22. rc[2].Close()*/
  23. print("#$$$$$$$$$$$")
  24. out, _ := enc.ReconstructData(rc, []int{0, 1, 2})
  25. //out, _ := enc.Encode(rc)
  26. buf := make([]byte, 100)
  27. out[0].Read(buf)
  28. fmt.Println(buf)
  29. out[1].Read(buf)
  30. fmt.Println(buf)
  31. t.Logf(string(buf))
  32. t.Log(buf)
  33. }
  34. /*
  35. ------------------------------------------------
  36. hash:QmX49sGugmtVPfNo13q84YL1NwGmr5yzWDDmJZ7PniQ9b6
  37. 内容:1111122222233333333334444444445663454543534534
  38. hash:QmcN1EJm2w9XT62Q9YqA5Ym7YDzjmnqJYc565bzRs5VosW
  39. (5,3),chunkSize:6
  40. data1:QmS2t7xFgTMTX2DGYsbDdmHnGvaG6sc7D9k1R2WZyuDx56
  41. data2:QmUSZvuABjfGKF1c4VxvVBdH31SroDm2QyLGBrVFomRM8P
  42. data3:QmcD3RpUh5rwMhf9yBywBeT6ibT1P5DSJC67aoD77jhTBn
  43. 内容:qqqqqqqqwwwwwwwwwwwwwweeeeeeeeeeeeerrrrrrrrrrr
  44. -----------------------------------------------------
  45. */
  46. func test_Fetch(t *testing.T) {
  47. blkReader, _ := NewBlockReader()
  48. /*****************************FetchBlock*************************/
  49. /*r, _ := blkReader.FetchBLock("QmX49sGugmtVPfNo13q84YL1NwGmr5yzWDDmJZ7PniQ9b6")
  50. data, _ := ioutil.ReadAll(r)
  51. t.Logf(string(data))*/
  52. /**********************FetchBlocks************************************
  53. hashs := []string{"QmcN1EJm2w9XT62Q9YqA5Ym7YDzjmnqJYc565bzRs5VosW", "QmX49sGugmtVPfNo13q84YL1NwGmr5yzWDDmJZ7PniQ9b6"}
  54. rs, _ := blkReader.FetchBLocks(hashs)
  55. data1, _ := ioutil.ReadAll(rs[0])
  56. data2, _ := ioutil.ReadAll(rs[1])
  57. t.Logf(string(data1))
  58. t.Logf(string(data2))
  59. /*************************JumpFetchBlock*********************************/
  60. blkReader.SetJumpRead("QmcN1EJm2w9XT62Q9YqA5Ym7YDzjmnqJYc565bzRs5VosW", 46, 3)
  61. blkReader.SetchunkSize(6)
  62. r, _ := blkReader.JumpFetchBlock(1)
  63. data, _ := ioutil.ReadAll(r)
  64. t.Logf(string(data))
  65. }
  66. func test_Fetch_and_Encode(t *testing.T) {
  67. chunkSize := int64(6)
  68. blkReader, _ := NewBlockReader()
  69. defer blkReader.Close()
  70. blkReader.SetJumpRead("QmcN1EJm2w9XT62Q9YqA5Ym7YDzjmnqJYc565bzRs5VosW", 46, 3)
  71. blkReader.SetchunkSize(int64(chunkSize))
  72. dataBlocks := make([]io.ReadCloser, 3)
  73. for i := range dataBlocks {
  74. dataBlocks[i], _ = blkReader.JumpFetchBlock(i)
  75. }
  76. enc, _ := NewRs(3, 5, chunkSize)
  77. parityBlocks, _ := enc.Encode(dataBlocks)
  78. parityData := make([]string, 2)
  79. finished := false
  80. for {
  81. if finished {
  82. break
  83. }
  84. buf := make([]byte, chunkSize)
  85. for i, pipe := range parityBlocks {
  86. _, err := pipe.Read(buf)
  87. if err != nil {
  88. finished = true
  89. break
  90. }
  91. parityData[i] = parityData[i] + string(buf)
  92. }
  93. }
  94. t.Logf(parityData[0])
  95. t.Logf(parityData[1])
  96. }
  97. func test_Fetch_and_Encode_and_Degraded(t *testing.T) {
  98. chunkSize := int64(6)
  99. blkReader, _ := NewBlockReader()
  100. defer blkReader.Close()
  101. blkReader.SetJumpRead("QmcN1EJm2w9XT62Q9YqA5Ym7YDzjmnqJYc565bzRs5VosW", 46, 3)
  102. blkReader.SetchunkSize(int64(chunkSize))
  103. dataBlocks := make([]io.ReadCloser, 3)
  104. for i := range dataBlocks {
  105. dataBlocks[i], _ = blkReader.JumpFetchBlock(i)
  106. }
  107. enc, _ := NewRs(3, 5, chunkSize)
  108. parityBlocks, _ := enc.Encode(dataBlocks)
  109. go func() {
  110. ioutil.ReadAll(parityBlocks[0])
  111. }()
  112. degradedBlocks := make([]io.ReadCloser, 3)
  113. degradedBlocks[0], _ = blkReader.JumpFetchBlock(1)
  114. degradedBlocks[1], _ = blkReader.JumpFetchBlock(2)
  115. degradedBlocks[2] = parityBlocks[1]
  116. newDataBlocks, _ := enc.ReconstructData(degradedBlocks, []int{1, 2, 4})
  117. newData := make([]string, 3)
  118. finished := false
  119. for {
  120. if finished {
  121. break
  122. }
  123. buf := make([]byte, chunkSize)
  124. for i, pipe := range newDataBlocks {
  125. _, err := pipe.Read(buf)
  126. if err != nil {
  127. finished = true
  128. break
  129. }
  130. newData[i] = newData[i] + string(buf)
  131. }
  132. }
  133. t.Logf(newData[0])
  134. t.Logf(newData[1])
  135. t.Logf(newData[2])
  136. }
  137. func test_pin_data_blocks(t *testing.T) {
  138. chunkSize := int64(6)
  139. blkReader, _ := NewBlockReader()
  140. defer blkReader.Close()
  141. blkReader.SetJumpRead("QmcN1EJm2w9XT62Q9YqA5Ym7YDzjmnqJYc565bzRs5VosW", 46, 3)
  142. blkReader.SetchunkSize(int64(chunkSize))
  143. dataBlocks := make([]io.ReadCloser, 3)
  144. ipfsclient, _ := stgglb.IPFSPool.Acquire()
  145. for i := range dataBlocks {
  146. dataBlocks[i], _ = blkReader.JumpFetchBlock(i)
  147. hash, _ := ipfsclient.CreateFile(dataBlocks[i])
  148. t.Logf(hash)
  149. }
  150. }
  151. func print_ioreaders(t *testing.T, readers []io.ReadCloser, chunkSize int64) {
  152. newData := make([]string, len(readers))
  153. finished := false
  154. for {
  155. if finished {
  156. break
  157. }
  158. buf := make([]byte, chunkSize)
  159. for i, pipe := range readers {
  160. _, err := pipe.Read(buf)
  161. if err != nil {
  162. finished = true
  163. break
  164. }
  165. newData[i] = newData[i] + string(buf)
  166. }
  167. }
  168. for _, data := range newData {
  169. t.Logf(data)
  170. }
  171. }
  172. func test_reconstructData(t *testing.T) {
  173. /*
  174. blkReader, _ := NewBlockReader()
  175. defer blkReader.Close()
  176. hashs := []string{"QmS2t7xFgTMTX2DGYsbDdmHnGvaG6sc7D9k1R2WZyuDx56", "QmUSZvuABjfGKF1c4VxvVBdH31SroDm2QyLGBrVFomRM8P", "QmcD3RpUh5rwMhf9yBywBeT6ibT1P5DSJC67aoD77jhTBn"}
  177. dataBlocks, _ := blkReader.FetchBLocks(hashs)
  178. chunkSize := int64(6)
  179. enc, _ := NewRs(3, 5, chunkSize)
  180. print("@@@@@@@@@")
  181. newDataBlocks, _ := enc.ReconstructSome(dataBlocks, []int{0, 1, 2}, []int{3, 4})
  182. print("!!!!!!!!!")
  183. print_ioreaders(t, newDataBlocks, chunkSize)
  184. */
  185. }
  186. func Test_main(t *testing.T) {
  187. //test_Encode(t)
  188. //stgglb.InitLocal(&config.Cfg().Local)
  189. stgglb.InitIPFSPool(&ipfs.Config{Port: 5001})
  190. //test_Fetch(t)
  191. //test_Fetch_and_Encode(t)
  192. //test_Fetch_and_Encode_and_Degraded(t)
  193. //test_pin_data_blocks(t)
  194. test_reconstructData(t)
  195. }
  196. /*
  197. func Test_Fetch_Encode_ReconstructData(t *testing.T) {
  198. inFileName := "test.txt"
  199. enc, _ := NewRs(3, 5, 10)
  200. file, err := os.Open(inFileName)
  201. if err != nil {
  202. t.Error(err)
  203. }
  204. var data io.ReadCloser
  205. data = file
  206. //enc.Encode(data)
  207. }*/

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