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.

object_block.go 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package db2
  2. import (
  3. "strconv"
  4. "strings"
  5. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  6. stgmod "gitlink.org.cn/cloudream/storage/common/models"
  7. "gorm.io/gorm/clause"
  8. )
  9. type ObjectBlockDB struct {
  10. *DB
  11. }
  12. func (db *DB) ObjectBlock() *ObjectBlockDB {
  13. return &ObjectBlockDB{DB: db}
  14. }
  15. func (db *ObjectBlockDB) GetByStorageID(ctx SQLContext, stgID cdssdk.StorageID) ([]stgmod.ObjectBlock, error) {
  16. var rets []stgmod.ObjectBlock
  17. err := ctx.Table("ObjectBlock").Where("StorageID = ?", stgID).Find(&rets).Error
  18. return rets, err
  19. }
  20. func (db *ObjectBlockDB) BatchGetByObjectID(ctx SQLContext, objectIDs []cdssdk.ObjectID) ([]stgmod.ObjectBlock, error) {
  21. if len(objectIDs) == 0 {
  22. return nil, nil
  23. }
  24. var blocks []stgmod.ObjectBlock
  25. err := ctx.Table("ObjectBlock").Where("ObjectID IN (?)", objectIDs).Order("ObjectID, `Index` ASC").Find(&blocks).Error
  26. return blocks, err
  27. }
  28. func (db *ObjectBlockDB) Create(ctx SQLContext, objectID cdssdk.ObjectID, index int, stgID cdssdk.StorageID, fileHash cdssdk.FileHash) error {
  29. block := stgmod.ObjectBlock{ObjectID: objectID, Index: index, StorageID: stgID, FileHash: fileHash}
  30. return ctx.Table("ObjectBlock").Create(&block).Error
  31. }
  32. func (db *ObjectBlockDB) BatchCreate(ctx SQLContext, blocks []stgmod.ObjectBlock) error {
  33. if len(blocks) == 0 {
  34. return nil
  35. }
  36. return ctx.Clauses(clause.Insert{Modifier: "ignore"}).Create(&blocks).Error
  37. }
  38. func (db *ObjectBlockDB) DeleteByObjectID(ctx SQLContext, objectID cdssdk.ObjectID) error {
  39. return ctx.Table("ObjectBlock").Where("ObjectID = ?", objectID).Delete(&stgmod.ObjectBlock{}).Error
  40. }
  41. func (db *ObjectBlockDB) BatchDeleteByObjectID(ctx SQLContext, objectIDs []cdssdk.ObjectID) error {
  42. if len(objectIDs) == 0 {
  43. return nil
  44. }
  45. return ctx.Table("ObjectBlock").Where("ObjectID IN (?)", objectIDs).Delete(&stgmod.ObjectBlock{}).Error
  46. }
  47. func (db *ObjectBlockDB) DeleteInPackage(ctx SQLContext, packageID cdssdk.PackageID) error {
  48. return ctx.Table("ObjectBlock").Where("ObjectID IN (SELECT ObjectID FROM Object WHERE PackageID = ?)", packageID).Delete(&stgmod.ObjectBlock{}).Error
  49. }
  50. func (db *ObjectBlockDB) StorageBatchDelete(ctx SQLContext, stgID cdssdk.StorageID, fileHashes []cdssdk.FileHash) error {
  51. if len(fileHashes) == 0 {
  52. return nil
  53. }
  54. return ctx.Table("ObjectBlock").Where("StorageID = ? AND FileHash IN (?)", stgID, fileHashes).Delete(&stgmod.ObjectBlock{}).Error
  55. }
  56. func (db *ObjectBlockDB) CountBlockWithHash(ctx SQLContext, fileHash string) (int, error) {
  57. var cnt int64
  58. err := ctx.Table("ObjectBlock").
  59. Select("COUNT(FileHash)").
  60. Joins("INNER JOIN Object ON ObjectBlock.ObjectID = Object.ObjectID").
  61. Joins("INNER JOIN Package ON Object.PackageID = Package.PackageID").
  62. Where("FileHash = ? AND Package.State = ?", fileHash, cdssdk.PackageStateNormal).
  63. Scan(&cnt).Error
  64. if err != nil {
  65. return 0, err
  66. }
  67. return int(cnt), nil
  68. }
  69. // 按逗号切割字符串,并将每一个部分解析为一个int64的ID。
  70. // 注:需要外部保证分隔的每一个部分都是正确的10进制数字格式
  71. func splitConcatedHubID(idStr string) []cdssdk.HubID {
  72. idStrs := strings.Split(idStr, ",")
  73. ids := make([]cdssdk.HubID, 0, len(idStrs))
  74. for _, str := range idStrs {
  75. // 假设传入的ID是正确的数字格式
  76. id, _ := strconv.ParseInt(str, 10, 64)
  77. ids = append(ids, cdssdk.HubID(id))
  78. }
  79. return ids
  80. }
  81. // 按逗号切割字符串
  82. func splitConcatedFileHash(idStr string) []string {
  83. idStrs := strings.Split(idStr, ",")
  84. return idStrs
  85. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。