...

Source file src/github.com/dgraph-io/ristretto/z/z.go

Documentation: github.com/dgraph-io/ristretto/z

     1  /*
     2   * Copyright 2019 Dgraph Labs, Inc. and Contributors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package z
    18  
    19  import (
    20  	"github.com/cespare/xxhash"
    21  )
    22  
    23  // TODO: Figure out a way to re-use memhash for the second uint64 hash, we
    24  //       already know that appending bytes isn't reliable for generating a
    25  //       second hash (see Ristretto PR #88).
    26  //
    27  //       We also know that while the Go runtime has a runtime memhash128
    28  //       function, it's not possible to use it to generate [2]uint64 or
    29  //       anything resembling a 128bit hash, even though that's exactly what
    30  //       we need in this situation.
    31  func KeyToHash(key interface{}) (uint64, uint64) {
    32  	if key == nil {
    33  		return 0, 0
    34  	}
    35  	switch k := key.(type) {
    36  	case uint64:
    37  		return k, 0
    38  	case string:
    39  		return MemHashString(k), xxhash.Sum64String(k)
    40  	case []byte:
    41  		return MemHash(k), xxhash.Sum64(k)
    42  	case byte:
    43  		return uint64(k), 0
    44  	case int:
    45  		return uint64(k), 0
    46  	case int32:
    47  		return uint64(k), 0
    48  	case uint32:
    49  		return uint64(k), 0
    50  	case int64:
    51  		return uint64(k), 0
    52  	default:
    53  		panic("Key type not supported")
    54  	}
    55  }
    56  

View as plain text