mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-17 07:00:12 +01:00
🎨 Improve Add to Database search sorting https://github.com/siyuan-note/siyuan/issues/10952
This commit is contained in:
parent
7d680ed391
commit
b3a2c49f15
3 changed files with 141 additions and 41 deletions
|
|
@ -6,6 +6,7 @@ import (
|
|||
"sync"
|
||||
|
||||
"github.com/88250/gulu"
|
||||
"github.com/88250/lute/ast"
|
||||
"github.com/siyuan-note/filelock"
|
||||
"github.com/siyuan-note/logging"
|
||||
"github.com/siyuan-note/siyuan/kernel/util"
|
||||
|
|
@ -16,6 +17,30 @@ var (
|
|||
AttributeViewBlocksLock = sync.Mutex{}
|
||||
)
|
||||
|
||||
func GetBlockRels() (ret map[string][]string) {
|
||||
AttributeViewBlocksLock.Lock()
|
||||
defer AttributeViewBlocksLock.Unlock()
|
||||
|
||||
ret = map[string][]string{}
|
||||
|
||||
blocks := filepath.Join(util.DataDir, "storage", "av", "blocks.msgpack")
|
||||
if !filelock.IsExist(blocks) {
|
||||
return
|
||||
}
|
||||
|
||||
data, err := filelock.ReadFile(blocks)
|
||||
if nil != err {
|
||||
logging.LogErrorf("read attribute view blocks failed: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err = msgpack.Unmarshal(data, &ret); nil != err {
|
||||
logging.LogErrorf("unmarshal attribute view blocks failed: %s", err)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func IsMirror(avID string) bool {
|
||||
AttributeViewBlocksLock.Lock()
|
||||
defer AttributeViewBlocksLock.Unlock()
|
||||
|
|
@ -86,6 +111,56 @@ func RemoveBlockRel(avID, blockID string) {
|
|||
}
|
||||
}
|
||||
|
||||
func BatchUpsertBlockRel(nodes []*ast.Node) {
|
||||
AttributeViewBlocksLock.Lock()
|
||||
defer AttributeViewBlocksLock.Unlock()
|
||||
|
||||
avBlocks := map[string][]string{}
|
||||
blocks := filepath.Join(util.DataDir, "storage", "av", "blocks.msgpack")
|
||||
if !filelock.IsExist(blocks) {
|
||||
if err := os.MkdirAll(filepath.Dir(blocks), 0755); nil != err {
|
||||
logging.LogErrorf("create attribute view dir failed: %s", err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
data, err := filelock.ReadFile(blocks)
|
||||
if nil != err {
|
||||
logging.LogErrorf("read attribute view blocks failed: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err = msgpack.Unmarshal(data, &avBlocks); nil != err {
|
||||
logging.LogErrorf("unmarshal attribute view blocks failed: %s", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
for _, n := range nodes {
|
||||
if ast.NodeAttributeView != n.Type {
|
||||
continue
|
||||
}
|
||||
|
||||
if "" == n.AttributeViewID || "" == n.ID {
|
||||
continue
|
||||
}
|
||||
|
||||
blockIDs := avBlocks[n.AttributeViewID]
|
||||
blockIDs = append(blockIDs, n.ID)
|
||||
blockIDs = gulu.Str.RemoveDuplicatedElem(blockIDs)
|
||||
avBlocks[n.AttributeViewID] = blockIDs
|
||||
}
|
||||
|
||||
data, err := msgpack.Marshal(avBlocks)
|
||||
if nil != err {
|
||||
logging.LogErrorf("marshal attribute view blocks failed: %s", err)
|
||||
return
|
||||
}
|
||||
if err = filelock.WriteFile(blocks, data); nil != err {
|
||||
logging.LogErrorf("write attribute view blocks failed: %s", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func UpsertBlockRel(avID, blockID string) {
|
||||
AttributeViewBlocksLock.Lock()
|
||||
defer AttributeViewBlocksLock.Unlock()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue