Skip to content

Commit

Permalink
Implement Env::NumFileLinks (facebook#4221)
Browse files Browse the repository at this point in the history
Summary:
Although delete scheduler implementation allows for the interface not to be supported, the delete_scheduler_test does not allow for that.
Address compiler warnings
Make sst_dump_test use test directory structure as the current execution directory may not be writiable.
Pull Request resolved: facebook#4221

Differential Revision: D9210152

Pulled By: siying

fbshipit-source-id: 381a74511e969ecb8089d5c4b4df87dc30c8df63
  • Loading branch information
yuslepukhin authored and facebook-github-bot committed Aug 9, 2018
1 parent de7f423 commit ab22cf3
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 49 deletions.
32 changes: 32 additions & 0 deletions port/win/env_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,34 @@ Status WinEnvIO::LinkFile(const std::string& src,
return result;
}

Status WinEnvIO::NumFileLinks(const std::string& fname,
uint64_t* count) {
Status s;
HANDLE handle = ::CreateFileA(fname.c_str(), 0,
FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
NULL);

if (INVALID_HANDLE_VALUE == handle) {
auto lastError = GetLastError();
s = IOErrorFromWindowsError(
"NumFileLinks: " + fname, lastError);
return s;
}
UniqueCloseHandlePtr handle_guard(handle, CloseHandleFunc);
FILE_STANDARD_INFO standard_info;
if (0 != GetFileInformationByHandleEx(handle, FileStandardInfo,
&standard_info, sizeof(standard_info))) {
*count = standard_info.NumberOfLinks;
} else {
auto lastError = GetLastError();
s = IOErrorFromWindowsError("GetFileInformationByHandleEx: " + fname, lastError);
}
return s;
}

Status WinEnvIO::AreFilesSame(const std::string& first,
const std::string& second, bool* res) {
// For MinGW builds
Expand Down Expand Up @@ -1325,6 +1353,10 @@ Status WinEnv::LinkFile(const std::string& src,
return winenv_io_.LinkFile(src, target);
}

Status WinEnv::NumFileLinks(const std::string& fname, uint64_t* count) {
return winenv_io_.NumFileLinks(fname, count);
}

Status WinEnv::AreFilesSame(const std::string& first,
const std::string& second, bool* res) {
return winenv_io_.AreFilesSame(first, second, res);
Expand Down
6 changes: 6 additions & 0 deletions port/win/env_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ class WinEnvIO {
virtual Status LinkFile(const std::string& src,
const std::string& target);

virtual Status NumFileLinks(const std::string& /*fname*/,
uint64_t* /*count*/);

virtual Status AreFilesSame(const std::string& first,
const std::string& second, bool* res);

Expand Down Expand Up @@ -268,6 +271,9 @@ class WinEnv : public Env {
Status LinkFile(const std::string& src,
const std::string& target) override;

Status NumFileLinks(const std::string& fname,
uint64_t* count) override;

Status AreFilesSame(const std::string& first,
const std::string& second, bool* res) override;

Expand Down
8 changes: 8 additions & 0 deletions tools/db_stress.cc
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,11 @@ enum RepFactory StringToRepFactory(const char* ctype) {
return kSkipList;
}

#ifdef _MSC_VER
#pragma warning(push)
// truncation of constant value on static_cast
#pragma warning(disable: 4309)
#endif
bool GetNextPrefix(const rocksdb::Slice& src, std::string* v) {
std::string ret = src.ToString();
for (int i = static_cast<int>(ret.size()) - 1; i >= 0; i--) {
Expand All @@ -604,6 +609,9 @@ bool GetNextPrefix(const rocksdb::Slice& src, std::string* v) {
*v = ret;
return true;
}
#ifdef _MSC_VER
#pragma warning(pop)
#endif
} // namespace

static enum RepFactory FLAGS_rep_factory;
Expand Down
94 changes: 45 additions & 49 deletions tools/sst_dump_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ void createSST(const std::string& file_name,
rocksdb::InternalKeyComparator ikc(opts.comparator);
unique_ptr<TableBuilder> tb;

env->NewWritableFile(file_name, &file, env_options);
ASSERT_OK(env->NewWritableFile(file_name, &file, env_options));

opts.table_factory = tf;
std::vector<std::unique_ptr<IntTblPropCollectorFactory> >
int_tbl_prop_collector_factories;
unique_ptr<WritableFileWriter> file_writer(
std::unique_ptr<WritableFileWriter> file_writer(
new WritableFileWriter(std::move(file), EnvOptions()));
std::string column_family_name;
int unknown_level = -1;
Expand Down Expand Up @@ -90,118 +91,113 @@ void cleanup(const std::string& file_name) {

// Test for sst dump tool "raw" mode
class SSTDumpToolTest : public testing::Test {
public:
std::string testDir_;
public:
BlockBasedTableOptions table_options_;

SSTDumpToolTest() {}
SSTDumpToolTest() {
testDir_ = test::TmpDir();
}

~SSTDumpToolTest() {}

std::string MakeFilePath(const std::string& file_name) const {
std::string path(testDir_);
path.append("/").append(file_name);
return path;
}

template<std::size_t N>
void PopulateCommandArgs(const std::string& file_path, const char* command,
char* (&usage)[N]) const {
for (int i = 0; i < N; ++i) {
usage[i] = new char[optLength];
}
snprintf(usage[0], optLength, "./sst_dump");
snprintf(usage[1], optLength, "%s", command);
snprintf(usage[2], optLength, "--file=%s", file_path.c_str());
}
};

TEST_F(SSTDumpToolTest, EmptyFilter) {
std::string file_name = "rocksdb_sst_test.sst";
createSST(file_name, table_options_);
std::string file_path = MakeFilePath("rocksdb_sst_test.sst");
createSST(file_path, table_options_);

char* usage[3];
for (int i = 0; i < 3; i++) {
usage[i] = new char[optLength];
}
snprintf(usage[0], optLength, "./sst_dump");
snprintf(usage[1], optLength, "--command=raw");
snprintf(usage[2], optLength, "--file=rocksdb_sst_test.sst");
PopulateCommandArgs(file_path, "--command=raw", usage);

rocksdb::SSTDumpTool tool;
ASSERT_TRUE(!tool.Run(3, usage));

cleanup(file_name);
cleanup(file_path);
for (int i = 0; i < 3; i++) {
delete[] usage[i];
}
}

TEST_F(SSTDumpToolTest, FilterBlock) {
table_options_.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, true));
std::string file_name = "rocksdb_sst_test.sst";
createSST(file_name, table_options_);
std::string file_path = MakeFilePath("rocksdb_sst_test.sst");
createSST(file_path, table_options_);

char* usage[3];
for (int i = 0; i < 3; i++) {
usage[i] = new char[optLength];
}
snprintf(usage[0], optLength, "./sst_dump");
snprintf(usage[1], optLength, "--command=raw");
snprintf(usage[2], optLength, "--file=rocksdb_sst_test.sst");
PopulateCommandArgs(file_path, "--command=raw", usage);

rocksdb::SSTDumpTool tool;
ASSERT_TRUE(!tool.Run(3, usage));

cleanup(file_name);
cleanup(file_path);
for (int i = 0; i < 3; i++) {
delete[] usage[i];
}
}

TEST_F(SSTDumpToolTest, FullFilterBlock) {
table_options_.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, false));
std::string file_name = "rocksdb_sst_test.sst";
createSST(file_name, table_options_);
std::string file_path = MakeFilePath("rocksdb_sst_test.sst");
createSST(file_path, table_options_);

char* usage[3];
for (int i = 0; i < 3; i++) {
usage[i] = new char[optLength];
}
snprintf(usage[0], optLength, "./sst_dump");
snprintf(usage[1], optLength, "--command=raw");
snprintf(usage[2], optLength, "--file=rocksdb_sst_test.sst");
PopulateCommandArgs(file_path, "--command=raw", usage);

rocksdb::SSTDumpTool tool;
ASSERT_TRUE(!tool.Run(3, usage));

cleanup(file_name);
cleanup(file_path);
for (int i = 0; i < 3; i++) {
delete[] usage[i];
}
}

TEST_F(SSTDumpToolTest, GetProperties) {
table_options_.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, false));
std::string file_name = "rocksdb_sst_test.sst";
createSST(file_name, table_options_);
std::string file_path = MakeFilePath("rocksdb_sst_test.sst");
createSST(file_path, table_options_);

char* usage[3];
for (int i = 0; i < 3; i++) {
usage[i] = new char[optLength];
}
snprintf(usage[0], optLength, "./sst_dump");
snprintf(usage[1], optLength, "--show_properties");
snprintf(usage[2], optLength, "--file=rocksdb_sst_test.sst");
PopulateCommandArgs(file_path, "--show_properties", usage);

rocksdb::SSTDumpTool tool;
ASSERT_TRUE(!tool.Run(3, usage));

cleanup(file_name);
cleanup(file_path);
for (int i = 0; i < 3; i++) {
delete[] usage[i];
}
}

TEST_F(SSTDumpToolTest, CompressedSizes) {
table_options_.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, false));
std::string file_name = "rocksdb_sst_test.sst";
createSST(file_name, table_options_);
std::string file_path = MakeFilePath("rocksdb_sst_test.sst");
createSST(file_path, table_options_);

char* usage[3];
for (int i = 0; i < 3; i++) {
usage[i] = new char[optLength];
}
PopulateCommandArgs(file_path, "--command=recompress", usage);

snprintf(usage[0], optLength, "./sst_dump");
snprintf(usage[1], optLength, "--command=recompress");
snprintf(usage[2], optLength, "--file=rocksdb_sst_test.sst");
rocksdb::SSTDumpTool tool;
ASSERT_TRUE(!tool.Run(3, usage));

cleanup(file_name);
cleanup(file_path);
for (int i = 0; i < 3; i++) {
delete[] usage[i];
}
Expand Down

0 comments on commit ab22cf3

Please sign in to comment.