github.com/m3db/m3@v1.5.0/src/dbnode/storage/shard_new_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  import (
    28  	"github.com/m3db/m3/src/x/ident"
    29  	"github.com/m3db/m3/src/x/pool"
    30  
    31  	"github.com/cespare/xxhash/v2"
    32  )
    33  
    34  // Copyright (c) 2018 Uber Technologies, Inc.
    35  //
    36  // Permission is hereby granted, free of charge, to any person obtaining a copy
    37  // of this software and associated documentation files (the "Software"), to deal
    38  // in the Software without restriction, including without limitation the rights
    39  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    40  // copies of the Software, and to permit persons to whom the Software is
    41  // furnished to do so, subject to the following conditions:
    42  //
    43  // The above copyright notice and this permission notice shall be included in
    44  // all copies or substantial portions of the Software.
    45  //
    46  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    47  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    48  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    49  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    50  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    51  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    52  // THE SOFTWARE.
    53  
    54  // shardMapOptions provides options used when created the map.
    55  type shardMapOptions struct {
    56  	InitialSize int
    57  	KeyCopyPool pool.BytesPool
    58  }
    59  
    60  // newShardMap returns a new byte keyed map.
    61  func newShardMap(opts shardMapOptions) *shardMap {
    62  	var (
    63  		copyFn     shardMapCopyFn
    64  		finalizeFn shardMapFinalizeFn
    65  	)
    66  	if pool := opts.KeyCopyPool; pool == nil {
    67  		copyFn = func(k ident.ID) ident.ID {
    68  			return ident.BytesID(append([]byte(nil), k.Bytes()...))
    69  		}
    70  	} else {
    71  		copyFn = func(k ident.ID) ident.ID {
    72  			bytes := k.Bytes()
    73  			keyLen := len(bytes)
    74  			pooled := pool.Get(keyLen)[:keyLen]
    75  			copy(pooled, bytes)
    76  			return ident.BytesID(pooled)
    77  		}
    78  		finalizeFn = func(k ident.ID) {
    79  			if slice, ok := k.(ident.BytesID); ok {
    80  				pool.Put(slice)
    81  			}
    82  		}
    83  	}
    84  	return _shardMapAlloc(_shardMapOptions{
    85  		hash: func(id ident.ID) shardMapHash {
    86  			return shardMapHash(xxhash.Sum64(id.Bytes()))
    87  		},
    88  		equals: func(x, y ident.ID) bool {
    89  			return x.Equal(y)
    90  		},
    91  		copy:        copyFn,
    92  		finalize:    finalizeFn,
    93  		initialSize: opts.InitialSize,
    94  	})
    95  }