Skip to content

Commit

Permalink
Merge 7d48c03 into 99874ea
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafaturan committed Feb 9, 2020
2 parents 99874ea + 7d48c03 commit 2a9feeb
Show file tree
Hide file tree
Showing 17 changed files with 377 additions and 127 deletions.
81 changes: 78 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,20 @@ generator.
Via go packages:
```go get github.com/mustafaturan/monoton```

## API

The method names and arities/args are stable now. No change should be expected
on the package for the version `1.x.x` except any bug fixes.

## Usage

### Using with Singleton

Create a new package if you want a singleton access to monoton id generator,
you can create a simple package like below, and then call `Next()` method:

```go
package main
package uniqid

// Import packages
import (
Expand All @@ -25,8 +35,66 @@ import (
"github.com/mustafaturan/monoton/sequencer"
)

var m monoton.Monoton

// On init configure the monoton
func init() {
m = *(newIDGenerator())
}

func newIDGenerator() *monoton.Monoton {
// Fetch your node id from a config server or generate from MAC/IP address
node := uint64(1)

// A unix time value which will be subtracted from the time sequence value.
// The initialTime value type corresponds to the sequencer type's time
// representation. If you are using Millisecond sequencer then it must be
// considered as Millisecond
// If we want to init the time with 2020-01-01 00:00:00 PST
initialTime := uint64(1577865600000)

// Configure monoton with a sequencer and the node
m, err = monoton.New(sequencer.NewMillisecond(), node, initialTime)
if err != nil{
panic(err)
}

return m
}

func Generate() string {
m.Next()
}
```

In any other package generate the ids like below:

```go
import (
"fmt"
"uniqid" // your local uniqid package from your project
)

func main() {
for i := 0; i < 100; i++ {
fmt.Println(uniqid.Generate())
}
}
```

### Using with Dependency Injection

```go
package main

// Import packages
import (
"fmt"
"github.com/mustafaturan/monoton"
"github.com/mustafaturan/monoton/sequencer"
)

func NewIDGenerator() *monoton.Monoton {
// Fetch your node id from a config server or generate from MAC/IP address
node := uint64(1)

Expand All @@ -37,12 +105,19 @@ func init() {
initialTime := uint64(0)

// Configure monoton with a sequencer and the node
monoton.Configure(sequencer.NewMillisecond(), node, initialTime)
m, err = monoton.New(sequencer.NewMillisecond(), node, initialTime)
if err != nil{
panic(err)
}

return m
}

func main() {
g := NewIDGenerator()

for i := 0; i < 100; i++ {
fmt.Println(monoton.Next())
fmt.Println(g.Next())
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ func ToBase62(u uint64) string {
// padding zeros
func ToBase62WithPaddingZeros(u uint64, padding int64) string {
formatter := "%+0" + strconv.FormatInt(padding, 10) + "s"
return fmt.Sprintf(formatter, ToBase62(uint64(u)))
return fmt.Sprintf(formatter, ToBase62(u))
}
22 changes: 16 additions & 6 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright 2020 Mustafa Turan. All rights reserved.
// Use of this source code is governed by a Apache License 2.0 license that can
// be found in the LICENSE file.

package monoton_test

import (
Expand All @@ -8,24 +12,30 @@ import (
"github.com/mustafaturan/monoton/sequencer"
)

func ExampleConfigure() {
func ExampleNew() {
s := sequencer.NewMillisecond() // has 4 bytes free space for a node
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:])
m, err := monoton.New(s, n, t)
if err != nil {
panic(err)
}
fmt.Println(m.Next()[12:])
// Output:
// 000J
}

func ExampleNext() {
func ExampleMonoton_Next() {
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()))
m, err := monoton.New(s, n, t)
if err != nil {
panic(err)
}
fmt.Println(len(m.Next()))
// Output:
// 16
}

0 comments on commit 2a9feeb

Please sign in to comment.