This commit is contained in:
Daniel 2024-10-09 16:38:43 +08:00
parent 97eb7de2c3
commit 7028759499
No known key found for this signature in database
GPG key ID: 86211BA83DF03017

View file

@ -43,18 +43,19 @@ func LoadTrees(ids []string) (ret map[string]*parse.Tree) {
luteEngine := util.NewLute() luteEngine := util.NewLute()
var boxIDs []string var boxIDs []string
var paths []string var paths []string
seen := make(map[string]bool) blockIDs := map[string]string{}
seen := map[string]bool{}
for _, bt := range bts { for _, bt := range bts {
key := bt.BoxID + bt.Path key := bt.BoxID + bt.Path
if !seen[key] { if !seen[key] {
seen[key] = true seen[key] = true
boxIDs = append(boxIDs, bt.BoxID) boxIDs = append(boxIDs, bt.BoxID)
paths = append(paths, bt.Path) paths = append(paths, bt.Path)
blockIDs[bt.RootID] = bt.ID
} }
} }
trees, errs := BatchLoadTrees(boxIDs, paths, luteEngine) trees, errs := batchLoadTrees(boxIDs, paths, luteEngine)
for i := range trees { for i := range trees {
tree := trees[i] tree := trees[i]
err := errs[i] err := errs[i]
@ -63,7 +64,8 @@ func LoadTrees(ids []string) (ret map[string]*parse.Tree) {
continue continue
} }
ret[tree.ID] = tree id := blockIDs[tree.Root.ID]
ret[id] = tree
} }
return return
} }
@ -80,11 +82,9 @@ func LoadTree(boxID, p string, luteEngine *lute.Lute) (ret *parse.Tree, err erro
return return
} }
func BatchLoadTrees(boxIDs, paths []string, luteEngine *lute.Lute) ([]*parse.Tree, []error) { func batchLoadTrees(boxIDs, paths []string, luteEngine *lute.Lute) (ret []*parse.Tree, errs []error) {
results := make([]*parse.Tree, len(paths))
errs := make([]error, len(paths))
var wg sync.WaitGroup var wg sync.WaitGroup
lock := sync.Mutex{}
for i := range paths { for i := range paths {
wg.Add(1) wg.Add(1)
go func(i int) { go func(i int) {
@ -93,13 +93,14 @@ func BatchLoadTrees(boxIDs, paths []string, luteEngine *lute.Lute) ([]*parse.Tre
boxID := boxIDs[i] boxID := boxIDs[i]
path := paths[i] path := paths[i]
tree, err := LoadTree(boxID, path, luteEngine) tree, err := LoadTree(boxID, path, luteEngine)
results[i] = tree lock.Lock()
errs[i] = err ret = append(ret, tree)
errs = append(errs, err)
lock.Unlock()
}(i) }(i)
} }
wg.Wait() wg.Wait()
return
return results, errs
} }
func LoadTreeByData(data []byte, boxID, p string, luteEngine *lute.Lute) (ret *parse.Tree, err error) { func LoadTreeByData(data []byte, boxID, p string, luteEngine *lute.Lute) (ret *parse.Tree, err error) {