Path: blob/main/vendor/github.com/google/uuid/version6.go
2875 views
// Copyright 2023 Google Inc. All rights reserved.1// Use of this source code is governed by a BSD-style2// license that can be found in the LICENSE file.34package uuid56import "encoding/binary"78// UUID version 6 is a field-compatible version of UUIDv1, reordered for improved DB locality.9// It is expected that UUIDv6 will primarily be used in contexts where there are existing v1 UUIDs.10// Systems that do not involve legacy UUIDv1 SHOULD consider using UUIDv7 instead.11//12// see https://datatracker.ietf.org/doc/html/draft-peabody-dispatch-new-uuid-format-03#uuidv613//14// NewV6 returns a Version 6 UUID based on the current NodeID and clock15// sequence, and the current time. If the NodeID has not been set by SetNodeID16// or SetNodeInterface then it will be set automatically. If the NodeID cannot17// be set NewV6 set NodeID is random bits automatically . If clock sequence has not been set by18// SetClockSequence then it will be set automatically. If GetTime fails to19// return the current NewV6 returns Nil and an error.20func NewV6() (UUID, error) {21var uuid UUID22now, seq, err := GetTime()23if err != nil {24return uuid, err25}2627/*280 1 2 3290 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 130+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+31| time_high |32+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+33| time_mid | time_low_and_version |34+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+35|clk_seq_hi_res | clk_seq_low | node (0-1) |36+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+37| node (2-5) |38+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+39*/4041binary.BigEndian.PutUint64(uuid[0:], uint64(now))42binary.BigEndian.PutUint16(uuid[8:], seq)4344uuid[6] = 0x60 | (uuid[6] & 0x0F)45uuid[8] = 0x80 | (uuid[8] & 0x3F)4647nodeMu.Lock()48if nodeID == zeroID {49setNodeInterface("")50}51copy(uuid[10:], nodeID[:])52nodeMu.Unlock()5354return uuid, nil55}565758