tree.go 8.0 KB

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