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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package db2
  2. import (
  3. "time"
  4. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  5. "gorm.io/gorm/clause"
  6. )
  7. type PinnedObjectDB struct {
  8. *DB
  9. }
  10. func (db *DB) PinnedObject() *PinnedObjectDB {
  11. return &PinnedObjectDB{DB: db}
  12. }
  13. func (*PinnedObjectDB) GetByStorageID(ctx SQLContext, stgID cdssdk.StorageID) ([]cdssdk.PinnedObject, error) {
  14. var ret []cdssdk.PinnedObject
  15. err := ctx.Table("PinnedObject").Find(&ret, "StorageID = ?", stgID).Error
  16. return ret, err
  17. }
  18. func (*PinnedObjectDB) GetObjectsByStorageID(ctx SQLContext, stgID cdssdk.StorageID) ([]cdssdk.Object, error) {
  19. var ret []cdssdk.Object
  20. err := ctx.Table("Object").Joins("inner join PinnedObject on Object.ObjectID = PinnedObject.ObjectID").Where("StorageID = ?", stgID).Find(&ret).Error
  21. return ret, err
  22. }
  23. func (*PinnedObjectDB) Create(ctx SQLContext, stgID cdssdk.StorageID, objectID cdssdk.ObjectID, createTime time.Time) error {
  24. return ctx.Table("PinnedObject").Create(&cdssdk.PinnedObject{StorageID: stgID, ObjectID: objectID, CreateTime: createTime}).Error
  25. }
  26. func (*PinnedObjectDB) BatchGetByObjectID(ctx SQLContext, objectIDs []cdssdk.ObjectID) ([]cdssdk.PinnedObject, error) {
  27. if len(objectIDs) == 0 {
  28. return nil, nil
  29. }
  30. var pinneds []cdssdk.PinnedObject
  31. err := ctx.Table("PinnedObject").Where("ObjectID in (?)", objectIDs).Order("ObjectID asc").Find(&pinneds).Error
  32. return pinneds, err
  33. }
  34. func (*PinnedObjectDB) TryCreate(ctx SQLContext, stgID cdssdk.StorageID, objectID cdssdk.ObjectID, createTime time.Time) error {
  35. return ctx.Clauses(clause.OnConflict{
  36. Columns: []clause.Column{{Name: "ObjectID"}, {Name: "StorageID"}},
  37. DoUpdates: clause.AssignmentColumns([]string{"CreateTime"}),
  38. }).Create(&cdssdk.PinnedObject{StorageID: stgID, ObjectID: objectID, CreateTime: createTime}).Error
  39. }
  40. func (*PinnedObjectDB) BatchTryCreate(ctx SQLContext, pinneds []cdssdk.PinnedObject) error {
  41. if len(pinneds) == 0 {
  42. return nil
  43. }
  44. return ctx.Clauses(clause.OnConflict{
  45. Columns: []clause.Column{{Name: "ObjectID"}, {Name: "StorageID"}},
  46. DoUpdates: clause.AssignmentColumns([]string{"CreateTime"}),
  47. }).Create(&pinneds).Error
  48. }
  49. func (*PinnedObjectDB) CreateFromPackage(ctx SQLContext, packageID cdssdk.PackageID, stgID cdssdk.StorageID) error {
  50. err := ctx.Exec(
  51. "insert ignore into PinnedObject(StorageID, ObjectID, CreateTime) select ? as StorageID, ObjectID, ? as CreateTime from Object where PackageID = ?",
  52. stgID,
  53. time.Now(),
  54. packageID,
  55. ).Error
  56. return err
  57. }
  58. func (db *PinnedObjectDB) ObjectBatchCreate(ctx SQLContext, objectID cdssdk.ObjectID, stgIDs []cdssdk.StorageID) error {
  59. if len(stgIDs) == 0 {
  60. return nil
  61. }
  62. for _, id := range stgIDs {
  63. err := db.TryCreate(ctx, id, objectID, time.Now())
  64. if err != nil {
  65. return err
  66. }
  67. }
  68. return nil
  69. }
  70. func (*PinnedObjectDB) Delete(ctx SQLContext, stgID cdssdk.StorageID, objectID cdssdk.ObjectID) error {
  71. err := ctx.Exec("delete from PinnedObject where StorageID = ? and ObjectID = ?", stgID, objectID).Error
  72. return err
  73. }
  74. func (*PinnedObjectDB) DeleteByObjectID(ctx SQLContext, objectID cdssdk.ObjectID) error {
  75. err := ctx.Exec("delete from PinnedObject where ObjectID = ?", objectID).Error
  76. return err
  77. }
  78. func (*PinnedObjectDB) BatchDeleteByObjectID(ctx SQLContext, objectIDs []cdssdk.ObjectID) error {
  79. if len(objectIDs) == 0 {
  80. return nil
  81. }
  82. err := ctx.Table("PinnedObject").Where("ObjectID in (?)", objectIDs).Delete(&cdssdk.PinnedObject{}).Error
  83. return err
  84. }
  85. func (*PinnedObjectDB) DeleteInPackage(ctx SQLContext, packageID cdssdk.PackageID) error {
  86. err := ctx.Table("PinnedObject").Where("ObjectID in (select ObjectID from Object where PackageID = ?)", packageID).Delete(&cdssdk.PinnedObject{}).Error
  87. return err
  88. }
  89. func (*PinnedObjectDB) DeleteInPackageAtStorage(ctx SQLContext, packageID cdssdk.PackageID, stgID cdssdk.StorageID) error {
  90. err := ctx.Exec("delete PinnedObject from PinnedObject inner join Object on PinnedObject.ObjectID = Object.ObjectID where PackageID = ? and StorageID = ?", packageID, stgID).Error
  91. return err
  92. }
  93. func (*PinnedObjectDB) StorageBatchDelete(ctx SQLContext, stgID cdssdk.StorageID, objectIDs []cdssdk.ObjectID) error {
  94. if len(objectIDs) == 0 {
  95. return nil
  96. }
  97. err := ctx.Table("PinnedObject").Where("StorageID = ? and ObjectID in (?)", stgID, objectIDs).Delete(&cdssdk.PinnedObject{}).Error
  98. return err
  99. }

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