mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-18 16:54:25 +08:00
add comments, code refactor
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/mayswind/lab/pkg/settings"
|
||||
)
|
||||
|
||||
// Length and mask of all information in uuid
|
||||
const (
|
||||
INTERNAL_UUID_UNIX_TIME_BITS = 32
|
||||
INTERNAL_UUID_UNIX_TIME_MASK = (1 << INTERNAL_UUID_UNIX_TIME_BITS) - 1
|
||||
@@ -24,6 +25,7 @@ const (
|
||||
SEQ_NUMBER_ID_MASK = (1 << SEQ_NUMBER_ID_BITS) - 1
|
||||
)
|
||||
|
||||
// InternalUuidInfo represents a struct which has all information in uuid
|
||||
type InternalUuidInfo struct {
|
||||
UnixTime uint32
|
||||
UuidType uint8
|
||||
@@ -31,11 +33,13 @@ type InternalUuidInfo struct {
|
||||
SequentialId uint32
|
||||
}
|
||||
|
||||
// InternalUuidGenerator represents internal bundled uuid generator
|
||||
type InternalUuidGenerator struct {
|
||||
uuidServerId uint8
|
||||
uuidSeqNumbers [1 << INTERNAL_UUID_TYPE_BITS]uint64
|
||||
}
|
||||
|
||||
// NewInternalUuidGenerator returns a new internal uuid generator
|
||||
func NewInternalUuidGenerator(config *settings.Config) (*InternalUuidGenerator, error) {
|
||||
generator := &InternalUuidGenerator{
|
||||
uuidServerId: config.UuidServerId,
|
||||
@@ -44,6 +48,7 @@ func NewInternalUuidGenerator(config *settings.Config) (*InternalUuidGenerator,
|
||||
return generator, nil
|
||||
}
|
||||
|
||||
// GenerateUuid returns a new uuid
|
||||
func (u *InternalUuidGenerator) GenerateUuid(idType UuidType) int64 {
|
||||
// 63bits = unixTime(32bits) + uuidType(4bits) + uuidServerId(8bits) + sequentialNumber(19bits)
|
||||
|
||||
@@ -79,6 +84,7 @@ func (u *InternalUuidGenerator) GenerateUuid(idType UuidType) int64 {
|
||||
return uuid
|
||||
}
|
||||
|
||||
// ParseUuidInfo returns a info struct which contains all information in uuid
|
||||
func (u *InternalUuidGenerator) ParseUuidInfo(uuid int64) *InternalUuidInfo {
|
||||
seqId := uint32(uuid & INTERNAL_UUID_SEQ_ID_MASK)
|
||||
uuid = uuid >> INTERNAL_UUID_SEQ_ID_BITS
|
||||
|
||||
@@ -5,14 +5,17 @@ import (
|
||||
"github.com/mayswind/lab/pkg/settings"
|
||||
)
|
||||
|
||||
// UuidContainer contains the current uuid generator
|
||||
type UuidContainer struct {
|
||||
Current UuidGenerator
|
||||
}
|
||||
|
||||
// Initialize a uuid container singleton instance
|
||||
var (
|
||||
Container = &UuidContainer{}
|
||||
)
|
||||
|
||||
// InitializeUuidGenerator initialized the current uuid generator according to the config
|
||||
func InitializeUuidGenerator(config *settings.Config) error {
|
||||
if config.UuidGeneratorType == settings.UUID_GENERATOR_TYPE_INTERNAL {
|
||||
generator, err := NewInternalUuidGenerator(config)
|
||||
@@ -24,6 +27,7 @@ func InitializeUuidGenerator(config *settings.Config) error {
|
||||
return errs.ErrInvalidUuidMode
|
||||
}
|
||||
|
||||
// GenerateUuid returns a new uuid by the current uuid generator
|
||||
func (u *UuidContainer) GenerateUuid(uuidType UuidType) int64 {
|
||||
return u.Current.GenerateUuid(uuidType)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package uuid
|
||||
|
||||
// UuidGenerator is common uuid generator interface
|
||||
type UuidGenerator interface {
|
||||
GenerateUuid(uuidType UuidType) int64
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package uuid
|
||||
|
||||
type UuidType uint8
|
||||
|
||||
// Types of uuid
|
||||
const (
|
||||
UUID_TYPE_DEFAULT UuidType = 0
|
||||
UUID_TYPE_USER UuidType = 1
|
||||
|
||||
Reference in New Issue
Block a user