|
@@ -25,7 +25,6 @@ import (
|
|
"path/filepath"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strconv"
|
|
"strings"
|
|
"strings"
|
|
- "sync"
|
|
|
|
"time"
|
|
"time"
|
|
|
|
|
|
"github.com/88250/gulu"
|
|
"github.com/88250/gulu"
|
|
@@ -33,7 +32,6 @@ import (
|
|
"github.com/siyuan-note/filelock"
|
|
"github.com/siyuan-note/filelock"
|
|
"github.com/siyuan-note/logging"
|
|
"github.com/siyuan-note/logging"
|
|
"github.com/siyuan-note/siyuan/kernel/util"
|
|
"github.com/siyuan-note/siyuan/kernel/util"
|
|
- "github.com/vmihailenco/msgpack/v5"
|
|
|
|
"golang.org/x/text/language"
|
|
"golang.org/x/text/language"
|
|
"golang.org/x/text/message"
|
|
"golang.org/x/text/message"
|
|
)
|
|
)
|
|
@@ -586,120 +584,6 @@ func NewAttributeView(id string) (ret *AttributeView) {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
-var (
|
|
|
|
- attributeViewBlocksLock = sync.Mutex{}
|
|
|
|
-)
|
|
|
|
-
|
|
|
|
-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
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
func ParseAttributeView(avID string) (ret *AttributeView, err error) {
|
|
func ParseAttributeView(avID string) (ret *AttributeView, err error) {
|
|
avJSONPath := GetAttributeViewDataPath(avID)
|
|
avJSONPath := GetAttributeViewDataPath(avID)
|
|
if !gulu.File.IsExist(avJSONPath) {
|
|
if !gulu.File.IsExist(avJSONPath) {
|