mock_env.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. //
  6. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  9. #pragma once
  10. #include <atomic>
  11. #include <map>
  12. #include <string>
  13. #include <vector>
  14. #include "env/composite_env_wrapper.h"
  15. #include "port/port.h"
  16. #include "rocksdb/env.h"
  17. #include "rocksdb/status.h"
  18. #include "rocksdb/system_clock.h"
  19. namespace ROCKSDB_NAMESPACE {
  20. class MemFile;
  21. class MockFileSystem : public FileSystem {
  22. public:
  23. explicit MockFileSystem(const std::shared_ptr<SystemClock>& clock,
  24. bool supports_direct_io = true);
  25. ~MockFileSystem() override;
  26. static const char* kClassName() { return "MemoryFileSystem"; }
  27. const char* Name() const override { return kClassName(); }
  28. IOStatus NewSequentialFile(const std::string& f, const FileOptions& file_opts,
  29. std::unique_ptr<FSSequentialFile>* r,
  30. IODebugContext* dbg) override;
  31. IOStatus NewRandomAccessFile(const std::string& f,
  32. const FileOptions& file_opts,
  33. std::unique_ptr<FSRandomAccessFile>* r,
  34. IODebugContext* dbg) override;
  35. IOStatus NewRandomRWFile(const std::string& fname,
  36. const FileOptions& file_opts,
  37. std::unique_ptr<FSRandomRWFile>* result,
  38. IODebugContext* dbg) override;
  39. IOStatus ReuseWritableFile(const std::string& fname,
  40. const std::string& old_fname,
  41. const FileOptions& file_opts,
  42. std::unique_ptr<FSWritableFile>* result,
  43. IODebugContext* dbg) override;
  44. IOStatus NewWritableFile(const std::string& fname,
  45. const FileOptions& file_opts,
  46. std::unique_ptr<FSWritableFile>* result,
  47. IODebugContext* dbg) override;
  48. IOStatus ReopenWritableFile(const std::string& fname,
  49. const FileOptions& options,
  50. std::unique_ptr<FSWritableFile>* result,
  51. IODebugContext* dbg) override;
  52. IOStatus NewDirectory(const std::string& /*name*/, const IOOptions& io_opts,
  53. std::unique_ptr<FSDirectory>* result,
  54. IODebugContext* dbg) override;
  55. IOStatus FileExists(const std::string& fname, const IOOptions& /*io_opts*/,
  56. IODebugContext* /*dbg*/) override;
  57. IOStatus GetChildren(const std::string& dir, const IOOptions& options,
  58. std::vector<std::string>* result,
  59. IODebugContext* dbg) override;
  60. IOStatus DeleteFile(const std::string& fname, const IOOptions& options,
  61. IODebugContext* dbg) override;
  62. IOStatus Truncate(const std::string& fname, size_t size,
  63. const IOOptions& options, IODebugContext* dbg) override;
  64. IOStatus CreateDir(const std::string& dirname, const IOOptions& options,
  65. IODebugContext* dbg) override;
  66. IOStatus CreateDirIfMissing(const std::string& dirname,
  67. const IOOptions& options,
  68. IODebugContext* dbg) override;
  69. IOStatus DeleteDir(const std::string& dirname, const IOOptions& options,
  70. IODebugContext* dbg) override;
  71. IOStatus GetFileSize(const std::string& fname, const IOOptions& options,
  72. uint64_t* file_size, IODebugContext* dbg) override;
  73. IOStatus GetFileModificationTime(const std::string& fname,
  74. const IOOptions& options,
  75. uint64_t* file_mtime,
  76. IODebugContext* dbg) override;
  77. IOStatus RenameFile(const std::string& src, const std::string& target,
  78. const IOOptions& options, IODebugContext* dbg) override;
  79. IOStatus LinkFile(const std::string& /*src*/, const std::string& /*target*/,
  80. const IOOptions& /*options*/,
  81. IODebugContext* /*dbg*/) override;
  82. IOStatus LockFile(const std::string& fname, const IOOptions& options,
  83. FileLock** lock, IODebugContext* dbg) override;
  84. IOStatus UnlockFile(FileLock* lock, const IOOptions& options,
  85. IODebugContext* dbg) override;
  86. IOStatus GetTestDirectory(const IOOptions& options, std::string* path,
  87. IODebugContext* dbg) override;
  88. IOStatus NewLogger(const std::string& fname, const IOOptions& io_opts,
  89. std::shared_ptr<Logger>* result,
  90. IODebugContext* dbg) override;
  91. // Get full directory name for this db.
  92. IOStatus GetAbsolutePath(const std::string& db_path,
  93. const IOOptions& /*options*/,
  94. std::string* output_path,
  95. IODebugContext* /*dbg*/) override;
  96. IOStatus IsDirectory(const std::string& /*path*/,
  97. const IOOptions& /*options*/, bool* /*is_dir*/,
  98. IODebugContext* /*dgb*/) override {
  99. return IOStatus::NotSupported("IsDirectory");
  100. }
  101. Status CorruptBuffer(const std::string& fname);
  102. Status PrepareOptions(const ConfigOptions& options) override;
  103. private:
  104. bool RenameFileInternal(const std::string& src, const std::string& dest);
  105. void DeleteFileInternal(const std::string& fname);
  106. bool GetChildrenInternal(const std::string& fname,
  107. std::vector<std::string>* results);
  108. std::string NormalizeMockPath(const std::string& path);
  109. private:
  110. // Map from filenames to MemFile objects, representing a simple file system.
  111. port::Mutex mutex_;
  112. std::map<std::string, MemFile*> file_map_; // Protected by mutex_.
  113. std::shared_ptr<SystemClock> system_clock_;
  114. SystemClock* clock_;
  115. bool supports_direct_io_;
  116. };
  117. class MockEnv : public CompositeEnvWrapper {
  118. public:
  119. static MockEnv* Create(Env* base);
  120. static MockEnv* Create(Env* base, const std::shared_ptr<SystemClock>& clock);
  121. static const char* kClassName() { return "MockEnv"; }
  122. const char* Name() const override { return kClassName(); }
  123. Status CorruptBuffer(const std::string& fname);
  124. private:
  125. MockEnv(Env* env, const std::shared_ptr<FileSystem>& fs,
  126. const std::shared_ptr<SystemClock>& clock);
  127. };
  128. } // namespace ROCKSDB_NAMESPACE