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.

update_storage.go 2.0 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package event
  2. import (
  3. tskcst "gitlink.org.cn/cloudream/common/consts/event"
  4. "gitlink.org.cn/cloudream/common/utils/logger"
  5. mysql "gitlink.org.cn/cloudream/db/sql"
  6. )
  7. type UpdateStorageEntry struct {
  8. ObjectID int
  9. Operation string
  10. }
  11. func NewUpdateStorageEntry(objectID int, op string) UpdateStorageEntry {
  12. return UpdateStorageEntry{
  13. ObjectID: objectID,
  14. Operation: op,
  15. }
  16. }
  17. type UpdateStorage struct {
  18. StorageID int
  19. DirectoryStatus string
  20. Entries []UpdateStorageEntry
  21. }
  22. func NewUpdateStorage(dirStatus string, entries []UpdateStorageEntry) UpdateStorage {
  23. return UpdateStorage{
  24. DirectoryStatus: dirStatus,
  25. Entries: entries,
  26. }
  27. }
  28. func (t *UpdateStorage) TryMerge(other Event) bool {
  29. event, ok := other.(*UpdateStorage)
  30. if !ok {
  31. return false
  32. }
  33. if event.StorageID != t.StorageID {
  34. return false
  35. }
  36. // 后投递的任务的状态更新一些
  37. t.DirectoryStatus = event.DirectoryStatus
  38. // TODO 可以考虑合并同FileHash和NodeID的记录
  39. t.Entries = append(t.Entries, event.Entries...)
  40. return true
  41. }
  42. func (t *UpdateStorage) Execute(execCtx ExecuteContext) {
  43. err := mysql.Storage.ChangeState(execCtx.Args.DB.SQLCtx(), t.StorageID, t.DirectoryStatus)
  44. if err != nil {
  45. logger.WithField("StorageID", t.StorageID).Warnf("change storage state failed, err: %s", err.Error())
  46. }
  47. for _, entry := range t.Entries {
  48. switch entry.Operation {
  49. case tskcst.UPDATE_STORAGE_DELETE:
  50. err := mysql.StorageObject.Delete(execCtx.Args.DB.SQLCtx(), t.StorageID, entry.ObjectID)
  51. if err != nil {
  52. logger.WithField("StorageID", t.StorageID).
  53. WithField("ObjectID", entry.ObjectID).
  54. Warnf("delete storage object failed, err: %s", err.Error())
  55. }
  56. case tskcst.UPDATE_STORAGE_SET_NORMAL:
  57. err := mysql.StorageObject.SetStateNormal(execCtx.Args.DB.SQLCtx(), t.StorageID, entry.ObjectID)
  58. if err != nil {
  59. logger.WithField("StorageID", t.StorageID).
  60. WithField("ObjectID", entry.ObjectID).
  61. Warnf("change storage object state failed, err: %s", err.Error())
  62. }
  63. }
  64. }
  65. }

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