Skip to content

Commit

Permalink
Update uint to uint64 to support 32bit systems
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafaturan committed Nov 22, 2019
1 parent a59c469 commit adc266a
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 70 deletions.
2 changes: 1 addition & 1 deletion encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func ToBase62(u uint64) string {

// ToBase62WithPaddingZeros converts int types to Base62 encoded string with
// padding zeros
func ToBase62WithPaddingZeros(u uint, padding int64) string {
func ToBase62WithPaddingZeros(u uint64, padding int64) string {
formatter := "%+0" + strconv.FormatInt(padding, 10) + "s"
return fmt.Sprintf(formatter, ToBase62(uint64(u)))
}
2 changes: 1 addition & 1 deletion encoder/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestToBase62(t *testing.T) {

func TestToBase62WithPaddingZeros(t *testing.T) {
tests := []struct {
val uint
val uint64
padding int64
want string
}{
Expand Down
10 changes: 5 additions & 5 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

func ExampleConfigure() {
s := sequencer.NewMillisecond() // has 4 bytes free space for a node
n := uint(19) // Base62 => J
t := uint(0) // initial time (start from unix time in ms)
n := uint64(19) // Base62 => J
t := uint64(0) // initial time (start from unix time in ms)

monoton.Configure(s, n, t)
fmt.Println(monoton.Next()[12:])
Expand All @@ -20,9 +20,9 @@ func ExampleConfigure() {
}

func ExampleNext() {
s := sequencer.NewSecond() // sequencer.Second
n := uint(19) // Base62 => J
t := uint(time.Now().Unix()) // initial time (start from unix time in s)
s := sequencer.NewSecond() // sequencer.Second
n := uint64(19) // Base62 => J
t := uint64(time.Now().Unix()) // initial time (start from unix time in s)

monoton.Configure(s, n, t)
fmt.Println(len(monoton.Next()))
Expand Down
10 changes: 5 additions & 5 deletions monoton.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const (

type config struct {
sequencer *sequencer.Sequencer
initialTime uint
initialTime uint64
node string
timeSeqByteSize int64
seqByteSize int64
Expand All @@ -81,7 +81,7 @@ var c config
// Configure configures the monoton with the given generator and node. If you
// need to reset the node, then you have to reconfigure. If you do not configure
// the node then the node will be set to zero value.
func Configure(s sequencer.Sequencer, node, initialTime uint) error {
func Configure(s sequencer.Sequencer, node, initialTime uint64) error {
c = config{sequencer: &s, initialTime: initialTime}

if err := configureByteSizes(); err != nil {
Expand Down Expand Up @@ -125,7 +125,7 @@ func configureByteSizes() error {
return nil
}

func configureNode(node uint) error {
func configureNode(node uint64) error {
nodeByteSize := totalByteSize - (c.timeSeqByteSize + c.seqByteSize)

if err := validateNode(node, nodeByteSize); err != nil {
Expand All @@ -136,8 +136,8 @@ func configureNode(node uint) error {
return nil
}

func validateNode(node uint, nodeByteSize int64) error {
maxNode := uint(math.Pow(62, float64(nodeByteSize))) - 1
func validateNode(node uint64, nodeByteSize int64) error {
maxNode := uint64(math.Pow(62, float64(nodeByteSize))) - 1

if node > maxNode {
return fmt.Errorf(maxNodeErrorMsg, maxNode, node)
Expand Down
42 changes: 21 additions & 21 deletions monoton_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,35 @@ import (
func TestConfigure(t *testing.T) {
tests := []struct {
s sequencer.Sequencer
node uint
initialTime uint
node uint64
initialTime uint64
wantErr error
wantNode string
wantTime uint
wantTime uint64
}{
{
&validSequencer{},
3843,
uint(1),
uint64(1),
nil,
"zz",
uint(1),
uint64(1),
},
{
&validSequencer{},
3844,
uint(2),
uint64(2),
errors.New("node can't be greater than 3843 (given 3844)"),
"",
uint(2),
uint64(2),
},
{
&invalidSequencer{},
1,
uint(0),
uint64(0),
errors.New("sum of s:8, t:8 bytes can't be >= total byte size"),
"",
uint(0),
uint64(0),
},
}

Expand Down Expand Up @@ -93,34 +93,34 @@ func TestNext(t *testing.T) {
}

type validSequencer struct {
counter uint
counter uint64
}

type invalidSequencer struct {
counter uint
counter uint64
}

func (v *validSequencer) MaxTime() uint {
return uint(math.Pow(62, 8)) - 1
func (v *validSequencer) MaxTime() uint64 {
return uint64(math.Pow(62, 8)) - 1
}

func (v *validSequencer) Max() uint {
return uint(math.Pow(62, 6)) - 1
func (v *validSequencer) Max() uint64 {
return uint64(math.Pow(62, 6)) - 1
}

func (v *validSequencer) Next() (uint, uint) {
func (v *validSequencer) Next() (uint64, uint64) {
v.counter++
return 1, v.counter
}

func (i *invalidSequencer) MaxTime() uint {
return uint(math.Pow(62, 8)) - 1
func (i *invalidSequencer) MaxTime() uint64 {
return uint64(math.Pow(62, 8)) - 1
}

func (i *invalidSequencer) Max() uint {
return uint(math.Pow(62, 8)) - 1
func (i *invalidSequencer) Max() uint64 {
return uint64(math.Pow(62, 8)) - 1
}

func (i *invalidSequencer) Next() (uint, uint) {
func (i *invalidSequencer) Next() (uint64, uint64) {
return 1, i.counter
}
8 changes: 4 additions & 4 deletions mtimer/mtimer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
)

var initialTime time.Time
var monotonicTime uint
var monotonicTime uint64

func init() {
initialTime = time.Now()
monotonicTime = uint(initialTime.UnixNano())
monotonicTime = uint64(initialTime.UnixNano())
}

// Now returns the current monotonic time in nanoseconds
func Now() uint {
return monotonicTime + uint(time.Since(initialTime).Nanoseconds())
func Now() uint64 {
return monotonicTime + uint64(time.Since(initialTime).Nanoseconds())
}
4 changes: 2 additions & 2 deletions sequencer/millisecond.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (

// NewMillisecond returns the preconfigured millisecond sequencer
func NewMillisecond() *Sequence {
millisecond := uint(time.Millisecond)
millisecond := uint64(time.Millisecond)
return &Sequence{
now: func() uint { return mtimer.Now() / millisecond },
now: func() uint64 { return mtimer.Now() / millisecond },
max: 62*62*62*62 - 1,
maxTime: 62*62*62*62*62*62*62*62 - 1,
}
Expand Down
2 changes: 1 addition & 1 deletion sequencer/nanosecond.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ func NewNanosecond() *Sequence {
return &Sequence{
now: mtimer.Now,
max: 62*62 - 1,
maxTime: uint(1<<64 - 1),
maxTime: uint64(1<<64 - 1),
}
}
4 changes: 2 additions & 2 deletions sequencer/second.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (

// NewSecond returns the preconfigured second sequencer
func NewSecond() *Sequence {
second := uint(time.Second)
second := uint64(time.Second)
return &Sequence{
now: func() uint { return mtimer.Now() / second },
now: func() uint64 { return mtimer.Now() / second },
max: 62*62*62*62*62*62 - 1,
maxTime: 62*62*62*62*62*62 - 1,
}
Expand Down
16 changes: 8 additions & 8 deletions sequencer/sequence.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ import "sync"
type Sequence struct {
sync.Mutex

current uint
time uint
max uint
maxTime uint
now func() uint
current uint64
time uint64
max uint64
maxTime uint64
now func() uint64
}

// Max returns the maximum possible sequence value
func (s *Sequence) Max() uint {
func (s *Sequence) Max() uint64 {
return s.max
}

// MaxTime returns the maximum possible time sequence value
func (s *Sequence) MaxTime() uint {
func (s *Sequence) MaxTime() uint64 {
return s.maxTime
}

// Next returns the next sequence
func (s *Sequence) Next() (uint, uint) {
func (s *Sequence) Next() (uint64, uint64) {
s.Lock()
defer s.Unlock()

Expand Down
12 changes: 6 additions & 6 deletions sequencer/sequence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestMax_Sequence(t *testing.T) {
want := uint(12345)
want := uint64(12345)
s := &Sequence{max: want}

if got := s.Max(); got != want {
Expand All @@ -17,7 +17,7 @@ func TestMax_Sequence(t *testing.T) {
}

func TestMaxTime_Sequence(t *testing.T) {
want := uint(1<<64 - 1)
want := uint64(1<<64 - 1)
s := &Sequence{maxTime: want}

if got := s.MaxTime(); got != want {
Expand All @@ -27,11 +27,11 @@ func TestMaxTime_Sequence(t *testing.T) {

func TestNext_Sequence(t *testing.T) {
tests := []struct {
want uint
now func() uint
want uint64
now func() uint64
}{
{want: uint(2), now: func() uint { return 0 }},
{want: uint(0), now: func() uint { time.Sleep(time.Nanosecond); return mtimer.Now() }},
{want: uint64(2), now: func() uint64 { return 0 }},
{want: uint64(0), now: func() uint64 { time.Sleep(time.Nanosecond); return mtimer.Now() }},
}

for _, test := range tests {
Expand Down
6 changes: 3 additions & 3 deletions sequencer/sequencer.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ package sequencer
// Sequencer is a generic behavior for the sequence generators
type Sequencer interface {
// Max returns the maximum possible sequence value for a given time
Max() uint
Max() uint64
// MaxTime returns the maximum possible time sequence value
MaxTime() uint
MaxTime() uint64
// Now returns the current monotonic time
Next() (uint, uint)
Next() (uint64, uint64)
}
22 changes: 11 additions & 11 deletions sequencer/sequencer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (

func TestMaxTime(t *testing.T) {
tests := []struct {
wantMaxTime uint
wantMaxTime uint64
seq Sequencer
}{
{uint(math.Pow(62, 6) - 1), NewSecond()},
{uint(math.Pow(62, 8) - 1), NewMillisecond()},
{uint(1<<64 - 1), NewNanosecond()},
{uint64(math.Pow(62, 6) - 1), NewSecond()},
{uint64(math.Pow(62, 8) - 1), NewMillisecond()},
{uint64(1<<64 - 1), NewNanosecond()},
}

for _, test := range tests {
Expand All @@ -33,12 +33,12 @@ func TestMaxTime(t *testing.T) {

func TestMax(t *testing.T) {
tests := []struct {
wantMax uint
wantMax uint64
seq Sequencer
}{
{uint(math.Pow(62, 6) - 1), NewSecond()},
{uint(math.Pow(62, 4) - 1), NewMillisecond()},
{uint(math.Pow(62, 2) - 1), NewNanosecond()},
{uint64(math.Pow(62, 6) - 1), NewSecond()},
{uint64(math.Pow(62, 4) - 1), NewMillisecond()},
{uint64(math.Pow(62, 2) - 1), NewNanosecond()},
}

for _, test := range tests {
Expand Down Expand Up @@ -69,8 +69,8 @@ func TestNext(t *testing.T) {
sequencer := test.sequencer

t.Run("at the same time", func(t *testing.T) {
sequenceTimeVals := [2]uint{1, 2}
sequenceVals := [2]uint{0, 0}
sequenceTimeVals := [2]uint64{1, 2}
sequenceVals := [2]uint64{0, 0}

// Loops until we have the same time for two calls
for sequenceTimeVals[0] != sequenceTimeVals[1] {
Expand Down Expand Up @@ -103,7 +103,7 @@ func TestNext(t *testing.T) {
for _, test := range futureTimeTests {
sequencer := test.sequencer
t.Run("in a future time", func(t *testing.T) {
sequenceTimeVals := [2]uint{0, 0}
sequenceTimeVals := [2]uint64{0, 0}

sequenceTimeVals[0], _ = sequencer.Next()
time.Sleep(test.sleepDuration)
Expand Down

0 comments on commit adc266a

Please sign in to comment.