Skip to content

Commit

Permalink
⚡ Improve performance of importing .sy.zip siyuan-note#10874
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Apr 4, 2024
1 parent 44d41a0 commit d937056
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
10 changes: 6 additions & 4 deletions kernel/model/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,18 @@ func ImportSY(zipPath, boxID, toPath string) (err error) {

// 新 ID 保留时间部分,仅修改随机值,避免时间变化导致更新时间早于创建时间
// Keep original creation time when importing .sy.zip https://github.com/siyuan-note/siyuan/issues/9923
newNodeID := util.TimeFromID(n.ID) + "-" + gulu.Rand.String(7)
newNodeID := util.TimeFromID(n.ID) + "-" + util.RandString(7)
blockIDs[n.ID] = newNodeID
oldNodeID := n.ID
n.ID = newNodeID
n.SetIALAttr("id", newNodeID)

// 重新指向数据库属性值
ial := parse.IAL2Map(n.KramdownIAL)
for k, _ := range ial {
if strings.HasPrefix(k, av.NodeAttrNameAvs) {
for _, kv := range n.KramdownIAL {
if 2 > len(kv) {
continue
}
if strings.HasPrefix(kv[0], av.NodeAttrNameAvs) {
avBlockIDs[oldNodeID] = newNodeID
}
}
Expand Down
18 changes: 18 additions & 0 deletions kernel/util/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,31 @@ package util

import (
"bytes"
"math/rand"
"strconv"
"strings"
"time"
"unicode"

"github.com/88250/lute/html"
)

func init() {
rand.Seed(time.Now().UTC().UnixNano())
}

var (
letter = []rune("abcdefghijklmnopqrstuvwxyz0123456789")
)

func RandString(length int) string {
b := make([]rune, length)
for i := range b {
b[i] = letter[rand.Intn(len(letter))]
}
return string(b)
}

// InsertElem inserts value at index into a.
// 0 <= index <= len(s)
func InsertElem[T any](s []T, index int, value T) []T {
Expand Down

0 comments on commit d937056

Please sign in to comment.