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

1 year ago
1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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(2, 3, 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. var DefaultLRCRedundancy = *NewLRCRedundancy(2, 4, []int{2}, 1024*1024*5)
  76. type LRCRedundancy struct {
  77. serder.Metadata `union:"lrc"`
  78. Type string `json:"type"`
  79. K int `json:"k"`
  80. N int `json:"n"`
  81. Groups []int `json:"groups"`
  82. ChunkSize int `json:"chunkSize"`
  83. }
  84. func NewLRCRedundancy(k int, n int, groups []int, chunkSize int) *LRCRedundancy {
  85. return &LRCRedundancy{
  86. Type: "lrc",
  87. K: k,
  88. N: n,
  89. Groups: groups,
  90. ChunkSize: chunkSize,
  91. }
  92. }
  93. func (b *LRCRedundancy) Value() (driver.Value, error) {
  94. return serder.ObjectToJSONEx[Redundancy](b)
  95. }
  96. // 判断指定块属于哪个组。如果都不属于,则返回-1。
  97. func (b *LRCRedundancy) FindGroup(idx int) int {
  98. if idx >= b.N-len(b.Groups) {
  99. return idx - (b.N - len(b.Groups))
  100. }
  101. for i, group := range b.Groups {
  102. if idx < group {
  103. return i
  104. }
  105. idx -= group
  106. }
  107. return -1
  108. }
  109. // M = N - len(Groups),即数据块+校验块的总数,不包括组校验块。
  110. func (b *LRCRedundancy) M() int {
  111. return b.N - len(b.Groups)
  112. }
  113. func (b *LRCRedundancy) GetGroupElements(grp int) []int {
  114. var idxes []int
  115. grpStart := 0
  116. for i := 0; i < grp; i++ {
  117. grpStart += b.Groups[i]
  118. }
  119. for i := 0; i < b.Groups[grp]; i++ {
  120. idxes = append(idxes, grpStart+i)
  121. }
  122. idxes = append(idxes, b.N-len(b.Groups)+grp)
  123. return idxes
  124. }
  125. const (
  126. PackageStateNormal = "Normal"
  127. PackageStateDeleted = "Deleted"
  128. )
  129. type Package struct {
  130. PackageID PackageID `db:"PackageID" json:"packageID"`
  131. Name string `db:"Name" json:"name"`
  132. BucketID BucketID `db:"BucketID" json:"bucketID"`
  133. State string `db:"State" json:"state"`
  134. }
  135. type Object struct {
  136. ObjectID ObjectID `db:"ObjectID" json:"objectID"`
  137. PackageID PackageID `db:"PackageID" json:"packageID"`
  138. Path string `db:"Path" json:"path"`
  139. Size int64 `db:"Size" json:"size,string"`
  140. FileHash string `db:"FileHash" json:"fileHash"`
  141. Redundancy Redundancy `db:"Redundancy" json:"redundancy"`
  142. CreateTime time.Time `db:"CreateTime" json:"createTime"`
  143. UpdateTime time.Time `db:"UpdateTime" json:"updateTime"`
  144. }
  145. type Node struct {
  146. NodeID NodeID `db:"NodeID" json:"nodeID"`
  147. Name string `db:"Name" json:"name"`
  148. LocalIP string `db:"LocalIP" json:"localIP"`
  149. ExternalIP string `db:"ExternalIP" json:"externalIP"`
  150. LocalGRPCPort int `db:"LocalGRPCPort" json:"localGRPCPort"`
  151. ExternalGRPCPort int `db:"ExternalGRPCPort" json:"externalGRPCPort"`
  152. LocationID LocationID `db:"LocationID" json:"locationID"`
  153. State string `db:"State" json:"state"`
  154. LastReportTime *time.Time `db:"LastReportTime" json:"lastReportTime"`
  155. }
  156. func (n Node) String() string {
  157. return fmt.Sprintf("%v(%v)", n.Name, n.NodeID)
  158. }
  159. type PinnedObject struct {
  160. ObjectID ObjectID `db:"ObjectID" json:"objectID"`
  161. NodeID NodeID `db:"NodeID" json:"nodeID"`
  162. CreateTime time.Time `db:"CreateTime" json:"createTime"`
  163. }
  164. type Bucket struct {
  165. BucketID BucketID `db:"BucketID" json:"bucketID"`
  166. Name string `db:"Name" json:"name"`
  167. CreatorID UserID `db:"CreatorID" json:"creatorID"`
  168. }
  169. type NodeConnectivity struct {
  170. FromNodeID NodeID `db:"FromNodeID" json:"fromNodeID"`
  171. ToNodeID NodeID `db:"ToNodeID" json:"ToNodeID"`
  172. Delay *float32 `db:"Delay" json:"delay"`
  173. TestTime time.Time `db:"TestTime" json:"testTime"`
  174. }
  175. type Storage struct {
  176. StorageID StorageID `db:"StorageID" json:"storageID"`
  177. Name string `db:"Name" json:"name"`
  178. NodeID NodeID `db:"NodeID" json:"nodeID"`
  179. LocalBase string `db:"LocalBase" json:"localBase"` // 存储服务挂载在代理节点的目录
  180. RemoteBase string `db:"RemoteBase" json:"remoteBase"` // 挂载在本地的目录对应存储服务的哪个路径
  181. State string `db:"State" json:"state"`
  182. }
  183. type NodePackageCachingInfo struct {
  184. NodeID NodeID `json:"nodeID"`
  185. FileSize int64 `json:"fileSize"`
  186. ObjectCount int64 `json:"objectCount"`
  187. }
  188. type PackageCachingInfo struct {
  189. NodeInfos []NodePackageCachingInfo `json:"nodeInfos"`
  190. PackageSize int64 `json:"packageSize"`
  191. }
  192. func NewPackageCachingInfo(nodeInfos []NodePackageCachingInfo, packageSize int64) PackageCachingInfo {
  193. return PackageCachingInfo{
  194. NodeInfos: nodeInfos,
  195. PackageSize: packageSize,
  196. }
  197. }
  198. type CodeError struct {
  199. Code string `json:"code"`
  200. Message string `json:"message"`
  201. }
  202. func (e *CodeError) Error() string {
  203. return fmt.Sprintf("code: %s, message: %s", e.Code, e.Message)
  204. }