diff --git a/include/rocksdb/env.h b/include/rocksdb/env.h index 9e7d317933..d81960c437 100644 --- a/include/rocksdb/env.h +++ b/include/rocksdb/env.h @@ -1002,7 +1002,7 @@ class WritableFile { /* * Get the size of valid data in the file. */ - virtual uint64_t GetFileSize() { return 0; } + virtual uint64_t GetFileSize() = 0; /* * Get and set the default pre-allocation block size for writes to diff --git a/include/rocksdb/file_system.h b/include/rocksdb/file_system.h index 08b339f2c7..fb2fc8dbb7 100644 --- a/include/rocksdb/file_system.h +++ b/include/rocksdb/file_system.h @@ -1144,9 +1144,7 @@ class FSWritableFile { * Get the size of valid data in the file. */ virtual uint64_t GetFileSize(const IOOptions& /*options*/, - IODebugContext* /*dbg*/) { - return 0; - } + IODebugContext* /*dbg*/) = 0; /* * Get and set the default pre-allocation block size for writes to diff --git a/test_util/testutil.h b/test_util/testutil.h index c484db649e..5643c82b65 100644 --- a/test_util/testutil.h +++ b/test_util/testutil.h @@ -189,6 +189,11 @@ class StringSink : public FSWritableFile { } } + uint64_t GetFileSize(const IOOptions& /*options*/, + IODebugContext* /*dbg*/) override { + return contents_.size(); + } + private: Slice* reader_contents_; size_t last_flush_; @@ -285,6 +290,11 @@ class OverwritingStringSink : public FSWritableFile { if (last_flush_ > contents_.size()) last_flush_ = contents_.size(); } + uint64_t GetFileSize(const IOOptions& /*options*/, + IODebugContext* /*dbg*/) override { + return contents_.size(); + } + private: std::string contents_; Slice* reader_contents_; @@ -562,6 +572,14 @@ class StringFS : public FileSystemWrapper { return IOStatus::OK(); } + uint64_t GetFileSize(const IOOptions& /*options*/, + IODebugContext* /*dbg*/) override { + if (contents_ != nullptr) { + return contents_->size(); + } + return 0; + } + private: std::string* contents_; }; diff --git a/unreleased_history/public_api_changes/remove_default_get_file_size.md b/unreleased_history/public_api_changes/remove_default_get_file_size.md new file mode 100644 index 0000000000..b983a25b7f --- /dev/null +++ b/unreleased_history/public_api_changes/remove_default_get_file_size.md @@ -0,0 +1 @@ +*Remove the default `WritableFile::GetFileSize` and `FSWritableFile::GetFileSize` implementation that returns 0 and make it pure virtual, so that subclasses are enforced to explicitly provide an implementation. \ No newline at end of file diff --git a/util/file_reader_writer_test.cc b/util/file_reader_writer_test.cc index dfdd7d32c7..f317119e75 100644 --- a/util/file_reader_writer_test.cc +++ b/util/file_reader_writer_test.cc @@ -463,6 +463,11 @@ TEST_F(WritableFileWriterTest, AppendStatusReturn) { void Setuse_direct_io(bool val) { use_direct_io_ = val; } void SetIOError(bool val) { io_error_ = val; } + uint64_t GetFileSize(const IOOptions& /*options*/, + IODebugContext* /*dbg*/) override { + return 0; + } + protected: bool use_direct_io_; bool io_error_; @@ -862,6 +867,11 @@ TEST_F(DBWritableFileWriterTest, IOErrorNotification) { ASSERT_EQ(file_flush_errors_, file_flush_errors); } + uint64_t GetFileSize(const IOOptions& /*options*/, + IODebugContext* /*dbg*/) override { + return 0; + } + protected: bool io_error_; std::atomic file_append_errors_; diff --git a/utilities/fault_injection_fs.h b/utilities/fault_injection_fs.h index 45691ba1b3..6b4836d503 100644 --- a/utilities/fault_injection_fs.h +++ b/utilities/fault_injection_fs.h @@ -96,6 +96,11 @@ class TestFSWritableFile : public FSWritableFile { return target_->use_direct_io(); } + virtual uint64_t GetFileSize(const IOOptions& options, + IODebugContext* dbg) override { + return target_->GetFileSize(options, dbg); + } + private: FSFileState state_; // Need protection by mutex_ FileOptions file_opts_;