tree.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 filesys
  17. import (
  18. "bytes"
  19. "encoding/json"
  20. "errors"
  21. "fmt"
  22. "os"
  23. "path/filepath"
  24. "strings"
  25. "sync"
  26. "time"
  27. "github.com/88250/lute"
  28. "github.com/88250/lute/parse"
  29. "github.com/88250/lute/render"
  30. jsoniter "github.com/json-iterator/go"
  31. "github.com/panjf2000/ants/v2"
  32. "github.com/siyuan-note/filelock"
  33. "github.com/siyuan-note/logging"
  34. "github.com/siyuan-note/siyuan/kernel/cache"
  35. "github.com/siyuan-note/siyuan/kernel/treenode"
  36. "github.com/siyuan-note/siyuan/kernel/util"
  37. )
  38. func LoadTrees(ids []string) (ret map[string]*parse.Tree) {
  39. ret = map[string]*parse.Tree{}
  40. bts := treenode.GetBlockTrees(ids)
  41. luteEngine := util.NewLute()
  42. var boxIDs []string
  43. var paths []string
  44. blockIDs := map[string][]string{}
  45. for _, bt := range bts {
  46. boxIDs = append(boxIDs, bt.BoxID)
  47. paths = append(paths, bt.Path)
  48. if _, ok := blockIDs[bt.RootID]; !ok {
  49. blockIDs[bt.RootID] = []string{}
  50. }
  51. blockIDs[bt.RootID] = append(blockIDs[bt.RootID], bt.ID)
  52. }
  53. trees, errs := batchLoadTrees(boxIDs, paths, luteEngine)
  54. for i := range trees {
  55. tree := trees[i]
  56. err := errs[i]
  57. if err != nil || tree == nil {
  58. logging.LogErrorf("load tree failed: %s", err)
  59. continue
  60. }
  61. bIDs := blockIDs[tree.Root.ID]
  62. for _, bID := range bIDs {
  63. ret[bID] = tree
  64. }
  65. }
  66. return
  67. }
  68. func LoadTree(boxID, p string, luteEngine *lute.Lute) (ret *parse.Tree, err error) {
  69. filePath := filepath.Join(util.DataDir, boxID, p)
  70. data, err := filelock.ReadFile(filePath)
  71. if err != nil {
  72. logging.LogErrorf("load tree [%s] failed: %s", p, err)
  73. return
  74. }
  75. ret, err = LoadTreeByData(data, boxID, p, luteEngine)
  76. return
  77. }
  78. func batchLoadTrees(boxIDs, paths []string, luteEngine *lute.Lute) (ret []*parse.Tree, errs []error) {
  79. waitGroup := sync.WaitGroup{}
  80. lock := sync.Mutex{}
  81. loaded := map[string]bool{}
  82. start := time.Now()
  83. p, _ := ants.NewPoolWithFunc(8, func(arg interface{}) {
  84. defer waitGroup.Done()
  85. i := arg.(int)
  86. boxID := boxIDs[i]
  87. path := paths[i]
  88. tree, err := LoadTree(boxID, path, luteEngine)
  89. lock.Lock()
  90. ret = append(ret, tree)
  91. errs = append(errs, err)
  92. lock.Unlock()
  93. })
  94. for i := range paths {
  95. if loaded[boxIDs[i]+paths[i]] {
  96. continue
  97. }
  98. loaded[boxIDs[i]+paths[i]] = true
  99. waitGroup.Add(1)
  100. p.Invoke(i)
  101. }
  102. waitGroup.Wait()
  103. p.Release()
  104. logging.LogInfof("batch load trees [%d] cost [%s]", len(paths), time.Since(start))
  105. return
  106. }
  107. func LoadTreeByData(data []byte, boxID, p string, luteEngine *lute.Lute) (ret *parse.Tree, err error) {
  108. ret = parseJSON2Tree(boxID, p, data, luteEngine)
  109. if nil == ret {
  110. logging.LogErrorf("parse tree [%s] failed", p)
  111. err = errors.New("parse tree failed")
  112. return
  113. }
  114. ret.Path = p
  115. ret.Root.Path = p
  116. parts := strings.Split(p, "/")
  117. parts = parts[1 : len(parts)-1] // 去掉开头的斜杆和结尾的自己
  118. if 1 > len(parts) {
  119. ret.HPath = "/" + ret.Root.IALAttr("title")
  120. ret.Hash = treenode.NodeHash(ret.Root, ret, luteEngine)
  121. return
  122. }
  123. // 构造 HPath
  124. hPathBuilder := bytes.Buffer{}
  125. hPathBuilder.WriteString("/")
  126. for i, _ := range parts {
  127. var parentAbsPath string
  128. if 0 < i {
  129. parentAbsPath = strings.Join(parts[:i+1], "/")
  130. } else {
  131. parentAbsPath = parts[0]
  132. }
  133. parentAbsPath += ".sy"
  134. parentPath := parentAbsPath
  135. parentAbsPath = filepath.Join(util.DataDir, boxID, parentAbsPath)
  136. parentData, readErr := filelock.ReadFile(parentAbsPath)
  137. if nil != readErr {
  138. if os.IsNotExist(readErr) {
  139. // 子文档缺失父文档时自动补全 https://github.com/siyuan-note/siyuan/issues/7376
  140. parentTree := treenode.NewTree(boxID, parentPath, hPathBuilder.String()+"Untitled", "Untitled")
  141. if _, writeErr := WriteTree(parentTree); nil != writeErr {
  142. logging.LogErrorf("rebuild parent tree [%s] failed: %s", parentAbsPath, writeErr)
  143. } else {
  144. logging.LogInfof("rebuilt parent tree [%s]", parentAbsPath)
  145. treenode.UpsertBlockTree(parentTree)
  146. }
  147. } else {
  148. logging.LogWarnf("read parent tree data [%s] failed: %s", parentAbsPath, readErr)
  149. }
  150. hPathBuilder.WriteString("Untitled/")
  151. continue
  152. }
  153. ial := ReadDocIAL(parentData)
  154. if 1 > len(ial) {
  155. logging.LogWarnf("tree [%s] is corrupted", filepath.Join(boxID, p))
  156. }
  157. title := ial["title"]
  158. if "" == title {
  159. title = "Untitled"
  160. }
  161. hPathBuilder.WriteString(title)
  162. hPathBuilder.WriteString("/")
  163. }
  164. hPathBuilder.WriteString(ret.Root.IALAttr("title"))
  165. ret.HPath = hPathBuilder.String()
  166. ret.Hash = treenode.NodeHash(ret.Root, ret, luteEngine)
  167. return
  168. }
  169. func WriteTree(tree *parse.Tree) (size uint64, err error) {
  170. data, filePath, err := prepareWriteTree(tree)
  171. if err != nil {
  172. return
  173. }
  174. size = uint64(len(data))
  175. if err = filelock.WriteFile(filePath, data); err != nil {
  176. msg := fmt.Sprintf("write data [%s] failed: %s", filePath, err)
  177. logging.LogErrorf(msg)
  178. err = errors.New(msg)
  179. return
  180. }
  181. afterWriteTree(tree)
  182. return
  183. }
  184. func prepareWriteTree(tree *parse.Tree) (data []byte, filePath string, err error) {
  185. luteEngine := util.NewLute() // 不关注用户的自定义解析渲染选项
  186. if nil == tree.Root.FirstChild {
  187. newP := treenode.NewParagraph()
  188. tree.Root.AppendChild(newP)
  189. tree.Root.SetIALAttr("updated", util.TimeFromID(newP.ID))
  190. treenode.UpsertBlockTree(tree)
  191. }
  192. filePath = filepath.Join(util.DataDir, tree.Box, tree.Path)
  193. if oldSpec := tree.Root.Spec; "" == oldSpec {
  194. parse.NestedInlines2FlattedSpans(tree, false)
  195. tree.Root.Spec = "1"
  196. logging.LogInfof("migrated tree [%s] from spec [%s] to [%s]", filePath, oldSpec, tree.Root.Spec)
  197. }
  198. tree.Root.SetIALAttr("type", "doc")
  199. renderer := render.NewJSONRenderer(tree, luteEngine.RenderOptions)
  200. data = renderer.Render()
  201. if !util.UseSingleLineSave {
  202. buf := bytes.Buffer{}
  203. buf.Grow(1024 * 1024 * 2)
  204. if err = json.Indent(&buf, data, "", "\t"); err != nil {
  205. return
  206. }
  207. data = buf.Bytes()
  208. }
  209. if err = os.MkdirAll(filepath.Dir(filePath), 0755); err != nil {
  210. return
  211. }
  212. return
  213. }
  214. func afterWriteTree(tree *parse.Tree) {
  215. docIAL := parse.IAL2MapUnEsc(tree.Root.KramdownIAL)
  216. cache.PutDocIAL(tree.Path, docIAL)
  217. }
  218. func parseJSON2Tree(boxID, p string, jsonData []byte, luteEngine *lute.Lute) (ret *parse.Tree) {
  219. var err error
  220. var needFix bool
  221. ret, needFix, err = ParseJSON(jsonData, luteEngine.ParseOptions)
  222. if err != nil {
  223. logging.LogErrorf("parse json [%s] to tree failed: %s", boxID+p, err)
  224. return
  225. }
  226. ret.Box = boxID
  227. ret.Path = p
  228. filePath := filepath.Join(util.DataDir, ret.Box, ret.Path)
  229. if oldSpec := ret.Root.Spec; "" == oldSpec {
  230. parse.NestedInlines2FlattedSpans(ret, false)
  231. ret.Root.Spec = "1"
  232. needFix = true
  233. logging.LogInfof("migrated tree [%s] from spec [%s] to [%s]", filePath, oldSpec, ret.Root.Spec)
  234. }
  235. if needFix {
  236. renderer := render.NewJSONRenderer(ret, luteEngine.RenderOptions)
  237. data := renderer.Render()
  238. if !util.UseSingleLineSave {
  239. buf := bytes.Buffer{}
  240. buf.Grow(1024 * 1024 * 2)
  241. if err = json.Indent(&buf, data, "", "\t"); err != nil {
  242. return
  243. }
  244. data = buf.Bytes()
  245. }
  246. if err = os.MkdirAll(filepath.Dir(filePath), 0755); err != nil {
  247. return
  248. }
  249. if err = filelock.WriteFile(filePath, data); err != nil {
  250. msg := fmt.Sprintf("write data [%s] failed: %s", filePath, err)
  251. logging.LogErrorf(msg)
  252. }
  253. }
  254. return
  255. }
  256. func ReadDocIAL(data []byte) (ret map[string]string) {
  257. ret = map[string]string{}
  258. val := jsoniter.Get(data, "Properties")
  259. if nil == val || val.ValueType() == jsoniter.InvalidValue {
  260. return
  261. }
  262. val.ToVal(&ret)
  263. return
  264. }