github.com/matrixorigin/matrixone@v1.2.0/pkg/container/types/uuid.go (about) 1 // Copyright 2021 - 2022 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package types 16 17 import ( 18 "github.com/google/uuid" 19 ) 20 21 func ParseUuid(str string) (Uuid, error) { 22 gUuid, err := uuid.Parse(str) 23 if err != nil { 24 return Uuid{}, err 25 } 26 return Uuid(gUuid), nil 27 } 28 29 func BuildUuid() (Uuid, error) { 30 gUuid, err := uuid.NewV7() 31 if err != nil { 32 return Uuid{}, err 33 } 34 return Uuid(gUuid), nil 35 } 36 37 func UuidToString(muuid Uuid) (string, error) { 38 return uuid.UUID(muuid).String(), nil 39 } 40 41 func EqualUuid(src Uuid, dest Uuid) bool { 42 return src == dest 43 } 44 45 func CompareUuid(left Uuid, right Uuid) int { 46 for i := 0; i < 16; i++ { 47 if left[i] == right[i] { 48 continue 49 } else if left[i] > right[i] { 50 return +1 51 } else { 52 return -1 53 } 54 } 55 return 0 56 } 57 58 func (d Uuid) ToString() string { 59 return uuid.UUID(d).String() 60 } 61 62 func (d Uuid) ClockSequence() int { 63 return uuid.UUID(d).ClockSequence() 64 } 65 66 func (d Uuid) Compare(other Uuid) int { 67 return int(CompareUuid(d, other)) 68 } 69 func (d Uuid) Eq(other Uuid) bool { 70 return d.Compare(other) == 0 71 } 72 func (d Uuid) Le(other Uuid) bool { 73 return d.Compare(other) <= 0 74 } 75 func (d Uuid) Lt(other Uuid) bool { 76 return d.Compare(other) < 0 77 } 78 func (d Uuid) Ge(other Uuid) bool { 79 return d.Compare(other) >= 0 80 } 81 func (d Uuid) Gt(other Uuid) bool { 82 return d.Compare(other) > 0 83 } 84 func (d Uuid) Ne(other Uuid) bool { 85 return d.Compare(other) != 0 86 } 87 88 // ProtoSize is used by gogoproto. 89 func (d *Uuid) ProtoSize() int { 90 return 16 91 } 92 93 // MarshalToSizedBuffer is used by gogoproto. 94 func (d *Uuid) MarshalToSizedBuffer(data []byte) (int, error) { 95 if len(data) < d.ProtoSize() { 96 panic("invalid byte slice") 97 } 98 n := copy(data, d[:]) 99 return n, nil 100 } 101 102 // MarshalTo is used by gogoproto. 103 func (d *Uuid) MarshalTo(data []byte) (int, error) { 104 size := d.ProtoSize() 105 return d.MarshalToSizedBuffer(data[:size]) 106 } 107 108 // Marshal is used by gogoproto. 109 func (d *Uuid) Marshal() ([]byte, error) { 110 data := make([]byte, d.ProtoSize()) 111 n, err := d.MarshalToSizedBuffer(data) 112 if err != nil { 113 return nil, err 114 } 115 return data[:n], err 116 } 117 118 // Unmarshal is used by gogoproto. 119 func (d *Uuid) Unmarshal(data []byte) error { 120 if len(data) < d.ProtoSize() { 121 panic("invalid byte slice") 122 } 123 copy(d[:], data) 124 return nil 125 }