-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from ccpwcn/dev
新增生成NanoID
- Loading branch information
Showing
3 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package kgo | ||
|
||
import ( | ||
"crypto/rand" | ||
"fmt" | ||
"math" | ||
) | ||
|
||
const ( | ||
defaultNanoIdSize = 21 | ||
defaultAlphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-" | ||
) | ||
|
||
// NanoId 生成一个默认大小的NanoID | ||
func NanoId() (string, error) { | ||
return newNanoIdWithSize(defaultNanoIdSize) | ||
} | ||
|
||
// MustNanoId 生成一个默认大小的NanoID,如果中途出现错误,抛出 panic | ||
func MustNanoId() string { | ||
id, err := newNanoIdWithSize(defaultNanoIdSize) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return id | ||
} | ||
|
||
func newNanoIdWithSize(size int) (string, error) { | ||
return newNanoIdWithAlphabet(size, defaultAlphabet) | ||
} | ||
|
||
func newNanoIdWithAlphabet(size int, alphabet string) (string, error) { | ||
if alphabet == "" || len(alphabet) >= 256 { | ||
return "", fmt.Errorf("alphabet must contain between 1 and 255 symbols") | ||
} | ||
|
||
if size <= 0 { | ||
return "", fmt.Errorf("size must be greater than zero") | ||
} | ||
|
||
mask := (2 << (int)(math.Log2(float64(len(alphabet)-1)))) - 1 | ||
step := int(math.Ceil(1.6 * float64(mask) * float64(size) / float64(len(alphabet)))) | ||
|
||
idBuilder := make([]byte, 0, size) | ||
bytes := make([]byte, step) | ||
|
||
for { | ||
_, err := rand.Read(bytes) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
for _, b := range bytes { | ||
alphabetIndex := int(b) & mask | ||
|
||
if alphabetIndex < len(alphabet) { | ||
idBuilder = append(idBuilder, alphabet[alphabetIndex]) | ||
if len(idBuilder) == size { | ||
return B2S(idBuilder), nil | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package kgo | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func TestMustNanoID(t *testing.T) { | ||
for i := 0; i < 10; i++ { | ||
t.Log(MustNanoId()) | ||
} | ||
} | ||
|
||
func TestNormalNanoID(t *testing.T) { | ||
for i := 0; i < 20; i++ { | ||
if id, err := NanoId(); err != nil { | ||
t.Error(err) | ||
} else { | ||
t.Log(id) | ||
} | ||
} | ||
} | ||
|
||
func TestNormalNanoID_CheckDuplicate(t *testing.T) { | ||
const count = 100_0000 | ||
ids := make(map[string]bool, count) | ||
for i := 0; i < count; i++ { | ||
if id, err := NanoId(); err != nil { | ||
t.Error(err) | ||
} else if ids[id] { | ||
t.Error("duplicate id", id) | ||
} | ||
} | ||
} | ||
|
||
func TestNormalNanoID_CheckDuplicate5(t *testing.T) { | ||
const count = 500_0000 | ||
ids := make(map[string]bool, count) | ||
for i := 0; i < count; i++ { | ||
if id, err := NanoId(); err != nil { | ||
t.Error(err) | ||
} else if ids[id] { | ||
t.Error("duplicate id", id) | ||
} | ||
} | ||
} | ||
|
||
func TestNormalNanoIDCheck_Duplicate10(t *testing.T) { | ||
const count = 1000_0000 | ||
ids := make(map[string]bool, count) | ||
for i := 0; i < count; i++ { | ||
if id, err := NanoId(); err != nil { | ||
t.Error(err) | ||
} else if ids[id] { | ||
t.Error("duplicate id", id) | ||
} | ||
} | ||
} | ||
|
||
func TestNormalNanoIDCheck_Duplicate100(t *testing.T) { | ||
const count = 1_0000_0000 | ||
ids := make(map[string]bool, count) | ||
for i := 0; i < count; i++ { | ||
if id, err := NanoId(); err != nil { | ||
t.Error(err) | ||
} else if ids[id] { | ||
t.Error("duplicate id", id) | ||
} | ||
} | ||
} | ||
|
||
func Benchmark_NanoId(b *testing.B) { | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
var buffer sync.Map | ||
for j := 0; j < 500_0000; j++ { | ||
b.RunParallel(func(pb *testing.PB) { | ||
for pb.Next() { | ||
id := MustNanoId() | ||
if _, ok := buffer.Load(id); ok { | ||
b.Errorf("duplicated UUID %s", id) | ||
} | ||
buffer.Store(id, true) | ||
} | ||
}) | ||
} | ||
} | ||
} |