Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
Vanessa 2023-11-02 16:25:46 +08:00
commit 6a01eca882
4 changed files with 158 additions and 91 deletions

View file

@ -21,6 +21,7 @@ import (
"github.com/88250/gulu"
"github.com/gin-gonic/gin"
"github.com/siyuan-note/siyuan/kernel/av"
"github.com/siyuan-note/siyuan/kernel/model"
"github.com/siyuan-note/siyuan/kernel/util"
)
@ -60,6 +61,7 @@ func renderAttributeView(c *gin.Context) {
"viewID": view.GetID(),
"views": views,
"view": view,
"isMirror": av.IsMirror(attrView.ID),
}
}

View file

@ -25,7 +25,6 @@ import (
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/88250/gulu"
@ -33,7 +32,6 @@ import (
"github.com/siyuan-note/filelock"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/util"
"github.com/vmihailenco/msgpack/v5"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
@ -586,95 +584,6 @@ func NewAttributeView(id string) (ret *AttributeView) {
return
}
var (
attributeViewBlocksLock = sync.Mutex{}
)
func RemoveBlockRel(avID, blockID string) {
attributeViewBlocksLock.Lock()
defer attributeViewBlocksLock.Unlock()
blocks := filepath.Join(util.DataDir, "storage", "av", "blocks.msgpack")
if !gulu.File.IsExist(blocks) {
return
}
data, err := filelock.ReadFile(blocks)
if nil != err {
logging.LogErrorf("read attribute view blocks failed: %s", err)
return
}
avBlocks := map[string][]string{}
if err = msgpack.Unmarshal(data, &avBlocks); nil != err {
logging.LogErrorf("unmarshal attribute view blocks failed: %s", err)
return
}
blockIDs := avBlocks[avID]
if nil == blockIDs {
return
}
var newBlockIDs []string
for _, v := range blockIDs {
if v != blockID {
newBlockIDs = append(newBlockIDs, v)
}
}
avBlocks[avID] = newBlockIDs
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()
avBlocks := map[string][]string{}
blocks := filepath.Join(util.DataDir, "storage", "av", "blocks.msgpack")
if !gulu.File.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
}
}
blockIDs := avBlocks[avID]
blockIDs = append(blockIDs, blockID)
blockIDs = gulu.Str.RemoveDuplicatedElem(blockIDs)
avBlocks[avID] = 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 ParseAttributeView(avID string) (ret *AttributeView, err error) {
avJSONPath := GetAttributeViewDataPath(avID)
if !gulu.File.IsExist(avJSONPath) {

152
kernel/av/mirror.go Normal file
View file

@ -0,0 +1,152 @@
package av
import (
"os"
"path/filepath"
"sync"
"github.com/88250/gulu"
"github.com/siyuan-note/filelock"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/util"
"github.com/vmihailenco/msgpack/v5"
)
var (
attributeViewBlocksLock = sync.Mutex{}
)
func GetMirrorBlockIDs(avID string) []string {
attributeViewBlocksLock.Lock()
defer attributeViewBlocksLock.Unlock()
blocks := filepath.Join(util.DataDir, "storage", "av", "blocks.msgpack")
if !gulu.File.IsExist(blocks) {
return nil
}
data, err := filelock.ReadFile(blocks)
if nil != err {
logging.LogErrorf("read attribute view blocks failed: %s", err)
return nil
}
avBlocks := map[string][]string{}
if err = msgpack.Unmarshal(data, &avBlocks); nil != err {
logging.LogErrorf("unmarshal attribute view blocks failed: %s", err)
return nil
}
blockIDs := avBlocks[avID]
return blockIDs
}
func IsMirror(avID string) bool {
attributeViewBlocksLock.Lock()
defer attributeViewBlocksLock.Unlock()
blocks := filepath.Join(util.DataDir, "storage", "av", "blocks.msgpack")
if !gulu.File.IsExist(blocks) {
return false
}
data, err := filelock.ReadFile(blocks)
if nil != err {
logging.LogErrorf("read attribute view blocks failed: %s", err)
return false
}
avBlocks := map[string][]string{}
if err = msgpack.Unmarshal(data, &avBlocks); nil != err {
logging.LogErrorf("unmarshal attribute view blocks failed: %s", err)
return false
}
blockIDs := avBlocks[avID]
return nil != blockIDs && 1 < len(blockIDs)
}
func RemoveBlockRel(avID, blockID string) {
attributeViewBlocksLock.Lock()
defer attributeViewBlocksLock.Unlock()
blocks := filepath.Join(util.DataDir, "storage", "av", "blocks.msgpack")
if !gulu.File.IsExist(blocks) {
return
}
data, err := filelock.ReadFile(blocks)
if nil != err {
logging.LogErrorf("read attribute view blocks failed: %s", err)
return
}
avBlocks := map[string][]string{}
if err = msgpack.Unmarshal(data, &avBlocks); nil != err {
logging.LogErrorf("unmarshal attribute view blocks failed: %s", err)
return
}
blockIDs := avBlocks[avID]
if nil == blockIDs {
return
}
var newBlockIDs []string
for _, v := range blockIDs {
if v != blockID {
newBlockIDs = append(newBlockIDs, v)
}
}
avBlocks[avID] = newBlockIDs
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()
avBlocks := map[string][]string{}
blocks := filepath.Join(util.DataDir, "storage", "av", "blocks.msgpack")
if !gulu.File.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
}
}
blockIDs := avBlocks[avID]
blockIDs = append(blockIDs, blockID)
blockIDs = gulu.Str.RemoveDuplicatedElem(blockIDs)
avBlocks[avID] = 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
}
}

View file

@ -36,6 +36,7 @@ import (
type BlockAttributeViewKeys struct {
AvID string `json:"avID"`
AvName string `json:"avName"`
BlockIDs []string `json:"blockIDs"`
KeyValues []*av.KeyValues `json:"keyValues"`
}
@ -140,9 +141,12 @@ func GetBlockAttributeViewKeys(blockID string) (ret []*BlockAttributeViewKeys) {
})
}
blockIDs := av.GetMirrorBlockIDs(avID)
ret = append(ret, &BlockAttributeViewKeys{
AvID: avID,
AvName: attrView.Name,
BlockIDs: blockIDs,
KeyValues: keyValues,
})
}