ints

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2024 License: MIT Imports: 7 Imported by: 4

README

ints

Ints for golang int128

Twitter snowflake ID

ints.NextID() Get next ID
ints.DefaultSnowflakeGenerator.ServerID = 5 Set Server ID (0-31 when uses DataCenterID or 0-1023 when not)
ints.DefaultSnowflakeGenerator.DataCenterID = 7 Set data center ID (0-31)

SnowflakeGenerator

Generator for Twitter snowflake ID.
Format: 1 - 0 | 41 - time in milliseconds | 5 - data center | 5 server | 12 sequence
You can configure the following settings:

  1. DataCenterID - data center id; acceptable values 0-31
  2. ServerID - server id; acceptable values 0-31
  3. TimeShift - time shift from UNIXTIME in milliseconds; use DefaultTimeShift for compatible with Twitter snowflake ID

GUID

ints.NextUUID() Gets next sequentional GUID
ints.RandUUID() Gets next realy random GUID

Bench test
BenchmarkUInt128Rand-16         20646118                54.48 ns/op           16 B/op          0 allocs/op
BenchmarkUInt128Next-16         13945237                77.94 ns/op            0 B/op          0 allocs/op

Documentation

Index

Constants

View Source
const (
	// DefaultTimeShift 2010-11-04, 01:42:54 UTC
	// ~69 years in 4 bit
	DefaultTimeShift = 1288834974657
)
View Source
const DefaultUuidRealRandomGeneratorBlocks = 4
View Source
const DefaultUuidRealRandomGeneratorSize = 1000
View Source
const Mval uint64 = ((1 << 2) << 4) << 8
View Source
const Nval uint64 = 1 << 63
View Source
const UuidSize = 16

Variables

View Source
var DefaultRandomUuidGenerator = &UuidRealRandomGenerator{}
View Source
var DefaultSnowflakeGenerator = &SnowflakeGenerator{
	TimeShift: DefaultTimeShift,
}
View Source
var DefaultUuidGenerator = &UuidSerialGenerator{}

Functions

func BytesAppendForward

func BytesAppendForward(buf []byte, l int) []byte

func GetTimeFormSerialUUID added in v0.0.3

func GetTimeFormSerialUUID(val Uuid) time.Time

func GetTimeFromID added in v0.0.4

func GetTimeFromID(id int64) time.Time

GetTimeFromID - get time from id

func LimitID added in v0.0.4

func LimitID(t time.Time) (id int64)

LimitID - generates min id for time

func NextID added in v0.0.4

func NextID() int64

NextID - gets next id: 1 - 0 | 41 - time in milliseconds | 5 - data center | 5 server | 12 sequence ~69 years in 4 bit

Types

type SnowflakeGenerator added in v0.0.4

type SnowflakeGenerator struct {
	Mx sync.Mutex
	// TimeShift - time shift / Use 1288834974657 for default generator
	TimeShift int64
	// DataCenterID - data center id 0-31 (valid values)
	DataCenterID int64
	// ServerID - server id 0-31 (valid values)
	ServerID int64
	// contains filtered or unexported fields
}

SnowflakeGenerator - generator IDs 1 - 0 | 41 - time in milliseconds | 5 - data center | 5 server | 12 sequence ~69 years in 4 bit

func (*SnowflakeGenerator) GetTimeFromID added in v0.0.4

func (fg *SnowflakeGenerator) GetTimeFromID(id int64) time.Time

GetTimeFromID - get time from id

func (*SnowflakeGenerator) LimitID added in v0.0.4

func (fg *SnowflakeGenerator) LimitID(t time.Time) (id int64)

LimitID - generates min id for time

func (*SnowflakeGenerator) Next added in v0.0.4

func (fg *SnowflakeGenerator) Next() int64

Next - gets next id: 1 - 0 | 41 - time in milliseconds | 5 - data center | 5 server | 12 sequence ~69 years in 4 bit

func (*SnowflakeGenerator) NextLock added in v0.0.4

func (fg *SnowflakeGenerator) NextLock() (id int64, owerload bool)

