Remove WritableFile(FSWritableFile)::GetFileSize default implementation (#12303)

Summary:
As titled. This changes public API behavior, and subclasses of `WritableFile` and `FSWritableFile` need to explicitly provide an implementation for the `GetFileSize` method after this change.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/12303

Reviewed By: ajkr

Differential Revision: D53205769

Pulled By: jowlyzhang

fbshipit-source-id: 2e613ca3650302913821b33159b742bdf1d24bc7
This commit is contained in:
Yu Zhang 2024-01-30 09:49:32 -08:00 committed by Facebook GitHub Bot
parent aacf60dda2
commit b10c171e58
6 changed files with 36 additions and 4 deletions

View File

@ -1002,7 +1002,7 @@ class WritableFile {
/* /*
* Get the size of valid data in the file. * 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 * Get and set the default pre-allocation block size for writes to

View File

@ -1144,9 +1144,7 @@ class FSWritableFile {
* Get the size of valid data in the file. * Get the size of valid data in the file.
*/ */
virtual uint64_t GetFileSize(const IOOptions& /*options*/, virtual uint64_t GetFileSize(const IOOptions& /*options*/,
IODebugContext* /*dbg*/) { IODebugContext* /*dbg*/) = 0;
return 0;
}
/* /*
* Get and set the default pre-allocation block size for writes to * Get and set the default pre-allocation block size for writes to

View File

@ -189,6 +189,11 @@ class StringSink : public FSWritableFile {
} }
} }
uint64_t GetFileSize(const IOOptions& /*options*/,
IODebugContext* /*dbg*/) override {
return contents_.size();
}
private: private:
Slice* reader_contents_; Slice* reader_contents_;
size_t last_flush_; size_t last_flush_;
@ -285,6 +290,11 @@ class OverwritingStringSink : public FSWritableFile {
if (last_flush_ > contents_.size()) last_flush_ = contents_.size(); if (last_flush_ > contents_.size()) last_flush_ = contents_.size();
} }
uint64_t GetFileSize(const IOOptions& /*options*/,
IODebugContext* /*dbg*/) override {
return contents_.size();
}
private: private:
std::string contents_; std::string contents_;
Slice* reader_contents_; Slice* reader_contents_;
@ -562,6 +572,14 @@ class StringFS : public FileSystemWrapper {
return IOStatus::OK(); return IOStatus::OK();
} }
uint64_t GetFileSize(const IOOptions& /*options*/,
IODebugContext* /*dbg*/) override {
if (contents_ != nullptr) {
return contents_->size();
}
return 0;
}
private: private:
std::string* contents_; std::string* contents_;
}; };

View File

@ -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.

View File

@ -463,6 +463,11 @@ TEST_F(WritableFileWriterTest, AppendStatusReturn) {
void Setuse_direct_io(bool val) { use_direct_io_ = val; } void Setuse_direct_io(bool val) { use_direct_io_ = val; }
void SetIOError(bool val) { io_error_ = val; } void SetIOError(bool val) { io_error_ = val; }
uint64_t GetFileSize(const IOOptions& /*options*/,
IODebugContext* /*dbg*/) override {
return 0;
}
protected: protected:
bool use_direct_io_; bool use_direct_io_;
bool io_error_; bool io_error_;
@ -862,6 +867,11 @@ TEST_F(DBWritableFileWriterTest, IOErrorNotification) {
ASSERT_EQ(file_flush_errors_, file_flush_errors); ASSERT_EQ(file_flush_errors_, file_flush_errors);
} }
uint64_t GetFileSize(const IOOptions& /*options*/,
IODebugContext* /*dbg*/) override {
return 0;
}
protected: protected:
bool io_error_; bool io_error_;
std::atomic<size_t> file_append_errors_; std::atomic<size_t> file_append_errors_;

View File

@ -96,6 +96,11 @@ class TestFSWritableFile : public FSWritableFile {
return target_->use_direct_io(); return target_->use_direct_io();
} }
virtual uint64_t GetFileSize(const IOOptions& options,
IODebugContext* dbg) override {
return target_->GetFileSize(options, dbg);
}
private: private:
FSFileState state_; // Need protection by mutex_ FSFileState state_; // Need protection by mutex_
FileOptions file_opts_; FileOptions file_opts_;