-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
mirror.go
209 lines (177 loc) · 5.07 KB
/
mirror.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package av
import (
"os"
"path/filepath"
"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"
"github.com/vmihailenco/msgpack/v5"
)
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()
blocks := filepath.Join(util.DataDir, "storage", "av", "blocks.msgpack")
if !filelock.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, existBlockTree func(string) bool) (ret bool) {
AttributeViewBlocksLock.Lock()
defer AttributeViewBlocksLock.Unlock()
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
}
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 {
if existBlockTree(v) {
newBlockIDs = append(newBlockIDs, v)
}
}
}
avBlocks[avID] = newBlockIDs
ret = len(newBlockIDs) != len(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
}
return
}
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) (ret bool) {
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
}
}
blockIDs := avBlocks[avID]
oldLen := len(blockIDs)
blockIDs = append(blockIDs, blockID)
blockIDs = gulu.Str.RemoveDuplicatedElem(blockIDs)
avBlocks[avID] = blockIDs
ret = oldLen != len(blockIDs) && 0 != oldLen
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
}
return
}