github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/uuid/rfc-4122/uuid.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package uuid
     4  
     5  import (
     6  	"bytes"
     7  	"crypto/rand"
     8  	"encoding/hex"
     9  	"io"
    10  
    11  	"github.com/GeniusesGroup/libgo/binary"
    12  	"github.com/GeniusesGroup/libgo/convert"
    13  )
    14  
    15  // RFC4122 representation compliant with specification described in https://tools.ietf.org/html/rfc4122.
    16  // https://github.com/google/uuid/blob/master/uuid.go
    17  type UUID [16]byte
    18  
    19  // V1 generate version 1 RFC4122 include date-time and MAC address
    20  // Use V1 for massive data that don't need to read much specially very close to write time!
    21  // These type of records write sequently in cluster and don't need very much move in cluster expand process!
    22  func V1() (uuid UUID) {
    23  	return
    24  }
    25  
    26  // V4 generate version 4 RFC4122 include randomly numbers.
    27  func V4() (uuid UUID) {
    28  	var err error
    29  	_, err = io.ReadFull(rand.Reader, uuid[:])
    30  	if err != nil {
    31  		panic(err)
    32  	}
    33  
    34  	// Set version to 4
    35  	uuid[6] = (uuid[6] & 0x0f) | (0x04 << 4)
    36  	// Set variant to RFC4122
    37  	uuid[8] = (uuid[8]&(0xff>>2) | (0x02 << 6))
    38  	return
    39  }
    40  
    41  // V5 generate version 5 RFC4122 include hash namespace and value
    42  func V5(nameSpace [16]byte, value []byte) (uuid UUID) {
    43  	return
    44  }
    45  
    46  // Equal returns true if uuid1 and uuid2 equals
    47  func (uuid UUID) Equal(uuid2 UUID) bool {
    48  	return bytes.Equal(uuid[:], uuid2[:])
    49  }
    50  
    51  func (uuid UUID) ToString() string { return uuid.String() }
    52  
    53  // String returns canonical string representation of RFC4122:
    54  // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
    55  func (uuid UUID) String() string {
    56  	var buf [36]byte
    57  	encodeHex(buf[:], uuid)
    58  	return string(buf[:])
    59  }
    60  
    61  // FromString will parsing RFC4122 from string input
    62  func (uuid UUID) FromString(s string) {
    63  	var text = convert.UnsafeStringToByteSlice(s)
    64  	hex.Decode(uuid[0:4], text[:8])
    65  	hex.Decode(uuid[4:6], text[9:13])
    66  	hex.Decode(uuid[6:8], text[14:18])
    67  	hex.Decode(uuid[8:10], text[19:23])
    68  	hex.Decode(uuid[10:], text[24:])
    69  }
    70  
    71  // URI returns the RFC 2141 URN form of uuid,
    72  // urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx,  or "" if uuid is invalid.
    73  func (uuid UUID) URI() string {
    74  	var buf [36 + 9]byte
    75  	copy(buf[:], "urn:uuid:")
    76  	encodeHex(buf[9:], uuid)
    77  	return string(buf[:])
    78  }
    79  
    80  func encodeHex(dst []byte, uuid UUID) {
    81  	hex.Encode(dst, uuid[:4])
    82  	dst[8] = '-'
    83  	hex.Encode(dst[9:13], uuid[4:6])
    84  	dst[13] = '-'
    85  	hex.Encode(dst[14:18], uuid[6:8])
    86  	dst[18] = '-'
    87  	hex.Encode(dst[19:23], uuid[8:10])
    88  	dst[23] = '-'
    89  	hex.Encode(dst[24:], uuid[10:])
    90  }
    91  
    92  func (uuid UUID) FirstUint64() (id uint64) { return binary.LittleEndian.Uint64(uuid[0:]) }
    93  func (uuid UUID) LastUint64() (id uint64)  { return binary.LittleEndian.Uint64(uuid[8:]) }
    94  func (uuid UUID) FirstUint32() (id uint32) { return binary.LittleEndian.Uint32(uuid[0:]) }