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 #pragma once
7 #include <string>
8
9 #include "file/filename.h"
10 #include "options/db_options.h"
11 #include "rocksdb/env.h"
12 #include "rocksdb/file_system.h"
13 #include "rocksdb/sst_file_writer.h"
14 #include "rocksdb/status.h"
15 #include "rocksdb/system_clock.h"
16 #include "rocksdb/types.h"
17 #include "trace_replay/io_tracer.h"
18
19 namespace ROCKSDB_NAMESPACE {
20 // use_fsync maps to options.use_fsync, which determines the way that
21 // the file is synced after copying.
22 extern IOStatus CopyFile(FileSystem* fs, const std::string& source,
23 const std::string& destination, uint64_t size,
24 bool use_fsync,
25 const std::shared_ptr<IOTracer>& io_tracer = nullptr);
26 inline IOStatus CopyFile(const std::shared_ptr<FileSystem>& fs,
27 const std::string& source,
28 const std::string& destination, uint64_t size,
29 bool use_fsync,
30 const std::shared_ptr<IOTracer>& io_tracer = nullptr) {
31 return CopyFile(fs.get(), source, destination, size, use_fsync, io_tracer);
32 }
33
34 extern IOStatus CreateFile(FileSystem* fs, const std::string& destination,
35 const std::string& contents, bool use_fsync);
36
CreateFile(const std::shared_ptr<FileSystem> & fs,const std::string & destination,const std::string & contents,bool use_fsync)37 inline IOStatus CreateFile(const std::shared_ptr<FileSystem>& fs,
38 const std::string& destination,
39 const std::string& contents, bool use_fsync) {
40 return CreateFile(fs.get(), destination, contents, use_fsync);
41 }
42
43 extern Status DeleteDBFile(const ImmutableDBOptions* db_options,
44 const std::string& fname,
45 const std::string& path_to_sync, const bool force_bg,
46 const bool force_fg);
47
48 extern IOStatus GenerateOneFileChecksum(
49 FileSystem* fs, const std::string& file_path,
50 FileChecksumGenFactory* checksum_factory,
51 const std::string& requested_checksum_func_name, std::string* file_checksum,
52 std::string* file_checksum_func_name,
53 size_t verify_checksums_readahead_size, bool allow_mmap_reads,
54 std::shared_ptr<IOTracer>& io_tracer, RateLimiter* rate_limiter = nullptr);
55
GenerateOneFileChecksum(const std::shared_ptr<FileSystem> & fs,const std::string & file_path,FileChecksumGenFactory * checksum_factory,const std::string & requested_checksum_func_name,std::string * file_checksum,std::string * file_checksum_func_name,size_t verify_checksums_readahead_size,bool allow_mmap_reads,std::shared_ptr<IOTracer> & io_tracer)56 inline IOStatus GenerateOneFileChecksum(
57 const std::shared_ptr<FileSystem>& fs, const std::string& file_path,
58 FileChecksumGenFactory* checksum_factory,
59 const std::string& requested_checksum_func_name, std::string* file_checksum,
60 std::string* file_checksum_func_name,
61 size_t verify_checksums_readahead_size, bool allow_mmap_reads,
62 std::shared_ptr<IOTracer>& io_tracer) {
63 return GenerateOneFileChecksum(
64 fs.get(), file_path, checksum_factory, requested_checksum_func_name,
65 file_checksum, file_checksum_func_name, verify_checksums_readahead_size,
66 allow_mmap_reads, io_tracer);
67 }
68
PrepareIOFromReadOptions(const ReadOptions & ro,SystemClock * clock,IOOptions & opts)69 inline IOStatus PrepareIOFromReadOptions(const ReadOptions& ro,
70 SystemClock* clock, IOOptions& opts) {
71 if (ro.deadline.count()) {
72 std::chrono::microseconds now =
73 std::chrono::microseconds(clock->NowMicros());
74 // Ensure there is atleast 1us available. We don't want to pass a value of
75 // 0 as that means no timeout
76 if (now >= ro.deadline) {
77 return IOStatus::TimedOut("Deadline exceeded");
78 }
79 opts.timeout = ro.deadline - now;
80 }
81
82 if (ro.io_timeout.count() &&
83 (!opts.timeout.count() || ro.io_timeout < opts.timeout)) {
84 opts.timeout = ro.io_timeout;
85 }
86 return IOStatus::OK();
87 }
88
89 // Test method to delete the input directory and all of its contents.
90 // This method is destructive and is meant for use only in tests!!!
91 Status DestroyDir(Env* env, const std::string& dir);
92 } // namespace ROCKSDB_NAMESPACE
93