av.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // SiYuan - Refactor your thinking
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package treenode
  17. import (
  18. "path/filepath"
  19. "github.com/siyuan-note/filelock"
  20. "github.com/siyuan-note/logging"
  21. "github.com/siyuan-note/siyuan/kernel/av"
  22. "github.com/siyuan-note/siyuan/kernel/util"
  23. "github.com/vmihailenco/msgpack/v5"
  24. )
  25. func GetMirrorAttrViewBlockIDs(avID string) (ret []string) {
  26. av.AttributeViewBlocksLock.Lock()
  27. defer av.AttributeViewBlocksLock.Unlock()
  28. ret = []string{}
  29. blocks := filepath.Join(util.DataDir, "storage", "av", "blocks.msgpack")
  30. if !filelock.IsExist(blocks) {
  31. return
  32. }
  33. data, err := filelock.ReadFile(blocks)
  34. if nil != err {
  35. logging.LogErrorf("read attribute view blocks failed: %s", err)
  36. return
  37. }
  38. avBlocks := map[string][]string{}
  39. if err = msgpack.Unmarshal(data, &avBlocks); nil != err {
  40. logging.LogErrorf("unmarshal attribute view blocks failed: %s", err)
  41. return
  42. }
  43. blockIDs := avBlocks[avID]
  44. for _, blockID := range blockIDs {
  45. if nil != GetBlockTree(blockID) {
  46. ret = append(ret, blockID)
  47. }
  48. }
  49. return
  50. }
  51. type AvBlock struct {
  52. AvID string
  53. BlockIDs []string
  54. }
  55. func BatchGetMirrorAttrViewBlocks(avIDs []string) (ret []*AvBlock) {
  56. av.AttributeViewBlocksLock.Lock()
  57. defer av.AttributeViewBlocksLock.Unlock()
  58. ret = []*AvBlock{}
  59. blocks := filepath.Join(util.DataDir, "storage", "av", "blocks.msgpack")
  60. if !filelock.IsExist(blocks) {
  61. return
  62. }
  63. data, err := filelock.ReadFile(blocks)
  64. if nil != err {
  65. logging.LogErrorf("read attribute view blocks failed: %s", err)
  66. return
  67. }
  68. avBlocks := map[string][]string{}
  69. if err = msgpack.Unmarshal(data, &avBlocks); nil != err {
  70. logging.LogErrorf("unmarshal attribute view blocks failed: %s", err)
  71. return
  72. }
  73. for _, avID := range avIDs {
  74. var blockIDs []string
  75. for _, blockID := range avBlocks[avID] {
  76. if nil == GetBlockTree(blockID) {
  77. continue
  78. }
  79. blockIDs = append(blockIDs, blockID)
  80. }
  81. avBlock := &AvBlock{
  82. AvID: avID,
  83. BlockIDs: blockIDs,
  84. }
  85. ret = append(ret, avBlock)
  86. }
  87. return
  88. }