Coverage Report

Created: 2025-07-23 07:17

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