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.

pinned_object.go 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package db
  2. import (
  3. "time"
  4. "github.com/jmoiron/sqlx"
  5. "github.com/samber/lo"
  6. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  7. "gitlink.org.cn/cloudream/storage/common/pkgs/db/model"
  8. )
  9. type PinnedObjectDB struct {
  10. *DB
  11. }
  12. func (db *DB) PinnedObject() *PinnedObjectDB {
  13. return &PinnedObjectDB{DB: db}
  14. }
  15. func (*PinnedObjectDB) GetByNodeID(ctx SQLContext, nodeID cdssdk.NodeID) ([]cdssdk.PinnedObject, error) {
  16. var ret []cdssdk.PinnedObject
  17. err := sqlx.Select(ctx, &ret, "select * from PinnedObject where NodeID = ?", nodeID)
  18. return ret, err
  19. }
  20. func (*PinnedObjectDB) GetObjectsByNodeID(ctx SQLContext, nodeID cdssdk.NodeID) ([]cdssdk.Object, error) {
  21. var ret []model.TempObject
  22. err := sqlx.Select(ctx, &ret, "select Object.* from PinnedObject, Object where PinnedObject.ObjectID = Object.ObjectID and NodeID = ?", nodeID)
  23. return lo.Map(ret, func(o model.TempObject, idx int) cdssdk.Object { return o.ToObject() }), err
  24. }
  25. func (*PinnedObjectDB) Create(ctx SQLContext, nodeID cdssdk.NodeID, objectID cdssdk.ObjectID, createTime time.Time) error {
  26. _, err := ctx.Exec("insert into PinnedObject values(?,?,?)", nodeID, objectID, createTime)
  27. return err
  28. }
  29. func (*PinnedObjectDB) TryCreate(ctx SQLContext, nodeID cdssdk.NodeID, objectID cdssdk.ObjectID, createTime time.Time) error {
  30. _, err := ctx.Exec("insert ignore into PinnedObject values(?,?,?)", nodeID, objectID, createTime)
  31. return err
  32. }
  33. func (*PinnedObjectDB) BatchTryCreate(ctx SQLContext, pinneds []cdssdk.PinnedObject) error {
  34. if len(pinneds) == 0 {
  35. return nil
  36. }
  37. return BatchNamedExec(ctx, "insert ignore into PinnedObject values(:NodeID,:ObjectID,:CreateTime)", 3, pinneds, nil)
  38. }
  39. func (*PinnedObjectDB) CreateFromPackage(ctx SQLContext, packageID cdssdk.PackageID, nodeID cdssdk.NodeID) error {
  40. _, err := ctx.Exec(
  41. "insert ignore into PinnedObject(NodeID, ObjectID, CreateTime) select ? as NodeID, ObjectID, ? as CreateTime from Object where PackageID = ?",
  42. nodeID,
  43. time.Now(),
  44. packageID,
  45. )
  46. return err
  47. }
  48. func (db *PinnedObjectDB) ObjectBatchCreate(ctx SQLContext, objectID cdssdk.ObjectID, nodeIDs []cdssdk.NodeID) error {
  49. if len(nodeIDs) == 0 {
  50. return nil
  51. }
  52. for _, id := range nodeIDs {
  53. err := db.TryCreate(ctx, id, objectID, time.Now())
  54. if err != nil {
  55. return err
  56. }
  57. }
  58. return nil
  59. }
  60. func (*PinnedObjectDB) Delete(ctx SQLContext, nodeID cdssdk.NodeID, objectID cdssdk.ObjectID) error {
  61. _, err := ctx.Exec("delete from PinnedObject where NodeID = ? and ObjectID = ?", nodeID, objectID)
  62. return err
  63. }
  64. func (*PinnedObjectDB) DeleteByObjectID(ctx SQLContext, objectID cdssdk.ObjectID) error {
  65. _, err := ctx.Exec("delete from PinnedObject where ObjectID = ?", objectID)
  66. return err
  67. }
  68. func (*PinnedObjectDB) BatchDeleteByObjectID(ctx SQLContext, objectIDs []cdssdk.ObjectID) error {
  69. if len(objectIDs) == 0 {
  70. return nil
  71. }
  72. // TODO in语句有长度限制
  73. query, args, err := sqlx.In("delete from PinnedObject where ObjectID in (?)", objectIDs)
  74. if err != nil {
  75. return err
  76. }
  77. _, err = ctx.Exec(query, args...)
  78. return err
  79. }
  80. func (*PinnedObjectDB) DeleteInPackage(ctx SQLContext, packageID cdssdk.PackageID) error {
  81. _, err := ctx.Exec("delete PinnedObject from PinnedObject inner join Object on PinnedObject.ObjectID = Object.ObjectID where PackageID = ?", packageID)
  82. return err
  83. }
  84. func (*PinnedObjectDB) DeleteInPackageAtNode(ctx SQLContext, packageID cdssdk.PackageID, nodeID cdssdk.NodeID) error {
  85. _, err := ctx.Exec("delete PinnedObject from PinnedObject inner join Object on PinnedObject.ObjectID = Object.ObjectID where PackageID = ? and NodeID = ?", packageID, nodeID)
  86. return err
  87. }
  88. func (*PinnedObjectDB) NodeBatchDelete(ctx SQLContext, nodeID cdssdk.NodeID, objectIDs []cdssdk.ObjectID) error {
  89. if len(objectIDs) == 0 {
  90. return nil
  91. }
  92. query, args, err := sqlx.In("delete from PinnedObject where NodeID = ? and ObjectID in (?)", nodeID, objectIDs)
  93. if err != nil {
  94. return err
  95. }
  96. _, err = ctx.Exec(query, args...)
  97. return err
  98. }

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