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

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. package cdssdk
  2. import (
  3. "database/sql/driver"
  4. "fmt"
  5. "time"
  6. "gitlink.org.cn/cloudream/common/pkgs/types"
  7. "gitlink.org.cn/cloudream/common/utils/serder"
  8. )
  9. const (
  10. ObjectPathSeparator = "/"
  11. )
  12. type NodeID int64
  13. type PackageID int64
  14. type ObjectID int64
  15. type UserID int64
  16. type BucketID int64
  17. type StorageID int64
  18. type LocationID int64
  19. // 文件的SHA256哈希值,全大写的16进制字符串格式
  20. type FileHash string
  21. /// TODO 将分散在各处的公共结构体定义集中到这里来
  22. type Redundancy interface {
  23. driver.Valuer
  24. }
  25. var RedundancyUnion = serder.UseTypeUnionInternallyTagged(types.Ref(types.NewTypeUnion[Redundancy](
  26. (*NoneRedundancy)(nil),
  27. (*RepRedundancy)(nil),
  28. (*ECRedundancy)(nil),
  29. (*LRCRedundancy)(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(3, 6, 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. const (
  131. PackageStateNormal = "Normal"
  132. PackageStateDeleted = "Deleted"
  133. )
  134. type Package struct {
  135. PackageID PackageID `gorm:"column:PackageID; primaryKey; type:bigint; autoIncrement" json:"packageID"`
  136. Name string `gorm:"column:Name; type:varchar(255); not null" json:"name"`
  137. BucketID BucketID `gorm:"column:BucketID; type:bigint; not null" json:"bucketID"`
  138. State string `gorm:"column:State; type:varchar(255); not null" json:"state"`
  139. }
  140. func (Package) TableName() string {
  141. return "Package"
  142. }
  143. type Object struct {
  144. ObjectID ObjectID `json:"objectID" gorm:"column:ObjectID; primaryKey; type:bigint; autoIncrement" `
  145. PackageID PackageID `json:"packageID" gorm:"column:PackageID; type:bigint; not null"`
  146. Path string `json:"path" gorm:"column:Path; type:varchar(1024); not null"`
  147. Size int64 `json:"size,string" gorm:"column:Size; type:bigint; not null"`
  148. FileHash FileHash `json:"fileHash" gorm:"column:FileHash; type:char(64); not null"`
  149. Redundancy Redundancy `json:"redundancy" gorm:"column:Redundancy; type: json; serializer:union"`
  150. CreateTime time.Time `json:"createTime" gorm:"column:CreateTime; type:datetime; not null"`
  151. UpdateTime time.Time `json:"updateTime" gorm:"column:UpdateTime; type:datetime; not null"`
  152. }
  153. func (Object) TableName() string {
  154. return "Object"
  155. }
  156. type Node struct {
  157. NodeID NodeID `gorm:"column:NodeID; primaryKey; type:bigint; autoIncrement" json:"nodeID"`
  158. Name string `gorm:"column:Name; type:varchar(255); not null" json:"name"`
  159. Address NodeAddressInfo `gorm:"column:Address; type:json; serializer:union" json:"address"`
  160. LocationID LocationID `gorm:"column:LocationID; type:bigint; not null" json:"locationID"`
  161. State string `gorm:"column:State; type:varchar(255); not null" json:"state"`
  162. LastReportTime *time.Time `gorm:"column:LastReportTime; type:datetime" json:"lastReportTime"`
  163. }
  164. func (Node) TableName() string {
  165. return "Node"
  166. }
  167. type NodeAddressInfo interface {
  168. }
  169. var NodeAddressUnion = types.NewTypeUnion[NodeAddressInfo](
  170. (*GRPCAddressInfo)(nil),
  171. (*HttpAddressInfo)(nil),
  172. )
  173. var _ = serder.UseTypeUnionInternallyTagged(&NodeAddressUnion, "type")
  174. type GRPCAddressInfo struct {
  175. serder.Metadata `union:"GRPC"`
  176. Type string `json:"type"`
  177. LocalIP string `json:"localIP"`
  178. ExternalIP string `json:"externalIP"`
  179. LocalGRPCPort int `json:"localGRPCPort"`
  180. ExternalGRPCPort int `json:"externalGRPCPort"`
  181. }
  182. type HttpAddressInfo struct {
  183. serder.Metadata `union:"HTTP"`
  184. Type string `json:"type"`
  185. LocalIP string `json:"localIP"`
  186. ExternalIP string `json:"externalIP"`
  187. Port int `json:"port"`
  188. }
  189. func (n Node) String() string {
  190. return fmt.Sprintf("%v(%v)", n.Name, n.NodeID)
  191. }
  192. type PinnedObject struct {
  193. ObjectID ObjectID `gorm:"column:ObjectID; primaryKey; type:bigint" json:"objectID"`
  194. StorageID StorageID `gorm:"column:StorageID; primaryKey; type:bigint" json:"storageID"`
  195. CreateTime time.Time `gorm:"column:CreateTime; type:datetime; not null" json:"createTime"`
  196. }
  197. func (PinnedObject) TableName() string {
  198. return "PinnedObject"
  199. }
  200. type Bucket struct {
  201. BucketID BucketID `gorm:"column:BucketID; primaryKey; type:bigint; autoIncrement" json:"bucketID"`
  202. Name string `gorm:"column:Name; type:varchar(255); not null" json:"name"`
  203. CreatorID UserID `gorm:"column:CreatorID; type:bigint; not null" json:"creatorID"`
  204. }
  205. func (Bucket) TableName() string {
  206. return "Bucket"
  207. }
  208. type NodeConnectivity struct {
  209. FromNodeID NodeID `gorm:"column:FromNodeID; primaryKey; type:bigint" json:"fromNodeID"`
  210. ToNodeID NodeID `gorm:"column:ToNodeID; primaryKey; type:bigint" json:"ToNodeID"`
  211. Delay *float32 `gorm:"column:Delay; type:float" json:"delay"`
  212. TestTime time.Time `gorm:"column:TestTime; type:datetime" json:"testTime"`
  213. }
  214. func (NodeConnectivity) TableName() string {
  215. return "NodeConnectivity"
  216. }
  217. type StoragePackageCachingInfo struct {
  218. StorageID StorageID `json:"storageID"`
  219. FileSize int64 `json:"fileSize"`
  220. ObjectCount int64 `json:"objectCount"`
  221. }
  222. type PackageCachingInfo struct {
  223. StorageInfos []StoragePackageCachingInfo `json:"stgInfos"`
  224. PackageSize int64 `json:"packageSize"`
  225. }
  226. func NewPackageCachingInfo(stgInfos []StoragePackageCachingInfo, packageSize int64) PackageCachingInfo {
  227. return PackageCachingInfo{
  228. StorageInfos: stgInfos,
  229. PackageSize: packageSize,
  230. }
  231. }
  232. type CodeError struct {
  233. Code string `json:"code"`
  234. Message string `json:"message"`
  235. }
  236. func (e *CodeError) Error() string {
  237. return fmt.Sprintf("code: %s, message: %s", e.Code, e.Message)
  238. }