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

7 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package types
  2. import (
  3. "time"
  4. "github.com/samber/lo"
  5. "gitlink.org.cn/cloudream/common/utils/sort2"
  6. cotypes "gitlink.org.cn/cloudream/storage2/coordinator/types"
  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. StorageID cotypes.StorageID `gorm:"column:StorageID; type:bigint; not null" json:"storageID"`
  60. }
  61. func (UserSpace) TableName() string {
  62. return "UserSpace"
  63. }
  64. type PackageAccessStat struct {
  65. PackageID PackageID `gorm:"column:PackageID; primaryKey; type:bigint" json:"packageID"`
  66. // 发起读取(调度)的用户空间ID
  67. UserSpaceID UserSpaceID `gorm:"column:UserSpaceID; primaryKey; type:bigint" json:"storageID"`
  68. // 前一日的读取量的滑动平均值
  69. Amount float64 `gorm:"column:Amount; type:double" json:"amount"`
  70. // 当日的读取量
  71. Counter float64 `gorm:"column:Counter; type:double" json:"counter"`
  72. }
  73. func (PackageAccessStat) TableName() string {
  74. return "PackageAccessStat"
  75. }
  76. type ObjectAccessStat struct {
  77. ObjectID ObjectID `gorm:"column:ObjectID; primaryKey; type:bigint" json:"objectID"`
  78. // 发起读取(调度)的用户空间ID
  79. UserSpaceID UserSpaceID `gorm:"column:UserSpaceID; primaryKey; type:bigint" json:"userStorageID"`
  80. // 前一日的读取量的滑动平均值
  81. Amount float64 `gorm:"column:Amount; type:float; not null" json:"amount"`
  82. // 当日的读取量
  83. Counter float64 `gorm:"column:Counter; type:float; not null" json:"counter"`
  84. }
  85. func (ObjectAccessStat) TableName() string {
  86. return "ObjectAccessStat"
  87. }
  88. type PinnedObject struct {
  89. ObjectID ObjectID `gorm:"column:ObjectID; primaryKey; type:bigint" json:"objectID"`
  90. UserSpaceID UserSpaceID `gorm:"column:UserSpaceID; primaryKey; type:bigint" json:"userSpaceID"`
  91. CreateTime time.Time `gorm:"column:CreateTime; type:datetime; not null" json:"createTime"`
  92. }
  93. func (PinnedObject) TableName() string {
  94. return "PinnedObject"
  95. }
  96. type ObjectDetail struct {
  97. Object Object `json:"object"`
  98. PinnedAt []UserSpaceID `json:"pinnedAt"`
  99. Blocks []ObjectBlock `json:"blocks"`
  100. }
  101. func NewObjectDetail(object Object, pinnedAt []UserSpaceID, blocks []ObjectBlock) ObjectDetail {
  102. return ObjectDetail{
  103. Object: object,
  104. PinnedAt: pinnedAt,
  105. Blocks: blocks,
  106. }
  107. }
  108. func DetailsFromObjects(objects []Object) []ObjectDetail {
  109. details := make([]ObjectDetail, len(objects))
  110. for i, object := range objects {
  111. details[i] = ObjectDetail{
  112. Object: object,
  113. }
  114. }
  115. return details
  116. }
  117. // 将blocks放到对应的object中。要求objs和blocks都按ObjectID升序
  118. func DetailsFillObjectBlocks(objs []ObjectDetail, blocks []ObjectBlock) {
  119. blksCur := 0
  120. for i := range objs {
  121. obj := &objs[i]
  122. // 1. 查询Object和ObjectBlock时均按照ObjectID升序排序
  123. // 2. ObjectBlock结果集中的不同ObjectID数只会比Object结果集的少
  124. // 因此在两个结果集上同时从头开始遍历时,如果两边的ObjectID字段不同,那么一定是ObjectBlock这边的ObjectID > Object的ObjectID,
  125. // 此时让Object的遍历游标前进,直到两边的ObjectID再次相等
  126. for ; blksCur < len(blocks); blksCur++ {
  127. if blocks[blksCur].ObjectID != obj.Object.ObjectID {
  128. break
  129. }
  130. obj.Blocks = append(obj.Blocks, blocks[blksCur])
  131. }
  132. }
  133. }
  134. // 将pinnedAt放到对应的object中。要求objs和pinnedAt都按ObjectID升序
  135. func DetailsFillPinnedAt(objs []ObjectDetail, pinnedAt []PinnedObject) {
  136. pinnedCur := 0
  137. for i := range objs {
  138. obj := &objs[i]
  139. for ; pinnedCur < len(pinnedAt); pinnedCur++ {
  140. if pinnedAt[pinnedCur].ObjectID != obj.Object.ObjectID {
  141. break
  142. }
  143. obj.PinnedAt = append(obj.PinnedAt, pinnedAt[pinnedCur].UserSpaceID)
  144. }
  145. }
  146. }
  147. type GrouppedObjectBlock struct {
  148. ObjectID ObjectID
  149. Index int
  150. FileHash FileHash
  151. Size int64
  152. UserSpaceIDs []UserSpaceID
  153. }
  154. func (o *ObjectDetail) GroupBlocks() []GrouppedObjectBlock {
  155. grps := make(map[int]GrouppedObjectBlock)
  156. for _, block := range o.Blocks {
  157. grp, ok := grps[block.Index]
  158. if !ok {
  159. grp = GrouppedObjectBlock{
  160. ObjectID: block.ObjectID,
  161. Index: block.Index,
  162. FileHash: block.FileHash,
  163. Size: block.Size,
  164. }
  165. }
  166. grp.UserSpaceIDs = append(grp.UserSpaceIDs, block.UserSpaceID)
  167. grps[block.Index] = grp
  168. }
  169. return sort2.Sort(lo.Values(grps), func(l, r GrouppedObjectBlock) int { return l.Index - r.Index })
  170. }

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