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 7.7 kB

7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package types
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/samber/lo"
  6. "gitlink.org.cn/cloudream/common/utils/sort2"
  7. cotypes "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. StorageID cotypes.StorageID `gorm:"column:StorageID; type:bigint; not null" json:"storageID"`
  63. // 用户在指定存储节点的凭证信息,比如用户账户,AK/SK等
  64. Credential cotypes.StorageCredential `gorm:"column:Credential; type:json; not null; serializer:union" json:"credential"`
  65. // 用户空间的分片存储配置,如果为空,则表示不使用分片存储
  66. ShardStore *cotypes.ShardStoreUserConfig `gorm:"column:ShardStore; type:json; serializer:json" json:"shardStore"`
  67. // 用户空间信息的版本号,每一次更改都需要更新版本号
  68. Revision int64 `gorm:"column:Revision; type:bigint; not null" json:"revision"`
  69. }
  70. func (UserSpace) TableName() string {
  71. return "UserSpace"
  72. }
  73. func (s UserSpace) String() string {
  74. return fmt.Sprintf("%v[id=%v,storageID=%v,rev=%v]", s.Name, s.UserSpaceID, s.StorageID, s.Revision)
  75. }
  76. type PackageAccessStat struct {
  77. PackageID PackageID `gorm:"column:PackageID; primaryKey; type:bigint" json:"packageID"`
  78. // 发起读取(调度)的用户空间ID
  79. UserSpaceID UserSpaceID `gorm:"column:UserSpaceID; primaryKey; type:bigint" json:"storageID"`
  80. // 前一日的读取量的滑动平均值
  81. Amount float64 `gorm:"column:Amount; type:double" json:"amount"`
  82. // 当日的读取量
  83. Counter float64 `gorm:"column:Counter; type:double" json:"counter"`
  84. }
  85. func (PackageAccessStat) TableName() string {
  86. return "PackageAccessStat"
  87. }
  88. type ObjectAccessStat struct {
  89. ObjectID ObjectID `gorm:"column:ObjectID; primaryKey; type:bigint" json:"objectID"`
  90. // 发起读取(调度)的用户空间ID
  91. UserSpaceID UserSpaceID `gorm:"column:UserSpaceID; primaryKey; type:bigint" json:"userStorageID"`
  92. // 前一日的读取量的滑动平均值
  93. Amount float64 `gorm:"column:Amount; type:float; not null" json:"amount"`
  94. // 当日的读取量
  95. Counter float64 `gorm:"column:Counter; type:float; not null" json:"counter"`
  96. }
  97. func (ObjectAccessStat) TableName() string {
  98. return "ObjectAccessStat"
  99. }
  100. type PinnedObject struct {
  101. ObjectID ObjectID `gorm:"column:ObjectID; primaryKey; type:bigint" json:"objectID"`
  102. UserSpaceID UserSpaceID `gorm:"column:UserSpaceID; primaryKey; type:bigint" json:"userSpaceID"`
  103. CreateTime time.Time `gorm:"column:CreateTime; type:datetime; not null" json:"createTime"`
  104. }
  105. func (PinnedObject) TableName() string {
  106. return "PinnedObject"
  107. }
  108. type ObjectDetail struct {
  109. Object Object `json:"object"`
  110. PinnedAt []UserSpaceID `json:"pinnedAt"`
  111. Blocks []ObjectBlock `json:"blocks"`
  112. }
  113. func NewObjectDetail(object Object, pinnedAt []UserSpaceID, blocks []ObjectBlock) ObjectDetail {
  114. return ObjectDetail{
  115. Object: object,
  116. PinnedAt: pinnedAt,
  117. Blocks: blocks,
  118. }
  119. }
  120. func DetailsFromObjects(objects []Object) []ObjectDetail {
  121. details := make([]ObjectDetail, len(objects))
  122. for i, object := range objects {
  123. details[i] = ObjectDetail{
  124. Object: object,
  125. }
  126. }
  127. return details
  128. }
  129. // 将blocks放到对应的object中。要求objs和blocks都按ObjectID升序
  130. func DetailsFillObjectBlocks(objs []ObjectDetail, blocks []ObjectBlock) {
  131. blksCur := 0
  132. for i := range objs {
  133. obj := &objs[i]
  134. // 1. 查询Object和ObjectBlock时均按照ObjectID升序排序
  135. // 2. ObjectBlock结果集中的不同ObjectID数只会比Object结果集的少
  136. // 因此在两个结果集上同时从头开始遍历时,如果两边的ObjectID字段不同,那么一定是ObjectBlock这边的ObjectID > Object的ObjectID,
  137. // 此时让Object的遍历游标前进,直到两边的ObjectID再次相等
  138. for ; blksCur < len(blocks); blksCur++ {
  139. if blocks[blksCur].ObjectID != obj.Object.ObjectID {
  140. break
  141. }
  142. obj.Blocks = append(obj.Blocks, blocks[blksCur])
  143. }
  144. }
  145. }
  146. // 将pinnedAt放到对应的object中。要求objs和pinnedAt都按ObjectID升序
  147. func DetailsFillPinnedAt(objs []ObjectDetail, pinnedAt []PinnedObject) {
  148. pinnedCur := 0
  149. for i := range objs {
  150. obj := &objs[i]
  151. for ; pinnedCur < len(pinnedAt); pinnedCur++ {
  152. if pinnedAt[pinnedCur].ObjectID != obj.Object.ObjectID {
  153. break
  154. }
  155. obj.PinnedAt = append(obj.PinnedAt, pinnedAt[pinnedCur].UserSpaceID)
  156. }
  157. }
  158. }
  159. type GrouppedObjectBlock struct {
  160. ObjectID ObjectID
  161. Index int
  162. FileHash FileHash
  163. Size int64
  164. UserSpaceIDs []UserSpaceID
  165. }
  166. func (o *ObjectDetail) GroupBlocks() []GrouppedObjectBlock {
  167. grps := make(map[int]GrouppedObjectBlock)
  168. for _, block := range o.Blocks {
  169. grp, ok := grps[block.Index]
  170. if !ok {
  171. grp = GrouppedObjectBlock{
  172. ObjectID: block.ObjectID,
  173. Index: block.Index,
  174. FileHash: block.FileHash,
  175. Size: block.Size,
  176. }
  177. }
  178. grp.UserSpaceIDs = append(grp.UserSpaceIDs, block.UserSpaceID)
  179. grps[block.Index] = grp
  180. }
  181. return sort2.Sort(lo.Values(grps), func(l, r GrouppedObjectBlock) int { return l.Index - r.Index })
  182. }
  183. type UserSpaceDetail struct {
  184. UserID cotypes.UserID
  185. UserSpace UserSpace
  186. Storage cotypes.Storage
  187. MasterHub *cotypes.Hub
  188. }
  189. func (d UserSpaceDetail) String() string {
  190. return d.UserSpace.String()
  191. }

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