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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package db
  2. import (
  3. "database/sql"
  4. "strconv"
  5. "strings"
  6. "github.com/jmoiron/sqlx"
  7. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  8. stgmod "gitlink.org.cn/cloudream/storage/common/models"
  9. )
  10. type ObjectBlockDB struct {
  11. *DB
  12. }
  13. func (db *DB) ObjectBlock() *ObjectBlockDB {
  14. return &ObjectBlockDB{DB: db}
  15. }
  16. func (db *ObjectBlockDB) GetByNodeID(ctx SQLContext, nodeID cdssdk.NodeID) ([]stgmod.ObjectBlock, error) {
  17. var rets []stgmod.ObjectBlock
  18. err := sqlx.Select(ctx, &rets, "select * from ObjectBlock where NodeID = ?", nodeID)
  19. return rets, err
  20. }
  21. func (db *ObjectBlockDB) Create(ctx SQLContext, objectID cdssdk.ObjectID, index int, nodeID cdssdk.NodeID, fileHash string) error {
  22. _, err := ctx.Exec("insert into ObjectBlock values(?,?,?,?)", objectID, index, nodeID, fileHash)
  23. return err
  24. }
  25. func (db *ObjectBlockDB) BatchCreate(ctx SQLContext, blocks []stgmod.ObjectBlock) error {
  26. return BatchNamedExec(ctx,
  27. "insert ignore into ObjectBlock(ObjectID, `Index`, NodeID, FileHash) values(:ObjectID, :Index, :NodeID, :FileHash)",
  28. 4,
  29. blocks,
  30. nil,
  31. )
  32. }
  33. func (db *ObjectBlockDB) DeleteByObjectID(ctx SQLContext, objectID cdssdk.ObjectID) error {
  34. _, err := ctx.Exec("delete from ObjectBlock where ObjectID = ?", objectID)
  35. return err
  36. }
  37. func (db *ObjectBlockDB) BatchDeleteByObjectID(ctx SQLContext, objectIDs []cdssdk.ObjectID) error {
  38. // TODO in语句有长度限制
  39. query, args, err := sqlx.In("delete from ObjectBlock where ObjectID in (?)", objectIDs)
  40. if err != nil {
  41. return err
  42. }
  43. _, err = ctx.Exec(query, args...)
  44. return err
  45. }
  46. func (db *ObjectBlockDB) DeleteInPackage(ctx SQLContext, packageID cdssdk.PackageID) error {
  47. _, err := ctx.Exec("delete ObjectBlock from ObjectBlock inner join Object on ObjectBlock.ObjectID = Object.ObjectID where PackageID = ?", packageID)
  48. return err
  49. }
  50. func (db *ObjectBlockDB) NodeBatchDelete(ctx SQLContext, nodeID cdssdk.NodeID, fileHashes []string) error {
  51. query, args, err := sqlx.In("delete from ObjectBlock where NodeID = ? and FileHash in (?)", nodeID, fileHashes)
  52. if err != nil {
  53. return err
  54. }
  55. _, err = ctx.Exec(query, args...)
  56. return err
  57. }
  58. func (db *ObjectBlockDB) CountBlockWithHash(ctx SQLContext, fileHash string) (int, error) {
  59. var cnt int
  60. err := sqlx.Get(ctx, &cnt,
  61. "select count(FileHash) from ObjectBlock, Object, Package where FileHash = ? and"+
  62. " ObjectBlock.ObjectID = Object.ObjectID and"+
  63. " Object.PackageID = Package.PackageID and"+
  64. " Package.State = ?", fileHash, cdssdk.PackageStateNormal)
  65. if err == sql.ErrNoRows {
  66. return 0, nil
  67. }
  68. return cnt, err
  69. }
  70. // 按逗号切割字符串,并将每一个部分解析为一个int64的ID。
  71. // 注:需要外部保证分隔的每一个部分都是正确的10进制数字格式
  72. func splitConcatedNodeID(idStr string) []cdssdk.NodeID {
  73. idStrs := strings.Split(idStr, ",")
  74. ids := make([]cdssdk.NodeID, 0, len(idStrs))
  75. for _, str := range idStrs {
  76. // 假设传入的ID是正确的数字格式
  77. id, _ := strconv.ParseInt(str, 10, 64)
  78. ids = append(ids, cdssdk.NodeID(id))
  79. }
  80. return ids
  81. }
  82. // 按逗号切割字符串
  83. func splitConcatedFileHash(idStr string) []string {
  84. idStrs := strings.Split(idStr, ",")
  85. return idStrs
  86. }

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