github.com/m3db/m3@v1.5.0/src/x/ident/bytes_id.go (about) 1 // Copyright (c) 2018 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package ident 22 23 import ( 24 "bytes" 25 ) 26 27 // BytesID is a small utility type to avoid the heavy weight of a true ID 28 // implementation when using in high throughput places like keys in a map. 29 type BytesID []byte 30 31 // var declaration to ensure package type BytesID implements ID 32 var _ ID = BytesID(nil) 33 34 // Bytes returns the underlying byte slice of the bytes ID. 35 func (v BytesID) Bytes() []byte { 36 return v 37 } 38 39 // String returns the bytes ID as a string. 40 func (v BytesID) String() string { 41 return string(v) 42 } 43 44 // Equal returns whether the bytes ID is equal to a given ID. 45 func (v BytesID) Equal(value ID) bool { 46 return bytes.Equal(value.Bytes(), v) 47 } 48 49 // NoFinalize is a no-op for a bytes ID as Finalize is already a no-op. 50 func (v BytesID) NoFinalize() { 51 } 52 53 // IsNoFinalize is always true since BytesID is not pooled. 54 func (v BytesID) IsNoFinalize() bool { 55 return true 56 } 57 58 // Finalize is a no-op for a bytes ID as it has no associated pool. 59 func (v BytesID) Finalize() { 60 } 61 62 var _ ID = (*ReusableBytesID)(nil) 63 64 // ReusableBytesID is a reusable bytes ID, use with extreme care in 65 // places where the lifecycle is known (there is no checking with this 66 // ID). 67 type ReusableBytesID struct { 68 bytes []byte 69 } 70 71 // NewReusableBytesID returns a new reusable bytes ID, use with extreme 72 // care in places where the lifecycle is known (there is no checking with 73 // this ID). 74 func NewReusableBytesID() *ReusableBytesID { 75 return &ReusableBytesID{} 76 } 77 78 // Reset resets the bytes ID for reuse, make sure there are zero references 79 // to this ID from any other data structure at this point. 80 func (i *ReusableBytesID) Reset(bytes []byte) { 81 i.bytes = bytes 82 } 83 84 // Bytes implements ID. 85 func (i *ReusableBytesID) Bytes() []byte { 86 return i.bytes 87 } 88 89 // Equal implements ID. 90 func (i *ReusableBytesID) Equal(value ID) bool { 91 return bytes.Equal(i.bytes, value.Bytes()) 92 } 93 94 // NoFinalize implements ID. 95 func (i *ReusableBytesID) NoFinalize() { 96 } 97 98 // IsNoFinalize implements ID. 99 func (i *ReusableBytesID) IsNoFinalize() bool { 100 // Reusable bytes ID are always not able to not be finalized 101 // as this ID is reused with reset. 102 return false 103 } 104 105 // Finalize implements ID. 106 func (i *ReusableBytesID) Finalize() { 107 // Noop as it will be re-used. 108 } 109 110 // String returns the bytes ID as a string. 111 func (i *ReusableBytesID) String() string { 112 return string(i.bytes) 113 }