| 1 |
package uuid |
| 2 |
|
| 3 |
import "fmt" |
| 4 |
|
| 5 |
import () |
| 6 |
|
| 7 |
// an iterated value to help ensure unique UUID generations values |
| 8 |
// across the same domain, server restarts and clock issues |
| 9 |
type Sequence uint16 |
| 10 |
|
| 11 |
// the last node id setup used by the generator |
| 12 |
type Node []byte |
| 13 |
|
| 14 |
// Store is used for storage of UUID generation history to ensure continuous |
| 15 |
// running of the UUID generator between restarts and to monitor synchronicity |
| 16 |
// while generating new V1 or V2 UUIDs |
| 17 |
type Store struct { |
| 18 |
Timestamp |
| 19 |
Sequence |
| 20 |
Node |
| 21 |
} |
| 22 |
|
| 23 |
// String returns a string representation of the Store |
| 24 |
func (o Store) String() string { |
| 25 |
return fmt.Sprintf("Timestamp[%s]-Sequence[%d]-Node[%x]", o.Timestamp, o.Sequence, o.Node) |
| 26 |
} |
| 27 |
|
| 28 |
// Saver is an interface to setup a non volatile store within your system |
| 29 |
// if you wish to use V1 and V2 UUIDs based on your node id and a constant time |
| 30 |
// it is highly recommended to implement this. |
| 31 |
// A default implementation has been provided. FileSystemStorage, the default |
| 32 |
// behaviour of the package is to generate random sequences where a Saver is not |
| 33 |
// specified. |
| 34 |
type Saver interface { |
| 35 |
// Read is run once, use this to setup your UUID state machine |
| 36 |
// Read should also return the UUID state from the non volatile store |
| 37 |
Read() (error, Store) |
| 38 |
|
| 39 |
// Save saves the state to the non volatile store and is called only if |
| 40 |
Save(Store) |
| 41 |
} |
| 42 |
|
| 43 |
// RegisterSaver register's a uuid.Saver implementation to the default package |
| 44 |
// uuid.Generator. If you wish to save the generator state, this function must |
| 45 |
// be run before any calls to V1 or V2 UUIDs. uuid.RegisterSaver cannot be run |
| 46 |
// in conjunction with uuid.Init. You may implement the uuid.Saver interface |
| 47 |
// or use the provided uuid.Saver's from the uuid/savers package. |
| 48 |
func RegisterSaver(pSaver Saver) { |
| 49 |
generator.Do(func() { |
| 50 |
defer generator.init() |
| 51 |
generator.Lock() |
| 52 |
defer generator.Unlock() |
| 53 |
generator.Saver = pSaver |
| 54 |
}) |
| 55 |
} |