git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/uuid/v6.go (about)

     1  // Copyright 2023 Google Inc.  All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package uuid
     6  
     7  import "encoding/binary"
     8  
     9  // UUID version 6 is a field-compatible version of UUIDv1, reordered for improved DB locality.
    10  // It is expected that UUIDv6 will primarily be used in contexts where there are existing v1 UUIDs.
    11  // Systems that do not involve legacy UUIDv1 SHOULD consider using UUIDv7 instead.
    12  //
    13  // see https://datatracker.ietf.org/doc/html/draft-peabody-dispatch-new-uuid-format-03#uuidv6
    14  //
    15  // NewV6 returns a Version 6 UUID based on the current NodeID and clock
    16  // sequence, and the current time. If the NodeID has not been set by SetNodeID
    17  // or SetNodeInterface then it will be set automatically. If the NodeID cannot
    18  // be set NewV6 set NodeID is random bits automatically . If clock sequence has not been set by
    19  // SetClockSequence then it will be set automatically. If GetTime fails to
    20  // return the current NewV6 returns Nil and an error.
    21  func NewV6() (UUID, error) {
    22  	var uuid UUID
    23  	now, seq, err := GetTime()
    24  	if err != nil {
    25  		return uuid, err
    26  	}
    27  
    28  	/*
    29  	    0                   1                   2                   3
    30  	    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
    31  	   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    32  	   |                           time_high                           |
    33  	   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    34  	   |           time_mid            |      time_low_and_version     |
    35  	   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    36  	   |clk_seq_hi_res |  clk_seq_low  |         node (0-1)            |
    37  	   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    38  	   |                         node (2-5)                            |
    39  	   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    40  	*/
    41  
    42  	binary.BigEndian.PutUint64(uuid[0:], uint64(now))
    43  	binary.BigEndian.PutUint16(uuid[8:], seq)
    44  
    45  	uuid[6] = 0x60 | (uuid[6] & 0x0F)
    46  	uuid[8] = 0x80 | (uuid[8] & 0x3F)
    47  
    48  	nodeMu.Lock()
    49  	if nodeID == zeroID {
    50  		setNodeInterface("")
    51  	}
    52  	copy(uuid[10:], nodeID[:])
    53  	nodeMu.Unlock()
    54  
    55  	return uuid, nil
    56  }