cuckoo_table_builder.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 <stdint.h>
  8. #include <limits>
  9. #include <string>
  10. #include <utility>
  11. #include <vector>
  12. #include "db/version_edit.h"
  13. #include "port/port.h"
  14. #include "rocksdb/status.h"
  15. #include "rocksdb/table.h"
  16. #include "rocksdb/table_properties.h"
  17. #include "table/table_builder.h"
  18. #include "util/autovector.h"
  19. namespace ROCKSDB_NAMESPACE {
  20. class CuckooTableBuilder: public TableBuilder {
  21. public:
  22. CuckooTableBuilder(WritableFileWriter* file, double max_hash_table_ratio,
  23. uint32_t max_num_hash_func, uint32_t max_search_depth,
  24. const Comparator* user_comparator,
  25. uint32_t cuckoo_block_size, bool use_module_hash,
  26. bool identity_as_first_hash,
  27. uint64_t (*get_slice_hash)(const Slice&, uint32_t,
  28. uint64_t),
  29. uint32_t column_family_id,
  30. const std::string& column_family_name);
  31. // No copying allowed
  32. CuckooTableBuilder(const CuckooTableBuilder&) = delete;
  33. void operator=(const CuckooTableBuilder&) = delete;
  34. // REQUIRES: Either Finish() or Abandon() has been called.
  35. ~CuckooTableBuilder() {}
  36. // Add key,value to the table being constructed.
  37. // REQUIRES: key is after any previously added key according to comparator.
  38. // REQUIRES: Finish(), Abandon() have not been called
  39. void Add(const Slice& key, const Slice& value) override;
  40. // Return non-ok iff some error has been detected.
  41. Status status() const override { return status_; }
  42. // Finish building the table. Stops using the file passed to the
  43. // constructor after this function returns.
  44. // REQUIRES: Finish(), Abandon() have not been called
  45. Status Finish() override;
  46. // Indicate that the contents of this builder should be abandoned. Stops
  47. // using the file passed to the constructor after this function returns.
  48. // If the caller is not going to call Finish(), it must call Abandon()
  49. // before destroying this builder.
  50. // REQUIRES: Finish(), Abandon() have not been called
  51. void Abandon() override;
  52. // Number of calls to Add() so far.
  53. uint64_t NumEntries() const override;
  54. // Size of the file generated so far. If invoked after a successful
  55. // Finish() call, returns the size of the final generated file.
  56. uint64_t FileSize() const override;
  57. TableProperties GetTableProperties() const override { return properties_; }
  58. // Get file checksum
  59. const std::string& GetFileChecksum() const override { return file_checksum_; }
  60. // Get file checksum function name
  61. const char* GetFileChecksumFuncName() const override;
  62. private:
  63. struct CuckooBucket {
  64. CuckooBucket()
  65. : vector_idx(kMaxVectorIdx), make_space_for_key_call_id(0) {}
  66. uint32_t vector_idx;
  67. // This number will not exceed kvs_.size() + max_num_hash_func_.
  68. // We assume number of items is <= 2^32.
  69. uint32_t make_space_for_key_call_id;
  70. };
  71. static const uint32_t kMaxVectorIdx = port::kMaxInt32;
  72. bool MakeSpaceForKey(const autovector<uint64_t>& hash_vals,
  73. const uint32_t call_id,
  74. std::vector<CuckooBucket>* buckets, uint64_t* bucket_id);
  75. Status MakeHashTable(std::vector<CuckooBucket>* buckets);
  76. inline bool IsDeletedKey(uint64_t idx) const;
  77. inline Slice GetKey(uint64_t idx) const;
  78. inline Slice GetUserKey(uint64_t idx) const;
  79. inline Slice GetValue(uint64_t idx) const;
  80. uint32_t num_hash_func_;
  81. WritableFileWriter* file_;
  82. const double max_hash_table_ratio_;
  83. const uint32_t max_num_hash_func_;
  84. const uint32_t max_search_depth_;
  85. const uint32_t cuckoo_block_size_;
  86. uint64_t hash_table_size_;
  87. bool is_last_level_file_;
  88. bool has_seen_first_key_;
  89. bool has_seen_first_value_;
  90. uint64_t key_size_;
  91. uint64_t value_size_;
  92. // A list of fixed-size key-value pairs concatenating into a string.
  93. // Use GetKey(), GetUserKey(), and GetValue() to retrieve a specific
  94. // key / value given an index
  95. std::string kvs_;
  96. std::string deleted_keys_;
  97. // Number of key-value pairs stored in kvs_ + number of deleted keys
  98. uint64_t num_entries_;
  99. // Number of keys that contain value (non-deletion op)
  100. uint64_t num_values_;
  101. Status status_;
  102. TableProperties properties_;
  103. const Comparator* ucomp_;
  104. bool use_module_hash_;
  105. bool identity_as_first_hash_;
  106. uint64_t (*get_slice_hash_)(const Slice& s, uint32_t index,
  107. uint64_t max_num_buckets);
  108. std::string largest_user_key_ = "";
  109. std::string smallest_user_key_ = "";
  110. bool closed_; // Either Finish() or Abandon() has been called.
  111. // Store file checksum. If checksum is disabled, its value is "0"
  112. std::string file_checksum_ = kUnknownFileChecksum;
  113. };
  114. } // namespace ROCKSDB_NAMESPACE
  115. #endif // ROCKSDB_LITE