github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/types/uuid.go (about) 1 // Copyright 2019 Dolthub, Inc. 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 "bytes" 19 "context" 20 21 "github.com/google/uuid" 22 23 "github.com/dolthub/dolt/go/store/hash" 24 ) 25 26 const ( 27 uuidNumBytes = 16 28 ) 29 30 // UUIDHashedFromValues generates a UUID from the first 16 byes of the hash.Hash 31 // generated from serialized |vals|. 32 func UUIDHashedFromValues(nbf *NomsBinFormat, vals ...Value) (UUID, error) { 33 w := binaryNomsWriter{make([]byte, 4), 0} 34 for _, v := range vals { 35 if v == nil || v.Kind() == NullKind { 36 continue 37 } 38 if err := v.writeTo(&w, nbf); err != nil { 39 return [16]byte{}, err 40 } 41 } 42 43 h := hash.Of(w.data()) 44 id, err := uuid.FromBytes(h[:uuidNumBytes]) 45 return UUID(id), err 46 } 47 48 type UUID uuid.UUID 49 50 func (v UUID) Value(ctx context.Context) (Value, error) { 51 return v, nil 52 } 53 54 func (v UUID) Equals(other Value) bool { 55 return v == other 56 } 57 58 func (v UUID) Less(nbf *NomsBinFormat, other LesserValuable) (bool, error) { 59 if v2, ok := other.(UUID); ok { 60 return bytes.Compare(v[:], v2[:]) < 0, nil 61 } 62 return UUIDKind < other.Kind(), nil 63 } 64 65 func (v UUID) Hash(nbf *NomsBinFormat) (hash.Hash, error) { 66 return getHash(v, nbf) 67 } 68 69 func (v UUID) isPrimitive() bool { 70 return true 71 } 72 73 func (v UUID) WalkValues(ctx context.Context, cb ValueCallback) error { 74 return nil 75 } 76 77 func (v UUID) WalkRefs(nbf *NomsBinFormat, cb RefCallback) error { 78 return nil 79 } 80 81 func (v UUID) typeOf() (*Type, error) { 82 return PrimitiveTypeMap[UUIDKind], nil 83 } 84 85 func (v UUID) Kind() NomsKind { 86 return UUIDKind 87 } 88 89 func (v UUID) valueReadWriter() ValueReadWriter { 90 return nil 91 } 92 93 func (v UUID) writeTo(w nomsWriter, nbf *NomsBinFormat) error { 94 id := UUID(v) 95 byteSl := id[:] 96 err := UUIDKind.writeTo(w, nbf) 97 98 if err != nil { 99 return err 100 } 101 102 w.writeRaw(byteSl) 103 return nil 104 } 105 106 func (v UUID) readFrom(_ *NomsBinFormat, b *binaryNomsReader) (Value, error) { 107 id := b.ReadUUID() 108 return UUID(id), nil 109 } 110 111 func (v UUID) skip(nbf *NomsBinFormat, b *binaryNomsReader) { 112 b.skipBytes(uuidNumBytes) 113 } 114 115 func (v UUID) String() string { 116 return uuid.UUID(v).String() 117 } 118 119 func (v UUID) HumanReadableString() string { 120 return v.String() 121 }