]>
git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/env/composite_env_wrapper.h
1 // Copyright (c) 2019-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).
8 #include "rocksdb/env.h"
9 #include "rocksdb/file_system.h"
10 #include "rocksdb/system_clock.h"
13 // Windows API macro interference
19 namespace ROCKSDB_NAMESPACE
{
21 class CompositeEnv
: public Env
{
23 // Initialize a CompositeEnvWrapper that delegates all thread/time related
24 // calls to env, and all file operations to fs
25 explicit CompositeEnv(const std::shared_ptr
<FileSystem
>& fs
,
26 const std::shared_ptr
<SystemClock
>& clock
)
29 Status
RegisterDbPaths(const std::vector
<std::string
>& paths
) override
{
30 return file_system_
->RegisterDbPaths(paths
);
32 Status
UnregisterDbPaths(const std::vector
<std::string
>& paths
) override
{
33 return file_system_
->UnregisterDbPaths(paths
);
36 // The following text is boilerplate that forwards all methods to target()
37 Status
NewSequentialFile(const std::string
& f
,
38 std::unique_ptr
<SequentialFile
>* r
,
39 const EnvOptions
& options
) override
;
41 Status
NewRandomAccessFile(const std::string
& f
,
42 std::unique_ptr
<RandomAccessFile
>* r
,
43 const EnvOptions
& options
) override
;
45 Status
NewWritableFile(const std::string
& f
, std::unique_ptr
<WritableFile
>* r
,
46 const EnvOptions
& options
) override
;
48 Status
ReopenWritableFile(const std::string
& fname
,
49 std::unique_ptr
<WritableFile
>* result
,
50 const EnvOptions
& options
) override
;
52 Status
ReuseWritableFile(const std::string
& fname
,
53 const std::string
& old_fname
,
54 std::unique_ptr
<WritableFile
>* r
,
55 const EnvOptions
& options
) override
;
57 Status
NewRandomRWFile(const std::string
& fname
,
58 std::unique_ptr
<RandomRWFile
>* result
,
59 const EnvOptions
& options
) override
;
61 Status
NewMemoryMappedFileBuffer(
62 const std::string
& fname
,
63 std::unique_ptr
<MemoryMappedFileBuffer
>* result
) override
{
64 return file_system_
->NewMemoryMappedFileBuffer(fname
, result
);
67 Status
NewDirectory(const std::string
& name
,
68 std::unique_ptr
<Directory
>* result
) override
;
70 Status
FileExists(const std::string
& f
) override
{
73 return file_system_
->FileExists(f
, io_opts
, &dbg
);
75 Status
GetChildren(const std::string
& dir
,
76 std::vector
<std::string
>* r
) override
{
79 return file_system_
->GetChildren(dir
, io_opts
, r
, &dbg
);
81 Status
GetChildrenFileAttributes(
82 const std::string
& dir
, std::vector
<FileAttributes
>* result
) override
{
85 return file_system_
->GetChildrenFileAttributes(dir
, io_opts
, result
, &dbg
);
87 Status
DeleteFile(const std::string
& f
) override
{
90 return file_system_
->DeleteFile(f
, io_opts
, &dbg
);
92 Status
Truncate(const std::string
& fname
, size_t size
) override
{
95 return file_system_
->Truncate(fname
, size
, io_opts
, &dbg
);
97 Status
CreateDir(const std::string
& d
) override
{
100 return file_system_
->CreateDir(d
, io_opts
, &dbg
);
102 Status
CreateDirIfMissing(const std::string
& d
) override
{
105 return file_system_
->CreateDirIfMissing(d
, io_opts
, &dbg
);
107 Status
DeleteDir(const std::string
& d
) override
{
110 return file_system_
->DeleteDir(d
, io_opts
, &dbg
);
112 Status
GetFileSize(const std::string
& f
, uint64_t* s
) override
{
115 return file_system_
->GetFileSize(f
, io_opts
, s
, &dbg
);
118 Status
GetFileModificationTime(const std::string
& fname
,
119 uint64_t* file_mtime
) override
{
122 return file_system_
->GetFileModificationTime(fname
, io_opts
, file_mtime
,
126 Status
RenameFile(const std::string
& s
, const std::string
& t
) override
{
129 return file_system_
->RenameFile(s
, t
, io_opts
, &dbg
);
132 Status
LinkFile(const std::string
& s
, const std::string
& t
) override
{
135 return file_system_
->LinkFile(s
, t
, io_opts
, &dbg
);
138 Status
NumFileLinks(const std::string
& fname
, uint64_t* count
) override
{
141 return file_system_
->NumFileLinks(fname
, io_opts
, count
, &dbg
);
144 Status
AreFilesSame(const std::string
& first
, const std::string
& second
,
145 bool* res
) override
{
148 return file_system_
->AreFilesSame(first
, second
, io_opts
, res
, &dbg
);
151 Status
LockFile(const std::string
& f
, FileLock
** l
) override
{
154 return file_system_
->LockFile(f
, io_opts
, l
, &dbg
);
157 Status
UnlockFile(FileLock
* l
) override
{
160 return file_system_
->UnlockFile(l
, io_opts
, &dbg
);
163 Status
GetAbsolutePath(const std::string
& db_path
,
164 std::string
* output_path
) override
{
167 return file_system_
->GetAbsolutePath(db_path
, io_opts
, output_path
, &dbg
);
170 Status
NewLogger(const std::string
& fname
,
171 std::shared_ptr
<Logger
>* result
) override
{
174 return file_system_
->NewLogger(fname
, io_opts
, result
, &dbg
);
177 Status
IsDirectory(const std::string
& path
, bool* is_dir
) override
{
180 return file_system_
->IsDirectory(path
, io_opts
, is_dir
, &dbg
);
183 Status
GetTestDirectory(std::string
* path
) override
{
186 return file_system_
->GetTestDirectory(io_opts
, path
, &dbg
);
189 EnvOptions
OptimizeForLogRead(const EnvOptions
& env_options
) const override
{
190 return file_system_
->OptimizeForLogRead(FileOptions(env_options
));
193 EnvOptions
OptimizeForManifestRead(
194 const EnvOptions
& env_options
) const override
{
195 return file_system_
->OptimizeForManifestRead(FileOptions(env_options
));
198 EnvOptions
OptimizeForLogWrite(const EnvOptions
& env_options
,
199 const DBOptions
& db_options
) const override
{
200 return file_system_
->OptimizeForLogWrite(FileOptions(env_options
),
204 EnvOptions
OptimizeForManifestWrite(
205 const EnvOptions
& env_options
) const override
{
206 return file_system_
->OptimizeForManifestWrite(FileOptions(env_options
));
209 EnvOptions
OptimizeForCompactionTableWrite(
210 const EnvOptions
& env_options
,
211 const ImmutableDBOptions
& immutable_ops
) const override
{
212 return file_system_
->OptimizeForCompactionTableWrite(
213 FileOptions(env_options
), immutable_ops
);
215 EnvOptions
OptimizeForCompactionTableRead(
216 const EnvOptions
& env_options
,
217 const ImmutableDBOptions
& db_options
) const override
{
218 return file_system_
->OptimizeForCompactionTableRead(
219 FileOptions(env_options
), db_options
);
221 EnvOptions
OptimizeForBlobFileRead(
222 const EnvOptions
& env_options
,
223 const ImmutableDBOptions
& db_options
) const override
{
224 return file_system_
->OptimizeForBlobFileRead(FileOptions(env_options
),
227 // This seems to clash with a macro on Windows, so #undef it here
231 Status
GetFreeSpace(const std::string
& path
, uint64_t* diskfree
) override
{
234 return file_system_
->GetFreeSpace(path
, io_opts
, diskfree
, &dbg
);
236 uint64_t NowMicros() override
{ return system_clock_
->NowMicros(); }
237 uint64_t NowNanos() override
{ return system_clock_
->NowNanos(); }
239 uint64_t NowCPUNanos() override
{ return system_clock_
->CPUNanos(); }
241 void SleepForMicroseconds(int micros
) override
{
242 system_clock_
->SleepForMicroseconds(micros
);
245 Status
GetCurrentTime(int64_t* unix_time
) override
{
246 return system_clock_
->GetCurrentTime(unix_time
);
248 std::string
TimeToString(uint64_t time
) override
{
249 return system_clock_
->TimeToString(time
);
253 class CompositeEnvWrapper
: public CompositeEnv
{
255 // Initialize a CompositeEnvWrapper that delegates all thread/time related
256 // calls to env, and all file operations to fs
257 explicit CompositeEnvWrapper(Env
* env
)
258 : CompositeEnvWrapper(env
, env
->GetFileSystem(), env
->GetSystemClock()) {}
259 explicit CompositeEnvWrapper(Env
* env
, const std::shared_ptr
<FileSystem
>& fs
)
260 : CompositeEnvWrapper(env
, fs
, env
->GetSystemClock()) {}
262 explicit CompositeEnvWrapper(Env
* env
, const std::shared_ptr
<SystemClock
>& sc
)
263 : CompositeEnvWrapper(env
, env
->GetFileSystem(), sc
) {}
265 explicit CompositeEnvWrapper(Env
* env
, const std::shared_ptr
<FileSystem
>& fs
,
266 const std::shared_ptr
<SystemClock
>& sc
);
268 explicit CompositeEnvWrapper(const std::shared_ptr
<Env
>& env
,
269 const std::shared_ptr
<FileSystem
>& fs
)
270 : CompositeEnvWrapper(env
, fs
, env
->GetSystemClock()) {}
272 explicit CompositeEnvWrapper(const std::shared_ptr
<Env
>& env
,
273 const std::shared_ptr
<SystemClock
>& sc
)
274 : CompositeEnvWrapper(env
, env
->GetFileSystem(), sc
) {}
276 explicit CompositeEnvWrapper(const std::shared_ptr
<Env
>& env
,
277 const std::shared_ptr
<FileSystem
>& fs
,
278 const std::shared_ptr
<SystemClock
>& sc
);
280 static const char* kClassName() { return "CompositeEnv"; }
281 const char* Name() const override
{ return kClassName(); }
282 bool IsInstanceOf(const std::string
& name
) const override
{
283 if (name
== kClassName()) {
286 return CompositeEnv::IsInstanceOf(name
);
289 const Customizable
* Inner() const override
{ return target_
.env
; }
291 Status
PrepareOptions(const ConfigOptions
& options
) override
;
293 std::string
SerializeOptions(const ConfigOptions
& config_options
,
294 const std::string
& header
) const override
;
295 #endif // ROCKSDB_LITE
297 // Return the target to which this Env forwards all calls
298 Env
* env_target() const { return target_
.env
; }
300 #if !defined(OS_WIN) && !defined(ROCKSDB_NO_DYNAMIC_EXTENSION)
301 Status
LoadLibrary(const std::string
& lib_name
,
302 const std::string
& search_path
,
303 std::shared_ptr
<DynamicLibrary
>* result
) override
{
304 return target_
.env
->LoadLibrary(lib_name
, search_path
, result
);
308 void Schedule(void (*f
)(void* arg
), void* a
, Priority pri
,
309 void* tag
= nullptr, void (*u
)(void* arg
) = nullptr) override
{
310 return target_
.env
->Schedule(f
, a
, pri
, tag
, u
);
313 int UnSchedule(void* tag
, Priority pri
) override
{
314 return target_
.env
->UnSchedule(tag
, pri
);
317 void StartThread(void (*f
)(void*), void* a
) override
{
318 return target_
.env
->StartThread(f
, a
);
320 void WaitForJoin() override
{ return target_
.env
->WaitForJoin(); }
321 unsigned int GetThreadPoolQueueLen(Priority pri
= LOW
) const override
{
322 return target_
.env
->GetThreadPoolQueueLen(pri
);
325 int ReserveThreads(int threads_to_be_reserved
, Priority pri
) override
{
326 return target_
.env
->ReserveThreads(threads_to_be_reserved
, pri
);
329 int ReleaseThreads(int threads_to_be_released
, Priority pri
) override
{
330 return target_
.env
->ReleaseThreads(threads_to_be_released
, pri
);
333 Status
GetHostName(char* name
, uint64_t len
) override
{
334 return target_
.env
->GetHostName(name
, len
);
336 void SetBackgroundThreads(int num
, Priority pri
) override
{
337 return target_
.env
->SetBackgroundThreads(num
, pri
);
339 int GetBackgroundThreads(Priority pri
) override
{
340 return target_
.env
->GetBackgroundThreads(pri
);
343 Status
SetAllowNonOwnerAccess(bool allow_non_owner_access
) override
{
344 return target_
.env
->SetAllowNonOwnerAccess(allow_non_owner_access
);
347 void IncBackgroundThreadsIfNeeded(int num
, Priority pri
) override
{
348 return target_
.env
->IncBackgroundThreadsIfNeeded(num
, pri
);
351 void LowerThreadPoolIOPriority(Priority pool
) override
{
352 target_
.env
->LowerThreadPoolIOPriority(pool
);
355 void LowerThreadPoolCPUPriority(Priority pool
) override
{
356 target_
.env
->LowerThreadPoolCPUPriority(pool
);
359 Status
LowerThreadPoolCPUPriority(Priority pool
, CpuPriority pri
) override
{
360 return target_
.env
->LowerThreadPoolCPUPriority(pool
, pri
);
363 Status
GetThreadList(std::vector
<ThreadStatus
>* thread_list
) override
{
364 return target_
.env
->GetThreadList(thread_list
);
367 ThreadStatusUpdater
* GetThreadStatusUpdater() const override
{
368 return target_
.env
->GetThreadStatusUpdater();
371 uint64_t GetThreadID() const override
{ return target_
.env
->GetThreadID(); }
373 std::string
GenerateUniqueId() override
{
374 return target_
.env
->GenerateUniqueId();
378 EnvWrapper::Target target_
;
380 } // namespace ROCKSDB_NAMESPACE