Skip to content

Commit

Permalink
修改id生成器的名字
Browse files Browse the repository at this point in the history
  • Loading branch information
juniaoshaonian authored and flycash committed Jul 27, 2024
1 parent 8235ebb commit 4be9510
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 18 deletions.
18 changes: 5 additions & 13 deletions internal/pkg/snowflake/snowflake.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand All @@ -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)
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/snowflake/snowflake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ 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)
})
}

}

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++ {
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions internal/user/internal/repository/dao/user_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 4be9510

Please sign in to comment.