YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/rocksdb/util/hash.cc
Line
Count
Source (jump to first uncovered line)
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2
//  This source code is licensed under the BSD-style license found in the
3
//  LICENSE file in the root directory of this source tree. An additional grant
4
//  of patent rights can be found in the PATENTS file in the same directory.
5
//
6
// The following only applies to changes made to this file as part of YugaByte development.
7
//
8
// Portions Copyright (c) YugaByte, Inc.
9
//
10
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
11
// in compliance with the License.  You may obtain a copy of the License at
12
//
13
// http://www.apache.org/licenses/LICENSE-2.0
14
//
15
// Unless required by applicable law or agreed to in writing, software distributed under the License
16
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
17
// or implied.  See the License for the specific language governing permissions and limitations
18
// under the License.
19
//
20
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
21
// Use of this source code is governed by a BSD-style license that can be
22
// found in the LICENSE file. See the AUTHORS file for names of contributors.
23
24
#include <string.h>
25
#include "yb/rocksdb/util/coding.h"
26
#include "yb/rocksdb/util/hash.h"
27
28
#include "yb/gutil/macros.h"
29
30
namespace rocksdb {
31
32
117M
uint32_t Hash(const char* data, size_t n, uint32_t seed) {
33
  // Similar to murmur hash
34
117M
  const uint32_t m = 0xc6a4a793;
35
117M
  const uint32_t r = 24;
36
117M
  const char* limit = data + n;
37
117M
  uint32_t h = static_cast<uint32_t>(seed ^ (n * m));
38
39
  // Pick up four bytes at a time
40
424M
  while (data + 4 <= limit) {
41
306M
    uint32_t w = DecodeFixed32(data);
42
306M
    data += 4;
43
306M
    h += w;
44
306M
    h *= m;
45
306M
    h ^= (h >> 16);
46
306M
  }
47
48
  // Pick up remaining bytes
49
117M
  switch (limit - data) {
50
    // Note: It would be better if this was cast to unsigned char, but that
51
    // would be a disk format change since we previously didn't have any cast
52
    // at all (so gcc used signed char).
53
    // To understand the difference between shifting unsigned and signed chars,
54
    // let's use 250 as an example. unsigned char will be 250, while signed char
55
    // will be -6. Bit-wise, they are equivalent: 11111010. However, when
56
    // converting negative number (signed char) to int, it will be converted
57
    // into negative int (of equivalent value, which is -6), while converting
58
    // positive number (unsigned char) will be converted to 250. Bitwise,
59
    // this looks like this:
60
    // signed char 11111010 -> int 11111111111111111111111111111010
61
    // unsigned char 11111010 -> int 00000000000000000000000011111010
62
8.10M
    case 3:
63
8.10M
      h += static_cast<uint32_t>(static_cast<signed char>(data[2]) << 16);
64
8.10M
      FALLTHROUGH_INTENDED;
65
16.9M
    case 2:
66
16.9M
      h += static_cast<uint32_t>(static_cast<signed char>(data[1]) << 8);
67
16.9M
      FALLTHROUGH_INTENDED;
68
32.1M
    case 1:
69
32.1M
      h += static_cast<uint32_t>(static_cast<signed char>(data[0]));
70
32.1M
      h *= m;
71
32.1M
      h ^= (h >> r);
72
32.1M
      break;
73
121M
  }
74
121M
  return h;
75
121M
}
76
77
}  // namespace rocksdb