From 4be9510eb9564e58fcf0e4e8969cf848dcca62cf Mon Sep 17 00:00:00 2001 From: zwl <1633720889@qq.com> Date: Mon, 22 Jul 2024 21:44:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9id=E7=94=9F=E6=88=90=E5=99=A8?= =?UTF-8?q?=E7=9A=84=E5=90=8D=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/pkg/snowflake/snowflake.go | 18 +++++------------- internal/pkg/snowflake/snowflake_test.go | 6 +++--- .../internal/repository/dao/user_callback.go | 4 ++-- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/internal/pkg/snowflake/snowflake.go b/internal/pkg/snowflake/snowflake.go index 77a75ca2..2a851a40 100644 --- a/internal/pkg/snowflake/snowflake.go +++ b/internal/pkg/snowflake/snowflake.go @@ -20,15 +20,7 @@ func (f ID) Int64() int64 { return int64(f) } -type AppIDGenerator interface { - // Generate 功能生成雪花id(ID)。返回雪花id(ID)的每一位的组成如下。返回值ID可以通过AppID()返回生成时输入的appid。 - // +---------------------------------------------------------------------------------------+ - // | 1 Bit Unused | 41 Bit Timestamp | 5 Bit APPID | 5 Bit NodeID | 12 Bit Sequence ID | - // +---------------------------------------------------------------------------------------+ - Generate(appid uint) (ID, error) -} - -type MeoyingIDGenerator struct { +type UIDGenerator struct { // 键为appid nodes *syncx.Map[uint, *snowflake.Node] } @@ -44,8 +36,8 @@ var ( ErrUnknownApp = errors.New("未知的app") ) -// NewMeoyingIDGenerator nodeId表示第几个节点,apps表示有几个应用 从0开始排序 0-webook 1-ielts 最多到31 -func NewMeoyingIDGenerator(nodeId uint, apps uint) (*MeoyingIDGenerator, error) { +// NewUIDGenerator nodeId表示第几个节点,apps表示有几个应用 从0开始排序 0-webook 1-ielts 最多到31 +func NewUIDGenerator(nodeId uint, apps uint) (*UIDGenerator, error) { nodeMap := &syncx.Map[uint, *snowflake.Node]{} if nodeId > maxNodeNum { return nil, fmt.Errorf("%w", ErrExceedNode) @@ -61,13 +53,13 @@ func NewMeoyingIDGenerator(nodeId uint, apps uint) (*MeoyingIDGenerator, error) } nodeMap.Store(uint(i), n) } - return &MeoyingIDGenerator{ + return &UIDGenerator{ nodes: nodeMap, }, nil } -func (c *MeoyingIDGenerator) Generate(appid uint) (ID, error) { +func (c *UIDGenerator) Generate(appid uint) (ID, error) { n, ok := c.nodes.Load(appid) if !ok { return 0, fmt.Errorf("%w", ErrUnknownApp) diff --git a/internal/pkg/snowflake/snowflake_test.go b/internal/pkg/snowflake/snowflake_test.go index 7e196629..85f30a47 100644 --- a/internal/pkg/snowflake/snowflake_test.go +++ b/internal/pkg/snowflake/snowflake_test.go @@ -39,7 +39,7 @@ func Test_NewGenerate(t *testing.T) { } for _, tt := range testcases { t.Run(tt.name, func(t *testing.T) { - _, err := NewMeoyingIDGenerator(tt.nodeId, tt.apps) + _, err := NewUIDGenerator(tt.nodeId, tt.apps) tt.wantErrFunc(t, err) }) } @@ -47,7 +47,7 @@ func Test_NewGenerate(t *testing.T) { } func Test_Generate(t *testing.T) { - idmaker, err := NewMeoyingIDGenerator(1, 6) + idmaker, err := NewUIDGenerator(1, 6) require.NoError(t, err) ids := make([]int64, 0) for i := 0; i < 6; i++ { @@ -68,7 +68,7 @@ func Test_Generate(t *testing.T) { } func Test_GenerateAppId(t *testing.T) { - idmaker, err := NewMeoyingIDGenerator(1, 16) + idmaker, err := NewUIDGenerator(1, 16) require.NoError(t, err) testcases := []struct { name string diff --git a/internal/user/internal/repository/dao/user_callback.go b/internal/user/internal/repository/dao/user_callback.go index 9ea1962f..a25f04bd 100644 --- a/internal/user/internal/repository/dao/user_callback.go +++ b/internal/user/internal/repository/dao/user_callback.go @@ -27,11 +27,11 @@ var ( type UserInsertCallBackBuilder struct { logger *elog.Component - idMaker snowflake.AppIDGenerator + idMaker *snowflake.UIDGenerator } func NewUserInsertCallBackBuilder(nodeid, apps uint) (*UserInsertCallBackBuilder, error) { - idMaker, err := snowflake.NewMeoyingIDGenerator(nodeid, apps) + idMaker, err := snowflake.NewUIDGenerator(nodeid, apps) if err != nil { return nil, err }