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.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package cdssdk
  2. import (
  3. "database/sql/driver"
  4. "fmt"
  5. "gitlink.org.cn/cloudream/common/pkgs/types"
  6. myreflect "gitlink.org.cn/cloudream/common/utils/reflect"
  7. "gitlink.org.cn/cloudream/common/utils/serder"
  8. )
  9. const (
  10. ObjectPathSeperator = "/"
  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. type RedundancyBase struct{}
  24. func (b *RedundancyBase) Value() (driver.Value, error) {
  25. return serder.ObjectToJSONEx[Redundancy](b)
  26. }
  27. var RedundancyUnion = serder.UseTypeUnionInternallyTagged(types.Ref(types.NewTypeUnion[Redundancy](
  28. (*RepRedundancy)(nil),
  29. (*ECRedundancy)(nil),
  30. )), "type")
  31. type RepRedundancy struct {
  32. RedundancyBase
  33. serder.Metadata `union:"rep"`
  34. Type string `json:"type"`
  35. }
  36. func NewRepRedundancy() *RepRedundancy {
  37. return &RepRedundancy{
  38. Type: "rep",
  39. }
  40. }
  41. type ECRedundancy struct {
  42. RedundancyBase
  43. serder.Metadata `union:"ec"`
  44. Type string `json:"type"`
  45. K int `json:"k"`
  46. N int `json:"n"`
  47. ChunkSize int `json:"chunkSize"`
  48. }
  49. func NewECRedundancy(k int, n int, chunkSize int) *ECRedundancy {
  50. return &ECRedundancy{
  51. Type: "ec",
  52. K: k,
  53. N: n,
  54. ChunkSize: chunkSize,
  55. }
  56. }
  57. const (
  58. PackageStateNormal = "Normal"
  59. PackageStateDeleted = "Deleted"
  60. )
  61. type Package struct {
  62. PackageID PackageID `db:"PackageID" json:"packageID"`
  63. Name string `db:"Name" json:"name"`
  64. BucketID BucketID `db:"BucketID" json:"bucketID"`
  65. State string `db:"State" json:"state"`
  66. }
  67. type Object struct {
  68. ObjectID ObjectID `db:"ObjectID" json:"objectID"`
  69. PackageID PackageID `db:"PackageID" json:"packageID"`
  70. Path string `db:"Path" json:"path"`
  71. Size int64 `db:"Size" json:"size,string"`
  72. Redundancy Redundancy `db:"Redundancy" json:"redundancy"`
  73. }
  74. func (i *Object) Scan(src interface{}) error {
  75. data, ok := src.([]uint8)
  76. if !ok {
  77. return fmt.Errorf("unknow src type: %v", myreflect.TypeOfValue(data))
  78. }
  79. obj, err := serder.JSONToObjectEx[*Object](data)
  80. if err != nil {
  81. return err
  82. }
  83. *i = *obj
  84. return nil
  85. }
  86. type ObjectCacheInfo struct {
  87. Object Object `json:"object"`
  88. FileHash string `json:"fileHash"`
  89. }
  90. func NewObjectCacheInfo(object Object, fileHash string) ObjectCacheInfo {
  91. return ObjectCacheInfo{
  92. Object: object,
  93. FileHash: fileHash,
  94. }
  95. }
  96. type NodePackageCachingInfo struct {
  97. NodeID NodeID `json:"nodeID"`
  98. FileSize int64 `json:"fileSize"`
  99. ObjectCount int64 `json:"objectCount"`
  100. }
  101. type PackageCachingInfo struct {
  102. NodeInfos []NodePackageCachingInfo `json:"nodeInfos"`
  103. PackageSize int64 `json:"packageSize"`
  104. RedunancyType string `json:"redunancyType"`
  105. }
  106. func NewPackageCachingInfo(nodeInfos []NodePackageCachingInfo, packageSize int64, redunancyType string) PackageCachingInfo {
  107. return PackageCachingInfo{
  108. NodeInfos: nodeInfos,
  109. PackageSize: packageSize,
  110. RedunancyType: redunancyType,
  111. }
  112. }
  113. type CodeError struct {
  114. Code string `json:"code"`
  115. Message string `json:"message"`
  116. }
  117. func (e *CodeError) Error() string {
  118. return fmt.Sprintf("code: %s, message: %s", e.Code, e.Message)
  119. }