blocktree.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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. "os"
  19. "path/filepath"
  20. "strings"
  21. "sync"
  22. "sync/atomic"
  23. "time"
  24. "github.com/88250/go-humanize"
  25. "github.com/88250/gulu"
  26. "github.com/88250/lute/ast"
  27. "github.com/88250/lute/parse"
  28. "github.com/panjf2000/ants/v2"
  29. util2 "github.com/siyuan-note/dejavu/util"
  30. "github.com/siyuan-note/logging"
  31. "github.com/siyuan-note/siyuan/kernel/task"
  32. "github.com/siyuan-note/siyuan/kernel/util"
  33. "github.com/vmihailenco/msgpack/v5"
  34. )
  35. var blockTrees = &sync.Map{}
  36. type btSlice struct {
  37. data map[string]*BlockTree
  38. changed time.Time
  39. m *sync.Mutex
  40. }
  41. type BlockTree struct {
  42. ID string // 块 ID
  43. RootID string // 根 ID
  44. ParentID string // 父 ID
  45. BoxID string // 笔记本 ID
  46. Path string // 文档数据路径
  47. HPath string // 文档可读路径
  48. Updated string // 更新时间
  49. Type string // 类型
  50. }
  51. func GetBlockTreesByType(typ string) (ret []*BlockTree) {
  52. blockTrees.Range(func(key, value interface{}) bool {
  53. slice := value.(*btSlice)
  54. slice.m.Lock()
  55. for _, b := range slice.data {
  56. if b.Type == typ {
  57. ret = append(ret, b)
  58. }
  59. }
  60. slice.m.Unlock()
  61. return true
  62. })
  63. return
  64. }
  65. func GetBlockTreeByPath(path string) (ret *BlockTree) {
  66. blockTrees.Range(func(key, value interface{}) bool {
  67. slice := value.(*btSlice)
  68. slice.m.Lock()
  69. for _, b := range slice.data {
  70. if b.Path == path {
  71. ret = b
  72. break
  73. }
  74. }
  75. slice.m.Unlock()
  76. return nil == ret
  77. })
  78. return
  79. }
  80. func CountTrees() (ret int) {
  81. roots := map[string]bool{}
  82. blockTrees.Range(func(key, value interface{}) bool {
  83. slice := value.(*btSlice)
  84. slice.m.Lock()
  85. for _, b := range slice.data {
  86. roots[b.RootID] = true
  87. }
  88. slice.m.Unlock()
  89. return true
  90. })
  91. ret = len(roots)
  92. return
  93. }
  94. func CountBlocks() (ret int) {
  95. blockTrees.Range(func(key, value interface{}) bool {
  96. slice := value.(*btSlice)
  97. slice.m.Lock()
  98. ret += len(slice.data)
  99. slice.m.Unlock()
  100. return true
  101. })
  102. return
  103. }
  104. func GetBlockTreeRootByPath(boxID, path string) (ret *BlockTree) {
  105. blockTrees.Range(func(key, value interface{}) bool {
  106. slice := value.(*btSlice)
  107. slice.m.Lock()
  108. for _, b := range slice.data {
  109. if b.BoxID == boxID && b.Path == path && b.RootID == b.ID {
  110. ret = b
  111. break
  112. }
  113. }
  114. slice.m.Unlock()
  115. return nil == ret
  116. })
  117. return
  118. }
  119. func GetBlockTreeRootByHPath(boxID, hPath string) (ret *BlockTree) {
  120. hPath = gulu.Str.RemoveInvisible(hPath)
  121. blockTrees.Range(func(key, value interface{}) bool {
  122. slice := value.(*btSlice)
  123. slice.m.Lock()
  124. for _, b := range slice.data {
  125. if b.BoxID == boxID && b.HPath == hPath && b.RootID == b.ID {
  126. ret = b
  127. break
  128. }
  129. }
  130. slice.m.Unlock()
  131. return nil == ret
  132. })
  133. return
  134. }
  135. func GetBlockTreeRootsByHPath(boxID, hPath string) (ret []*BlockTree) {
  136. hPath = gulu.Str.RemoveInvisible(hPath)
  137. blockTrees.Range(func(key, value interface{}) bool {
  138. slice := value.(*btSlice)
  139. slice.m.Lock()
  140. for _, b := range slice.data {
  141. if b.BoxID == boxID && b.HPath == hPath && b.RootID == b.ID {
  142. ret = append(ret, b)
  143. }
  144. }
  145. slice.m.Unlock()
  146. return true
  147. })
  148. return
  149. }
  150. func GetBlockTreeRootByHPathPreferredParentID(boxID, hPath, preferredParentID string) (ret *BlockTree) {
  151. hPath = gulu.Str.RemoveInvisible(hPath)
  152. var roots []*BlockTree
  153. blockTrees.Range(func(key, value interface{}) bool {
  154. slice := value.(*btSlice)
  155. slice.m.Lock()
  156. for _, b := range slice.data {
  157. if b.BoxID == boxID && b.HPath == hPath && b.RootID == b.ID {
  158. if "" == preferredParentID {
  159. ret = b
  160. break
  161. }
  162. roots = append(roots, b)
  163. }
  164. }
  165. slice.m.Unlock()
  166. return nil == ret
  167. })
  168. if 1 > len(roots) {
  169. return
  170. }
  171. for _, root := range roots {
  172. if root.ID == preferredParentID {
  173. ret = root
  174. return
  175. }
  176. }
  177. ret = roots[0]
  178. return
  179. }
  180. func ExistBlockTree(id string) bool {
  181. hash := btHash(id)
  182. val, ok := blockTrees.Load(hash)
  183. if !ok {
  184. return false
  185. }
  186. slice := val.(*btSlice)
  187. slice.m.Lock()
  188. _, ok = slice.data[id]
  189. slice.m.Unlock()
  190. return ok
  191. }
  192. func GetBlockTree(id string) (ret *BlockTree) {
  193. if "" == id {
  194. return
  195. }
  196. hash := btHash(id)
  197. val, ok := blockTrees.Load(hash)
  198. if !ok {
  199. return
  200. }
  201. slice := val.(*btSlice)
  202. slice.m.Lock()
  203. ret = slice.data[id]
  204. slice.m.Unlock()
  205. return
  206. }
  207. func SetBlockTreePath(tree *parse.Tree) {
  208. RemoveBlockTreesByRootID(tree.ID)
  209. IndexBlockTree(tree)
  210. }
  211. func RemoveBlockTreesByRootID(rootID string) {
  212. var ids []string
  213. blockTrees.Range(func(key, value interface{}) bool {
  214. slice := value.(*btSlice)
  215. slice.m.Lock()
  216. for _, b := range slice.data {
  217. if b.RootID == rootID {
  218. ids = append(ids, b.ID)
  219. }
  220. }
  221. slice.m.Unlock()
  222. return true
  223. })
  224. ids = gulu.Str.RemoveDuplicatedElem(ids)
  225. for _, id := range ids {
  226. val, ok := blockTrees.Load(btHash(id))
  227. if !ok {
  228. continue
  229. }
  230. slice := val.(*btSlice)
  231. slice.m.Lock()
  232. delete(slice.data, id)
  233. slice.changed = time.Now()
  234. slice.m.Unlock()
  235. }
  236. }
  237. func GetBlockTreesByPathPrefix(pathPrefix string) (ret []*BlockTree) {
  238. blockTrees.Range(func(key, value interface{}) bool {
  239. slice := value.(*btSlice)
  240. slice.m.Lock()
  241. for _, b := range slice.data {
  242. if strings.HasPrefix(b.Path, pathPrefix) {
  243. ret = append(ret, b)
  244. }
  245. }
  246. slice.m.Unlock()
  247. return true
  248. })
  249. return
  250. }
  251. func GetBlockTreesByRootID(rootID string) (ret []*BlockTree) {
  252. blockTrees.Range(func(key, value interface{}) bool {
  253. slice := value.(*btSlice)
  254. slice.m.Lock()
  255. for _, b := range slice.data {
  256. if b.RootID == rootID {
  257. ret = append(ret, b)
  258. }
  259. }
  260. slice.m.Unlock()
  261. return true
  262. })
  263. return
  264. }
  265. func RemoveBlockTreesByPathPrefix(pathPrefix string) {
  266. var ids []string
  267. blockTrees.Range(func(key, value interface{}) bool {
  268. slice := value.(*btSlice)
  269. slice.m.Lock()
  270. for _, b := range slice.data {
  271. if strings.HasPrefix(b.Path, pathPrefix) {
  272. ids = append(ids, b.ID)
  273. }
  274. }
  275. slice.m.Unlock()
  276. return true
  277. })
  278. ids = gulu.Str.RemoveDuplicatedElem(ids)
  279. for _, id := range ids {
  280. val, ok := blockTrees.Load(btHash(id))
  281. if !ok {
  282. continue
  283. }
  284. slice := val.(*btSlice)
  285. slice.m.Lock()
  286. delete(slice.data, id)
  287. slice.changed = time.Now()
  288. slice.m.Unlock()
  289. }
  290. }
  291. func GetBlockTreesByBoxID(boxID string) (ret []*BlockTree) {
  292. blockTrees.Range(func(key, value interface{}) bool {
  293. slice := value.(*btSlice)
  294. slice.m.Lock()
  295. for _, b := range slice.data {
  296. if b.BoxID == boxID {
  297. ret = append(ret, b)
  298. }
  299. }
  300. slice.m.Unlock()
  301. return true
  302. })
  303. return
  304. }
  305. func RemoveBlockTreesByBoxID(boxID string) (ids []string) {
  306. blockTrees.Range(func(key, value interface{}) bool {
  307. slice := value.(*btSlice)
  308. slice.m.Lock()
  309. for _, b := range slice.data {
  310. if b.BoxID == boxID {
  311. ids = append(ids, b.ID)
  312. }
  313. }
  314. slice.m.Unlock()
  315. return true
  316. })
  317. ids = gulu.Str.RemoveDuplicatedElem(ids)
  318. for _, id := range ids {
  319. val, ok := blockTrees.Load(btHash(id))
  320. if !ok {
  321. continue
  322. }
  323. slice := val.(*btSlice)
  324. slice.m.Lock()
  325. delete(slice.data, id)
  326. slice.changed = time.Now()
  327. slice.m.Unlock()
  328. }
  329. return
  330. }
  331. func RemoveBlockTree(id string) {
  332. val, ok := blockTrees.Load(btHash(id))
  333. if !ok {
  334. return
  335. }
  336. slice := val.(*btSlice)
  337. slice.m.Lock()
  338. delete(slice.data, id)
  339. slice.changed = time.Now()
  340. slice.m.Unlock()
  341. }
  342. func IndexBlockTree(tree *parse.Tree) {
  343. var changedNodes []*ast.Node
  344. ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  345. if !entering || !n.IsBlock() {
  346. return ast.WalkContinue
  347. }
  348. if "" == n.ID {
  349. return ast.WalkContinue
  350. }
  351. hash := btHash(n.ID)
  352. val, ok := blockTrees.Load(hash)
  353. if !ok {
  354. val = &btSlice{data: map[string]*BlockTree{}, changed: time.Time{}, m: &sync.Mutex{}}
  355. blockTrees.Store(hash, val)
  356. }
  357. slice := val.(*btSlice)
  358. slice.m.Lock()
  359. bt := slice.data[n.ID]
  360. slice.m.Unlock()
  361. if nil != bt {
  362. if bt.Updated != n.IALAttr("updated") || bt.Type != TypeAbbr(n.Type.String()) || bt.Path != tree.Path || bt.BoxID != tree.Box || bt.HPath != tree.HPath {
  363. children := ChildBlockNodes(n) // 需要考虑子块,因为一些操作(比如移动块)后需要同时更新子块
  364. changedNodes = append(changedNodes, children...)
  365. }
  366. } else {
  367. children := ChildBlockNodes(n)
  368. changedNodes = append(changedNodes, children...)
  369. }
  370. return ast.WalkContinue
  371. })
  372. for _, n := range changedNodes {
  373. updateBtSlice(n, tree)
  374. }
  375. }
  376. func updateBtSlice(n *ast.Node, tree *parse.Tree) {
  377. var parentID string
  378. if nil != n.Parent {
  379. parentID = n.Parent.ID
  380. }
  381. hash := btHash(n.ID)
  382. val, ok := blockTrees.Load(hash)
  383. if !ok {
  384. val = &btSlice{data: map[string]*BlockTree{}, changed: time.Time{}, m: &sync.Mutex{}}
  385. blockTrees.Store(hash, val)
  386. }
  387. slice := val.(*btSlice)
  388. slice.m.Lock()
  389. slice.data[n.ID] = &BlockTree{ID: n.ID, ParentID: parentID, RootID: tree.ID, BoxID: tree.Box, Path: tree.Path, HPath: tree.HPath, Updated: n.IALAttr("updated"), Type: TypeAbbr(n.Type.String())}
  390. slice.changed = time.Now()
  391. slice.m.Unlock()
  392. }
  393. var blockTreeLock = sync.Mutex{}
  394. func InitBlockTree(force bool) {
  395. blockTreeLock.Lock()
  396. defer blockTreeLock.Unlock()
  397. start := time.Now()
  398. if force {
  399. err := os.RemoveAll(util.BlockTreePath)
  400. if nil != err {
  401. logging.LogErrorf("remove block tree file failed: %s", err)
  402. }
  403. blockTrees = &sync.Map{}
  404. return
  405. }
  406. entries, err := os.ReadDir(util.BlockTreePath)
  407. if nil != err {
  408. logging.LogErrorf("read block tree dir failed: %s", err)
  409. os.Exit(logging.ExitCodeFileSysErr)
  410. return
  411. }
  412. loadErr := atomic.Bool{}
  413. size := atomic.Int64{}
  414. waitGroup := &sync.WaitGroup{}
  415. p, _ := ants.NewPoolWithFunc(4, func(arg interface{}) {
  416. defer waitGroup.Done()
  417. entry := arg.(os.DirEntry)
  418. p := filepath.Join(util.BlockTreePath, entry.Name())
  419. f, err := os.OpenFile(p, os.O_RDONLY, 0644)
  420. if nil != err {
  421. logging.LogErrorf("open block tree failed: %s", err)
  422. loadErr.Store(true)
  423. return
  424. }
  425. defer f.Close()
  426. info, err := f.Stat()
  427. if nil != err {
  428. logging.LogErrorf("stat block tree failed: %s", err)
  429. loadErr.Store(true)
  430. return
  431. }
  432. size.Add(info.Size())
  433. sliceData := map[string]*BlockTree{}
  434. if err = msgpack.NewDecoder(f).Decode(&sliceData); nil != err {
  435. logging.LogErrorf("unmarshal block tree failed: %s", err)
  436. loadErr.Store(true)
  437. return
  438. }
  439. name := entry.Name()[0:strings.Index(entry.Name(), ".")]
  440. blockTrees.Store(name, &btSlice{data: sliceData, changed: time.Time{}, m: &sync.Mutex{}})
  441. })
  442. for _, entry := range entries {
  443. if !strings.HasSuffix(entry.Name(), ".msgpack") {
  444. continue
  445. }
  446. waitGroup.Add(1)
  447. p.Invoke(entry)
  448. }
  449. waitGroup.Wait()
  450. p.Release()
  451. if loadErr.Load() {
  452. logging.LogInfof("cause block tree load error, remove block tree file")
  453. if removeErr := os.RemoveAll(util.BlockTreePath); nil != removeErr {
  454. logging.LogErrorf("remove block tree file failed: %s", removeErr)
  455. os.Exit(logging.ExitCodeFileSysErr)
  456. return
  457. }
  458. blockTrees = &sync.Map{}
  459. return
  460. }
  461. elapsed := time.Since(start).Seconds()
  462. logging.LogInfof("read block tree [%s] to [%s], elapsed [%.2fs]", humanize.BytesCustomCeil(uint64(size.Load()), 2), util.BlockTreePath, elapsed)
  463. return
  464. }
  465. func SaveBlockTreeJob() {
  466. SaveBlockTree(false)
  467. }
  468. func SaveBlockTree(force bool) {
  469. blockTreeLock.Lock()
  470. defer blockTreeLock.Unlock()
  471. if task.ContainIndexTask() {
  472. //logging.LogInfof("skip saving block tree because indexing")
  473. return
  474. }
  475. //logging.LogInfof("saving block tree")
  476. start := time.Now()
  477. if err := os.MkdirAll(util.BlockTreePath, 0755); nil != err {
  478. logging.LogErrorf("create block tree dir [%s] failed: %s", util.BlockTreePath, err)
  479. os.Exit(logging.ExitCodeFileSysErr)
  480. return
  481. }
  482. size := uint64(0)
  483. var count int
  484. blockTrees.Range(func(key, value interface{}) bool {
  485. slice := value.(*btSlice)
  486. slice.m.Lock()
  487. if !force && slice.changed.IsZero() {
  488. slice.m.Unlock()
  489. return true
  490. }
  491. data, err := msgpack.Marshal(slice.data)
  492. if nil != err {
  493. logging.LogErrorf("marshal block tree failed: %s", err)
  494. os.Exit(logging.ExitCodeFileSysErr)
  495. return false
  496. }
  497. slice.m.Unlock()
  498. p := filepath.Join(util.BlockTreePath, key.(string)) + ".msgpack"
  499. if err = gulu.File.WriteFileSafer(p, data, 0644); nil != err {
  500. logging.LogErrorf("write block tree failed: %s", err)
  501. os.Exit(logging.ExitCodeFileSysErr)
  502. return false
  503. }
  504. slice.m.Lock()
  505. slice.changed = time.Time{}
  506. slice.m.Unlock()
  507. size += uint64(len(data))
  508. count++
  509. return true
  510. })
  511. if 0 < count {
  512. //logging.LogInfof("wrote block trees [%d]", count)
  513. }
  514. elapsed := time.Since(start).Seconds()
  515. if 2 < elapsed {
  516. logging.LogWarnf("save block tree [size=%s] to [%s], elapsed [%.2fs]", humanize.BytesCustomCeil(size, 2), util.BlockTreePath, elapsed)
  517. }
  518. }
  519. func CeilTreeCount(count int) int {
  520. if 100 > count {
  521. return 100
  522. }
  523. for i := 1; i < 40; i++ {
  524. if count < i*500 {
  525. return i * 500
  526. }
  527. }
  528. return 500*40 + 1
  529. }
  530. func CeilBlockCount(count int) int {
  531. if 5000 > count {
  532. return 5000
  533. }
  534. for i := 1; i < 100; i++ {
  535. if count < i*10000 {
  536. return i * 10000
  537. }
  538. }
  539. return 10000*100 + 1
  540. }
  541. func btHash(id string) string {
  542. return util2.Hash([]byte(id))[0:2]
  543. }