mirror of
https://github.com/facebook/rocksdb.git
synced 2024-12-03 14:52:53 +00:00
76c834e441
Summary: ... to follow modern C++ style / idioms. Used this hack: ``` for FILE in `cat my_list_of_files`; do perl -pi -e 'BEGIN{undef $/;} s/ virtual( [^;{]* override)/$1/smg' $FILE; done ``` Pull Request resolved: https://github.com/facebook/rocksdb/pull/12319 Test Plan: existing tests, CI Reviewed By: jaykorean Differential Revision: D53275303 Pulled By: pdillinger fbshipit-source-id: bc0881af270aa8ef4d0ae4f44c5a6614b6407377
93 lines
3 KiB
C++
93 lines
3 KiB
C++
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
// 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).
|
|
#pragma once
|
|
|
|
#include <algorithm>
|
|
#include <atomic>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <set>
|
|
#include <string>
|
|
#include <utility>
|
|
|
|
#include "db/version_edit.h"
|
|
#include "port/port.h"
|
|
#include "rocksdb/comparator.h"
|
|
#include "rocksdb/io_status.h"
|
|
#include "rocksdb/table.h"
|
|
#include "table/internal_iterator.h"
|
|
#include "table/table_builder.h"
|
|
#include "table/table_reader.h"
|
|
#include "test_util/testharness.h"
|
|
#include "test_util/testutil.h"
|
|
#include "util/kv_map.h"
|
|
#include "util/mutexlock.h"
|
|
|
|
namespace ROCKSDB_NAMESPACE {
|
|
namespace mock {
|
|
using KVPair = std::pair<std::string, std::string>;
|
|
using KVVector = std::vector<KVPair>;
|
|
|
|
KVVector MakeMockFile(std::initializer_list<KVPair> l = {});
|
|
void SortKVVector(KVVector* kv_vector,
|
|
const Comparator* ucmp = BytewiseComparator());
|
|
|
|
struct MockTableFileSystem {
|
|
port::Mutex mutex;
|
|
std::map<uint32_t, KVVector> files;
|
|
};
|
|
|
|
class MockTableFactory : public TableFactory {
|
|
public:
|
|
enum MockCorruptionMode {
|
|
kCorruptNone,
|
|
kCorruptKey,
|
|
kCorruptValue,
|
|
kCorruptReorderKey,
|
|
};
|
|
|
|
MockTableFactory();
|
|
static const char* kClassName() { return "MockTable"; }
|
|
const char* Name() const override { return kClassName(); }
|
|
using TableFactory::NewTableReader;
|
|
Status NewTableReader(
|
|
const ReadOptions& ro, const TableReaderOptions& table_reader_options,
|
|
std::unique_ptr<RandomAccessFileReader>&& file, uint64_t file_size,
|
|
std::unique_ptr<TableReader>* table_reader,
|
|
bool prefetch_index_and_filter_in_cache = true) const override;
|
|
TableBuilder* NewTableBuilder(
|
|
const TableBuilderOptions& table_builder_options,
|
|
WritableFileWriter* file) const override;
|
|
|
|
// This function will directly create mock table instead of going through
|
|
// MockTableBuilder. file_contents has to have a format of <internal_key,
|
|
// value>. Those key-value pairs will then be inserted into the mock table.
|
|
Status CreateMockTable(Env* env, const std::string& fname,
|
|
KVVector file_contents);
|
|
|
|
std::string GetPrintableOptions() const override { return std::string(); }
|
|
|
|
void SetCorruptionMode(MockCorruptionMode mode) { corrupt_mode_ = mode; }
|
|
|
|
void SetKeyValueSize(size_t size) { key_value_size_ = size; }
|
|
// This function will assert that only a single file exists and that the
|
|
// contents are equal to file_contents
|
|
void AssertSingleFile(const KVVector& file_contents);
|
|
void AssertLatestFiles(const std::vector<KVVector>& files_contents);
|
|
|
|
private:
|
|
Status GetAndWriteNextID(WritableFileWriter* file, uint32_t* id) const;
|
|
Status GetIDFromFile(RandomAccessFileReader* file, uint32_t* id) const;
|
|
|
|
mutable MockTableFileSystem file_system_;
|
|
mutable std::atomic<uint32_t> next_id_;
|
|
MockCorruptionMode corrupt_mode_;
|
|
|
|
size_t key_value_size_ = 1;
|
|
};
|
|
|
|
} // namespace mock
|
|
} // namespace ROCKSDB_NAMESPACE
|