mock_table.h 2.9 KB
Newer Older
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
S
Siying Dong 已提交
2 3 4
//  This source code is licensed under both the GPLv2 (found in the
//  COPYING file in the root directory) and Apache 2.0 License
//  (found in the LICENSE.Apache file in the root directory).
5
#pragma once
6

7
#include <algorithm>
I
Igor Canadi 已提交
8
#include <atomic>
9
#include <map>
10 11
#include <memory>
#include <set>
12
#include <string>
13
#include <utility>
14

15
#include "db/version_edit.h"
16
#include "port/port.h"
A
Andres Notzli 已提交
17
#include "rocksdb/comparator.h"
18
#include "rocksdb/io_status.h"
19
#include "rocksdb/table.h"
S
sdong 已提交
20
#include "table/internal_iterator.h"
21
#include "table/table_builder.h"
22
#include "table/table_reader.h"
23 24
#include "test_util/testharness.h"
#include "test_util/testutil.h"
25 26
#include "util/kv_map.h"
#include "util/mutexlock.h"
27

28
namespace ROCKSDB_NAMESPACE {
I
Igor Canadi 已提交
29
namespace mock {
30 31
using KVPair = std::pair<std::string, std::string>;
using KVVector = std::vector<KVPair>;
32

33
KVVector MakeMockFile(std::initializer_list<KVPair> l = {});
34 35
void SortKVVector(KVVector* kv_vector,
                  const Comparator* ucmp = BytewiseComparator());
36 37 38

struct MockTableFileSystem {
  port::Mutex mutex;
39
  std::map<uint32_t, KVVector> files;
40 41 42 43
};

class MockTableFactory : public TableFactory {
 public:
44 45 46 47
  enum MockCorruptionMode {
    kCorruptNone,
    kCorruptKey,
    kCorruptValue,
48
    kCorruptReorderKey,
49 50
  };

51 52
  MockTableFactory();
  const char* Name() const override { return "MockTable"; }
53
  using TableFactory::NewTableReader;
54
  Status NewTableReader(
55
      const ReadOptions& ro, const TableReaderOptions& table_reader_options,
56 57
      std::unique_ptr<RandomAccessFileReader>&& file, uint64_t file_size,
      std::unique_ptr<TableReader>* table_reader,
58
      bool prefetch_index_and_filter_in_cache = true) const override;
59 60
  TableBuilder* NewTableBuilder(
      const TableBuilderOptions& table_builder_options,
61
      uint32_t column_familly_id, WritableFileWriter* file) const override;
62

I
Igor Canadi 已提交
63
  // This function will directly create mock table instead of going through
64 65
  // MockTableBuilder. file_contents has to have a format of <internal_key,
  // value>. Those key-value pairs will then be inserted into the mock table.
I
Igor Canadi 已提交
66
  Status CreateMockTable(Env* env, const std::string& fname,
67
                         KVVector file_contents);
I
Igor Canadi 已提交
68

69
  virtual std::string GetPrintableOptions() const override {
70 71 72
    return std::string();
  }

73
  void SetCorruptionMode(MockCorruptionMode mode) { corrupt_mode_ = mode; }
74 75
  // This function will assert that only a single file exists and that the
  // contents are equal to file_contents
76 77
  void AssertSingleFile(const KVVector& file_contents);
  void AssertLatestFile(const KVVector& file_contents);
78 79

 private:
D
Dmitri Smirnov 已提交
80
  uint32_t GetAndWriteNextID(WritableFileWriter* file) const;
81
  uint32_t GetIDFromFile(RandomAccessFileReader* file) const;
82 83 84

  mutable MockTableFileSystem file_system_;
  mutable std::atomic<uint32_t> next_id_;
85
  MockCorruptionMode corrupt_mode_;
86 87
};

I
Igor Canadi 已提交
88
}  // namespace mock
89
}  // namespace ROCKSDB_NAMESPACE