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.

types.go 9.0 kB

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

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