From 482bbf197287c53f609cdd5d189dcc47f390b202 Mon Sep 17 00:00:00 2001 From: Nathan VanBenschoten Date: Thu, 26 Apr 2018 20:19:21 -0400 Subject: [PATCH 1/2] Add virtual Truncate method to Env 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. --- 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 719e9ebdda1..9b019260dd9 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 ba1e5fa31e7..816256ab08c 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 212b1770a99..12243fdce22 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; From 595465ebf60e84e499a329ddabd2c795b1457263 Mon Sep 17 00:00:00 2001 From: Andrew Kryczka Date: Thu, 26 Apr 2018 18:31:27 -0700 Subject: [PATCH 2/2] comment out unused params --- include/rocksdb/env.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rocksdb/env.h b/include/rocksdb/env.h index 12243fdce22..1f1f0601030 100644 --- a/include/rocksdb/env.h +++ b/include/rocksdb/env.h @@ -259,7 +259,7 @@ class Env { 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) { + virtual Status Truncate(const std::string& /*fname*/, size_t /*size*/) { return Status::NotSupported("Truncate is not supported for this Env"); }