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

1 year ago
1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package cdssdk
  2. import (
  3. "database/sql/driver"
  4. "fmt"
  5. "gitlink.org.cn/cloudream/common/pkgs/types"
  6. "gitlink.org.cn/cloudream/common/utils/serder"
  7. )
  8. const (
  9. ObjectPathSeperator = "/"
  10. )
  11. type NodeID int64
  12. type PackageID int64
  13. type ObjectID int64
  14. type UserID int64
  15. type BucketID int64
  16. type StorageID int64
  17. type LocationID int64
  18. /// TODO 将分散在各处的公共结构体定义集中到这里来
  19. type Redundancy interface {
  20. driver.Valuer
  21. }
  22. var RedundancyUnion = serder.UseTypeUnionInternallyTagged(types.Ref(types.NewTypeUnion[Redundancy](
  23. (*NoneRedundancy)(nil),
  24. (*RepRedundancy)(nil),
  25. (*ECRedundancy)(nil),
  26. )), "type")
  27. type NoneRedundancy struct {
  28. serder.Metadata `union:"none"`
  29. Type string `json:"type"`
  30. }
  31. func NewNoneRedundancy() *NoneRedundancy {
  32. return &NoneRedundancy{
  33. Type: "none",
  34. }
  35. }
  36. func (b *NoneRedundancy) Value() (driver.Value, error) {
  37. return serder.ObjectToJSONEx[Redundancy](b)
  38. }
  39. var DefaultRepRedundancy = *NewRepRedundancy(2)
  40. type RepRedundancy struct {
  41. serder.Metadata `union:"rep"`
  42. Type string `json:"type"`
  43. RepCount int `json:"repCount"`
  44. }
  45. func NewRepRedundancy(repCount int) *RepRedundancy {
  46. return &RepRedundancy{
  47. Type: "rep",
  48. RepCount: repCount,
  49. }
  50. }
  51. func (b *RepRedundancy) Value() (driver.Value, error) {
  52. return serder.ObjectToJSONEx[Redundancy](b)
  53. }
  54. var DefaultECRedundancy = *NewECRedundancy(2, 3, 1024*1024*5)
  55. type ECRedundancy struct {
  56. serder.Metadata `union:"ec"`
  57. Type string `json:"type"`
  58. K int `json:"k"`
  59. N int `json:"n"`
  60. ChunkSize int `json:"chunkSize"`
  61. }
  62. func NewECRedundancy(k int, n int, chunkSize int) *ECRedundancy {
  63. return &ECRedundancy{
  64. Type: "ec",
  65. K: k,
  66. N: n,
  67. ChunkSize: chunkSize,
  68. }
  69. }
  70. func (b *ECRedundancy) Value() (driver.Value, error) {
  71. return serder.ObjectToJSONEx[Redundancy](b)
  72. }
  73. const (
  74. PackageStateNormal = "Normal"
  75. PackageStateDeleted = "Deleted"
  76. )
  77. type Package struct {
  78. PackageID PackageID `db:"PackageID" json:"packageID"`
  79. Name string `db:"Name" json:"name"`
  80. BucketID BucketID `db:"BucketID" json:"bucketID"`
  81. State string `db:"State" json:"state"`
  82. }
  83. type Object struct {
  84. ObjectID ObjectID `db:"ObjectID" json:"objectID"`
  85. PackageID PackageID `db:"PackageID" json:"packageID"`
  86. Path string `db:"Path" json:"path"`
  87. Size int64 `db:"Size" json:"size,string"`
  88. FileHash string `db:"FileHash" json:"fileHash"`
  89. Redundancy Redundancy `db:"Redundancy" json:"redundancy"`
  90. }
  91. type NodePackageCachingInfo struct {
  92. NodeID NodeID `json:"nodeID"`
  93. FileSize int64 `json:"fileSize"`
  94. ObjectCount int64 `json:"objectCount"`
  95. }
  96. type PackageCachingInfo struct {
  97. NodeInfos []NodePackageCachingInfo `json:"nodeInfos"`
  98. PackageSize int64 `json:"packageSize"`
  99. }
  100. func NewPackageCachingInfo(nodeInfos []NodePackageCachingInfo, packageSize int64) PackageCachingInfo {
  101. return PackageCachingInfo{
  102. NodeInfos: nodeInfos,
  103. PackageSize: packageSize,
  104. }
  105. }
  106. type CodeError struct {
  107. Code string `json:"code"`
  108. Message string `json:"message"`
  109. }
  110. func (e *CodeError) Error() string {
  111. return fmt.Sprintf("code: %s, message: %s", e.Code, e.Message)
  112. }