github.com/muyo/sno@v1.2.1/global.go (about)

     1  package sno
     2  
     3  import (
     4  	"sort"
     5  	"time"
     6  	"unsafe"
     7  
     8  	"github.com/muyo/sno/internal"
     9  )
    10  
    11  var (
    12  	generator *Generator
    13  	zero      ID
    14  )
    15  
    16  func init() {
    17  	doInit()
    18  }
    19  
    20  func doInit() {
    21  	g, err := NewGenerator(nil, nil)
    22  	if err != nil {
    23  		panic(err)
    24  	}
    25  
    26  	generator = g
    27  }
    28  
    29  // New uses the package-level generator to generate a new ID using the current system
    30  // time for its timestamp.
    31  func New(meta byte) ID {
    32  	return generator.New(meta)
    33  }
    34  
    35  // NewWithTime uses the package-level generator to generate a new ID using the given time
    36  // for the timestamp.
    37  //
    38  // IDs generated using this method are subject to several caveats.
    39  // See generator.NewWithTime() for their documentation.
    40  func NewWithTime(meta byte, t time.Time) ID {
    41  	return generator.NewWithTime(meta, t)
    42  }
    43  
    44  // FromBinaryBytes takes a byte slice and copies its contents into an ID, returning the bytes as an ID.
    45  //
    46  // The slice must have a length of 10. Returns a InvalidDataSizeError if it does not.
    47  func FromBinaryBytes(src []byte) (id ID, err error) {
    48  	return id, id.UnmarshalBinary(src)
    49  }
    50  
    51  // FromEncodedBytes decodes a canonically base32-encoded byte slice representation of an ID
    52  // into its binary representation and returns it.
    53  //
    54  // The slice must have a length of 16. Returns a InvalidDataSizeError if it does not.
    55  func FromEncodedBytes(src []byte) (id ID, err error) {
    56  	return id, id.UnmarshalText(src)
    57  }
    58  
    59  // FromEncodedString decodes a canonically base32-encoded string representation of an ID
    60  // into its binary representation and returns it.
    61  //
    62  // The string must have a length of 16. Returns a InvalidDataSizeError if it does not.
    63  func FromEncodedString(src string) (id ID, err error) {
    64  	if len(src) != SizeEncoded {
    65  		return zero, &InvalidDataSizeError{Size: len(src)}
    66  	}
    67  
    68  	// We only read in the data pointer (and input is read-only), so this does the job.
    69  	return internal.Decode(*(*[]byte)(unsafe.Pointer(&src))), nil
    70  }
    71  
    72  type collection []ID
    73  
    74  func (ids collection) Len() int           { return len(ids) }
    75  func (ids collection) Less(i, j int) bool { return ids[i].Compare(ids[j]) < 0 }
    76  func (ids collection) Swap(i, j int)      { ids[i], ids[j] = ids[j], ids[i] }
    77  
    78  // Sort performs an in-place lexicographic sort of a slice of sno IDs.
    79  func Sort(s []ID) {
    80  	sort.Sort(collection(s))
    81  }
    82  
    83  // Zero returns the zero value of an ID, which is 10 zero bytes and equivalent to:
    84  //
    85  //	id := sno.ID{}
    86  // ... e.g. ...
    87  //	id := sno.ID{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
    88  func Zero() ID {
    89  	return zero
    90  }