From 37cd617b6bd9efb04a2a4e9b0bed65431b7732fd Mon Sep 17 00:00:00 2001 From: Nathan VanBenschoten Date: Thu, 26 Apr 2018 21:08:46 -0700 Subject: [PATCH] Add virtual Truncate method to Env Summary: This change adds a virtual `Truncate` method to `Env`, which truncates the named file to the specified size. At the moment, this is only supported for `MockEnv`, but other `Env's` could be extended to override the method too. This is the same approach that methods like `LinkFile` and `AreSameFile` have taken. This is useful for any user of the in-memory `Env`. The implementation's header is not exported, so before this change, it was impossible to access it's already existing `Truncate` method. Closes https://github.com/facebook/rocksdb/pull/3779 Differential Revision: D7785789 Pulled By: ajkr fbshipit-source-id: 3bcdaeea7b7180529f7d9b496dc67b791a00bbf0 --- env/mock_env.cc | 23 +++++++++++------------ env/mock_env.h | 5 ++--- include/rocksdb/env.h | 5 +++++ 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/env/mock_env.cc b/env/mock_env.cc index 719e9ebdd..9b019260d 100644 --- a/env/mock_env.cc +++ b/env/mock_env.cc @@ -575,6 +575,17 @@ Status MockEnv::DeleteFile(const std::string& fname) { return Status::OK(); } +Status MockEnv::Truncate(const std::string& fname, size_t size) { + auto fn = NormalizePath(fname); + MutexLock lock(&mutex_); + auto iter = file_map_.find(fn); + if (iter == file_map_.end()) { + return Status::IOError(fn, "File not found"); + } + iter->second->Truncate(size); + return Status::OK(); +} + Status MockEnv::CreateDir(const std::string& dirname) { auto dn = NormalizePath(dirname); if (file_map_.find(dn) == file_map_.end()) { @@ -725,18 +736,6 @@ uint64_t MockEnv::NowNanos() { return EnvWrapper::NowNanos() + fake_sleep_micros_.load() * 1000; } -// Non-virtual functions, specific to MockEnv -Status MockEnv::Truncate(const std::string& fname, size_t size) { - auto fn = NormalizePath(fname); - MutexLock lock(&mutex_); - auto iter = file_map_.find(fn); - if (iter == file_map_.end()) { - return Status::IOError(fn, "File not found"); - } - iter->second->Truncate(size); - return Status::OK(); -} - Status MockEnv::CorruptBuffer(const std::string& fname) { auto fn = NormalizePath(fname); MutexLock lock(&mutex_); diff --git a/env/mock_env.h b/env/mock_env.h index ba1e5fa31..816256ab0 100644 --- a/env/mock_env.h +++ b/env/mock_env.h @@ -60,6 +60,8 @@ class MockEnv : public EnvWrapper { virtual Status DeleteFile(const std::string& fname) override; + virtual Status Truncate(const std::string& fname, size_t size) override; + virtual Status CreateDir(const std::string& dirname) override; virtual Status CreateDirIfMissing(const std::string& dirname) override; @@ -92,9 +94,6 @@ class MockEnv : public EnvWrapper { virtual uint64_t NowMicros() override; virtual uint64_t NowNanos() override; - // Non-virtual functions, specific to MockEnv - Status Truncate(const std::string& fname, size_t size); - Status CorruptBuffer(const std::string& fname); // Doesn't really sleep, just affects output of GetCurrentTime(), NowMicros() diff --git a/include/rocksdb/env.h b/include/rocksdb/env.h index 212b1770a..1f1f06010 100644 --- a/include/rocksdb/env.h +++ b/include/rocksdb/env.h @@ -258,6 +258,11 @@ class Env { // Delete the named file. virtual Status DeleteFile(const std::string& fname) = 0; + // Truncate the named file to the specified size. + virtual Status Truncate(const std::string& /*fname*/, size_t /*size*/) { + return Status::NotSupported("Truncate is not supported for this Env"); + } + // Create the specified directory. Returns error if directory exists. virtual Status CreateDir(const std::string& dirname) = 0;