YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/rocksdb/table/block_hash_index.h
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
#ifndef ROCKSDB_TABLE_BLOCK_HASH_INDEX_H
21
#define ROCKSDB_TABLE_BLOCK_HASH_INDEX_H
22
23
#pragma once
24
25
#include <string>
26
#include <unordered_map>
27
28
#include "yb/util/slice.h"
29
#include "yb/rocksdb/status.h"
30
31
#include "yb/rocksdb/util/arena.h"
32
#include "yb/rocksdb/util/murmurhash.h"
33
34
namespace rocksdb {
35
36
class Comparator;
37
class InternalIterator;
38
class SliceTransform;
39
40
// Build a hash-based index to speed up the lookup for "index block".
41
// BlockHashIndex accepts a key and, if found, returns its restart index within
42
// that index block.
43
class BlockHashIndex {
44
 public:
45
  // Represents a restart index in the index block's restart array.
46
  struct RestartIndex {
47
    explicit RestartIndex(uint32_t _first_index, uint32_t _num_blocks = 1)
48
200k
        : first_index(_first_index), num_blocks(_num_blocks) {}
49
50
    // For a given prefix, what is the restart index for the first data block
51
    // that contains it.
52
    uint32_t first_index = 0;
53
54
    // How many data blocks contains this prefix?
55
    uint32_t num_blocks = 1;
56
  };
57
58
  // @params own_prefixes indicate if we should take care the memory space for
59
  // the `key_prefix`
60
  // passed by Add()
61
  explicit BlockHashIndex(const SliceTransform* hash_key_extractor,
62
                          bool own_prefixes)
63
5
      : hash_key_extractor_(hash_key_extractor), kOwnPrefixes(own_prefixes) {}
64
65
  // Maps a key to its restart first_index.
66
  // Returns nullptr if the restart first_index is found
67
  const RestartIndex* GetRestartIndex(const Slice& key);
68
69
  bool Add(const Slice& key_prefix, uint32_t restart_index,
70
           uint32_t num_blocks);
71
72
0
  size_t ApproximateMemoryUsage() const {
73
0
    return arena_.ApproximateMemoryUsage();
74
0
  }
75
76
 private:
77
  const SliceTransform* hash_key_extractor_;
78
  std::unordered_map<Slice, RestartIndex, murmur_hash> restart_indices_;
79
80
  Arena arena_;
81
  bool kOwnPrefixes;
82
};
83
84
// Create hash index by reading from the metadata blocks.
85
// @params prefixes: a sequence of prefixes.
86
// @params prefix_meta: contains the "metadata" to of the prefixes.
87
Status CreateBlockHashIndex(const SliceTransform* hash_key_extractor,
88
                            const Slice& prefixes, const Slice& prefix_meta,
89
                            BlockHashIndex** hash_index);
90
91
// Create hash index by scanning the entries in index as well as the whole
92
// dataset.
93
// @params index_iter: an iterator with the pointer to the first entry in a
94
//                     block.
95
// @params data_iter: an iterator that can scan all the entries reside in a
96
//                     table.
97
// @params num_restarts: used for correctness verification.
98
// @params hash_key_extractor: extract the hashable part of a given key.
99
// On error, nullptr will be returned.
100
BlockHashIndex* CreateBlockHashIndexOnTheFly(
101
    InternalIterator* index_iter, InternalIterator* data_iter,
102
    const uint32_t num_restarts, const Comparator* comparator,
103
    const SliceTransform* hash_key_extractor);
104
105
}  // namespace rocksdb
106
107
#endif // ROCKSDB_TABLE_BLOCK_HASH_INDEX_H