diff --git a/velox/common/file/File.cpp b/velox/common/file/File.cpp index 6547750de5f0..ea5ef04b4b7e 100644 --- a/velox/common/file/File.cpp +++ b/velox/common/file/File.cpp @@ -273,6 +273,7 @@ void LocalWriteFile::append(std::string_view data) { bytesWritten, data.size(), folly::errnoStr(errno)); + size_ += bytesWritten; } void LocalWriteFile::append(std::unique_ptr data) { @@ -298,6 +299,7 @@ void LocalWriteFile::append(std::unique_ptr data) { "Failure in LocalWriteFile::append, {} vs {}", totalBytesWritten, totalBytesToWrite); + size_ += totalBytesWritten; } void LocalWriteFile::flush() { @@ -322,7 +324,4 @@ void LocalWriteFile::close() { } } -uint64_t LocalWriteFile::size() const { - return ftell(file_); -} } // namespace facebook::velox diff --git a/velox/common/file/File.h b/velox/common/file/File.h index acd1359f1e08..2ce64cad3b98 100644 --- a/velox/common/file/File.h +++ b/velox/common/file/File.h @@ -155,7 +155,9 @@ class WriteFile { // Close the file. Any cleanup (disk flush, etc.) will be done here. virtual void close() = 0; - // Current file size, i.e. the sum of all previous Appends. + /// Current file size, i.e. the sum of all previous Appends. No flush should + /// be needed to get the exact size written, and this should be able to be + /// called after the file close. virtual uint64_t size() const = 0; }; @@ -283,11 +285,14 @@ class LocalWriteFile final : public WriteFile { void append(std::unique_ptr data) final; void flush() final; void close() final; - uint64_t size() const final; + + uint64_t size() const final { + return size_; + } private: FILE* file_; - mutable long size_; + uint64_t size_{0}; bool closed_{false}; }; diff --git a/velox/common/file/tests/FileTest.cpp b/velox/common/file/tests/FileTest.cpp index af5654a72369..9f837fc94a23 100644 --- a/velox/common/file/tests/FileTest.cpp +++ b/velox/common/file/tests/FileTest.cpp @@ -134,6 +134,8 @@ TEST(LocalFile, writeAndRead) { { LocalWriteFile writeFile(filename); writeData(&writeFile, useIOBuf); + writeFile.close(); + ASSERT_EQ(writeFile.size(), 15 + kOneMB); } LocalReadFile readFile(filename); readData(&readFile);