github.com/matrixorigin/matrixone@v0.7.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.NewUUID() 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) int64 { 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 }