github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/storage/dirty_series_map_gen.go (about) 1 // Copyright (c) 2021 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 // This file was automatically generated by genny. 22 // Any changes will be lost if this file is regenerated. 23 // see https://github.com/mauricelam/genny 24 25 package storage 26 27 // Copyright (c) 2018 Uber Technologies, Inc. 28 // 29 // Permission is hereby granted, free of charge, to any person obtaining a copy 30 // of this software and associated documentation files (the "Software"), to deal 31 // in the Software without restriction, including without limitation the rights 32 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 33 // copies of the Software, and to permit persons to whom the Software is 34 // furnished to do so, subject to the following conditions: 35 // 36 // The above copyright notice and this permission notice shall be included in 37 // all copies or substantial portions of the Software. 38 // 39 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 40 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 41 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 42 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 43 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 44 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 45 // THE SOFTWARE. 46 47 // dirtySeriesMapHash is the hash for a given map entry, this is public to support 48 // iterating over the map using a native Go for loop. 49 type dirtySeriesMapHash uint64 50 51 // dirtySeriesMapHashFn is the hash function to execute when hashing a key. 52 type dirtySeriesMapHashFn func(idAndBlockStart) dirtySeriesMapHash 53 54 // dirtySeriesMapEqualsFn is the equals key function to execute when detecting equality of a key. 55 type dirtySeriesMapEqualsFn func(idAndBlockStart, idAndBlockStart) bool 56 57 // dirtySeriesMapCopyFn is the copy key function to execute when copying the key. 58 type dirtySeriesMapCopyFn func(idAndBlockStart) idAndBlockStart 59 60 // dirtySeriesMapFinalizeFn is the finalize key function to execute when finished with a key. 61 type dirtySeriesMapFinalizeFn func(idAndBlockStart) 62 63 // dirtySeriesMap uses the genny package to provide a generic hash map that can be specialized 64 // by running the following command from this root of the repository: 65 // ``` 66 // make hashmap-gen pkg=outpkg key_type=Type value_type=Type out_dir=/tmp 67 // ``` 68 // Or if you would like to use bytes or ident.ID as keys you can use the 69 // partially specialized maps to generate your own maps as well: 70 // ``` 71 // make byteshashmap-gen pkg=outpkg value_type=Type out_dir=/tmp 72 // make idhashmap-gen pkg=outpkg value_type=Type out_dir=/tmp 73 // ``` 74 // This will output to stdout the generated source file to use for your map. 75 // It uses linear probing by incrementing the number of the hash created when 76 // hashing the identifier if there is a collision. 77 // dirtySeriesMap is a value type and not an interface to allow for less painful 78 // upgrades when adding/removing methods, it is not likely to need mocking so 79 // an interface would not be super useful either. 80 type dirtySeriesMap struct { 81 _dirtySeriesMapOptions 82 83 // lookup uses hash of the identifier for the key and the MapEntry value 84 // wraps the value type and the key (used to ensure lookup is correct 85 // when dealing with collisions), we use uint64 for the hash partially 86 // because lookups of maps with uint64 keys has a fast path for Go. 87 lookup map[dirtySeriesMapHash]dirtySeriesMapEntry 88 } 89 90 // _dirtySeriesMapOptions is a set of options used when creating an identifier map, it is kept 91 // private so that implementers of the generated map can specify their own options 92 // that partially fulfill these options. 93 type _dirtySeriesMapOptions struct { 94 // hash is the hash function to execute when hashing a key. 95 hash dirtySeriesMapHashFn 96 // equals is the equals key function to execute when detecting equality. 97 equals dirtySeriesMapEqualsFn 98 // copy is the copy key function to execute when copying the key. 99 copy dirtySeriesMapCopyFn 100 // finalize is the finalize key function to execute when finished with a 101 // key, this is optional to specify. 102 finalize dirtySeriesMapFinalizeFn 103 // initialSize is the initial size for the map, use zero to use Go's std map 104 // initial size and consequently is optional to specify. 105 initialSize int 106 } 107 108 // dirtySeriesMapEntry is an entry in the map, this is public to support iterating 109 // over the map using a native Go for loop. 110 type dirtySeriesMapEntry struct { 111 // key is used to check equality on lookups to resolve collisions 112 key _dirtySeriesMapKey 113 // value type stored 114 value *idElement 115 } 116 117 type _dirtySeriesMapKey struct { 118 key idAndBlockStart 119 finalize bool 120 } 121 122 // Key returns the map entry key. 123 func (e dirtySeriesMapEntry) Key() idAndBlockStart { 124 return e.key.key 125 } 126 127 // Value returns the map entry value. 128 func (e dirtySeriesMapEntry) Value() *idElement { 129 return e.value 130 } 131 132 // _dirtySeriesMapAlloc is a non-exported function so that when generating the source code 133 // for the map you can supply a public constructor that sets the correct 134 // hash, equals, copy, finalize options without users of the map needing to 135 // implement them themselves. 136 func _dirtySeriesMapAlloc(opts _dirtySeriesMapOptions) *dirtySeriesMap { 137 m := &dirtySeriesMap{_dirtySeriesMapOptions: opts} 138 m.Reallocate() 139 return m 140 } 141 142 func (m *dirtySeriesMap) newMapKey(k idAndBlockStart, opts _dirtySeriesMapKeyOptions) _dirtySeriesMapKey { 143 key := _dirtySeriesMapKey{key: k, finalize: opts.finalizeKey} 144 if !opts.copyKey { 145 return key 146 } 147 148 key.key = m.copy(k) 149 return key 150 } 151 152 func (m *dirtySeriesMap) removeMapKey(hash dirtySeriesMapHash, key _dirtySeriesMapKey) { 153 delete(m.lookup, hash) 154 if key.finalize { 155 m.finalize(key.key) 156 } 157 } 158 159 // Get returns a value in the map for an identifier if found. 160 func (m *dirtySeriesMap) Get(k idAndBlockStart) (*idElement, bool) { 161 hash := m.hash(k) 162 for entry, ok := m.lookup[hash]; ok; entry, ok = m.lookup[hash] { 163 if m.equals(entry.key.key, k) { 164 return entry.value, true 165 } 166 // Linear probe to "next" to this entry (really a rehash) 167 hash++ 168 } 169 var empty *idElement 170 return empty, false 171 } 172 173 // Set will set the value for an identifier. 174 func (m *dirtySeriesMap) Set(k idAndBlockStart, v *idElement) { 175 m.set(k, v, _dirtySeriesMapKeyOptions{ 176 copyKey: true, 177 finalizeKey: m.finalize != nil, 178 }) 179 } 180 181 // dirtySeriesMapSetUnsafeOptions is a set of options to use when setting a value with 182 // the SetUnsafe method. 183 type dirtySeriesMapSetUnsafeOptions struct { 184 NoCopyKey bool 185 NoFinalizeKey bool 186 } 187 188 // SetUnsafe will set the value for an identifier with unsafe options for how 189 // the map treats the key. 190 func (m *dirtySeriesMap) SetUnsafe(k idAndBlockStart, v *idElement, opts dirtySeriesMapSetUnsafeOptions) { 191 m.set(k, v, _dirtySeriesMapKeyOptions{ 192 copyKey: !opts.NoCopyKey, 193 finalizeKey: !opts.NoFinalizeKey, 194 }) 195 } 196 197 type _dirtySeriesMapKeyOptions struct { 198 copyKey bool 199 finalizeKey bool 200 } 201 202 func (m *dirtySeriesMap) set(k idAndBlockStart, v *idElement, opts _dirtySeriesMapKeyOptions) { 203 hash := m.hash(k) 204 for entry, ok := m.lookup[hash]; ok; entry, ok = m.lookup[hash] { 205 if m.equals(entry.key.key, k) { 206 m.lookup[hash] = dirtySeriesMapEntry{ 207 key: entry.key, 208 value: v, 209 } 210 return 211 } 212 // Linear probe to "next" to this entry (really a rehash) 213 hash++ 214 } 215 216 m.lookup[hash] = dirtySeriesMapEntry{ 217 key: m.newMapKey(k, opts), 218 value: v, 219 } 220 } 221 222 // Iter provides the underlying map to allow for using a native Go for loop 223 // to iterate the map, however callers should only ever read and not write 224 // the map. 225 func (m *dirtySeriesMap) Iter() map[dirtySeriesMapHash]dirtySeriesMapEntry { 226 return m.lookup 227 } 228 229 // Len returns the number of map entries in the map. 230 func (m *dirtySeriesMap) Len() int { 231 return len(m.lookup) 232 } 233 234 // Contains returns true if value exists for key, false otherwise, it is 235 // shorthand for a call to Get that doesn't return the value. 236 func (m *dirtySeriesMap) Contains(k idAndBlockStart) bool { 237 _, ok := m.Get(k) 238 return ok 239 } 240 241 // Delete will remove a value set in the map for the specified key. 242 func (m *dirtySeriesMap) Delete(k idAndBlockStart) { 243 hash := m.hash(k) 244 for entry, ok := m.lookup[hash]; ok; entry, ok = m.lookup[hash] { 245 if m.equals(entry.key.key, k) { 246 m.removeMapKey(hash, entry.key) 247 return 248 } 249 // Linear probe to "next" to this entry (really a rehash) 250 hash++ 251 } 252 } 253 254 // Reset will reset the map by simply deleting all keys to avoid 255 // allocating a new map. 256 func (m *dirtySeriesMap) Reset() { 257 for hash, entry := range m.lookup { 258 m.removeMapKey(hash, entry.key) 259 } 260 } 261 262 // Reallocate will avoid deleting all keys and reallocate a new 263 // map, this is useful if you believe you have a large map and 264 // will not need to grow back to a similar size. 265 func (m *dirtySeriesMap) Reallocate() { 266 if m.initialSize > 0 { 267 m.lookup = make(map[dirtySeriesMapHash]dirtySeriesMapEntry, m.initialSize) 268 } else { 269 m.lookup = make(map[dirtySeriesMapHash]dirtySeriesMapEntry) 270 } 271 }