Next - gets next id: 1 - 0 | 41 - time in milliseconds | 5 - data center | 5 server | 12 sequence ~69 years in 4 bit

func (*SnowflakeGenerator) NextRaw added in v0.0.4

func (fg *SnowflakeGenerator) NextRaw() (id int64, owerload bool)

NextRaw - gets next id: 1 - 0 | 41 - time in milliseconds | 5 - data center | 5 server | 12 sequence ~69 years in 4 bit

type UInt128

type UInt128 [2]uint64

UInt128 128 bit unsigned integer

func UInt128FromBytes

func UInt128FromBytes(buf [16]byte) (i UInt128)

func UInt128FromInt added in v0.0.8

func UInt128FromInt(value int) (i UInt128)

func UInt128FromText added in v0.0.6

func UInt128FromText(value string, base int, ignoreFail bool) (i UInt128, err error)

func UInt128FromTextByte added in v0.0.6

func UInt128FromTextByte(value []byte, base int, ignoreFail bool) (i UInt128, err error)

func UInt128FromTextByteMust added in v0.0.7

func UInt128FromTextByteMust(value []byte, base int, ignoreFail bool) (i UInt128)

func UInt128FromTextMust added in v0.0.7

func UInt128FromTextMust(value string, base int, ignoreFail bool) (i UInt128)

func UInt128FromUint64 added in v0.0.8

func UInt128FromUint64(value uint64) (i UInt128)

func (UInt128) Add

func (i UInt128) Add(y UInt128) (result UInt128)

Add creates result to the sum i+y

func (UInt128) AddOverflow

func (i UInt128) AddOverflow(y UInt128, carry uint64) (result UInt128, carryOut uint64)

Add creates result to the sum i+y, carry is 0

func (UInt128) AddUInt64

func (i UInt128) AddUInt64(y uint64) (result UInt128)

AddUInt64 creates result to the sum i+y

func (UInt128) AsBytes

func (i UInt128) AsBytes() (result [16]byte)

func (UInt128) BytesLen

func (i UInt128) BytesLen() int

BytesLen returs count of requared bytes (16..0) if 0 then i is 0

func (UInt128) Copy

func (i UInt128) Copy() UInt128

func (UInt128) Div

func (i UInt128) Div(y UInt128) (quo UInt128, rem UInt128)

Div creates result to the quo = i / y; rem = i - div*y

func (UInt128) DivUint64

func (i UInt128) DivUint64(y uint64) (quo UInt128, rem UInt128)

DivUint64 creates result to the quo = i / y; rem = i - div*y

func (UInt128) Equal

func (i UInt128) Equal(val UInt128) bool

Equal returns true when i == val

func (*UInt128) FromBytes

func (i *UInt128) FromBytes(buf [16]byte) *UInt128

func (*UInt128) FromText

func (i *UInt128) FromText(value string, base int, ignoreFail bool) error

func (*UInt128) FromTextByte

func (i *UInt128) FromTextByte(value []byte, base int, ignoreFail bool) error

func (UInt128) GetBit

func (i UInt128) GetBit(pos int) bool

GetBit gets bit from pos (0..127)

func (UInt128) Int

func (i UInt128) Int() int

func (UInt128) IsEmpty

func (i UInt128) IsEmpty() bool

func (UInt128) IsUint64

func (i UInt128) IsUint64() bool

func (UInt128) Less

func (i UInt128) Less(val UInt128) bool

Less returns true when i < val

func (i UInt128) Link() *UInt128

func (UInt128) MarshalJSON

func (i UInt128) MarshalJSON() ([]byte, error)

MarshalJSON() implements json.Marshaler.

func (UInt128) MoveBitDown

func (i UInt128) MoveBitDown(move int) (result UInt128)

MoveBitDown creates result >> (move)

func (*UInt128) MoveBitDown1Internal

func (i *UInt128) MoveBitDown1Internal()

MoveBitDown creates result >> (1)

func (UInt128) MoveBitUp

func (i UInt128) MoveBitUp(move int) (result UInt128)

