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.

client.go 8.9 kB

7 months ago
7 months ago
7 months ago
7 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package jcstypes
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/samber/lo"
  6. "gitlink.org.cn/cloudream/common/utils/sort2"
  7. )
  8. const (
  9. ObjectPathSeparator = "/"
  10. )
  11. type PackageID int64
  12. type ObjectID int64
  13. type BucketID int64
  14. type UserSpaceID int64
  15. type Bucket struct {
  16. BucketID BucketID `gorm:"column:BucketID; primaryKey; type:bigint; autoIncrement" json:"bucketID"`
  17. Name string `gorm:"column:Name; type:varchar(255); not null" json:"name"`
  18. CreateTime time.Time `gorm:"column:CreateTime; type:datetime; not null" json:"createTime"`
  19. }
  20. func (Bucket) TableName() string {
  21. return "Bucket"
  22. }
  23. type Package struct {
  24. PackageID PackageID `gorm:"column:PackageID; primaryKey; type:bigint; autoIncrement" json:"packageID"`
  25. Name string `gorm:"column:Name; type:varchar(255); not null" json:"name"`
  26. BucketID BucketID `gorm:"column:BucketID; type:bigint; not null" json:"bucketID"`
  27. CreateTime time.Time `gorm:"column:CreateTime; type:datetime; not null" json:"createTime"`
  28. }
  29. func (Package) TableName() string {
  30. return "Package"
  31. }
  32. type Object struct {
  33. ObjectID ObjectID `json:"objectID" gorm:"column:ObjectID; primaryKey; type:bigint; autoIncrement" `
  34. PackageID PackageID `json:"packageID" gorm:"column:PackageID; type:bigint; not null"`
  35. Path string `json:"path" gorm:"column:Path; type:varchar(1024); not null"`
  36. Size int64 `json:"size,string" gorm:"column:Size; type:bigint; not null"`
  37. FileHash FileHash `json:"fileHash" gorm:"column:FileHash; type:char(68); not null"`
  38. Redundancy Redundancy `json:"redundancy" gorm:"column:Redundancy; type: json; serializer:union"`
  39. CreateTime time.Time `json:"createTime" gorm:"column:CreateTime; type:datetime; not null"`
  40. UpdateTime time.Time `json:"updateTime" gorm:"column:UpdateTime; type:datetime; not null"`
  41. }
  42. func (Object) TableName() string {
  43. return "Object"
  44. }
  45. type ObjectBlock struct {
  46. ObjectID ObjectID `gorm:"column:ObjectID; primaryKey; type:bigint" json:"objectID"`
  47. Index int `gorm:"column:Index; primaryKey; type:int" json:"index"`
  48. // 这个块应该在哪个空间中
  49. UserSpaceID UserSpaceID `gorm:"column:UserSpaceID; primaryKey; type:bigint" json:"userSpaceID"`
  50. FileHash FileHash `gorm:"column:FileHash; type:char(68); not null" json:"fileHash"`
  51. Size int64 `gorm:"column:Size; type:bigint" json:"size"`
  52. }
  53. func (ObjectBlock) TableName() string {
  54. return "ObjectBlock"
  55. }
  56. type UserSpace struct {
  57. UserSpaceID UserSpaceID `gorm:"column:UserSpaceID; primaryKey; type:bigint" json:"userSpaceID"`
  58. // 用户空间名称
  59. Name string `gorm:"column:Name; type:varchar(255); not null" json:"name"`
  60. // 用户空间所在的存储服务配置
  61. Storage StorageType `gorm:"column:Storage; type:json; not null; serializer:union" json:"storage"`
  62. // 用户在指定存储节点的凭证信息,比如用户账户,AK/SK等
  63. Credential StorageCredential `gorm:"column:Credential; type:json; not null; serializer:union" json:"credential"`
  64. // 用户空间的分片存储配置,如果为空,则表示不使用分片存储
  65. ShardStore *ShardStoreUserConfig `gorm:"column:ShardStore; type:json; serializer:json" json:"shardStore"`
  66. // 存储服务特性功能的配置
  67. Features []StorageFeature `json:"features" gorm:"column:Features; type:json; serializer:union"`
  68. // 各种组件保存数据的根目录。组件工作过程中都会以这个目录为根(除了BaseStore)。
  69. WorkingDir JPath `gorm:"column:WorkingDir; type:varchar(1024); not null; serializer:string" json:"workingDir"`
  70. // 工作目录在存储系统中的真实路径。当工作路径在挂载点内时,这个字段记录的是挂载背后的真实路径。部分直接与存储系统交互的组件需要知道真实路径。
  71. // RealWorkingDir string `gorm:"column:RealWorkingDir; type:varchar(1024); not null" json:"realWorkingDir"`
  72. // 用户空间信息的版本号,每一次更改都需要更新版本号
  73. Revision int64 `gorm:"column:Revision; type:bigint; not null" json:"revision"`
  74. }
  75. func (UserSpace) TableName() string {
  76. return "UserSpace"
  77. }
  78. func (s UserSpace) String() string {
  79. return fmt.Sprintf("%v[id=%v,storage=%v,rev=%v]", s.Name, s.UserSpaceID, s.Storage, s.Revision)
  80. }
  81. type PackageAccessStat struct {
  82. PackageID PackageID `gorm:"column:PackageID; primaryKey; type:bigint" json:"packageID"`
  83. // 发起读取(调度)的用户空间ID
  84. UserSpaceID UserSpaceID `gorm:"column:UserSpaceID; primaryKey; type:bigint" json:"storageID"`
  85. // 前一日的读取量的滑动平均值
  86. Amount float64 `gorm:"column:Amount; type:double" json:"amount"`
  87. // 当日的读取量
  88. Counter float64 `gorm:"column:Counter; type:double" json:"counter"`
  89. }
  90. func (PackageAccessStat) TableName() string {
  91. return "PackageAccessStat"
  92. }
  93. type ObjectAccessStat struct {
  94. ObjectID ObjectID `gorm:"column:ObjectID; primaryKey; type:bigint" json:"objectID"`
  95. // 发起读取(调度)的用户空间ID
  96. UserSpaceID UserSpaceID `gorm:"column:UserSpaceID; primaryKey; type:bigint" json:"userStorageID"`
  97. // 前一日的读取量的滑动平均值
  98. Amount float64 `gorm:"column:Amount; type:float; not null" json:"amount"`
  99. // 当日的读取量
  100. Counter float64 `gorm:"column:Counter; type:float; not null" json:"counter"`
  101. }
  102. func (ObjectAccessStat) TableName() string {
  103. return "ObjectAccessStat"
  104. }
  105. type PinnedObject struct {
  106. ObjectID ObjectID `gorm:"column:ObjectID; primaryKey; type:bigint" json:"objectID"`
  107. UserSpaceID UserSpaceID `gorm:"column:UserSpaceID; primaryKey; type:bigint" json:"userSpaceID"`
  108. CreateTime time.Time `gorm:"column:CreateTime; type:datetime; not null" json:"createTime"`
  109. }
  110. func (PinnedObject) TableName() string {
  111. return "PinnedObject"
  112. }
  113. type ObjectDetail struct {
  114. Object Object `json:"object"`
  115. PinnedAt []UserSpaceID `json:"pinnedAt"`
  116. Blocks []ObjectBlock `json:"blocks"`
  117. }
  118. func NewObjectDetail(object Object, pinnedAt []UserSpaceID, blocks []ObjectBlock) ObjectDetail {
  119. return ObjectDetail{
  120. Object: object,
  121. PinnedAt: pinnedAt,
  122. Blocks: blocks,
  123. }
  124. }
  125. func DetailsFromObjects(objects []Object) []ObjectDetail {
  126. details := make([]ObjectDetail, len(objects))
  127. for i, object := range objects {
  128. details[i] = ObjectDetail{
  129. Object: object,
  130. }
  131. }
  132. return details
  133. }
  134. // 将blocks放到对应的object中。要求objs和blocks都按ObjectID升序
  135. func DetailsFillObjectBlocks(objs []ObjectDetail, blocks []ObjectBlock) {
  136. blksCur := 0
  137. for i := range objs {
  138. obj := &objs[i]
  139. // 1. 查询Object和ObjectBlock时均按照ObjectID升序排序
  140. // 2. ObjectBlock结果集中的不同ObjectID数只会比Object结果集的少
  141. // 因此在两个结果集上同时从头开始遍历时,如果两边的ObjectID字段不同,那么一定是ObjectBlock这边的ObjectID > Object的ObjectID,
  142. // 此时让Object的遍历游标前进,直到两边的ObjectID再次相等
  143. for ; blksCur < len(blocks); blksCur++ {
  144. if blocks[blksCur].ObjectID != obj.Object.ObjectID {
  145. break
  146. }
  147. obj.Blocks = append(obj.Blocks, blocks[blksCur])
  148. }
  149. }
  150. }
  151. // 将pinnedAt放到对应的object中。要求objs和pinnedAt都按ObjectID升序
  152. func DetailsFillPinnedAt(objs []ObjectDetail, pinnedAt []PinnedObject) {
  153. pinnedCur := 0
  154. for i := range objs {
  155. obj := &objs[i]
  156. for ; pinnedCur < len(pinnedAt); pinnedCur++ {
  157. if pinnedAt[pinnedCur].ObjectID != obj.Object.ObjectID {
  158. break
  159. }
  160. obj.PinnedAt = append(obj.PinnedAt, pinnedAt[pinnedCur].UserSpaceID)
  161. }
  162. }
  163. }
  164. type GrouppedObjectBlock struct {
  165. ObjectID ObjectID
  166. Index int
  167. FileHash FileHash
  168. Size int64
  169. UserSpaceIDs []UserSpaceID
  170. }
  171. func (o *ObjectDetail) GroupBlocks() []GrouppedObjectBlock {
  172. grps := make(map[int]GrouppedObjectBlock)
  173. for _, block := range o.Blocks {
  174. grp, ok := grps[block.Index]
  175. if !ok {
  176. grp = GrouppedObjectBlock{
  177. ObjectID: block.ObjectID,
  178. Index: block.Index,
  179. FileHash: block.FileHash,
  180. Size: block.Size,
  181. }
  182. }
  183. grp.UserSpaceIDs = append(grp.UserSpaceIDs, block.UserSpaceID)
  184. grps[block.Index] = grp
  185. }
  186. return sort2.Sort(lo.Values(grps), func(l, r GrouppedObjectBlock) int { return l.Index - r.Index })
  187. }
  188. func (o *ObjectDetail) ContainsBlock(idx int, userSpaceID UserSpaceID) bool {
  189. for _, block := range o.Blocks {
  190. if block.Index == idx && block.UserSpaceID == userSpaceID {
  191. return true
  192. }
  193. }
  194. return false
  195. }
  196. func (o *ObjectDetail) ContainsPinned(userSpaceID UserSpaceID) bool {
  197. for _, spaceID := range o.PinnedAt {
  198. if spaceID == userSpaceID {
  199. return true
  200. }
  201. }
  202. return false
  203. }
  204. type UserSpaceDetail struct {
  205. UserID UserID
  206. UserSpace UserSpace
  207. RecommendHub *Hub
  208. }
  209. func (d UserSpaceDetail) String() string {
  210. return d.UserSpace.String()
  211. }
  212. type PackageDetail struct {
  213. Package Package
  214. ObjectCount int64
  215. TotalSize int64
  216. }
  217. type SpaceToSpaceResult struct {
  218. Success []string `json:"success"`
  219. Failed []string `json:"failed"`
  220. }

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