block_prefix_index.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #pragma once
  6. #include <stdint.h>
  7. #include "rocksdb/status.h"
  8. namespace ROCKSDB_NAMESPACE {
  9. class Comparator;
  10. class Iterator;
  11. class Slice;
  12. class SliceTransform;
  13. // Build a hash-based index to speed up the lookup for "index block".
  14. // BlockHashIndex accepts a key and, if found, returns its restart index within
  15. // that index block.
  16. class BlockPrefixIndex {
  17. public:
  18. // Maps a key to a list of data blocks that could potentially contain
  19. // the key, based on the prefix.
  20. // Returns the total number of relevant blocks, 0 means the key does
  21. // not exist.
  22. uint32_t GetBlocks(const Slice& key, uint32_t** blocks);
  23. size_t ApproximateMemoryUsage() const {
  24. return sizeof(BlockPrefixIndex) +
  25. (num_block_array_buffer_entries_ + num_buckets_) * sizeof(uint32_t);
  26. }
  27. // Create hash index by reading from the metadata blocks.
  28. // @params prefixes: a sequence of prefixes.
  29. // @params prefix_meta: contains the "metadata" to of the prefixes.
  30. static Status Create(const SliceTransform* hash_key_extractor,
  31. const Slice& prefixes, const Slice& prefix_meta,
  32. BlockPrefixIndex** prefix_index);
  33. ~BlockPrefixIndex() {
  34. delete[] buckets_;
  35. delete[] block_array_buffer_;
  36. }
  37. private:
  38. class Builder;
  39. friend Builder;
  40. BlockPrefixIndex(const SliceTransform* internal_prefix_extractor,
  41. uint32_t num_buckets, uint32_t* buckets,
  42. uint32_t num_block_array_buffer_entries,
  43. uint32_t* block_array_buffer)
  44. : internal_prefix_extractor_(internal_prefix_extractor),
  45. num_buckets_(num_buckets),
  46. num_block_array_buffer_entries_(num_block_array_buffer_entries),
  47. buckets_(buckets),
  48. block_array_buffer_(block_array_buffer) {}
  49. const SliceTransform* internal_prefix_extractor_;
  50. uint32_t num_buckets_;
  51. uint32_t num_block_array_buffer_entries_;
  52. uint32_t* buckets_;
  53. uint32_t* block_array_buffer_;
  54. };
  55. } // namespace ROCKSDB_NAMESPACE