Skip to content

Commit

Permalink
improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
houseme committed Feb 15, 2022
1 parent ae703e1 commit 0f5510f
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"sync"
"time"

"github.com/housemecn/snowflake"
"github.com/houseme/snowflake"
)

// TestLoad generate 20k ids
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: Go
name: Snowflake - cli

on:
push:
branches: [ master ]
branches: [ main ]
pull_request:
branches: [ master ]
branches: [ main ]

jobs:

Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@
See the License for the specific language governing permissions and
limitations under the License.

Copyright (c) 2016, Bruce
Copyright (c) 2016, houseme
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# ❄️ GO-Snowflake
# ❄️ Go-Snowflake

[![Go Doc](https://godoc.org/github.com/housemecn/snowflake?status.svg)](https://godoc.org/github.com/housemecn/snowflake)
[![Build Status](https://travis-ci.com/housemecn/snowflake.svg?branch=master)](https://travis-ci.com/housemecn/snowflake)
[![Go Report](https://goreportcard.com/badge/github.com/housemecn/snowflake?v=1)](https://goreportcard.com/report/github.com/housemecn/snowflake)
[![Go Reference](https://pkg.go.dev/badge/github.com/houseme/snowflake.svg)](https://pkg.go.dev/github.com/houseme/snowflake)
[![GoFrame CI](https://github.com/houseme/snowflake/actions/workflows/gf.yml/badge.svg)](https://github.com/houseme/snowflake/actions/workflows/gf.yml)
[![Go Report](https://goreportcard.com/badge/github.com/houseme/snowflake?v=1)](https://goreportcard.com/report/github.com/houseme/snowflake)
[![Production Ready](https://img.shields.io/badge/production-ready-blue.svg)](https://github.com/housemecn/snowflake)
[![License](https://img.shields.io/github/license/housemecn/snowflake.svg?style=flat)](https://github.com/housemecn/snowflake)

Expand All @@ -27,18 +27,19 @@ Twitter设计了Snowflake算法为分布式系统生成ID,Snowflake的id是int64
### 🕹 克隆 & 运行

```bash
git clone https://github.com/housemecn/go-snowflake.git
git clone https://github.com/houseme/snowflake.git

go run ./.example/main.go
```

### 💾 安装 & 导入

```bash
go get github.com/housemecn/snowflake

go get github.com/houseme/snowflake
```
```go
// 在项目中导入模块
import "github.com/housemecn/snowflake"
import "github.com/houseme/snowflake"
```

### ⚠️注意事项
Expand Down Expand Up @@ -92,7 +93,7 @@ func TestLoad() {

> 运行结果
![load](https://github.com/housemecn/go-snowflake/raw/master/docs/[email protected])
![load](https://github.com/houseme/snowflake/raw/master/docs/[email protected])

## 🗂 使用说明

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/housemecn/snowflake
module github.com/houseme/snowflake

go 1.14
go 1.16
80 changes: 40 additions & 40 deletions snowflake.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func init() {
}
}

//Snowflake is a custom type
// Snowflake is a custom type
type Snowflake struct {
sync.Mutex
timestamp int64
Expand All @@ -80,7 +80,7 @@ type Snowflake struct {
sequence int64
}

//NewSnowflake returns a new snowflake node that can be used to generate snowflake
// NewSnowflake returns a new snowflake node that can be used to generate snowflake
func NewSnowflake(datacenterID, workerID int64) (*Snowflake, error) {
if datacenterID < 0 || datacenterID > datacenterIDMax {
return nil, fmt.Errorf("datacenterid must be between 0 and %d", datacenterIDMax-1)
Expand Down Expand Up @@ -136,7 +136,7 @@ func GetDeviceID(sid int64) (datacenterID, workerID int64) {
return
}

//GetTimestamp returns an int64 unix timestamp in milliseconds of the snowflake ID time
// GetTimestamp returns an int64 unix timestamp in milliseconds of the snowflake ID time
func GetTimestamp(sid ID) (timestamp int64) {
timestamp = (int64(sid) >> timestampShift) & timestampMax
return
Expand All @@ -162,8 +162,8 @@ func GetTimestampStatus() (state float64) {
}

// Int64 returns an int64 of the snowflake ID
func (f ID) Int64() int64 {
return int64(f)
func (sid ID) Int64() int64 {
return int64(sid)
}

// ParseInt64 converts an int64 into a snowflake ID
Expand All @@ -172,20 +172,20 @@ func ParseInt64(id int64) ID {
}

// String returns a string of the snowflake ID
func (f ID) String() string {
return strconv.FormatInt(int64(f), 10)
func (sid ID) String() string {
return strconv.FormatInt(int64(sid), 10)
}

// ParseString converts a string into a snowflake ID
func ParseString(id string) (ID, error) {
i, err := strconv.ParseInt(id, 10, 64)
func ParseString(sid string) (ID, error) {
i, err := strconv.ParseInt(sid, 10, 64)
return ID(i), err

}

// Base2 returns a string base2 of the snowflake ID
func (f ID) Base2() string {
return strconv.FormatInt(int64(f), 2)
func (sid ID) Base2() string {
return strconv.FormatInt(int64(sid), 2)
}

// ParseBase2 converts a Base2 string into a snowflake ID
Expand All @@ -198,18 +198,18 @@ func ParseBase2(id string) (ID, error) {
// to base58, allowing it to create an even smaller result string.
// NOTE: There are many different base32 implementations so becareful when
// doing any interoperation.
func (f ID) Base32() string {
func (sid ID) Base32() string {

if f < 32 {
return string(encodeBase32Map[f])
if sid < 32 {
return string(encodeBase32Map[sid])
}

b := make([]byte, 0, 12)
for f >= 32 {
b = append(b, encodeBase32Map[f%32])
f /= 32
for sid >= 32 {
b = append(b, encodeBase32Map[sid%32])
sid /= 32
}
b = append(b, encodeBase32Map[f])
b = append(b, encodeBase32Map[sid])

for x, y := 0, len(b)-1; x < y; x, y = x+1, y-1 {
b[x], b[y] = b[y], b[x]
Expand All @@ -236,8 +236,8 @@ func ParseBase32(b []byte) (ID, error) {
}

// Base36 returns a base36 string of the snowflake ID
func (f ID) Base36() string {
return strconv.FormatInt(int64(f), 36)
func (sid ID) Base36() string {
return strconv.FormatInt(int64(sid), 36)
}

// ParseBase36 converts a Base36 string into a snowflake ID
Expand All @@ -247,18 +247,18 @@ func ParseBase36(id string) (ID, error) {
}

// Base58 returns a base58 string of the snowflake ID
func (f ID) Base58() string {
func (sid ID) Base58() string {

if f < 58 {
return string(encodeBase58Map[f])
if sid < 58 {
return string(encodeBase58Map[sid])
}

b := make([]byte, 0, 11)
for f >= 58 {
b = append(b, encodeBase58Map[f%58])
f /= 58
for sid >= 58 {
b = append(b, encodeBase58Map[sid%58])
sid /= 58
}
b = append(b, encodeBase58Map[f])
b = append(b, encodeBase58Map[sid])

for x, y := 0, len(b)-1; x < y; x, y = x+1, y-1 {
b[x], b[y] = b[y], b[x]
Expand All @@ -283,8 +283,8 @@ func ParseBase58(b []byte) (ID, error) {
}

// Base64 returns a base64 string of the snowflake ID
func (f ID) Base64() string {
return base64.StdEncoding.EncodeToString(f.Bytes())
func (sid ID) Base64() string {
return base64.StdEncoding.EncodeToString(sid.Bytes())
}

// ParseBase64 converts a base64 string into a snowflake ID
Expand All @@ -297,9 +297,9 @@ func ParseBase64(id string) (ID, error) {

}

// Bytes returns a byte slice of the snowflake ID
func (f ID) Bytes() []byte {
return []byte(f.String())
// Bytes return a byte slice of the snowflake ID
func (sid ID) Bytes() []byte {
return []byte(sid.String())
}

// ParseBytes converts a byte slice into a snowflake ID
Expand All @@ -310,9 +310,9 @@ func ParseBytes(id []byte) (ID, error) {

// IntBytes returns an array of bytes of the snowflake ID, encoded as a
// big endian integer.
func (f ID) IntBytes() [8]byte {
func (sid ID) IntBytes() [8]byte {
var b [8]byte
binary.BigEndian.PutUint64(b[:], uint64(f))
binary.BigEndian.PutUint64(b[:], uint64(sid))
return b
}

Expand All @@ -323,16 +323,16 @@ func ParseIntBytes(id [8]byte) ID {
}

// MarshalJSON returns a json byte array string of the snowflake ID.
func (f ID) MarshalJSON() ([]byte, error) {
func (sid ID) MarshalJSON() ([]byte, error) {
buff := make([]byte, 0, 22)
buff = append(buff, '"')
buff = strconv.AppendInt(buff, int64(f), 10)
buff = strconv.AppendInt(buff, int64(sid), 10)
buff = append(buff, '"')
return buff, nil
}

// UnmarshalJSON converts a json byte array of a snowflake ID into an ID type.
func (f *ID) UnmarshalJSON(b []byte) error {
func (sid *ID) UnmarshalJSON(b []byte) error {
if len(b) < 3 || b[0] != '"' || b[len(b)-1] != '"' {
return JSONSyntaxError{b}
}
Expand All @@ -342,13 +342,13 @@ func (f *ID) UnmarshalJSON(b []byte) error {
return err
}

*f = ID(i)
*sid = ID(i)
return nil
}

// Time returns an int64 unix timestamp in milliseconds of the snowflake ID time
func (f ID) Time() int64 {
return (int64(f) >> timestampShift) + epoch
func (sid ID) Time() int64 {
return (int64(sid) >> timestampShift) + epoch
}

// GetTimestampMax Timestamp maximum
Expand Down

0 comments on commit 0f5510f

Please sign in to comment.