Skip to content

Commit

Permalink
Feat: Made tsid and tsidfactory struct public
Browse files Browse the repository at this point in the history
  • Loading branch information
vishal-bihani authored Oct 7, 2024
1 parent 64ebfa0 commit 2941419
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
34 changes: 17 additions & 17 deletions tsid.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,19 @@ func init() {
ALPHABET_VALUES['O'] = 0x00
}

type tsid struct {
type Tsid struct {
number int64
}

// NewTsid returns pointer to new tsid
func NewTsid(number int64) *tsid {
return &tsid{
func NewTsid(number int64) *Tsid {
return &Tsid{
number: number,
}
}

// Fast returns a pointer to new random tsid
func Fast() *tsid {
func Fast() *Tsid {
// Incrementing before using it
cnt := atomicCounter.Add(1)

Expand All @@ -132,13 +132,13 @@ func Fast() *tsid {
}

// FromNumber returns pointer to tsid using the given number
func FromNumber(number int64) *tsid {
func FromNumber(number int64) *Tsid {
return NewTsid(number)
}

// FromBytes returns pointer to tsid by converting the given bytes to
// number
func FromBytes(bytes []byte) *tsid {
func FromBytes(bytes []byte) *Tsid {

// TODO: Add validation

Expand All @@ -158,7 +158,7 @@ func FromBytes(bytes []byte) *tsid {

// FromString returns pointer to tsid by converting the given string to
// number. It validates the string before conversion.
func FromString(str string) *tsid {
func FromString(str string) *Tsid {
arr := ToRuneArray(str)

var number int64 = 0
Expand Down Expand Up @@ -211,12 +211,12 @@ func IsValidRuneArray(arr []rune) bool {
}

// ToNumber returns the numerical component of the tsid
func (t *tsid) ToNumber() int64 {
func (t *Tsid) ToNumber() int64 {
return t.number
}

// ToBytes converts the number to bytes and returns the byte array
func (t *tsid) ToBytes() []byte {
func (t *Tsid) ToBytes() []byte {
bytes := make([]byte, TSID_BYTES)

bytes[0] = byte(uint64(t.number) >> 56)
Expand All @@ -234,19 +234,19 @@ func (t *tsid) ToBytes() []byte {
// ToString converts the number to a canonical string.
// The output is 13 characters long and only contains characters from
// Crockford's base32 alphabets
func (t *tsid) ToString() string {
func (t *Tsid) ToString() string {
return t.ToStringWithAlphabets(ALPHABET_UPPERCASE)
}

// ToLowerCase converts the number to a canonical string in lower case.
// The output is 13 characters long and only contains characters from
// Crockford's base32 alphabets
func (t *tsid) ToLowerCase() string {
func (t *Tsid) ToLowerCase() string {
return t.ToStringWithAlphabets(ALPHABET_LOWERCASE)
}

// ToStringWithAlphabets converts the number to string using the given alphabets and returns it
func (t *tsid) ToStringWithAlphabets(alphabets []rune) string {
func (t *Tsid) ToStringWithAlphabets(alphabets []rune) string {
chars := make([]rune, TSID_CHARS)

chars[0] = alphabets[((uint64(t.number) >> 60) & 0b11111)]
Expand All @@ -267,26 +267,26 @@ func (t *tsid) ToStringWithAlphabets(alphabets []rune) string {
}

// IsValid checks if the given tsid string is valid or not
func (t *tsid) IsValid(str string) bool {
func (t *Tsid) IsValid(str string) bool {
return len(str) != 0 && IsValidRuneArray([]rune(str))
}

// GetRandom returns random component (node + counter) of the tsid
func (t *tsid) GetRandom() int64 {
func (t *Tsid) GetRandom() int64 {
return t.number & int64(RANDOM_MASK)
}

// GetUnixMillis returns time of creation in millis since 1970-01-01
func (t *tsid) GetUnixMillis() int64 {
func (t *Tsid) GetUnixMillis() int64 {
return t.getTime() + TSID_EPOCH
}

// GetUnixMillis returns time of creation in millis since 1970-01-01
func (t *tsid) GetUnixMillisWithCustomEpoch(epoch int64) int64 {
func (t *Tsid) GetUnixMillisWithCustomEpoch(epoch int64) int64 {
return t.getTime() + epoch
}

// getTime returns the time component
func (t *tsid) getTime() int64 {
func (t *Tsid) getTime() int64 {
return int64(uint64(t.number) >> int64(RANDOM_BITS))
}
20 changes: 10 additions & 10 deletions tsid_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ var lock = &sync.Mutex{}
var rLock = &sync.Mutex{}

// Only a single instance of tsidFactory will be used per node
var tsidFactoryInstance *tsidFactory
var tsidFactoryInstance *TsidFactory

// tsidFactory is a singleton which
// should be used to generate random tsid
type tsidFactory struct {
type TsidFactory struct {
node int32
nodeBits int32
nodeMask int32
Expand All @@ -49,10 +49,10 @@ type tsidFactory struct {
randomBytes int32
}

func newTsidFactory(builder *tsidFactoryBuilder) (*tsidFactory, error) {
func newTsidFactory(builder *tsidFactoryBuilder) (*TsidFactory, error) {

// properties from builder
tsidFactory := &tsidFactory{
tsidFactory := &TsidFactory{
customEpoch: builder.GetCustomEpoch(),
clock: builder.GetClock(),
random: builder.GetRandom(),
Expand Down Expand Up @@ -92,7 +92,7 @@ func newTsidFactory(builder *tsidFactoryBuilder) (*tsidFactory, error) {
}

// Generate will return a tsid with random number
func (factory *tsidFactory) Generate() (*tsid, error) {
func (factory *TsidFactory) Generate() (*Tsid, error) {
time, err := factory.getTime()
if err != nil {
return nil, err
Expand All @@ -106,7 +106,7 @@ func (factory *tsidFactory) Generate() (*tsid, error) {
return NewTsid(tsidNumber), nil
}

func (factory *tsidFactory) getTime() (int64, error) {
func (factory *TsidFactory) getTime() (int64, error) {
time := factory.clock.UnixMilli()
if time <= factory.lastTime {
lock.Lock()
Expand All @@ -129,11 +129,11 @@ func (factory *tsidFactory) getTime() (int64, error) {
return (time - factory.customEpoch), nil
}

func (factory *tsidFactory) getRandomValue() (int32, error) {
func (factory *TsidFactory) getRandomValue() (int32, error) {
return factory.getRandomCounter()
}

func (factory *tsidFactory) getRandomCounter() (int32, error) {
func (factory *TsidFactory) getRandomCounter() (int32, error) {
switch factory.random.(type) {
case *byteRandom:
{
Expand Down Expand Up @@ -258,7 +258,7 @@ func (builder *tsidFactoryBuilder) GetCustomEpoch() int64 {
return builder.customEpoch
}

func (builder *tsidFactoryBuilder) Build() (*tsidFactory, error) {
func (builder *tsidFactoryBuilder) Build() (*TsidFactory, error) {
if tsidFactoryInstance != nil {
return tsidFactoryInstance, nil
}
Expand All @@ -276,6 +276,6 @@ func (builder *tsidFactoryBuilder) Build() (*tsidFactory, error) {
return tsidFactoryInstance, nil
}

func (builder *tsidFactoryBuilder) NewInstance() (*tsidFactory, error) {
func (builder *tsidFactoryBuilder) NewInstance() (*TsidFactory, error) {
return newTsidFactory(builder)
}

0 comments on commit 2941419

Please sign in to comment.