MoveBitUp creates result << (move)

func (UInt128) Mul

func (i UInt128) Mul(y UInt128) (result UInt128)

Mul creates result to the mult i * y

func (UInt128) MulOverflow

func (i UInt128) MulOverflow(y UInt128) (hi, lo UInt128)

Mul creates result to the mult i * y

func (UInt128) MulUInt64

func (i UInt128) MulUInt64(y uint64) (result UInt128)

MulUInt64 creates result to the mult i * y

func (UInt128) Power

func (i UInt128) Power(e UInt128) (p UInt128)

Power returns p = i^e

func (UInt128) Root

func (i UInt128) Root(base uint64) (r UInt128)

Root r => r^b=i when no resault then nearest r^b<=i

func (*UInt128) Set

func (i *UInt128) Set(val *UInt128) *UInt128

func (*UInt128) SetBit

func (i *UInt128) SetBit(pos int, val bool)

SetBit set bit as val in pos (0..127)

func (*UInt128) SetBit0

func (i *UInt128) SetBit0(pos int)

SetBit0 set bit as 0 in pos (0..127)

func (*UInt128) SetBit1

func (i *UInt128) SetBit1(pos int)

SetBit1 set bit as 1 in pos (0..127)

func (*UInt128) SetBytes

func (i *UInt128) SetBytes(buf []byte) *UInt128

func (*UInt128) SetUint64

func (i *UInt128) SetUint64(val *uint64) *UInt128

func (UInt128) Sqrt

func (i UInt128) Sqrt() (r UInt128)

Sqrt r => r^2=i when no resault then nearest r^2<=i

func (UInt128) String

func (i UInt128) String() string

func (UInt128) Sub

func (i UInt128) Sub(y UInt128) (result UInt128)

Sub creates result to the difference i-y

func (UInt128) SubOverflow

func (i UInt128) SubOverflow(y UInt128, borrow uint64) (result UInt128, borrowOut uint64)

Sub creates result to the difference i-y, borrow is 0

func (UInt128) Text

func (i UInt128) Text(base int) string

func (UInt128) UInt64

func (i UInt128) UInt64() uint64

func (*UInt128) UnmarshalJSON

func (i *UInt128) UnmarshalJSON(input []byte) error

UnmarshalJSON implements json.Unmarshaler.

type Uuid

type Uuid struct {
	UInt128
}

func LimitSerialUUID added in v0.0.3

func LimitSerialUUID(timeCalc time.Time) (res Uuid)

LimitSerialUUID - Limit (min) Serial id limit by date with format xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx M 0100 (version 4) N 10 (variant 1) tttttttt-tttt-Msss-Nrrr-rrrrrrrrrrrr M - 0100 (version 4) N - 10ss (variant 1) t - time (unix milli) s - step r - random value (Generated on Init)

func NextUUID

func NextUUID() (res Uuid)

NextUUID - Next Serial id from DefaultUuidGenerator xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx M 0100 (version 4) N 10 (variant 1) tttttttt-tttt-Msss-Nrrr-rrrrrrrrrrrr M - 0100 (version 4) N - 10ss (variant 1) t - time (unix milli) s - step r - random value (Generated on Init)

func RandUUID added in v0.0.2

func RandUUID() (res Uuid)

RandUUID - Next Random id from DefaultRandomUuidGenerator xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx M 0100 (version 4) N 10 (variant 1) rrrrrrrr-rrrr-Mrrr-Nrrr-rrrrrrrrrrrr M - 0100 (version 4) N - 10rr (variant 1) r - random value

func UuidFromText added in v0.0.6

func UuidFromText(value string, base int, ignoreFail bool) (i Uuid, err error)

func UuidFromTextByte added in v0.0.6

func UuidFromTextByte(value []byte, base int, ignoreFail bool) (i Uuid, err error)

func UuidFromTextByteMust added in v0.0.7

func UuidFromTextByteMust(value []byte, base int, ignoreFail bool) (i Uuid)

func UuidFromTextMust added in v0.0.7

func UuidFromTextMust(value string, base int, ignoreFail bool) (i Uuid)

