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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package cdssdk
  2. import (
  3. "fmt"
  4. myreflect "gitlink.org.cn/cloudream/common/utils/reflect"
  5. "gitlink.org.cn/cloudream/common/utils/serder"
  6. )
  7. const (
  8. ObjectPathSeperator = "/"
  9. )
  10. /// TODO 将分散在各处的公共结构体定义集中到这里来
  11. const (
  12. RedundancyRep = "rep"
  13. RedundancyEC = "ec"
  14. )
  15. // 冗余模式的描述信息。
  16. // 注:如果在mq中的消息结构体使用了此类型,记得使用RegisterTypeSet注册相关的类型。
  17. type RedundancyInfo interface{}
  18. type RedundancyInfoConst interface {
  19. RepRedundancyInfo | ECRedundancyInfo
  20. }
  21. type RepRedundancyInfo struct {
  22. RepCount int `json:"repCount"`
  23. }
  24. func NewRepRedundancyInfo(repCount int) RepRedundancyInfo {
  25. return RepRedundancyInfo{
  26. RepCount: repCount,
  27. }
  28. }
  29. type ECRedundancyInfo struct {
  30. ECName string `json:"ecName"`
  31. PacketSize int64 `json:"packetSize"`
  32. }
  33. func NewECRedundancyInfo(ecName string, packetSize int64) ECRedundancyInfo {
  34. return ECRedundancyInfo{
  35. ECName: ecName,
  36. PacketSize: packetSize,
  37. }
  38. }
  39. type TypedRedundancyInfo struct {
  40. Type string `json:"type"`
  41. Info RedundancyInfo `json:"info"`
  42. }
  43. func NewTypedRedundancyInfo[T RedundancyInfoConst](info T) TypedRedundancyInfo {
  44. var typ string
  45. if myreflect.TypeOf[T]() == myreflect.TypeOf[RepRedundancyInfo]() {
  46. typ = RedundancyRep
  47. } else if myreflect.TypeOf[T]() == myreflect.TypeOf[ECRedundancyInfo]() {
  48. typ = RedundancyEC
  49. }
  50. return TypedRedundancyInfo{
  51. Type: typ,
  52. Info: info,
  53. }
  54. }
  55. func NewTypedRepRedundancyInfo(repCount int) TypedRedundancyInfo {
  56. return TypedRedundancyInfo{
  57. Type: RedundancyRep,
  58. Info: RepRedundancyInfo{
  59. RepCount: repCount,
  60. },
  61. }
  62. }
  63. func NewTypedECRedundancyInfo(ecName string, packetSize int64) TypedRedundancyInfo {
  64. return TypedRedundancyInfo{
  65. Type: RedundancyRep,
  66. Info: ECRedundancyInfo{
  67. ECName: ecName,
  68. PacketSize: packetSize,
  69. },
  70. }
  71. }
  72. func (i *TypedRedundancyInfo) IsRepInfo() bool {
  73. return i.Type == RedundancyRep
  74. }
  75. func (i *TypedRedundancyInfo) IsECInfo() bool {
  76. return i.Type == RedundancyEC
  77. }
  78. func (i *TypedRedundancyInfo) ToRepInfo() (RepRedundancyInfo, error) {
  79. var info RepRedundancyInfo
  80. err := serder.AnyToAny(i.Info, &info)
  81. return info, err
  82. }
  83. func (i *TypedRedundancyInfo) ToECInfo() (ECRedundancyInfo, error) {
  84. var info ECRedundancyInfo
  85. err := serder.AnyToAny(i.Info, &info)
  86. return info, err
  87. }
  88. func (i *TypedRedundancyInfo) Scan(src interface{}) error {
  89. data, ok := src.([]uint8)
  90. if !ok {
  91. return fmt.Errorf("unknow src type: %v", myreflect.TypeOfValue(data))
  92. }
  93. return serder.JSONToObject(data, i)
  94. }
  95. type NodePackageCachingInfo struct {
  96. NodeID int64 `json:"nodeID"`
  97. FileSize int64 `json:"fileSize"`
  98. ObjectCount int64 `json:"objectCount"`
  99. }
  100. type PackageCachingInfo struct {
  101. NodeInfos []NodePackageCachingInfo `json:"nodeInfos"`
  102. PackageSize int64 `json:"packageSize"`
  103. RedunancyType string `json:"redunancyType"`
  104. }
  105. func NewPackageCachingInfo(nodeInfos []NodePackageCachingInfo, packageSize int64, redunancyType string) PackageCachingInfo {
  106. return PackageCachingInfo{
  107. NodeInfos: nodeInfos,
  108. PackageSize: packageSize,
  109. RedunancyType: redunancyType,
  110. }
  111. }
  112. type Object struct {
  113. ObjectID int64 `db:"ObjectID" json:"objectID"`
  114. PackageID int64 `db:"PackageID" json:"packageID"`
  115. Path string `db:"Path" json:"path"`
  116. Size int64 `db:"Size" json:"size,string"`
  117. }
  118. type Package struct {
  119. PackageID int64 `db:"PackageID" json:"packageID"`
  120. Name string `db:"Name" json:"name"`
  121. BucketID int64 `db:"BucketID" json:"bucketID"`
  122. State string `db:"State" json:"state"`
  123. Redundancy TypedRedundancyInfo `db:"Redundancy" json:"redundancy"`
  124. }
  125. type ObjectCacheInfo struct {
  126. Object Object `json:"object"`
  127. FileHash string `json:"fileHash"`
  128. }
  129. func NewObjectCacheInfo(object Object, fileHash string) ObjectCacheInfo {
  130. return ObjectCacheInfo{
  131. Object: object,
  132. FileHash: fileHash,
  133. }
  134. }
  135. type CodeError struct {
  136. Code string `json:"code"`
  137. Message string `json:"message"`
  138. }
  139. func (e *CodeError) Error() string {
  140. return fmt.Sprintf("code: %s, message: %s", e.Code, e.Message)
  141. }