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

1 year ago
1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. /// TODO 将分散在各处的公共结构体定义集中到这里来
  20. type Redundancy interface {
  21. driver.Valuer
  22. }
  23. var RedundancyUnion = serder.UseTypeUnionInternallyTagged(types.Ref(types.NewTypeUnion[Redundancy](
  24. (*NoneRedundancy)(nil),
  25. (*RepRedundancy)(nil),
  26. (*ECRedundancy)(nil),
  27. (*LRCRedundancy)(nil),
  28. )), "type")
  29. type NoneRedundancy struct {
  30. serder.Metadata `union:"none"`
  31. Type string `json:"type"`
  32. }
  33. func NewNoneRedundancy() *NoneRedundancy {
  34. return &NoneRedundancy{
  35. Type: "none",
  36. }
  37. }
  38. func (b *NoneRedundancy) Value() (driver.Value, error) {
  39. return serder.ObjectToJSONEx[Redundancy](b)
  40. }
  41. var DefaultRepRedundancy = *NewRepRedundancy(2)
  42. type RepRedundancy struct {
  43. serder.Metadata `union:"rep"`
  44. Type string `json:"type"`
  45. RepCount int `json:"repCount"`
  46. }
  47. func NewRepRedundancy(repCount int) *RepRedundancy {
  48. return &RepRedundancy{
  49. Type: "rep",
  50. RepCount: repCount,
  51. }
  52. }
  53. func (b *RepRedundancy) Value() (driver.Value, error) {
  54. return serder.ObjectToJSONEx[Redundancy](b)
  55. }
  56. var DefaultECRedundancy = *NewECRedundancy(3, 6, 1024*1024*5)
  57. type ECRedundancy struct {
  58. serder.Metadata `union:"ec"`
  59. Type string `json:"type"`
  60. K int `json:"k"`
  61. N int `json:"n"`
  62. ChunkSize int `json:"chunkSize"`
  63. }
  64. func NewECRedundancy(k int, n int, chunkSize int) *ECRedundancy {
  65. return &ECRedundancy{
  66. Type: "ec",
  67. K: k,
  68. N: n,
  69. ChunkSize: chunkSize,
  70. }
  71. }
  72. func (b *ECRedundancy) Value() (driver.Value, error) {
  73. return serder.ObjectToJSONEx[Redundancy](b)
  74. }
  75. func (b *ECRedundancy) StripSize() int64 {
  76. return int64(b.ChunkSize) * int64(b.K)
  77. }
  78. var DefaultLRCRedundancy = *NewLRCRedundancy(2, 4, []int{2}, 1024*1024*5)
  79. type LRCRedundancy struct {
  80. serder.Metadata `union:"lrc"`
  81. Type string `json:"type"`
  82. K int `json:"k"`
  83. N int `json:"n"`
  84. Groups []int `json:"groups"`
  85. ChunkSize int `json:"chunkSize"`
  86. }
  87. func NewLRCRedundancy(k int, n int, groups []int, chunkSize int) *LRCRedundancy {
  88. return &LRCRedundancy{
  89. Type: "lrc",
  90. K: k,
  91. N: n,
  92. Groups: groups,
  93. ChunkSize: chunkSize,
  94. }
  95. }
  96. func (b *LRCRedundancy) Value() (driver.Value, error) {
  97. return serder.ObjectToJSONEx[Redundancy](b)
  98. }
  99. // 判断指定块属于哪个组。如果都不属于,则返回-1。
  100. func (b *LRCRedundancy) FindGroup(idx int) int {
  101. if idx >= b.N-len(b.Groups) {
  102. return idx - (b.N - len(b.Groups))
  103. }
  104. for i, group := range b.Groups {
  105. if idx < group {
  106. return i
  107. }
  108. idx -= group
  109. }
  110. return -1
  111. }
  112. // M = N - len(Groups),即数据块+校验块的总数,不包括组校验块。
  113. func (b *LRCRedundancy) M() int {
  114. return b.N - len(b.Groups)
  115. }
  116. func (b *LRCRedundancy) GetGroupElements(grp int) []int {
  117. var idxes []int
  118. grpStart := 0
  119. for i := 0; i < grp; i++ {
  120. grpStart += b.Groups[i]
  121. }
  122. for i := 0; i < b.Groups[grp]; i++ {
  123. idxes = append(idxes, grpStart+i)
  124. }
  125. idxes = append(idxes, b.N-len(b.Groups)+grp)
  126. return idxes
  127. }
  128. const (
  129. PackageStateNormal = "Normal"
  130. PackageStateDeleted = "Deleted"
  131. )
  132. type Package struct {
  133. PackageID PackageID `db:"PackageID" json:"packageID"`
  134. Name string `db:"Name" json:"name"`
  135. BucketID BucketID `db:"BucketID" json:"bucketID"`
  136. State string `db:"State" json:"state"`
  137. }
  138. type Object struct {
  139. ObjectID ObjectID `db:"ObjectID" json:"objectID"`
  140. PackageID PackageID `db:"PackageID" json:"packageID"`
  141. Path string `db:"Path" json:"path"`
  142. Size int64 `db:"Size" json:"size,string"`
  143. FileHash string `db:"FileHash" json:"fileHash"`
  144. Redundancy Redundancy `db:"Redundancy" json:"redundancy"`
  145. CreateTime time.Time `db:"CreateTime" json:"createTime"`
  146. UpdateTime time.Time `db:"UpdateTime" json:"updateTime"`
  147. }
  148. type Node struct {
  149. NodeID NodeID `gorm:"column:NodeID;type:varchar(255)" json:"nodeID"`
  150. Name string `gorm:"column:Name;type:varchar(255)" json:"name"`
  151. LocalIP string `gorm:"column:LocalIP;type:varchar(255)" json:"localIP"`
  152. ExternalIP string `gorm:"column:ExternalIP;type:varchar(255)" json:"externalIP"`
  153. LocalGRPCPort int `gorm:"column:LocalGRPCPort;type:int" json:"localGRPCPort"`
  154. ExternalGRPCPort int `gorm:"column:ExternalGRPCPort;type:int" json:"externalGRPCPort"`
  155. LocationID LocationID `gorm:"column:LocationID;type:varchar(255)" json:"locationID"`
  156. State string `gorm:"column:State;type:varchar(255)" json:"state"`
  157. LastReportTime *time.Time `gorm:"column:LastReportTime;type:timestamp" json:"lastReportTime"`
  158. Address NodeAddressInfo `gorm:"column:Address;type:json;serializer:union" json:"address"`
  159. }
  160. type NodeAddressInfo interface {
  161. }
  162. var NodeAddressUnion = types.NewTypeUnion[NodeAddressInfo](
  163. (*GRPCAddressInfo)(nil),
  164. (*HttpAddressInfo)(nil),
  165. )
  166. var _ = serder.UseTypeUnionInternallyTagged(&NodeAddressUnion, "type")
  167. type GRPCAddressInfo struct {
  168. serder.Metadata `union:"GRPC"`
  169. Type string `json:"type"`
  170. LocalIP string `json:"localIP"`
  171. ExternalIP string `json:"externalIP"`
  172. LocalGRPCPort int `json:"localGRPCPort"`
  173. ExternalGRPCPort int `json:"externalGRPCPort"`
  174. }
  175. type HttpAddressInfo struct {
  176. serder.Metadata `union:"HTTP"`
  177. Type string `json:"type"`
  178. LocalIP string `json:"localIP"`
  179. ExternalIP string `json:"externalIP"`
  180. Port int `json:"port"`
  181. }
  182. func (n Node) String() string {
  183. return fmt.Sprintf("%v(%v)", n.Name, n.NodeID)
  184. }
  185. type PinnedObject struct {
  186. ObjectID ObjectID `gorm:"column:ObjectID; primaryKey" json:"objectID"`
  187. StorageID StorageID `gorm:"column:StorageID; primaryKey" json:"storageID"`
  188. CreateTime time.Time `gorm:"column:CreateTime" json:"createTime"`
  189. }
  190. type Bucket struct {
  191. BucketID BucketID `db:"BucketID" json:"bucketID"`
  192. Name string `db:"Name" json:"name"`
  193. CreatorID UserID `db:"CreatorID" json:"creatorID"`
  194. }
  195. type NodeConnectivity struct {
  196. FromNodeID NodeID `db:"FromNodeID" json:"fromNodeID"`
  197. ToNodeID NodeID `db:"ToNodeID" json:"ToNodeID"`
  198. Delay *float32 `db:"Delay" json:"delay"`
  199. TestTime time.Time `db:"TestTime" json:"testTime"`
  200. }
  201. type NodePackageCachingInfo struct {
  202. NodeID NodeID `json:"nodeID"`
  203. FileSize int64 `json:"fileSize"`
  204. ObjectCount int64 `json:"objectCount"`
  205. }
  206. type PackageCachingInfo struct {
  207. NodeInfos []NodePackageCachingInfo `json:"nodeInfos"`
  208. PackageSize int64 `json:"packageSize"`
  209. }
  210. func NewPackageCachingInfo(nodeInfos []NodePackageCachingInfo, packageSize int64) PackageCachingInfo {
  211. return PackageCachingInfo{
  212. NodeInfos: nodeInfos,
  213. PackageSize: packageSize,
  214. }
  215. }
  216. type CodeError struct {
  217. Code string `json:"code"`
  218. Message string `json:"message"`
  219. }
  220. func (e *CodeError) Error() string {
  221. return fmt.Sprintf("code: %s, message: %s", e.Code, e.Message)
  222. }