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

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