From 7abebedddd685b219cf10581efdfa805bd24db58 Mon Sep 17 00:00:00 2001 From: Sydonian <794346190@qq.com> Date: Thu, 27 Mar 2025 17:03:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8A=9F=E8=83=BD=E5=87=BD?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkgs/logger/global_logger.go | 8 ++++++++ pkgs/trie/trie.go | 18 ++++++++++++------ sdks/storage/models.go | 16 +++++++++------- sdks/storage/utils.go | 5 +++++ utils/lo2/lo.go | 21 +++++++++++++++++++++ utils/math2/math.go | 12 ++++++++++++ 6 files changed, 67 insertions(+), 13 deletions(-) diff --git a/pkgs/logger/global_logger.go b/pkgs/logger/global_logger.go index 1a577cc..6d07e73 100644 --- a/pkgs/logger/global_logger.go +++ b/pkgs/logger/global_logger.go @@ -83,6 +83,14 @@ func Init(cfg *Config) error { // 下面是日志记录的方法,它们分别对应不同的日志级别和格式。 // 这些方法最终都会调用logrus对应的方法来记录日志。 +func Trace(args ...interface{}) { + logrus.Trace(args...) +} + +func Tracef(format string, args ...interface{}) { + logrus.Tracef(format, args...) +} + func Debug(args ...interface{}) { logrus.Debug(args...) } diff --git a/pkgs/trie/trie.go b/pkgs/trie/trie.go index 829a49f..4f5d2f9 100644 --- a/pkgs/trie/trie.go +++ b/pkgs/trie/trie.go @@ -102,10 +102,15 @@ func (n *Node[T]) RemoveSelf(cleanParent bool) { } // 修改时需要注意允许在visitorFn中删除当前节点 -func (n *Node[T]) Iterate(visitorFn func(word string, node *Node[T], isWordNode bool) VisitCtrl) { +func (n *Node[T]) Iterate(visitorFn func(path []string, node *Node[T], isWordNode bool) VisitCtrl) { + n.iterate([]string{}, visitorFn) +} + +func (n *Node[T]) iterate(path []string, visitorFn func(path []string, node *Node[T], isWordNode bool) VisitCtrl) { if n.WordNexts != nil { for word, node := range n.WordNexts { - ret := visitorFn(word, node, true) + newPath := append(path, word) + ret := visitorFn(newPath, node, true) if ret == VisitBreak { return } @@ -114,12 +119,13 @@ func (n *Node[T]) Iterate(visitorFn func(word string, node *Node[T], isWordNode continue } - node.Iterate(visitorFn) + node.iterate(newPath, visitorFn) } } if n.AnyNext != nil { - ret := visitorFn("", n.AnyNext, false) + newPath := append(path, "") + ret := visitorFn(newPath, n.AnyNext, false) if ret == VisitBreak { return } @@ -128,7 +134,7 @@ func (n *Node[T]) Iterate(visitorFn func(word string, node *Node[T], isWordNode return } - n.AnyNext.Iterate(visitorFn) + n.AnyNext.iterate(newPath, visitorFn) } } @@ -198,6 +204,6 @@ func (t *Trie[T]) CreateWords(words []string) *Node[T] { return ptr } -func (n *Trie[T]) Iterate(visitorFn func(word string, node *Node[T], isWordNode bool) VisitCtrl) { +func (n *Trie[T]) Iterate(visitorFn func(path []string, node *Node[T], isWordNode bool) VisitCtrl) { n.Root.Iterate(visitorFn) } diff --git a/sdks/storage/models.go b/sdks/storage/models.go index 293618a..ecf4867 100644 --- a/sdks/storage/models.go +++ b/sdks/storage/models.go @@ -257,10 +257,11 @@ const ( ) type Package struct { - PackageID PackageID `gorm:"column:PackageID; primaryKey; type:bigint; autoIncrement" json:"packageID"` - Name string `gorm:"column:Name; type:varchar(255); not null" json:"name"` - BucketID BucketID `gorm:"column:BucketID; type:bigint; not null" json:"bucketID"` - State string `gorm:"column:State; type:varchar(255); not null" json:"state"` + PackageID PackageID `gorm:"column:PackageID; primaryKey; type:bigint; autoIncrement" json:"packageID"` + Name string `gorm:"column:Name; type:varchar(255); not null" json:"name"` + BucketID BucketID `gorm:"column:BucketID; type:bigint; not null" json:"bucketID"` + CreateTime time.Time `gorm:"column:CreateTime; type:datetime; not null" json:"createTime"` + State string `gorm:"column:State; type:varchar(255); not null" json:"state"` } func (Package) TableName() string { @@ -337,9 +338,10 @@ func (PinnedObject) TableName() string { } type Bucket struct { - BucketID BucketID `gorm:"column:BucketID; primaryKey; type:bigint; autoIncrement" json:"bucketID"` - Name string `gorm:"column:Name; type:varchar(255); not null" json:"name"` - CreatorID UserID `gorm:"column:CreatorID; type:bigint; not null" json:"creatorID"` + BucketID BucketID `gorm:"column:BucketID; primaryKey; type:bigint; autoIncrement" json:"bucketID"` + Name string `gorm:"column:Name; type:varchar(255); not null" json:"name"` + CreateTime time.Time `gorm:"column:CreateTime; type:datetime; not null" json:"createTime"` + CreatorID UserID `gorm:"column:CreatorID; type:bigint; not null" json:"creatorID"` } func (Bucket) TableName() string { diff --git a/sdks/storage/utils.go b/sdks/storage/utils.go index 39268dd..94c6813 100644 --- a/sdks/storage/utils.go +++ b/sdks/storage/utils.go @@ -11,3 +11,8 @@ func JoinObjectPath(comps ...string) string { func SplitObjectPath(pat string) []string { return strings.Split(pat, ObjectPathSeparator) } + +func BaseName(pat string) string { + idx := strings.LastIndex(pat, ObjectPathSeparator) + return pat[idx+1:] +} diff --git a/utils/lo2/lo.go b/utils/lo2/lo.go index 930f6d3..41b794c 100644 --- a/utils/lo2/lo.go +++ b/utils/lo2/lo.go @@ -73,3 +73,24 @@ func Deref[T any](arr []*T) []T { return result } + +func AppendNew[T any](arr []T, items ...T) []T { + narr := make([]T, 0, len(arr)+len(items)) + narr = append(narr, arr...) + narr = append(narr, items...) + return narr +} + +func ArrayEquals[T comparable](arr1, arr2 []T) bool { + if len(arr1) != len(arr2) { + return false + } + + for i := 0; i < len(arr1); i++ { + if arr1[i] != arr2[i] { + return false + } + } + + return true +} diff --git a/utils/math2/math.go b/utils/math2/math.go index 4e6d268..d1bab7d 100644 --- a/utils/math2/math.go +++ b/utils/math2/math.go @@ -2,6 +2,18 @@ package math2 import "golang.org/x/exp/constraints" +func Sign[T constraints.Signed](v T) int { + if v > 0 { + return 1 + } + + if v < 0 { + return -1 + } + + return 0 +} + func Max[T constraints.Ordered](v1, v2 T) T { if v1 < v2 { return v2