cuckoo_table_factory.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #ifndef ROCKSDB_LITE
  7. #include <string>
  8. #include "rocksdb/table.h"
  9. #include "util/murmurhash.h"
  10. #include "rocksdb/options.h"
  11. namespace ROCKSDB_NAMESPACE {
  12. const uint32_t kCuckooMurmurSeedMultiplier = 816922183;
  13. static inline uint64_t CuckooHash(
  14. const Slice& user_key, uint32_t hash_cnt, bool use_module_hash,
  15. uint64_t table_size_, bool identity_as_first_hash,
  16. uint64_t (*get_slice_hash)(const Slice&, uint32_t, uint64_t)) {
  17. #if !defined NDEBUG || defined OS_WIN
  18. // This part is used only in unit tests but we have to keep it for Windows
  19. // build as we run test in both debug and release modes under Windows.
  20. if (get_slice_hash != nullptr) {
  21. return get_slice_hash(user_key, hash_cnt, table_size_);
  22. }
  23. #else
  24. (void)get_slice_hash;
  25. #endif
  26. uint64_t value = 0;
  27. if (hash_cnt == 0 && identity_as_first_hash) {
  28. value = (*reinterpret_cast<const int64_t*>(user_key.data()));
  29. } else {
  30. value = MurmurHash(user_key.data(), static_cast<int>(user_key.size()),
  31. kCuckooMurmurSeedMultiplier * hash_cnt);
  32. }
  33. if (use_module_hash) {
  34. return value % table_size_;
  35. } else {
  36. return value & (table_size_ - 1);
  37. }
  38. }
  39. // Cuckoo Table is designed for applications that require fast point lookups
  40. // but not fast range scans.
  41. //
  42. // Some assumptions:
  43. // - Key length and Value length are fixed.
  44. // - Does not support Snapshot.
  45. // - Does not support Merge operations.
  46. // - Does not support prefix bloom filters.
  47. class CuckooTableFactory : public TableFactory {
  48. public:
  49. explicit CuckooTableFactory(const CuckooTableOptions& table_options)
  50. : table_options_(table_options) {}
  51. ~CuckooTableFactory() {}
  52. const char* Name() const override { return "CuckooTable"; }
  53. Status NewTableReader(
  54. const TableReaderOptions& table_reader_options,
  55. std::unique_ptr<RandomAccessFileReader>&& file, uint64_t file_size,
  56. std::unique_ptr<TableReader>* table,
  57. bool prefetch_index_and_filter_in_cache = true) const override;
  58. TableBuilder* NewTableBuilder(
  59. const TableBuilderOptions& table_builder_options,
  60. uint32_t column_family_id, WritableFileWriter* file) const override;
  61. // Sanitizes the specified DB Options.
  62. Status SanitizeOptions(
  63. const DBOptions& /*db_opts*/,
  64. const ColumnFamilyOptions& /*cf_opts*/) const override {
  65. return Status::OK();
  66. }
  67. std::string GetPrintableTableOptions() const override;
  68. void* GetOptions() override { return &table_options_; }
  69. Status GetOptionString(std::string* /*opt_string*/,
  70. const std::string& /*delimiter*/) const override {
  71. return Status::OK();
  72. }
  73. private:
  74. CuckooTableOptions table_options_;
  75. };
  76. } // namespace ROCKSDB_NAMESPACE
  77. #endif // ROCKSDB_LITE