func (Uuid) AsUUID

func (i Uuid) AsUUID() string

func (Uuid) Equal added in v0.0.1

func (i Uuid) Equal(val Uuid) bool

Equal returns true when i == val

func (Uuid) Less added in v0.0.1

func (i Uuid) Less(val Uuid) bool

Less returns true when i < val

func (i Uuid) Link() *Uuid

func (Uuid) MarshalJSON

func (i Uuid) MarshalJSON() ([]byte, error)

MarshalJSON() implements json.Marshaler.

func (Uuid) StepPart

func (i Uuid) StepPart() uint64

func (Uuid) String

func (i Uuid) String() string

func (Uuid) Text added in v0.0.6

func (i Uuid) Text(base int) string

func (Uuid) TimePart

func (i Uuid) TimePart() time.Time

func (Uuid) UniquePart

func (i Uuid) UniquePart() uint64

func (*Uuid) UnmarshalJSON

func (i *Uuid) UnmarshalJSON(input []byte) error

UnmarshalJSON implements json.Unmarshaler.

type UuidMap added in v0.0.5

type UuidMap[T any] map[Uuid]T

func (UuidMap[T]) MarshalJSON added in v0.0.5

func (m UuidMap[T]) MarshalJSON() ([]byte, error)

func (*UuidMap[T]) UnmarshalJSON added in v0.0.5

func (mT *UuidMap[T]) UnmarshalJSON(input []byte) error

type UuidRealRandomGenerator added in v0.0.2

type UuidRealRandomGenerator struct {
	Size int
	// contains filtered or unexported fields
}

UuidRealRandomGenerator - random uuid generator with random values

func (*UuidRealRandomGenerator) GetRandBytes added in v0.0.2

func (urg *UuidRealRandomGenerator) GetRandBytes() []byte

GetRandBytes returns 16 bytes

func (*UuidRealRandomGenerator) Init added in v0.0.2

func (urg *UuidRealRandomGenerator) Init() error

func (*UuidRealRandomGenerator) Next added in v0.0.2

func (urg *UuidRealRandomGenerator) Next() (res Uuid)

Next Random id xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx M 0100 (version 4) N 10 (variant 1) rrrrrrrr-rrrr-Mrrr-Nrrr-rrrrrrrrrrrr M - 0100 (version 4) N - 10rr (variant 1) r - random value

func (*UuidRealRandomGenerator) NextOrDefault added in v0.0.2

func (usg *UuidRealRandomGenerator) NextOrDefault() (res Uuid)

NextOrDefault gets Next from usg where usg != nil else uses DefaultRandomUuidGenerator.Next()

type UuidSerialGenerator

type UuidSerialGenerator struct {
	RandomTail UInt128
	Mx         sync.Mutex
	// contains filtered or unexported fields
}

func (*UuidSerialGenerator) Init

func (usg *UuidSerialGenerator) Init() error

func (*UuidSerialGenerator) Next

func (usg *UuidSerialGenerator) Next() (res Uuid)

Next Serial id xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx M 0100 (version 4) N 10 (variant 1) tttttttt-tttt-Msss-Nrrr-rrrrrrrrrrrr M - 0100 (version 4) N - 10ss (variant 1) t - time (unix milli) s - step r - random value (Generated on Init)

func (*UuidSerialGenerator) NextOrDefault added in v0.0.1

func (usg *UuidSerialGenerator) NextOrDefault() (res Uuid)

NextOrDefault gets Next from usg where usg != nil else uses DefaultUuidGenerator.Next()

func (*UuidSerialGenerator) NextRaw

func (usg *UuidSerialGenerator) NextRaw() (res Uuid, overflow bool)

NextRaw - Next Serial id (requare to lock befor call) xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx M 0100 (version 4) N 10 (variant 1) tttttttt-tttt-Msss-Nrrr-rrrrrrrrrrrr M - 0100 (version 4) N - 10ss (variant 1) t - time (unix milli) s - step r - random value (Generated on Init) overflow - may be not correct or unique result

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL