mock_table.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 <algorithm>
  7. #include <atomic>
  8. #include <map>
  9. #include <memory>
  10. #include <set>
  11. #include <string>
  12. #include <utility>
  13. #include "db/version_edit.h"
  14. #include "port/port.h"
  15. #include "rocksdb/comparator.h"
  16. #include "rocksdb/io_status.h"
  17. #include "rocksdb/table.h"
  18. #include "table/internal_iterator.h"
  19. #include "table/table_builder.h"
  20. #include "table/table_reader.h"
  21. #include "test_util/testharness.h"
  22. #include "test_util/testutil.h"
  23. #include "util/kv_map.h"
  24. #include "util/mutexlock.h"
  25. namespace ROCKSDB_NAMESPACE {
  26. namespace mock {
  27. using KVPair = std::pair<std::string, std::string>;
  28. using KVVector = std::vector<KVPair>;
  29. KVVector MakeMockFile(std::initializer_list<KVPair> l = {});
  30. void SortKVVector(KVVector* kv_vector,
  31. const Comparator* ucmp = BytewiseComparator());
  32. struct MockTableFileSystem {
  33. port::Mutex mutex;
  34. std::map<uint32_t, KVVector> files;
  35. };
  36. class MockTableFactory : public TableFactory {
  37. public:
  38. enum MockCorruptionMode {
  39. kCorruptNone,
  40. kCorruptKey,
  41. kCorruptValue,
  42. kCorruptReorderKey,
  43. };
  44. MockTableFactory();
  45. static const char* kClassName() { return "MockTable"; }
  46. const char* Name() const override { return kClassName(); }
  47. using TableFactory::NewTableReader;
  48. Status NewTableReader(
  49. const ReadOptions& ro, const TableReaderOptions& table_reader_options,
  50. std::unique_ptr<RandomAccessFileReader>&& file, uint64_t file_size,
  51. std::unique_ptr<TableReader>* table_reader,
  52. bool prefetch_index_and_filter_in_cache = true) const override;
  53. TableBuilder* NewTableBuilder(
  54. const TableBuilderOptions& table_builder_options,
  55. WritableFileWriter* file) const override;
  56. // This function will directly create mock table instead of going through
  57. // MockTableBuilder. file_contents has to have a format of <internal_key,
  58. // value>. Those key-value pairs will then be inserted into the mock table.
  59. Status CreateMockTable(Env* env, const std::string& fname,
  60. KVVector file_contents);
  61. std::string GetPrintableOptions() const override { return std::string(); }
  62. void SetCorruptionMode(MockCorruptionMode mode) { corrupt_mode_ = mode; }
  63. void SetKeyValueSize(size_t size) { key_value_size_ = size; }
  64. // This function will assert that only a single file exists and that the
  65. // contents are equal to file_contents
  66. void AssertSingleFile(const KVVector& file_contents);
  67. void AssertLatestFiles(const std::vector<KVVector>& files_contents);
  68. std::unique_ptr<TableFactory> Clone() const override {
  69. return nullptr; // Not implemented
  70. }
  71. private:
  72. Status GetAndWriteNextID(WritableFileWriter* file, uint32_t* id) const;
  73. Status GetIDFromFile(RandomAccessFileReader* file, uint32_t* id) const;
  74. mutable MockTableFileSystem file_system_;
  75. mutable std::atomic<uint32_t> next_id_;
  76. MockCorruptionMode corrupt_mode_;
  77. size_t key_value_size_ = 1;
  78. };
  79. class MockTableReader : public TableReader {
  80. public:
  81. explicit MockTableReader(const mock::KVVector& table) : table_(table) {
  82. tp_.num_entries = table_.size();
  83. tp_.num_range_deletions = 0;
  84. tp_.raw_key_size = 1;
  85. tp_.raw_value_size = 1;
  86. }
  87. explicit MockTableReader(const mock::KVVector& table,
  88. const TableProperties& tp)
  89. : table_(table), tp_(tp) {}
  90. virtual InternalIterator* NewIterator(
  91. const ReadOptions&, const SliceTransform* prefix_extractor, Arena* arena,
  92. bool skip_filters, TableReaderCaller caller,
  93. size_t compaction_readahead_size = 0,
  94. bool allow_unprepared_value = false) override;
  95. virtual Status Get(const ReadOptions& readOptions, const Slice& key,
  96. GetContext* get_context,
  97. const SliceTransform* prefix_extractor,
  98. bool skip_filters = false) override;
  99. virtual uint64_t ApproximateOffsetOf(const ReadOptions& /*read_options*/,
  100. const Slice& /*key*/,
  101. TableReaderCaller /*caller*/) override {
  102. return 0;
  103. }
  104. virtual uint64_t ApproximateSize(const ReadOptions& /*read_options*/,
  105. const Slice& /*start*/, const Slice& /*end*/,
  106. TableReaderCaller /*caller*/) override {
  107. return 0;
  108. }
  109. virtual size_t ApproximateMemoryUsage() const override { return 0; }
  110. virtual void SetupForCompaction() override {}
  111. virtual std::shared_ptr<const TableProperties> GetTableProperties()
  112. const override {
  113. return std::make_shared<const TableProperties>(tp_);
  114. }
  115. ~MockTableReader() = default;
  116. private:
  117. const KVVector& table_;
  118. TableProperties tp_;
  119. };
  120. } // namespace mock
  121. } // namespace ROCKSDB_NAMESPACE