]> git.proxmox.com Git - ceph.git/blame - ceph/src/rocksdb/table/mock_table.h
import quincy beta 17.1.0
[ceph.git] / ceph / src / rocksdb / table / mock_table.h
CommitLineData
7c673cae 1// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
11fdf7f2
TL
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).
7c673cae
FG
5#pragma once
6
7#include <algorithm>
8#include <atomic>
9#include <map>
10#include <memory>
11#include <set>
12#include <string>
13#include <utility>
14
f67539c2 15#include "db/version_edit.h"
7c673cae
FG
16#include "port/port.h"
17#include "rocksdb/comparator.h"
20effc67 18#include "rocksdb/io_status.h"
7c673cae
FG
19#include "rocksdb/table.h"
20#include "table/internal_iterator.h"
21#include "table/table_builder.h"
22#include "table/table_reader.h"
f67539c2
TL
23#include "test_util/testharness.h"
24#include "test_util/testutil.h"
25#include "util/kv_map.h"
7c673cae 26#include "util/mutexlock.h"
7c673cae 27
f67539c2 28namespace ROCKSDB_NAMESPACE {
7c673cae 29namespace mock {
20effc67
TL
30using KVPair = std::pair<std::string, std::string>;
31using KVVector = std::vector<KVPair>;
7c673cae 32
20effc67
TL
33KVVector MakeMockFile(std::initializer_list<KVPair> l = {});
34void SortKVVector(KVVector* kv_vector,
35 const Comparator* ucmp = BytewiseComparator());
7c673cae
FG
36
37struct MockTableFileSystem {
38 port::Mutex mutex;
20effc67 39 std::map<uint32_t, KVVector> files;
7c673cae
FG
40};
41
42class MockTableFactory : public TableFactory {
43 public:
20effc67
TL
44 enum MockCorruptionMode {
45 kCorruptNone,
46 kCorruptKey,
47 kCorruptValue,
48 kCorruptReorderKey,
49 };
50
7c673cae
FG
51 MockTableFactory();
52 const char* Name() const override { return "MockTable"; }
20effc67 53 using TableFactory::NewTableReader;
7c673cae 54 Status NewTableReader(
20effc67 55 const ReadOptions& ro, const TableReaderOptions& table_reader_options,
494da23a
TL
56 std::unique_ptr<RandomAccessFileReader>&& file, uint64_t file_size,
57 std::unique_ptr<TableReader>* table_reader,
7c673cae
FG
58 bool prefetch_index_and_filter_in_cache = true) const override;
59 TableBuilder* NewTableBuilder(
60 const TableBuilderOptions& table_builder_options,
61 uint32_t column_familly_id, WritableFileWriter* file) const override;
62
63 // This function will directly create mock table instead of going through
64 // MockTableBuilder. file_contents has to have a format of <internal_key,
65 // value>. Those key-value pairs will then be inserted into the mock table.
66 Status CreateMockTable(Env* env, const std::string& fname,
20effc67 67 KVVector file_contents);
7c673cae 68
20effc67 69 virtual std::string GetPrintableOptions() const override {
7c673cae
FG
70 return std::string();
71 }
72
20effc67 73 void SetCorruptionMode(MockCorruptionMode mode) { corrupt_mode_ = mode; }
7c673cae
FG
74 // This function will assert that only a single file exists and that the
75 // contents are equal to file_contents
20effc67
TL
76 void AssertSingleFile(const KVVector& file_contents);
77 void AssertLatestFile(const KVVector& file_contents);
7c673cae
FG
78
79 private:
80 uint32_t GetAndWriteNextID(WritableFileWriter* file) const;
81 uint32_t GetIDFromFile(RandomAccessFileReader* file) const;
82
83 mutable MockTableFileSystem file_system_;
84 mutable std::atomic<uint32_t> next_id_;
20effc67 85 MockCorruptionMode corrupt_mode_;
7c673cae
FG
86};
87
88} // namespace mock
f67539c2 89} // namespace ROCKSDB_NAMESPACE