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
main
Nathan VanBenschoten 7 years ago committed by Facebook Github Bot
parent db36f222d8
commit 37cd617b6b
  1. 23
      env/mock_env.cc
  2. 5
      env/mock_env.h
  3. 5
      include/rocksdb/env.h

23
env/mock_env.cc vendored

@ -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_);

5
env/mock_env.h vendored

@ -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()

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

Loading…
Cancel
Save