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.

models.go 10 kB

11 months ago
11 months ago
11 months ago
11 months ago
10 months ago
1 year ago
1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. package cdssdk
  2. import (
  3. "database/sql/driver"
  4. "fmt"
  5. "time"
  6. "github.com/samber/lo"
  7. "gitlink.org.cn/cloudream/common/pkgs/types"
  8. "gitlink.org.cn/cloudream/common/utils/math2"
  9. "gitlink.org.cn/cloudream/common/utils/serder"
  10. )
  11. const (
  12. ObjectPathSeparator = "/"
  13. )
  14. type HubID int64
  15. type PackageID int64
  16. type ObjectID int64
  17. type UserID int64
  18. type BucketID int64
  19. type StorageID int64
  20. type LocationID int64
  21. /// TODO 将分散在各处的公共结构体定义集中到这里来
  22. type Redundancy interface {
  23. }
  24. var RedundancyUnion = serder.UseTypeUnionInternallyTagged(types.Ref(types.NewTypeUnion[Redundancy](
  25. (*NoneRedundancy)(nil),
  26. (*RepRedundancy)(nil),
  27. (*ECRedundancy)(nil),
  28. (*LRCRedundancy)(nil),
  29. (*SegmentRedundancy)(nil),
  30. )), "type")
  31. type NoneRedundancy struct {
  32. serder.Metadata `union:"none"`
  33. Type string `json:"type"`
  34. }
  35. func NewNoneRedundancy() *NoneRedundancy {
  36. return &NoneRedundancy{
  37. Type: "none",
  38. }
  39. }
  40. func (b *NoneRedundancy) Value() (driver.Value, error) {
  41. return serder.ObjectToJSONEx[Redundancy](b)
  42. }
  43. var DefaultRepRedundancy = *NewRepRedundancy(2)
  44. type RepRedundancy struct {
  45. serder.Metadata `union:"rep"`
  46. Type string `json:"type"`
  47. RepCount int `json:"repCount"`
  48. }
  49. func NewRepRedundancy(repCount int) *RepRedundancy {
  50. return &RepRedundancy{
  51. Type: "rep",
  52. RepCount: repCount,
  53. }
  54. }
  55. func (b *RepRedundancy) Value() (driver.Value, error) {
  56. return serder.ObjectToJSONEx[Redundancy](b)
  57. }
  58. var DefaultECRedundancy = *NewECRedundancy(2, 3, 1024*1024*5)
  59. type ECRedundancy struct {
  60. serder.Metadata `union:"ec"`
  61. Type string `json:"type"`
  62. K int `json:"k"`
  63. N int `json:"n"`
  64. ChunkSize int `json:"chunkSize"`
  65. }
  66. func NewECRedundancy(k int, n int, chunkSize int) *ECRedundancy {
  67. return &ECRedundancy{
  68. Type: "ec",
  69. K: k,
  70. N: n,
  71. ChunkSize: chunkSize,
  72. }
  73. }
  74. func (b *ECRedundancy) Value() (driver.Value, error) {
  75. return serder.ObjectToJSONEx[Redundancy](b)
  76. }
  77. func (b *ECRedundancy) StripSize() int64 {
  78. return int64(b.ChunkSize) * int64(b.K)
  79. }
  80. var DefaultLRCRedundancy = *NewLRCRedundancy(2, 4, []int{2}, 1024*1024*5)
  81. type LRCRedundancy struct {
  82. serder.Metadata `union:"lrc"`
  83. Type string `json:"type"`
  84. K int `json:"k"`
  85. N int `json:"n"`
  86. Groups []int `json:"groups"`
  87. ChunkSize int `json:"chunkSize"`
  88. }
  89. func NewLRCRedundancy(k int, n int, groups []int, chunkSize int) *LRCRedundancy {
  90. return &LRCRedundancy{
  91. Type: "lrc",
  92. K: k,
  93. N: n,
  94. Groups: groups,
  95. ChunkSize: chunkSize,
  96. }
  97. }
  98. func (b *LRCRedundancy) Value() (driver.Value, error) {
  99. return serder.ObjectToJSONEx[Redundancy](b)
  100. }
  101. // 判断指定块属于哪个组。如果都不属于,则返回-1。
  102. func (b *LRCRedundancy) FindGroup(idx int) int {
  103. if idx >= b.N-len(b.Groups) {
  104. return idx - (b.N - len(b.Groups))
  105. }
  106. for i, group := range b.Groups {
  107. if idx < group {
  108. return i
  109. }
  110. idx -= group
  111. }
  112. return -1
  113. }
  114. // M = N - len(Groups),即数据块+校验块的总数,不包括组校验块。
  115. func (b *LRCRedundancy) M() int {
  116. return b.N - len(b.Groups)
  117. }
  118. func (b *LRCRedundancy) GetGroupElements(grp int) []int {
  119. var idxes []int
  120. grpStart := 0
  121. for i := 0; i < grp; i++ {
  122. grpStart += b.Groups[i]
  123. }
  124. for i := 0; i < b.Groups[grp]; i++ {
  125. idxes = append(idxes, grpStart+i)
  126. }
  127. idxes = append(idxes, b.N-len(b.Groups)+grp)
  128. return idxes
  129. }
  130. type SegmentRedundancy struct {
  131. serder.Metadata `union:"segment"`
  132. Type string `json:"type"`
  133. Segments []int64 `json:"segments"` // 每一段的大小
  134. }
  135. func NewSegmentRedundancy(totalSize int64, segmentCount int) *SegmentRedundancy {
  136. return &SegmentRedundancy{
  137. Type: "segment",
  138. Segments: math2.SplitN(totalSize, segmentCount),
  139. }
  140. }
  141. func (r *SegmentRedundancy) SegmentCount() int {
  142. return len(r.Segments)
  143. }
  144. func (r *SegmentRedundancy) CalcSegmentStart(index int) int64 {
  145. return lo.Sum(r.Segments[:index])
  146. }
  147. // 计算指定位置取整到最近的段的起始位置。
  148. func (r *SegmentRedundancy) FloorSegmentPosition(pos int64) int64 {
  149. fpos := int64(0)
  150. for _, segLen := range r.Segments {
  151. segEnd := fpos + segLen
  152. if pos < segEnd {
  153. break
  154. }
  155. fpos += segLen
  156. }
  157. return fpos
  158. }
  159. // 计算指定范围内的段索引范围,参数和返回值所代表的范围都是左闭右开的。
  160. // 如果end == -1,则代表计算从start到最后一个字节的范围。
  161. func (b *SegmentRedundancy) CalcSegmentRange(start int64, end *int64) (segIdxStart int, segIdxEnd int) {
  162. segIdxStart = len(b.Segments)
  163. segIdxEnd = len(b.Segments)
  164. // 找到第一个包含start的段索引
  165. segStart := int64(0)
  166. for i, segLen := range b.Segments {
  167. segEnd := segStart + segLen
  168. if start < segEnd {
  169. segIdxStart = i
  170. break
  171. }
  172. segStart += segLen
  173. }
  174. if end != nil {
  175. // 找到第一个包含end的段索引
  176. segStart = int64(0)
  177. for i, segLen := range b.Segments {
  178. segEnd := segStart + segLen
  179. if *end <= segEnd {
  180. segIdxEnd = i + 1
  181. break
  182. }
  183. segStart += segLen
  184. }
  185. }
  186. return
  187. }
  188. type User struct {
  189. UserID UserID `gorm:"column:UserID; primaryKey; type:bigint" json:"userID"`
  190. Name string `gorm:"column:Name; type:varchar(255); not null" json:"name"`
  191. Password string `gorm:"column:Password; type:varchar(255); not null" json:"password"`
  192. }
  193. func (User) TableName() string {
  194. return "User"
  195. }
  196. const (
  197. PackageStateNormal = "Normal"
  198. PackageStateDeleted = "Deleted"
  199. )
  200. type Package struct {
  201. PackageID PackageID `gorm:"column:PackageID; primaryKey; type:bigint; autoIncrement" json:"packageID"`
  202. Name string `gorm:"column:Name; type:varchar(255); not null" json:"name"`
  203. BucketID BucketID `gorm:"column:BucketID; type:bigint; not null" json:"bucketID"`
  204. State string `gorm:"column:State; type:varchar(255); not null" json:"state"`
  205. }
  206. func (Package) TableName() string {
  207. return "Package"
  208. }
  209. type Object struct {
  210. ObjectID ObjectID `json:"objectID" gorm:"column:ObjectID; primaryKey; type:bigint; autoIncrement" `
  211. PackageID PackageID `json:"packageID" gorm:"column:PackageID; type:bigint; not null"`
  212. Path string `json:"path" gorm:"column:Path; type:varchar(1024); not null"`
  213. Size int64 `json:"size,string" gorm:"column:Size; type:bigint; not null"`
  214. FileHash FileHash `json:"fileHash" gorm:"column:FileHash; type:char(68); not null"`
  215. Redundancy Redundancy `json:"redundancy" gorm:"column:Redundancy; type: json; serializer:union"`
  216. CreateTime time.Time `json:"createTime" gorm:"column:CreateTime; type:datetime; not null"`
  217. UpdateTime time.Time `json:"updateTime" gorm:"column:UpdateTime; type:datetime; not null"`
  218. }
  219. func (Object) TableName() string {
  220. return "Object"
  221. }
  222. type Hub struct {
  223. HubID HubID `gorm:"column:HubID; primaryKey; type:bigint; autoIncrement" json:"hubID"`
  224. Name string `gorm:"column:Name; type:varchar(255); not null" json:"name"`
  225. Address HubAddressInfo `gorm:"column:Address; type:json; serializer:union" json:"address"`
  226. LocationID LocationID `gorm:"column:LocationID; type:bigint; not null" json:"locationID"`
  227. State string `gorm:"column:State; type:varchar(255); not null" json:"state"`
  228. LastReportTime *time.Time `gorm:"column:LastReportTime; type:datetime" json:"lastReportTime"`
  229. }
  230. func (Hub) TableName() string {
  231. return "Hub"
  232. }
  233. type HubAddressInfo interface {
  234. }
  235. var HubAddressUnion = types.NewTypeUnion[HubAddressInfo](
  236. (*GRPCAddressInfo)(nil),
  237. (*HttpAddressInfo)(nil),
  238. )
  239. var _ = serder.UseTypeUnionInternallyTagged(&HubAddressUnion, "type")
  240. type GRPCAddressInfo struct {
  241. serder.Metadata `union:"GRPC"`
  242. Type string `json:"type"`
  243. LocalIP string `json:"localIP"`
  244. ExternalIP string `json:"externalIP"`
  245. LocalGRPCPort int `json:"localGRPCPort"`
  246. ExternalGRPCPort int `json:"externalGRPCPort"`
  247. }
  248. type HttpAddressInfo struct {
  249. serder.Metadata `union:"HTTP"`
  250. Type string `json:"type"`
  251. LocalIP string `json:"localIP"`
  252. ExternalIP string `json:"externalIP"`
  253. Port int `json:"port"`
  254. }
  255. func (n Hub) String() string {
  256. return fmt.Sprintf("%v(%v)", n.Name, n.HubID)
  257. }
  258. type PinnedObject struct {
  259. ObjectID ObjectID `gorm:"column:ObjectID; primaryKey; type:bigint" json:"objectID"`
  260. StorageID StorageID `gorm:"column:StorageID; primaryKey; type:bigint" json:"storageID"`
  261. CreateTime time.Time `gorm:"column:CreateTime; type:datetime; not null" json:"createTime"`
  262. }
  263. func (PinnedObject) TableName() string {
  264. return "PinnedObject"
  265. }
  266. type Bucket struct {
  267. BucketID BucketID `gorm:"column:BucketID; primaryKey; type:bigint; autoIncrement" json:"bucketID"`
  268. Name string `gorm:"column:Name; type:varchar(255); not null" json:"name"`
  269. CreatorID UserID `gorm:"column:CreatorID; type:bigint; not null" json:"creatorID"`
  270. }
  271. func (Bucket) TableName() string {
  272. return "Bucket"
  273. }
  274. type HubConnectivity struct {
  275. FromHubID HubID `gorm:"column:FromHubID; primaryKey; type:bigint" json:"fromHubID"`
  276. ToHubID HubID `gorm:"column:ToHubID; primaryKey; type:bigint" json:"ToHubID"`
  277. Latency *float32 `gorm:"column:Latency; type:float" json:"latency"`
  278. TestTime time.Time `gorm:"column:TestTime; type:datetime" json:"testTime"`
  279. }
  280. func (HubConnectivity) TableName() string {
  281. return "HubConnectivity"
  282. }
  283. type StoragePackageCachingInfo struct {
  284. StorageID StorageID `json:"storageID"`
  285. FileSize int64 `json:"fileSize"`
  286. ObjectCount int64 `json:"objectCount"`
  287. }
  288. type PackageCachingInfo struct {
  289. StorageInfos []StoragePackageCachingInfo `json:"stgInfos"`
  290. PackageSize int64 `json:"packageSize"`
  291. }
  292. func NewPackageCachingInfo(stgInfos []StoragePackageCachingInfo, packageSize int64) PackageCachingInfo {
  293. return PackageCachingInfo{
  294. StorageInfos: stgInfos,
  295. PackageSize: packageSize,
  296. }
  297. }
  298. type CodeError struct {
  299. Code string `json:"code"`
  300. Message string `json:"message"`
  301. }
  302. func (e *CodeError) Error() string {
  303. return fmt.Sprintf("code: %s, message: %s", e.Code, e.Message)
  304. }